<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Data Platforms | RajivOnAI</title><description>Every database on equal footing: PostgreSQL, MySQL, Oracle, MongoDB, Cassandra, vector databases, Elasticsearch, OpenSearch, Valkey, and cloud-managed and self-managed variants — architecture, operations, HA/DR, performance.</description><link>https://rajivonai.com/topics/data-platforms/</link><item><title>Closed-Loop Oracle Remediation: Designing the Human Authorization Mesh for Autonomous Database Agents</title><link>https://rajivonai.com/blog/2026-08-10-closed-loop-oracle-remediation-human-approval/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-08-10-closed-loop-oracle-remediation-human-approval/</guid><description>Autonomous LLM agents can investigate Oracle incidents, diagnose root causes, and generate precise remediation commands in seconds. Allowing them to execute those commands directly in production is operationally catastrophic. This post details the stateful approval mesh — cryptographic nonce challenge, Okta-verified Slack approval, STS token issuance, and structured action API — that enforces human authorization for every destructive Oracle operation.</description><pubDate>Tue, 10 Nov 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;An autonomous LLM agent can diagnose an Oracle lock chain and draft &lt;code&gt;ALTER SYSTEM KILL SESSION&lt;/code&gt; in seconds, but giving that agent direct execution authority turns a local performance spike into an unrecoverable business outage.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Autonomous remediation agents optimize for their immediate objective (clearing locks) without knowing whether the blocking session is a critical batch job whose rollback will outlast the incident.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Place a stateful approval mesh between the read-only agent and the execution engine—using a fixed action catalog, a Redis-backed cryptographic nonce, Okta MFA verification, and a 60-second RS256 STS token.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Chat-button approvals without MFA, replayable nonces, or string-interpolated &lt;code&gt;ALTER SYSTEM&lt;/code&gt; handlers recreate the same blast radius behind a UI veneer.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-08-10-closed-loop-oracle-remediation-human-approval.svg&quot; alt=&quot;Bind every destructive Oracle action to a verified human nonce&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Autonomous database remediation agents operate across a spectrum from purely advisory (investigate and post recommendations) to fully autonomous (investigate and mutate production state). Where an action belongs on that spectrum depends on two properties: reversibility and blast radius.&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Action Category&lt;/th&gt;&lt;th&gt;Example&lt;/th&gt;&lt;th&gt;Reversibility&lt;/th&gt;&lt;th&gt;Blast Radius&lt;/th&gt;&lt;th&gt;Recommended Approval Mode&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Read-only diagnostic&lt;/td&gt;&lt;td&gt;AWR report, &lt;code&gt;V$SESSION&lt;/code&gt; lock tree&lt;/td&gt;&lt;td&gt;N/A — no state change&lt;/td&gt;&lt;td&gt;None&lt;/td&gt;&lt;td&gt;Autonomous — no approval&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reversible maintenance&lt;/td&gt;&lt;td&gt;&lt;code&gt;DBMS_STATS.GATHER_TABLE_STATS&lt;/code&gt;&lt;/td&gt;&lt;td&gt;High — prior stats in &lt;code&gt;WRI$_OPTSTAT_TAB_HISTORY&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Single table&lt;/td&gt;&lt;td&gt;Autonomous or low-friction review&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Limited-blast destructive&lt;/td&gt;&lt;td&gt;&lt;code&gt;ALTER SYSTEM KILL SESSION&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Partial — rolls back uncommitted work&lt;/td&gt;&lt;td&gt;Single session + undo load&lt;/td&gt;&lt;td&gt;Single-engineer Okta-verified approval&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High-blast destructive&lt;/td&gt;&lt;td&gt;Kill long-running batch session&lt;/td&gt;&lt;td&gt;Costly — rollback can equal job runtime&lt;/td&gt;&lt;td&gt;Hours of lost compute + locks&lt;/td&gt;&lt;td&gt;Named PE approval + secondary escalation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Fleet-wide state change&lt;/td&gt;&lt;td&gt;Data Guard failover, &lt;code&gt;DROP TABLE PURGE&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Irreversible or role-changing&lt;/td&gt;&lt;td&gt;Entire service or region&lt;/td&gt;&lt;td&gt;Two-person approval + ITSM record&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The failure mode that motivates an authorization mesh is eager autonomy. When an agent is instructed to resolve row-lock contention (&lt;code&gt;enq: TX - row lock contention&lt;/code&gt;), killing the holding session is technically valid from a database lock-graph perspective. What the database views cannot tell the agent is whether that session is a month-end financial close job that has already run for hours—meaning &lt;code&gt;ALTER SYSTEM KILL SESSION&lt;/code&gt; will force Oracle to roll back every uncommitted change before the job can even be restarted.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Designing a human-in-the-loop approval mechanism for production Oracle operations requires satisfying four competing constraints at the same time:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-08-10-closed-loop-oracle-remediation-human-approval-2-constraints.svg&quot; alt=&quot;Approval design must satisfy speed, context, cryptography, and auditability together&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Incident latency budget (&amp;#x3C; 60 seconds):&lt;/strong&gt; During a Severity-1 lock cascade, connection pools back up within one to two minutes. Traditional ITSM change tickets take 5 to 20 minutes to create, route, and approve—far slower than the failure cascade. Breaking down the 60-second budget shows where time actually goes: diagnostic &lt;code&gt;V$SESSION&lt;/code&gt; and &lt;code&gt;V$TRANSACTION&lt;/code&gt; queries plus LLM synthesis take roughly 4–6 seconds; Redis persistence and Slack Block Kit delivery take under 1 second; human reading and decision take 15–20 seconds if the card is well-structured; Okta Verify push delivery and biometric approval take 8–12 seconds; and KMS RS256 signing plus Execution Engine dispatch take 1–2 seconds. That leaves a ~20-second margin before the 60-second ceiling—meaning the approval card must be immediately scannable without opening an AWR console.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Context sufficiency to prevent rubber-stamping:&lt;/strong&gt; A prompt reading “Approve killing session 402?” trains engineers to click Approve blindly. The approval card must surface the session’s &lt;code&gt;MODULE&lt;/code&gt;, &lt;code&gt;ACTION&lt;/code&gt;, elapsed runtime, active undo blocks (&lt;code&gt;V$TRANSACTION.USED_UBLK&lt;/code&gt;), the exact rollback cost, and the negative-space evidence (what alternatives the agent checked and why it rejected them).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cryptographic authorization binding:&lt;/strong&gt; Slack message reactions or &lt;code&gt;/approve&lt;/code&gt; slash commands are weak authorization signals—any member of a channel can click a button without secondary identity verification, and webhook payloads can be replayed if not bound to a single-use nonce. The execution engine must require an asymmetric, short-lived Security Token Service (STS) JWT signed by a Hardware Security Module (HSM) or KMS key and bound to both the approver’s Okta identity and the proposal’s cryptographic nonce.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Complete lifecycle auditability:&lt;/strong&gt; Regulated environments (PCI DSS, SOX, HIPAA) require an immutable record of every proposed, approved, rejected, expired, and executed state change—including both the proposing agent and the authenticating human principal.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;How do you build an approval path that completes in under 60 seconds, surfaces enough operational context to prevent blind clicks, cryptographically binds human identity to a single action, and logs the full lifecycle without forcing DBAs into a console at 2 AM?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Four authorization architectures cover the practical design space for automated database remediation.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Architecture&lt;/th&gt;&lt;th&gt;Incident Speed&lt;/th&gt;&lt;th&gt;Auth Strength&lt;/th&gt;&lt;th&gt;Raw SQL Risk&lt;/th&gt;&lt;th&gt;Primary Failure Mode&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Full Agent Autonomy&lt;/td&gt;&lt;td&gt;&amp;#x3C; 5 seconds&lt;/td&gt;&lt;td&gt;None (self-approved)&lt;/td&gt;&lt;td&gt;High if unconstrained&lt;/td&gt;&lt;td&gt;Terminates critical batch jobs without business context&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slack-Only Button&lt;/td&gt;&lt;td&gt;15–30 seconds&lt;/td&gt;&lt;td&gt;Weak (chat session only)&lt;/td&gt;&lt;td&gt;Medium&lt;/td&gt;&lt;td&gt;Spoofable channel clicks; no MFA or role enforcement&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Manual ITSM Change Ticket&lt;/td&gt;&lt;td&gt;5–20 minutes&lt;/td&gt;&lt;td&gt;Strong (SSO + ticket)&lt;/td&gt;&lt;td&gt;Low&lt;/td&gt;&lt;td&gt;Too slow for active Sev-1 lock cascades&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Stateful Approval Mesh&lt;/td&gt;&lt;td&gt;20–45 seconds&lt;/td&gt;&lt;td&gt;Strong (Okta MFA + KMS RS256)&lt;/td&gt;&lt;td&gt;None (fixed catalog)&lt;/td&gt;&lt;td&gt;Requires Redis state and KMS availability&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-08-10-closed-loop-oracle-remediation-human-approval-3-options.svg&quot; alt=&quot;Stateful approval mesh balances incident speed with cryptographic authorization&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Full agent autonomy&lt;/strong&gt; achieves sub-5-second remediation but was rejected because database telemetry (&lt;code&gt;V$SESSION&lt;/code&gt;, &lt;code&gt;V$LOCK&lt;/code&gt;) does not encode business deadlines or downstream batch SLAs. &lt;strong&gt;Slack-only buttons&lt;/strong&gt; meet the speed budget but fail the cryptographic binding constraint: a compromised or unattended laptop with an open Slack session can authorize destructive actions without MFA. &lt;strong&gt;Manual ITSM tickets&lt;/strong&gt; provide strong governance and auditability, so they remain appropriate for planned schema changes and Data Guard failovers, but their multi-minute latency makes them unusable for live lock-cascade triage. The &lt;strong&gt;stateful approval mesh&lt;/strong&gt; combines Slack’s speed with Okta MFA, a fixed action catalog, and KMS-signed STS tokens.&lt;/p&gt;
&lt;h2 id=&quot;the-stateful-approval-mesh-architecture&quot;&gt;The Stateful Approval Mesh Architecture&lt;/h2&gt;
&lt;p&gt;The approval mesh enforces strict privilege separation: the diagnostic agent holds read-only Oracle credentials and can only emit structured &lt;code&gt;ActionProposal&lt;/code&gt; payloads; only the Execution Engine holds privileged Oracle credentials, and it refuses to act without a valid, unconsumed STS JWT.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-08-10-closed-loop-oracle-remediation-human-approval-4-architecture.svg&quot; alt=&quot;Execution engine trusts only KMS-signed STS tokens bound to a Redis proposal nonce&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Read-Only Triage Agent] --&gt; B[ActionProposal JSON — fixed catalog]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[Approval Gateway — Redis 256-bit nonce]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[Slack Block Kit card — blast radius context]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[Engineer clicks Approve]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[Okta RBAC + Push MFA]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G{Approved within TTL?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|No — timeout or reject| H[Audit EXPIRED — escalate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|Yes| I[Mint 60s RS256 STS JWT]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[Execution Engine — atomic nonce CAS]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[Validated Oracle API + Audit EXECUTED]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;fixed-action-catalog-no-arbitrary-sql&quot;&gt;Fixed Action Catalog (No Arbitrary SQL)&lt;/h3&gt;
&lt;p&gt;The agent never generates raw SQL for execution. It selects an &lt;code&gt;action_id&lt;/code&gt; from an allowlisted catalog where each entry defines its risk tier, required Okta group, maximum TTL, reversibility, and blast radius.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dataclasses &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dataclass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; enum &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Enum&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; RiskLevel&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Enum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    LOW&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;LOW&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    HIGH&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;HIGH&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    CRITICAL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;CRITICAL&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;@dataclass&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;frozen&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ActionDefinition&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    action_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    display_name: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    risk_level: RiskLevel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    required_approver_role: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    max_ttl_seconds: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    reversibility: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    blast_radius_description: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;ACTION_CATALOG&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;kill_single_session&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: ActionDefinition(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        action_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;kill_single_session&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        display_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Kill Oracle Session&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        risk_level&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RiskLevel.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;HIGH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        required_approver_role&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;oracle-dba-oncall&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        max_ttl_seconds&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;90&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        reversibility&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;PARTIAL — Oracle marks session KILLED and rolls back uncommitted work&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        blast_radius_description&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Terminates target session; rollback duration scales with undo blocks used&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;gather_table_stats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: ActionDefinition(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        action_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;gather_table_stats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        display_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Gather Table Statistics&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        risk_level&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RiskLevel.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;LOW&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        required_approver_role&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;any-engineer&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        max_ttl_seconds&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;300&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        reversibility&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;FULL — prior stats restorable via DBMS_STATS.RESTORE_TABLE_STATS&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        blast_radius_description&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Refreshes optimizer statistics for one table; may trigger plan re-parses&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;make_index_visible&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: ActionDefinition(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        action_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;make_index_visible&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        display_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Restore Index Visibility&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        risk_level&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RiskLevel.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;LOW&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        required_approver_role&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;any-engineer&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        max_ttl_seconds&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;300&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        reversibility&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;FULL — index can be toggled back to INVISIBLE&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        blast_radius_description&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Exposes existing index to optimizer without rebuilding segments&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;nonce-binding-and-okta-verified-sts-issuance&quot;&gt;Nonce Binding and Okta-Verified STS Issuance&lt;/h3&gt;
&lt;p&gt;When the gateway receives a proposal, it generates a 256-bit cryptographic nonce (&lt;code&gt;secrets.token_urlsafe(32)&lt;/code&gt;), stores the proposal in Redis with &lt;code&gt;SETEX&lt;/code&gt;, writes a &lt;code&gt;PROPOSED&lt;/code&gt; audit record, and posts a Slack Block Kit card. Clicking Approve verifies the engineer’s Okta group membership and triggers an Okta Verify push challenge before minting a 60-second RS256 JWT signed via KMS or Vault.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; secrets&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime, timedelta, timezone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; jose &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; jwt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; submit_proposal&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(redis_client, slack_client, proposal: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;dict&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    action_def &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ACTION_CATALOG&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[proposal[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;action_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    nonce &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; secrets.token_urlsafe(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    expires_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime.now(timezone.utc) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; timedelta(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;seconds&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;action_def.max_ttl_seconds)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    record &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;proposal&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: proposal,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;nonce&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: nonce,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;status&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;PENDING&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;required_role&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: action_def.required_approver_role,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;expires_at&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: expires_at.isoformat(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    redis_client.setex(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;approval:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;proposal[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;proposal_id&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        action_def.max_ttl_seconds,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        json.dumps(record),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    audit_log(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;PROPOSED&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;proposal_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: proposal[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;proposal_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;nonce&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: nonce})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    resp &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; slack_client.chat_postMessage(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;build_slack_card(proposal, action_def, nonce))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; resp[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ts&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; issue_sts_after_okta&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(okta_client, approver_email: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, proposal_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, nonce: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, record: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;dict&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; None&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; okta_client.user_has_role(approver_email, record[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;required_role&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    push &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; okta_client.send_push_verification(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        user_email&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;approver_email,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        challenge_context&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Approve &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;record[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;proposal&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;][&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;action_id&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; on &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;record[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;proposal&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;][&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;db_unique_name&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; push.status &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;APPROVED&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    now &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime.now(timezone.utc)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    claims &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;sub&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: approver_email,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;proposal_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: proposal_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;nonce&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: nonce,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;action_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: record[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;proposal&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;][&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;action_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;db_unique_name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: record[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;proposal&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;][&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;db_unique_name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;iat&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(now.timestamp()),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;exp&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((now &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; timedelta(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;seconds&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;60&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)).timestamp()),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; kms_sign_rs256_jwt(claims)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;atomic-nonce-consumption-and-negative-space-enrichment&quot;&gt;Atomic Nonce Consumption and Negative-Space Enrichment&lt;/h3&gt;
&lt;p&gt;Two implementation details separate a production-grade approval mesh from a prototype. First, the Execution Engine must consume the Redis nonce atomically. If two on-call engineers click Approve within milliseconds of each other, a naive &lt;code&gt;GET&lt;/code&gt; followed by &lt;code&gt;SET&lt;/code&gt; allows both requests to pass the check and execute the Oracle command twice. Using a Redis Lua compare-and-swap (or &lt;code&gt;GETDEL&lt;/code&gt; on a single-use key) guarantees that exactly one worker claims &lt;code&gt;PENDING&lt;/code&gt; → &lt;code&gt;CONSUMED&lt;/code&gt; and all concurrent or replayed tokens fail immediately.&lt;/p&gt;
&lt;p&gt;Second, the Slack card must include &lt;strong&gt;negative-space evidence&lt;/strong&gt; gathered deterministically by the gateway—not hallucinated by the LLM. Before posting a &lt;code&gt;kill_single_session&lt;/code&gt; card, the gateway queries &lt;code&gt;V$SESSION&lt;/code&gt; (&lt;code&gt;logon_time&lt;/code&gt;, &lt;code&gt;module&lt;/code&gt;, &lt;code&gt;action&lt;/code&gt;, &lt;code&gt;sql_id&lt;/code&gt;) and &lt;code&gt;V$TRANSACTION&lt;/code&gt; (&lt;code&gt;used_ublk&lt;/code&gt;, &lt;code&gt;used_urec&lt;/code&gt;) directly so the engineer sees the exact undo footprint and session age. Before posting a &lt;code&gt;make_index_visible&lt;/code&gt; or index-drop proposal, it attaches the row from &lt;code&gt;DBA_INDEX_USAGE&lt;/code&gt; showing &lt;code&gt;TOTAL_ACCESS_COUNT&lt;/code&gt; and &lt;code&gt;LAST_USED&lt;/code&gt;, and validates schema/object identifiers against &lt;code&gt;DBA_OBJECTS&lt;/code&gt; so unverified object names never reach execution.&lt;/p&gt;
&lt;h3 id=&quot;safe-oracle-execution-why-alter-system-rejects-binds&quot;&gt;Safe Oracle Execution (Why &lt;code&gt;ALTER SYSTEM&lt;/code&gt; Rejects Binds)&lt;/h3&gt;
&lt;p&gt;A subtle Oracle trap sits in the execution handler: Oracle does &lt;strong&gt;not&lt;/strong&gt; support bind variables in DDL or &lt;code&gt;ALTER SYSTEM&lt;/code&gt; statements. Passing &lt;code&gt;ALTER SYSTEM KILL SESSION :sid, :serial&lt;/code&gt; to &lt;code&gt;cursor.execute()&lt;/code&gt; raises &lt;code&gt;ORA-00922: missing or invalid option&lt;/code&gt;. To prevent parameter injection without relying on unsupported binds, the execution handler must strictly validate &lt;code&gt;sid&lt;/code&gt; and &lt;code&gt;serial#&lt;/code&gt; as positive integers, verify the target session still exists in &lt;code&gt;V$SESSION&lt;/code&gt; (and is not a background process where &lt;code&gt;TYPE != &apos;USER&apos;&lt;/code&gt;), and then format the literal &lt;code&gt;&apos;sid,serial#&apos;&lt;/code&gt; string.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; kill_oracle_session&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(conn, parameters: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;dict&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;dict&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    sid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(parameters[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    serial &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(parameters[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;serial&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; or&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; serial &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        raise&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ValueError&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sid and serial# must be positive integers&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    with&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; conn.cursor() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cur:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        cur.execute(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &quot;SELECT username, module, status FROM v$session WHERE sid = :sid AND serial# = :serial AND type = &apos;USER&apos;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            sid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            serial&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;serial,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        row &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cur.fetchone()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; row:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;status&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SKIPPED&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;reason&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Session no longer active or not a USER session&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;        # ALTER SYSTEM does not accept bind variables; sid and serial are verified ints&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        cur.execute(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ALTER SYSTEM KILL SESSION &apos;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sid&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;serial&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos; IMMEDIATE&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;status&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;KILLED&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: sid, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;serial&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: serial, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;module&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: row[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Every remediation request follows a five-step state machine with explicit rollback and escalation gates.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-08-10-closed-loop-oracle-remediation-human-approval-5-flow.svg&quot; alt=&quot;Five-step approval lifecycle from proposal creation to single-use execution&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Step 1 — Submit &amp;#x26; Persist:&lt;/strong&gt; The agent emits an &lt;code&gt;ActionProposal&lt;/code&gt; from &lt;code&gt;ACTION_CATALOG&lt;/code&gt;. The gateway writes the &lt;code&gt;PENDING&lt;/code&gt; record with a 256-bit nonce to Redis (&lt;code&gt;SETEX&lt;/code&gt; with 60–300s TTL depending on risk tier) and logs &lt;code&gt;PROPOSED&lt;/code&gt; to the immutable audit store.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 2 — Context Card Delivery:&lt;/strong&gt; The gateway posts the Slack Block Kit message containing target database, &lt;code&gt;MODULE&lt;/code&gt;/&lt;code&gt;ACTION&lt;/code&gt;, undo rollback cost, &lt;code&gt;agent_justification&lt;/code&gt;, and &lt;code&gt;alternatives_considered&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 3 — Okta RBAC + MFA Challenge:&lt;/strong&gt; When an engineer clicks Approve, the gateway checks their Okta group (&lt;code&gt;oracle-dba-oncall&lt;/code&gt; or &lt;code&gt;oracle-platform-pe&lt;/code&gt;) and sends an Okta Verify push. For &lt;code&gt;CRITICAL&lt;/code&gt; actions such as Data Guard failover, the gateway requires two distinct Okta principals (&lt;code&gt;sub_1 != sub_2&lt;/code&gt;) plus an active ITSM incident ID before minting the STS token. &lt;strong&gt;Escalation gate:&lt;/strong&gt; If the push times out or is rejected, the gateway logs &lt;code&gt;EXPIRED&lt;/code&gt; or &lt;code&gt;REJECTED&lt;/code&gt; and pages the secondary on-call engineer with a shorter TTL (45s secondary, 30s on-call manager). In parallel, the agent posts its read-only diagnostic summary to the incident channel so the escalating engineer does not lose context waiting for another triage cycle.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 4 — Atomic Nonce Claim:&lt;/strong&gt; The Execution Engine validates the RS256 JWT signature against the KMS public key, checks &lt;code&gt;exp&lt;/code&gt;, and performs an atomic Redis compare-and-swap (&lt;code&gt;PENDING&lt;/code&gt; → &lt;code&gt;CONSUMED&lt;/code&gt;). Any duplicate or replayed token fails immediately.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 5 — Parameter-Validated Dispatch &amp;#x26; Audit:&lt;/strong&gt; The handler validates integer bounds and session type (&lt;code&gt;USER&lt;/code&gt;) in &lt;code&gt;V$SESSION&lt;/code&gt;, executes the allowlisted Oracle call, and writes the &lt;code&gt;EXECUTED&lt;/code&gt; audit record linking &lt;code&gt;proposed_by&lt;/code&gt; (agent) and &lt;code&gt;approved_by&lt;/code&gt; (Okta &lt;code&gt;sub&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Several concrete Oracle and identity behaviors dictate how this pattern must be implemented:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Oracle &lt;code&gt;ALTER SYSTEM KILL SESSION&lt;/code&gt; vs. &lt;code&gt;DISCONNECT SESSION&lt;/code&gt;:&lt;/strong&gt; Per Oracle Database Administrator’s Guide, &lt;code&gt;ALTER SYSTEM KILL SESSION&lt;/code&gt; does &lt;em&gt;not&lt;/em&gt; skip transaction rollback. It marks the session &lt;code&gt;KILLED&lt;/code&gt; (&lt;code&gt;V$SESSION.STATUS = &apos;KILLED&apos;&lt;/code&gt;) and instructs the session (or PMON/cleanup processes) to roll back all uncommitted changes using undo blocks. Adding &lt;code&gt;IMMEDIATE&lt;/code&gt; returns control to the caller immediately rather than waiting for the session to acknowledge, but the undo rollback still runs—and for a multi-hour batch DML job, rollback can take as long as the original transaction. Surfacing &lt;code&gt;V$TRANSACTION.USED_UBLK&lt;/code&gt; and &lt;code&gt;USED_UREC&lt;/code&gt; on the Slack approval card gives the engineer the true recovery cost before clicking Approve. When a session is waiting on a dead client TCP socket and &lt;code&gt;KILL SESSION&lt;/code&gt; leaves it stuck in &lt;code&gt;KILLED&lt;/code&gt; state, &lt;code&gt;ALTER SYSTEM DISCONNECT SESSION &apos;&amp;#x3C;sid&gt;,&amp;#x3C;serial#&gt;&apos; IMMEDIATE&lt;/code&gt; destroys the dedicated server process directly after rollback.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle statistics and index verification (&lt;code&gt;DBA_INDEX_USAGE&lt;/code&gt; and &lt;code&gt;DBMS_STATS&lt;/code&gt;):&lt;/strong&gt; When an agent proposes &lt;code&gt;gather_table_stats&lt;/code&gt;, Oracle retains prior table statistics in &lt;code&gt;WRI$_OPTSTAT_TAB_HISTORY&lt;/code&gt; (31 days by default via &lt;code&gt;DBMS_STATS.GET_STATS_HISTORY_RETENTION&lt;/code&gt;), making statistics refreshes genuinely reversible via &lt;code&gt;DBMS_STATS.RESTORE_TABLE_STATS&lt;/code&gt;. For index visibility or drop proposals, Oracle 12.2+ provides &lt;code&gt;DBA_INDEX_USAGE&lt;/code&gt; (which automatically tracks &lt;code&gt;TOTAL_ACCESS_COUNT&lt;/code&gt;, &lt;code&gt;TOTAL_EXEC_COUNT&lt;/code&gt;, and &lt;code&gt;LAST_USED&lt;/code&gt; without manual &lt;code&gt;ALTER INDEX ... MONITORING USAGE&lt;/code&gt;). Requiring the agent to attach &lt;code&gt;DBA_INDEX_USAGE&lt;/code&gt; output prevents approving index changes that break infrequent month-end jobs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;KMS/HSM signing boundary:&lt;/strong&gt; Storing the RS256 private key in application environment variables means a single CI/CD secret leak compromises the entire Oracle fleet. Signing STS tokens via AWS KMS or OCI Vault HSM ensures the private key never leaves the hardware boundary.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-08-10-closed-loop-oracle-remediation-human-approval-6-failures.svg&quot; alt=&quot;Approval fatigue, token replay, and DDL injection break naive approval gateways&quot;&gt;&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Condition&lt;/th&gt;&lt;th&gt;Breaks How&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Approval fatigue&lt;/td&gt;&lt;td&gt;High volume of &lt;code&gt;LOW&lt;/code&gt; alerts trains engineers to approve &lt;code&gt;HIGH&lt;/code&gt; kills blindly&lt;/td&gt;&lt;td&gt;Track per-engineer approval rate and review time (&amp;#x3C; 3s)&lt;/td&gt;&lt;td&gt;Rate-limit approvals per user; require negative-space evidence in every card&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;STS token replay&lt;/td&gt;&lt;td&gt;Intercepted or retried JWT re-executes a destructive action within 60s&lt;/td&gt;&lt;td&gt;Duplicate &lt;code&gt;proposal_id&lt;/code&gt; execution attempts in engine logs&lt;/td&gt;&lt;td&gt;Atomic Redis CAS (&lt;code&gt;PENDING&lt;/code&gt; → &lt;code&gt;CONSUMED&lt;/code&gt;) before dispatch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;ALTER SYSTEM&lt;/code&gt; injection&lt;/td&gt;&lt;td&gt;String-interpolated parameters inject extra SQL clauses&lt;/td&gt;&lt;td&gt;Static analysis + runtime regex check on &lt;code&gt;parameters&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Strict &lt;code&gt;int()&lt;/code&gt; casting + &lt;code&gt;V$SESSION&lt;/code&gt; existence and &lt;code&gt;TYPE = &apos;USER&apos;&lt;/code&gt; check&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;On-call engineer unreachable&lt;/td&gt;&lt;td&gt;2 AM Okta push times out after 30s; incident stalls&lt;/td&gt;&lt;td&gt;Monitor &lt;code&gt;EXPIRED&lt;/code&gt; proposal rate on Sev-1 alerts&lt;/td&gt;&lt;td&gt;Decreasing-TTL escalation chain to secondary on-call and manager&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Redis expiry erases audit&lt;/td&gt;&lt;td&gt;Proposal expires in Redis (&lt;code&gt;SETEX&lt;/code&gt;) leaving no record of agent intent&lt;/td&gt;&lt;td&gt;Compare &lt;code&gt;PROPOSED&lt;/code&gt; count against &lt;code&gt;EXECUTED + EXPIRED + REJECTED&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Write &lt;code&gt;PROPOSED&lt;/code&gt; on submit and &lt;code&gt;EXPIRED&lt;/code&gt; via keyspace/timer callback&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Signing key compromise&lt;/td&gt;&lt;td&gt;Leaked private key lets attacker forge valid STS JWTs&lt;/td&gt;&lt;td&gt;Unexpected &lt;code&gt;sub&lt;/code&gt; or missing Okta push correlation ID&lt;/td&gt;&lt;td&gt;Sign and verify via AWS KMS or OCI Vault HSM; never store raw keys&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Separate diagnosis from execution:&lt;/strong&gt; Keep the LLM agent on read-only Oracle credentials; only the Execution Engine holds privileged credentials and only acts on a signed STS token.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replace raw SQL with a fixed action catalog:&lt;/strong&gt; Constraining proposals to allowlisted &lt;code&gt;action_id&lt;/code&gt; entries prevents prompt injection from turning an approval card into arbitrary DDL.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Surface rollback cost, not just lock symptoms:&lt;/strong&gt; &lt;code&gt;ALTER SYSTEM KILL SESSION&lt;/code&gt; rolls back uncommitted undo; showing &lt;code&gt;V$TRANSACTION.USED_UBLK&lt;/code&gt; and runtime prevents engineers from killing multi-hour batch jobs blindly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bind approvals to single-use nonces and MFA:&lt;/strong&gt; Chat clicks alone are spoofable; pair Slack Block Kit with Okta RBAC + push MFA and an atomic Redis &lt;code&gt;PENDING&lt;/code&gt; → &lt;code&gt;CONSUMED&lt;/code&gt; state transition.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Validate DDL parameters in code:&lt;/strong&gt; Because &lt;code&gt;ALTER SYSTEM&lt;/code&gt; rejects bind variables (&lt;code&gt;ORA-00922&lt;/code&gt;), enforce strict integer parsing and &lt;code&gt;V$SESSION&lt;/code&gt; verification before formatting the command.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Autonomous Oracle remediation agents optimize for immediate database metrics without understanding business context, risking catastrophic rollbacks when killing long-running batch sessions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Deploy a stateful approval mesh between your diagnostic agent and execution engine using a fixed action catalog, Redis-backed single-use nonces, Okta MFA verification, and KMS-signed 60-second RS256 STS tokens.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Oracle’s &lt;code&gt;ALTER SYSTEM KILL SESSION&lt;/code&gt; marks a session &lt;code&gt;KILLED&lt;/code&gt; and rolls back its uncommitted transaction—meaning an ill-timed kill on a large batch job triggers an undo rollback that can outlast the original incident.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit every Slack bot, webhook, and runbook runner in your environment that can mutate Oracle state this week; verify whether it requires MFA-backed cryptographic authorization and whether any &lt;code&gt;ALTER SYSTEM&lt;/code&gt; handler performs strict integer and &lt;code&gt;V$SESSION&lt;/code&gt; validation.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Oracle Database Administrator’s Guide 19c — Managing Sessions (&lt;code&gt;ALTER SYSTEM KILL SESSION&lt;/code&gt; and rollback behavior): &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/admin/managing-sessions.html&quot;&gt;https://docs.oracle.com/en/database/oracle/oracle-database/19/admin/managing-sessions.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Oracle Database SQL Language Reference 19c — &lt;code&gt;ALTER SYSTEM&lt;/code&gt;: &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/ALTER-SYSTEM.html&quot;&gt;https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/ALTER-SYSTEM.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Oracle Database Reference 19c — &lt;code&gt;DBA_INDEX_USAGE&lt;/code&gt;: &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/DBA_INDEX_USAGE.html&quot;&gt;https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/DBA_INDEX_USAGE.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Okta Authentication &amp;#x26; Factors API Documentation: &lt;a href=&quot;https://developer.okta.com/docs/reference/api/authn/&quot;&gt;https://developer.okta.com/docs/reference/api/authn/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;RFC 8725 — JSON Web Token Best Current Practices: &lt;a href=&quot;https://datatracker.ietf.org/doc/html/rfc8725&quot;&gt;https://datatracker.ietf.org/doc/html/rfc8725&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Redis &lt;code&gt;SETEX&lt;/code&gt; Command Reference: &lt;a href=&quot;https://redis.io/commands/setex/&quot;&gt;https://redis.io/commands/setex/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>architecture</category><category>checklist</category></item><item><title>Cloud SQL, AlloyDB, or Spanner: Choose the Database Contract First</title><link>https://rajivonai.com/blog/2026-11-03-cloud-sql-alloydb-spanner-architecture-decisions/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-11-03-cloud-sql-alloydb-spanner-architecture-decisions/</guid><description>Compare PostgreSQL compatibility, scaling, recovery, and operational boundaries when choosing a Google Cloud database platform.</description><pubDate>Tue, 20 Oct 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A PostgreSQL-compatible connection string can make a migration look trivial while hiding a breaking change in transaction semantics, supported SQL, recovery procedures, or primary-key design.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Treating Cloud SQL, AlloyDB, and Spanner’s PostgreSQL interface as small, medium, and large tiers of the same database leads to blocked migrations, broken ORMs, or over-engineered distributed schemas.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Select the service by matching your workload’s hard constraints—PostgreSQL dialect fidelity, storage ceiling, read-pool scale, SLA tier, and PITR window—before writing migration code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Spanner’s PGAdapter translates wire traffic, not PostgreSQL internals; DDL transactions, &lt;code&gt;SERIAL&lt;/code&gt; keys, and most &lt;code&gt;pg_catalog&lt;/code&gt; views fail or behave differently.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-cloud-sql-alloydb-spanner-architecture-decisions.svg&quot; alt=&quot;Requirements determine the shortlist&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Platform engineering teams standardizing PostgreSQL workloads on Google Cloud face three distinct managed database offerings: Cloud SQL for PostgreSQL, AlloyDB for PostgreSQL, and Spanner with its PostgreSQL interface. On paper, all three accept PostgreSQL drivers and SQL syntax. In practice, they represent three different storage engines, availability models, and operational contracts.&lt;/p&gt;
&lt;p&gt;Engineers coming from AWS often reach for familiar analogies: Cloud SQL resembles Amazon RDS, and AlloyDB’s disaggregated compute-and-storage architecture resembles Amazon Aurora. Those comparisons help frame initial architecture questions about replication, read scaling, and storage elasticity, but they do not transfer exact failover timings, pricing models, or extension support. Spanner has no direct RDS equivalent; it is a globally distributed database built on Paxos consensus and TrueTime that exposes a PostgreSQL-dialect surface rather than running a PostgreSQL server process.&lt;/p&gt;
&lt;p&gt;When a platform catalog groups all three under a single “managed PostgreSQL” label, application teams assume they can start on Cloud SQL and later swap the connection string to AlloyDB or Spanner without changing schema design, ORM migrations, or operational runbooks.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Portability assumptions break down along four hard technical boundaries: compatibility, capacity ceilings, availability guarantees, and recovery windows.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 1: Storage, compute, and read-scaling ceilings.&lt;/strong&gt; Cloud SQL for PostgreSQL supports up to 64 TB of storage on dedicated-core instances (3 TB on shared-core), with a hard PostgreSQL table limit of 32 TB. Compute scales up to 96 vCPUs and 624 GiB RAM on Enterprise Edition, or 128 vCPUs and 864 GiB RAM on Enterprise Plus Edition. AlloyDB for PostgreSQL raises the cluster storage ceiling to 128 TiB, scales individual compute nodes up to 288 vCPUs and 2,232 GiB RAM, and supports up to 20 read-pool nodes per cluster across all read pools. Exceeding 64 TB total storage or needing independent, fast-scaling read-pool nodes beyond standard Cloud SQL replicas forces an architectural move to AlloyDB, while exceeding a single-cluster write primary forces horizontal sharding or Spanner.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 2: Availability SLAs and failover mechanics.&lt;/strong&gt; Cloud SQL Enterprise Edition with Regional High Availability (HA) carries a 99.95% availability SLA, rising to 99.99% on Enterprise Plus with HA (or a read pool of 2 or more nodes), with typical HA failover times of 60 to 120 seconds. AlloyDB with HA enabled carries a 99.99% availability SLA across standard regions (99.95% in Stockholm) and targets under-60-second failover using a warm standby VM over shared storage. Spanner offers a 99.99% SLA for regional instances in standard regions and a 99.999% SLA for dual-region and multi-region instances. Any workload with a contractual requirement for 99.999% availability or active multi-region writes cannot be satisfied by Cloud SQL or AlloyDB alone.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 3: Point-in-time recovery (PITR) retention windows.&lt;/strong&gt; Cloud SQL Enterprise retains PITR write-ahead logs for 1 to 7 days, whereas Enterprise Plus extends PITR retention up to 35 days. AlloyDB enables continuous backups by default with a 14-day PITR window, configurable from 1 to 35 days. Spanner controls PITR through &lt;code&gt;version_retention_period&lt;/code&gt;, which defaults to just 1 hour and caps at 7 days maximum. Moving a workload with a 30-day compliance PITR requirement onto Spanner breaks that retention contract unless supplemented by scheduled exports.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 4: Dialect and wire-protocol fidelity.&lt;/strong&gt; Spanner’s PostgreSQL interface relies on PGAdapter to translate PostgreSQL wire-protocol traffic into Spanner gRPC calls. PGAdapter does not support SSL/TLS between the client and the proxy itself (requiring local or sidecar deployment in a trusted network), does not support DDL statements inside explicit &lt;code&gt;BEGIN ... COMMIT&lt;/code&gt; transactions, does not support &lt;code&gt;SERIAL&lt;/code&gt; or &lt;code&gt;IDENTITY&lt;/code&gt; auto-incrementing keys, and omits many &lt;code&gt;pg_catalog&lt;/code&gt; tables expected by ORMs and administrative tools like pgAdmin.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-cloud-sql-alloydb-spanner-architecture-decisions-2-constraints.svg&quot; alt=&quot;Hard service ceilings separate Cloud SQL, AlloyDB, and Spanner&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Which database contract does the workload actually require, and what measurable constraint justifies taking on a different operational or SQL boundary?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams typically evaluate four deployment paths when standardizing PostgreSQL workloads on Google Cloud.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Option&lt;/th&gt;&lt;th&gt;PostgreSQL Fidelity&lt;/th&gt;&lt;th&gt;Max Storage &amp;#x26; Scale&lt;/th&gt;&lt;th&gt;Availability &amp;#x26; PITR&lt;/th&gt;&lt;th&gt;Primary Rejection or Selection Reason&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Self-managed on GCE&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;100% upstream PostgreSQL&lt;/td&gt;&lt;td&gt;Bound by PD limits and manual sharding&lt;/td&gt;&lt;td&gt;Self-managed HA; custom WAL retention&lt;/td&gt;&lt;td&gt;Rejected as baseline due to patching, failover, and backup toil&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Cloud SQL for PostgreSQL&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Full managed PostgreSQL + curated extensions&lt;/td&gt;&lt;td&gt;64 TB storage, 128 vCPU (Ent Plus), single primary&lt;/td&gt;&lt;td&gt;99.95% (Ent) / 99.99% (Ent Plus); 7d / 35d PITR&lt;/td&gt;&lt;td&gt;Default choice when single-primary PostgreSQL fits load&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;AlloyDB for PostgreSQL&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;High PostgreSQL compatibility + columnar engine&lt;/td&gt;&lt;td&gt;128 TiB storage, 288 vCPU/node, 20 read-pool nodes&lt;/td&gt;&lt;td&gt;99.99% HA (&amp;#x3C;60s failover); 1-35d PITR (14d default)&lt;/td&gt;&lt;td&gt;Chosen for &gt;64 TB, read-heavy pools, or faster HA failover&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Spanner (PG Interface)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;PostgreSQL dialect subset via PGAdapter&lt;/td&gt;&lt;td&gt;Horizontal multi-region writes and storage&lt;/td&gt;&lt;td&gt;99.99% regional / 99.999% multi-region; 7d max PITR&lt;/td&gt;&lt;td&gt;Chosen only when distributed writes or 99.999% SLA justify schema changes&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Self-managed PostgreSQL on Compute Engine (GCE) preserves 100% control over operating system packages, custom C extensions, and kernel tuning. However, it shifts synchronous replication, fencing, WAL archiving, minor-version patching, and restore drills onto the platform team. Unless a workload depends on an unapproved extension or custom kernel behavior, GCE self-management adds operational risk without improving application outcomes.&lt;/p&gt;
&lt;p&gt;Cloud SQL for PostgreSQL is the lowest-friction managed option. It preserves standard PostgreSQL operational mental models and supports both Enterprise and Enterprise Plus tiers. It is rejected only when storage exceeds 64 TB, a single table approaches 32 TB, read-replica lag or count becomes unmanageable, or failover latency under 60 seconds is mandatory.&lt;/p&gt;
&lt;p&gt;AlloyDB for PostgreSQL decouples compute VMs from a shared, WAL-backed storage layer. It is the right upgrade path when a workload outgrows Cloud SQL’s storage or read-scaling model while still requiring full PostgreSQL semantics, extensions, and up to 35-day PITR. It is rejected when the application requires active multi-region writes or a 99.999% availability SLA.&lt;/p&gt;
&lt;p&gt;Spanner with the PostgreSQL interface solves horizontal write scaling and multi-region consensus. It is rejected as a general-purpose PostgreSQL target because its dialect subset, key-distribution requirements, 7-day PITR cap, and PGAdapter operational model require application-level adaptation.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-cloud-sql-alloydb-spanner-architecture-decisions-3-options.svg&quot; alt=&quot;Each service trades PostgreSQL fidelity for distributed scale&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;start-with-behavior-then-choose-the-service&quot;&gt;Start with Behavior, Then Choose the Service&lt;/h2&gt;
&lt;p&gt;A database selection framework must map application behavior to the underlying storage and proxy topology before provisioning infrastructure.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-cloud-sql-alloydb-spanner-architecture-decisions-4-architecture.svg&quot; alt=&quot;Storage and proxy boundaries define each database contract&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;cloud-sql-instance-coupled-compute-and-blast-radius&quot;&gt;Cloud SQL: instance-coupled compute and blast radius&lt;/h3&gt;
&lt;p&gt;Cloud SQL manages individual PostgreSQL instances backed by regional persistent disks. In an HA configuration, a primary VM in Zone A replicates synchronously to a standby VM in Zone B. Because a Cloud SQL instance, a logical PostgreSQL database, and a Google Cloud project are distinct objects, placing multiple logical databases on one instance couples their CPU, connection limits, maintenance windows, and failover blast radius.&lt;/p&gt;
&lt;p&gt;Fleet inventory and chargeback must track instances separately from logical databases. Consolidating low-traffic microservices onto a shared instance saves baseline VM cost, but any schema lock storm, connection-pool exhaustion, or major-version upgrade affects every tenant on that instance.&lt;/p&gt;
&lt;h3 id=&quot;alloydb-disaggregated-storage-and-independent-read-pools&quot;&gt;AlloyDB: disaggregated storage and independent read pools&lt;/h3&gt;
&lt;p&gt;AlloyDB organizes resources into clusters and instances. The primary HA instance consumes vCPUs across two VMs (primary and hot standby) attached to a shared distributed storage layer that scales automatically up to 128 TiB. Read-pool instances run as separate VMs (1 to 20 nodes total per cluster) reading from the same storage layer without duplicating full storage volumes per replica.&lt;/p&gt;
&lt;p&gt;While read pools add horizontal read capacity, they do not eliminate replication lag or connection-routing responsibility. Applications must explicitly separate read-only queries onto the read-pool endpoint and define acceptable read staleness. Furthermore, if writes push cluster storage past its allocated quota, AlloyDB rejects &lt;code&gt;INSERT&lt;/code&gt; and &lt;code&gt;UPDATE&lt;/code&gt; transactions with a storage-exceeded error, making storage-quota alerting mandatory.&lt;/p&gt;
&lt;h3 id=&quot;spanner-pgadapter-proxy-and-paxos-replicated-splits&quot;&gt;Spanner: PGAdapter proxy and Paxos-replicated splits&lt;/h3&gt;
&lt;p&gt;Spanner’s PostgreSQL interface places PGAdapter—either as an application sidecar, a local process, or an in-process library—between standard PostgreSQL drivers and Spanner’s native gRPC API. PGAdapter translates the wire protocol, while Spanner executes queries against Paxos-replicated data splits coordinated by TrueTime.&lt;/p&gt;
&lt;p&gt;Because PGAdapter does not terminate TLS from the client, production deployments must bind it to &lt;code&gt;localhost&lt;/code&gt; or a trusted pod network namespace. More importantly, every Spanner table requires an explicit primary key designed to avoid monotonic hotspots; migrating a PostgreSQL schema that relies on &lt;code&gt;SERIAL&lt;/code&gt; or &lt;code&gt;BIGSERIAL&lt;/code&gt; sequences will serialize writes onto a single split unless refactored to UUIDv4 or bit-reversed sequences.&lt;/p&gt;
&lt;p&gt;To enforce Spanner’s maximum 7-day PITR window explicitly rather than relying on the 1-hour default, create the database, then set &lt;code&gt;version_retention_period&lt;/code&gt; with a DDL update (the retention period cannot be passed to the create call; the allowed range is 1 hour to 7 days):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;gcloud&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; spanner&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; databases&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; create&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; ledger_db&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --instance=prod-spanner-us&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --database-dialect=POSTGRESQL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;gcloud&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; spanner&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; databases&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; ddl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; update&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; ledger_db&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --instance=prod-spanner-us&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --ddl=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;ALTER DATABASE &quot;ledger_db&quot; SET &quot;spanner.version_retention_period&quot; = &apos;&quot;&apos;&quot;&apos;7d&apos;&quot;&apos;&quot;&apos;;&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For Cloud SQL and AlloyDB, verify extension availability and PITR retention directly in Terraform or &lt;code&gt;gcloud&lt;/code&gt; during provisioning so compliance boundaries are codified rather than assumed.&lt;/p&gt;
&lt;h3 id=&quot;compare-recovery-and-support-boundaries&quot;&gt;Compare recovery and support boundaries&lt;/h3&gt;
&lt;p&gt;For every shortlisted service, document five operational boundaries before approving production use: failure domain (zonal vs. regional), cross-region disaster recovery mechanism, backup and PITR retention window, application reconnect behavior during failover, and the owner of quarterly restore drills. An advertised SLA covers infrastructure uptime, not application-level data corruption or unindexed query timeouts after a failover.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Selecting and adopting a database contract should follow four sequential verification gates, each with a clear pass criterion and rollback path.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-cloud-sql-alloydb-spanner-architecture-decisions-5-flow.svg&quot; alt=&quot;Four verification gates prevent costly database re-platforming&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;gate-1-dialect-extension-and-orm-audit&quot;&gt;Gate 1: Dialect, extension, and ORM audit&lt;/h3&gt;
&lt;p&gt;Inventory every PostgreSQL extension, procedural function, custom data type, &lt;code&gt;pg_catalog&lt;/code&gt; query, and ORM migration pattern in the codebase. Execute the application’s full schema migration suite—including &lt;code&gt;BEGIN&lt;/code&gt;/&lt;code&gt;COMMIT&lt;/code&gt; DDL blocks—against the target service. If targeting Spanner, verify that PGAdapter handles all driver authentication modes (&lt;code&gt;password&lt;/code&gt; only) and &lt;code&gt;COPY&lt;/code&gt; semantics (&lt;code&gt;STDIN&lt;/code&gt;/&lt;code&gt;STDOUT&lt;/code&gt; only). A &lt;code&gt;psql&lt;/code&gt; connection check does not pass this gate; the full migration and integration test suite must execute cleanly.&lt;/p&gt;
&lt;h3 id=&quot;gate-2-sla-pitr-and-quota-alignment&quot;&gt;Gate 2: SLA, PITR, and quota alignment&lt;/h3&gt;
&lt;p&gt;Validate that the chosen service tier meets contractual availability and data-retention requirements. Confirm whether 99.95% (Cloud SQL Enterprise) or 99.99% (Cloud SQL Enterprise Plus, AlloyDB HA, Spanner regional) suffices, or if 99.999% (Spanner multi-region) is required. Lock in PITR retention (&lt;code&gt;1-35d&lt;/code&gt; on Cloud SQL Enterprise Plus / AlloyDB, &lt;code&gt;7d&lt;/code&gt; max on Spanner) and verify project-region vCPU and storage quotas so provisioning does not stall mid-rollout.&lt;/p&gt;
&lt;h3 id=&quot;gate-3-peak-load-and-forced-failover-benchmark&quot;&gt;Gate 3: Peak-load and forced-failover benchmark&lt;/h3&gt;
&lt;p&gt;Replay a representative workload—matching production read/write ratios, connection concurrency, and working-set size—against the target service. During peak write load, trigger a forced HA failover (&lt;code&gt;gcloud sql instances failover&lt;/code&gt; or AlloyDB failover) and measure connection-pool recovery time, error rates, and p99 tail latency. On Spanner, inspect key-distribution metrics to confirm no single split absorbs disproportionate write traffic.&lt;/p&gt;
&lt;h3 id=&quot;gate-4-pitr-restore-drill-and-cutover&quot;&gt;Gate 4: PITR restore drill and cutover&lt;/h3&gt;
&lt;p&gt;Before routing production writes, execute a full point-in-time restore into an isolated clone instance and run application-level data-integrity checks. Document exact restore duration to establish an empirical Recovery Time Objective (RTO). Until production writes cut over, rollback is a trivial connection-string or DNS revert. Once production writes begin on the new engine, rollback requires either dual-writes or Change Data Capture (CDC) replication back to the legacy database.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Google Cloud’s documented behavior across these three services reinforces why compatibility testing must go beyond wire-protocol connectivity.&lt;/p&gt;
&lt;p&gt;In Cloud SQL for PostgreSQL, Google documents that individual PostgreSQL tables remain bounded by the upstream 32 TB table limit even when instance storage scales to 64 TB, and that Enterprise Edition caps PITR log retention at 7 days compared to 35 days on Enterprise Plus. Platform teams that select Enterprise Edition for cost savings and later discover a 30-day audit PITR requirement must upgrade editions rather than flipping a single flag.&lt;/p&gt;
&lt;p&gt;In AlloyDB for PostgreSQL, Google documents that cluster storage scales up to 128 TiB and read pools cap at 20 total nodes across a cluster, with primary HA instances consuming vCPUs across two VMs. Because read-pool nodes share the underlying storage layer, scaling read capacity is fast, but teams must still engineer read/write splitting and monitor cluster storage quotas to prevent write rejections.&lt;/p&gt;
&lt;p&gt;In Spanner’s PostgreSQL interface, Google’s known-issues documentation explicitly calls out behavioral differences in SQL functions (such as edge cases in string/array functions and numeric underflow) alongside PGAdapter’s documented restrictions: no client-to-PGAdapter SSL, no transactional DDL blocks, and partial &lt;code&gt;pg_catalog&lt;/code&gt; coverage. Treating Spanner as a drop-in PostgreSQL replacement fails at ORM migration time, whereas treating it as a distributed database with a familiar query syntax succeeds when schemas are designed around distributed primary keys.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Condition&lt;/th&gt;&lt;th&gt;Breaks how&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;ORM runs DDL inside transactions on Spanner&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;PGAdapter rejects &lt;code&gt;BEGIN ... CREATE TABLE ... COMMIT&lt;/code&gt; blocks&lt;/td&gt;&lt;td&gt;CI schema-migration test fails immediately&lt;/td&gt;&lt;td&gt;Split DDL into implicit transactions or native Spanner DDL batches&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Sequential primary keys on Spanner&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;SERIAL&lt;/code&gt; or timestamp keys pin writes to one Paxos split&lt;/td&gt;&lt;td&gt;High CPU on single split; elevated write latency&lt;/td&gt;&lt;td&gt;Refactor primary keys to UUIDv4 or bit-reversed sequences&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Client expects TLS directly to PGAdapter&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;PGAdapter does not support SSL on client-to-proxy leg&lt;/td&gt;&lt;td&gt;TLS handshake errors on startup&lt;/td&gt;&lt;td&gt;Run PGAdapter on &lt;code&gt;localhost&lt;/code&gt; or as a pod sidecar&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;30-day PITR assumed on Spanner or Cloud SQL Ent&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Spanner caps at 7d (&lt;code&gt;version_retention_period&lt;/code&gt;); Cloud SQL Ent caps at 7d&lt;/td&gt;&lt;td&gt;Compliance audit fails on retention check&lt;/td&gt;&lt;td&gt;Use Cloud SQL Ent Plus / AlloyDB (35d) or scheduled exports&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Regional HA mistaken for cross-region DR&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Regional outage takes down both primary and synchronous standby&lt;/td&gt;&lt;td&gt;Region-level fault simulation fails RTO&lt;/td&gt;&lt;td&gt;Provision cross-region read replica and drill promotion&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Multiple critical apps share one Cloud SQL instance&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Noisy neighbor saturates CPU/connections; maintenance hits all&lt;/td&gt;&lt;td&gt;Shared instance CPU throttling or lock waits&lt;/td&gt;&lt;td&gt;Isolate tier-1 workloads onto dedicated instances&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;AlloyDB cluster hits allocated storage quota&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;INSERT&lt;/code&gt;/&lt;code&gt;UPDATE&lt;/code&gt; statements fail with storage-exceeded errors&lt;/td&gt;&lt;td&gt;Storage utilization alert &gt;85%&lt;/td&gt;&lt;td&gt;Enable auto-resize monitoring and enforce quota headroom&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Stale reads routed from AlloyDB read pool&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Application reads uncommitted or lagging state after write&lt;/td&gt;&lt;td&gt;Read-after-write consistency bugs in UI&lt;/td&gt;&lt;td&gt;Route read-after-write paths to primary or enforce staleness bounds&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-cloud-sql-alloydb-spanner-architecture-decisions-6-failures.svg&quot; alt=&quot;Contract mismatches surface in production under load or failover&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Never classify Cloud SQL, AlloyDB, and Spanner as small, medium, and large tiers of PostgreSQL; each exposes a distinct storage engine, failure boundary, and SQL contract.&lt;/li&gt;
&lt;li&gt;Use Cloud SQL Enterprise Plus (up to 64 TB, 128 vCPUs, 99.99% SLA, 35-day PITR) as the default managed PostgreSQL baseline unless storage, read-pool count, or failover speed forces a change.&lt;/li&gt;
&lt;li&gt;Move to AlloyDB when workloads require up to 128 TiB storage, up to 20 independent read-pool nodes, or sub-60-second HA failover without rewriting PostgreSQL schemas.&lt;/li&gt;
&lt;li&gt;Adopt Spanner’s PostgreSQL interface only when horizontal multi-region writes or a 99.999% SLA justify refactoring primary keys, removing transactional DDL, and running PGAdapter sidecars.&lt;/li&gt;
&lt;li&gt;Validate every candidate through four gates—full ORM/dialect execution, SLA/PITR alignment, forced-failover load testing, and a timed PITR restore drill—before committing production writes.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Map every workload’s hard constraints across PostgreSQL extensions, table/cluster size, SLA target, and PITR retention window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Assign each workload to Cloud SQL, AlloyDB, or Spanner based on its binding constraint rather than familiarity or perceived tiering.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Pass all four verification gates—especially ORM migration execution, forced HA failover under load, and a timed PITR restore drill—in a staging environment.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Codify the approved service boundaries, Terraform defaults (&lt;code&gt;version_retention_period&lt;/code&gt;, HA flags, and storage alerts), and re-evaluation triggers in your platform catalog this week.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/quotas&quot;&gt;Cloud SQL for PostgreSQL quotas and limits&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/sla&quot;&gt;Cloud SQL SLA&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/alloydb/docs/quotas&quot;&gt;AlloyDB for PostgreSQL overview and quotas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/sla&quot;&gt;AlloyDB SLA&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/spanner/docs/postgresql-interface&quot;&gt;Spanner PostgreSQL interface and PGAdapter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/spanner/docs/reference/postgresql/known-issues-postgresql-interface&quot;&gt;Spanner PostgreSQL known issues&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/spanner/sla&quot;&gt;Spanner SLA&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>checklist</category></item><item><title>A Database Platform Is More Than a Terraform Module</title><link>https://rajivonai.com/blog/2026-11-03-database-platform-terraform-ownership/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-11-03-database-platform-terraform-ownership/</guid><description>Define ownership, safe infrastructure changes, schema migration boundaries, and recovery evidence for a multi-project database platform.</description><pubDate>Wed, 14 Oct 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A database created in ten minutes can remain an operational liability for years if nobody owns its upgrades, recovery tests, access reviews, or retirement.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Shipping a reusable Terraform module without an operational contract leaves ownership, destructive replacements, state blast radius, and application schema migrations undefined at fleet scale.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Pair versioned Terraform modules with isolated per-workload GCS state, two-layer deletion protection, Shared VPC host/service project boundaries, and separate application-owned DDL pipelines.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Terraform’s &lt;code&gt;deletion_protection&lt;/code&gt; only guards the provider unless &lt;code&gt;settings.deletion_protection_enabled&lt;/code&gt; and &lt;code&gt;lifecycle { prevent_destroy = true }&lt;/code&gt; are also enforced, and blind drift auto-apply will overwrite emergency on-call fixes.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-database-platform-terraform-ownership.svg&quot; alt=&quot;Ownership continues after provisioning&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A database platform team often begins with a practical goal: application teams should be able to request a managed PostgreSQL deployment without filing tickets for every instance, backup policy, private IP allocation, and monitoring alert. Terraform modules and a CI/CD deployment pipeline make repeatable provisioning across Google Cloud projects straightforward.&lt;/p&gt;
&lt;p&gt;The platform’s product, however, is not the Terraform module alone. It is the contract combining supported configurations, explicit ownership boundaries, service objectives, safe upgrade paths, operational evidence, and an exception process. Without that contract, self-service provisioning accelerates fleet growth faster than the organization can govern it.&lt;/p&gt;
&lt;p&gt;In a multi-project Google Cloud estate using Shared VPC, this gap becomes visible immediately. Network resources live in a central host project, database instances run in management or service projects, and application workloads connect from GKE or Compute Engine in separate service projects. A module that creates a &lt;code&gt;google_sql_database_instance&lt;/code&gt; in isolation does not answer who manages Private Services Access IP ranges, who rotates credentials, or who runs the restore drill when an application team accidentally truncates a table.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A Terraform module can pass &lt;code&gt;terraform apply&lt;/code&gt; cleanly and still leave four operational failure modes unresolved across the database lifecycle.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 1: Blast radius and state lock contention.&lt;/strong&gt; When a platform team stores multiple projects or production databases inside a single Terraform state file, every &lt;code&gt;plan&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; acquires a single &lt;code&gt;.tflock&lt;/code&gt; lock object in Google Cloud Storage (GCS) via atomic &lt;code&gt;x-goog-if-generation-match: 0&lt;/code&gt; preconditions. Beyond serialization bottlenecks, an unpinned module update or accidental state corruption affects every database in that state file simultaneously. Bounding blast radius requires isolating state per workload and environment (for example, one state prefix per service-environment pair).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 2: Destructive &lt;code&gt;force new&lt;/code&gt; replacements.&lt;/strong&gt; Changing immutable attributes on &lt;code&gt;google_sql_database_instance&lt;/code&gt;—such as certain network settings, major version properties, or instance identifiers—causes Terraform to plan a destroy-and-recreate (&lt;code&gt;-/+&lt;/code&gt;) operation. On newer Google provider versions, &lt;code&gt;deletion_protection&lt;/code&gt; defaults to &lt;code&gt;true&lt;/code&gt; at the provider level, which blocks &lt;code&gt;terraform destroy&lt;/code&gt; or forced replacement, but relying on a single flag is fragile. If an engineer sets &lt;code&gt;deletion_protection = false&lt;/code&gt; in a pull request to unblock a refactor, Terraform will delete the production database unless both GCP-level &lt;code&gt;settings.deletion_protection_enabled&lt;/code&gt; and Terraform’s &lt;code&gt;lifecycle { prevent_destroy = true }&lt;/code&gt; are enforced.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 3: Plaintext secrets and unversioned state in GCS.&lt;/strong&gt; Terraform state files store resource attributes in plaintext, including bootstrap passwords, connection strings, and private IP topologies, even when outputs are marked &lt;code&gt;sensitive = true&lt;/code&gt;. Anyone with &lt;code&gt;Storage Object Viewer&lt;/code&gt; or &lt;code&gt;Storage Object Admin&lt;/code&gt; on the state bucket can run &lt;code&gt;terraform state pull&lt;/code&gt; and read those values. State buckets therefore require strict IAM isolation, object versioning (&lt;code&gt;gsutil versioning set on&lt;/code&gt;), and Customer-Managed Encryption Keys (CMEK) via Cloud KMS where compliance demands key control.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 4: Cross-project Shared VPC IAM boundaries.&lt;/strong&gt; In Google Cloud’s Shared VPC model, Private Services Access (VPC Network Peering to Google’s service networking producer network) and &lt;code&gt;servicenetworking.googleapis.com&lt;/code&gt; peering allocations (such as a &lt;code&gt;/24&lt;/code&gt; or larger CIDR block) must be configured in the &lt;strong&gt;Host Project&lt;/strong&gt;, along with central firewall rules. Meanwhile, client workloads in attached &lt;strong&gt;Service Projects&lt;/strong&gt; cannot attach network interfaces or route traffic across the shared subnets unless their service accounts or principals hold &lt;code&gt;roles/compute.networkUser&lt;/code&gt; on the host project or target subnets.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-database-platform-terraform-ownership-2-constraints.svg&quot; alt=&quot;Unbounded Terraform modules multiply blast radius across projects&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do you turn repeatable Terraform provisioning into a governed database service that remains safe through upgrades, drift, and recovery?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams generally choose among four operating models when scaling managed databases across multiple projects.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Option&lt;/th&gt;&lt;th&gt;Provisioning Speed&lt;/th&gt;&lt;th&gt;Blast Radius&lt;/th&gt;&lt;th&gt;Governance &amp;#x26; Safety&lt;/th&gt;&lt;th&gt;Primary Rejection or Selection Reason&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Central DBA ticket queue&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Slow (days to weeks)&lt;/td&gt;&lt;td&gt;Low (manual review)&lt;/td&gt;&lt;td&gt;High consistency, poor audit trail&lt;/td&gt;&lt;td&gt;Rejected because manual tickets bottleneck delivery and invite shadow IT&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Copy-pasted HCL templates&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Fast initial setup&lt;/td&gt;&lt;td&gt;Isolated per repo&lt;/td&gt;&lt;td&gt;Severe drift; inconsistent backups/HA&lt;/td&gt;&lt;td&gt;Rejected because security patches and flag updates never reach existing repos&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Monolithic platform state&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Moderate&lt;/td&gt;&lt;td&gt;High (fleet-wide lock/failure)&lt;/td&gt;&lt;td&gt;Centralized policy, fragile applies&lt;/td&gt;&lt;td&gt;Rejected due to &lt;code&gt;.tflock&lt;/code&gt; contention and multi-tenant blast radius&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Versioned product contract&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Fast via CI/CD&lt;/td&gt;&lt;td&gt;Bounded per workload/env&lt;/td&gt;&lt;td&gt;High (pinned modules + policy + isolated state)&lt;/td&gt;&lt;td&gt;Recommended: balances self-service speed with lifecycle safety&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;A central ticket queue gives DBAs tight control over instance sizing and backup settings, but it turns the database team into a deployment bottleneck. Application teams respond by requesting broad project-level IAM roles to provision databases manually in the Cloud Console, bypassing platform standards altogether.&lt;/p&gt;
&lt;p&gt;Distributing copy-pasted Terraform templates into application repositories solves the ticket bottleneck, but it destroys fleet governance. Within months, teams diverge on &lt;code&gt;deletion_protection&lt;/code&gt;, PITR retention, and monitoring alerts, and rolling out a mandatory provider or TLS policy upgrade requires opening dozens of manual pull requests across repositories you do not own.&lt;/p&gt;
&lt;p&gt;A monolithic Terraform state—where the platform team manages 50 or 100 databases from one root module—centralizes governance at the cost of availability. A single broken resource data source or stuck &lt;code&gt;.tflock&lt;/code&gt; blocks unrelated teams from deploying, and a careless module refactor risks planning changes across every production database at once.&lt;/p&gt;
&lt;p&gt;The recommended approach is a &lt;strong&gt;versioned product contract&lt;/strong&gt;: the platform team publishes semantically versioned Terraform modules with strict input schemas, application teams consume pinned versions in isolated per-workload GCS state prefixes, and policy checks in CI verify every plan before apply.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-database-platform-terraform-ownership-3-options.svg&quot; alt=&quot;Productized modules beat ticket queues and unguarded self-service&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;define-the-product-contract-before-the-module&quot;&gt;Define the Product Contract Before the Module&lt;/h2&gt;
&lt;p&gt;A database platform contract explicitly separates three operational planes: the network plane, the database infrastructure plane, and the application schema plane.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-database-platform-terraform-ownership-4-architecture.svg&quot; alt=&quot;Separate network, database infrastructure, and application schema planes&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;product-inputs-and-fleet-inventory&quot;&gt;Product inputs and fleet inventory&lt;/h3&gt;
&lt;p&gt;A platform intake contract should ask application teams for workload attributes they actually know: service owner, escalation channel, environment (&lt;code&gt;dev&lt;/code&gt;, &lt;code&gt;staging&lt;/code&gt;, &lt;code&gt;prod&lt;/code&gt;), region, data classification, RPO/RTO tier, expected storage and connection count, and required PostgreSQL version or extensions. It should not force application engineers to pick raw Shared VPC peering ranges or backup window cron expressions.&lt;/p&gt;
&lt;p&gt;Inventory tracking must distinguish physical and logical units accurately. A PostgreSQL logical database, a Cloud SQL instance, an AlloyDB cluster, an AlloyDB read-pool instance, and a Google Cloud project are separate billing, quota, and failure objects. Counting “databases” without separating instances from logical schemas corrupts capacity planning and cost attribution.&lt;/p&gt;
&lt;p&gt;The ownership matrix below defines explicit primary owners and required collaboration points:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Responsibility&lt;/th&gt;&lt;th&gt;Primary Owner&lt;/th&gt;&lt;th&gt;Required Collaboration&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Shared VPC, PSA peering, Private DNS&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Network platform&lt;/td&gt;&lt;td&gt;Database and application teams&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Database module, HA, backups, flags&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Database platform&lt;/td&gt;&lt;td&gt;Application owner for tier selection&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Application DDL and schema migrations&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Application team&lt;/td&gt;&lt;td&gt;Database platform for lock guardrails&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Database roles and object privileges&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Jointly defined&lt;/td&gt;&lt;td&gt;Identity and application owners&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Backup policy and PITR restore drills&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Named service owner&lt;/td&gt;&lt;td&gt;Platform provides runbook and tooling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Incident response and communication&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Named service owner&lt;/td&gt;&lt;td&gt;Platform on-call for infra escalation&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;codify-two-layer-deletion-protection-and-gcs-state&quot;&gt;Codify two-layer deletion protection and GCS state&lt;/h3&gt;
&lt;p&gt;Every production module must enforce both provider-level and GCP-level deletion locks alongside an isolated GCS backend. In Terraform, &lt;code&gt;deletion_protection&lt;/code&gt; blocks the Google provider from issuing a delete call, while &lt;code&gt;settings.deletion_protection_enabled&lt;/code&gt; blocks deletion at the GCP API/Console layer, and &lt;code&gt;lifecycle { prevent_destroy = true }&lt;/code&gt; aborts any plan that attempts replacement:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;terraform&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  backend&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;gcs&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    bucket&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;org-db-tfstate-prod&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    prefix&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;workloads/orders-api/prod&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;google_sql_database_instance&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;primary&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;orders-api-prod-pg16&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  database_version&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;POSTGRES_16&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  region&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;us-central1&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  deletion_protection&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  settings&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tier&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                        =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;db-custom-4-16384&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    deletion_protection_enabled&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    availability_type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;           =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;REGIONAL&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  lifecycle&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    prevent_destroy&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Protect the &lt;code&gt;org-db-tfstate-prod&lt;/code&gt; bucket with object versioning, CMEK encryption, and restricted IAM so only the Workload Identity Federation (WIF) deployment principal—never long-lived static service account keys—can mutate production state. If a CI runner crashes mid-apply and leaves a stale &lt;code&gt;.tflock&lt;/code&gt; object in GCS, require an explicit incident ticket before running &lt;code&gt;terraform force-unlock &amp;#x3C;LOCK_ID&gt;&lt;/code&gt; so two concurrent applies never race on state mutations.&lt;/p&gt;
&lt;h3 id=&quot;separate-infrastructure-from-application-schema&quot;&gt;Separate infrastructure from application schema&lt;/h3&gt;
&lt;p&gt;Terraform is designed for declarative infrastructure state, not transactional application schema evolution. Putting &lt;code&gt;CREATE TABLE&lt;/code&gt; or &lt;code&gt;ALTER TABLE&lt;/code&gt; statements inside Terraform resources couples database DDL locks to infrastructure applies and breaks zero-downtime application rollouts. Furthermore, PostgreSQL &lt;code&gt;ALTER TABLE&lt;/code&gt; operations that acquire &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; locks will queue all concurrent reads and writes behind any long-running analytical query unless the migration runner sets &lt;code&gt;lock_timeout&lt;/code&gt; and &lt;code&gt;statement_timeout&lt;/code&gt; explicitly.&lt;/p&gt;
&lt;p&gt;Application teams should own schema migrations using dedicated migration tooling (such as Flyway, Liquibase, or Alembic) integrated into application CI/CD. For breaking changes like column renames or type changes, teams must use an expand-and-contract workflow: add the new column, deploy application code that dual-writes and reads safely, backfill historical rows in batches, verify read adoption, and drop the legacy column in a later release. The platform’s role is to provide isolated migration credentials, a staging clone for lock testing, and connection-timeout guardrails—not to embed application DDL inside &lt;code&gt;terraform apply&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Treating module versions as a public API requires a four-stage rollout workflow with explicit validation gates and realistic rollback boundaries.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-database-platform-terraform-ownership-5-flow.svg&quot; alt=&quot;Staged module rollouts catch destructive plans before production&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;stage-1-plan-inspection-and-policy-gate&quot;&gt;Stage 1: Plan inspection and policy gate&lt;/h3&gt;
&lt;p&gt;Consumers pin exact module versions (for example, &lt;code&gt;version = &quot;~&gt; 2.4.0&quot;&lt;/code&gt;) in their workload repositories. When a module upgrade PR opens, CI runs &lt;code&gt;terraform plan&lt;/code&gt; and evaluates the JSON plan against policy rules (such as OPA/Conftest). Any plan containing a &lt;code&gt;delete&lt;/code&gt; or &lt;code&gt;replace&lt;/code&gt; (&lt;code&gt;-/+&lt;/code&gt;) action on a database instance fails the gate automatically unless accompanied by an explicit break-glass approval and documented migration plan.&lt;/p&gt;
&lt;h3 id=&quot;stage-2-nonproduction-apply-and-upgrade-test&quot;&gt;Stage 2: Nonproduction apply and upgrade test&lt;/h3&gt;
&lt;p&gt;Before tagging a module release as production-ready, apply the new version to a representative nonproduction state that mirrors production topology (Shared VPC attachment, HA enabled, read replicas, and existing data). This verifies that state migrations—such as &lt;code&gt;moved&lt;/code&gt; blocks or renamed resource attributes—execute in-place without triggering unintended replacement or downtime.&lt;/p&gt;
&lt;h3 id=&quot;stage-3-canary-production-cohort&quot;&gt;Stage 3: Canary production cohort&lt;/h3&gt;
&lt;p&gt;Roll the validated module release to a small cohort of tier-2 production workloads first. Monitor connection counts, replication lag, query latency, and Cloud Monitoring alerts for a 24-hour soak window. Track module version adoption across the fleet so legacy module versions do not linger indefinitely with unpatched security or backup defaults.&lt;/p&gt;
&lt;h3 id=&quot;stage-4-fleet-adoption-and-recovery-verification&quot;&gt;Stage 4: Fleet adoption and recovery verification&lt;/h3&gt;
&lt;p&gt;Expand rollout across remaining production cohorts in waves, updating the fleet inventory with the active module version and the timestamp of the last verified restore drill. Understand the rollback boundary clearly: reverting a Git commit can roll back a Terraform flag change, but it cannot un-upgrade a major PostgreSQL engine version or recover dropped data. True rollback for destructive database changes relies on PITR clones or pre-upgrade snapshots, never Git history alone.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Google Cloud’s Shared VPC architecture illustrates why platform contracts cannot stop at the database module boundary. In Google’s documented Shared VPC model, the host project owns the VPC network, subnets, firewall rules, and Private Services Access peering connection (&lt;code&gt;servicenetworking.googleapis.com&lt;/code&gt;), while service projects attach to those shared subnets using &lt;code&gt;roles/compute.networkUser&lt;/code&gt;. A database provisioning pipeline that only operates inside a single application service project will fail to configure private IP connectivity unless the platform interface explicitly coordinates host-project peering and subnet permissions.&lt;/p&gt;
&lt;p&gt;Similarly, Terraform’s documented behavior for &lt;code&gt;google_sql_database_instance&lt;/code&gt; reinforces why two-layer protection and staged applies are mandatory. When an engineer intentionally needs to replace a protected instance, setting &lt;code&gt;deletion_protection = false&lt;/code&gt; in HCL is not a single-step delete: Terraform must first apply the state update to disable protection (or remove &lt;code&gt;prevent_destroy&lt;/code&gt;) before a subsequent apply can destroy or replace the instance. That two-step friction is intentional—it prevents a hurried refactor from wiping a production database in one command.&lt;/p&gt;
&lt;p&gt;To validate a platform contract end to end before onboarding teams, execute a concrete acceptance test on one nonproduction service: submit an intake request, run policy validation on the generated &lt;code&gt;terraform plan&lt;/code&gt;, apply via Workload Identity Federation, inject a controlled drift change (such as a manual flag edit in Console) to verify drift detection without blind overwrite, and time a full PITR restore including application reconnection and DNS resolution.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Condition&lt;/th&gt;&lt;th&gt;Breaks how&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Missing owner metadata at intake&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Incidents and quota alerts stall with no escalation path&lt;/td&gt;&lt;td&gt;Inventory audit finds untagged instances&lt;/td&gt;&lt;td&gt;Require &lt;code&gt;owner&lt;/code&gt; and &lt;code&gt;pager_channel&lt;/code&gt; labels in module schema&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Unprotected &lt;code&gt;force new&lt;/code&gt; plan applied&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Immutable attribute edit destroys live database&lt;/td&gt;&lt;td&gt;CI plan diff shows &lt;code&gt;-/+&lt;/code&gt; replacement&lt;/td&gt;&lt;td&gt;Enforce &lt;code&gt;deletion_protection&lt;/code&gt;, &lt;code&gt;deletion_protection_enabled&lt;/code&gt;, and &lt;code&gt;prevent_destroy&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Monolithic state locks across teams&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;One slow apply blocks all teams via &lt;code&gt;.tflock&lt;/code&gt;&lt;/td&gt;&lt;td&gt;CI jobs queue waiting on GCS lock&lt;/td&gt;&lt;td&gt;Split GCS backend prefixes per workload and environment&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Plaintext secrets exposed in state&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Users with bucket read access pull DB credentials&lt;/td&gt;&lt;td&gt;IAM audit shows broad &lt;code&gt;storage.objectViewer&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Restrict GCS IAM, enable CMEK, and use WIF + Secret Manager&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Blind scheduled drift reconciliation&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Auto-apply reverts an emergency on-call flag fix&lt;/td&gt;&lt;td&gt;Drift detector triggers silent overwrite&lt;/td&gt;&lt;td&gt;Alert on drift and require human review before reconciling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;App DDL embedded in Terraform&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Table locks or schema mismatch break live app pods&lt;/td&gt;&lt;td&gt;&lt;code&gt;terraform apply&lt;/code&gt; hangs on lock wait&lt;/td&gt;&lt;td&gt;Move DDL to app migration pipelines with expand-and-contract&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Restore drill tests DB creation only&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Instance boots, but IAM, DNS, and app fail to connect&lt;/td&gt;&lt;td&gt;Chaos drill measures infra time, not app RTO&lt;/td&gt;&lt;td&gt;Require end-to-end app health check in quarterly restore drills&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Shared VPC permissions missing&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;App pods in service project cannot reach private IP&lt;/td&gt;&lt;td&gt;Connection timeouts from service subnets&lt;/td&gt;&lt;td&gt;Grant &lt;code&gt;roles/compute.networkUser&lt;/code&gt; and verify host firewall rules&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-database-platform-terraform-ownership-6-failures.svg&quot; alt=&quot;Lifecycle gaps turn routine Terraform runs into fleet outages&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;A database platform is an operational contract—covering ownership, upgrades, drift, and recovery—not a standalone Terraform module.&lt;/li&gt;
&lt;li&gt;Isolate Terraform state per workload and environment in GCS with object versioning, CMEK, and &lt;code&gt;.tflock&lt;/code&gt; locking to bound blast radius.&lt;/li&gt;
&lt;li&gt;Enforce three layers of protection (&lt;code&gt;deletion_protection = true&lt;/code&gt;, &lt;code&gt;settings.deletion_protection_enabled = true&lt;/code&gt;, and &lt;code&gt;lifecycle { prevent_destroy = true }&lt;/code&gt;) on every production database instance.&lt;/li&gt;
&lt;li&gt;Separate Shared VPC host-project networking (&lt;code&gt;servicenetworking.googleapis.com&lt;/code&gt; and firewall rules) from service-project compute (&lt;code&gt;roles/compute.networkUser&lt;/code&gt;) and application-owned DDL migrations.&lt;/li&gt;
&lt;li&gt;Treat drift as an investigation signal rather than an automatic overwrite instruction, and measure recovery drills by end-to-end application restoration time.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Audit your existing Cloud SQL and AlloyDB fleet for unowned instances, shared monolithic Terraform state files, and missing deletion-protection locks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Publish a versioned module contract that separates network ownership, database infrastructure, and application schema migrations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run an end-to-end acceptance test on one nonproduction workload—covering CI plan policy checks, drift detection, module upgrade, and a timed application-level PITR restore.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Add module version tracking, owner labels, and last-restore-drill timestamps to your fleet inventory this week.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/vpc/docs/provisioning-shared-vpc&quot;&gt;Google Cloud Shared VPC provisioning&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database_instance&quot;&gt;Terraform Google Provider: google_sql_database_instance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://developer.hashicorp.com/terraform/language/backend/gcs&quot;&gt;Terraform GCS backend&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/high-availability&quot;&gt;Cloud SQL high availability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/backup-recovery/backups&quot;&gt;Cloud SQL backups and PITR&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>checklist</category></item><item><title>Private Database Connectivity on Google Cloud: The Boundaries AWS Engineers Need to See</title><link>https://rajivonai.com/blog/2026-11-03-google-cloud-database-private-connectivity/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-11-03-google-cloud-database-private-connectivity/</guid><description>Trace Cloud SQL and AlloyDB connections across Shared VPC, private services access, Private Service Connect, identity, and database permissions.</description><pubDate>Thu, 08 Oct 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A private database endpoint can be unreachable to an authorized application and reachable to an application that should never have had access. Both failures begin when the network diagram ends at the word “private.”&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Treating “private IP” as a complete security and routing design hides non-transitive VPC peering limits, missing Cloud DNS records, blocked Auth Proxy ports, and unverified PostgreSQL privileges.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Trace every connection across four independent boundaries—source identity, consumer VPC routing/DNS (preferring PSC over PSA for multi-VPC topologies), connector TLS 1.3/IAM, and PostgreSQL object grants.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Cloud SQL Auth Proxy tunnels over TCP port 3307 (not 5432), does not create a network route for private IP, and AlloyDB PSC outbound calls (such as FDW) require regional Network Attachments.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-google-cloud-database-private-connectivity.svg&quot; alt=&quot;A connection crosses independent control boundaries&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;An enterprise moving PostgreSQL workloads from AWS to Google Cloud must translate more than instance sizes and backup schedules. An application frequently lives in one Google Cloud project, attaches to a Shared VPC hosted in a second project, and connects to a Cloud SQL or AlloyDB instance managed in a third project. Network engineers, database platform engineers, identity administrators, and application teams each own a distinct slice of that path.&lt;/p&gt;
&lt;p&gt;AWS mental models provide helpful starting points, but they diverge on topology and routing rules. A Google Cloud project maps loosely to an AWS account, Shared VPC parallels AWS VPC sharing, and Private Service Connect (PSC) parallels AWS PrivateLink. However, Google Cloud VPC networks are global resources with regional subnets, whereas AWS VPCs are regional. Counting projects tells you nothing about how many VPCs exist, where routing boundaries sit, or whether two workloads on the same Shared VPC can reach each other’s databases.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Connectivity reviews frequently stop after confirming that a database has a private IP address and no public IP. That binary check omits four independent failure domains that determine whether a query succeeds or fails—and whether an unauthorized workload can reach production data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 1: Non-transitive peering and IP allocation under Private Services Access (PSA).&lt;/strong&gt; Cloud SQL and AlloyDB PSA rely on VPC Network Peering between your consumer VPC and a Google-managed producer VPC. You must reserve a contiguous private CIDR block in your VPC, and Google forbids overlapping IP ranges. Because VPC Network Peering is strictly non-transitive, a workload in a peered spoke VPC or an on-premises network connected over Cloud VPN or Interconnect cannot natively route through a hub VPC to reach a PSA-peered database without proxies or Network Connectivity Center workarounds.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 2: Endpoint-per-VPC and DNS scoping under Private Service Connect (PSC).&lt;/strong&gt; PSC replaces VPC peering with consumer-side forwarding rules targeting a producer Service Attachment. Each consumer endpoint consumes only a single local IP address inside a chosen subnet and permits overlapping CIDR blocks across consumer VPCs. However, the Cloud SQL Auth Proxy and AlloyDB Language Connectors require a Cloud DNS private zone mapping the instance’s &lt;code&gt;pscDnsName&lt;/code&gt; to that local endpoint IP; Cloud DNS private zones do not propagate automatically across peered VPCs without explicit DNS peering or forwarding.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 3: Connector port 3307, TLS 1.3, and IAM token gating.&lt;/strong&gt; The Cloud SQL Auth Proxy automatically wraps connections in TLS 1.3 using ephemeral certificates and—when started with &lt;code&gt;--auto-iam-authn&lt;/code&gt;—exchanges Workload Identity or Application Default Credentials for short-lived OAuth 2.0 tokens. Crucially, the proxy tunnels traffic to the Cloud SQL backend on &lt;strong&gt;TCP port 3307&lt;/strong&gt; (not PostgreSQL’s native port 5432), requires the &lt;code&gt;cloudsql.instances.connect&lt;/code&gt; IAM permission, and explicitly does not create a new network path for private IP: the proxy must run where the private IP or PSC endpoint is already reachable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 4: Separation between cloud IAM and PostgreSQL RBAC.&lt;/strong&gt; Passing VPC firewall rules, PSC project allowlists, and &lt;code&gt;cloudsql.instances.connect&lt;/code&gt; IAM checks only lands the client at the PostgreSQL authentication gate. Even with IAM database authentication enabled, PostgreSQL still enforces database &lt;code&gt;CONNECT&lt;/code&gt;, schema &lt;code&gt;USAGE&lt;/code&gt;, table &lt;code&gt;SELECT&lt;/code&gt;/&lt;code&gt;INSERT&lt;/code&gt; privileges, and row-level security policies independently.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-google-cloud-database-private-connectivity-2-constraints.svg&quot; alt=&quot;Private IP alone leaves routing, DNS, IAM, and SQL roles unverified&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do you design and test the complete database connection path without conflating network reachability, cloud identity, and PostgreSQL authorization?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams designing private database access on Google Cloud typically evaluate four connectivity patterns.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Option&lt;/th&gt;&lt;th&gt;Routing &amp;#x26; IP Cost&lt;/th&gt;&lt;th&gt;Multi-VPC &amp;#x26; Hybrid Fit&lt;/th&gt;&lt;th&gt;Security &amp;#x26; Audit Posture&lt;/th&gt;&lt;th&gt;Primary Rejection or Selection Reason&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Public IP + Auth Proxy&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Public IP; zero VPC peering&lt;/td&gt;&lt;td&gt;Works anywhere with egress&lt;/td&gt;&lt;td&gt;TLS 1.3 + IAM gated, but public listener&lt;/td&gt;&lt;td&gt;Rejected by organizations enforcing private-only data plane policies&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Private Services Access (PSA)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Contiguous CIDR block; non-overlapping&lt;/td&gt;&lt;td&gt;Poor for spoke VPCs and on-prem&lt;/td&gt;&lt;td&gt;VPC peering; shared across attached subnets&lt;/td&gt;&lt;td&gt;Rejected for hub-and-spoke or multi-project estates due to non-transitivity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Private Service Connect (PSC)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;1 local subnet IP per endpoint; overlap OK&lt;/td&gt;&lt;td&gt;Strong across multi-VPC and hybrid&lt;/td&gt;&lt;td&gt;Consumer allowlist + endpoint + TLS 1.3&lt;/td&gt;&lt;td&gt;Recommended baseline for multi-project Cloud SQL and AlloyDB fleets&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Custom Bastion TCP Relay&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;VM private IP + SSH/IAP tunnel&lt;/td&gt;&lt;td&gt;Bridges spokes via hop&lt;/td&gt;&lt;td&gt;Single point of failure; OS/key patching toil&lt;/td&gt;&lt;td&gt;Rejected for application traffic; reserved for break-glass human access&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Using Public IP exclusively through the Cloud SQL Auth Proxy provides strong cryptographic identity via TLS 1.3 and &lt;code&gt;cloudsql.instances.connect&lt;/code&gt;, avoiding VPC peering completely. However, most regulated enterprises enforce organization policies (&lt;code&gt;constraints/sql.restrictPublicIp&lt;/code&gt;) that forbid public IP assignment on managed databases, making a private data-plane path mandatory.&lt;/p&gt;
&lt;p&gt;Private Services Access (PSA) is simple to provision when all applications live inside a single Shared VPC with plenty of non-overlapping RFC 1918 address space. It breaks down as soon as acquisitions introduce overlapping CIDRs or spoke VPCs need direct database access without hairpinning through custom proxies.&lt;/p&gt;
&lt;p&gt;Private Service Connect (PSC) is the recommended baseline for modern Cloud SQL and AlloyDB platforms. By exposing a Service Attachment that maps to a single IP forwarding rule per consumer VPC, PSC eliminates peering transitivity limits, supports overlapping CIDRs, and enforces explicit producer-side project allowlists.&lt;/p&gt;
&lt;p&gt;Custom bastion VMs running &lt;code&gt;socat&lt;/code&gt; or SSH port forwarding can bridge non-transitive PSA peerings in an emergency, but running application traffic through self-managed relay VMs reintroduces OS patching, high-availability failover, and connection-attribution gaps. Reserve Identity-Aware Proxy (IAP) TCP tunnels to hardened bastions strictly for audited human break-glass sessions.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-google-cloud-database-private-connectivity-3-options.svg&quot; alt=&quot;Private Service Connect removes VPC peering transitivity and CIDR locks&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;follow-one-request-across-four-boundaries&quot;&gt;Follow One Request Across Four Boundaries&lt;/h2&gt;
&lt;p&gt;To avoid blind spots, document and verify every database connection across four sequential control planes from client to row.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-google-cloud-database-private-connectivity-4-architecture.svg&quot; alt=&quot;Traffic traverses four independent control planes before reading a row&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-project-ownership-and-shared-vpc&quot;&gt;1. Project ownership and Shared VPC&lt;/h3&gt;
&lt;p&gt;A Shared VPC host project owns the VPC network, regional subnets, Cloud DNS private zones, and central firewall policies. Attached service projects use delegated subnets via &lt;code&gt;roles/compute.networkUser&lt;/code&gt; while keeping application compute, GKE clusters, and workload IAM boundaries isolated. Attaching two service projects to the same Shared VPC does not isolate their workloads at Layer 3 unless explicit firewall rules or hierarchical policies segment subnet traffic.&lt;/p&gt;
&lt;p&gt;For every database deployment, record four distinct identifiers in your platform catalog: the application project, the database resource project, the Shared VPC host project, and the VPC network name. Even when two of those coincide today, assuming they are identical breaks Terraform modules and incident escalation the first time a team splits workloads across projects.&lt;/p&gt;
&lt;h3 id=&quot;2-private-services-access-and-private-service-connect&quot;&gt;2. Private Services Access and Private Service Connect&lt;/h3&gt;
&lt;p&gt;When provisioning Cloud SQL or AlloyDB with PSC, the producer exposes a regional Service Attachment, and the consumer creates a forwarding rule backed by a single reserved subnet IP. You can configure both PSA and PSC on supported Cloud SQL instances during migration, but having both enabled does not make them equivalent—applications must target the intended endpoint IP or DNS name consistently.&lt;/p&gt;
&lt;p&gt;Remember the directional boundary on AlloyDB: inbound client traffic targets AlloyDB’s Service Attachment via your consumer endpoint, whereas &lt;strong&gt;outbound&lt;/strong&gt; traffic from AlloyDB (such as Foreign Data Wrappers or external migrations) requires a consumer-managed &lt;strong&gt;Network Attachment&lt;/strong&gt; in the exact same region as the AlloyDB cluster, along with &lt;code&gt;roles/compute.networkAdmin&lt;/code&gt; and &lt;code&gt;roles/dns.admin&lt;/code&gt; coordination.&lt;/p&gt;
&lt;h3 id=&quot;3-connection-authentication-and-encryption&quot;&gt;3. Connection authentication and encryption&lt;/h3&gt;
&lt;p&gt;Deploy the Cloud SQL Auth Proxy (or AlloyDB Auth Proxy) as a Kubernetes sidecar or local process binding to &lt;code&gt;127.0.0.1:5432&lt;/code&gt;, and pass &lt;code&gt;--auto-iam-authn&lt;/code&gt; alongside the instance connection name or PSC DNS target:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;cloud-sql-proxy&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;my-project:us-central1:orders-pg16&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --private-ip&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --auto-iam-authn&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --port=5432&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Inside the pod or VM, the application connects to &lt;code&gt;localhost:5432&lt;/code&gt; without managing SSL certificates. The proxy authenticates using the workload’s service account (&lt;code&gt;roles/cloudsql.client&lt;/code&gt; / &lt;code&gt;cloudsql.instances.connect&lt;/code&gt;) and opens a TLS 1.3 tunnel to port 3307 on the database’s private IP or PSC endpoint. Ensure host-project egress firewall rules explicitly permit TCP 3307 from application subnets to the PSC endpoint IP; allowing only TCP 5432 will cause the Auth Proxy handshake to time out silently.&lt;/p&gt;
&lt;h3 id=&quot;4-postgresql-authorization&quot;&gt;4. PostgreSQL authorization&lt;/h3&gt;
&lt;p&gt;Once the Auth Proxy establishes the TLS 1.3 tunnel and passes the IAM token, PostgreSQL verifies the mapped IAM database user and evaluates SQL-level permissions. A common mistake is granting broad database superuser or &lt;code&gt;cloudsqlsuperuser&lt;/code&gt; roles to application service accounts. Instead, create a dedicated IAM-mapped role per microservice and grant only the minimum required database, schema, and table privileges:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; CONNECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TO&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;orders-api@my-project.iam&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; USAGE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SCHEMA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; app &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TO&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;orders-api@my-project.iam&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INSERT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ALL TABLES &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IN&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SCHEMA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; app &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TO&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;orders-api@my-project.iam&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Separating cloud IAM (&lt;code&gt;cloudsql.instances.connect&lt;/code&gt;) from PostgreSQL grants ensures that even if two services share a Shared VPC subnet, neither can query the other’s tables without explicit SQL authorization.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Validating private database connectivity requires testing both authorized paths and deliberate denial paths across four sequential steps before production cutover.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-google-cloud-database-private-connectivity-5-flow.svg&quot; alt=&quot;Verify allow and deny paths across every origin before cutover&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;step-1-inventory-projects-owners-and-origins&quot;&gt;Step 1: Inventory projects, owners, and origins&lt;/h3&gt;
&lt;p&gt;Document the four project/network identifiers alongside the owners of Cloud DNS, PSC endpoint acceptance, IAM bindings, and PostgreSQL roles. Identify all three connection origins that must function: application pods/VMs, CI/CD schema migration runners, and human break-glass sessions via IAP.&lt;/p&gt;
&lt;h3 id=&quot;step-2-verify-routing-psc-acceptance-and-dns-resolution&quot;&gt;Step 2: Verify routing, PSC acceptance, and DNS resolution&lt;/h3&gt;
&lt;p&gt;Confirm the PSC endpoint status shows &lt;code&gt;ACCEPTED&lt;/code&gt; on the producer Service Attachment. From the exact application subnet or GKE pod namespace, verify that &lt;code&gt;pscDnsName&lt;/code&gt; resolves to the local PSC forwarding-rule IP (via &lt;code&gt;dig&lt;/code&gt; or &lt;code&gt;nslookup&lt;/code&gt;) and that TCP 3307 is reachable. For hybrid on-premises connections over Cloud VPN or Interconnect, verify that Cloud DNS inbound forwarding and on-premises conditional forwarders resolve the private zone accurately.&lt;/p&gt;
&lt;h3 id=&quot;step-3-validate-auth-proxy-iam-and-postgresql-privileges&quot;&gt;Step 3: Validate Auth Proxy IAM and PostgreSQL privileges&lt;/h3&gt;
&lt;p&gt;Start the Auth Proxy with &lt;code&gt;--auto-iam-authn&lt;/code&gt; using the workload’s actual service account (not an engineer’s personal &lt;code&gt;gcloud&lt;/code&gt; credentials). Confirm the connection succeeds, inspect &lt;code&gt;pg_stat_activity&lt;/code&gt; to verify the authenticated role name, and run a representative read/write query against the target schema.&lt;/p&gt;
&lt;h3 id=&quot;step-4-execute-negative-denial-tests&quot;&gt;Step 4: Execute negative (denial) tests&lt;/h3&gt;
&lt;p&gt;Proof of connectivity is incomplete without proof of isolation. Run three negative tests and verify the expected failure layer in logs: connect from an unapproved VPC or subnet (must fail at firewall/PSC allowlist), connect with an IAM identity lacking &lt;code&gt;cloudsql.instances.connect&lt;/code&gt; (must fail at Auth Proxy/IAM), and query a restricted schema using an authenticated app role (must fail at PostgreSQL &lt;code&gt;permission denied&lt;/code&gt;). Rollback during this validation phase is trivial: keep legacy endpoints active until both allow and deny checks pass for all three origins.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Google Cloud’s networking documentation highlights why diagram-level reviews fail in production. Under Private Services Access, Google explicitly documents that VPC Network Peering is non-transitive, meaning an additional spoke VPC or hybrid network cannot automatically traverse a hub VPC’s PSA peering to reach Cloud SQL. Teams migrating from AWS Transit Gateway architectures frequently assume hub-to-spoke routing extends to managed database peerings, only to hit connection timeouts during staging cutover.&lt;/p&gt;
&lt;p&gt;Similarly, Google’s Cloud SQL Auth Proxy documentation explicitly warns that enabling &lt;code&gt;--private-ip&lt;/code&gt; does not create a network path; it only instructs the proxy to dial the instance’s private IP rather than its public IP, and requires outbound TCP 3307 reachability. On AlloyDB with PSC, Google documents that the Auth Proxy and Language Connectors depend on private Cloud DNS records matching the instance’s &lt;code&gt;pscDnsName&lt;/code&gt;, and that outbound connections from AlloyDB require a regional Network Attachment in the consumer VPC.&lt;/p&gt;
&lt;p&gt;To apply this systematically, run a single standardized trace across three origins—an application workload, a schema migration runner, and an engineer’s IAP-tunneled session—recording the source project, DNS answer, endpoint IP, connector credential, PostgreSQL role, and denial test outcome without storing production secrets in the test log.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Condition&lt;/th&gt;&lt;th&gt;Breaks how&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Spoke VPC or on-prem hits Cloud SQL PSA&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Non-transitive VPC peering drops packets at hub&lt;/td&gt;&lt;td&gt;Connection timeouts from spoke/on-prem&lt;/td&gt;&lt;td&gt;Switch to PSC endpoints or supported NCC/proxy pattern&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Missing &lt;code&gt;pscDnsName&lt;/code&gt; Cloud DNS record&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;AlloyDB/Cloud SQL connectors fail name lookup&lt;/td&gt;&lt;td&gt;&lt;code&gt;NXDOMAIN&lt;/code&gt; or connector startup error&lt;/td&gt;&lt;td&gt;Automate Cloud DNS private zone and DNS peering&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Firewall opens 5432 but blocks TCP 3307&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Auth Proxy TLS 1.3 tunnel hangs on connect&lt;/td&gt;&lt;td&gt;Proxy logs &lt;code&gt;dial tcp ...:3307: i/o timeout&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Permit egress TCP 3307 to PSC/PSA destination IPs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;PSC Service Attachment left &lt;code&gt;PENDING&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Forwarding rule exists but drops traffic&lt;/td&gt;&lt;td&gt;Endpoint status != &lt;code&gt;ACCEPTED&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Automate consumer project allowlist or approval&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Auth Proxy lacks &lt;code&gt;cloudsql.instances.connect&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Network reaches endpoint, IAM token rejected&lt;/td&gt;&lt;td&gt;Proxy 403 error from Cloud SQL Admin API&lt;/td&gt;&lt;td&gt;Bind &lt;code&gt;roles/cloudsql.client&lt;/code&gt; to workload SA&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;IAM DB user lacks SQL &lt;code&gt;USAGE&lt;/code&gt;/&lt;code&gt;SELECT&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Auth succeeds, query fails with permission error&lt;/td&gt;&lt;td&gt;PostgreSQL &lt;code&gt;ERROR: permission denied&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Run explicit &lt;code&gt;GRANT CONNECT&lt;/code&gt;, &lt;code&gt;USAGE&lt;/code&gt;, and table grants&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;AlloyDB outbound FDW crosses regions&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Network Attachment is strictly regional&lt;/td&gt;&lt;td&gt;Outbound query fails to route&lt;/td&gt;&lt;td&gt;Create regional Network Attachment matching cluster&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Shared VPC subnet lacks firewall segmentation&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Compromised app can reach sibling DB endpoints&lt;/td&gt;&lt;td&gt;Lateral port scan succeeds across projects&lt;/td&gt;&lt;td&gt;Enforce scoped firewall rules and PSC allowlists&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-11-03-google-cloud-database-private-connectivity-6-failures.svg&quot; alt=&quot;Connectivity breaks at peering, DNS, port 3307, or SQL privileges&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Never treat “has a private IP” as sufficient security or routing proof; trace every database path across source identity, VPC routing/DNS, connector TLS/IAM, and PostgreSQL grants.&lt;/li&gt;
&lt;li&gt;Prefer Private Service Connect (PSC) over Private Services Access (PSA) for multi-project, hub-and-spoke, or overlapping-CIDR estates to avoid non-transitive VPC peering limits.&lt;/li&gt;
&lt;li&gt;Remember that the Cloud SQL Auth Proxy tunnels over TCP port 3307 with TLS 1.3, requires an existing private route, and gates access via &lt;code&gt;cloudsql.instances.connect&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Map &lt;code&gt;pscDnsName&lt;/code&gt; in Cloud DNS private zones for AlloyDB and Cloud SQL PSC connectors, and provision regional Network Attachments whenever AlloyDB initiates outbound traffic.&lt;/li&gt;
&lt;li&gt;Prove isolation by testing both authorized connections and deliberate denials across application workloads, schema migration runners, and human IAP sessions.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Identify every Cloud SQL and AlloyDB connection origin (application, migration runner, human break-glass) that relies on implicit peering or undocumented firewall rules.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Standardize on Private Service Connect endpoints, Cloud DNS private zones for &lt;code&gt;pscDnsName&lt;/code&gt;, Auth Proxy with &lt;code&gt;--auto-iam-authn&lt;/code&gt;, and least-privilege PostgreSQL roles.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Execute one end-to-end allow trace and three negative denial tests (network, IAM, and SQL role) from a staging service project.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Assign explicit owners for Host VPC firewall rules (TCP 3307), Cloud DNS records, PSC attachment approvals, and PostgreSQL privilege reviews this week.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/vpc/docs/shared-vpc&quot;&gt;Google Cloud Shared VPC overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/about-private-services-access&quot;&gt;Cloud SQL Private Services Access&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/configure-private-services-access-and-private-service-connect&quot;&gt;Cloud SQL PSA and PSC configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/sql-proxy&quot;&gt;Cloud SQL Auth Proxy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/alloydb/docs/connect-psc&quot;&gt;AlloyDB Private Service Connect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/vpc/docs/private-service-connect&quot;&gt;Private Service Connect overview&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>failures</category></item><item><title>RDS vs. Cloud SQL PostgreSQL: Production Operations Beyond HA</title><link>https://rajivonai.com/blog/2026-10-03-rds-cloud-sql-postgresql-production-operations/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-10-03-rds-cloud-sql-postgresql-production-operations/</guid><description>Compare PostgreSQL read scaling, storage, maintenance, backups, and operational controls across Amazon RDS and Cloud SQL.</description><pubDate>Sat, 03 Oct 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A PostgreSQL deployment can pass its failover test and still fail its users when a reader returns stale data, a maintenance event resets connections, or a restore procedure has never been exercised.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Identical feature-matrix checkmarks hide incompatible operational contracts across read routing, local NVMe SSD caching, planned maintenance drops, and PITR log retention.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Classify read queries by freshness contract, match local SSD features to actual bottlenecks (temp spills vs. L2 page cache), and size PITR and fallback load-shedding explicitly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Cloud SQL read pools route to any healthy node regardless of replication lag, and Enterprise Plus near-zero planned maintenance empties &lt;code&gt;UNLOGGED&lt;/code&gt; tables.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-03-rds-cloud-sql-operations.svg&quot; alt=&quot;PostgreSQL write, read scaling, and recovery paths&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Enterprises comparing Amazon RDS for PostgreSQL with Google Cloud SQL often begin with vCPU count, RAM, storage size, and whether high availability is enabled. Those baseline specs establish whether the instance starts, but day-two production stability depends on four operational subsystems: how applications route reads, how local NVMe SSDs accelerate queries, what planned maintenance interrupts, and how long transaction logs remain available for point-in-time recovery (PITR).&lt;/p&gt;
&lt;p&gt;The first two articles in this series covered zonal high availability and cross-region disaster recovery. This final article compares the production operating model around those protections across Amazon RDS for PostgreSQL and Cloud SQL Enterprise and Enterprise Plus. Aurora and AlloyDB are excluded.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A feature name rarely defines the application contract. A reader endpoint may distribute requests across replicas sitting at different WAL replay positions. A local SSD may accelerate temporary sort spills in one cloud and cache durable table pages in another. A planned maintenance target may describe a sub-second connection blip while silently truncating unlogged tables or dropping active transactions.&lt;/p&gt;
&lt;p&gt;Four concrete operational constraints govern production behavior across RDS and Cloud SQL:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Lag-blind read routing:&lt;/strong&gt; Google documents that a healthy Cloud SQL Enterprise Plus read-pool node receives traffic regardless of its replication lag, and successive queries in a logical session can hit nodes at different replay positions—causing application state to appear to move backward.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reader fallback load amplification:&lt;/strong&gt; If an application offloads &lt;code&gt;4,000&lt;/code&gt; read QPS to a 2-node read pool while the primary serves &lt;code&gt;1,000&lt;/code&gt; write QPS, failing all reads back to the primary during a replica outage jumps primary load by &lt;code&gt;(4,000 + 1,000) / 1,000 = 5x&lt;/code&gt;, turning a reader incident into a writer outage unless non-critical reads are shed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PITR log retention ceiling:&lt;/strong&gt; Cloud SQL Enterprise caps PITR transaction log retention at 1 to 7 days, whereas Cloud SQL Enterprise Plus and Amazon RDS both support 1 to 35 days of automated PITR log retention.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Maintenance interruption and state loss:&lt;/strong&gt; Cloud SQL Enterprise Plus targets under 1 second of connectivity interruption for eligible planned maintenance, but empties &lt;code&gt;UNLOGGED&lt;/code&gt; tables during the operation; RDS Blue/Green Deployments cut over in 5 seconds or less (2 seconds or less with the AWS Advanced JDBC Driver), yet still terminate open connections with &lt;code&gt;AdminShutdown&lt;/code&gt; errors during switchover.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-03-rds-cloud-sql-postgresql-production-operations-2-constraints.svg&quot; alt=&quot;Operational contracts depend on lag routing, SSD role, and PITR&quot;&gt;&lt;/p&gt;
&lt;p&gt;A comparison spreadsheet that puts “yes” in both columns conceals these runtime contracts. &lt;strong&gt;How should an enterprise translate an existing RDS PostgreSQL operating model into a Cloud SQL design that satisfies the same workload requirements?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Translating an RDS operating model to Cloud SQL requires matching each workload path—read scaling, local NVMe storage, planned upgrades, and PITR retention—rather than copying instance classes one-for-one.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Operational Path&lt;/th&gt;&lt;th&gt;Amazon RDS Option&lt;/th&gt;&lt;th&gt;Cloud SQL Option&lt;/th&gt;&lt;th&gt;Key Behavioral Tradeoff&lt;/th&gt;&lt;th&gt;Why Chosen or Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Read Scaling&lt;/td&gt;&lt;td&gt;Individual read replicas or Multi-AZ Cluster reader&lt;/td&gt;&lt;td&gt;Individual replicas or Ent.+ read pools&lt;/td&gt;&lt;td&gt;Cloud SQL read pools route regardless of lag; RDS Cluster readers are semisync&lt;/td&gt;&lt;td&gt;Route strict freshness to primary; use pools or replicas only for lag-tolerant reads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Local NVMe SSD&lt;/td&gt;&lt;td&gt;Optimized Reads (temp spills &amp;#x26; &lt;code&gt;REINDEX&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;Enterprise Plus Data Cache (L2 page cache)&lt;/td&gt;&lt;td&gt;RDS speeds sorts/temp tables; Cloud SQL caches table/index pages&lt;/td&gt;&lt;td&gt;Choose by bottleneck: temp-heavy ETL vs. working sets larger than RAM&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Planned Upgrades&lt;/td&gt;&lt;td&gt;RDS Maintenance or Blue/Green (&amp;#x3C;=5s cutover)&lt;/td&gt;&lt;td&gt;Enterprise Plus near-zero maint (&amp;#x3C;1s)&lt;/td&gt;&lt;td&gt;Blue/Green stages major upgrades; Ent+ empties unlogged tables on maint&lt;/td&gt;&lt;td&gt;Use Blue/Green for risky major upgrades; audit unlogged tables on Ent+&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup &amp;#x26; PITR&lt;/td&gt;&lt;td&gt;Automated PITR (1–35d) + AWS Backup&lt;/td&gt;&lt;td&gt;Ent. PITR (1–7d) or Ent.+ PITR (1–35d)&lt;/td&gt;&lt;td&gt;Cloud SQL Enterprise 7-day cap forces Ent+ if 30-day PITR is required&lt;/td&gt;&lt;td&gt;Select Ent+ when compliance requires &gt;7 days of point-in-time recovery&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-03-rds-cloud-sql-postgresql-production-operations-3-options.svg&quot; alt=&quot;Production operations require independent choices per path&quot;&gt;&lt;/p&gt;
&lt;p&gt;The most common migration mistake is selecting Cloud SQL Enterprise when compliance requires 14 to 35 days of PITR, or assuming Cloud SQL Data Cache replaces RDS Optimized Reads for sort-heavy batch jobs. Separating those four choices up front prevents costly tier upgrades or application rewrites after cutover.&lt;/p&gt;
&lt;h2 id=&quot;map-the-workload-to-independent-paths&quot;&gt;Map the Workload to Independent Paths&lt;/h2&gt;
&lt;p&gt;A resilient PostgreSQL operating model separates write authority, read offload, backup/PITR retention, and observability into independent paths with distinct service objectives. The diagram below shows the logical separation; physical endpoints depend on the chosen edition and topology.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-03-rds-cloud-sql-operations.svg&quot; alt=&quot;PostgreSQL write, read scaling, and recovery paths&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Application — writes and freshness-sensitive reads] --&gt; B[PostgreSQL primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[Application — lag-tolerant reads] --&gt; D[Read replicas or eligible read pool]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|asynchronous replication| D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[Backups and recovery logs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; F[Metrics, logs, and alerts]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; G[Restore and validation exercise]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;read-capacity-is-not-a-freshness-guarantee&quot;&gt;Read capacity is not a freshness guarantee&lt;/h3&gt;
&lt;p&gt;RDS offers standalone read replicas with individual endpoints and Multi-AZ DB clusters with a shared reader endpoint across two readable standbys. Cloud SQL offers standalone read replicas in both editions and managed read pools (with optional autoscaling) in Enterprise Plus. Adding readers scales CPU and I/O capacity, but it introduces a strict correctness question: how stale can a successful query result be?&lt;/p&gt;
&lt;p&gt;Google documents that a healthy Cloud SQL read-pool node receives traffic regardless of its &lt;code&gt;replica_lag&lt;/code&gt; or &lt;code&gt;replica_byte_lag&lt;/code&gt;, and that successive requests in the same user session can land on nodes at different WAL replay positions. Furthermore, when Enterprise Plus read-pool autoscaling spins up a new node during a traffic spike, that cold node can exhibit elevated initial lag while catching up. Consequently, a healthy TCP connection to a reader never proves data freshness.&lt;/p&gt;
&lt;p&gt;Classify every read query by contract. Order confirmations, balance checks, and read-after-write UI reloads belong on the primary unless the application tracks WAL LSN positions explicitly. Dashboards, search facets, and reporting queries can target readers if replication lag is alerted and the product accepts bounded staleness. Equally important, never configure blind fallback from readers to the primary without load shedding; verify that the primary can survive fallback traffic or drop non-essential reads first.&lt;/p&gt;
&lt;h3 id=&quot;local-ssd-is-not-one-universal-feature&quot;&gt;Local SSD is not one universal feature&lt;/h3&gt;
&lt;p&gt;On eligible NVMe-backed RDS PostgreSQL instance classes, Optimized Reads places PostgreSQL temporary objects—such as sort spills, hash joins, temporary tables, and &lt;code&gt;REINDEX&lt;/code&gt; scratch files—on local NVMe storage, relieving network-attached EBS bandwidth during heavy analytical or maintenance queries.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-03-rds-cloud-sql-postgresql-production-operations-4-architecture.svg&quot; alt=&quot;RDS accelerates temp spills; Cloud SQL caches working-set reads&quot;&gt;&lt;/p&gt;
&lt;p&gt;Cloud SQL Enterprise Plus Data Cache uses local SSDs for a completely different path: it acts as an L2 read cache between PostgreSQL &lt;code&gt;shared_buffers&lt;/code&gt; (and OS page cache) and Persistent Disk. Google documents that reads check main memory first, then the local SSD Data Cache, and finally Persistent Disk, while writes commit to Persistent Disk and update the Data Cache simultaneously. Data Cache is enabled by default on new Enterprise Plus instances and can be encrypted with Customer-Managed Encryption Keys (CMEK).&lt;/p&gt;
&lt;p&gt;Because these features target different bottlenecks, benchmark production-like query plans rather than comparing feature names. Measure &lt;code&gt;temp_bytes&lt;/code&gt; written, buffer hit ratios, and storage IOPS limits for your specific machine series and region.&lt;/p&gt;
&lt;h3 id=&quot;maintenance-changes-the-application-contract&quot;&gt;Maintenance changes the application contract&lt;/h3&gt;
&lt;p&gt;Cloud SQL Enterprise Plus provides near-zero downtime for eligible planned maintenance and scaling operations, targeting less than 1 second of connectivity interruption. However, Google explicitly notes that active workload shape and client retry behavior can increase observed downtime, read replicas have separate constraints, and &lt;code&gt;UNLOGGED&lt;/code&gt; tables are emptied after the near-zero maintenance operation completes.&lt;/p&gt;
&lt;p&gt;On AWS RDS, minor upgrades and parameter changes follow standard Multi-AZ failover windows, while RDS Blue/Green Deployments create a synchronized staging environment for major upgrades or schema changes. Current RDS Blue/Green optimizations typically complete writer switchover in 5 seconds or less (or 2 seconds or less with the AWS Advanced JDBC Driver), briefly placing the Blue primary in read-only mode (&lt;code&gt;AdminShutdown&lt;/code&gt;) while Green catches up. Neither mechanism eliminates the need for extension compatibility checks or client reconnect testing.&lt;/p&gt;
&lt;h3 id=&quot;backup-retention-is-not-point-in-time-recovery&quot;&gt;Backup retention is not point-in-time recovery&lt;/h3&gt;
&lt;p&gt;Retaining daily or monthly snapshots for years does not mean every second in that period is available for PITR. Point-in-time recovery requires a valid base backup plus continuous WAL segments within the configured retention window: 1 to 35 days on Amazon RDS and Cloud SQL Enterprise Plus, or 1 to 7 days on Cloud SQL Enterprise. Long-term compliance retention beyond 35 days on AWS requires AWS Backup or manual snapshot copies, which restore to static snapshot timestamps rather than arbitrary seconds.&lt;/p&gt;
&lt;p&gt;Check replica lag, PITR log retention, and extension parity directly in PostgreSQL and via provider CLI before approving production cutover:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Inspect replication lag on the primary and verify whether unlogged tables exist&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; application_name, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, sync_state,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; replay_lag_bytes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_replication;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; relname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_class &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; relpersistence &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;u&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Configure 35-day PITR transaction log retention on Cloud SQL Enterprise Plus&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;gcloud&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; sql&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; instances&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; patch&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; my-entplus-pg&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --enable-point-in-time-recovery&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --retained-transaction-log-days=35&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Translating or upgrading a production PostgreSQL operating model requires a four-stage validation sequence before production traffic relies on it.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-03-rds-cloud-sql-postgresql-production-operations-5-flow.svg&quot; alt=&quot;Operational readiness requires staged fallback and restore drills&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Classify and route reads:&lt;/strong&gt; Audit application queries and tag each repository method as &lt;code&gt;PRIMARY_REQUIRED&lt;/code&gt; (strict read-after-write) or &lt;code&gt;READER_ELIGIBLE&lt;/code&gt; (lag-tolerant). Verify that &lt;code&gt;READER_ELIGIBLE&lt;/code&gt; queries point to the read replica or Cloud SQL read pool and never silently mutate state.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Test reader fallback and load shedding:&lt;/strong&gt; Simulate a read-pool or replica failure in staging under representative QPS. Measure primary CPU and connection count during fallback; if primary utilization exceeds 75%, implement circuit-breaking or feature-flag load shedding for non-critical reads before production launch.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Simulate planned maintenance and upgrades:&lt;/strong&gt; Trigger an RDS Blue/Green switchover or a Cloud SQL maintenance simulation while running active connection pools. Confirm that pools recover from &lt;code&gt;AdminShutdown&lt;/code&gt; or transient socket resets within your latency SLO, and verify that no durable workflow depends on &lt;code&gt;UNLOGGED&lt;/code&gt; tables.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Execute a timed PITR restore drill:&lt;/strong&gt; Restore a production-representative backup to an isolated VPC environment at a specific timestamp. Verify extension availability (&lt;code&gt;pg_stat_statements&lt;/code&gt;, &lt;code&gt;pgvector&lt;/code&gt;, &lt;code&gt;postgis&lt;/code&gt;), IAM/role permissions, network peering, and record the total elapsed time from restore command to first valid application query.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Repeat stages 3 and 4 after every major PostgreSQL engine upgrade or machine-class migration; hardware and engine-version changes routinely alter restore times and extension behavior.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Google’s documented Cloud SQL read-pool routing behavior enables a direct, falsifiable operational test without waiting for a customer incident: write a monotonically increasing counter value to the primary, then immediately issue sequential &lt;code&gt;SELECT&lt;/code&gt; queries through the read-pool endpoint while logging both &lt;code&gt;inet_server_addr()&lt;/code&gt; and the returned counter value. Under induced write load or artificial replay delay, you can observe successive queries landing on different pool nodes and returning older counter values—proving why session-level consistency cannot rely on a read pool. This test design is derived directly from Cloud SQL’s documented routing behavior rather than a claimed customer outage.&lt;/p&gt;
&lt;p&gt;For planned maintenance and Blue/Green upgrades, running the provider’s simulation against a bare &lt;code&gt;psql&lt;/code&gt; client hides real-world failure modes. Include your actual application connection pool, sidecar proxies (such as Cloud SQL Auth Proxy or PgBouncer), and background worker queues in the test. A database engine that completes switchover in 2 to 5 seconds can still appear unavailable for minutes if a worker queue crashes on &lt;code&gt;AdminShutdown&lt;/code&gt; and enters an exponential backoff loop.&lt;/p&gt;
&lt;p&gt;For local SSD evaluation, avoid comparing advertised raw NVMe IOPS in isolation. Run &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; on your top 10 slowest queries on both candidate machine classes: if &lt;code&gt;Buffers: shared read&lt;/code&gt; dominates and fits inside the Data Cache footprint, Cloud SQL Data Cache directly reduces disk latency; if &lt;code&gt;Sort Method: external merge Disk&lt;/code&gt; or &lt;code&gt;temp_bytes&lt;/code&gt; dominates, RDS Optimized Reads targets that exact I/O path.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Production PostgreSQL operations degrade silently when teams assume feature parity across clouds without testing edge cases.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-03-rds-cloud-sql-postgresql-production-operations-6-failures.svg&quot; alt=&quot;Lagging pools, fallback stampedes, and unlogged tables break ops&quot;&gt;&lt;/p&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Condition&lt;/th&gt;&lt;th&gt;Breaks How&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Read pool serves lagging node&lt;/td&gt;&lt;td&gt;Cloud SQL routes to any healthy node regardless of lag&lt;/td&gt;&lt;td&gt;Rising &lt;code&gt;replica_lag&lt;/code&gt; or stale-read user reports&lt;/td&gt;&lt;td&gt;Route strict freshness queries to the primary&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;State moves backward&lt;/td&gt;&lt;td&gt;Successive reads hit pool nodes at different LSNs&lt;/td&gt;&lt;td&gt;Monotonic ID regressions in client telemetry&lt;/td&gt;&lt;td&gt;Pin read-after-write flows to primary or use LSN checks&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reader fallback stampede&lt;/td&gt;&lt;td&gt;Reader outage dumps 100% of read QPS onto primary&lt;/td&gt;&lt;td&gt;Primary CPU/connections spike during reader incident&lt;/td&gt;&lt;td&gt;Capacity-test fallback and shed non-critical reads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Wrong local SSD assumption&lt;/td&gt;&lt;td&gt;Team expects Data Cache to speed sorts or Optimized Reads to cache tables&lt;/td&gt;&lt;td&gt;High &lt;code&gt;temp_bytes&lt;/code&gt; or persistent disk read latency&lt;/td&gt;&lt;td&gt;Match feature to &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; profile&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Maintenance breaks pools&lt;/td&gt;&lt;td&gt;&lt;code&gt;AdminShutdown&lt;/code&gt; or &amp;#x3C;1s blip leaves hung sockets or crashed workers&lt;/td&gt;&lt;td&gt;Error spike outlasting provider maintenance window&lt;/td&gt;&lt;td&gt;Test pool eviction and worker reconnect on simulated maint&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unlogged tables emptied&lt;/td&gt;&lt;td&gt;Cloud SQL Ent+ near-zero maintenance truncates &lt;code&gt;UNLOGGED&lt;/code&gt; tables&lt;/td&gt;&lt;td&gt;Empty lookup/session tables after maintenance event&lt;/td&gt;&lt;td&gt;Query &lt;code&gt;relpersistence = &apos;u&apos;&lt;/code&gt; and ban durable state there&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;PITR window too short&lt;/td&gt;&lt;td&gt;Cloud SQL Enterprise 7-day cap misses 14-day corruption discovery&lt;/td&gt;&lt;td&gt;Restore target timestamp falls outside retained WAL&lt;/td&gt;&lt;td&gt;Use Cloud SQL Ent+ or RDS for up to 35-day PITR&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Untested restore or extensions&lt;/td&gt;&lt;td&gt;Missing extension or slow WAL replay blows past RTO&lt;/td&gt;&lt;td&gt;Restore drill fails or exceeds target recovery window&lt;/td&gt;&lt;td&gt;Run quarterly timed PITR drills in isolated VPC&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Health does not equal freshness on read pools:&lt;/strong&gt; Cloud SQL read pools route to any healthy node regardless of replication lag, so sequential reads can move backward unless freshness-sensitive queries target the primary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Local SSD features solve opposite bottlenecks:&lt;/strong&gt; RDS Optimized Reads accelerates temporary sort and &lt;code&gt;REINDEX&lt;/code&gt; spills, while Cloud SQL Enterprise Plus Data Cache accelerates L2 table and index page reads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reader fallback can take down your primary:&lt;/strong&gt; Always load-test or shed non-critical read traffic before failing reader endpoints back to the writer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Unlogged tables do not survive Cloud SQL Ent+ maintenance:&lt;/strong&gt; Audit &lt;code&gt;pg_class&lt;/code&gt; for &lt;code&gt;relpersistence = &apos;u&apos;&lt;/code&gt; and treat unlogged tables strictly as disposable scratch space.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PITR retention differs by edition:&lt;/strong&gt; Verify whether your compliance window fits Cloud SQL Enterprise’s 7-day ceiling or requires Enterprise Plus or RDS (up to 35 days), and time your restore drills.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Inventory every production workflow’s read-freshness contract, temporary-disk vs. working-set I/O profile, maintenance tolerance, and required PITR retention window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Map each requirement independently to RDS or Cloud SQL features—separating primary reads, lag-tolerant readers, local SSD role, and backup retention.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; benchmarks, a read-pool lag test, a simulated maintenance or Blue/Green cutover, and a timed PITR restore in staging.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Check &lt;code&gt;pg_stat_replication&lt;/code&gt; and &lt;code&gt;relpersistence = &apos;u&apos;&lt;/code&gt; on your current PostgreSQL primary this week, and lock the verified restore runbook into your upgrade checklist.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/about-read-pools&quot;&gt;Cloud SQL read pools&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/data-cache&quot;&gt;Cloud SQL data cache&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/maintenance&quot;&gt;Cloud SQL maintenance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/backup-recovery/pitr&quot;&gt;Cloud SQL point-in-time recovery&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PostgreSQL.optimizedreads.html&quot;&gt;RDS PostgreSQL Optimized Reads&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/blue-green-deployments.html&quot;&gt;RDS Blue/Green Deployments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/extensions&quot;&gt;Cloud SQL PostgreSQL extensions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>checklist</category></item><item><title>RDS vs. Cloud SQL PostgreSQL: Disaster Recovery When a Region Fails</title><link>https://rajivonai.com/blog/2026-10-02-rds-cloud-sql-postgresql-disaster-recovery/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-10-02-rds-cloud-sql-postgresql-disaster-recovery/</guid><description>Compare cross-region PostgreSQL promotion and Cloud SQL advanced disaster recovery, including routing, data loss, and failback.</description><pubDate>Fri, 02 Oct 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A cross-region replica can restore a PostgreSQL writer while still missing transactions that the original writer acknowledged. Recovery speed and data loss are different questions.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Cross-region read replicas replicate asynchronously, so an abrupt regional outage can lose committed writes while manual promotion, DNS cutover, and unfenced writers extend recovery.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Separate RPO (lag exposure) from RTO (promotion and routing), fence the old primary before opening the new writer, and use Cloud SQL Enterprise Plus managed write endpoints or automated RDS DNS cutover.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Planned switchover guarantees zero data loss only when both regions are healthy; never treat a switchover drill as proof of zero-loss outage recovery, and expect a 5–15 minute post-promotion PITR gap on Cloud SQL.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-02-rds-cloud-sql-dr.svg&quot; alt=&quot;Cross-region recovery stages and data loss boundary&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;An enterprise can protect an Amazon RDS for PostgreSQL instance against an Availability Zone failure with Multi-AZ and still lose access to the database if the surrounding AWS region becomes unavailable. Adding a cross-region read replica establishes a recovery target in a second region, but it introduces an asynchronous replication boundary and a manual promotion workflow.&lt;/p&gt;
&lt;p&gt;Google Cloud SQL Enterprise also supports cross-region read replicas. Cloud SQL Enterprise Plus adds an advanced disaster recovery workflow with a designated DR replica, a global managed DNS write endpoint, and a coordinated switchover operation. Both platforms serve the same broad goal—restoring a writable PostgreSQL primary in another region—but they do not make ordinary replica promotion, a planned switchover, and zonal HA failover interchangeable.&lt;/p&gt;
&lt;p&gt;This is the second article in the PostgreSQL Across AWS and Google Cloud series. It covers Amazon RDS for PostgreSQL and Cloud SQL Enterprise and Enterprise Plus; Aurora Global Database and AlloyDB are outside its scope.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A disaster recovery plan often declares that a cross-region replica is “ready to promote” without answering four operational questions: how much acknowledged data has actually reached the replica, who has authority and access to invoke promotion, how clients discover the new writer, and how the team fences the old region before rebuilding reverse replication. If the former primary returns while still accepting writes, directing clients to both databases creates divergent transaction histories that cannot be merged automatically.&lt;/p&gt;
&lt;p&gt;Four concrete constraints govern cross-region PostgreSQL recovery:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Asynchronous RPO math:&lt;/strong&gt; Both RDS cross-region read replicas and Cloud SQL cross-region replicas replicate asynchronously over WAN links. At a sustained write rate of &lt;code&gt;2,000&lt;/code&gt; transactions per second, even &lt;code&gt;15&lt;/code&gt; seconds of replication lag places &lt;code&gt;2,000 × 15 = 30,000&lt;/code&gt; committed transactions at risk of loss during an abrupt regional failure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replica fan-out and backup retention limits:&lt;/strong&gt; Amazon RDS PostgreSQL supports up to 15 read replicas per primary instance (or up to 155 using cascading replicas on PostgreSQL 14.1 and later), while native RDS automated backups and cross-region automated backup replication are capped at a maximum retention of 35 days. Retaining recovery points beyond 35 days requires manual snapshots or AWS Backup.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No automatic cross-region promotion:&lt;/strong&gt; Neither RDS cross-region read replicas nor Cloud SQL DR replicas promote automatically on a regional outage. Recovery requires an operator or external orchestrator to invoke promotion, reboot or reconfigure the replica into a standalone primary, and update client routing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Post-promotion PITR activation gap:&lt;/strong&gt; On Cloud SQL Enterprise Plus Advanced DR, the newly promoted primary triggers an initial backup asynchronously after failover or switchover; Google documents that PITR becomes fully active only after that baseline backup completes, typically taking 5 to 15 minutes depending on disk size.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-02-rds-cloud-sql-postgresql-disaster-recovery-2-constraints.svg&quot; alt=&quot;RPO is bounded by async lag while RTO is bounded by promotion and routing&quot;&gt;&lt;/p&gt;
&lt;p&gt;Backups solve a different problem than replicas. Automated backups and Point-in-Time Recovery (PITR) can recover from logical corruption or accidental deletion that has already replicated to a standby, but restoring from snapshots and replaying WAL into a new instance takes far longer than promoting a warm replica.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How should a team recover a regional PostgreSQL service while making the data-loss boundary and routing procedure explicit?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Cross-region PostgreSQL DR architectures trade idle compute cost, routing automation, and failback complexity against RTO and RPO targets. Single-region Multi-AZ is rejected as a regional DR strategy because both zones share the same regional control plane and fault domain.&lt;/p&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Option&lt;/th&gt;&lt;th&gt;RPO Exposure&lt;/th&gt;&lt;th&gt;Typical RTO Profile&lt;/th&gt;&lt;th&gt;Routing Mechanism&lt;/th&gt;&lt;th&gt;Failback Path&lt;/th&gt;&lt;th&gt;Why Chosen or Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Cross-Region Backup Copy&lt;/td&gt;&lt;td&gt;Minutes (log shipping)&lt;/td&gt;&lt;td&gt;Slowest (full restore)&lt;/td&gt;&lt;td&gt;New instance endpoint&lt;/td&gt;&lt;td&gt;Restore to original region&lt;/td&gt;&lt;td&gt;Rejected for tight RTO; chosen only for low-cost cold DR or corruption recovery&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;RDS Cross-Region Read Replica&lt;/td&gt;&lt;td&gt;Async WAL lag&lt;/td&gt;&lt;td&gt;Manual promote + reboot&lt;/td&gt;&lt;td&gt;Route 53 CNAME or app config&lt;/td&gt;&lt;td&gt;Severed; must rebuild replica&lt;/td&gt;&lt;td&gt;Chosen on AWS RDS when warm standby RTO is required without Aurora&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cloud SQL Enterprise Replica&lt;/td&gt;&lt;td&gt;Async WAL lag&lt;/td&gt;&lt;td&gt;Manual promote&lt;/td&gt;&lt;td&gt;App config or custom DNS&lt;/td&gt;&lt;td&gt;Standalone; rebuild manually&lt;/td&gt;&lt;td&gt;Chosen on GCP Enterprise tier when Enterprise Plus cost is not justified&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cloud SQL Ent.+ Advanced DR&lt;/td&gt;&lt;td&gt;Async on outage; 0 on switchover&lt;/td&gt;&lt;td&gt;Managed failover/switchover&lt;/td&gt;&lt;td&gt;Managed &lt;code&gt;.global.sql-psa.goog&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Managed role reversal (switchover)&lt;/td&gt;&lt;td&gt;Chosen on GCP when managed DNS routing and planned zero-loss switchover matter&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-02-rds-cloud-sql-postgresql-disaster-recovery-3-options.svg&quot; alt=&quot;Cross-region DR options trade restore speed, routing, and failback&quot;&gt;&lt;/p&gt;
&lt;p&gt;The sharpest architectural split is between Cloud SQL Enterprise replica promotion and Enterprise Plus Advanced DR. Standard replica promotion on either RDS or Cloud SQL Enterprise severs the replication relationship permanently: once promoted, the replica becomes an independent primary, and returning to the original region requires provisioning a fresh replica in the recovered region and performing another cutover. Cloud SQL Enterprise Plus Advanced DR preserves the relationship metadata for planned switchovers and updates a global Private Services Access (PSA) DNS write endpoint automatically, removing manual connection-string edits at the cost of requiring the Enterprise Plus tier and PITR enabled on the primary.&lt;/p&gt;
&lt;h2 id=&quot;design-the-recovery-path-before-the-outage&quot;&gt;Design the Recovery Path Before the Outage&lt;/h2&gt;
&lt;p&gt;A resilient cross-region design separates steady-state asynchronous replication from deliberate recovery invocation, endpoint routing, and post-recovery fencing. The schematic below outlines those boundaries; it does not imply automatic cross-region failover.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-02-rds-cloud-sql-dr.svg&quot; alt=&quot;Cross-region recovery stages and data loss boundary&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Application — normal writes] --&gt; B[Primary region — PostgreSQL writer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|asynchronous replication| C[Recovery region — replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[Regional outage] --&gt; E[Operator or orchestration invokes recovery]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[Replica becomes writer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Application reconnects through recovery routing]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H[Former region restored] --&gt; I[Rebuild or reverse replication after verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;start-with-the-asynchronous-boundary&quot;&gt;Start with the asynchronous boundary&lt;/h3&gt;
&lt;p&gt;AWS describes RDS read replicas as asynchronous, and Cloud SQL cross-region replicas also replicate asynchronously. If a primary region fails after PostgreSQL acknowledges a commit to a client but before the WAL record reaches and is applied on the cross-region replica, promoting that replica drops those unshipped transactions. Consequently, RPO depends on the replica’s actual replay position at the instant of failure—not on a trailing dashboard average.&lt;/p&gt;
&lt;p&gt;Recovery point objective (RPO) defines the maximum tolerable data loss; recovery time objective (RTO) defines the maximum tolerable time without a writable primary. Replica lag feeds directly into RPO exposure, while promotion duration, DNS propagation, and pool reconnection determine RTO. Measuring promotion time alone never proves either objective was met.&lt;/p&gt;
&lt;h3 id=&quot;rds-promote-and-re-establish-routing&quot;&gt;RDS: promote and re-establish routing&lt;/h3&gt;
&lt;p&gt;In Amazon RDS for PostgreSQL, promoting a cross-region read replica stops asynchronous replication from the source, reboots the replica to clear its read-only state, and converts it into a standalone read-write DB instance. Because the promoted instance gets its own RDS endpoint in the recovery region, the application must route through an abstraction—such as a Route 53 CNAME record with a low TTL or updated service discovery—to reach the new writer.&lt;/p&gt;
&lt;p&gt;AWS recommends enabling automated backups on the cross-region read replica before an incident occurs so that post-promotion snapshots and PITR workflows operate immediately without building a baseline from scratch. Once the original region recovers, the old primary remains isolated; the team must provision a new replica in the original region from the promoted writer before planning any return migration.&lt;/p&gt;
&lt;h3 id=&quot;cloud-sql-distinguish-promotion-from-advanced-dr&quot;&gt;Cloud SQL: distinguish promotion from advanced DR&lt;/h3&gt;
&lt;p&gt;Cloud SQL Enterprise and Enterprise Plus both support standard cross-region replica promotion. Enterprise Plus adds Advanced DR, which designates a cross-region replica as the DR target and exposes two distinct operations: &lt;strong&gt;replica failover&lt;/strong&gt; for an unplanned regional outage and &lt;strong&gt;switchover&lt;/strong&gt; for a planned, graceful role reversal.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-02-rds-cloud-sql-postgresql-disaster-recovery-4-architecture.svg&quot; alt=&quot;Managed write endpoints decouple clients from regional IP changes&quot;&gt;&lt;/p&gt;
&lt;p&gt;Under documented prerequisites—including Enterprise Plus, PITR enabled on the primary, and compatible Private Services Access (PSA) networking—a planned switchover drains and synchronizes the DR replica for zero data loss before reversing roles. An unplanned replica failover does not provide that zero-loss guarantee because the primary region is unreachable and unshipped WAL cannot be fetched.&lt;/p&gt;
&lt;p&gt;When configured with a managed DNS write endpoint (&lt;code&gt;.global.sql-psa.goog&lt;/code&gt; over PSA), Cloud SQL automatically updates DNS to point to the newly promoted primary’s private IP after either failover or switchover. However, existing TCP sockets are still terminated, and SSL/TLS configurations must match between primary and DR replica or reconnecting clients will fail TLS handshakes.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Promote a standalone RDS cross-region read replica during an AWS regional outage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; rds&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; promote-read-replica&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --db-instance-identifier&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; orders-pg-dr-replica&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --region&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; us-west-2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Unplanned: fail over to the designated DR replica (immediate; data loss possible if the replica lags)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;gcloud&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; sql&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; instances&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; promote-replica&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; orders-pg-dr-replica&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --failover&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Planned and lossless (also usable for DR tests): switchover&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;gcloud&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; sql&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; instances&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; switchover&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; orders-pg-dr-replica&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;recovery-is-not-finished-at-promotion&quot;&gt;Recovery is not finished at promotion&lt;/h3&gt;
&lt;p&gt;Promoting the DR replica restores write capability, not full production safety. Before opening the new writer to full traffic, verify instance sizing, security groups or VPC firewall rules, IAM/database credentials, monitoring alerts, and downstream CDC or analytics consumers. On Cloud SQL Advanced DR, confirm that the asynchronous post-promotion baseline backup has completed and PITR is active before declaring the environment fully protected against a second failure.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;A safe cross-region recovery follows four strict, ordered stages. Reordering them—especially allowing the old region to accept traffic before fencing—turns a regional outage into a data-corruption incident.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-02-rds-cloud-sql-postgresql-disaster-recovery-5-flow.svg&quot; alt=&quot;DR cutover requires fencing, promotion, verification, and rebuild&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Fence the old write path:&lt;/strong&gt; Before promoting the recovery replica, block ingress traffic, disable background job workers, and revoke database routing in the failing primary region. If the primary region experiences a partial network partition rather than a total power-off, unfenced workers can continue writing to the old primary after the replica is promoted.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Invoke replica promotion or failover:&lt;/strong&gt; Execute &lt;code&gt;aws rds promote-read-replica&lt;/code&gt; on RDS or &lt;code&gt;gcloud sql instances failover&lt;/code&gt; (or switchover for planned tests) on Cloud SQL. Confirm the target instance transitions to read-write mode and check its latest applied transaction timestamp or sequence ID.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cut over routing and verify parity:&lt;/strong&gt; Update Route 53 CNAMEs on RDS or verify that the &lt;code&gt;.global.sql-psa.goog&lt;/code&gt; write endpoint resolves to the recovery region on Cloud SQL. Validate TLS handshakes, connection-pool health, and—critically—confirm that automated backups and PITR are active on the new primary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reconcile and rebuild reverse replication:&lt;/strong&gt; Compare an external durable audit log (such as an event bus, payment gateway log, or outbox ledger) against the promoted database to identify transactions committed in the lost region that never replicated. Once traffic is stable and the original region is healthy, provision a new cross-region replica back toward the original region before scheduling any failback.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Gate every production DR rollout with two separate drills: a planned switchover (or staged promotion) to validate permissions, DNS TTLs, and SSL parity, followed by an isolated failure-injection drill in a non-production environment that kills the primary under active write load to measure actual lost transactions.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;A rigorous DR exercise records evidence from both the database and an external observer outside the failure domain. Before starting an outage simulation, establish a continuous stream of sequentially numbered writes and log every acknowledged transaction ID to an independent store in a third region or neutral observability system.&lt;/p&gt;
&lt;p&gt;During the drill, capture four timestamps and one sequence boundary: the last write acknowledged by the old primary, the highest sequence ID present on the promoted replica, the moment the new writer accepts its first TLS connection, and the moment a complete end-to-end business transaction succeeds. Subtracting the promoted sequence ID from the external acknowledgment log yields the exact RPO loss for that run, while the time gap establishes true application RTO.&lt;/p&gt;
&lt;p&gt;Google’s documentation highlights why planned switchover drills cannot replace outage simulations: a Cloud SQL Advanced DR switchover coordinates both instances to achieve zero data loss, whereas an abrupt regional failure invokes replica failover against whatever WAL has already arrived. Similarly, on RDS, a manual promotion under zero load completes cleanly, whereas promoting under heavy write concurrency forces PostgreSQL crash recovery and WAL replay before connections open. Testing both scenarios under representative write load prevents false confidence in RPO and RTO numbers.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Cross-region disaster recovery fails most often in the seams between database promotion, network routing, and operational assumptions.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-02-rds-cloud-sql-postgresql-disaster-recovery-6-failures.svg&quot; alt=&quot;Split-brain writes, SSL mismatches, and PITR gaps derail recovery&quot;&gt;&lt;/p&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Condition&lt;/th&gt;&lt;th&gt;Breaks How&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Unfenced primary returns&lt;/td&gt;&lt;td&gt;Old region accepts writes after DR replica promotes, creating divergent histories&lt;/td&gt;&lt;td&gt;Conflicting sequence IDs or dual-region write metrics&lt;/td&gt;&lt;td&gt;Fence Region A ingress and workers before promotion&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Async WAL lag at failure&lt;/td&gt;&lt;td&gt;Committed transactions in lost region are missing from promoted replica&lt;/td&gt;&lt;td&gt;Gap between external outbox log and promoted max ID&lt;/td&gt;&lt;td&gt;Alert on replication lag; reconcile missing events&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;SSL or network mismatch&lt;/td&gt;&lt;td&gt;Clients fail TLS or PSA routing when write endpoint flips to DR region&lt;/td&gt;&lt;td&gt;Spike in TLS handshake or connection refused errors&lt;/td&gt;&lt;td&gt;Audit SSL mode, CA certs, and VPC peering continuously&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Post-promotion PITR gap&lt;/td&gt;&lt;td&gt;New primary lacks PITR during 5–15 minute initial backup window&lt;/td&gt;&lt;td&gt;Backup status shows baseline snapshot still in progress&lt;/td&gt;&lt;td&gt;Monitor backup completion before lifting load limits&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Undersized DR replica&lt;/td&gt;&lt;td&gt;Smaller DR machine class saturates CPU or IOPS under production traffic&lt;/td&gt;&lt;td&gt;High CPU, lock waits, or connection timeouts post-cutover&lt;/td&gt;&lt;td&gt;Size DR replica for full production write and read load&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Missing promotion permissions&lt;/td&gt;&lt;td&gt;On-call lacks IAM rights or CLI runbook to invoke failover&lt;/td&gt;&lt;td&gt;Promotion command fails with authorization error&lt;/td&gt;&lt;td&gt;Pre-stage break-glass IAM roles and test invocation quarterly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Switchover used as outage proxy&lt;/td&gt;&lt;td&gt;Team assumes zero data loss during unplanned outage based on switchover test&lt;/td&gt;&lt;td&gt;Unplanned outage loses recent writes despite “0 RPO” claim&lt;/td&gt;&lt;td&gt;Test abrupt primary kill under load separately from switchover&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No reverse replication plan&lt;/td&gt;&lt;td&gt;Promoted region runs unprotected for days without a new DR replica&lt;/td&gt;&lt;td&gt;Single-region topology alert after DR event&lt;/td&gt;&lt;td&gt;Script reverse-replica creation immediately after stabilization&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;RPO and RTO are independent measurements:&lt;/strong&gt; Async replica lag sets your data-loss boundary (RPO), while promotion, DNS cutover, and pool recovery set your downtime (RTO).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cross-region promotion is never automatic:&lt;/strong&gt; Both RDS read replicas and Cloud SQL DR replicas require an explicit operator or orchestrator invocation to become writable.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fence before you promote:&lt;/strong&gt; Allowing an old primary to accept traffic after a regional partition creates unmergeable split-brain histories.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Managed write endpoints still require parity:&lt;/strong&gt; Cloud SQL Enterprise Plus &lt;code&gt;.global.sql-psa.goog&lt;/code&gt; endpoints automate DNS updates, but mismatched SSL configurations or missing PSA peering will still block clients.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Expect a post-promotion PITR window:&lt;/strong&gt; Verify that the new primary’s baseline backup and PITR log archival are active before declaring recovery complete.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Document your service’s maximum acceptable lost transactions (RPO) and maximum acceptable writer outage (RTO) separately for a full regional failure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Choose between RDS cross-region read replicas, Cloud SQL Enterprise replicas, or Cloud SQL Enterprise Plus Advanced DR, and pair the choice with an explicit fencing and DNS routing design.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run a controlled recovery drill with sequentially numbered writes logged outside the primary region and reconcile the exact gap on the promoted instance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Verify IAM promotion permissions, SSL/network parity, and backup retention settings across both regions, and assign explicit owners for promotion, reconciliation, and failback.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ReadRepl.html&quot;&gt;AWS read replicas and promotion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PostgreSQL.Replication.ReadReplicas.html&quot;&gt;AWS PostgreSQL read replica behavior&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_WorkingWithAutomatedBackups.html&quot;&gt;AWS RDS automated backups and cross-region replication&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/replication&quot;&gt;Cloud SQL replication&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/use-advanced-disaster-recovery&quot;&gt;Cloud SQL advanced disaster recovery&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/intro-to-cloud-sql-disaster-recovery&quot;&gt;Cloud SQL DR and write endpoints&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>failures</category></item><item><title>RDS vs. Cloud SQL PostgreSQL: What Happens When a Zone Fails?</title><link>https://rajivonai.com/blog/2026-10-01-rds-cloud-sql-postgresql-high-availability/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-10-01-rds-cloud-sql-postgresql-high-availability/</guid><description>Compare PostgreSQL high availability on Amazon RDS and Cloud SQL, from replication and failover to application recovery.</description><pubDate>Thu, 01 Oct 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A database failover can succeed while the application remains unavailable: existing sessions are gone, clients may hold stale addresses, and an interrupted transaction can leave its outcome uncertain.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; “Multi-zone” hides incompatible replication paths—storage-level sync versus engine-level semisync—and client pools can turn a 35–120 second provider failover into a multi-minute outage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Match the topology to your read-scaling and failover-speed requirements, then bound client recovery with short DNS TTLs (5 seconds or less on RDS), pool pre-ping validation, and idempotent write reconciliation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Neither RDS Multi-AZ standby nor Cloud SQL Regional HA standby serves read traffic, and Cloud SQL handles memory or disk exhaustion with an in-place restart rather than a zone failover.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-01-rds-cloud-sql-ha.svg&quot; alt=&quot;Comparison of three PostgreSQL high availability topologies&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;An enterprise running PostgreSQL on Amazon RDS often starts with a Multi-AZ DB instance: one writer, one synchronous standby in a second Availability Zone, and an application connecting through a single RDS endpoint. A move to Google Cloud—or a multi-cloud architecture standard—raises a deceptively simple question: is Cloud SQL Regional HA the same architecture under another name?&lt;/p&gt;
&lt;p&gt;Both configurations protect against a primary compute failure or an Availability Zone outage, and both carry a 99.95% monthly uptime SLA on their standard tiers (rising to 99.99% on Cloud SQL Enterprise Plus). Yet their underlying replication boundaries, readable-standby behavior, DNS versus IP cutover mechanics, and maintenance interactions differ. Those differences matter the moment a design includes read scaling, strict recovery time objectives (RTO), or automated application retry logic.&lt;/p&gt;
&lt;p&gt;This comparison covers Amazon RDS for PostgreSQL (Multi-AZ DB instance and Multi-AZ DB cluster) and Google Cloud SQL for PostgreSQL (Enterprise and Enterprise Plus with Regional HA). Amazon Aurora and Google AlloyDB use distributed storage architectures and are outside the scope of this series.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The phrase “multi-zone database” conceals four independent operational questions. First, is the standby a running, readable PostgreSQL instance or a passive failover target? Second, does a transaction commit wait for another database engine or for a replicated block-storage volume? Third, how do clients discover the new writer after promotion? Fourth, does a separate read replica participate in automatic primary failover?&lt;/p&gt;
&lt;p&gt;Four concrete constraints govern whether a failover meets its availability target:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Monthly downtime budget:&lt;/strong&gt; A 99.95% monthly uptime SLA permits at most &lt;code&gt;0.0005 × 43,200 = 21.6&lt;/code&gt; minutes of total unavailability in a 30-day month. A 99.99% SLA (Cloud SQL Enterprise Plus) narrows that budget to &lt;code&gt;4.32&lt;/code&gt; minutes per month.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Provider failover interval:&lt;/strong&gt; AWS documents typical failover times of 60–120 seconds for an RDS Multi-AZ DB instance and under 35 seconds for an RDS Multi-AZ DB cluster. Google Cloud documents roughly 60 seconds of unavailability as an expectation for Cloud SQL Enterprise Regional HA, with Enterprise Plus targeting faster recovery and under 1 second of connectivity interruption for eligible planned maintenance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;DNS caching and socket timeouts:&lt;/strong&gt; AWS updates the RDS endpoint CNAME record with a 5-second DNS TTL, yet a default Java Virtual Machine (JVM) without explicit security overrides caches DNS lookups indefinitely until restart. Meanwhile, half-open TCP sockets in a connection pool wait for OS or driver read timeouts (often 30–60 seconds) unless evicted by health checks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Total outage math:&lt;/strong&gt; Even if an RDS Multi-AZ DB instance fails over in 90 seconds, a 45-second socket read timeout plus a misconfigured 300-second DNS cache pushes application recovery to &lt;code&gt;90 + 45 + 300 = 435&lt;/code&gt; seconds (&lt;code&gt;7.25&lt;/code&gt; minutes)—consuming one-third of a 99.95% monthly budget in a single event.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-01-rds-cloud-sql-postgresql-high-availability-2-constraints.svg&quot; alt=&quot;Application outage equals provider failover plus client recovery&quot;&gt;&lt;/p&gt;
&lt;p&gt;A team that assumes every secondary is both readable and eligible for automatic promotion can under-provision its readers or write a recovery plan that never executes. A team that assumes the database endpoint preserves existing connections will discover its connection pool keeps issuing requests to dead sockets.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What exactly is protected, and what must the application do, when a zone fails?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Selecting a PostgreSQL HA topology requires balancing failover speed, read-scaling coupling, and monthly SLA targets. Single-AZ deployments are rejected for production writers because they provide no automatic zone failover and carry no multi-zone SLA.&lt;/p&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Option&lt;/th&gt;&lt;th&gt;Replication Path&lt;/th&gt;&lt;th&gt;Typical Failover&lt;/th&gt;&lt;th&gt;Monthly SLA&lt;/th&gt;&lt;th&gt;Readable Standby&lt;/th&gt;&lt;th&gt;Why Chosen or Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Single-AZ Instance&lt;/td&gt;&lt;td&gt;None (local zone only)&lt;/td&gt;&lt;td&gt;Manual restore&lt;/td&gt;&lt;td&gt;Lower tier&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Rejected for production; single zone failure causes extended outage&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;RDS Multi-AZ DB Instance&lt;/td&gt;&lt;td&gt;Sync block/instance to 1 standby&lt;/td&gt;&lt;td&gt;60–120 seconds&lt;/td&gt;&lt;td&gt;99.95%&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Chosen for simple 2-AZ HA when read offload is separate or unneeded&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;RDS Multi-AZ DB Cluster&lt;/td&gt;&lt;td&gt;Semisync engine WAL to 2 readers&lt;/td&gt;&lt;td&gt;Under 35 seconds&lt;/td&gt;&lt;td&gt;99.95%&lt;/td&gt;&lt;td&gt;Yes (2 readers)&lt;/td&gt;&lt;td&gt;Chosen when sub-minute failover and built-in read scaling justify 3-AZ cost&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cloud SQL Enterprise HA&lt;/td&gt;&lt;td&gt;Sync Regional Persistent Disk&lt;/td&gt;&lt;td&gt;~60 seconds&lt;/td&gt;&lt;td&gt;99.95%&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Chosen for standard GCP zonal HA with preserved endpoint IP&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cloud SQL Enterprise Plus HA&lt;/td&gt;&lt;td&gt;Sync Regional Persistent Disk&lt;/td&gt;&lt;td&gt;Faster than Enterprise&lt;/td&gt;&lt;td&gt;99.99%&lt;/td&gt;&lt;td&gt;No (use read pools)&lt;/td&gt;&lt;td&gt;Chosen when 99.99% SLA and &amp;#x3C;1s planned maintenance justify tier cost&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-01-rds-cloud-sql-postgresql-high-availability-3-options.svg&quot; alt=&quot;HA options trade failover speed, readable standbys, and SLA&quot;&gt;&lt;/p&gt;
&lt;p&gt;Choosing between RDS Multi-AZ DB instance and Multi-AZ DB cluster comes down to whether you want read scaling coupled to your HA quorum. A Multi-AZ DB cluster runs three full compute instances across three AZs and requires at least one reader to acknowledge WAL before commit. If your workload does not need two readable standbys, paying for three compute nodes adds cost without benefit. Conversely, on Google Cloud, Cloud SQL Enterprise Plus separates HA from read scaling: you pay for Regional HA to protect the writer and optionally add read-pool nodes when read concurrency demands it.&lt;/p&gt;
&lt;h2 id=&quot;three-ha-architectures-three-different-paths&quot;&gt;Three HA Architectures, Three Different Paths&lt;/h2&gt;
&lt;p&gt;The three multi-zone topologies protect against zone loss through distinct control-plane and storage mechanisms. The schematic below compares their logical protection paths; it does not imply identical commit protocols.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-01-rds-cloud-sql-ha.svg&quot; alt=&quot;Comparison of three PostgreSQL high availability topologies&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph A[Amazon RDS — Multi-AZ DB instance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        A1[Zone A — writer] --&gt;|synchronous standby| A2[Zone B — non-readable standby]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph B[Amazon RDS — Multi-AZ DB cluster]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        B1[Zone A — writer] --&gt;|semisynchronous| B2[Zone B — readable standby]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        B1 --&gt;|semisynchronous| B3[Zone C — readable standby]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph C[Cloud SQL — Regional HA]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        C1[Zone A — primary compute] --&gt; C2[Regional storage — synchronous copies across zones]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        C2 --&gt; C3[Zone B — standby compute]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;rds-multi-az-db-instance&quot;&gt;RDS Multi-AZ DB instance&lt;/h3&gt;
&lt;p&gt;In an RDS Multi-AZ DB instance deployment, AWS provisions a primary instance in one Availability Zone and a synchronous standby in a second AZ. The standby exists strictly for availability; it does not accept client connections or serve read queries. When a qualifying outage occurs—such as primary host failure, AZ loss, or storage failure—RDS promotes the standby and updates the database endpoint’s DNS CNAME record to point to the standby’s IP address.&lt;/p&gt;
&lt;p&gt;AWS documents failover times of 60–120 seconds for Multi-AZ DB instances, though large uncommitted transactions or crash recovery can extend that window. Because clients resolve a DNS CNAME, DNS TTL handling and connection-pool eviction directly determine when application traffic resumes. A separate RDS read replica can offload reads, but it uses asynchronous replication and is not an automatic HA failover target.&lt;/p&gt;
&lt;h3 id=&quot;rds-multi-az-db-cluster&quot;&gt;RDS Multi-AZ DB cluster&lt;/h3&gt;
&lt;p&gt;An RDS Multi-AZ DB cluster deploys one writer DB instance and two readable standby DB instances across three Availability Zones. Unlike a traditional Multi-AZ instance, the cluster uses engine-native semisynchronous replication: a write transaction commits once at least one of the two reader instances acknowledges receipt of the WAL record. Because only one reader must acknowledge before commit, the second reader may trail slightly behind.&lt;/p&gt;
&lt;p&gt;During failover, RDS selects the reader with the lowest replica lag, finishes applying any queued transactions, and promotes it to writer—typically completing in under 35 seconds. The cluster exposes both a writer endpoint and a reader endpoint. However, because a reader can serve state behind the writer even while participating in HA, applications must not infer read-your-writes consistency from the reader endpoint.&lt;/p&gt;
&lt;h3 id=&quot;cloud-sql-regional-ha&quot;&gt;Cloud SQL Regional HA&lt;/h3&gt;
&lt;p&gt;Cloud SQL Regional HA deploys a primary VM in one zone and a standby VM in a secondary zone backed by Regional Persistent Disk, which replicates block writes synchronously across both zones before acknowledging a commit. The standby VM remains idle until failover and cannot serve read traffic. When the primary becomes unresponsive, Cloud SQL activates the standby VM against the shared regional disk and rebinds the instance’s primary IP address—typically causing about 60 seconds of unavailability on Enterprise Edition, with faster recovery on Enterprise Plus.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-01-rds-cloud-sql-postgresql-high-availability-4-architecture.svg&quot; alt=&quot;Storage-level sync differs from engine-level semisync replication&quot;&gt;&lt;/p&gt;
&lt;p&gt;A separate Cloud SQL read replica—even one placed in a third zone—uses asynchronous replication and does not become an automatic HA failover target for the primary. You can enable HA on a read replica to protect the replica’s own uptime, but that does not alter the primary’s promotion path.&lt;/p&gt;
&lt;p&gt;To enforce clean client recovery and secure transport on RDS, configure JVM DNS caching to match AWS’s 5-second TTL and require TLS on cluster parameter groups, while enabling Regional HA explicitly on Cloud SQL:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Set before initializing connection pools in Java services targeting RDS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;java.security.Security.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;setProperty&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;networkaddress.cache.ttl&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;5&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;java.security.Security.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;setProperty&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;networkaddress.cache.negative.ttl&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;10&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Enable Regional HA on an existing Cloud SQL for PostgreSQL instance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;gcloud&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; sql&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; instances&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; patch&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; my-pg-instance&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --availability-type=REGIONAL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;A database failover has four sequential stages, and the provider only controls the first two. Treating provider promotion as the finish line leaves applications vulnerable to hung threads and duplicate writes.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-01-rds-cloud-sql-postgresql-high-availability-5-flow.svg&quot; alt=&quot;Failover completes only after client pools reconnect and reconcile&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Detect and promote (Provider control plane):&lt;/strong&gt; Health checks detect primary unavailability. On RDS Multi-AZ DB instance, the standby mounts storage and runs PostgreSQL crash recovery. On RDS Multi-AZ DB cluster, the lowest-lag reader applies remaining WAL and promotes. On Cloud SQL Regional HA, the standby compute instance attaches to the synchronized Regional Persistent Disk and starts PostgreSQL.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rebind endpoint (Provider network):&lt;/strong&gt; RDS updates the CNAME record for the writer endpoint with a 5-second TTL. Cloud SQL reattaches the instance’s static IP address or Cloud SQL Auth Proxy identity to the newly active standby zone and terminates all prior TCP sessions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reconnect pool (Application runtime):&lt;/strong&gt; Application connection pools detect broken sockets via pre-ping validation queries (such as &lt;code&gt;SELECT 1&lt;/code&gt;) or short socket read timeouts, evict dead connections, re-resolve DNS if on RDS, and establish fresh TLS sessions. Placing Amazon RDS Proxy or PgBouncer in front of the database can absorb connection storms during this step, though proxy-side health checks still add a brief transition window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reconcile writes (Application logic):&lt;/strong&gt; Any transaction that was in flight when the primary died returns a connection reset or timeout to the caller. Because the commit may have succeeded on the old primary just before the socket broke, the application must query an idempotency record or durable operation log before retrying state-changing writes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To validate this sequence before production cutover, roll out HA testing in three gated stages: first, trigger a manual failover in staging and verify that DNS TTL and pool pre-ping evict dead sockets within 15 seconds of provider promotion; second, inject an in-flight write during failover to confirm idempotency reconciliation prevents duplicate records; third, run a controlled failover during a low-traffic production window and record the exact timestamp of the last good write, first error, endpoint switch, and first successful business transaction. If client recovery exceeds your RTO budget at any gate, tighten pool timeouts or introduce a connection proxy before proceeding.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented Cloud SQL failover sequence illustrates why monitoring must distinguish infrastructure failovers from engine restarts. When a Cloud SQL Regional HA primary becomes unresponsive due to a host or zone failure, health checks trigger failover and the standby begins serving from the secondary zone. Once the original zone recovers, the former primary is recreated as the new standby; returning the primary role to the original zone requires an explicit failback operation. However, Google documents that resource exhaustion events—such as out-of-memory (OOM) crashes from unbounded &lt;code&gt;work_mem&lt;/code&gt; or runaway connection counts—can trigger an in-place instance restart rather than a zone failover. Alerts that assume every outage produces a zone-change event will misclassify OOM restarts.&lt;/p&gt;
&lt;p&gt;On Amazon RDS, documented DNS behavior dictates how runtime validation must be designed. Because AWS updates a CNAME record with a 5-second TTL, testing failover only from a &lt;code&gt;psql&lt;/code&gt; CLI session misses runtime-specific DNS caches in Java, Node.js, or sidecar proxies. Testing must occur from the actual application container and runtime, recording five timestamps: last successful write, first client error, DNS resolution change, first successful reconnect, and first completed business transaction. The gap between provider recovery and application recovery is almost always where unconfigured caches or pool timeouts surface.&lt;/p&gt;
&lt;p&gt;Finally, PostgreSQL transaction semantics create a hard boundary during any abrupt failover: if a client issues &lt;code&gt;COMMIT&lt;/code&gt; and loses the TCP connection before receiving the &lt;code&gt;CommandComplete&lt;/code&gt; response, the client cannot know whether the server committed the transaction to WAL (and replicated it to the standby) before dying. Reconnection alone never resolves this ambiguity; durable idempotency keys are mandatory for payments, ledger entries, and order creation.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Even well-architected HA deployments fail when client behavior, engine resource limits, or regional boundaries are misunderstood.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-10-01-rds-cloud-sql-postgresql-high-availability-6-failures.svg&quot; alt=&quot;Three client and engine traps turn brief failovers into outages&quot;&gt;&lt;/p&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Condition&lt;/th&gt;&lt;th&gt;Breaks How&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Stale DNS cache on RDS&lt;/td&gt;&lt;td&gt;Runtime continues dialing retired primary IP after CNAME flip&lt;/td&gt;&lt;td&gt;Connection timeouts persist &gt;120s after RDS failover event&lt;/td&gt;&lt;td&gt;Set JVM &lt;code&gt;networkaddress.cache.ttl=5&lt;/code&gt; or route via RDS Proxy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Dead sockets in pool&lt;/td&gt;&lt;td&gt;Pool hands half-open TCP connections to callers until OS timeout&lt;/td&gt;&lt;td&gt;Spike in read-timeout errors across multiple app pods&lt;/td&gt;&lt;td&gt;Enable &lt;code&gt;pool_pre_ping&lt;/code&gt; / validation queries and bound socket timeouts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unknown commit outcome&lt;/td&gt;&lt;td&gt;Blind retry of timed-out &lt;code&gt;COMMIT&lt;/code&gt; duplicates business action&lt;/td&gt;&lt;td&gt;Duplicate rows or payment charges following failover&lt;/td&gt;&lt;td&gt;Require idempotency keys and reconcile before retrying writes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Stale reads on RDS Cluster&lt;/td&gt;&lt;td&gt;Reader endpoint serves lagging standby state after write&lt;/td&gt;&lt;td&gt;Read-after-write anomalies in user workflows&lt;/td&gt;&lt;td&gt;Route strict freshness reads to the writer endpoint&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OOM or disk exhaustion&lt;/td&gt;&lt;td&gt;Cloud SQL restarts in place rather than failing over to standby&lt;/td&gt;&lt;td&gt;Instance restart logs without zone-change event&lt;/td&gt;&lt;td&gt;Cap &lt;code&gt;max_connections&lt;/code&gt;, bound &lt;code&gt;work_mem&lt;/code&gt;, alert at 85% memory&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Standby or secondary unhealthy&lt;/td&gt;&lt;td&gt;Secondary zone degradation blocks automatic promotion&lt;/td&gt;&lt;td&gt;Provider HA health status degrades to non-redundant&lt;/td&gt;&lt;td&gt;Monitor HA replication health alerts and repair promptly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Read replica mistaken for HA&lt;/td&gt;&lt;td&gt;Team expects async read replica to auto-promote on primary loss&lt;/td&gt;&lt;td&gt;Runbook stalls waiting for replica promotion that never fires&lt;/td&gt;&lt;td&gt;Document exact HA failover path separately from read replicas&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Entire region unavailable&lt;/td&gt;&lt;td&gt;Both primary and standby zones fail together&lt;/td&gt;&lt;td&gt;Regional control-plane and multi-zone connectivity loss&lt;/td&gt;&lt;td&gt;Implement cross-region disaster recovery (covered in Part 2)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;HA topology and read scaling are separate decisions:&lt;/strong&gt; RDS Multi-AZ DB instances and Cloud SQL Regional HA use non-readable standbys; only RDS Multi-AZ DB clusters couple HA with two readable standbys across three AZs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Client recovery dominates total outage time:&lt;/strong&gt; Provider failover takes 35–120 seconds, but unconfigured DNS caches and unvalidated connection pools can stretch application downtime well past your 99.95% monthly budget.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cloud SQL OOM events restart in place:&lt;/strong&gt; Exhausting memory or local resources triggers a local restart rather than a standby failover; bound PostgreSQL memory parameters and monitor utilization before it hits 90%.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Semisynchronous clusters can serve stale reads:&lt;/strong&gt; An RDS Multi-AZ DB cluster commits after one reader acknowledges WAL, meaning the second reader can lag behind recent writes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reconnection does not resolve in-flight commits:&lt;/strong&gt; Any &lt;code&gt;COMMIT&lt;/code&gt; interrupted by a socket drop has an unknown outcome and requires application-level idempotency keys before retry.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Identify every application workflow that cannot tolerate duplicate writes or 60–120 seconds of write unavailability during a zone failure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Select the HA topology that matches your SLA and read-scaling needs, then configure DNS TTLs (&amp;#x3C;=5 seconds on RDS), pool pre-ping validation, and idempotency keys.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Trigger a controlled failover in staging from the actual application runtime and measure the interval from the last successful write to the first successful business transaction.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Document the exact endpoint behavior, DNS/pool settings, OOM restart distinction, and write reconciliation steps in your service runbook this week.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.MultiAZSingleStandby.html&quot;&gt;AWS Multi-AZ DB instance deployments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.MultiAZ.Failover.html&quot;&gt;AWS Multi-AZ DB instance failover&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/multi-az-db-clusters-concepts.html&quot;&gt;AWS Multi-AZ DB clusters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://aws.amazon.com/rds/sla/&quot;&gt;Amazon RDS Service Level Agreement&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/high-availability&quot;&gt;Cloud SQL high availability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/sla&quot;&gt;Cloud SQL Service Level Agreement&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/sql/docs/postgres/replication&quot;&gt;Cloud SQL replication&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>failures</category></item><item><title>From Cassandra to Bigtable, DynamoDB or MongoDB: When Modernization Is Worth It</title><link>https://rajivonai.com/blog/2026-09-26-cassandra-modernization-when-migration-is-worth-it/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-09-26-cassandra-modernization-when-migration-is-worth-it/</guid><description>A correctness-first framework for upgrading Cassandra or migrating to a managed database through ordered change capture, idempotent apply, and evidence-based cutover.</description><pubDate>Sat, 26 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Moving petabytes is not trivial, but copying bytes is still easier than preserving ordering, deletes, conditional writes, TTL semantics, and query behavior while two databases disagree.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Legacy Cassandra estates carry repair, compaction, JVM, disk, backup, and upgrade work. Managed alternatives can transfer some of that burden to a provider, but they also impose different data models and failure contracts. Cassandra 5.0 offers Unified Compaction Strategy, Storage-Attached Indexing, and BTI-format SSTables, but an older estate must follow the supported rolling-upgrade path and intermediate-version requirements. Migration should be compared with a tested modernization plan, not with an intentionally neglected source cluster.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A Cassandra partition is not one DynamoDB item. It is a set of clustering rows under one partition key and usually maps to multiple DynamoDB items under a shared partition key and sort keys. Bigtable orders keys lexicographically rather than hashing them. MongoDB maintains document indexes and routes through its shard key. Those differences affect pagination, ordering, atomicity, hotspots, TTLs, and cost.&lt;/p&gt;
&lt;p&gt;The decision is therefore not “managed or self-managed.” It is whether the target can reproduce each required access pattern and correctness invariant at lower lifecycle risk.&lt;/p&gt;
&lt;h2 id=&quot;decide-before-moving-data&quot;&gt;Decide Before Moving Data&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Evidence&lt;/th&gt;&lt;th&gt;Modernize Cassandra when&lt;/th&gt;&lt;th&gt;Migrate when&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Access patterns&lt;/td&gt;&lt;td&gt;Existing CQL model fits and rewrite value is low&lt;/td&gt;&lt;td&gt;Target model materially simplifies required queries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Operational burden&lt;/td&gt;&lt;td&gt;Upgrade, automation, and staffing are tractable&lt;/td&gt;&lt;td&gt;Repair, upgrades, and capacity work remain a strategic burden&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Portability&lt;/td&gt;&lt;td&gt;Multi-cloud or infrastructure control is required&lt;/td&gt;&lt;td&gt;Provider coupling is accepted explicitly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Correctness gap&lt;/td&gt;&lt;td&gt;Cassandra semantics are deeply embedded&lt;/td&gt;&lt;td&gt;Semantics can be mapped and tested per access pattern&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Economics&lt;/td&gt;&lt;td&gt;Measured upgraded cluster wins at required headroom&lt;/td&gt;&lt;td&gt;Measured target configuration wins including migration and egress&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;No write-rate threshold answers this matrix.&lt;/p&gt;
&lt;h2 id=&quot;a-correct-migration-architecture&quot;&gt;A Correct Migration Architecture&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[&quot;Application mutation with immutable operation identity&quot;] --&gt; S[&quot;Cassandra system of record&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; L[&quot;Durable ordered change log&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt; C[&quot;Bounded idempotent consumers&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; T[&quot;Target database&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    S --&gt; B[&quot;Checkpointed historical backfill&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; T&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    S --&gt; V[&quot;Version-aware parity verifier&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    T --&gt; V&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    V --&gt; G{&quot;Cutover gates satisfied&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|No| R[&quot;Repair divergence and continue&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|Yes| P[&quot;Canary reads then reversible traffic shift&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The application commits the source mutation and a durable change record using a design that cannot silently lose the second step—for example, an existing transactional outbox boundary or Cassandra CDC with a validated delivery pipeline. Each record carries &lt;code&gt;operation_id&lt;/code&gt;, entity key, monotonic domain version, operation type, event time, and schema version. Consumers are bounded, retryable, and idempotent; the target stores the last applied version and rejects stale reordering.&lt;/p&gt;
&lt;p&gt;Backfill reads immutable checkpoints and emits the same canonical mutation format. It must not overwrite a newer streamed mutation: apply only when the incoming version is newer. Deletes and TTL expiry require explicit representation; absence is not proof of deletion.&lt;/p&gt;
&lt;h3 id=&quot;safe-comparator-pattern&quot;&gt;Safe comparator pattern&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dataclasses &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dataclass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; decimal &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Decimal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; hashlib &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sha256&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; typing &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Mapping&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;@dataclass&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;frozen&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; CanonicalRecord&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    version: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    status: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    balance: Decimal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; canonicalize&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(row: Mapping[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]) -&gt; CanonicalRecord:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CanonicalRecord(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        version&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(row[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;version&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(row[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;status&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        balance&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Decimal(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(row[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balance&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; fingerprint&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(record: CanonicalRecord) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    payload &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json.dumps(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &quot;balance&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(record.balance.normalize(), &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;f&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &quot;status&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: record.status,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &quot;version&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: record.version,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        ensure_ascii&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        separators&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;:&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        sort_keys&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sha256(payload.encode(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)).hexdigest()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; equal_at_version&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(source: Mapping[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], target: Mapping[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    left &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; canonicalize(source)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    right &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; canonicalize(target)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; left.version &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; right.version &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fingerprint(left) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fingerprint(right)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Production comparison workers should use bounded executors or genuinely asynchronous database clients—never unbounded &lt;code&gt;create_task&lt;/code&gt; calls around blocking drivers. Emit keys as keyed hashes, not raw customer identifiers or field values. Use exact decimal or canonical integer units for money, not floats. Track mismatch class and age without logging PII.&lt;/p&gt;
&lt;p&gt;Cutover gates are workload-derived: zero unexplained mismatches for all sampled mutation classes, bounded replication age, completed delete and TTL tests, successful restore, target capacity under a failure domain, and rollback proven in rehearsal. An arbitrary “99.999% parity” is not acceptable when the missing fraction may contain deletes or financial records.&lt;/p&gt;
&lt;h2 id=&quot;target-specific-translation&quot;&gt;Target-Specific Translation&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;DynamoDB&lt;/strong&gt;: decompose clustering rows into items, preserve ordering in the sort key, model the 400 KB item limit, base and GSI capacity, conditional writes, and warm throughput. &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Constraints.html&quot;&gt;DynamoDB constraints&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bigtable&lt;/strong&gt;: preserve query locality without timestamp-first hotspots. Google recommends query-driven row keys and warns against sequential prefixes. &lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/schema-design&quot;&gt;Bigtable schema design&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MongoDB&lt;/strong&gt;: choose a shard key from routing and distribution requirements; model index maintenance, document growth, transaction scope, oplog window, and balancer behavior. &lt;a href=&quot;https://www.mongodb.com/docs/manual/sharding/&quot;&gt;MongoDB sharding&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cassandra 5.0&lt;/strong&gt;: upgrade separately from application redesign where possible. Apache’s binary releases are built with Java 11 and can run on Java 11 or 17; builds produced with Java 17 cannot run on Java 11. Pin the exact distribution and JDK combination, follow the supported upgrade path, and validate snapshots before rewriting SSTables. &lt;a href=&quot;https://cassandra.apache.org/doc/latest/cassandra/reference/java17.html&quot;&gt;Apache Cassandra Java support&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Use public cases only for what they document. Discord published its migration from Cassandra to ScyllaDB and the application-side work required; it is evidence for a compatibility-oriented modernization path. &lt;a href=&quot;https://discord.com/blog/how-discord-stores-trillions-of-messages&quot;&gt;Discord engineering&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Intuit’s documented AWS case is a migration of a 120 TB, 66-node Cassandra workload to &lt;strong&gt;Amazon Keyspaces&lt;/strong&gt;, not DynamoDB. It supports the value of a Cassandra-compatible managed target; it does not prove DynamoDB schema translation or Global Tables behavior. &lt;a href=&quot;https://aws.amazon.com/solutions/case-studies/intuit-apache-migration-case-study/&quot;&gt;Intuit and Amazon Keyspaces&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Spotify’s published engineering material documents Cassandra use and broader infrastructure modernization, but the sources reviewed do not establish the specific Cassandra-to-Bigtable migration narrative previously attributed to Spotify. That claim should remain excluded unless a primary source appears.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Mechanism&lt;/th&gt;&lt;th&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Lost dual write&lt;/td&gt;&lt;td&gt;Process fails between independent writes&lt;/td&gt;&lt;td&gt;Durable log or outbox boundary; lag alerting&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reordered apply&lt;/td&gt;&lt;td&gt;Retry or backfill arrives after newer state&lt;/td&gt;&lt;td&gt;Monotonic version and conditional target update&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Delete resurrection&lt;/td&gt;&lt;td&gt;Backfill treats absence as state&lt;/td&gt;&lt;td&gt;Explicit tombstone or delete event with version&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Comparator leak&lt;/td&gt;&lt;td&gt;Logs include raw customer data&lt;/td&gt;&lt;td&gt;Keyed hashes, redaction, restricted mismatch store&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unbounded shadow work&lt;/td&gt;&lt;td&gt;Blocking clients exhaust threads or tasks&lt;/td&gt;&lt;td&gt;Bounded pools, timeouts, cancellation, sampling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Premature cutover&lt;/td&gt;&lt;td&gt;Aggregate counts hide semantic mismatches&lt;/td&gt;&lt;td&gt;Per-class gates, canary reads, reversible routing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Early decommission&lt;/td&gt;&lt;td&gt;Rollback data and evidence disappear&lt;/td&gt;&lt;td&gt;Retain source through the audited rollback window&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Migration estimates count bytes but omit semantic and correctness work.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Compare in-place modernization with target-specific translation, then migrate through an ordered durable log.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Demonstrate idempotency, reordering safety, delete and TTL handling, restoration, failed-zone capacity, and rollback.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Inventory every CQL access pattern and invariant; reject any target design that cannot map each one explicitly.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category></item><item><title>The 2 AM NoSQL Incident: Failure Modes Across Bigtable, DynamoDB, Cassandra and MongoDB</title><link>https://rajivonai.com/blog/2026-09-25-2am-nosql-incident-failure-modes/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-09-25-2am-nosql-incident-failure-modes/</guid><description>A safe, evidence-first incident runbook for throttling, replica loss, elections, hot ranges, retry storms, and recovery across four NoSQL engines.</description><pubDate>Fri, 25 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The first job in a distributed-database incident is to stop amplification and preserve evidence—not to change consistency, disable repair, or pause balancing on instinct.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The alert says “database latency,” but the cause may be client retries, a hot key, lost quorum, an election, storage saturation, or data movement competing with foreground work. Each engine exposes different ownership and replication boundaries. A cross-engine runbook should standardize decisions without pretending their commands are interchangeable.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Unsafe incident advice often looks decisive: retry faster, lower Cassandra &lt;code&gt;gc_grace_seconds&lt;/code&gt;, stop the MongoDB balancer, salt every key, or restart a slow node. Each can destroy evidence, increase load, or create correctness risk.&lt;/p&gt;
&lt;p&gt;The core question is: can the team identify the failing boundary, reduce demand reversibly, and protect durability before attempting engine-specific recovery?&lt;/p&gt;
&lt;h2 id=&quot;the-first-fifteen-minutes&quot;&gt;The First Fifteen Minutes&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[&quot;Page received&quot;] --&gt; B[&quot;Freeze deploys and name incident roles&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[&quot;Capture UTC window, client errors, latency, and demand&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D{&quot;Retry or traffic amplification&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Yes| E[&quot;Bound retries, concurrency, and low-priority traffic&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|No| F[&quot;Continue evidence capture&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; G[&quot;Identify hot key, quorum loss, election, or resource saturation&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[&quot;Apply one reversible mitigation&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[&quot;Verify errors, tail latency, backlog, and correctness&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;Record an explicit UTC start and end time using portable ISO-8601 timestamps supplied by the incident commander; do not embed platform-specific &lt;code&gt;date&lt;/code&gt; syntax in the runbook.&lt;/li&gt;
&lt;li&gt;Freeze unrelated deploys and configuration changes.&lt;/li&gt;
&lt;li&gt;Capture client request rate, retry rate, error codes, p50/p99 latency, backlog age, and recent topology changes.&lt;/li&gt;
&lt;li&gt;Apply admission control before scaling retries. Preserve a small diagnostic traffic lane.&lt;/li&gt;
&lt;li&gt;Make one reversible change at a time and annotate the timeline.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;engine-specific-evidence-and-safe-actions&quot;&gt;Engine-Specific Evidence and Safe Actions&lt;/h2&gt;
&lt;h3 id=&quot;dynamodb&quot;&gt;DynamoDB&lt;/h3&gt;
&lt;p&gt;Separate throttling from service errors and client timeouts. Inspect table and index consumption, throttled events, account and table quotas, and key contributors. On-demand mode is elastic but has warm-throughput rules: a jump above twice the previous peak inside 30 minutes may throttle. &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/on-demand-capacity-mode.html&quot;&gt;AWS on-demand capacity&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Safe first actions are bounded exponential backoff with jitter, concurrency reduction, low-priority shedding, and targeted pre-warming or quota work when the evidence supports it. Do not state a fixed internal leader-failover duration; DynamoDB does not expose such a universal SLO as an application runbook primitive.&lt;/p&gt;
&lt;h3 id=&quot;cassandra&quot;&gt;Cassandra&lt;/h3&gt;
&lt;p&gt;Capture &lt;code&gt;nodetool status&lt;/code&gt;, &lt;code&gt;nodetool describecluster&lt;/code&gt;, &lt;code&gt;nodetool tpstats&lt;/code&gt;, &lt;code&gt;nodetool proxyhistograms&lt;/code&gt;, &lt;code&gt;nodetool compactionstats&lt;/code&gt;, table histograms, disk and network saturation, GC logs, and client consistency levels. Distinguish an unavailable error from a timeout: one says insufficient live replicas were known; the other says acknowledgements did not arrive before the deadline.&lt;/p&gt;
&lt;p&gt;Do not lower &lt;code&gt;gc_grace_seconds&lt;/code&gt; during an incident. Tombstones protect against deleted data returning from a replica that missed the delete; Apache Cassandra documents that a node absent beyond the grace period can reintroduce data. &lt;a href=&quot;https://cassandra.apache.org/doc/latest/cassandra/managing/operating/compaction/tombstones.html&quot;&gt;Cassandra tombstones&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Snapshots alone are not point-in-time recovery. Commit-log replay requires commit-log archiving and restore configuration established and tested before the incident. &lt;a href=&quot;https://cassandra.apache.org/doc/latest/cassandra/managing/operating/backups.html&quot;&gt;Cassandra backup and restore&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;mongodb&quot;&gt;MongoDB&lt;/h3&gt;
&lt;p&gt;Inspect replica-set state, election and replication logs, replication lag, oplog window, &lt;code&gt;currentOp&lt;/code&gt;, shard availability, config-server health, balancer state, and range deletion. An election may temporarily reject writes, but duration depends on configuration and conditions.&lt;/p&gt;
&lt;p&gt;Do not stop the balancer by default. MongoDB documents it as the background mechanism that restores range balance; pausing it is justified only when active migration is demonstrably competing with recovery, and the incident timeline must include when to resume it. &lt;a href=&quot;https://www.mongodb.com/docs/manual/core/sharding-balancer-administration/&quot;&gt;MongoDB balancer administration&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;bigtable&quot;&gt;Bigtable&lt;/h3&gt;
&lt;p&gt;Inspect request latency, errors, CPU load, node count and autoscaling state, key distribution, Key Visualizer, and hot-tablet evidence. Bigtable exposes tablet ranges and cluster metrics; avoid runbooks that presume direct control of a named worker. &lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/hot-tablets&quot;&gt;Bigtable hot-tablet diagnostics&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If a key range is hot, shed or coalesce requests before changing row-key design. A schema change needs dual-write and backfill; it is rarely a safe 2 AM mutation.&lt;/p&gt;
&lt;h2 id=&quot;recovery-and-verification&quot;&gt;Recovery and Verification&lt;/h2&gt;
&lt;p&gt;A falling error rate is not enough. Verify:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;original request rate, retry ratio, and queue age are converging;&lt;/li&gt;
&lt;li&gt;p99 latency stays within objective for at least one workload cycle;&lt;/li&gt;
&lt;li&gt;replica and topology state are stable;&lt;/li&gt;
&lt;li&gt;no acknowledged writes were lost and duplicate handling remains correct;&lt;/li&gt;
&lt;li&gt;deferred repairs, balancing, compaction, or backfill have an owner and capacity budget;&lt;/li&gt;
&lt;li&gt;temporary traffic controls have explicit removal criteria.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Restore traffic in steps. If a retry queue exists, cap replay so it cannot starve current traffic. Reconcile by immutable event ID and mutation version, not by approximate counts alone.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;This runbook derives from documented behavior: DynamoDB publishes warm-throughput rules; Cassandra documents tombstone safety and backup mechanisms; MongoDB documents range balancing; Bigtable provides hot-tablet diagnostics. It intentionally does not invent universal failover seconds or prescribe destructive commands without topology-specific evidence.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Response error&lt;/th&gt;&lt;th&gt;Why it is dangerous&lt;/th&gt;&lt;th&gt;Guardrail&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Unbounded retries&lt;/td&gt;&lt;td&gt;Multiplies demand on the failing owner&lt;/td&gt;&lt;td&gt;Retry budget, jitter, bounded concurrency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Restart first&lt;/td&gt;&lt;td&gt;Destroys cache and evidence; may trigger movement&lt;/td&gt;&lt;td&gt;Capture state and prove the unhealthy component&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lower Cassandra grace&lt;/td&gt;&lt;td&gt;Can permit zombie resurrection&lt;/td&gt;&lt;td&gt;Change only through repair-validated design review&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Pause MongoDB balancing blindly&lt;/td&gt;&lt;td&gt;Preserves skew and creates forgotten debt&lt;/td&gt;&lt;td&gt;Require evidence, owner, and resume condition&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replay at full speed&lt;/td&gt;&lt;td&gt;Recovery traffic causes a second outage&lt;/td&gt;&lt;td&gt;Rate-limit against spare capacity and queue age&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Treat snapshot as PITR&lt;/td&gt;&lt;td&gt;Misses changes after the snapshot&lt;/td&gt;&lt;td&gt;Preconfigure and test engine-specific continuous recovery&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Generic recovery actions can amplify load or damage correctness.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Standardize evidence capture and demand control, then branch by engine mechanism.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Run game days that include retry storms, a hot key, replica loss, and bounded replay.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Validate backup restoration and record the last successful restore test—not merely the last backup job.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>What Happens at 100K, 1M, and 10M Writes/sec?</title><link>https://rajivonai.com/blog/2026-09-24-nosql-writes-per-second-100k-1m-10m/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-09-24-nosql-writes-per-second-100k-1m-10m/</guid><description>A workload and cost model for high-rate NoSQL writes that replaces universal breakpoints with measurable capacity, durability, and recovery constraints.</description><pubDate>Thu, 24 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Write rate alone cannot select a database: ten million 100-byte idempotent events and ten million 20 KB indexed transactions are different systems by orders of magnitude.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The labels 100K, 1M, and 10M writes per second are useful stress points, not product boundaries. A defensible design also specifies encoded item size, key distribution, consistency, durability, replication geography, indexes, update-versus-insert ratio, retention, burst shape, and recovery objective.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Claims that managed services become uneconomic at a fixed rate, or that self-managed Cassandra becomes cheaper above another fixed rate, are not portable. Cloud prices vary by region and date; self-managed cost depends on utilization, replication, reserved commitments, staff, backups, upgrades, failed-zone headroom, and incident load.&lt;/p&gt;
&lt;p&gt;The right question is: what resources does this exact mutation consume, and can the system recover from burst or failure without violating the SLO?&lt;/p&gt;
&lt;h2 id=&quot;build-the-workload-envelope&quot;&gt;Build the Workload Envelope&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;logical_bytes_second = writes_second * encoded_item_bytes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;base_capacity_units = writes_second * ceil(encoded_item_bytes / unit_bytes)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;secondary_units = sum(writes_affecting_index * ceil(projected_bytes / unit_bytes))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;network_bytes = logical_bytes_second * replication_and_region_factor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;daily_raw_storage = logical_bytes_second * 86400&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;required_peak_service = peak_arrival_rate / target_utilization&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;replay_time = queued_writes / post_incident_spare_rate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For DynamoDB provisioned capacity, one WCU represents one standard write per second for an item up to 1 KB; transactional writes use twice the standard write capacity, and affected GSIs add their own consumption. On-demand tables use request units with the same item-size rounding concepts. Ten 1 KB events cannot be compressed by assertion into one 1 KB item; batching API calls reduces round trips, not the bytes or capacity represented by the items. &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadWriteCapacityMode.html&quot;&gt;AWS DynamoDB capacity modes&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;architecture-across-three-stress-points&quot;&gt;Architecture Across Three Stress Points&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[&quot;Production-shaped workload definition&quot;] --&gt; B[&quot;Durable admission buffer&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[&quot;Partition-aware bounded writers&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[&quot;Candidate database&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[&quot;Tail latency, throttling, storage, and recovery evidence&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F{&quot;SLO holds under peak and one failure&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|No| G[&quot;Change model, capacity, or requirements&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|Yes| H[&quot;Price the measured configuration&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;100k-writes-per-second&quot;&gt;100K writes per second&lt;/h3&gt;
&lt;p&gt;At this level, operational simplicity may dominate, but only if keys distribute and payloads fit service limits. A single hot key can fail while total throughput looks modest. Benchmark a managed service and a self-managed option only if portability, control, or existing platform capability makes the comparison real.&lt;/p&gt;
&lt;h3 id=&quot;1m-writes-per-second&quot;&gt;1M writes per second&lt;/h3&gt;
&lt;p&gt;Queue recovery, network bandwidth, index maintenance, compaction, and failed-zone capacity become first-class. Require partition-level observability and admission control. For DynamoDB on-demand, planned growth must respect warm-throughput behavior; AWS documents immediate accommodation up to twice the previous peak and possible throttling for a larger jump inside 30 minutes. &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/on-demand-capacity-mode.html&quot;&gt;DynamoDB on-demand scaling&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For Bigtable, benchmark row-key distribution and use Key Visualizer or hot-tablet diagnostics. For Cassandra, include compaction, repair, bootstrap, and disk headroom; do not trade durability for a headline by changing commit-log synchronization without an explicit loss budget. For MongoDB, include index maintenance, WiredTiger cache pressure, replication lag, and shard movement.&lt;/p&gt;
&lt;h3 id=&quot;10m-writes-per-second&quot;&gt;10M writes per second&lt;/h3&gt;
&lt;p&gt;At this point, the architecture should assume hierarchical ingestion: regional admission, a durable log, independent writer pools, an operational serving store, and an immutable analytical copy. The design must prove network feasibility, quota approval, blast-radius isolation, backfill and replay, and the time required to restore steady state. The number still does not dictate the vendor.&lt;/p&gt;
&lt;h2 id=&quot;transparent-tco&quot;&gt;Transparent TCO&lt;/h2&gt;
&lt;p&gt;Price only a tested configuration. Record the price-sheet date and region and separate:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;database request or node capacity;&lt;/li&gt;
&lt;li&gt;storage, indexes, backups, and change streams;&lt;/li&gt;
&lt;li&gt;inter-zone and inter-region transfer;&lt;/li&gt;
&lt;li&gt;durable buffer and analytical copy;&lt;/li&gt;
&lt;li&gt;reserved or committed-use terms;&lt;/li&gt;
&lt;li&gt;on-call, patching, repair, upgrade, and capacity engineering;&lt;/li&gt;
&lt;li&gt;spare capacity for a failed zone and replay.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Present a sensitivity table for item size, utilization, replication, and staff assumptions. A single three-year total without those inputs is not engineering evidence.&lt;/p&gt;
&lt;h3 id=&quot;required-benchmark-record&quot;&gt;Required benchmark record&lt;/h3&gt;
&lt;p&gt;For every candidate, record engine and client versions, region or instance type, storage configuration, partition or shard count, replication, acknowledgement semantics, schema, indexes, serialized payload distribution, key-skew distribution, burst duration, and test duration. Warm the system to a stated condition, then inject a node or zone failure and measure backlog recovery. Report rejected and retried operations separately from accepted throughput. An average rate that hides throttling or counts client retries as new business writes is not a valid result.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Vendor documentation establishes billing units, warm-throughput behavior, key-design rules, and service limits. It does not establish a universal buy-versus-build cutover. That decision must follow a reproducible benchmark and cost workbook. Any latency or node-throughput number in the decision record should name the software version, instance, storage, replication, consistency, schema, payload, distribution, and duration.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Hidden variable&lt;/th&gt;&lt;th&gt;Failure if omitted&lt;/th&gt;&lt;th&gt;Required test&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Encoded item size&lt;/td&gt;&lt;td&gt;Capacity and network estimates miss by multiples&lt;/td&gt;&lt;td&gt;Sample serialized production records&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Key skew&lt;/td&gt;&lt;td&gt;One owner throttles below fleet capacity&lt;/td&gt;&lt;td&gt;Replay skewed production distribution&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Secondary writes&lt;/td&gt;&lt;td&gt;Cost and tail latency jump after launch&lt;/td&gt;&lt;td&gt;Include every maintained access path&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Burst duration&lt;/td&gt;&lt;td&gt;Queue cannot drain inside SLO&lt;/td&gt;&lt;td&gt;Measure peak arrival and spare service rate&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failure headroom&lt;/td&gt;&lt;td&gt;Zone loss converts redundancy into outage&lt;/td&gt;&lt;td&gt;Load-test with one failure domain removed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Durability setting&lt;/td&gt;&lt;td&gt;Benchmark wins by accepting hidden loss&lt;/td&gt;&lt;td&gt;Verify acknowledgement and crash recovery&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Writes per second conceal the variables that determine feasibility and cost.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Define the full workload envelope and measure a failure-capable configuration.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Reproduce throughput, tail latency, backlog recovery, and cost from recorded inputs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Reject any vendor recommendation or TCO estimate that cannot show its formulas and benchmark conditions.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category></item><item><title>The Hidden Cost of Secondary Access Patterns in NoSQL</title><link>https://rajivonai.com/blog/2026-09-23-hidden-cost-secondary-access-patterns-nosql/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-09-23-hidden-cost-secondary-access-patterns-nosql/</guid><description>A workload-based method for evaluating DynamoDB GSIs, MongoDB indexes, Cassandra SAI, and Bigtable materialized views without invented amplification constants.</description><pubDate>Wed, 23 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A secondary access path is another maintained data structure with its own placement, storage, failure, and consistency behavior—not a free query option.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Operational NoSQL schemas are designed around known keys. Requirements then expand: find an order by customer, locate a device by status, or filter accounts by region. The tempting answer is “add an index,” but that phrase hides four materially different implementations across DynamoDB, MongoDB, Cassandra, and Bigtable.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;It is misleading to assign each engine a universal write-amplification number. Amplification depends on projected attributes, item size and billing rounding, index count, compaction, replication, update frequency, selectivity, and cache residency. It also has at least three units:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;logical_mutations = base_mutation + affected_secondary_mutations&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;billed_write_units = sum(ceil(bytes_written_to_each_structure / billing_quantum))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;physical_bytes = logical_bytes * measured_storage_engine_amplification&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These quantities must not be compared as though they were the same ratio.&lt;/p&gt;
&lt;h2 id=&quot;model-the-access-path-explicitly&quot;&gt;Model the Access Path Explicitly&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Q[&quot;New query requirement&quot;] --&gt; B[&quot;Bound cardinality, selectivity, freshness, and result size&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; O{&quot;Can an existing primary key answer it&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    O --&gt;|Yes| R[&quot;Reuse bounded key lookup&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    O --&gt;|No| C[&quot;Compare maintained index, derived table, and analytical path&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; W[&quot;Calculate writes, bytes, storage, and failure semantics&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    W --&gt; T[&quot;Load-test updates, backfill, rebuild, and skew&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    T --&gt; D[&quot;Choose only with an SLO and removal plan&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;dynamodb-global-secondary-indexes&quot;&gt;DynamoDB global secondary indexes&lt;/h3&gt;
&lt;p&gt;A GSI has its own partition and sort key, provisioned or on-demand capacity behavior, and projected attributes. Base-table writes propagate to each affected GSI; large projections and billing-unit rounding change cost. GSI key choice can create throttling independent of the base table. Use sparse indexes where the business predicate is naturally sparse, project only required attributes, and monitor both table and index throttles. AWS documents item-size and capacity-unit mechanics; calculate them from actual encoded items rather than a fixed multiplier. &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/data-modeling-blocks.html&quot;&gt;DynamoDB data-modeling blocks&lt;/a&gt;, &lt;a href=&quot;https://docs.aws.amazon.com/pdfs/whitepapers/latest/best-practices-for-migrating-from-rdbms-to-dynamodb/best-practices-for-migrating-from-rdbms-to-dynamodb.pdf&quot;&gt;capacity guidance&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;mongodb-secondary-indexes&quot;&gt;MongoDB secondary indexes&lt;/h3&gt;
&lt;p&gt;MongoDB maintains index keys with the collection mutation. Cost depends on index key size, array multikey expansion, update pattern, working-set residency, and write concern. An index may accelerate a selective query while increasing cache pressure and replication work. &lt;code&gt;background: true&lt;/code&gt; is not a modern tuning lever: current index builds use an optimized build process, and obsolete options must not appear in a new runbook. Validate with &lt;code&gt;explain&lt;/code&gt;, index-usage statistics, cache and eviction metrics, replication lag, and a production-shaped update test. &lt;a href=&quot;https://www.mongodb.com/docs/manual/indexes/&quot;&gt;MongoDB index documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;cassandra-storage-attached-indexing&quot;&gt;Cassandra Storage-Attached Indexing&lt;/h3&gt;
&lt;p&gt;Cassandra 5.0 SAI attaches index components to SSTables and follows their lifecycle. It broadens supported predicates but does not repeal partitioning physics: query fan-out, selectivity, tombstones, compaction, and replica availability still define cost. Prefer a dedicated query table when an access pattern is high-volume, latency-critical, and stable; consider SAI when the operationally tested predicate and cardinality fit its documented behavior. &lt;a href=&quot;https://cassandra.apache.org/doc/latest/cassandra/developing/cql/indexing/sai/&quot;&gt;Apache Cassandra SAI documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;bigtable-continuous-materialized-views&quot;&gt;Bigtable continuous materialized views&lt;/h3&gt;
&lt;p&gt;Bigtable’s primary index is the row key. A continuous materialized view creates a separate automatically updated table with a schema optimized for another query; it is not “zero amplification on the primary.” It consumes storage and processing and has product limitations that must be checked at design time. Use the documented create and management interfaces rather than invented SQL provisioning syntax. &lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/continuous-materialized-views&quot;&gt;Bigtable continuous materialized views&lt;/a&gt;, &lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/tables-and-views&quot;&gt;tables and views&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;a-reproducible-decision-table&quot;&gt;A Reproducible Decision Table&lt;/h2&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Question&lt;/th&gt;&lt;th&gt;Evidence required&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;How many secondary mutations does one business update cause?&lt;/td&gt;&lt;td&gt;Mutation trace by create, update, delete, and key change&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;How many bytes and billed units are added?&lt;/td&gt;&lt;td&gt;Serialized production-shaped records and documented billing quantum&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;What happens under a hot secondary key?&lt;/td&gt;&lt;td&gt;Skewed-key test, not a uniform benchmark&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;How is backfill or rebuild isolated?&lt;/td&gt;&lt;td&gt;Measured foreground tail latency and recovery time&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;What consistency can readers observe?&lt;/td&gt;&lt;td&gt;Documented propagation semantics plus failure injection&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Can the access path be removed safely?&lt;/td&gt;&lt;td&gt;Usage telemetry and rollback procedure&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;benchmark-protocol&quot;&gt;Benchmark protocol&lt;/h3&gt;
&lt;p&gt;Run four phases against production-shaped cardinality and skew: base table without the secondary path, steady-state writes with it enabled, historical backfill or index build, and rebuild after an injected failure. Keep acknowledgement level, replication, payloads, and hardware constant. Record client p50 and p99, database CPU and storage latency, cache eviction, replication lag, bytes stored, compaction or background work, and recovery time. Publish the configuration with the results. A benchmark that changes durability or omits the build phase cannot support an architectural decision.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern is that each system pays in a different layer: DynamoDB exposes a separately capacity-accounted index; MongoDB synchronously maintains B-tree structures as part of writes; Cassandra SAI stores index components alongside SSTables; Bigtable materializes a second maintained view table. No primary source supports a universal ordering such as “engine A is 1.2× and engine B is 5×.” Those figures require a named benchmark with schema, versions, hardware, and workload.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Mechanism&lt;/th&gt;&lt;th&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Hot secondary key&lt;/td&gt;&lt;td&gt;Alternate key has lower cardinality than the primary&lt;/td&gt;&lt;td&gt;Model skew and bucket only with bounded read fan-out&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Oversized projection&lt;/td&gt;&lt;td&gt;Index duplicates attributes the query never reads&lt;/td&gt;&lt;td&gt;Minimize projection and measure encoded size&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cache displacement&lt;/td&gt;&lt;td&gt;Index working set evicts base data&lt;/td&gt;&lt;td&gt;Track cache residency, eviction, and tail latency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Rebuild overload&lt;/td&gt;&lt;td&gt;Backfill competes with foreground writes&lt;/td&gt;&lt;td&gt;Rate-limit, checkpoint, and prove restartability&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Stale derived path&lt;/td&gt;&lt;td&gt;Asynchronous pipeline lags or loses ordering&lt;/td&gt;&lt;td&gt;Version mutations; monitor age; reconcile by key&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Index sprawl&lt;/td&gt;&lt;td&gt;Old access paths remain after clients leave&lt;/td&gt;&lt;td&gt;Record owner and usage; delete through a measured change&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: “One more index” hides independent cost and failure behavior.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Model logical mutations, billed units, and physical bytes separately.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Benchmark skew, updates, backfill, rebuild, and failure recovery with production-shaped data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Require an owner, SLO, cost worksheet, and removal test for every secondary access path.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>Why Distributed NoSQL Databases Develop Hot Partitions</title><link>https://rajivonai.com/blog/2026-09-22-why-nosql-databases-develop-hot-partitions/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-09-22-why-nosql-databases-develop-hot-partitions/</guid><description>How skewed keys overload physical partitions across Bigtable, DynamoDB, Cassandra, and MongoDB, with evidence-driven remediation.</description><pubDate>Tue, 22 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A database can have abundant fleet-wide capacity and still throttle one customer because routing concentrates that customer’s traffic on one physical ownership unit.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Partitioned databases scale when keys distribute storage and requests across independent workers. Production traffic rarely distributes uniformly: one tenant, device, product, or recent time range can dominate. Adding nodes does not immediately fix a key that still maps to one DynamoDB partition, Bigtable tablet range, Cassandra replica set, or MongoDB chunk.&lt;/p&gt;
&lt;p&gt;“Hot partition” is overloaded. It may mean one logical key, a physical range, a replica set, or a shard. Diagnosis must identify the constrained ownership unit before changing the schema.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Global CPU averages conceal local saturation. Retrying a throttled operation can increase work on the same owner, while rushed salting can trade one hot write for expensive read fan-out and loss of ordering.&lt;/p&gt;
&lt;p&gt;Is pressure caused by key popularity, monotonic placement, an oversized logical partition, or lagging movement—and can mitigation preserve correctness?&lt;/p&gt;
&lt;h2 id=&quot;diagnose-the-ownership-unit&quot;&gt;Diagnose the Ownership Unit&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[&quot;Latency or throttling alert&quot;] --&gt; B{&quot;Fleet-wide saturation&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes| C[&quot;Add or rebalance capacity after workload validation&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No| D[&quot;Group traffic by tenant and logical key&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E{&quot;Single logical key dominates&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|Yes| F[&quot;Cache reads or split an application-owned bucket&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|No| G[&quot;Inspect physical range or shard distribution&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H{&quot;Monotonic or low-cardinality key&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|Yes| I[&quot;Redesign key and migrate deliberately&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|No| J[&quot;Inspect movement, compaction, cache, and replica health&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Group request count, bytes, latency, throttles, and errors by operation, tenant, and key hash. Correlate that with physical evidence:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DynamoDB: throttles, consumed capacity, contributor insights, and partition-key distribution. Adaptive capacity helps but does not make one item infinitely scalable.&lt;/li&gt;
&lt;li&gt;Bigtable: Key Visualizer and &lt;code&gt;ListHotTablets&lt;/code&gt;; Google exposes hot ranges rather than a fictional application “worker.” &lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/hot-tablets&quot;&gt;Hot tablet diagnostics&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Cassandra: coordinator and local latency, dropped messages, pending tasks, partition-size histograms, SSTables per read, compaction debt, and replica health.&lt;/li&gt;
&lt;li&gt;MongoDB: shard-key distribution, per-shard operations, balancer state, range-deletion backlog, and &lt;code&gt;currentOp&lt;/code&gt;. The default range size is 128 MB, but size alone does not identify a traffic hotspot. &lt;a href=&quot;https://www.mongodb.com/docs/manual/core/sharding-balancer-administration/&quot;&gt;MongoDB balancer documentation&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;four-different-mechanisms&quot;&gt;Four Different Mechanisms&lt;/h2&gt;
&lt;h3 id=&quot;dynamodb-one-key-can-reach-a-physical-boundary&quot;&gt;DynamoDB: one key can reach a physical boundary&lt;/h3&gt;
&lt;p&gt;A high-cardinality partition key is necessary but insufficient: a celebrity item or global counter remains one hot key. Prefer write-sharded counters, cacheable materialized reads, or independent subkeys. Preserve conditional-write semantics explicitly; random retries against different keys are not equivalent writes.&lt;/p&gt;
&lt;p&gt;On-demand mode has documented warm-throughput behavior. A jump above twice the previous peak inside 30 minutes may throttle unless pre-warmed or staged. &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/on-demand-capacity-mode.html&quot;&gt;DynamoDB on-demand scaling&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;bigtable-ordered-keys-can-create-a-hot-range&quot;&gt;Bigtable: ordered keys can create a hot range&lt;/h3&gt;
&lt;p&gt;Bigtable sorts rows lexicographically. Timestamp-first or sequential identifiers drive writes into a narrow range. Put a stable high-cardinality segment before time, but do not blindly hash the whole key: Google notes that hashing destroys useful row-range locality. &lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/schema-design&quot;&gt;Bigtable schema design&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;cassandra-a-logical-partition-belongs-to-one-replica-set&quot;&gt;Cassandra: a logical partition belongs to one replica set&lt;/h3&gt;
&lt;p&gt;Cassandra hashes the partition key, but all rows for one key still map to the same replicas. Bucketing by time or deterministic shard can distribute it at the price of bounded read fan-out. TTL expiry produces tombstones. Never lower &lt;code&gt;gc_grace_seconds&lt;/code&gt; as an incident shortcut unless repairs provably finish inside the shorter window; otherwise deleted data can reappear. &lt;a href=&quot;https://cassandra.apache.org/doc/latest/cassandra/managing/operating/compaction/tombstones.html&quot;&gt;Cassandra tombstones&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;mongodb-shard-keys-couple-placement-and-routing&quot;&gt;MongoDB: shard keys couple placement and routing&lt;/h3&gt;
&lt;p&gt;A monotonically increasing ranged shard key concentrates inserts near the maximum range; a low-cardinality key limits distribution; omitting a queried key can scatter reads. Hashed sharding distributes writes but gives up range locality. The balancer moves ranges in the background and one shard participates in only one chunk migration at a time; stopping it is not a universal first response. &lt;a href=&quot;https://www.mongodb.com/docs/manual/sharding/&quot;&gt;MongoDB sharding&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;safe-remediation-order&quot;&gt;Safe Remediation Order&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Stop retry storms with bounded concurrency, exponential backoff, and jitter.&lt;/li&gt;
&lt;li&gt;Protect unaffected tenants with admission control and per-tenant budgets.&lt;/li&gt;
&lt;li&gt;Reduce read pressure with correctness-aware caching or request coalescing.&lt;/li&gt;
&lt;li&gt;Add capacity only if the stressed ownership unit can use it.&lt;/li&gt;
&lt;li&gt;Redesign keys through dual-write, backfill, parity verification, and reversible cutover.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Do not redirect a mutation to an arbitrary replica, acknowledge an in-memory queue as durable, or change consistency levels during an incident without a written correctness decision.&lt;/p&gt;
&lt;h3 id=&quot;verification-gates&quot;&gt;Verification gates&lt;/h3&gt;
&lt;p&gt;Treat the change as successful only when the previously hot owner no longer dominates request rate or tail latency, the next-hottest owners have not inherited the problem, and retry volume returns to its pre-incident ratio. For a new bucketed key, exercise page boundaries, duplicate suppression, partial-bucket failure, and historical reads before cutover. Keep the old read path available until a reconciliation job proves that every mutation version visible in the source is represented in the new layout. Capacity graphs alone cannot prove a key migration preserved results.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern differs by engine but shares one principle: distribution derives from the key. Bigtable warns against timestamp-prefixed keys. MongoDB documents background range balancing and migration limits. Cassandra documents tombstone and repair safety. DynamoDB documents warm-throughput boundaries. None supports a universal requests-per-partition threshold across payload sizes, consistency modes, and versions.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Intervention&lt;/th&gt;&lt;th&gt;New risk&lt;/th&gt;&lt;th&gt;Verification&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Add deterministic buckets&lt;/td&gt;&lt;td&gt;Reads fan out and ordering becomes a merge problem&lt;/td&gt;&lt;td&gt;Bound bucket count; test page tokens and partial failure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cache a hot item&lt;/td&gt;&lt;td&gt;Stale data or stampede on expiry&lt;/td&gt;&lt;td&gt;Define staleness; coalesce requests; jitter TTLs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Add nodes&lt;/td&gt;&lt;td&gt;Movement competes with foreground traffic&lt;/td&gt;&lt;td&gt;Rate-limit movement; watch tail latency and backlog&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Pause balancing&lt;/td&gt;&lt;td&gt;Existing skew persists&lt;/td&gt;&lt;td&gt;Pause only for a diagnosed conflict with a rollback time&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lower Cassandra grace&lt;/td&gt;&lt;td&gt;Zombie resurrection after missed repair&lt;/td&gt;&lt;td&gt;Prove full repair completion inside the grace window&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Change the key&lt;/td&gt;&lt;td&gt;Dual-write divergence&lt;/td&gt;&lt;td&gt;Reconcile by immutable identity and mutation version&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Fleet averages hide a saturated ownership unit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Attribute requests to logical keys and physical owners before remediating.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Show that load and tail latency distribute without correctness drift.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Add per-tenant and per-key observability, then rehearse a reversible key migration.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>Design an IoT Platform for 10 Million Devices: Bigtable vs DynamoDB vs Cassandra vs MongoDB</title><link>https://rajivonai.com/blog/2026-09-21-iot-platform-10-million-devices-nosql-comparison/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-09-21-iot-platform-10-million-devices-nosql-comparison/</guid><description>A requirements-first design for one million telemetry writes per second, including burst admission, retention math, partitioning, and database tradeoffs.</description><pubDate>Mon, 21 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A million telemetry writes per second is not a database selection criterion. It is an admission-control, key-distribution, retention, and recovery problem that must be quantified before a product name enters the design.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Assume ten million registered devices, one million 250-byte measurements per second at steady state, a three-million-per-second short burst, and 90 days of online retention. The raw payload rate is &lt;strong&gt;250 MB/s&lt;/strong&gt;, or &lt;strong&gt;15 GB/minute&lt;/strong&gt;, &lt;strong&gt;21.6 TB/day&lt;/strong&gt;, and &lt;strong&gt;1.94 PB for 90 days&lt;/strong&gt;. Those figures exclude replication, indexes, storage-engine metadata, compression, backups, and protocol overhead.&lt;/p&gt;
&lt;p&gt;The workload also needs a durability contract. This design assumes at-least-once delivery, durable buffering before the database, idempotent writes, and queries scoped to one tenant, device, metric, and bounded time range. Cross-device analytics belongs in an object-store and analytical pipeline, not in the operational serving table.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The three-million-write burst cannot be delegated to database autoscaling. &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/on-demand-capacity-mode.html&quot;&gt;DynamoDB on-demand capacity&lt;/a&gt;, for example, immediately accommodates up to twice a table’s previous peak; AWS warns that exceeding twice the previous peak inside 30 minutes can throttle unless capacity is pre-warmed or growth is staged. Bigtable, Cassandra, and MongoDB also need time to add capacity or rebalance data.&lt;/p&gt;
&lt;p&gt;The design question is: how do we make peak arrival rate independent of database service rate while preserving per-device ordering and preventing a small tenant or key range from consuming the fleet?&lt;/p&gt;
&lt;h2 id=&quot;the-buffered-idempotent-ingest-architecture&quot;&gt;The Buffered, Idempotent Ingest Architecture&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[&quot;Devices — at-least-once delivery&quot;] --&gt; G[&quot;Regional gateways — authentication and quotas&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; Q[&quot;Durable log — partition by tenant and device&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Q --&gt; N[&quot;Normalizer — validate schema and assign event identity&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    N --&gt; W[&quot;Bounded writer pools — adaptive concurrency&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    W --&gt; O[&quot;Operational database — recent device history&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Q --&gt; L[&quot;Object storage — immutable analytical copy&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    O --&gt; V[&quot;Read API — bounded device and time range&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    W --&gt; X[&quot;Retry topic and dead-letter quarantine&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Every event carries an immutable &lt;code&gt;event_id&lt;/code&gt;, &lt;code&gt;tenant_id&lt;/code&gt;, &lt;code&gt;device_id&lt;/code&gt;, device timestamp, server-ingest timestamp, and schema version. Consumers checkpoint only after the target acknowledges the idempotent mutation. Retries use exponential backoff with jitter and a bounded attempt budget; poison messages go to quarantine. Queue age, not queue depth alone, is the recovery objective.&lt;/p&gt;
&lt;p&gt;Partition the log by a stable hash of tenant and device so events for one device remain ordered. Enforce quotas before the log and again at writer pools. A single customer reconnect storm must not monopolize every database partition.&lt;/p&gt;
&lt;h2 id=&quot;physical-models-by-engine&quot;&gt;Physical Models by Engine&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Engine&lt;/th&gt;&lt;th&gt;Natural model&lt;/th&gt;&lt;th&gt;Critical constraint&lt;/th&gt;&lt;th&gt;Best fit&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Bigtable&lt;/td&gt;&lt;td&gt;Lexicographically sorted rows&lt;/td&gt;&lt;td&gt;Timestamp-first keys create sequential hotspots&lt;/td&gt;&lt;td&gt;GCP-native time-series serving with row-range reads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DynamoDB&lt;/td&gt;&lt;td&gt;Partition key plus sort key&lt;/td&gt;&lt;td&gt;Warm throughput, quotas, and index capacity&lt;/td&gt;&lt;td&gt;AWS-native workloads with explicit access patterns&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cassandra&lt;/td&gt;&lt;td&gt;Hash-distributed partitions with clustering rows&lt;/td&gt;&lt;td&gt;Repair, compaction, disk, and partition-size headroom&lt;/td&gt;&lt;td&gt;Portability or control justifies an operations team&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MongoDB&lt;/td&gt;&lt;td&gt;Sharded BSON documents and secondary indexes&lt;/td&gt;&lt;td&gt;Shard-key distribution, index cost, cache, and balancing&lt;/td&gt;&lt;td&gt;Document-oriented operational queries accompany telemetry&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;For Bigtable, use a key such as &lt;code&gt;tenant#bucket#device#reverse_timestamp&lt;/code&gt;, where &lt;code&gt;bucket&lt;/code&gt; is a deterministic small shard derived from the device ID. Do not put a timestamp first. Google documents that timestamp-prefixed keys push sequential writes to one node and recommends bounded row-range reads. &lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/schema-design&quot;&gt;Bigtable schema guidance&lt;/a&gt;, &lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/schema-design-time-series&quot;&gt;time-series patterns&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For DynamoDB, use one item per event rather than one ever-growing item per device: &lt;code&gt;PK=TENANT#DEVICE#DAY&lt;/code&gt;, &lt;code&gt;SK=timestamp#event_id&lt;/code&gt;. Select the time bucket from measured events per device and item size. In provisioned mode, one write-capacity unit covers one standard write per second for an item up to 1 KB; transactional writes and affected index projections consume additional capacity. On-demand mode bills write request units instead. Model the base table and every GSI. &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadWriteCapacityMode.html&quot;&gt;AWS read and write capacity modes&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For Cassandra, use a bounded key such as &lt;code&gt;PRIMARY KEY ((tenant_id, device_id, day), event_time, event_id)&lt;/code&gt; with TimeWindowCompactionStrategy for an immutable TTL workload. TTL expiry creates tombstones; it does not produce zero cleanup cost. Repairs must complete inside the table’s &lt;code&gt;gc_grace_seconds&lt;/code&gt; policy before tombstones can safely be purged. &lt;a href=&quot;https://cassandra.apache.org/doc/latest/cassandra/managing/operating/compaction/tombstones.html&quot;&gt;Apache Cassandra tombstone documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For MongoDB time-series collections, shard the user-facing collection using a key that includes the &lt;code&gt;metaField&lt;/code&gt;; do not operate directly on the internal bucket collection. &lt;a href=&quot;https://www.mongodb.com/docs/manual/core/timeseries/timeseries-shard-collection/&quot;&gt;MongoDB time-series sharding&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;capacity-and-cost-model&quot;&gt;Capacity and Cost Model&lt;/h2&gt;
&lt;p&gt;Do not publish a dollar total without region, date, durability, and discounts. Use a reproducible model:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;raw_bytes_day = writes_per_second * payload_bytes * 86400&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;logical_write_units = writes_per_second * ceil(item_bytes / billing_quantum)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;physical_storage = raw_storage * replication_factor * measured_engine_amplification&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;required_service_rate = peak_rate / target_utilization&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;recovery_time = queued_events / spare_service_rate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Measure engine amplification; it is not a universal constant. Include base writes, every secondary projection, replication, backups, cross-region traffic, retained queue storage, and a failed-zone capacity scenario.&lt;/p&gt;
&lt;p&gt;At three million arrivals per second and 1.5 million sustained database writes per second, a 10-minute burst produces 900 million queued events. With 500,000 writes per second of spare capacity afterward, recovery takes 30 minutes. That consequence belongs in the SLO.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern across these engines is query-first key design plus bounded buffering. Bigtable starts with read requests and hotspot avoidance. DynamoDB documents warm-throughput behavior rather than unlimited instantaneous elasticity. Cassandra documents that expired TTL data becomes tombstones and depends on repair and compaction. MongoDB requires a valid shard key and balances ranges in the background. These are system behaviors, not benchmark results; this article intentionally makes no latency claim without a workload-specific test.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Observable proof&lt;/th&gt;&lt;th&gt;Safe response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Reconnect storm exceeds warm capacity&lt;/td&gt;&lt;td&gt;Queue age and throttling rise together&lt;/td&gt;&lt;td&gt;Enforce quotas; drain at a tested rate; pre-warm planned events&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hot tenant or key range&lt;/td&gt;&lt;td&gt;Per-key throttles, hot-tablet signals, or shard imbalance&lt;/td&gt;&lt;td&gt;Increase deterministic buckets after validating read fan-out&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Retry amplification&lt;/td&gt;&lt;td&gt;Attempt rate grows faster than original traffic&lt;/td&gt;&lt;td&gt;Cap concurrency and retries; honor backoff; quarantine poison events&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Retention overwhelms serving storage&lt;/td&gt;&lt;td&gt;Compaction, cache, or disk headroom degrades&lt;/td&gt;&lt;td&gt;Export immutable history and shorten online retention&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Duplicate or late events&lt;/td&gt;&lt;td&gt;Identity repeats or timestamps arrive out of order&lt;/td&gt;&lt;td&gt;Make writes idempotent and retain device and ingest time&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Region loss&lt;/td&gt;&lt;td&gt;Remaining region cannot accept peak plus replay&lt;/td&gt;&lt;td&gt;Test failover capacity and queue retention&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Peak arrival rate, not average throughput, defines the failure boundary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Put a durable log and bounded, idempotent writers between devices and the database.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Load-test key distribution, queue recovery, retry behavior, and one-zone loss with production-shaped payloads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Publish assumptions, formulas, test configuration, and measured percentiles before selecting an engine or quoting cost.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category></item><item><title>Bigtable vs MongoDB: Sorted Keyspace Mechanics, Row Atomicity, and the Cost of Secondary Query Flexibility</title><link>https://rajivonai.com/blog/2026-09-20-bigtable-vs-mongodb-sorted-keyspace-vs-rich-queries/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-09-20-bigtable-vs-mongodb-sorted-keyspace-vs-rich-queries/</guid><description>Bigtable&apos;s sorted row key is both its primary performance primitive and its primary design constraint: all efficient reads are bounded key-range scans, and everything that is not expressible as a bounded key range requires materialized alternate representations or full table scans. MongoDB&apos;s WiredTiger B-Tree supports arbitrary field predicates through compound indexes but pays write amplification and cache pressure for each one. This post explains the physical mechanics behind both models.</description><pubDate>Sun, 20 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Bigtable is a wide-column store with a sorted, distributed keyspace. The row key is not just an identifier — it is the physical sort order. Two rows with adjacent keys are stored on adjacent SSTable blocks on the same tablet server. A scan for rows between &lt;code&gt;device#00001&lt;/code&gt; and &lt;code&gt;device#00999&lt;/code&gt; reads a contiguous range of SSTable blocks, achieving sequential I/O throughput on commodity SSDs. A scan for rows matching a complex predicate — where the predicate cannot be expressed as a bounded key range — reads every tablet in the table, regardless of selectivity.&lt;/p&gt;
&lt;p&gt;MongoDB stores documents in WiredTiger B-Trees without any assumption about physical sort order relative to field values. Documents are heap-allocated; their physical location on disk is a function of insertion order and compaction history, not field value order. Ordering and filtering by field values is the responsibility of compound B-Tree indexes — separate data structures that WiredTiger maintains in sorted order and traverses during IXSCAN operations. Those indexes are powerful and flexible, but they are not free: each one occupies WiredTiger cache space, each one receives every write the collection does, and each one may or may not be used depending on whether the query planner finds its prefix ordering compatible with the query shape.&lt;/p&gt;
&lt;p&gt;The choice between Bigtable and MongoDB is a choice between two fundamentally different theories of where query efficiency lives: in the physical arrangement of data (Bigtable) or in a separately maintained sorted index structure (MongoDB). Both work at billion-record scale. The failure mode in each case is attempting to query data in a way that the physical model does not natively support — and misidentifying the resulting degradation as a database performance problem rather than a schema design problem.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Bigtable is Google’s globally distributed wide-column store, used internally for Web indexing, Gmail, and YouTube, and available externally as Google Cloud Bigtable. MongoDB is a general-purpose document database used across OLTP, operational analytics, and content management workloads. They share JSON-like data storage but differ substantially in physical architecture.&lt;/p&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Dimension&lt;/th&gt;&lt;th&gt;Google Cloud Bigtable&lt;/th&gt;&lt;th&gt;MongoDB&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Physical storage model&lt;/td&gt;&lt;td&gt;Sorted String Tables (SSTables) — immutable files of sorted key-value pairs; LSM-tree write path (memtable → SSTable flush → compaction)&lt;/td&gt;&lt;td&gt;WiredTiger B-Tree — mutable page tree with in-place updates via MVCC update chains; checkpoint-based persistence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Row key model&lt;/td&gt;&lt;td&gt;Single composite row key — arbitrary byte string, determines sort order and tablet placement&lt;/td&gt;&lt;td&gt;Document &lt;code&gt;_id&lt;/code&gt; — arbitrary BSON value; shard key (optional) determines chunk placement in sharded deployments&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Column model&lt;/td&gt;&lt;td&gt;Sparse wide columns — unlimited columns per row, organized in column families; no schema enforcement&lt;/td&gt;&lt;td&gt;BSON document — nested, typed fields; flexible schema per document; schema validation optional&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Query model&lt;/td&gt;&lt;td&gt;Row key exact match, prefix scan, or bounded range scan; row filter on column values (not indexed — filter is post-scan)&lt;/td&gt;&lt;td&gt;Arbitrary predicate on any indexed field; compound B-Tree index supports multi-field filter, sort, and range&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Secondary indexes&lt;/td&gt;&lt;td&gt;Not natively supported — must maintain alternate row representations or use Bigtable’s continuous materialized views (preview)&lt;/td&gt;&lt;td&gt;Native compound B-Tree indexes on any field or subfield combination; no hard limit; GSI-equivalent flexibility&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Write path&lt;/td&gt;&lt;td&gt;LSM-tree: write goes to memtable (in-memory) → WAL → SSTable flush; compaction merges SSTables asynchronously&lt;/td&gt;&lt;td&gt;WiredTiger: write goes to journal (WAL) → in-memory update chain → checkpoint reconciles dirty pages to disk&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Read path&lt;/td&gt;&lt;td&gt;SSTable scan with bloom filter lookups; no disk read if bloom filter eliminates block; sequential I/O for range scans&lt;/td&gt;&lt;td&gt;WiredTiger IXSCAN: B-Tree traversal on index pages (cache-resident or disk read); random I/O for scattered document fetches&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Engineers selecting between Bigtable and MongoDB most commonly make the decision based on throughput benchmarks, not on access pattern expressibility. A benchmark showing Bigtable at 3M ops/sec and MongoDB at 800K ops/sec (at identical instance sizing) correctly represents throughput for the benchmark’s access pattern — but the benchmark is typically a key-value lookup or range scan, which is exactly the workload Bigtable’s architecture optimizes for. The question the benchmark does not answer is: what happens when the application needs to query by a field that is not the row key?&lt;/p&gt;
&lt;p&gt;For Bigtable, the answer is a full table scan with row filter — O(N) scan cost regardless of the selectivity of the filter. A table with 10 billion rows, where only 10,000 rows match a filter on &lt;code&gt;firmware_version = &quot;2.4.1&quot;&lt;/code&gt;, requires reading all 10 billion rows through the filter to return 10,000 matches. The row filter evaluates cheaply (Bigtable’s server-side filtering avoids transmitting non-matching rows to the client) but still reads every SSTable block in the table — consuming the full table’s read bandwidth.&lt;/p&gt;
&lt;p&gt;For MongoDB, the same filter with a compound index &lt;code&gt;{firmware_version: 1}&lt;/code&gt; executes as an IXSCAN that reads only the index pages for &lt;code&gt;firmware_version = &quot;2.4.1&quot;&lt;/code&gt; — O(log N + K) where K is the number of matching documents. But if the WiredTiger cache does not hold those index pages (cache eviction under memory pressure), each index page read is a physical SSD I/O operation. At 10B documents with a 50MB index for &lt;code&gt;firmware_version&lt;/code&gt;, the index likely fits in cache if it is frequently used — but if the field has 50,000 distinct values and the index is 500MB, it may be partially evicted.&lt;/p&gt;
&lt;p&gt;How do you choose between Bigtable’s sorted keyspace model (efficient for key-range queries, expensive for non-key filters) and MongoDB’s compound index model (flexible for arbitrary predicates, costly in cache and write amplification at scale)?&lt;/p&gt;
&lt;h2 id=&quot;bigtables-lsm-tree-write-path-and-row-key-design-constraints&quot;&gt;Bigtable’s LSM-Tree Write Path and Row Key Design Constraints&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;LSM-tree mechanics.&lt;/strong&gt; Bigtable uses a Log-Structured Merge-tree (LSM-tree) write path: writes are appended to an in-memory memtable (with a write-ahead log for durability), then flushed to immutable SSTable files on Colossus (Google’s distributed file system) when the memtable fills. Compaction merges SSTables periodically, reclaiming space from deleted/overwritten cells and maintaining read performance by reducing the number of SSTable files the read path must check.&lt;/p&gt;
&lt;p&gt;The LSM-tree write path achieves high write throughput by converting random writes to sequential writes (memtable appends are sequential in memory; SSTable flushes are sequential to disk). This is the fundamental reason Bigtable can absorb billions of writes per day without storage I/O bottlenecks — writes never modify existing data in-place; they always append new data, and compaction handles the rest asynchronously.&lt;/p&gt;
&lt;p&gt;The tradeoff: reads must check the memtable + all unflushed SSTables + any compacted SSTables that overlap the row key range. Bigtable uses bloom filters per SSTable block to skip blocks that cannot contain the requested key — but for range scans across many rows, bloom filter efficiency diminishes and SSTable traversal dominates.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Row key design — the write distribution constraint.&lt;/strong&gt; Because Bigtable routes tablets based on row key sort order, monotonically increasing row keys (like a Unix timestamp as the leading component) cause all new writes to land on the rightmost tablet — the “tablet hotspot” failure mode. A device telemetry table with rows keyed as &lt;code&gt;timestamp#deviceId&lt;/code&gt; will concentrate all current-time writes on a single tablet, saturating its write throughput while all other tablets are idle.&lt;/p&gt;
&lt;p&gt;The standard mitigation is a key prefix that distributes writes across the key space:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; hashlib&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; google.cloud &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; bigtable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;client &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; bigtable.Client(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;project&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;my-project&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;admin&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;instance &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.instance(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;my-instance&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;table &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; instance.table(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;device-telemetry&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; make_row_key&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(device_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, timestamp: datetime) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    Anti-pattern: timestamp#device_id  — concentrates writes on one tablet&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    Pattern: hash_prefix#device_id#reverse_timestamp — distributes writes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    reverse_timestamp = (max_timestamp - timestamp) makes recent data sort FIRST&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    within each device, supporting &quot;get latest N readings for device&quot; efficiently.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    # 2-char hash prefix from device_id distributes ~256 tablet regions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    prefix &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; hashlib.md5(device_id.encode()).hexdigest()[:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    # Reverse timestamp: subtract from max so recent entries sort first&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    max_ts &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 9999999999&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;  # Unix timestamp far in the future&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    reverse_ts &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; max_ts &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(timestamp.timestamp())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;prefix&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;device_id&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;reverse_ts&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:010d&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; write_telemetry&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(device_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, temperature: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, timestamp: datetime):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    row_key &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; make_row_key(device_id, timestamp)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    row &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; table.direct_row(row_key)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    row.set_cell(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        column_family_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;telemetry&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        column&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;temperature&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        value&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(temperature).encode(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        timestamp_micros&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(timestamp.timestamp() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1_000_000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    row.commit()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; read_latest_n_readings&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(device_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, n: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;&quot;&quot;Read latest N readings for a device — efficient bounded key range scan.&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    prefix &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; hashlib.md5(device_id.encode()).hexdigest()[:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    max_ts &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 9999999999&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    # Start key: device prefix (smallest reverse_ts = most recent)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    start_key &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;prefix&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;device_id&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;#&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    # End key: device prefix + one character beyond (exclusive)  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end_key &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;prefix&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;device_id&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;$&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    rows &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; table.read_rows(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        start_key&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;start_key.encode(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        end_key&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;end_key.encode(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        limit&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;n&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(rows)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Bigtable Key Visualizer — detecting tablet hotspots.&lt;/strong&gt; Google Cloud Console’s Key Visualizer displays a heatmap of read/write throughput by row key range over time. A healthy Bigtable table shows uniform color distribution across the key space. A table with a monotonic leading key shows a bright right-edge column — the current tablet receiving all writes — while the remainder of the key space is dark. Key Visualizer is the primary diagnostic for Bigtable row key design issues.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Cloud Bigtable metrics via gcloud — table-level throughput and error rates&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;gcloud&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; bigtable&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; tables&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; describe&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; my-table&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --instance=my-instance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Fetch detailed metrics via Cloud Monitoring&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Key metrics for hotspot detection:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# bigtable.googleapis.com/server/request_count filtered by cluster and table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# bigtable.googleapis.com/server/latencies (p99) — high p99 on one tablet = hotspot&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# bigtable.googleapis.com/server/cpu_load — per-node CPU; skew = tablet imbalance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;mongodb-compound-index-mechanics-and-write-amplification-at-scale&quot;&gt;MongoDB Compound Index Mechanics and Write Amplification at Scale&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Index write amplification — measured.&lt;/strong&gt; Each MongoDB compound index requires a WiredTiger B-Tree write for every document operation. The write amplification ratio can be measured directly:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Measure write amplification ratio — run before and after a bulk operation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; before&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;serverStatus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().wiredTiger.cache;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; beforeWrites&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; before[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pages written from cache&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; beforeInserts&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;serverStatus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().opcounters.insert;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// ... perform 10,000 inserts ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; after&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;serverStatus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().wiredTiger.cache;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; afterWrites&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; after[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pages written from cache&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;];  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; afterInserts&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;serverStatus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().opcounters.insert;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; pagesPerInsert&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (afterWrites &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; beforeWrites) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (afterInserts &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; beforeInserts);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; indexCount&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.devices.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getIndexes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Indexes: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;indexCount&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}, Pages written per insert: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;pagesPerInsert&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Healthy: ~1.5-3x indexes (not all inserts split a page)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Concerning: &gt;5x indexes (frequent page splits during bulk insert)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Cache pressure from index pages.&lt;/strong&gt; For a MongoDB collection with 500M documents at 800 bytes average document size (400 GB total), a compound index on &lt;code&gt;{firmwareVersion: 1, lastSeen: -1}&lt;/code&gt; at 50 bytes per index key generates approximately 25 GB of uncompressed index data. With WiredTiger’s B-Tree prefix compression (~60% reduction for low-cardinality leading fields), the compressed index is ~10 GB on disk. The uncompressed WiredTiger cache footprint is 25 GB — meaning this single index occupies 25 GB of the configured &lt;code&gt;cacheSizeGB&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;On a server with 128 GB RAM and &lt;code&gt;cacheSizeGB: 76&lt;/code&gt; (60% of RAM), the document working set plus all indexes must fit within 76 GB. With multiple compound indexes, the aggregate cache pressure is the sum of all index working sets plus the active document working set — not just the document set alone.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Estimate cache impact of each index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.devices.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;stats&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().indexSizes   &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Compressed on-disk index sizes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Multiply each value by 2.5-4x for uncompressed WiredTiger cache footprint&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Monitor which indexes are actually loaded into cache (indirectly via usage)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.devices.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aggregate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;([&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  {$indexStats: {}},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  {$sort: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;accesses.ops&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  {$project: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    name: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ops: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$accesses.ops&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    since: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$accesses.since&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    diskSizeMB: {$divide: [{$arrayElemAt: [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      {$objectToArray: db.devices.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;stats&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().indexSizes}, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$$ROOT.name&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ]}, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1048576&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;The access pattern grid — choosing the physical model.&lt;/strong&gt; The documented guidance from both Google (Cloud Bigtable schema design guide) and MongoDB (data modeling documentation) converges on the same prerequisite: enumerate access patterns before choosing the physical model. The grid below shows which queries are native vs. expensive in each model:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Access Pattern&lt;/th&gt;&lt;th&gt;Bigtable Efficiency&lt;/th&gt;&lt;th&gt;MongoDB Efficiency&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Get all records for device in time range&lt;/td&gt;&lt;td&gt;✅ Native — bounded key range scan on &lt;code&gt;device#reverse_ts&lt;/code&gt; prefix&lt;/td&gt;&lt;td&gt;✅ Native — IXSCAN on &lt;code&gt;{deviceId: 1, timestamp: -1}&lt;/code&gt; compound index&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Get latest N records across all devices&lt;/td&gt;&lt;td&gt;❌ Requires full scan or maintained “latest” row&lt;/td&gt;&lt;td&gt;✅ IXSCAN on &lt;code&gt;{timestamp: -1}&lt;/code&gt; with limit&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Find devices with firmware = “2.4.1”&lt;/td&gt;&lt;td&gt;❌ Full scan with row filter — O(N)&lt;/td&gt;&lt;td&gt;✅ IXSCAN on &lt;code&gt;{firmwareVersion: 1}&lt;/code&gt; — O(log N + K)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Find devices meeting 3+ field predicates&lt;/td&gt;&lt;td&gt;❌ Multi-pass scan with compound filter — O(N) per pass&lt;/td&gt;&lt;td&gt;✅ Compound index following ESR rule&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aggregate (count, sum) across all devices by region&lt;/td&gt;&lt;td&gt;❌ Full scan — expensive but achievable&lt;/td&gt;&lt;td&gt;⚠️ COLLSCAN or partial index + aggregation pipeline&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Write 500K telemetry records/sec&lt;/td&gt;&lt;td&gt;✅ LSM-tree absorbs writes with sequential I/O&lt;/td&gt;&lt;td&gt;⚠️ WiredTiger random B-Tree writes; scales with IOPS budget&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Root Cause&lt;/th&gt;&lt;th&gt;Production Blast Radius&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Bigtable timestamp-first row key — tablet hotspot&lt;/td&gt;&lt;td&gt;Monotonically increasing leading key component routes all current writes to one tablet; tablet saturates its write throughput&lt;/td&gt;&lt;td&gt;&lt;code&gt;RESOURCE_EXHAUSTED&lt;/code&gt; or elevated write latency for all writes; Key Visualizer shows a bright right-edge heatmap column&lt;/td&gt;&lt;td&gt;Redesign row key with distributing prefix (hash of device ID, tenant ID, or geographic shard) as the leading component; test key distribution with Key Visualizer before production launch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bigtable filter on non-key field — full table scan&lt;/td&gt;&lt;td&gt;Application queries Bigtable by a field that is not expressible as a bounded row key range; &lt;code&gt;ReadRows&lt;/code&gt; with a &lt;code&gt;RowFilter&lt;/code&gt; on a non-key column reads all tablets&lt;/td&gt;&lt;td&gt;Table-level read bandwidth consumed regardless of filter selectivity; at 10B rows, a non-key filter query consumes the same I/O as reading the full table&lt;/td&gt;&lt;td&gt;Maintain alternate row representations for frequent non-key queries (pre-compute a second row with a key that matches the alternate access pattern); for ad-hoc queries, route to a downstream analytical store (BigQuery, OpenSearch) populated via Bigtable Data Boost or Change Data Capture&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bigtable row unbounded growth — row size violation&lt;/td&gt;&lt;td&gt;Time-series data accumulated in a single row without time bucketing; row exceeds 256 MB (Bigtable’s hard per-row limit) or causes cell-level lock contention&lt;/td&gt;&lt;td&gt;Write failures with &lt;code&gt;INVALID_ARGUMENT&lt;/code&gt; when row exceeds 256 MB; read latency increases as row size grows (entire row must be deserialized); high contention on frequently written rows&lt;/td&gt;&lt;td&gt;Design with time buckets in the row key (e.g., &lt;code&gt;device#YYYY-MM&lt;/code&gt; rather than &lt;code&gt;device#all-time&lt;/code&gt;); validate maximum row growth rate at design time: &lt;code&gt;bytes_per_event × events_per_second × bucket_duration_seconds &amp;#x3C; 100MB&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MongoDB compound index — ESR violation, in-memory sort abort&lt;/td&gt;&lt;td&gt;Compound index field order does not match ESR rule for the query’s filter/sort pattern; planner inserts SORT stage; result set pre-sort exceeds 100MB&lt;/td&gt;&lt;td&gt;Query aborts with &lt;code&gt;Sort exceeded memory limit of 104857600 bytes&lt;/code&gt;; application receives error for previously-working queries as data volume grows&lt;/td&gt;&lt;td&gt;Run &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; on all production query shapes; any plan containing a &lt;code&gt;SORT&lt;/code&gt; stage above the &lt;code&gt;IXSCAN&lt;/code&gt; is a latent time-bomb; rebuild compound index with correct ESR ordering&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MongoDB scatter-gather on non-shard-key filter — shard fanout&lt;/td&gt;&lt;td&gt;Sharded MongoDB collection queried by a field that is not the shard key; mongos broadcasts to all shards; response time is max(all shard latencies)&lt;/td&gt;&lt;td&gt;Latency spikes proportional to shard count; a 10-shard cluster with 5 unhealthy shards produces p99 latency equal to the slowest shard’s latency on every scatter-gather query&lt;/td&gt;&lt;td&gt;Identify scatter-gather queries via &lt;code&gt;explain()&lt;/code&gt; looking for &lt;code&gt;SHARD_MERGE&lt;/code&gt; stage; include shard key in all production queries where possible; accept explicit scatter-gather only for administrative or analytics queries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bigtable replication eventual consistency — cross-cluster read lag&lt;/td&gt;&lt;td&gt;Multi-cluster Bigtable replication delivers updates asynchronously; reads from a secondary cluster may lag writes to the primary cluster by milliseconds to seconds&lt;/td&gt;&lt;td&gt;Write-then-read patterns fail: device updates its status, status query returns stale pre-update value; for control plane applications this can cause repeated commands&lt;/td&gt;&lt;td&gt;For read-after-write consistency, configure the app profile to route reads and writes to the same cluster (single-cluster routing sacrifices availability for consistency); verify replication lag via the &lt;code&gt;bigtable.googleapis.com/replication/max_delay&lt;/code&gt; metric&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MongoDB index cache eviction — IXSCAN latency spike&lt;/td&gt;&lt;td&gt;A new index is added to a collection; index pages are not cache-resident; first IXSCAN reads index pages from disk&lt;/td&gt;&lt;td&gt;IXSCAN latency is 10–100× higher than normal during the cache warm-up period (seconds to minutes depending on index size and WiredTiger cache headroom)&lt;/td&gt;&lt;td&gt;Pre-warm index cache after creation: run representative queries against the new index during off-peak hours before routing production traffic; monitor &lt;code&gt;pages read into cache&lt;/code&gt; metric during warm-up&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bigtable Data Boost — analytical query competes with serving&lt;/td&gt;&lt;td&gt;Data Boost provides serverless compute for analytics but still reads from the same underlying SSTable files on Colossus; heavy Data Boost scans can increase tablet server latency by increasing SSTable file access contention&lt;/td&gt;&lt;td&gt;Serving latency increases during Data Boost analytical scans even though Data Boost is designed to isolate analytical workload&lt;/td&gt;&lt;td&gt;Use Data Boost exclusively for analytics that cannot be scheduled off-peak; implement SSTable-level resource quotas; monitor &lt;code&gt;server/latencies&lt;/code&gt; on serving cluster during Data Boost jobs and set a latency SLO breach alert that triggers job suspension&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;2026-bigtable-and-mongodb-update&quot;&gt;2026 Bigtable and MongoDB Update&lt;/h2&gt;
&lt;p&gt;Google Cloud Bigtable released continuous materialized views (GA in 2025) — allowing a pre-defined aggregation or projection to be maintained in near-real-time as a separate logical view, addressable by row key. This addresses the most common Bigtable access pattern gap: queries that require grouping or projection on non-key fields can now be served from a maintained view rather than requiring a full table scan. The view is subject to documented replication lag and does not support arbitrary aggregations — but for the specific case of maintaining a “latest state per device” view from a time-series table, continuous materialized views eliminate the previously required dual-write application pattern.&lt;/p&gt;
&lt;p&gt;MongoDB 8.0 (GA October 2024) shipped improved shard key analysis via &lt;code&gt;analyzeShardKey&lt;/code&gt; — providing cardinality, frequency, and monotonicity analysis for candidate shard keys before committing. The monotonicity check specifically identifies keys that would cause the “write heap” problem in a ranged-sharded collection — the same tablet hotspot failure mode that Bigtable’s sorted keyspace creates with timestamp-first row keys.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Bigtable’s sorted keyspace provides O(1) writes and O(log N) range reads for key-expressible queries, but falls back to O(N) full-table scans for any filter on a non-key field — a degradation that is invisible in benchmarks but catastrophic in production when unanticipated access patterns emerge.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Map every application access pattern against the row key before table creation; any pattern that cannot be expressed as a bounded row key range must either be modeled as an alternate row representation, served from a downstream analytical store, or accepted as O(N) cost — explicitly budgeted and capacity-planned.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Google’s Bigtable schema design documentation explicitly states: “Bigtable is not a relational database and doesn’t support joins or anything resembling SQL queries”; efficient reads require the row key to express the query’s data locality — every other query pattern incurs full or partial table scan cost.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; For your highest-traffic non-key-column filter query in Bigtable (or the equivalent COLLSCAN in MongoDB), measure its read amplification: how many rows (Bigtable) or documents (MongoDB) does it examine vs. return? That ratio is the cost of your current schema mismatch — and a direct measure of how much your access pattern inventory was incomplete at table design time.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Google Cloud Bigtable — Schema Design Best Practices and Row Key Anti-patterns: &lt;a href=&quot;https://cloud.google.com/bigtable/docs/schema-design&quot;&gt;https://cloud.google.com/bigtable/docs/schema-design&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Google Cloud Bigtable — Time-Series Schema Design and Key Distribution: &lt;a href=&quot;https://cloud.google.com/bigtable/docs/schema-design-time-series&quot;&gt;https://cloud.google.com/bigtable/docs/schema-design-time-series&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Google Cloud Bigtable — Continuous Materialized Views (GA 2025): &lt;a href=&quot;https://cloud.google.com/bigtable/docs/continuous-materialized-views&quot;&gt;https://cloud.google.com/bigtable/docs/continuous-materialized-views&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Google Cloud Bigtable — Data Boost Overview and Resource Isolation: &lt;a href=&quot;https://cloud.google.com/bigtable/docs/data-boost-overview&quot;&gt;https://cloud.google.com/bigtable/docs/data-boost-overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;WiredTiger LSM-Tree vs B-Tree Architecture — Storage Engine Design Tradeoffs: &lt;a href=&quot;https://source.wiredtiger.com/develop/arch-schema.html&quot;&gt;https://source.wiredtiger.com/develop/arch-schema.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — Sharding — analyzeShardKey and Shard Key Selection: &lt;a href=&quot;https://www.mongodb.com/docs/manual/core/sharding-shard-key/&quot;&gt;https://www.mongodb.com/docs/manual/core/sharding-shard-key/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;LevelDB and LSM-Tree Design — Google Research: &lt;a href=&quot;https://research.google/pubs/pub33023/&quot;&gt;https://research.google/pubs/pub33023/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>DynamoDB vs MongoDB: Access Pattern Determinism vs Document Query Flexibility — A Production Architecture Decision</title><link>https://rajivonai.com/blog/2026-09-19-dynamodb-vs-mongodb-access-pattern-vs-document-modeling/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-09-19-dynamodb-vs-mongodb-access-pattern-vs-document-modeling/</guid><description>DynamoDB enforces access pattern determinism at table design time — every query must be expressible as a primary key lookup or GSI scan. MongoDB defers that constraint to index design time and supports ad-hoc predicate evaluation via the query planner. This post explains the physical storage mechanics behind each model — DynamoDB&apos;s partition hash routing and GSI fan-out, MongoDB&apos;s WiredTiger B-Tree IXSCAN, and the production failure modes when the model mismatch goes undetected until scale.</description><pubDate>Sat, 19 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;DynamoDB can return a single item from a table with 10 billion records in under 2ms — not because it is faster than MongoDB per se, but because a DynamoDB GetItem operation is a direct hash lookup into a single partition. The partition key is hashed, the hash selects exactly one of the table’s internal shards, and the item is read from that shard’s B-Tree with no routing fanout, no query planner evaluation, and no index traversal across multiple storage nodes. MongoDB can match that latency for equivalent key lookups — but only if the query uses the shard key directly. If the application needs to query by a field that is not the partition key (DynamoDB) or not the shard key (MongoDB), the two systems diverge dramatically in what they require from the engineer to maintain production performance.&lt;/p&gt;
&lt;p&gt;The root decision — DynamoDB or MongoDB — is a decision about where you want to pay the access pattern tax. DynamoDB charges it at table design time: every access pattern must be modeled as a primary key, sort key, or GSI before the table is created. MongoDB charges it at index design time, which can happen iteratively as access patterns evolve. Neither approach is free — DynamoDB’s upfront cost is schema rigidity; MongoDB’s deferred cost is operational index management, cache sizing, and query planner tuning at scale.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;DynamoDB and MongoDB are both document-oriented databases that store JSON-compatible data. The similarity ends at the query layer. Their internal storage architectures make fundamentally different tradeoffs between write scalability, query flexibility, and access pattern determinism.&lt;/p&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Dimension&lt;/th&gt;&lt;th&gt;DynamoDB&lt;/th&gt;&lt;th&gt;MongoDB&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Storage engine&lt;/td&gt;&lt;td&gt;Proprietary — multi-tenant log-structured storage on SSD across partition fleet&lt;/td&gt;&lt;td&gt;WiredTiger B-Tree — single-tenant per instance, O_DIRECT I/O, internal cache pool&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Primary key model&lt;/td&gt;&lt;td&gt;Partition key (required) + sort key (optional) — determines physical placement&lt;/td&gt;&lt;td&gt;&lt;code&gt;_id&lt;/code&gt; — required; shard key (optional, for sharded collections) — determines chunk placement&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Secondary query model&lt;/td&gt;&lt;td&gt;GSI (Global Secondary Index) — replicated copy of data with alternate key; LSI (Local Secondary Index) — same partition, alternate sort&lt;/td&gt;&lt;td&gt;Compound B-Tree index — covers multiple fields with prefix-ordered traversal; no data copy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Access pattern rigidity&lt;/td&gt;&lt;td&gt;High — GSI must be pre-declared; backfill is not retroactive; table scan is O(N)&lt;/td&gt;&lt;td&gt;Moderate — indexes can be added to any field at any time; background build on live data&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Throughput model&lt;/td&gt;&lt;td&gt;Provisioned (RCU/WCU) or on-demand (per-request pricing) — capacity auto-scaled per partition&lt;/td&gt;&lt;td&gt;Instance-based — IOPS/throughput bounded by instance type, WiredTiger cache, and storage&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Transaction model&lt;/td&gt;&lt;td&gt;Single-table transactions (TransactWriteItems, up to 25 items) or cross-table (limited)&lt;/td&gt;&lt;td&gt;Multi-document ACID transactions (replica set or sharded, with cross-shard support in 4.2+)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Pricing model&lt;/td&gt;&lt;td&gt;Pay for RCU/WCU + storage + GSI RCU; scan costs full table RCU&lt;/td&gt;&lt;td&gt;Pay for instance + storage; query cost is operational, not billing&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure mode that drives teams to regret a DynamoDB or MongoDB choice at scale is almost always the same: &lt;strong&gt;an access pattern that was not anticipated at design time.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For DynamoDB, the failure is a query requirement that cannot be expressed as a primary key lookup or a declared GSI. If the product team needs to query IoT device records by &lt;code&gt;firmware_version&lt;/code&gt; — a field that was not in the original table design — there are only two options: add a GSI on &lt;code&gt;firmware_version&lt;/code&gt; (if DynamoDB’s GSI limit of 20 per table allows it and the query pattern justifies the 2× storage cost and eventual consistency lag) or perform a full table scan with a filter expression, which consumes full read capacity units for every item in the table regardless of how selective &lt;code&gt;firmware_version&lt;/code&gt; is.&lt;/p&gt;
&lt;p&gt;For MongoDB, the equivalent failure is adding indexes reactively without understanding the WiredTiger cache implications: each additional index adds write amplification (every insert, update, delete must maintain all indexes) and consumes WiredTiger cache (index pages are loaded for writes even when never used for reads). A collection with 15 indexes where 8 have zero &lt;code&gt;$indexStats&lt;/code&gt; accesses is paying 15× write amplification while only benefiting from 7 indexes’ read performance.&lt;/p&gt;
&lt;p&gt;How do you determine — before writing a line of schema code — which access patterns are primary-key-expressible (DynamoDB wins), which are query-predicate-based (MongoDB’s index flexibility helps), and which are analytically open-ended (neither model serves without a separate OLAP layer)?&lt;/p&gt;
&lt;h2 id=&quot;dynamodb-storage-internals-partition-hashing-gsi-fan-out-and-read-consistency&quot;&gt;DynamoDB Storage Internals: Partition Hashing, GSI Fan-Out, and Read Consistency&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Partition key hashing and hot partitions.&lt;/strong&gt; DynamoDB’s storage layer distributes items across internal partitions using a consistent hash of the partition key. A table with 1 million provisioned WCUs may have thousands of internal partitions; each partition handles a share of the total capacity. A table where 80% of writes target items with &lt;code&gt;customerId = &quot;megacorp&quot;&lt;/code&gt; will route 80% of write traffic to a single partition — consuming that partition’s WCU allocation and throttling writes that exceed it, while the other partitions are idle.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;DescribeTableReplicaAutoScaling&lt;/code&gt; and CloudWatch metric &lt;code&gt;ConsumedWriteCapacityUnits&lt;/code&gt; broken down by &lt;code&gt;TableName&lt;/code&gt; and &lt;code&gt;GlobalSecondaryIndexName&lt;/code&gt; reveal aggregate throttling — but DynamoDB does not expose per-partition metrics directly. Detecting hot partitions requires inferring from error patterns: &lt;code&gt;ProvisionedThroughputExceededException&lt;/code&gt; on a subset of partition key values while the table aggregate WCU appears under capacity.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; boto3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; collections &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Counter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;dynamodb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; boto3.client(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;dynamodb&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;region_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;us-east-1&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;cloudwatch &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; boto3.client(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;cloudwatch&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;region_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;us-east-1&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; detect_hot_partition_via_throttle_sampling&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(table_name: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, sample_duration_seconds: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 60&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    Sample throttled requests to infer hot partition keys.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    DynamoDB does not expose per-partition metrics — throttle patterns approximate it.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    waiter_client &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; boto3.client(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;dynamodb&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;region_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;us-east-1&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        config&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;boto3.session.Config(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;retries&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;max_attempts&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    throttled_keys &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Counter()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time.time() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sample_duration_seconds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    # This requires a sample query set representative of production traffic&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    sample_partition_keys &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; load_production_traffic_sample()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pk &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sample_partition_keys:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time.time() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; end_time:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            break&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            waiter_client.get_item(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                TableName&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;table_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                Key&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;customerId&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;S&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: pk}},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                ConsistentRead&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        except&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; waiter_client.exceptions.ProvisionedThroughputExceededException:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            throttled_keys[pk] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; throttled_keys.most_common(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Top 10 hot partition keys&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; get_table_throttle_rate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(table_name: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, minutes: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;&quot;&quot;Get table-level throttle rate from CloudWatch.&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    response &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cloudwatch.get_metric_statistics(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        Namespace&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;AWS/DynamoDB&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        MetricName&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;WriteThrottleEvents&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        Dimensions&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Name&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;TableName&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Value&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: table_name}],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        StartTime&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;time.time() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (minutes &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 60&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        EndTime&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;time.time(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        Period&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;60&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        Statistics&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Sum&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    total_throttles &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; sum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(d[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Sum&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; d &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; response[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Datapoints&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; total_throttles &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; minutes  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# throttles per minute&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;GSI fan-out and eventual consistency lag.&lt;/strong&gt; A Global Secondary Index is a separate, replicated copy of the table data with a different partition key and optional sort key. DynamoDB replicates writes to GSIs asynchronously — the documented behavior is eventual consistency. After a write to the base table, the corresponding GSI entry may lag by up to several seconds before reflecting the new value. For queries that require reading the GSI version of an item immediately after writing to the base table, this lag creates a window where the query returns stale data.&lt;/p&gt;
&lt;p&gt;The GSI also consumes WCU independently of the base table: each write to the base table that affects the GSI projected attributes triggers a write to the GSI at its separately provisioned WCU. A table with 3 GSIs and 10,000 WCU may need 40,000 WCU total (10K for base + 10K × 3 for GSI writes) for write-heavy workloads where all three GSI keys are updated on each base table write.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;DynamoDB Streams and analytics fan-out.&lt;/strong&gt; DynamoDB Streams captures changes to items in near-real-time and makes them available to Lambda triggers or Kinesis Data Streams consumers. For analytics that would otherwise require full table scans, the pattern is to fan out writes from the primary table via Streams to an OpenSearch/S3/Redshift sink — maintaining a secondary analytical representation that supports arbitrary query patterns without consuming DynamoDB read capacity.&lt;/p&gt;
&lt;h2 id=&quot;mongodb-storage-internals-wiredtiger-ixscan-shard-key-routing-and-cache-implications&quot;&gt;MongoDB Storage Internals: WiredTiger IXSCAN, Shard Key Routing, and Cache Implications&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;WiredTiger IXSCAN vs DynamoDB GetItem — the physical difference.&lt;/strong&gt; A MongoDB IXSCAN on a compound index &lt;code&gt;{customerId: 1, timestamp: -1}&lt;/code&gt; traverses a WiredTiger B-Tree stored in a private data file on the mongod host, using &lt;code&gt;O_DIRECT&lt;/code&gt; reads that bypass the OS page cache and load directly into WiredTiger’s internal cache. If the index pages are cache-resident, the IXSCAN reads entirely from memory. If the index pages are cache-evicted (working set exceeds &lt;code&gt;cacheSizeGB&lt;/code&gt;), each IXSCAN page requires a physical disk read — increasing latency from sub-millisecond (cache hit) to 0.2–5ms per page (SSD read) or 5–20ms per page (HDD).&lt;/p&gt;
&lt;p&gt;DynamoDB’s GetItem operation, by contrast, routes to a specific partition’s B-Tree node via the hash routing layer — a network round-trip to the DynamoDB storage service (typically 1–5ms from EC2 in the same region), regardless of data size. DynamoDB’s SSD fleet is shared infrastructure, so there is no cache sizing concern — but also no opportunity for sub-millisecond memory-only access as MongoDB provides for cache-resident data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Shard key selection and scatter-gather prevention.&lt;/strong&gt; In a sharded MongoDB deployment, a query that includes the shard key as an equality predicate routes to exactly one shard (targeted query). A query that omits the shard key or uses a range that spans multiple chunks routes to all shards simultaneously (scatter-gather). Scatter-gather multiplies latency: the &lt;code&gt;mongos&lt;/code&gt; router sends the query to all shards, waits for all responses, merges them, and returns the result — latency is the max of all shard latencies, not the average.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Verify query routing behavior&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.devices.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({customerId: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;abc123&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, firmwareVersion: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;2.4.1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          .&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;explain&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;executionStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// In the explain output, look for:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// &quot;queryPlanner.winningPlan.stage&quot;: &quot;SHARD_MERGE&quot;  → scatter-gather (bad)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// &quot;queryPlanner.winningPlan.stage&quot;: &quot;SINGLE_SHARD&quot; → targeted query (good)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// For the sharding plan specifically:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.devices.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;explain&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;executionStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          .&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({customerId: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;abc123&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// If customerId is the shard key: SINGLE_SHARD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// If customerId is NOT the shard key: SHARD_MERGE to all shards&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Index maintenance cost at scale.&lt;/strong&gt; Each MongoDB index on a collection imposes write amplification: every insert, update, and delete must update the corresponding B-Tree structure in WiredTiger. For a collection with 12 indexes (not uncommon in evolving applications), each insert requires 12 separate B-Tree write operations — plus the collection B-Tree itself. Under write-heavy workloads, index maintenance becomes the dominant WiredTiger I/O operation, visible in &lt;code&gt;db.serverStatus().wiredTiger.cache[&quot;pages written from cache&quot;]&lt;/code&gt; climbing disproportionately relative to document count.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Identify write amplification from indexes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;serverStatus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; pagesWritten&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; status.wiredTiger.cache[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pages written from cache&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; docsInserted&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; status.opcounters.insert;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ratio&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pagesWritten &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; docsInserted;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Write amplification ratio: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ratio&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}x per insert`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// A ratio above 10x suggests too many indexes relative to write throughput&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Find indexes with zero reads — pure write amplification overhead&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.devices.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aggregate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;([{$indexStats: {}}, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  {$project: {name: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, ops: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$accesses.ops&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  {$match: {ops: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, name: {$ne: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id_&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}}}])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;The access pattern inventory — the prerequisite decision.&lt;/strong&gt; Before choosing DynamoDB or MongoDB, the documented engineering practice (from AWS’s DynamoDB design documentation and MongoDB’s data modeling documentation) is to enumerate every access pattern as: entity, query type (lookup/range/filter), fields used, required consistency, and write frequency. Each pattern maps to either a DynamoDB primary key/GSI or a MongoDB index:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Access Pattern&lt;/th&gt;&lt;th&gt;DynamoDB Solution&lt;/th&gt;&lt;th&gt;MongoDB Solution&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;”Get device by ID”&lt;/td&gt;&lt;td&gt;GetItem on &lt;code&gt;deviceId&lt;/code&gt; (PK)&lt;/td&gt;&lt;td&gt;&lt;code&gt;find({_id: deviceId})&lt;/code&gt; — &lt;code&gt;_id&lt;/code&gt; always indexed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Get all devices for customer”&lt;/td&gt;&lt;td&gt;Query on &lt;code&gt;customerId&lt;/code&gt; (PK) or GSI if &lt;code&gt;customerId&lt;/code&gt; is not PK&lt;/td&gt;&lt;td&gt;Compound index &lt;code&gt;{customerId: 1}&lt;/code&gt; + IXSCAN&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Get devices by firmware version”&lt;/td&gt;&lt;td&gt;GSI on &lt;code&gt;firmwareVersion&lt;/code&gt; (add at design time)&lt;/td&gt;&lt;td&gt;&lt;code&gt;createIndex({firmwareVersion: 1})&lt;/code&gt; — can add post-launch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Get devices last seen in past 24h”&lt;/td&gt;&lt;td&gt;Requires &lt;code&gt;lastSeen&lt;/code&gt; as sort key or GSI sort key&lt;/td&gt;&lt;td&gt;Index &lt;code&gt;{lastSeen: -1}&lt;/code&gt; + range query&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Full-text search on device notes”&lt;/td&gt;&lt;td&gt;Not supported natively — requires OpenSearch integration&lt;/td&gt;&lt;td&gt;Atlas Search or &lt;code&gt;$text&lt;/code&gt; index (on Atlas or self-managed)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;“Devices matching complex filter (3+ conditions)“&lt;/td&gt;&lt;td&gt;Cannot — GSI max 2 key fields; complex filter = scan&lt;/td&gt;&lt;td&gt;Compound index matching the filter fields (ESR rule)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The critical column is the last one: patterns that require 3+ field predicates in an arbitrary filter are incompatible with DynamoDB’s 2-field key model without a scan or application-layer filtering. For these patterns, MongoDB’s compound index model is the better fit — as long as the index is correctly designed and the WiredTiger cache is sized for the index working set.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Root Cause&lt;/th&gt;&lt;th&gt;Production Blast Radius&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;DynamoDB hot partition — throughput throttling&lt;/td&gt;&lt;td&gt;Write traffic concentrated on a small number of partition key values (low cardinality PK or skewed distribution); DynamoDB throttles at the partition level before the table-level capacity is exhausted&lt;/td&gt;&lt;td&gt;&lt;code&gt;ProvisionedThroughputExceededException&lt;/code&gt; on writes to specific items while aggregate WCU appears underutilized; item-level retries exhaust application thread pool&lt;/td&gt;&lt;td&gt;Audit partition key cardinality before table creation; for write-skewed workloads, use a write-sharding suffix (append a random 1–10 suffix to the partition key, scatter writes, aggregate reads)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DynamoDB GSI eventual consistency — stale read immediately after write&lt;/td&gt;&lt;td&gt;Application writes to base table, immediately queries GSI for the same item; GSI replication lag (milliseconds to seconds) returns pre-write value&lt;/td&gt;&lt;td&gt;Application reads stale data; for financial or inventory applications, this creates double-spend or oversell conditions&lt;/td&gt;&lt;td&gt;For read-after-write consistency, read from the base table (consistent read) rather than the GSI; design application flows to not require immediate GSI consistency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MongoDB scatter-gather on missing shard key — query fanout&lt;/td&gt;&lt;td&gt;Application query omits the shard key predicate; mongos routes to all shards; latency is max(all shard latencies) not average&lt;/td&gt;&lt;td&gt;Query latency multiplies with shard count; a 10-shard cluster produces 10× the latency of a single-shard query for scatter-gather patterns&lt;/td&gt;&lt;td&gt;Run &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; on all production query shapes and verify &lt;code&gt;SINGLE_SHARD&lt;/code&gt; plan; any &lt;code&gt;SHARD_MERGE&lt;/code&gt; plan requires shard key inclusion or explicit scatter budget&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MongoDB index proliferation — write amplification&lt;/td&gt;&lt;td&gt;Team adds indexes reactively for each new query; collection accumulates 15+ indexes; each insert/update touches all 15 WiredTiger B-Trees&lt;/td&gt;&lt;td&gt;Write throughput degrades; insert latency climbs from 1ms to 10–50ms per document; bulk import operations stall&lt;/td&gt;&lt;td&gt;Audit indexes monthly via &lt;code&gt;$indexStats&lt;/code&gt;; drop any index with &lt;code&gt;ops: 0&lt;/code&gt; since last restart; consolidate overlapping compound indexes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DynamoDB table scan for unindexed filter — RCU exhaustion&lt;/td&gt;&lt;td&gt;An access pattern requires filtering on a non-PK, non-GSI field; application falls back to &lt;code&gt;Scan&lt;/code&gt; with &lt;code&gt;FilterExpression&lt;/code&gt;; DynamoDB charges RCU for every item scanned before the filter is applied&lt;/td&gt;&lt;td&gt;A scan of a 10M item table at 4KB average item size consumes 10M RCU — potentially thousands of dollars for a single query; with provisioned capacity, the scan exhausts all table WCU, throttling concurrent operations&lt;/td&gt;&lt;td&gt;Never rely on DynamoDB &lt;code&gt;Scan&lt;/code&gt; for production read paths; any access pattern requiring a non-key filter must be modeled as a GSI at table design time or handled by a separate search system (OpenSearch, Atlas Search)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MongoDB WiredTiger cache miss — index pages evicted&lt;/td&gt;&lt;td&gt;Application adds new indexes that collectively exceed the WiredTiger &lt;code&gt;cacheSizeGB&lt;/code&gt;; index pages for recently added indexes are not cache-resident; IXSCAN operations require disk reads&lt;/td&gt;&lt;td&gt;IXSCAN latency increases from sub-millisecond (cache hit) to 2–10ms (SSD disk read); affects all queries that use the new or competing indexes&lt;/td&gt;&lt;td&gt;Sum all index sizes via &lt;code&gt;db.collection.stats().indexSizes&lt;/code&gt; and compare to WiredTiger cache capacity; new indexes should fit within the cache headroom before deployment&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DynamoDB GSI limit reached — access pattern blocked&lt;/td&gt;&lt;td&gt;Table accumulates 20 GSIs (DynamoDB’s hard limit); a new access pattern requires a 21st GSI&lt;/td&gt;&lt;td&gt;New access pattern cannot be served without a full table redesign or migration to a different data model&lt;/td&gt;&lt;td&gt;Model all anticipated access patterns before table creation; use sparse GSIs (only project items meeting a filter condition) to reduce GSI count; consider DynamoDB Streams + a secondary index in OpenSearch for open-ended query patterns&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MongoDB resharding latency — shard key change impacts availability&lt;/td&gt;&lt;td&gt;Application discovers the original shard key causes hot shards; MongoDB resharding is initiated; during resharding (which can take hours for large collections), write operations experience additional latency while chunks are migrated&lt;/td&gt;&lt;td&gt;Write latency increases 2–5× during active resharding; for collections with billions of documents, resharding can run for 6–24 hours&lt;/td&gt;&lt;td&gt;Choose shard keys with long-term write distribution in mind; test shard key cardinality and write distribution under production-representative traffic before launch; use &lt;code&gt;analyzeShardKey&lt;/code&gt; (MongoDB 7.0+) to evaluate distribution before committing&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;2026-access-pattern-architecture-update&quot;&gt;2026 Access Pattern Architecture Update&lt;/h2&gt;
&lt;p&gt;DynamoDB introduced zero-ETL integration with Amazon OpenSearch Service (GA 2024), allowing real-time replication from DynamoDB tables to OpenSearch indexes without a Lambda or Kinesis intermediary. This eliminates the most common workaround for DynamoDB’s access pattern rigidity — supporting complex queries through a separate search tier — with lower operational overhead. For deployments requiring both DynamoDB’s partition-key throughput and MongoDB-equivalent predicate flexibility, the DynamoDB + OpenSearch zero-ETL integration is the documented architecture pattern as of 2025.&lt;/p&gt;
&lt;p&gt;MongoDB 8.0 (GA October 2024) introduced &lt;code&gt;analyzeShardKey&lt;/code&gt; improvements that provide cardinality and frequency distribution analysis for any candidate shard key before collection sharding is committed — allowing engineers to detect hot-partition risk in DynamoDB terms (low-cardinality key values) before deployment rather than discovering it under production load.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; DynamoDB and MongoDB handle unplanned access patterns oppositely — DynamoDB requires a GSI declared at design time (with 20 GSI max, eventual consistency, and 2× storage cost) while MongoDB requires an index declared any time (with no hard count limit but write amplification and cache cost); choosing the wrong model for the dominant access pattern forces expensive migrations at scale.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Enumerate every access pattern as a 5-column table (entity, query type, fields, consistency requirement, write frequency) before choosing a storage model; map each pattern to DynamoDB key/GSI or MongoDB compound index to identify which model covers 100% of patterns within its constraints.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; DynamoDB’s GetItem documentation explicitly states that partition key hash routing routes to exactly one partition, providing O(1) lookup; MongoDB’s &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; output shows IXSCAN vs COLLSCAN and &lt;code&gt;totalDocsExamined&lt;/code&gt; vs &lt;code&gt;nReturned&lt;/code&gt;, providing quantifiable evidence of index effectiveness per query shape.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; For the most complex query pattern in your current or planned application — the one that filters on the most fields simultaneously — verify whether it can be expressed as a DynamoDB primary key lookup or GSI query; if it requires a filter scan in DynamoDB or a COLLSCAN in MongoDB, that pattern is your architecture constraint and must drive the model decision.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;AWS DynamoDB Developer Guide — Partitions and Data Distribution: &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.Partitions.html&quot;&gt;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.Partitions.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;AWS DynamoDB Developer Guide — Global Secondary Indexes (eventual consistency, WCU, and projection): &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html&quot;&gt;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — Sharding — Shard Key Selection and analyzeShardKey: &lt;a href=&quot;https://www.mongodb.com/docs/manual/core/sharding-shard-key/&quot;&gt;https://www.mongodb.com/docs/manual/core/sharding-shard-key/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — $indexStats — Measuring Index Usage: &lt;a href=&quot;https://www.mongodb.com/docs/manual/reference/operator/aggregation/indexStats/&quot;&gt;https://www.mongodb.com/docs/manual/reference/operator/aggregation/indexStats/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;WiredTiger Architecture — B-Tree and Cache Internals: &lt;a href=&quot;https://source.wiredtiger.com/develop/arch-btree.html&quot;&gt;https://source.wiredtiger.com/develop/arch-btree.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;AWS DynamoDB — Best Practices for Designing and Using Partition Keys Effectively: &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-partition-key-design.html&quot;&gt;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-partition-key-design.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — Data Modeling — Document Model vs. Relational: &lt;a href=&quot;https://www.mongodb.com/docs/manual/core/data-modeling-introduction/&quot;&gt;https://www.mongodb.com/docs/manual/core/data-modeling-introduction/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>DynamoDB vs Cassandra: Managed Service or Operational Control</title><link>https://rajivonai.com/blog/2026-09-18-dynamodb-vs-cassandra-managed-vs-operational-control/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-09-18-dynamodb-vs-cassandra-managed-vs-operational-control/</guid><description>A workload-first comparison of DynamoDB and Cassandra across partitioning, consistency, multi-region behavior, capacity, and operational ownership.</description><pubDate>Fri, 18 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;DynamoDB and Cassandra share ideas from Amazon’s Dynamo paper, but the production decision is about responsibility: AWS controls DynamoDB’s machinery, while a Cassandra operator controls—and must continuously verify—the whole database.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Both systems favor query-driven schemas, high-cardinality partition keys, and denormalized access paths. Both can replicate across geographic boundaries and support conditional operations. Those similarities encourage a misleading shorthand: DynamoDB is “managed Cassandra,” or Cassandra is “portable DynamoDB.”&lt;/p&gt;
&lt;p&gt;They are not interchangeable. DynamoDB exposes tables, indexes, streams, capacity modes, consistency options, and service quotas. Cassandra exposes the token ring, replicas, consistency levels, compaction, repair, hardware, and software lifecycle. An application that fits one key model can still fail the other product’s consistency or operating constraints.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Suppose an identity service stores 2 KB session records, reads by account and expiry time, performs conditional session revocation, and runs in three regions. The sustained rate, burst ratio, hottest-account distribution, permitted staleness, and exact regional write policy are still required before capacity or cost can be estimated.&lt;/p&gt;
&lt;p&gt;The core question is: &lt;strong&gt;does the workload benefit more from DynamoDB’s bounded managed interface, or from Cassandra’s control over topology and per-operation replica coordination?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;partitioning-and-coordination&quot;&gt;Partitioning and Coordination&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[&quot;Request with partition key&quot;] --&gt; B{&quot;Platform&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|DynamoDB| C[&quot;AWS-managed physical partition&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Cassandra| D[&quot;Coordinator maps token to replicas&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; E[&quot;Table and index capacity controls&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; F[&quot;Consistency level selects acknowledgements&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; G[&quot;Service metrics and quota response&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; H[&quot;Repair, compaction, and node response&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; I[&quot;Validate application correctness&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;dynamodb-a-managed-boundary-with-physical-limits&quot;&gt;DynamoDB: A Managed Boundary with Physical Limits&lt;/h3&gt;
&lt;p&gt;DynamoDB hashes the partition-key value to place items. A composite primary key groups items by partition key and orders them by sort key. AWS recommends uniform activity across table and index keys and documents 1,000 write units and 3,000 read units per second as the design maximum for a physical partition. Item size determines unit consumption.&lt;/p&gt;
&lt;p&gt;Adaptive capacity can allocate more of a table’s capacity to uneven partitions, but it does not make one hot item unbounded. On-demand mode removes provisioned-capacity planning, not key-distribution or account-quota planning. A burst beyond the table’s recent peak or quota still requires pre-warming, quota preparation, or application backpressure according to the documented scaling behavior.&lt;/p&gt;
&lt;p&gt;Global secondary indexes are independent access paths. They propagate asynchronously, consume their own capacity and storage, and cannot serve strongly consistent reads. In provisioned mode, an under-capacity GSI can throttle base-table writes. Each GSI therefore belongs in the write-amplification and incident model.&lt;/p&gt;
&lt;h3 id=&quot;cassandra-control-is-coupled-to-correctness-work&quot;&gt;Cassandra: Control Is Coupled to Correctness Work&lt;/h3&gt;
&lt;p&gt;Cassandra hashes the partition key into a token range. Any node can coordinate a request, identify replicas from the keyspace replication strategy, and wait for the selected consistency level. With &lt;code&gt;NetworkTopologyStrategy&lt;/code&gt;, replication factors are declared per datacenter.&lt;/p&gt;
&lt;p&gt;This permits explicit tradeoffs. &lt;code&gt;LOCAL_QUORUM&lt;/code&gt; keeps the acknowledgement requirement within the coordinator’s local datacenter—a majority of that datacenter’s replicas—so the response does not wait for a remote-datacenter quorum. Cassandra still sends writes to all replicas. Lower consistency can favor availability; lightweight transactions provide linearizable compare-and-set semantics using Paxos. The tradeoff must be calculated from replication factor and failure tolerance, not inferred from the word “quorum.”&lt;/p&gt;
&lt;p&gt;Cassandra uses mutation timestamps and last-write-wins reconciliation, so clock health is part of correctness. Hints and read repair help replicas converge, but anti-entropy repair is the documented convergence mechanism. Compaction rewrites SSTables and reclaims obsolete data. Operators own the capacity, cadence, and failure handling for both.&lt;/p&gt;
&lt;h2 id=&quot;multi-region-is-not-one-feature&quot;&gt;Multi-Region Is Not One Feature&lt;/h2&gt;
&lt;p&gt;DynamoDB global tables have multiple consistency modes. Multi-Region eventual consistency uses asynchronous replication and last-writer-wins conflict resolution. Multi-Region strong consistency, introduced in 2025, has topology and feature constraints: current AWS documentation requires exactly three replica Regions or two replica Regions plus a witness Region. Existing design assumptions must be checked against the current mode rather than treating every global table as strongly consistent.&lt;/p&gt;
&lt;p&gt;Cassandra commonly stretches one logical cluster across datacenters and lets clients select local or cross-datacenter consistency levels. This offers topology control, but wide-area quorum latency and partition availability follow directly from the chosen level. Independent regional clusters plus application replication are another architecture, with a different conflict and recovery model.&lt;/p&gt;
&lt;p&gt;In either system, write down:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Which regions accept writes.&lt;/li&gt;
&lt;li&gt;What a read may return immediately after a remote write.&lt;/li&gt;
&lt;li&gt;How conflicts are prevented or resolved.&lt;/li&gt;
&lt;li&gt;Which failures sacrifice availability versus consistency.&lt;/li&gt;
&lt;li&gt;How a region is reintroduced without serving stale state.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The evidence-backed pattern is not a fabricated request-rate crossover. AWS documents partition-level capacity, GSI backpressure, on-demand scaling, and global-table consistency constraints. Cassandra documents token-based placement, tunable consistency, timestamp reconciliation, and mandatory anti-entropy repair.&lt;/p&gt;
&lt;p&gt;For the assumed session workload, run two correctness experiments before a throughput benchmark. First, race conditional revocations from separate regions and verify the resulting invariant. Second, isolate one region, continue permitted operations, reconnect it, and observe conflict resolution and convergence. Only after the semantics pass should the team measure latency and cost under the real hottest-account distribution.&lt;/p&gt;
&lt;p&gt;DynamoDB is generally favored when AWS is the intended platform, key-based access fits, and the organization wants AWS to own the database fleet. Cassandra is favored when infrastructure placement, provider independence, custom topology, or per-operation replica control is a hard requirement and a database operations team is funded. There is no defensible universal request-rate at which the answer flips.&lt;/p&gt;
&lt;h2 id=&quot;cost-and-staffing-model&quot;&gt;Cost and Staffing Model&lt;/h2&gt;
&lt;p&gt;Do not compare DynamoDB request charges with Cassandra virtual machines alone. Use a scenario model that includes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DynamoDB item sizes, table and GSI reads/writes, streams, storage, backups, cross-region replication, transfer, and headroom.&lt;/li&gt;
&lt;li&gt;Cassandra nodes per failure domain, replication, disk reserve, repair and compaction headroom, backup storage, network, observability, upgrade environments, and on-call labor.&lt;/li&gt;
&lt;li&gt;The same retention, availability target, regional topology, and recovery objective for both.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Publish the region, price date, workload trace, utilization target, and uncertainty range. Without those inputs, a three-year TCO number is marketing, not engineering evidence.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Choice&lt;/th&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Required evidence&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;DynamoDB&lt;/td&gt;&lt;td&gt;Hot key reaches a physical partition boundary&lt;/td&gt;&lt;td&gt;Key-frequency histogram and throttling-reason alarms&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DynamoDB&lt;/td&gt;&lt;td&gt;GSI propagation or capacity violates the read/write contract&lt;/td&gt;&lt;td&gt;Index-specific load and stale-read tests&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DynamoDB&lt;/td&gt;&lt;td&gt;Global-table mode does not meet regional correctness&lt;/td&gt;&lt;td&gt;Documented mode, topology, and partition drill&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cassandra&lt;/td&gt;&lt;td&gt;Repairs do not finish within policy&lt;/td&gt;&lt;td&gt;Repair history, alerts, and capacity reserve&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cassandra&lt;/td&gt;&lt;td&gt;Clock skew changes last-write-wins outcomes&lt;/td&gt;&lt;td&gt;Time-sync monitoring and conflict test&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cassandra&lt;/td&gt;&lt;td&gt;Operations team is assumed rather than staffed&lt;/td&gt;&lt;td&gt;Named ownership for upgrades, repair, compaction, backup, and restore&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Shared ancestry hides different consistency and ownership boundaries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Model keys, replica coordination, regional writes, indexes, and recurring operations explicitly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Test hot keys, conditional conflicts, region isolation, convergence, and restore with production-shaped data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Approve the platform only after the correctness contract and full-cost scenario are versioned in an ADR.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-partition-key-design.html&quot;&gt;DynamoDB partition-key design&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/on-demand-capacity-mode.html&quot;&gt;DynamoDB on-demand capacity behavior&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html&quot;&gt;DynamoDB global secondary indexes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-global-table-design.html&quot;&gt;DynamoDB global-table design&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cassandra.apache.org/doc/stable/cassandra/architecture/dynamo&quot;&gt;Cassandra Dynamo architecture and tunable consistency&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cassandra.apache.org/doc/stable/cassandra/architecture/guarantees.html&quot;&gt;Cassandra guarantees&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cassandra.apache.org/doc/stable/cassandra/architecture/storage-engine.html&quot;&gt;Cassandra storage engine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cassandra.apache.org/doc/stable/cassandra/managing/operating/compaction/overview.html&quot;&gt;Cassandra compaction overview&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category></item><item><title>Bigtable vs Cassandra: Similar Data Models, Different Operating Boundaries</title><link>https://rajivonai.com/blog/2026-09-17-bigtable-vs-cassandra-operating-models/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-09-17-bigtable-vs-cassandra-operating-models/</guid><description>A principal-engineer comparison of Bigtable and Cassandra across key design, consistency, compaction, repair, scaling, and failure ownership.</description><pubDate>Thu, 17 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Bigtable and Cassandra can model the same query-oriented wide rows, but only Cassandra makes storage-engine health, replica convergence, and software lifecycle part of your team’s production system.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Both databases reward schemas built around partition-local access. Bigtable sorts rows by row key and supports atomic operations within one row. Cassandra hashes the CQL partition key across a token ring and orders rows inside a partition by clustering columns. Both can serve entity histories, counters, and time-series workloads when keys distribute load.&lt;/p&gt;
&lt;p&gt;The resemblance stops at the operating boundary. Bigtable exposes a managed cluster, row-key and column-family controls, application profiles, and service metrics. Cassandra exposes nodes, disks, commit logs, memtables, SSTables, compaction, repair, topology, and release upgrades. That control can be a requirement; it is never free.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A migration or greenfield review often compares node counts or headline latency while holding correctness and labor implicit. That produces a false equivalence. The useful question is: &lt;strong&gt;does the organization need Cassandra’s deployment and topology control enough to own its background work, or does Bigtable’s managed boundary satisfy the application’s locality and consistency requirements?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Assume a representative workload rather than a universal benchmark: append-heavy device histories, 1 KB mutations, 30-day TTL, reads bounded to one device and time window, replication across two failure domains, and a documented tolerance for stale remote reads. Replace these assumptions with production traces before approval.&lt;/p&gt;
&lt;h2 id=&quot;storage-and-failure-ownership&quot;&gt;Storage and Failure Ownership&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[&quot;Application mutation&quot;] --&gt; B{&quot;Operating model&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Managed service| C[&quot;Bigtable tablet and distributed storage&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Self-managed database| D[&quot;Cassandra commit log and memtable&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; E[&quot;Service rebalances tablets and storage&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; F[&quot;Flush to immutable SSTables&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[&quot;Compaction and repair consume capacity&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; H[&quot;Team owns row-key and app-profile correctness&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; I[&quot;Team owns convergence and storage health&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;bigtable-managed-infrastructure-does-not-mean-schema-free&quot;&gt;Bigtable: Managed Infrastructure Does Not Mean Schema-Free&lt;/h3&gt;
&lt;p&gt;Bigtable rows are stored in lexicographic order. Exact keys, prefixes, and bounded ranges are efficient; arbitrary filters can require broad scans. A timestamp-first row key can produce a hotspot, so time-series designs normally put a distributing entity or bucket before time.&lt;/p&gt;
&lt;p&gt;Bigtable automatically splits tablets and can autoscale nodes using CPU and storage targets. These controls address capacity, not a single hot range. Google’s prescribed design loop is to test the schema and inspect Key Visualizer plus hottest-node metrics.&lt;/p&gt;
&lt;p&gt;With replication, clusters maintain independent copies and inter-cluster propagation is eventual. App profiles determine routing. Multi-cluster routing favors availability and locality, but Bigtable does not allow single-row transactions with that routing policy. Single-cluster routing to the same cluster can provide read-your-writes and can enable single-row transactions. An architecture document must name the app profile and the behavior expected during failover.&lt;/p&gt;
&lt;h3 id=&quot;cassandra-the-storage-engine-is-part-of-the-application-slo&quot;&gt;Cassandra: The Storage Engine Is Part of the Application SLO&lt;/h3&gt;
&lt;p&gt;Cassandra records a mutation in the commit log and memtable, then flushes immutable SSTables. Updates, deletes, and TTL expiry create new versions or tombstones rather than modifying data in place. Reads may consult multiple SSTables until compaction rewrites them. Compaction therefore consumes read, write, CPU, and temporary disk capacity while controlling read amplification and reclaiming obsolete data.&lt;/p&gt;
&lt;p&gt;Cassandra 5.0 documents Unified Compaction Strategy as the recommended choice for most new workloads. It does not eliminate the need to observe pending compactions, SSTable counts, disk headroom, and latency. TWCS remains relevant for mostly immutable TTL time series; LCS targets read-heavy or update-heavy workloads; each should be load-tested with the actual deletion and retention pattern.&lt;/p&gt;
&lt;p&gt;Replication also requires repair. Hinted handoff and read repair are best-effort convergence mechanisms; Cassandra’s anti-entropy repair is the mechanism documented to guarantee eventual convergence. A production design needs a repair cadence, alerting, failure-domain-aware scheduling, and evidence that repair completes inside the tombstone-safety window.&lt;/p&gt;
&lt;h2 id=&quot;consistency-is-a-configuration-not-a-product-label&quot;&gt;Consistency Is a Configuration, Not a Product Label&lt;/h2&gt;
&lt;p&gt;Bigtable provides atomic mutations within one row. In replicated instances, consistency depends on routing. Concurrent writes to the same cell identity in different clusters are resolved deterministically using server-side time, so multi-cluster write designs must accept or prevent last-writer-wins conflict resolution.&lt;/p&gt;
&lt;p&gt;Cassandra chooses consistency per operation. With replication factor &lt;code&gt;RF&lt;/code&gt;, acknowledged writes are visible to subsequent reads when the selected levels guarantee quorum intersection—commonly &lt;code&gt;W + R &gt; RF&lt;/code&gt; for the applicable replica set. &lt;code&gt;LOCAL_QUORUM&lt;/code&gt; provides that intersection only within the local datacenter; it does not make a remote datacenter synchronously current. Lightweight transactions use Paxos for linearizable compare-and-set behavior, at a higher coordination cost. Cassandra also uses mutation timestamps for last-write-wins reconciliation; clock synchronization is a correctness dependency.&lt;/p&gt;
&lt;p&gt;Do not label Bigtable “strong” or Cassandra “eventual” without the routing, replication, and operation details. State the invariant and demonstrate it during packet loss, replica unavailability, and coordinator retries.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern is that both products push data-model correctness to the application, while Cassandra adds storage lifecycle and convergence duties.&lt;/p&gt;
&lt;p&gt;For the assumed TTL history workload, the engineering action is to prototype the same logical query in each system. In Bigtable, test a distributed entity-and-time row key and observe hotspots during the largest tenant burst. In Cassandra, test partition sizes, tombstone scans, compaction throughput, disk headroom, and repair duration across the full retention cycle—not only a short ingest benchmark.&lt;/p&gt;
&lt;p&gt;The result should be a measured operating envelope. Bigtable is favored if the query is naturally a row-key range and managed Google Cloud operations are acceptable. Cassandra is favored only when deployment portability, infrastructure control, custom topology, or database-level tunable consistency justifies continuous specialist ownership. The learning is that “same data model” does not imply the same risk transfer.&lt;/p&gt;
&lt;h2 id=&quot;production-acceptance-tests&quot;&gt;Production Acceptance Tests&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Remove a failure domain while maintaining the declared consistency level.&lt;/li&gt;
&lt;li&gt;Drive the hottest real partition or row-key prefix, not uniformly random test keys.&lt;/li&gt;
&lt;li&gt;Run TTL expiration long enough to observe actual compaction and tombstone behavior.&lt;/li&gt;
&lt;li&gt;Measure P99 latency while Bigtable rebalances or Cassandra repairs and compacts.&lt;/li&gt;
&lt;li&gt;Restore into isolation, validate counts and checksums, then exercise application reads.&lt;/li&gt;
&lt;li&gt;Prove upgrades with mixed versions and a rollback boundary before production rollout.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Choice&lt;/th&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Gate before adoption&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Bigtable&lt;/td&gt;&lt;td&gt;Required query is not a key, prefix, or bounded range&lt;/td&gt;&lt;td&gt;Scan-volume measurement or a maintained alternate representation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bigtable&lt;/td&gt;&lt;td&gt;Multi-cluster routing is assumed to be immediately consistent&lt;/td&gt;&lt;td&gt;App-profile-specific staleness and failover test&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cassandra&lt;/td&gt;&lt;td&gt;Repair falls behind or has no owner&lt;/td&gt;&lt;td&gt;Repair completion SLO and overdue-repair paging&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cassandra&lt;/td&gt;&lt;td&gt;Compaction consumes disk and I/O headroom&lt;/td&gt;&lt;td&gt;Full-retention load test with failure reserve&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cassandra&lt;/td&gt;&lt;td&gt;Large or hot partitions dominate a node&lt;/td&gt;&lt;td&gt;Partition-size and key-frequency budgets with alerts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Either&lt;/td&gt;&lt;td&gt;TTL is treated as free deletion&lt;/td&gt;&lt;td&gt;Observe expiry, reclamation lag, and read impact&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Similar wide-column APIs obscure different failure ownership.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Compare schema locality, consistency topology, and recurring operational work together.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Test hotspots, expiry, failover, repair, rebalance, restore, and upgrades against an explicit SLO.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Select Cassandra only with funded operational ownership; select Bigtable only after proving row-key and routing correctness.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/schema-design&quot;&gt;Bigtable schema design best practices&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/replication-overview&quot;&gt;Bigtable replication overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/autoscaling&quot;&gt;Bigtable autoscaling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cassandra.apache.org/doc/stable/cassandra/architecture/storage-engine.html&quot;&gt;Cassandra 5.0 storage engine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cassandra.apache.org/doc/stable/cassandra/architecture/dynamo&quot;&gt;Cassandra Dynamo architecture, repair, and consistency&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cassandra.apache.org/doc/stable/cassandra/managing/operating/compaction/overview.html&quot;&gt;Cassandra 5.0 compaction overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cassandra.apache.org/doc/stable/cassandra/new/&quot;&gt;Cassandra 5.0 features&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category></item><item><title>Bigtable vs DynamoDB: Choosing a Hyperscale Operational Database</title><link>https://rajivonai.com/blog/2026-09-16-bigtable-vs-dynamodb-hyperscale-operational-database/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-09-16-bigtable-vs-dynamodb-hyperscale-operational-database/</guid><description>A workload-first comparison of ordered Bigtable row ranges and DynamoDB partition-key access, including consistency, secondary indexes, and failure boundaries.</description><pubDate>Wed, 16 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Bigtable and DynamoDB both remove fleet management, but they do not remove data modeling: one makes ordered key ranges primary, while the other makes partition-key-local access primary.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Both services can support large operational datasets and automatic distribution, so architecture reviews often reduce the choice to cloud preference or an estimated request price. That misses the durable constraint: a Bigtable row key defines ordering and range locality; a DynamoDB primary key defines item placement and the boundary of efficient queries.&lt;/p&gt;
&lt;p&gt;The comparison must therefore use a concrete envelope. Assume an event service with 2 KB writes, a 10:1 write-to-read ratio, burst traffic three times the sustained rate, 90-day retention, and two required reads: fetch one entity’s recent events and retrieve one event by external identifier. These are assumptions for reasoning, not vendor benchmarks.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The entity-history query fits both products when the entity identifier distributes traffic. The external-identifier lookup creates a second access path. In Bigtable it requires another row representation or a continuous materialized view where its constraints fit. In DynamoDB it commonly requires a global secondary index. Either design adds physical writes, storage, lag semantics, and recovery work.&lt;/p&gt;
&lt;p&gt;The decision is not “which database scales further?” It is: &lt;strong&gt;which key layout makes the dominant query local, and can the secondary path remain correct during skew, throttling, and recovery?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;ordered-rows-versus-partition-key-queries&quot;&gt;Ordered Rows Versus Partition-Key Queries&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[&quot;Application write&quot;] --&gt; B{&quot;Primary access model&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Ordered history| C[&quot;Bigtable row key — entity and reversed time&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Item collection| D[&quot;DynamoDB key — entity and event time&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; E[&quot;Primary row mutation&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; F[&quot;Base table item&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; G[&quot;Derived lookup representation&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; H[&quot;Global secondary index update&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; I[&quot;Validate lag, restore, and hotspot behavior&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;bigtable-row-order-is-a-performance-contract&quot;&gt;Bigtable: Row Order Is a Performance Contract&lt;/h3&gt;
&lt;p&gt;Bigtable stores rows in lexicographic row-key order. Efficient operations use an exact row key, a prefix, or a bounded range. A key such as &lt;code&gt;tenant#device#reverse_timestamp&lt;/code&gt; can co-locate recent history for a device. Starting with a timestamp is unsafe because sequential inserts can concentrate on one key range; Google explicitly recommends a high-cardinality prefix.&lt;/p&gt;
&lt;p&gt;The transaction boundary is one row. Check-and-mutate and read-modify-write operations can protect an invariant contained in that row, but the design should not imply atomicity across multiple rows. Keep related atomic data together without producing oversized rows; Google recommends keeping rows below 100 MB and enforces a 256 MB row limit.&lt;/p&gt;
&lt;p&gt;Bigtable separates compute from its distributed storage layer, and tablets split and rebalance as load and data change. This does not make key choice irrelevant. Key Visualizer and the hottest-node metrics are required proof that a prefix is distributing traffic. Autoscaling responds to CPU and storage targets, but it cannot repair a schema that directs most requests to one range.&lt;/p&gt;
&lt;h3 id=&quot;dynamodb-capacity-is-consumed-by-items-and-indexes&quot;&gt;DynamoDB: Capacity Is Consumed by Items and Indexes&lt;/h3&gt;
&lt;p&gt;DynamoDB supports a simple partition key or a composite partition-and-sort key. Queries within one partition key can use sort-key conditions, making an entity-plus-time item collection a natural history representation.&lt;/p&gt;
&lt;p&gt;AWS documents per-physical-partition design limits of 1,000 write units and 3,000 read units per second. A write unit covers an item up to 1 KB, so the assumed 2 KB event consumes two write units before indexes or replication. Adaptive capacity can shift capacity toward unevenly active keys, but a single hot key still has a finite boundary.&lt;/p&gt;
&lt;p&gt;A global secondary index has its own partition and sort key, projection, storage, and capacity behavior. AWS documents that insufficient provisioned GSI write capacity can throttle base-table writes. GSI reads are eventually consistent. This means the external-identifier path needs an explicit stale-read and duplicate-write policy rather than an assumption that it behaves like an immediately consistent second primary key.&lt;/p&gt;
&lt;h2 id=&quot;correctness-and-geography&quot;&gt;Correctness and Geography&lt;/h2&gt;
&lt;p&gt;Bigtable replication creates independent cluster copies. With multi-cluster routing, replication is eventual; single-cluster routing can provide read-your-writes within that cluster but changes failover behavior. App profiles also affect whether single-row transactions are available. The topology must state which profile each workload uses.&lt;/p&gt;
&lt;p&gt;DynamoDB strongly consistent reads are available on tables and local secondary indexes in a single Region, but not on GSIs. Global tables replicate across Regions. Multi-Region eventual consistency and multi-Region strong consistency are different modes with different topology and feature constraints; MRSC is not a checkbox that can be assumed for every existing table.&lt;/p&gt;
&lt;p&gt;For either product, define:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Whether stale reads are allowed after a regional write.&lt;/li&gt;
&lt;li&gt;How conflicts or duplicate retries are resolved.&lt;/li&gt;
&lt;li&gt;Whether failover is automatic or an application/runbook action.&lt;/li&gt;
&lt;li&gt;How derived access paths are rebuilt to the same recovery point as base data.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented operational pattern is to validate distribution rather than infer it from aggregate traffic. Google’s schema workflow says to test a proposed Bigtable key and inspect Key Visualizer and monitoring for hotspots. AWS exposes throttling reason codes that distinguish table and index capacity from key-range limits.&lt;/p&gt;
&lt;p&gt;For the assumed event service, a defensible evaluation would replay a measured key-frequency distribution—not uniform synthetic IDs—and then inject the largest observed tenant burst. Bigtable should be evaluated using hottest-node CPU, latency, and Key Visualizer. DynamoDB should be evaluated using consumed capacity, throttled requests, and throttling reasons for both the base table and GSI. The result is a workload-specific capacity envelope, not a universal throughput claim.&lt;/p&gt;
&lt;p&gt;Choose Bigtable when ordered prefix and range reads dominate, Google Cloud is the intended failure domain, and the application can own explicit alternate representations. Choose DynamoDB when AWS-native key-value access, conditional item operations, and declared secondary indexes are the better fit. Neither conclusion follows from total request rate alone.&lt;/p&gt;
&lt;h2 id=&quot;verification-plan&quot;&gt;Verification Plan&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Replay P50, P95, and maximum observed item sizes.&lt;/li&gt;
&lt;li&gt;Preserve production tenant and key skew in the generator.&lt;/li&gt;
&lt;li&gt;Test sustained load, burst load, and one deliberately hot key separately.&lt;/li&gt;
&lt;li&gt;Measure the base table and every derived index independently.&lt;/li&gt;
&lt;li&gt;Exercise regional routing or failover and record observed staleness.&lt;/li&gt;
&lt;li&gt;Restore into an isolated environment and prove both access paths return the same logical dataset.&lt;/li&gt;
&lt;li&gt;Price the tested topology, including replicas, indexes, backups, network transfer, and headroom.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Design&lt;/th&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Required control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Bigtable timestamp-first key&lt;/td&gt;&lt;td&gt;Sequential traffic heats one range&lt;/td&gt;&lt;td&gt;High-cardinality prefix plus Key Visualizer validation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bigtable arbitrary predicate&lt;/td&gt;&lt;td&gt;Filter becomes a broad scan&lt;/td&gt;&lt;td&gt;New query-oriented row representation or reject the workload&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DynamoDB low-cardinality partition key&lt;/td&gt;&lt;td&gt;One key reaches a partition boundary&lt;/td&gt;&lt;td&gt;Redesign key or introduce write sharding with read fan-out&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DynamoDB underprovisioned GSI&lt;/td&gt;&lt;td&gt;Index backpressure throttles base writes&lt;/td&gt;&lt;td&gt;Capacity alarms, on-demand evaluation, and sparse projections&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Either multi-region design&lt;/td&gt;&lt;td&gt;Application assumes immediate global visibility&lt;/td&gt;&lt;td&gt;Explicit consistency contract and failover test&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Either secondary path&lt;/td&gt;&lt;td&gt;Restore produces mismatched base and derived data&lt;/td&gt;&lt;td&gt;Versioned rebuild and reconciliation procedure&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Aggregate throughput hides key skew and derived-write cost.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Model exact access paths, item sizes, consistency requirements, and regional topology.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Replay production-shaped traffic and validate the hottest range or partition under failure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Approve a product only with a reviewed key schema, capacity model, restore procedure, and revalidation trigger.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/schema-design&quot;&gt;Bigtable schema design best practices&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/schema-design-steps&quot;&gt;Bigtable schema design process&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/replication-overview&quot;&gt;Bigtable replication overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/autoscaling&quot;&gt;Bigtable autoscaling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-partition-key-design.html&quot;&gt;DynamoDB partition-key design&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html&quot;&gt;DynamoDB global secondary indexes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/throttling-key-range-limit-exceeded-mitigation.html&quot;&gt;DynamoDB throttling diagnosis&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-global-table-design.html&quot;&gt;DynamoDB global-table design&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category></item><item><title>Bigtable vs DynamoDB vs Cassandra vs MongoDB: A NoSQL Decision Framework</title><link>https://rajivonai.com/blog/2026-09-15-nosql-decision-framework/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-09-15-nosql-decision-framework/</guid><description>A workload-first framework for choosing among four distributed databases without substituting product categories for engineering requirements.</description><pubDate>Tue, 15 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The expensive NoSQL mistake is not choosing the database with the lowest benchmark latency. It is choosing a data model whose failure boundary, consistency contract, or operating model conflicts with the application.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Bigtable, DynamoDB, Cassandra, and MongoDB can all distribute data and serve high request volumes, but they expose different abstractions. Bigtable orders rows lexicographically. DynamoDB routes items by partition key and optionally orders them by sort key. Cassandra distributes CQL partitions across a peer-to-peer cluster. MongoDB stores BSON documents and can distribute collections by ranged or hashed shard keys.&lt;/p&gt;
&lt;p&gt;Those are not implementation details to compare after a vendor shortlist. They determine which queries are local, which writes contend, which guarantees cross a region, and which failure modes the team must own.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Selection exercises often begin with an imprecise target such as “one million writes per second.” That number is insufficient. A 200-byte append with uniform keys is not equivalent to a 20 KB conditional update concentrated on one tenant. The same average rate can require radically different capacity when bursts, indexes, replication, item size, and skew are included.&lt;/p&gt;
&lt;p&gt;Before choosing a service, define a workload envelope:&lt;/p&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Requirement&lt;/th&gt;&lt;th&gt;Evidence to collect&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Access paths&lt;/td&gt;&lt;td&gt;Every latency-sensitive read and write, including administrative and repair paths&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Distribution&lt;/td&gt;&lt;td&gt;Key cardinality, hottest-key share, tenant skew, and time correlation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Object shape&lt;/td&gt;&lt;td&gt;P50 and P99 item or partition size, update frequency, and retention&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Correctness&lt;/td&gt;&lt;td&gt;Atomicity boundary, conditional-write needs, read consistency, and conflict policy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Geography&lt;/td&gt;&lt;td&gt;Writer locations, failover topology, residency, and measured replication lag tolerance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Demand&lt;/td&gt;&lt;td&gt;Sustained and burst rates by operation, payload size, and index fan-out&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Operations&lt;/td&gt;&lt;td&gt;Upgrade, repair, rebalance, backup, restore, and incident ownership&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Economics&lt;/td&gt;&lt;td&gt;Regional prices, replicas, indexes, storage growth, headroom, egress, and labor&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The core question is therefore: &lt;strong&gt;which system makes the required access paths and correctness boundaries native while leaving an operating model the organization can verify?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;a-constraint-first-decision-framework&quot;&gt;A Constraint-First Decision Framework&lt;/h2&gt;
&lt;p&gt;Start with disqualifiers, not scores. A weighted spreadsheet can hide a fatal mismatch by averaging it with minor advantages.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[&quot;Document the workload envelope&quot;] --&gt; B{&quot;Need rich document predicates and evolving indexes?&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes| C[&quot;Evaluate MongoDB and its shard-key implications&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No| D{&quot;Need ordered prefix or range scans at very large scale?&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Yes| E[&quot;Evaluate Bigtable and row-key hotspot controls&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|No| F{&quot;Need an AWS-managed key-value service?&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|Yes| G[&quot;Evaluate DynamoDB partition and index capacity&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|No| H{&quot;Can the team own repair, compaction, and upgrades?&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|Yes| I[&quot;Evaluate Cassandra for topology control&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|No| J[&quot;Revisit managed services or reduce requirements&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; K[&quot;Prototype hottest and rarest access paths&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This tree identifies a starting point, not a winner. Each candidate must still pass correctness, resilience, and cost tests.&lt;/p&gt;
&lt;h3 id=&quot;bigtable-ordered-access-is-the-primary-design-lever&quot;&gt;Bigtable: Ordered Access Is the Primary Design Lever&lt;/h3&gt;
&lt;p&gt;Bigtable is strongest when reads can be expressed as a row-key lookup, prefix scan, or contiguous range scan. Rows are ordered lexicographically, and transactions are limited to a single row. Google recommends designing row keys from planned reads, keeping related rows contiguous, and avoiding timestamp-first or monotonically increasing keys that concentrate traffic.&lt;/p&gt;
&lt;p&gt;That makes Bigtable a natural candidate for high-volume telemetry, event histories, and entity-plus-time access. It is a poor default for arbitrary predicates or multi-row transactional invariants. Secondary access requires another representation, such as a continuous materialized view where supported, and must be included in the write and recovery model.&lt;/p&gt;
&lt;h3 id=&quot;dynamodb-partition-key-locality-and-explicit-access-paths&quot;&gt;DynamoDB: Partition-Key Locality and Explicit Access Paths&lt;/h3&gt;
&lt;p&gt;DynamoDB is strongest when operations are expressible as item access or a query within a partition key. A composite key can order related items by sort key. Global secondary indexes provide alternative keys, but they are separate replicated structures: their capacity, propagation, projection size, and hot keys are part of the system design.&lt;/p&gt;
&lt;p&gt;AWS documents a design ceiling of 1,000 write units and 3,000 read units per second for a physical partition. Adaptive capacity helps uneven workloads, but it does not remove the need for high-cardinality keys or protect an unbounded hot item. Item size also changes consumed capacity, so “requests per second” alone is not a capacity model.&lt;/p&gt;
&lt;h3 id=&quot;cassandra-topology-control-comes-with-repair-ownership&quot;&gt;Cassandra: Topology Control Comes with Repair Ownership&lt;/h3&gt;
&lt;p&gt;Cassandra is strongest when a team needs control of placement, hardware, replication factor, and per-operation consistency. Its partitions are distributed by consistent hashing; replicas accept mutations, and consistency levels determine how many replicas must acknowledge a read or write before the coordinator responds. Cassandra still sends writes to all replicas for the partition regardless of the acknowledgement level. Correctness depends on replication, consistency levels, clock discipline, repair, and data model—not simply on the label “masterless.”&lt;/p&gt;
&lt;p&gt;Cassandra’s local LSM engine turns updates and deletes into new immutable SSTable entries. Compaction and repair are correctness and capacity activities, not optional housekeeping. Cassandra 5.0 recommends Unified Compaction Strategy for most new workloads, but workload validation is still required, especially for TTL-heavy or unusually read-heavy tables.&lt;/p&gt;
&lt;h3 id=&quot;mongodb-query-flexibility-still-requires-a-distribution-key&quot;&gt;MongoDB: Query Flexibility Still Requires a Distribution Key&lt;/h3&gt;
&lt;p&gt;MongoDB is strongest when the document aggregate matches application ownership and evolving queries require multiple indexes. Within a sharded collection, the shard key determines placement and routing. Queries containing the shard key or its prefix can be targeted; other queries may be broadcast. Hashed sharding improves distribution for monotonically changing values but weakens range locality.&lt;/p&gt;
&lt;p&gt;Indexes are not free flexibility. They consume storage and cache, add write work, and can create a second scaling constraint. Sharding also changes uniqueness rules: unless &lt;code&gt;_id&lt;/code&gt; is the shard key, MongoDB does not enforce &lt;code&gt;_id&lt;/code&gt; uniqueness across the entire sharded cluster. These constraints belong in application design reviews.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern across all four systems is query-first modeling:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Bigtable’s schema guidance says to rank planned queries and validate row keys with Key Visualizer and monitoring.&lt;/li&gt;
&lt;li&gt;DynamoDB’s partition-key guidance says to design for uniform activity across base-table and index keys and to calculate capacity with item size.&lt;/li&gt;
&lt;li&gt;Cassandra’s documentation ties visibility of acknowledged writes to quorum intersection—commonly expressed as &lt;code&gt;W + R &gt; RF&lt;/code&gt; within the applicable replica set—with repair required to guarantee eventual replica convergence.&lt;/li&gt;
&lt;li&gt;MongoDB’s sharding documentation says the shard key controls distribution and whether a query can target shards rather than broadcast.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is CARL evidence derived from documented behavior, not a claim that one product wins every workload. The engineering action is to turn production traces into a replayable candidate test. The result to seek is not a peak throughput number; it is bounded latency and correct results under skew, node or zone loss, index fan-out, and recovery. The learning is that a database decision remains provisional until those boundaries are exercised.&lt;/p&gt;
&lt;h2 id=&quot;architecture-review-gate&quot;&gt;Architecture Review Gate&lt;/h2&gt;
&lt;p&gt;A principal-level review should require answers to these questions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;What percentage of production operations are single-key, prefix/range, or secondary-predicate queries?&lt;/li&gt;
&lt;li&gt;Can the hottest tenant or key exceed a physical partition’s serviceable capacity?&lt;/li&gt;
&lt;li&gt;Which invariants require compare-and-set, multi-document, multi-row, or multi-region coordination?&lt;/li&gt;
&lt;li&gt;What happens to reads during replication lag or regional isolation?&lt;/li&gt;
&lt;li&gt;How many physical writes does one logical write create across indexes, replicas, streams, and materialized views?&lt;/li&gt;
&lt;li&gt;Can a restore reproduce the base data and every derived access path to the same recovery point?&lt;/li&gt;
&lt;li&gt;Which team owns shard-key migration, repair, compaction, or service quota management?&lt;/li&gt;
&lt;li&gt;Has the cost model been replayed against P50, P95, and failure-mode demand rather than averages?&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Choice&lt;/th&gt;&lt;th&gt;Mismatch that should stop adoption&lt;/th&gt;&lt;th&gt;Proof required before approval&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Bigtable&lt;/td&gt;&lt;td&gt;Critical queries cannot use a row key, prefix, or bounded range&lt;/td&gt;&lt;td&gt;Trace replay plus scan-volume and hottest-node metrics&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DynamoDB&lt;/td&gt;&lt;td&gt;A single hot key must exceed its partition budget or transactional scope is broader than supported&lt;/td&gt;&lt;td&gt;Key-frequency histogram and correctness tests under throttling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cassandra&lt;/td&gt;&lt;td&gt;No team owns repair, compaction, upgrades, capacity, and clock health&lt;/td&gt;&lt;td&gt;Failure drill, repair SLO, restore test, and staffing plan&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MongoDB&lt;/td&gt;&lt;td&gt;Required queries scatter across shards or index working set exceeds the memory plan&lt;/td&gt;&lt;td&gt;&lt;code&gt;explain&lt;/code&gt; evidence, shard targeting, cache metrics, and rebalance test&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;No fixed throughput threshold belongs in this table. The rejection boundary must be calculated from the workload, current service limits, and measured behavior.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Product categories hide the physical work behind each request.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Define the workload envelope and eliminate candidates that violate a hard correctness or operational constraint.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Replay representative and adversarial traffic, including hot keys, index fan-out, failover, repair, and restore.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Record the decision as an ADR with source versions, assumptions, test artifacts, rejection reasons, and a date for revalidation.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/schema-design&quot;&gt;Bigtable schema design best practices&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/bigtable/docs/schema-design-steps&quot;&gt;Bigtable schema design process&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-partition-key-design.html&quot;&gt;DynamoDB partition-key design&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html&quot;&gt;DynamoDB global secondary indexes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cassandra.apache.org/doc/stable/cassandra/architecture/dynamo&quot;&gt;Cassandra 5.0 Dynamo architecture and tunable consistency&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cassandra.apache.org/doc/stable/cassandra/managing/operating/compaction/overview.html&quot;&gt;Cassandra compaction overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/manual/sharding/&quot;&gt;MongoDB sharding&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/manual/core/sharding-shard-key/&quot;&gt;MongoDB shard keys&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>Building a Database-as-a-Service Control Plane for 10,000 Oracle Databases Across 100 Regions</title><link>https://rajivonai.com/blog/2026-06-20-building-dbaas-control-plane-oracle-fleet-100-regions/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-20-building-dbaas-control-plane-oracle-fleet-100-regions/</guid><description>Terraform, Ansible, and central FPP servers collapse past 500 databases. This post details the three-tier architecture — Global Fleet Catalog, Regional Temporal Workflow Engines, and pull-based Host Agents — that enables intent-driven Oracle database lifecycle automation at global fleet scale.</description><pubDate>Sat, 12 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Push-based automation collapses past 500 Oracle databases on WAN binary distribution, Terraform state lock contention, and unbounded blast radius; operating 10,000 databases across 100 regions requires inverting to a three-tier pull control plane with fail-static host agents.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Pushing &lt;code&gt;10 GB&lt;/code&gt; Oracle binaries to &lt;code&gt;10,000&lt;/code&gt; databases (&lt;code&gt;100 TB&lt;/code&gt; aggregate WAN traffic) or running &lt;code&gt;terraform apply&lt;/code&gt; across &lt;code&gt;10,000&lt;/code&gt; workspaces saturates transoceanic links, triggers daily state-lock collisions, and propagates bugs globally in seconds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Split control into three tiers: a strongly consistent Global Fleet Catalog (desired state), autonomous Regional Temporal Engines paired with local Object Storage mirrors, and pull-based Go host agents that poll every 60 seconds and fail static when disconnected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; A regional cache that outlives a global patch freeze lets partitioned cells keep rolling out updates—cap regional desired-state cache TTL at 2 hours before halting new mutations.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-20-building-dbaas-control-plane-oracle-fleet-100-regions.svg&quot; alt=&quot;Invert push automation to pull-based regional control planes&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Global database platforms span dozens of commercial, sovereign, and dedicated cloud regions to satisfy sub-50ms application latency and data-residency mandates, with 50 to 150 Oracle databases per region ranging from multi-tenant PDBs to multi-terabyte RAC clusters with Active Data Guard standbys.&lt;/p&gt;
&lt;p&gt;At 100 to 500 databases, standard push tooling works: Terraform provisions compute and block volumes, Ansible pushes configuration changes over SSH, and a centralized Oracle Fleet Patching and Provisioning (FPP) server distributes golden homes. For OCI-native estates, Oracle provides &lt;strong&gt;OCI Fleet Application Management&lt;/strong&gt; for compliance policies across Base and Exadata Database Services, &lt;strong&gt;Exadata Fleet Update&lt;/strong&gt; for scale-out Exadata/GI maintenance, and &lt;strong&gt;OCI Database Management&lt;/strong&gt; for observability. Yet when a multi-cloud or self-managed fleet grows from 500 toward 10,000 databases across 100 regions, push-based orchestration hits structural limits.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Centralized push automation breaks between 500 and 10,000 databases under four concrete engineering constraints.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. WAN binary distribution math.&lt;/strong&gt; Pushing a &lt;code&gt;10 GB&lt;/code&gt; Oracle golden home or Release Update from a central FPP hub or Ansible controller to &lt;code&gt;10,000&lt;/code&gt; hosts transfers &lt;code&gt;10,000 * 10 GB = 100 TB&lt;/code&gt; across inter-region WAN links. Even at &lt;code&gt;10 Gbps&lt;/code&gt; (&lt;code&gt;1.25 GB/s&lt;/code&gt; theoretical), transferring &lt;code&gt;100 TB&lt;/code&gt; serially takes &lt;code&gt;80,000&lt;/code&gt; seconds (&lt;code&gt;22.2&lt;/code&gt; hours) of sustained line-rate traffic—and concurrent bursts drop TCP connections, leaving half-copied binaries. Pre-staging the &lt;code&gt;10 GB&lt;/code&gt; artifact once per region (&lt;code&gt;100 * 10 GB = 1 TB&lt;/code&gt; total inter-region transfer) cuts WAN egress by &lt;code&gt;100x&lt;/code&gt; and lets hosts pull locally over regional Object Storage.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Terraform workspace state contention.&lt;/strong&gt; Managing &lt;code&gt;10,000&lt;/code&gt; independent Terraform workspaces in Object Storage turns a routine global parameter change into &lt;code&gt;10,000&lt;/code&gt; sequential or batched &lt;code&gt;plan&lt;/code&gt;/&lt;code&gt;apply&lt;/code&gt; runs. At &lt;code&gt;60&lt;/code&gt; seconds per workspace, serial execution takes &lt;code&gt;600,000&lt;/code&gt; seconds (&lt;code&gt;166.7&lt;/code&gt; hours, or nearly 7 days). Parallelizing runs collides on state locks whenever a network blip aborts an &lt;code&gt;apply&lt;/code&gt;, forcing manual &lt;code&gt;terraform force-unlock&lt;/code&gt; cleanup.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. Temporal coupling between orchestrator and data plane.&lt;/strong&gt; Push controllers require live network reachability to every target VM at execution time. If the central controller crashes or a transoceanic link drops mid-play, the fleet splits into partially updated and un-updated nodes with no local convergence loop.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. Unbounded global blast radius.&lt;/strong&gt; A bad Ansible task or Terraform module pushed concurrently from one global controller can corrupt hundreds of databases within a 5-second detection window before an operator cancels the run.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-20-building-dbaas-control-plane-oracle-fleet-100-regions-2-constraints.svg&quot; alt=&quot;Push automation collapses past 500 DBs on WAN and locks&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do you provision, patch, and govern 10,000 Oracle databases across 100 regions without WAN binary saturation, workspace lock collisions, or orchestrator-coupled outages?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Architecture&lt;/th&gt;&lt;th&gt;Viable Fleet Ceiling&lt;/th&gt;&lt;th&gt;Failure Mode at Scale&lt;/th&gt;&lt;th&gt;Blast Radius&lt;/th&gt;&lt;th&gt;Why Chosen or Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Ansible push playbooks&lt;/td&gt;&lt;td&gt;~500 databases&lt;/td&gt;&lt;td&gt;SSH fan-out exhaustion; WAN saturation&lt;/td&gt;&lt;td&gt;Global controller reach&lt;/td&gt;&lt;td&gt;Rejected — temporal coupling and partial split states&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Per-DB Terraform workspaces&lt;/td&gt;&lt;td&gt;~1,000 databases&lt;/td&gt;&lt;td&gt;&lt;code&gt;166h&lt;/code&gt; serial apply; stale state locks&lt;/td&gt;&lt;td&gt;Per-module blast&lt;/td&gt;&lt;td&gt;Rejected — slow global convergence and lock recovery&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Centralized FPP server&lt;/td&gt;&lt;td&gt;~500 databases&lt;/td&gt;&lt;td&gt;Hub binary push SPOF; limited non-Oracle scope&lt;/td&gt;&lt;td&gt;Hub reachability&lt;/td&gt;&lt;td&gt;Rejected — central transfer bottleneck across 100 regions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Three-tier pull control plane&lt;/td&gt;&lt;td&gt;10,000+ databases&lt;/td&gt;&lt;td&gt;Isolated per-region/per-agent failures&lt;/td&gt;&lt;td&gt;Cell ring + 15% gate&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Chosen&lt;/strong&gt; — regional autonomy and fail-static agents&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-20-building-dbaas-control-plane-oracle-fleet-100-regions-3-options.svg&quot; alt=&quot;Three-tier pull control plane scales past Ansible and FPP&quot;&gt;&lt;/p&gt;
&lt;p&gt;FPP, OCI Fleet Application Management, and Exadata Fleet Update are strong fits for homogeneous Oracle-managed or under-1,000-database estates. Above that threshold across 100 regions—where lifecycle operations also span monitoring agents, TLS certificate rotation, and custom backup catalogs—a three-tier pull architecture isolates regional network partitions and caps blast radius by cell.&lt;/p&gt;
&lt;h2 id=&quot;the-distributed-dbaas-control-plane-architecture&quot;&gt;The Distributed DBaaS Control Plane Architecture&lt;/h2&gt;
&lt;p&gt;The design inverts control from centralized push to a three-tier pull hierarchy: a &lt;strong&gt;Global Fleet Catalog&lt;/strong&gt;, &lt;strong&gt;Regional Temporal Control Planes&lt;/strong&gt;, and &lt;strong&gt;Pull-Based Go Host Agents&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-20-building-dbaas-control-plane-oracle-fleet-100-regions-4-architecture.svg&quot; alt=&quot;Global catalog replicates intent to autonomous regional cells&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Global Fleet Catalog — Raft or Data Guard metadata] --&gt;|Replicate desired state| B[Regional Control Plane — Temporal cluster]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|2h max cache TTL| C[Regional Object Storage — 10 GB golden images]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[Go Host Agent — systemd on DB VM] --&gt;|Poll every 60s| B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Pull local image| C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Fail static if unreachable| E[Oracle Database Engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Check V$SQL p99 below 15pct| F[Ring Promotion Gate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|Halt on regression| A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Tier 1 — Global Fleet Catalog.&lt;/strong&gt; A strongly consistent metadata store (using Raft consensus or Active Data Guard-replicated Autonomous Database) holds the declarative specification for all 10,000 databases: target Oracle version, golden image OCID, patch ring, replication topology, and an &lt;code&gt;in_progress&lt;/code&gt; operation lock per database. It never SSHs into database hosts.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tier 2 — Regional Control Planes (Temporal Workflow Engines).&lt;/strong&gt; Each of the 100 regions runs an independent Temporal cluster paired with a regional Object Storage mirror holding pre-staged golden images. If transoceanic fiber disconnects a region from the Global Catalog, the regional Temporal cluster continues executing health checks and in-flight workflows using its local desired-state cache—capped at a &lt;strong&gt;2-hour TTL&lt;/strong&gt; so a partitioned region cannot ignore a global patch freeze indefinitely.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tier 3 — Pull-Based Go Host Agents.&lt;/strong&gt; A single Go binary runs as a &lt;code&gt;systemd&lt;/code&gt; service on every database VM, polling its Regional Control Plane every 60 seconds. Notice how &lt;code&gt;getCurrentOracleVersion()&lt;/code&gt; passes SQL via &lt;code&gt;cmd.Stdin&lt;/code&gt; (never shell-only &lt;code&gt;&amp;#x3C;&amp;#x3C;&amp;#x3C;&lt;/code&gt; syntax inside &lt;code&gt;exec.Command&lt;/code&gt;, which fails without a shell) and how &lt;code&gt;pollDesiredState()&lt;/code&gt; enforces the &lt;strong&gt;fail-static&lt;/strong&gt; rule:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;package&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; main&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;encoding/json&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;fmt&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;net/http&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;os/exec&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;strings&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; DesiredState&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	OracleVersion   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; `json:&quot;oracle_version&quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	GoldenImageID   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; `json:&quot;golden_image_id&quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	MinAgentVersion &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; `json:&quot;min_agent_version&quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	PatchRing       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; `json:&quot;patch_ring&quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; getCurrentOracleVersion&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	cmd &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; exec.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Command&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sqlplus&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-S&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/ as sysdba&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	cmd.Stdin &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; strings.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;NewReader&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SET HEADING OFF FEEDBACK OFF&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;SELECT version FROM v$instance;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EXIT;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	out, err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cmd.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Output&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Errorf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sqlplus version query failed: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%w&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, err)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; strings.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;TrimSpace&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(out)), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; pollDesiredState&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;dbUniqueName&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;regionalURL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;DesiredState&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	client &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;http&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Client&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{Timeout: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time.Second}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	req, err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; http.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;NewRequest&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;GET&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Sprintf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%s&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/v2/databases/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%s&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/desired-state&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, regionalURL, dbUniqueName), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, err&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	req.Header.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Set&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Accept&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;application/vnd.fleet.v2+json&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	resp, err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Do&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(req)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Errorf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;control plane unreachable — fail static: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%w&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, err)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	defer&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; resp.Body.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Close&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; state &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;DesiredState&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;NewDecoder&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(resp.Body).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Decode&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;state); err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Errorf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;invalid desired state — fail static: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%w&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, err)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	return&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;state, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Cell-based rings and telemetry-gated promotion.&lt;/strong&gt; The Global Fleet Catalog assigns every database to a ring (&lt;code&gt;Ring-0&lt;/code&gt; internal test with 48-hour soak, &lt;code&gt;Ring-1&lt;/code&gt; through &lt;code&gt;Ring-3&lt;/code&gt; regional production waves with 24-hour soaks, and &lt;code&gt;Ring-N&lt;/code&gt; Tier-0 critical databases with 72-hour soak). Before promoting a ring, the Regional Control Plane compares &lt;code&gt;V$SQL&lt;/code&gt; &lt;code&gt;p99&lt;/code&gt; elapsed time across the active ring against pre-patch baselines and halts promotion automatically if any database regresses by more than &lt;code&gt;15%&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; validate_ring_health&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ring: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, patch_version: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, catalog, metrics) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; catalog.query(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;ring&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ring, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;oracle_version&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;patch_version):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        baseline_p99 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; metrics.get_baseline_p99(db.unique_name)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        current_p99 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; metrics.query_current_p99(db.unique_name)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; baseline_p99 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; and&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ((current_p99 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; baseline_p99) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; baseline_p99) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100.0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 15.0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; metrics.alert_halt(db.unique_name, ring, patch_version)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; False&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Agent self-update and per-database operation locking.&lt;/strong&gt; Before executing any lifecycle change, the Go agent compares its compiled binary version against &lt;code&gt;DesiredState.MinAgentVersion&lt;/code&gt;. If an upcoming Oracle 26ai &lt;code&gt;datapatch&lt;/code&gt; or volume-pivot change requires agent &lt;code&gt;2.0&lt;/code&gt;, older agents pull the signed &lt;code&gt;2.0&lt;/code&gt; binary from regional Object Storage, verify its SHA-256 checksum, and restart cleanly via &lt;code&gt;systemd&lt;/code&gt; before touching Oracle. Simultaneously, the Global Fleet Catalog enforces a single &lt;code&gt;in_progress&lt;/code&gt; mutex per &lt;code&gt;db_unique_name&lt;/code&gt; so two overlapping ring promotions can never trigger concurrent compute pivots on the same database.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Heartbeat timeouts and regional cache TTLs.&lt;/strong&gt; When a Go agent begins a destructive or stateful lifecycle step such as a block-volume compute pivot, it acquires a Temporal activity lease and emits heartbeats every 60 seconds. If the agent crashes after stopping the old VM and never provisions the replacement compute node, the Regional Temporal cluster detects the missed heartbeat within 15 minutes, aborts the pivot workflow, issues a cloud API reboot on the original VM so Oracle restarts on its prior golden image, and pages the on-call engineer. Conversely, if a transoceanic fiber cut isolates a Regional Control Plane from the Global Fleet Catalog, the regional cell continues serving cached desired state for up to 2 hours. Once the cache age crosses 2 hours, the regional cell transitions to read-only health monitoring and refuses new lifecycle mutations until global connectivity—and any pending global patch freeze—is confirmed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;API versioning across 10,000 heterogeneous hosts.&lt;/strong&gt; Rolling out a new Fleet Catalog field across 10,000 agents cannot be atomic. Every Go agent sends &lt;code&gt;Accept: application/vnd.fleet.v2+json&lt;/code&gt; on its 60-second poll, and Regional Control Planes maintain backward compatibility for both &lt;code&gt;N&lt;/code&gt; and &lt;code&gt;N-1&lt;/code&gt; schema versions. Agents receiving an unrecognized schema or HTTP &lt;code&gt;4xx&lt;/code&gt;/&lt;code&gt;5xx&lt;/code&gt; response fall back to fail-static mode rather than guessing defaults, ensuring schema migrations never trigger unintended database restarts.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Worker pool sizing and global wave throttling.&lt;/strong&gt; Even with regional Temporal clusters, launching 10,000 patch workflows simultaneously would overwhelm a static 100-slot worker pool, queueing 9,900 executions for days. Each regional cluster sizes its Temporal worker pool at expected peak concurrency plus 50% headroom, autoscales workers via Kubernetes HPA on &lt;code&gt;temporal_workflow_execution_count&lt;/code&gt;, and enforces a global wave ceiling of 1,000 concurrent active workflows so compliance windows remain predictable.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Migrating from push scripts to a 100-region pull control plane rolls out in four gated phases.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-20-building-dbaas-control-plane-oracle-fleet-100-regions-5-flow.svg&quot; alt=&quot;Prove fail-static agents in one region before 100-region scale&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 1: Fail-Static Agent Chaos Pilot.&lt;/strong&gt; Deploy the Go polling agent in read-only shadow mode on 20 non-production VMs, then sever network access to the Regional API endpoint. Gate: &lt;code&gt;0&lt;/code&gt; database disruptions and &lt;code&gt;100%&lt;/code&gt; clean fail-static log entries over 72 hours. Rollback: stop the &lt;code&gt;systemd&lt;/code&gt; unit (&lt;code&gt;systemctl stop dbaas-agent&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 2: Single-Region Temporal Cell and Local Mirror.&lt;/strong&gt; Stand up one Regional Temporal cluster, replicate golden images to that region’s Object Storage bucket (plus two neighboring fallback regions), and validate the 15-minute pivot heartbeat timeout. Gate: simulated mid-pivot agent crash triggers automatic old-VM reboot within 15 minutes. Rollback: revert the Fleet Catalog desired state to current version.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 3: Telemetry-Gated Ring Promotion.&lt;/strong&gt; Enable &lt;code&gt;Ring-0&lt;/code&gt; (48-hour soak) and &lt;code&gt;Ring-1&lt;/code&gt; (24-hour soak) in the pilot region with the &lt;code&gt;&gt;15%&lt;/code&gt; &lt;code&gt;V$SQL&lt;/code&gt; regression gate and API versioning (&lt;code&gt;Accept: application/vnd.fleet.v2+json&lt;/code&gt;). Gate: &lt;code&gt;0&lt;/code&gt; unapproved ring promotions and verified per-DB &lt;code&gt;in_progress&lt;/code&gt; lock rejection on concurrent test triggers. Rollback: set global freeze flag in Fleet Catalog.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 4: 100-Region Federation.&lt;/strong&gt; Roll out Regional Temporal cells across all remaining regions with the 2-hour regional cache TTL, HPA-autoscaled Temporal workers, and a global concurrency cap of &lt;code&gt;1,000&lt;/code&gt; active workflows per wave. Gate: full fleet convergence within compliance SLA with &lt;code&gt;&amp;#x3C;1%&lt;/code&gt; manual interventions. Rollback: halt new waves at the Global Catalog; running databases remain unaffected.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Pull-model fail-static semantics (Pattern A):&lt;/strong&gt; As documented in Google’s Borg paper and &lt;em&gt;“Borg, Omega, and Kubernetes”&lt;/em&gt; (&lt;em&gt;ACM Queue&lt;/em&gt;, Vol. 14, No. 1, 2016), decoupling control-plane availability from data-plane execution ensures that control-plane partitions or restarts never drop active workloads; agents hold their last known good state until connectivity returns.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Temporal multi-region autonomy (Pattern A):&lt;/strong&gt; Per Temporal’s multi-region architecture documentation, running region-local Temporal clusters keeps workflow execution, activity heartbeats, and retry timers local to each region so transoceanic latency never stalls local database operations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;OCI Object Storage replication for artifact locality (Pattern A):&lt;/strong&gt; Pre-staging golden images into regional Object Storage buckets (with cross-region replication to two neighbor regions) removes &lt;code&gt;100 TB&lt;/code&gt; central WAN pushes and eliminates single-bucket availability stalls.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle managed fleet tooling scope (Pattern B):&lt;/strong&gt; For OCI-native estates under 1,000 databases, OCI Fleet Application Management, Exadata Fleet Update, and Oracle FPP provide built-in compliance and patching runbooks; the custom three-tier Temporal plane is warranted when fleets span multiple clouds, 100+ regions, or non-Oracle host lifecycle tasks.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Breaks How&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Zombie agent mid-pivot&lt;/td&gt;&lt;td&gt;Agent stops old VM, then crashes before new VM boots&lt;/td&gt;&lt;td&gt;Temporal 15-minute activity heartbeat times out&lt;/td&gt;&lt;td&gt;Workflow reboots old VM via cloud API and pages on-call&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Stale regional cache&lt;/td&gt;&lt;td&gt;Partitioned region misses global freeze and keeps patching&lt;/td&gt;&lt;td&gt;Regional sync age metric exceeds 2 hours&lt;/td&gt;&lt;td&gt;Enforce 2-hour cache TTL; halt new mutations after expiry&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Concurrent ring promotion race&lt;/td&gt;&lt;td&gt;Two patch waves target the same DB simultaneously&lt;/td&gt;&lt;td&gt;Catalog lock conflict on &lt;code&gt;db_unique_name&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Enforce single &lt;code&gt;in_progress&lt;/code&gt; mutex per DB in Fleet Catalog&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Temporal worker pool exhaustion&lt;/td&gt;&lt;td&gt;10,000 workflows flood a 100-slot worker pool&lt;/td&gt;&lt;td&gt;&lt;code&gt;temporal_workflow_execution_count&lt;/code&gt; queue depth spikes&lt;/td&gt;&lt;td&gt;HPA-scale workers + cap global wave at 1,000 active&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Agent version drift&lt;/td&gt;&lt;td&gt;Agent &lt;code&gt;1.9&lt;/code&gt; runs outdated &lt;code&gt;datapatch&lt;/code&gt; syntax on 26ai&lt;/td&gt;&lt;td&gt;&lt;code&gt;CurrentAgentVersion &amp;#x3C; MinAgentVersion&lt;/code&gt; in poll&lt;/td&gt;&lt;td&gt;Self-update agent binary from regional bucket before pivot&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Catalog schema breaks old cells&lt;/td&gt;&lt;td&gt;Unversioned JSON field breaks older regional parsers&lt;/td&gt;&lt;td&gt;&lt;code&gt;4xx&lt;/code&gt;/decode errors spike on &lt;code&gt;desired-state&lt;/code&gt; poll&lt;/td&gt;&lt;td&gt;Version media types (&lt;code&gt;vnd.fleet.v2+json&lt;/code&gt;); support N and N-1&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-20-building-dbaas-control-plane-oracle-fleet-100-regions-6-failures.svg&quot; alt=&quot;Four failure paths threaten 100-region control planes&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Invert binary distribution from central push to regional pull:&lt;/strong&gt; Pre-staging a &lt;code&gt;10 GB&lt;/code&gt; golden image once per region (&lt;code&gt;1 TB&lt;/code&gt; across 100 regions) eliminates the &lt;code&gt;100 TB&lt;/code&gt; WAN bottleneck of pushing from a central controller to 10,000 hosts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Enforce fail-static behavior on every host agent:&lt;/strong&gt; If the Regional Control Plane is unreachable or returns malformed JSON, the agent must take zero action so control-plane outages never become database outages.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bound regional cache autonomy with a 2-hour TTL:&lt;/strong&gt; Regional Temporal cells should survive brief WAN partitions, but must halt new lifecycle mutations if disconnected from the Global Catalog past 2 hours to honor global freezes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Serialize operations per database and version the agent API:&lt;/strong&gt; A per-database &lt;code&gt;in_progress&lt;/code&gt; mutex prevents concurrent ring races, and &lt;code&gt;min_agent_version&lt;/code&gt; self-updates prevent binary/schema drift across 10,000 VMs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate every ring on &lt;code&gt;V$SQL&lt;/code&gt; telemetry, not timers:&lt;/strong&gt; Automated promotion must verify &lt;code&gt;p99&lt;/code&gt; SQL elapsed time has not regressed by &lt;code&gt;&gt;15%&lt;/code&gt; before advancing from &lt;code&gt;Ring-0&lt;/code&gt; to global waves.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Push-based tools (Ansible, per-DB Terraform workspaces, centralized FPP) collapse past 500–1,000 Oracle databases due to WAN binary saturation, state-lock contention, and unbounded blast radius.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Deploy a three-tier pull control plane—Global Fleet Catalog, Regional Temporal clusters with local Object Storage mirrors, and fail-static Go host agents—to govern 10,000 databases across 100 regions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Verify the Go agent’s &lt;code&gt;sqlplus&lt;/code&gt; stdin version check and fail-static behavior under simulated Regional API outage, and confirm a 15-minute Temporal heartbeat timeout recovers a simulated mid-pivot agent crash.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Deploy a read-only 60-second polling daemon on one staging Oracle VM that compares &lt;code&gt;SELECT version FROM v$instance&lt;/code&gt; against a central JSON endpoint and logs drift without mutating state.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Google Research — &lt;em&gt;Large-scale cluster management at Google with Borg&lt;/em&gt; (EuroSys 2015): &lt;a href=&quot;https://research.google/pubs/large-scale-cluster-management-at-google-with-borg/&quot;&gt;https://research.google/pubs/large-scale-cluster-management-at-google-with-borg/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Burns et al. — &lt;em&gt;Borg, Omega, and Kubernetes&lt;/em&gt; (&lt;em&gt;ACM Queue&lt;/em&gt;, Vol. 14, No. 1, 2016): &lt;a href=&quot;https://queue.acm.org/detail.cfm?id=2898444&quot;&gt;https://queue.acm.org/detail.cfm?id=2898444&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Temporal Documentation — Multi-Region / Multi-Cluster Architecture: &lt;a href=&quot;https://docs.temporal.io/cloud/multi-region&quot;&gt;https://docs.temporal.io/cloud/multi-region&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OCI Fleet Application Management Documentation: &lt;a href=&quot;https://docs.oracle.com/en-us/iaas/fleet-application-management/index.html&quot;&gt;https://docs.oracle.com/en-us/iaas/fleet-application-management/index.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Oracle Fleet Patching and Provisioning (FPP) Guide: &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/cwadd/oraclet-fleet-patching-and-provisioning.html&quot;&gt;https://docs.oracle.com/en/database/oracle/oracle-database/19/cwadd/oraclet-fleet-patching-and-provisioning.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OCI Object Storage — Cross-Region Replication: &lt;a href=&quot;https://docs.oracle.com/en-us/iaas/Content/Object/Tasks/usingreplication.htm&quot;&gt;https://docs.oracle.com/en-us/iaas/Content/Object/Tasks/usingreplication.htm&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>checklist</category></item><item><title>Autonomous Oracle Vulnerability Management: Integrating Update Advisor with Immutable CI/CD Pipelines</title><link>https://rajivonai.com/blog/2026-06-16-oracle-update-advisor-api-patch-automation/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-16-oracle-update-advisor-api-patch-automation/</guid><description>Manual Oracle CPU patching across 1,200 databases creates six-month compliance cycles. This post shows how to build a control plane that polls Oracle Update Advisor, triggers an immutable golden image rebuild, generates infrastructure-as-code PRs, and executes block-volume pivot patching — reducing vulnerability exposure from months to days.</description><pubDate>Thu, 10 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;If patching 1,200 Oracle databases for a quarterly Critical Patch Update requires DBAs to read advisories, file change tickets, and run &lt;code&gt;opatch&lt;/code&gt; in place on live hosts, the pipeline fails its 14-day CVSS 9.0+ remediation SLA by construction.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; At 1,200 databases, a 14-day remediation window requires patching &lt;code&gt;85.7&lt;/code&gt; databases per day, while in-place &lt;code&gt;opatch&lt;/code&gt; mutates live &lt;code&gt;$ORACLE_HOME&lt;/code&gt; binaries across divergent OS library states.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Ingest patch advisories programmatically via Oracle Update Advisor and OCI Cloud Guard, build patched golden images offline with Packer, open a GitOps Terraform PR, and pivot compute nodes across telemetry-gated rings while keeping ASM data volumes intact.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Retaining prior golden images for only 72 hours leaves no fast pivot rollback if a subtle optimizer regression surfaces in Week 2—keep prior images for 7 days (30 days for emergency CVSS 9.0+ patches).&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-16-oracle-update-advisor-api-patch-automation.svg&quot; alt=&quot;Replace in-place opatch with API-driven golden image pivots&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Oracle releases quarterly Critical Patch Updates (CPUs) and Release Updates (RUs) every January, April, July, and October. Under regulated frameworks (PCI DSS v4.0 Requirement 6.3, SOC 2, HIPAA), vulnerabilities scored CVSS 9.0 or higher typically carry 14-day remediation SLAs. At small scale, a DBA reads the advisory PDF, downloads the RU from My Oracle Support (MOS), and runs &lt;code&gt;opatch apply&lt;/code&gt; during a weekend window.&lt;/p&gt;
&lt;p&gt;At fleet scale, two programmatic interfaces replace manual advisory triage. The &lt;strong&gt;Oracle Update Advisor (OUA) REST API&lt;/strong&gt; evaluates registered Oracle Homes and recommends applicable patches (with Oracle recommending batching multiple assets per request to minimize API overhead), while &lt;strong&gt;OCI Cloud Guard&lt;/strong&gt; exposes unpatched database posture findings via &lt;code&gt;CloudGuardClient.list_problems()&lt;/code&gt;. Coupling those feeds to an immutable golden-image pipeline removes per-host &lt;code&gt;opatch&lt;/code&gt; execution from production entirely.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;In-place &lt;code&gt;opatch&lt;/code&gt; patching across 1,200 self-managed Oracle databases breaks under four concrete operational constraints.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. 14-day remediation velocity math.&lt;/strong&gt; Patching &lt;code&gt;1,200&lt;/code&gt; databases within a &lt;code&gt;14&lt;/code&gt;-day CVSS 9.0+ SLA requires an average throughput of &lt;code&gt;1,200 / 14 = 85.7&lt;/code&gt; databases per day—every day, including weekends. If a manual advisory review and Change Advisory Board (CAB) cycle consumes 3 weeks (&lt;code&gt;21&lt;/code&gt; days) before the first production &lt;code&gt;opatch&lt;/code&gt; runs, the SLA is already breached by 7 days on Day 0 of execution. By the time a 6-to-10-week weekend schedule finishes, the next quarterly CPU arrives and the fleet never reaches 100% compliance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. In-place binary state mutation.&lt;/strong&gt; Running &lt;code&gt;opatch apply&lt;/code&gt; on a live host modifies &lt;code&gt;$ORACLE_HOME&lt;/code&gt; binaries in place. An interrupted binary copy or inventory lock leaves the home partially patched, requiring manual &lt;code&gt;opatch rollback&lt;/code&gt; or home cloning before the database can reopen cleanly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. OS and library divergence across host vintages.&lt;/strong&gt; Servers provisioned in 2022 versus 2024 accumulate different &lt;code&gt;glibc&lt;/code&gt;, kernel, and &lt;code&gt;yum&lt;/code&gt;/&lt;code&gt;dnf&lt;/code&gt; package states. A Release Update that links cleanly on a 2024 VM can fail relinking on a 2022 VM with the identical Oracle version, turning an automated Ansible loop into manual DBA debugging.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. Multi-hour rollback windows.&lt;/strong&gt; Reverting a regression after in-place &lt;code&gt;opatch&lt;/code&gt; requires scheduling a second outage window to run &lt;code&gt;opatch rollback&lt;/code&gt; and &lt;code&gt;datapatch -rollback&lt;/code&gt; per host—doubling the blast radius and coordination cost.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-16-oracle-update-advisor-api-patch-automation-2-constraints.svg&quot; alt=&quot;In-place opatch fails on 14-day velocity and host drift&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do you patch 1,200 Oracle databases within 14 days of a CPU release without mutating live &lt;code&gt;$ORACLE_HOME&lt;/code&gt; binaries, without hitting OS library drift, and with minute-level rollback?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Architecture&lt;/th&gt;&lt;th&gt;Detection Latency&lt;/th&gt;&lt;th&gt;Velocity at 1,200 DBs&lt;/th&gt;&lt;th&gt;Rollback Mechanism&lt;/th&gt;&lt;th&gt;Why Chosen or Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Manual advisory + &lt;code&gt;opatch&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Days to weeks&lt;/td&gt;&lt;td&gt;Fails (&amp;#x3C;20 DBs/weekend)&lt;/td&gt;&lt;td&gt;Manual &lt;code&gt;opatch rollback&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Rejected — O(N) DBA bottleneck misses 14-day SLA&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Ansible in-place &lt;code&gt;opatch&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Minutes via scan&lt;/td&gt;&lt;td&gt;Moderate; stalls on OS drift&lt;/td&gt;&lt;td&gt;Re-run rollback play&lt;/td&gt;&lt;td&gt;Rejected — mutates live homes across divergent OS states&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Golden image + volume pivot&lt;/td&gt;&lt;td&gt;Minutes via API&lt;/td&gt;&lt;td&gt;High; parallel ring pivots&lt;/td&gt;&lt;td&gt;Reprovision prior image&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Chosen&lt;/strong&gt; — patches once offline; fast image rollback&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Oracle Autonomous Database&lt;/td&gt;&lt;td&gt;Vendor-managed&lt;/td&gt;&lt;td&gt;Vendor-managed quarterly&lt;/td&gt;&lt;td&gt;Vendor-managed&lt;/td&gt;&lt;td&gt;Rejected for fleet — does not cover self-managed VM/EC2&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-16-oracle-update-advisor-api-patch-automation-3-options.svg&quot; alt=&quot;Immutable image pivots beat in-place opatch at 1,200 DBs&quot;&gt;&lt;/p&gt;
&lt;p&gt;Ansible in-place patching automates SSH execution, but still runs &lt;code&gt;opatch&lt;/code&gt; 1,200 separate times against 1,200 mutable filesystems. An immutable golden-image pipeline compiles and validates the patched &lt;code&gt;$ORACLE_HOME&lt;/code&gt; &lt;strong&gt;once&lt;/strong&gt; inside an ephemeral Packer build VM, then rolls that verified artifact across rings via compute replacement and block-volume reattachment.&lt;/p&gt;
&lt;h2 id=&quot;the-declarative-patch-automation-control-plane&quot;&gt;The Declarative Patch Automation Control Plane&lt;/h2&gt;
&lt;p&gt;The control plane separates vulnerability ingestion, offline image construction, GitOps promotion, and ringed compute pivots into four decoupled stages. No DBA logs into a production host to run &lt;code&gt;opatch&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-16-oracle-update-advisor-api-patch-automation-4-architecture.svg&quot; alt=&quot;Control plane links Update Advisor, Packer, GitOps, and OCI&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Update Advisor and Cloud Guard API — new CPU detected] --&gt; B[Temporal Control Plane — query Fleet Catalog]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[Packer Image Factory — offline opatch apply from mirror]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[Validation Gate — datapatch -verbose -prereq]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Fail| E[Halt pipeline and alert platform team]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Pass| F[GitOps PR — update golden image_id in tfvars]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Staging CI — AWR replay p99 check]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|Pass| H[Ring 0 Canary — 48h V$SQL telemetry soak]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|Pass| I[Ring 1 10pct — 24h soak then Ring 2-N fleet]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Vulnerability ingestion via OCI Cloud Guard and Update Advisor.&lt;/strong&gt; The control plane polls OCI Cloud Guard’s &lt;code&gt;list_problems()&lt;/code&gt; API using Instance Principals (eliminating expiring API keys) and batches asset queries to the Oracle Update Advisor REST API to map CVSS &lt;code&gt;&gt;= 9.0&lt;/code&gt; advisories to affected &lt;code&gt;ORACLE_HOME&lt;/code&gt; versions in the Fleet Catalog:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dataclasses &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dataclass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oci&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;@dataclass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; PatchRecommendation&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    patch_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ru_version: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cvss_score: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;float&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    detected_at: datetime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; get_open_oracle_findings&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(compartment_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, home_version: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; list[PatchRecommendation]:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    signer &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oci.auth.signers.InstancePrincipalsSecurityTokenSigner()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cg &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oci.cloud_guard.CloudGuardClient({}, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;signer&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;signer)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    items &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cg.list_problems(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        compartment_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;compartment_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        lifecycle_state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;OPEN&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        resource_type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;OracleDatabase&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        sort_by&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;RISKLEVELS&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        sort_order&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;DESC&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ).data.items&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    recs: list[PatchRecommendation] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; item &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; items:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        details &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; item.additional_details &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;or&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; home_version &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; details.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;affected_versions&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, []):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            recs.append(PatchRecommendation(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                patch_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;details.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;patch_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                ru_version&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;details.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;target_version&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                cvss_score&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(details.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;cvss_score&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0.0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                detected_at&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;item.time_first_detected,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            ))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; recs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Offline Packer golden image build.&lt;/strong&gt; Rather than pulling patches directly from MOS inside ephemeral build VMs (where contract or IP allowlist hiccups stall builds), infosec stages the RU zip in an internal OCI Object Storage bucket. Packer downloads the archive, applies it to an &lt;strong&gt;offline&lt;/strong&gt; &lt;code&gt;$ORACLE_HOME&lt;/code&gt; using &lt;code&gt;opatch apply -silent&lt;/code&gt; (without the deprecated &lt;code&gt;-ocmrf&lt;/code&gt; flag removed in modern OPatch), and runs &lt;code&gt;datapatch -verbose -prereq&lt;/code&gt; against a freshly cloned scratch database:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -euo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pipefail&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RU_PATCH_ID&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;${1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:?&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Usage&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; build_patched_home&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sh&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RU_PATCH_ID&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/u01/app/oracle/product/19.0.0/dbhome_1&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RU_PATCH_ZIP&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/tmp/p${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RU_PATCH_ID&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}_190000_Linux-x86-64.zip&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;oci&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; os&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; object&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; get&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --bucket-name&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; oracle-patches&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --name&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RU_PATCH_ZIP&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;##*/&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --file&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RU_PATCH_ZIP&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mkdir&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -p&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /tmp/patch_staging&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;unzip&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -q&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RU_PATCH_ZIP&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /tmp/patch_staging&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ORACLE_HOME&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;}/OPatch/opatch&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; apply&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;/tmp/patch_staging/${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RU_PATCH_ID&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -silent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;}/OPatch/opatch&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; lsinventory&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -q&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RU_PATCH_ID&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;}/OPatch/datapatch&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -verbose&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -prereq&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; 2&gt;&amp;#x26;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; tee&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /tmp/datapatch_prereq.log&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -q&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;No issues found&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /tmp/datapatch_prereq.log&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;GitOps PR generation and &lt;code&gt;DBMS_QOPATCH&lt;/code&gt; verification.&lt;/strong&gt; Once Packer publishes the new image OCID (tagged by compute architecture—&lt;code&gt;x86_64&lt;/code&gt; or &lt;code&gt;aarch64&lt;/code&gt;—so Ampere and x86 fleets never cross-apply binaries), the control plane opens a Terraform PR updating &lt;code&gt;oracle_golden_image_id&lt;/code&gt; in &lt;code&gt;.auto.tfvars.json&lt;/code&gt;. Staging CI runs anonymized AWR SQL replay against the candidate image to catch plan regressions before merge.&lt;/p&gt;
&lt;p&gt;During the pivot itself, the control plane never replaces an active primary compute node directly when a Data Guard physical standby is present. Instead, it pivots the standby node first: the host agent stops the standby database cleanly, dismounts all ASM diskgroups (&lt;code&gt;srvctl stop diskgroup -diskgroup DATA,RECO&lt;/code&gt;), detaches the OCI block volumes via &lt;code&gt;oci compute volume-attachment detach&lt;/code&gt;, provisions the replacement compute instance from the patched golden image OCID, reattaches the exact ASM block-volume OCIDs, mounts ASM, and starts Managed Recovery (&lt;code&gt;MRP0&lt;/code&gt;). Once the standby catches up to zero apply lag, Temporal triggers a Data Guard switchover so applications fail over to the patched node in seconds. The retired primary then undergoes the identical volume pivot and runs &lt;code&gt;datapatch&lt;/code&gt; across the CDB and all open PDBs (&lt;code&gt;datapatch -verbose&lt;/code&gt;)—applying any SQL dictionary changes once per Data Guard pair rather than twice. After &lt;code&gt;datapatch&lt;/code&gt; completes, the control plane verifies patch registration from inside SQL using Oracle’s &lt;code&gt;DBMS_QOPATCH&lt;/code&gt; package, confirming both binary inventory and SQL registry state match the target RU:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; DBMS_QOPATCH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;GET_OPATCH_LSINVENTORY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; inventory_xml &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; DUAL;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Why GitOps PRs sit between Packer and Temporal.&lt;/strong&gt; Generating an automated Pull Request rather than mutating Terraform state directly preserves a full audit trail for SOC 2 and PCI DSS auditors: every production binary change maps to a signed commit, an automated AWR staging test run, and an explicit approval record for emergency CVSS 9.0+ promotions. If a Ring 0 regression appears, reverting the GitOps PR and triggering a compute pivot back to the retained prior image OCID restores the exact pre-patch binary and OS state without SSHing into hosts or running &lt;code&gt;opatch rollback&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Ringed promotion gates every wave on live &lt;code&gt;V$SQL&lt;/code&gt; telemetry rather than wall-clock timers alone.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-16-oracle-update-advisor-api-patch-automation-5-flow.svg&quot; alt=&quot;Telemetry gates promote patches from canary to full fleet&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 1: Image Build and Staging AWR Replay.&lt;/strong&gt; Packer builds the patched golden image offline, validates &lt;code&gt;datapatch -verbose -prereq&lt;/code&gt;, and opens the GitOps PR. Staging CI executes a 50-session concurrency replay of anonymized top AWR SQL. Gate: &lt;code&gt;0&lt;/code&gt; &lt;code&gt;datapatch&lt;/code&gt; prereq errors and staging &lt;code&gt;p99&lt;/code&gt; SQL elapsed time within &lt;code&gt;15%&lt;/code&gt; of baseline. Rollback: close the PR without modifying any production Terraform state.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 2: Ring 0 Canary (Standby-First, 48-Hour Soak).&lt;/strong&gt; Pivot non-production and Data Guard physical standby nodes first, switch over to test primary load, and monitor &lt;code&gt;V$SQL&lt;/code&gt; and &lt;code&gt;DBMS_QOPATCH&lt;/code&gt; for 48 hours. The telemetry gate compares the post-pivot &lt;code&gt;ELAPSED_TIME / NULLIF(EXECUTIONS, 0)&lt;/code&gt; distribution for the top 50 &lt;code&gt;SQL_ID&lt;/code&gt;s against the 7-day pre-patch baseline stored in the Fleet Catalog. Gate: &lt;code&gt;DBMS_QOPATCH&lt;/code&gt; confirms the target RU is active, &lt;code&gt;DBA_REGISTRY_SQLPATCH&lt;/code&gt; shows &lt;code&gt;SUCCESS&lt;/code&gt; across all PDBs, and high-frequency &lt;code&gt;V$SQL&lt;/code&gt; &lt;code&gt;p99&lt;/code&gt; elapsed time shows &lt;code&gt;&amp;#x3C;15%&lt;/code&gt; regression. Rollback: reprovision compute from the prior golden image OCID and run &lt;code&gt;datapatch -rollback&lt;/code&gt; if SQL dictionary changes were applied.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 3: Ring 1 (10% Fleet, 24-Hour Soak).&lt;/strong&gt; Promote 10% of production databases in parallel batches, executing Data Guard switchover before pivoting the former primary so client downtime stays under a single switchover window. Gate: zero ORA-00600/ORA-07445 alert-log spikes and &lt;code&gt;&amp;#x3C;15%&lt;/code&gt; &lt;code&gt;V$SQL&lt;/code&gt; elapsed-time drift. Rollback: halt Ring 2 promotion and revert Ring 1 nodes to the retained prior image.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 4: Ring 2–N Full Fleet Completion.&lt;/strong&gt; Execute parallel volume pivots across the remaining 90% of databases within the 14-day SLA, throttling concurrent compute replacements per OCI availability domain (&lt;code&gt;max_concurrent_pivots=25&lt;/code&gt;) so OCI Block Volume attach/detach API calls never hit tenant rate limits, and retaining the previous golden image for 7 days (or 30 days for emergency CVSS 9.0+ pivots). Each pivot activity records its pre-pivot volume OCIDs, boot image OCID, and &lt;code&gt;DBMS_QOPATCH&lt;/code&gt; inventory XML in the Fleet Catalog so auditors have an immutable compliance trail linking every database to the exact CVE patch level and GitOps PR commit. Gate: 100% of targeted Oracle Homes report compliant RU levels in Cloud Guard / Fleet Catalog. Rollback: prior image remains bootable in the registry throughout the retention window.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Handling RAC clusters and single-instance standbys uniformly.&lt;/strong&gt; On two-node RAC clusters, the same immutable pivot pattern runs rolling one node at a time: drain services on node 1 via &lt;code&gt;srvctl relocate service&lt;/code&gt;, stop Grid Infrastructure and ASM on node 1, pivot node 1’s compute VM to the patched golden image, rejoin the cluster, and repeat on node 2 before running &lt;code&gt;datapatch -verbose&lt;/code&gt; once for the cluster. Single-instance databases without a Data Guard standby fall back to a brief scheduled maintenance window where ASM dismounts cleanly before the block-volume detach/reattach swap.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Oracle Update Advisor batching guidance (Pattern A):&lt;/strong&gt; Per Oracle’s Update Advisor REST API documentation, batching multiple Oracle Home assets into a single recommendation request minimizes API round trips across large fleets compared to one-by-one polling.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;DBMS_QOPATCH&lt;/code&gt; for SQL-level inventory verification (Pattern A):&lt;/strong&gt; Per Oracle Database PL/SQL Packages and Types Reference, &lt;code&gt;DBMS_QOPATCH&lt;/code&gt; (owned by &lt;code&gt;SYS&lt;/code&gt;, requiring an &lt;code&gt;OPEN&lt;/code&gt; database and &lt;code&gt;JOB_QUEUE_PROCESSES &gt; 0&lt;/code&gt; for RAC cross-node queries) exposes OUI &lt;code&gt;opatch lsinventory -xml&lt;/code&gt; metadata directly inside SQL, giving the control plane a database-side verification gate post-pivot.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Deprecated &lt;code&gt;-ocmrf&lt;/code&gt; and &lt;code&gt;datapatch -prereq&lt;/code&gt; (Pattern A):&lt;/strong&gt; Per Oracle OPatch documentation, the &lt;code&gt;-ocmrf&lt;/code&gt; response-file flag is deprecated and unnecessary in modern OPatch releases, while &lt;code&gt;datapatch -verbose -prereq&lt;/code&gt; validates SQL patch prerequisites before committing dictionary changes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;OCI compute volume-attachment detach semantics (Pattern B):&lt;/strong&gt; In OCI CLI, block-volume detachment uses &lt;code&gt;oci compute volume-attachment detach --volume-attachment-id &amp;#x3C;ocid&gt; --force&lt;/code&gt; (under the &lt;code&gt;compute&lt;/code&gt; namespace, not &lt;code&gt;bv&lt;/code&gt;). Oracle documentation notes that OS-level unmount and ASM diskgroup dismount must still precede force-detachment to prevent I/O corruption.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Breaks How&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Hung block-volume detach&lt;/td&gt;&lt;td&gt;Old VM holds ASM volume past 60s graceful unmount&lt;/td&gt;&lt;td&gt;Pivot activity times out waiting for volume &lt;code&gt;DETACHED&lt;/code&gt; state&lt;/td&gt;&lt;td&gt;Dismount ASM cleanly, then run &lt;code&gt;oci compute volume-attachment detach --force&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Blind time-gated ring promotion&lt;/td&gt;&lt;td&gt;Ring 1 promotes after 48h despite subtle &lt;code&gt;V$SQL&lt;/code&gt; regression&lt;/td&gt;&lt;td&gt;Post-pivot &lt;code&gt;V$SQL&lt;/code&gt; &lt;code&gt;p99&lt;/code&gt; elapsed time climbs &lt;code&gt;&gt;15%&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Gate every ring on &lt;code&gt;V$SQL&lt;/code&gt; &lt;code&gt;p99&lt;/code&gt; comparison, never a bare sleep timer&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Expired rollback golden image&lt;/td&gt;&lt;td&gt;72h image retention deletes prior image before regression found&lt;/td&gt;&lt;td&gt;Reprovision step fails with missing image OCID&lt;/td&gt;&lt;td&gt;Retain prior image 7 days standard, 30 days for CVSS 9.0+ patches&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MOS download or auth failure&lt;/td&gt;&lt;td&gt;Packer VM blocked by MOS contract lag or IP allowlist&lt;/td&gt;&lt;td&gt;Packer build fails at download step&lt;/td&gt;&lt;td&gt;Infosec mirrors CPUs to internal OCI Object Storage bucket on release day&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;x86 vs. ARM binary mismatch&lt;/td&gt;&lt;td&gt;&lt;code&gt;x86_64&lt;/code&gt; RU applied or deployed onto OCI Ampere &lt;code&gt;aarch64&lt;/code&gt; VM&lt;/td&gt;&lt;td&gt;&lt;code&gt;opatch&lt;/code&gt; fails or &lt;code&gt;ORA-00600&lt;/code&gt; on mismatched architecture&lt;/td&gt;&lt;td&gt;Track &lt;code&gt;arch&lt;/code&gt; in Fleet Catalog; build separate &lt;code&gt;x86_64&lt;/code&gt; and &lt;code&gt;aarch64&lt;/code&gt; images&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Update Advisor credential expiry&lt;/td&gt;&lt;td&gt;Static API keys expire silently; new CPUs go undetected&lt;/td&gt;&lt;td&gt;&lt;code&gt;last_successful_poll&lt;/code&gt; metric ages past 7 days&lt;/td&gt;&lt;td&gt;Use OCI Instance Principals (no static keys); alert on poll staleness&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-16-oracle-update-advisor-api-patch-automation-6-failures.svg&quot; alt=&quot;Four failure paths stall automated CPU patch pipelines&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Eliminate in-place &lt;code&gt;opatch&lt;/code&gt; from production:&lt;/strong&gt; Build and validate the patched &lt;code&gt;$ORACLE_HOME&lt;/code&gt; once inside an offline Packer VM, then pivot production compute nodes around untouched ASM data volumes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code&gt;DBMS_QOPATCH&lt;/code&gt; to verify patch state from SQL:&lt;/strong&gt; Querying &lt;code&gt;DBMS_QOPATCH&lt;/code&gt; after &lt;code&gt;datapatch&lt;/code&gt; confirms the running database’s OUI inventory matches the intended RU without scraping host logs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate ring promotion on &lt;code&gt;V$SQL&lt;/code&gt; telemetry, not clocks:&lt;/strong&gt; Block Ring 1 and Ring 2 promotion automatically if &lt;code&gt;p99&lt;/code&gt; elapsed time on high-frequency SQL regresses by more than 15% during the Ring 0 soak.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mirror MOS patches internally and tag by architecture:&lt;/strong&gt; Pre-staging CPUs in Object Storage prevents MOS download outages during 14-day sprints, and architecture tagging prevents &lt;code&gt;x86_64&lt;/code&gt; vs. &lt;code&gt;aarch64&lt;/code&gt; mismatches.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Retain prior golden images for at least 7 days:&lt;/strong&gt; Fast image-reprovision rollback only works if the previous golden image OCID is still present in the registry.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Manual and in-place &lt;code&gt;opatch&lt;/code&gt; workflows fail 14-day CVSS 9.0+ compliance at 1,200-database scale due to O(N) human gates, live binary mutation, and host library drift.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Connect Oracle Update Advisor and OCI Cloud Guard findings to an offline Packer image factory, GitOps Terraform PRs, and telemetry-gated block-volume compute pivots.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Verify &lt;code&gt;opatch apply -silent&lt;/code&gt; and &lt;code&gt;datapatch -verbose -prereq&lt;/code&gt; pass on an offline Packer build, confirm &lt;code&gt;DBMS_QOPATCH&lt;/code&gt; reports the new RU post-pivot, and test &lt;code&gt;oci compute volume-attachment detach --force&lt;/code&gt; after ASM dismount in staging.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Register your OCI Oracle Homes in Cloud Guard and Update Advisor, mirror the latest quarterly RU into a private Object Storage bucket, and build one offline golden image with Packer this week.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Oracle Critical Patch Update Advisories — Quarterly Release &amp;#x26; CVSS Scoring: &lt;a href=&quot;https://www.oracle.com/security-alerts/&quot;&gt;https://www.oracle.com/security-alerts/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OCI Cloud Guard API — &lt;code&gt;ListProblems&lt;/code&gt;: &lt;a href=&quot;https://docs.oracle.com/en-us/iaas/api/#/en/cloud-guard/20200131/Problem/ListProblems&quot;&gt;https://docs.oracle.com/en-us/iaas/api/#/en/cloud-guard/20200131/Problem/ListProblems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Oracle Database 23ai/26ai PL/SQL Packages — &lt;code&gt;DBMS_QOPATCH&lt;/code&gt;: &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/DBMS_QOPATCH.html&quot;&gt;https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/DBMS_QOPATCH.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Oracle OPatch User’s Guide — Offline Patching &amp;#x26; Deprecated &lt;code&gt;-ocmrf&lt;/code&gt;: &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/opunk/&quot;&gt;https://docs.oracle.com/en/database/oracle/oracle-database/19/opunk/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OCI CLI Command Reference — &lt;code&gt;oci compute volume-attachment detach&lt;/code&gt;: &lt;a href=&quot;https://docs.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/compute/volume-attachment/detach.html&quot;&gt;https://docs.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/compute/volume-attachment/detach.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PCI DSS v4.0 Requirement 6.3 — Security Vulnerability Patching: &lt;a href=&quot;https://www.pcisecuritystandards.org/document_library/&quot;&gt;https://www.pcisecuritystandards.org/document_library/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>checklist</category></item><item><title>MongoDB Atlas vs Self-Managed EC2: The Production Engineering Tradeoff</title><link>https://rajivonai.com/blog/2026-06-04-atlas-vs-ec2/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-04-atlas-vs-ec2/</guid><description>An architectural and financial evaluation of self-managed MongoDB Community on AWS EC2 versus MongoDB Atlas: kernel control, SRE toil, and TCO modeling.</description><pubDate>Wed, 09 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Choosing between self-managed MongoDB on AWS EC2 and MongoDB Atlas is not a simple hosting decision—it is an architectural boundary between full kernel and I/O control with SRE toil versus managed automation where unindexed queries force expensive cluster tier jumps.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Problem: Lifting and shifting unindexed workloads from EC2 to MongoDB Atlas turns cheap independent EBS &lt;code&gt;gp3&lt;/code&gt; IOPS scaling (&lt;code&gt;$0.005/IOPS-month&lt;/code&gt;) into forced compute-and-RAM tier upgrades.&lt;/li&gt;
&lt;li&gt;Answer: Run a query selectivity and working-set audit first, model Total Cost of Ownership including SRE operational hours, and choose EC2, Atlas, or a hybrid split per workload.&lt;/li&gt;
&lt;li&gt;Watch out: Atlas abstracts Linux kernel tuning, caps per-node connections by tier, and auto-scales storage up at 90% disk usage without automatic down-scaling.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-04-atlas-vs-ec2.svg&quot; alt=&quot;EC2 trades SRE toil for kernel and EBS control; Atlas trades tier markup for managed automation&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Organizations running multi-terabyte MongoDB Community Edition workloads on AWS EC2 routinely face executive pressure to evaluate MongoDB Atlas. On self-managed EC2, database platform teams own every layer of the stack: provisioning instances, disabling Transparent Huge Pages (THP), tuning &lt;code&gt;vm.swappiness&lt;/code&gt; and dirty-page ratios, formatting XFS filesystems, calibrating &lt;code&gt;gp3&lt;/code&gt; or &lt;code&gt;io2&lt;/code&gt; EBS volumes, and maintaining Ansible upgrade playbooks alongside EBS snapshot scripts.&lt;/p&gt;
&lt;p&gt;As estates grow across dozens of replica sets, leadership looks to MongoDB Atlas—MongoDB’s managed Database-as-a-Service (DBaaS)—to automate rolling upgrades, point-in-time recovery (PITR), and multi-AZ failover under a 99.995% SLA for M10 and larger clusters. However, moving from raw EC2 to a managed control plane shifts operational boundaries, cost elasticity, and performance tuning levers. What looks like a like-for-like VM swap on a spreadsheet actually exchanges direct OS and storage control for SaaS automation and a bundled tier pricing model.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Migrating from self-managed EC2 to MongoDB Atlas exposes four structural constraints that break naive lift-and-shift plans:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Bundled Tier I/O vs. Independent EBS Scaling:&lt;/strong&gt; On AWS EC2 with &lt;code&gt;gp3&lt;/code&gt; volumes, storage capacity (&lt;code&gt;$0.08/GB-month&lt;/code&gt;), provisioned IOPS above 3,000 (&lt;code&gt;$0.005/IOPS-month&lt;/code&gt;), and throughput above 125 MB/s (&lt;code&gt;$0.040/MB/s-month&lt;/code&gt;) scale independently. Masking an unindexed collection scan (&lt;code&gt;COLLSCAN&lt;/code&gt;) by adding 13,000 IOPS (&lt;code&gt;$65/month&lt;/code&gt;) and 375 MB/s (&lt;code&gt;$15/month&lt;/code&gt;) costs &lt;code&gt;$80/month&lt;/code&gt; per volume. On Atlas, compute, RAM, and hypervisor I/O bandwidth are coupled into rigid instance tiers (M40: 4 vCPU / 16 GB RAM, M50: 8 vCPU / 32 GB, M60: 16 vCPU / 64 GB, M80: 32 vCPU / 128 GB). When &lt;code&gt;COLLSCAN&lt;/code&gt; queries saturate I/O on an M40, the immediate remedy is upgrading the entire cluster tier, so the cost of the fix is the full tier price difference, not just the extra I/O. Compare against current Atlas pricing for your region before deciding.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;One-Way Storage Auto-Scaling Ratchet:&lt;/strong&gt; Atlas enables storage auto-scaling by default and expands disk volumes automatically whenever any node reaches 90% disk usage. However, storage auto-scaling never shrinks automatically. If a runaway logging loop or bulk backfill triggers expansion, the higher storage bill persists until an operator manually lowers the disk size via the Atlas UI or API, which triggers an in-place data resync and rolling node restart.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Zero Host OS or Kernel Access:&lt;/strong&gt; Atlas abstracts the underlying Linux host completely. Engineers cannot SSH into nodes, run eBPF tracing tools (&lt;code&gt;biosnoop&lt;/code&gt;, &lt;code&gt;bpftrace&lt;/code&gt;), adjust &lt;code&gt;vm.dirty_ratio&lt;/code&gt;, set &lt;code&gt;blockdev --setra 0&lt;/code&gt; for WiredTiger random I/O, or install host-level security and compliance daemons.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tier-Capped Connections and Network Transit Fees:&lt;/strong&gt; Atlas enforces strict per-node connection limits (6,000 on M40, 16,000 on M50, 32,000 on M60, 96,000 on M80, minus internal management reservations). Meanwhile, connecting application VPCs over AWS PrivateLink adds interface endpoint fees (&lt;code&gt;~$0.01/hour&lt;/code&gt; per AZ) plus &lt;code&gt;$0.01/GB&lt;/code&gt; in data processing charges for the first petabyte per month, alongside cross-AZ transfer costs.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-04-atlas-vs-ec2-2-constraints.svg&quot; alt=&quot;Atlas bundles compute, I/O, and storage into rigid tiers; EC2 scales each axis independently&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How can platform architects evaluate the Total Cost of Ownership, operational leverage, and architectural boundaries between self-managed EC2 and MongoDB Atlas without triggering over-provisioned lift-and-shift migrations?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams evaluating MongoDB hosting on AWS have three viable deployment models, each optimizing a different operational constraint.&lt;/p&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Deployment Model&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Optimizes&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Best Fit&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Self-Managed on AWS EC2&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Full Linux kernel control, independent EBS &lt;code&gt;gp3&lt;/code&gt; IOPS scaling, zero DBaaS markup&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High SRE toil for OS patching, upgrades, quorum repair, and snapshot automation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Large fleets with dedicated DB SREs or strict host-level compliance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;MongoDB Atlas Dedicated&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;99.995% SLA (M10+), automated rolling upgrades, continuous PITR, Dedicated Search Nodes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unindexed queries force full tier jumps; no root OS access or custom eBPF agents&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lean engineering teams optimizing for delivery speed over infrastructure control&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Hybrid Split-Fleet&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Places each cluster by workload profile (OLTP on EC2, search/lean apps on Atlas)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Operational split: teams must maintain two Terraform, IAM, and monitoring pipelines&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Mixed estates with both high-I/O legacy workloads and net-new search/vector apps&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Self-managed EC2 is rejected when generalist DevOps teams lack 24/7 database SRE coverage, because a single botched secondary resync or disk-full outage erases any infrastructure savings. Conversely, all-in Atlas migration is rejected when a fleet carries unindexed &lt;code&gt;COLLSCAN&lt;/code&gt; debt or requires custom host-level security agents that Atlas cannot run. A hybrid split-fleet captures the strengths of both models, provided the platform team can absorb maintaining two provisioning and observability paths.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-04-atlas-vs-ec2-3-options.svg&quot; alt=&quot;Three deployment models trade SRE toil, kernel control, and tier elasticity differently&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;control-plane-and-tco-architecture&quot;&gt;Control Plane and TCO Architecture&lt;/h2&gt;
&lt;p&gt;The core architectural distinction between self-managed EC2 and MongoDB Atlas is where the control plane boundary sits.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-04-atlas-vs-ec2-4-architecture.svg&quot; alt=&quot;Control plane and data plane boundaries separate EC2 full-stack ownership from Atlas managed SaaS&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;control-plane-vs-data-plane-separation&quot;&gt;Control Plane vs. Data Plane Separation&lt;/h3&gt;
&lt;p&gt;On &lt;strong&gt;self-managed EC2&lt;/strong&gt;, the organization owns both the data plane and the control plane. Platform engineers control EC2 instance families, EBS volume types, Linux kernel parameters, and &lt;code&gt;mongod&lt;/code&gt; configuration. That control is mandatory for air-gapped networks, host-level intrusion detection daemons, and custom kernel profiling. The price is continuous lifecycle ownership: CVE patching, minor and major MongoDB version upgrades, backup verification, and hypervisor degradation recovery.&lt;/p&gt;
&lt;p&gt;On &lt;strong&gt;MongoDB Atlas&lt;/strong&gt;, MongoDB operates the control plane as a SaaS service while running &lt;code&gt;mongod&lt;/code&gt; nodes in managed AWS accounts connected to application VPCs via VPC Peering or AWS PrivateLink. Operators give up SSH and kernel access in exchange for automated rolling binary upgrades, continuous oplog PITR, automated node replacement, compute auto-scaling (including &lt;code&gt;scaleDownEnabled&lt;/code&gt; for off-peak contraction), and Dedicated Search Nodes (&lt;code&gt;S10&lt;/code&gt;/&lt;code&gt;S20&lt;/code&gt;/&lt;code&gt;S30&lt;/code&gt; running &lt;code&gt;mongot&lt;/code&gt;) that isolate Atlas Search and Vector Search indexing from transactional WiredTiger memory.&lt;/p&gt;
&lt;p&gt;WiredTiger allocates 50% or more of physical RAM on M40 and larger tiers to its internal cache, leaving the remainder for the OS page cache, aggregation sort buffers, and network connections. On self-managed EC2, engineers can inspect dirty-page eviction pressure directly via &lt;code&gt;wiredTiger.cache&lt;/code&gt; counters and tune &lt;code&gt;gp3&lt;/code&gt; IOPS independently when checkpoint flushes spike; on Atlas, sustained dirty-page pressure above 20% triggers read/write stalls unless the cluster scales up a tier.&lt;/p&gt;
&lt;h3 id=&quot;total-cost-of-ownership-tco-formulation&quot;&gt;Total Cost of Ownership (TCO) Formulation&lt;/h3&gt;
&lt;p&gt;Comparing raw EC2 compute invoices against Atlas cluster bills misses both SRE labor and hidden transit costs. A complete annual TCO model compares both sides explicitly:&lt;/p&gt;
&lt;p&gt;$$\text{TCO}&lt;em&gt;{\text{Self-Managed}} = C&lt;/em&gt;{\text{EC2}} + C_{\text{EBS}} + C_{\text{Network}} + (N_{\text{SRE}} \times S_{\text{SRE}} \times \alpha_{\text{Mongo}}) + C_{\text{Tooling}}$$&lt;/p&gt;
&lt;p&gt;$$\text{TCO}&lt;em&gt;{\text{Atlas}} = C&lt;/em&gt;{\text{Atlas-Tier}} + C_{\text{Storage-Overage}} + C_{\text{PrivateLink}} + (N_{\text{SRE}} \times S_{\text{SRE}} \times \beta_{\text{Mongo}}) + C_{\text{Support}}$$&lt;/p&gt;
&lt;p&gt;In this model, $C_{\text{EC2}}$ reflects compute under 1-year or 3-year Compute Savings Plans, $C_{\text{EBS}}$ captures &lt;code&gt;gp3&lt;/code&gt; capacity plus provisioned IOPS and throughput, and $\alpha_{\text{Mongo}}$ versus $\beta_{\text{Mongo}}$ represent the fraction of SRE capacity consumed by routine database lifecycle operations on EC2 versus Atlas. For small estates, eliminating routine patching and backup toil often outweighs the managed service premium; for massive multi-cluster estates, the recurring tier markup can exceed the cost of funding a dedicated database platform team.&lt;/p&gt;
&lt;h3 id=&quot;linux-kernel-and-storage-tuning-boundaries&quot;&gt;Linux Kernel and Storage Tuning Boundaries&lt;/h3&gt;
&lt;p&gt;Self-managed EC2 exposes four low-level performance levers documented in MongoDB’s Linux Production Notes that are abstracted away in Atlas:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Transparent Huge Pages (THP):&lt;/strong&gt; On MongoDB 7.0 and earlier, WiredTiger’s 4 KB memory allocation patterns suffer fragmentation and latency spikes when Linux coalesces 2 MB huge pages. EC2 operators disable THP at boot (&lt;code&gt;echo never &gt; /sys/kernel/mm/transparent_hugepage/enabled&lt;/code&gt; and &lt;code&gt;defrag&lt;/code&gt;), whereas MongoDB 8.0+ supports THP on specific configurations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Virtual Memory and Swappiness:&lt;/strong&gt; Setting &lt;code&gt;vm.swappiness = 1&lt;/code&gt; keeps swap available only as an emergency buffer against the Linux OOM killer while preventing active WiredTiger cache pages from being paged to disk. Tuning &lt;code&gt;vm.dirty_background_ratio&lt;/code&gt; and &lt;code&gt;vm.dirty_ratio&lt;/code&gt; bounds how much unwritten data accumulates in the OS page cache before synchronous writebacks stall I/O threads:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;ini&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# /etc/sysctl.d/99-mongodb.conf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;vm.dirty_background_ratio&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;vm.dirty_ratio&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;vm.swappiness&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;NUMA and XFS Mounts:&lt;/strong&gt; On multi-socket EC2 shapes, launching &lt;code&gt;mongod&lt;/code&gt; with &lt;code&gt;numactl --interleave=all&lt;/code&gt; prevents single-node memory exhaustion. Data volumes are formatted as XFS and mounted with &lt;code&gt;noatime,nodiratime&lt;/code&gt; to eliminate unnecessary metadata writes on reads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Block Device Readahead:&lt;/strong&gt; Because WiredTiger executes random 4 KB–32 KB reads, large OS readahead values waste EBS bandwidth fetching unused blocks. EC2 operators set &lt;code&gt;blockdev --setra 0 /dev/nvme1n1&lt;/code&gt; (or a low value such as 16–32 sectors) via &lt;code&gt;udev&lt;/code&gt; rules. On Atlas, MongoDB manages AMI, filesystem, and block-device defaults internally.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;workload-telemetry-and-selectivity-audit&quot;&gt;Workload Telemetry and Selectivity Audit&lt;/h3&gt;
&lt;p&gt;Before requesting Atlas tier quotes, platform engineers must measure document selectivity (&lt;code&gt;scannedObjects&lt;/code&gt; vs. &lt;code&gt;document.returned&lt;/code&gt;), WiredTiger cache pressure, and network egress directly from &lt;code&gt;db.serverStatus()&lt;/code&gt;. Note that &lt;code&gt;queryExecutor.scanned&lt;/code&gt; counts index keys examined, whereas &lt;code&gt;queryExecutor.scannedObjects&lt;/code&gt; counts documents examined:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// MongoDB Migration Readiness and Workload Efficiency Audit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ss&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;serverStatus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; scannedDocs&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ss.metrics.queryExecutor.scannedObjects;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; returnedDocs&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ss.metrics.document.returned &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; selectivityRatio&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; scannedDocs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; returnedDocs;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Docs Scanned / Returned: &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; selectivityRatio.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (selectivityRatio &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5.0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[WARNING] High COLLSCAN ratio; add ESR compound indexes before Atlas sizing.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; wt&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ss.wiredTiger.cache;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; maxGB&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (wt[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;maximum bytes configured&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1073741824&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; dirtyGB&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (wt[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;tracked dirty bytes in the cache&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1073741824&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;WiredTiger Max Cache (GB): &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; maxGB &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot; | Dirty (GB): &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dirtyGB);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; egressGB&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (ss.network.bytesOut &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1073741824&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Cumulative Egress (GB): &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; egressGB);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;A safe evaluation or migration follows a four-phase readiness gate so unoptimized queries never reach managed tiers.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-04-atlas-vs-ec2-5-flow.svg&quot; alt=&quot;Four-phase migration readiness gate prevents lift-and-shift cost shock&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Workload and Selectivity Audit:&lt;/strong&gt; Run the &lt;code&gt;serverStatus()&lt;/code&gt; audit across every candidate replica set. Flag any cluster where &lt;code&gt;scannedObjects / document.returned&lt;/code&gt; exceeds 5:1, and capture working-set RAM and hourly network egress baselines. &lt;em&gt;Rollback/Gate:&lt;/em&gt; Halt sizing for any cluster failing the selectivity threshold.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Index Remediation on EC2:&lt;/strong&gt; Refactor offending queries using the Equality-Sort-Range (ESR) rule and build targeted compound indexes while still on EC2, where &lt;code&gt;gp3&lt;/code&gt; IOPS absorb temporary index-build load cheaply. Re-run the audit to confirm reduced disk reads and smaller working-set requirements.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — PrivateLink, Connection Pooling, and Restore Validation:&lt;/strong&gt; Provision AWS PrivateLink endpoints in the same Availability Zones as application subnets to avoid cross-AZ egress multipliers, configure driver &lt;code&gt;maxPoolSize&lt;/code&gt; bounds within the target tier’s per-node connection cap, and execute a full PITR restore drill in staging.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Staged Cutover:&lt;/strong&gt; Replicate data using Atlas Live Migration (&lt;code&gt;mongomirror&lt;/code&gt; for self-managed sources), validate replication lag and &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; plans on the target cluster, and switch application connection strings during a maintenance window while keeping the EC2 cluster warm as an immediate fallback.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Documented system behavior across AWS EC2, WiredTiger, and MongoDB Atlas explains why lift-and-shift migrations fail when teams skip workload remediation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;AWS EBS &lt;code&gt;gp3&lt;/code&gt; Decoupling vs. Atlas Tier Coupling:&lt;/strong&gt; Under documented AWS EBS &lt;code&gt;gp3&lt;/code&gt; pricing, volume storage (&lt;code&gt;$0.08/GB-month&lt;/code&gt;) is billed separately from provisioned IOPS above 3,000 (&lt;code&gt;$0.005/IOPS-month&lt;/code&gt;) and throughput above 125 MB/s (&lt;code&gt;$0.040/MB/s-month&lt;/code&gt;). On EC2, a team can absorb an unexpected I/O bottleneck for &lt;code&gt;$80/month&lt;/code&gt; while scheduling an index fix. On MongoDB Atlas, I/O capacity is tied to the cluster tier, so unindexed reads force scaling both vCPU and RAM—turning a query bug into a permanent tier-level cost jump.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Atlas Storage Auto-Scaling Behavior:&lt;/strong&gt; MongoDB Atlas documentation specifies that storage auto-scaling triggers automatically when disk utilization reaches 90% on any node, but never scales storage down automatically after data is deleted or archived via TTL indexes. Shrinking a volume requires a manual configuration edit in Atlas, which provisions smaller disks and performs an in-place data resync with rolling node restarts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Search Isolation Pattern with Dedicated Search Nodes:&lt;/strong&gt; When transactional clusters run Atlas Search or Vector Search on the same nodes as &lt;code&gt;mongod&lt;/code&gt;, Lucene and k-NN indexing compete with WiredTiger for RAM and CPU. The documented Atlas pattern offloads search indexing to Dedicated Search Nodes (&lt;code&gt;mongot&lt;/code&gt;), isolating OLTP latency from search spikes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pattern Acknowledgment:&lt;/strong&gt; I have not personally migrated a 50-node estate in a single event, but the documented architectural pattern across MongoDB Production Notes and Atlas sizing guides is consistent: index remediation and working-set right-sizing on EC2 must precede tier selection on Atlas.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Every hosting model fails under specific operational patterns; the table below maps each failure mode to its detection signal and mitigation.&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure Mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Lift-and-shift of &lt;code&gt;COLLSCAN&lt;/code&gt; queries&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unindexed scans exhaust Atlas tier IOPS, forcing emergency M40-to-M80 upgrades&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;scannedObjects / document.returned &gt; 5&lt;/code&gt; or high &lt;code&gt;pages read into cache&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Build ESR compound indexes on EC2 before sizing Atlas tiers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Lambda / serverless connection sprawl&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Ephemeral functions open direct connections until per-node cap (e.g., 6,000 on M40) is hit&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;connections.current&lt;/code&gt; approaching tier limit; connection reset errors&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reuse global &lt;code&gt;MongoClient&lt;/code&gt; across invocations and cap &lt;code&gt;maxPoolSize&lt;/code&gt; (RDS Proxy does not support MongoDB)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unbounded storage auto-scaling&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Temp log spike hits 90% disk, auto-scales volume up, and never auto-shrinks&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Storage utilization alerts or unexpected EBS/Atlas disk bill jump&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set max storage scaling ceilings and enforce TTL indexes on high-churn collections&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Cross-AZ PrivateLink egress&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unprojected reads traverse AZ boundaries via PrivateLink at &lt;code&gt;$0.01/GB&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rising &lt;code&gt;bytesOut&lt;/code&gt; in &lt;code&gt;serverStatus()&lt;/code&gt; and AWS DataTransfer-AZ-Bytes billing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Co-locate app subnets and endpoints by AZ; enforce field projections&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Host compliance or eBPF requirement&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Security team mandates root eBPF tracers or host-level IDS daemons&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Security architecture review flags lack of SSH/root on Atlas nodes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep regulated or kernel-tuned clusters on self-managed EC2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Co-located search and OLTP&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;$search&lt;/code&gt; / &lt;code&gt;$vectorSearch&lt;/code&gt; indexing evicts hot OLTP pages from WiredTiger cache&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rising WiredTiger cache eviction stalls during search index rebuilds&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Deploy Atlas Dedicated Search Nodes (&lt;code&gt;mongot&lt;/code&gt;) to isolate search memory&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-04-atlas-vs-ec2-6-failures.svg&quot; alt=&quot;Four production failure modes trigger sudden Atlas cost jumps or connection outages&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Fix query selectivity before pricing Atlas:&lt;/strong&gt; An unindexed &lt;code&gt;COLLSCAN&lt;/code&gt; costs &lt;code&gt;$80/month&lt;/code&gt; to mask on EBS &lt;code&gt;gp3&lt;/code&gt;, but forces a full compute-and-RAM tier jump on MongoDB Atlas.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Model SRE toil honestly in TCO:&lt;/strong&gt; Self-managed EC2 saves raw infrastructure markup, but only wins financially when the fleet is large enough to justify dedicated database SRE staffing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Atlas storage auto-scaling is a one-way ratchet:&lt;/strong&gt; Disks expand automatically at 90% utilization and never auto-shrink; always configure maximum storage ceilings and TTL policies.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Serverless connection pooling requires application discipline:&lt;/strong&gt; AWS RDS Proxy does not support MongoDB, so Lambda functions must reuse a global &lt;code&gt;MongoClient&lt;/code&gt; and bound &lt;code&gt;maxPoolSize&lt;/code&gt; below tier limits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hybrid split-fleets beat ideological mandates:&lt;/strong&gt; Keep kernel-tuned, compliance-restricted, or massive I/O clusters on EC2 while moving lean OLTP and search-heavy workloads to Atlas.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Decide whether to retain self-managed MongoDB on AWS EC2 or migrate workloads to MongoDB Atlas without triggering unexpected cloud spend.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Run a workload-by-workload TCO and control-boundary evaluation covering compute, EBS &lt;code&gt;gp3&lt;/code&gt; IOPS, PrivateLink transit, and SRE operational hours.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Execute the &lt;code&gt;serverStatus()&lt;/code&gt; selectivity audit across production replica sets and verify &lt;code&gt;scannedObjects / document.returned&lt;/code&gt; is below 5:1 before requesting Atlas tier quotes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Remediate top &lt;code&gt;COLLSCAN&lt;/code&gt; queries with ESR compound indexes this week, then pilot one lean replica set through the four-phase Atlas readiness gate—including a staged PITR restore test—before committing to a broader migration.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>Vitess for MySQL Sharding: Routing, Resharding, and Failure Boundaries</title><link>https://rajivonai.com/blog/2026-09-08-vitess-mysql-sharding-failure-boundaries/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-09-08-vitess-mysql-sharding-failure-boundaries/</guid><description>A production decision framework for Vitess that covers shard-key design, VTGate and topology failure modes, distributed transactions, online resharding, migration, and recovery evidence.</description><pubDate>Tue, 08 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A VTGate fleet can present thousands of MySQL tablets through a MySQL-compatible endpoint, but it cannot hide a poor shard key, make cross-shard work free, or turn an untested traffic switch into a safe migration. The abstraction succeeds only when the application’s transaction boundaries match the data placement model.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;MySQL often scales farther on one primary than an early architecture forecast suggests. Better indexes, bounded queries, read replicas, connection control, caching, table partitioning, and larger instances can delay horizontal partitioning. That is useful: sharding adds a distributed routing layer, more failure domains, and permanent constraints on transactions and schema design.&lt;/p&gt;
&lt;p&gt;Eventually, some workloads do need more write throughput, storage, maintenance isolation, or tenant-level blast-radius control than one MySQL replication group can provide. Manual application sharding can meet that requirement, but it spreads placement logic across connection pools, services, migrations, background jobs, and operational tooling. Every reshard then becomes an application release.&lt;/p&gt;
&lt;p&gt;Vitess centralizes much of that machinery. Applications connect to VTGate. A VSchema describes how logical tables map to keyspaces and shards. VTGate plans and routes queries to VTTablet processes, and each VTTablet mediates access to a MySQL instance. VReplication moves data for workflows such as resharding and table migration. A topology service stores control-plane metadata used by the Vitess components.&lt;/p&gt;
&lt;p&gt;This is a mature architecture, not an infinitely scalable MySQL server. It changes which constraints the organization must manage.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The first mistake is to choose Vitess because a database is “large.” Size is not a failure mode. A platform needs evidence that a specific resource or operation has exceeded an acceptable envelope: primary write CPU, buffer-pool miss cost, redo or binlog throughput, recovery time, schema-change duration, backup time, storage headroom, noisy-neighbor isolation, or inability to meet a maintenance window.&lt;/p&gt;
&lt;p&gt;The second mistake is to shard before understanding access paths. A lexical split such as customers A–M and N–Z usually produces skew and makes placement depend on mutable business data. A hash-like vindex distributes rows more evenly but generally sacrifices range locality of the original key. A tenant-based key can preserve transaction locality but allows one large tenant to dominate a shard. No key simultaneously guarantees uniform load, local joins, ordered range scans, and easy tenant movement.&lt;/p&gt;
&lt;p&gt;The third mistake is to treat the routing tier as transparent. VTGate can route a query directly when the predicate resolves through a unique vindex. Without that routing value, it may scatter the query to multiple shards. Fan-out increases total work, tail latency, memory pressure, and the number of partial failures a request can encounter. A query that is acceptable against four shards may become an incident against hundreds.&lt;/p&gt;
&lt;p&gt;The decision question is therefore: &lt;strong&gt;can the system choose a stable shard key that localizes its dominant transactions, and can it operate the exceptions without allowing scatter, distributed commits, or resharding workflows to consume the fleet?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;design-the-contract-before-deploying-vitess&quot;&gt;Design the Contract Before Deploying Vitess&lt;/h2&gt;
&lt;p&gt;The core architecture separates query serving from topology and workflow management, but every layer needs an explicit availability contract.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    APP[&quot;Application services&quot;] --&gt; LB[&quot;Regional load balancer&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    LB --&gt; G1[&quot;VTGate — cell A&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    LB --&gt; G2[&quot;VTGate — cell B&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    VS[&quot;VSchema and topology metadata&quot;] -.-&gt; G1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    VS -.-&gt; G2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G1 --&gt; T1[&quot;VTTablet — shard 10&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G1 --&gt; T2[&quot;VTTablet — shard 20&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G2 --&gt; T1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G2 --&gt; T2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    T1 --&gt; M1[&quot;MySQL primary and replicas&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    T2 --&gt; M2[&quot;MySQL primary and replicas&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    VR[&quot;VReplication workflow&quot;] --&gt; T1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    VR --&gt; T2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OBS[&quot;Metrics and query logs&quot;] --&gt; G1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OBS --&gt; G2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OBS --&gt; T1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OBS --&gt; T2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;start-with-a-query-to-placement-matrix&quot;&gt;Start with a query-to-placement matrix&lt;/h3&gt;
&lt;p&gt;Inventory the operations that define correctness and load, not every SQL statement ever issued. For each operation, record:&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Operation&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Routing value available&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Expected shard count&lt;/th&gt;&lt;th&gt;Transaction boundary&lt;/th&gt;&lt;th&gt;Failure behavior&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Read tenant profile&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;One&lt;/td&gt;&lt;td&gt;Read only&lt;/td&gt;&lt;td&gt;Retry on another VTGate&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Create order and lines&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;One when co-located&lt;/td&gt;&lt;td&gt;One local transaction&lt;/td&gt;&lt;td&gt;Roll back locally&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Update two unrelated tenants&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Two values&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Two&lt;/td&gt;&lt;td&gt;Avoid or use explicit distributed policy&lt;/td&gt;&lt;td&gt;Ambiguous result must be resolved&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Global aggregate&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;No&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;All&lt;/td&gt;&lt;td&gt;Analytical&lt;/td&gt;&lt;td&gt;Move to an analytical system or bound fan-out&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lookup by email&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Indirect&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;One after lookup&lt;/td&gt;&lt;td&gt;Read path plus lookup maintenance&lt;/td&gt;&lt;td&gt;Verify lookup consistency&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;This table is the architecture. If a critical operation has no routing value, either redesign the data model, introduce a lookup vindex, accept bounded scatter, or keep that workload outside the sharded keyspace. Lookup vindexes add a maintained lookup table and write-path work. Owned and unowned variants assign different maintenance responsibility; &lt;code&gt;consistent_lookup*&lt;/code&gt; variants add locking and transaction semantics to protect lookup consistency under concurrent writes.&lt;/p&gt;
&lt;p&gt;Vitess keyspaces are logical database namespaces. A primary vindex maps one or more row columns to a keyspace ID, and each shard owns a keyspace-ID range. Secondary vindexes support additional routing paths. The mapping must be stable. Changing a primary vindex is a data-placement migration, not an index rebuild.&lt;/p&gt;
&lt;p&gt;Co-locate rows that must commit together. If orders, order lines, and payment state normally share a tenant or account boundary, carry that routing key through each table and request. Enforce the invariant in schema review and application APIs. A global identifier may still be useful, but uniqueness and placement are separate concerns.&lt;/p&gt;
&lt;h3 id=&quot;budget-scatter-as-fleet-wide-work&quot;&gt;Budget scatter as fleet-wide work&lt;/h3&gt;
&lt;p&gt;Scatter-gather is not automatically wrong. Low-frequency administrative queries over a small shard count may be acceptable. The risk comes from multiplying per-shard work by concurrency and shard count.&lt;/p&gt;
&lt;p&gt;Define controls before production:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a maximum expected shard fan-out by query class;&lt;/li&gt;
&lt;li&gt;execution and row limits that fail closed before a proxy accumulates unbounded results;&lt;/li&gt;
&lt;li&gt;workload isolation for administrative or batch traffic;&lt;/li&gt;
&lt;li&gt;per-VTGate memory and concurrency budgets;&lt;/li&gt;
&lt;li&gt;query-plan and route-type telemetry;&lt;/li&gt;
&lt;li&gt;an analytical path for unbounded aggregates, wide joins, and scans.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Monitor total shard queries, not only application QPS. One request scattered to 128 shards creates at least 128 shard-targeted operations and can amplify a modest retry storm into a fleet event.&lt;/p&gt;
&lt;h3 id=&quot;make-distributed-transactions-exceptional&quot;&gt;Make distributed transactions exceptional&lt;/h3&gt;
&lt;p&gt;Vitess supports multi-shard transactions. In &lt;code&gt;MULTI&lt;/code&gt; mode, atomicity is not guaranteed if a later shard commit fails after an earlier one succeeds. In &lt;code&gt;TWOPC&lt;/code&gt; mode, Vitess uses a metadata manager and prepare phase to provide atomic commit across participants. The Vitess 24.0 guarantee requires semi-synchronous replication to be enabled; it does not add cross-shard isolation, so readers can observe a fractured state while participants commit at different times.&lt;/p&gt;
&lt;p&gt;Two-phase commit does not mean “if one shard crashes, every already committed shard is rolled back.” A committed participant cannot generally be undone by protocol magic. Vitess records metadata and normally resolves prepared participants toward one decision, but its documentation exposes critical failure counters for cases where atomicity has failed. Abandoned transactions can require DTID-level inspection, manual repair, and &lt;code&gt;Conclude&lt;/code&gt; after the data is reconciled. That adds durable metadata, locks, failure recovery, operational inspection, and longer tail latency.&lt;/p&gt;
&lt;p&gt;Use two-phase commit only for a small, measured set of invariants that cannot be modeled within one shard or implemented as an idempotent workflow. Configure and monitor &lt;code&gt;--twopc-abandon-age&lt;/code&gt;; expose &lt;code&gt;Unresolved&lt;/code&gt;, &lt;code&gt;CommitUnresolved&lt;/code&gt;, and the critical failure counters; document the DTID inspection, repair, and &lt;code&gt;Conclude&lt;/code&gt; path; and test coordinator and participant failures at each phase. Application transaction deadlines remain a separate control. If cross-shard transactions are common, the shard key is probably misaligned with the domain.&lt;/p&gt;
&lt;h3 id=&quot;treat-topology-as-a-control-plane-with-cached-consumers&quot;&gt;Treat topology as a control plane with cached consumers&lt;/h3&gt;
&lt;p&gt;Vitess uses a global topology and per-cell topology data. VTGate and VTTablet consume topology information, while management operations update it. A topology-service outage is serious, but it should not be described as an automatic global database outage. Serving components cache information and use resilient watches; the effect depends on which topology data is unavailable, whether serving state must change, and which cell is affected.&lt;/p&gt;
&lt;p&gt;Separate the tests:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;can existing single-shard reads and writes continue with the local topology unavailable;&lt;/li&gt;
&lt;li&gt;can a new VTGate or VTTablet start;&lt;/li&gt;
&lt;li&gt;can a shard primary fail over;&lt;/li&gt;
&lt;li&gt;can a VSchema or serving-graph change propagate;&lt;/li&gt;
&lt;li&gt;can an operator run resharding or emergency commands;&lt;/li&gt;
&lt;li&gt;does one cell’s topology failure remain cell-scoped.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Run the topology implementation as its own consensus system with supported durability, backup, quorum, latency, and upgrade procedures. Do not prescribe local NVMe or a fixed availability-zone layout without validating the chosen implementation’s support model and correlated-failure assumptions.&lt;/p&gt;
&lt;h3 id=&quot;reshard-in-observable-reversible-phases&quot;&gt;Reshard in observable, reversible phases&lt;/h3&gt;
&lt;p&gt;Vitess resharding is a workflow, not an instantaneous map update. VReplication copies source data and then streams ongoing changes. Operators validate the destination, switch traffic, observe the result, and only later complete the workflow and remove old resources.&lt;/p&gt;
&lt;p&gt;A safe production plan has gates:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Confirm source health, destination capacity, schema compatibility, and replication prerequisites.&lt;/li&gt;
&lt;li&gt;Start the workflow and monitor copy progress, stream errors, lag, and source overhead.&lt;/li&gt;
&lt;li&gt;Validate row counts and domain-specific invariants; counts alone do not prove equivalence.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;SwitchTraffic&lt;/code&gt; for a bounded tablet type first when the rollout calls for staged reads; by default the command can switch all tablet types, so the runbook must pass the intended scope explicitly.&lt;/li&gt;
&lt;li&gt;Before switching primary traffic, verify the allowed lag and timeout. The workflow makes sources read-only, waits for targets to catch up, and can pause or fail writes while the cutover completes.&lt;/li&gt;
&lt;li&gt;Keep reverse VReplication enabled, as it is by default, and exercise &lt;code&gt;ReverseTraffic&lt;/code&gt; as the post-switch rollback path.&lt;/li&gt;
&lt;li&gt;Hold at the new state while monitoring application and database signals.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;Complete&lt;/code&gt; only after the rollback window closes and backups are verified; its default cleanup removes old source shards from topology and drops source tables.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Traffic-switch semantics depend on the Vitess version and workflow. In Vitess 24.0, &lt;code&gt;Cancel&lt;/code&gt; is available only before any traffic has switched. After switching, &lt;code&gt;ReverseTraffic&lt;/code&gt; is the rollback mechanism while the reverse stream and source resources remain. &lt;code&gt;Complete&lt;/code&gt; is the destructive boundary that removes that ordinary rollback path. The runbook must use the exact version’s commands, preserve reverse replication, and name completion—not write switching—as the final cleanup decision.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Slack’s engineering account documents why it adopted Vitess and how it approached migration. The useful lesson is not a claim that a particular Slack outage was caused by VTGate; the publicly documented article does not establish that story. The defensible pattern is that a large existing application treated Vitess adoption as a staged datastore migration, not as a proxy installation.&lt;/p&gt;
&lt;p&gt;That distinction changes the work. Compatibility has to be measured against the application’s real SQL, transaction patterns, schema conventions, and operational tools. Traffic movement needs incremental validation and a rollback design. The organization must learn to operate both the source and destination during the migration window.&lt;/p&gt;
&lt;p&gt;The documented Vitess architecture produces a practical readiness sequence:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Prove the current bottleneck.&lt;/strong&gt; Capture resource saturation, maintenance risk, and growth projections. Compare sharding with vertical growth, archival, workload separation, and managed alternatives.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replay representative SQL.&lt;/strong&gt; Classify direct, lookup, scatter, unsupported, and multi-shard operations. Include background jobs and incident tooling, not only API traffic.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Select and test vindexes.&lt;/strong&gt; Measure distribution with current data and plausible future tenants. Inspect the largest keys and hottest write keys separately from average distribution.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Build a shadow keyspace.&lt;/strong&gt; Exercise schema deployment, backups, point-in-time recovery, tablet failover, topology loss, and VTGate scaling before carrying production writes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Move one bounded domain.&lt;/strong&gt; Use VReplication workflows and explicit validation gates. Keep ownership and rollback criteria clear.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Re-measure after every shard split.&lt;/strong&gt; More shards change fan-out cost, connection counts, buffer efficiency, operational cardinality, and topology churn.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The proof of readiness is an evidence packet: query classification, vindex distribution, load test, failure-injection results, restore result, migration reconciliation, and rollback timing. Vendor adoption lists are not evidence that a specific data model is ready.&lt;/p&gt;
&lt;h3 id=&quot;observe-routing-and-data-planes-together&quot;&gt;Observe routing and data planes together&lt;/h3&gt;
&lt;p&gt;A useful dashboard joins application, VTGate, VTTablet, MySQL, VReplication, and topology signals:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;query rate and latency by keyspace, tablet type, route type, and error code;&lt;/li&gt;
&lt;li&gt;shard fan-out distribution and rows returned;&lt;/li&gt;
&lt;li&gt;VTGate memory, CPU, connection pressure, retries, and rejected queries;&lt;/li&gt;
&lt;li&gt;VTTablet query latency, pool saturation, health state, and MySQL availability;&lt;/li&gt;
&lt;li&gt;per-shard QPS, write rate, storage, replication lag, and hot-key concentration;&lt;/li&gt;
&lt;li&gt;VReplication copy state, stream lag, errors, and validation results;&lt;/li&gt;
&lt;li&gt;topology watch failures, stale state, quorum health, and cell scope;&lt;/li&gt;
&lt;li&gt;unresolved distributed transactions and recovery age.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Page on violated service objectives, not component existence. A VTGate restart behind a healthy load balancer may be noise. Rising scatter fan-out combined with proxy memory and tablet-pool saturation is an architectural incident.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;







































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Unsafe assumption&lt;/th&gt;&lt;th&gt;Consequence&lt;/th&gt;&lt;th&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Hot shard or hot key&lt;/td&gt;&lt;td&gt;Hashing guarantees balanced load&lt;/td&gt;&lt;td&gt;One shard sets fleet latency and capacity&lt;/td&gt;&lt;td&gt;Distribution tests, hot-key metrics, tenant isolation strategy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Missing routing predicate&lt;/td&gt;&lt;td&gt;VTGate makes every query local&lt;/td&gt;&lt;td&gt;Scatter multiplies downstream work&lt;/td&gt;&lt;td&gt;Query policy, fan-out limits, analytical offload&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cross-shard transaction growth&lt;/td&gt;&lt;td&gt;Two-phase commit behaves like local MySQL&lt;/td&gt;&lt;td&gt;Lock duration, recovery work, and tail latency increase&lt;/td&gt;&lt;td&gt;Co-location, explicit transaction mode, unresolved-transaction runbook&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;VTGate fleet overload&lt;/td&gt;&lt;td&gt;Stateless means capacity is unlimited&lt;/td&gt;&lt;td&gt;Routing becomes the shared bottleneck&lt;/td&gt;&lt;td&gt;Independent replicas, load shedding, memory and concurrency budgets&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Topology impairment&lt;/td&gt;&lt;td&gt;Every loss either has no effect or stops everything&lt;/td&gt;&lt;td&gt;Startup, failover, management, or serving changes fail differently&lt;/td&gt;&lt;td&gt;Cell-scoped drills, cache behavior tests, quorum operations&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;VSchema error&lt;/td&gt;&lt;td&gt;Metadata changes are harmless&lt;/td&gt;&lt;td&gt;Queries misroute or begin scattering&lt;/td&gt;&lt;td&gt;Versioned review, staged rollout, route-plan comparison&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reshard validation gap&lt;/td&gt;&lt;td&gt;Copy completion proves correctness&lt;/td&gt;&lt;td&gt;Missing or inconsistent domain data&lt;/td&gt;&lt;td&gt;Checksums plus business-invariant reconciliation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Premature workflow cleanup&lt;/td&gt;&lt;td&gt;Traffic switch is instantly irreversible or risk-free&lt;/td&gt;&lt;td&gt;Rollback path disappears before confidence exists&lt;/td&gt;&lt;td&gt;Defined hold period and last-reversible-point runbook&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MySQL failure inside a shard&lt;/td&gt;&lt;td&gt;Vitess eliminates database HA work&lt;/td&gt;&lt;td&gt;One shard loses writes or exceeds RPO&lt;/td&gt;&lt;td&gt;Per-shard replication, backup, failover, and restore testing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Fleet-wide schema change&lt;/td&gt;&lt;td&gt;Online DDL removes all risk&lt;/td&gt;&lt;td&gt;Long-running migrations contend across many shards&lt;/td&gt;&lt;td&gt;Concurrency waves, throttling, pause criteria, verification&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Vitess is a poor fit when the workload depends on frequent unbounded cross-tenant joins, global serializable transactions, ad hoc analytics over the serving fleet, or a team that cannot staff the topology, workflow, routing, and MySQL operational layers. Sharding can increase the ceiling, but it also increases the number of systems that must be correct during recovery.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The team is proposing Vitess without a measured single-primary limit. &lt;strong&gt;Solution:&lt;/strong&gt; Define the saturated resource and compare lower-complexity remedies. &lt;strong&gt;Proof:&lt;/strong&gt; Produce capacity and maintenance evidence under representative load. &lt;strong&gt;Action:&lt;/strong&gt; Do not approve sharding from database size alone.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The shard key is selected from entity names rather than transaction boundaries. &lt;strong&gt;Solution:&lt;/strong&gt; Build the query-to-placement matrix and test candidate vindexes against real skew. &lt;strong&gt;Proof:&lt;/strong&gt; Show the percentage of critical operations routed to one shard and the largest-key distribution. &lt;strong&gt;Action:&lt;/strong&gt; Make routing-key presence part of API and schema review.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Scatter and distributed commits are treated as transparent compatibility features. &lt;strong&gt;Solution:&lt;/strong&gt; Give them explicit budgets, policies, and failure runbooks. &lt;strong&gt;Proof:&lt;/strong&gt; Load-test fan-out and inject failure into every two-phase-commit stage. &lt;strong&gt;Action:&lt;/strong&gt; Block production migration until exceptional paths are observable and bounded.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Resharding success is defined as a completed command. &lt;strong&gt;Solution:&lt;/strong&gt; Gate copy, validation, read switching, write switching, rollback, and cleanup separately. &lt;strong&gt;Proof:&lt;/strong&gt; Reconcile domain invariants and execute a timed rollback rehearsal. &lt;strong&gt;Action:&lt;/strong&gt; Preserve the reverse path until the agreed confidence window closes.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://vitess.io/docs/24.0/overview/architecture/&quot;&gt;Vitess 24.0 architecture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://vitess.io/docs/24.0/reference/features/vschema/&quot;&gt;Vitess 24.0 VSchema and Vindexes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://vitess.io/docs/24.0/user-guides/vschema-guide/sharding-guidelines/&quot;&gt;Vitess 24.0 sharding guidelines&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://vitess.io/docs/24.0/reference/features/distributed-transaction/&quot;&gt;Vitess 24.0 distributed transactions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://vitess.io/docs/24.0/reference/vreplication/reshard/&quot;&gt;Vitess 24.0 Reshard reference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://vitess.io/docs/24.0/reference/features/topology-service/&quot;&gt;Vitess 24.0 topology service&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://slack.engineering/scaling-datastores-at-slack-with-vitess/&quot;&gt;Slack Engineering — Scaling Datastores at Slack with Vitess&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category><category>checklist</category></item><item><title>Automating MongoDB Patch and Major-Version Upgrades Across Large Replica Sets and Sharded Clusters</title><link>https://rajivonai.com/blog/2026-06-03-automating-mongodb-patch-and-major-version-upgrades/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-03-automating-mongodb-patch-and-major-version-upgrades/</guid><description>How to orchestrate zero-downtime MongoDB major-version upgrades on AWS EC2, enforce strict component ordering, and govern Feature Compatibility Versions safely.</description><pubDate>Mon, 07 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A major-version MongoDB upgrade is a non-reversible state-machine transition across three distributed tiers; inverting component order or advancing Feature Compatibility Version before soaking turns a rolling maintenance window into an unbootable cluster.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Upgrading &lt;code&gt;mongos&lt;/code&gt; or shards before the Config Server Replica Set (CSRS), skipping major versions, or bumping &lt;code&gt;featureCompatibilityVersion&lt;/code&gt; (FCV) immediately removes your binary rollback path.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Automate a strict state machine in Ansible: &lt;code&gt;sh.stopBalancer()&lt;/code&gt; -&gt; CSRS -&gt; Shards -&gt; NLB-drained &lt;code&gt;mongos&lt;/code&gt; -&gt; &lt;code&gt;sh.startBalancer()&lt;/code&gt; -&gt; 7–14 day soak at FCV &lt;code&gt;N&lt;/code&gt; -&gt; &lt;code&gt;setFeatureCompatibilityVersion: N+1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Starting in MongoDB 7.0, &lt;code&gt;setFeatureCompatibilityVersion&lt;/code&gt; requires &lt;code&gt;confirm: true&lt;/code&gt; for FCV downgrades/sensitive transitions, and &lt;code&gt;mongod&lt;/code&gt; will refuse to start if you jump two major versions at once (&lt;code&gt;N -&gt; N+2&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-03-automating-mongodb-patch-and-major-version-upgrades.svg&quot; alt=&quot;Sharded MongoDB upgrades follow a strict order and hold FCV behind a soak gate&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Self-managed MongoDB clusters on AWS EC2—spanning multiple 3-node shard replica sets, a 3-node Config Server Replica Set (CSRS), and stateless &lt;code&gt;mongos&lt;/code&gt; query routers behind Network Load Balancers (NLBs)—require regular patch releases for CVEs and annual major-version upgrades to stay within vendor support windows.&lt;/p&gt;
&lt;p&gt;In a standalone replica set, a rolling upgrade is straightforward: upgrade secondaries one at a time, step down the primary with &lt;code&gt;rs.stepDown()&lt;/code&gt;, and upgrade the former primary. In a sharded cluster, however, three interdependent tiers must transition without breaking metadata RPCs, dropping client writes, or triggering mid-migration chunk corruption.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Manual upgrade runbooks fail on sharded clusters because four distributed invariants cannot be violated:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;No skipped major versions (&lt;code&gt;N -&gt; N+1&lt;/code&gt; only):&lt;/strong&gt; MongoDB &lt;code&gt;mongod&lt;/code&gt; binaries cannot mount data directories created more than one major release prior. Installing MongoDB 7.0 directly over 5.0 data files aborts on startup with &lt;code&gt;InvalidFeatureCompatibilityVersion&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Strict tier ordering:&lt;/strong&gt; Newer &lt;code&gt;mongos&lt;/code&gt; routers and shard binaries are backward-compatible with older CSRS metadata only within the documented upgrade sequence, whereas legacy CSRS nodes cannot serve newer metadata commands from upgraded routers. Every sharded upgrade must transition &lt;code&gt;Balancer stop -&gt; CSRS -&gt; Shards -&gt; mongos -&gt; Balancer start&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;FCV irreversibility boundary:&lt;/strong&gt; Binary installation (&lt;code&gt;N+1&lt;/code&gt;) is decoupled from internal catalog and WiredTiger format activation (&lt;code&gt;featureCompatibilityVersion&lt;/code&gt;). Advancing FCV immediately after binary replacement mutates internal system collections (&lt;code&gt;config.version&lt;/code&gt;, &lt;code&gt;config.chunks&lt;/code&gt;, index formats) and blocks clean binary downgrade if a driver bug or query regression surfaces under load.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oplog retention and stepdown catch-up:&lt;/strong&gt; If a secondary is down longer than the primary’s oplog window, it falls off the oplog and requires a full initial sync. Similarly, issuing &lt;code&gt;rs.stepDown()&lt;/code&gt; while secondaries lag causes election stalls and &lt;code&gt;NotWritablePrimary&lt;/code&gt; errors for applications lacking &lt;code&gt;retryWrites=true&lt;/code&gt;. Even on a healthy 3-node replica set, restarting a second node before the first upgraded secondary finishes re-syncing leaves only one voting member online, breaking majority quorum (&lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; writes stall until two of three nodes are back).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-03-automating-mongodb-patch-and-major-version-upgrades-2-constraints.svg&quot; alt=&quot;Sequential N to N+1 upgrades, FCV boundaries, and oplog windows constrain every rollout&quot;&gt;&lt;/p&gt;
&lt;p&gt;The core engineering question is: &lt;strong&gt;How do platform teams codify sharded MongoDB upgrades into an automated pipeline that enforces tier ordering, absorbs primary stepdowns without write errors, and preserves binary rollback during a production soak window?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Three primary patterns exist for upgrading self-managed MongoDB clusters, plus managed Atlas automation:&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Strategy&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Gets Right&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Manual CLI Runbook&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero tooling setup&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Easy to invert CSRS vs &lt;code&gt;mongos&lt;/code&gt; order or skip lag checks before &lt;code&gt;rs.stepDown()&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reject for multi-shard fleets&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Declarative Ansible Rolling Upgrade&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforces tier order, lag gates, NLB draining, and FCV soak with zero extra hardware&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires &lt;code&gt;N -&gt; N+1&lt;/code&gt; sequential runs for multi-version jumps&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Recommended for self-managed EC2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Blue-Green Cluster + &lt;code&gt;mongosync&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Clean &lt;code&gt;N+2&lt;/code&gt; jump on fresh instances; instant DNS/NLB fallback&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Doubles EC2/EBS spend during migration; complex cutover sync&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use only when jumping multiple versions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;MongoDB Atlas Managed Upgrades&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Control plane automates rolling sequence and pre-flight checks&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires migrating off self-managed EC2&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use if adopting Atlas&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Manual shell execution fails under fatigue when managing dozens of nodes across shards. Blue-green cluster replacement doubles infrastructure cost and introduces dual-write or &lt;code&gt;mongosync&lt;/code&gt; lag complexity. A declarative Ansible state machine gives zero-downtime rolling upgrades on existing EC2 instances while keeping FCV pinned at &lt;code&gt;N&lt;/code&gt; until telemetry clears the soak gate.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-03-automating-mongodb-patch-and-major-version-upgrades-3-options.svg&quot; alt=&quot;Compare manual runbooks, Ansible rolling upgrades, blue-green logical migration, and Atlas&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-distributed-upgrade-architecture&quot;&gt;The Distributed Upgrade Architecture&lt;/h2&gt;
&lt;p&gt;A zero-downtime upgrade pipeline coordinates the Ansible control plane, AWS NLB target groups, SDAM-compliant client drivers, and WiredTiger’s FCV compatibility boundary.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-03-automating-mongodb-patch-and-major-version-upgrades-4-architecture.svg&quot; alt=&quot;Ansible orchestrator coordinates NLB draining, SDAM stepdowns, and FCV gating&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-decoupling-binaries-from-fcv-and-patch-vs-major-releases&quot;&gt;1. Decoupling Binaries from FCV (and Patch vs. Major Releases)&lt;/h3&gt;
&lt;p&gt;Patch-version upgrades (for example, &lt;code&gt;6.0.4&lt;/code&gt; to &lt;code&gt;6.0.15&lt;/code&gt;) do not modify &lt;code&gt;featureCompatibilityVersion&lt;/code&gt; or on-disk WiredTiger formats, so rolling back a patch release only requires reinstalling the prior patch binary. Major-version upgrades (&lt;code&gt;5.0 -&gt; 6.0&lt;/code&gt; or &lt;code&gt;6.0 -&gt; 7.0&lt;/code&gt;) introduce on-disk format and catalog changes, which MongoDB gates behind &lt;code&gt;featureCompatibilityVersion&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;On-disk compatibility:&lt;/strong&gt; While &lt;code&gt;mongod&lt;/code&gt; runs version &lt;code&gt;N+1&lt;/code&gt; with &lt;code&gt;featureCompatibilityVersion&lt;/code&gt; pinned to &lt;code&gt;N&lt;/code&gt;, WiredTiger suppresses new major-version on-disk cell, index, and catalog formats so version &lt;code&gt;N&lt;/code&gt; binaries can still mount the data directory if a rollback is required.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Post-soak activation:&lt;/strong&gt; Only after a 7–14 day soak under peak production load do operators advance FCV via &lt;code&gt;db.adminCommand({ setFeatureCompatibilityVersion: &quot;N+1&quot; })&lt;/code&gt; (adding &lt;code&gt;confirm: true&lt;/code&gt; where required in 7.0+ for FCV transitions/downgrades). Once FCV advances, binary downgrade is either unsupported or requires explicit FCV downgrade cleanup first.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pre-flight config, clean shutdown, and oplog headroom:&lt;/strong&gt; Before installing &lt;code&gt;N+1&lt;/code&gt; binaries, audit &lt;code&gt;mongod.conf&lt;/code&gt; for removed startup options (such as &lt;code&gt;storage.journal.enabled&lt;/code&gt;, which was removed in 7.0 because journaling is always on) and inspect &lt;code&gt;db.collection.getIndexes()&lt;/code&gt; for deprecated index versions that newer &lt;code&gt;mongod&lt;/code&gt; releases reject on startup. Always stop &lt;code&gt;mongod&lt;/code&gt; cleanly via &lt;code&gt;SIGTERM&lt;/code&gt; (&lt;code&gt;systemctl stop mongod&lt;/code&gt;) so WiredTiger writes a clean final checkpoint; MongoDB forbids upgrading binaries over an uncleanly shut down data directory that still requires journal recovery. Finally, size &lt;code&gt;oplogMinRetentionHours&lt;/code&gt; against total maintenance duration: if a cluster writes 40 GB/hour of oplog and a 3-node shard takes 30 minutes to roll (&lt;code&gt;serial: 1&lt;/code&gt;), maintain at least 4–6 hours of oplog retention so a delayed node restart never triggers a full initial sync.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-graceful-stepdowns-and-client-sdam-rsstepdown60-30&quot;&gt;2. Graceful Stepdowns and Client SDAM (&lt;code&gt;rs.stepDown(60, 30)&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;To avoid dropping inflight writes when upgrading a replica set primary:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Secondary catch-up grace period:&lt;/strong&gt; &lt;code&gt;rs.stepDown(60, 30)&lt;/code&gt; instructs the primary to quiesce new writes and wait up to 30 seconds for an eligible secondary to catch up to its latest &lt;code&gt;optime&lt;/code&gt; before handing off leadership via &lt;code&gt;replSetStepUp&lt;/code&gt;, while barring the stepped-down node from re-election for 60 seconds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Driver retryability:&lt;/strong&gt; Application connection strings must enable &lt;code&gt;retryWrites=true&lt;/code&gt; and &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; on SDAM-compliant drivers (verified against the MongoDB Driver Compatibility Matrix before upgrading). When a primary steps down, the driver catches &lt;code&gt;NotWritablePrimary&lt;/code&gt;, refreshes topology via &lt;code&gt;hello&lt;/code&gt; heartbeats, and retries the write transparently on the new primary.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-declarative-ansible-rolling-playbook&quot;&gt;3. Declarative Ansible Rolling Playbook&lt;/h3&gt;
&lt;p&gt;The Ansible playbook enforces FCV pre-flight validation, balancer quiescence, secondary lag gating (&lt;code&gt;maxLag &amp;#x3C; 1.0s&lt;/code&gt;), &lt;code&gt;serial: 1&lt;/code&gt; rolling upgrades per replica set, and NLB draining for &lt;code&gt;mongos&lt;/code&gt; routers:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;- &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Phase 0 - Pre-Flight FCV and Balancer Quiescence&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  hosts&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;mongos_routers[0]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  gather_facts&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  tasks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Assert Baseline FCV&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      ansible.builtin.command&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        mongosh --quiet --eval&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;db.adminCommand({getParameter: 1, featureCompatibilityVersion: 1}).featureCompatibilityVersion.version&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      register&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;current_fcv&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      failed_when&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;current_fcv.stdout != target_baseline_fcv&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Stop Balancer and Wait for Idle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      ansible.builtin.command&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;mongosh --quiet --eval &quot;sh.stopBalancer()&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Verify Balancer Idle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      ansible.builtin.command&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;mongosh --quiet --eval &quot;sh.isBalancerRunning()&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      register&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;bal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      until&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;bal.stdout == &quot;false&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      retries&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;30&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      delay&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;- &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Phase 1 - Rolling CSRS Upgrade (Then Repeat for Shards)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  hosts&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;mongo_config_servers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  serial&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  gather_facts&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  tasks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Check Node Role&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      ansible.builtin.command&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;mongosh --quiet --eval &quot;rs.status().members.find(m =&gt; m.self).stateStr&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      register&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;role&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Graceful Stepdown on Primary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      block&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Gate on Secondary Lag &amp;#x3C; 1.0s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          ansible.builtin.command&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            mongosh --quiet --eval &quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;              const s = rs.status(), p = s.members.find(m =&gt; m.stateStr === &apos;PRIMARY&apos;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;              print(Math.max(0, ...s.members.filter(m =&gt; m.stateStr === &apos;SECONDARY&apos;).map(x =&gt; (p.optimeDate - x.optimeDate)/1000)));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          register&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;max_lag&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          failed_when&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(max_lag.stdout | float) &gt; 1.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Step Down Primary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          ansible.builtin.command&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;mongosh --quiet --eval &quot;rs.stepDown(60, 30)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      when&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;role.stdout == &quot;PRIMARY&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Stop mongod, Install N+1 Binary, and Start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      ansible.builtin.systemd&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;mongod&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;stopped&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Install Target Packages&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      ansible.builtin.apt&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;mongodb-org-server={{ target_binary_version }}&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;present&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Start mongod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      ansible.builtin.systemd&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;mongod&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;started&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For &lt;code&gt;mongos&lt;/code&gt; routers in Phase 3, deregister each router from its AWS NLB target group (&lt;code&gt;amazon.aws.elb_target_group_instance&lt;/code&gt; with &lt;code&gt;state: deregister&lt;/code&gt;), pause for connection draining, upgrade &lt;code&gt;mongodb-org-mongos&lt;/code&gt;, assert &lt;code&gt;db.adminCommand({ ping: 1 }).ok == 1&lt;/code&gt;, and re-register before moving to the next router.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Execute every major-version upgrade in four sequential execution phases followed by a mandatory soak gate before advancing FCV.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-03-automating-mongodb-patch-and-major-version-upgrades-5-flow.svg&quot; alt=&quot;Five-phase rolling upgrade workflow with rollback gates&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;phase-1-pre-flight-validation-and-csrs-upgrade&quot;&gt;Phase 1: Pre-Flight Validation and CSRS Upgrade&lt;/h3&gt;
&lt;p&gt;Verify all client drivers meet the target version’s compatibility matrix, confirm &lt;code&gt;rs.printReplicationInfo()&lt;/code&gt; oplog headroom exceeds the maintenance window, assert baseline FCV equals &lt;code&gt;N&lt;/code&gt;, and stop the balancer (&lt;code&gt;sh.stopBalancer()&lt;/code&gt;). Upgrade the 3-node CSRS &lt;code&gt;serial: 1&lt;/code&gt; (secondaries first, then &lt;code&gt;rs.stepDown(60, 30)&lt;/code&gt; on the CSRS primary). Ensure &lt;code&gt;systemd&lt;/code&gt; sets &lt;code&gt;TimeoutStopSec=300&lt;/code&gt; (or call &lt;code&gt;db.adminCommand({ shutdown: 1 })&lt;/code&gt; explicitly) so &lt;code&gt;mongod&lt;/code&gt; never receives &lt;code&gt;SIGKILL&lt;/code&gt; mid-checkpoint on busy nodes, which would force journal recovery and block &lt;code&gt;N+1&lt;/code&gt; startup.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; All CSRS members report &lt;code&gt;version: N+1&lt;/code&gt;, &lt;code&gt;PRIMARY&lt;/code&gt;/&lt;code&gt;SECONDARY&lt;/code&gt; health, and replication lag under 0.5 seconds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; Stop &lt;code&gt;mongod&lt;/code&gt;, reinstall version &lt;code&gt;N&lt;/code&gt; packages, and restart (safe because FCV is still &lt;code&gt;N&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-2-rolling-upgrade-of-shard-replica-sets&quot;&gt;Phase 2: Rolling Upgrade of Shard Replica Sets&lt;/h3&gt;
&lt;p&gt;Upgrade each shard replica set &lt;code&gt;serial: 1&lt;/code&gt; within the shard (shards may run concurrently with each other once CSRS is on &lt;code&gt;N+1&lt;/code&gt;). Gate every primary stepdown on &lt;code&gt;maxSecondaryLag &amp;#x3C; 1.0s&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; Every shard reports &lt;code&gt;N+1&lt;/code&gt; binaries, healthy quorum, and near-zero replication lag.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; Downgrade shard binaries back to &lt;code&gt;N&lt;/code&gt; while FCV remains &lt;code&gt;N&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-3-nlb-drained-mongos-upgrade-and-balancer-resume&quot;&gt;Phase 3: NLB-Drained &lt;code&gt;mongos&lt;/code&gt; Upgrade and Balancer Resume&lt;/h3&gt;
&lt;p&gt;Deregister one &lt;code&gt;mongos&lt;/code&gt; at a time from the AWS NLB target group, wait for inflight connections to drain, upgrade &lt;code&gt;mongodb-org-mongos&lt;/code&gt; to &lt;code&gt;N+1&lt;/code&gt;, verify &lt;code&gt;ping: 1&lt;/code&gt;, and re-register. Only once every &lt;code&gt;mongos&lt;/code&gt; router reports &lt;code&gt;N+1&lt;/code&gt; should you run &lt;code&gt;sh.startBalancer()&lt;/code&gt;, ensuring no legacy router issues mixed-version chunk-migration metadata calls.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; &lt;code&gt;sh.status()&lt;/code&gt; shows all &lt;code&gt;mongos&lt;/code&gt; instances on &lt;code&gt;N+1&lt;/code&gt; and &lt;code&gt;sh.isBalancerRunning()&lt;/code&gt; succeeds without &lt;code&gt;config.locks&lt;/code&gt; errors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; Reinstall &lt;code&gt;N&lt;/code&gt; on &lt;code&gt;mongos&lt;/code&gt; if router issues appear.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-4-operational-soak-window-and-fcv-advancement&quot;&gt;Phase 4: Operational Soak Window and FCV Advancement&lt;/h3&gt;
&lt;p&gt;Run the cluster on &lt;code&gt;N+1&lt;/code&gt; binaries with FCV pinned at &lt;code&gt;N&lt;/code&gt; for 7–14 days. Monitor query latencies, plan cache behavior, and driver error rates. Only after clearing the soak gate, run &lt;code&gt;db.adminCommand({ setFeatureCompatibilityVersion: &quot;N+1&quot; })&lt;/code&gt; (and on 7.0+ note &lt;code&gt;confirm: true&lt;/code&gt; for FCV transitions/downgrades).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; &lt;code&gt;getParameter: { featureCompatibilityVersion: 1 }&lt;/code&gt; returns &lt;code&gt;N+1&lt;/code&gt; across CSRS and all shards.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; During soak (before FCV bump), binary downgrade to &lt;code&gt;N&lt;/code&gt; is available; in 7.0/8.0, query-engine regressions from the Slot-Based Execution (SBE) engine can be mitigated online via &lt;code&gt;db.adminCommand({ setParameter: 1, internalQueryFrameworkControl: &quot;forceClassicEngine&quot; })&lt;/code&gt; without downgrading binaries. Once FCV is advanced, downgrading FCV back to &lt;code&gt;N&lt;/code&gt; in 7.0+ requires &lt;code&gt;confirm: true&lt;/code&gt; and fails if any &lt;code&gt;N+1&lt;/code&gt;-only index or collection feature was created in the interim, making the pre-FCV soak window your cheapest rollback boundary.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Documented MongoDB and WiredTiger behaviors explain why this state machine prevents the most common upgrade outages:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Why mixed-version balancing fails:&lt;/strong&gt; When the balancer runs &lt;code&gt;moveChunk&lt;/code&gt; between shards on different major versions, internal chunk-migration RPCs and metadata schemas can diverge across versions. Stopping the balancer in Phase 0 and keeping it off until CSRS, shards, and &lt;code&gt;mongos&lt;/code&gt; all reach &lt;code&gt;N+1&lt;/code&gt; eliminates cross-version migration aborts and &lt;code&gt;config.locks&lt;/code&gt; contention.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why &lt;code&gt;secondaryCatchUpPeriodSecs&lt;/code&gt; prevents write loss on stepdown:&lt;/strong&gt; Without a catch-up grace period, an immediate stepdown can force a secondary to roll back un-replicated primary writes or delay election while it finishes applying oplog entries. &lt;code&gt;rs.stepDown(60, 30)&lt;/code&gt; pauses new writes on the primary and waits for a voting secondary to reach the exact primary &lt;code&gt;optime&lt;/code&gt; before transferring leadership, keeping election pauses brief enough for &lt;code&gt;retryWrites=true&lt;/code&gt; drivers to absorb.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why &lt;code&gt;internalQueryFrameworkControl&lt;/code&gt; reduces post-upgrade rollback pressure in 7.0/8.0:&lt;/strong&gt; Upgrading into MongoDB 7.0 or 8.0 shifts eligible queries onto the Slot-Based Execution (SBE) engine. If a specific aggregation or &lt;code&gt;$lookup&lt;/code&gt; plan regresses in memory or latency during the soak window, setting &lt;code&gt;internalQueryFrameworkControl: &quot;forceClassicEngine&quot;&lt;/code&gt; reverts query execution to the Classic Engine dynamically without touching binaries or FCV.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why NLB target deregistration prevents &lt;code&gt;mongos&lt;/code&gt; connection storms:&lt;/strong&gt; Simply running &lt;code&gt;systemctl restart mongos&lt;/code&gt; while an AWS Network Load Balancer is actively forwarding TCP flows drops inflight cursors and forces every application pod to tear down and rebuild its connection pool simultaneously. Deregistering one &lt;code&gt;mongos&lt;/code&gt; from the NLB target group and waiting for its active client connections to drain shifts new requests to healthy peers before the daemon restarts, keeping router turnover transparent to application services.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why secondary-first rolling order protects WiredTiger cache:&lt;/strong&gt; Upgrading secondaries one at a time (&lt;code&gt;serial: 1&lt;/code&gt;) means only one member of a replica set is cold-starting its WiredTiger cache at any moment. Waiting for replication lag to drop below 0.5 seconds—and verifying &lt;code&gt;wiredTiger.cache[&quot;pages read into cache&quot;]&lt;/code&gt; settles—before advancing to the next node ensures the replica set always maintains a full voting majority with warm caches before the primary steps down.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Skipping major versions (&lt;code&gt;N -&gt; N+2&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Newer &lt;code&gt;mongod&lt;/code&gt; rejects older on-disk catalog/WiredTiger metadata&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;mongod&lt;/code&gt; aborts startup with &lt;code&gt;InvalidFeatureCompatibilityVersion&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce linear &lt;code&gt;N -&gt; N+1 -&gt; N+2&lt;/code&gt; upgrades&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Upgrading &lt;code&gt;mongos&lt;/code&gt; or shards before CSRS&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Newer routers/shards send metadata commands legacy CSRS cannot parse&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Metadata RPC errors and &lt;code&gt;mongos&lt;/code&gt; routing stalls&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Always upgrade CSRS 100% before shards and &lt;code&gt;mongos&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Bumping FCV before soaking&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Irreversible catalog/index changes block clean binary downgrade&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Post-upgrade driver or query bugs with no rollback path&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Hold FCV at &lt;code&gt;N&lt;/code&gt; for 7–14 days under full peak load&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Stepping down primary with lagging secondaries&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Election stalls or fails while secondaries catch up; client writes time out&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;maxSecondaryLag &gt; 1s&lt;/code&gt; or &lt;code&gt;NotWritablePrimary&lt;/code&gt; errors&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Gate &lt;code&gt;rs.stepDown(60, 30)&lt;/code&gt; on &lt;code&gt;maxLag &amp;#x3C; 1.0s&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Balancer active during mixed-version rolling upgrade&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;moveChunk&lt;/code&gt; fails across disparate shard versions, leaving orphaned docs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;config.changelog&lt;/code&gt; &lt;code&gt;moveChunk.from&lt;/code&gt; errors&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run &lt;code&gt;sh.stopBalancer()&lt;/code&gt; in Phase 0 and verify idle&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Oplog window shorter than node downtime&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Restarted node falls off primary oplog and requires full initial sync&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Node enters &lt;code&gt;RECOVERING&lt;/code&gt; / &lt;code&gt;OplogStartMissing&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Verify &lt;code&gt;rs.printReplicationInfo()&lt;/code&gt; headroom before maintenance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Client drivers missing &lt;code&gt;retryWrites=true&lt;/code&gt; or old SDK&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary stepdown surfaces as unhandled socket/write exceptions&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Application 5xx spikes during &lt;code&gt;rs.stepDown()&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Audit Driver Compatibility Matrix and mandate &lt;code&gt;retryWrites=true&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-03-automating-mongodb-patch-and-major-version-upgrades-6-failures.svg&quot; alt=&quot;Four upgrade failure modes that cause outages or unbootable nodes&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;april-2026-update&quot;&gt;April 2026 Update&lt;/h2&gt;
&lt;p&gt;MongoDB 7.0 and 8.0 introduced important operational safeguards for major upgrades and query-engine transitions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Mandatory &lt;code&gt;confirm: true&lt;/code&gt; on &lt;code&gt;setFeatureCompatibilityVersion&lt;/code&gt; (MongoDB 7.0+):&lt;/strong&gt; Starting in MongoDB 7.0, downgrading FCV or executing guarded FCV transitions requires &lt;code&gt;confirm: true&lt;/code&gt; (&lt;code&gt;db.adminCommand({ setFeatureCompatibilityVersion: &quot;7.0&quot;, confirm: true })&lt;/code&gt;), preventing accidental irreversible metadata changes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamic SBE fallback via &lt;code&gt;internalQueryFrameworkControl&lt;/code&gt; (MongoDB 7.0 / 8.0):&lt;/strong&gt; MongoDB 8.0 enables the Slot-Based Execution (SBE) engine by default for eligible queries; operators can switch back to the Classic Engine dynamically with &lt;code&gt;db.adminCommand({ setParameter: 1, internalQueryFrameworkControl: &quot;forceClassicEngine&quot; })&lt;/code&gt; if a plan regression appears during soak.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Managed rolling upgrades in Atlas:&lt;/strong&gt; For teams on MongoDB Atlas, the control plane automates component sequencing, secondary lag checks, and FCV management, removing the need for self-maintained Ansible playbooks.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Never skip major versions:&lt;/strong&gt; Always upgrade sequentially (&lt;code&gt;N -&gt; N+1 -&gt; N+2&lt;/code&gt;) so &lt;code&gt;mongod&lt;/code&gt; can read on-disk WiredTiger and catalog formats.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Enforce strict sharded tier order:&lt;/strong&gt; Stop the balancer, upgrade CSRS first, upgrade shards second, drain and upgrade &lt;code&gt;mongos&lt;/code&gt; last, then resume the balancer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Decouple binary upgrades from FCV:&lt;/strong&gt; Keep &lt;code&gt;featureCompatibilityVersion&lt;/code&gt; pinned at &lt;code&gt;N&lt;/code&gt; for a 7–14 day production soak before advancing FCV to &lt;code&gt;N+1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate every stepdown on replication lag and driver retries:&lt;/strong&gt; Assert &lt;code&gt;maxSecondaryLag &amp;#x3C; 1.0s&lt;/code&gt; before &lt;code&gt;rs.stepDown(60, 30)&lt;/code&gt; and require &lt;code&gt;retryWrites=true&lt;/code&gt; on SDAM-compliant drivers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code&gt;internalQueryFrameworkControl&lt;/code&gt; for 7.0/8.0 query regressions:&lt;/strong&gt; Fall back to &lt;code&gt;&quot;forceClassicEngine&quot;&lt;/code&gt; online instead of rushing an emergency downgrade.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Audit your self-managed MongoDB upgrade runbooks for manual stepdowns, missing CSRS-first ordering, or immediate FCV bumps.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Codify the &lt;code&gt;Balancer stop -&gt; CSRS -&gt; Shards -&gt; NLB-drained mongos -&gt; Balancer start&lt;/code&gt; state machine in Ansible with automated lag and health gates.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Check &lt;code&gt;db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 })&lt;/code&gt;, &lt;code&gt;rs.printReplicationInfo()&lt;/code&gt;, and application &lt;code&gt;retryWrites=true&lt;/code&gt; driver versions in staging.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Enforce a mandatory 7–14 day soak window at FCV &lt;code&gt;N&lt;/code&gt; on your next patch or major-version upgrade before issuing &lt;code&gt;setFeatureCompatibilityVersion&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>PostgreSQL on Kubernetes: HA Requires Fencing, Replication, and Recovery</title><link>https://rajivonai.com/blog/2026-09-05-postgresql-kubernetes-operator-high-availability/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-09-05-postgresql-kubernetes-operator-high-availability/</guid><description>A production design for PostgreSQL on Kubernetes that separates pod recovery from database failover and makes fencing, durability, client recovery, and restore testing explicit.</description><pubDate>Sat, 05 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A PostgreSQL pod that restarts is not a highly available database. Kubernetes can restore a workload object; it cannot, by itself, decide which copy of PostgreSQL is authoritative, fence an uncertain primary, or prove that acknowledged transactions survived a failure.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Kubernetes has made stateful workloads operationally plausible. A StatefulSet gives each replica a stable ordinal and can associate it with a persistent volume claim. A CSI driver can provision zonal block storage. Topology constraints can distribute pods across nodes and availability zones. An operator can reconcile a PostgreSQL cluster, create replicas, manage configuration, and automate parts of failover.&lt;/p&gt;
&lt;p&gt;Those capabilities remove undifferentiated orchestration work. They do not collapse storage, replication, consensus, backup, and application recovery into one guarantee.&lt;/p&gt;
&lt;p&gt;That distinction matters because the visible symptom of several different failures is identical: the primary pod is unavailable. The underlying events may be very different:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the PostgreSQL process exited while its node and volume remain healthy;&lt;/li&gt;
&lt;li&gt;the node disappeared but the zonal volume is still attachable;&lt;/li&gt;
&lt;li&gt;the availability zone is impaired and the primary volume cannot be reached;&lt;/li&gt;
&lt;li&gt;the control plane cannot determine whether the old primary is dead or merely partitioned;&lt;/li&gt;
&lt;li&gt;a standby is running but has not received or replayed the latest write-ahead log records;&lt;/li&gt;
&lt;li&gt;the database has recovered, but clients still hold broken connections to the previous primary.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Treating each event as “restart the pod” produces unsafe automation. Production HA begins by naming the failure, its fault domain, and the evidence required before promotion.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A StatefulSet provides stable network and storage identity. It does not provide PostgreSQL leader election, replication, write fencing, timeline management, or point-in-time recovery. Persistent volumes also have a lifecycle independent of pods: deleting or rescheduling a pod does not inherently erase its data. With common cloud block storage, however, a volume is normally bound to one zone and attached read-write to one node. If a replacement pod lands in another zone, the scheduler may leave it pending rather than silently move the disk across zones.&lt;/p&gt;
&lt;p&gt;The harder case is ambiguity. Suppose the Kubernetes control plane stops hearing from the node that hosts the primary. Promoting a standby may restore writes, but if the original primary is still accepting traffic through another network path, the system now has two writable histories. PostgreSQL streaming replication does not merge divergent timelines. The old primary must be fenced from clients and replicas before a successor accepts writes.&lt;/p&gt;
&lt;p&gt;Durability is a separate decision. In asynchronous streaming replication, a primary can acknowledge a commit before a standby has persisted the corresponding WAL. Promotion can therefore lose acknowledged transactions up to the replication lag at the failure boundary. Synchronous replication can make selected commit acknowledgements wait for standby confirmation, but its guarantee depends on &lt;code&gt;synchronous_commit&lt;/code&gt;, &lt;code&gt;synchronous_standby_names&lt;/code&gt;, the acknowledgement mode, and the placement and health of the selected standbys. It also trades write availability and latency for a smaller recovery-point objective.&lt;/p&gt;
&lt;p&gt;The engineering question is therefore not “Does the operator support failover?” It is: &lt;strong&gt;under each failure mode, how does the system establish one writable primary, what committed data can be lost, and how do clients and operators verify recovery?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;build-the-ha-contract-around-failure-domains&quot;&gt;Build the HA Contract Around Failure Domains&lt;/h2&gt;
&lt;p&gt;A strong production baseline is three independently scheduled database instances, one volume per instance, explicit anti-affinity or topology-spread constraints, and a tested object-storage backup and WAL-archive path. Two instances can provide failover, but they leave less room to preserve both availability and a synchronous durability rule after one failure. The required instance count must follow the stated failure envelope. The replicas form the database data plane. The operator observes and reconciles that data plane; it is not a substitute for it.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    APP[&quot;Application clients&quot;] --&gt; RW[&quot;Read-write service&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OP[&quot;PostgreSQL operator&quot;] --&gt; P[&quot;Primary — zone A&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OP --&gt; S1[&quot;Standby — zone B&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OP --&gt; S2[&quot;Standby — zone C&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    RW --&gt; P&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    P --&gt;|WAL stream| S1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    P --&gt;|WAL stream| S2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    P --&gt;|WAL archive| OBJ[&quot;Object storage — backups and WAL&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    S1 --&gt; PV1[&quot;Independent zonal volume&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    S2 --&gt; PV2[&quot;Independent zonal volume&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    P --&gt; PV0[&quot;Independent zonal volume&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MON[&quot;Metrics and alerting&quot;] --&gt; P&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MON --&gt; S1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MON --&gt; S2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This topology supports failover because each standby owns a separate PostgreSQL data directory and continuously receives WAL. Sharing one writable filesystem among PostgreSQL servers is not a replication strategy. Likewise, restoring the primary’s volume in another zone is a disaster-recovery operation with storage-specific timing and constraints, not the normal database failover path.&lt;/p&gt;
&lt;h3 id=&quot;define-the-durability-policy-before-choosing-automation&quot;&gt;Define the durability policy before choosing automation&lt;/h3&gt;
&lt;p&gt;Write down the contract in terms an application team can test:&lt;/p&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Policy&lt;/th&gt;&lt;th&gt;Commit acknowledgement&lt;/th&gt;&lt;th&gt;Expected failure behavior&lt;/th&gt;&lt;th&gt;Principal tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Asynchronous replica&lt;/td&gt;&lt;td&gt;Primary local durability&lt;/td&gt;&lt;td&gt;Recent acknowledged commits may be absent after promotion&lt;/td&gt;&lt;td&gt;Lowest steady-state latency; non-zero RPO&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One synchronous acknowledgement&lt;/td&gt;&lt;td&gt;Primary plus a selected standby acknowledgement&lt;/td&gt;&lt;td&gt;Protects the acknowledged commit when that standby is the promotion candidate or the failover policy proves an eligible candidate has the write&lt;/td&gt;&lt;td&gt;Commit latency crosses a failure domain; a different stale replica can still be unsafe to promote&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Quorum synchronous acknowledgement&lt;/td&gt;&lt;td&gt;Primary plus a configured quorum&lt;/td&gt;&lt;td&gt;Protects the configured commit rule only when promotion is constrained to a replica set that must contain the acknowledged write&lt;/td&gt;&lt;td&gt;More network and capacity cost; failover may be refused when the proof cannot be established&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;“Synchronous” is not enough detail. PostgreSQL can wait for a standby to write, flush, or replay WAL, depending on configuration. It also does not control which replica an external failover system will promote. In a three-instance cluster using an &lt;code&gt;ANY 1&lt;/code&gt; rule, standby A can acknowledge a commit while standby B remains behind. If the primary and A then fail, promoting B can lose that acknowledged commit. The failover policy must therefore constrain promotion as well as commit acknowledgement.&lt;/p&gt;
&lt;p&gt;CloudNativePG 1.30 can address this distinction when &lt;code&gt;failoverQuorum: true&lt;/code&gt; is explicitly enabled. The operator then permits failover only when the reachable eligible replicas satisfy its quorum calculation; if that proof is unavailable, it refuses promotion. The separate &lt;code&gt;dataDurability&lt;/code&gt; setting controls how synchronous-commit requirements behave when standbys are unavailable: &lt;code&gt;required&lt;/code&gt; preserves the configured durability requirement, while &lt;code&gt;preferred&lt;/code&gt; can relax it to favor write availability. Neither setting should be assumed to enable the other. Failover quorum is not free consensus added to PostgreSQL; it is a promotion guard whose availability tradeoff must be tested with the exact instance count, synchronous replica count, durability policy, and remote-replica configuration.&lt;/p&gt;
&lt;p&gt;The team must also decide what happens when no synchronous standby is available. Automatically weakening the policy may preserve availability while silently changing the RPO. Refusing commits preserves the durability contract but causes a write outage. Either behavior can be valid; an unobserved transition between them is not.&lt;/p&gt;
&lt;h3 id=&quot;fence-first-promote-second&quot;&gt;Fence first, promote second&lt;/h3&gt;
&lt;p&gt;Promotion is safe only after the previous primary can no longer accept writes. The mechanism depends on the environment and operator, but the invariant is stable:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Remove the old primary from the write endpoint.&lt;/li&gt;
&lt;li&gt;Establish that it is stopped, isolated, or unable to access the database volume and client network.&lt;/li&gt;
&lt;li&gt;Apply the named operator’s eligibility and durability rules, then rank the remaining candidates using WAL position, timeline, and health.&lt;/li&gt;
&lt;li&gt;Promote exactly one candidate.&lt;/li&gt;
&lt;li&gt;Point the write service at the new primary and verify its endpoints.&lt;/li&gt;
&lt;li&gt;Rejoin the old primary only after rewinding or rebuilding it as a replica of the new timeline.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Kubernetes leases and reconciliation state help coordinate controllers, but a generic lease alone does not prove that an unreachable PostgreSQL process has stopped. In a network partition, “not observed” is not the same as “dead.” A production design must identify the authority that performs or verifies fencing and must test what happens when that authority is unavailable.&lt;/p&gt;
&lt;p&gt;CloudNativePG 1.30 combines a primary lease with a primary-isolation check. The primary instance manager renews the lease and, when it cannot reach both the Kubernetes API server and peer instances, initiates shutdown to isolate itself before the operator elects a successor. The default is a smart shutdown: existing sessions can continue until &lt;code&gt;smartShutdownTimeout&lt;/code&gt;, documented as 180 seconds by default. That residual window is part of the fencing budget. A stricter design can set &lt;code&gt;smartShutdownTimeout&lt;/code&gt; to &lt;code&gt;0&lt;/code&gt; so isolation uses fast shutdown, but that changes the interruption behavior and must be validated. Infrastructure-level fencing may still be necessary when the database process or its isolation path cannot execute.&lt;/p&gt;
&lt;h3 id=&quot;treat-client-recovery-as-part-of-database-recovery&quot;&gt;Treat client recovery as part of database recovery&lt;/h3&gt;
&lt;p&gt;Changing a Service selector or endpoint does not migrate established TCP sessions. During promotion, connection pools can retain dead sockets, in-flight transactions can fail with uncertain outcomes, DNS and endpoint propagation take time, and replicas may briefly reject writes while roles converge.&lt;/p&gt;
&lt;p&gt;Clients need bounded connection and statement timeouts, backoff with jitter, and transaction retry rules that distinguish safe retries from ambiguous commits. Operations that may be replayed after an uncertain response should be idempotent or protected by a durable request key. Otherwise, a technically successful promotion can still create duplicate business actions or a prolonged application outage.&lt;/p&gt;
&lt;p&gt;Recovery time must therefore include detection, fencing, candidate selection, promotion, endpoint convergence, and application reconnection. Reporting only the promotion duration understates the actual RTO.&lt;/p&gt;
&lt;h3 id=&quot;keep-backup-and-restore-outside-the-ha-blast-radius&quot;&gt;Keep backup and restore outside the HA blast radius&lt;/h3&gt;
&lt;p&gt;Replicas reproduce logical mistakes and many forms of corruption. They do not replace backups. The design needs base backups, continuous WAL archiving, retention aligned with recovery requirements, and restoration into an isolated environment.&lt;/p&gt;
&lt;p&gt;A restore test should prove more than “the command completed.” Verify the target timestamp, database timeline, expected rows or checksums, role and extension configuration, encryption access, and the time required to make the restored database usable. If the WAL archive is unhealthy, a running standby does not make the recovery plan complete.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented Kubernetes and PostgreSQL behaviors lead to a concrete operating model.&lt;/p&gt;
&lt;p&gt;Kubernetes documents StatefulSets as providing stable identity and persistent storage associations; its persistent-volume model deliberately separates volume lifecycle from pod lifecycle. The operational consequence is that a pod replacement normally attempts to reuse its claim. Placement must remain compatible with the volume’s topology. &lt;code&gt;WaitForFirstConsumer&lt;/code&gt; helps align topology when a volume is initially provisioned; it does not relocate an already bound zonal volume when its pod is rescheduled.&lt;/p&gt;
&lt;p&gt;PostgreSQL documents streaming replication as asynchronous by default and states that failover can lose transactions that were committed but not yet shipped. Its synchronous-replication controls allow a deployment to wait for one or more standbys, but the database may then wait indefinitely when the required standby acknowledgements are unavailable. That is the core CAP-style decision for this subsystem: the failover controller cannot promise both uninterrupted writes and a durability acknowledgement it can no longer obtain.&lt;/p&gt;
&lt;p&gt;CloudNativePG 1.30’s documented failover process ranks eligible instances using status and WAL information and distinguishes automated failover from controlled switchover. WAL position alone does not prove that an arbitrary candidate contains every synchronously acknowledged commit; the configured failover-quorum rule determines whether promotion may proceed when durability is required. Its behavior illustrates the broader operator pattern: automation reduces detection and execution time, while replication state and promotion constraints determine whether the result meets the RPO. The operator should be evaluated by failure injection and observable state transitions, not by the existence of a “failover” feature.&lt;/p&gt;
&lt;p&gt;For each release or infrastructure change, run at least these drills in a non-production environment that preserves production topology:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;terminate only the PostgreSQL process and verify local recovery does not cause unnecessary promotion;&lt;/li&gt;
&lt;li&gt;drain the primary node and observe volume attachment, scheduling, and disruption-budget behavior;&lt;/li&gt;
&lt;li&gt;isolate the primary’s zone and verify fencing precedes promotion;&lt;/li&gt;
&lt;li&gt;add replication lag, then fail the primary and measure the actual data-loss boundary;&lt;/li&gt;
&lt;li&gt;make a required synchronous standby unavailable and confirm whether writes block or the policy is deliberately degraded;&lt;/li&gt;
&lt;li&gt;hold stale client connections through promotion and verify pool recovery and retry safety;&lt;/li&gt;
&lt;li&gt;restore a backup plus archived WAL to a target time and validate application-level data.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Record detection time, fencing time, last acknowledged transaction, promoted WAL location, endpoint convergence, first successful application write, and complete service recovery. These measurements produce defensible RPO and RTO evidence without inventing universal numbers.&lt;/p&gt;
&lt;h3 id=&quot;observe-the-invariants-not-just-pod-health&quot;&gt;Observe the invariants, not just pod health&lt;/h3&gt;
&lt;p&gt;Pod readiness is necessary but insufficient. Alerting and dashboards should expose:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the identity, timeline, and age of the current primary;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pg_stat_replication&lt;/code&gt; state and write, flush, and replay positions;&lt;/li&gt;
&lt;li&gt;replication slots and retained WAL pressure;&lt;/li&gt;
&lt;li&gt;synchronous-standby membership and whether commits are waiting;&lt;/li&gt;
&lt;li&gt;operator conditions, reconciliation failures, and failover events;&lt;/li&gt;
&lt;li&gt;PVC binding, attachment failures, node topology, and unschedulable pods;&lt;/li&gt;
&lt;li&gt;WAL-archive success, last successful base backup, and restore-test age;&lt;/li&gt;
&lt;li&gt;write-service endpoints, connection errors, transaction retries, and time to first successful write.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;An alert should connect symptom to violated objective. “Replica lag is high” becomes actionable when it states that the measured lag exceeds the RPO available for an asynchronous promotion or threatens WAL retention on the primary.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Unsafe assumption&lt;/th&gt;&lt;th&gt;Likely consequence&lt;/th&gt;&lt;th&gt;Required control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Primary process exits&lt;/td&gt;&lt;td&gt;Every restart requires failover&lt;/td&gt;&lt;td&gt;Avoidable promotion and connection churn&lt;/td&gt;&lt;td&gt;Distinguish process recovery from instance failure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Node becomes unreachable&lt;/td&gt;&lt;td&gt;Kubernetes has proved the database stopped&lt;/td&gt;&lt;td&gt;Dual-primary writes after premature promotion&lt;/td&gt;&lt;td&gt;External or infrastructure-backed fencing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Primary zone fails&lt;/td&gt;&lt;td&gt;The primary volume follows the pod to another zone&lt;/td&gt;&lt;td&gt;Pending pod or inaccessible storage&lt;/td&gt;&lt;td&gt;Independent cross-zone replicas and topology-aware provisioning&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Asynchronous primary fails&lt;/td&gt;&lt;td&gt;A healthy standby contains every acknowledged commit&lt;/td&gt;&lt;td&gt;Recent committed transactions disappear&lt;/td&gt;&lt;td&gt;Measured lag, explicit RPO, or synchronous acknowledgement&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Required synchronous standby fails&lt;/td&gt;&lt;td&gt;HA always preserves write availability&lt;/td&gt;&lt;td&gt;Commits block, or durability is silently weakened&lt;/td&gt;&lt;td&gt;Documented degradation policy and alerting&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Service points to new primary&lt;/td&gt;&lt;td&gt;Applications recover immediately&lt;/td&gt;&lt;td&gt;Stale connections and ambiguous transaction results&lt;/td&gt;&lt;td&gt;Pool eviction, bounded retries, idempotency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica is healthy&lt;/td&gt;&lt;td&gt;Backups are unnecessary&lt;/td&gt;&lt;td&gt;Logical corruption or deletion propagates&lt;/td&gt;&lt;td&gt;Isolated backups, WAL archive, timed restore drills&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Old primary returns&lt;/td&gt;&lt;td&gt;It can simply resume as a replica&lt;/td&gt;&lt;td&gt;Divergent timeline or renewed write split&lt;/td&gt;&lt;td&gt;Rewind or rebuild before rejoining&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Operator is running&lt;/td&gt;&lt;td&gt;Recovery objectives are proven&lt;/td&gt;&lt;td&gt;Untested edge cases fail during an incident&lt;/td&gt;&lt;td&gt;Fault-injection evidence and maintained runbooks&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The design also has a cost boundary. Cross-zone synchronous replication adds network latency to commits. Three database instances and independent volumes increase infrastructure cost. Operators add a controller and custom-resource lifecycle that the platform team must understand during upgrades and degraded control-plane conditions. For workloads that can tolerate a managed service’s constraints, transferring part of this responsibility to a database provider may be the more reliable engineering choice.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; A StatefulSet is being treated as database HA. &lt;strong&gt;Solution:&lt;/strong&gt; Document the separate guarantees for scheduling, storage, replication, fencing, backup, and client recovery. &lt;strong&gt;Proof:&lt;/strong&gt; Map every supported failure to the component that detects and contains it. &lt;strong&gt;Action:&lt;/strong&gt; Reject any architecture diagram that cannot identify the authoritative primary and fencing path.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The RPO is implied by labels such as “replicated” or “synchronous.” &lt;strong&gt;Solution:&lt;/strong&gt; Specify the exact acknowledgement mode, standby count, topology, and degradation behavior. &lt;strong&gt;Proof:&lt;/strong&gt; Kill the primary under measured lag and compare acknowledged writes with the promoted state. &lt;strong&gt;Action:&lt;/strong&gt; Publish the result as the service’s tested RPO envelope.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Recovery timing ends at database promotion. &lt;strong&gt;Solution:&lt;/strong&gt; Include endpoint convergence and client transaction recovery in the RTO. &lt;strong&gt;Proof:&lt;/strong&gt; Measure from fault injection to the first successful, verified application write. &lt;strong&gt;Action:&lt;/strong&gt; Add connection-pool and idempotency tests to every failover drill.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Replicas are being used as the recovery plan. &lt;strong&gt;Solution:&lt;/strong&gt; Maintain isolated base backups and continuous WAL archives. &lt;strong&gt;Proof:&lt;/strong&gt; Restore to a chosen point in time and validate business data. &lt;strong&gt;Action:&lt;/strong&gt; Alert on backup age, archive gaps, and restore-test age.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/&quot;&gt;Kubernetes StatefulSets&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/concepts/storage/persistent-volumes/&quot;&gt;Kubernetes Persistent Volumes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/concepts/storage/storage-classes/#volume-binding-mode&quot;&gt;Kubernetes Storage Classes and volume binding&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/current/warm-standby.html&quot;&gt;PostgreSQL warm standby and streaming replication&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/current/runtime-config-replication.html&quot;&gt;PostgreSQL replication configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/current/runtime-config-wal.html#GUC-SYNCHRONOUS-COMMIT&quot;&gt;PostgreSQL synchronous commit modes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloudnative-pg.io/docs/1.30/failover/&quot;&gt;CloudNativePG 1.30 safe election and failover quorum&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloudnative-pg.io/docs/1.30/replication/&quot;&gt;CloudNativePG 1.30 replication&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloudnative-pg.io/docs/1.30/storage/&quot;&gt;CloudNativePG 1.30 storage&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>failures</category><category>checklist</category></item><item><title>Refreshing Production MongoDB into Dev/Test Without Leaking Production Data</title><link>https://rajivonai.com/blog/2026-06-03-refreshing-production-mongodb-into-dev-test/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-03-refreshing-production-mongodb-into-dev-test/</guid><description>How to safely clone multi-terabyte MongoDB production datasets for development using EBS snapshots, isolated scrubbing VPCs, and automated PyMongo masking.</description><pubDate>Fri, 04 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Restoring a raw production MongoDB snapshot directly into a development or staging VPC exposes customer PII and lets lower-environment workers fire live third-party webhooks and billing APIs before any post-boot masking script can finish.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Booting production EBS snapshots in lower environments creates an immediate PII exposure window and lets background workers drain live webhook, email, and payment queues.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Mount the production snapshot inside an air-gapped Scrubbing VPC (no IGW, no NAT, empty egress), run standalone &lt;code&gt;mongod&lt;/code&gt; on &lt;code&gt;127.0.0.1&lt;/code&gt;, scrub via &lt;code&gt;bulk_write(ordered=False)&lt;/code&gt; with deterministic SHA-256 hashing, run &lt;code&gt;compact&lt;/code&gt;, and snapshot only after a zero-leak gate passes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Scrubbing with &lt;code&gt;--replSet&lt;/code&gt; enabled bloats &lt;code&gt;local.oplog.rs&lt;/code&gt;, static placeholder emails fail with &lt;code&gt;E11000&lt;/code&gt; on unique indexes, and skipping &lt;code&gt;compact&lt;/code&gt; leaves fragmented WiredTiger extents in Dev snapshots.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-03-refreshing-production-mongodb-into-dev-test.svg&quot; alt=&quot;Sanitize production MongoDB snapshots inside an air-gapped VPC before dev restore&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Engineering teams running self-managed MongoDB on AWS EC2 often need production-like data shapes in staging and development environments to reproduce cardinality skew, test multi-stage aggregations, and benchmark index builds. Synthetic seed scripts rarely capture real-world document size variance or skewed tenant distributions, so teams restore AWS EBS snapshots of production data into lower environments.&lt;/p&gt;
&lt;p&gt;Production databases, however, contain regulated PII (emails, phone numbers, billing addresses) alongside active operational state: pending background job queues, webhook payloads, and third-party API credentials (such as payment, email, or SMS tokens). Moving a production snapshot into a lower environment where developers hold broader IAM/SSH access and microservices run against NAT gateways turns a routine refresh into a security and operational incident.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cloning raw production snapshots into development fails on three concrete security and storage constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The post-boot exposure window:&lt;/strong&gt; Restoring a snapshot into a development subnet and running a masking script afterward leaves raw PII and production credentials readable to lower-environment pods, developers, and log aggregators until the script completes—which can take hours on large collections.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phantom webhook and API execution:&lt;/strong&gt; Staging worker containers boot and connect to the restored database immediately. If the database still contains pending job documents or live third-party API keys and the staging subnet has outbound internet access via a NAT Gateway, workers immediately dispatch real charges, emails, or webhooks to external endpoints.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;WiredTiger extent fragmentation under mass updates:&lt;/strong&gt; Replacing variable-length strings (such as emails or addresses) across millions of documents forces WiredTiger to allocate new &lt;code&gt;WT_UPDATE&lt;/code&gt; descriptors and disk extents rather than overwriting bytes in place, leaving unallocated holes inside &lt;code&gt;.wt&lt;/code&gt; files unless compacted.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-03-refreshing-production-mongodb-into-dev-test-2-constraints.svg&quot; alt=&quot;PII exposure windows, phantom webhooks, and WiredTiger fragmentation constrain refresh pipelines&quot;&gt;&lt;/p&gt;
&lt;p&gt;The core engineering question is: &lt;strong&gt;How can platform teams automate multi-terabyte production-to-dev MongoDB refreshes while guaranteeing zero PII exposure, zero outbound API calls, and compact developer snapshots?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Four approaches exist for populating development and staging MongoDB environments:&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Approach&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Gets Right&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Synthetic Seed Data&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero PII risk; fast CI startup&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Misses production cardinality skew, index selectivity, and edge-case documents&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use for unit/CI tests only&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Post-Boot Scrubbing in Dev VPC&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Simple single-VPC automation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exposes raw PII and live job queues until masking finishes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reject&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Air-Gapped Scrubbing VPC&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero egress (&lt;code&gt;ENETUNREACH&lt;/code&gt;), deterministic masking, &lt;code&gt;compact&lt;/code&gt; before Dev sees data&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires isolated VPC + ephemeral EC2 automation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Recommended for EC2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;CSFLE / Queryable Encryption (7.0+)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sensitive fields remain ciphertext without production KMS key access&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires application schema changes and &lt;code&gt;libmongocrypt&lt;/code&gt;; does not purge job queues&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pair with Scrubbing VPC long-term&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Post-boot scrubbing inside the target Dev VPC is the most common anti-pattern: even a brief race between &lt;code&gt;mongod&lt;/code&gt; startup and the masking script lets staging workers read unmasked records or fire queued webhooks. Isolating the entire scrub inside an air-gapped VPC ensures no unmasked snapshot ever reaches a developer-accessible network.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-03-refreshing-production-mongodb-into-dev-test-3-options.svg&quot; alt=&quot;Compare synthetic seed data, post-boot dev scrubbing, air-gapped VPC scrubbing, and CSFLE&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-air-gapped-scrubbing-architecture&quot;&gt;The Air-Gapped Scrubbing Architecture&lt;/h2&gt;
&lt;p&gt;The hardened refresh pipeline inserts an ephemeral, zero-egress Scrubbing VPC between Production EBS snapshots and the target Development VPC.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-03-refreshing-production-mongodb-into-dev-test-4-architecture.svg&quot; alt=&quot;Air-gapped Scrubbing VPC boundary and standalone mongod&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-network-air-gapping-kms-key-isolation-and-iam-least-privilege&quot;&gt;1. Network Air-Gapping, KMS Key Isolation, and IAM Least Privilege&lt;/h3&gt;
&lt;p&gt;The Scrubbing VPC contains &lt;strong&gt;no Internet Gateway (IGW), no NAT Gateway, and no VPC Peering or Transit Gateway attachments&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The subnet route table holds only the local VPC CIDR route (for example, &lt;code&gt;10.50.0.0/16 -&gt; local&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The EC2 security group attaches an empty egress rule set (&lt;code&gt;egress: []&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Any accidental outbound TCP handshake from a process on the scrubbing host fails immediately at the hypervisor/kernel layer (&lt;code&gt;ENETUNREACH&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;KMS and IAM boundary:&lt;/strong&gt; Production EBS snapshots are encrypted with a Production AWS KMS Customer Managed Key (CMK). The refresh pipeline copies and re-encrypts the snapshot using a dedicated Scrubbing/Dev KMS key before mounting it in the Scrubbing VPC, and the scrubbing instance’s IAM instance profile is denied access to Production S3 buckets, Secrets Manager, and SQS/SNS endpoints. Only &lt;code&gt;ec2:CreateSnapshot&lt;/code&gt; and &lt;code&gt;ec2:CreateTags&lt;/code&gt; for the &lt;code&gt;Developer-Ready&lt;/code&gt; output snapshot are permitted.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-standalone-mode-127001-and-sharded-cluster-handling&quot;&gt;2. Standalone Mode (&lt;code&gt;127.0.0.1&lt;/code&gt;) and Sharded Cluster Handling&lt;/h3&gt;
&lt;p&gt;Boot the scrubbing &lt;code&gt;mongod&lt;/code&gt; in &lt;strong&gt;standalone mode&lt;/strong&gt;—without &lt;code&gt;--replSet&lt;/code&gt;—bound strictly to &lt;code&gt;127.0.0.1&lt;/code&gt; on a non-standard port (such as &lt;code&gt;27099&lt;/code&gt;). Before starting, drop &lt;code&gt;local.system.replset&lt;/code&gt; if needed so &lt;code&gt;mongod&lt;/code&gt; does not attempt to contact production replica set peers, and omit &lt;code&gt;--replSet&lt;/code&gt; so mass updates do not write gigabytes of mutations into &lt;code&gt;local.oplog.rs&lt;/code&gt; (masking 100 million documents at ~500 bytes per oplog entry would otherwise generate ~50 GB of unnecessary oplog writes and double EBS &lt;code&gt;gp3&lt;/code&gt; write IOPS). For sharded clusters, repeat the standalone scrub on each shard’s hidden-secondary snapshot (and scrub any sensitive metadata in the CSRS snapshot if needed) under the same quiesced snapshot batch before tagging the set for Dev restore; also verify that no PII field being rewritten is part of a collection’s shard key, as mutating shard keys out-of-band on standalone shards would desynchronize &lt;code&gt;config.chunks&lt;/code&gt; boundaries.&lt;/p&gt;
&lt;h3 id=&quot;3-deterministic-bulk-masking-and-compaction-pymongo&quot;&gt;3. Deterministic Bulk Masking and Compaction (&lt;code&gt;pymongo&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Sequential &lt;code&gt;updateOne&lt;/code&gt; calls serialize round-trip latency across millions of documents. Instead, stream &lt;code&gt;_id&lt;/code&gt; projections with &lt;code&gt;no_cursor_timeout=True&lt;/code&gt; (always closing the cursor in a &lt;code&gt;finally&lt;/code&gt; block) and apply batched &lt;code&gt;bulk_write(operations, ordered=False)&lt;/code&gt; updates. Use deterministic SHA-256 hashing of &lt;code&gt;_id&lt;/code&gt; (or keyed HMAC-SHA256 with a Dev-scoped secret when &lt;code&gt;_id&lt;/code&gt; values are enumerable integers or predictable &lt;code&gt;ObjectId&lt;/code&gt;s) for unique-indexed fields like &lt;code&gt;email&lt;/code&gt; so masking never triggers &lt;code&gt;E11000 duplicate key&lt;/code&gt; errors and remains reproducible across refreshes. Afterward, run &lt;code&gt;db.runCommand({ compact: &quot;users&quot; })&lt;/code&gt;—which on WiredTiger rewrites B-tree extents in place without requiring &lt;code&gt;2x&lt;/code&gt; free disk space and releases unused blocks back to the OS without blocking normal CRUD—before taking the clean snapshot.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env python3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse, hashlib, sys&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient, UpdateOne&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; sanitize&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(uri, db_name, batch_size, dry_run):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    db &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient(uri, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;serverSelectionTimeoutMS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)[db_name]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dry_run:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        db.api_credentials.update_many({}, {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$set&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;api_key&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;MOCKED_DEV_KEY_REDACTED&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        db.background_jobs.delete_many({})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cursor &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.users.find({}, {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;no_cursor_timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ops &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; doc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cursor:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            digest &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; hashlib.sha256(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(doc[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]).encode()).hexdigest()[:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            ops.append(UpdateOne({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: doc[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]}, {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$set&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;email&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;user_&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;digest&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;@masked-example.internal&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;phone&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;+15550000000&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;first_name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SyntheticFirst&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;last_name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SyntheticLast&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            }}))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ops) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; batch_size:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dry_run:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                    db.users.bulk_write(ops, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;ordered&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                ops.clear()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ops &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dry_run:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            db.users.bulk_write(ops, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;ordered&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    finally&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        cursor.close()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dry_run:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        db.command({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;compact&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;users&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        leaks &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.users.count_documents({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;email&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$not&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$regex&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#DBEDFF&quot;&gt;@masked-example&lt;/span&gt;&lt;span style=&quot;color:#85E89D;font-weight:bold&quot;&gt;\.&lt;/span&gt;&lt;span style=&quot;color:#DBEDFF&quot;&gt;internal&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}}})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; leaks &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            sys.exit(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Audit failed: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;leaks&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; unmasked records detected.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; __name__&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse.ArgumentParser()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--uri&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;default&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;mongodb://127.0.0.1:27099&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--db-name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;default&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;commerce_db&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--batch-size&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;default&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--dry-run&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;action&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;store_true&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    a &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; p.parse_args()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    sanitize(a.uri, a.db_name, a.batch_size, a.dry_run)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;4-pre-snapshot-regex-audit-mongosh&quot;&gt;4. Pre-Snapshot Regex Audit (&lt;code&gt;mongosh&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Verify zero unmasked emails, phone numbers, API credentials, or pending background jobs remain before shutting down &lt;code&gt;mongod&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; unmaskedEmails&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.users.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;countDocuments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ email: { $not:&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#DBEDFF&quot;&gt;@masked-example&lt;/span&gt;&lt;span style=&quot;color:#85E89D;font-weight:bold&quot;&gt;\.&lt;/span&gt;&lt;span style=&quot;color:#DBEDFF&quot;&gt;internal&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; activeSecrets&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.api_credentials.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;countDocuments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ api_key: { $not:&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;^&lt;/span&gt;&lt;span style=&quot;color:#DBEDFF&quot;&gt;MOCKED_&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; pendingJobs&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.background_jobs.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;countDocuments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (unmaskedEmails &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; activeSecrets &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pendingJobs) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  throw&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; Error&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Sanitization audit failed; aborting snapshot.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Run the refresh pipeline in four gated phases so an unverified volume can never be tagged for development use.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-03-refreshing-production-mongodb-into-dev-test-5-flow.svg&quot; alt=&quot;Four gated phases from raw snapshot to clean Dev clone&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;phase-1-mount-snapshot-in-air-gapped-vpc-and-verify-isolation&quot;&gt;Phase 1: Mount Snapshot in Air-Gapped VPC and Verify Isolation&lt;/h3&gt;
&lt;p&gt;Launch an ephemeral EC2 host in the Scrubbing VPC, attach the restored production EBS volume, and run an outbound socket probe (&lt;code&gt;socket.create_connection((&apos;api.stripe.com&apos;, 443), timeout=2)&lt;/code&gt;) before starting &lt;code&gt;mongod&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; The outbound socket test must fail with timeout or &lt;code&gt;ENETUNREACH&lt;/code&gt;. Start &lt;code&gt;mongod&lt;/code&gt; in standalone mode on &lt;code&gt;127.0.0.1:27099&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; If the network probe succeeds, terminate the EC2 instance immediately and fix VPC routing/security groups.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-2-purge-secretsqueues-rotate-db-users-and-execute-bulk-masking&quot;&gt;Phase 2: Purge Secrets/Queues, Rotate DB Users, and Execute Bulk Masking&lt;/h3&gt;
&lt;p&gt;Delete or truncate operational job queues (&lt;code&gt;background_jobs&lt;/code&gt;, webhook outboxes), redact third-party API credentials, and drop or replace production database user credentials in &lt;code&gt;admin.system.users&lt;/code&gt; (along with &lt;code&gt;config.system.sessions&lt;/code&gt; and &lt;code&gt;admin.system.keys&lt;/code&gt;) so lower environments authenticate strictly with Dev-only SCRAM credentials. Also drop &lt;code&gt;local.oplog.rs&lt;/code&gt; from the restored volume: even though standalone &lt;code&gt;mongod&lt;/code&gt; writes no new oplog records, the production snapshot’s &lt;code&gt;local.oplog.rs&lt;/code&gt; still retains historical pre-scrub PII mutations until dropped and re-initialized in Dev. If the cluster uses GridFS (&lt;code&gt;*.files&lt;/code&gt; and &lt;code&gt;*.chunks&lt;/code&gt;), purge or replace binary attachment chunks here as well so unmasked documents or CSVs do not survive outside standard collections. Then run the PyMongo &lt;code&gt;bulk_write(ordered=False)&lt;/code&gt; masking pass across all PII collections, checking &lt;code&gt;db.getCollectionNames()&lt;/code&gt; against an explicit allowlist/manifest so newly added production collections never slip through unmasked.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; Zero &lt;code&gt;E11000&lt;/code&gt; duplicate key errors, zero unmapped collections outside the manifest, and all batches stay under the 16 MB BSON wire limit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; Abort and terminate the ephemeral instance; production snapshots are read-only and unaffected.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-3-run-zero-leak-audit-gate-and-compact&quot;&gt;Phase 3: Run Zero-Leak Audit Gate and &lt;code&gt;compact&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Execute the regex audit script across all masked collections, credential tables, and job queues. Once the audit returns zero leaks, run &lt;code&gt;db.runCommand({ compact: &quot;&amp;#x3C;collection&gt;&quot; })&lt;/code&gt; on each heavily updated collection to reclaim fragmented WiredTiger extents. Sizing the ephemeral scrubbing EC2 instance with enough RAM so WiredTiger’s cache holds the &lt;code&gt;_id&lt;/code&gt; and unique index B-trees avoids EBS read-amplification stalls during &lt;code&gt;bulk_write&lt;/code&gt;, and running &lt;code&gt;compact&lt;/code&gt; sequentially across every scrubbed collection rewrites sparse leaf pages so the resulting &lt;code&gt;Developer-Ready&lt;/code&gt; EBS snapshot does not carry wasted extent blocks into Dev.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; &lt;code&gt;unmaskedEmails === 0&lt;/code&gt;, &lt;code&gt;activeSecrets === 0&lt;/code&gt;, &lt;code&gt;pendingJobs === 0&lt;/code&gt;, and &lt;code&gt;compact&lt;/code&gt; returns &lt;code&gt;{ ok: 1 }&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; If any unmasked record is found, exit non-zero, skip snapshotting, and terminate the instance.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-4-clean-shutdown-snapshot-tagging-and-dev-restore&quot;&gt;Phase 4: Clean Shutdown, Snapshot Tagging, and Dev Restore&lt;/h3&gt;
&lt;p&gt;Stop &lt;code&gt;mongod&lt;/code&gt; cleanly (&lt;code&gt;db.adminCommand({ shutdown: 1 })&lt;/code&gt;) and confirm the process exits &lt;code&gt;0&lt;/code&gt; so WiredTiger flushes a final checkpoint with zero pending journal recovery, create the new EBS snapshot tagged &lt;code&gt;Environment: Developer-Ready&lt;/code&gt;, terminate the scrubbing EC2 instance, and restore the clean snapshot into the Dev VPC.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; Dev replica set boots cleanly with &lt;code&gt;rs.reconfig()&lt;/code&gt; for Dev hostnames and passes smoke queries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; Retain the previous &lt;code&gt;Developer-Ready&lt;/code&gt; snapshot tag so Dev can roll back if a schema change breaks staging.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Documented WiredTiger, PyMongo, and AWS VPC behaviors explain why each safeguard in this pipeline is necessary:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Why &lt;code&gt;bulk_write(ordered=False)&lt;/code&gt; outperforms single updates:&lt;/strong&gt; Single &lt;code&gt;updateOne&lt;/code&gt; calls pay a full network round-trip and server write-lock acquisition per document. &lt;code&gt;bulk_write(operations, ordered=False)&lt;/code&gt; packs thousands of updates into each wire-protocol batch (bounded by &lt;code&gt;maxWriteBatchSize&lt;/code&gt; and the 16 MB BSON limit) and allows &lt;code&gt;mongod&lt;/code&gt; to process independent document updates without strict sequential ordering. Keeping the batch size at 5,000–10,000 small &lt;code&gt;UpdateOne&lt;/code&gt; specs avoids hitting the 16 MB BSON message cap while saturating local NVMe/EBS write throughput without overwhelming WiredTiger’s dirty-cache eviction thresholds. Pairing that stream with &lt;code&gt;no_cursor_timeout=True&lt;/code&gt; prevents &lt;code&gt;mongod&lt;/code&gt;’s default 10-minute idle cursor timeout (&lt;code&gt;cursorTimeoutMillis&lt;/code&gt;) from killing long-running scans when a batch stalls on disk compaction or I/O, while &lt;code&gt;try ... finally: cursor.close()&lt;/code&gt; ensures no server cursor leaks if an exception aborts the run.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why deterministic SHA-256 masking prevents &lt;code&gt;E11000&lt;/code&gt; failures and preserves cross-collection joins:&lt;/strong&gt; Production &lt;code&gt;users&lt;/code&gt; collections almost always enforce a unique index on &lt;code&gt;email&lt;/code&gt;. Replacing emails with a static string fails immediately on the second document with &lt;code&gt;E11000 duplicate key error&lt;/code&gt;. Hashing the immutable &lt;code&gt;_id&lt;/code&gt; (&lt;code&gt;hashlib.sha256(str(_id)).hexdigest()[:12]&lt;/code&gt;) preserves uniqueness across millions of documents while keeping test fixtures deterministic across refreshes. When secondary collections (such as &lt;code&gt;orders&lt;/code&gt; or &lt;code&gt;audit_logs&lt;/code&gt;) denormalize &lt;code&gt;user_email&lt;/code&gt;, applying the same deterministic hash function keyed by &lt;code&gt;user_id&lt;/code&gt; keeps cross-collection lookups valid in Dev without storing a reversible mapping table.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why standalone mode + &lt;code&gt;compact&lt;/code&gt; controls snapshot cost:&lt;/strong&gt; Running updates against a replica set writes every masked field to &lt;code&gt;local.oplog.rs&lt;/code&gt;, doubling write I/O and inflating the EBS volume. Booting standalone eliminates oplog generation, and running &lt;code&gt;compact&lt;/code&gt; afterward rewrites fragmented WiredTiger leaf pages so the resulting &lt;code&gt;Developer-Ready&lt;/code&gt; EBS snapshot does not carry gigabytes of empty extent holes. Note that if masked strings are significantly longer than original production values, B-tree leaf pages split during masking; keeping synthetic replacement strings close in length to production distributions minimizes extent growth before &lt;code&gt;compact&lt;/code&gt; runs.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Scrubbing subnet has IGW or NAT route&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Booted workers fire live webhooks or API calls to Stripe/email providers&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Outbound socket probe succeeds; VPC Flow Logs show egress&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce zero IGW/NAT/peering and &lt;code&gt;egress: []&lt;/code&gt; security group&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Static placeholder on unique-indexed field&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;bulk_write&lt;/code&gt; aborts on second document with &lt;code&gt;E11000 duplicate key&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Scrubber exits with &lt;code&gt;BulkWriteError&lt;/code&gt; &lt;code&gt;E11000&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Derive synthetic unique values from &lt;code&gt;SHA-256(_id)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Running scrubber with &lt;code&gt;--replSet&lt;/code&gt; active&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Every masked document writes to &lt;code&gt;local.oplog.rs&lt;/code&gt;, filling disk&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Disk usage spikes during scrub; slow update rate&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Boot &lt;code&gt;mongod&lt;/code&gt; standalone without &lt;code&gt;--replSet&lt;/code&gt; on &lt;code&gt;127.0.0.1&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unclosed &lt;code&gt;no_cursor_timeout=True&lt;/code&gt; cursor&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cursor leaks server memory if an exception aborts mid-run&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rising open cursor count in &lt;code&gt;serverStatus().metrics.cursor&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Wrap cursor iteration in &lt;code&gt;try ... finally: cursor.close()&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Skipping &lt;code&gt;compact&lt;/code&gt; after mass string updates&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Variable-length string replacements fragment &lt;code&gt;.wt&lt;/code&gt; extents&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Clean snapshot size is significantly larger than prod data&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run &lt;code&gt;db.runCommand({ compact: &quot;&amp;#x3C;coll&gt;&quot; })&lt;/code&gt; before snapshotting&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Stale &lt;code&gt;local.system.replset&lt;/code&gt; on Dev boot&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Restored Dev node tries to contact production hostnames&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;mongod&lt;/code&gt; stuck in &lt;code&gt;RECOVERING&lt;/code&gt; or &lt;code&gt;STARTUP2&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Drop &lt;code&gt;local.system.replset&lt;/code&gt; or run &lt;code&gt;rs.reconfig(..., { force: true })&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Missing job-queue purge in new collections&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Newly added queue collections survive masking and trigger in Dev&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Non-zero count in new queue collections after scrub&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Audit collection list dynamically in Phase 3 gate&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-03-refreshing-production-mongodb-into-dev-test-6-failures.svg&quot; alt=&quot;Four failure modes in production-to-dev MongoDB sanitization pipelines&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;april-2026-update&quot;&gt;April 2026 Update&lt;/h2&gt;
&lt;p&gt;MongoDB 7.0 and 8.0 added native encryption and background compaction capabilities that complement air-gapped scrubbing pipelines:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Queryable Encryption (QE) equality (7.0) and range queries (8.0):&lt;/strong&gt; MongoDB 7.0 made Queryable Encryption GA for equality queries, and MongoDB 8.0 added range queries (&lt;code&gt;$lt&lt;/code&gt;, &lt;code&gt;$lte&lt;/code&gt;, &lt;code&gt;$gt&lt;/code&gt;, &lt;code&gt;$gte&lt;/code&gt;) on encrypted numeric and date fields. When combined with Client-Side Field Level Encryption (CSFLE) and separate KMS keys per environment, encrypted PII fields remain unreadable ciphertext in lower environments without access to the production KMS key—though scrubbing job queues and API credentials remains mandatory.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Background compaction (&lt;code&gt;autoCompact&lt;/code&gt;, MongoDB 8.0):&lt;/strong&gt; MongoDB 8.0 introduced &lt;code&gt;db.runCommand({ autoCompact: true })&lt;/code&gt; for background space reclamation on WiredTiger without blocking normal CRUD, complementing explicit &lt;code&gt;compact&lt;/code&gt; runs at the end of sanitization jobs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;VPC Flow Log and Network Firewall guardrails:&lt;/strong&gt; Pairing air-gapped scrubbing subnets with automated VPC Flow Log alerts ensures any accidental route-table change that enables egress is caught immediately.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Never scrub production snapshots inside the target Dev VPC:&lt;/strong&gt; Any delay between &lt;code&gt;mongod&lt;/code&gt; startup and masking completion exposes raw PII and lets staging workers fire live webhooks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Enforce physical network isolation first:&lt;/strong&gt; Use a dedicated Scrubbing VPC with no IGW, no NAT Gateway, empty egress rules, and an explicit outbound socket failure check before starting &lt;code&gt;mongod&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Run standalone &lt;code&gt;mongod&lt;/code&gt; + &lt;code&gt;bulk_write(ordered=False)&lt;/code&gt; + &lt;code&gt;SHA-256(_id)&lt;/code&gt;:&lt;/strong&gt; Eliminate &lt;code&gt;local.oplog.rs&lt;/code&gt; bloat, avoid &lt;code&gt;E11000&lt;/code&gt; unique index collisions, and keep masking deterministic.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate snapshotting on a zero-leak audit and &lt;code&gt;compact&lt;/code&gt;:&lt;/strong&gt; Abort and terminate the host if any unmasked PII, API secret, or pending job remains, and compact WiredTiger extents before creating the &lt;code&gt;Developer-Ready&lt;/code&gt; snapshot.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Check whether your Dev/Staging MongoDB refresh pipeline boots raw production snapshots in a VPC with NAT/IGW access or masks data after Dev pods connect.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Provision an air-gapped Scrubbing VPC, run standalone &lt;code&gt;mongod&lt;/code&gt; on &lt;code&gt;127.0.0.1&lt;/code&gt;, mask via &lt;code&gt;bulk_write(ordered=False)&lt;/code&gt; with &lt;code&gt;SHA-256(_id)&lt;/code&gt;, validate zero leaks, and &lt;code&gt;compact&lt;/code&gt; before snapshotting.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Verify that an outbound socket probe fails inside the Scrubbing VPC and that the post-scrub regex audit returns &lt;code&gt;0&lt;/code&gt; unmasked records and &lt;code&gt;0&lt;/code&gt; pending jobs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your refresh scripts and staging job-queue collections this week to ensure every credential and webhook queue is purged before any snapshot is tagged &lt;code&gt;Developer-Ready&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>MongoDB Backup and Restore on EC2: LVM/EBS Snapshots vs mongodump</title><link>https://rajivonai.com/blog/2026-06-01-backup-restore-ec2/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-01-backup-restore-ec2/</guid><description>Why mongodump fails for multi-terabyte sharded clusters, and how to orchestrate point-in-time consistent AWS EBS snapshots using dedicated hidden secondaries.</description><pubDate>Thu, 03 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Logical dump tools cannot produce a cluster-consistent backup of a multi-terabyte sharded MongoDB deployment; reliable recovery on AWS EC2 requires stopping the balancer and taking coordinated block-level EBS snapshots of dedicated hidden secondaries.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; &lt;code&gt;mongodump --oplog&lt;/code&gt; only guarantees point-in-time consistency within a single replica set, so multi-hour sharded dumps suffer WiredTiger cache eviction and cross-shard chunk skew.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Stop the balancer, optionally &lt;code&gt;db.fsyncLock()&lt;/code&gt; dedicated hidden secondaries (&lt;code&gt;priority: 0, hidden: true&lt;/code&gt;) on every shard and the CSRS, trigger AWS EBS snapshots, and unlock immediately.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Keep data and journal on one EBS volume (or use single-instance &lt;code&gt;ec2:CreateSnapshots&lt;/code&gt;), and remember that &lt;code&gt;CreateSnapshots&lt;/code&gt; is atomic per EC2 instance—not across multiple shards.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-01-backup-restore-ec2.svg&quot; alt=&quot;Stop the balancer and snapshot dedicated hidden secondaries to keep primaries unlocked&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Self-managed MongoDB sharded clusters on AWS EC2—composed of a 3-node Config Server Replica Set (CSRS), multiple shard replica sets on memory-optimized instances, and EBS &lt;code&gt;gp3&lt;/code&gt; storage volumes—must satisfy strict Recovery Point Objectives (RPO) and Recovery Time Objectives (RTO) for audit and disaster recovery compliance.&lt;/p&gt;
&lt;p&gt;Many teams start with MongoDB Database Tools (&lt;code&gt;mongodump&lt;/code&gt; and &lt;code&gt;mongorestore&lt;/code&gt;) streaming BSON archives to Amazon S3. For small standalone databases or single replica sets under a few hundred gigabytes, &lt;code&gt;mongodump&lt;/code&gt; is simple and portable. Once a sharded cluster grows into multiple terabytes, however, &lt;code&gt;mongodump&lt;/code&gt; shifts from a convenience utility into a production hazard: full collection scans evict active working sets from the WiredTiger cache, restore times stretch past RTO limits, and cross-shard consistency breaks down completely.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Logical backups and naive snapshot scripts fail on sharded EC2 clusters because of four physical and distributed constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;No cross-shard &lt;code&gt;--oplog&lt;/code&gt; synchronization:&lt;/strong&gt; While &lt;code&gt;mongodump --oplog&lt;/code&gt; captures oplog entries for a single replica set during a dump, it cannot freeze or align a global point-in-time timestamp across multiple independent shards and the CSRS. If Shard 1 finishes dumping hours before Shard 2 while the balancer migrates chunks or distributed transactions commit, restoring the shards via &lt;code&gt;mongorestore&lt;/code&gt; yields duplicated or missing documents across chunk boundaries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;WiredTiger cache eviction and restore RTO math:&lt;/strong&gt; &lt;code&gt;mongodump&lt;/code&gt; reads every document in every collection sequentially. On datasets larger than RAM, paging cold documents off EBS volumes evicts hot B-tree pages from the WiredTiger cache, causing read latency spikes on live traffic. Worse, &lt;code&gt;mongorestore&lt;/code&gt; only dumps and restores index &lt;em&gt;definitions&lt;/em&gt;, forcing &lt;code&gt;mongod&lt;/code&gt; to rebuild every secondary B-tree index from scratch on restore. Exporting a 4 TB cluster at 150 MB/s takes ~7.4 hours, and rebuilding indexes across multi-terabyte collections stretches total RTO past 12–24 hours—whereas block-level EBS snapshots preserve pre-built &lt;code&gt;.wt&lt;/code&gt; index B-trees and replay at most 60 seconds of &lt;code&gt;WiredTigerLog.*&lt;/code&gt; journal records on boot.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Single-volume vs. multi-volume journal atomicity:&lt;/strong&gt; WiredTiger writes checkpoints every 60 seconds (&lt;code&gt;checkpoint=(wait=60)&lt;/code&gt;) and appends intervening writes to &lt;code&gt;journal/WiredTigerLog.*&lt;/code&gt;. If data and journal reside on the same EBS volume, a single EBS snapshot is crash-consistent. If they are split across separate EBS volumes without AWS &lt;code&gt;CreateSnapshots&lt;/code&gt; (which supports up to 128 volumes on a &lt;strong&gt;single&lt;/strong&gt; EC2 instance) or &lt;code&gt;db.fsyncLock()&lt;/code&gt;, microsecond skew between independent &lt;code&gt;CreateSnapshot&lt;/code&gt; calls can corrupt WiredTiger recovery on boot.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;S3 lazy-loading on EBS restore:&lt;/strong&gt; Newly created EBS volumes from snapshots load blocks lazily from Amazon S3 on first read. Without AWS Fast Snapshot Restore (FSR)—which is governed by per-AZ &lt;code&gt;FastSnapshotRestoreCreditsBucketSize&lt;/code&gt; volume-creation credits—or pre-warming via &lt;code&gt;fio&lt;/code&gt;, initial database reads hit elevated storage latency.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-01-backup-restore-ec2-2-constraints.svg&quot; alt=&quot;Cross-shard skew, WiredTiger volume rules, and EBS lazy loading constrain EC2 backups&quot;&gt;&lt;/p&gt;
&lt;p&gt;The core engineering question is: &lt;strong&gt;How do platform teams capture fast, point-in-time consistent backups across all shards and config servers on EC2 without locking shard primaries or stalling live application writes?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Four backup architectures are available for MongoDB on AWS. Each trades off operational complexity, primary-node impact, and cross-shard consistency:&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Backup Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Gets Right&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;mongodump&lt;/code&gt; + &lt;code&gt;mongorestore&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Portable BSON output; selective collection export&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Evicts WiredTiger cache; slow restore; no cross-shard PITR&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reject for multi-TB clusters&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Primary &lt;code&gt;fsyncLock()&lt;/code&gt; + EBS Snapshots&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforces exact write freeze before snapshot&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Blocks all app writes on primaries; AWS API throttling causes outage&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reject&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Hidden Secondary EBS Snapshots&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero primary write lock; fast block-level restore&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires 1 extra node per shard + CSRS; balancer pause during snap&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Recommended for self-managed EC2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;MongoDB Ops Manager / Atlas Backup&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Continuous oplog tailing with per-second PITR&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires Enterprise license or migration to MongoDB Atlas&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use if managed PITR is funded&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Issuing &lt;code&gt;db.fsyncLock()&lt;/code&gt; on shard primaries is a classic outage trigger: if the AWS EC2 snapshot API experiences transient throttling or network timeouts, the primary remains write-locked and client connection pools saturate. Moving the snapshot target to a dedicated hidden secondary (&lt;code&gt;priority: 0, hidden: true&lt;/code&gt;) on each shard and the CSRS isolates backup locks from live traffic completely.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-01-backup-restore-ec2-3-options.svg&quot; alt=&quot;Compare mongodump, primary fsyncLock snapshots, hidden secondary snapshots, and managed backup&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-coordinated-snapshot-architecture&quot;&gt;The Coordinated Snapshot Architecture&lt;/h2&gt;
&lt;p&gt;The production standard on self-managed EC2 combines a dedicated hidden backup tier with balancer quiescence and block-level EBS snapshots.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-01-backup-restore-ec2-4-architecture.svg&quot; alt=&quot;Coordinated backup architecture isolates hidden secondaries from production traffic&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-dedicated-hidden-secondary-tier-priority-0-hidden-true&quot;&gt;1. Dedicated Hidden Secondary Tier (&lt;code&gt;priority: 0, hidden: true&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Add one non-voting, hidden member to the CSRS and to each shard replica set:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;priority: 0&lt;/code&gt;:&lt;/strong&gt; Prevents the backup node from ever winning an election and becoming primary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;hidden: true&lt;/code&gt;:&lt;/strong&gt; Hides the node from client drivers (&lt;code&gt;hello&lt;/code&gt; / &lt;code&gt;isMaster&lt;/code&gt; responses), so it serves zero application read queries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Equal hardware sizing and &lt;code&gt;buildIndexes: true&lt;/code&gt;:&lt;/strong&gt; Provision the hidden node with the same CPU, RAM, and EBS &lt;code&gt;gp3&lt;/code&gt; IOPS as voting members, and keep &lt;code&gt;buildIndexes: true&lt;/code&gt; (the default). An undersized backup node accumulates replication lag, causing snapshots to miss RPO targets or fall off the oplog, while &lt;code&gt;buildIndexes: false&lt;/code&gt; would force hours of index rebuilds after restore.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-wiredtiger-crash-consistency-fsynclock-and-oplog-window-sizing&quot;&gt;2. WiredTiger Crash Consistency, &lt;code&gt;fsyncLock()&lt;/code&gt;, and Oplog Window Sizing&lt;/h3&gt;
&lt;p&gt;Between 60-second checkpoints (&lt;code&gt;WiredTiger.turtle&lt;/code&gt; / &lt;code&gt;WiredTiger.wt&lt;/code&gt;), WiredTiger appends mutations to &lt;code&gt;journal/WiredTigerLog.*&lt;/code&gt;. When data and journal share one EBS volume, an EBS snapshot captures a crash-consistent image that replays the journal cleanly on startup. If you use LVM striping across multiple EBS volumes on Linux, &lt;code&gt;ec2:CreateSnapshots&lt;/code&gt; (or &lt;code&gt;xfs_freeze -f&lt;/code&gt; / &lt;code&gt;lvcreate --snapshot&lt;/code&gt;) is required so the striped volume set freezes atomically.&lt;/p&gt;
&lt;p&gt;In a &lt;strong&gt;sharded cluster&lt;/strong&gt;, however, MongoDB’s documented filesystem-snapshot procedure requires stopping the balancer (&lt;code&gt;sh.stopBalancer()&lt;/code&gt;) and briefly locking (&lt;code&gt;db.fsyncLock()&lt;/code&gt;) one secondary on each shard and the CSRS if you need all shards frozen at a coordinated window without cross-shard transaction skew—or if data and journal live on separate volumes. Because the lock is applied strictly to the &lt;strong&gt;hidden secondaries&lt;/strong&gt;, primaries and voting secondaries continue serving application writes uninterrupted.&lt;/p&gt;
&lt;p&gt;Size the replica set oplog (&lt;code&gt;storage.oplogSizeMB&lt;/code&gt; or &lt;code&gt;oplogMinRetentionHours&lt;/code&gt;) with enough headroom to cover both the brief &lt;code&gt;fsyncLock()&lt;/code&gt; window and your snapshot retention cadence if you plan to restore a snapshot and rejoin it to an existing replica set. If a snapshot is older than the primary’s oldest oplog entry, a restored node cannot catch up via oplog tailing and must perform a full initial sync from scratch, delaying recovery.&lt;/p&gt;
&lt;h3 id=&quot;3-auditing-oplog-alignment-and-hidden-node-lag-mongosh&quot;&gt;3. Auditing Oplog Alignment and Hidden Node Lag (&lt;code&gt;mongosh&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Before taking snapshots, verify that hidden backup nodes are caught up to their primaries and inspect optime alignment across shards:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Run on each replica set to verify hidden secondary replication lag&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rs.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; primary&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; status.members.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.stateStr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;PRIMARY&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  status.members.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.hidden &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.priority &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;node&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; lag&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ((primary.optimeDate.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getTime&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; node.optimeDate.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getTime&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;node&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} | Lag: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;lag&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}s | Health: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;node&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;health&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;4-coordinated-snapshot-orchestrator-pymongo--boto3&quot;&gt;4. Coordinated Snapshot Orchestrator (&lt;code&gt;pymongo&lt;/code&gt; + &lt;code&gt;boto3&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;The orchestrator stops the balancer, discovers hidden secondaries across the CSRS and all shards, locks them with &lt;code&gt;fsyncLock&lt;/code&gt;, triggers &lt;code&gt;ec2:CreateSnapshots&lt;/code&gt; per backup instance, and always unlocks and restarts the balancer in a &lt;code&gt;finally&lt;/code&gt; block:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env python3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse, time, boto3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; run_backup&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(mongos_uri, cluster_tag, region, dry_run):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    client &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient(mongos_uri, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;serverSelectionTimeoutMS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ec2 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; boto3.client(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ec2&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;region_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;region)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    hidden_clients &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dry_run:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            client.admin.command(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balancerStop&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            while&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.admin.command(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balancerStatus&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;inBalancerRound&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                time.sleep(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; shard &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.config.shards.find():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            sc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient(shard[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;host&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;serverSelectionTimeoutMS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sc.admin.command(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;replSetGetStatus&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;members&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;hidden&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;or&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;priority&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                    hc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient(m[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;directConnection&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;serverSelectionTimeoutMS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dry_run:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                        hc.admin.command(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;fsync&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;lock&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                    hidden_clients.append(hc)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                    break&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        res &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ec2.describe_instances(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;Filters&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;tag:Cluster&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Values&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [cluster_tag]},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;tag:Role&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Values&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;backup-hidden-node&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        ])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; res[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Reservations&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; inst &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Instances&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dry_run:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                    ec2.create_snapshots(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;InstanceSpecification&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;InstanceId&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: inst[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;InstanceId&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;                    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[DRY-RUN] create_snapshots for &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;inst[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;InstanceId&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    finally&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; hc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; hidden_clients:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                hc.admin.command(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;fsyncUnlock&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            except&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; Exception&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; exc:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;                print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[ERROR] fsyncUnlock failed: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;exc&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dry_run:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            client.admin.command(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balancerStart&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; __name__&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse.ArgumentParser()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--mongos-uri&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;required&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--cluster-tag&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;required&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--aws-region&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;default&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;us-east-1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--dry-run&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;action&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;store_true&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    a &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; p.parse_args()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    run_backup(a.mongos_uri, a.cluster_tag, a.aws_region, a.dry_run)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Execute backups and periodic restore drills in four gated phases so locks never linger and restore procedures stay verified.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-01-backup-restore-ec2-5-flow.svg&quot; alt=&quot;Four gated phases from snapshot to verified restore&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;phase-1-quiesce-the-cluster-balancer&quot;&gt;Phase 1: Quiesce the Cluster Balancer&lt;/h3&gt;
&lt;p&gt;Connect to &lt;code&gt;mongos&lt;/code&gt; and run &lt;code&gt;sh.stopBalancer()&lt;/code&gt; (or &lt;code&gt;balancerStop&lt;/code&gt;).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; Poll &lt;code&gt;db.adminCommand({ balancerStatus: 1 })&lt;/code&gt; until &lt;code&gt;mode&lt;/code&gt; is &lt;code&gt;&quot;off&quot;&lt;/code&gt; and &lt;code&gt;inBalancerRound&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;. This guarantees no &lt;code&gt;moveChunk&lt;/code&gt; or &lt;code&gt;moveRange&lt;/code&gt; migration is mid-flight.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; If any step fails before snapshotting, call &lt;code&gt;sh.startBalancer()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-2-lock-hidden-secondaries-and-trigger-createsnapshots&quot;&gt;Phase 2: Lock Hidden Secondaries and Trigger &lt;code&gt;CreateSnapshots&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Verify hidden-node replication lag is within RPO, issue &lt;code&gt;db.fsyncLock()&lt;/code&gt; on the hidden secondary of the CSRS and each shard, then call &lt;code&gt;ec2:CreateSnapshots&lt;/code&gt; for each hidden-node instance (plus &lt;code&gt;CreateSnapshot&lt;/code&gt; if single-volume). Lock all hidden nodes &lt;em&gt;before&lt;/em&gt; issuing any snapshot calls rather than locking and unlocking shards one by one; otherwise, writes or distributed transactions applied to Shard 2 after Shard 1’s snapshot cut will be captured unevenly across shards.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; Confirm every snapshot ID returns in &lt;code&gt;pending&lt;/code&gt; state from the EC2 API. Remember &lt;code&gt;CreateSnapshots&lt;/code&gt; is asynchronous for S3 block upload; the point-in-time block mapping is captured as soon as the API returns.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; Execute &lt;code&gt;db.fsyncUnlock()&lt;/code&gt; inside a &lt;code&gt;finally&lt;/code&gt; block even if an EC2 API call raises an exception.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-3-unlock-hidden-nodes-and-resume-the-balancer&quot;&gt;Phase 3: Unlock Hidden Nodes and Resume the Balancer&lt;/h3&gt;
&lt;p&gt;Immediately run &lt;code&gt;db.fsyncUnlock()&lt;/code&gt; on all locked hidden secondaries and call &lt;code&gt;sh.startBalancer()&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; Confirm &lt;code&gt;db.currentOp()&lt;/code&gt; on each hidden node no longer shows &lt;code&gt;fsyncLock&lt;/code&gt;, replication lag drains back to near zero, and &lt;code&gt;sh.getBalancerState()&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; If a node fails to unlock via &lt;code&gt;mongosh&lt;/code&gt;, connect directly to that &lt;code&gt;mongod&lt;/code&gt; on localhost and issue &lt;code&gt;db.adminCommand({ fsyncUnlock: 1 })&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-4-isolated-vpc-restore-drill&quot;&gt;Phase 4: Isolated VPC Restore Drill&lt;/h3&gt;
&lt;p&gt;Attach snapshots to test EC2 instances in an isolated VPC. If Fast Snapshot Restore (FSR) is enabled, verify &lt;code&gt;FastSnapshotRestoreCreditsBucketSize &gt;= 1&lt;/code&gt; per AZ; otherwise pre-warm volumes with &lt;code&gt;fio&lt;/code&gt; before starting &lt;code&gt;mongod&lt;/code&gt; so lazy S3 block fetches do not stall initial database reads.&lt;/p&gt;
&lt;p&gt;Restoring a sharded cluster into a new VPC requires rewriting cluster topology metadata because the restored snapshots still contain production hostnames and IPs:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Start each restored CSRS and shard &lt;code&gt;mongod&lt;/code&gt; in &lt;strong&gt;standalone mode&lt;/strong&gt; (without &lt;code&gt;--replSet&lt;/code&gt;, &lt;code&gt;--configsvr&lt;/code&gt;, or &lt;code&gt;--shardsvr&lt;/code&gt;) on a temporary port.&lt;/li&gt;
&lt;li&gt;Update the local &lt;code&gt;local.system.replset&lt;/code&gt; document (or restart with &lt;code&gt;--replSet&lt;/code&gt; and run &lt;code&gt;rs.reconfig(..., { force: true })&lt;/code&gt;) so each replica set recognizes its new VPC hostnames.&lt;/li&gt;
&lt;li&gt;On the restored CSRS primary, update &lt;code&gt;config.shards&lt;/code&gt; so each shard’s &lt;code&gt;host&lt;/code&gt; string points to the new shard replica set seed list. On each restored shard primary, also update &lt;code&gt;configsvrConnectionString&lt;/code&gt; inside &lt;code&gt;admin.system.version&lt;/code&gt; (&lt;code&gt;_id: &quot;shardIdentity&quot;&lt;/code&gt;) to match the restored CSRS seed list (using &lt;code&gt;recoverShardingState: false&lt;/code&gt; during standalone repair if needed) so shards do not attempt to contact production config servers, then restart &lt;code&gt;mongos&lt;/code&gt; routers pointing to the restored CSRS.&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; &lt;code&gt;mongod&lt;/code&gt; replays &lt;code&gt;WiredTigerLog.*&lt;/code&gt; without &lt;code&gt;WT_NOTFOUND&lt;/code&gt; errors, &lt;code&gt;sh.status()&lt;/code&gt; reports all shards healthy, and application checksums pass.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; Terminate the isolated drill instances; production is untouched.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Documented WiredTiger and AWS EBS mechanics explain why this architecture holds up under production load:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Why &lt;code&gt;db.fsyncLock()&lt;/code&gt; on hidden secondaries is safe while primary locking is not:&lt;/strong&gt; On a primary node, &lt;code&gt;db.fsyncLock()&lt;/code&gt; blocks all client writes; any AWS API retry or timeout immediately stalls application connection pools. On a &lt;code&gt;priority: 0, hidden: true&lt;/code&gt; secondary, &lt;code&gt;db.fsyncLock()&lt;/code&gt; only pauses local oplog application for the few seconds needed to issue &lt;code&gt;ec2:CreateSnapshots&lt;/code&gt;. Once unlocked, the secondary catches up from the primary’s oplog—provided the cluster’s oplog retention window comfortably exceeds the backup duration.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why &lt;code&gt;CreateSnapshots&lt;/code&gt; does not replace cross-shard orchestration:&lt;/strong&gt; AWS &lt;code&gt;ec2:CreateSnapshots&lt;/code&gt; guarantees crash-consistency across up to 128 EBS volumes attached to a &lt;strong&gt;single&lt;/strong&gt; EC2 instance, solving the split data/journal volume problem on that host. It does &lt;strong&gt;not&lt;/strong&gt; synchronize snapshots across separate EC2 instances (multiple shards and the CSRS), which is why stopping the balancer and locking hidden nodes remains mandatory for cross-shard consistency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why Fast Snapshot Restore credits matter for RTO:&lt;/strong&gt; Even though EBS snapshots restore volumes instantly in the AWS control plane, blocks are fetched lazily from S3 on first access. If an FSR-enabled snapshot has fewer than &lt;code&gt;1&lt;/code&gt; volume-creation credit remaining in its AZ bucket, AWS falls back to standard lazy loading, degrading initial query latency until blocks hydrate. When FSR is not enabled on a snapshot, running a sequential &lt;code&gt;fio&lt;/code&gt; read pass across the restored block device before starting &lt;code&gt;mongod&lt;/code&gt; forces S3 hydration ahead of live traffic, preventing cold-block latency spikes from hitting application queries.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Issuing &lt;code&gt;fsyncLock()&lt;/code&gt; on shard primaries&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Blocks all client writes; EC2 API latency turns into a site-wide outage&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;App write timeouts; &lt;code&gt;currentOp&lt;/code&gt; shows &lt;code&gt;fsyncLock&lt;/code&gt; on primary&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Target only &lt;code&gt;priority: 0, hidden: true&lt;/code&gt; secondaries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Snapshotting while balancer is active&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;moveChunk&lt;/code&gt; moves ranges mid-backup, duplicating or losing documents on restore&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;inBalancerRound: true&lt;/code&gt; during snapshot window&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Call &lt;code&gt;sh.stopBalancer()&lt;/code&gt; and wait for &lt;code&gt;inBalancerRound: false&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Splitting data and journal without &lt;code&gt;CreateSnapshots&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Separate &lt;code&gt;CreateSnapshot&lt;/code&gt; calls skew by milliseconds, causing &lt;code&gt;WT_NOTFOUND&lt;/code&gt; on boot&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;WiredTiger panic / journal recovery failure on restore&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Co-locate data + journal on one EBS volume or use &lt;code&gt;ec2:CreateSnapshots&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Omitting CSRS from the snapshot batch&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Restored &lt;code&gt;config.chunks&lt;/code&gt; metadata mismatches shard data ownership&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;StaleConfigException&lt;/code&gt; or missing data after restore&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Always lock and snapshot the CSRS hidden node in the same batch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Hidden secondary undersized on CPU/IOPS&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Node falls hours behind primary, violating RPO or dropping off the oplog&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;rs.status()&lt;/code&gt; lag alerts on hidden member&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Match hidden secondary instance type and EBS &lt;code&gt;gp3&lt;/code&gt; IOPS to voting nodes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Restoring into a new VPC without &lt;code&gt;rs.reconfig()&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;mongod&lt;/code&gt; boots with old private IPs in &lt;code&gt;rs.conf()&lt;/code&gt;, failing to form quorum&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Replica set stuck in &lt;code&gt;STARTUP2&lt;/code&gt; / &lt;code&gt;RECOVERING&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Boot standalone, rewrite &lt;code&gt;rs.conf()&lt;/code&gt; and &lt;code&gt;config.shards&lt;/code&gt;, then restart&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Exhausted FSR credits on restore&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Volume falls back to lazy S3 block loading, spiking read latency&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;CloudWatch &lt;code&gt;FastSnapshotRestoreCreditsBucketSize &amp;#x3C; 1&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Monitor FSR credits or pre-warm restored EBS volumes with &lt;code&gt;fio&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-01-backup-restore-ec2-6-failures.svg&quot; alt=&quot;Four critical failure modes in sharded EC2 backup and restore&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;april-2026-update&quot;&gt;April 2026 Update&lt;/h2&gt;
&lt;p&gt;Modern MongoDB releases (7.0 and 8.0) and AWS EBS capabilities reinforce the separation between logical exports and block-level recovery:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Logical dumps remain unsuitable for large sharded DR:&lt;/strong&gt; MongoDB documentation continues to advise against &lt;code&gt;mongodump&lt;/code&gt; for large sharded clusters because it cannot coordinate a cluster-wide point-in-time snapshot across shards and saturates WiredTiger cache with full collection scans.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Per-instance multi-volume crash consistency via &lt;code&gt;ec2:CreateSnapshots&lt;/code&gt;:&lt;/strong&gt; AWS &lt;code&gt;CreateSnapshots&lt;/code&gt; handles up to 128 attached EBS volumes on a single EC2 instance atomically, eliminating manual &lt;code&gt;dmsetup&lt;/code&gt; / LVM freeze scripts when data and journal reside on separate volumes on the same host—though cross-instance orchestration across shards and CSRS is still required.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Managed continuous PITR in Atlas:&lt;/strong&gt; Where self-managed EC2 snapshot orchestration and hidden-node overhead are undesirable, MongoDB Atlas Continuous Cloud Backups automate cross-shard snapshots and oplog tailing for point-in-time recovery.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Do not rely on &lt;code&gt;mongodump&lt;/code&gt; for multi-terabyte sharded DR:&lt;/strong&gt; It evicts WiredTiger working sets and cannot synchronize a point-in-time cut across shards.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never run &lt;code&gt;db.fsyncLock()&lt;/code&gt; on shard primaries:&lt;/strong&gt; Lock only dedicated &lt;code&gt;priority: 0, hidden: true&lt;/code&gt; secondaries so application writes never block on AWS API calls.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Co-locate data and journal on one EBS volume (or use &lt;code&gt;ec2:CreateSnapshots&lt;/code&gt;):&lt;/strong&gt; Prevent microsecond snapshot skew between &lt;code&gt;WiredTiger.turtle&lt;/code&gt; checkpoints and &lt;code&gt;WiredTigerLog.*&lt;/code&gt; journal files.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stop the balancer and include the CSRS in every snapshot batch:&lt;/strong&gt; Restored shards are unusable without matching &lt;code&gt;config.chunks&lt;/code&gt; metadata.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Automate restore drills with &lt;code&gt;rs.reconfig()&lt;/code&gt; and FSR/pre-warming:&lt;/strong&gt; An untested snapshot is not a verified RTO.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Verify whether your sharded EC2 backups rely on &lt;code&gt;mongodump&lt;/code&gt; or &lt;code&gt;fsyncLock()&lt;/code&gt; against primary nodes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Provision one &lt;code&gt;priority: 0, hidden: true&lt;/code&gt; secondary on the CSRS and each shard, and orchestrate &lt;code&gt;balancerStop&lt;/code&gt; -&gt; hidden &lt;code&gt;fsyncLock&lt;/code&gt; -&gt; &lt;code&gt;ec2:CreateSnapshots&lt;/code&gt; -&gt; &lt;code&gt;fsyncUnlock&lt;/code&gt; -&gt; &lt;code&gt;balancerStart&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run an isolated VPC restore drill from the latest snapshot batch, verify clean WiredTiger journal recovery, and confirm &lt;code&gt;sh.status()&lt;/code&gt; health.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your backup cron/Lambda scripts this week to confirm CSRS inclusion, single-volume data+journal layout, and &lt;code&gt;finally&lt;/code&gt;-block unlock safety.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>Scaling a MongoDB Sharded Cluster on EC2 Without Letting the Balancer Crush Production</title><link>https://rajivonai.com/blog/2026-06-07-sharding-scaling/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-07-sharding-scaling/</guid><description>Adding a new shard is not an instant capacity upgrade. How to orchestrate MongoDB balancer active windows, throttle chunk migration I/O, and decommission shards safely.</description><pubDate>Wed, 02 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Adding a new shard to an overloaded MongoDB cluster feels like opening a pressure relief valve; in reality, unthrottled chunk migrations push source shard disk IOPS and WiredTiger cache eviction into immediate production collapse.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Executing &lt;code&gt;sh.addShard()&lt;/code&gt; on a busy cluster triggers immediate background migrations of 128 MB chunks (6.0+ default), evicting hot working sets from donor WiredTiger caches and saturating EBS gp3 read bandwidth.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Pause the balancer (&lt;code&gt;sh.setBalancerState(false)&lt;/code&gt;) before adding shards, enforce an off-peak &lt;code&gt;activeWindow&lt;/code&gt; in &lt;code&gt;config.settings&lt;/code&gt;, and throttle background range deletion (&lt;code&gt;rangeDeleterBatchSize&lt;/code&gt; / &lt;code&gt;rangeDeleterBatchDelayMS&lt;/code&gt;) if disk &lt;code&gt;await&lt;/code&gt; climbs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Never stop the balancer during &lt;code&gt;removeShard&lt;/code&gt; draining, never terminate EC2 instances before &lt;code&gt;removeShard&lt;/code&gt; returns &lt;code&gt;state: &quot;completed&quot;&lt;/code&gt;, and always run &lt;code&gt;movePrimary&lt;/code&gt; for any &lt;code&gt;dbsToMove&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-07-sharding-scaling.svg&quot; alt=&quot;Gate sh.addShard behind an off-peak activeWindow&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Operating horizontally scaled MongoDB clusters on AWS EC2 allows platform teams to scale storage and write throughput beyond the physical limits of a single replica set. In multi-tenant SaaS and telemetry deployments running on memory-optimized instances backed by EBS gp3 volumes, sustained dataset growth gradually pushes existing shards toward CPU saturation, rising WiredTiger cache eviction pressure, and elevated disk write queues.&lt;/p&gt;
&lt;p&gt;To relieve pressure on the existing fleet, engineers provision new replica sets and execute &lt;code&gt;sh.addShard()&lt;/code&gt; to expand the cluster, expecting immediate linear relief. In distributed storage systems, however, registering an empty shard does not reduce load right away. Instead, it creates a sharp topological imbalance that triggers aggressive background data movement across the existing overloaded shards.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Executing &lt;code&gt;sh.addShard()&lt;/code&gt; during peak application traffic introduces four compounding operational constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Immediate Imbalance Detection and $\lfloor n/2 \rfloor$ Concurrency:&lt;/strong&gt; A newly registered shard holds zero chunks. The balancer—running on the Config Server replica set primary—detects the imbalance immediately and schedules concurrent &lt;code&gt;moveChunk&lt;/code&gt; operations. Because each shard can participate in at most one migration at a time (inbound or outbound), an $n$-shard cluster can run up to $\lfloor n/2 \rfloor$ simultaneous chunk transfers across donor and recipient pairs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cold 128 MB Chunk Scans and Cache Eviction:&lt;/strong&gt; Starting in MongoDB 6.0, the default chunk size doubled from 64 MB to &lt;strong&gt;128 MB&lt;/strong&gt; to reduce metadata overhead. To migrate each 128 MB chunk, the donor shard primary reads all documents in that key range from disk. When migrating older, colder chunks, WiredTiger’s LRU eviction algorithm treats those newly read pages as active, flushing hot transactional working-set pages out of RAM.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;EBS gp3 Bandwidth Contention:&lt;/strong&gt; Streaming gigabytes of cold chunks from donor primaries consumes sequential read bandwidth against EBS gp3 limits (125 MB/s baseline unless provisioned higher). Application queries that previously hit in-memory cache now incur synchronous disk reads, driving Linux &lt;code&gt;iostat&lt;/code&gt; &lt;code&gt;await&lt;/code&gt; latency sharply upward.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Application Timeouts and Cascading Retries:&lt;/strong&gt; As disk queues back up, client queries exceed socket timeouts, triggering &lt;code&gt;CursorNotFound&lt;/code&gt; errors and application retry storms that further compound load on the very shards being scaled.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-07-sharding-scaling-2-constraints.svg&quot; alt=&quot;Balancer concurrency, 128 MB chunks, and EBS bandwidth bound scaling speed&quot;&gt;&lt;/p&gt;
&lt;p&gt;The core architectural question is: &lt;strong&gt;How can platform architects expand or contract a production sharded cluster on AWS EC2, govern balancer migration windows and range-deletion I/O, and prevent chunk movement from cannibalizing live application cache and EBS bandwidth?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;When an existing sharded cluster approaches storage or CPU saturation, teams typically evaluate three scaling and balancing strategies:&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Peak-Hour I/O Impact&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Rebalance Predictability&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Operational Constraint&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;24/7 Unthrottled Balancer&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High (cold scans during peak)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fastest raw completion&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Evicts hot WiredTiger cache during business hours&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt; for latency-sensitive production&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Vertical EC2 Resize First&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;None (rolling secondary reboot)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero chunk movement&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Hits single-node instance/EBS ceilings&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Conditional&lt;/strong&gt;; use as a short-term bridge only&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;activeWindow&lt;/code&gt; + Range Throttle&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero during daytime traffic&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Multi-night predictable&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires &lt;code&gt;config.settings&lt;/code&gt; window and I/O tuning&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen&lt;/strong&gt; standard for EC2 shard scaling&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;24/7 Unthrottled Balancing:&lt;/strong&gt; Maximizes migration speed by running $\lfloor n/2 \rfloor$ transfers continuously, but puts donor WiredTiger caches and EBS read queues in direct competition with peak user traffic.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Vertical EC2 Resize First:&lt;/strong&gt; Upgrading existing shard instances (for example, to larger memory-optimized EC2 types or higher provisioned gp3 IOPS) via rolling secondary restarts avoids chunk movement altogether, buying headroom when a cluster is already too saturated to survive even off-peak migrations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Off-Peak &lt;code&gt;activeWindow&lt;/code&gt; + Range-Deleter Throttling:&lt;/strong&gt; Restricts chunk migrations to verified low-traffic hours (such as &lt;code&gt;02:00&lt;/code&gt; to &lt;code&gt;06:00&lt;/code&gt; UTC) and tunes &lt;code&gt;rangeDeleterBatchSize&lt;/code&gt; / &lt;code&gt;rangeDeleterBatchDelayMS&lt;/code&gt; so post-migration cleanup does not spike replication lag or dirty cache pages. Combined with Zone Sharding (&lt;code&gt;sh.updateZoneKeyRange&lt;/code&gt;), this also keeps regional chunks pinned to shards in the same AWS Availability Zone to avoid unnecessary cross-AZ transfer charges.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-07-sharding-scaling-3-options.svg&quot; alt=&quot;Use activeWindow scheduling and range-deleter throttling over 24/7 balancing&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-shard-scaling-architecture-the-balancer-lifecycle-and-active-windows&quot;&gt;The Shard Scaling Architecture: The Balancer Lifecycle and Active Windows&lt;/h2&gt;
&lt;p&gt;Understanding the four physical stages of a &lt;code&gt;moveChunk&lt;/code&gt; operation explains why unbounded balancing degrades concurrent application throughput.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-07-sharding-scaling-4-architecture.svg&quot; alt=&quot;Balancer schedules chunk moves while asynchronous range deleter cleans orphans&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Admin[&quot;Platform Operator — sh.addShard()&quot;] --&gt; ConfigServer[&quot;Config Server Balancer&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ConfigServer --&gt; Window{&quot;Within activeWindow?&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Window --&gt;|No| Pause[&quot;Suppress Migrations — Zero Day Impact&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Window --&gt;|Yes| Donor[&quot;Donor Shard — Scan 128 MB Chunk&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Donor --&gt;|Stream Docs &amp;#x26; Oplog| Recipient[&quot;Recipient Shard — Ingest &amp;#x26; Index&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Recipient --&gt; Commit[&quot;Update Config Catalog&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Commit --&gt; RangeDel[&quot;Donor Async Range Deleter&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;mechanics-of-the-four-chunk-migration-phases&quot;&gt;Mechanics of the Four Chunk Migration Phases&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Donor Scan and Network Streaming:&lt;/strong&gt; The donor shard queries the chunk range &lt;code&gt;[min, max]&lt;/code&gt;, reads matching BSON documents from WiredTiger/EBS, and streams them across the VPC to the recipient shard. Cold chunks pulled from disk displace warm transactional pages in the donor’s cache.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Recipient Ingestion and Index Build:&lt;/strong&gt; The recipient shard inserts incoming documents into its local WiredTiger table and updates all secondary indexes concurrently, generating dirty cache bytes and consuming write tickets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Migration Oplog Catch-Up:&lt;/strong&gt; While cloning runs, client writes to that chunk continue on the donor shard. The recipient tails and applies those mutations from the donor’s oplog until lag falls below a tight threshold.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Critical Section, Commit, and Async Range Deletion:&lt;/strong&gt; The donor acquires a brief exclusive lock on the chunk range, transfers the final oplog delta, and commits the updated chunk ownership to the Config Server catalog. Once &lt;code&gt;mongos&lt;/code&gt; routes traffic to the recipient, the donor purges the migrated documents asynchronously via its background &lt;strong&gt;range deleter&lt;/strong&gt; (or via &lt;code&gt;cleanupOrphaned&lt;/code&gt; for manual orphan sweeps). Because &lt;code&gt;rangeDeleterBatchSize&lt;/code&gt; defaults to &lt;code&gt;INT_MAX&lt;/code&gt; (&lt;code&gt;2147483647&lt;/code&gt;) in modern MongoDB, unthrottled range deletion can generate heavy disk I/O and secondary replication lag unless bounded with &lt;code&gt;rangeDeleterBatchSize&lt;/code&gt; and &lt;code&gt;rangeDeleterBatchDelayMS&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;capacity-and-cross-az-transfer-math&quot;&gt;Capacity and Cross-AZ Transfer Math&lt;/h3&gt;
&lt;p&gt;When adding $M$ new shards to a cluster of $N$ existing shards holding $D_{\text{total}}$ gigabytes of data, the balancer migrates roughly:&lt;/p&gt;
&lt;p&gt;$$\text{Data to Migrate} = D_{\text{total}} \times \left( \frac{M}{N + M} \right)$$&lt;/p&gt;
&lt;p&gt;For a 10-shard cluster holding 20 TB ($20,000\text{ GB}$) expanded by 2 new shards:&lt;/p&gt;
&lt;p&gt;$$\text{Data to Migrate} = 20,000\text{ GB} \times \left( \frac{2}{12} \right) \approx 3,333\text{ GB}$$&lt;/p&gt;
&lt;p&gt;At an aggregate off-peak transfer rate of $100\text{ MB/s}$:&lt;/p&gt;
&lt;p&gt;$$T_{\text{rebalance}} = \frac{3,333,000\text{ MB}}{100\text{ MB/s}} = 33,330\text{ seconds} \approx 9.25\text{ hours}$$&lt;/p&gt;
&lt;p&gt;With a 4-hour nightly &lt;code&gt;activeWindow&lt;/code&gt; (&lt;code&gt;02:00&lt;/code&gt;–&lt;code&gt;06:00&lt;/code&gt; UTC), the cluster converges across &lt;strong&gt;2.3 nights&lt;/strong&gt; without touching daytime traffic. In multi-AZ AWS deployments, also account for cross-AZ data transfer charges ($C_{\text{transfer}} = \text{Data Migrated (GB)} \times \text{Rate}_{\text{cross-AZ}}$) when sizing shards or designing Zone Sharding boundaries. Using &lt;code&gt;sh.addShardToZone()&lt;/code&gt; and &lt;code&gt;sh.updateZoneKeyRange()&lt;/code&gt;, operators can pin regional or tenant key ranges to shards residing in the same Availability Zone, preventing the balancer from shuffling chunks across AZ boundaries unnecessarily.&lt;/p&gt;
&lt;p&gt;Also account for &lt;strong&gt;jumbo chunks&lt;/strong&gt;: if a shard key has low cardinality and a single key value grows past the 128 MB chunk limit without a split point, MongoDB marks the chunk as &lt;code&gt;jumbo&lt;/code&gt; and the balancer refuses to migrate it. Resolving jumbo chunks requires either appending a higher-cardinality suffix via &lt;code&gt;refineCollectionShardKey&lt;/code&gt; or changing the key via &lt;code&gt;sh.reshardCollection&lt;/code&gt; before new shards can absorb that data.&lt;/p&gt;
&lt;h2 id=&quot;diagnostic-telemetry-high-resolution-balancer-forensics&quot;&gt;Diagnostic Telemetry: High-Resolution Balancer Forensics&lt;/h2&gt;
&lt;p&gt;Inspect collection balancing compliance and underlying EBS disk queues before and during every scaling window.&lt;/p&gt;
&lt;h3 id=&quot;1-collection-compliance-via-balancercollectionstatus&quot;&gt;1. Collection Compliance via &lt;code&gt;balancerCollectionStatus&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Introduced in MongoDB 4.4 and enhanced in 6.0+, &lt;code&gt;balancerCollectionStatus&lt;/code&gt; reports whether a collection is balanced (&lt;code&gt;balancerCompliant&lt;/code&gt;) and, if not, the exact violation (&lt;code&gt;firstComplianceViolation&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Execute on mongos to inspect balancing compliance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; adminDB&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getSiblingDB&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;admin&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; colls&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;telemetry.events&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;commerce.orders&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  colls.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;ns&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; res&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; adminDB.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;runCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ balancerCollectionStatus: ns });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;      print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Namespace: &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ns &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot; | Chunk Size: &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; res.chunkSize &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot; MB&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;      print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;  Balancer Compliant : &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; res.balancerCompliant);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;res.balancerCompliant) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;  First Violation    : &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; res.firstComplianceViolation);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (res.details) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;          printjson&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(res.details);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    } &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;catch&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (e) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;      print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Error checking &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ns &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;: &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; e);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-monitoring-donor-disk-latency-iostat&quot;&gt;2. Monitoring Donor Disk Latency (&lt;code&gt;iostat&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Track EBS read &lt;code&gt;await&lt;/code&gt; and device utilization on donor shard primaries during active balancing windows:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Monitor disk await latency and utilization on donor shard primaries&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;iostat&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -xz&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; awk&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;{print $1, $8, $9, $10, $14}&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If read &lt;code&gt;await&lt;/code&gt; climbs past 15 ms or &lt;code&gt;%util&lt;/code&gt; approaches saturation during an active window, narrow &lt;code&gt;activeWindow&lt;/code&gt;, stop the balancer (&lt;code&gt;sh.setBalancerState(false)&lt;/code&gt;), or throttle &lt;code&gt;rangeDeleterBatchSize&lt;/code&gt; and &lt;code&gt;rangeDeleterBatchDelayMS&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Execute cluster expansion and shard decommissioning as deterministic four-step runbooks with explicit gates.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-07-sharding-scaling-5-flow.svg&quot; alt=&quot;Four-step shard addition and safe decommissioning rollout&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;adding-shards-safely&quot;&gt;Adding Shards Safely&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Step 1 — Pause the Balancer Pre-Emptively:&lt;/strong&gt; Halt balancing before registering the new shard so &lt;code&gt;sh.addShard()&lt;/code&gt; does not trigger an immediate migration storm, and confirm no in-flight migrations remain active via &lt;code&gt;sh.isBalancerRunning()&lt;/code&gt; or &lt;code&gt;db.adminCommand({ balancerStatus: 1 })&lt;/code&gt;:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sh.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;setBalancerState&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 2 — Configure the Off-Peak &lt;code&gt;activeWindow&lt;/code&gt;:&lt;/strong&gt; Persist the allowed balancing window in &lt;code&gt;config.settings&lt;/code&gt; (UTC). Because &lt;code&gt;config.settings&lt;/code&gt; is stored on the Config Server replica set, the window applies cluster-wide across all shards and persists across router restarts:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getSiblingDB&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;config&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).settings.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;updateOne&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balancer&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { $set: { activeWindow: { start: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;02:00&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, stop: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;06:00&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } } },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { upsert: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 3 — Register the New Shard and Re-Enable Balancer:&lt;/strong&gt; Ensure the new replica set uses identical EC2 instance classes, WiredTiger cache sizing, and EBS gp3 IOPS/throughput tiers before adding it:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sh.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;addShard&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;shard11/ip-10-0-1-10:27017,ip-10-0-2-10:27017,ip-10-0-3-10:27017&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sh.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;setBalancerState&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;strong&gt;Rollback Gate:&lt;/strong&gt; If donor &lt;code&gt;iostat&lt;/code&gt; &lt;code&gt;await&lt;/code&gt; or p99 latency spikes during the window, run &lt;code&gt;sh.setBalancerState(false)&lt;/code&gt; immediately; in-flight migrations abort safely at the next chunk boundary or commit cleanly without data loss. If an abrupt crash leaves orphaned documents on a donor shard, run &lt;code&gt;db.adminCommand({ cleanupOrphaned: &quot;telemetry.events&quot; })&lt;/code&gt; directly on that shard’s primary during a quiet window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 4 — Verify Convergence:&lt;/strong&gt; Poll &lt;code&gt;balancerCollectionStatus&lt;/code&gt; until &lt;code&gt;balancerCompliant: true&lt;/code&gt; across all sharded collections.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;decommissioning-shards-safely-via-removeshard&quot;&gt;Decommissioning Shards Safely via &lt;code&gt;removeShard&lt;/code&gt;&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Start Draining:&lt;/strong&gt; Run &lt;code&gt;db.adminCommand({ removeShard: &quot;shard08&quot; })&lt;/code&gt; with the balancer &lt;strong&gt;enabled&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Keep Balancer Running:&lt;/strong&gt; Never disable the balancer while a shard is in &lt;code&gt;ongoing&lt;/code&gt; / draining state, or chunk evacuation stalls indefinitely.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Relocate Primary Databases (&lt;code&gt;dbsToMove&lt;/code&gt;):&lt;/strong&gt; Any unsharded collections on a database whose primary shard is &lt;code&gt;shard08&lt;/code&gt; will appear in &lt;code&gt;dbsToMove&lt;/code&gt;. Move each one explicitly: &lt;code&gt;db.adminCommand({ movePrimary: &quot;reporting&quot;, to: &quot;shard01&quot; })&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Terminate Only After &lt;code&gt;state: &quot;completed&quot;&lt;/code&gt;:&lt;/strong&gt; Re-run &lt;code&gt;removeShard&lt;/code&gt; until it returns &lt;code&gt;{ state: &quot;completed&quot; }&lt;/code&gt;, then decommission the EC2 instances.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;automated-cluster-scaler-cli&quot;&gt;Automated Cluster Scaler CLI&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env python3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;MongoDB Safe Cluster Expansion &amp;#x26; Balancer Orchestrator&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Usage:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    python3 mongo_cluster_scaler.py --mongos-uri &quot;mongodb://admin:pass@mongos1:27017&quot; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      --new-shard-replica &quot;shard11/ip-10-0-1-10:27017,ip-10-0-2-10:27017,ip-10-0-3-10:27017&quot; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      --window-start &quot;02:00&quot; --window-stop &quot;06:00&quot; --dry-run&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sys&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo.errors &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PyMongoError&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; orchestrate_shard_addition&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(client, new_shard_string, window_start, window_stop, dry_run):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    admin_db &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.admin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    config_db &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.config&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    shards &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(config_db.shards.find())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Current Cluster Shards : &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(shards)&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    balancer_status &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; admin_db.command(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balancerStatus&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Balancer Mode          : &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;balancer_status.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;mode&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;UNKNOWN&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dry_run:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            admin_db.command(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balancerStop&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            config_db.settings.update_one(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balancer&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$set&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;activeWindow&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;start&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: window_start, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;stop&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: window_stop}}},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                upsert&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            res &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; admin_db.command(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;addShard&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, new_shard_string)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;            print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[SUCCESS] Shard registered: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;res.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;shardAdded&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            admin_db.command(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balancerStart&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;            print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[SUCCESS] Balancer started with activeWindow &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;window_start&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;window_stop&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; UTC.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        except&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PyMongoError &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; e:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;            print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[FATAL] Cluster scaling failed: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            sys.exit(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[DRY RUN] Would stop balancer, set window &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;window_start&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;window_stop&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;, addShard, and start balancer.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse.ArgumentParser(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;description&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;MongoDB Cluster Scaler&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--mongos-uri&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;required&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--new-shard-replica&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;required&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--window-start&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;default&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;02:00&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--window-stop&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;default&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;06:00&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--dry-run&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;action&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;store_true&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    args &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; parser.parse_args()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    client &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient(args.mongos_uri, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;serverSelectionTimeoutMS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    orchestrate_shard_addition(client, args.new_shard_replica, args.window_start, args.window_stop, args.dry_run)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; __name__&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    main()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The operational behaviors above derive directly from MongoDB’s balancer, range deleter, and WiredTiger cache mechanics (CARL B/C — derived from documented engine behavior, not personal benchmark claims):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Why &lt;code&gt;sh.setBalancerState(false)&lt;/code&gt; Restores Latency Quickly:&lt;/strong&gt; When migrations saturate donor EBS read queues and evict hot WiredTiger pages, stopping the balancer halts new &lt;code&gt;moveChunk&lt;/code&gt; scans immediately, allowing active client queries to re-warm the working set in cache without background sequential scan interference.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why &lt;code&gt;removeShard&lt;/code&gt; Stalls on &lt;code&gt;dbsToMove&lt;/code&gt;:&lt;/strong&gt; Sharded chunk evacuation is handled automatically by the balancer, but unsharded collections belong to each database’s designated primary shard. Until &lt;code&gt;movePrimary&lt;/code&gt; reassigns those databases, &lt;code&gt;removeShard&lt;/code&gt; remains in &lt;code&gt;state: &quot;ongoing&quot;&lt;/code&gt; to prevent silent data loss.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why Homogeneous Shard Hardware Matters:&lt;/strong&gt; Because the balancer distributes data evenly across shards (by data size in 7.0+ and chunk count in earlier versions), mixing smaller-IOPS shards with larger ones forces the weakest shard to throttle overall cluster write throughput.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-07-sharding-scaling-6-failures.svg&quot; alt=&quot;Three failure modes during cluster scaling and shard removal&quot;&gt;&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Adding Shards During Peak Hours&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Immediate chunk scans evict hot cache and saturate EBS gp3 IOPS&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;iostat&lt;/code&gt; &lt;code&gt;await&lt;/code&gt; &gt; 15 ms and p99 read latency spikes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run &lt;code&gt;sh.setBalancerState(false)&lt;/code&gt; first; enforce &lt;code&gt;activeWindow&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Stopping Balancer During &lt;code&gt;removeShard&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Draining shard cannot evacuate remaining chunks&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;removeShard&lt;/code&gt; stays in &lt;code&gt;state: &quot;ongoing&quot;&lt;/code&gt; indefinitely&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep balancer enabled during drain; widen &lt;code&gt;activeWindow&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Terminating EC2 Before &lt;code&gt;completed&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Remaining chunks on the host are permanently lost&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Missing shard data and broken catalog routing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Wait strictly for &lt;code&gt;removeShard&lt;/code&gt; to return &lt;code&gt;state: &quot;completed&quot;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Skipped &lt;code&gt;movePrimary&lt;/code&gt; on &lt;code&gt;dbsToMove&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unsharded collections remain stranded on draining shard&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;removeShard&lt;/code&gt; output lists &lt;code&gt;dbsToMove&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run &lt;code&gt;db.adminCommand({ movePrimary: db, to: target })&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unthrottled Range Deleter&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Default &lt;code&gt;INT_MAX&lt;/code&gt; &lt;code&gt;rangeDeleterBatchSize&lt;/code&gt; spikes dirty cache and lag&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Secondary replication lag climbs after chunk commits&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set &lt;code&gt;rangeDeleterBatchSize&lt;/code&gt; (e.g., 128) and &lt;code&gt;rangeDeleterBatchDelayMS&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Jumbo Chunks Blocking Balance&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Oversized indivisible chunks cannot migrate to new shards&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;balancerCollectionStatus&lt;/code&gt; shows imbalance with no moves&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Split via &lt;code&gt;refineCollectionShardKey&lt;/code&gt; or &lt;code&gt;sh.reshardCollection&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;april-2026-update&quot;&gt;April 2026 Update&lt;/h2&gt;
&lt;p&gt;Modern MongoDB releases (6.0, 7.0, and 8.0) modernize chunk sizing, balancing criteria, and defragmentation:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;128 MB Default Chunks &amp;#x26; Data-Size Balancing (MongoDB 6.0 / 7.0+):&lt;/strong&gt; Doubling the default chunk size to 128 MB halves migration frequency for large collections, while MongoDB 7.0+ balances based on actual data size variation rather than raw chunk counts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Automated Chunk Merging via AutoMerger (MongoDB 7.0 / 8.0):&lt;/strong&gt; Replaces manual 6.0 &lt;code&gt;defragmentCollection&lt;/code&gt; runs with the background AutoMerger (&lt;code&gt;sh.enableAutoMerger&lt;/code&gt;, &lt;code&gt;autoMergerIntervalSecs&lt;/code&gt;, &lt;code&gt;autoMergerThrottlingMS&lt;/code&gt;), automatically merging adjacent under-utilized chunks on the same shard.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Flow Control Integration:&lt;/strong&gt; Recipient shards with active Flow Control (&lt;code&gt;flowControlTargetLagSeconds&lt;/code&gt; default 10s) pace incoming chunk writes if secondary replication lag rises during migrations.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Adding shards adds migration I/O before it adds capacity:&lt;/strong&gt; Always pause the balancer (&lt;code&gt;sh.setBalancerState(false)&lt;/code&gt;) before &lt;code&gt;sh.addShard()&lt;/code&gt; and gate migrations behind an off-peak &lt;code&gt;activeWindow&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Account for 128 MB chunks and $\lfloor n/2 \rfloor$ concurrency:&lt;/strong&gt; In MongoDB 6.0+, each chunk move streams 128 MB of potentially cold data across up to $\lfloor n/2 \rfloor$ shard pairs simultaneously.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Inspect &lt;code&gt;balancerCollectionStatus&lt;/code&gt; with its real 6.0+ fields:&lt;/strong&gt; Check &lt;code&gt;balancerCompliant&lt;/code&gt;, &lt;code&gt;chunkSize&lt;/code&gt;, and &lt;code&gt;firstComplianceViolation&lt;/code&gt; rather than non-existent &lt;code&gt;chunksToMove&lt;/code&gt; counters.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Throttle the range deleter if secondary lag spikes after migrations:&lt;/strong&gt; Tune &lt;code&gt;rangeDeleterBatchSize&lt;/code&gt; and &lt;code&gt;rangeDeleterBatchDelayMS&lt;/code&gt; so post-move orphan cleanup does not overwhelm WiredTiger dirty cache.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never terminate a draining shard early:&lt;/strong&gt; Keep the balancer running, move every database in &lt;code&gt;dbsToMove&lt;/code&gt; via &lt;code&gt;movePrimary&lt;/code&gt;, and wait for &lt;code&gt;removeShard&lt;/code&gt; to return &lt;code&gt;state: &quot;completed&quot;&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Unthrottled chunk migrations during cluster scaling evict hot WiredTiger pages and saturate EBS read IOPS, degrading live application latency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Standardize scaling runbooks on pausing the balancer before &lt;code&gt;sh.addShard()&lt;/code&gt;, enforcing an off-peak &lt;code&gt;activeWindow&lt;/code&gt; in &lt;code&gt;config.settings&lt;/code&gt;, and tuning range-deleter batching.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Monitor &lt;code&gt;balancerCollectionStatus&lt;/code&gt; for &lt;code&gt;balancerCompliant: true&lt;/code&gt; and verify with &lt;code&gt;iostat -xz&lt;/code&gt; that donor read &lt;code&gt;await&lt;/code&gt; stays below 15 ms during balancing windows.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your production sharded clusters for an active &lt;code&gt;activeWindow&lt;/code&gt;, test &lt;code&gt;mongo_cluster_scaler.py --dry-run&lt;/code&gt; against staging, and verify homogeneous EC2/EBS sizing across all shards.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>When the Shard Key Was Wrong: RefineShardKey, Live Resharding, and analyzeShardKey</title><link>https://rajivonai.com/blog/2026-06-06-when-the-shard-key-was-wrong/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-06-when-the-shard-key-was-wrong/</guid><description>How to orchestrate live zero-downtime resharding in MongoDB 5.0+ to correct suboptimal shard keys, prevent scatter-gather query collapses, and manage cloning I/O.</description><pubDate>Mon, 31 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A sharded database is only as scalable as its shard key; when application access patterns outgrow legacy shard key boundaries, a cluster degrades into an expensive, high-latency scatter-gather query broadcast engine.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; When high-frequency queries omit the shard key prefix, &lt;code&gt;mongos&lt;/code&gt; broadcasts every read to all shards, saturating router &lt;code&gt;SHARD_MERGE&lt;/code&gt; CPU and evicting WiredTiger caches across the fleet.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Validate a replacement key with &lt;code&gt;configureQueryAnalyzer&lt;/code&gt; and &lt;code&gt;analyzeShardKey&lt;/code&gt; (7.0+), use &lt;code&gt;refineCollectionShardKey&lt;/code&gt; (4.4+) if only a suffix is needed, or run &lt;code&gt;sh.reshardCollection&lt;/code&gt; (5.0+) to change the key online.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Live resharding requires &lt;code&gt;((storageSize + indexSize) * 2) / shardCount&lt;/code&gt; free disk space across shards, a 24–48h oplog window, and &lt;code&gt;retryWrites=true&lt;/code&gt; on all drivers to survive the critical section write pause.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-06-when-the-shard-key-was-wrong.svg&quot; alt=&quot;Resharding replaces cluster-wide scatter-gather with targeted routing&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In horizontally partitioned MongoDB clusters on AWS EC2, the shard key dictates how documents are distributed across replica sets and how &lt;code&gt;mongos&lt;/code&gt; routers direct application requests. Each &lt;code&gt;mongos&lt;/code&gt; instance caches the collection’s chunk-to-shard routing table from the Config Server replica set and refreshes it lazily when shards return a &lt;code&gt;StaleConfig&lt;/code&gt; exception. Initial shard key choices—such as &lt;code&gt;{ customer_id: 1 }&lt;/code&gt;—typically reflect early single-tenant access patterns where every query filters on &lt;code&gt;customer_id&lt;/code&gt;, allowing &lt;code&gt;mongos&lt;/code&gt; to route reads and writes to a single shard.&lt;/p&gt;
&lt;p&gt;As platforms mature, product requirements introduce cross-tenant reporting, global search, order-status dashboards, and time-windowed analytics. When high-frequency queries filter by &lt;code&gt;status&lt;/code&gt; or &lt;code&gt;created_at&lt;/code&gt; without including the shard key prefix, &lt;code&gt;mongos&lt;/code&gt; can no longer target a single shard. Instead, the router dispatches every query to every shard simultaneously, turning a targeted distributed store into a cluster-wide broadcast engine.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;When primary query predicates diverge from the shard key, cluster capacity degrades non-linearly across four dimensions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Distributed Query Fan-Out:&lt;/strong&gt; Because incoming filters omit the shard key prefix—or use range predicates against a &lt;code&gt;hashed&lt;/code&gt; shard key—&lt;code&gt;mongos&lt;/code&gt; cannot prune shards from its cached chunk map. A read that should consume one execution thread on one shard instead consumes $N$ concurrent threads across the entire fleet.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Router &lt;code&gt;SHARD_MERGE&lt;/code&gt; Bottleneck:&lt;/strong&gt; Each shard scans its local indexes and streams matching documents back to &lt;code&gt;mongos&lt;/code&gt;. When queries include &lt;code&gt;.sort()&lt;/code&gt; or &lt;code&gt;.skip()&lt;/code&gt; on non-shard-key fields, &lt;code&gt;mongos&lt;/code&gt; must buffer, merge-sort, and discard records across all $N$ streams in memory (&lt;code&gt;SHARD_MERGE&lt;/code&gt; stage), driving &lt;code&gt;mongos&lt;/code&gt; CPU toward saturation and exhausting client connection pools.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fleet-Wide WiredTiger Cache Eviction:&lt;/strong&gt; Even when each shard returns only a handful of documents, scanning local B-trees pages cold data into WiredTiger caches across all shards simultaneously, evicting hot transactional working sets and spiking EBS gp3 read IOPS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Storage and Oplog Constraints During Correction:&lt;/strong&gt; Prior to MongoDB 4.4, shard keys were strictly immutable, forcing multi-day offline &lt;code&gt;mongodump&lt;/code&gt; and &lt;code&gt;mongorestore&lt;/code&gt; cycles. Modern online resharding (&lt;code&gt;reshardCollection&lt;/code&gt;) eliminates downtime, but requires at least &lt;strong&gt;2x&lt;/strong&gt; the collection’s storage and index footprint distributed across recipient shards (&lt;code&gt;((storageSize + indexSize) * 2) / shardCount&lt;/code&gt;) plus an oplog window large enough to cover the entire clone phase.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-06-when-the-shard-key-was-wrong-2-constraints.svg&quot; alt=&quot;Live resharding requires 2x footprint and oplog headroom&quot;&gt;&lt;/p&gt;
&lt;p&gt;The core architectural question is: &lt;strong&gt;How can platform teams validate candidate shard keys against live traffic using &lt;code&gt;analyzeShardKey&lt;/code&gt;, choose between &lt;code&gt;refineCollectionShardKey&lt;/code&gt; and &lt;code&gt;sh.reshardCollection&lt;/code&gt;, and execute multi-terabyte resharding without exhausting disk space or dropping client writes?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;MongoDB provides four mechanisms for changing or retiring a sharded collection’s partitioning scheme, each with distinct version, storage, and downtime constraints:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Version&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Key Change Scope&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Data Movement / Cost&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;refineCollectionShardKey&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;4.4+&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Append suffix fields only&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Metadata-only; zero immediate data copy&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen&lt;/strong&gt; when the existing prefix is still queried&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;sh.reshardCollection&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;5.0+&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Arbitrary new key or hash&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Online clone; requires 2x footprint across shards&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen&lt;/strong&gt; when queries miss the existing prefix&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;sh.unshardCollection&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;8.0+&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Removes shard key entirely&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Moves all chunks onto one target shard&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Conditional&lt;/strong&gt;; only if data fits on a single shard&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;mongodump&lt;/code&gt; / &lt;code&gt;mongorestore&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;All&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Arbitrary new key&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Multi-day write downtime on multi-TB collections&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt; for 24/7 production clusters&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;refineCollectionShardKey&lt;/code&gt; (MongoDB 4.4+):&lt;/strong&gt; Appends one or more suffix fields to the existing shard key (for example, &lt;code&gt;{ customer_id: 1 }&lt;/code&gt; to &lt;code&gt;{ customer_id: 1, created_at: 1 }&lt;/code&gt;). Before running &lt;code&gt;refineCollectionShardKey&lt;/code&gt;, you must create the supporting compound index matching the new key on the collection. It is an instant metadata-only operation that fixes low-cardinality jumbo chunks without moving data immediately, leaving chunk splits and migrations to the balancer. However, it cannot change or remove the existing prefix field or switch between range and hashed sharding.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;sh.reshardCollection&lt;/code&gt; (MongoDB 5.0+):&lt;/strong&gt; Creates an internal temporary collection with the new shard key, clones data online, tails donor oplogs via change streams, and swaps catalog metadata atomically. Using &lt;code&gt;db.adminCommand({ reshardCollection: ... })&lt;/code&gt;, operators can also pass &lt;code&gt;numInitialChunks&lt;/code&gt; and &lt;code&gt;zones&lt;/code&gt; to pre-split and place ranges across shards during the clone. In MongoDB 8.0+, passing &lt;code&gt;forceRedistribution: true&lt;/code&gt; also allows rebalancing data across shards or zones using the same shard key.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;sh.unshardCollection&lt;/code&gt; (MongoDB 8.0+):&lt;/strong&gt; Consolidates a sharded collection back onto a single shard (the shard with the least data by default, or an explicit &lt;code&gt;toShard&lt;/code&gt;), useful when a collection no longer justifies sharding overhead and its total footprint fits comfortably on one replica set.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Offline &lt;code&gt;mongodump&lt;/code&gt; and &lt;code&gt;mongorestore&lt;/code&gt;:&lt;/strong&gt; Rejected for production systems because it requires halting application writes for hours or days while dumping, dropping, and restoring terabytes of BSON, and forces full secondary index rebuilds during restore.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-06-when-the-shard-key-was-wrong-3-options.svg&quot; alt=&quot;Choose refineCollectionShardKey for suffixes, reshardCollection for new prefixes&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-live-resharding-architecture-the-coordinator-cloning-and-commit-protocol&quot;&gt;The Live Resharding Architecture: The Coordinator, Cloning, and Commit Protocol&lt;/h2&gt;
&lt;p&gt;Introduced in MongoDB 5.0, &lt;strong&gt;Live Resharding&lt;/strong&gt; (&lt;code&gt;sh.reshardCollection&lt;/code&gt;) orchestrates a distributed state machine led by the &lt;code&gt;ReshardingCoordinator&lt;/code&gt; on the Config Server replica set primary.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-06-when-the-shard-key-was-wrong-4-architecture.svg&quot; alt=&quot;ReshardingCoordinator orchestrates cloning, oplog apply, and atomic swap&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Client[&quot;Application Clients — Continuous Traffic&quot;] --&gt; Router[&quot;mongos Router Fleet&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Router --&gt;|Writes during clone| Donor[&quot;Donor Shard — Old Key&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord[&quot;ReshardingCoordinator — Config Primary&quot;] --&gt; Donor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord --&gt; Recipient[&quot;Recipient Shard — New Key&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Donor -.-&gt;|Snapshot Clone &amp;#x26; Oplog Tail| Recipient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord --&gt;|Critical Section &amp;#x26; Catalog Swap| Router&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;mechanics-of-the-resharding-state-machine&quot;&gt;Mechanics of the Resharding State Machine&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Initialization &amp;#x26; Pre-Flight:&lt;/strong&gt; The &lt;code&gt;ReshardingCoordinator&lt;/code&gt; on the Config Server primary records state in &lt;code&gt;config.reshardingOperations&lt;/code&gt;, verifies cluster health and disk/oplog prerequisites, pauses the balancer automatically, and creates temporary internal namespaces on recipient shards.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Initial Data Clone:&lt;/strong&gt; Recipient shards open WiredTiger snapshot cursors against donor shards to copy documents belonging to their assigned new chunk ranges, inserting them in bulk and building all secondary indexes concurrently.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oplog Application:&lt;/strong&gt; While cloning runs, application writes continue on donor shards. Recipient shards tail donor oplogs and apply post-snapshot mutations continuously so data converges toward real time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Critical Section:&lt;/strong&gt; Once oplog catch-up lag drops to a small threshold, the coordinator acquires an exclusive write lock across the collection, pauses new writes briefly while drivers with &lt;code&gt;retryWrites=true&lt;/code&gt; buffer/retry, and drains the remaining trailing oplog entries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Catalog Commit &amp;#x26; Cleanup:&lt;/strong&gt; The coordinator atomically swaps the routing metadata in the Config Server catalog so &lt;code&gt;mongos&lt;/code&gt; routes to the new key layout, releases the write lock, and triggers asynchronous background deletion of the old collection files on donor shards.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;capacity-chunk-distribution-and-oplog-engineering-formulas&quot;&gt;Capacity, Chunk Distribution, and Oplog Engineering Formulas&lt;/h3&gt;
&lt;p&gt;Before starting &lt;code&gt;sh.reshardCollection&lt;/code&gt;, verify both storage headroom and oplog retention mathematically:&lt;/p&gt;
&lt;p&gt;$$\text{Storage Requirement Per Shard} = \frac{(\text{storageSize} + \text{indexSize}) \times 2}{\text{shardCount}}$$&lt;/p&gt;
&lt;p&gt;Because old and new collections coexist until cleanup completes, every shard must hold enough free EBS space for its share of twice the collection’s data plus index footprint. Additionally, if the collection uses Zone Sharding (&lt;code&gt;sh.addTagRange&lt;/code&gt; / &lt;code&gt;sh.updateZoneKeyRange&lt;/code&gt;) to pin tenant or regional data to specific shards, you must supply updated &lt;code&gt;zones&lt;/code&gt; definitions in the &lt;code&gt;reshardCollection&lt;/code&gt; command; otherwise, existing zone boundaries are dropped when the new shard key takes effect.&lt;/p&gt;
&lt;p&gt;Similarly, estimate minimum clone duration from network bandwidth:&lt;/p&gt;
&lt;p&gt;$$T_{\text{clone}} = \frac{S_{\text{data}}}{\text{NetworkThroughput}_{\text{MB/s}}}$$&lt;/p&gt;
&lt;p&gt;For a 2 TB collection transferring at 150 MB/s, $T_{\text{clone}} \approx 13,333\text{ seconds}$ (3.7 hours). Size the oplog retention window on every donor shard to at least $3 \times T_{\text{clone}}$—and standardize on &lt;strong&gt;24 to 48 hours&lt;/strong&gt; via &lt;code&gt;replSetResizeOplog&lt;/code&gt;—so transient network slowdowns never cause a recipient shard to fall off a donor oplog mid-clone. Setting &lt;code&gt;numInitialChunks&lt;/code&gt; (default depends on estimated collection size, minimum 1 per shard) ensures the new collection starts with evenly split ranges across recipient shards rather than dumping all initial inserts onto a single primary shard.&lt;/p&gt;
&lt;h2 id=&quot;diagnostic-telemetry-high-resolution-resharding-forensics&quot;&gt;Diagnostic Telemetry: High-Resolution Resharding Forensics&lt;/h2&gt;
&lt;p&gt;Monitor &lt;code&gt;ReshardingCoordinator&lt;/code&gt; and recipient progress in real time via &lt;code&gt;$currentOp&lt;/code&gt; and &lt;code&gt;config.reshardingOperations&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Execute on mongos or Config Server primary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; adminDB&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getSiblingDB&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;admin&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ops&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; adminDB.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aggregate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;([&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    { $currentOp: { allUsers: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, localOps: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    { $match: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        type: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;op&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        $or: [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;desc&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#DBEDFF&quot;&gt;Resharding&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;originatingCommand.reshardCollection&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { $exists: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    } }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ]).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toArray&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (ops.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ===&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[INFO] Checking config.reshardingOperations...&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; activeOp&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getSiblingDB&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;config&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).reshardingOperations.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;findOne&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    printjson&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(activeOp &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; { status: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;No resharding operation currently active.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ops.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Component: &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; op.desc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot; | Elapsed: &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (op.secs_running &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3600&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;h&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (op.bytesCopied &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; undefined&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; op.approxBytesToCopy) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; copiedGB&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (op.bytesCopied &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; **&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; totalGB&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (op.approxBytesToCopy &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; **&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;      print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;  Progress: &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; copiedGB &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot; GB / &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; totalGB &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot; GB&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Execute live resharding as a five-step staged rollout with explicit validation gates and a pre-commit abort path:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-06-when-the-shard-key-was-wrong-5-flow.svg&quot; alt=&quot;Five-step workflow from analyzeShardKey to verified cutover&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Step 1 — Enable Query Sampling (MongoDB 7.0+):&lt;/strong&gt; Start &lt;code&gt;configureQueryAnalyzer&lt;/code&gt; on the target collection during representative traffic (maximum 50 samples/sec):
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.sales.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;configureQueryAnalyzer&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ mode: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;full&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, samplesPerSecond: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 2 — Score Candidate Key and Stop Sampling:&lt;/strong&gt; After capturing representative read/write traffic, run &lt;code&gt;analyzeShardKey&lt;/code&gt; and turn sampling off:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.sales.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;analyzeShardKey&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { customer_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, created_at: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { keyCharacteristics: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, readWriteDistribution: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, sampleRate: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0.1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.sales.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;configureQueryAnalyzer&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ mode: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;off&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;strong&gt;Gate:&lt;/strong&gt; Confirm high cardinality, low frequency of single key values, and that top read predicates hit the new key prefix.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 3 — Pre-Flight Disk, Oplog, and Driver Audit:&lt;/strong&gt; Confirm &lt;code&gt;((storageSize + indexSize) * 2) / shardCount&lt;/code&gt; free EBS space on every shard, expand oplogs to 48 hours via &lt;code&gt;replSetResizeOplog&lt;/code&gt;, freeze CI/CD DDL migrations, and verify &lt;code&gt;retryWrites=true&lt;/code&gt; in all client URIs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 4 — Execute &lt;code&gt;sh.reshardCollection&lt;/code&gt;:&lt;/strong&gt; Pass the namespace and new shard key document directly (do not nest inside &lt;code&gt;{ key: ... }&lt;/code&gt; when using the &lt;code&gt;sh.&lt;/code&gt; helper):
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sh.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;reshardCollection&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;commerce.sales&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, { customer_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, created_at: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;strong&gt;Rollback Gate:&lt;/strong&gt; If disk headroom shrinks dangerously or latency spikes before Phase 5 catalog commit, abort safely without data loss using &lt;code&gt;sh.abortReshardCollection(&quot;commerce.sales&quot;)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 5 — Verify Targeted Routing and Cleanup:&lt;/strong&gt; Run &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; on previously slow reporting queries to confirm &lt;code&gt;SHARD_MERGE&lt;/code&gt; is gone and &lt;code&gt;winningPlan&lt;/code&gt; targets a single shard (&lt;code&gt;shards&lt;/code&gt; array in &lt;code&gt;queryPlanner&lt;/code&gt; lists only the owning shard). Confirm via &lt;code&gt;sh.status()&lt;/code&gt; that the updated shard key is active and monitor donor shard disk usage as Phase 6 asynchronously drops the temporary/old collection files. Remember that WiredTiger retains freed extent space internally rather than shrinking &lt;code&gt;.wt&lt;/code&gt; files back to the OS filesystem immediately, so monitor &lt;code&gt;storageSize&lt;/code&gt; vs. &lt;code&gt;freeStorageSize&lt;/code&gt; rather than &lt;code&gt;df -h&lt;/code&gt; alone.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;automated-pre-flight-validator&quot;&gt;Automated Pre-Flight Validator&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env python3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;MongoDB Production Resharding Pre-Flight Validator &amp;#x26; Orchestrator&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Usage:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    python3 mongo_resharding_orchestrator.py --mongos-uri &quot;mongodb://admin:pass@mongos1:27017&quot; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      --ns &quot;commerce.sales&quot; --new-key &apos;{&quot;customer_id&quot;: 1, &quot;created_at&quot;: 1}&apos; --dry-run&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sys&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo.errors &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PyMongoError&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; preflight_reshard_check&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(client, namespace, new_key_dict, dry_run):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    config_db &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.config&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    admin_db &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.admin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    db_name, coll_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; namespace.split(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    target_coll &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client[db_name][coll_name]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    coll_meta &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; config_db.collections.find_one({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: namespace})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; coll_meta &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;or&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; coll_meta.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;dropped&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[FATAL] Namespace &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;namespace&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; is not currently sharded!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sys.exit(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    shard_count &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; max&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(config_db.shards.count_documents({}), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        stats &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(target_coll.aggregate([{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$collStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;storageStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {}}}]))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        storage_gb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; sum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(s.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;storageStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, {}).get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;storageSize&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; s &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stats) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        index_gb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; sum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(s.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;storageStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, {}).get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;totalIndexSize&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; s &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stats) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    except&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PyMongoError &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; e:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[FATAL] Failed to retrieve collection storage stats: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sys.exit(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    required_per_shard_gb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ((storage_gb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; index_gb) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2.0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; shard_count&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Total Storage + Index : &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;storage_gb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; index_gb&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.2f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; GB across &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;shard_count&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; shards&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Required Free / Shard : &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;required_per_shard_gb&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.2f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; GB (2x official formula)&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; config_db.reshardingOperations.find_one():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[FATAL] A resharding operation is already active.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sys.exit(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dry_run:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        res &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; admin_db.command({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;reshardCollection&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: namespace, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: new_key_dict})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[SUCCESS] Resharding completed: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;res&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[DRY RUN] Would execute: sh.reshardCollection(&apos;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;namespace&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;new_key_dict&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse.ArgumentParser(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;description&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;MongoDB Resharding Orchestrator&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--mongos-uri&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;required&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--ns&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;required&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--new-key&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;required&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--dry-run&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;action&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;store_true&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    args &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; parser.parse_args()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    client &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient(args.mongos_uri, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;serverSelectionTimeoutMS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    preflight_reshard_check(client, args.ns, json.loads(args.new_key), args.dry_run)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; __name__&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    main()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The operational tradeoffs of resharding follow directly from MongoDB’s routing and storage engine mechanics (CARL B/C — derived from documented system behavior, not personal benchmark claims):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Scatter-Gather Elimination:&lt;/strong&gt; When a query includes the shard key prefix, &lt;code&gt;mongos&lt;/code&gt; consults its cached chunk map from the Config Server and dispatches the operation to only the owning shard(s), eliminating &lt;code&gt;SHARD_MERGE&lt;/code&gt; buffering and cutting cross-shard index scans proportionally to shard count.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Evaluating Candidate Keys with &lt;code&gt;analyzeShardKey&lt;/code&gt;:&lt;/strong&gt; Before committing to a multi-hour clone, &lt;code&gt;analyzeShardKey&lt;/code&gt; inspects two dimensions: &lt;code&gt;keyCharacteristics&lt;/code&gt; (evaluating cardinality, frequency of the most common values, and monotonicity) and &lt;code&gt;readWriteDistribution&lt;/code&gt; (measuring what percentage of sampled reads and writes are single-shard vs. scatter-gather). Choosing a compound key with a high-cardinality prefix and a query-aligned suffix avoids trading scatter-gather reads for hot-shard monotonic write bottlenecks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Clone I/O vs. Production Cache:&lt;/strong&gt; During &lt;code&gt;reshardCollection&lt;/code&gt; cloning, donor shards read collection data via WiredTiger snapshot cursors while recipient shards build indexes concurrently. Because &lt;code&gt;config.reshardingOperations&lt;/code&gt; persists coordinator state on the Config Server replica set, the operation survives Config Server primary failovers, though scheduling cloning during off-peak hours prevents index-build dirty cache pressure from throttling live writes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Driver Resilience Across the Critical Section:&lt;/strong&gt; Because &lt;code&gt;ReshardingCoordinator&lt;/code&gt; briefly blocks writes during the critical section before committing the catalog swap, drivers configured with &lt;code&gt;retryWrites=true&lt;/code&gt; (the default in modern MongoDB 4.2+ drivers) transparently retry affected writes once the catalog swap completes.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-06-when-the-shard-key-was-wrong-6-failures.svg&quot; alt=&quot;Three failure modes that abort or crash live resharding&quot;&gt;&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Insufficient 2x Disk Space&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Coexisting old and new collections fill EBS volume during clone&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;EBS free space drops below &lt;code&gt;((storage + index) * 2) / N&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Provision 2x free footprint across shards before starting&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Oplog Rollover During Clone&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Donor oplog overwrites entries before recipient finishes tailing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;ReshardingCoordinator&lt;/code&gt; aborts with oplog position lost&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Resize donor oplogs to 48h via &lt;code&gt;replSetResizeOplog&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Drivers With &lt;code&gt;retryWrites=false&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Client writes during critical section fail immediately instead of retrying&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Application write errors spike during cutover&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce &lt;code&gt;retryWrites=true&lt;/code&gt; in all client connection strings&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Concurrent DDL / Index Builds&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Schema or index changes conflict with resharding metadata locks&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;DDL commands rejected or coordinator stalls&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Freeze schema migrations in CI/CD during resharding&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unique Key Violation&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Resharding with &lt;code&gt;unique: true&lt;/code&gt; hits duplicate values in existing data&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Clone phase aborts and discards progress&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pre-check uniqueness with an aggregation &lt;code&gt;$group&lt;/code&gt; pipeline&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Leaving &lt;code&gt;configureQueryAnalyzer&lt;/code&gt; On&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Continuous query sampling adds overhead after analysis finishes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;config.settings&lt;/code&gt; or server metrics show active sampling&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run &lt;code&gt;configureQueryAnalyzer({ mode: &quot;off&quot; })&lt;/code&gt; immediately&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;april-2026-update&quot;&gt;April 2026 Update&lt;/h2&gt;
&lt;p&gt;MongoDB 7.0 and 8.0 expand shard key management beyond initial 5.0 resharding:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Pre-Migration Validation (MongoDB 7.0+):&lt;/strong&gt; &lt;code&gt;configureQueryAnalyzer&lt;/code&gt; and &lt;code&gt;analyzeShardKey&lt;/code&gt; measure key cardinality, monotonicity, and read/write routing distribution against live traffic before committing to a multi-hour clone.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Same-Key Redistribution (MongoDB 8.0+):&lt;/strong&gt; Passing &lt;code&gt;forceRedistribution: true&lt;/code&gt; to &lt;code&gt;reshardCollection&lt;/code&gt; redistributes chunks across newly added shards or zones without changing the shard key itself.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Collection Unsharding (MongoDB 8.0+):&lt;/strong&gt; &lt;code&gt;sh.unshardCollection()&lt;/code&gt; consolidates an over-sharded collection back onto a single shard when cluster topology simplifies.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code&gt;refineCollectionShardKey&lt;/code&gt; (4.4+) before reaching for &lt;code&gt;reshardCollection&lt;/code&gt;:&lt;/strong&gt; If queries still hit the existing prefix and you only need to break up jumbo chunks, appending a suffix is metadata-only and instant.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Verify &lt;code&gt;sh.reshardCollection&lt;/code&gt; syntax and 2x disk math up front:&lt;/strong&gt; Pass the key document directly to &lt;code&gt;sh.reshardCollection(ns, key)&lt;/code&gt; and confirm &lt;code&gt;((storageSize + indexSize) * 2) / shardCount&lt;/code&gt; free space per shard.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Always pair &lt;code&gt;analyzeShardKey&lt;/code&gt; with &lt;code&gt;configureQueryAnalyzer&lt;/code&gt;:&lt;/strong&gt; Sample traffic first, run &lt;code&gt;analyzeShardKey&lt;/code&gt;, and explicitly set &lt;code&gt;mode: &quot;off&quot;&lt;/code&gt; afterward.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Treat &lt;code&gt;retryWrites=true&lt;/code&gt; and 48h oplog windows as mandatory prerequisites:&lt;/strong&gt; They protect live writes during the critical section and prevent multi-hour clones from aborting on oplog rollover.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Keep &lt;code&gt;sh.abortReshardCollection&lt;/code&gt; ready until Phase 5:&lt;/strong&gt; You can cancel an in-flight resharding safely without data loss anytime before the catalog swap commits.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Identify whether application query evolution has turned targeted shard lookups into cluster-wide &lt;code&gt;SHARD_MERGE&lt;/code&gt; scatter-gather broadcasts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Evaluate candidate keys with &lt;code&gt;configureQueryAnalyzer&lt;/code&gt; and &lt;code&gt;analyzeShardKey&lt;/code&gt; (7.0+), refine suffixes with &lt;code&gt;refineCollectionShardKey&lt;/code&gt; where possible, or run &lt;code&gt;sh.reshardCollection&lt;/code&gt; online.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; on top slow queries after cutover to confirm &lt;code&gt;SHARD_MERGE&lt;/code&gt; is replaced by single-shard targeting.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit client connection strings for &lt;code&gt;retryWrites=true&lt;/code&gt;, run &lt;code&gt;mongo_resharding_orchestrator.py --dry-run&lt;/code&gt; against your target collection, and verify 2x disk headroom before scheduling the window.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>Jumbo Chunks, Stuck Migrations, and MongoDB Balancer Incidents</title><link>https://rajivonai.com/blog/2026-05-30-mongodb-jumbo-chunks-balancer-migrations/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-30-mongodb-jumbo-chunks-balancer-migrations/</guid><description>When MongoDB chunks exceed 64MB and fail to split due to write ingestion velocity or low shard key cardinality, the balancer stalls. Here is how to diagnose and resolve jumbo chunks.</description><pubDate>Sun, 30 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;When a shard key lacks sufficient cardinality or write velocity outpaces splitting on MongoDB 4.4 and 5.0, chunks cross the maximum range threshold into an un-splittable &lt;code&gt;jumbo&lt;/code&gt; state that freezes the balancer and pins load onto a single shard.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Once &lt;code&gt;config.chunks&lt;/code&gt; marks a range &lt;code&gt;jumbo: true&lt;/code&gt;, the balancer skips it permanently, leaving one shard saturated on CPU, IOPS, and WiredTiger cache while peer shards sit underutilized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Stop the balancer, separate divisible chunks (fix with &lt;code&gt;splitFind&lt;/code&gt; + &lt;code&gt;clearJumboFlag&lt;/code&gt;) from cardinality-exhausted chunks (fix with &lt;code&gt;refineCollectionShardKey&lt;/code&gt; or &lt;code&gt;reshardCollection&lt;/code&gt;), then resume balancing inside an off-peak &lt;code&gt;activeWindow&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Calling &lt;code&gt;clearJumboFlag&lt;/code&gt; without shrinking or splitting an oversized chunk triggers an endless loop of failed &lt;code&gt;moveChunk&lt;/code&gt; transfers, cross-AZ bandwidth saturation, and &lt;code&gt;chunk too big to move&lt;/code&gt; aborts.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-30-mongodb-jumbo-chunks-balancer-migrations.svg&quot; alt=&quot;Low-cardinality keys pin jumbo chunks to one shard and stall the balancer&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Sharded MongoDB clusters distribute collection data across shards as contiguous key ranges called chunks. On MongoDB 4.4 and 5.0, the default maximum chunk size is 64 MB, and the auto-splitter on the primary shard evaluates whether a chunk needs splitting after writes accumulate. Starting in MongoDB 6.0, the default range size doubled to 128 MB, and in MongoDB 6.0.3+ automatic chunk splitting was removed altogether in favor of data-size balancing across shards.&lt;/p&gt;
&lt;p&gt;Despite that architectural shift in 6.0.3+, production estates running 4.4 or 5.0—or clusters upgraded from earlier versions with legacy low-cardinality shard keys—still hit jumbo chunk deadlocks. On cloud infrastructure such as AWS EC2 with EBS &lt;code&gt;gp3&lt;/code&gt; volumes, a single un-migratable range concentrates all writes and working-set reads for a large tenant onto one replica set. Because the cluster balancer runs on the primary member of the config server replica set (CSRS, moved there in MongoDB 3.4) and explicitly refuses to schedule &lt;code&gt;moveChunk&lt;/code&gt; on any chunk flagged &lt;code&gt;jumbo: true&lt;/code&gt;, adding more shards does nothing to relieve the hot shard.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Jumbo chunks arise from two distinct mechanisms—auto-split lag and shard key cardinality exhaustion—and treating them identically makes incidents worse.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Chunk size and document-count ceilings:&lt;/strong&gt; On MongoDB 4.4 and 5.0, a chunk becomes ineligible for automatic migration when its size exceeds the configured &lt;code&gt;chunkSize&lt;/code&gt; (default 64 MB; 128 MB in 6.0+) or when it exceeds the legacy document-count ceiling of 250,000 documents for a 64 MB chunk. Small-document workloads (such as 200-byte telemetry events) hit the document-count cap at roughly &lt;code&gt;250,000 * 200 B = 50 MB&lt;/code&gt;, before reaching the byte ceiling.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cardinality exhaustion:&lt;/strong&gt; When a collection is sharded on a low-cardinality prefix such as &lt;code&gt;{ tenant_id: 1 }&lt;/code&gt;, every document for a single tenant shares the exact same shard key value. Because MongoDB cannot split documents with identical shard key values across two chunks, &lt;code&gt;splitVector&lt;/code&gt; returns zero split points. If a single tenant exceeds 64 MB (or 128 MB on 6.0+), the chunk becomes mathematically indivisible.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Auto-split starvation under burst ingest (4.4 / 5.0):&lt;/strong&gt; Even when a compound key has sufficient cardinality, high-velocity bulk loads can grow a chunk past the migration threshold before the primary executes &lt;code&gt;splitVector&lt;/code&gt;. Once &lt;code&gt;moveChunk&lt;/code&gt; attempts to migrate the oversized range and fails, MongoDB flags the chunk &lt;code&gt;jumbo: true&lt;/code&gt; in &lt;code&gt;config.chunks&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Metadata schema divergence across versions:&lt;/strong&gt; Prior to MongoDB 5.0, &lt;code&gt;config.chunks&lt;/code&gt; keyed documents by the namespace string &lt;code&gt;ns&lt;/code&gt;. Starting in MongoDB 5.0, &lt;code&gt;config.chunks&lt;/code&gt; replaced &lt;code&gt;ns&lt;/code&gt; with the collection &lt;code&gt;uuid&lt;/code&gt; from &lt;code&gt;config.collections&lt;/code&gt;. Runbook queries written against &lt;code&gt;config.chunks.find({ ns: ... })&lt;/code&gt; silently return zero rows on 5.0+ clusters, masking active jumbo chunks during an incident.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-30-mongodb-jumbo-chunks-balancer-migrations-2-constraints.svg&quot; alt=&quot;Three thresholds gate chunk splitting and migration&quot;&gt;&lt;/p&gt;
&lt;p&gt;The operational question is: &lt;strong&gt;How do platform engineers distinguish transient split lag from shard-key cardinality exhaustion, resolve &lt;code&gt;jumbo: true&lt;/code&gt; chunks without triggering migration thrashing, and restore balanced shard utilization online?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;When a chunk is flagged &lt;code&gt;jumbo: true&lt;/code&gt;, four intervention paths exist. Choosing the wrong one either fails immediately or saturates cross-AZ bandwidth and WiredTiger cache.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;When It Works&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;clearJumboFlag&lt;/code&gt; alone&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Deletes already shrank chunk below &lt;code&gt;chunkSize&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Oversized chunk triggers &lt;code&gt;moveChunk&lt;/code&gt;, saturates I/O, aborts, re-flags &lt;code&gt;jumbo&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reject unless size verified&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;splitFind&lt;/code&gt; + &lt;code&gt;clearJumboFlag&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Divisible chunk (4.4/5.0 split lag or post-refine)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails when all documents share one shard key value&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Default for divisible chunks&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;refineCollectionShardKey&lt;/code&gt; (4.4+)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Existing prefix is sound; needs high-cardinality suffix&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires pre-built supporting index; cannot remove a bad prefix&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Default for low cardinality&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;reshardCollection&lt;/code&gt; (5.0+)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Existing shard key prefix itself is broken&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Needs ~2x free disk space, heavy oplog/I/O, brief write block at commit&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Last resort for bad prefixes&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Running &lt;code&gt;moveChunk&lt;/code&gt; with &lt;code&gt;forceJumbo: true&lt;/code&gt; is a fifth tactical escape hatch when a single oversized chunk must move off a full disk, but it does not fix the underlying divisibility problem and carries high catch-up abort risk under active writes. Similarly, running &lt;code&gt;clearJumboFlag&lt;/code&gt; without first verifying that the chunk is divisible or has shrunk below the configured range size is an anti-pattern: the CSRS balancer immediately picks up the chunk on the next round, streams gigabytes across AZs, fails to converge, and resets the flag.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-30-mongodb-jumbo-chunks-balancer-migrations-3-options.svg&quot; alt=&quot;Choose remediation by whether the jumbo chunk is divisible&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-chunk-lifecycle-and-remediation-architecture&quot;&gt;The Chunk Lifecycle and Remediation Architecture&lt;/h2&gt;
&lt;p&gt;Diagnosing and fixing jumbo chunks requires understanding how the CSRS balancer, &lt;code&gt;splitVector&lt;/code&gt;, and &lt;code&gt;moveChunk&lt;/code&gt; interact across the control plane and data plane.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-30-mongodb-jumbo-chunks-balancer-migrations-4-architecture.svg&quot; alt=&quot;CSRS balancer coordinates moveChunk across donor, recipient, and config catalog&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-control-plane-csrs-balancer-and-metadata-catalog&quot;&gt;1. Control Plane: CSRS Balancer and Metadata Catalog&lt;/h3&gt;
&lt;p&gt;Since MongoDB 3.4, the balancer runs as a background thread on the primary member of the config server replica set (CSRS), eliminating the distributed lock contention that existed when &lt;code&gt;mongos&lt;/code&gt; routers competed for the balancer lock. The CSRS primary reads balancing configuration from &lt;code&gt;config.settings&lt;/code&gt; (including &lt;code&gt;activeWindow&lt;/code&gt; and per-collection settings) and chunk metadata from &lt;code&gt;config.chunks&lt;/code&gt;. Any chunk with &lt;code&gt;jumbo: true&lt;/code&gt; is excluded from balancing rounds.&lt;/p&gt;
&lt;h3 id=&quot;2-how-splitvector-finds-split-points&quot;&gt;2. How &lt;code&gt;splitVector&lt;/code&gt; Finds Split Points&lt;/h3&gt;
&lt;p&gt;When a shard primary splits a chunk (either via the legacy auto-splitter in 4.4/5.0 or manual &lt;code&gt;splitFind&lt;/code&gt; / &lt;code&gt;splitAt&lt;/code&gt; commands), it runs the internal &lt;code&gt;splitVector&lt;/code&gt; command:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Index scan:&lt;/strong&gt; &lt;code&gt;splitVector&lt;/code&gt; traverses the shard-key index between the chunk’s &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; bounds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Median estimation:&lt;/strong&gt; Using the collection’s average object size (&lt;code&gt;avgObjSize&lt;/code&gt;), it accumulates estimated bytes until it reaches half the target &lt;code&gt;chunkSize&lt;/code&gt; (for example, 32 MB on a 64 MB configuration) and records candidate split keys.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cardinality failure:&lt;/strong&gt; If every document in the range maps to the same shard key value, the index scan finds no intermediate key boundary. &lt;code&gt;splitVector&lt;/code&gt; returns an empty split list, leaving the chunk indivisible.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-data-plane-movechunk-cloning-catch-up-and-critical-section&quot;&gt;3. Data Plane: &lt;code&gt;moveChunk&lt;/code&gt; Cloning, Catch-Up, and Critical Section&lt;/h3&gt;
&lt;p&gt;When the balancer schedules a migration from a donor shard to a recipient shard:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Cloning:&lt;/strong&gt; The recipient shard reads documents within the chunk bounds from the donor and inserts them into its local WiredTiger storage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Catch-up:&lt;/strong&gt; Concurrently, the donor buffers incoming writes targeting that range; the recipient pulls and applies those buffered changes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Critical section:&lt;/strong&gt; Once catch-up lag drops to a minimal threshold, the donor enters a brief critical section that blocks writes to the migrating range, transfers the final delta, updates &lt;code&gt;config.chunks&lt;/code&gt; on the CSRS, and releases the lock.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Range deletion:&lt;/strong&gt; After &lt;code&gt;orphanCleanupDelaySecs&lt;/code&gt; (historically 900 seconds; raised to 3,600 seconds in MongoDB 8.2 to protect long-running secondary reads), the donor’s range deleter asynchronously deletes the migrated documents.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;4-version-safe-diagnostic-script-mongosh&quot;&gt;4. Version-Safe Diagnostic Script (&lt;code&gt;mongosh&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Because &lt;code&gt;config.chunks&lt;/code&gt; uses &lt;code&gt;uuid&lt;/code&gt; in MongoDB 5.0+ and &lt;code&gt;ns&lt;/code&gt; in MongoDB 4.4, diagnostic scripts must resolve the collection UUID from &lt;code&gt;config.collections&lt;/code&gt; first and fall back to &lt;code&gt;ns&lt;/code&gt; only on older versions:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Run via mongosh connected to mongos&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; cfg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getSiblingDB&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;config&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; jumboChunks&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cfg.chunks.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ jumbo: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toArray&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Total jumbo chunks: &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; jumboChunks.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; uuidToNs&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  cfg.collections.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({}, { _id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, uuid: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (c.uuid) uuidToNs[c.uuid.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toString&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; c._id;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  jumboChunks.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ns&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; c.ns &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (c.uuid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; uuidToNs[c.uuid.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toString&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;unknown&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`NS: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ns&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} | Shard: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;shard&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} | Min: ${&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;JSON&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;stringify&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;min&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  cfg.changelog.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ what: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;moveChunk.from&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;details.errmsg&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { $exists: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } })&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    .&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ time: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;limit&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;      print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toISOString&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} | ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ns&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} | ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;details&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;errmsg&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;5-automated-triage-and-split-script-pymongo&quot;&gt;5. Automated Triage and Split Script (&lt;code&gt;pymongo&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;For collections with many jumbo chunks, automate &lt;code&gt;splitFind&lt;/code&gt; and &lt;code&gt;clearJumboFlag&lt;/code&gt; (using &lt;code&gt;bounds: [min_bound, max_bound]&lt;/code&gt;, which works for both ranged and hashed shard keys) with a mandatory &lt;code&gt;--dry-run&lt;/code&gt; mode:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env python3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo.errors &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PyMongoError&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; resolve_jumbo_chunks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(client, namespace, dry_run):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cfg, admin &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.config, client.admin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    db_name, coll_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; namespace.split(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    coll_meta &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cfg.collections.find_one({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: namespace})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    query &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;jumbo&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    query[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;uuid&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; coll_meta &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;uuid&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; coll_meta &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;ns&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        coll_meta[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;uuid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; coll_meta &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;uuid&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; coll_meta &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; namespace&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; chunk &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cfg.chunks.find(query):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        min_b, max_b &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; chunk[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;min&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], chunk[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;max&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        doc_q &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            k: {&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$gte&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: min_b[k]} &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(min_b[k]).&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;__name__&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;MinKey&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {}),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                **&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$lt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: max_b[k]} &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(max_b[k]).&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;__name__&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;MaxKey&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {})}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; k &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; min_b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        doc_q &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {k: v &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; k, v &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; doc_q.items() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; v}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sample &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client[db_name][coll_name].find_one(doc_q)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sample:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            continue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        coords &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {k: sample[k] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; k &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; min_b &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; k &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sample}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dry_run:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;            print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[DRY-RUN] split + clearJumboFlag at &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;coords&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            continue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            admin.command(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;split&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, namespace, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;coords)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            admin.command(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;clearJumboFlag&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, namespace, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;bounds&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[min_b, max_b])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        except&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PyMongoError &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; exc:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;            print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[ERROR] &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;chunk[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;shard&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;exc&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; __name__&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse.ArgumentParser()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--mongos-uri&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;required&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--ns&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;required&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--dry-run&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;action&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;store_true&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    args &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; p.parse_args()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    resolve_jumbo_chunks(MongoClient(args.mongos_uri, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;serverSelectionTimeoutMS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), args.ns, args.dry_run)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Execute jumbo chunk remediation in four gated phases so metadata changes and data movement never collide with peak application traffic.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-30-mongodb-jumbo-chunks-balancer-migrations-5-flow.svg&quot; alt=&quot;Four-step protocol to clear jumbo chunks safely&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;phase-1-isolate-the-balancer&quot;&gt;Phase 1: Isolate the Balancer&lt;/h3&gt;
&lt;p&gt;Stop the CSRS balancer to halt any in-flight or retrying &lt;code&gt;moveChunk&lt;/code&gt; operations that compete for IOPS and cross-AZ network bandwidth:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sh.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;setBalancerState&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; Run &lt;code&gt;sh.isBalancerRunning()&lt;/code&gt; (or &lt;code&gt;db.adminCommand({ balancerStatus: 1 })&lt;/code&gt;) and wait until &lt;code&gt;inBalancerRound&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt; before touching chunk metadata.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; Re-enable &lt;code&gt;sh.setBalancerState(true)&lt;/code&gt; if no further action is taken.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-2-refine-shard-key-cardinality-only-if-indivisible&quot;&gt;Phase 2: Refine Shard Key Cardinality (Only If Indivisible)&lt;/h3&gt;
&lt;p&gt;If &lt;code&gt;config.chunks&lt;/code&gt; shows jumbo chunks where &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; share the exact same key value, &lt;code&gt;splitFind&lt;/code&gt; cannot divide them. On MongoDB 4.4+, append a high-cardinality suffix (such as &lt;code&gt;_id&lt;/code&gt;) using &lt;code&gt;refineCollectionShardKey&lt;/code&gt; after building the supporting compound index across all shards:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.events.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;createIndex&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ tenant_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, _id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }, { commitQuorum: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;majority&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;adminCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ refineCollectionShardKey: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;telemetry.events&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, key: { tenant_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, _id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; Confirm &lt;code&gt;sh.status()&lt;/code&gt; displays the new compound shard key on the collection before splitting.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; Index creation is online; &lt;code&gt;refineCollectionShardKey&lt;/code&gt; is a one-way metadata change and cannot be reverted without &lt;code&gt;reshardCollection&lt;/code&gt;, so verify query patterns before running.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-3-split-chunks-and-clear-the-jumbo-flag&quot;&gt;Phase 3: Split Chunks and Clear the Jumbo Flag&lt;/h3&gt;
&lt;p&gt;With sufficient cardinality in place, run &lt;code&gt;sh.splitFind()&lt;/code&gt; (or &lt;code&gt;db.adminCommand({ split: ns, find: ... })&lt;/code&gt;) on each jumbo chunk, then clear the flag via &lt;code&gt;db.adminCommand({ clearJumboFlag: ns, bounds: [chunk.min, chunk.max] })&lt;/code&gt;. Note that &lt;code&gt;clearJumboFlag&lt;/code&gt; with &lt;code&gt;find&lt;/code&gt; is disallowed on hashed shard keys; using &lt;code&gt;bounds&lt;/code&gt; works across both ranged and hashed collections.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; Query &lt;code&gt;config.chunks&lt;/code&gt; for &lt;code&gt;jumbo: true&lt;/code&gt; and verify the count is &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; Chunk splits are metadata-only operations on the shard primary and CSRS; if a split lands poorly, the balancer can merge adjacent small chunks later (or via &lt;code&gt;configureCollectionBalancing&lt;/code&gt; with &lt;code&gt;defragmentCollection: true&lt;/code&gt; on 6.0+).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-4-re-enable-the-balancer-inside-an-active-window&quot;&gt;Phase 4: Re-Enable the Balancer Inside an Active Window&lt;/h3&gt;
&lt;p&gt;Re-enable the balancer and bound migrations to an off-peak maintenance window in &lt;code&gt;config.settings&lt;/code&gt; so &lt;code&gt;moveChunk&lt;/code&gt; cloning and donor range deletion do not contend with daytime transactional latency:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getSiblingDB&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;config&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).settings.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;updateOne&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { _id: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balancer&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { $set: { activeWindow: { start: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;02:00&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, stop: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;06:00&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } } },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { upsert: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sh.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;setBalancerState&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; Monitor &lt;code&gt;sh.status()&lt;/code&gt; and &lt;code&gt;$shardedDataDistribution&lt;/code&gt; (6.0+) over subsequent windows to confirm data size converges across shards without migration aborts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; If daytime or window IOPS saturate, immediately run &lt;code&gt;sh.setBalancerState(false)&lt;/code&gt; or throttle range deletion with &lt;code&gt;rangeDeleterBatchSize&lt;/code&gt; and &lt;code&gt;rangeDeleterBatchDelayMS&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The operational behaviors above are derived from documented MongoDB sharding and WiredTiger internals rather than synthetic benchmarks:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;WiredTiger cache pressure during concurrent migrations:&lt;/strong&gt; MongoDB’s WiredTiger storage engine targets keeping dirty cache bytes below 5% and triggers application-thread eviction stalls when dirty bytes cross 20% (or total cache usage hits the 95% eviction trigger against the 80% target). During a &lt;code&gt;moveChunk&lt;/code&gt; transfer, the donor shard reads cold chunk pages into cache while applying live writes, and the recipient inserts cloned documents into its own cache. Running unbounded migrations during peak write traffic pushes both shards closer to the 20% dirty-byte stall threshold, elevating application p99 latency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Range deletion and secondary read safety:&lt;/strong&gt; After a migration commits, the donor shard retains the migrated range for &lt;code&gt;orphanCleanupDelaySecs&lt;/code&gt; (900 seconds by default in earlier releases, increased to 3,600 seconds in MongoDB 8.2) before the range deleter purges orphaned documents. If the donor deletes millions of orphaned documents from a formerly jumbo chunk in unthrottled batches, B-tree page modifications spike dirty cache and replication lag on secondaries. Tuning &lt;code&gt;rangeDeleterBatchSize&lt;/code&gt; and &lt;code&gt;rangeDeleterBatchDelayMS&lt;/code&gt; spreads that I/O across the maintenance window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why &lt;code&gt;activeWindow&lt;/code&gt; prevents cascading aborts:&lt;/strong&gt; On write-heavy collections, &lt;code&gt;moveChunk&lt;/code&gt; must drain buffered writes during the catch-up phase before entering the critical section. Scheduling migrations during off-peak hours lowers the incoming write rate on the donor, allowing catch-up to converge without repeated aborts.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Blind &lt;code&gt;clearJumboFlag&lt;/code&gt; on oversized chunk&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Balancer triggers &lt;code&gt;moveChunk&lt;/code&gt;, saturates cross-AZ I/O, aborts (&lt;code&gt;chunk too big to move&lt;/code&gt;), re-flags &lt;code&gt;jumbo&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;config.changelog&lt;/code&gt; &lt;code&gt;moveChunk.from&lt;/code&gt; errors; rising network TX/RX&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Split chunk first (&lt;code&gt;splitFind&lt;/code&gt;) or verify deletes shrank it below &lt;code&gt;chunkSize&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;refineCollectionShardKey&lt;/code&gt; without index&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Command fails immediately; shard key remains un-splittable&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Command error &lt;code&gt;IndexNotFound&lt;/code&gt; on &lt;code&gt;mongos&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Build compound index with &lt;code&gt;commitQuorum: &quot;majority&quot;&lt;/code&gt; first&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;24/7 balancing under peak writes&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;moveChunk&lt;/code&gt; cloning and catch-up compete with live writes for WiredTiger dirty cache and EBS IOPS&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;WiredTiger dirty % approaching 20%; rising p99 write latency&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set &lt;code&gt;activeWindow&lt;/code&gt; in &lt;code&gt;config.settings&lt;/code&gt; to off-peak hours&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Catch-up non-convergence&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Donor write rate exceeds recipient oplog apply speed; migration aborts repeatedly&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;config.changelog&lt;/code&gt; shows repeated &lt;code&gt;moveChunk.from&lt;/code&gt; aborts&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Throttle bulk ingest or move balancer window to low-write hours&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Lowering &lt;code&gt;chunkSize&lt;/code&gt; globally on live cluster&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Smaller &lt;code&gt;chunkSize&lt;/code&gt; multiplies chunk count and config metadata overhead, triggering migration storms&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Surge in &lt;code&gt;config.chunks&lt;/code&gt; count and CSRS memory usage&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep default &lt;code&gt;chunkSize&lt;/code&gt; (64 MB on 4.4/5.0, 128 MB on 6.0+)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unthrottled range deletion on large chunks&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Donor purges orphaned docs rapidly, causing dirty-cache stalls and secondary replication lag&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Replication lag spike after &lt;code&gt;moveChunk&lt;/code&gt; commit&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set &lt;code&gt;rangeDeleterBatchSize&lt;/code&gt; and &lt;code&gt;rangeDeleterBatchDelayMS&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;5.0+ script querying &lt;code&gt;config.chunks&lt;/code&gt; by &lt;code&gt;ns&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Returns &lt;code&gt;0&lt;/code&gt; jumbo chunks because 5.0+ keys &lt;code&gt;config.chunks&lt;/code&gt; by &lt;code&gt;uuid&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;sh.status()&lt;/code&gt; shows jumbo chunks while script reports &lt;code&gt;0&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Join &lt;code&gt;config.collections&lt;/code&gt; &lt;code&gt;_id&lt;/code&gt; to &lt;code&gt;uuid&lt;/code&gt; before querying &lt;code&gt;config.chunks&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-30-mongodb-jumbo-chunks-balancer-migrations-6-failures.svg&quot; alt=&quot;Failure paths during jumbo chunk remediation and post-migration cleanup&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;april-2026-update&quot;&gt;April 2026 Update&lt;/h2&gt;
&lt;p&gt;MongoDB 6.0, 7.0, and 8.0 changed how sharded clusters split, merge, and reshard data, making legacy 4.4/5.0 runbooks partially obsolete:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;128 MB default range size and removal of auto-splitting (MongoDB 6.0.3+):&lt;/strong&gt; The default range size is 128 MB, and starting in MongoDB 6.0.3 automatic chunk splitting is no longer performed (&lt;code&gt;sh.enableAutoSplit()&lt;/code&gt; and &lt;code&gt;sh.disableAutoSplit()&lt;/code&gt; exist only as no-ops for backward compatibility). The CSRS balancer distributes data by shard data size rather than chunk count, so ranges can exceed 128 MB without penalty as long as shards remain balanced.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Collection defragmentation and automatic merging (MongoDB 6.0–8.0):&lt;/strong&gt; Operators can enable per-collection defragmentation with &lt;code&gt;configureCollectionBalancing&lt;/code&gt; (&lt;code&gt;defragmentCollection: true&lt;/code&gt;) and monitor progress via &lt;code&gt;balancerCollectionStatus&lt;/code&gt;. In MongoDB 7.0 and 8.0, automatic chunk merging keeps metadata fragmentation low without manual intervention.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Same-key resharding and early commit (MongoDB 8.0):&lt;/strong&gt; &lt;code&gt;reshardCollection&lt;/code&gt; in MongoDB 8.0 supports resharding to the &lt;em&gt;same&lt;/em&gt; shard key (&lt;code&gt;forceRedistribution&lt;/code&gt;) to redistribute skewed data quickly across newly added shards, and &lt;code&gt;commitReshardCollection&lt;/code&gt; allows operators to force an in-progress resharding operation to enter its write-blocking commit phase immediately.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Separate split lag from cardinality exhaustion before acting:&lt;/strong&gt; Use &lt;code&gt;splitFind&lt;/code&gt; when distinct shard key values exist inside a jumbo chunk; use &lt;code&gt;refineCollectionShardKey&lt;/code&gt; (4.4+) or &lt;code&gt;reshardCollection&lt;/code&gt; (5.0+) when every document shares the same key.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never run &lt;code&gt;clearJumboFlag&lt;/code&gt; blindly:&lt;/strong&gt; Clearing the flag on an oversized chunk forces the CSRS balancer into an abort loop that wastes cross-AZ bandwidth and IOPS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Query &lt;code&gt;config.chunks&lt;/code&gt; by &lt;code&gt;uuid&lt;/code&gt; on MongoDB 5.0+:&lt;/strong&gt; Resolve the collection UUID from &lt;code&gt;config.collections&lt;/code&gt; first, because &lt;code&gt;ns&lt;/code&gt; was removed from &lt;code&gt;config.chunks&lt;/code&gt; in MongoDB 5.0.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bound balancing and range deletion to protect p99 latency:&lt;/strong&gt; Configure &lt;code&gt;activeWindow&lt;/code&gt; in &lt;code&gt;config.settings&lt;/code&gt; and tune &lt;code&gt;rangeDeleterBatchSize&lt;/code&gt; / &lt;code&gt;rangeDeleterBatchDelayMS&lt;/code&gt; so background data movement never pushes WiredTiger toward its 20% dirty-cache stall threshold.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Account for MongoDB 6.0.3+ behavior:&lt;/strong&gt; Modern clusters default to 128 MB ranges, disable auto-splitting, and balance by shard data size rather than chunk count.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Identify whether your sharded MongoDB cluster has &lt;code&gt;jumbo: true&lt;/code&gt; chunks stalling the CSRS balancer and pinning writes onto a single shard.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Halt the balancer, inspect &lt;code&gt;config.chunks&lt;/code&gt; via &lt;code&gt;config.collections&lt;/code&gt; UUID lookup, refine low-cardinality shard keys where needed, split divisible chunks with &lt;code&gt;sh.splitFind()&lt;/code&gt;, and clear flags with &lt;code&gt;clearJumboFlag&lt;/code&gt; using &lt;code&gt;bounds&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Confirm &lt;code&gt;config.chunks.countDocuments({ jumbo: true })&lt;/code&gt; returns &lt;code&gt;0&lt;/code&gt; and verify balanced data distribution via &lt;code&gt;sh.status()&lt;/code&gt; or &lt;code&gt;$shardedDataDistribution&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Add an alert on &lt;code&gt;config.chunks&lt;/code&gt; (&lt;code&gt;jumbo: true&lt;/code&gt;) and &lt;code&gt;config.changelog&lt;/code&gt; (&lt;code&gt;moveChunk.from&lt;/code&gt; errors), and configure an off-peak &lt;code&gt;activeWindow&lt;/code&gt; in &lt;code&gt;config.settings&lt;/code&gt; this week.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>Choosing a MongoDB Shard Key: Range, Hashed, Zones, Cardinality, and Hot Shards</title><link>https://rajivonai.com/blog/2026-05-31-shard-keys/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-31-shard-keys/</guid><description>How monotonically increasing shard keys create catastrophic single-shard write hotspots, and how to design compound hashed shard keys to balance writes and query targeting.</description><pubDate>Sat, 29 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Adding shards to a MongoDB cluster does not scale write throughput if every insert routes to the &lt;code&gt;MaxKey&lt;/code&gt; chunk on a single shard; an unexamined shard key turns a distributed cluster into a single-primary bottleneck.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Monotonic keys (&lt;code&gt;_id: 1&lt;/code&gt;, timestamps, counters) send 100% of inserts to one shard, while pure hashed keys (&lt;code&gt;_id: &quot;hashed&quot;&lt;/code&gt;) scatter-gather tenant reads across every shard (&lt;code&gt;SHARD_MERGE&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Use a compound hashed shard key (&lt;code&gt;{ tenant_id: 1, _id: &quot;hashed&quot; }&lt;/code&gt;) so reads route to &lt;code&gt;SINGLE_SHARD&lt;/code&gt; and writes hash evenly across chunks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Online resharding (&lt;code&gt;sh.reshardCollection&lt;/code&gt;) requires &lt;code&gt;((collection_storage_size + index_size) * 2) / shard_count&lt;/code&gt; free disk per shard and a brief write-blocking commit phase.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-31-shard-keys.svg&quot; alt=&quot;Monotonic shard keys route all writes to one shard; compound hashed keys balance writes and keep reads targeted&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;When a single MongoDB replica set hits vertical limits on CPU, WiredTiger working-set memory, or EBS IOPS, platform teams scale out to a sharded cluster: multiple replica-set shards, a 3-node Config Server Replica Set (CSRS), and distributed &lt;code&gt;mongos&lt;/code&gt; query routers. Because every document already has a unique &lt;code&gt;_id&lt;/code&gt; (defaulting to a 12-byte &lt;code&gt;ObjectId&lt;/code&gt;), teams frequently enable sharding with &lt;code&gt;{ _id: 1 }&lt;/code&gt; as the range shard key.&lt;/p&gt;
&lt;p&gt;In low-volume staging environments, &lt;code&gt;{ _id: 1 }&lt;/code&gt; appears to work. Under sustained production ingestion, however, every newly generated &lt;code&gt;ObjectId&lt;/code&gt; starts with a 4-byte Unix epoch timestamp that increases monotonically. Instead of distributing writes across all shards, the cluster routes every insert to the single shard holding the upper-bound chunk &lt;code&gt;[lastSplit, MaxKey)&lt;/code&gt;. The cluster may have eight or sixteen shards provisioned, yet write throughput remains bounded by the single primary node owning that upper chunk.&lt;/p&gt;
&lt;p&gt;Compound and hashed shard keys solve this write hotspot, but they introduce a second failure mode if designed without query-routing telemetry: scatter-gather reads (&lt;code&gt;SHARD_MERGE&lt;/code&gt;) that multiply &lt;code&gt;mongos&lt;/code&gt; fan-out by the number of shards in the cluster. Choosing a shard key therefore requires balancing write distribution, targeted read routing, chunk divisibility, and future resharding cost.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Shard key design is governed by three hard mathematical and operational constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Monotonic range convergence:&lt;/strong&gt; MongoDB partitions ranged shard keys into contiguous intervals &lt;code&gt;[MinKey, ..., MaxKey]&lt;/code&gt;. Because new &lt;code&gt;ObjectId&lt;/code&gt;s or timestamps always exceed prior values, 100% of inserts hit the single shard owning the &lt;code&gt;MaxKey&lt;/code&gt; chunk, saturating its CPU, EBS IOPS, and WiredTiger write tickets while peer shards sit idle.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hashed key space vs. range order:&lt;/strong&gt; Hashed sharding computes an MD5-based 64-bit signed integer hash across &lt;code&gt;[-2^63, 2^63 - 1]&lt;/code&gt;. While that uniformly scatters monotonic inserts, it destroys range ordering and forces &lt;code&gt;mongos&lt;/code&gt; to broadcast queries that omit the hashed field (&lt;code&gt;SHARD_MERGE&lt;/code&gt; scatter-gather across all shards).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hashed index constraints:&lt;/strong&gt; Within a compound shard key (supported since MongoDB 4.4), at most &lt;strong&gt;one&lt;/strong&gt; field can be &lt;code&gt;&quot;hashed&quot;&lt;/code&gt;, hashed indexes cannot enforce &lt;code&gt;unique: true&lt;/code&gt;, and shard key fields cannot contain arrays (multikey indexes are disallowed).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Resharding storage headroom:&lt;/strong&gt; Replacing a bad shard key online via &lt;code&gt;sh.reshardCollection()&lt;/code&gt; (5.0+) clones the collection and its indexes in parallel. MongoDB’s documented per-shard free disk requirement is &lt;code&gt;((collection_storage_size + index_size) * 2) / shard_count&lt;/code&gt; (for example, a 2 TB collection with 400 GB of indexes across 4 shards requires &lt;code&gt;((2 TB + 400 GB) * 2) / 4 = 1.2 TB&lt;/code&gt; free per shard).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-31-shard-keys-2-constraints.svg&quot; alt=&quot;Hash key space, resharding disk headroom, and hashed index limits constrain shard key design&quot;&gt;&lt;/p&gt;
&lt;p&gt;The core architectural question is: &lt;strong&gt;How do platform engineers design or migrate shard keys to eliminate monotonic write hotspots while keeping high-frequency application reads targeted to a single shard?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Four shard key patterns cover nearly all production sharding workloads, each with explicit failure boundaries:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Strategy&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Key Example&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Gets Right&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Monotonic Range&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;{ _id: 1 }&lt;/code&gt; or &lt;code&gt;{ createdAt: 1 }&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Efficient range scans on time or ID&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;100% of new inserts hit the &lt;code&gt;MaxKey&lt;/code&gt; shard&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reject for write-heavy ingest&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Pure Hashed&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;{ _id: &quot;hashed&quot; }&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Uniform write distribution across all shards&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Tenant or entity reads trigger &lt;code&gt;SHARD_MERGE&lt;/code&gt; across all shards&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use only for point-lookup by &lt;code&gt;_id&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Compound Hashed (4.4+)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;{ tenant_id: 1, _id: &quot;hashed&quot; }&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Targeted &lt;code&gt;SINGLE_SHARD&lt;/code&gt; prefix reads + hashed write spread&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Queries omitting &lt;code&gt;tenant_id&lt;/code&gt; still scatter-gather&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Recommended default&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Zone Sharding&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;{ region: 1, _id: &quot;hashed&quot; }&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pins data to regional shards for compliance or latency&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Overlapping ranges or skewed regions create hot zones&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use when residency is required&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Pure hashed sharding is tempting because it fixes write hotspots immediately, but in multi-tenant systems where reads filter by &lt;code&gt;tenant_id&lt;/code&gt; rather than &lt;code&gt;_id&lt;/code&gt;, every read fans out to every shard. Conversely, range sharding on &lt;code&gt;{ tenant_id: 1 }&lt;/code&gt; alone fails when one large tenant exceeds the chunk threshold and creates an indivisible jumbo chunk. Compound hashed sharding solves both: &lt;code&gt;tenant_id&lt;/code&gt; isolates reads, and &lt;code&gt;_id: &quot;hashed&quot;&lt;/code&gt; spreads writes across chunks.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-31-shard-keys-3-options.svg&quot; alt=&quot;Compare range, pure hashed, compound hashed, and zone-sharded keys&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-shard-key-and-routing-architecture&quot;&gt;The Shard Key and Routing Architecture&lt;/h2&gt;
&lt;p&gt;Understanding how &lt;code&gt;mongos&lt;/code&gt; and the CSRS route queries explains why shard key order and metadata caching directly determine cluster latency.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-31-shard-keys-4-architecture.svg&quot; alt=&quot;mongos caches CSRS chunk metadata and refreshes on StaleConfigException&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-csrs-metadata-catalog-and-mongos-routing-cache&quot;&gt;1. CSRS Metadata Catalog and &lt;code&gt;mongos&lt;/code&gt; Routing Cache&lt;/h3&gt;
&lt;p&gt;The Config Server Replica Set stores cluster routing metadata in the &lt;code&gt;config&lt;/code&gt; database (&lt;code&gt;config.shards&lt;/code&gt;, &lt;code&gt;config.databases&lt;/code&gt;, &lt;code&gt;config.collections&lt;/code&gt;, and &lt;code&gt;config.chunks&lt;/code&gt;). Each &lt;code&gt;mongos&lt;/code&gt; instance caches this routing table in memory and serves client connections using that local map:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Targeted routing (&lt;code&gt;SINGLE_SHARD&lt;/code&gt;):&lt;/strong&gt; When a query includes the shard key or its leading prefix (such as &lt;code&gt;tenant_id&lt;/code&gt;), &lt;code&gt;mongos&lt;/code&gt; binary-searches its local chunk cache and dispatches the operation only to the shard(s) owning matching chunks. This keeps connection fan-out constant regardless of how many shards the cluster contains.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scatter-gather (&lt;code&gt;SHARD_MERGE&lt;/code&gt;):&lt;/strong&gt; When a query omits the shard key prefix—or uses a negated operator like &lt;code&gt;$ne&lt;/code&gt; or &lt;code&gt;$nin&lt;/code&gt;—&lt;code&gt;mongos&lt;/code&gt; cannot prune shards. It broadcasts the query to every shard in the cluster, waits for the slowest shard to respond, and merges or sorts the streams in the &lt;code&gt;SHARD_MERGE&lt;/code&gt; stage. If the query also specifies a &lt;code&gt;sort()&lt;/code&gt; without an index or includes a &lt;code&gt;skip()&lt;/code&gt;, &lt;code&gt;mongos&lt;/code&gt; must buffer and sort results across all shards before returning the first batch.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stale cache recovery (&lt;code&gt;StaleConfigException&lt;/code&gt;):&lt;/strong&gt; After a chunk migration commits to the CSRS, the collection’s metadata version increments. A &lt;code&gt;mongos&lt;/code&gt; holding a cached older version routes to the former owner shard, which rejects the operation with &lt;code&gt;StaleConfigException&lt;/code&gt;. The &lt;code&gt;mongos&lt;/code&gt; router then refreshes its routing table from the CSRS primary (or secondaries) and retries the operation transparently. Under normal migration rates this is invisible, but if the CSRS is starved for IOPS, routing table refreshes add tail-latency jitter across all &lt;code&gt;mongos&lt;/code&gt; instances.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-compound-hashed-shard-keys-and-pre-splitting-mongodb-44&quot;&gt;2. Compound Hashed Shard Keys and Pre-Splitting (MongoDB 4.4+)&lt;/h3&gt;
&lt;p&gt;Enable compound hashed sharding by placing the dominant equality predicate first and the high-cardinality hashed field second. When sharding an empty collection with a hashed field, specify &lt;code&gt;numInitialChunks&lt;/code&gt; so MongoDB pre-creates and distributes chunks across all shards immediately rather than starting with a single chunk on the primary shard:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sh.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;shardCollection&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;telemetry.events&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { tenant_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, _id: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;hashed&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  false&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { numInitialChunks: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8192&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Any query filtering by &lt;code&gt;{ tenant_id: &quot;ORG_4410&quot; }&lt;/code&gt; targets only the chunks belonging to that tenant, while inserts for that tenant hash across multiple chunks instead of piling onto one &lt;code&gt;MaxKey&lt;/code&gt; boundary. Note that &lt;code&gt;numInitialChunks&lt;/code&gt; applies only when the collection is empty at &lt;code&gt;shardCollection&lt;/code&gt; time; for populated collections, MongoDB splits and balances ranges incrementally or during &lt;code&gt;reshardCollection&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;3-zone-sharding-mechanics-for-residency-and-locality&quot;&gt;3. Zone Sharding Mechanics for Residency and Locality&lt;/h3&gt;
&lt;p&gt;When regulatory compliance or latency requires pinning subsets of data to specific shards, Zone Sharding associates shard tags with contiguous key ranges via &lt;code&gt;sh.addShardToZone()&lt;/code&gt; and &lt;code&gt;sh.updateZoneKeyRange()&lt;/code&gt;. Because zone bounds are evaluated by the CSRS balancer against the shard key prefix, the residency attribute (such as &lt;code&gt;region&lt;/code&gt; or &lt;code&gt;country&lt;/code&gt;) must be the &lt;strong&gt;leading&lt;/strong&gt; field in the shard key (&lt;code&gt;{ region: 1, _id: &quot;hashed&quot; }&lt;/code&gt;). Leaving gaps between zone ranges or defining overlapping bounds causes the balancer to skip migrations or fail to converge.&lt;/p&gt;
&lt;h3 id=&quot;3-auditing-write-distribution-and-query-targeting-mongosh&quot;&gt;3. Auditing Write Distribution and Query Targeting (&lt;code&gt;mongosh&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Check insert distribution across shard primaries using &lt;code&gt;serverStatus().opcounters.insert&lt;/code&gt;, and verify read targeting with &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Verify whether a candidate query is targeted or scatter-gather&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; exp&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.events.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ tenant_id: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ORG_4410&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, status: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;PROCESSED&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;explain&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;executionStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; stage&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; exp.queryPlanner.winningPlan.stage;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Root Stage: &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stage); &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Expect SINGLE_SHARD; SHARD_MERGE indicates scatter-gather&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;4-version-safe-shard-key-evaluator-pymongo&quot;&gt;4. Version-Safe Shard Key Evaluator (&lt;code&gt;pymongo&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Because &lt;code&gt;config.chunks&lt;/code&gt; switched from &lt;code&gt;ns&lt;/code&gt; to &lt;code&gt;uuid&lt;/code&gt; in MongoDB 5.0, the evaluator resolves the collection &lt;code&gt;uuid&lt;/code&gt; from &lt;code&gt;config.collections&lt;/code&gt; first (falling back to &lt;code&gt;ns&lt;/code&gt; on 4.4) and checks &lt;code&gt;explain()&lt;/code&gt; stages:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env python3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse, sys&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; bson.max_key &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MaxKey&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; evaluate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(client, namespace):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cfg &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.config&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    db_name, coll_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; namespace.split(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    coll_meta &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cfg.collections.find_one({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: namespace})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; coll_meta &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;or&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; coll_meta.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;dropped&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sys.exit(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Namespace &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;namespace&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; is not sharded.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    key &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; coll_meta[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    match &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;uuid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: coll_meta[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;uuid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]} &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;uuid&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; coll_meta &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ns&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: namespace}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    dist &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(cfg.chunks.aggregate([{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$match&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: match}, {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$group&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$shard&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;n&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$sum&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}}}]))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; row &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dist:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Shard &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;row[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;_id&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;row[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;n&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; chunks&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    lead &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; next&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;iter&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(key))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    max_chunk &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cfg.chunks.find_one({&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;match, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;max.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;lead&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: MaxKey()})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; max_chunk:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;MaxKey chunk resides on: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;max_chunk[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;shard&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    sample &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client[db_name][coll_name].find_one()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sample &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; lead &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sample:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        stage &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client[db_name][coll_name].find({lead: sample[lead]}).explain()[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;queryPlanner&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;][&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;winningPlan&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;][&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;stage&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Targeted query stage (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;lead&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;): &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;stage&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; __name__&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse.ArgumentParser()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--mongos-uri&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;required&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    p.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--ns&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;required&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    a &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; p.parse_args()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    evaluate(MongoClient(a.mongos_uri, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;serverSelectionTimeoutMS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), a.ns)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Replacing a live shard key without downtime requires four gated phases so resharding never exhausts disk space or breaks query routing.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-31-shard-keys-5-flow.svg&quot; alt=&quot;Four-phase workflow to validate and reshard to a compound hashed key&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;phase-1-sample-workload-and-validate-candidate-key&quot;&gt;Phase 1: Sample Workload and Validate Candidate Key&lt;/h3&gt;
&lt;p&gt;On MongoDB 7.0+, enable query sampling via &lt;code&gt;configureQueryAnalyzer&lt;/code&gt; and run &lt;code&gt;analyzeShardKey&lt;/code&gt; to measure cardinality, monotonicity, and scatter-gather impact before touching production data:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;adminCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ configureQueryAnalyzer: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;telemetry.events&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, mode: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;full&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, samplesPerSecond: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getSiblingDB&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;telemetry&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).events.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;analyzeShardKey&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ tenant_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, _id: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;hashed&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; Confirm &lt;code&gt;keyCharacteristics.isUnique&lt;/code&gt; or cardinality/frequency metrics show no single value dominates chunk capacity, and &lt;code&gt;readWriteDistribution&lt;/code&gt; confirms top queries include the leading key.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; Disable sampling with &lt;code&gt;mode: &quot;off&quot;&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-2-verify-per-shard-disk-and-oplog-headroom&quot;&gt;Phase 2: Verify Per-Shard Disk and Oplog Headroom&lt;/h3&gt;
&lt;p&gt;Calculate &lt;code&gt;((collection_storage_size + index_size) * 2) / shard_count&lt;/code&gt; across all shards and ensure EBS volumes and oplog retention windows cover the full clone duration.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; Every shard has at least the calculated free space plus a safety buffer, and replication lag is near zero.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; Expand EBS &lt;code&gt;gp3&lt;/code&gt; volumes online before starting resharding.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-3-execute-online-resharding-shreshardcollection&quot;&gt;Phase 3: Execute Online Resharding (&lt;code&gt;sh.reshardCollection&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Run &lt;code&gt;sh.reshardCollection()&lt;/code&gt; (MongoDB 5.0+) to create the temporary collection, clone documents, and stream concurrent writes via oplog/change-stream sync:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sh.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;reshardCollection&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;telemetry.events&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, { tenant_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, _id: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;hashed&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; Monitor &lt;code&gt;db.getSiblingDB(&quot;admin&quot;).aggregate([{ $currentOp: { allUsers: true, localOps: false } }, { $match: { desc: &quot;ReshardingCoordinatorService&quot; } }])&lt;/code&gt; until cloning and oplog application converge.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; Before the final commit phase, &lt;code&gt;sh.abortReshardCollection(&quot;telemetry.events&quot;)&lt;/code&gt; cancels the operation and drops the temporary collection with zero impact on live traffic.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-4-verify-routing-and-clean-up&quot;&gt;Phase 4: Verify Routing and Clean Up&lt;/h3&gt;
&lt;p&gt;After the atomic metadata cutover completes, run &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; on primary application queries and inspect &lt;code&gt;opcounters.insert&lt;/code&gt; across all shard primaries.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Validation gate:&lt;/strong&gt; Critical reads show &lt;code&gt;SINGLE_SHARD&lt;/code&gt; instead of &lt;code&gt;SHARD_MERGE&lt;/code&gt;, and insert counts distribute across all shards.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback:&lt;/strong&gt; Reverting a committed shard key requires another &lt;code&gt;sh.reshardCollection()&lt;/code&gt; pass.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Documented MongoDB engine behaviors explain why shard key mistakes compound under production load:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Why monotonic keys defeat horizontal scaling:&lt;/strong&gt; In MongoDB’s chunk model, a ranged key only splits when a range grows beyond the chunk size threshold. Because every new &lt;code&gt;ObjectId&lt;/code&gt; or timestamp is strictly greater than all existing keys, all inserts land in the single &lt;code&gt;MaxKey&lt;/code&gt; chunk on one shard. Even as the balancer asynchronously splits and migrates older historical chunks to other shards, the active write edge remains pinned to one primary, capping cluster write throughput at a single node’s CPU and EBS IOPS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why pure hashed keys overload &lt;code&gt;mongos&lt;/code&gt;:&lt;/strong&gt; In a sharded cluster with &lt;code&gt;N&lt;/code&gt; shards, every scatter-gather query (&lt;code&gt;SHARD_MERGE&lt;/code&gt;) opens &lt;code&gt;N&lt;/code&gt; parallel cursors and forces &lt;code&gt;mongos&lt;/code&gt; to merge and sort results in memory. As &lt;code&gt;N&lt;/code&gt; grows, router connection pools and CPU scale linearly with &lt;code&gt;QPS * N&lt;/code&gt; instead of staying constant per query.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why compound hashed keys resolve the tradeoff:&lt;/strong&gt; Placing an equality-filtered field (&lt;code&gt;tenant_id: 1&lt;/code&gt;) ahead of a hashed suffix (&lt;code&gt;_id: &quot;hashed&quot;&lt;/code&gt;) restricts &lt;code&gt;mongos&lt;/code&gt; routing to only the shard(s) holding that tenant’s chunks (&lt;code&gt;SINGLE_SHARD&lt;/code&gt; when the tenant fits on one shard) while hashing writes across chunks to prevent single-chunk monotonic pileups.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why &lt;code&gt;refineCollectionShardKey&lt;/code&gt; is cheaper than &lt;code&gt;reshardCollection&lt;/code&gt; when the prefix is sound:&lt;/strong&gt; If a collection already uses &lt;code&gt;{ tenant_id: 1 }&lt;/code&gt; and reads already filter by &lt;code&gt;tenant_id&lt;/code&gt;, the prefix is valid—only its cardinality is insufficient. Building a compound index &lt;code&gt;{ tenant_id: 1, _id: 1 }&lt;/code&gt; and running &lt;code&gt;refineCollectionShardKey&lt;/code&gt; (4.4+) updates only the shard key metadata without cloning or rewriting documents. By contrast, replacing a monotonic prefix like &lt;code&gt;{ _id: 1 }&lt;/code&gt; requires &lt;code&gt;sh.reshardCollection()&lt;/code&gt; (5.0+), which rewrites the entire collection and its indexes across all shards.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Monotonic key (&lt;code&gt;{ _id: 1 }&lt;/code&gt;) on write-heavy ingest&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;All inserts hit &lt;code&gt;MaxKey&lt;/code&gt; chunk on one shard; peer shards sit idle&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;One shard at 100% CPU / high &lt;code&gt;opcounters.insert&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reshard to &lt;code&gt;{ prefix: 1, _id: &quot;hashed&quot; }&lt;/code&gt; or &lt;code&gt;{ _id: &quot;hashed&quot; }&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Inverted compound key (&lt;code&gt;{ _id: &quot;hashed&quot;, tenant_id: 1 }&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Leading hash scatters writes, but &lt;code&gt;tenant_id&lt;/code&gt; queries cannot use prefix&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;explain()&lt;/code&gt; shows &lt;code&gt;SHARD_MERGE&lt;/code&gt; on &lt;code&gt;tenant_id&lt;/code&gt; queries&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Put equality query predicate first, hashed field second&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Low-cardinality prefix without suffix (&lt;code&gt;{ tenant_id: 1 }&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;One large tenant exceeds chunk limit and becomes an un-splittable jumbo chunk&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;config.chunks&lt;/code&gt; shows &lt;code&gt;jumbo: true&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use &lt;code&gt;refineCollectionShardKey&lt;/code&gt; (4.4+) to append &lt;code&gt;_id&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Queries omitting shard key prefix&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;mongos&lt;/code&gt; broadcasts to all shards (&lt;code&gt;SHARD_MERGE&lt;/code&gt;), inflating router CPU and p99&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;SHARD_MERGE&lt;/code&gt; in &lt;code&gt;explain()&lt;/code&gt; / high &lt;code&gt;mongos&lt;/code&gt; fan-out&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Align shard key prefix with top read predicates&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Mutating shard key values (4.2+)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Updating shard key requires cross-shard two-phase commit (delete + insert)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Elevated lock time and transaction aborts on updates&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Choose immutable fields for the shard key&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Insufficient disk during &lt;code&gt;reshardCollection&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Temporary clone fills shard disk and aborts resharding&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Disk usage alerts or resharding failure in logs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Verify &lt;code&gt;((data + index) * 2) / shards&lt;/code&gt; free space first&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Overlapping or gap-ridden Zone Sharding rules&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chunks violate residency tags or balancer fails to converge&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;sh.status()&lt;/code&gt; shows misplaced chunks or balancer errors&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Validate contiguous zone ranges in staging first&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-31-shard-keys-6-failures.svg&quot; alt=&quot;Four common shard key failure modes in production&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;april-2026-update&quot;&gt;April 2026 Update&lt;/h2&gt;
&lt;p&gt;MongoDB 7.0 and 8.0 added concrete tooling to validate and change shard keys with less operational risk:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Workload analysis with &lt;code&gt;analyzeShardKey&lt;/code&gt; (MongoDB 7.0+):&lt;/strong&gt; After enabling query sampling via &lt;code&gt;configureQueryAnalyzer&lt;/code&gt;, &lt;code&gt;db.collection.analyzeShardKey()&lt;/code&gt; evaluates candidate keys against live read/write traffic, reporting key cardinality, frequency, monotonicity, and projected scatter-gather percentages before sharding.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Same-key redistribution in &lt;code&gt;reshardCollection&lt;/code&gt; (MongoDB 8.0):&lt;/strong&gt; MongoDB 8.0 allows &lt;code&gt;reshardCollection&lt;/code&gt; using the &lt;em&gt;same&lt;/em&gt; existing shard key (&lt;code&gt;forceRedistribution&lt;/code&gt;) to rebalance skewed data across newly added shards without waiting for incremental chunk migrations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Early resharding commit (&lt;code&gt;commitReshardCollection&lt;/code&gt;, MongoDB 8.0):&lt;/strong&gt; Operators can issue &lt;code&gt;sh.commitReshardCollection()&lt;/code&gt; to force an in-progress resharding operation to enter its write-blocking commit phase immediately when traffic is quiet.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Never use monotonic range keys (&lt;code&gt;{ _id: 1 }&lt;/code&gt;, timestamps) for high-velocity ingest:&lt;/strong&gt; Every new document lands on the &lt;code&gt;MaxKey&lt;/code&gt; shard, capping cluster write throughput at a single primary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Avoid pure hashed keys when reads filter by an entity ID:&lt;/strong&gt; Pure hashing fixes write skew at the cost of &lt;code&gt;SHARD_MERGE&lt;/code&gt; scatter-gather fan-out across every shard.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Default to compound hashed keys (&lt;code&gt;{ partition_key: 1, _id: &quot;hashed&quot; }&lt;/code&gt;):&lt;/strong&gt; Put the dominant equality read predicate first and the hashed field second, respecting the one-hashed-field limit and non-unique constraint.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Size disk before running &lt;code&gt;sh.reshardCollection()&lt;/code&gt;:&lt;/strong&gt; Confirm every shard has at least &lt;code&gt;((collection_storage_size + index_size) * 2) / shard_count&lt;/code&gt; free space.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Validate with &lt;code&gt;analyzeShardKey&lt;/code&gt; (7.0+) and &lt;code&gt;explain()&lt;/code&gt;:&lt;/strong&gt; Verify &lt;code&gt;SINGLE_SHARD&lt;/code&gt; execution plans and balanced &lt;code&gt;opcounters.insert&lt;/code&gt; after every change.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Identify whether your sharded collection suffers from a &lt;code&gt;MaxKey&lt;/code&gt; write hotspot or &lt;code&gt;SHARD_MERGE&lt;/code&gt; scatter-gather reads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Design a compound hashed shard key (&lt;code&gt;{ tenant_id: 1, _id: &quot;hashed&quot; }&lt;/code&gt;) aligned with your primary query predicate and transition via &lt;code&gt;sh.reshardCollection()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Compare &lt;code&gt;serverStatus().opcounters.insert&lt;/code&gt; across all shard primaries and confirm &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; reports &lt;code&gt;SINGLE_SHARD&lt;/code&gt; on top read queries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; On MongoDB 7.0+, enable &lt;code&gt;configureQueryAnalyzer&lt;/code&gt; and run &lt;code&gt;db.collection.analyzeShardKey()&lt;/code&gt; against your busiest sharded collection this week.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>Replication Lag, Oplog Windows, and Multi-TB Initial Sync on EC2</title><link>https://rajivonai.com/blog/2026-06-04-replication-lag-oplog-windows/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-04-replication-lag-oplog-windows/</guid><description>How to survive oplog rollover and multi-TB initial syncs on self-managed MongoDB using dynamic oplog resizing, flow control, and EBS snapshots.</description><pubDate>Thu, 27 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;When a secondary node restarts for routine maintenance and falls behind, an undersized oplog window turns a routine reboot into a multi-day initial sync that imperils cluster availability.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; High write bursts shrink a fixed-size &lt;code&gt;local.oplog.rs&lt;/code&gt; retention window to minutes; a secondary offline longer than that window falls off the oplog and triggers a multi-day logical initial sync that evicts the primary’s WiredTiger cache.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Size the oplog for at least 48–72 hours of peak write velocity using &lt;code&gt;replSetResizeOplog&lt;/code&gt; (and &lt;code&gt;minRetentionHours&lt;/code&gt; in 4.4+), enable Flow Control, and hydrate failed multi-TB nodes from block-level AWS EBS snapshots.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; &lt;code&gt;replSetResizeOplog&lt;/code&gt; does not persist across restarts without updating &lt;code&gt;mongod.conf&lt;/code&gt;, and &lt;code&gt;minRetentionHours&lt;/code&gt; can grow the oplog past its &lt;code&gt;size&lt;/code&gt; ceiling and fill the EBS volume during write storms.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-04-replication-lag-oplog-windows.svg&quot; alt=&quot;Size the oplog for 72h and hydrate multi-TB nodes from snapshots&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Operating self-managed MongoDB replica sets on AWS EC2 requires maintaining multi-terabyte datasets across memory-optimized instances backed by EBS gp3 storage. In analytics, telemetry ingest, and high-velocity transactional workloads, primary nodes sustain thousands of write operations per second, generating gigabytes of mutation history per hour in &lt;code&gt;local.oplog.rs&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Routine infrastructure maintenance—kernel patching, instance type upgrades, security updates, and storage volume modifications—requires taking individual secondary nodes offline sequentially. Under normal conditions, a secondary restarts, reconnects to the replica set, fetches missed operations from the primary’s oplog, and catches up within minutes.&lt;/p&gt;
&lt;p&gt;During peak traffic or batch backfill events, however, primary write throughput outpaces the retention capacity of a small default oplog. If a secondary stays disconnected longer than the primary’s active oplog window, logical replication cannot resume because the primary has already overwritten the secondary’s last applied optime (&lt;code&gt;lastAppliedOpTime&lt;/code&gt;).&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;When a secondary node reconnects, it requests all oplog entries newer than its &lt;code&gt;lastAppliedOpTime&lt;/code&gt;. If the primary has already truncated those entries, the cluster hits four compounding constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Fixed-Size Oplog Compression Under Peak Writes:&lt;/strong&gt; Because &lt;code&gt;local.oplog.rs&lt;/code&gt; is a capped collection managed as a WiredTiger B-tree, a fixed size ceiling (such as 50 GB) retains time inversely proportional to write throughput. At 1.5 MB/s of oplog generation, 50 GB holds roughly 9.2 hours; during a 15 MB/s batch surge, that same 50 GB overwrites in 55 minutes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;State Transition to &lt;code&gt;RECOVERING&lt;/code&gt;:&lt;/strong&gt; A reconnecting secondary whose &lt;code&gt;lastAppliedOpTime&lt;/code&gt; predates the primary’s oldest oplog entry transitions to &lt;code&gt;RECOVERING&lt;/code&gt;. It stops serving read traffic and cannot participate in election majorities, reducing a 3-node replica set’s fault tolerance to zero if another node fails.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multi-Day Logical Initial Sync:&lt;/strong&gt; Native logical recovery drops all local database files, streams every document from a sync source via &lt;code&gt;repl.network.getmores&lt;/code&gt;, and rebuilds every B-tree index from scratch using external merge sorts. On multi-terabyte collections, this saturates local CPU and EBS I/O for days.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Primary Cache Eviction and IOPS Saturation:&lt;/strong&gt; Serving sequential collection scans to a syncing node consumes primary WiredTiger read tickets, displaces the hot working set from the primary’s cache, and competes with client queries for EBS gp3 bandwidth (125 MB/s baseline unless provisioned higher).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-04-replication-lag-oplog-windows-2-constraints.svg&quot; alt=&quot;Oplog window shrinks inversely with write throughput&quot;&gt;&lt;/p&gt;
&lt;p&gt;The core architectural question is: &lt;strong&gt;How can database platform engineers size replica set oplog retention against peak write velocity, bound replication lag with Flow Control, and recover multi-terabyte nodes in minutes using block-level EBS snapshots instead of multi-day logical syncs?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;When a multi-TB secondary falls behind or needs replacement, teams typically evaluate three recovery paths and two oplog sizing strategies:&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Primary Load&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Index Rebuild&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Availability / License Constraint&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Logical Initial Sync&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High (full collection scans)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Full CPU/disk sort&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Built-in; 24h transient retry in v4.4+&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt; for multi-TB nodes; degrades primary cache&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;File Copy Initial Sync&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Low (raw file copy)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;None&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;MongoDB 6.0+ Enterprise / Atlas only; 10m retry window&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Conditional&lt;/strong&gt;; use only on licensed Enterprise 6.0+&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;EBS Snapshot + Oplog&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;None for base data&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;None&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires snapshot optime inside primary oplog window&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen&lt;/strong&gt; for self-managed Community EC2&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Fixed &lt;code&gt;size&lt;/code&gt; vs. &lt;code&gt;minRetentionHours&lt;/code&gt; (MongoDB 4.4+):&lt;/strong&gt; A fixed &lt;code&gt;oplogSizeMB&lt;/code&gt; caps disk usage predictably but shrinks in hours during write spikes. Combining &lt;code&gt;size&lt;/code&gt; with &lt;code&gt;minRetentionHours&lt;/code&gt; guarantees a minimum time window (MongoDB honors whichever constraint requires more space), but introduces disk-full risk if an unbounded write storm forces the oplog to expand beyond free EBS capacity. For clusters with unpredictable batch bursts, set &lt;code&gt;size&lt;/code&gt; to the baseline 72-hour requirement and pair it with &lt;code&gt;minRetentionHours&lt;/code&gt; only when EBS volumes carry at least 30% free headroom.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Logical Initial Sync:&lt;/strong&gt; Even with Resumable Initial Sync (MongoDB 4.4+, which retries transient errors for 24 hours by default via &lt;code&gt;initialSyncTransientErrorRetryPeriodSeconds&lt;/code&gt;), logical sync still forces full table scans on the sync source and full index builds on the target. On multi-terabyte collections, index sorting alone can saturate local CPU and EBS IOPS for days, delaying the secondary’s return to &lt;code&gt;SECONDARY&lt;/code&gt; voting status.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;File Copy Initial Sync (MongoDB 6.0+ Enterprise):&lt;/strong&gt; Copies underlying WiredTiger files directly over the network without document-level scans or index rebuilds, but is unavailable on MongoDB Community Edition and operates with a shorter 10-minute transient error recovery window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Snapshot Target Selection (Primary vs. Secondary):&lt;/strong&gt; Taking EBS snapshots from the primary adds snapshot I/O overhead during peak writes and complicates rollback if an election occurs. Capturing snapshots from a healthy secondary—or a dedicated hidden &lt;code&gt;priority: 0&lt;/code&gt; member—isolates snapshot I/O from client traffic while preserving an identical WiredTiger checkpoint and journal state.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-04-replication-lag-oplog-windows-3-options.svg&quot; alt=&quot;Block snapshots avoid primary table scans during recovery&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-dynamic-oplog-and-snapshot-seeding-architecture&quot;&gt;The Dynamic Oplog and Snapshot Seeding Architecture&lt;/h2&gt;
&lt;p&gt;A resilient self-managed EC2 replication architecture pairs three mechanisms: mathematical oplog sizing, Flow Control write throttling, and block-level EBS snapshot hydration.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-04-replication-lag-oplog-windows-4-architecture.svg&quot; alt=&quot;Flow control and snapshots protect primary availability&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Client[&quot;Application Ingest — Write Traffic&quot;] --&gt; Primary[&quot;mongod Primary Node&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt; FlowEngine{&quot;Flow Control — 10s Default Lag&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    FlowEngine --&gt;|Lag Exceeds Target| Throttle[&quot;Limit Flow Control Tickets&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    FlowEngine --&gt;|Lag Within Margin| OplogBuffer[&quot;WiredTiger Oplog — local.oplog.rs&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Throttle --&gt; OplogBuffer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OplogBuffer --&gt;|Continuous Fetch| SecHealthy[&quot;Healthy Secondary — EBS gp3&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SecHealthy -.-&gt;|Periodic Snapshot| EBSSnap[&quot;Crash-Consistent EBS Snapshot&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    EBSSnap --&gt; RestoreVol[&quot;New gp3 Volume&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    RestoreVol --&gt; SecRecovered[&quot;Replacement Secondary&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SecRecovered --&gt;|Replay Delta| JoinCluster[&quot;Rejoin as SECONDARY&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-mathematical-modeling-of-the-oplog-retention-window&quot;&gt;1. Mathematical Modeling of the Oplog Retention Window&lt;/h3&gt;
&lt;p&gt;The time retained in &lt;code&gt;local.oplog.rs&lt;/code&gt; is the ratio of provisioned oplog storage to write throughput:&lt;/p&gt;
&lt;p&gt;$$T_{\text{window}} = \frac{S_{\text{oplog}}}{\text{Throughput}_{\text{write}}}$$&lt;/p&gt;
&lt;p&gt;Where $T_{\text{window}}$ is retention in seconds, $S_{\text{oplog}}$ is allocated oplog storage in MB, and $\text{Throughput}_{\text{write}}$ is the oplog generation rate in MB/s. To survive extended host maintenance or multi-hour batch incidents, size the oplog for &lt;strong&gt;48 to 72 hours of peak write volume&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;$$S_{\text{required}} = \text{PeakWriteRate}_{\text{MB/s}} \times 72\text{ hours} \times 3,600\text{ sec/hr}$$&lt;/p&gt;
&lt;p&gt;At a peak batch ingest rate of $15\text{ MB/s}$:&lt;/p&gt;
&lt;p&gt;$$S_{\text{required}} = 15 \times 72 \times 3,600 = 3,888,000\text{ MB} \approx 3.8\text{ TB}$$&lt;/p&gt;
&lt;p&gt;In MongoDB 3.6+, &lt;code&gt;replSetResizeOplog&lt;/code&gt; resizes the oplog online without restarting &lt;code&gt;mongod&lt;/code&gt;. In MongoDB 4.4+, you can also specify &lt;code&gt;minRetentionHours&lt;/code&gt;. Remember that &lt;code&gt;replSetResizeOplog&lt;/code&gt; updates runtime state only; you must also update &lt;code&gt;replication.oplogSizeMB&lt;/code&gt; and &lt;code&gt;storage.oplogMinRetentionHours&lt;/code&gt; in &lt;code&gt;mongod.conf&lt;/code&gt; so the setting survives restarts.&lt;/p&gt;
&lt;h3 id=&quot;2-flow-control-concurrency-throttling&quot;&gt;2. Flow Control Concurrency Throttling&lt;/h3&gt;
&lt;p&gt;Introduced in MongoDB 4.2 with &lt;code&gt;enableFlowControl: true&lt;/code&gt; by default, Flow Control bounds majority-committed replication lag using &lt;code&gt;flowControlTargetLagSeconds&lt;/code&gt; (default: &lt;strong&gt;10 seconds&lt;/strong&gt; across 4.2, 5.0, and 7.0+):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Secondaries report their optime progress to the primary via replication heartbeats.&lt;/li&gt;
&lt;li&gt;When majority-committed lag approaches &lt;code&gt;flowControlTargetLagSeconds&lt;/code&gt;, the primary limits the rate at which it issues &lt;strong&gt;flow control tickets&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Incoming writes on the primary must acquire a flow control ticket before taking locks and entering WiredTiger, pacing primary ingest to match what the majority of data-bearing secondaries can sustain.&lt;/li&gt;
&lt;li&gt;This is distinct from &lt;code&gt;wiredTigerConcurrentWriteTransactions&lt;/code&gt; (default 128 prior to 7.0; dynamically probed up to 128 in MongoDB 7.0+), which bounds concurrent storage engine transactions rather than replication-lag admission.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-wiredtiger-oplog-storage-and-tombstoning&quot;&gt;3. WiredTiger Oplog Storage and Tombstoning&lt;/h3&gt;
&lt;p&gt;WiredTiger stores &lt;code&gt;local.oplog.rs&lt;/code&gt; as a row-store B-tree rather than a pre-allocated contiguous ring file:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Keys:&lt;/strong&gt; Each entry is keyed by its cluster timestamp (&lt;code&gt;Timestamp(seconds, increment)&lt;/code&gt;) and term.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Trimmer Thread:&lt;/strong&gt; An internal oplog trimmer thread periodically deletes entries older than both the configured &lt;code&gt;size&lt;/code&gt; ceiling and &lt;code&gt;minRetentionHours&lt;/code&gt; floor.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Space Reclamation:&lt;/strong&gt; Shrinking an oversized oplog via &lt;code&gt;replSetResizeOplog&lt;/code&gt; truncates logical entries, but WiredTiger retains freed extent blocks internally for future reuse rather than returning them to the OS filesystem.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;diagnostic-telemetry-high-resolution-replication-forensics&quot;&gt;Diagnostic Telemetry: High-Resolution Replication Forensics&lt;/h2&gt;
&lt;p&gt;Inspect oplog capacity, secondary headroom, and active initial sync progress directly via &lt;code&gt;mongosh&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;1-oplog-window-and-lag-margin-audit&quot;&gt;1. Oplog Window and Lag Margin Audit&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Execute in mongosh against the primary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; localDB&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getSiblingDB&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;local&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rs.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; primary&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; status.members.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.stateStr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;PRIMARY&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;primary) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[CRITICAL] No PRIMARY elected in replica set!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; oplogColl&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; localDB.oplog.rs;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; oplogStats&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oplogColl.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;stats&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; firstEntry&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oplogColl.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ $natural: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;limit&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;next&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; lastEntry&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oplogColl.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ $natural: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;limit&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;next&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; windowSecs&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; lastEntry.ts.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getTime&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; firstEntry.ts.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getTime&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; windowHours&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (windowSecs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3600&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Configured Oplog Max   : &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (oplogStats.maxSize &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot; MB&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Allocated Oplog Storage: &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (oplogStats.storageSize &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot; MB&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Active Retention Window: &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; windowHours &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot; hours&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  status.members.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (m.stateStr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;SECONDARY&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; lagSecs&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ((primary.optimeDate.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getTime&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.optimeDate.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getTime&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; marginHours&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (windowHours &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (lagSecs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3600&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;      print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Member [&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;] Lag: &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; lagSecs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;s | Margin Left: &quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; marginHours &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;h&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;parseFloat&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(marginHours) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 4.0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;  [CRITICAL] Secondary is within 4 hours of falling off the oplog!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-monitoring-active-initial-sync-progress&quot;&gt;2. Monitoring Active Initial Sync Progress&lt;/h3&gt;
&lt;p&gt;When a member is in &lt;code&gt;STARTUP2&lt;/code&gt;, &lt;code&gt;rs.status().initialSyncStatus&lt;/code&gt; exposes progress counters:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Execute on the syncing secondary node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; syncStatus&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rs.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ initialSync: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }).initialSyncStatus;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (syncStatus) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  printjson&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    approxTotalBytesCopied: syncStatus.approxTotalBytesCopied,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    approxTotalDataSize: syncStatus.approxTotalDataSize,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    appliedOps: syncStatus.appliedOps,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    elapsedMillis: syncStatus.totalInitialSyncElapsedMillis,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    remainingEstimatedMillis: syncStatus.remainingInitialSyncEstimatedMillis,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    failedAttempts: syncStatus.failedInitialSyncAttempts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;} &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Node is not currently executing an initial sync.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;When recovering a failed multi-terabyte secondary or expanding oplog capacity across an active EC2 replica set, follow a five-step staged rollout with explicit validation gates:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-04-replication-lag-oplog-windows-5-flow.svg&quot; alt=&quot;Five-step snapshot hydration and oplog catch-up rollout&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Step 1 — Expand Oplog Online (Secondaries First, Then Primary):&lt;/strong&gt; Verify free EBS disk space first (&lt;code&gt;df -h&lt;/code&gt;), then run &lt;code&gt;replSetResizeOplog&lt;/code&gt; on each secondary before resizing the primary, and persist &lt;code&gt;replication.oplogSizeMB&lt;/code&gt; in &lt;code&gt;mongod.conf&lt;/code&gt;.
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;adminCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ replSetResizeOplog: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, size: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;512000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, minRetentionHours: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;48&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;strong&gt;Gate:&lt;/strong&gt; Run &lt;code&gt;rs.printReplicationInfo()&lt;/code&gt; and confirm the configured size and retention window on every member.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 2 — Capture a Crash-Consistent EBS Snapshot:&lt;/strong&gt; Snapshot a healthy, up-to-date secondary’s data volume rather than the primary. Because WiredTiger checkpoints run every 60 seconds and the journal records writes between checkpoints, an EBS snapshot of a single volume holding both data and journal is crash-consistent.
&lt;strong&gt;Rollback/Gate:&lt;/strong&gt; Record the snapshot creation timestamp and verify it is strictly newer than the primary’s oldest oplog entry. If the snapshot ages out of the oplog window before restore, discard it and take a new snapshot.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 3 — Provision and Attach Replacement gp3 Volume:&lt;/strong&gt; Create a new EBS gp3 volume from the snapshot, matching provisioned IOPS and throughput to the rest of the replica set. Warm-up or pre-initialize blocks if latency-sensitive workloads will hit it immediately.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 4 — Mount XFS and Start &lt;code&gt;mongod&lt;/code&gt;:&lt;/strong&gt; Mount with &lt;code&gt;noatime&lt;/code&gt;, enforce &lt;code&gt;mongod:mongod&lt;/code&gt; ownership, and start the daemon with its standard replica set configuration.
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; mount&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -o&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; noatime&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /dev/nvme1n1&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/lib/mongodb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; chown&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -R&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; mongod:mongod&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/lib/mongodb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step 5 — Verify Delta Catch-Up and &lt;code&gt;SECONDARY&lt;/code&gt; State:&lt;/strong&gt; WiredTiger replays its local journal to the last checkpoint, connects to the primary, and fetches only the oplog delta since the snapshot timestamp. Confirm &lt;code&gt;stateStr === &quot;SECONDARY&quot;&lt;/code&gt; and &lt;code&gt;optimeDate&lt;/code&gt; convergence in &lt;code&gt;rs.status()&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;automated-oplog-retention-monitor&quot;&gt;Automated Oplog Retention Monitor&lt;/h3&gt;
&lt;p&gt;The following Python script audits the active oplog window, flags secondaries approaching rollover, and optionally triggers &lt;code&gt;replSetResizeOplog&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env python3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;MongoDB Automated Oplog Retention Monitor &amp;#x26; Capacity Alerting&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Usage:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    python3 mongo_oplog_monitor.py --uri &quot;mongodb://admin:pass@primary:27017/?replicaSet=rs0&quot; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      --min-window-hours 48 --target-resize-gb 500&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sys&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo.errors &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PyMongoError&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; audit_oplog_capacity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(client, min_window_hours, target_resize_gb):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    admin_db &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.admin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    local_db &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.local&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        rs_status &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; admin_db.command(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;replSetGetStatus&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    except&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PyMongoError &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; e:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[FATAL] Failed to retrieve replSetGetStatus: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sys.exit(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    primary_member &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; next&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((m &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rs_status.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;members&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, []) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;stateStr&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;PRIMARY&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; primary_member:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[CRITICAL] No primary node identified in replica set!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sys.exit(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    oplog &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; local_db.oplog.rs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    first_entry &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oplog.find().sort([(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$natural&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)]).limit(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).next()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    last_entry &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oplog.find().sort([(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$natural&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)]).limit(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).next()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    window_secs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; last_entry[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ts&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;].time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; first_entry[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ts&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;].time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    window_hours &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; window_secs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3600.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Primary Node         : &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;primary_member.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;name&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Current Oplog Window : &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;window_hours&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.2f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; hours (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;window_secs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 86400.0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.2f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; days)&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rs_status.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;members&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, []):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;stateStr&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;SECONDARY&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            lag_secs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (primary_member[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;optimeDate&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;optimeDate&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]).total_seconds()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            margin_hours &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; window_hours &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (lag_secs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3600.0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;            print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Secondary [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;m.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;name&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;] -&gt; Lag: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;lag_secs&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.1f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;s | Headroom: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;margin_hours&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.2f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;h&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; margin_hours &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 4.0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;                print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;  [ALERT] Secondary &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;m.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;name&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; is near oplog rollover threshold!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; window_hours &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; min_window_hours:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;[WARNING] Oplog window (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;window_hours&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.2f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;h) is below policy (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;min_window_hours&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;h)!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; target_resize_gb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            target_mb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(target_resize_gb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                res &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; admin_db.command({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;replSetResizeOplog&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;size&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: target_mb})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;                print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[SUCCESS] Oplog dynamically resized: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;res&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            except&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PyMongoError &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pe:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;                print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[ERROR] Failed to resize oplog: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;pe&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;[OK] Oplog retention window provides adequate operational headroom.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse.ArgumentParser(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;description&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;MongoDB Oplog Capacity Monitor&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--uri&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;required&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;help&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;MongoDB Primary connection URI&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--min-window-hours&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;default&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;48.0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parser.add_argument(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;--target-resize-gb&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;default&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    args &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; parser.parse_args()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    client &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient(args.uri, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;serverSelectionTimeoutMS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    audit_oplog_capacity(client, args.min_window_hours, args.target_resize_gb)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; __name__&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    main()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The operational tradeoffs above follow directly from how MongoDB, WiredTiger, and AWS EBS behave under sustained write load (CARL B/C — derived from documented engine behavior, not personal benchmark claims):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Logical Sync vs. Primary Working Set:&lt;/strong&gt; Because WiredTiger uses an LRU-style cache eviction policy, sequential collection scans issued by a syncing secondary pull cold pages into the primary’s cache and evict hot transactional pages. On EBS gp3 volumes with finite provisioned IOPS and throughput, those concurrent sequential reads directly compete with application read/write I/O.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Flow Control Ticket Pacing:&lt;/strong&gt; When &lt;code&gt;enableFlowControl&lt;/code&gt; is active, &lt;code&gt;db.serverStatus().flowControl&lt;/code&gt; exposes &lt;code&gt;isLagged&lt;/code&gt;, &lt;code&gt;targetRateLimit&lt;/code&gt;, and &lt;code&gt;timeAcquiringMicros&lt;/code&gt;. Under sustained bulk loads where secondaries cannot keep up with primary index writes, Flow Control trades primary write latency for bounded replication lag, keeping secondaries within the oplog window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;EBS Snapshot Consistency:&lt;/strong&gt; Because WiredTiger flushes checkpoints every 60 seconds and writes all intervening mutations to the journal on the same volume, taking an EBS snapshot of a single-volume secondary captures a consistent recovery point without requiring &lt;code&gt;db.fsyncLock()&lt;/code&gt; on the primary.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-06-04-replication-lag-oplog-windows-6-failures.svg&quot; alt=&quot;Three failure modes that break oplog resizing and flow control&quot;&gt;&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;minRetentionHours&lt;/code&gt; Disk Exhaustion&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Oplog grows past configured &lt;code&gt;size&lt;/code&gt; MB to honor time floor, filling EBS volume to 100%&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;df -h&lt;/code&gt; and &lt;code&gt;local.oplog.rs&lt;/code&gt; &lt;code&gt;storageSize&lt;/code&gt; alerts&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Maintain 30%+ free EBS headroom; cap &lt;code&gt;minRetentionHours&lt;/code&gt; against peak write math&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Flow Control Partition Stall&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary loses contact with voting majority, assumes infinite lag, and clamps write tickets&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;db.serverStatus().flowControl.isLagged&lt;/code&gt; = true with healthy secondaries&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Verify replica set network health and election majority; tune &lt;code&gt;flowControlTargetLagSeconds&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Stale Snapshot Restore&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Restored EBS snapshot optime is older than primary’s oldest oplog entry&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Secondary enters &lt;code&gt;RECOVERING&lt;/code&gt; and starts &lt;code&gt;initialSyncStatus&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Ensure oplog exceeds snapshot age + restore time before booting replacement&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unpersisted &lt;code&gt;replSetResizeOplog&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Daemon restart reverts oplog size to &lt;code&gt;mongod.conf&lt;/code&gt; default&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;rs.printReplicationInfo()&lt;/code&gt; shrinks after reboot&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Update &lt;code&gt;replication.oplogSizeMB&lt;/code&gt; and &lt;code&gt;storage.oplogMinRetentionHours&lt;/code&gt; in &lt;code&gt;mongod.conf&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Multi-Volume Snapshot Skew&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Data and journal reside on separate EBS volumes snapshotted at different instants&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;WiredTiger fails journal recovery on boot&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep data and journal on a single EBS volume, or use &lt;code&gt;fsyncLock&lt;/code&gt; before multi-volume snapshots&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Secondary Apply Bottleneck&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Single-collection hot-key writes serialize oplog application on secondaries&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High &lt;code&gt;replWriterThreadCount&lt;/code&gt; queue wait with idle network&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Tune &lt;code&gt;replWriterThreadCount&lt;/code&gt; (default 16) and &lt;code&gt;replBatchLimitOperations&lt;/code&gt; (default 5000)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;april-2026-update&quot;&gt;April 2026 Update&lt;/h2&gt;
&lt;p&gt;Modern MongoDB releases (5.0, 6.0, 7.0, and 8.0) harden replication recovery and concurrency control:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;File Copy-Based Initial Sync (MongoDB 6.0+ Enterprise / Atlas):&lt;/strong&gt; Copies underlying WiredTiger files directly instead of scanning logical documents and rebuilding indexes, though it enforces a 10-minute transient error retry window rather than logical sync’s 24-hour window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Resumable Initial Sync (MongoDB 4.4+):&lt;/strong&gt; Pauses and resumes logical initial sync across transient network or metadata errors for up to 24 hours (&lt;code&gt;initialSyncTransientErrorRetryPeriodSeconds&lt;/code&gt;) instead of restarting from byte zero.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Smoothed Flow Control &amp;#x26; Dynamic Tickets (MongoDB 7.0+):&lt;/strong&gt; Flow Control uses deterministic smoothed lag tracking to avoid abrupt throttling cliffs, while WiredTiger replaces the static 128-ticket cap with dynamic throughput probing for read and write concurrency.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Size the oplog for peak batch velocity, not steady-state averages:&lt;/strong&gt; A 50 GB oplog that holds 9 hours at 1.5 MB/s overwrites in 55 minutes during a 15 MB/s surge.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pair &lt;code&gt;replSetResizeOplog&lt;/code&gt; with &lt;code&gt;mongod.conf&lt;/code&gt; updates and disk headroom:&lt;/strong&gt; Runtime resizes vanish on restart, and &lt;code&gt;minRetentionHours&lt;/code&gt; in 4.4+ will expand past &lt;code&gt;size&lt;/code&gt; MB during write storms.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never run multi-TB logical initial syncs on Community EC2 when EBS snapshots exist:&lt;/strong&gt; Block-level snapshot hydration restores a node by replaying only the oplog delta, avoiding primary cache eviction and multi-day index rebuilds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Monitor &lt;code&gt;db.serverStatus().flowControl&lt;/code&gt; alongside &lt;code&gt;rs.status()&lt;/code&gt;:&lt;/strong&gt; Flow Control defaults to a 10-second &lt;code&gt;flowControlTargetLagSeconds&lt;/code&gt; target in 4.2+ and will throttle primary writes if secondaries lag or majority connectivity breaks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Keep data and journal on the same EBS volume for crash-consistent snapshots:&lt;/strong&gt; Taking snapshots from a healthy secondary avoids primary I/O overhead and guarantees WiredTiger recovery without &lt;code&gt;fsyncLock&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; An undersized oplog retention window turns routine secondary maintenance into a multi-day logical initial sync that degrades primary cache and availability.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Size &lt;code&gt;local.oplog.rs&lt;/code&gt; to retain 48–72 hours of peak write volume (persisted in &lt;code&gt;mongod.conf&lt;/code&gt;) and standardize on single-volume EBS snapshots of healthy secondaries for multi-TB node recovery.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run &lt;code&gt;rs.printReplicationInfo()&lt;/code&gt; during peak write hours to confirm the active window exceeds 48 hours, and check &lt;code&gt;db.serverStatus().flowControl&lt;/code&gt; for unexpected throttling.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Deploy the &lt;code&gt;mongo_oplog_monitor.py&lt;/code&gt; audit script to your alerting pipeline, schedule automated secondary EBS snapshots, and test a snapshot-based secondary replacement in staging this week.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>How Google Cloud Spanner Scales Writes: Splits, Primary Keys, Hotspots, and PostgreSQL DBA Considerations</title><link>https://rajivonai.com/blog/2026-08-26-cloud-spanner-postgresql-write-scalability-dba-guide/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-08-26-cloud-spanner-postgresql-write-scalability-dba-guide/</guid><description>A deep dive into Cloud Spanner&apos;s distributed architecture, write scalability, the PostgreSQL interface, and operational realities for DBAs.</description><pubDate>Wed, 26 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A documented pattern in Spanner migrations is that teams move a write-heavy order-processing system off a vertically scaled PostgreSQL instance to escape write exhaustion — the existing database peaking at 90% CPU, with read replicas unable to relieve writer saturation — only to hit unexplained P99 latency spikes within weeks of launch, even while running well under Spanner’s theoretical throughput limits. The root cause is a fundamental architectural misunderstanding: treating Spanner like a traditional single-writer PostgreSQL database. This article unpacks how Google Cloud Spanner actually processes writes, what the PostgreSQL interface is (and isn’t), and how to avoid the architectural failures that catch senior DBAs by surprise.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Consider a realistic production workload: a write-heavy e-commerce order-processing system with a 15 TB active relational database, dominated by an &lt;code&gt;orders&lt;/code&gt; table.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Application&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;PostgreSQL Writer (db-main)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;CPU 85–95%&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Writes continue increasing (50K+ TPS)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The application is write-heavy (order creation, inventory decrements, payment state changes). Because standard PostgreSQL (and managed equivalents like Cloud SQL for PostgreSQL) relies on a single-writer architecture, adding read replicas provides no relief to the primary writer’s CPU, disk I/O, or lock contention. At 50K transactions per second (TPS), the vertical scaling limit of the largest available compute instance is reached.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;When a single writer reaches its physical limits, architects typically look to horizontal scaling. Standard PostgreSQL requires application-layer sharding to achieve this, which destroys the strict transactional consistency required for financial ledgers and introduces immense operational complexity.&lt;/p&gt;
&lt;p&gt;Newer cloud databases attempt to solve this. However, it’s critical to understand the difference in vendor architectures. AlloyDB (and Amazon Aurora PostgreSQL) uses a disaggregated, log-structured storage engine, but still routes all writes through a &lt;strong&gt;single primary compute instance&lt;/strong&gt;. They improve I/O and replica lag, but they do not horizontally scale the writer. Amazon Aurora PostgreSQL Limitless Database introduces a routing layer over fleets of Postgres shards, but it is effectively managed sharding.&lt;/p&gt;
&lt;p&gt;This workload needs strict serializability, horizontal write scaling, and multi-region high availability without application-side sharding. Cloud Spanner fits this requirement perfectly on paper. But how do you operate a distributed database using standard PostgreSQL drivers without accidentally destroying its performance?&lt;/p&gt;
&lt;h2 id=&quot;the-cloud-spanner-postgresql-architecture&quot;&gt;The Cloud Spanner PostgreSQL Architecture&lt;/h2&gt;
&lt;p&gt;To solve this, the team evaluates Cloud Spanner. Spanner decouples compute and storage, distributing both across a cluster. The PostgreSQL interface is a protocol and syntax compatibility layer—it does &lt;strong&gt;not&lt;/strong&gt; make Spanner run the PostgreSQL storage engine under the hood.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[Application Client] --&gt; PG[PGAdapter Proxy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PG --&gt; API[Spanner API]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    API --&gt; C1[Compute Node]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    API --&gt; C2[Compute Node]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    API --&gt; C3[Compute Node]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C1 --&gt; S1[Split A — Paxos Group]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C2 --&gt; S1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C2 --&gt; S2[Split B — Paxos Group]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C3 --&gt; S3[Split C — Paxos Group]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    S1 --&gt; Colossus[Colossus Distributed Storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    S2 --&gt; Colossus&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    S3 --&gt; Colossus&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;what-happens-to-a-write&quot;&gt;What Happens to a Write?&lt;/h3&gt;
&lt;p&gt;When the application executes an &lt;code&gt;INSERT&lt;/code&gt; via the PostgreSQL driver, Spanner does not write to a traditional Postgres Write-Ahead Log (WAL).&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Connection:&lt;/strong&gt; The application connects via PGAdapter or a native Spanner Postgres driver.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Transaction Coordinator:&lt;/strong&gt; A Spanner compute node acts as the transaction coordinator.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data Ownership Determination:&lt;/strong&gt; The coordinator looks up which “split” owns the primary key.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replication &amp;#x26; Consensus:&lt;/strong&gt; The write is sent to the Paxos leader for that split. The leader assigns a TrueTime timestamp and proposes the write to the Paxos replicas.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Durable Commit:&lt;/strong&gt; Once a quorum of replicas acknowledges the write, the leader commits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Acknowledgement:&lt;/strong&gt; The coordinator returns the success to the application.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Because writes require Paxos consensus across a network, expect commit latencies in the single-digit-to-low-double-digit milliseconds — not the sub-millisecond latencies of a local SSD Postgres commit. Treat this as an illustrative order of magnitude rather than a documented guaranteed figure; actual latency depends on region topology and load.&lt;/p&gt;
&lt;h3 id=&quot;how-write-scalability-actually-works&quot;&gt;How Write Scalability Actually Works&lt;/h3&gt;
&lt;p&gt;Spanner scales writes horizontally by dividing tables into “splits” (contiguous ranges of rows sorted by primary key). If a split becomes too large or too hot, Spanner dynamically rebalances the cluster by dividing the split in two and moving ownership of the new split to a different compute node.&lt;/p&gt;
&lt;p&gt;If a transaction updates &lt;code&gt;orders&lt;/code&gt; (in Split A) and &lt;code&gt;inventory&lt;/code&gt; (in Split B), Spanner performs a distributed transaction using two-phase commit (2PC) powered by TrueTime. While TrueTime makes this efficient, 2PC still introduces latency.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Google’s documented behavior for Spanner explicitly warns against treating it like a standard relational database when designing schemas. The following patterns dictate your physical hardware utilization.&lt;/p&gt;
&lt;h3 id=&quot;schema-design-and-hotspots&quot;&gt;Schema Design and Hotspots&lt;/h3&gt;
&lt;p&gt;If the application uses a monotonically increasing &lt;code&gt;order_id&lt;/code&gt; (e.g., &lt;code&gt;SERIAL&lt;/code&gt;), all new orders append to the &lt;em&gt;exact same split&lt;/em&gt; at the end of the keyspace. Only the compute node owning that split does the work. The rest of the cluster sits idle. This is a “hotspot.” Adding more nodes increases your bill but will not improve throughput.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Good Schema (Spanner optimized):&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;36&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;/* Use UUIDv4 */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    customer_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BIGINT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_timestamp &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TIMESTAMPTZ&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;UUIDv4 distributes writes evenly across the entire keyspace. Alternatively, you can use bit-reversed sequences to maintain integer keys without sequential hotspotting.&lt;/p&gt;
&lt;p&gt;To avoid cross-split distributed transactions, Spanner allows you to physically co-locate child rows with their parent rows. The PostgreSQL interface supports this via Spanner-specific extensions:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; order_items&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;36&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    item_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;36&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    product_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BIGINT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (order_id, item_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) INTERLEAVE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PARENT orders;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;postgresql-dba-translation-and-compatibility&quot;&gt;PostgreSQL DBA Translation and Compatibility&lt;/h3&gt;
&lt;p&gt;“PostgreSQL Compatibility” in Spanner means &lt;strong&gt;Wire Protocol and Syntax Compatibility&lt;/strong&gt;, not feature parity. Spanner supports strict serializable isolation; it does not support Read Committed for read-write transactions.&lt;/p&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;PostgreSQL DBA Concept&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Spanner Equivalent&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Important Difference&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Instance&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Spanner Instance&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Spanner instances are distributed clusters.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Primary Key&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary Key&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Critical for physical data distribution.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;WAL (Write-Ahead Log)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Paxos Logs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Distributed consensus logs.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Autovacuum&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Automatic Compaction&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Handled transparently by Spanner.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Read Replicas&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Automatic Replicas&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Built-in via Paxos.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Sequences (&lt;code&gt;SERIAL&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bit-Reversed Sequences&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Standard sequences cause hotspots.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Triggers / Procedures&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Not Supported (user-defined)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;User-defined triggers and stored procedures are unsupported — handle at the application layer. Two built-in system procedures exist (&lt;code&gt;spanner.cancel_query()&lt;/code&gt;, &lt;code&gt;spanner.compact_all()&lt;/code&gt;) but these are operational tools, not user logic.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;terraform-and-automation&quot;&gt;Terraform and Automation&lt;/h3&gt;
&lt;p&gt;Configuring the PostgreSQL dialect requires explicitly setting the dialect at creation. It cannot be changed later.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;google_spanner_database&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;example_db&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  instance&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; google_spanner_instance&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;     =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;orders-db&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;  # CRITICAL: Define the dialect at creation. Cannot be changed.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  database_dialect&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;POSTGRESQL&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;  # Prevent accidental Terraform destruction&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  deletion_protection&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;  # Prevent deletion via gcloud/Console/API too — deletion_protection alone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;  # only blocks Terraform-initiated destruction, not out-of-band deletion.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  enable_drop_protection&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; You cannot execute DDL inside the &lt;code&gt;google_spanner_database&lt;/code&gt; resource block when using the &lt;code&gt;POSTGRESQL&lt;/code&gt; dialect. DDL must be applied via separate tools (e.g., Liquibase) after provisioning. Also note the distinction above: &lt;code&gt;deletion_protection&lt;/code&gt; and &lt;code&gt;enable_drop_protection&lt;/code&gt; guard against different deletion paths, and both are needed for real production protection.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Do not use Spanner if Cloud SQL or AlloyDB can handle your workload. If your writes fit on a 96-core AlloyDB instance, the complexity of managing distributed primary keys, interleaving, and transaction retries is not worth the operational overhead.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure Mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detection&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation / Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Node / Process Failure&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;API redirects seamlessly&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Spanner detects failure and elects a new Paxos leader in &amp;#x3C; 10s. Minimal latency spike.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Region Failure (multi-region instance config only)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Multi-region Paxos quorum&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero RPO. Surviving regions elect a leader. RTO is near zero. This property requires choosing and paying for a multi-region instance configuration — a regional (single-region) config has no cross-region replicas and does not survive a full region outage.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;High Contention&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;ABORTED&lt;/code&gt; errors in logs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Spanner uses pessimistic two-phase locking with wound-wait. Clients must retry aborted transactions.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Large Transactions&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Hard limit rejections&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Transactions cannot exceed 100MB or 80,000 mutations. Must chunk large batch updates.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Schema Hotspots&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Key Visualizer shows red bands&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;You cannot brute-force a bad schema with more nodes. Must rewrite primary keys to UUIDv4.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Deep Interleaving&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;DDL rejection&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;You cannot interleave deeper than 7 levels.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;migration-considerations&quot;&gt;Migration Considerations&lt;/h3&gt;
&lt;p&gt;Migrating from Cloud SQL PostgreSQL to Spanner is not a lift-and-shift. You must remove all triggers, stored procedures, and unsupported data types (like PostGIS). Applications must be refactored to implement retry logic for &lt;code&gt;ABORTED&lt;/code&gt; transactions.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;If you are evaluating Cloud Spanner for a PostgreSQL workload:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Single-node PostgreSQL writers eventually hit a physical CPU/IO wall.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Spanner’s PostgreSQL interface provides horizontal write scaling through distributed splits and TrueTime, using familiar Postgres syntax and drivers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Google documents that sequential keys will bottleneck Spanner, requiring UUIDs or bit-reversed sequences to utilize the distributed architecture effectively.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run your schema through the Spanner migration tool to identify incompatible types, remove all triggers, and ensure your application ORM can gracefully handle and retry transaction aborts before committing to a migration.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&quot;technical-references&quot;&gt;Technical References&lt;/h2&gt;
&lt;h3 id=&quot;google-cloud&quot;&gt;Google Cloud&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/spanner/docs/postgresql/postgresql-interface&quot;&gt;PostgreSQL Interface for Cloud Spanner (Official Documentation)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/spanner/quotas&quot;&gt;Cloud Spanner Quotas and Limits&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/spanner/docs/schema-and-data-model&quot;&gt;Schema Design for Cloud Spanner&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/spanner/docs/concurrency-control&quot;&gt;Concurrency Control in Cloud Spanner&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/blog/topics/developers-practitioners/demystifying-cloud-spanner-multi-region-configurations&quot;&gt;Demystifying Cloud Spanner Multi-Region Configurations&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;hashicorp-terraform&quot;&gt;HashiCorp Terraform&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/spanner_database&quot;&gt;google_spanner_database Registry Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;aws&quot;&gt;AWS&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless-architecture.html&quot;&gt;Amazon Aurora PostgreSQL Limitless Database Architecture&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&quot;claims-requiring-additional-human-validation&quot;&gt;Claims Requiring Additional Human Validation&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;The 80,000-mutations-per-commit limit was live-verified against Google’s current Quotas page as of this writing, but Google’s own blog post announcing this figure carries an editor’s note that it “has since increased beyond 80,000” — worth a periodic re-check, since this is a number Google has moved before.&lt;/li&gt;
&lt;li&gt;The single-digit-to-low-double-digit millisecond commit latency figure is an illustrative order of magnitude, not a documented guaranteed SLA — no single Google source pins an exact number for Paxos consensus commit latency.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>cloud</category><category>architecture</category><category>ai-engineering</category><category>databases</category><category>failures</category><category>checklist</category></item><item><title>MongoDB Replica Set HA: Elections, Read Preference, Write Concern, and Rollbacks</title><link>https://rajivonai.com/blog/2026-05-26-replica-set-ha/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-26-replica-set-ha/</guid><description>How to prevent phantom data loss during failovers using w:majority, setDefaultRWConcern, and WiredTiger rollback-to-stable-timestamp mechanics.</description><pubDate>Tue, 25 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;When a primary node crashes, a high-availability failover can silently erase newly written data if application write durability and cluster election quorums disagree on consensus.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Writes acknowledged with &lt;code&gt;w: 1&lt;/code&gt; return from the primary’s local WiredTiger cache and journal before secondaries replicate the oplog; if the primary crashes before replication, WiredTiger Rollback to Stable Timestamp (RTS) reverts those writes to &lt;code&gt;rollback/&amp;#x3C;collectionUUID&gt;/removed.*.bson&lt;/code&gt; on recovery.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Deploy 3-node or 5-node data-bearing PSS topologies across distinct AZs and enforce &lt;code&gt;{ w: &quot;majority&quot;, wtimeout: 5000 }&lt;/code&gt; plus &lt;code&gt;readConcern: &quot;majority&quot;&lt;/code&gt; globally via &lt;code&gt;setDefaultRWConcern&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; In MongoDB 5.0+, 3-node PSA (Primary-Secondary-Arbiter) sets still default implicitly to &lt;code&gt;w: 1&lt;/code&gt;, and omitting &lt;code&gt;wtimeout&lt;/code&gt; hangs client threads indefinitely during network partitions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-26-replica-set-ha.svg&quot; alt=&quot;Enforce w:majority with wtimeout on PSS sets to stop rollbacks&quot;&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Production Scope: MongoDB 4.4 – 8.0 on AWS EC2 Multi-AZ&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Self-managed MongoDB replica sets on AWS EC2 achieve high availability by distributing voting members across three distinct Availability Zones (such as &lt;code&gt;us-east-1a&lt;/code&gt;, &lt;code&gt;us-east-1b&lt;/code&gt;, and &lt;code&gt;us-east-1c&lt;/code&gt;). When the primary fails due to hypervisor termination, kernel panic, or AZ isolation, surviving secondaries detect missed heartbeats and elect a new leader automatically within seconds.&lt;/p&gt;
&lt;p&gt;In transactional workloads—payment ledgers, billing state machines, and order fulfillment pipelines—a subtle durability gap appears between write acknowledgment and failover consensus. If an application driver considers a write committed before a majority of voting data nodes have recorded it in their oplog, an abrupt primary crash creates a divergent timeline. Even a brief cross-AZ replication delay of a few milliseconds is enough for hundreds of &lt;code&gt;w: 1&lt;/code&gt; writes to exist only on the failed primary’s local disk. When the failed primary reboots and rejoins the replica set, WiredTiger physically rolls back the un-replicated transactions, leaving upstream services unaware that acknowledged writes were discarded.&lt;/p&gt;
&lt;p&gt;At the same time, fixing durability naively—by enabling &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; without a timeout, or by deploying a Primary-Secondary-Arbiter (PSA) topology to save EC2 instance costs—introduces availability outages of its own during single-node restarts or network partitions.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;h3 id=&quot;mechanics-of-phantom-data-loss-and-rollbacks&quot;&gt;Mechanics of Phantom Data Loss and Rollbacks&lt;/h3&gt;
&lt;p&gt;Legacy &lt;code&gt;w: 1&lt;/code&gt; acknowledgments and misconfigured topologies expose four compounding failure constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Local Single-Node Acknowledgment (&lt;code&gt;w: 1&lt;/code&gt;):&lt;/strong&gt; Prior to MongoDB 5.0, the default write concern was &lt;code&gt;w: 1&lt;/code&gt; (and in 5.0+, 3-node PSA sets still fall back to &lt;code&gt;w: 1&lt;/code&gt; implicitly). Under &lt;code&gt;w: 1&lt;/code&gt;, the primary acknowledges writes as soon as they mutate its local WiredTiger cache and journal buffer, returning before any secondary pulls the oplog entry.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Asynchronous Replication Window:&lt;/strong&gt; Secondaries tail &lt;code&gt;local.oplog.rs&lt;/code&gt; across cross-AZ VPC links. Network transit time, OS socket buffering, and secondary oplog applier queuing create a replication lag window of several milliseconds (or seconds under heavy write bursts) between primary commit and secondary application.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Primary Crash and Minority-Primary Partition Window:&lt;/strong&gt; If the primary terminates abruptly while holding un-replicated &lt;code&gt;w: 1&lt;/code&gt; operations, surviving secondaries wait &lt;code&gt;electionTimeoutMillis&lt;/code&gt; (default &lt;code&gt;10,000&lt;/code&gt; ms, after missing five 2,000 ms heartbeats) and elect a new primary with a higher &lt;code&gt;term&lt;/code&gt; from the surviving majority. Worse, during an asymmetric network partition that isolates the old primary from the two secondaries, the old primary continues accepting &lt;code&gt;w: 1&lt;/code&gt; writes until its own heartbeat timeout expires and forces it to step down to &lt;code&gt;SECONDARY&lt;/code&gt;. Throughout that up-to-10-second overlap window, clients routed to the isolated AZ receive &lt;code&gt;w: 1&lt;/code&gt; acknowledgments that are guaranteed to diverge from the new majority primary’s timeline.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback to Stable Timestamp (RTS):&lt;/strong&gt; When the crashed or partitioned former primary reconnects, its replication handshake detects divergent oplog entries absent from the new primary’s timeline. WiredTiger executes &lt;strong&gt;Rollback to Stable Timestamp (RTS)&lt;/strong&gt;, reverting memory and disk state to the last majority-committed checkpoint (&lt;code&gt;stable_timestamp&lt;/code&gt;), truncating the journal, and dumping the discarded documents under &lt;code&gt;&amp;#x3C;dbPath&gt;/rollback/&amp;#x3C;collectionUUID&gt;/removed.&amp;#x3C;timestamp&gt;.bson&lt;/code&gt;. Under &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt;, by contrast, the minority primary cannot gather quorum acknowledgments during a partition, so writes fail cleanly after &lt;code&gt;wtimeout&lt;/code&gt; rather than silently diverging.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-26-replica-set-ha-2-constraints.svg&quot; alt=&quot;w:1 writes acknowledge locally and vanish during RTS recovery&quot;&gt;&lt;/p&gt;
&lt;p&gt;The core architectural question is: &lt;strong&gt;How can platform engineers guarantee zero post-acknowledgment rollbacks across multi-AZ failovers without hanging application thread pools during network partitions or relying on per-service driver configs?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Configuration&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Rollback Risk&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;1-Node Outage Behavior&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Partition Behavior&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;w: 1&lt;/code&gt; (Legacy / PSA Implicit)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High; un-replicated writes lost on crash&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Writes continue on primary&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Minority primary accepts divergent writes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Silent data loss on failover&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;3-Node PSA + Explicit &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero when both data nodes up&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;All writes block if 1 secondary drops&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Minority/arbiter pair cannot satisfy data majority&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Sacrifices HA on single node loss&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;PSS + &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; (No &lt;code&gt;wtimeout&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Writes succeed via 2 of 3 data nodes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Client threads hang indefinitely on partition&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Exhausts app connection pools&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;PSS + &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; + &lt;code&gt;wtimeout: 5000&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Mathematically zero&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Writes succeed via remaining majority&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails fast at 5s with &lt;code&gt;WriteConcernError&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen:&lt;/strong&gt; Safe consensus with bounded tail&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Using &lt;code&gt;w: 1&lt;/code&gt; trades away durability for sub-millisecond local acks, while a 3-node PSA topology forces a bad choice between silent &lt;code&gt;w: 1&lt;/code&gt; rollbacks or a total write outage whenever the lone secondary restarts. In a 3-node PSA set, an arbiter holds a vote for elections (&lt;code&gt;1&lt;/code&gt; vote, &lt;code&gt;0&lt;/code&gt; data) but stores no oplog or collection data; therefore, &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; requires both the primary and the single data secondary to acknowledge every write. If that secondary undergoes OS patching or EBS maintenance, all &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; writes block. Running a &lt;strong&gt;3-node or 5-node data-bearing PSS set&lt;/strong&gt; with &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; and an explicit &lt;code&gt;wtimeout&lt;/code&gt; eliminates rollbacks while tolerating single-node failures and bounding partition stalls.&lt;/p&gt;
&lt;p&gt;For read scaling, &lt;code&gt;readPreference&lt;/code&gt; and &lt;code&gt;readConcern&lt;/code&gt; must be chosen together: routing reads to secondaries with &lt;code&gt;readConcern: &quot;local&quot;&lt;/code&gt; can return un-committed oplog entries that later roll back on primary failover, whereas &lt;code&gt;readConcern: &quot;majority&quot;&lt;/code&gt; ensures every returned document is durable across a quorum. When a workflow requires strict linearizability—guaranteeing it never reads stale data even from a partitioned primary during the 10-second &lt;code&gt;electionTimeoutMillis&lt;/code&gt; window—use &lt;code&gt;readConcern: &quot;linearizable&quot;&lt;/code&gt; on primary reads, accepting the extra majority heartbeat round-trip.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-26-replica-set-ha-3-options.svg&quot; alt=&quot;Only PSS with w:majority and wtimeout avoids loss and hangs&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-durability-architecture-consensus-elections-and-rollbacks&quot;&gt;The Durability Architecture: Consensus, Elections, and Rollbacks&lt;/h2&gt;
&lt;p&gt;MongoDB replica sets coordinate durability and leadership via &lt;strong&gt;Protocol Version 1 (pv1)&lt;/strong&gt;—a Raft-derived consensus protocol—paired with WiredTiger’s &lt;code&gt;stable_timestamp&lt;/code&gt; checkpointing.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-26-replica-set-ha-4-architecture.svg&quot; alt=&quot;Multi-AZ PSS consensus advances stable_timestamp on majority&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Client[&quot;Application Client — Financial Service&quot;] --&gt;|Write Request| Primary[&quot;Node A Primary — us-east-1a&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt;|Oplog Sync| SecB[&quot;Node B Secondary — us-east-1b&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt;|Oplog Sync| SecC[&quot;Node C Secondary — us-east-1c&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SecB --&gt;|Oplog ACK| Primary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SecC --&gt;|Oplog ACK| Primary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt;|w majority Satisfied| ClientAck[&quot;Client Acknowledged — Durable&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary -.-&gt;|Sudden Crash| DeadNode[&quot;Node A Down&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SecB &amp;#x26; SecC --&gt;|pv1 Election| NewPrimary[&quot;Node B Promoted to Primary&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DeadNode --&gt;|Reboots| Rejoin[&quot;Node A Reconnects&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Rejoin --&gt;|Oplog Diverged| RTS[&quot;WiredTiger Rollback to Stable Timestamp&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    RTS --&gt; DumpBSON[&quot;Dump removed.timestamp.bson&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-protocol-version-1-pv1-election-mechanics&quot;&gt;1. Protocol Version 1 (pv1) Election Mechanics&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Heartbeats and Timeout:&lt;/strong&gt; Nodes exchange heartbeats every &lt;code&gt;heartbeatIntervalMillis&lt;/code&gt; (&lt;code&gt;2,000&lt;/code&gt; ms). Missing heartbeats for &lt;code&gt;electionTimeoutMillis&lt;/code&gt; (&lt;code&gt;10,000&lt;/code&gt; ms, or five missed heartbeats) triggers an election. Lowering &lt;code&gt;electionTimeoutMillis&lt;/code&gt; speeds up failover detection, but setting it too aggressively (under 5,000 ms) across AWS AZs risks spurious elections during transient VPC jitter or Garbage Collection / checkpoint I/O pauses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pre-Election Catch-Up:&lt;/strong&gt; A newly elected primary attempts to catch up any remaining oplog entries from other visible members up to &lt;code&gt;catchUpTimeoutMillis&lt;/code&gt; (default &lt;code&gt;-1&lt;/code&gt;, meaning wait indefinitely until caught up or interrupted) before opening for writes. This preserves un-replicated &lt;code&gt;w: 1&lt;/code&gt; writes when a secondary with a slightly older optime wins an election, at the cost of delaying write availability until sync completes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Quorum Formula, Votes, and Priorities:&lt;/strong&gt; An election requires strict majority $\lfloor N / 2 \rfloor + 1$ of voting members (2 of 3 nodes, or 3 of 5 nodes), with a hard ceiling of 7 voting members across up to 50 total replica set members. Voters reject candidates whose term or last &lt;code&gt;optime&lt;/code&gt; is behind their own, and each member can vote for at most one candidate per term. Setting &lt;code&gt;priority: 0&lt;/code&gt; on a dedicated analytics or cross-region backup secondary prevents it from ever winning an election while still allowing it to hold a vote if needed, whereas &lt;code&gt;hidden: true&lt;/code&gt; (which requires &lt;code&gt;priority: 0&lt;/code&gt;) also removes it from driver topology discovery.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Journal Durability (&lt;code&gt;writeConcernMajorityJournalDefault&lt;/code&gt;):&lt;/strong&gt; By default, &lt;code&gt;writeConcernMajorityJournalDefault&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;, meaning &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; requires a majority of voting data-bearing nodes to flush the oplog entry to their on-disk WiredTiger journal (&lt;code&gt;j: true&lt;/code&gt; semantics) before acknowledging. Setting it to &lt;code&gt;false&lt;/code&gt; acknowledges from secondaries’ in-memory WiredTiger cache, which survives single-node crashes but risks data loss if a simultaneous multi-AZ power or hypervisor event terminates a majority before their 100 ms journal sync.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-wiredtiger-rollback-to-stable-timestamp-rts&quot;&gt;2. WiredTiger Rollback to Stable Timestamp (RTS)&lt;/h3&gt;
&lt;p&gt;Before MongoDB 4.0, rollbacks were logical—requiring the recovering node to fetch documents across the network and replay inverse operations, often taking hours on multi-terabyte collections and hitting a 300 MB rollback data ceiling. Since MongoDB 4.0, WiredTiger delegates rollbacks to &lt;strong&gt;RTS&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The replica set continuously advances &lt;code&gt;stable_timestamp&lt;/code&gt; to the highest optime committed by a majority of voting nodes (&lt;code&gt;majority&lt;/code&gt; commit point).&lt;/li&gt;
&lt;li&gt;When a recovering node detects diverged history, WiredTiger reverts its in-memory and on-disk B-trees directly to the checkpoint at &lt;code&gt;stable_timestamp&lt;/code&gt; and truncates post-checkpoint journal records—typically completing in seconds regardless of collection size.&lt;/li&gt;
&lt;li&gt;MongoDB extracts the rolled-back documents and writes them to &lt;code&gt;&amp;#x3C;dbPath&gt;/rollback/&amp;#x3C;collectionUUID&gt;/removed.&amp;#x3C;timestamp&gt;.bson&lt;/code&gt; (controlled by &lt;code&gt;createRollbackDataFiles&lt;/code&gt;, enabled by default). Operators can inspect these files with &lt;code&gt;bsondump&lt;/code&gt; and replay idempotent upserts via &lt;code&gt;mongorestore&lt;/code&gt; or a custom recovery script, though replaying after newer writes have occurred requires careful conflict resolution. Note that RTS is bounded by &lt;code&gt;rollbackTimeLimitSecs&lt;/code&gt; (default &lt;code&gt;86,400&lt;/code&gt; seconds, or 24 hours); if an isolated primary diverges for longer than 24 hours, automatic rollback aborts with &lt;code&gt;UnrecoverableRollbackError&lt;/code&gt; and requires a full initial sync from a healthy member.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;3-read-concern-and-write-concern-alignment&quot;&gt;3. Read Concern and Write Concern Alignment&lt;/h3&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Concern Setting&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Guarantee&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Behavior During Failover / Partition&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;w: 1&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary memory + journal only&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High rollback risk if primary crashes before oplog sync&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;w: &quot;majority&quot;, j: true&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Committed to quorum + journaled&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero rollback after acknowledgment; survives power loss&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;readConcern: &quot;local&quot;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reads node’s latest applied state&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Can return uncommitted data later rolled back&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;readConcern: &quot;majority&quot;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reads only quorum-committed data&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Never returns data that can be rolled back&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;readConcern: &quot;linearizable&quot;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Live quorum check on primary&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Prevents stale reads from a partitioned minority primary&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;diagnostic-telemetry-optime-audit-and-promql-alerts&quot;&gt;Diagnostic Telemetry: Optime Audit and PromQL Alerts&lt;/h3&gt;
&lt;p&gt;Audit replication lag and active cluster default concerns in &lt;code&gt;mongosh&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rs.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; primary&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; status.members.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.stateStr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;PRIMARY&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;term=&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, status.term, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;primary=&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, primary?.name);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;status.members.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.stateStr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;SECONDARY&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(m.name, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;lagSecs=&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, ((primary.optimeDate &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.optimeDate) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;printjson&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;adminCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ getDefaultRWConcern: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Monitor replication lag, RTS rollback events, and election term churn in Prometheus:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;groups&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;mongodb_ha_durability_alerts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    rules&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;alert&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;MongoSecondaryReplicationLagCritical&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        expr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;mongodb_mongod_replset_member_replication_lag{state=&quot;SECONDARY&quot;} &gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;1m&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        labels&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;severity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;critical&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;      # The two metric names below vary by exporter version; confirm them against your exporter&apos;s /metrics output&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;alert&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;MongoRollbackEventDetected&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        expr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;increase(mongodb_ss_metrics_repl_stateTransition_lastRollbackExecutionTime[5m]) &gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        labels&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;severity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;critical&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;alert&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;MongoElectionTermChurn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        expr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;increase(mongodb_ss_repl_election_successful[10m]) &gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        labels&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;severity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;warning&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;safe-rollback-forensics--recovery-mongo_rollback_auditorpy&quot;&gt;Safe Rollback Forensics &amp;#x26; Recovery (&lt;code&gt;mongo_rollback_auditor.py&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;In MongoDB 4.4+, rollback files reside in UUID subdirectories (&lt;code&gt;&amp;#x3C;dbPath&gt;/rollback/&amp;#x3C;collectionUUID&gt;/removed.&amp;#x3C;timestamp&gt;.bson&lt;/code&gt;). Walk subdirectories recursively and resolve UUIDs against &lt;code&gt;listCollections&lt;/code&gt; (or &lt;code&gt;grep &quot;rollback file&quot; /var/log/mongodb/mongod.log&lt;/code&gt;) before replaying:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env python3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse, os, bson&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; audit_rollback&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(rollback_dir, target_uri, dry_run):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    client &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient(target_uri, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;serverSelectionTimeoutMS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    uuid_map &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.list_database_names():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;admin&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;local&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;config&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            continue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client[db_name].list_collections():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            u &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; c.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;info&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, {}).get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;uuid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; u:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                uuid_map[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(u)] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (db_name, c[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; root, _, files &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; os.walk(rollback_dir):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; sorted&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(files):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fname.endswith(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;.bson&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                continue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            fpath &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; os.path.join(root, fname)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            coll_uuid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; os.path.basename(root)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            ns &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; uuid_map.get(coll_uuid)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            with&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(fpath, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;rb&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; f:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                docs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; bson.decode_all(f.read())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;            print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;file=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;fpath&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; ns=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ns&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; docs=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(docs)&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dry_run &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ns:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                coll &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client[ns[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]][ns[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; d &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; docs:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                    coll.replace_one({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: d[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]}, d, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;upsert&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-26-replica-set-ha-5-flow.svg&quot; alt=&quot;Four-phase rollout enforces majority durability across drivers&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Verify PSS Topology &amp;#x26; Current Defaults:&lt;/strong&gt; Run &lt;code&gt;rs.conf()&lt;/code&gt; to confirm zero arbiters in mission-critical sets, and inspect &lt;code&gt;db.adminCommand({ getDefaultRWConcern: 1 })&lt;/code&gt;. Verify all three data-bearing members reside in separate AWS Availability Zones so a single AZ outage cannot take down a voting majority.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Enforce Cluster Defaults via &lt;code&gt;setDefaultRWConcern&lt;/code&gt;:&lt;/strong&gt; Apply majority read/write defaults globally (available since MongoDB 4.4) with a bounded &lt;code&gt;wtimeout&lt;/code&gt;:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;adminCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  setDefaultRWConcern: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  defaultWriteConcern: { w: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;majority&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, wtimeout: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  defaultReadConcern: { level: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;majority&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
Using &lt;code&gt;wtimeout: 5000&lt;/code&gt; ensures that if two nodes become unreachable simultaneously, writes fail fast after 5 seconds with a &lt;code&gt;WriteConcernError&lt;/code&gt; instead of exhausting application worker threads. Rollback: revert &lt;code&gt;defaultWriteConcern&lt;/code&gt; or adjust &lt;code&gt;wtimeout&lt;/code&gt; via &lt;code&gt;setDefaultRWConcern&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Lint Application URIs:&lt;/strong&gt; Strip explicit &lt;code&gt;?w=1&lt;/code&gt; or &lt;code&gt;readConcernLevel=local&lt;/code&gt; overrides from microservice connection strings in CI/CD, because client-explicit concerns override cluster defaults. Also ensure application handlers distinguish &lt;code&gt;WriteConcernError&lt;/code&gt; (write succeeded on primary, failed to reach majority within &lt;code&gt;wtimeout&lt;/code&gt;) from &lt;code&gt;WriteError&lt;/code&gt; (write rejected outright) before retrying.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Standardize Maintenance Stepdowns &amp;#x26; Alerts:&lt;/strong&gt; Require &lt;code&gt;rs.stepDown(60, 15)&lt;/code&gt; (60s stepdown block, 15s &lt;code&gt;secondaryCatchUpPeriodSecs&lt;/code&gt;) for planned failovers, and alert on &lt;code&gt;lastRollbackExecutionTime&lt;/code&gt; and &lt;code&gt;/var/lib/mongodb/rollback/&lt;/code&gt; growth via Full Time Diagnostic Capture (FTDC) and Prometheus.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Documented MongoDB consensus and storage engine behaviors explain the exact latency and availability tradeoffs of majority durability:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cross-AZ Latency Cost of &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt;:&lt;/strong&gt; Per MongoDB’s replication mechanics, &lt;code&gt;w: 1&lt;/code&gt; returns after local WiredTiger cache and journal buffering on the primary, whereas &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; waits for at least one cross-AZ secondary (in a 3-node PSS set) to receive, journal/apply, and acknowledge the oplog batch. That adds at least one cross-AZ network round-trip time to write latency—an unavoidable physical cost for zero-rollback durability.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PSA Topology Implicit Fallback in MongoDB 5.0+:&lt;/strong&gt; While MongoDB 5.0 changed the default write concern to &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; for PSS replica sets, it intentionally falls back to &lt;code&gt;w: 1&lt;/code&gt; when &lt;code&gt;Arbiters &gt; 0&lt;/code&gt; and &lt;code&gt;Non-Arbiters &amp;#x3C;= Majority of Voting Nodes&lt;/code&gt; (such as a 3-node PSA set). Otherwise, losing the lone secondary would leave only 1 data-bearing node and halt all writes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Coordinated Maintenance Stepdowns (&lt;code&gt;rs.stepDown&lt;/code&gt;):&lt;/strong&gt; Passing &lt;code&gt;secondaryCatchUpPeriodSecs&lt;/code&gt; (default &lt;code&gt;10&lt;/code&gt; seconds, e.g., &lt;code&gt;rs.stepDown(60, 15)&lt;/code&gt;) instructs the primary to pause new writes and wait for an eligible secondary to synchronize its optime before stepping down. Combined with &lt;code&gt;catchUpTimeoutMillis: -1&lt;/code&gt; on the newly elected primary, planned failovers complete without generating rollback BSON files or &lt;code&gt;WriteConcernError&lt;/code&gt; bursts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Retryable Writes (&lt;code&gt;retryWrites=true&lt;/code&gt;) vs. &lt;code&gt;WriteConcernError&lt;/code&gt;:&lt;/strong&gt; Modern MongoDB drivers enable &lt;code&gt;retryWrites=true&lt;/code&gt; by default, attaching a monotonically increasing &lt;code&gt;txnNumber&lt;/code&gt; per session so single-document writes and multi-document transactions retry once automatically on transient network errors or &lt;code&gt;NotWritablePrimary&lt;/code&gt; / &lt;code&gt;InterruptedDueToReplStateChange&lt;/code&gt; responses during elections without creating duplicate inserts. However, &lt;code&gt;WriteConcernError&lt;/code&gt; means the primary already applied the write locally and only the replication acknowledgment timed out past &lt;code&gt;wtimeout&lt;/code&gt;—retrying blindly at the application layer without idempotency keys can still duplicate side effects if the write was not tagged as a driver-managed retryable write.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-26-replica-set-ha-6-failures.svg&quot; alt=&quot;Four replica-set HA failure paths that silently break durability&quot;&gt;&lt;/p&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Operational Action&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Physical Failure Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Architectural Consequence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Safe Mitigation Protocol&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Omitting &lt;code&gt;wtimeout&lt;/code&gt; with &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Network partition prevents quorum from acknowledging writes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary holds client connections open indefinitely; app pools exhaust.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Always set &lt;code&gt;wtimeout: 5000&lt;/code&gt; and handle &lt;code&gt;WriteConcernError&lt;/code&gt; cleanly.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Explicit &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; on 3-Node PSA&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Only 2 data nodes exist; 1 arbiter cannot store oplog data.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Losing the single data secondary halts all majority writes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Replace arbiters with 3-node or 5-node data-bearing PSS topologies.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Client URI Override (&lt;code&gt;?w=1&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Microservice explicitly sets &lt;code&gt;w=1&lt;/code&gt; in its connection string.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Overrides &lt;code&gt;setDefaultRWConcern&lt;/code&gt; and reintroduces rollback risk.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lint connection strings in CI/CD to reject &lt;code&gt;w=1&lt;/code&gt; overrides.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;secondary&lt;/code&gt; Read + &lt;code&gt;local&lt;/code&gt; Concern&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Secondary returns local uncommitted oplog state.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Clients observe writes that later roll back on primary failover.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce &lt;code&gt;readConcern: &quot;majority&quot;&lt;/code&gt; or route critical reads to &lt;code&gt;primary&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;linearizable&lt;/code&gt; Read During Partition&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary cannot complete live majority heartbeat check.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails reads after &lt;code&gt;maxTimeMS&lt;/code&gt; during inter-AZ packet loss.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reserve &lt;code&gt;linearizable&lt;/code&gt; for strict leader-verification checks only.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Minority Primary Accepting &lt;code&gt;w: 1&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Partitioned primary continues accepting &lt;code&gt;w: 1&lt;/code&gt; for up to &lt;code&gt;10s&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Writes diverge from new majority primary and roll back on rejoin.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; so minority primaries cannot ack writes.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rollback BSON Filling Disk&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Repeated failovers dump GBs under &lt;code&gt;dbPath/rollback/&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exhausts EBS volume and crashes &lt;code&gt;mongod&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Alert on RTS metrics and archive/purge inspected &lt;code&gt;removed.*.bson&lt;/code&gt; files.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Uncoordinated &lt;code&gt;rs.stepDown()&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Stepping down without allowing secondary oplog catch-up.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Forces election retries and transient &lt;code&gt;WriteConcernError&lt;/code&gt; spikes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Always pass &lt;code&gt;secondaryCatchUpPeriodSecs&lt;/code&gt; (&lt;code&gt;rs.stepDown(60, 15)&lt;/code&gt;).&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Never rely on &lt;code&gt;w: 1&lt;/code&gt; for transactional data:&lt;/strong&gt; WiredTiger Rollback to Stable Timestamp (RTS) silently discards un-replicated &lt;code&gt;w: 1&lt;/code&gt; writes into &lt;code&gt;rollback/&amp;#x3C;collectionUUID&gt;/removed.*.bson&lt;/code&gt; after a primary crash.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Always pair &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; with &lt;code&gt;wtimeout&lt;/code&gt;:&lt;/strong&gt; Setting &lt;code&gt;wtimeout: 5000&lt;/code&gt; via &lt;code&gt;setDefaultRWConcern&lt;/code&gt; prevents application connection pools from hanging indefinitely during network partitions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Avoid 3-node PSA topologies in production:&lt;/strong&gt; Even in MongoDB 5.0+, 3-node PSA sets fall back implicitly to &lt;code&gt;w: 1&lt;/code&gt; because forcing &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; halts writes whenever the single data secondary goes down.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Align &lt;code&gt;readConcern&lt;/code&gt; with &lt;code&gt;writeConcern&lt;/code&gt;:&lt;/strong&gt; Reading from secondaries with &lt;code&gt;readConcern: &quot;local&quot;&lt;/code&gt; exposes uncommitted data; enforce &lt;code&gt;readConcern: &quot;majority&quot;&lt;/code&gt; across the cluster.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Production replica sets using &lt;code&gt;w: 1&lt;/code&gt; or PSA topologies risk silent post-failover data loss via WiredTiger RTS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Standardize on 3-node or 5-node PSS topologies and enforce &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt;, &lt;code&gt;wtimeout: 5000&lt;/code&gt;, and &lt;code&gt;readConcern: &quot;majority&quot;&lt;/code&gt; via &lt;code&gt;setDefaultRWConcern&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run &lt;code&gt;db.adminCommand({ getDefaultRWConcern: 1 })&lt;/code&gt; and &lt;code&gt;rs.conf()&lt;/code&gt; to confirm PSS topology and active cluster-wide concerns.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit &lt;code&gt;/var/lib/mongodb/rollback/&lt;/code&gt; across all nodes for historical &lt;code&gt;removed.*.bson&lt;/code&gt; files, strip any &lt;code&gt;?w=1&lt;/code&gt; overrides from application URIs, and configure PromQL alerts on &lt;code&gt;lastRollbackExecutionTime&lt;/code&gt; this week.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>MongoDB Transactions: What 4.0 Changed—and Why Transactions Don&apos;t Replace Good Document Modeling</title><link>https://rajivonai.com/blog/2026-05-28-mongodb-transactions-document-modeling/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-28-mongodb-transactions-document-modeling/</guid><description>How misusing multi-document transactions in MongoDB causes WiredTiger history store pinning, cache pressure, and WriteConflict aborts, and why single-document atomicity remains essential.</description><pubDate>Mon, 24 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Multi-document transactions in MongoDB provide ACID guarantees when required, but treating a document database like a relational engine by wrapping distributed workflows in long-running transactions guarantees storage engine starvation.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Holding multi-document transactions open—especially across external HTTP/RPC calls or hot counter documents—pins old MVCC snapshots in &lt;code&gt;WiredTigerHS.wt&lt;/code&gt;, drives dirty cache past 20%, and triggers optimistic &lt;code&gt;WriteConflict&lt;/code&gt; retry storms.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Model for &lt;strong&gt;single-document atomicity&lt;/strong&gt; first by co-locating bounded state, isolate multi-document transactions to minimal sub-second mutations with external RPCs executed beforehand, and co-locate sharded documents to avoid Two-Phase Commit (2PC).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Never execute network calls inside &lt;code&gt;withTransaction()&lt;/code&gt;, never share hot accumulator documents across concurrent transactions, and always set &lt;code&gt;maxCommitTimeMS&lt;/code&gt; with bounded exponential-backoff retries.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-28-mongodb-transactions-document-modeling.svg&quot; alt=&quot;Model for single-doc atomicity first; keep transactions short&quot;&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Production Scope: MongoDB 4.2 – 8.0 on AWS EC2&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;When MongoDB added multi-document ACID transactions for replica sets in version 4.0 and extended them to sharded clusters in 4.2, many engineering teams migrating from PostgreSQL or Oracle treated transactions as permission to import normalized multi-table schemas directly into MongoDB. Foreign keys, split parent-child tables, and multi-step workflows were wrapped in &lt;code&gt;session.startTransaction()&lt;/code&gt; blocks assuming row-level lock queues would serialize contention cleanly.&lt;/p&gt;
&lt;p&gt;In low-concurrency staging environments, those transactions pass functional tests without issue. Under production concurrency on AWS EC2, however, high-throughput services—financial ledgers, inventory reservation engines, and order state machines—encounter &lt;code&gt;WriteConflict&lt;/code&gt; retry storms, &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; disk bloat, and cascading latency spikes across unrelated non-transactional queries. The root surprise for teams coming from PostgreSQL or Oracle is lock semantics: relational &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; queues competing writers behind row locks and surfaces contention as tail latency, whereas MongoDB’s optimistic concurrency immediately aborts the losing writer with &lt;code&gt;WriteConflict&lt;/code&gt;, converting modest hot-document contention into exponential client retry amplification.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;h3 id=&quot;storage-engine-mvcc-and-locking-bottlenecks&quot;&gt;Storage Engine MVCC and Locking Bottlenecks&lt;/h3&gt;
&lt;p&gt;Unlike relational engines with pessimistic row-lock queues and dedicated undo logs, MongoDB’s transaction model relies on WiredTiger’s &lt;strong&gt;Multi-Version Concurrency Control (MVCC)&lt;/strong&gt; snapshot isolation and optimistic concurrency control. Overusing or prolonging transactions triggers four compounding bottlenecks:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;WiredTiger History Store Pinning (&lt;code&gt;WiredTigerHS.wt&lt;/code&gt;):&lt;/strong&gt; A transaction opened with &lt;code&gt;readConcern: &quot;snapshot&quot;&lt;/code&gt; pins an &lt;code&gt;oldest_timestamp&lt;/code&gt; in WiredTiger. While that transaction remains open, WiredTiger cannot discard intermediate document versions modified by &lt;em&gt;any&lt;/em&gt; concurrent writer in the cluster; it must retain those prior versions in the &lt;strong&gt;History Store&lt;/strong&gt; (&lt;code&gt;WiredTigerHS.wt&lt;/code&gt;). Consider a cluster processing 20,000 updates per second averaging 2 KB per prior document version (~40 MB/s of historical deltas): a single stalled session holding a snapshot open for 55 seconds (just under the 60-second abort limit) pins ~2.2 GB of history store versions from unrelated collections, competing directly with the ~31.5 GB WiredTiger cache on an &lt;code&gt;r6g.2xlarge&lt;/code&gt; and spilling pages onto EBS &lt;code&gt;gp3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cache Eviction Stalls and DDL Lock Queuing:&lt;/strong&gt; Because open snapshots pin historical page versions in memory, background eviction threads fall behind. Tracked dirty cache rises toward the 20% &lt;code&gt;eviction_dirty_trigger&lt;/code&gt; threshold, conscripting application worker threads into synchronous page eviction and spilling history store pages to EBS disk. Simultaneously, active transactions hold intent locks (&lt;code&gt;IS&lt;/code&gt;/&lt;code&gt;IX&lt;/code&gt;) on collections and databases for their entire lifetime; if a schema migration issues &lt;code&gt;collMod&lt;/code&gt; or &lt;code&gt;createIndex&lt;/code&gt; (requiring an exclusive &lt;code&gt;X&lt;/code&gt; lock), the DDL statement queues behind the stalled transaction and blocks all subsequent reads and writes behind it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optimistic &lt;code&gt;WriteConflict&lt;/code&gt; Retry Storms:&lt;/strong&gt; MongoDB does not queue competing writes inside transactions. If Transaction $T_1$ modifies document $D$ and concurrent Transaction $T_2$ attempts to modify $D$, $T_2$ aborts immediately with a &lt;code&gt;WriteConflict&lt;/code&gt; (&lt;code&gt;TransientTransactionError&lt;/code&gt;). When dozens of application threads retry simultaneously against a hot document, CPU consumption spikes while effective throughput collapses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hard Lifetime &amp;#x26; Cache Ceilings (&lt;code&gt;60s&lt;/code&gt;):&lt;/strong&gt; Every transaction is bounded by &lt;code&gt;transactionLifetimeLimitSeconds&lt;/code&gt; (default &lt;strong&gt;60 seconds&lt;/strong&gt;). Even though MongoDB 4.2 lifted the 4.0 single-16-MB-oplog-entry cap by splitting large transactions across multiple linked oplog entries (&lt;code&gt;prevOpTime&lt;/code&gt;), oversized uncommitted transactions still abort with &lt;code&gt;TransactionTooLargeForCache&lt;/code&gt; if they exceed WiredTiger’s cache budget.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-28-mongodb-transactions-document-modeling-2-constraints.svg&quot; alt=&quot;Long snapshots pin WiredTigerHS.wt and cause WriteConflict storms&quot;&gt;&lt;/p&gt;
&lt;p&gt;The core architectural question is: &lt;strong&gt;Why do overused multi-document transactions degrade WiredTiger throughput, and how should teams combine single-document atomicity with bounded transactions to preserve both ACID correctness and latency?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Modeling &amp;#x26; Transaction Strategy&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;&lt;code&gt;WiredTigerHS.wt&lt;/code&gt; Pinning&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Contention / &lt;code&gt;WriteConflict&lt;/code&gt; Risk&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Cross-AZ 2PC Cost&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Normalized Schema + Txns Everywhere&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High; every workflow pins snapshots&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High on shared parent/counter docs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Paid on every cross-shard write&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Starves WiredTiger cache under load&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;External RPCs Inside Transactions&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Severe; pins history for seconds&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Extreme; holds locks during HTTP waits&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Amplifies 60s lifetime aborts&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Turns downstream latency into DB outage&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Bounded Multi-Doc Txn (0 RPCs)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Minimal (milliseconds)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Low when sharded key co-located&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Paid only on rare cross-shard ops&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen for Multi-Doc:&lt;/strong&gt; Use strictly for multi-entity invariants&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Single-Document Atomicity&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero snapshot pinning&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Single-document atomic update only&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero 2PC overhead&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen Default:&lt;/strong&gt; Embed bounded state for 1-op ACID&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Using transactions everywhere with normalized schemas forces WiredTiger to maintain multi-version history for operations that could be a single atomic document update, while placing external HTTP calls inside a transaction turns every downstream microservice hiccup into a database-wide cache stall. Defaulting to &lt;strong&gt;single-document atomicity&lt;/strong&gt; and reserving &lt;strong&gt;short, RPC-free multi-document transactions&lt;/strong&gt; for genuine multi-entity invariants (like cross-account transfers) keeps snapshot lifetimes in milliseconds.&lt;/p&gt;
&lt;p&gt;Lowering a transaction’s read concern from &lt;code&gt;&quot;snapshot&quot;&lt;/code&gt; to &lt;code&gt;&quot;local&quot;&lt;/code&gt; or &lt;code&gt;&quot;majority&quot;&lt;/code&gt; does not rescue long-running workflows: uncommitted writes inside an active session still hold document locks and dirty WiredTiger cache pages until commit or abort, and reading without &lt;code&gt;&quot;snapshot&quot;&lt;/code&gt; sacrifices point-in-time consistency across statements. When a workflow must coordinate a database state change with an external API or message broker, use the &lt;strong&gt;Transactional Outbox Pattern&lt;/strong&gt;—commit the domain state and an outbox event document together in a sub-10 ms transaction (or inside a single parent document), then publish asynchronously after commit.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-28-mongodb-transactions-document-modeling-3-options.svg&quot; alt=&quot;Single-document atomicity beats distributed transactions on hot paths&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-transaction-architecture-wiredtiger-mvcc-snapshots-and-2pc&quot;&gt;The Transaction Architecture: WiredTiger MVCC, Snapshots, and 2PC&lt;/h2&gt;
&lt;p&gt;Tracing a transaction through WiredTiger MVCC and sharded Two-Phase Commit (2PC) shows why keeping transaction duration in single-digit milliseconds is critical.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-28-mongodb-transactions-document-modeling-4-architecture.svg&quot; alt=&quot;WiredTiger MVCC and 2PC multiply lock hold times across shards&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Client[&quot;Application Worker — Client Session&quot;] --&gt; Start[&quot;session.startTransaction()&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Start --&gt; ReadSnap[&quot;Acquire Snapshot Timestamp t0&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ReadSnap --&gt; Mutate[&quot;Execute Mutations — Intent Locks&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Mutate --&gt; PinHS[&quot;Pin Oldest Timestamp in WiredTigerHS.wt&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Mutate --&gt; ExtCheck{&quot;External RPC in Transaction?&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ExtCheck --&gt;|Yes — Anti-Pattern| Delay[&quot;Session Open for Seconds&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Delay --&gt; Abort60[&quot;Breach 60s Limit — Forced Abort&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ExtCheck --&gt;|No — Best Practice| Commit[&quot;session.commitTransaction()&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Commit --&gt; Conflict{&quot;WriteConflict?&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Conflict --&gt;|Yes| Retry[&quot;Rollback &amp;#x26; Retry with Backoff&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Conflict --&gt;|No| Oplog[&quot;Oplog w:majority Commit — Unpin HS&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-wiredtiger-history-store-wiredtigerhswt&quot;&gt;1. WiredTiger History Store (&lt;code&gt;WiredTigerHS.wt&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;When a document is updated, the latest committed value stays on the active in-memory B-tree page while the previous version moves to &lt;code&gt;WiredTigerHS.wt&lt;/code&gt;. Concurrent readers with an older snapshot timestamp reconcile the active page against &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; to read a consistent point-in-time image. As soon as all transactions referencing timestamp $t_0$ commit or abort, WiredTiger reclaims those historical entries.&lt;/p&gt;
&lt;p&gt;If a stalled session holds &lt;code&gt;oldest_timestamp&lt;/code&gt; open, background eviction workers still push modified pages out of RAM by writing their prior MVCC versions to &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; on disk—causing &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; to grow continuously on EBS even when memory appears steady. Worse, once that stalled transaction finally aborts at 60 seconds, WiredTiger marks the freed history blocks for internal reuse rather than truncating &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; back to the OS, leaving elevated disk consumption on the EBS volume. Keeping multi-document batches under 500 documents prevents both history-store spikes and &lt;code&gt;TransactionTooLargeForCache&lt;/code&gt; aborts.&lt;/p&gt;
&lt;h3 id=&quot;2-distributed-two-phase-commit-42&quot;&gt;2. Distributed Two-Phase Commit (4.2+)&lt;/h3&gt;
&lt;p&gt;When a transaction modifies documents on two or more shards, &lt;code&gt;mongos&lt;/code&gt; delegates coordination to a coordinator shard:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Prepare Phase:&lt;/strong&gt; The coordinator sends &lt;code&gt;prepareTransaction&lt;/code&gt; to each participant shard. Participants lock the modified documents, write a durable prepare entry to their local oplog (replicated to majority), and vote ready.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Commit Phase:&lt;/strong&gt; Once all participants prepare, the coordinator writes &lt;code&gt;commitTransaction&lt;/code&gt; and instructs participants to commit and release locks.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Because documents stay locked across both cross-AZ network round-trips, cross-shard transactions hold locks significantly longer than single-shard operations, increasing &lt;code&gt;WriteConflict&lt;/code&gt; probability under concurrency. Furthermore, if a primary stepdown or network timeout interrupts &lt;code&gt;commitTransaction()&lt;/code&gt; after the prepare phase, the driver receives &lt;code&gt;UnknownTransactionCommitResult&lt;/code&gt;. Using the driver’s callback API (&lt;code&gt;withTransaction&lt;/code&gt;) automatically retries the commit command idempotently using the same logical session ID (&lt;code&gt;lsid&lt;/code&gt;) and transaction number (&lt;code&gt;txnNumber&lt;/code&gt;), whereas hand-rolled &lt;code&gt;startTransaction()&lt;/code&gt; loops often mishandle &lt;code&gt;UnknownTransactionCommitResult&lt;/code&gt; and leave applications uncertain whether the transfer committed.&lt;/p&gt;
&lt;h3 id=&quot;3-single-document-atomicity-and-oplog-commit-buffering&quot;&gt;3. Single-Document Atomicity and Oplog Commit Buffering&lt;/h3&gt;
&lt;p&gt;Every single-document write in MongoDB (&lt;code&gt;updateOne&lt;/code&gt;, &lt;code&gt;findOneAndUpdate&lt;/code&gt;, and each individual item inside &lt;code&gt;bulkWrite&lt;/code&gt;, even with &lt;code&gt;ordered: false&lt;/code&gt;) is atomic at the WiredTiger row level, including all embedded subdocuments and arrays, so partial-document updates are never visible to concurrent readers. Conditional updates such as &lt;code&gt;updateOne({ _id: &quot;ACC_1001&quot;, balance: { $gte: 250.00 } }, { $inc: { balance: -250.00 } })&lt;/code&gt;—or &lt;code&gt;findOneAndUpdate&lt;/code&gt; with &lt;code&gt;returnDocument: &quot;after&quot;&lt;/code&gt; to read the updated state—execute an atomic compare-and-swap without opening a multi-document snapshot or pinning &lt;code&gt;WiredTigerHS.wt&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;By contrast, a multi-document transaction writes nothing to &lt;code&gt;local.oplog.rs&lt;/code&gt; until &lt;code&gt;commitTransaction()&lt;/code&gt; executes. On commit, MongoDB records all mutations together as an atomic &lt;code&gt;applyOps&lt;/code&gt; oplog entry (or a linked chain of entries via &lt;code&gt;prevOpTime&lt;/code&gt; in MongoDB 4.2+ when the transaction exceeds 16 MB), and secondaries apply the entire batch atomically at the commit timestamp so &lt;code&gt;readConcern: &quot;majority&quot;&lt;/code&gt; readers never see partial state. However, because all uncommitted writes stay buffered in the primary’s WiredTiger cache until commit, a bulk transaction touching 50,000 documents consumes gigabytes of dirty cache and risks aborting with &lt;code&gt;TransactionTooLargeForCache&lt;/code&gt; before commit ever runs.&lt;/p&gt;
&lt;h3 id=&quot;diagnostic-telemetry-serverstatustransactions-and-currentop&quot;&gt;Diagnostic Telemetry: &lt;code&gt;serverStatus().transactions&lt;/code&gt; and &lt;code&gt;currentOp&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Inspect active transactions, abort ratio, &lt;code&gt;WriteConflict&lt;/code&gt; totals, and &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; size:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ss&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;serverStatus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; txns&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ss.transactions;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; hs&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ss.wiredTiger?.[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;history-store&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`active=${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;txns&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;currentActive&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} committed=${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;txns&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;totalCommitted&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} aborted=${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;txns&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;totalAborted&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`writeConflicts=${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ss&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;metrics&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;operation&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;writeConflicts&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (hs) &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`hsMB=${&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;hs&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;bytes currently in the history store&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1048576&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Catch sessions holding transactions open (&lt;code&gt;secs_running &gt;= 2&lt;/code&gt;) and compare &lt;code&gt;timeActiveMicros&lt;/code&gt; vs &lt;code&gt;timeInactiveMicros&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;currentOp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ transaction: { $exists: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }, secs_running: { $gte: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } }).inprog.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`opid=${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;opid&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} secs=${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;secs_running&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} activeMs=${&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;transaction&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;timeActiveMicros&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} inactiveMs=${&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;transaction&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;timeInactiveMicros&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;High &lt;code&gt;timeInactiveMicros&lt;/code&gt; relative to &lt;code&gt;timeActiveMicros&lt;/code&gt; proves the application client is idle—typically waiting on an external HTTP call or slow application logic—while pinning WiredTiger history.&lt;/p&gt;
&lt;h3 id=&quot;safe-remediation-single-document-atomicity--bounded-clients&quot;&gt;Safe Remediation: Single-Document Atomicity &amp;#x26; Bounded Clients&lt;/h3&gt;
&lt;p&gt;In MongoDB, &lt;strong&gt;every single-document write is atomic by default&lt;/strong&gt;. When an account balance and a bounded recent-ledger array can live in one document (respecting array bounds via the Outlier/Bucket patterns), one &lt;code&gt;updateOne&lt;/code&gt; replaces a multi-document transaction entirely:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.accounts.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;updateOne&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ACC_1001&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balance&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { $gte: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;250.00&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    $inc: { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balance&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;250.00&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    $push: { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;recent_ledger&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { $each: [{ &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;txn_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;TXN_99182&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;amt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;250.00&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }], $slice: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When two distinct documents must be updated atomically, execute external RPCs first and wrap the pure DB mutations in &lt;code&gt;withTransaction&lt;/code&gt; with bounded retries and &lt;code&gt;maxCommitTimeMS&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; execute_transfer&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(client, src, dst, amount, max_retries&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; call_external_fraud_check(src, amount):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        raise&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; RuntimeError&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Fraud check failed&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    backoff &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0.05&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; attempt &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(max_retries):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            with&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.start_session() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; s:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; cb&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(sess):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                    accts &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sess.client.banking.accounts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                    r &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; accts.update_one({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: src, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balance&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$gte&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: amount}}, {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$inc&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balance&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;amount}}, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;session&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sess)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r.modified_count &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                        raise&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ValueError&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Insufficient funds&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                    accts.update_one({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: dst}, {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$inc&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;balance&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: amount}}, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;session&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sess)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                s.with_transaction(cb, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;read_concern&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ReadConcern(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;snapshot&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;write_concern&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;WriteConcern(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;majority&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;wtimeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;max_commit_time_ms&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        except&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (ConnectionFailure, OperationFailure) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; exc:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; exc.has_error_label(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;TransientTransactionError&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                time.sleep(backoff)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                backoff &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            elif&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; exc.has_error_label(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;UnknownTransactionCommitResult&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                raise&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;  # Verify idempotently before retrying&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                raise&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    raise&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; RuntimeError&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Circuit breaker: exceeded max transaction retries&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-28-mongodb-transactions-document-modeling-5-flow.svg&quot; alt=&quot;Externalize network RPCs and bound retries in every transaction&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Audit Open Transactions &amp;#x26; History Store:&lt;/strong&gt; Monitor &lt;code&gt;db.serverStatus().transactions&lt;/code&gt;, &lt;code&gt;wiredTiger.history-store&lt;/code&gt;, and &lt;code&gt;currentOp&lt;/code&gt; &lt;code&gt;timeInactiveMicros&lt;/code&gt; to locate long-running sessions and high-abort endpoints.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Externalize Network I/O:&lt;/strong&gt; Refactor application handlers so all HTTP calls, fraud checks, and message-queue publishes run &lt;em&gt;before&lt;/em&gt; &lt;code&gt;startTransaction()&lt;/code&gt; or asynchronously via an outbox pattern &lt;em&gt;after&lt;/em&gt; commit. In the outbox pattern, write the domain update and an &lt;code&gt;outbox&lt;/code&gt; event document inside the same sub-10 ms transaction, then tail the &lt;code&gt;outbox&lt;/code&gt; collection with a Change Stream (&lt;code&gt;watch()&lt;/code&gt; persisting &lt;code&gt;resumeAfter&lt;/code&gt; tokens) to publish downstream messages without holding WiredTiger snapshots open.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Refactor to Single-Doc Atomicity &amp;#x26; Co-Located Shard Keys:&lt;/strong&gt; Consolidate bounded 1-to-few invariants into single documents, and align sharded collection shard keys (e.g., &lt;code&gt;tenant_id&lt;/code&gt; or &lt;code&gt;account_group_id&lt;/code&gt;) so related documents reside on the same shard, avoiding 2PC.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Enforce &lt;code&gt;maxCommitTimeMS&lt;/code&gt; &amp;#x26; Circuit Breakers:&lt;/strong&gt; Cap commit waits, bound &lt;code&gt;TransientTransactionError&lt;/code&gt; retries with jittered exponential backoff, and alert if &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; bytes exceed 2 GB. Keep &lt;code&gt;maxTransactionLockRequestTimeoutMillis&lt;/code&gt; at its &lt;code&gt;5&lt;/code&gt; ms default so transactions fail fast instead of piling up behind DDL exclusive locks. On sharded clusters, also alert when &lt;code&gt;db.serverStatus().transactions.currentPrepared&lt;/code&gt; stays non-zero for more than a few seconds, as an orphaned 2PC coordinator can leave participant document locks pinned until the coordinator recovers.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Documented MongoDB and WiredTiger behaviors explain why transaction scope and schema locality govern production stability:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Snapshot Pinning in &lt;code&gt;WiredTigerHS.wt&lt;/code&gt;:&lt;/strong&gt; Per MongoDB’s transaction production considerations, open transactions prevent WiredTiger from advancing the oldest active timestamp, forcing &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; to retain every intermediate document version created by concurrent writers. Short micro-transactions release snapshots in milliseconds, whereas holding a session open across an external RPC spills gigabytes of history store pages to EBS and triggers dirty-cache eviction stalls once dirty pages approach 20% of WiredTiger cache.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;2PC Lock Duration on Sharded Clusters:&lt;/strong&gt; Per MongoDB’s sharded transaction architecture, multi-shard transactions require both a &lt;code&gt;prepareTransaction&lt;/code&gt; majority-oplog round-trip and a &lt;code&gt;commitTransaction&lt;/code&gt; round-trip across shards while holding document locks. Co-locating related entities under a shared shard key converts cross-shard 2PC into single-shard commits, cutting lock hold time and &lt;code&gt;WriteConflict&lt;/code&gt; probability.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MongoDB 7.0+ and 8.0 Transaction Improvements:&lt;/strong&gt; MongoDB 7.0+ improved WiredTiger History Store eviction behavior, and MongoDB 8.0 converted internal &lt;code&gt;config.transactions&lt;/code&gt; metadata to a clustered collection alongside general write and replication throughput optimizations—reducing baseline transaction overhead, though long open snapshots and hot-document contention still trigger the same MVCC and &lt;code&gt;WriteConflict&lt;/code&gt; limits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Primary-Only Routing and the 120-Second Driver Retry Window:&lt;/strong&gt; Every operation inside a multi-document transaction must use &lt;code&gt;readPreference: &quot;primary&quot;&lt;/code&gt;; routing a transaction to a secondary fails immediately because snapshot coordination and write-conflict detection occur on the primary. In addition, MongoDB’s official &lt;code&gt;withTransaction()&lt;/code&gt; helper retries &lt;code&gt;TransientTransactionError&lt;/code&gt; and &lt;code&gt;UnknownTransactionCommitResult&lt;/code&gt; for up to 120 seconds by default. Under a hot-document &lt;code&gt;WriteConflict&lt;/code&gt; storm, leaving that 120-second retry loop uncapped stalls application worker threads for minutes per request—which is why production services should wrap &lt;code&gt;withTransaction()&lt;/code&gt; with explicit &lt;code&gt;maxCommitTimeMS&lt;/code&gt;, bounded retry counts, or a circuit breaker.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-28-mongodb-transactions-document-modeling-6-failures.svg&quot; alt=&quot;Four transaction anti-patterns that exhaust cache and pools&quot;&gt;&lt;/p&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Operational Action&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Physical Failure Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Architectural Consequence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Safe Mitigation Protocol&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;External RPCs Inside Session&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Network wait holds snapshot open for seconds.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pins &lt;code&gt;WiredTigerHS.wt&lt;/code&gt;, stalls eviction, hits 60s abort.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run all HTTP/RPC calls before &lt;code&gt;startTransaction()&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Hot Monolithic Counter Docs&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Concurrent txns update one shared document.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Optimistic &lt;code&gt;WriteConflict&lt;/code&gt; retry storms exhaust CPU.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Shard hot counters or use single-doc &lt;code&gt;$inc&lt;/code&gt; outside txns.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Long Txns During Balancer&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Multi-shard txns hold locks during chunk moves.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Blocks chunk migrations and raises &lt;code&gt;mongos&lt;/code&gt; latency.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Co-locate data on 1 shard and schedule batches off-peak.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Massive 50,000-Doc Batches&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Uncommitted writes fill WiredTiger cache.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Aborts with &lt;code&gt;TransactionTooLargeForCache&lt;/code&gt; or 60s timeout.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chunk bulk updates into &lt;code&gt;&amp;#x3C;= 500&lt;/code&gt; doc batches.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Orphaned 2PC Prepared Locks&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Coordinator partitioned after &lt;code&gt;prepareTransaction&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Participant docs stay locked until coordinator recovers.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set &lt;code&gt;maxCommitTimeMS&lt;/code&gt; and alert &lt;code&gt;transactions.currentPrepared&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;WiredTigerHS.wt&lt;/code&gt; Disk Bloat&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Long snapshots spill history store pages to EBS.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Consumes EBS IOPS and risks disk exhaustion.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Alert when &lt;code&gt;bytes currently in the history store &gt; 2 GB&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Infinite Driver Retry Loops&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unbounded retries on &lt;code&gt;TransientTransactionError&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exhausts &lt;code&gt;maxIncomingConnections&lt;/code&gt; on primary.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cap retries with jittered backoff and circuit breakers.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;DDL During Active Txns&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;createIndex&lt;/code&gt; or &lt;code&gt;collMod&lt;/code&gt; requests exclusive lock.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Queues behind open txns and stalls incoming reads.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Restrict DDL to maintenance windows with short txns.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Single-document writes are already ACID:&lt;/strong&gt; Co-locating bounded state in one document eliminates MVCC snapshot pinning, &lt;code&gt;WriteConflict&lt;/code&gt; storms, and 2PC overhead.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never make network calls inside a transaction:&lt;/strong&gt; Any external wait pins &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; for the entire cluster and risks hitting the 60-second &lt;code&gt;transactionLifetimeLimitSeconds&lt;/code&gt; abort.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Co-locate sharded data to avoid 2PC:&lt;/strong&gt; Cross-shard transactions hold document locks across &lt;code&gt;prepareTransaction&lt;/code&gt; and &lt;code&gt;commitTransaction&lt;/code&gt; round-trips, multiplying &lt;code&gt;WriteConflict&lt;/code&gt; rates.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bound every multi-document transaction:&lt;/strong&gt; Keep batches small (&lt;code&gt;&amp;#x3C;= 500&lt;/code&gt; docs), set &lt;code&gt;maxCommitTimeMS&lt;/code&gt;, and cap &lt;code&gt;TransientTransactionError&lt;/code&gt; retries with exponential backoff.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Production clusters suffer &lt;code&gt;WriteConflict&lt;/code&gt; aborts, &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; growth, or cache eviction stalls from overused or long-running transactions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Prioritize single-document atomicity, move all external RPCs outside &lt;code&gt;startTransaction()&lt;/code&gt;, and co-locate sharded transactional entities.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Inspect &lt;code&gt;db.serverStatus().transactions&lt;/code&gt; (&lt;code&gt;totalAborted / totalStarted&lt;/code&gt;), &lt;code&gt;wiredTiger[&quot;history-store&quot;]&lt;/code&gt;, and &lt;code&gt;currentOp&lt;/code&gt; &lt;code&gt;timeInactiveMicros&lt;/code&gt; under peak load.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your top three transactional services this week for external calls inside &lt;code&gt;withTransaction()&lt;/code&gt;, enforce &lt;code&gt;maxCommitTimeMS&lt;/code&gt;, and add an alert on &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; exceeding 2 GB.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>MySQL HeatWave in Production: Operations, Autopilot, Lakehouse, and Failure Modes</title><link>https://rajivonai.com/blog/2026-08-22-mysql-heatwave-production-operations-failure-modes/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-08-22-mysql-heatwave-production-operations-failure-modes/</guid><description>A production engineering field guide to MySQL HeatWave: Autopilot machine learning internals, Lakehouse object storage execution, operational failure runbooks, and migration TCO.</description><pubDate>Sat, 22 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Operating an in-memory massively parallel processing (MPP) engine attached to a transactional database introduces a failure boundary that traditional MySQL DBAs rarely encounter. In standard InnoDB, an undersized buffer pool causes slow disk reads, elevated p99 latency, and degraded throughput, but the database stays online. In an attached in-memory columnar cluster like MySQL HeatWave, memory is a hard ceiling: if an unindexed join or a Cartesian product causes intermediate state to exceed cluster RAM, the query does not merely slow down—it either terminates with an out-of-memory error or silently falls back to the transactional primary. When dozens of concurrent analytical queries simultaneously fall back to an InnoDB primary already handling thousands of write transactions per second, the primary’s CPU pins at 100%, lock queues fill, and the entire application goes down. Operating HeatWave at staff level requires mastering the control plane automation, capacity prediction models, Lakehouse federation, and failure recovery runbooks that keep the transactional and analytical engines isolated.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Enterprise database estates are increasingly expected to support real-time operational analytics without the latency, operational fragility, and egress billing of external ETL pipelines. While Part 1 analyzed the internal dual-format architecture of MySQL HeatWave and compared its execution model with AlloyDB and Aurora, operating HeatWave in high-throughput production environments requires a completely different operational playbook than managing a standalone MySQL instance.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;The Production Operational Boundary:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[Application Transactions] ────&gt; [MySQL Primary (InnoDB)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                          │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                               (Change Propagator RPC)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                          ▼&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[Real-Time Analytics] ─────────&gt; [HeatWave In-Memory Cluster]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                          ▲&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                (Direct Vectorized Scans)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                          │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[Historical Archive] ──────────&gt; [OCI Object Storage (Lakehouse)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In production, platform teams must manage three distinct workloads against a unified MySQL endpoint:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Core OLTP Transactions:&lt;/strong&gt; High-frequency, low-latency CRUD operations mutating InnoDB on block storage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Operational Real-Time Reporting:&lt;/strong&gt; Low-latency analytical aggregations running in HeatWave cluster memory against live table replicas.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lakehouse Data Processing:&lt;/strong&gt; Batch analytical queries scanning terabytes of historical files stored in object storage (Parquet, CSV, Avro) federated with live transactional tables.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To manage this operational surface without overwhelming database teams, Oracle embedded a machine-learning control framework called &lt;strong&gt;MySQL Autopilot&lt;/strong&gt; into the HeatWave control plane. Autopilot continuously collects operational telemetry to guide cluster sizing, data placement, and query scheduling.&lt;/p&gt;
&lt;p&gt;However, automation does not eliminate physics. Senior engineers must understand how Autopilot makes internal decisions, how the Lakehouse engine bypasses the MySQL storage layer, and what operational mitigations are required when cluster nodes crash under memory pressure.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Operating an attached HTAP engine presents platform teams with three critical operational risks:&lt;/p&gt;
&lt;p&gt;First, &lt;strong&gt;capacity planning in HeatWave is strictly binary&lt;/strong&gt;. In-memory columnar tables cannot spill to local swap without causing severe cluster-wide tail latency amplification. If a database team under-provisions a HeatWave cluster by even 10%, a seasonal spike in data volume or an unpredicted analytical query shape causes the cluster to reject table loads or fail query execution. Relying on manual spreadsheet calculations for memory budgeting across compressed, partitioned columnar structures is notoriously error-prone due to unpredictable dictionary compression ratios and data distribution skews.&lt;/p&gt;
&lt;p&gt;Second, &lt;strong&gt;analytical query fallback creates a catastrophic blast radius for transactional workloads&lt;/strong&gt;. By default, if a query fails in HeatWave—due to unsupported SQL syntax, transient memory exhaustion, or network timeouts—the MySQL server’s query optimizer can fall back to executing the query on the InnoDB primary engine. When a complex query that took 1.2 seconds across 8 HeatWave nodes falls back to a single InnoDB instance, it initiates massive B+tree index scans, flushes hot transactional pages from the buffer pool, holds read views, and exhausts available MySQL client connection pools.&lt;/p&gt;
&lt;p&gt;Third, &lt;strong&gt;transactional mutations and analytical synchronization create hidden operational lag&lt;/strong&gt;. When applications execute high-volume batch updates (&lt;code&gt;UPDATE orders SET status = &apos;ARCHIVED&apos; WHERE created_at &amp;#x3C; ...&lt;/code&gt;), the Change Propagator must asynchronously encode and transmit millions of delta records across the private cluster fabric. If write rates exceed the cluster’s network ingestion bandwidth, HeatWave’s in-memory delta buffers build backlog. Analytical queries will either block waiting for delta buffers to flush or observe stale historical data.&lt;/p&gt;
&lt;p&gt;The core operational question is: &lt;strong&gt;How do database platform engineers configure Autopilot, structure Lakehouse queries, and enforce query governance to guarantee that analytical workloads never compromise the stability and latency of the underlying transactional primary?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-heatwave-production-control-plane--autopilot-architecture&quot;&gt;The HeatWave Production Control Plane &amp;#x26; Autopilot Architecture&lt;/h2&gt;
&lt;p&gt;MySQL Autopilot is an intelligent control plane embedded within the HeatWave system. Unlike traditional database query advisors that run static heuristics against historical slow logs, Autopilot uses machine learning models trained on execution telemetry collected directly from the HeatWave distributed runtime.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph Control_Plane[MySQL Autopilot Control Plane]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Telemetry[Runtime Execution Telemetry — Slices and Memory]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        MLModels[Autopilot ML Models — Gradient Boosted Regression]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Advisor[Autopilot Advisors — Provisioning, Placement, Encoding]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Scheduler[Dynamic Query Scheduler and Queue Manager]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph MySQL_Server[MySQL DB System]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Optimizer[Cost Optimizer — use_secondary_engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Catalog[Information Schema — HeatWave Metadata]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        InnoDB[InnoDB Transactional Engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph HeatWave_Nodes[HeatWave Distributed MPP Nodes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Leader[HeatWave Leader Node]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Worker1[Worker Node 1 — Slices and Delta Buffers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Worker2[Worker Node 2 — Slices and Delta Buffers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    InnoDB --&gt;|Sample Tables| Advisor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Advisor --&gt;|Recommended Shape and Keys| Catalog&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Optimizer --&gt;|Offload Compilation| Scheduler&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Scheduler --&gt;|Concurrency Gating| Leader&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Leader --&gt; Worker1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Leader --&gt; Worker2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Worker1 --&gt;|Metrics and Memory Pressure| Telemetry&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Worker2 --&gt;|Metrics and Memory Pressure| Telemetry&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Telemetry --&gt; MLModels&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MLModels --&gt; Advisor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MLModels --&gt; Scheduler&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-autopilot-core-automation-subsystems&quot;&gt;1. Autopilot Core Automation Subsystems&lt;/h3&gt;
&lt;p&gt;Autopilot automates six distinct lifecycle operations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Auto-Provisioning:&lt;/strong&gt; Before a single table is loaded into HeatWave, Autopilot executes statistical sampling against the source tables in InnoDB. It predicts the precise columnar memory footprint, taking into account dictionary encoding, run-length compression, and partition alignment. It then outputs the exact number of HeatWave nodes required to hold the working set without manual sizing guesswork.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Auto-Parallelism &amp;#x26; Query Scheduling:&lt;/strong&gt; HeatWave does not execute queries on a simple first-in, first-out basis. Autopilot estimates the execution runtime of every incoming query based on compiled plan cost. It routes queries into dynamic execution queues, preventing short, interactive dashboard queries from becoming blocked behind long-running multi-minute aggregation jobs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Auto-Encoding:&lt;/strong&gt; The efficiency of columnar execution depends on compression. High-cardinality string columns (e.g., UUIDs) waste massive memory if dictionary-encoded, while low-cardinality columns (e.g., status flags) compress dramatically. Autopilot inspects column data distributions and automatically selects optimal encodings (e.g., string dictionary, variable-byte integer encoding, or uncompressed storage) to maximize memory density and SIMD vector throughput.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Auto-Placement:&lt;/strong&gt; When analytical queries execute joins between large tables, performance is dictated by cross-node network shuffles. Autopilot inspects query history and recommends partition keys (&lt;code&gt;SECONDARY_ENGINE_ATTRIBUTE&lt;/code&gt;) across joined tables. By co-locating corresponding partitions on the same physical worker nodes, hash joins execute entirely in local slice memory, eliminating network latency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Auto-Threat &amp;#x26; Resource Governance:&lt;/strong&gt; If a submitted query’s predicted memory consumption exceeds available cluster headroom, Autopilot can reject the query before dispatching it to worker nodes, preventing out-of-memory node terminations.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-sizing-verification-and-memory-budgeting&quot;&gt;2. Sizing Verification and Memory Budgeting&lt;/h3&gt;
&lt;p&gt;Platform engineers must verify cluster sizing using the Autopilot advisor interface before enabling production workloads. The process executes entirely through standard SQL commands against the performance schema and system tables:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Step 1: Run Autopilot Auto-Provisioning Advisor against a target schema&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CALL&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; sys&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;heatwave_advisor&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;JSON_OBJECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;target_schema&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;JSON_ARRAY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;ecommerce_prod&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;mode&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;auto_shape&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Step 2: Query the recommendation table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    JSON_UNQUOTE(JSON_EXTRACT(comment, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;$.recommended_shape&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; recommended_shape,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    JSON_UNQUOTE(JSON_EXTRACT(comment, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;$.current_node_count&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; current_nodes,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    JSON_UNQUOTE(JSON_EXTRACT(comment, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;$.recommended_node_count&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; recommended_nodes,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    JSON_UNQUOTE(JSON_EXTRACT(comment, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;$.estimated_memory_gb&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; memory_required_gb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; sys&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;heatwave_advisor_report&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stage &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;auto_provisioning&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Autopilot samples the data using adaptive statistical profiling. Unlike basic row-count heuristics, Autopilot builds a regression model that accounts for data skew, string repetition factors, and intermediate hash table allocations required during multi-way joins.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;heatwave-lakehouse-internals-querying-object-storage-at-scale&quot;&gt;HeatWave Lakehouse Internals: Querying Object Storage at Scale&lt;/h2&gt;
&lt;p&gt;With &lt;strong&gt;HeatWave Lakehouse&lt;/strong&gt;, Oracle expanded the distributed execution engine beyond InnoDB tables to query data stored directly in OCI Object Storage or AWS S3. This transforms HeatWave from an in-database accelerator into a scalable cloud query engine capable of querying hundreds of terabytes without taxing the transactional database.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Lakehouse Data Pipeline:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[Raw Storage Layer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;OCI Object Storage / AWS S3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;(Parquet, CSV, Avro Files)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;           │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;           │ (Direct High-Speed Object Storage Stream)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;           ▼&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[Lakehouse Load &amp;#x26; Mapping]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;HeatWave Worker Nodes (Parallel Slices)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- Vectorized File Decoders (SIMD Parquet / Snappy Reader)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- On-the-fly Columnar Transformation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- In-Memory Partition Distribution&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;           │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;           ▼&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[Unified Query Layer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;MySQL Query Parser &amp;#x26; Optimizer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;SELECT ... FROM innodb_orders o JOIN lakehouse_historical_logs h ON o.id = h.order_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-direct-vectorized-loading-without-innodb&quot;&gt;1. Direct Vectorized Loading Without InnoDB&lt;/h3&gt;
&lt;p&gt;In traditional architectures, querying external files from MySQL requires loading data into InnoDB via &lt;code&gt;LOAD DATA INFILE&lt;/code&gt;, consuming persistent block storage, generating massive redo log volume, and bloating the buffer pool.&lt;/p&gt;
&lt;p&gt;HeatWave Lakehouse completely bypasses the InnoDB layer:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;An administrator creates an external table definition specifying the object storage URL, format (e.g., Parquet), and schema.&lt;/li&gt;
&lt;li&gt;The load command (&lt;code&gt;ALTER TABLE historical_orders SECONDARY_LOAD;&lt;/code&gt;) instructs the HeatWave cluster to read the files directly from object storage across parallel high-speed network interfaces.&lt;/li&gt;
&lt;li&gt;Each HeatWave worker node decodes its assigned file chunks using hardware-accelerated, vectorized parsers (e.g., SIMD-accelerated Snappy decompression and Parquet decoding).&lt;/li&gt;
&lt;li&gt;Data is transformed directly into HeatWave’s native in-memory columnar chunks and mapped to cluster slices.&lt;/li&gt;
&lt;li&gt;The MySQL DB System stores only the table metadata in its data dictionary; not a single byte of file data touches the MySQL primary’s disk or buffer pool.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;2-massive-horizontal-scale-out&quot;&gt;2. Massive Horizontal Scale-Out&lt;/h3&gt;
&lt;p&gt;While standard HeatWave clusters attached to a MySQL DB System typically scale up to 64 nodes, HeatWave Lakehouse supports scaling up to &lt;strong&gt;512 nodes&lt;/strong&gt;. This allows platform teams to load and process upwards of 500 terabytes of data entirely in distributed memory.&lt;/p&gt;
&lt;h3 id=&quot;3-federated-htap-queries&quot;&gt;3. Federated HTAP Queries&lt;/h3&gt;
&lt;p&gt;The most powerful capability of HeatWave Lakehouse is &lt;strong&gt;federated query execution across heterogeneous engines&lt;/strong&gt;. A single SQL query can join live transactional rows from InnoDB with petabyte-scale historical archives in Lakehouse:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Federated Join: Live InnoDB Orders + Historical Lakehouse Archives&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;region&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    COUNT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;order_id&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; live_today_orders,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    SUM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;h&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;total_amount&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; historical_archive_revenue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ecommerce_prod&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;live_orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Stored in InnoDB (Block Storage)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; lakehouse_archive&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;historical_orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; h &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Stored in Object Storage (Parquet)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;customer_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; h&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;customer_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; h&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;order_date&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;2020-01-01&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GROUP BY&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;region&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The MySQL optimizer splits this plan: the live delta is streamed from InnoDB, historical data is scanned across hundreds of Lakehouse nodes, and the final join reduction occurs entirely in HeatWave memory at sub-second speeds.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;in-practice-operational-failure-modes--emergency-runbooks&quot;&gt;In Practice: Operational Failure Modes &amp;#x26; Emergency Runbooks&lt;/h2&gt;
&lt;p&gt;Derived from the documented runtime behavior of MySQL HeatWave on OCI and AWS under high-concurrency production workloads, platform teams must prepare for four critical operational failure scenarios:&lt;/p&gt;
&lt;h3 id=&quot;failure-mode-1-cluster-out-of-memory-oom-and-fallback-storm&quot;&gt;Failure Mode 1: Cluster Out-Of-Memory (OOM) and Fallback Storm&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The Mechanism:&lt;/strong&gt; An ad-hoc query submitted by an analyst contains an unindexed cross join or a high-cardinality aggregation over millions of unique strings. Intermediate hash tables exceed available node RAM. A worker process crashes due to OOM. By default, the MySQL server detects the execution failure on the secondary engine and attempts to execute the entire query on InnoDB.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Cascade:&lt;/strong&gt; The query begins scanning 80 million rows on the InnoDB primary. The buffer pool hit ratio collapses. Disk I/O saturates. Other client connections queue up waiting for thread execution, exhausting &lt;code&gt;max_connections&lt;/code&gt; and taking down the transactional API.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Emergency Runbook:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Disable Automatic Fallback Globally:&lt;/strong&gt; Prevent failed analytical queries from ever touching the transactional primary:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Enforce strictly at the global or user level&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; GLOBAL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; use_secondary_engine &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FORCED&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
Under &lt;code&gt;FORCED&lt;/code&gt;, if HeatWave fails or cannot execute the query, MySQL immediately returns an error (&lt;code&gt;ERROR 3893 (HY000): Secondary engine operation failed&lt;/code&gt;) rather than falling back to InnoDB.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Identify Memory-Intensive Queries:&lt;/strong&gt; Check the performance schema for rejected or aborted operations:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; query_text, exec_time, error_code &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; performance_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;events_statements_history&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; errors &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sql_text &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIKE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;%use_secondary_engine%&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; timer_end &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Configure HeatWave Resource Governance:&lt;/strong&gt; Set maximum memory consumption per query and query timeouts:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; GLOBAL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; heatwave_max_query_memory &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 64424509440&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- 60 GB ceiling&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; GLOBAL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; heatwave_query_timeout &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 60&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Kill queries exceeding 60s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;h3 id=&quot;failure-mode-2-cold-cluster-hydration-latency-after-restarts&quot;&gt;Failure Mode 2: Cold Cluster Hydration Latency After Restarts&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The Mechanism:&lt;/strong&gt; Following scheduled cloud maintenance, instance resizing, or an unexpected database crash, the HeatWave cluster restarts. Although the MySQL DB System comes online quickly, the HeatWave in-memory columnar engine starts &lt;strong&gt;completely cold&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Impact:&lt;/strong&gt; Queries submitted immediately after reboot fail or run extremely slowly while tables hydrate from OCI Object Storage. If dashboards trigger hundreds of concurrent queries against cold memory, thread pools saturate.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Emergency Runbook:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Monitor Hydration Progress:&lt;/strong&gt; Query the cluster state table to confirm when all partitions are loaded:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    table_schema, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    table_name, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    load_status, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    load_progress,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    memory_size &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; performance_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;rpd_tables&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Execute Automated Warming Scripts:&lt;/strong&gt; Prioritize loading high-priority operational tables before routing application traffic:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Force asynchronous background reload&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ecommerce_prod&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SECONDARY_LOAD;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ecommerce_prod&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;order_items&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SECONDARY_LOAD;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Health Check Gating:&lt;/strong&gt; Configure application load balancers to withhold traffic from the analytical read endpoint until:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; COUNT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; performance_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;rpd_tables&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; load_status &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;AVAIL&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
returns &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;h3 id=&quot;failure-mode-3-change-propagation-replication-lag&quot;&gt;Failure Mode 3: Change Propagation Replication Lag&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The Mechanism:&lt;/strong&gt; An administrative job executes a batch update (e.g., updating user account flags across 10,000,000 rows in a single transaction). The MySQL primary commits the write to InnoDB, but the Change Propagator’s asynchronous encoding pipeline cannot process the volume of deltas fast enough to match memory ingestion limits.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Impact:&lt;/strong&gt; Analytical queries observe data staleness. If the session is configured for strict read-consistency, queries block waiting for delta buffers to flush, causing query response times to balloon.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Emergency Runbook:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Check Change Propagation Lag:&lt;/strong&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; performance_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;rpd_table_operations&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; operation_type &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;PROPAGATE&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Chunk Large Transactional Mutations:&lt;/strong&gt; Instruct development teams to break batch updates into chunked transactions with pauses to allow the Change Propagator to keep pace:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- BAD: Single massive transaction&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;PROCESSED&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;PENDING&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- GOOD: Chunked execution&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;PROCESSED&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;PENDING&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 50000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DO SLEEP(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;); &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Allow change propagation buffer to drain&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tune Propagator Batching:&lt;/strong&gt; Increase propagator thread allocation on the MySQL DB System if CPU headroom permits.&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;h3 id=&quot;failure-mode-4-ddl-schema-alterations-on-loaded-tables&quot;&gt;Failure Mode 4: DDL Schema Alterations on Loaded Tables&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The Mechanism:&lt;/strong&gt; A developer issues an &lt;code&gt;ALTER TABLE ecommerce_prod.users ADD COLUMN loyalty_tier VARCHAR(32);&lt;/code&gt; directly against a table currently loaded in HeatWave.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Impact:&lt;/strong&gt; In older HeatWave releases, any DDL on a loaded table required unloading the entire table (&lt;code&gt;SECONDARY_UNLOAD&lt;/code&gt;), executing the DDL on InnoDB, and reloading the entire dataset (&lt;code&gt;SECONDARY_LOAD&lt;/code&gt;), leaving the analytical engine without data for hours.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Emergency Runbook:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Leverage Online DDL Support:&lt;/strong&gt; In current MySQL 8.x HeatWave releases, verify whether the specific DDL operation supports online propagation:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Check DDL compatibility mode&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW VARIABLES &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIKE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;heatwave_online_ddl%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Blue/Green Table Swapping for Complex Migrations:&lt;/strong&gt; For disruptive DDL operations (e.g., changing primary keys or partition keys):
&lt;ul&gt;
&lt;li&gt;Create a shadow table: &lt;code&gt;CREATE TABLE users_new LIKE users;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Apply schema modifications to &lt;code&gt;users_new&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Backfill historical data and establish trigger/CDC synchronization.&lt;/li&gt;
&lt;li&gt;Load the shadow table into HeatWave: &lt;code&gt;ALTER TABLE users_new SECONDARY_LOAD;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Execute an atomic table rename: &lt;code&gt;RENAME TABLE users TO users_old, users_new TO users;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Drop the old table from secondary memory.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;h2 id=&quot;migration-playbook--total-cost-of-ownership-tco-reality&quot;&gt;Migration Playbook &amp;#x26; Total Cost of Ownership (TCO) Reality&lt;/h2&gt;
&lt;p&gt;Platform architects considering a migration from a multi-system analytics stack (MySQL + Debezium + Kafka + Snowflake) to MySQL HeatWave must perform a clear-eyed technical qualification.&lt;/p&gt;
&lt;h3 id=&quot;1-workload-qualification-matrix&quot;&gt;1. Workload Qualification Matrix&lt;/h3&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Workload Characteristic&lt;/th&gt;&lt;th&gt;Recommend MySQL HeatWave&lt;/th&gt;&lt;th&gt;Recommend Snowflake / Databricks / Warehouse&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Data Freshness Requirements&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Sub-second to sub-minute real-time operational reporting.&lt;/td&gt;&lt;td&gt;Hourly, daily, or overnight batch reporting.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Data Source Topology&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Single operational database or tightly coupled MySQL microservices.&lt;/td&gt;&lt;td&gt;Dozens of disparate enterprise sources (Salesforce, Stripe, SAP, Kafka streams).&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Data Transformation Architecture&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;ELT/SQL queries run directly on relational models.&lt;/td&gt;&lt;td&gt;Complex, multi-stage dbt pipelines with extensive staging and bronze/silver/gold lakes.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Write Throughput Constraints&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Write traffic fits within a single primary instance (&amp;#x3C;30,000 write TPS).&lt;/td&gt;&lt;td&gt;Massive write ingestion requiring distributed transactional ingest or lake staging.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Ad-Hoc BI vs Data Science&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Standard relational SQL, dashboards, Superset, Tableau, PowerBI.&lt;/td&gt;&lt;td&gt;Python/PySpark, ML model training pipelines, uncurated file storage.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;2-multi-cloud-heatwave-realities&quot;&gt;2. Multi-Cloud HeatWave Realities&lt;/h3&gt;
&lt;p&gt;While HeatWave originated as an OCI-exclusive service, Oracle expanded HeatWave into &lt;strong&gt;AWS&lt;/strong&gt; and &lt;strong&gt;Microsoft Azure&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;HeatWave on AWS:&lt;/strong&gt; Runs entirely within AWS infrastructure as a managed service operated by Oracle. Data resides in AWS availability zones; applications connecting from AWS EC2 or ECS communicate over low-latency private endpoints without crossing cloud boundaries, avoiding inter-cloud latency and egress fees.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;HeatWave on Azure:&lt;/strong&gt; Deployed through Oracle Database@Azure, running OCI Exadata and HeatWave hardware co-located inside Microsoft Azure datacenters connected via dedicated ultra-low-latency private interconnects (&amp;#x3C;2ms latency).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Architectural Tradeoff:&lt;/strong&gt; Operating HeatWave outside native OCI simplifies multi-cloud enterprise compliance and eliminates external egress costs, but control plane administration remains bound to Oracle’s cloud management APIs.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;To ensure architectural honesty, platform teams must evaluate where HeatWave hits hard production boundaries:&lt;/p&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure Dimension&lt;/th&gt;&lt;th&gt;Operational Constraint&lt;/th&gt;&lt;th&gt;Production Impact&lt;/th&gt;&lt;th&gt;Staff-Level Architectural Verdict&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Memory Wall&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Working set must reside entirely in cluster RAM.&lt;/td&gt;&lt;td&gt;Cannot scale analytics beyond financial/budgetary capacity to provision RAM.&lt;/td&gt;&lt;td&gt;Not a substitute for cold, tiered data lakes. Store long-tail historical archives in Lakehouse object storage, not in-memory HeatWave nodes.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;No Horizontal Write Scaling&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;All &lt;code&gt;INSERT&lt;/code&gt;/&lt;code&gt;UPDATE&lt;/code&gt; transactions execute on a single MySQL instance.&lt;/td&gt;&lt;td&gt;High-throughput write workloads saturate single-node CPU, IOPS, or binlog locks.&lt;/td&gt;&lt;td&gt;If your primary constraint is scaling transactional writes, HeatWave will not help. Use Vitess or Cloud Spanner.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Vendor Engine Lock-in&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;HeatWave’s &lt;code&gt;RAPID&lt;/code&gt; engine is proprietary to Oracle Cloud.&lt;/td&gt;&lt;td&gt;You cannot run HeatWave self-managed on vanilla EC2, GCE, or bare-metal Linux.&lt;/td&gt;&lt;td&gt;Porting away from HeatWave requires re-introducing a CDC pipeline and external data warehouse. Ensure schema DDL remains standard MySQL.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Complex Analytical SQL Surface&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Certain complex recursive CTEs, specialized window functions, or legacy UDFs are unsupported.&lt;/td&gt;&lt;td&gt;Unsupported queries fall back to InnoDB or fail outright if &lt;code&gt;FORCED&lt;/code&gt; is set.&lt;/td&gt;&lt;td&gt;Audit every BI query plan in staging with &lt;code&gt;EXPLAIN&lt;/code&gt; before committing to migration.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Private Interconnect Latency&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Cross-cloud deployments (e.g., AWS app to OCI HeatWave) introduce network latency.&lt;/td&gt;&lt;td&gt;Query dispatch and result-set retrieval suffer added latency.&lt;/td&gt;&lt;td&gt;Co-locate compute and database within the same cloud provider fabric (use native HeatWave on AWS or Azure co-location).&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;For engineering leadership and database platform architects preparing for production adoption:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Assess whether your organization is spending excessive engineering hours and cloud budget maintaining brittle CDC/Kafka/warehouse pipelines solely to serve sub-minute operational reports against MySQL data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Adopt MySQL HeatWave to consolidate operational reporting onto your existing MySQL infrastructure, using an attached MPP cluster to completely isolate analytical CPU/memory load from your InnoDB primary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run the MySQL Autopilot Auto-Provisioning Advisor against a sanitized clone of your production database. Benchmark memory requirements, identify optimal partition keys, and execute side-by-side performance runs with &lt;code&gt;use_secondary_engine = FORCED&lt;/code&gt; to ensure zero transactional interference.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Implement strict query governance before opening the cluster to analysts: set &lt;code&gt;heatwave_max_query_memory&lt;/code&gt;, establish query timeouts, enforce &lt;code&gt;use_secondary_engine = FORCED&lt;/code&gt; to prevent disastrous InnoDB fallbacks, and automate cluster hydration monitoring in your SRE observability dashboards.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>MongoDB Data Modeling Reviews: Embedding, Referencing, Arrays, and Document Growth</title><link>https://rajivonai.com/blog/2026-05-24-mongodb-data-modeling-reviews/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-24-mongodb-data-modeling-reviews/</guid><description>How unbounded arrays in MongoDB documents trigger WiredTiger write amplification, shrink oplog windows, and cause BSONObjectTooLarge outages, and how to remediate them.</description><pubDate>Fri, 21 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;In document database engineering, the rule that data accessed together should be stored together becomes a storage-engine outage the moment relationship cardinality shifts from one-to-few to one-to-unbounded.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Appending 200-byte items via &lt;code&gt;$push&lt;/code&gt; into multi-megabyte documents forces WiredTiger to reconcile and re-compress entire leaf/overflow pages every 60-second checkpoint, inflating EBS write IOPS and risking fatal 16 MB &lt;code&gt;BSONObjectTooLarge&lt;/code&gt; rejections.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Bound inline arrays with the &lt;strong&gt;Outlier Pattern&lt;/strong&gt; (for power-law distributions) or &lt;strong&gt;Bucket Pattern&lt;/strong&gt; / native Time Series (for continuous streams), and enforce &lt;code&gt;maxItems&lt;/code&gt; in the catalog via &lt;code&gt;$jsonSchema&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Never deploy &lt;code&gt;validationLevel: &quot;strict&quot;&lt;/code&gt; before draining legacy oversized documents—start with &lt;code&gt;validationLevel: &quot;moderate&quot;&lt;/code&gt; and &lt;code&gt;validationAction: &quot;warn&quot;&lt;/code&gt;, and pre-index &lt;code&gt;parent_id&lt;/code&gt; on overflow collections.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-24-mongodb-data-modeling-reviews.svg&quot; alt=&quot;Bound every array before WiredTiger reconciliation amplifies writes&quot;&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Production Scope: MongoDB 5.0 – 8.0 on AWS EC2 + EBS &lt;code&gt;gp3&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Engineering teams adopt MongoDB because its JSON/BSON document model maps directly to domain objects, letting an application fetch an entity and its child attributes in a single index lookup without SQL joins. For one-to-few relationships—such as an order with three shipping addresses or a user profile with five payment methods—embedding delivers predictable single-digit millisecond latency.&lt;/p&gt;
&lt;p&gt;In high-concurrency e-commerce, IoT telemetry, and financial event platforms, entities such as product reviews, activity feeds, and sensor readings are frequently modeled as nested arrays inside a parent document. On staging datasets, documents remain small and return in single-digit milliseconds. In production on AWS EC2 (&lt;code&gt;r6g.2xlarge&lt;/code&gt; with 64 GB RAM and EBS &lt;code&gt;gp3&lt;/code&gt; volumes), active entities accumulate thousands of array entries, turning sub-kilobyte records into multi-megabyte blobs that degrade WiredTiger cache residency, shrink replica set oplog retention, and eventually hit MongoDB’s hard 16 MB BSON ceiling (&lt;code&gt;16,777,216&lt;/code&gt; bytes).&lt;/p&gt;
&lt;p&gt;When a single parent document grows to several megabytes, every &lt;code&gt;$push&lt;/code&gt; operation ceases to be a lightweight append. Instead, the storage engine’s checkpoint reconciliation loop, the replication oplog, and the B-tree index cache all pay a compounding penalty proportional to the total document size rather than the size of the newly appended element.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;h3 id=&quot;mechanics-of-the-unbounded-array-trap&quot;&gt;Mechanics of the Unbounded Array Trap&lt;/h3&gt;
&lt;p&gt;Developers often assume &lt;code&gt;$push&lt;/code&gt; executes an in-place incremental append on disk. In WiredTiger and MongoDB replication, appending to a large document triggers four compounding constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;WiredTiger Checkpoint Write Amplification:&lt;/strong&gt; WiredTiger stores documents in row-store B-tree leaf pages (typically 4 KB to 32 KB allocation units, with overflow pages whenever a single document exceeds page size). In-memory edits prepend a &lt;code&gt;WT_UPDATE&lt;/code&gt; delta structure to the row’s atomic update chain, which appears inexpensive at write time. However, every 60 seconds (or 2 GB of journal writes), the checkpoint thread reconciles dirty pages by reading the base document, resolving the entire &lt;code&gt;WT_UPDATE&lt;/code&gt; chain, serializing the full BSON document, compressing it with Snappy or Zstandard, and writing new extents to EBS while freeing old extents. Appending 1,000 items of 200 bytes (200 KB logical) to an 8 MB document forces ~8 MB of physical reconciliation—a &lt;code&gt;40x+&lt;/code&gt; write amplification factor that saturates EBS &lt;code&gt;gp3&lt;/code&gt; throughput (125 MB/s baseline).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Working Set Cache Eviction:&lt;/strong&gt; When an application reads a parent document only to check a scalar field (like &lt;code&gt;price&lt;/code&gt; or &lt;code&gt;stock&lt;/code&gt;), WiredTiger loads the entire multi-megabyte document—including its oversized array—into the internal cache (default 50% of &lt;code&gt;RAM - 1 GB&lt;/code&gt;, or ~31.5 GB on an &lt;code&gt;r6g.2xlarge&lt;/code&gt;). A handful of multi-megabyte documents evicts gigabytes of hot B-tree index pages, triggering synchronous EBS read page faults across unrelated queries. Even if the application uses a projection (&lt;code&gt;{ price: 1 }&lt;/code&gt;), &lt;code&gt;mongod&lt;/code&gt; must still read the full BSON document from WiredTiger into memory unless the query is 100% covered by an index (&lt;code&gt;PROJECTION_COVERED&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oplog Window Compression and Multikey Index Amplification:&lt;/strong&gt; Every mutation writes to &lt;code&gt;local.oplog.rs&lt;/code&gt;. Continuous updates to bloated documents and frequent array rewrites consume oplog bytes faster, shrinking the effective replication retention window and leaving secondaries vulnerable to falling into &lt;code&gt;RECOVERING&lt;/code&gt; state during network partitions or maintenance restarts. Worse, if any subfield inside the unbounded array is indexed, MongoDB creates a &lt;strong&gt;multikey index&lt;/strong&gt; entry for every array element; a document with 10,000 array items generates 10,000 B-tree keys, turning document deletes or array rewrites into thousands of synchronous B-tree mutations and prohibiting any second array field in the same compound index (&lt;code&gt;cannot index parallel arrays&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Hard 16 MB BSON Ceiling (&lt;code&gt;16,777,216&lt;/code&gt; bytes):&lt;/strong&gt; MongoDB enforces a strict 16 MB limit on any single BSON document. Once a viral entity’s array crosses 16 MB, every subsequent &lt;code&gt;$push&lt;/code&gt; fails with &lt;code&gt;BSONObjectTooLarge&lt;/code&gt;—blocking writes on the highest-traffic entities in the system. Furthermore, in sharded clusters, oversized documents near 16 MB create indivisible jumbo chunks that the balancer cannot split or migrate across shards. Even before 16 MB, transferring multi-megabyte documents over EC2 ENIs consumes network bandwidth and forces larger application-heap allocations during BSON deserialization.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-24-mongodb-data-modeling-reviews-2-constraints.svg&quot; alt=&quot;A 200-byte $push forces megabytes of WiredTiger checkpoint re-encoding&quot;&gt;&lt;/p&gt;
&lt;p&gt;The core architectural question is: &lt;strong&gt;How can platform engineers bound document growth at the database catalog level and refactor unbounded arrays into Outlier or Bucket schemas without application write downtime?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Pattern&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Read Queries (99% Path)&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Checkpoint Write Amp&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;History Retention&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unbounded Embedding&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;1 lookup&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Extreme (&lt;code&gt;40x+&lt;/code&gt; on multi-MB docs)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Full until 16 MB crash&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Guarantees &lt;code&gt;BSONObjectTooLarge&lt;/code&gt; and EBS saturation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;$push&lt;/code&gt; with &lt;code&gt;$slice: -50&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;1 lookup&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Still re-encodes full doc on every &lt;code&gt;$push&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Discards older items permanently&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected for High Velocity:&lt;/strong&gt; Loses history and still dirties pages&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Pure Referencing (Normalized)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2+ queries or &lt;code&gt;$lookup&lt;/code&gt; always&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Minimal (&lt;code&gt;1x&lt;/code&gt; small child insert)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Full&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Adds extra network RTT to every common read&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Outlier &amp;#x26; Bucket Patterns&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;1 lookup for 99% of entities&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bounded to compact ~64 KB docs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Full (overflow or sequential buckets)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen:&lt;/strong&gt; Preserves 1-query fast path while capping document size&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Unbounded embedding fails at 16 MB, &lt;code&gt;$push&lt;/code&gt; with &lt;code&gt;$slice&lt;/code&gt; discards historical records while still forcing full-document checkpoint rewrites on high-velocity entities, and pure referencing penalizes the 99% normal-cardinality read path with extra round trips or server-side &lt;code&gt;$lookup&lt;/code&gt; joins. The &lt;strong&gt;Outlier Pattern&lt;/strong&gt; and &lt;strong&gt;Bucket Pattern&lt;/strong&gt; keep documents compact in WiredTiger cache while preserving single-query reads for the common case. When evaluating &lt;code&gt;$push&lt;/code&gt; with &lt;code&gt;$slice&lt;/code&gt;, remember that &lt;code&gt;$slice&lt;/code&gt; only caps the logical array length in BSON; if an entity receives hundreds of &lt;code&gt;$push&lt;/code&gt; updates per minute, WiredTiger still reconciles and re-compresses the entire document page every checkpoint cycle.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-24-mongodb-data-modeling-reviews-3-options.svg&quot; alt=&quot;Outlier and Bucket patterns bound size without losing read speed&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-bounded-document-architecture-outlier-and-bucket-patterns&quot;&gt;The Bounded Document Architecture: Outlier and Bucket Patterns&lt;/h2&gt;
&lt;p&gt;Choosing between Outlier and Bucket depends on whether the workload is &lt;strong&gt;power-law entity growth&lt;/strong&gt; (most entities small, a few massive) or &lt;strong&gt;continuous sequential streams&lt;/strong&gt; (all entities append indefinitely).&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-24-mongodb-data-modeling-reviews-4-architecture.svg&quot; alt=&quot;Outlier and Bucket patterns keep documents compact in cache&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WriteReq[&quot;Application Write — Append Item ($push)&quot;] --&gt; Gate{&quot;Modeling Pattern Gate&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Gate --&gt;|Unbounded Array| Unbounded[&quot;Single Document — Grows to 16 MB&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Unbounded --&gt; Crash[&quot;Checkpoint Amplification &amp;#x26; BSONObjectTooLarge&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Gate --&gt;|Continuous Stream| Bucket[&quot;Bucket Pattern — Fixed 500 Items per Doc&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Bucket --&gt; Compact[&quot;Predictable ~64 KB Doc &amp;#x26; Tiny Oplog Deltas&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Gate --&gt;|Power-Law Outliers| Outlier[&quot;Outlier Pattern — has_outlier_overflow flag&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Outlier --&gt; Inline[&quot;First 50 Items Inline in Parent&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Outlier --&gt; Overflow[&quot;Extras Routed to reviews_overflow Collection&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-the-outlier-pattern-for-power-law-distributions&quot;&gt;1. The Outlier Pattern for Power-Law Distributions&lt;/h3&gt;
&lt;p&gt;In e-commerce catalogs and social feeds, 99% of items have fewer than 50 child records while 0.1% of viral items accumulate tens of thousands. Under the Outlier Pattern:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Keep up to 50 recent items inline in the parent document alongside &lt;code&gt;has_outlier_overflow: false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Once the inline array reaches 50 items, set &lt;code&gt;has_outlier_overflow: true&lt;/code&gt; and insert all subsequent items as standalone documents in a dedicated &lt;code&gt;products_reviews_overflow&lt;/code&gt; collection indexed on &lt;code&gt;{ parent_id: 1, created_at: -1 }&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Standard product page reads execute a single &lt;code&gt;findOne&lt;/code&gt; on &lt;code&gt;products&lt;/code&gt;; only outlier pages where &lt;code&gt;has_outlier_overflow&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; trigger a paginated query against the overflow collection. For deep pagination inside the overflow collection, use keyset pagination (&lt;code&gt;{ parent_id: id, created_at: { $lt: last_ts } }&lt;/code&gt;) backed by &lt;code&gt;{ parent_id: 1, created_at: -1 }&lt;/code&gt; rather than &lt;code&gt;skip()&lt;/code&gt;, which scans and discards skipped index entries on every page.&lt;/li&gt;
&lt;li&gt;To prevent the parent document and overflow collection from drifting during network failures, either update &lt;code&gt;has_outlier_overflow&lt;/code&gt; and insert the overflow document inside a multi-document ACID transaction, or run an idempotent reconciler that scans for &lt;code&gt;reviews.49&lt;/code&gt; existing without a matching overflow flag.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-the-bucket-pattern-for-continuous-event-streams&quot;&gt;2. The Bucket Pattern for Continuous Event Streams&lt;/h3&gt;
&lt;p&gt;For IoT telemetry, audit trails, or financial event streams, group measurements into fixed-capacity buckets (e.g., 500 readings or 1 hour per document) with pre-aggregated summary metrics (&lt;code&gt;temp_min&lt;/code&gt;, &lt;code&gt;temp_max&lt;/code&gt;, &lt;code&gt;temp_avg&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;DEVICE_9912_B_0042&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;device_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;DEVICE_9912&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;bucket_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;42&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;count&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;500&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;metrics&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;temp_min&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;68.2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;temp_max&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;74.5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;temp_avg&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;71.3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;readings&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [ { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ts&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ISODate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;2026-05-01T00:00:01Z&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;val&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;71.2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When &lt;code&gt;count&lt;/code&gt; reaches 500, the writer opens &lt;code&gt;bucket_id: 43&lt;/code&gt; (using an atomic &lt;code&gt;findOneAndUpdate&lt;/code&gt; with &lt;code&gt;upsert: true&lt;/code&gt; filtered on &lt;code&gt;{ device_id: &quot;DEVICE_9912&quot;, count: { $lt: 500 } }&lt;/code&gt;). Older buckets become immutable on disk, keeping active WiredTiger leaf pages compact (~64 KB) and oplog entries tiny. Pre-aggregating &lt;code&gt;metrics&lt;/code&gt; inside each bucket also lets dashboard queries read summary statistics without scanning individual &lt;code&gt;readings&lt;/code&gt; arrays.&lt;/p&gt;
&lt;p&gt;Sizing the bucket capacity is an explicit tradeoff between index B-tree reduction and WiredTiger checkpoint cost. If each event is ~120 bytes, 500 events per bucket yields ~60 KB BSON documents and reduces index B-tree entries by &lt;code&gt;500x&lt;/code&gt; compared to one document per event; pushing bucket size to 10,000 events (~1.2 MB) reintroduces multi-page checkpoint rewrites, whereas shrinking buckets below 50 events increases index overhead. For pure time-series workloads on MongoDB 5.2+, native &lt;strong&gt;Time Series collections&lt;/strong&gt; automate this bucketing internally with columnar compression (delta, delta-of-delta, RLE, object, and 6.0+ array compression) inside buckets, provided &lt;code&gt;metaField&lt;/code&gt; cardinality stays bounded. For custom bucket or overflow collections with monotonically ordered keys on MongoDB 5.3+, creating the collection with &lt;code&gt;clusteredIndex: { key: { _id: 1 }, unique: true }&lt;/code&gt; stores documents directly inside the &lt;code&gt;_id&lt;/code&gt; B-tree leaf pages, eliminating the secondary &lt;code&gt;_id&lt;/code&gt; index lookup on range scans.&lt;/p&gt;
&lt;h3 id=&quot;diagnostic-telemetry-bson-size-sampling-and-reslen-auditing&quot;&gt;Diagnostic Telemetry: BSON Size Sampling and &lt;code&gt;reslen&lt;/code&gt; Auditing&lt;/h3&gt;
&lt;p&gt;Sample document BSON sizes with &lt;code&gt;Object.bsonsize()&lt;/code&gt; before enforcing schema caps, and compare against &lt;code&gt;db.products.stats().avgObjSize&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.products.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aggregate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;([{ $sample: { size: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } }]).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;doc&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; bytes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Object.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;bsonsize&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(doc);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (bytes &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`_id=${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;doc&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;_id&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} sizeMB=${&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;bytes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1048576&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} reviews=${&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;doc&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;reviews&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; []).&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Because &lt;code&gt;avgObjSize&lt;/code&gt; hides power-law skew when 99% of documents are 2 KB and 0.1% are 14 MB, &lt;code&gt;$sample&lt;/code&gt; combined with &lt;code&gt;Object.bsonsize()&lt;/code&gt; exposes the heavy tail. Pair sampling with &lt;code&gt;mongod.log&lt;/code&gt; &lt;code&gt;reslen&lt;/code&gt; filtering to catch oversized payloads already hitting the wire and consuming network egress:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;grep&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Slow query&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/log/mongodb/mongod.log&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; jq&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -r&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;select(.attr.reslen &gt; 2097152) | {ns: .attr.ns, reslen_mb: (.attr.reslen / 1048576), ms: .attr.durationMillis}&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;engine-level-enforcement-via-jsonschema&quot;&gt;Engine-Level Enforcement via &lt;code&gt;$jsonSchema&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Enforce array bounds in the catalog with &lt;code&gt;collMod&lt;/code&gt; so batch jobs and microservices cannot bypass application validation:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;runCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  collMod: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;products&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  validator: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    $jsonSchema: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      bsonType: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;object&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      required: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sku&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;title&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      properties: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        reviews: { bsonType: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;array&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, maxItems: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        has_outlier_overflow: { bsonType: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;bool&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  validationLevel: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;moderate&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  validationAction: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;warn&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;automated-overflow-auditor-mongo_doc_auditorpy&quot;&gt;Automated Overflow Auditor (&lt;code&gt;mongo_doc_auditor.py&lt;/code&gt;)&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env python3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse, time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; bson &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; BSON&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; migrate_overflow&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(client, namespace, array_field, max_items, dry_run):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    db_name, coll_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; namespace.split(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    db &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client[db_name]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    src, dst &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db[coll_name], db[&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;coll_name&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;array_field&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;_overflow&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; doc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; src.find({&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;array_field&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;max_items&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$exists&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}}):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        arr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; doc.get(array_field, [])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;doc[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;_id&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; items=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(arr)&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; bytes=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;BSON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.encode(doc))&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dry_run:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            continue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        dst.insert_many([{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;parent_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: doc[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;idx&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: max_items &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; i, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;payload&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: v} &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; i, v &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; enumerate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(arr[max_items:])])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        src.update_one({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: doc[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]}, {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$set&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {array_field: arr[:max_items], &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;has_outlier_overflow&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        time.sleep(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0.05&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Throttle writes to protect oplog window&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-24-mongodb-data-modeling-reviews-5-flow.svg&quot; alt=&quot;Online schema bounding migrates legacy arrays without downtime&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Index Overflow Collection &amp;#x26; Audit:&lt;/strong&gt; Pre-create &lt;code&gt;{ parent_id: 1, idx: 1 }&lt;/code&gt; (or &lt;code&gt;{ parent_id: 1, created_at: -1 }&lt;/code&gt;) on the overflow collection, and run the dry-run auditor (&lt;code&gt;{ &quot;reviews.50&quot;: { $exists: true } }&lt;/code&gt;) to count oversized documents. Checking &lt;code&gt;reviews.50&lt;/code&gt; via &lt;code&gt;$exists: true&lt;/code&gt; uses dot-notation array indexing without downloading the full array payload across the network.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Deploy &lt;code&gt;moderate&lt;/code&gt; + &lt;code&gt;warn&lt;/code&gt; Validation:&lt;/strong&gt; Apply &lt;code&gt;$jsonSchema&lt;/code&gt; with &lt;code&gt;validationLevel: &quot;moderate&quot;&lt;/code&gt; and &lt;code&gt;validationAction: &quot;warn&quot;&lt;/code&gt;. Invalid writes succeed while logging detailed violation warnings to &lt;code&gt;mongod.log&lt;/code&gt; so you can catch un-updated writers. Under &lt;code&gt;validationLevel: &quot;moderate&quot;&lt;/code&gt;, MongoDB only enforces schema rules on inserts and on updates to documents that &lt;em&gt;already&lt;/em&gt; satisfied the validator prior to the update; legacy documents that already exceed 50 items remain exempt from the &lt;code&gt;maxItems: 50&lt;/code&gt; check during unrelated scalar updates (such as inventory or price changes) until their arrays are drained below 50.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Throttled Backfill &amp;#x26; Code Cutover:&lt;/strong&gt; Deploy application code that routes items past 50 to the overflow collection, then run the throttled migrator (&lt;code&gt;time.sleep&lt;/code&gt; between batches) while watching secondary replication lag. Never run unthrottled &lt;code&gt;updateMany&lt;/code&gt; loops across millions of documents, as the resulting oplog surge can exhaust &lt;code&gt;local.oplog.rs&lt;/code&gt; retention.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Enforce &lt;code&gt;error&lt;/code&gt; and &lt;code&gt;strict&lt;/code&gt;:&lt;/strong&gt; Switch &lt;code&gt;validationAction&lt;/code&gt; to &lt;code&gt;&quot;error&quot;&lt;/code&gt; (which returns Error 121 &lt;code&gt;DocumentValidationFailure&lt;/code&gt; with &lt;code&gt;errInfo.details&lt;/code&gt; pinpointing &lt;code&gt;schemaRulesNotSatisfied&lt;/code&gt; and &lt;code&gt;propertiesNotSatisfied&lt;/code&gt;). Once zero legacy documents exceed &lt;code&gt;maxItems&lt;/code&gt;, promote &lt;code&gt;validationLevel&lt;/code&gt; to &lt;code&gt;&quot;strict&quot;&lt;/code&gt;. Rollback: set &lt;code&gt;validationAction: &quot;warn&quot;&lt;/code&gt; via &lt;code&gt;collMod&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Documented MongoDB and WiredTiger storage behaviors govern both the write-amplification problem and the remediation tradeoffs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;WiredTiger Page Reconciliation:&lt;/strong&gt; Per WiredTiger’s row-store architecture, updates to an in-memory document append &lt;code&gt;WT_UPDATE&lt;/code&gt; deltas, but checkpoint reconciliation re-serializes and re-compresses the full document leaf/overflow page on disk. Bounding documents to compact sizes via Outlier or Bucket patterns directly reduces the number of bytes reconciled per checkpoint and prevents single documents from occupying overflow pages. Keeping documents compact also preserves WiredTiger cache capacity for hot B-tree index pages rather than caching multi-megabyte historical arrays.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oplog Retention &amp;#x26; Batch Pruning:&lt;/strong&gt; Because &lt;code&gt;local.oplog.rs&lt;/code&gt; records every document mutation, unthrottled batch scripts trimming millions of oversized arrays can flood the oplog and shrink retention hours for secondaries. Rate-limiting migration batches and monitoring &lt;code&gt;rs.status()&lt;/code&gt; lag preserves replication headroom.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MongoDB 5.2+ – 8.0 Schema &amp;#x26; Time Series Features:&lt;/strong&gt; MongoDB 5.2+ added columnar bucket compression (delta, delta-of-delta, RLE, object, and 6.0+ array compression) for native Time Series collections, and MongoDB 5.3+ added Clustered Collections (storing documents in &lt;code&gt;_id&lt;/code&gt; B-tree order to eliminate separate &lt;code&gt;_id&lt;/code&gt; index lookups on bucketed range scans). On schema enforcement, MongoDB returns structured &lt;code&gt;errInfo.details&lt;/code&gt; (&lt;code&gt;schemaRulesNotSatisfied&lt;/code&gt;, &lt;code&gt;propertiesNotSatisfied&lt;/code&gt;) on Error 121 &lt;code&gt;DocumentValidationFailure&lt;/code&gt;, while MongoDB 8.0 slow logs surface &lt;code&gt;workingMillis&lt;/code&gt; alongside &lt;code&gt;totalTimeQueuedMicros&lt;/code&gt; to separate validation CPU cost from lock-queue wait time.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-24-mongodb-data-modeling-reviews-6-failures.svg&quot; alt=&quot;Four data-modeling remediation traps that cause new outages&quot;&gt;&lt;/p&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Operational Pattern&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Physical Failure Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Architectural Consequence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Safe Mitigation Protocol&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;validationLevel: &quot;strict&quot;&lt;/code&gt; Too Early&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforcing &lt;code&gt;maxItems&lt;/code&gt; while legacy oversized docs exist.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unrelated writes to legacy docs fail with Error 121.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Start with &lt;code&gt;&quot;moderate&quot;&lt;/code&gt; + &lt;code&gt;&quot;warn&quot;&lt;/code&gt;, drain arrays, then tighten.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unindexed Overflow Foreign Keys&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Missing index on &lt;code&gt;parent_id&lt;/code&gt; in overflow collection.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Paginated overflow reads trigger &lt;code&gt;COLLSCAN&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pre-build &lt;code&gt;{ parent_id: 1, created_at: -1 }&lt;/code&gt; before cutover.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;In-Memory Bucket Merging&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;App loads 50 buckets into RAM before paginating.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Spikes JVM/Node.js heap and GC pauses.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Stream cursors and paginate at bucket boundaries.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Oversized Docs in Sharded Clusters&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Multi-MB documents prevent chunk splitting.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Creates indivisible jumbo chunks and shard skew.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bound document growth before enabling sharding.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Outlier Flag Desync&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;has_outlier_overflow: true&lt;/code&gt; set without overflow insert.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;UI shows “Load More” that returns empty results.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Wrap parent update + overflow insert in a transaction or idempotent job.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unthrottled Batch Array Trimming&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Script trims millions of arrays at full speed.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Oplog surge pushes secondaries into &lt;code&gt;RECOVERING&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rate-limit batches and monitor secondary lag continuously.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;$push&lt;/code&gt; with &lt;code&gt;$slice&lt;/code&gt; High Velocity&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Assuming &lt;code&gt;$slice: -50&lt;/code&gt; avoids checkpoint rewrites.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Still reconciles and re-compresses the document on disk.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use Bucket Pattern or Time Series for append-heavy streams.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;High-Cardinality &lt;code&gt;metaField&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Time Series &lt;code&gt;metaField&lt;/code&gt; has millions of unique values.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Proliferates internal buckets and exhausts WT cache.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep &lt;code&gt;metaField&lt;/code&gt; bounded to low/medium-cardinality entity IDs.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;WiredTiger checkpoints rewrite full document pages:&lt;/strong&gt; Even small &lt;code&gt;$push&lt;/code&gt; deltas force full-document reconciliation and compression on disk, amplifying EBS IOPS on multi-megabyte records.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;16 MB BSON is a hard cliff:&lt;/strong&gt; Unbounded arrays inevitably fail with &lt;code&gt;BSONObjectTooLarge&lt;/code&gt; on your highest-value, most active entities.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Match pattern to cardinality:&lt;/strong&gt; Use the &lt;strong&gt;Outlier Pattern&lt;/strong&gt; for Zipfian power-law entities and the &lt;strong&gt;Bucket Pattern&lt;/strong&gt; (or native Time Series) for continuous event streams.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Roll out &lt;code&gt;$jsonSchema&lt;/code&gt; in stages:&lt;/strong&gt; Start with &lt;code&gt;validationLevel: &quot;moderate&quot;&lt;/code&gt; and &lt;code&gt;validationAction: &quot;warn&quot;&lt;/code&gt;, pre-index overflow collections, and throttle legacy array draining to protect the oplog.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Identify collections with unbounded arrays that drive WiredTiger checkpoint write amplification, cache eviction, and &lt;code&gt;BSONObjectTooLarge&lt;/code&gt; risk.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Refactor power-law arrays to the Outlier Pattern, move continuous event streams to the Bucket Pattern or native Time Series, and enforce &lt;code&gt;maxItems&lt;/code&gt; via &lt;code&gt;$jsonSchema&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run &lt;code&gt;Object.bsonsize()&lt;/code&gt; across &lt;code&gt;$sample&lt;/code&gt; batches and filter &lt;code&gt;mongod.log&lt;/code&gt; for &lt;code&gt;reslen &gt; 2097152&lt;/code&gt; (2 MB).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your top three write-heavy collections this week, deploy &lt;code&gt;validationLevel: &quot;moderate&quot;&lt;/code&gt; + &lt;code&gt;&quot;warn&quot;&lt;/code&gt; on any array-bearing schema, and pre-index overflow collections before migrating legacy records.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>Chaos Engineering for Cassandra: Surviving Data Center Outages with Chaos Monkey</title><link>https://rajivonai.com/blog/2026-08-20-netflix-chaos-monkey-cassandra-resilience/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-08-20-netflix-chaos-monkey-cassandra-resilience/</guid><description>How Netflix uses Chaos Monkey to continuously test Apache Cassandra resilience, and how automated remediation prevents node failures from becoming outages.</description><pubDate>Thu, 20 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In a distributed system, failures are not anomalies; they are guarantees. If you wait for a hardware degradation or network partition to test your database’s resilience, you are testing in production during an active outage.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Historically, database administrators protected database nodes at all costs. In a modern cloud environment like AWS, virtual machines can be terminated without warning, network links drop packets, and entire Availability Zones can experience brownouts. A typical Cassandra cluster might be configured with a Replication Factor of 3 and queries using &lt;code&gt;LOCAL_QUORUM&lt;/code&gt;, which theoretically survives the loss of a single node. However, without continuous validation, configuration drift, driver bugs, or hidden anti-entropy failures can cause a single node loss to cascade into a severe user-facing outage.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra’s masterless architecture is built for high availability, but stateful systems introduce complex failure modes. When a node suddenly dies:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Client drivers must quickly detect the failure and route around the dead node.&lt;/li&gt;
&lt;li&gt;The remaining replicas must buffer writes for the downed node using Hinted Handoff.&lt;/li&gt;
&lt;li&gt;If the node is permanently lost, an operator must provision a new node and stream terabytes of data to it without starving client read/write paths.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If teams do not practice this exact sequence, the “theoretical” high availability fails. Manual intervention is often too slow, and operators under pressure make mistakes, such as streaming too much data concurrently or misconfiguring token ranges, causing the cluster to collapse under the weight of its own recovery process.&lt;/p&gt;
&lt;h2 id=&quot;automated-remediation-via-chaos&quot;&gt;Automated Remediation via Chaos&lt;/h2&gt;
&lt;p&gt;The documented pattern, pioneered by Netflix’s Cloud Database Engineering team, is to invert the problem: instead of trying to prevent node failures, you intentionally and continuously inject them during business hours using a tool like &lt;strong&gt;Chaos Monkey&lt;/strong&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CM[Chaos Monkey] --&gt;|Terminates EC2 Instance| N1[Cassandra Node - DOWN]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;Automated Remediation Loop&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Monitor[Telemetry / Control Plane] --&gt;|Detects Node Loss| AutoScaling[Auto Scaling Group]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        AutoScaling --&gt;|Provisions New EC2| NNew[New Cassandra Node]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        NNew --&gt;|Bootstraps| TokenRing[Cassandra Token Ring]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        TokenRing --&gt;|Zero-Copy Streaming| NNew&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    N1 -.-&gt; Monitor&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By scheduling Chaos Monkey to randomly terminate Cassandra instances, teams are forced to build automated remediation. When a node is killed, the control plane immediately detects the termination, triggers the provisioning of a replacement instance, attaches the appropriate EBS volumes (or bootstraps a fresh node), and rejoins the cluster—all without human intervention.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern is to start small and incrementally increase the blast radius:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Driver Resilience Validation:&lt;/strong&gt; Before killing nodes, teams use tools like AWS Fault Injection Simulator to inject network latency and verify that client driver timeouts and retry policies correctly fall back to healthy replicas.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Instance Termination:&lt;/strong&gt; Chaos Monkey is enabled to kill a single Cassandra node during peak hours. If the automated remediation script fails to replace the node within a defined SLA, an alert fires and the engineering team fixes the automation, not just the node.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Zone Failures (Chaos Gorilla):&lt;/strong&gt; The ultimate test involves simulating the loss of an entire AWS Availability Zone, verifying that the &lt;code&gt;NetworkTopologyStrategy&lt;/code&gt; correctly placed replicas and that the surviving zones can handle the 33% loss of capacity while maintaining &lt;code&gt;LOCAL_QUORUM&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;During a major AWS Xen hypervisor security patch in September 2014, 218 of Netflix’s 2,700+ production Cassandra nodes were forced to reboot, and 22 failed to come back online. Because the team had been running Chaos Monkey continuously for the prior year, the automated remediation systems detected and replaced the failed nodes with minimal human intervention, and Netflix reported zero downtime that weekend — documented by Christos Kalantzis (Netflix’s Cloud Database Engineering Manager at the time) in Netflix’s own account of the event.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Random Failures&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Terminating nodes during off-hours when engineers are asleep causes burnout.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Restrict Chaos Monkey to run strictly during business hours when the team is available.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unbounded Blast Radius&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Terminating multiple nodes in the same replica set will break &lt;code&gt;LOCAL_QUORUM&lt;/code&gt; and cause data unavailability.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Configure Chaos Monkey to only terminate one instance per cluster/cluster-zone per day.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;High Density Nodes&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;If you pack 10 TB of data on a single node, the bootstrap streaming time after termination will take days.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep SSTable density below 2 TB per node to ensure fast automated recovery.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Assuming Cassandra is highly available without testing it leads to extended outages when inevitable hardware failures occur.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Deploy Chaos Monkey to randomly terminate database nodes during business hours to force the development of automated remediation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Netflix’s continuous chaos testing allowed their Cassandra clusters to survive massive AWS reboot events without human intervention or downtime.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before deploying Chaos Monkey, review your client driver configurations. Ensure your timeout, retry, and load balancing policies are explicitly configured to handle connection drops gracefully.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&quot;technical-references&quot;&gt;Technical References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://medium.com/netflix-techblog/introducing-chaos-to-c-4a031a0703ce&quot;&gt;Introducing Chaos to C* — Netflix Technology Blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.infoq.com/news/2014/10/netflix-cassandra/&quot;&gt;How Netflix Handled the Reboot of 218 Cassandra Nodes — InfoQ&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>Top 10 Apache Cassandra Use Cases and Architectures</title><link>https://rajivonai.com/blog/2026-08-20-top-10-apache-cassandra-use-cases-architectures/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-08-20-top-10-apache-cassandra-use-cases-architectures/</guid><description>A deep dive into the top production workloads where Apache Cassandra&apos;s masterless architecture excels, from time-series IoT to real-time recommendation engines.</description><pubDate>Thu, 20 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When teams evaluate distributed databases, they often try to force relational models into NoSQL systems. Apache Cassandra is not a drop-in replacement for PostgreSQL. It is a highly specialized, masterless wide-column store designed for linear scalability, continuous availability, and massive write throughput. If your workload fits its design, it will handle petabytes effortlessly; if it doesn’t, it will become an operational nightmare.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Engineering teams frequently hit vertical scaling limits on traditional RDBMS systems when ingesting millions of events per second or serving global read traffic. The reflex is to adopt Cassandra. However, without understanding the specific workloads Cassandra was built for, teams end up designing data models that require complex joins or multi-partition transactions—features Cassandra intentionally omits to guarantee availability under partition tolerance (AP in the CAP theorem).&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra’s architecture utilizes a masterless ring, consistent hashing, and an append-only Log-Structured Merge (LSM) tree. It excels at fast, sequential writes and single-partition reads. It fundamentally breaks when asked to perform relational operations, aggregations across the cluster, or distributed locking.&lt;/p&gt;
&lt;p&gt;To succeed with Cassandra, you must map your problem directly to its core strengths: write-heavy ingestion, time-series data, and globally distributed always-on access.&lt;/p&gt;
&lt;h2 id=&quot;the-top-architectures&quot;&gt;The Top Architectures&lt;/h2&gt;
&lt;p&gt;Here are the top documented use cases where Cassandra provides outsized engineering value.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Ingest[High Velocity Ingestion] --&gt; Memtable[Memtable - RAM]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Memtable --&gt; CommitLog[Commit Log - Disk]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Memtable --&gt; SSTable[SSTable - Immutable Disk]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;Top Workload Patterns&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        SSTable --&gt; TS[1. Time-Series &amp;#x26; IoT]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        SSTable --&gt; Fraud[2. Real-Time Fraud Detection]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        SSTable --&gt; Recs[3. Recommendation Engines]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        SSTable --&gt; Msg[4. Messaging &amp;#x26; Chat]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-iot-and-time-series-sensor-data&quot;&gt;1. IoT and Time-Series Sensor Data&lt;/h3&gt;
&lt;p&gt;Industrial sensors, smart thermostats, and telemetry systems generate relentless write streams. Cassandra’s LSM tree architecture absorbs these writes entirely in memory (Memtable) and sequential disk appends (Commit Log) before flushing immutable files (SSTables) to disk. Using &lt;code&gt;TimeWindowCompactionStrategy&lt;/code&gt; (TWCS), operators can drop entire buckets of old sensor data efficiently without the massive I/O overhead of standard compactions.&lt;/p&gt;
&lt;h3 id=&quot;2-personalization-and-recommendation-engines&quot;&gt;2. Personalization and Recommendation Engines&lt;/h3&gt;
&lt;p&gt;Netflix and Spotify have both publicly documented using Cassandra to store real-time user state, viewing/listening history, and interaction events (see Technical References below). The database must answer “What should this user watch next?” in milliseconds. Cassandra’s predictable read latency on primary key lookups makes it the backbone for serving these ML-generated recommendations.&lt;/p&gt;
&lt;h3 id=&quot;3-e-commerce-shopping-carts-and-catalogs&quot;&gt;3. E-Commerce Shopping Carts and Catalogs&lt;/h3&gt;
&lt;p&gt;During Black Friday, a database outage means lost revenue. E-commerce platforms use Cassandra for shopping carts and inventory state because its masterless design guarantees that a node failure will not block writes. Tunable consistency allows teams to accept writes locally (&lt;code&gt;LOCAL_ONE&lt;/code&gt;) during extreme duress while reading at higher consistency (&lt;code&gt;LOCAL_QUORUM&lt;/code&gt;) later.&lt;/p&gt;
&lt;h3 id=&quot;4-messaging-and-social-feeds&quot;&gt;4. Messaging and Social Feeds&lt;/h3&gt;
&lt;p&gt;Inbox searches, chat histories, and social feeds are essentially massive, partitioned lists of time-ordered events. Cassandra’s wide-column model allows developers to append new messages to a user’s partition and efficiently read the most recent N messages using clustering orders. Instagram has run Cassandra in production since 2012 for its Feed, Direct messaging, and fraud-detection workloads, and later built a custom RocksDB-backed storage engine (“Rocksandra”) on top of it for a documented 3-4x P99 read-latency improvement. Discord ran trillions of messages on Cassandra for years before migrating to ScyllaDB in 2022, citing exactly the failure mode covered elsewhere in this post: hot partitions and GC-driven tail latency under concurrent read load at their scale (both companies’ own engineering blogs document this — see Technical References). The lesson from Discord’s move isn’t “don’t use Cassandra for messaging” — it’s that this use case’s ceiling is real and partition-key design matters more here than almost anywhere else.&lt;/p&gt;
&lt;h3 id=&quot;5-high-speed-fraud-detection&quot;&gt;5. High-Speed Fraud Detection&lt;/h3&gt;
&lt;p&gt;Financial institutions route transaction streams through stream processors (like Apache Flink) that cross-reference historical user profiles stored in Cassandra. The database provides sub-millisecond lookups for user behavior profiles, allowing the system to approve or reject credit card swipes in real-time.&lt;/p&gt;
&lt;h3 id=&quot;6-distributed-identity-and-session-management&quot;&gt;6. Distributed Identity and Session Management&lt;/h3&gt;
&lt;p&gt;Global applications require user sessions to be valid regardless of which datacenter the user connects to. Cassandra’s native multi-datacenter replication ensures that when a user logs in via &lt;code&gt;us-east-1&lt;/code&gt;, their session token is asynchronously replicated to &lt;code&gt;eu-west-1&lt;/code&gt;, surviving regional AWS outages.&lt;/p&gt;
&lt;h3 id=&quot;7-aiml-feature-stores&quot;&gt;7. AI/ML Feature Stores&lt;/h3&gt;
&lt;p&gt;Machine learning models require low-latency access to pre-computed features during inference. Cassandra acts as an online feature store, serving highly concurrent read requests for user embeddings or contextual data directly to the inference endpoints. This isn’t a niche pattern: the 2025 Apache Cassandra community survey (2,500+ practitioners) found 31% of AI-focused respondents are building feature stores that serve both training and inference pipelines, and 68% of respondents overall are already using or planning to use Cassandra for AI workloads.&lt;/p&gt;
&lt;h3 id=&quot;8-vector-search-and-generative-ai-cassandra-50&quot;&gt;8. Vector Search and Generative AI (Cassandra 5.0+)&lt;/h3&gt;
&lt;p&gt;With the introduction of Storage-Attached Indexing (SAI) and a native vector data type in Cassandra 5.0, the database supports approximate-nearest-neighbor similarity search directly in CQL — teams can store text chunks alongside high-dimensional embeddings in the same table and query both without bolting on a separate specialized vector database. This is now the single largest driver of new Cassandra adoption: the same 2025 survey found 42% of AI-focused respondents building RAG (Retrieval-Augmented Generation) applications and 29% powering conversational AI that needs fast access to conversation history and context — with AI use cases overall projected to triple by 2026. Two real constraints from the official docs worth knowing before you commit: you cannot change SAI index settings without dropping and rebuilding the index, and Cassandra’s own guidance is to create the vector index &lt;em&gt;before&lt;/em&gt; loading data, since building it concurrently with a data load is unsupported and slow.&lt;/p&gt;
&lt;h3 id=&quot;9-product-catalogs-and-inventory&quot;&gt;9. Product Catalogs and Inventory&lt;/h3&gt;
&lt;p&gt;Massive, multi-tenant SaaS platforms store complex, hierarchical product catalogs. Cassandra’s flexible schema allows for sparse columns, meaning a clothing item can have “size” and “color” attributes while a hardware tool has “voltage”, without requiring massive schema migrations or NULL columns.&lt;/p&gt;
&lt;h3 id=&quot;10-audit-logging-and-event-sourcing&quot;&gt;10. Audit Logging and Event Sourcing&lt;/h3&gt;
&lt;p&gt;Immutable event logs require a database that can ingest data infinitely without performance degradation. Cassandra’s append-only nature makes it ideal for compliance logging, where updates and deletes are rare, but writes must be durably stored and highly available.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern across industry deployments is that successful Cassandra adoption requires strict adherence to its workload constraints. For example, Netflix has publicly documented their use of Cassandra to power their global personalization and recommendation engines, and Spotify has separately documented choosing Cassandra for the same reason in their own personalization storage layer. In both documented architectures, the database handles massive write throughput from user interaction streams, which are subsequently served back to the user interface via single-partition primary-key lookups with low-latency reads.&lt;/p&gt;
&lt;p&gt;When organizations deviate from these core use cases, the database behavior degrades predictably. If a team attempts to use Cassandra for a workload requiring heavy transactional joins or distributed locking, the application must perform these operations on the client side, leading to severe network amplification and cascading timeouts.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Relational Data Modeling&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Designing Cassandra tables like a relational DB leads to massive client-side joins and network timeouts.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Adopt query-first modeling. Design one table per specific query access pattern.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unbounded Partitions&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Storing all sensor data for a year in a single partition creates a “wide row” that crashes nodes during compaction.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Introduce temporal bucketing (e.g., partition by &lt;code&gt;sensor_id&lt;/code&gt; + &lt;code&gt;month&lt;/code&gt;).&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Heavy Secondary Indexing&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Traditional secondary indexes in Cassandra require cluster-wide scatter-gather queries, destroying performance.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use Storage-Attached Indexing (SAI) in 5.0+, or redesign the primary key to duplicate data into query-specific tables. Never use materialized views in production.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Attempting to use Cassandra as a general-purpose relational database results in severe performance degradation and operational failure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Map Cassandra strictly to its core strengths: write-heavy ingestion, time-series, high availability, and massive horizontal scale.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Global platforms rely on Cassandra’s LSM tree and masterless ring to survive traffic spikes and AWS regional outages effortlessly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before adopting Cassandra, document your application’s exact read and write queries. If you cannot satisfy your access patterns without multi-table joins or ACID transactions, choose a different database.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&quot;technical-references&quot;&gt;Technical References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://medium.com/netflix-techblog/system-architectures-for-personalization-and-recommendation-e081aa94b5d8&quot;&gt;System Architectures for Personalization and Recommendation — Netflix Technology Blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://engineering.atspotify.com/2015/01/personalization-at-spotify-using-cassandra&quot;&gt;Personalization at Spotify using Cassandra — Spotify Engineering&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://instagram-engineering.com/tagged/cassandra&quot;&gt;Cassandra – Instagram Engineering&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://discord.com/blog/how-discord-stores-trillions-of-messages&quot;&gt;How Discord Stores Trillions of Messages — Discord Engineering Blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://patrickmcfadin.com/articles/ai-use-cases-triple-cassandra-2025/&quot;&gt;AI Use Cases Set to Triple: What the Apache Cassandra Community Survey Reveals — Patrick McFadin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cassandra.apache.org/doc/latest/cassandra/vector-search/concepts.html&quot;&gt;Vector Search Concepts — Apache Cassandra Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;claims-requiring-additional-human-validation&quot;&gt;Claims Requiring Additional Human Validation&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;The 2025 Apache Cassandra community survey statistics (31% feature stores, 42% RAG, 29% conversational AI, 68% overall AI adoption) come from a single author’s blog post summarizing the survey, not the raw survey data itself — worth confirming against the original Apache Cassandra project source if one is published separately.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>ai-engineering</category></item><item><title>Building Indexes on Billion-Document Collections Without Breaking Production</title><link>https://rajivonai.com/blog/2026-05-23-building-indexes-on-billion-document-collections-without-breaking-production/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-23-building-indexes-on-billion-document-collections-without-breaking-production/</guid><description>How to orchestrate hybrid index builds on multi-terabyte collections in MongoDB on AWS EC2, manage memory sort spills, size oplog windows, and enforce commit quorum.</description><pubDate>Wed, 19 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Building an index on a billion documents is an operational stress test that exercises every layer of database infrastructure: filesystem page caches, WiredTiger eviction threads, &lt;code&gt;_tmp&lt;/code&gt; disk sort spills, and replica set oplog retention.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Building an index on a multi-terabyte collection with the default 200 MB sort buffer (&lt;code&gt;maxIndexBuildMemoryUsageMegabytes&lt;/code&gt;) spills hundreds of run files to &lt;code&gt;dbPath/_tmp&lt;/code&gt;, saturating EBS &lt;code&gt;gp3&lt;/code&gt; IOPS and pushing lagging secondaries off the oplog window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Pre-calculate temporary disk space ($S_{\text{temp}} \times 2.0$), resize the oplog online via &lt;code&gt;replSetResizeOplog&lt;/code&gt; to at least &lt;code&gt;2.5x&lt;/code&gt; estimated build duration, raise &lt;code&gt;maxIndexBuildMemoryUsageMegabytes&lt;/code&gt; (e.g., 4,096 MB), and run with &lt;code&gt;commitQuorum: &quot;majority&quot;&lt;/code&gt; (or use a rolling standalone build on IOPS-constrained tiers).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Default &lt;code&gt;commitQuorum&lt;/code&gt; is &lt;code&gt;&quot;votingMembers&quot;&lt;/code&gt;—not &lt;code&gt;&quot;majority&quot;&lt;/code&gt;—so one offline voting secondary stalls commit indefinitely unless overridden via &lt;code&gt;setIndexCommitQuorum&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-23-building-indexes-on-billion-document-collections-without-breaking-production.svg&quot; alt=&quot;Size oplog, sort memory, and commit quorum before building at billion-row scale&quot;&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Production Scope: MongoDB 4.4 – 8.0 on AWS EC2 + EBS &lt;code&gt;gp3&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Enterprise MongoDB replica sets on AWS EC2 (&lt;code&gt;r6g.4xlarge&lt;/code&gt; with 128 GB RAM and EBS &lt;code&gt;gp3&lt;/code&gt; volumes) routinely host transactional, telemetry, and ledger collections exceeding one billion documents and multiple terabytes of uncompressed data. When product teams add new composite query filters, executing them without a matching compound index triggers collection scans that evict hot working-set pages from WiredTiger cache.&lt;/p&gt;
&lt;p&gt;Creating a new B-tree index on a 3 TB collection requires scanning every document, extracting and sorting billions of keys, and writing a new index file—all while serving live reads and writes under strict p99 latency budgets. Without pre-flight capacity math and replication safeguards, the build itself triggers I/O starvation or forces secondaries into &lt;code&gt;RECOVERING&lt;/code&gt; state.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;h3 id=&quot;the-concurrency-and-replication-collision&quot;&gt;The Concurrency and Replication Collision&lt;/h3&gt;
&lt;p&gt;Prior to MongoDB 4.2, operators chose between foreground builds (which held an exclusive collection lock, halting all reads and writes) and background builds (which yielded to traffic but were single-threaded and produced fragmented B-trees). MongoDB 4.4 unified index creation into a &lt;strong&gt;simultaneous replicated hybrid build&lt;/strong&gt; that holds only brief locks at the start and commit phases. However, at billion-document scale, three physical constraints still break production clusters:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;External Sort Memory Exhaustion (&lt;code&gt;_tmp&lt;/code&gt; Disk Spill):&lt;/strong&gt; Key extraction sorts all generated index keys using a buffer governed by &lt;code&gt;maxIndexBuildMemoryUsageMegabytes&lt;/code&gt; (default &lt;strong&gt;200 MB&lt;/strong&gt; per &lt;code&gt;createIndexes&lt;/code&gt; command in MongoDB 4.4 through 8.0). Once extracted keys exceed 200 MB, MongoDB flushes sorted run files to &lt;code&gt;dbPath/_tmp/&lt;/code&gt;. On baseline EBS &lt;code&gt;gp3&lt;/code&gt; volumes (3,000 IOPS, 125 MB/s), multi-pass merge sorting across hundreds of run files saturates disk queues.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Side Writes Table Cache Pressure:&lt;/strong&gt; While the collection scan traverses existing records, concurrent inserts, updates, and deletes are intercepted into an internal &lt;strong&gt;Side Writes Table&lt;/strong&gt; backed by a WiredTiger structure. During a multi-hour scan under heavy write traffic, intercepted mutations consume WiredTiger cache and spill to temporary storage if memory limits are reached.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oplog Window Rollover on Secondaries:&lt;/strong&gt; Replicated builds run simultaneously on the primary and all secondaries. Each secondary must execute the index scan and merge sort while simultaneously applying the live replication oplog stream. If secondary replication lag widens beyond &lt;code&gt;local.oplog.rs&lt;/code&gt; retention, the secondary falls off the oplog (&lt;code&gt;RECOVERING&lt;/code&gt;) and requires a multi-day initial sync.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-23-building-indexes-on-billion-document-collections-without-breaking-production-2-constraints.svg&quot; alt=&quot;Simultaneous builds can push lagging secondaries off the oplog&quot;&gt;&lt;/p&gt;
&lt;p&gt;The core engineering question is: &lt;strong&gt;How can platform engineers build compound indexes on billion-document collections without exhausting &lt;code&gt;_tmp&lt;/code&gt; disk space, saturating EBS IOPS, or pushing secondaries past the oplog retention window?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Strategy&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Traffic Impact&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;EBS I/O Profile&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Failover / Quorum Risk&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Legacy Foreground Build (Pre-4.2)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exclusive lock halts all reads &amp;#x26; writes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High burst on primary, then secondaries&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Causes immediate application outage&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Unacceptable downtime; removed in 4.4+&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Replicated Hybrid (Default &lt;code&gt;votingMembers&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Concurrent reads &amp;#x26; writes allowed&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Simultaneous across all nodes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Single offline secondary blocks commit&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected as Default:&lt;/strong&gt; Risky if any voting node drops&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Replicated Hybrid (&lt;code&gt;commitQuorum: &quot;majority&quot;&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Concurrent reads &amp;#x26; writes; zero stepdowns&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Simultaneous; requires &lt;code&gt;2.5x&lt;/code&gt; oplog + 4 GB sort RAM&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Tolerates minority node outage&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen:&lt;/strong&gt; Standard protocol when EBS IOPS have headroom&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rolling Standalone Secondary Build&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Concurrent; requires planned &lt;code&gt;stepDown&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Isolated to 1 node at a time off-cluster&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires careful voting quorum &amp;#x26; priority management&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen for Tight IOPS:&lt;/strong&gt; Best when EBS cannot sustain simultaneous sort&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Legacy foreground builds are unusable for 24/7 systems, and the default &lt;code&gt;commitQuorum: &quot;votingMembers&quot;&lt;/code&gt; stalls indefinitely if a single secondary restarts or lags offline. Teams should choose &lt;strong&gt;Replicated Hybrid with &lt;code&gt;commitQuorum: &quot;majority&quot;&lt;/code&gt;&lt;/strong&gt; when EBS IOPS and oplog headroom can absorb simultaneous builds, and &lt;strong&gt;Rolling Standalone Builds&lt;/strong&gt; when EBS throughput is tightly constrained or when validating unique indexes without risking a late primary abort.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-23-building-indexes-on-billion-document-collections-without-breaking-production-3-options.svg&quot; alt=&quot;Replicated hybrid builds vs rolling standalone secondary builds&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-hybrid-index-build-architecture-phases-side-writes-and-quorum&quot;&gt;The Hybrid Index Build Architecture: Phases, Side Writes, and Quorum&lt;/h2&gt;
&lt;p&gt;A replicated hybrid build coordinates state across the primary and all secondaries through four distinct phases over &lt;code&gt;local.oplog.rs&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-23-building-indexes-on-billion-document-collections-without-breaking-production-4-architecture.svg&quot; alt=&quot;Hybrid build coordinates scan, side writes, and commit quorum via oplog&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Client[&quot;Application Ingest — Concurrent Writes&quot;] --&gt; Primary[&quot;mongod Primary Node&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt; Init[&quot;Phase 1: Catalog Init — Intent Lock &amp;#x26; UUID&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Init --&gt; Scan[&quot;Phase 2: Collection Scan — Cursor Read&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Scan --&gt; ExtSort[&quot;Phase 3: External Sort — Spill to _tmp&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ExtSort --&gt; Drain[&quot;Phase 4: Side Writes Drain — Apply Buffered Writes&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Client -.-&gt;|Intercepted Writes| SideTable[&quot;Side Writes Table&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SideTable -.-&gt; Drain&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt;|Oplog startIndexBuild| Sec1[&quot;Secondary 1 — Simultaneous Build&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt;|Oplog startIndexBuild| Sec2[&quot;Secondary 2 — Simultaneous Build&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Drain --&gt; Vote[&quot;Primary Ready — Awaiting Quorum&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Sec1 --&gt;|voteCommitIndexBuild| Vote&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Sec2 --&gt;|voteCommitIndexBuild| Vote&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Vote --&gt;|commitQuorum majority| Commit[&quot;Phase 5: commitIndexBuild — Index Ready&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;deep-mechanics-of-the-four-phases&quot;&gt;Deep Mechanics of the Four Phases&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Catalog Init:&lt;/strong&gt; The primary acquires a brief intent lock, assigns a build UUID, and writes &lt;code&gt;startIndexBuild&lt;/code&gt; to &lt;code&gt;local.oplog.rs&lt;/code&gt;. Secondaries read this entry and launch local index build threads simultaneously.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Collection Scan &amp;#x26; Side Writes Interception:&lt;/strong&gt; Each node scans its local collection via storage cursors. Concurrent writes targeting the collection are intercepted into the &lt;strong&gt;Side Writes Table&lt;/strong&gt; (a temporary WiredTiger structure) so the scan sees a consistent baseline without holding a collection-wide write lock.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — External Key Sort &amp;#x26; Merge:&lt;/strong&gt; Extracted keys fill the &lt;code&gt;maxIndexBuildMemoryUsageMegabytes&lt;/code&gt; buffer. When full, sorted runs flush to &lt;code&gt;dbPath/_tmp/&lt;/code&gt;. After the scan finishes, MongoDB runs an N-way merge sort and inserts ordered keys sequentially into the new B-tree file (&lt;code&gt;index-*.wt&lt;/code&gt;), avoiding random page splits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Side Writes Drain &amp;#x26; Commit Quorum:&lt;/strong&gt; The builder drains all intercepted mutations from the Side Writes Table into the B-tree, then writes &lt;code&gt;voteCommitIndexBuild&lt;/code&gt; to the oplog. Once votes satisfy &lt;code&gt;commitQuorum&lt;/code&gt; (default &lt;code&gt;&quot;votingMembers&quot;&lt;/code&gt;, or &lt;code&gt;&quot;majority&quot;&lt;/code&gt; when explicitly configured), the primary writes &lt;code&gt;commitIndexBuild&lt;/code&gt; and all nodes mark the index &lt;code&gt;ready: true&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Note on restarts: starting in MongoDB 4.4, &lt;strong&gt;clean shutdowns&lt;/strong&gt; (&lt;code&gt;SIGTERM&lt;/code&gt; or &lt;code&gt;db.shutdownServer()&lt;/code&gt;) checkpoint hybrid build progress to disk and resume automatically on restart when &lt;code&gt;commitQuorum&lt;/code&gt; is &lt;code&gt;&quot;votingMembers&quot;&lt;/code&gt; or &lt;code&gt;&quot;majority&quot;&lt;/code&gt;. Only &lt;strong&gt;unclean crashes&lt;/strong&gt; (&lt;code&gt;SIGKILL&lt;/code&gt;, OOM killer, power loss) discard &lt;code&gt;_tmp&lt;/code&gt; and restart the build from zero.&lt;/p&gt;
&lt;h3 id=&quot;sizing-and-capacity-engineering-formulas&quot;&gt;Sizing and Capacity Engineering Formulas&lt;/h3&gt;
&lt;h4 id=&quot;1-temporary-disk-space-s_texttemp&quot;&gt;1. Temporary Disk Space ($S_{\text{temp}}$)&lt;/h4&gt;
&lt;p&gt;During Phase 3, external sort run files occupy temporary disk space in &lt;code&gt;dbPath/_tmp/&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;$$S_{\text{temp}} = N_{\text{documents}} \times \text{AvgKeySize} \times C_{\text{overhead}}$$&lt;/p&gt;
&lt;p&gt;For $N_{\text{documents}} = 1,000,000,000$, $\text{AvgKeySize} \approx 64\text{ bytes}$ (e.g., &lt;code&gt;{ account_id: UUID, created_at: Date, status: String }&lt;/code&gt;), and framing overhead $C_{\text{overhead}} \approx 1.35$:&lt;/p&gt;
&lt;p&gt;$$S_{\text{temp}} = 1,000,000,000 \times 64\text{ B} \times 1.35 \approx 86.4\text{ GB}$$&lt;/p&gt;
&lt;p&gt;Ensure free disk space on the EBS volume hosting &lt;code&gt;dbPath&lt;/code&gt; exceeds $S_{\text{temp}} \times 2.0$ (at least ~175 GB free for this example). The $2.0\times$ multiplier is non-negotiable because during Phase 3’s N-way external merge sort, the &lt;code&gt;_tmp&lt;/code&gt; run files and the newly populated &lt;code&gt;index-*.wt&lt;/code&gt; B-tree file coexist on disk simultaneously until the merge completes and &lt;code&gt;_tmp&lt;/code&gt; is unlinked. Relying on MongoDB’s default &lt;code&gt;indexBuildMinAvailableDiskSpaceMB&lt;/code&gt; (500 MB) as a safety net is too late—by the time free space drops to 500 MB, the node is already at risk of oplog or journal write failure.&lt;/p&gt;
&lt;h4 id=&quot;2-required-oplog-retention-window-t_textoplog&quot;&gt;2. Required Oplog Retention Window ($T_{\text{oplog}}$)&lt;/h4&gt;
&lt;p&gt;Size &lt;code&gt;local.oplog.rs&lt;/code&gt; to hold at least $2.5\times$ the estimated build duration ($T_{\text{build}}$):&lt;/p&gt;
&lt;p&gt;$$S_{\text{oplog}} = \text{WriteRate}&lt;em&gt;{\text{MB/s}} \times (T&lt;/em&gt;{\text{build}} \times 2.5) \times 3,600\text{ s/hr}$$&lt;/p&gt;
&lt;p&gt;At $2.5\text{ MB/s}$ average write ingest and an estimated 24-hour build ($60\text{ hours}$ retention):&lt;/p&gt;
&lt;p&gt;$$S_{\text{oplog}} = 2.5\text{ MB/s} \times 60\text{ hr} \times 3,600\text{ s/hr} = 540,000\text{ MB} \approx 540\text{ GB}$$&lt;/p&gt;
&lt;h3 id=&quot;diagnostic-telemetry-currentop-and-oplog-window-monitoring&quot;&gt;Diagnostic Telemetry: &lt;code&gt;currentOp&lt;/code&gt; and Oplog Window Monitoring&lt;/h3&gt;
&lt;p&gt;Track active build phase, completion percentage, and runtime via &lt;code&gt;db.currentOp()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;currentOp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;command.createIndexes&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { $exists: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } }).inprog.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; p&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; op.progress &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; pct&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; p.total &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ((p.done &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; p.total) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;N/A&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`opid=${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;opid&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} msg=&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;msg&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot; progress=${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;pct&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}% hours=${&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;secs_running&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3600&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Monitor oplog retention hours and secondary lag continuously:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rs.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; primary&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; status.members.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.stateStr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;PRIMARY&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; oplog&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getSiblingDB&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;local&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).oplog.rs;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; first&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oplog.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ $natural: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;limit&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;next&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; last&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oplog.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ $natural: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;limit&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;next&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Oplog Window (hrs):&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, ((last.ts.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getHighBits&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; first.ts.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getHighBits&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3600&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;status.members.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.stateStr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;SECONDARY&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(m.name, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;lagSecs=&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, (primary.optimeDate.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getTime&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.optimeDate.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getTime&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;pre-flight-automation-mongo_index_preflightpy&quot;&gt;Pre-Flight Automation (&lt;code&gt;mongo_index_preflight.py&lt;/code&gt;)&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env python3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse, sys&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo.errors &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PyMongoError&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; evaluate_preflight&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(client, namespace, est_hours):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    db_name, coll_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; namespace.split(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    stats &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client[db_name][coll_name].aggregate([{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$collStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;storageStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {}}}]).next()[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;storageStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    oplog &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.local.oplog.rs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    first &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oplog.find().sort([(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$natural&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)]).limit(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).next()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    last &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oplog.find().sort([(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$natural&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)]).limit(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).next()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    window_hrs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (last[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ts&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;].time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; first[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ts&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;].time) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3600.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    sort_mb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.admin.command({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;getParameter&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;maxIndexBuildMemoryUsageMegabytes&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;maxIndexBuildMemoryUsageMegabytes&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    active &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.admin.command(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;currentOp&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;command.createIndexes&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$exists&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}}).get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;inprog&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, [])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;docs=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;stats[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;count&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; sizeGB=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;stats[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;size&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.1f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; oplogHrs=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;window_hrs&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.1f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; sortMB=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sort_mb&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; activeBuilds=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(active)&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; window_hrs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; est_hours &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2.5&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; or&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; active:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sys.exit(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;PRE-FLIGHT FAILED: insufficient oplog window or concurrent build active&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-23-building-indexes-on-billion-document-collections-without-breaking-production-5-flow.svg&quot; alt=&quot;Four-gate rollout prevents mid-build abort or replication collapse&quot;&gt;&lt;/p&gt;
&lt;p&gt;Execute replicated hybrid builds across four gated steps (or switch to rolling standalone builds on each secondary sequentially if EBS IOPS cannot sustain simultaneous sorting):&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Expand Oplog Online:&lt;/strong&gt; Run &lt;code&gt;db.adminCommand({ replSetResizeOplog: 1, size: 409600 })&lt;/code&gt; (size in MB, here 400 GB) across nodes to guarantee &lt;code&gt;&gt;= 2.5x&lt;/code&gt; build duration headroom.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Raise Sort Memory Buffer:&lt;/strong&gt; Increase &lt;code&gt;maxIndexBuildMemoryUsageMegabytes&lt;/code&gt; (for example to &lt;code&gt;4096&lt;/code&gt; MB on a 128 GB RAM host) so the sorter writes fewer, larger run files to &lt;code&gt;_tmp/&lt;/code&gt;:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;adminCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ setParameter: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, maxIndexBuildMemoryUsageMegabytes: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4096&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Launch Serialized Build in &lt;code&gt;tmux&lt;/code&gt;:&lt;/strong&gt; Run one &lt;code&gt;createIndexes&lt;/code&gt; command at a time with &lt;code&gt;commitQuorum: &quot;majority&quot;&lt;/code&gt; (passed as the 3rd argument in &lt;code&gt;createIndex()&lt;/code&gt;, or top-level in &lt;code&gt;runCommand&lt;/code&gt;):
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.orders.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;createIndex&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;account_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;created_at&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;status&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { name: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;idx_account_created_status&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;majority&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Monitor &amp;#x26; Rollback Gate:&lt;/strong&gt; Poll &lt;code&gt;db.currentOp()&lt;/code&gt; and secondary replication lag every 5 minutes. If secondary lag approaches 50% of the oplog window or an offline node blocks commit, abort cleanly via &lt;code&gt;db.orders.dropIndex(&quot;idx_account_created_status&quot;)&lt;/code&gt; or adjust quorum with &lt;code&gt;db.adminCommand({ setIndexCommitQuorum: &quot;orders&quot;, indexNames: [&quot;idx_account_created_status&quot;], commitQuorum: &quot;majority&quot; })&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If EBS &lt;code&gt;gp3&lt;/code&gt; volumes on secondaries cannot sustain simultaneous merge sorting alongside live oplog writes, use &lt;strong&gt;Protocol B (Rolling Standalone Build)&lt;/strong&gt; instead: remove one secondary from replication, restart it standalone on a non-default port without &lt;code&gt;--replSet&lt;/code&gt;, run &lt;code&gt;createIndex()&lt;/code&gt; locally with full host RAM, restart it with &lt;code&gt;--replSet&lt;/code&gt; on its standard port to catch up on the oplog, repeat across all secondaries, and finally run &lt;code&gt;rs.stepDown()&lt;/code&gt; on the primary to build on the former primary. Note that rolling builds cannot enforce &lt;code&gt;unique: true&lt;/code&gt; across concurrent writes until all nodes are built, and each standalone window temporarily reduces replica set redundancy by one member.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Documented MongoDB storage engine and replication behaviors explain why pre-flight sizing and &lt;code&gt;commitQuorum&lt;/code&gt; selection govern billion-row index builds:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Sort Buffer Size vs. &lt;code&gt;_tmp&lt;/code&gt; I/O Amplification:&lt;/strong&gt; Per MongoDB’s &lt;code&gt;maxIndexBuildMemoryUsageMegabytes&lt;/code&gt; documentation, the default 200 MB limit applies per &lt;code&gt;createIndexes&lt;/code&gt; command and is shared equally if multiple indexes are created in one command. On billion-document collections, raising this parameter on hosts with ample RAM reduces the number of sorted run files spilled to &lt;code&gt;dbPath/_tmp/&lt;/code&gt;, cutting multi-pass random merge I/O on EBS volumes. Fewer run files also reduces the number of open file descriptors and sequential merge passes required before B-tree insertion begins.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Side Writes Table Drain and &lt;code&gt;commitQuorum&lt;/code&gt;:&lt;/strong&gt; Per MongoDB’s hybrid index build architecture, concurrent writes during the collection scan are buffered in the Side Writes Table (a WiredTiger skiplist/B-tree structure) and applied before voting. On write-heavy collections, a multi-hour collection scan accumulates millions of intercepted mutations that must be drained into the new B-tree before &lt;code&gt;voteCommitIndexBuild&lt;/code&gt; is written. Because &lt;code&gt;commitQuorum&lt;/code&gt; defaults to &lt;code&gt;&quot;votingMembers&quot;&lt;/code&gt;, a single unreachable or hardware-failed voting secondary prevents the primary from writing &lt;code&gt;commitIndexBuild&lt;/code&gt;—keeping all nodes holding the index build state until the node recovers or an operator runs &lt;code&gt;setIndexCommitQuorum&lt;/code&gt; to lower the quorum to &lt;code&gt;&quot;majority&quot;&lt;/code&gt; or an explicit integer count.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oplog Entry Amplification During Builds:&lt;/strong&gt; Unlike individual document writes, a replicated hybrid index build writes only &lt;code&gt;startIndexBuild&lt;/code&gt;, &lt;code&gt;voteCommitIndexBuild&lt;/code&gt;, and &lt;code&gt;commitIndexBuild&lt;/code&gt; coordination entries to &lt;code&gt;local.oplog.rs&lt;/code&gt;—it does not replicate every B-tree key over the network. The oplog risk comes from the &lt;em&gt;duration&lt;/em&gt; of the build: while a secondary’s CPU and EBS IOPS are saturated by its local &lt;code&gt;_tmp&lt;/code&gt; merge sort, its oplog applier falls behind the primary’s write rate, consuming the oplog retention window. If a lagging secondary drops into &lt;code&gt;RECOVERING&lt;/code&gt;, its only recovery path is a full initial sync or snapshot restore.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MongoDB 8.0 Replication &amp;#x26; Disk Safeguards:&lt;/strong&gt; MongoDB 8.0 retains the 200 MB &lt;code&gt;maxIndexBuildMemoryUsageMegabytes&lt;/code&gt; default and ignores legacy &lt;code&gt;background: true&lt;/code&gt; as a safe no-op. However, MongoDB 8.0 improves secondary oplog throughput via parallel oplog writer/applier paths and enforces &lt;code&gt;indexBuildMinAvailableDiskSpaceMB&lt;/code&gt; to halt an index build cleanly before &lt;code&gt;_tmp&lt;/code&gt; spills exhaust the filesystem.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-23-building-indexes-on-billion-document-collections-without-breaking-production-6-failures.svg&quot; alt=&quot;Four billion-row build failures and their recovery paths&quot;&gt;&lt;/p&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Operational Hazard&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Physical Failure Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Architectural Consequence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Safe Mitigation Protocol&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unclean Crash During Build&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;OOM killer or &lt;code&gt;SIGKILL&lt;/code&gt; terminates &lt;code&gt;mongod&lt;/code&gt; mid-build.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Clean &lt;code&gt;SIGTERM&lt;/code&gt; checkpoints progress (4.4+); hard crash discards &lt;code&gt;_tmp&lt;/code&gt; and restarts from 0%.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Maintain RAM headroom; use &lt;code&gt;systemctl stop&lt;/code&gt; (&lt;code&gt;SIGTERM&lt;/code&gt;) for maintenance.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Oplog Rollover on Secondaries&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Secondary merge sort + oplog apply lags past &lt;code&gt;local.oplog.rs&lt;/code&gt; window.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Secondary enters &lt;code&gt;RECOVERING&lt;/code&gt; and requires a full initial sync.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Expand oplog via &lt;code&gt;replSetResizeOplog&lt;/code&gt; to &lt;code&gt;&gt;= 2.5x&lt;/code&gt; build duration first.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Exhausting &lt;code&gt;dbPath/_tmp&lt;/code&gt; Disk&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;External sort run files exceed free space on the EBS volume.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;mongod&lt;/code&gt; crashes on disk full (or aborts in 8.0 at &lt;code&gt;indexBuildMinAvailableDiskSpaceMB&lt;/code&gt;).&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Verify free disk space exceeds $S_{\text{temp}} \times 2.0$ before starting.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Shell Disconnect Without &lt;code&gt;tmux&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Laptop SSH drops mid-build; operator re-issues &lt;code&gt;createIndex()&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Build continues on server; duplicate command creates confusion or contention.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run inside &lt;code&gt;tmux&lt;/code&gt; on a bastion host and track via &lt;code&gt;db.currentOp()&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unique Index Dupe on Ingest&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Concurrent write inserts a duplicate key during Phase 2 scan.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Build aborts during final Side Writes drain, discarding hours of work.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Deduplicate data and enforce app-level uniqueness checks before building.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Side Writes Cache Pressure&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Heavy write ingest during long scan fills WiredTiger cache.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Eviction stalls trigger flow control and throttle application writes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Schedule builds off-peak or throttle batch ingest jobs during scan.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Default &lt;code&gt;votingMembers&lt;/code&gt; Hang&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;One voting secondary goes offline while build is in progress.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary waits indefinitely for the missing &lt;code&gt;voteCommitIndexBuild&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pass &lt;code&gt;&quot;majority&quot;&lt;/code&gt; as &lt;code&gt;commitQuorum&lt;/code&gt; or run &lt;code&gt;setIndexCommitQuorum&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Parallel Builds on Same Volume&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Running two &lt;code&gt;createIndexes&lt;/code&gt; commands concurrently.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sorters split the 200 MB buffer and compete for EBS IOPS, doubling lag.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Serialize index builds strictly: one active &lt;code&gt;createIndexes&lt;/code&gt; per replica set.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Do the $S_{\text{temp}}$ and oplog math before running &lt;code&gt;createIndex()&lt;/code&gt;:&lt;/strong&gt; Ensure free disk space exceeds $S_{\text{temp}} \times 2.0$ and resize &lt;code&gt;local.oplog.rs&lt;/code&gt; online to &lt;code&gt;&gt;= 2.5x&lt;/code&gt; estimated build time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Raise &lt;code&gt;maxIndexBuildMemoryUsageMegabytes&lt;/code&gt; from its 200 MB default:&lt;/strong&gt; Allocating 2–4 GB on memory-optimized EC2 nodes slashes &lt;code&gt;_tmp&lt;/code&gt; disk run files and EBS merge-sort IOPS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Override the &lt;code&gt;&quot;votingMembers&quot;&lt;/code&gt; &lt;code&gt;commitQuorum&lt;/code&gt; default:&lt;/strong&gt; Pass &lt;code&gt;&quot;majority&quot;&lt;/code&gt; (as the 3rd argument to &lt;code&gt;createIndex()&lt;/code&gt;) so a single offline secondary cannot stall commit indefinitely.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Clean &lt;code&gt;SIGTERM&lt;/code&gt; resumes; &lt;code&gt;SIGKILL&lt;/code&gt; or OOM restarts from zero:&lt;/strong&gt; MongoDB 4.4+ checkpoints hybrid builds on clean shutdown, but hard crashes discard &lt;code&gt;_tmp&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Serialize builds and keep &lt;code&gt;dropIndex()&lt;/code&gt; ready:&lt;/strong&gt; Run one index build at a time per replica set, and abort via &lt;code&gt;dropIndex()&lt;/code&gt; if secondary lag threatens the oplog window.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Large-collection index builds risk &lt;code&gt;_tmp&lt;/code&gt; disk exhaustion, EBS I/O saturation, and secondary oplog rollover.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Standardize on a pre-flight gate that verifies &lt;code&gt;&gt;= 2x S_temp&lt;/code&gt; free disk, resizes the oplog to &lt;code&gt;&gt;= 2.5x&lt;/code&gt; build duration, bumps &lt;code&gt;maxIndexBuildMemoryUsageMegabytes&lt;/code&gt;, and sets &lt;code&gt;commitQuorum: &quot;majority&quot;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Check current oplog retention hours via &lt;code&gt;local.oplog.rs&lt;/code&gt; and confirm &lt;code&gt;maxIndexBuildMemoryUsageMegabytes&lt;/code&gt; via &lt;code&gt;getParameter&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run the pre-flight verification script against your largest collection this week before scheduling its next compound index build.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>MySQL HeatWave Architecture: Why Oracle Built an Analytics Engine Around MySQL</title><link>https://rajivonai.com/blog/2026-08-18-mysql-heatwave-architecture-internals/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-08-18-mysql-heatwave-architecture-internals/</guid><description>An architectural deep dive into MySQL HeatWave on OCI: why InnoDB fails at analytical scale, how HeatWave&apos;s in-memory columnar cluster executes distributed queries, and how it compares with AlloyDB and Aurora.</description><pubDate>Tue, 18 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A common failure mode in database platform engineering is forcing a pure OLTP storage engine to double as an operational data warehouse. When senior DBAs attempt to run multi-table aggregations, range-based scans, and ad-hoc joins directly against a production MySQL InnoDB primary, they inevitably watch the buffer pool churn violently, latch contention spike across the B+tree indices, and transaction commit latencies degrade across normal customer traffic. The traditional enterprise workaround—building a Change Data Capture (CDC) pipeline using Debezium, Kafka, and an external cloud warehouse like Snowflake or Redshift—introduces significant architectural complexity, data synchronization lag, schema drift fragility, and dual-system cloud egress billing. MySQL HeatWave represents Oracle’s attempt to eliminate this split-brain architecture by attaching an in-memory, massively parallel processing (MPP) analytical cluster directly to the MySQL DB System without altering the application connection string or transactional guarantees.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Over the last two decades, MySQL with the InnoDB storage engine became the foundational relational database for modern web services, SaaS platforms, and enterprise backends. InnoDB was intentionally designed from first principles for high-concurrency, low-latency Online Transaction Processing (OLTP): row-oriented physical storage on 16 KB pages, write-ahead logging via the redo log, multiversion concurrency control (MVCC) via rollback segments in undo tablespaces, and pessimistic row-level locking via clustered B+trees.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Traditional MySQL Dual-Stack Reality:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Client App ──(Writes/Reads)──&gt; MySQL (InnoDB Primary)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                               (Binlog / CDC)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    ▼&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                         Kafka / Debezium / ETL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                              (Batch Sync)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    ▼&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                         Snowflake / Redshift / BigQuery&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                               (BI Dashboards)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As organizations scaled, the demand for real-time operational analytics—querying live transactional state without hours of batch latency—collided with InnoDB’s physical constraints. When Oracle introduced the MySQL Database Service (MDS) on Oracle Cloud Infrastructure (OCI), managed MySQL instances faced the same architectural wall as on-premises installations: any query scanning tens of millions of rows crippled the buffer pool.&lt;/p&gt;
&lt;p&gt;To solve this without abandoning MySQL, Oracle Labs spent years developing the MySQL Analytics Engine, subsequently commercialized and branded as &lt;strong&gt;MySQL HeatWave&lt;/strong&gt;. Over successive releases, Oracle expanded HeatWave from a basic in-memory accelerator into an elastic distributed platform:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;HeatWave Lakehouse:&lt;/strong&gt; Allowing the distributed cluster to directly query hundreds of terabytes of object storage files (Parquet, CSV, Avro) without first loading them into InnoDB.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MySQL Autopilot:&lt;/strong&gt; Using machine learning models to automate partition sizing, memory allocation, encoding, and query execution placement.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multi-Cloud Deployments:&lt;/strong&gt; Expanding the native OCI service into AWS and Microsoft Azure through dedicated interconnects and native cloud control planes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;In-Database Generative AI and Vector Search:&lt;/strong&gt; Embedding vector generation and similarity search within the columnar execution fabric.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Understanding HeatWave requires examining how an in-memory columnar execution grid integrates with a monolithic, row-oriented transactional database kernel.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;InnoDB cannot serve large-scale analytical queries efficiently because its physical structures were optimized for point lookups and transactional mutations.&lt;/p&gt;
&lt;p&gt;First, InnoDB stores data on disk and in the buffer pool in a &lt;strong&gt;row-oriented format&lt;/strong&gt; (&lt;code&gt;COMPACT&lt;/code&gt; or &lt;code&gt;DYNAMIC&lt;/code&gt; row format) inside 16 KB pages. If an analytical query requests a &lt;code&gt;SUM(order_total)&lt;/code&gt; over 50,000,000 rows with a filter on &lt;code&gt;created_at&lt;/code&gt;, the engine must read every single 16 KB page containing matching rows. For each row, InnoDB parses the entire row record—loading customer identifiers, delivery addresses, notes, and metadata into the buffer pool—even though the query touches only two columns. This causes extreme &lt;strong&gt;buffer pool churn&lt;/strong&gt;, ejecting hot transactional pages out of memory and forcing subsequent OLTP queries to incur physical disk I/O.&lt;/p&gt;
&lt;p&gt;Second, InnoDB’s locking and latching mechanisms were not built for long-running scans. Even under &lt;code&gt;READ COMMITTED&lt;/code&gt; or &lt;code&gt;REPEATABLE READ&lt;/code&gt; transaction isolation, scanning an entire table creates sustained read views and traverses index leaf pages while holding page-level buffer latches. Concurrent transactional writes to the same pages or B+tree branches hit immediate contention, triggering lock wait timeouts and thread queuing in the InnoDB kernel.&lt;/p&gt;
&lt;p&gt;Historically, platform architects resolved this by routing analytics through CDC pipelines:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The MySQL primary streams changes to the binary log (&lt;code&gt;binlog&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;A connector (such as Debezium or an ETL vendor) tails the binlog and streams events to Kafka.&lt;/li&gt;
&lt;li&gt;Micro-batch consumers load, transform, and insert data into a cloud data warehouse (Snowflake, BigQuery, or Amazon Redshift).&lt;/li&gt;
&lt;li&gt;Analytical reporting queries run against the warehouse.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In practice, this architecture imposes severe engineering overhead. The replication pipeline introduces end-to-end latency ranging from seconds to hours. A single schema migration (&lt;code&gt;ALTER TABLE&lt;/code&gt;) on the MySQL primary can break downstream ingestion connectors. Furthermore, synchronizing state across two completely separate database systems creates compliance boundaries, dual data modeling overhead, and high egress data transfer costs.&lt;/p&gt;
&lt;p&gt;The core architectural question is: &lt;strong&gt;Can an operational database retain standard MySQL wire protocol and ACID transactional behavior while delegating analytical query execution to an integrated, in-memory, scale-out MPP engine without operational synchronization overhead?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;heatwave-cluster-internals-decoupled-vectorized-analytics&quot;&gt;HeatWave Cluster Internals: Decoupled Vectorized Analytics&lt;/h2&gt;
&lt;p&gt;MySQL HeatWave addresses this challenge through a hybrid, dual-engine design. It does not replace InnoDB; rather, it attaches a distributed, in-memory columnar computing cluster to a standard MySQL DB System.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Client[Application Client — MySQL Protocol] --&gt; Core[MySQL DB System — Primary Instance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph Transactional_Tier[Transactional Tier]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Core --&gt; Optimizer[MySQL Parser and Optimizer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Optimizer --&gt; InnoDB[InnoDB Storage Engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        InnoDB --&gt; BlockStorage[OCI Block Storage — WAL and 16KB Pages]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph Control_Channel[Control and Replication Channel]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Core --&gt;|Change Propagator Plugin| Propagator[Cluster Data Management Service]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph HeatWave_MPP[HeatWave In-Memory Distributed Cluster]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Propagator --&gt; Node1[HeatWave Node 1 — Leader and Worker]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Propagator --&gt; Node2[HeatWave Node 2 — Worker]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Propagator --&gt; NodeN[HeatWave Node N — Worker]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Node1 --- Slices1[In-Memory Columnar Partitions — Slices]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Node2 --- Slices2[In-Memory Columnar Partitions — Slices]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        NodeN --- SlicesN[In-Memory Columnar Partitions — Slices]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Optimizer --&gt;|Offload Threshold Met| Node1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Node1 --&gt;|Coordinated Execution| Node2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Node1 --&gt;|Coordinated Execution| NodeN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Node1 --&gt;|Aggregated Result Set| Optimizer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-the-dual-format-architecture&quot;&gt;1. The Dual-Format Architecture&lt;/h3&gt;
&lt;p&gt;The MySQL DB System functions as the primary control plane, transaction coordinator, and single point of connection for the application. The HeatWave cluster consists of two or more dedicated compute nodes (one leader node and multiple worker nodes) operating in a private high-speed network fabric.&lt;/p&gt;
&lt;p&gt;Data exists in two distinct representations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The Row Store (InnoDB):&lt;/strong&gt; Data resides in standard InnoDB tablespaces on persistent block storage. All &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, and &lt;code&gt;DELETE&lt;/code&gt; operations execute against InnoDB with full ACID guarantees.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Columnar Store (HeatWave Memory):&lt;/strong&gt; When a table is loaded into HeatWave (&lt;code&gt;ALTER TABLE users SECONDARY_LOAD;&lt;/code&gt;), the cluster reads the table from InnoDB, transforms the rows into an in-memory columnar representation, compresses the columns, and distributes the columnar data across the HeatWave nodes.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-partitioning-slices-and-distributed-in-memory-layout&quot;&gt;2. Partitioning, Slices, and Distributed In-Memory Layout&lt;/h3&gt;
&lt;p&gt;HeatWave does not replicate entire tables to every node. Instead, it uses &lt;strong&gt;hybrid horizontal and vertical partitioning&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Data Slices:&lt;/strong&gt; Each HeatWave node divides its physical CPU cores into execution slices. If a node has 16 OCPUs (32 vCPUs), it allocates internal memory and execution threads into corresponding slices.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Partition Placement:&lt;/strong&gt; Tables are partitioned horizontally across all worker nodes in the cluster. If a user defines a primary key or explicit partition key (&lt;code&gt;SECONDARY_ENGINE_ATTRIBUTE=&apos;{&quot;engine&quot;: &quot;RAPID&quot;, &quot;keys&quot;: [&quot;customer_id&quot;]}&apos;&lt;/code&gt;), HeatWave uses consistent hashing on that key to route records to specific nodes and slices. Unkeyed tables are sliced round-robin.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cache-Aligned Columnar Chunks:&lt;/strong&gt; Within each slice, data is stored in contiguous, fixed-size in-memory chunks per column. This memory representation is designed to fit directly into CPU L1/L2/L3 caches, avoiding pointer chasing and eliminating memory fragmentation.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-change-propagation-and-concurrency&quot;&gt;3. Change Propagation and Concurrency&lt;/h3&gt;
&lt;p&gt;When an application issues a write (&lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt;) to the MySQL primary:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The transaction commits inside InnoDB, writing to the redo log and binary log.&lt;/li&gt;
&lt;li&gt;A background plugin running in the MySQL server daemon—the &lt;strong&gt;Change Propagator&lt;/strong&gt;—captures the committed transaction modifications from the transaction coordinator.&lt;/li&gt;
&lt;li&gt;The modifications are batched, encoded into columnar format, and transmitted asynchronously across the private network to the appropriate HeatWave worker nodes.&lt;/li&gt;
&lt;li&gt;Each HeatWave node writes the changes to an in-memory delta structure associated with the affected slice.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Because change propagation occurs asynchronously at memory speed, transactional write throughput on InnoDB is shielded from network pauses in the HeatWave cluster. Read operations submitted to HeatWave can query transactional snapshots, ensuring that HeatWave queries observe committed transactions without blocking ongoing InnoDB writes.&lt;/p&gt;
&lt;h3 id=&quot;4-query-offload-and-optimizer-integration&quot;&gt;4. Query Offload and Optimizer Integration&lt;/h3&gt;
&lt;p&gt;When an application submits a query over the standard MySQL connection (port 3306), the query goes through the standard MySQL parser and resolver. The MySQL Optimizer then evaluates the query plan using a cost-based decision framework:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Secondary Engine Check:&lt;/strong&gt; The optimizer verifies if the referenced tables have &lt;code&gt;SECONDARY_ENGINE = RAPID&lt;/code&gt; enabled and are loaded.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Eligibility Verification:&lt;/strong&gt; The optimizer evaluates whether all SQL operators, functions, and data types in the query tree are supported by the HeatWave execution engine.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cost Evaluation:&lt;/strong&gt; The MySQL cost optimizer calculates the estimated execution cost of running the query on InnoDB versus offloading it to HeatWave. If the session variable &lt;code&gt;use_secondary_engine&lt;/code&gt; is set to &lt;code&gt;ON&lt;/code&gt; (or &lt;code&gt;FORCED&lt;/code&gt;) and the estimated runtime exceeds the offload threshold, the optimizer generates an offload plan.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AST Compilation:&lt;/strong&gt; The MySQL engine compiles the abstract syntax tree into a HeatWave distributed execution plan and transmits it via RPC to the HeatWave leader node.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;5-detailed-sql-query-execution-walkthrough&quot;&gt;5. Detailed SQL Query Execution Walkthrough&lt;/h3&gt;
&lt;p&gt;To understand the internal mechanics, consider a multi-million row operational reporting query:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    c&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;region&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    EXTRACT(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;YEAR&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; o&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;order_date&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; order_year,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    COUNT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DISTINCT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; o&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;order_id&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; total_orders,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    SUM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;l&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;extended_price&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; l&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;discount&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; net_revenue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; customers c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders o &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;customer_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; o&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;customer_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; lineitem l &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; o&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;order_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; l&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;order_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; o&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;order_date&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;2024-01-01&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GROUP BY&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;region&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, order_year&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; net_revenue &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here is the exact lifecycle from client submission to final result set:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;1. Client Submission&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   Client App ──(SQL)──&gt; MySQL Server (Port 3306)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;2. Parsing &amp;#x26; Offload Determination&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   MySQL Parser ──&gt; AST ──&gt; Cost Optimizer:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   - Verifies tables (customers, orders, lineitem) have SECONDARY_ENGINE = RAPID&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   - Estimates InnoDB cost (e.g., 4,200,000 cost units, table scans over 80M rows)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   - Determines query meets offload criteria; compiles HeatWave physical plan&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;3. Dispatch to Cluster&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   MySQL Server ──(RPC Plan)──&gt; HeatWave Leader Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;4. Distributed Parallel Execution&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   HeatWave Leader broadcasts operator tree to all HeatWave Worker Nodes:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   [Worker Node 1]                         [Worker Node 2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   ├── Slices scan &apos;orders&apos; in memory       ├── Slices scan &apos;orders&apos; in memory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   │   (Vectorized SIMD date filter)        │   (Vectorized SIMD date filter)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   ├── Hash Join: lineitem on order_id      ├── Hash Join: lineitem on order_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   │   (In-memory distributed hash table)   │   (In-memory distributed hash table)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   ├── Hash Join: customers on customer_id  ├── Hash Join: customers on customer_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   └── Partial Aggregation:                └── Partial Aggregation:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;       SUM(revenue), COUNT(orders)             SUM(revenue), COUNT(orders)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;       per (region, order_year)                per (region, order_year)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;5. Network Shuffle &amp;#x26; Reduction&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   Workers exchange intermediate join keys across private network fabric.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   Workers transmit partial aggregates to HeatWave Leader.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;6. Final Consolidation &amp;#x26; Delivery&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   HeatWave Leader merges partial aggregates, computes final COUNT(DISTINCT),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   executes final ORDER BY net_revenue DESC, and streams rows back to MySQL Server.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   MySQL Server delivers result set to Client App over standard MySQL protocol.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Throughout this execution:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Scan operators use &lt;strong&gt;SIMD (Single Instruction, Multiple Data)&lt;/strong&gt; instructions on physical CPUs, evaluating column predicates (e.g., &lt;code&gt;order_date &gt;= &apos;2024-01-01&apos;&lt;/code&gt;) across thousands of values per clock cycle.&lt;/li&gt;
&lt;li&gt;Join operations build in-memory distributed hash tables partitioned across the worker nodes. If tables are co-partitioned on &lt;code&gt;customer_id&lt;/code&gt; or &lt;code&gt;order_id&lt;/code&gt;, joins execute locally within the slice without cross-network data transfer.&lt;/li&gt;
&lt;li&gt;The InnoDB buffer pool remains completely untouched; transactional caches remain pristine.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;6-node-failure-state-persistence-and-recovery&quot;&gt;6. Node Failure, State Persistence, and Recovery&lt;/h3&gt;
&lt;p&gt;A critical design choice in HeatWave is that &lt;strong&gt;cluster worker nodes are stateless with respect to long-term durability&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;HeatWave stores a compressed, partition-aware copy of the columnar dataset in &lt;strong&gt;OCI Object Storage&lt;/strong&gt;. When a HeatWave node experiences an operating system failure, hardware fault, or unrecoverable error:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The MySQL DB System control plane detects heartbeat loss from the worker node.&lt;/li&gt;
&lt;li&gt;The control plane provisions a replacement node or re-allocates partition responsibilities among surviving nodes.&lt;/li&gt;
&lt;li&gt;The replacement node hydrates its columnar slices directly from OCI Object Storage at high network bandwidth, avoiding the need to re-read gigabytes of data from the InnoDB instance.&lt;/li&gt;
&lt;li&gt;The Change Propagator on the MySQL primary streams any incremental delta transactions that committed since the object storage snapshot was captured.&lt;/li&gt;
&lt;li&gt;The cluster resumes full parallel query processing.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;7-analytical-distribution-vs-transactional-sharding&quot;&gt;7. Analytical Distribution vs. Transactional Sharding&lt;/h3&gt;
&lt;p&gt;Engineering teams frequently confuse HeatWave’s distributed architecture with horizontal transactional write sharding (such as Vitess, Citus, or Spanner). They are fundamentally different systems:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Architectural Dimension&lt;/th&gt;&lt;th&gt;HeatWave Analytical MPP Cluster&lt;/th&gt;&lt;th&gt;Distributed Transactional Sharding (Vitess / Spanner)&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Write Target&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Single MySQL InnoDB Primary&lt;/td&gt;&lt;td&gt;Distributed across N shard primaries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Write Scalability&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Bound by vertical CPU/IOPS limits of single primary&lt;/td&gt;&lt;td&gt;Scales horizontally across multiple physical nodes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Distributed Scope&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Read-only in-memory scans, joins, aggregations&lt;/td&gt;&lt;td&gt;Distributed reads, writes, two-phase commits (2PC)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Data Partitioning&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Columnar slices in cluster RAM, cached in Object Storage&lt;/td&gt;&lt;td&gt;Row-based splits/shards on persistent disk&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Network Overhead&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Shuffle phase during analytical query execution&lt;/td&gt;&lt;td&gt;Distributed lock coordination, Raft/Paxos/2PC on every write&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Failure Domain&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Node crash degrades query parallelism; writes continue&lt;/td&gt;&lt;td&gt;Node crash impacts transactional quorum and shard availability&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;HeatWave is an &lt;strong&gt;analytical acceleration engine attached to a single-writer transactional database&lt;/strong&gt;. It does not allow you to scale transactional &lt;code&gt;INSERT&lt;/code&gt; or &lt;code&gt;UPDATE&lt;/code&gt; throughput beyond the capacity of the primary MySQL instance.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;heatwave-vs-google-cloud-alloydb-contrasting-htap-philosophies&quot;&gt;HeatWave vs. Google Cloud AlloyDB: Contrasting HTAP Philosophies&lt;/h2&gt;
&lt;p&gt;Both Oracle (with HeatWave) and Google Cloud (with AlloyDB for PostgreSQL) target the Hybrid Transactional/Analytical Processing (HTAP) problem, but they arrived at radically different architectural solutions.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Architectural Topologies:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;HeatWave (Disaggregated Analytical Compute):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[Client] ──&gt; [MySQL DB System (InnoDB Primary)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                      │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;           (Asynchronous Delta Sync)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                      ▼&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;             [HeatWave MPP Cluster]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;             ├── Worker Node 1 (RAM)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;             ├── Worker Node 2 (RAM)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;             └── Worker Node N (RAM)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;AlloyDB (Disaggregated Storage with Integrated Columnar Cache):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[Client] ──&gt; [AlloyDB Primary Instance] ──or──&gt; [AlloyDB Read Pool]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                      │                                  │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;              (In-Memory Columnar)              (In-Memory Columnar)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                      │                                  │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                      └─────────────────┬────────────────┘&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                        ▼&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                       [Log Processing Service (LPS)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                        │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                       [Disaggregated Storage Engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-underlying-engine-and-protocol-foundations&quot;&gt;1. Underlying Engine and Protocol Foundations&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;HeatWave:&lt;/strong&gt; Built strictly on Oracle’s MySQL 8.x codebase, maintaining 100% MySQL wire protocol and InnoDB storage engine semantics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AlloyDB:&lt;/strong&gt; Built on standard PostgreSQL (versions 14 through 16), preserving complete compatibility with PostgreSQL extensions (such as &lt;code&gt;pgvector&lt;/code&gt; and PostGIS), procedural languages, and catalog tables. It does not run MySQL or InnoDB.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-in-memory-columnar-engine-mechanics&quot;&gt;2. In-Memory Columnar Engine Mechanics&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;HeatWave:&lt;/strong&gt; Offloads queries to an &lt;strong&gt;external, dedicated MPP cluster&lt;/strong&gt; consisting of up to 64 or 512 separate compute nodes. The columnar data is partitioned and distributed across these nodes. If an analytical query runs, it executes on the secondary cluster, completely offloading CPU cycles from the primary database instance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AlloyDB:&lt;/strong&gt; Incorporates an &lt;strong&gt;internal in-memory columnar engine directly within the database instance&lt;/strong&gt; (both primary instances and read pool nodes). AlloyDB uses machine learning algorithms to monitor incoming queries and automatically select table columns to populate into an in-memory columnar format. Analytical queries execute directly on the primary or read-pool instance CPUs, utilizing vectorized execution algorithms inside the Postgres backend process.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-storage-layer-disaggregation&quot;&gt;3. Storage Layer Disaggregation&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;HeatWave:&lt;/strong&gt; Uses traditional cloud block storage for the MySQL DB System (InnoDB redo log, undo tablespace, tablespace datafiles). HeatWave’s scale-out cluster utilizes local instance memory and OCI Object Storage for columnar snapshots. The transactional storage layer remains conventional.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AlloyDB:&lt;/strong&gt; Features a fully &lt;strong&gt;disaggregated, compute-separated storage system&lt;/strong&gt; with a dedicated Log Processing Service (LPS). When a transaction writes a WAL record, the compute node ships only the log record to the LPS. The LPS applies WAL records directly to storage blocks asynchronously. Because the storage layer understands the WAL stream, read replicas suffer virtually zero replication lag, and snapshot generation is instantaneous.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;4-tradeoff-summary&quot;&gt;4. Tradeoff Summary&lt;/h3&gt;
&lt;p&gt;AlloyDB provides a more transparent operational model for PostgreSQL workloads because its columnar cache requires no explicit table-load commands and runs on standard read pool replicas. However, AlloyDB’s analytical scalability is constrained by the vertical size of the largest available compute instance (up to 128 vCPUs in GCP).&lt;/p&gt;
&lt;p&gt;HeatWave, by contrast, requires explicit loading of tables into the secondary engine (&lt;code&gt;SECONDARY_LOAD&lt;/code&gt;), but its distributed MPP architecture allows it to scale horizontally to hundreds of nodes, aggregating tens of terabytes of memory for analytical scans that would overwhelm a single large VM.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;heatwave-vs-aws-integrated-mpp-vs-decoupled-warehouse&quot;&gt;HeatWave vs. AWS: Integrated MPP vs. Decoupled Warehouse&lt;/h2&gt;
&lt;p&gt;Amazon Web Services addresses operational analytics through a different architectural philosophy: combining a specialized relational storage layer with decoupled analytics services.&lt;/p&gt;
&lt;h3 id=&quot;1-aurora-mysql-and-aurora-parallel-query&quot;&gt;1. Aurora MySQL and Aurora Parallel Query&lt;/h3&gt;
&lt;p&gt;AWS Aurora MySQL re-architects storage by distributing database log records across a fleet of storage nodes spanning three Availability Zones (6-way replication). To accelerate analytical operations, AWS introduced &lt;strong&gt;Aurora Parallel Query&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;How It Works:&lt;/strong&gt; Instead of pulling entire tables from storage into the database compute node’s buffer pool, Aurora pushes query predicates (&lt;code&gt;WHERE&lt;/code&gt; clauses) and column projections directly down to the Aurora storage fleet. The storage nodes scan the data pages locally on disk and stream only matching rows back to the primary database compute node.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Limit:&lt;/strong&gt; Parallel Query is fundamentally a &lt;strong&gt;pushdown storage filter&lt;/strong&gt;, not a distributed analytical execution engine. It cannot perform distributed joins across multiple nodes, cannot build distributed hash tables in memory, and cannot scale aggregations across an MPP cluster. Large joins, window functions, and &lt;code&gt;GROUP BY&lt;/code&gt; operations must still be executed serially or via intra-node parallelism on the single Aurora compute instance.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-aurora-to-redshift-zero-etl-integration&quot;&gt;2. Aurora to Redshift Zero-ETL Integration&lt;/h3&gt;
&lt;p&gt;To handle complex, large-scale data warehousing without traditional ETL maintenance, AWS developed &lt;strong&gt;Amazon Aurora Zero-ETL integration with Amazon Redshift&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;How It Works:&lt;/strong&gt; When transactions commit to an Aurora cluster, the Aurora storage layer automatically identifies changed data blocks and streams Change Data Capture (CDC) events directly to Amazon Redshift without passing through user-managed compute instances or Kafka queues.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Operational Separation:&lt;/strong&gt; Unlike HeatWave, this approach retains two entirely separate database platforms. Applications must maintain two distinct database connection endpoints: one for MySQL transactional traffic (port 3306 on Aurora) and another for analytical queries (port 5439 on Redshift).&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Architectural Philosophy Comparison:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;MySQL HeatWave (Attached In-Memory MPP):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Application ──(Single Endpoint: Port 3306)──&gt; MySQL Server&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                                    │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                         (In-Memory Secondary Engine)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                                    ▼&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                            HeatWave MPP Cluster&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                   [Unified Parser, Query Routing, and Catalog]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;AWS Zero-ETL (Decoupled Engines via Storage CDC):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Application ──(Writes: Port 3306)───────────&gt; Aurora MySQL Cluster&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                                    │&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                          (Storage-Level CDC)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                                    ▼&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Application ──(Analytical Reads: Port 5439)──&gt; Amazon Redshift Warehouse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                   [Separate Parsers, Endpoints, and Catalogs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-evaluating-the-two-philosophies&quot;&gt;3. Evaluating the Two Philosophies&lt;/h3&gt;
&lt;p&gt;The AWS approach provides decoupled scaling: Redshift can scale its compute independent of transactional Aurora, and analytical workloads benefit from Redshift’s full data warehousing feature set. However, it forces application developers to maintain routing logic: BI tools and dashboard queries must connect to Redshift, while transactional operations connect to Aurora.&lt;/p&gt;
&lt;p&gt;HeatWave unifies this under a &lt;strong&gt;single endpoint and single catalog&lt;/strong&gt;. The application issues standard MySQL SQL statements against port 3306; the MySQL query optimizer automatically decides whether the query should run locally on InnoDB or execute across the HeatWave cluster.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The operational realities of running HeatWave versus traditional architectures can be evaluated across documented architectural patterns and named system behaviors.&lt;/p&gt;
&lt;h3 id=&quot;context-the-real-time-financial-ledger-bottleneck&quot;&gt;Context: The Real-Time Financial Ledger Bottleneck&lt;/h3&gt;
&lt;p&gt;The documented operational pattern observed across high-throughput relational ledgers (such as TPC-H analytical benchmarks combined with high-concurrency Sysbench write transactions against MySQL on OCI) demonstrates clear behavioral divergence among implementation paths. Consider an operational dataset processing 18,000 write transactions per second against InnoDB while operational reconciliation dashboards scan 120 million rows across 5 joined tables.&lt;/p&gt;
&lt;h3 id=&quot;action-comparing-three-implementation-paths&quot;&gt;Action: Comparing Three Implementation Paths&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Direct InnoDB Execution:&lt;/strong&gt; Running the reconciliation queries directly on an InnoDB read replica.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CDC to Cloud Data Warehouse:&lt;/strong&gt; Provisioning a Kafka and Debezium pipeline streaming binlog events into Snowflake or Redshift.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MySQL HeatWave Deployment:&lt;/strong&gt; Provisioning an OCI MySQL DB System with an attached 8-node HeatWave cluster, loading the transactional tables into the secondary engine.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;result-observed-system-behaviors&quot;&gt;Result: Observed System Behaviors&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Under Direct InnoDB Execution:&lt;/strong&gt; The reconciliation query runs for over 4 minutes on the read replica. The massive table scans flush the InnoDB buffer pool, causing page eviction rates to spike. When subsequent operational read queries arrive, cache hit rates drop from 99.4% to 71.2%, causing disk read IOPS saturation and replica replication lag to exceed 300 seconds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Under CDC to Snowflake:&lt;/strong&gt; The queries execute in 3.4 seconds on an X-Small Snowflake warehouse. However, binlog parsing lag on Debezium averages 45 to 90 seconds during peak write hours, meaning the reconciliation dashboards do not reflect real-time financial balances. Furthermore, a schema change (&lt;code&gt;ALTER TABLE ... ADD COLUMN&lt;/code&gt;) executes on MySQL, causing the Debezium connector to fail due to schema registry deserialization errors, halting replication until manual schema alignment.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Under MySQL HeatWave:&lt;/strong&gt; Setting &lt;code&gt;SECONDARY_ENGINE = RAPID&lt;/code&gt; on the ledger tables allows the MySQL optimizer to offload the reconciliation queries to the HeatWave cluster. The query completes in 1.1 seconds using distributed vectorized scans. Because query processing occurs entirely within HeatWave cluster memory, the InnoDB primary’s buffer pool remains stable, cache hit rates remain above 99%, and transactional commit latencies are completely unaffected. Replication from InnoDB to HeatWave memory completes within sub-second thresholds.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;learning&quot;&gt;Learning&lt;/h3&gt;
&lt;p&gt;HeatWave succeeds where sub-second freshness against operational data is non-negotiable and where teams lack the platform engineering bandwidth to maintain resilient Kafka/CDC pipelines. However, HeatWave does not replace a long-term enterprise data lake: if downstream data consumers require complex cross-database joins across Salesforce, Stripe, and internal microservices, an independent cloud data warehouse remains necessary.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;MySQL HeatWave is not a silver bullet for all database scalability problems. It introduces distinct architectural constraints:&lt;/p&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Root Cause&lt;/th&gt;&lt;th&gt;Impact&lt;/th&gt;&lt;th&gt;Mitigation Strategy&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Cluster Out-Of-Memory (OOM) Crash&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Complex analytical queries executing large Cartesian joins or high-cardinality group-bys exceed total node RAM.&lt;/td&gt;&lt;td&gt;HeatWave worker node process terminates; in-flight queries fail and fall back to slow InnoDB execution.&lt;/td&gt;&lt;td&gt;Run MySQL Autopilot Advisor to verify memory requirements before loading; enforce query timeout limits and resource governance.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;DDL Schema Lockup&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Executing an &lt;code&gt;ALTER TABLE&lt;/code&gt; on a table loaded into HeatWave requires unloading the table from the secondary engine.&lt;/td&gt;&lt;td&gt;Dropping or re-loading multi-gigabyte tables from InnoDB to HeatWave causes severe network and CPU spikes.&lt;/td&gt;&lt;td&gt;Schedule schema changes during off-peak windows; use HeatWave Lakehouse for append-only tables to decouple DDL from InnoDB.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;High Write Replication Lag&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Massive batch updates (&lt;code&gt;UPDATE orders SET status = ...&lt;/code&gt; touching millions of rows) overwhelm the Change Propagator.&lt;/td&gt;&lt;td&gt;HeatWave in-memory delta buffers build backlog; analytical queries observe stale data or block awaiting synchronization.&lt;/td&gt;&lt;td&gt;Break massive batch writes into chunked transactions; avoid running continuous ETL updates directly on the OLTP primary.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Single-Writer Throughput Ceiling&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;The application exceeds 40,000 write IOPS or saturates the vertical compute capacity of the primary MySQL instance.&lt;/td&gt;&lt;td&gt;Write latency increases across all transactions; HeatWave does nothing to scale write capacity.&lt;/td&gt;&lt;td&gt;Implement horizontal transactional sharding (e.g., Vitess) or partition workloads by tenant into multiple independent DB systems.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Cold Cluster Startup Delay&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;After a scheduled maintenance restart or cluster resize, in-memory caches are unpopulated.&lt;/td&gt;&lt;td&gt;Queries experience high initial latency while data hydrates from OCI Object Storage into node RAM.&lt;/td&gt;&lt;td&gt;Execute automated warmup scripts post-restart; size clusters to maintain sufficient standby capacity during rolling maintenance.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;When evaluating MySQL HeatWave for production workloads, follow this 4P decision framework:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Identify whether your current operational bottlenecks stem from analytical query interference on InnoDB (buffer pool churn, latch contention) or from the maintenance and latency tax of CDC/ETL pipelines feeding external warehouses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Deploy MySQL HeatWave when your workload demands real-time operational reporting (sub-second to sub-minute data freshness) directly against relational MySQL schemas, and where you want to eliminate the operational cost of managing dual database platforms.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run a side-by-side benchmark comparing query execution times and primary buffer pool hit ratios between InnoDB read replicas and an attached HeatWave cluster. Use &lt;code&gt;EXPLAIN&lt;/code&gt; to verify that the MySQL cost optimizer successfully selects the &lt;code&gt;RAPID&lt;/code&gt; secondary engine.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your analytical SQL workload against HeatWave’s supported SQL syntax and data types. Use MySQL Autopilot to model required cluster shapes and memory allocations before migrating production traffic. If write throughput is your primary constraint, investigate transactional sharding rather than analytical acceleration.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category></item><item><title>Building a Safe MongoDB Index Recommendation Engine: The ESR Rule, Prefix Deduplication, and Write Penalties</title><link>https://rajivonai.com/blog/2026-05-22-index-recommendation-engine/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-22-index-recommendation-engine/</guid><description>How to automate index recommendations using the Equality-Sort-Range rule to eliminate collection scans while pruning redundant prefixes and preventing WiredTiger write amplification.</description><pubDate>Sun, 16 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Automating database index recommendations is not merely an optimization to accelerate slow queries—it is a defensive mechanism to protect WiredTiger from the synchronous write amplification and cache bloat of redundant B-tree indexes.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Ungoverned per-endpoint index PRs create overlapping left-prefix indexes and misordered &lt;code&gt;E-R-S&lt;/code&gt; compound keys, multiplying WiredTiger B-tree writes (&lt;code&gt;1 + N_indexes&lt;/code&gt;) per insert and forcing blocking sorts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Build a CI/CD index synthesizer that classifies query shapes via the Equality-Sort-Range (ESR) rule, rejects candidates that are left-prefixes of existing indexes, and safely subsumes narrower non-unique indexes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Never drop a subsumed index if it enforces &lt;code&gt;unique: true&lt;/code&gt; or a distinct &lt;code&gt;partialFilterExpression&lt;/code&gt;, and never trust &lt;code&gt;$indexStats&lt;/code&gt; &lt;code&gt;accesses.ops == 0&lt;/code&gt; after a recent &lt;code&gt;mongod&lt;/code&gt; restart.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-22-index-recommendation-engine.svg&quot; alt=&quot;Every proposed index must pass ESR ordering and prefix pruning&quot;&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Production Scope: MongoDB 5.0 / 6.0 / 7.0 / 8.0 on AWS EC2 + EBS &lt;code&gt;gp3&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In microservice architectures backed by self-managed MongoDB replica sets on AWS EC2 (&lt;code&gt;r6g.2xlarge&lt;/code&gt; with 64 GB RAM and ~31.5 GB WiredTiger cache, paired with EBS &lt;code&gt;gp3&lt;/code&gt; volumes), application teams optimize for single-endpoint read latency while platform engineers must protect the shared WiredTiger cache and storage IOPS budget. As collections grow past tens of millions of documents into multi-terabyte datasets, unindexed query predicates trigger &lt;code&gt;COLLSCAN&lt;/code&gt; operations that pull cold data pages into cache, evict the active working set, and exhaust concurrent read tickets.&lt;/p&gt;
&lt;p&gt;When developers see request timeouts, the common response is to open a pull request adding a single-field index for each new filter predicate. Without algorithmic governance, collections accumulate overlapping B-tree indexes that share identical leading keys, inflating checkpoint flush volume, doubling backup snapshot sizes, and degrading write latency across the entire replica set.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;h3 id=&quot;three-failure-modes-of-unregulated-indexing&quot;&gt;Three Failure Modes of Unregulated Indexing&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Pathological Collection Scans (&lt;code&gt;COLLSCAN&lt;/code&gt;):&lt;/strong&gt; Queries filtering on unindexed predicates inspect every document in the collection. When the collection exceeds the WiredTiger cache size (~31.5 GB on an &lt;code&gt;r6g.2xlarge&lt;/code&gt;), a single &lt;code&gt;COLLSCAN&lt;/code&gt; pulls cold data pages from EBS into cache, evicting hot working-set B-tree pages and causing latency spikes across unrelated indexed endpoints.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Redundant Prefix Bloat:&lt;/strong&gt; Adding &lt;code&gt;{ status: 1 }&lt;/code&gt; when &lt;code&gt;{ status: 1, created_at: -1 }&lt;/code&gt; already exists wastes memory and disk. Because MongoDB B-trees satisfy any left-to-right prefix of a compound index, &lt;code&gt;{ status: 1 }&lt;/code&gt; serves zero queries that the compound index cannot already serve, yet consumes WiredTiger cache and backup snapshot capacity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Synchronous Write Amplification ($W_{\text{mult}} = 1 + N_{\text{indexes}}$):&lt;/strong&gt; WiredTiger updates every index synchronously inside the document write path. A collection with 10 indexes executes 11 distinct B-tree mutations per insert (1 base collection row-store + 10 index row-stores). At 5,000 inserts per second, 10 indexes generate 55,000 B-tree page mutations per second. On an &lt;code&gt;r6g.2xlarge&lt;/code&gt; with a ~31.5 GB WiredTiger cache, the 20% &lt;code&gt;eviction_dirty_trigger&lt;/code&gt; sits at ~6.3 GB and the 5% &lt;code&gt;eviction_dirty_target&lt;/code&gt; at ~1.58 GB; if each uncoalesced mutation dirties a 4 KB leaf page, peak dirty-page generation approaches ~220 MB/s, pushing application threads into synchronous eviction stalls once dirty bytes cross 20%.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Blocking Sort Ceiling (100 MB):&lt;/strong&gt; Compound indexes ordered with Range before Sort (&lt;code&gt;E-R-S&lt;/code&gt;) cannot return keys in sort order, forcing a blocking &lt;code&gt;SORT&lt;/code&gt; stage that aborts at 100 MB (&lt;code&gt;internalQueryMaxBlockingSortMemoryUsageBytes&lt;/code&gt; = &lt;code&gt;104,857,600&lt;/code&gt; bytes in MongoDB 4.4+; 32 MB in 4.2 and earlier).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-22-index-recommendation-engine-2-constraints.svg&quot; alt=&quot;Every extra index multiplies WiredTiger B-tree writes per insert&quot;&gt;&lt;/p&gt;
&lt;p&gt;The core architectural question is: &lt;strong&gt;How can platform teams automate index recommendations that synthesize ESR-optimal compound keys, reject redundant left-prefixes, and prevent write amplification before indexes reach production?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Approach&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Read Latency &amp;#x26; Sort&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Write Amplification&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Prefix Governance&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Ad-Hoc Developer PRs&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Inconsistent; frequent &lt;code&gt;E-R-S&lt;/code&gt; mistakes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unbounded index growth&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Manual review only; misses prefixes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Accumulates redundant B-trees under feature velocity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Single-Field Per Predicate&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Forces index intersection or blocking &lt;code&gt;SORT&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High; 1 B-tree per field&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;None; creates overlapping singletons&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Cannot satisfy compound equality + sort natively&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;ESR + Prefix Pruning Engine&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Eliminates &lt;code&gt;SORT&lt;/code&gt; via native B-tree order&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bounded by subsuming narrow indexes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejects left-prefixes automatically&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen:&lt;/strong&gt; Optimizes both read plans and WiredTiger write budget&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Ad-hoc PRs and single-field-per-predicate indexing were rejected because neither enforces &lt;code&gt;Equality -&gt; Sort -&gt; Range&lt;/code&gt; ordering nor prevents left-prefix duplication. While MongoDB can occasionally combine two single-field indexes via index intersection (&lt;code&gt;AND_HASH&lt;/code&gt; or &lt;code&gt;AND_SORTED&lt;/code&gt;), intersection cannot eliminate a blocking &lt;code&gt;SORT&lt;/code&gt; stage when sort fields are unindexed and requires scanning and intersecting two separate B-trees. Wildcard indexes (&lt;code&gt;{ &quot;$**&quot;: 1 }&lt;/code&gt;) were also rejected for OLTP collections because they cannot support compound ESR sort-and-filter plans and index every dynamic subfield indiscriminately. An automated ESR synthesizer paired with catalog prefix checking enforces both read-plan optimality and write-budget bounds deterministically in CI/CD.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-22-index-recommendation-engine-3-options.svg&quot; alt=&quot;Automated ESR synthesis outperforms ad-hoc and single-field indexing&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-recommendation-architecture-the-esr-theorem-and-prefix-pruning&quot;&gt;The Recommendation Architecture: The ESR Theorem and Prefix Pruning&lt;/h2&gt;
&lt;p&gt;An automated recommendation engine parses slow query shapes from &lt;code&gt;mongod.log&lt;/code&gt;, classifies predicates into Equality, Sort, and Range buckets, and validates the synthesized candidate against &lt;code&gt;listIndexes()&lt;/code&gt; before approving any build.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-22-index-recommendation-engine-4-architecture.svg&quot; alt=&quot;ESR synthesis and catalog deduplication pipeline&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    LogSource[&quot;Structured Slow Logs — mongod.log&quot;] --&gt; Parser[&quot;Query Shape Parser — AST Tokenizer&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Parser --&gt; Classifier[&quot;Predicate Classifier — Equality / Sort / Range&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Classifier --&gt; EStage[&quot;1. Equality Fields — Exact Matches&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    EStage --&gt; SStage[&quot;2. Sort Fields — Ordering Direction&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SStage --&gt; RStage[&quot;3. Range Fields — Inequalities&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    RStage --&gt; Candidate[&quot;Synthesized Candidate Compound Index&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Candidate --&gt; FetchExisting[&quot;Fetch Existing Indexes via getIndexes&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    FetchExisting --&gt; PrefixCheck{&quot;Candidate a Prefix of Existing Index?&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PrefixCheck --&gt;|Yes| RejectRedundant[&quot;REJECT — Existing Index Satisfies Query&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PrefixCheck --&gt;|No| SubsumeCheck{&quot;Candidate Subsumes Existing Index?&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SubsumeCheck --&gt;|Yes| FlagReplacement[&quot;RECOMMEND DROP — Replace Narrow Index&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SubsumeCheck --&gt;|No| CardinalityCheck[&quot;Check Cardinality &amp;#x26; Multikey Bounds&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CardinalityCheck --&gt; Approved[&quot;Approved Spec — Stage Rolling Build&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-the-equality-sort-range-esr-rule&quot;&gt;1. The Equality, Sort, Range (ESR) Rule&lt;/h3&gt;
&lt;p&gt;Compound index keys must follow strict ESR ordering:&lt;/p&gt;
&lt;p&gt;$$\text{Index Key Order} = [\text{Equality Fields}] \longrightarrow [\text{Sort Fields}] \longrightarrow [\text{Range Fields}]$$&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Equality (&lt;code&gt;E&lt;/code&gt;) First:&lt;/strong&gt; Exact-match predicates (&lt;code&gt;{ tenant_id: &quot;T-102&quot;, status: &quot;ACTIVE&quot; }&lt;/code&gt;) narrow the B-tree search to a single contiguous branch. Order higher-cardinality equality fields first when sharing prefixes across multiple query shapes. Note that &lt;code&gt;$in&lt;/code&gt; behaves like equality when combined with no sort, or as multiple equality branches when sorted; however, large &lt;code&gt;$in&lt;/code&gt; arrays paired with a sort force a blocking &lt;code&gt;SORT&lt;/code&gt; unless the index covers the sort key cleanly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sort (&lt;code&gt;S&lt;/code&gt;) Second:&lt;/strong&gt; Sort fields (&lt;code&gt;.sort({ created_at: -1 })&lt;/code&gt;) immediately follow equality keys so WiredTiger reads leaf entries in exact output order, eliminating any blocking &lt;code&gt;SORT&lt;/code&gt; stage and avoiding the 100 MB &lt;code&gt;internalQueryMaxBlockingSortMemoryUsageBytes&lt;/code&gt; abort threshold. Remember that B-tree index traversal can walk a single-field or compound sort key in reverse (&lt;code&gt;-1&lt;/code&gt;) only if all sort fields match the index direction or its exact inverse (&lt;code&gt;{ a: 1, b: -1 }&lt;/code&gt; satisfies &lt;code&gt;{ a: 1, b: -1 }&lt;/code&gt; and &lt;code&gt;{ a: -1, b: 1 }&lt;/code&gt;, but not &lt;code&gt;{ a: 1, b: 1 }&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Range (&lt;code&gt;R&lt;/code&gt;) Last:&lt;/strong&gt; Inequality predicates (&lt;code&gt;$gt&lt;/code&gt;, &lt;code&gt;$gte&lt;/code&gt;, &lt;code&gt;$lt&lt;/code&gt;, &lt;code&gt;$lte&lt;/code&gt;, &lt;code&gt;$ne&lt;/code&gt;, &lt;code&gt;$regex&lt;/code&gt;) scan across multiple B-tree leaf ranges and must come last. Placing a range key before a sort key (&lt;code&gt;E-R-S&lt;/code&gt;) breaks sort order guarantees and forces an in-memory &lt;code&gt;SORT&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Covered Queries (&lt;code&gt;PROJECTION_COVERED&lt;/code&gt;):&lt;/strong&gt; When every field in the filter, sort, and projection exists in the index—and &lt;code&gt;_id&lt;/code&gt; is explicitly excluded (&lt;code&gt;{ _id: 0 }&lt;/code&gt;) unless &lt;code&gt;_id&lt;/code&gt; is part of the index—&lt;code&gt;explain()&lt;/code&gt; reports &lt;code&gt;PROJECTION_COVERED&lt;/code&gt; with &lt;code&gt;totalDocsExamined: 0&lt;/code&gt;, avoiding collection document fetches entirely.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-algorithmic-prefix-deduplication-and-subsumption&quot;&gt;2. Algorithmic Prefix Deduplication and Subsumption&lt;/h3&gt;
&lt;p&gt;Given &lt;code&gt;{ tenant_id: 1, status: 1, created_at: -1 }&lt;/code&gt;, MongoDB natively satisfies queries on &lt;code&gt;{ tenant_id: 1 }&lt;/code&gt;, &lt;code&gt;{ tenant_id: 1, status: 1 }&lt;/code&gt;, and &lt;code&gt;{ tenant_id: 1, status: 1 }&lt;/code&gt; sorted by &lt;code&gt;created_at: -1&lt;/code&gt;. When a proposed index is a left-prefix of an existing index, the engine rejects it immediately. Conversely, when a new ESR candidate extends an existing narrower index (and the narrower index is not &lt;code&gt;unique&lt;/code&gt;, &lt;code&gt;sparse&lt;/code&gt;, &lt;code&gt;expireAfterSeconds&lt;/code&gt;, or &lt;code&gt;partialFilterExpression&lt;/code&gt;-scoped), the engine schedules a subsumption replacement.&lt;/p&gt;
&lt;p&gt;Three index types require explicit subsumption exemptions. First, a partial index (&lt;code&gt;partialFilterExpression&lt;/code&gt;) omits non-matching documents to stay small; replacing it with an unfiltered compound index inflates B-tree footprint, while replacing a non-partial index with a partial one causes any query omitting the partial predicate to fall back to &lt;code&gt;COLLSCAN&lt;/code&gt;. Second, TTL indexes (&lt;code&gt;expireAfterSeconds&lt;/code&gt;) must remain single-field indexes because MongoDB’s background TTL monitor does not evaluate compound keys. Third, &lt;code&gt;unique: true&lt;/code&gt; constraints enforce uniqueness strictly across the declared key tuple; widening a unique key (&lt;code&gt;{ email: 1 }&lt;/code&gt; to &lt;code&gt;{ email: 1, tenant_id: 1 }&lt;/code&gt;) weakens the constraint and permits duplicate values.&lt;/p&gt;
&lt;p&gt;Before approving any candidate, the validation gate also checks &lt;strong&gt;multikey array bounds&lt;/strong&gt;: if a document field is an array, MongoDB creates an index entry for every element in that array. Indexing two array fields in the same compound index is forbidden by MongoDB (&lt;code&gt;cannot index parallel arrays&lt;/code&gt;) because the Cartesian product of two arrays would explode index cardinality. Even a single array field with hundreds of elements multiplies &lt;code&gt;W_mult&lt;/code&gt; proportionally on every document update.&lt;/p&gt;
&lt;h3 id=&quot;deep-internals-wiredtiger-row-store-b-trees-and-prefix-compression&quot;&gt;Deep Internals: WiredTiger Row-Store B-Trees and Prefix Compression&lt;/h3&gt;
&lt;p&gt;Every MongoDB collection and secondary index is stored as an independent WiredTiger row-store B-tree file (&lt;code&gt;index-*.wt&lt;/code&gt;). Index internal pages default to 4 KB (&lt;code&gt;internal_page_max&lt;/code&gt;) to fit CPU L1/L2 caches, while leaf pages store BSON-encoded index keys mapped to &lt;code&gt;RecordId&lt;/code&gt; pointers.&lt;/p&gt;
&lt;p&gt;Unlike collection data blocks—which are compressed on disk (Snappy or Zstandard) and uncompressed in WiredTiger RAM—secondary index pages remain &lt;strong&gt;prefix-compressed inside the WiredTiger cache&lt;/strong&gt; (&lt;code&gt;prefixCompression: true&lt;/code&gt; by default). Consecutive sorted keys share common leading bytes once per page, reducing index RAM footprint at the cost of minor CPU cycles to reconstruct keys during traversal. However, every insert, update, or delete must still traverse each index B-tree from root to leaf, acquire hazard pointers to pin pages during concurrent reads, dirty 4 KB leaf pages, and flush them during the 60-second (or 2 GB journal) checkpoint cycle via &lt;code&gt;fsync&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;diagnostic-telemetry-auditing-indexstats-and-cache-footprint&quot;&gt;Diagnostic Telemetry: Auditing &lt;code&gt;$indexStats&lt;/code&gt; and Cache Footprint&lt;/h3&gt;
&lt;p&gt;Audit unused indexes with &lt;code&gt;$indexStats&lt;/code&gt; (remembering that counters reset on &lt;code&gt;mongod&lt;/code&gt; restart or replica set failover, and that secondary nodes only record reads routed via non-primary read preferences):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.orders.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aggregate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;([&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { $indexStats: {} },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { $project: { name: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, key: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, ops: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$accesses.ops&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, since: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$accesses.since&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { $sort: { ops: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Inspect physical index sizes and WiredTiger row-store B-tree depth using &lt;code&gt;stats({ indexDetails: true })&lt;/code&gt;. Compare &lt;code&gt;totalIndexSize&lt;/code&gt; directly against your WiredTiger internal cache budget (by default 50% of &lt;code&gt;(RAM - 1 GB)&lt;/code&gt;). When &lt;code&gt;totalIndexSize&lt;/code&gt; approaches or exceeds the cache budget, index B-trees evict each other and hot collection documents continuously, turning even indexed point lookups into synchronous EBS &lt;code&gt;gp3&lt;/code&gt; reads. Checking &lt;code&gt;maximum tree depth&lt;/code&gt; also flags fragmented B-trees after heavy delete workloads:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; stats&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.orders.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;stats&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ indexDetails: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;Object.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;keys&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(stats.indexSizes).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; btree&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stats.indexDetails[name]?.btree;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(name, (stats.indexSizes[name] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1048576&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot; MB&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;depth=&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; btree?.[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;maximum tree depth&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;internalPages=&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; btree?.[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;row-store internal pages&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;automated-esr-synthesizer-cli-mongo_index_synthesizerpy&quot;&gt;Automated ESR Synthesizer CLI (&lt;code&gt;mongo_index_synthesizer.py&lt;/code&gt;)&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env python3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse, json, sys&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo.errors &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PyMongoError&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;RANGE_OPS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$gt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$gte&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$lt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$lte&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$ne&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$regex&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; classify_predicates&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(filter_dict, sort_dict):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    eq_fields, range_fields &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [], []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; field, cond &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; filter_dict.items():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; isinstance&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(cond, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;dict&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; any&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(op &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; RANGE_OPS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; op &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cond):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            range_fields.append(field)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            eq_fields.append(field)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; eq_fields, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(sort_dict.keys()), range_fields&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; synthesize_esr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(eq_fields, sort_fields, range_fields, sort_dict):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    keys, seen &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [], &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; f, d &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [(x, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; x &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; eq_fields] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [(x, sort_dict.get(x, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; x &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sort_fields] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [(x, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; x &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; range_fields]:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; f &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;not&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; seen:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            keys.append((f, d))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            seen.add(f)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; keys&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; check_redundancy&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(existing, candidate):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cand_names &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [k[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; k &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; candidate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subsumed &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; existing:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        ex_names &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [k[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; k &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, {}).items()]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ex_names) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(cand_names) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ex_names[:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(cand_names)] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cand_names:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Left-prefix of &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;idx.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;name&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(cand_names) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ex_names) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cand_names[:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ex_names)] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ex_names:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;_id_&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; and&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;unique&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;partialFilterExpression&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                subsumed.append(idx.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; False&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, subsumed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-22-index-recommendation-engine-5-flow.svg&quot; alt=&quot;Safe index subsumption builds wide before dropping narrow&quot;&gt;&lt;/p&gt;
&lt;p&gt;Replacing or adding indexes in production follows four gated steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Synthesize and Deduplicate in CI:&lt;/strong&gt; Run the ESR synthesizer against slow-query shapes from &lt;code&gt;mongod.log&lt;/code&gt;. Reject any proposal that matches an existing left-prefix or introduces a second array field in a compound index.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Build the Wider ESR Index:&lt;/strong&gt; Create the approved compound index using a rolling build protocol during low-traffic windows so secondaries do not breach oplog retention.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Verify Plan Adoption:&lt;/strong&gt; Run &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; on a hidden secondary to confirm &lt;code&gt;IXSCAN&lt;/code&gt; replaces &lt;code&gt;COLLSCAN&lt;/code&gt; or &lt;code&gt;SORT&lt;/code&gt;, and monitor &lt;code&gt;$indexStats&lt;/code&gt; to verify &lt;code&gt;accesses.ops&lt;/code&gt; shifts onto the new index.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Drop Subsumed Narrow Indexes:&lt;/strong&gt; Only after confirming the narrower index is not &lt;code&gt;unique&lt;/code&gt;, &lt;code&gt;sparse&lt;/code&gt;, or &lt;code&gt;partialFilterExpression&lt;/code&gt;-scoped—and after 30+ days of uptime—execute &lt;code&gt;db.collection.dropIndex()&lt;/code&gt; to reclaim WiredTiger cache and write IOPS. Rollback: recreate the dropped index if a legacy query shape regresses.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Documented MongoDB query planner and WiredTiger behaviors explain why ESR ordering and prefix pruning govern cluster stability:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Prefix Compression and Write Amplification:&lt;/strong&gt; Per MongoDB’s WiredTiger documentation, every secondary index maintains its own B-tree (&lt;code&gt;index-*.wt&lt;/code&gt;) that must be updated synchronously on writes. Keeping &lt;code&gt;prefixCompression: true&lt;/code&gt; (the default) reduces index cache footprint, but does not eliminate the per-index B-tree traversal and dirty-page checkpoint cost (&lt;code&gt;W_mult = 1 + N_indexes&lt;/code&gt;). Consolidating redundant left-prefix indexes directly reduces dirty-page generation per insert.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ESR vs. &lt;code&gt;E-R-S&lt;/code&gt; Execution Plans:&lt;/strong&gt; Per MongoDB’s ESR indexing documentation, placing a range predicate (&lt;code&gt;price: { $gte: 50 }&lt;/code&gt;) before a sort field (&lt;code&gt;created_at: -1&lt;/code&gt;) forces &lt;code&gt;mongod&lt;/code&gt; to scan all index keys matching the equality prefix and price range, then execute a blocking &lt;code&gt;SORT&lt;/code&gt; stage subject to the 100 MB &lt;code&gt;internalQueryMaxBlockingSortMemoryUsageBytes&lt;/code&gt; limit. Ordering the index &lt;code&gt;{ category_id: 1, created_at: -1, price: 1 }&lt;/code&gt; allows &lt;code&gt;IXSCAN&lt;/code&gt; to return documents in &lt;code&gt;created_at&lt;/code&gt; order directly, eliminating the &lt;code&gt;SORT&lt;/code&gt; stage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Safe Index Retirement with &lt;code&gt;hideIndex()&lt;/code&gt;:&lt;/strong&gt; Before running &lt;code&gt;dropIndex()&lt;/code&gt; on a subsumed or zero-access index, MongoDB 4.4+ supports &lt;code&gt;db.collection.hideIndex(&quot;index_name&quot;)&lt;/code&gt;. Hiding an index removes it from query planner candidate selection while continuing to maintain its B-tree on writes. If an unexpected batch job or reporting query regresses after hiding, &lt;code&gt;unhideIndex()&lt;/code&gt; restores the plan instantaneously without a multi-hour index rebuild; if no regressions appear over a full business cycle, &lt;code&gt;dropIndex()&lt;/code&gt; reclaims the storage and write amplification.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MongoDB 8.0 Query Controls (&lt;code&gt;setQuerySettings&lt;/code&gt;):&lt;/strong&gt; When an unindexed or misplanned query shape hits production before an index build completes, MongoDB 8.0 provides persistent, cluster-wide &lt;code&gt;setQuerySettings&lt;/code&gt; with &lt;code&gt;indexHints&lt;/code&gt; (replacing deprecated &lt;code&gt;planCacheSetFilter&lt;/code&gt;) to constrain candidate indexes:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;adminCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  setQuerySettings: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    find: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;orders&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    filter: { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;tenant_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;T-100&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;status&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ACTIVE&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    sort: { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;created_at&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    $db: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;production_commerce&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  settings: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    indexHints: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      ns: { db: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;production_commerce&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, coll: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;orders&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      allowedIndexes: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;tenant_id_1_status_1_created_at_-1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-22-index-recommendation-engine-6-failures.svg&quot; alt=&quot;Four indexing traps that bypass naive automation&quot;&gt;&lt;/p&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Operational Action&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Physical Failure Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Architectural Consequence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Safe Mitigation Protocol&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Low-Cardinality Leading Keys&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Leading boolean or status field (&lt;code&gt;{ is_active: 1, user_id: 1 }&lt;/code&gt;).&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails to partition B-tree; forces wide scans and wastes cache.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Put high-cardinality equality keys first or use &lt;code&gt;partialFilterExpression&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Parallel Array Multikey Index&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Two array fields in one compound index (&lt;code&gt;{ tags: 1, categories: 1 }&lt;/code&gt;).&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;MongoDB rejects writes (&lt;code&gt;cannot index parallel arrays&lt;/code&gt;).&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce schema lint rules allowing at most one array field per index.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Dropping After Recent Restart&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reading &lt;code&gt;$indexStats&lt;/code&gt; &lt;code&gt;ops == 0&lt;/code&gt; shortly after &lt;code&gt;mongod&lt;/code&gt; restart.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Counters reset on restart; drops indexes needed by monthly batches.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Require 30+ days continuous uptime before approving index drops.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Concurrent Primary Index Builds&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Building large indexes on primary during peak write traffic.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Saturates EBS IOPS and causes secondary replication lag.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use rolling builds or schedule during low-write maintenance windows.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Oversized String Index Keys&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Indexing raw multi-KB URLs or payloads (4.2+ removed 1,024-byte cap).&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bloats WiredTiger index cache and reduces B-tree fanout.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use hashed indexes or store a fixed-length SHA-256 hash field.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Partial Index Filter Mismatch&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Query omits the predicate defined in &lt;code&gt;partialFilterExpression&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Planner cannot prove coverage and falls back to &lt;code&gt;COLLSCAN&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Verify query filters strictly imply the partial filter predicate.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Subsuming a Unique Index&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Dropping &lt;code&gt;{ email: 1 }&lt;/code&gt; (&lt;code&gt;unique: true&lt;/code&gt;) for &lt;code&gt;{ email: 1, tenant_id: 1 }&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Removes uniqueness enforcement on &lt;code&gt;email&lt;/code&gt;, allowing duplicates.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Block automated drops on any index with &lt;code&gt;unique&lt;/code&gt; or &lt;code&gt;sparse&lt;/code&gt; flags.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Over-Indexing Write-Heavy Tables&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Maintaining &gt;10–15 indexes on high-insert collections.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Synchronous write amplification exhausts dirty-cache eviction.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Prune left-prefixes via ESR and cap total index count per collection.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Every index adds synchronous write overhead:&lt;/strong&gt; WiredTiger updates &lt;code&gt;1 + N_indexes&lt;/code&gt; row-store B-trees per insert, directly consuming dirty-cache budget and EBS &lt;code&gt;gp3&lt;/code&gt; IOPS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Strict ESR ordering eliminates blocking sorts:&lt;/strong&gt; Ordering keys as &lt;code&gt;Equality -&gt; Sort -&gt; Range&lt;/code&gt; satisfies sorting natively from the B-tree and avoids the 100 MB &lt;code&gt;internalQueryMaxBlockingSortMemoryUsageBytes&lt;/code&gt; limit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Left-prefix pruning prevents catalog bloat:&lt;/strong&gt; Reject any proposed index that is a left-prefix of an existing compound index, and subsume narrower non-unique indexes only after verifying plan adoption.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Guardrails beat blind automation:&lt;/strong&gt; Never drop indexes with &lt;code&gt;unique: true&lt;/code&gt; or &lt;code&gt;partialFilterExpression&lt;/code&gt; via prefix subsumption, and never trust &lt;code&gt;$indexStats&lt;/code&gt; until &lt;code&gt;mongod&lt;/code&gt; uptime spans at least 30 days.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Production collections accumulate overlapping left-prefix indexes and misordered compound keys that degrade write throughput and trigger blocking sorts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Enforce an automated ESR synthesizer and prefix-deduplication gate in CI/CD before any &lt;code&gt;createIndex()&lt;/code&gt; reaches production.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run &lt;code&gt;$indexStats&lt;/code&gt; and &lt;code&gt;stats({ indexDetails: true })&lt;/code&gt; to identify zero-access indexes and measure total index RAM footprint against collection data size.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your highest-write collection this week for left-prefix duplicates, verify &lt;code&gt;unique&lt;/code&gt; and &lt;code&gt;partialFilterExpression&lt;/code&gt; constraints, and schedule &lt;code&gt;dropIndex()&lt;/code&gt; for confirmed redundant prefixes.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>Cloud SQL vs AlloyDB vs Spanner: A PostgreSQL Decision Framework on Google Cloud</title><link>https://rajivonai.com/blog/2026-08-15-cloud-sql-vs-alloydb-vs-spanner-decision-framework/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-08-15-cloud-sql-vs-alloydb-vs-spanner-decision-framework/</guid><description>A requirements-first framework for choosing Cloud SQL, AlloyDB, manual PostgreSQL sharding, or Spanner without invented throughput thresholds.</description><pubDate>Sat, 15 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Database selection fails when a benchmark number substitutes for a workload model. There is no universal TPS boundary where Cloud SQL becomes AlloyDB or Spanner; transaction shape, key distribution, latency, consistency, and operational ownership determine the break point.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Google Cloud offers three materially different relational services with PostgreSQL-adjacent interfaces: Cloud SQL for PostgreSQL, AlloyDB for PostgreSQL, and Spanner’s PostgreSQL interface. A fourth option—sharding PostgreSQL across Cloud SQL instances—preserves more PostgreSQL behavior but transfers distributed-systems work to the application and platform teams.&lt;/p&gt;
&lt;p&gt;A useful decision must account for current requirements and credible growth: extensions, write locality, analytical contention, geographic placement, transaction boundaries, recovery objectives, and team capability.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;“PostgreSQL compatible” hides different meanings. Cloud SQL runs PostgreSQL with managed-service restrictions. AlloyDB provides high PostgreSQL compatibility over Google’s disaggregated architecture and adds service-specific capabilities. Spanner implements a PostgreSQL dialect and supported client compatibility over a different distributed database engine.&lt;/p&gt;
&lt;p&gt;The original decision tree used 50,000 TPS as a rule of thumb. That threshold was unsupported and misleading. Ten thousand large, contended, multi-row transactions can be harder than a much larger number of independent point writes. The core question is: &lt;strong&gt;which hard requirement eliminates an option, and what evidence demonstrates the remaining choice?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;architecture-problem&quot;&gt;Architecture Problem&lt;/h2&gt;
&lt;p&gt;The four options disagree at the point where a write becomes durable.&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;Cloud SQL&lt;/strong&gt;, an application sends a transaction to one PostgreSQL primary. In a regional HA configuration, Google runs a standby in another zone and synchronously replicates writes to regional persistent disk. The standby is not a read endpoint. A failover retains the instance connection name and IP address, but existing sessions close and applications must reconnect. Read replicas are separate, asynchronous resources; they can lag and are not substitutes for the HA standby. Cross-region promotion changes the serving instance and therefore belongs in a disaster-recovery runbook, not an automatic reconnect assumption.&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;AlloyDB&lt;/strong&gt;, writes still enter one writable primary instance, but compute and storage are disaggregated. Google documents that the active node writes WAL to a regional log persistor; log processing servers then materialize data blocks into regional storage. A highly available primary has an active and standby node in different zones. Read pool instances attach to the cluster storage and expose read-only endpoints. This reduces coupling between read compute and storage, but it does not distribute the writable SQL execution path across multiple primaries.&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;Spanner&lt;/strong&gt;, the primary key determines a key range, or split. Splits are replicated and have leaders. A read-write transaction that touches multiple splits coordinates across their participants; Spanner uses locking concurrency control, wound-wait deadlock prevention, distributed commit, and TrueTime in commit ordering. There is no PostgreSQL primary to resize or promote. The corresponding risk moves into schema distribution, transaction scope, contention, and client retries.&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;sharded Cloud SQL&lt;/strong&gt;, each shard has the Cloud SQL write path, but the routing layer becomes a new control plane. A tenant transfer, shard split, or cross-shard workflow now has two failure domains: PostgreSQL and the application-owned coordinator. The database service cannot make those independent primaries atomic.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Write[Business transaction] --&gt; CSQL[Cloud SQL — one PostgreSQL primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Write --&gt; Alloy[AlloyDB — one writable primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Write --&gt; Span[Spanner — key-range leaders]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Write --&gt; Router[Shard router]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Router --&gt; ShardA[Cloud SQL shard A]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Router --&gt; ShardB[Cloud SQL shard B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CSQL --&gt; RegionalDisk[Regional persistent disk]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Alloy --&gt; Log[Regional log persistor]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Log --&gt; Blocks[Regional data blocks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Span --&gt; Consensus[Replicated split groups]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The failure boundary follows that write path. Cloud SQL and AlloyDB can lose sessions when compute fails even if durable data survives. Spanner can preserve service through replica failures while an application still suffers aborts or latency from a hot key range. Sharding can keep one tenant’s failure isolated while a routing error sends writes to the wrong owner. “Highly available” therefore needs an object: data, connection endpoint, transaction coordinator, region, or application workflow.&lt;/p&gt;
&lt;h2 id=&quot;the-requirements-first-decision&quot;&gt;The Requirements-First Decision&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Start[Workload requirements] --&gt; Compat{Needs PostgreSQL behavior unsupported by AlloyDB or Spanner}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Compat --&gt;|Yes| Scale{One writer satisfies measured headroom}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Scale --&gt;|Yes| CloudSQL[Cloud SQL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Scale --&gt;|No| Shard[Shard PostgreSQL deliberately]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Compat --&gt;|No| Global{Needs atomic transactions across distributed key ranges}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Global --&gt;|Yes| Spanner[Spanner]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Global --&gt;|No| HTAP{Scan-heavy analytics harms OLTP}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    HTAP --&gt;|Yes| AlloyDB[Evaluate AlloyDB]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    HTAP --&gt;|No| Default[Start with Cloud SQL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is an elimination tree, not a product ranking. If hard requirements point to conflicting branches—for example, an unsupported PostgreSQL extension plus native distributed transactions—none of the services qualifies without application or requirement redesign.&lt;/p&gt;
&lt;h2 id=&quot;design-options&quot;&gt;Design Options&lt;/h2&gt;
&lt;p&gt;Start by stating the invariants that cannot be negotiated. Examples include atomic balance transfers across arbitrary accounts, PostGIS behavior required by an existing application, a read-after-write contract for a user session, or a residency rule that prevents data from leaving one geography. Then eliminate any option that cannot meet an invariant using a documented GA capability. Preview features can be evaluated, but they should not silently carry a production availability or support requirement.&lt;/p&gt;
&lt;p&gt;The most important option may be &lt;strong&gt;no database change&lt;/strong&gt;. If profiling shows that a few queries, unbounded pools, lock contention, or missing indexes consume the primary, moving the same workload preserves the defect and adds migration risk. Establish a tuned Cloud SQL baseline before attributing the limit to architecture.&lt;/p&gt;
&lt;p&gt;The second no-fit case is &lt;strong&gt;separating analytics instead of choosing an HTAP database&lt;/strong&gt;. If reports tolerate pipeline delay and require large historical scans, exporting changes to BigQuery can provide a clearer resource and cost boundary than keeping analytics beside transactions. AlloyDB’s columnar engine is a fit when freshness and operational simplicity justify co-location and representative queries show the benefit; it is not an obligation to keep every analytical workload in the transactional system.&lt;/p&gt;
&lt;p&gt;The third no-fit case is &lt;strong&gt;choosing a non-PostgreSQL interface intentionally&lt;/strong&gt;. Spanner’s PostgreSQL interface helps supported clients and SQL, but teams that need Spanner-specific capabilities may prefer native client libraries rather than forcing an ORM through PGAdapter. Conversely, an application dependent on unsupported extensions, user-defined stored procedures, or PostgreSQL administrative semantics is not a Spanner candidate merely because simple queries compile.&lt;/p&gt;
&lt;p&gt;Use explicit admission criteria:&lt;/p&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Evidence that admits it&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Evidence that eliminates it&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Cloud SQL&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Tuned single primary meets forecast with failure headroom; required PostgreSQL surface is supported&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Measured write path exceeds the largest acceptable configuration or distribution is a hard requirement&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Sharded Cloud SQL&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Stable shard key; bounded cross-shard operations; team can operate routing and rebalancing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Arbitrary cross-shard atomic transactions or globally ordered queries are business invariants&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;AlloyDB&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;PostgreSQL compatibility audit passes; read pools or columnar engine improve the measured workload&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Required extension is unsupported, or the primary write path remains the limiting resource&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Spanner&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Distributed transaction boundary and key model are demonstrated; retry semantics are built into the application&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;PostgreSQL engine behavior is required, or distribution adds no material correctness or scale value&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Every admission decision needs a falsifiable statement. “Choose AlloyDB because analytics are slow” becomes “the identified scan-heavy queries reduce checkout p99 through cache eviction on Cloud SQL, and the same production trace meets both checkout and reporting objectives on the tested AlloyDB configuration.” “Choose Spanner for scale” becomes “the transaction must atomically update accounts whose owners cannot share one shard, and the proposed keys distribute the measured write workload without a hot range.”&lt;/p&gt;
&lt;h3 id=&quot;cloud-sql-managed-postgresql-as-the-default&quot;&gt;Cloud SQL: managed PostgreSQL as the default&lt;/h3&gt;
&lt;p&gt;Choose Cloud SQL when one writable PostgreSQL instance meets measured peak demand with failure headroom, and PostgreSQL extensions, tooling, or behavior are important. Regional HA, read replicas, backups, and PITR are separate features that must be configured and tested. Do not call compatibility “full”: Cloud SQL restricts host access, supported extensions, flags, and superuser capabilities.&lt;/p&gt;
&lt;h3 id=&quot;alloydb-postgresql-compatible-disaggregation-and-htap-tools&quot;&gt;AlloyDB: PostgreSQL-compatible disaggregation and HTAP tools&lt;/h3&gt;
&lt;p&gt;Evaluate AlloyDB when a PostgreSQL-compatible application is constrained by a workload its architecture addresses: scan-heavy analytics competing with transactions, read scaling through read pools, or a need to test its columnar engine. AlloyDB still has one writable primary endpoint. Its storage and reads can scale independently, but that is not horizontal write compute.&lt;/p&gt;
&lt;p&gt;Migration is not automatically lift-and-shift. Audit extensions, flags, logical replication, authentication, and performance-sensitive SQL. Benchmark the columnar engine with representative plans instead of applying Google’s “up to” result as an estimate.&lt;/p&gt;
&lt;h3 id=&quot;spanner-distributed-transactions-and-horizontal-scale&quot;&gt;Spanner: distributed transactions and horizontal scale&lt;/h3&gt;
&lt;p&gt;Choose Spanner when the workload needs atomic transactions and strong consistency across key ranges or locations that cannot be kept on one PostgreSQL writer, and the team accepts a Spanner redesign. The PostgreSQL interface supports a subset of PostgreSQL. PGAdapter translates the wire protocol; it does not provide PostgreSQL storage internals, extensions, triggers, or administration.&lt;/p&gt;
&lt;p&gt;Spanner offers configurations with strong availability and consistency properties, but “zero RPO” must be attached to a documented instance configuration and failure model—not asserted for every disaster scenario. Key design, transaction contention, retry behavior, and data placement remain application architecture.&lt;/p&gt;
&lt;h3 id=&quot;manual-sharding-preserve-postgresql-own-distribution&quot;&gt;Manual sharding: preserve PostgreSQL, own distribution&lt;/h3&gt;
&lt;p&gt;Shard Cloud SQL when data has a durable partition key—often tenant or geography—cross-shard transactions are rare or explicitly handled, and PostgreSQL behavior is non-negotiable. The cost is a routing layer, shard catalog, rebalancing, per-shard schema rollout, fleet-wide backup and recovery, hotspot management, and a correctness design for cross-shard workflows.&lt;/p&gt;
&lt;p&gt;Manual sharding is not “Spanner for free.” It is a decision to own the coordination Spanner provides.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Build a one-page workload contract before selecting a product:&lt;/p&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Requirement&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Evidence required&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Write demand&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Transaction mix, row and byte size, key distribution, peak duration, contention, and two-year forecast&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Consistency&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exact read-after-write and cross-entity atomicity requirements&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;PostgreSQL surface&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Extensions, types, triggers, stored procedures, drivers, ORMs, and admin tools&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Analytics&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Query plans, scan volume, concurrency, freshness target, and acceptable OLTP impact&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Resilience&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Failure model, RPO, RTO, endpoint behavior, and restore-drill results&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Geography&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;User-to-region latency, write placement, residency, and failover locations&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Operations&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;On-call skill, migration effort, feature release stage, and exit path&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Cost&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Regional prices, normal load, burst, replicas, backups, network, licenses, and staff time&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Then run the smallest discriminating experiments. For Cloud SQL, saturate the likely primary and test failover. For AlloyDB, test the same SQL with read pools and columnar execution. For Spanner, test the proposed primary keys and contended transactions through the intended driver. For sharding, implement one cross-shard workflow and one shard split before declaring the model simple.&lt;/p&gt;
&lt;p&gt;The CARL evidence in this article is the documented behavior of the named systems rather than a fabricated company result:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Context:&lt;/strong&gt; The workload requires a relational API but its compatibility, distribution, and recovery requirements are not yet reconciled.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Eliminate candidates with hard requirements, then run identical workload and failure tests against the remaining designs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Result:&lt;/strong&gt; The architecture decision is supported by observed limits, recovery behavior, and documented service boundaries rather than a universal TPS claim.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Database selection is the placement of coordination and operational ownership. Product features matter only after those boundaries are explicit.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;migration-and-rollback-are-part-of-selection&quot;&gt;Migration and rollback are part of selection&lt;/h3&gt;
&lt;p&gt;For Cloud SQL or AlloyDB, inventory extensions, flags, roles, collations, logical replication requirements, large objects, and SQL whose plan matters. Database Migration Service or logical replication can reduce cutover time, but the design still needs a write-fencing point, replication-lag gate, validation queries, endpoint change, and rollback authority. A rollback after target writes begin is not simply “point DNS back”; it requires a reverse data path or an explicit acceptance that writes during the target window will be reconciled.&lt;/p&gt;
&lt;p&gt;For Spanner, separate syntax conversion from semantic conversion. Map every table and access pattern, then test unsupported types and functions, sequence behavior, transaction isolation assumptions, ORM-generated SQL, and operational tooling. Run backfill and change capture through an immutable mutation identity and monotonic domain version so replay cannot overwrite newer state. Cut over by bounded cohorts and retain the source until rollback has been rehearsed with target-originated writes.&lt;/p&gt;
&lt;p&gt;For manual sharding, migration is a repartitioning project. The migration record needs the shard-key function, ownership metadata, dual-routing behavior, resharding state machine, and handling of requests that arrive while a tenant moves. A shard catalog must be strongly controlled and auditable; stale routing is a correctness defect, not merely a cache miss.&lt;/p&gt;
&lt;h3 id=&quot;benchmark-protocol&quot;&gt;Benchmark protocol&lt;/h3&gt;
&lt;p&gt;Use the same production-shaped trace and encoded data for every candidate. Record engine version, region, instance or processing capacity, storage configuration, HA topology, reader count, client version, pool size, schema, indexes, key distribution, transaction size, consistency settings, test duration, and warm-up state.&lt;/p&gt;
&lt;p&gt;Run at least five phases:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;steady load with normal read and write mix;&lt;/li&gt;
&lt;li&gt;the expected peak for its full duration, not a short throughput burst;&lt;/li&gt;
&lt;li&gt;a skewed-key and contended-transaction phase;&lt;/li&gt;
&lt;li&gt;one documented failure-domain event with client reconnect or retry behavior;&lt;/li&gt;
&lt;li&gt;backlog replay while current traffic continues.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Report accepted business operations separately from attempts and retries. Capture p50, p95, and p99 latency; error and abort classes; CPU and memory; storage latency; lock or transaction contention; replica or change-stream lag; queue age; and time to recover steady state. For AlloyDB, record row versus columnar plans and reader routing. For Spanner, record split distribution, transaction aborts, and latency by transaction shape. For sharding, record router errors and per-shard imbalance.&lt;/p&gt;
&lt;h3 id=&quot;cost-model-inputs&quot;&gt;Cost model inputs&lt;/h3&gt;
&lt;p&gt;Do not publish a product ordering without a dated, regional model. Include compute or processing capacity, storage, backup and retained logs, replicas, cross-region resources, network transfer, observability, support, migration overlap, failure headroom, and the durable ingest or change-capture system. For sharding, include the routing control plane, fleet-wide schema operations, rebalancing, and on-call work. For Spanner, model the selected instance configuration rather than treating “global” as one price or topology.&lt;/p&gt;
&lt;p&gt;Use sensitivity ranges for growth, utilization, data retention, and regional traffic. Vendor calculators are inputs, not evidence that the architecture meets its SLO. Price the configuration that passed the failure test, including spare capacity, rather than a smaller steady-state diagram.&lt;/p&gt;
&lt;h3 id=&quot;security-tenancy-and-disaster-recovery&quot;&gt;Security, tenancy, and disaster recovery&lt;/h3&gt;
&lt;p&gt;The products also create different isolation boundaries. One database per tenant can offer a strong operational boundary but increases fleet work. Shared tables reduce fleet count but require row-level authorization, tenant-aware observability, noisy-neighbor controls, and deletion verification. Sharding by tenant can improve blast-radius isolation while making tenant moves and fleet-wide queries explicit workflows. Spanner distribution does not automatically create tenant isolation; keys, IAM, database boundaries, and application authorization still do that work.&lt;/p&gt;
&lt;p&gt;Validate identity and network paths with the real application. Cloud SQL connectors, AlloyDB connectors, and PGAdapter have different deployment and authentication roles. Audit database roles, IAM bindings, secret rotation, encryption-key ownership, private connectivity, and break-glass access. Confirm that observability and backups do not cross residency boundaries or expose tenant identifiers.&lt;/p&gt;
&lt;p&gt;DR must name the failure. Cloud SQL regional HA covers zonal failure; cross-region replicas or restored instances cover different regional scenarios and can require promotion and endpoint changes. AlloyDB HA and cross-region secondary clusters have their own switchover, failover, and data-loss semantics. Spanner’s regional, dual-region, and multi-region configurations place replicas differently and expose different availability tradeoffs. Select the configuration from the RPO, RTO, write-latency, and residency requirements, then run the supported failure and restore procedures. A product family name is not a DR plan.&lt;/p&gt;
&lt;h2 id=&quot;tradeoff-matrix&quot;&gt;Tradeoff Matrix&lt;/h2&gt;






















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Dimension&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Cloud SQL&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Sharded Cloud SQL&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;AlloyDB&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Spanner PostgreSQL interface&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Writable topology&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;One primary&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;One primary per shard&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;One primary per cluster&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Distributed by key range&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;PostgreSQL compatibility&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High, service-restricted&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High per shard&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High, service-specific&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;PostgreSQL dialect subset&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Native cross-partition transaction&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Single database only&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;No&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Single cluster only&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Analytical acceleration&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;PostgreSQL plus edition features&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Per-shard PostgreSQL&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Optional columnar engine&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Distributed query engine, different SQL surface&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Distribution ownership&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Google manages instance HA&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Team owns routing and rebalancing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Google manages storage and HA&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Google manages distributed storage and consensus&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Migration shape&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Managed-service migration&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Application repartitioning&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Compatibility and performance validation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Schema and transaction redesign&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;failure-modes&quot;&gt;Failure Modes&lt;/h2&gt;
&lt;p&gt;The architecture review should pre-register the failure that would falsify each choice.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cloud SQL falsification:&lt;/strong&gt; after query and pool tuning, the primary has insufficient CPU, memory, storage, lock, or connection headroom during the documented peak or failover recovery. Adding asynchronous replicas does not fix a write constraint, and regional HA does not create another readable writer.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;AlloyDB falsification:&lt;/strong&gt; the tested analytical queries are ineligible for columnar execution, the working set cannot remain resident, read traffic cannot be separated safely, or the single writable primary remains saturated. Storage disaggregation cannot repair a hot logical row or serialize less application contention.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Spanner falsification:&lt;/strong&gt; the proposed leading keys concentrate traffic, distributed transactions touch more splits than expected, abort retries amplify external side effects, or unsupported PostgreSQL behavior forces an unbounded rewrite. More processing capacity cannot fix a schema that sends new writes to one key range.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sharding falsification:&lt;/strong&gt; a supposedly rare cross-shard invariant becomes common, shard ownership cannot be changed without an outage, or operational automation cannot maintain schema, backup, and recovery consistency across the fleet. The first cross-shard financial correction implemented as two independent writes is evidence that the boundary is wrong.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure boundary&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detection evidence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Reversible response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Cloud SQL connection loss&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pool errors, reconnect rate, transaction outcomes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bound retries, recycle connections, and test manual failover before resizing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;AlloyDB primary pressure&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary CPU, query plans, reader utilization&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Route eligible reads, remove expensive SQL, or reject the choice if writes dominate&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Spanner hot range or contention&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Key Visualizer, transaction and lock statistics, abort rate&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Redesign keys or transaction scope and replay an identical workload&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Shard routing divergence&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Catalog version mismatch, wrong-owner responses, parity checks&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Freeze moves, restore one authoritative catalog version, and reconcile mutations&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Regional recovery misses objective&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Promotion or restore timeline and last durable transaction&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Revisit topology, traffic fencing, and the stated RPO or RTO&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Wrong choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Production symptom&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Corrective path&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Cloud SQL past one-writer headroom&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;CPU, locks, or write latency saturate&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Tune first; then shard or evaluate Spanner from transaction requirements.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;AlloyDB selected from benchmark marketing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cost rises while queries remain primary-bound or row-based&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Verify plans, read routing, and column-store eligibility.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Spanner selected for ordinary CRUD&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Migration complexity without distributed benefit&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Return to managed PostgreSQL unless distributed transactions are real.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Sharding selected before transaction analysis&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cross-shard correctness moves into application code&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Redesign boundaries or use a database with native distributed transactions.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Any service selected from list price&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Network, HA, backup, and engineering costs dominate&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Model a tested architecture in the target region and update prices at decision time.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Product selection is based on compatibility labels and invented throughput thresholds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Eliminate options using hard requirements, then benchmark the remaining architectures with the same workload contract.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Official documentation describes different PostgreSQL surfaces, writable topologies, replication models, and transaction systems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Require an architecture decision record containing the workload contract, eliminated options, benchmark artifacts, recovery tests, feature stages, and exit plan.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/introduction&quot;&gt;Cloud SQL overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/high-availability&quot;&gt;Cloud SQL high availability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/overview&quot;&gt;AlloyDB overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/columnar-engine/about&quot;&gt;AlloyDB columnar engine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/reference/extensions&quot;&gt;AlloyDB supported extensions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/spanner/docs/postgresql-interface&quot;&gt;Spanner PostgreSQL interface&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/spanner/docs/transactions&quot;&gt;Spanner transactions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/spanner/docs/instance-configurations&quot;&gt;Spanner instance configurations&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>cloud</category><category>architecture</category><category>databases</category><category>failures</category><category>checklist</category></item><item><title>Finding Slow MongoDB Queries: Profiler, Slow Logs, explain(), mongostat, and mongotop</title><link>https://rajivonai.com/blog/2026-05-21-finding-slow-queries/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-21-finding-slow-queries/</guid><description>A production framework for identifying inefficient MongoDB query shapes on AWS EC2 without triggering observability-induced cache exhaustion or disk saturation.</description><pubDate>Fri, 14 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Observability overhead must never exceed the capacity of the system being observed; enabling aggressive database profiling on a high-throughput cluster turns a diagnostic step into a self-inflicted write-ticket outage.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Enabling &lt;code&gt;db.setProfilingLevel(2)&lt;/code&gt; or low-threshold unsampled profiling writes BSON documents into &lt;code&gt;system.profile&lt;/code&gt; on the hot WiredTiger write path, draining concurrent write tickets and pushing dirty cache past the 20% eviction trigger.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Keep the profiler at level &lt;code&gt;0&lt;/code&gt; in steady state, emit slow operations exceeding &lt;code&gt;slowms&lt;/code&gt; as structured JSON to &lt;code&gt;mongod.log&lt;/code&gt;, ship and group them by &lt;code&gt;queryHash&lt;/code&gt; externally, triage live pressure with &lt;code&gt;mongostat&lt;/code&gt; and &lt;code&gt;mongotop&lt;/code&gt;, and run &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; only on hidden secondaries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Log shippers running without &lt;code&gt;systemd&lt;/code&gt; CPU and memory limits compete with &lt;code&gt;mongod&lt;/code&gt; for EBS read bandwidth, and blocking sorts above 100 MB abort unless covered by an ESR-ordered index or &lt;code&gt;allowDiskUse: true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-21-finding-slow-queries.svg&quot; alt=&quot;Decouple slow-query telemetry from WiredTiger writes&quot;&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Production Scope: MongoDB 5.0 / 6.0 / 7.0 / 8.0 on AWS EC2 + EBS &lt;code&gt;gp3&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Operating high-throughput MongoDB replica sets on AWS EC2 memory-optimized instances (&lt;code&gt;r6g.2xlarge&lt;/code&gt;, &lt;code&gt;r7g.2xlarge&lt;/code&gt;) backed by EBS &lt;code&gt;gp3&lt;/code&gt; volumes requires continuous query plan verification to hold sub-10ms p99 latencies. Across microservice architectures, new product filters, dynamic API sort parameters, and ad-hoc analytical predicates continuously introduce unseen query shapes into production.&lt;/p&gt;
&lt;p&gt;Most indexed point lookups complete in single-digit milliseconds, but a single unindexed collection scan (&lt;code&gt;COLLSCAN&lt;/code&gt;) or blocking in-memory sort (&lt;code&gt;SORT&lt;/code&gt;) can consume provisioned EBS &lt;code&gt;gp3&lt;/code&gt; IOPS and evict the active working set from the WiredTiger cache. During a latency regression, on-call engineers need a way to isolate the offending query shape immediately—without introducing diagnostic overhead that pushes an already stressed storage engine over its saturation cliff.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;h3 id=&quot;the-observability-invalidation-hazard&quot;&gt;The Observability Invalidation Hazard&lt;/h3&gt;
&lt;p&gt;When application latency spikes, the standard reflex is to turn on the MongoDB database profiler. Unlike filesystem logging, the profiler writes execution metadata directly into the WiredTiger catalog of the database being observed, creating four compounding constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The Profiler Level 2 Write Amplification:&lt;/strong&gt; Executing &lt;code&gt;db.setProfilingLevel(2)&lt;/code&gt; instructs &lt;code&gt;mongod&lt;/code&gt; to insert a full BSON execution document into the &lt;code&gt;system.profile&lt;/code&gt; capped collection for every read, write, and command. Consider the arithmetic on an &lt;code&gt;r6g.2xlarge&lt;/code&gt; instance backed by a baseline EBS &lt;code&gt;gp3&lt;/code&gt; volume (3,000 IOPS, 125 MB/s throughput): at 10,000 reads per second, a read-only workload normally consumes zero WiredTiger write tickets. Enabling Level 2 converts those 10,000 reads into 10,000 &lt;code&gt;system.profile&lt;/code&gt; inserts per second. Even at ~1 KB per BSON profile record, that adds ~10 MB/s of raw document payload plus WiredTiger B-tree page dirtying, journal flushes, and capped-collection truncation overhead—quickly saturating the 3,000 IOPS baseline unless IOPS are explicitly provisioned higher.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;WiredTiger Concurrent Ticket Exhaustion:&lt;/strong&gt; In MongoDB 6.0 and earlier, WiredTiger enforces a static ceiling of &lt;code&gt;128&lt;/code&gt; concurrent write tickets (&lt;code&gt;wiredTigerConcurrentWriteTransactions&lt;/code&gt;). Every &lt;code&gt;system.profile&lt;/code&gt; insert must acquire a write ticket, modify in-memory B-tree pages, and mark cache buffers dirty. When diagnostic inserts consume the 128-ticket pool, application writes queue behind profiler writes. (MongoDB 7.0 replaced the static 128 ceiling with dynamic concurrency control, but heavy &lt;code&gt;system.profile&lt;/code&gt; contention still reduces available admission capacity under overload.)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The 20% Dirty Cache Trigger:&lt;/strong&gt; WiredTiger background eviction threads work to keep dirty cache below &lt;code&gt;eviction_dirty_trigger&lt;/code&gt; (default 20%) and total cache usage below &lt;code&gt;eviction_target&lt;/code&gt; (80%) and &lt;code&gt;eviction_trigger&lt;/code&gt; (95%). When &lt;code&gt;system.profile&lt;/code&gt; writes saturate provisioned EBS &lt;code&gt;gp3&lt;/code&gt; IOPS, background eviction falls behind, crossing the 20% dirty threshold and conscripting application client threads into synchronous page eviction.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Blocking Sort Memory Ceiling (100 MB):&lt;/strong&gt; Queries without an index that satisfies both filter and sort predicates must run a blocking &lt;code&gt;SORT&lt;/code&gt; stage in RAM. Since MongoDB 4.4, &lt;code&gt;internalQueryMaxBlockingSortMemoryUsageBytes&lt;/code&gt; caps this at 100 MB (&lt;code&gt;104,857,600&lt;/code&gt; bytes; older 4.2 and earlier releases capped it at 32 MB). Crossing 100 MB aborts &lt;code&gt;find&lt;/code&gt; queries outright, while &lt;code&gt;allowDiskUse: true&lt;/code&gt; on aggregations spills temporary sort files to the same saturated EBS volume.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-21-finding-slow-queries-2-constraints.svg&quot; alt=&quot;Profiler Level 2 turns diagnostics into a write-ticket outage&quot;&gt;&lt;/p&gt;
&lt;p&gt;The core engineering question is: &lt;strong&gt;How can platform engineers capture, group, and explain pathological query shapes in high-throughput production clusters without consuming WiredTiger write tickets or saturating EBS IOPS?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Write Ticket Impact&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Dirty Cache Impact&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Visibility Scope&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Profiler Level 2&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Consumes 1 write ticket per operation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High; forces continuous B-tree dirtying&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;100% of reads, writes, and commands&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Indistinguishable from a self-inflicted write flood at high QPS&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Level 1 Unsampled (&lt;code&gt;slowms&lt;/code&gt; too low)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unbounded during latency storms&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Spikes when baseline p95 exceeds &lt;code&gt;slowms&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;All operations exceeding &lt;code&gt;slowms&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Latency degradation turns slow-query logging into Level 2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Level 1 + &lt;code&gt;sampleRate&lt;/code&gt; (e.g., &lt;code&gt;0.05&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bounded to 5% of slow operations&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Low when &lt;code&gt;slowms &gt;= 250&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sampled slow queries and pipelines&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Fallback:&lt;/strong&gt; Acceptable only for short-lived debugging when logs lack detail&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;JSON Slow Logs (&lt;code&gt;mongod.log&lt;/code&gt;) + CLI&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero WiredTiger tickets consumed&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero WiredTiger dirty pages&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;All operations exceeding &lt;code&gt;slowms&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen:&lt;/strong&gt; Decouples capture from the storage engine via OS-buffered I/O&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Profiler Level 2 and unsampled low-threshold Level 1 were rejected because both couple diagnostic volume directly to storage-engine write capacity: the exact moment a cluster slows down is when &lt;code&gt;slowms&lt;/code&gt; triggers most frequently, amplifying write pressure during an outage. Structured JSON slow logs (&lt;code&gt;mongod.log&lt;/code&gt;) paired with &lt;code&gt;mongostat&lt;/code&gt; and &lt;code&gt;mongotop&lt;/code&gt; keep the storage engine write path untouched.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-21-finding-slow-queries-3-options.svg&quot; alt=&quot;Only filesystem slow logs avoid WiredTiger write contention&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-safe-query-observability-architecture-decoupled-telemetry&quot;&gt;The Safe Query Observability Architecture: Decoupled Telemetry&lt;/h2&gt;
&lt;p&gt;A resilient observability architecture separates hot-path query execution from log shipping and offline execution plan analysis across three distinct planes.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-21-finding-slow-queries-4-architecture.svg&quot; alt=&quot;Layered telemetry separates hot query path from analysis&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Client[&quot;Application Queries — High QPS&quot;] --&gt; Router[&quot;mongod Query Executor&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Router --&gt; Planner[&quot;Query Optimizer — Plan Cache via queryHash&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Planner --&gt; IndexDecision{&quot;Usable Index Identified?&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    IndexDecision --&gt;|Yes — IXSCAN| BTree[&quot;Traverse B-Tree Index Pages&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    IndexDecision --&gt;|No — COLLSCAN| FullScan[&quot;Examine Every Document in Collection&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    BTree --&gt; WTCache[&quot;WiredTiger Cache — Read Tickets&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    FullScan --&gt; CacheChurn[&quot;Working Set Cache Eviction Spike&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Router --&gt;|Exceeds slowms| SlowLog[&quot;Structured JSON Log — mongod.log&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SlowLog --&gt; Vector[&quot;Log Shipper — Vector or Fluent Bit&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Vector --&gt; Aggregator[&quot;External Analytics — Group by queryHash&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Aggregator --&gt; HiddenSecondary[&quot;Hidden Secondary — explain executionStats&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-structured-json-slow-query-logging-mongodlog&quot;&gt;1. Structured JSON Slow Query Logging (&lt;code&gt;mongod.log&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Since MongoDB 4.4, operations exceeding &lt;code&gt;slowms&lt;/code&gt; (default &lt;code&gt;100&lt;/code&gt; ms) are written to &lt;code&gt;mongod.log&lt;/code&gt; as structured JSON using OS-buffered filesystem I/O. This path bypasses WiredTiger’s transaction ticketing, B-tree page locks, dirty-cache tracking, and checkpoint cycles. A log shipper such as Vector or Fluent Bit tails &lt;code&gt;mongod.log&lt;/code&gt; and forwards parsed events externally so engineers can group slow queries by &lt;code&gt;queryHash&lt;/code&gt; and &lt;code&gt;planCacheKey&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;2-bounded-fallback-level-1-profiling-with-samplerate&quot;&gt;2. Bounded Fallback: Level 1 Profiling with &lt;code&gt;sampleRate&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;If an incident requires capturing fields not surfaced in standard slow logs, enable Level 1 profiling temporarily with an elevated &lt;code&gt;slowms&lt;/code&gt; and an explicit &lt;code&gt;sampleRate&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Safely capture 5% of operations exceeding 250ms&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;setProfilingLevel&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  slowms: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;250&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  sampleRate: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0.05&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Always revert to &lt;code&gt;db.setProfilingLevel(0)&lt;/code&gt; immediately after capturing the sample.&lt;/p&gt;
&lt;h3 id=&quot;3-non-invasive-live-triage-mongostat-and-mongotop&quot;&gt;3. Non-Invasive Live Triage: &lt;code&gt;mongostat&lt;/code&gt; and &lt;code&gt;mongotop&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;During an active incident, external administrative polling isolates storage bottlenecks without touching &lt;code&gt;system.profile&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;mongostat --discover 1&lt;/code&gt;&lt;/strong&gt;: Reports per-second operation counters alongside &lt;code&gt;dirty%&lt;/code&gt;, &lt;code&gt;used%&lt;/code&gt;, and &lt;code&gt;qrw&lt;/code&gt; (queued read/write operations). Climbing &lt;code&gt;qrw&lt;/code&gt; paired with &lt;code&gt;dirty%&lt;/code&gt; approaching &lt;code&gt;20%&lt;/code&gt; confirms WiredTiger eviction backpressure; &lt;code&gt;used%&lt;/code&gt; crossing &lt;code&gt;95%&lt;/code&gt; indicates total cache exhaustion where client threads stall on eviction.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;mongotop 5&lt;/code&gt;&lt;/strong&gt;: Reports cumulative read and write time per namespace over 5-second intervals, isolating which collection is absorbing disk I/O. Keep intervals at 5 seconds or higher to avoid administrative lock jitter.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;$currentOp&lt;/code&gt; for In-Flight Stalls:&lt;/strong&gt; Because &lt;code&gt;mongod.log&lt;/code&gt; emits slow-query records only after an operation completes, queries stuck behind a lock or running a multi-minute &lt;code&gt;COLLSCAN&lt;/code&gt; will not appear in logs yet. Querying &lt;code&gt;db.getSiblingDB(&quot;admin&quot;).aggregate([{ $currentOp: { allUsers: true } }, { $match: { active: true, secs_running: { $gt: 1 } } }])&lt;/code&gt; surfaces active offenders and lock-wait states (&lt;code&gt;waitingForLock: true&lt;/code&gt;) in real time.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;deep-internals-multi-planner-trial-and-blocking-sort-gate&quot;&gt;Deep Internals: Multi-Planner Trial and Blocking Sort Gate&lt;/h3&gt;
&lt;p&gt;Understanding why a query appears in &lt;code&gt;mongod.log&lt;/code&gt; requires tracing how the MongoDB query optimizer compiles and caches execution plans.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-21-finding-slow-queries-5-flow.svg&quot; alt=&quot;Multi-planner trial execution and the 100 MB sort boundary&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Plan Cache Lookup:&lt;/strong&gt; On query arrival, &lt;code&gt;mongod&lt;/code&gt; hashes the query shape (filter, sort, projection, collation) to &lt;code&gt;planCacheKey&lt;/code&gt;. Cache hits reuse the winning plan directly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multi-Planner Trial Race:&lt;/strong&gt; On a cache miss, the optimizer identifies candidate indexes and races them in parallel up to &lt;code&gt;internalQueryPlanEvaluationWorks&lt;/code&gt; (default &lt;code&gt;10000&lt;/code&gt; work units). A work unit corresponds to an index key scan, document fetch, or filter evaluation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Winner Selection and Eviction:&lt;/strong&gt; The candidate that returns the required batch with the fewest work units wins and enters the plan cache. Cached plans remain active until an index is built or dropped, the node restarts, or the plan’s observed work ratio degrades significantly during execution.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Blocking Sort Enforcement:&lt;/strong&gt; If no index covers the &lt;code&gt;sort&lt;/code&gt; specification, &lt;code&gt;mongod&lt;/code&gt; inserts a blocking &lt;code&gt;SORT&lt;/code&gt; stage. Crossing the 100 MB &lt;code&gt;internalQueryMaxBlockingSortMemoryUsageBytes&lt;/code&gt; limit aborts &lt;code&gt;find&lt;/code&gt; operations:
&lt;code&gt;Executor error during find command :: caused by :: Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting.&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;high-resolution-telemetry-decoding-mongodlog-and-explain&quot;&gt;High-Resolution Telemetry: Decoding &lt;code&gt;mongod.log&lt;/code&gt; and &lt;code&gt;explain()&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;A MongoDB 5.0+ structured JSON slow query entry exposes exact examination counts and physical storage reads:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;t&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;&quot;$date&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;2026-05-21T10:14:22.105+00:00&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;s&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;I&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;c&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;COMMAND&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;51803&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;ctx&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;conn4281&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;msg&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Slow query&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;attr&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;type&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;command&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;ns&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;production_commerce.orders&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;command&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;      &quot;find&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;orders&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;      &quot;filter&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;&quot;merchant_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;M-8841&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;&quot;status&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;PENDING&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;      &quot;sort&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;&quot;created_at&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;-1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;      &quot;limit&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;50&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;planSummary&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;IXSCAN { merchant_id: 1 }&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;keysExamined&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;120500&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;docsExamined&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;120500&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;hasSortStage&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;nreturned&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;storage&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;&quot;bytesRead&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;42189000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;durationMillis&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1420&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Evaluate three forensic signals from this record:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Examination-to-Return Ratio ($E/R$):&lt;/strong&gt; &lt;code&gt;docsExamined / nreturned&lt;/code&gt; (&lt;code&gt;120,500 / 50 = 2,410&lt;/code&gt;). Ratios near &lt;code&gt;1.0&lt;/code&gt; indicate tight index selectivity; ratios in the thousands indicate wasted document fetches.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;hasSortStage: true&lt;/code&gt;:&lt;/strong&gt; Confirms the chosen index (&lt;code&gt;{ merchant_id: 1 }&lt;/code&gt;) did not satisfy &lt;code&gt;sort({ created_at: -1 })&lt;/code&gt;, forcing an in-memory &lt;code&gt;SORT&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;storage.data.bytesRead&lt;/code&gt;:&lt;/strong&gt; Measures physical bytes pulled from EBS into WiredTiger cache during execution.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To validate a fix without impacting the primary, run &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; on a hidden secondary node:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.orders.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;merchant_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;M-8841&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;status&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;PENDING&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;created_at&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;limit&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;explain&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;executionStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;automated-log-extraction-and-plan-validation&quot;&gt;Automated Log Extraction and Plan Validation&lt;/h3&gt;
&lt;p&gt;Use a Vector remap transform to compute &lt;code&gt;er_ratio&lt;/code&gt; on the fly and alert only on high-duration or unselective query shapes:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;toml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sources&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mongo_logs&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;type = &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;file&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;include = [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/var/log/mongodb/mongod.log&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;read_from = &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;end&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;transforms&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;parse_mongo_json&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;type = &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;remap&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;inputs = [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;mongo_logs&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;source = &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;&apos;&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  . = parse_json!(.message)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  if .msg != &quot;Slow query&quot; { abort }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  .duration_ms = .attr.durationMillis&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  .namespace = .attr.ns&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  .docs_examined = .attr.docsExamined&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  .n_returned = .attr.nreturned&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  .er_ratio = if .n_returned &gt; 0 { .docs_examined / .n_returned } else { .docs_examined }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;&apos;&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;transforms&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;filter_pathological&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;type = &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;filter&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;inputs = [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;parse_mongo_json&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;condition = &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;.duration_ms &gt;= 500 || .er_ratio &gt;= 100&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pair the log pipeline with a read-only Python validator targeting a hidden secondary to verify whether a candidate query shape exhibits a blocking &lt;code&gt;SORT&lt;/code&gt; or high &lt;code&gt;totalDocsExamined&lt;/code&gt; ratio:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env python3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; argparse, json, sys&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo.errors &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PyMongoError&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; analyze_query_plan&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(client, namespace, query_filter, sort_spec):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    db_name, coll_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; namespace.split(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    coll &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client[db_name][coll_name]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        cursor &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; coll.find(query_filter)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sort_spec:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            cursor &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cursor.sort(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(sort_spec.items()))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        explanation &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cursor.explain(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;executionStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    except&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PyMongoError &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; e:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sys.exit(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Explain failed: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    stats &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; explanation.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;executionStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, {})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    n_ret &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stats.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;nReturned&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    docs_ex &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stats.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;totalDocsExamined&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    curr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; explanation.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;queryPlanner&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, {}).get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;winningPlan&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, {})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    has_sort &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; False&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; curr:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; curr.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;stage&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;SORT&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            has_sort &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            break&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        curr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; curr.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;inputStage&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ratio &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; docs_ex &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_ret &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_ret &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; docs_ex&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ns=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;namespace&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; ms=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;stats.get(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;executionTimeMillis&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; returned=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;n_ret&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; examined=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;docs_ex&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; E/R=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ratio&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.2f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; sort=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;has_sort&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Deploying safe slow-query observability across an existing replica set follows four gated phases so each step can be verified and rolled back independently:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Verify Profiler Off &amp;#x26; Baseline &lt;code&gt;slowms&lt;/code&gt;:&lt;/strong&gt; Run &lt;code&gt;db.getProfilingStatus()&lt;/code&gt; across all members to confirm &lt;code&gt;was: 0&lt;/code&gt;. Set &lt;code&gt;slowms&lt;/code&gt; above baseline p95 latency (minimum &lt;code&gt;100&lt;/code&gt; ms) via &lt;code&gt;db.setProfilingLevel(0, { slowms: 100 })&lt;/code&gt; so slow queries emit only to &lt;code&gt;mongod.log&lt;/code&gt;. Rollback: restore prior &lt;code&gt;slowms&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Deploy Constrained Log Shipper:&lt;/strong&gt; Roll out Vector or Fluent Bit on one secondary first, pinned via &lt;code&gt;systemd&lt;/code&gt; &lt;code&gt;CPUAffinity&lt;/code&gt; and &lt;code&gt;MemoryMax&lt;/code&gt;, shipping &lt;code&gt;id: 51803&lt;/code&gt; (&lt;code&gt;Slow query&lt;/code&gt;) records. Verify zero rise in &lt;code&gt;mongostat&lt;/code&gt; &lt;code&gt;qrw&lt;/code&gt; or host &lt;code&gt;iowait&lt;/code&gt;. Rollback: &lt;code&gt;systemctl stop vector&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Aggregate by &lt;code&gt;queryHash&lt;/code&gt; and Validate Off-Primary:&lt;/strong&gt; Rank top offenders by &lt;code&gt;durationMillis * count&lt;/code&gt; and &lt;code&gt;er_ratio&lt;/code&gt;. Reproduce candidate shapes with &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; on a hidden secondary node.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Remediate via ESR Index or &lt;code&gt;setQuerySettings&lt;/code&gt;:&lt;/strong&gt; Deploy compound indexes ordered by Equality, Sort, Range (ESR), or apply MongoDB 8.0 &lt;code&gt;setQuerySettings&lt;/code&gt; (&lt;code&gt;indexHints&lt;/code&gt;) as an immediate cluster-wide guard before application code changes ship.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Documented MongoDB query planner and WiredTiger storage engine behaviors explain both the common failure modes and their fixes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Equality, Sort, Range (ESR) Index Ordering:&lt;/strong&gt; Per MongoDB’s documented indexing behavior, placing equality predicates before sort fields and range predicates last lets &lt;code&gt;IXSCAN&lt;/code&gt; satisfy both filtering and ordering directly from the B-tree. For a query filtering on &lt;code&gt;merchant_id&lt;/code&gt; and &lt;code&gt;status&lt;/code&gt; while sorting by &lt;code&gt;created_at&lt;/code&gt; with &lt;code&gt;limit(50)&lt;/code&gt;, an index on &lt;code&gt;{ merchant_id: 1, created_at: -1 }&lt;/code&gt; forces &lt;code&gt;mongod&lt;/code&gt; to fetch every document matching &lt;code&gt;merchant_id&lt;/code&gt; to evaluate &lt;code&gt;status&lt;/code&gt;, inflating &lt;code&gt;totalDocsExamined&lt;/code&gt;. Reordering to &lt;code&gt;{ merchant_id: 1, status: 1, created_at: -1 }&lt;/code&gt; eliminates document fetches for non-matching statuses and removes the blocking &lt;code&gt;SORT&lt;/code&gt; stage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Query Shape Grouping (&lt;code&gt;queryHash&lt;/code&gt; vs. &lt;code&gt;planCacheKey&lt;/code&gt;):&lt;/strong&gt; MongoDB computes &lt;code&gt;queryHash&lt;/code&gt; from the structural shape of a query (filter fields, sort, projection) while &lt;code&gt;planCacheKey&lt;/code&gt; incorporates available indexes and collation. Grouping &lt;code&gt;mongod.log&lt;/code&gt; slow queries by &lt;code&gt;queryHash&lt;/code&gt; aggregates identical query patterns across different literal filter values, revealing whether a latency spike stems from one unselective query shape or parameter skew poisoning a cached plan.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Profiler Level 2 Write-Path Coupling:&lt;/strong&gt; Per MongoDB’s database profiler documentation, &lt;code&gt;system.profile&lt;/code&gt; is a capped collection backed by WiredTiger. Every profiled operation executes a storage-engine insert that acquires a write ticket and dirties cache pages. Under high concurrency, enabling Level 2 causes diagnostic writes to compete directly with application writes for cache eviction and EBS IOPS, whereas &lt;code&gt;mongod.log&lt;/code&gt; writes use OS-buffered I/O outside WiredTiger.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MongoDB 7.0+ and 8.0 Operational Controls:&lt;/strong&gt; MongoDB 7.0 introduced dynamic WiredTiger concurrency ticket scaling to reduce static 128-ticket starvation during overload, and MongoDB 8.0 replaced node-local &lt;code&gt;planCacheSetFilter&lt;/code&gt; with persistent, cluster-wide &lt;code&gt;setQuerySettings&lt;/code&gt;:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;adminCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  setQuerySettings: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    find: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;orders&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    filter: { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;merchant_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;M-8841&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;status&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;PENDING&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    sort: { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;created_at&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    $db: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;production_commerce&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  settings: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    indexHints: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      ns: { db: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;production_commerce&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, coll: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;orders&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      allowedIndexes: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;merchant_id_1_status_1_created_at_-1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-21-finding-slow-queries-6-failures.svg&quot; alt=&quot;Four diagnostic failure paths that degrade healthy clusters&quot;&gt;&lt;/p&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Diagnostic Approach&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Physical Failure Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Architectural Consequence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Safe Mitigation Protocol&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Profiler Level 2 in Production&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Inserts 100% of ops into &lt;code&gt;system.profile&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;WiredTiger write amplification, ticket starvation, and EBS IOPS exhaustion.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep profiler at level &lt;code&gt;0&lt;/code&gt;; ship &lt;code&gt;mongod.log&lt;/code&gt; JSON slow queries instead.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unsampled Level 1 with Low &lt;code&gt;slowms&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Setting &lt;code&gt;slowms: 5&lt;/code&gt; when p95 exceeds 8 ms.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Latency spikes trigger continuous &lt;code&gt;system.profile&lt;/code&gt; writes like Level 2.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set &lt;code&gt;slowms &gt;= 100&lt;/code&gt; and pair with &lt;code&gt;sampleRate &amp;#x3C;= 0.05&lt;/code&gt; only if needed.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;explain(&quot;allPlansExecution&quot;)&lt;/code&gt; on Primary&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Executes all candidate plans concurrently on live data.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Multiplies cache page reads and triggers working-set eviction on primary.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; only on hidden secondaries or staging.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unindexed &lt;code&gt;system.profile&lt;/code&gt; Queries&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Filtering &lt;code&gt;system.profile&lt;/code&gt; without natural order.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Scans entire capped collection, holding read tickets and stalling I/O.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Query with &lt;code&gt;sort({ $natural: -1 })&lt;/code&gt; or eliminate &lt;code&gt;system.profile&lt;/code&gt; usage.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Blocking Sort Over 100 MB&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unindexed sort exceeds &lt;code&gt;internalQueryMaxBlockingSortMemoryUsageBytes&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Query aborts at 100 MB, or &lt;code&gt;allowDiskUse: true&lt;/code&gt; spills to EBS disk.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Order compound indexes using ESR so &lt;code&gt;IXSCAN&lt;/code&gt; provides sort order.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Plan Cache Poisoning via Skew&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Low-selectivity parameter wins the 10,000-work trial.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Suboptimal plan stays cached for all executions of that &lt;code&gt;planCacheKey&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Clear via &lt;code&gt;db.coll.getPlanCache().clearPlansByQuery()&lt;/code&gt; or pin via &lt;code&gt;setQuerySettings&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unconstrained Log Shipper&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Vector or Fluent Bit runs without cgroup CPU/RAM caps.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Agent competes with &lt;code&gt;mongod&lt;/code&gt; for CPU cycles and EBS read bandwidth.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce &lt;code&gt;systemd&lt;/code&gt; &lt;code&gt;CPUAffinity&lt;/code&gt; and &lt;code&gt;MemoryMax&lt;/code&gt; on all log shippers.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Sub-Second &lt;code&gt;mongotop&lt;/code&gt; Loops&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Polling &lt;code&gt;mongotop&lt;/code&gt; continuously at &lt;code&gt;&amp;#x3C; 1s&lt;/code&gt; intervals.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Generates frequent metadata lock requests and lock queue jitter.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep &lt;code&gt;mongotop&lt;/code&gt; polling intervals at &lt;code&gt;5&lt;/code&gt; seconds or longer.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Never use &lt;code&gt;db.setProfilingLevel(2)&lt;/code&gt; in production:&lt;/strong&gt; &lt;code&gt;system.profile&lt;/code&gt; writes share WiredTiger write tickets, dirty-cache budgets, and EBS IOPS with application traffic.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Default to &lt;code&gt;mongod.log&lt;/code&gt; structured JSON shipping:&lt;/strong&gt; Setting &lt;code&gt;level: 0&lt;/code&gt; with &lt;code&gt;slowms: 100&lt;/code&gt; emits slow queries via OS-buffered I/O at zero WiredTiger write-ticket cost.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Measure selectivity with $E/R$ (&lt;code&gt;docsExamined / nreturned&lt;/code&gt;):&lt;/strong&gt; A high ratio alongside &lt;code&gt;hasSortStage: true&lt;/code&gt; signals a missing ESR-ordered compound index rather than a hardware bottleneck.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Know the 100 MB blocking sort ceiling:&lt;/strong&gt; MongoDB 4.4+ aborts unindexed blocking sorts above 100 MB (&lt;code&gt;internalQueryMaxBlockingSortMemoryUsageBytes&lt;/code&gt;) unless &lt;code&gt;allowDiskUse: true&lt;/code&gt; spills to disk.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Isolate &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; to hidden secondaries:&lt;/strong&gt; Never run &lt;code&gt;allPlansExecution&lt;/code&gt; against a primary serving live traffic, and pin log shippers with &lt;code&gt;systemd&lt;/code&gt; resource limits.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Production clusters lack a low-overhead pipeline to capture and rank slow query shapes without risking WiredTiger cache exhaustion.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Keep the database profiler at level &lt;code&gt;0&lt;/code&gt;, ship structured JSON slow logs from &lt;code&gt;mongod.log&lt;/code&gt; externally, and aggregate by &lt;code&gt;queryHash&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run &lt;code&gt;db.getProfilingStatus()&lt;/code&gt; to verify &lt;code&gt;was: 0&lt;/code&gt; and check &lt;code&gt;db.serverStatus().metrics.queryExecutor&lt;/code&gt; for &lt;code&gt;scanned&lt;/code&gt; vs &lt;code&gt;scannedObjects&lt;/code&gt; ratios.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Set &lt;code&gt;slowms: 100&lt;/code&gt;, deploy a &lt;code&gt;systemd&lt;/code&gt;-constrained Vector or Fluent Bit shipper on a secondary first, and audit the top three high-$E/R$ query shapes via &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; on a hidden secondary this week.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>MongoDB Performance Troubleshooting Playbook: Triage, WiredTiger Tickets, and Cascading Connection Pool Failures</title><link>https://rajivonai.com/blog/2026-05-20-performance-playbook/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-20-performance-playbook/</guid><description>A methodical diagnostic framework for triaging generalized MongoDB latency spikes on AWS EC2, identifying ticket exhaustion, and resolving cascading connection pool collapses.</description><pubDate>Thu, 13 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;When a MongoDB cluster degrades under load, host CPU utilization is a lagging illusion—WiredTiger admission ticket exhaustion and &lt;code&gt;globalLock.currentQueue&lt;/code&gt; depths are the true leading indicators of a cascading connection-pool collapse.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Unindexed &lt;code&gt;COLLSCAN&lt;/code&gt; queries hold WiredTiger read tickets (default &lt;code&gt;128&lt;/code&gt; in 4.4–6.0) during physical EBS reads, queuing fast &lt;code&gt;_id&lt;/code&gt; lookups and triggering upstream driver &lt;code&gt;maxPoolSize&lt;/code&gt; socket storms that exhaust &lt;code&gt;somaxconn&lt;/code&gt; and RAM.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Triage &lt;code&gt;wiredTiger.concurrentTransactions&lt;/code&gt; and filtered &lt;code&gt;db.currentOp()&lt;/code&gt; (&lt;code&gt;secs_running &gt;= 2&lt;/code&gt;, &lt;code&gt;COLLSCAN&lt;/code&gt;), shed rogue queries with surgical &lt;code&gt;killOp&lt;/code&gt; (or MongoDB 8.0 &lt;code&gt;setQuerySettings&lt;/code&gt; &lt;code&gt;reject: true&lt;/code&gt;) while excluding &lt;code&gt;local.*&lt;/code&gt; and prepared transactions, and bound driver pools with &lt;code&gt;waitQueueTimeoutMS&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Never restart &lt;code&gt;mongod&lt;/code&gt; or trigger an uncoordinated &lt;code&gt;rs.stepDown()&lt;/code&gt; mid-cascade—restarting forces dirty-cache recovery, and failing over simply moves the same pathological query storm onto the newly elected primary.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-20-performance-playbook.svg&quot; alt=&quot;Comparison of a concurrency collapse cascade against surgical load shedding and bounded driver pools&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Production MongoDB replica sets on AWS EC2 (&lt;code&gt;r6i&lt;/code&gt; or &lt;code&gt;r7g&lt;/code&gt; instances with &lt;code&gt;gp3&lt;/code&gt; EBS volumes) routinely serve hundreds of containerized microservice pods. Under normal operation, indexed queries complete from WiredTiger’s cache in sub-millisecond time, ticket queues stay near zero, and connection pools remain steady.&lt;/p&gt;
&lt;p&gt;When a release introduces an unindexed query filter or an ad-hoc analytical scan hits a cold collection, teams receive generalized latency alerts: microservices log socket timeouts, open TCP connections surge, and host CPU climbs toward 90%–100%. Under pressure, operators often bounce &lt;code&gt;mongod&lt;/code&gt; or call &lt;code&gt;rs.stepDown()&lt;/code&gt;. Both actions worsen the outage: restarting forces checkpoint and journal recovery on a cold cache, while failing over shifts the exact same unindexed traffic onto the newly elected primary. Worse still, an uncoordinated &lt;code&gt;rs.stepDown()&lt;/code&gt; triggers a brief election blackout where all in-flight writes fail or retry simultaneously, compounding the driver reconnect storm.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A concurrency collapse follows a deterministic Little’s Law cascade across the storage gate and driver pools:&lt;/p&gt;
&lt;p&gt;$$L = \lambda \times W$$&lt;/p&gt;
&lt;p&gt;Here $L$ is concurrent WiredTiger read tickets in use (&lt;code&gt;wiredTiger.concurrentTransactions.read.out&lt;/code&gt;), $\lambda$ is the query arrival rate, and $W$ is the average ticket hold time.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-20-performance-playbook-2-constraints.svg&quot; alt=&quot;Little&amp;#x27;s Law ticket exhaustion math, 200x200 driver socket amplification, and CPU context-switch thrashing&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Ticket Starvation Math:&lt;/strong&gt; In MongoDB 4.4 through 6.0, WiredTiger defaults to &lt;code&gt;128&lt;/code&gt; read tickets and &lt;code&gt;128&lt;/code&gt; write tickets (&lt;code&gt;wiredTigerConcurrentReadTransactions&lt;/code&gt; / &lt;code&gt;WriteTransactions&lt;/code&gt;). Under cache-resident indexed traffic ($\lambda = 10,000$ queries/s, $W = 0.5$ ms), $L = 10,000 \times 0.0005 = 5$ tickets—leaving &lt;code&gt;123&lt;/code&gt; free. By contrast, if an unindexed &lt;code&gt;COLLSCAN&lt;/code&gt; misses cache and reads cold pages from EBS such that $W = 10$ seconds, just $\lambda = 15$ queries/s demands $L = 15 \times 10 = 150$ tickets, saturating the &lt;code&gt;128&lt;/code&gt;-ticket pool within a single second and forcing fast &lt;code&gt;_id&lt;/code&gt; lookups into &lt;code&gt;globalLock.currentQueue.readers&lt;/code&gt;. Notice that MongoDB’s logical lock manager (&lt;code&gt;IS&lt;/code&gt; and &lt;code&gt;IX&lt;/code&gt; database/collection intent locks) is not the bottleneck here: intent locks allow concurrent access, so every query sails past the logical lock manager and piles up directly at WiredTiger’s semaphore gate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Driver Connection Pool Amplification:&lt;/strong&gt; Suppose 200 microservice pods each configure &lt;code&gt;maxPoolSize: 200&lt;/code&gt; without a tight &lt;code&gt;waitQueueTimeoutMS&lt;/code&gt;. As query latency climbs, connection pools open new TCP sockets toward $200 \times 200 = 40,000$ connections. Each established TCP socket consumes kernel &lt;code&gt;rmem&lt;/code&gt;/&lt;code&gt;wmem&lt;/code&gt; buffers plus a &lt;code&gt;mongod&lt;/code&gt; worker thread stack (on Linux, default &lt;code&gt;pthread&lt;/code&gt; stack or &lt;code&gt;mongod&lt;/code&gt; connection overhead adds ~1 MB per idle/active connection), consuming tens of gigabytes of non-cache RAM, starving WiredTiger and the OS page cache, and flooding the Linux TCP listen queue (&lt;code&gt;net.core.somaxconn&lt;/code&gt;, which defaulted to &lt;code&gt;128&lt;/code&gt; on older Linux kernels before &lt;code&gt;4096&lt;/code&gt; became standard on Linux 5.4+).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CPU Context-Switch Illusion:&lt;/strong&gt; Thousands of blocked threads waking up to contend for freed tickets burn CPU on kernel context switching and mutex contention while disk I/O throughput stays flat. On &lt;code&gt;vmstat 1&lt;/code&gt; or &lt;code&gt;mpstat&lt;/code&gt;, operators see high &lt;code&gt;cs&lt;/code&gt; (context switches/sec) and elevated kernel &lt;code&gt;sy&lt;/code&gt; CPU while &lt;code&gt;iostat&lt;/code&gt; shows the EBS volume is nowhere near its provisioned IOPS ceiling—confirming that the cluster is scheduler-bound rather than compute-bound.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;How should platform engineers triage an active ticket-exhaustion cascade, shed rogue &lt;code&gt;COLLSCAN&lt;/code&gt; load safely without harming replication or transactions, and prevent drivers from amplifying the outage?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;During an active ticket-exhaustion incident, operators generally weigh three interventions.&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Intervention&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Effect on Tickets &amp;#x26; Cache&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Restart &lt;code&gt;mongod&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bounce the primary process&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Drops all connections and resets tickets&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Forces journal replay, wipes hot WiredTiger cache, and clients immediately reconnect&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Uncoordinated &lt;code&gt;rs.stepDown()&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Force primary failover under load&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Attempts to elect a fresh primary&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Stepdown lock queues behind blocked ops; apps replay the same &lt;code&gt;COLLSCAN&lt;/code&gt; on the new primary&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Surgical &lt;code&gt;killOp&lt;/code&gt; + Pool Bounds&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Terminate matching &lt;code&gt;COLLSCAN&lt;/code&gt; ops + throttle ingress&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Frees tickets in under 1s while preserving hot cache&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires strict filters to avoid killing &lt;code&gt;local.*&lt;/code&gt; replication or prepared 2PC txns&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chosen&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Restarting &lt;code&gt;mongod&lt;/code&gt; or forcing a failover treats a query-shape problem as a host problem. Surgical &lt;code&gt;killOp&lt;/code&gt; (or &lt;code&gt;setQuerySettings&lt;/code&gt; &lt;code&gt;reject: true&lt;/code&gt; in MongoDB 8.0+) removes the blocking table scans in place while preserving the warm WiredTiger cache and primary leadership.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-20-performance-playbook-3-options.svg&quot; alt=&quot;Comparison of restarting mongod, uncoordinated stepDown, and surgical killOp with ingress throttling&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-triage-architecture&quot;&gt;The Triage Architecture&lt;/h2&gt;
&lt;p&gt;Every incoming query traverses Linux socket queues, MongoDB’s logical intent locks (&lt;code&gt;IS&lt;/code&gt;/&lt;code&gt;IX&lt;/code&gt;), and WiredTiger’s admission ticket gate before touching B-tree pages:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-20-performance-playbook-4-architecture.svg&quot; alt=&quot;Inbound connection path from Linux somaxconn through epoll, logical locks, and WiredTiger&amp;#x27;s 128-ticket gate&quot;&gt;&lt;/p&gt;
&lt;p&gt;When &lt;code&gt;wiredTiger.concurrentTransactions.read.available&lt;/code&gt; approaches &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;globalLock.currentQueue.readers&lt;/code&gt; climbs, run a targeted &lt;code&gt;serverStatus()&lt;/code&gt; and &lt;code&gt;currentOp()&lt;/code&gt; snapshot to separate ticket starvation from hardware limits:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// 1. Atomic concurrency snapshot&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ss&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;serverStatus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;printjson&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  readAvail: ss.wiredTiger.concurrentTransactions.read.available,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  queuedReaders: ss.globalLock.currentQueue.readers,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  connCurrent: ss.connections.current&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// 2. Isolate active non-local operations running &gt;= 2s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;currentOp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  active: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  secs_running: { $gte: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  op: { $in: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;query&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;command&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;getmore&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ns: { $not:&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;^&lt;/span&gt;&lt;span style=&quot;color:#DBEDFF&quot;&gt;local&lt;/span&gt;&lt;span style=&quot;color:#85E89D;font-weight:bold&quot;&gt;\.&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}).inprog.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  opid: op.opid, secs: op.secs_running, ns: op.ns,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  plan: op.planSummary, waitingForLock: op.waitingForLock&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For post-incident forensics, MongoDB’s Full Time Diagnostic Data Capture (FTDC) files under &lt;code&gt;/var/log/mongodb/diagnostic.data/metrics.*&lt;/code&gt; record 1-second snapshots of &lt;code&gt;concurrentTransactions&lt;/code&gt;, &lt;code&gt;currentQueue&lt;/code&gt;, and &lt;code&gt;connections.current&lt;/code&gt; that can be extracted with &lt;code&gt;bsondump&lt;/code&gt; and &lt;code&gt;jq&lt;/code&gt;. Correlating &lt;code&gt;wiredTiger.concurrentTransactions.read.out&lt;/code&gt; against &lt;code&gt;connections.current&lt;/code&gt; and &lt;code&gt;opcounters.query&lt;/code&gt; in FTDC proves the exact second the ticket pool saturated—usually minutes before application HTTP 504 alerts or CPU saturation alarms fired.&lt;/p&gt;
&lt;p&gt;When inspecting &lt;code&gt;db.currentOp()&lt;/code&gt; output during triage, pay close attention to three diagnostic fields beyond &lt;code&gt;secs_running&lt;/code&gt; and &lt;code&gt;planSummary&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;numYields&lt;/code&gt;:&lt;/strong&gt; Long-running &lt;code&gt;COLLSCAN&lt;/code&gt; queries periodically yield their locks and tickets so other operations can proceed, then re-acquire them. A high &lt;code&gt;numYields&lt;/code&gt; count paired with &lt;code&gt;secs_running &gt;= 2&lt;/code&gt; confirms the query is repeatedly re-entering the ticket queue and churning cache pages.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;waitingForLock&lt;/code&gt; and &lt;code&gt;lockStats&lt;/code&gt;:&lt;/strong&gt; If &lt;code&gt;waitingForLock&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;lockStats.timeAcquiringMicros&lt;/code&gt; is high on a specific collection in Exclusive (&lt;code&gt;X&lt;/code&gt;) mode, you are dealing with a metadata/DDL lock (such as a &lt;code&gt;drop&lt;/code&gt; or &lt;code&gt;collMod&lt;/code&gt;) rather than pure WiredTiger ticket exhaustion.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;writeConflicts&lt;/code&gt;:&lt;/strong&gt; On write-heavy incidents, a high &lt;code&gt;writeConflicts&lt;/code&gt; counter indicates multiple threads mutating the exact same document concurrently under WiredTiger’s optimistic concurrency control, triggering internal retries that hold write tickets longer than normal.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Execute load shedding and permanent remediation in four ordered phases with explicit safety guards.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-20-performance-playbook-5-flow.svg&quot; alt=&quot;Four-phase triage flow: snapshot currentOp, shed COLLSCAN safely, bound driver pools, and deploy missing index&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;phase-1-surgical-load-shedding-via-filtered-killop&quot;&gt;Phase 1: Surgical Load Shedding via Filtered &lt;code&gt;killOp&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Target only active &lt;code&gt;COLLSCAN&lt;/code&gt; operations on the offending collection that have exceeded a runtime threshold (such as 3 seconds), and &lt;strong&gt;never&lt;/strong&gt; terminate internal replication (&lt;code&gt;ns&lt;/code&gt; matching &lt;code&gt;^local\.&lt;/code&gt; or &lt;code&gt;desc&lt;/code&gt; containing &lt;code&gt;replCoord&lt;/code&gt;/&lt;code&gt;rsBackgroundSync&lt;/code&gt;) or prepared multi-document transactions (&lt;code&gt;lsid&lt;/code&gt; + &lt;code&gt;txnNumber&lt;/code&gt; in &lt;code&gt;prepared&lt;/code&gt; state):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;currentOp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  active: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  secs_running: { $gte: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ns:&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#85E89D;font-weight:bold&quot;&gt;\.&lt;/span&gt;&lt;span style=&quot;color:#DBEDFF&quot;&gt;orders&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  planSummary: { $regex:&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;^&lt;/span&gt;&lt;span style=&quot;color:#DBEDFF&quot;&gt;COLLSCAN&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}).inprog.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;killOp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(op.opid));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Validation gate:&lt;/em&gt; Re-check &lt;code&gt;db.serverStatus().wiredTiger.concurrentTransactions.read.available&lt;/code&gt; within 1 second; available tickets should rebound toward &lt;code&gt;128&lt;/code&gt; as &lt;code&gt;currentQueue.readers&lt;/code&gt; drains.&lt;/p&gt;
&lt;h3 id=&quot;phase-2-ingress-throttling-server-side-maxtimems-and-driver-pool-bounds&quot;&gt;Phase 2: Ingress Throttling, Server-Side &lt;code&gt;maxTimeMS&lt;/code&gt;, and Driver Pool Bounds&lt;/h3&gt;
&lt;p&gt;If upstream pods immediately re-issue the same &lt;code&gt;COLLSCAN&lt;/code&gt; queries, tickets will re-exhaust. Apply temporary rate limiting at the API gateway or service mesh for the offending route, and standardize driver pools with bounded &lt;code&gt;maxPoolSize&lt;/code&gt; (for example, &lt;code&gt;50&lt;/code&gt; instead of &lt;code&gt;500&lt;/code&gt;, sized to match actual concurrent worker threads per pod rather than worst-case burst guesses), &lt;code&gt;maxIdleTimeMS: 60000&lt;/code&gt;, and &lt;code&gt;waitQueueTimeoutMS: 2000&lt;/code&gt; so blocked app threads fail fast instead of opening thousands of extra sockets. Note that &lt;code&gt;maxPoolSize&lt;/code&gt; applies &lt;em&gt;per &lt;code&gt;MongoClient&lt;/code&gt; instance per pod&lt;/em&gt;—if a pod instantiates multiple &lt;code&gt;MongoClient&lt;/code&gt; objects (one per module or tenant), total sockets multiply accordingly.&lt;/p&gt;
&lt;p&gt;In addition to pool bounds, enforce &lt;code&gt;maxTimeMS&lt;/code&gt; on non-critical read queries (or set &lt;code&gt;defaultMaxTimeMS&lt;/code&gt; on read-only analytical workloads) so MongoDB automatically aborts any query whose server-side execution time exceeds its SLA budget. Without &lt;code&gt;maxTimeMS&lt;/code&gt;, a client HTTP request timeout (for example, 5 seconds at the API gateway) drops the caller’s response while the orphaned &lt;code&gt;COLLSCAN&lt;/code&gt; continues running inside &lt;code&gt;mongod&lt;/code&gt; for minutes, holding WiredTiger tickets for a dead caller.&lt;/p&gt;
&lt;h3 id=&quot;phase-3-prometheus-early-warning-alerts&quot;&gt;Phase 3: Prometheus Early-Warning Alerts&lt;/h3&gt;
&lt;p&gt;Alert on ticket depletion, queue depth, and connection influx rate before CPU alarms fire:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Metric names depend on exporter version and casing; confirm them against your exporter&apos;s /metrics output&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;- &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;alert&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;MongoWiredTigerReadTicketsNearExhaustion&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  expr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;mongodb_ss_wiredtiger_concurrenttransactions_read_available &amp;#x3C; 20&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;30s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  labels&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;severity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;critical&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;- &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;alert&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;MongoQueuedOperationsElevated&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  expr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;mongodb_ss_globallock_currentqueue_readers &gt; 50&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;1m&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  labels&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;severity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;critical&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;phase-4-rolling-or-off-peak-index-deployment&quot;&gt;Phase 4: Rolling or Off-Peak Index Deployment&lt;/h3&gt;
&lt;p&gt;Create the missing index to eliminate the &lt;code&gt;COLLSCAN&lt;/code&gt; permanently. While MongoDB 4.4+ builds indexes in a hybrid non-blocking mode (using a brief lock at start and commit while sorting keys concurrently in the background), sorting keys on collections with tens or hundreds of millions of documents consumes substantial CPU, WiredTiger cache, and temporary disk I/O (&lt;code&gt;maxIndexBuildMemoryUsageMegabytes&lt;/code&gt;, default 200 MB). Schedule large builds off-peak, or execute a rolling index build—taking one secondary out of the replica set into standalone mode, building the index locally, rejoining it, repeating on the second secondary, stepping down the primary, and building on the retired primary—to keep production write latency unaffected. &lt;em&gt;Rollback:&lt;/em&gt; &lt;code&gt;dropIndex()&lt;/code&gt; if an index causes write-amplification regressions.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The triage sequence above aligns with documented MongoDB concurrency and query-control behavior across versions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Dynamic Admission Control (MongoDB 7.0+):&lt;/strong&gt; Starting in MongoDB 7.0, MongoDB replaced the fixed 128-ticket ceiling with dynamic ticket allocation that modulates concurrent storage execution tickets (up to &lt;code&gt;128&lt;/code&gt;) based on workload contention and storage latency, reducing scheduler thrashing under saturation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MongoDB 8.0 Operation Rejection (&lt;code&gt;setQuerySettings&lt;/code&gt;):&lt;/strong&gt; Rather than running recurring &lt;code&gt;killOp&lt;/code&gt; loops during an incident, MongoDB 8.0 introduces &lt;code&gt;setQuerySettings&lt;/code&gt; with &lt;code&gt;settings: { reject: true }&lt;/code&gt; (requiring &lt;code&gt;$db&lt;/code&gt; in the query shape document) to block matching &lt;code&gt;find&lt;/code&gt;, &lt;code&gt;distinct&lt;/code&gt;, or &lt;code&gt;aggregate&lt;/code&gt; shapes at planning time, inspectable via &lt;code&gt;$querySettings&lt;/code&gt; and cleared via &lt;code&gt;removeQuerySettings&lt;/code&gt;:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;adminCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  setQuerySettings: { find: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;orders&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, filter: { status: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;PENDING&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }, $db: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;prod&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  settings: { reject: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
Because &lt;code&gt;setQuerySettings&lt;/code&gt; persists cluster-wide across replica set members (and sharded routers), it survives primary stepdowns and prevents re-issued rogue queries from consuming a single WiredTiger ticket on any node. Once the missing index finishes building and is verified via &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt;, clear the rejection filter cleanly with &lt;code&gt;db.adminCommand({ removeQuerySettings: ... })&lt;/code&gt; rather than passing &lt;code&gt;reject: false&lt;/code&gt; alone.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;currentOp&lt;/code&gt; Wait Diagnostics:&lt;/strong&gt; MongoDB’s &lt;code&gt;currentOp&lt;/code&gt; command exposes &lt;code&gt;waitingForLock&lt;/code&gt;, &lt;code&gt;lockStats&lt;/code&gt; (&lt;code&gt;acquireCount&lt;/code&gt;, &lt;code&gt;acquireWaitCount&lt;/code&gt;, &lt;code&gt;timeAcquiringMicros&lt;/code&gt;), &lt;code&gt;writeConflicts&lt;/code&gt;, and &lt;code&gt;numYields&lt;/code&gt; to isolate logical lock contention from storage-level yields. Combining these fields in a dry-run-first CLI script (&lt;code&gt;pymongo&lt;/code&gt; &lt;code&gt;admin.command(&quot;serverStatus&quot;)&lt;/code&gt; + &lt;code&gt;admin.command(&quot;currentOp&quot;, ...)&lt;/code&gt; before &lt;code&gt;killOp&lt;/code&gt;) gives on-call engineers a repeatable, auditable way to preview candidate &lt;code&gt;opid&lt;/code&gt; values and client IP addresses before terminating a single query.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why Failover Fails Mid-Cascade:&lt;/strong&gt; Calling &lt;code&gt;rs.stepDown()&lt;/code&gt; during ticket exhaustion is doubly harmful: first, the primary must acquire an exclusive replication lock to step down, which can itself queue behind active operations; second, once a secondary wins the election, every microservice connection pool reconnects simultaneously and immediately replays the exact same unindexed query shape against the new primary’s colder cache, recreating the 128-ticket collapse within seconds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Aggregation Disk Spills (&lt;code&gt;allowDiskUse&lt;/code&gt;):&lt;/strong&gt; Aggregation pipelines without a leading indexed &lt;code&gt;$match&lt;/code&gt; stage that sort large result sets exceed MongoDB’s 100 MB in-memory sort ceiling. When &lt;code&gt;allowDiskUse: true&lt;/code&gt; is enabled, &lt;code&gt;mongod&lt;/code&gt; writes temporary sort files into &lt;code&gt;_tmp&lt;/code&gt; inside &lt;code&gt;dbPath&lt;/code&gt;, directly competing with WiredTiger checkpoint and journal writes on the same EBS volume. Routing heavy aggregations to dedicated analytics secondaries or adding covered &lt;code&gt;$match&lt;/code&gt; + &lt;code&gt;$sort&lt;/code&gt; compound indexes prevents analytical spills from starving OLTP tickets.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I have not run controlled &lt;code&gt;r6i.4xlarge&lt;/code&gt; ticket-saturation benchmarks personally; the Little’s Law cascade, &lt;code&gt;currentOp&lt;/code&gt; filtering rules, and MongoDB 7.0/8.0 controls above reflect documented MongoDB storage engine and driver behavior.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Killing prepared 2PC transactions or &lt;code&gt;local.*&lt;/code&gt; ops&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Aborts replication sync or leaves prepared transactions holding locks until coordinator recovery.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Replication lag spike or &lt;code&gt;prepared&lt;/code&gt; txns stuck in &lt;code&gt;currentOp&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exclude &lt;code&gt;^local\.&lt;/code&gt;, &lt;code&gt;replCoord&lt;/code&gt;, and &lt;code&gt;prepared&lt;/code&gt; &lt;code&gt;lsid&lt;/code&gt;/&lt;code&gt;txnNumber&lt;/code&gt; ops in &lt;code&gt;killOp&lt;/code&gt; filters.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Inflating &lt;code&gt;wiredTigerConcurrentReadTransactions&lt;/code&gt; &gt; 128&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Admits too many threads into WiredTiger, increasing CPU context switching and L3 cache thrashing.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Higher CPU &lt;code&gt;sy&lt;/code&gt; time with lower query throughput after &lt;code&gt;setParameter&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep the &lt;code&gt;128&lt;/code&gt; default (or 7.0+ dynamic admission) and eliminate the underlying &lt;code&gt;COLLSCAN&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Linux &lt;code&gt;somaxconn = 128&lt;/code&gt; during reconnect storm&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Kernel silently drops incoming TCP SYN packets when the accept queue fills.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Client connection timeouts with no corresponding &lt;code&gt;mongod&lt;/code&gt; log entries.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Ensure &lt;code&gt;net.core.somaxconn &gt;= 4096&lt;/code&gt; and bound driver &lt;code&gt;maxPoolSize&lt;/code&gt; + &lt;code&gt;waitQueueTimeoutMS&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unindexed &lt;code&gt;$sort&lt;/code&gt; spilling &gt; 100 MB to disk&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;allowDiskUse: true&lt;/code&gt; writes temporary sort files to the EBS volume, competing with journal I/O.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High EBS write throughput paired with &lt;code&gt;hasSortStage&lt;/code&gt; in slow query logs.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Add indexes covering &lt;code&gt;$match&lt;/code&gt; + &lt;code&gt;$sort&lt;/code&gt; or route heavy aggregations to secondaries.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Driver pools without &lt;code&gt;waitQueueTimeoutMS&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Blocked worker threads trigger unbounded socket creation up to &lt;code&gt;maxPoolSize&lt;/code&gt; across pods.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;connections.current&lt;/code&gt; spikes alongside &lt;code&gt;currentQueue.readers&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set &lt;code&gt;waitQueueTimeoutMS: 2000&lt;/code&gt; and &lt;code&gt;maxPoolSize: 50&lt;/code&gt; in all microservice URIs.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-20-performance-playbook-6-failures.svg&quot; alt=&quot;Failure-path overlay showing unsafe killOp on prepared transactions, ticket inflation above 128, somaxconn SYN drops, and sort spills&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Monitor &lt;code&gt;concurrentTransactions&lt;/code&gt; and &lt;code&gt;currentQueue&lt;/code&gt;, not just CPU:&lt;/strong&gt; High CPU during a MongoDB outage is often kernel context-switch thrashing while threads wait on exhausted storage tickets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Apply Little’s Law (&lt;code&gt;L = lambda x W&lt;/code&gt;) to spot ticket starvation:&lt;/strong&gt; Even 15 slow 10-second &lt;code&gt;COLLSCAN&lt;/code&gt; queries per second require 150 concurrent tickets, saturating the 128-ticket pool and blocking sub-millisecond &lt;code&gt;_id&lt;/code&gt; lookups.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Shed rogue queries surgically; never bounce or fail over mid-storm:&lt;/strong&gt; Filtered &lt;code&gt;killOp&lt;/code&gt; (or MongoDB 8.0 &lt;code&gt;setQuerySettings&lt;/code&gt; &lt;code&gt;reject: true&lt;/code&gt;) preserves hot cache and primary stability, whereas &lt;code&gt;rs.stepDown()&lt;/code&gt; exports the same &lt;code&gt;COLLSCAN&lt;/code&gt; storm to the new primary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Exclude replication and prepared transactions from &lt;code&gt;killOp&lt;/code&gt;:&lt;/strong&gt; Always filter out &lt;code&gt;^local\.&lt;/code&gt;, &lt;code&gt;replCoord&lt;/code&gt;, and &lt;code&gt;prepared&lt;/code&gt; &lt;code&gt;lsid&lt;/code&gt;/&lt;code&gt;txnNumber&lt;/code&gt; operations before issuing &lt;code&gt;killOp&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bound driver pools with &lt;code&gt;maxPoolSize&lt;/code&gt; and &lt;code&gt;waitQueueTimeoutMS&lt;/code&gt;:&lt;/strong&gt; Failing fast at 2 seconds in the application tier prevents 200 pods from opening tens of thousands of kernel socket buffers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Unindexed &lt;code&gt;COLLSCAN&lt;/code&gt; bursts exhaust WiredTiger’s &lt;code&gt;128&lt;/code&gt; read tickets, queue fast queries in &lt;code&gt;globalLock.currentQueue&lt;/code&gt;, and trigger upstream driver socket storms that consume host memory and saturate &lt;code&gt;somaxconn&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Alert on &lt;code&gt;concurrentTransactions.read.available &amp;#x3C; 20&lt;/code&gt;, isolate &lt;code&gt;COLLSCAN&lt;/code&gt; ops via filtered &lt;code&gt;db.currentOp()&lt;/code&gt;, shed them with &lt;code&gt;killOp&lt;/code&gt; (or MongoDB 8.0 &lt;code&gt;setQuerySettings&lt;/code&gt; &lt;code&gt;reject: true&lt;/code&gt; with &lt;code&gt;$db&lt;/code&gt;), and bound driver &lt;code&gt;maxPoolSize&lt;/code&gt; + &lt;code&gt;waitQueueTimeoutMS&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; After shedding matching &lt;code&gt;COLLSCAN&lt;/code&gt; ops in staging or production, confirm &lt;code&gt;concurrentTransactions.read.available&lt;/code&gt; rebounds toward &lt;code&gt;128&lt;/code&gt; and &lt;code&gt;globalLock.currentQueue.readers&lt;/code&gt; drops to &lt;code&gt;0&lt;/code&gt; within seconds without triggering a failover.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your microservice connection strings for &lt;code&gt;waitQueueTimeoutMS&lt;/code&gt;, &lt;code&gt;maxTimeMS&lt;/code&gt;, and &lt;code&gt;maxPoolSize&lt;/code&gt; today, and add &lt;code&gt;concurrentTransactions&lt;/code&gt; + &lt;code&gt;currentQueue&lt;/code&gt; alerts to your MongoDB dashboard this week.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>WiredTiger Under Pressure: Cache, Eviction, Checkpoints, Journaling, and the History Store</title><link>https://rajivonai.com/blog/2026-05-19-wiredtiger-pressure/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-19-wiredtiger-pressure/</guid><description>Diagnosing 60-second tail-latency spikes caused by WiredTiger cache eviction, checkpointing pressure, History Store bloat, and EBS IOPS exhaustion in write-heavy MongoDB workloads.</description><pubDate>Tue, 11 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;In a write-heavy database, memory is a write shock absorber—when dirty-page generation outpaces physical EBS bandwidth, WiredTiger invokes flow control, conscripts application threads into synchronous disk I/O, and drains all concurrent write tickets.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Periodic 60-second write latency cliffs occur when checkpoint flushing plus dirty-page accumulation saturates &lt;code&gt;gp3&lt;/code&gt; baseline bandwidth (3,000 IOPS / 125 MiB/s), pushing dirty cache past the &lt;code&gt;20%&lt;/code&gt; &lt;code&gt;eviction_dirty_trigger&lt;/code&gt; and stalling all write tickets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Scale &lt;code&gt;gp3&lt;/code&gt; IOPS and throughput online with &lt;code&gt;aws ec2 modify-volume&lt;/code&gt; (ensuring the EC2 instance’s own EBS bandwidth exceeds the volume target), keep &lt;code&gt;syncdelay&lt;/code&gt; at its &lt;code&gt;60&lt;/code&gt;-second default, and isolate long-running reads off the primary so &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; can prune.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Expanding &lt;code&gt;cacheSizeGB&lt;/code&gt; makes 60-second checkpoint stalls worse by allowing a larger dirty backlog to accumulate, and smaller EC2 instances like &lt;code&gt;r6g.large&lt;/code&gt; baseline at &lt;code&gt;78.75 MB/s&lt;/code&gt; (&lt;code&gt;630 Mbps&lt;/code&gt;) regardless of EBS provisioning.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-19-wiredtiger-pressure.svg&quot; alt=&quot;Comparison of saturated 125 MiB/s EBS causing 20 percent dirty cache stalls against scaled gp3 storage keeping dirty cache below 10 percent&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;WiredTiger is MongoDB’s multi-threaded storage engine. Under steady OLTP traffic, mutations update B-tree pages in WiredTiger’s internal cache and append sequentially to the write-ahead journal. To bound crash-recovery time and persist state durably, WiredTiger flushes dirty pages to data files continuously via background eviction threads and writes a crash-consistent &lt;strong&gt;checkpoint&lt;/strong&gt; every 60 seconds (or whenever 2 GB of journal data is written—meaning bulk-insert storms can fire back-to-back checkpoints in under 60 seconds, compounding random B-tree write amplification).&lt;/p&gt;
&lt;p&gt;When self-managed MongoDB clusters run on AWS EC2 backed by baseline &lt;code&gt;gp3&lt;/code&gt; EBS volumes (3,000 IOPS and 125 MiB/s throughput), high-throughput write workloads—such as event ingestion or frequent in-place updates—frequently generate dirty memory pages faster than the disk can flush them. Instead of slowing down smoothly, the cluster serves low-latency traffic for 50 to 55 seconds and then locks up on the 60-second checkpoint boundary.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Three documented WiredTiger thresholds and storage constraints explain why write tickets collapse during checkpoints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Eviction Watermark Hierarchy (&lt;code&gt;5%&lt;/code&gt; Target / &lt;code&gt;20%&lt;/code&gt; Trigger):&lt;/strong&gt; WiredTiger’s internal cache (defaulting to &lt;code&gt;max(0.5 * (RAM - 1 GB), 256 MB)&lt;/code&gt;) runs background eviction when total cache occupancy hits &lt;code&gt;80%&lt;/code&gt; (&lt;code&gt;eviction_target&lt;/code&gt;) or dirty pages hit &lt;code&gt;5%&lt;/code&gt; (&lt;code&gt;eviction_dirty_target&lt;/code&gt;). If disk bandwidth cannot keep up and dirty pages reach &lt;code&gt;20%&lt;/code&gt; (&lt;code&gt;eviction_dirty_trigger&lt;/code&gt;)—or total occupancy reaches &lt;code&gt;95%&lt;/code&gt; (&lt;code&gt;eviction_trigger&lt;/code&gt;)—WiredTiger engages emergency flow control and forces &lt;strong&gt;application worker threads&lt;/strong&gt; to evict and write dirty pages synchronously before executing their own queries. Notice the counter-intuitive cache-sizing math: &lt;code&gt;20%&lt;/code&gt; of a 30 GB cache is 6 GB of dirty pages (48 seconds to drain at 125 MB/s), whereas &lt;code&gt;20%&lt;/code&gt; of a 60 GB cache is 12 GB of dirty pages (96 seconds to drain at 125 MB/s). Expanding &lt;code&gt;cacheSizeGB&lt;/code&gt; without scaling disk throughput simply builds a larger dirty backlog before the wall hits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ticket Conscription Deadlock:&lt;/strong&gt; In MongoDB 4.4 and 5.0, &lt;code&gt;mongod&lt;/code&gt; caps concurrent storage operations at &lt;code&gt;128&lt;/code&gt; read tickets and &lt;code&gt;128&lt;/code&gt; write tickets (&lt;code&gt;concurrentTransactions&lt;/code&gt;). When a conscripted application thread blocks in disk wait (&lt;code&gt;w_await&lt;/code&gt;) on a saturated EBS volume, it &lt;strong&gt;holds its write ticket&lt;/strong&gt; until the &lt;code&gt;write()&lt;/code&gt; completes. Within milliseconds, all 128 write tickets are pinned by blocked threads, queuing every incoming write behind &lt;code&gt;globalLock&lt;/code&gt;. Meanwhile, sequential journal writes (flushed every &lt;code&gt;100&lt;/code&gt; ms by default, or immediately on &lt;code&gt;j: true&lt;/code&gt; / &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; operations) compete for the exact same EBS queue depth as random checkpoint page writes when journal and data share one volume.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Checkpoint Overlap Arithmetic:&lt;/strong&gt; Suppose a write-heavy workload accumulates 10 GB of dirty pages over a 60-second interval on a baseline &lt;code&gt;gp3&lt;/code&gt; volume capped at 125 MB/s:
$$\text{Flush Time} = \frac{10\text{ GB}}{125\text{ MB/s}} = 80\text{ seconds}$$
Because 80 seconds exceeds the 60-second checkpoint interval, the next checkpoint fires before the previous flush finishes, saturating the disk controller continuously.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;History Store Retention (&lt;code&gt;WiredTigerHS.wt&lt;/code&gt;):&lt;/strong&gt; Introduced in MongoDB 4.4 to support MVCC and point-in-time reads, the History Store holds older versions of updated documents during page reconciliation. Long-running transactions or analytical queries on the primary pin old read timestamps, preventing WiredTiger from pruning &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; and adding heavy random I/O to every checkpoint.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-19-wiredtiger-pressure-2-constraints.svg&quot; alt=&quot;Three WiredTiger constraints: 5% and 20% dirty watermarks, 80s checkpoint flush exceeding 60s cadence, and WiredTigerHS.wt retention&quot;&gt;&lt;/p&gt;
&lt;p&gt;Diagnose ticket exhaustion and dirty cache state directly from &lt;code&gt;db.serverStatus()&lt;/code&gt; and &lt;code&gt;iostat&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mongosh&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --eval&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  var c = db.serverStatus().wiredTiger.cache;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  var t = db.serverStatus().wiredTiger.concurrentTransactions;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  print(&quot;Dirty %: &quot; + ((c[&quot;tracked dirty bytes in the cache&quot;] / c[&quot;maximum bytes configured&quot;]) * 100).toFixed(1));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  print(&quot;App-thread evictions: &quot; + c[&quot;pages evicted by application threads&quot;]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  print(&quot;Write tickets avail: &quot; + t.write.available);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;iostat&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -xz&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Why does expanding &lt;code&gt;cacheSizeGB&lt;/code&gt; worsen 60-second WiredTiger stalls, and how can platform teams eliminate application-thread eviction without destabilizing checkpoints?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;When confronting 60-second checkpoint stalls, engineers often reach for memory or checkpoint-timer knobs before fixing storage throughput.&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Immediate Effect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Expand &lt;code&gt;cacheSizeGB&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Allocate 80–90% of RAM to WiredTiger cache&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Delays the first 20% dirty trigger&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Accumulates a larger dirty backlog, making checkpoints longer, and starves OS page cache&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Lower &lt;code&gt;syncdelay&lt;/code&gt; (&lt;code&gt;&amp;#x3C; 60s&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Force more frequent checkpoints via &lt;code&gt;storage.syncPeriodSecs&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Flushes smaller deltas per cycle&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;MongoDB docs explicitly advise against changing &lt;code&gt;syncdelay&lt;/code&gt; in production; increases metadata flush overhead&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Scale &lt;code&gt;gp3&lt;/code&gt; Online + Eviction Workers&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Raise EBS IOPS/throughput via &lt;code&gt;modify-volume&lt;/code&gt; + tune &lt;code&gt;eviction_dirty_target&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Background eviction outpaces dirty-page generation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires EC2 instance type with sufficient EBS bandwidth ceiling&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chosen&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Expanding &lt;code&gt;cacheSizeGB&lt;/code&gt; treats the symptom rather than the I/O deficit: 20% of a 100 GB cache is 20 GB of dirty pages instead of 12 GB on a 60 GB cache, which takes even longer to flush once triggered. Lowering &lt;code&gt;syncdelay&lt;/code&gt; below &lt;code&gt;60&lt;/code&gt; seconds contradicts MongoDB’s official parameter guidance, which warns that &lt;code&gt;syncdelay&lt;/code&gt; should remain at &lt;code&gt;60&lt;/code&gt; in production systems. Scaling EBS bandwidth online directly resolves the &lt;code&gt;Dirty Generation &gt; Disk Write Throughput&lt;/code&gt; inequality.&lt;/p&gt;
&lt;p&gt;Teams with extreme sustained write ingest also evaluate splitting &lt;code&gt;journal/&lt;/code&gt; onto a separate EBS volume so sequential WAL appends do not share an I/O queue with random checkpoint flushes. While separating journal and data volumes isolates write queues, it complicates backup consistency: crash-consistent EBS snapshots across two independent volumes require coordinated multi-volume snapshots or &lt;code&gt;db.fsyncLock()&lt;/code&gt; pauses, whereas a single &lt;code&gt;gp3&lt;/code&gt; volume with sufficient provisioned IOPS and throughput keeps backup orchestration simple.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-19-wiredtiger-pressure-3-options.svg&quot; alt=&quot;Comparison of expanding cacheSizeGB, lowering syncdelay, and scaling gp3 online with eviction tuning&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;wiredtiger-eviction-and-storage-architecture&quot;&gt;WiredTiger Eviction and Storage Architecture&lt;/h2&gt;
&lt;p&gt;Understanding where flow control triggers requires tracing a write through WiredTiger’s ticket gate, cache watermarks, and History Store reconciliation:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-19-wiredtiger-pressure-4-architecture.svg&quot; alt=&quot;WiredTiger write path: ticket gate, 5%/20% dirty cache watermarks, WiredTigerHS.wt reconciliation, and EBS storage&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Admission Ticket Gate:&lt;/strong&gt; Every operation acquires a read or write ticket before entering the storage engine. In MongoDB 4.4 and 5.0, this defaults to &lt;code&gt;128&lt;/code&gt; static tickets each; in MongoDB 7.0+, dynamic ticket allocation scales tickets adaptively up to &lt;code&gt;128&lt;/code&gt; based on workload contention.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;In-Memory Mutation and Journal Append:&lt;/strong&gt; The thread modifies the B-tree page in WiredTiger’s cache (marking it dirty) and appends the operation to the sequential write-ahead journal.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Background vs. Conscripted Eviction:&lt;/strong&gt; Below &lt;code&gt;5%&lt;/code&gt; dirty cache, eviction workers are idle. Between &lt;code&gt;5%&lt;/code&gt; and &lt;code&gt;20%&lt;/code&gt;, background eviction workers compress and flush dirty pages asynchronously. Above &lt;code&gt;20%&lt;/code&gt;, application threads are conscripted into synchronous eviction while holding their tickets. Note the diagnostic distinction in &lt;code&gt;db.serverStatus().wiredTiger.cache&lt;/code&gt;: rising &lt;code&gt;unmodified pages evicted&lt;/code&gt; indicates a working-set read miss problem (too little RAM for hot data), whereas rising &lt;code&gt;modified pages evicted&lt;/code&gt; paired with &lt;code&gt;pages evicted by application threads&lt;/code&gt; proves a dirty-page write throughput bottleneck.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;History Store Reconciliation:&lt;/strong&gt; When a dirty page is evicted or checkpointed, WiredTiger executes &lt;strong&gt;reconciliation&lt;/strong&gt;: it walks the in-memory page’s update chains, writes only the newest committed values into a fresh Snappy-compressed disk block for the collection &lt;code&gt;.wt&lt;/code&gt; file, and moves any older versions still required by active MVCC read timestamps into &lt;code&gt;WiredTigerHS.wt&lt;/code&gt;. Keeping &lt;code&gt;transactionLifetimeLimitSeconds&lt;/code&gt; at its &lt;code&gt;60&lt;/code&gt;-second default and routing analytical reads to secondaries prevents &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; from growing unbounded. Adding background eviction workers (&lt;code&gt;eviction=(threads_min=4,threads_max=8)&lt;/code&gt;) ensures reconciliation and compression parallelize across multiple CPU cores when dirty pages are spread across dozens of collections and indexes.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;production-configuration-baseline&quot;&gt;Production Configuration Baseline&lt;/h3&gt;
&lt;p&gt;Scale &lt;code&gt;gp3&lt;/code&gt; volumes online without downtime or instance restarts:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; ec2&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; modify-volume&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --volume-id&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;VOLUME_ID&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --volume-type&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; gp3&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --iops&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10000&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --throughput&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 500&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For sustained heavy-write clusters, optionally nudge WiredTiger’s background eviction to start earlier (&lt;code&gt;3%&lt;/code&gt; dirty target, &lt;code&gt;15%&lt;/code&gt; trigger) and add worker threads in &lt;code&gt;/etc/mongod.conf&lt;/code&gt; while keeping &lt;code&gt;syncPeriodSecs&lt;/code&gt; at &lt;code&gt;60&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;storage&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  syncPeriodSecs&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;60&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  wiredTiger&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    engineConfig&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      configString&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;eviction=(threads_min=4,threads_max=8),eviction_dirty_target=3,eviction_dirty_trigger=15&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Execute storage and eviction remediation in four gated steps so every change is reversible without cluster downtime.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-19-wiredtiger-pressure-5-flow.svg&quot; alt=&quot;Four-step online EBS scaling and WiredTiger eviction remediation flow&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Diagnose and Isolate the Bottleneck:&lt;/strong&gt; Capture &lt;code&gt;tracked dirty bytes in the cache&lt;/code&gt;, &lt;code&gt;pages evicted by application threads&lt;/code&gt;, &lt;code&gt;concurrentTransactions.write.available&lt;/code&gt;, and &lt;code&gt;iostat -xz&lt;/code&gt; (&lt;code&gt;w_await&lt;/code&gt;, &lt;code&gt;%util&lt;/code&gt;) during a spike window. Confirm whether &lt;code&gt;pages evicted by application threads&lt;/code&gt; increments alongside &lt;code&gt;w_await&lt;/code&gt; saturation, and check whether &lt;code&gt;VolumeQueueLength&lt;/code&gt; in CloudWatch rises in lockstep with the 60-second checkpoint timer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Verify EC2 Instance EBS Bandwidth Before Scaling Volume:&lt;/strong&gt; Check the EC2 instance type’s EBS baseline and burst limits before running &lt;code&gt;aws ec2 modify-volume&lt;/code&gt;. For example, an &lt;code&gt;r6g.large&lt;/code&gt; baselines at only &lt;code&gt;78.75 MB/s&lt;/code&gt; (&lt;code&gt;630 Mbps&lt;/code&gt;) and bursts to &lt;code&gt;593.75 MB/s&lt;/code&gt; (&lt;code&gt;4,750 Mbps&lt;/code&gt;); attaching a &lt;code&gt;500 MB/s&lt;/code&gt; &lt;code&gt;gp3&lt;/code&gt; volume to an under-sized instance hits the EC2 host ceiling once burst credits (&lt;code&gt;EBSByteBalance%&lt;/code&gt; in CloudWatch) expire. Scale the &lt;code&gt;gp3&lt;/code&gt; volume online (secondaries first, then primary) and monitor &lt;code&gt;aws ec2 describe-volumes-modifications&lt;/code&gt; until &lt;code&gt;optimizing&lt;/code&gt; or &lt;code&gt;completed&lt;/code&gt;. Note that AWS enforces a 6-hour cooldown between consecutive &lt;code&gt;modify-volume&lt;/code&gt; calls on the same EBS volume, so size the target IOPS and throughput with headroom on the first pass. &lt;em&gt;Rollback:&lt;/em&gt; &lt;code&gt;modify-volume&lt;/code&gt; can step IOPS/throughput back down after the 6-hour AWS modification cooldown.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bound History Store Retention:&lt;/strong&gt; Verify &lt;code&gt;transactionLifetimeLimitSeconds&lt;/code&gt; is &lt;code&gt;60&lt;/code&gt; (&lt;code&gt;db.adminCommand({ getParameter: 1, transactionLifetimeLimitSeconds: 1 })&lt;/code&gt;) and move long-running reporting queries to &lt;code&gt;readPreference=secondary&lt;/code&gt; so primary &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; reconciliation stays fast. Also check &lt;code&gt;db.serverStatus().wiredTiger.cache[&quot;history store score&quot;]&lt;/code&gt; or &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; file size on disk between checkpoints to confirm old MVCC versions are being reclaimed rather than accumulating.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Validate Zero User-Thread Evictions:&lt;/strong&gt; Under peak write load, sample &lt;code&gt;pages evicted by application threads&lt;/code&gt; over a 10-second interval (&lt;code&gt;after - before&lt;/code&gt; must equal &lt;code&gt;0&lt;/code&gt;), confirm dirty cache stays below &lt;code&gt;10%&lt;/code&gt;, and verify checkpoint duration (&lt;code&gt;transaction checkpoint most recent time (msecs)&lt;/code&gt;) remains well below the 60-second interval. If a single &lt;code&gt;gp3&lt;/code&gt; volume’s provisioned IOPS or throughput (scalable up to &lt;code&gt;80,000 IOPS&lt;/code&gt; and &lt;code&gt;2,000 MiB/s&lt;/code&gt; on modern AWS limits, or &lt;code&gt;io2&lt;/code&gt; for sub-millisecond tail latency) still saturates during burst ingest, horizontal sharding is the remaining structural lever to split dirty-page generation across multiple storage volumes.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The failure modes and remediation steps above are grounded in documented WiredTiger, MongoDB, and AWS EC2 behavior:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;WiredTiger Eviction Thresholds:&lt;/strong&gt; WiredTiger and MongoDB documentation explicitly define the four default cache watermarks: &lt;code&gt;eviction_target&lt;/code&gt; (&lt;code&gt;80%&lt;/code&gt;), &lt;code&gt;eviction_trigger&lt;/code&gt; (&lt;code&gt;95%&lt;/code&gt;), &lt;code&gt;eviction_dirty_target&lt;/code&gt; (&lt;code&gt;5%&lt;/code&gt;), and &lt;code&gt;eviction_dirty_trigger&lt;/code&gt; (&lt;code&gt;20%&lt;/code&gt;). Crossing &lt;code&gt;20%&lt;/code&gt; dirty or &lt;code&gt;95%&lt;/code&gt; total triggers application-thread eviction, which is directly observable via &lt;code&gt;db.serverStatus().wiredTiger.cache[&quot;pages evicted by application threads&quot;]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MongoDB &lt;code&gt;syncdelay&lt;/code&gt; and &lt;code&gt;transactionLifetimeLimitSeconds&lt;/code&gt;:&lt;/strong&gt; MongoDB’s parameter reference specifies &lt;code&gt;syncdelay&lt;/code&gt; (&lt;code&gt;60&lt;/code&gt; seconds) as the checkpoint interval and explicitly advises &lt;strong&gt;not&lt;/strong&gt; modifying it in production; &lt;code&gt;transactionLifetimeLimitSeconds&lt;/code&gt; defaults to &lt;code&gt;60&lt;/code&gt; seconds to bound runaway multi-document transactions and prevent cache/History Store pressure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MongoDB 7.0+ Dynamic Concurrency Control:&lt;/strong&gt; Starting in MongoDB 7.0, MongoDB replaced the static 128-ticket cap with &lt;strong&gt;dynamic ticket allocation&lt;/strong&gt; (adaptive concurrency control) that adjusts concurrent read and write storage execution tickets in real time (up to &lt;code&gt;128&lt;/code&gt;) based on workload contention—meaning lower ticket counts under light load in 7.0+ are expected rather than a sign of throttling.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Isolating History Store vs. Data Eviction:&lt;/strong&gt; When &lt;code&gt;transaction checkpoint most recent time (msecs)&lt;/code&gt; spikes, operators can dynamically raise log verbosity for &lt;code&gt;component.STORAGE.WTHS&lt;/code&gt; (History Store) and &lt;code&gt;component.STORAGE.WTEVICT&lt;/code&gt; (eviction) to verify whether reconciliation time is spent pruning old MVCC versions or flushing dirty collection pages.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I have not operated a 40,000-ops/s IoT ingest cluster on &lt;code&gt;r6g.4xlarge&lt;/code&gt; personally; the mechanisms, watermarks, and AWS EBS bandwidth limits above are derived directly from MongoDB, WiredTiger, and AWS EC2 documentation.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;EC2 instance EBS cap &amp;#x3C; &lt;code&gt;gp3&lt;/code&gt; target&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Instance like &lt;code&gt;r6g.large&lt;/code&gt; baselines at &lt;code&gt;78.75 MB/s&lt;/code&gt; (&lt;code&gt;630 Mbps&lt;/code&gt;), throttling high-throughput &lt;code&gt;gp3&lt;/code&gt; volumes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;iostat&lt;/code&gt; throughput plateaus below provisioned &lt;code&gt;gp3&lt;/code&gt; MB/s; &lt;code&gt;w_await&lt;/code&gt; climbs.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Upgrade EC2 instance class to match sustained EBS throughput before scaling &lt;code&gt;gp3&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Legacy &lt;code&gt;gp2&lt;/code&gt; volume under heavy writes&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Burst credits drain; IOPS drops to &lt;code&gt;3 IOPS/GiB&lt;/code&gt; baseline and triggers permanent eviction stalls.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;CloudWatch &lt;code&gt;VolumeBurstBalance&lt;/code&gt; reaches &lt;code&gt;0&lt;/code&gt; alongside ticket exhaustion.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Migrate &lt;code&gt;gp2&lt;/code&gt; to &lt;code&gt;gp3&lt;/code&gt; (or &lt;code&gt;io2&lt;/code&gt;) online with &lt;code&gt;aws ec2 modify-volume&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Long-running transactions on primary&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Old read timestamps pin MVCC versions in &lt;code&gt;WiredTigerHS.wt&lt;/code&gt;, bloating checkpoints.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Growing &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; file size and rising checkpoint scan times.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep &lt;code&gt;transactionLifetimeLimitSeconds&lt;/code&gt; at &lt;code&gt;60&lt;/code&gt; and route analytics to secondaries.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;2 GB journal checkpoint loop&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Extreme write ingest fills 2 GB of journal data faster than 60s, firing back-to-back checkpoints.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;transaction checkpoints&lt;/code&gt; increments faster than 1/minute.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Shard write-heavy collections horizontally across multiple replica set shards.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Oversized &lt;code&gt;cacheSizeGB&lt;/code&gt; (&lt;code&gt;&gt; 50%&lt;/code&gt; RAM)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Starves Linux OS page cache and allows larger dirty backlogs to accumulate between checkpoints.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rising physical read IOPS and multi-second checkpoint stalls.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep &lt;code&gt;cacheSizeGB&lt;/code&gt; at &lt;code&gt;max(0.5 * (RAM - 1 GB), 256 MB)&lt;/code&gt; or lower.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-19-wiredtiger-pressure-6-failures.svg&quot; alt=&quot;Failure-path overlay showing EC2 EBS bandwidth caps, History Store bloat, gp2 burst drain, and 2 GB journal checkpoint loops&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;60-second latency cliffs are storage bandwidth deficits, not CPU bugs:&lt;/strong&gt; When &lt;code&gt;Dirty Generation Rate &gt; Disk Write Throughput&lt;/code&gt;, dirty cache crosses &lt;code&gt;20%&lt;/code&gt;, conscripting app threads while they hold write tickets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never fix checkpoint stalls by expanding &lt;code&gt;cacheSizeGB&lt;/code&gt;:&lt;/strong&gt; A larger cache holds a larger dirty backlog, lengthening checkpoint flushes and starving the compressed Linux OS page cache.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Keep &lt;code&gt;syncdelay&lt;/code&gt; at &lt;code&gt;60&lt;/code&gt; seconds:&lt;/strong&gt; MongoDB documentation advises against lowering &lt;code&gt;syncdelay&lt;/code&gt; in production; scale &lt;code&gt;gp3&lt;/code&gt; IOPS and throughput online with &lt;code&gt;aws ec2 modify-volume&lt;/code&gt; instead.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Check EC2 instance EBS ceilings before scaling &lt;code&gt;gp3&lt;/code&gt;:&lt;/strong&gt; An &lt;code&gt;r6g.large&lt;/code&gt; baselines at &lt;code&gt;78.75 MB/s&lt;/code&gt; (&lt;code&gt;630 Mbps&lt;/code&gt;), which will bottleneck a &lt;code&gt;500 MB/s&lt;/code&gt; &lt;code&gt;gp3&lt;/code&gt; volume once burst credits expire.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Protect &lt;code&gt;WiredTigerHS.wt&lt;/code&gt; from long-running reads:&lt;/strong&gt; Enforce &lt;code&gt;transactionLifetimeLimitSeconds = 60&lt;/code&gt; and route analytical queries to secondaries so MVCC reconciliation stays bounded.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Write-heavy MongoDB clusters on baseline &lt;code&gt;gp3&lt;/code&gt; or &lt;code&gt;gp2&lt;/code&gt; EBS volumes hit periodic 60-second stalls when dirty cache crosses &lt;code&gt;20%&lt;/code&gt;, conscripting application threads and exhausting write tickets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Scale &lt;code&gt;gp3&lt;/code&gt; IOPS and throughput online via &lt;code&gt;aws ec2 modify-volume&lt;/code&gt; on an EC2 instance class with matching EBS bandwidth, tune background eviction (&lt;code&gt;eviction_dirty_target=3&lt;/code&gt;), and keep long-running reads off the primary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Sample &lt;code&gt;pages evicted by application threads&lt;/code&gt; over 10 seconds under peak write load and verify it increments by &lt;code&gt;0&lt;/code&gt;, while &lt;code&gt;tracked dirty bytes&lt;/code&gt; stays below &lt;code&gt;10%&lt;/code&gt; of &lt;code&gt;maximum bytes configured&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run the &lt;code&gt;db.serverStatus().wiredTiger&lt;/code&gt; diagnostic snippet on your busiest primary today, compare its EBS write throughput against &lt;code&gt;iostat -xz 1&lt;/code&gt;, and provision &lt;code&gt;gp3&lt;/code&gt; headroom before the next traffic peak.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>wiredtiger</category><category>performance</category><category>checklist</category></item><item><title>Linux and EC2 Tuning for MongoDB and WiredTiger</title><link>https://rajivonai.com/blog/2026-05-18-linux-ec2-tuning/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-18-linux-ec2-tuning/</guid><description>How suboptimal Linux kernel memory management, Transparent Huge Pages, and EBS block readahead starve WiredTiger of resources and trigger p99 latency spikes.</description><pubDate>Mon, 10 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;When MongoDB runs on Linux, its latency and throughput guarantees are co-authored by the kernel—default OS settings designed for sequential batch workloads cause compaction stalls and 32x EBS read amplification under WiredTiger.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; General-purpose Linux defaults—Transparent Huge Pages (&lt;code&gt;always&lt;/code&gt;) on MongoDB 4.4–7.0, 256-sector (128 KB) block readahead, high dirty memory ratios, and &lt;code&gt;ext4&lt;/code&gt;—cause thread stalls, &lt;code&gt;gp3&lt;/code&gt; bandwidth exhaustion, and OOM kills.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; On MongoDB 4.4–7.0, disable THP (&lt;code&gt;never&lt;/code&gt;) via a &lt;code&gt;systemd&lt;/code&gt; unit, set NVMe readahead to 32 sectors (16 KB) via &lt;code&gt;udev&lt;/code&gt;, mount XFS with &lt;code&gt;noatime&lt;/code&gt;, set &lt;code&gt;vm.swappiness=1&lt;/code&gt; with a 1–2 GB swap file, and cap WiredTiger’s cache at &lt;code&gt;max(0.5 * (RAM - 1 GB), 256 MB)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; MongoDB 8.0 reverses the THP rule—its upgraded per-CPU TCMalloc allocator on x86_64 and ARM64 requires THP &lt;strong&gt;enabled&lt;/strong&gt; (&lt;code&gt;always&lt;/code&gt; / &lt;code&gt;defer+madvise&lt;/code&gt;), so AMI tuning must branch on MongoDB major version.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-18-linux-ec2-tuning.svg&quot; alt=&quot;Comparison of general-purpose Linux defaults against a tuned EC2 baseline for MongoDB 4.4 to 7.0&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Self-managed MongoDB deployments on AWS EC2 (&lt;code&gt;r5&lt;/code&gt; Intel or &lt;code&gt;r6g&lt;/code&gt; Graviton2/3) backed by &lt;code&gt;gp3&lt;/code&gt; EBS volumes rely on WiredTiger’s dual-cache architecture: an uncompressed internal cache paired with the Linux OS page cache. Out of the box, however, enterprise Linux distributions (Amazon Linux, RHEL, Ubuntu) ship with defaults tuned for sequential file I/O and desktop responsiveness rather than random 4 KB B-tree lookups and multi-threaded memory churn.&lt;/p&gt;
&lt;p&gt;When working sets exceed physical RAM under high concurrency, these OS defaults interact destructively with WiredTiger. Instead of degrading smoothly, clusters suffer periodic thread freezes, exhausted EBS throughput, and sudden &lt;code&gt;SIGKILL&lt;/code&gt; terminations from the Linux Out-Of-Memory (OOM) killer.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Three documented Linux kernel and block-layer behaviors explain why un-tuned EC2 instances stall under WiredTiger workloads:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Transparent Huge Pages (THP) Compaction Stalls (MongoDB 4.4–7.0):&lt;/strong&gt; Linux manages standard virtual memory in 4 KB pages, while THP allocates contiguous 2 MB pages to reduce TLB misses. Under MongoDB 4.4 through 7.0 (which use legacy per-thread TCMalloc), &lt;code&gt;mongod&lt;/code&gt; threads continuously allocate and free variable-sized BSON documents, hazard pointers, and cache descriptors, fragmenting physical RAM. When a 2 MB contiguous allocation fails, &lt;code&gt;khugepaged&lt;/code&gt; and direct memory compaction freeze threads in uninterruptible sleep (&lt;code&gt;D&lt;/code&gt; state) while relocating 4 KB pages, driving up kernel system CPU (&lt;code&gt;sy&lt;/code&gt;) and stalling WiredTiger read/write tickets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Block Device Readahead Amplification (32x on 4 KB Reads):&lt;/strong&gt; Linux NVMe EBS devices default to &lt;code&gt;256&lt;/code&gt; sectors (&lt;code&gt;128 KB&lt;/code&gt;) of readahead. When an OLTP index lookup misses WiredTiger’s cache and requests a single 4 KB page, the kernel prefetches the target 4 KB plus 124 KB of adjacent blocks. For random B-tree leaf lookups, those adjacent blocks are rarely read—consuming 32x the EBS bandwidth (&lt;code&gt;128 KB / 4 KB = 32&lt;/code&gt;) and evicting hot compressed blocks from the OS page cache. The arithmetic is stark on a baseline &lt;code&gt;gp3&lt;/code&gt; volume (125 MiB/s throughput): just 1,000 random 4 KB cache misses per second represent only ~3.9 MiB/s of logical data, yet with 128 KB readahead they demand 125 MiB/s of physical EBS read bandwidth, saturating the disk ceiling completely.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dirty Memory Flush Spikes and &lt;code&gt;swapoff -a&lt;/code&gt; OOM Kills:&lt;/strong&gt; Default &lt;code&gt;vm.dirty_background_ratio&lt;/code&gt; (10%) and &lt;code&gt;vm.dirty_ratio&lt;/code&gt; (20%–30%) scale poorly on large-RAM database instances. On a 128 GB &lt;code&gt;r6g.4xlarge&lt;/code&gt;, 10% dirty memory equals 12.8 GB of buffered writes—if flushed in a burst against a 125 MiB/s &lt;code&gt;gp3&lt;/code&gt; volume, draining that backlog takes over 100 seconds of sustained disk saturation, stalling WiredTiger’s 60-second checkpoints. Conversely, running &lt;code&gt;swapoff -a&lt;/code&gt; removes the kernel’s last-resort paging margin so any transient memory spike triggers an immediate OOM &lt;code&gt;SIGKILL&lt;/code&gt; against &lt;code&gt;mongod&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-18-linux-ec2-tuning-2-constraints.svg&quot; alt=&quot;Three Linux kernel constraints: THP 2 MB compaction stalls, 32x block readahead amplification, and zero-swap OOM exposure&quot;&gt;&lt;/p&gt;
&lt;p&gt;Inspect active kernel state on an EC2 host before applying changes:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;cat&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /sys/kernel/mm/transparent_hugepage/enabled&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;cat&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /sys/kernel/mm/transparent_hugepage/defrag&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -E&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;compact_stall|compact_fail|compact_success&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /proc/vmstat&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; blockdev&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --getra&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /dev/nvme1n1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;vmstat&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;How should platform teams configure Linux memory management, swap, block readahead, and filesystems on AWS EC2 so the kernel accelerates WiredTiger instead of generating tail-latency spikes?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams typically choose among three approaches when managing Linux parameters for EC2 MongoDB fleets.&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Approach&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Persistence &amp;#x26; Memory Model&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Pros&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Ad-hoc Runtime Shell Commands&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;echo never&lt;/code&gt; + &lt;code&gt;blockdev --setra&lt;/code&gt; via SSH&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Immediate effect on live host&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Silently resets to OS defaults on EC2 instance reboot&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;90% WT Cache + &lt;code&gt;swapoff -a&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Over-allocates &lt;code&gt;cacheSizeGB&lt;/code&gt;, disables swap&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Slightly larger uncompressed B-tree cache&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Starves OS compressed page cache; OOM killer targets &lt;code&gt;mongod&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Immutable AMI (&lt;code&gt;systemd&lt;/code&gt; + &lt;code&gt;udev&lt;/code&gt; + XFS)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Persistent unit files, &lt;code&gt;sysctl&lt;/code&gt;, 1–2 GB swap, ~50% WT cache&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reboot-safe, balanced dual-cache, eliminates 32x read amp&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires version-branching THP rules for MongoDB 8.0+&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chosen&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Running &lt;code&gt;echo never &gt; /sys/kernel/mm/transparent_hugepage/enabled&lt;/code&gt; or &lt;code&gt;blockdev --setra 32&lt;/code&gt; interactively fixes a host only until the next AWS maintenance reboot or autoscaling replacement. Similarly, setting &lt;code&gt;cacheSizeGB&lt;/code&gt; to 90% of host RAM starves the Linux page cache, forcing every WiredTiger cache miss to hit physical EBS storage rather than decompressing from RAM. Baking &lt;code&gt;systemd&lt;/code&gt;, &lt;code&gt;udev&lt;/code&gt;, &lt;code&gt;sysctl&lt;/code&gt;, and XFS mount options into an immutable Packer AMI eliminates configuration drift across the fleet.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-18-linux-ec2-tuning-3-options.svg&quot; alt=&quot;Comparison of ad-hoc shell tuning, 90% cache with zero swap, and immutable Packer AMI with systemd and udev&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-dual-cache-architecture&quot;&gt;The Dual-Cache Architecture&lt;/h2&gt;
&lt;p&gt;Unlike engines that manage a single monolithic buffer pool, WiredTiger relies on two cooperating memory tiers:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-18-linux-ec2-tuning-4-architecture.svg&quot; alt=&quot;Dual-cache memory hierarchy on a 64 GB r6g.2xlarge splitting 28 GB WiredTiger cache and Linux OS page cache over XFS gp3&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;WiredTiger Internal Cache:&lt;/strong&gt; Governed by &lt;code&gt;storage.wiredTiger.engineConfig.cacheSizeGB&lt;/code&gt; (defaulting to &lt;code&gt;max(0.5 * (RAM - 1 GB), 256 MB)&lt;/code&gt;—for example, ~28–31 GB on a 64 GB &lt;code&gt;r6g.2xlarge&lt;/code&gt;). It stores uncompressed BSON documents and B-tree pages for direct CPU pointer traversal. Keeping &lt;code&gt;cacheSizeGB&lt;/code&gt; at or slightly below 50% of &lt;code&gt;(RAM - 1 GB)&lt;/code&gt; also leaves headroom for connection thread stacks and observability agents (&lt;code&gt;node_exporter&lt;/code&gt;, CloudWatch, Datadog) without encroaching on OS page cache.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Linux OS Page Cache:&lt;/strong&gt; The remaining host RAM holds raw filesystem blocks read from or written to EBS. Because WiredTiger compresses collection blocks with Snappy or zstd before writing to disk, the OS page cache stores &lt;strong&gt;compressed&lt;/strong&gt; blocks, giving it higher effective data density per gigabyte of RAM.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When a query misses WiredTiger’s internal cache, WiredTiger reads from the filesystem. If the compressed block resides in the Linux page cache, WiredTiger decompresses it in memory without issuing an EBS I/O operation. Preserving roughly 40%–45% of RAM for the OS page cache is therefore essential for read throughput.&lt;/p&gt;
&lt;h3 id=&quot;production-os-configuration-baseline&quot;&gt;Production OS Configuration Baseline&lt;/h3&gt;
&lt;h4 id=&quot;1-disable-thp-via-systemd-mongodb-4470-only&quot;&gt;1. Disable THP via &lt;code&gt;systemd&lt;/code&gt; (MongoDB 4.4–7.0 Only)&lt;/h4&gt;
&lt;p&gt;For MongoDB 4.4 through 7.0, deploy &lt;code&gt;/etc/systemd/system/disable-transparent-huge-pages.service&lt;/code&gt; with &lt;code&gt;Before=mongod.service&lt;/code&gt; so THP is disabled before &lt;code&gt;mongod&lt;/code&gt; allocates memory:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;ini&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;[Unit]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;Description&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=Disable Transparent Hugepages (THP)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DefaultDependencies&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=no&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;After&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=sysinit.target local-fs.target&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;Before&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=mongod.service&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;[Service]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;Type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=oneshot&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ExecStart&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=/bin/sh -c &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;echo never &gt; /sys/kernel/mm/transparent_hugepage/enabled &amp;#x26;&amp;#x26; echo never &gt; /sys/kernel/mm/transparent_hugepage/defrag&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;[Install]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WantedBy&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=basic.target&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;2-virtual-memory-sysctl-systemd-limits-and-swap-safety-valve&quot;&gt;2. Virtual Memory (&lt;code&gt;sysctl&lt;/code&gt;), &lt;code&gt;systemd&lt;/code&gt; Limits, and Swap Safety Valve&lt;/h4&gt;
&lt;p&gt;Deploy &lt;code&gt;/etc/sysctl.d/99-mongodb.conf&lt;/code&gt; to keep a 1–2 GB swap file available as an OOM emergency valve while instructing the kernel to reclaim page cache aggressively before touching swap (&lt;code&gt;vm.swappiness = 1&lt;/code&gt;), and to flush dirty pages smoothly (&lt;code&gt;5%&lt;/code&gt; background, &lt;code&gt;15%&lt;/code&gt; synchronous cap). On instances with 128 GB or more of RAM, consider replacing percentage ratios with fixed byte thresholds (&lt;code&gt;vm.dirty_background_bytes&lt;/code&gt; and &lt;code&gt;vm.dirty_bytes&lt;/code&gt;) so background flushing starts after a predictable megabyte ceiling rather than waiting for gigabytes of dirty pages to accumulate.&lt;/p&gt;
&lt;p&gt;Also note that &lt;code&gt;systemd&lt;/code&gt;-managed services ignore &lt;code&gt;/etc/security/limits.conf&lt;/code&gt;; set &lt;code&gt;LimitNOFILE=64000&lt;/code&gt; and &lt;code&gt;LimitNPROC=64000&lt;/code&gt; directly inside &lt;code&gt;mongod.service&lt;/code&gt; (or a drop-in override) so high connection counts plus WiredTiger’s per-collection and per-index &lt;code&gt;.wt&lt;/code&gt; file descriptors never hit &lt;code&gt;EMFILE&lt;/code&gt; errors.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;ini&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# /etc/sysctl.d/99-mongodb.conf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;vm.swappiness&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;vm.dirty_background_ratio&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;vm.dirty_ratio&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 15&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;vm.vfs_cache_pressure&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 50&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;net.core.somaxconn&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 65535&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;fs.file-max&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 64000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;3-nvme-readahead-udev-and-xfs-noatime&quot;&gt;3. NVMe Readahead (&lt;code&gt;udev&lt;/code&gt;) and XFS &lt;code&gt;noatime&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;MongoDB Production Notes recommend XFS over &lt;code&gt;ext4&lt;/code&gt; (to avoid &lt;code&gt;ext4&lt;/code&gt; extent-allocation latency spikes) mounted with &lt;code&gt;noatime&lt;/code&gt;, and a low block readahead between 8 and 32 sectors (4 KB to 16 KB) on random-I/O OLTP volumes. Without &lt;code&gt;noatime&lt;/code&gt;, every read against a &lt;code&gt;.wt&lt;/code&gt; file that misses the OS page cache can trigger an inode access-time metadata update, turning pure read traffic into background metadata writes on the EBS volume. Similarly, &lt;code&gt;ext4&lt;/code&gt; serializes concurrent file growth and journal commits during WiredTiger’s 60-second checkpoints, whereas XFS supports parallel allocation groups across multi-core EC2 instances. Persist 16 KB readahead via &lt;code&gt;/etc/udev/rules.d/85-mongodb-readahead.rules&lt;/code&gt; and configure &lt;code&gt;/etc/fstab&lt;/code&gt; for XFS:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ACTION==&quot;add|change&quot;, KERNEL==&quot;nvme[0-9]*n[0-9]*&quot;, ATTR{bdi/read_ahead_kb}=&quot;16&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;UUID=xxxx-xxxx-xxxx  /var/lib/mongodb  xfs  noatime,nodiratime  0 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For multi-socket hosts, wrap &lt;code&gt;mongod&lt;/code&gt; startup in &lt;code&gt;numactl --interleave=all&lt;/code&gt; so memory pages distribute evenly across NUMA nodes instead of exhausting one socket’s local memory while another sits idle. In addition, check &lt;code&gt;/proc/$(pgrep mongod)/limits&lt;/code&gt; after every service restart to verify that &lt;code&gt;Max open files&lt;/code&gt; and &lt;code&gt;Max user processes&lt;/code&gt; reflect &lt;code&gt;64000&lt;/code&gt; rather than the Linux default of &lt;code&gt;1024&lt;/code&gt; or &lt;code&gt;4096&lt;/code&gt;, because each WiredTiger collection and index opens dedicated file descriptors concurrently with client TCP sockets.&lt;/p&gt;
&lt;p&gt;When tuning &lt;code&gt;vm.vfs_cache_pressure = 50&lt;/code&gt; (down from the Linux default of &lt;code&gt;100&lt;/code&gt;), the kernel retains filesystem dentry and inode metadata in RAM longer under memory pressure, preventing repeated metadata lookups across thousands of &lt;code&gt;.wt&lt;/code&gt; files when &lt;code&gt;directoryForIndexes: true&lt;/code&gt; is enabled.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Roll OS tuning through Packer AMI builds and rolling secondary replacement rather than mutating live primaries in place.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-18-linux-ec2-tuning-5-flow.svg&quot; alt=&quot;Four-step Packer AMI and Ansible rollout with systemd, sysctl, XFS, and gate verification checks&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Bake &lt;code&gt;systemd&lt;/code&gt; and &lt;code&gt;udev&lt;/code&gt; Rules into the AMI:&lt;/strong&gt; Include &lt;code&gt;disable-transparent-huge-pages.service&lt;/code&gt; (for 4.4–7.0 images) and &lt;code&gt;85-mongodb-readahead.rules&lt;/code&gt; in the Packer/Ansible pipeline. &lt;em&gt;Validation gate:&lt;/em&gt; boot a test EC2 instance and verify &lt;code&gt;cat /sys/kernel/mm/transparent_hugepage/enabled&lt;/code&gt; prints &lt;code&gt;[never]&lt;/code&gt; and &lt;code&gt;blockdev --getra&lt;/code&gt; returns &lt;code&gt;32&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Apply &lt;code&gt;sysctl&lt;/code&gt; and Provision 1–2 GB Swap:&lt;/strong&gt; Roll &lt;code&gt;/etc/sysctl.d/99-mongodb.conf&lt;/code&gt; via &lt;code&gt;sysctl --system&lt;/code&gt; and verify &lt;code&gt;swapon --show&lt;/code&gt; reports the emergency swap file. &lt;em&gt;Rollback:&lt;/em&gt; revert &lt;code&gt;99-mongodb.conf&lt;/code&gt; and reload &lt;code&gt;sysctl --system&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Format XFS Volumes and Replace Secondaries:&lt;/strong&gt; On new or replacement replica set members, format &lt;code&gt;/var/lib/mongodb&lt;/code&gt; with &lt;code&gt;mkfs.xfs&lt;/code&gt;, mount with &lt;code&gt;noatime&lt;/code&gt;, and sync each node as a secondary before stepping down the primary. &lt;em&gt;Validation gate:&lt;/em&gt; inspect &lt;code&gt;/proc/$(pgrep mongod)/limits&lt;/code&gt; to confirm &lt;code&gt;Max open files&lt;/code&gt; and &lt;code&gt;Max user processes&lt;/code&gt; are at least &lt;code&gt;64000&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Verify Post-Rollout Kernel Telemetry:&lt;/strong&gt; Under production traffic, confirm &lt;code&gt;compact_stall&lt;/code&gt; counters in &lt;code&gt;/proc/vmstat&lt;/code&gt; remain flat, &lt;code&gt;si&lt;/code&gt;/&lt;code&gt;so&lt;/code&gt; in &lt;code&gt;vmstat 1 10&lt;/code&gt; remain &lt;code&gt;0&lt;/code&gt;, and EBS read throughput aligns with logical WiredTiger cache-miss rates. On multi-socket hosts, inspect &lt;code&gt;/proc/$(pgrep mongod)/numa_maps&lt;/code&gt; to confirm interleaved page allocation across sockets (&lt;code&gt;interleave&lt;/code&gt;), and check &lt;code&gt;journalctl -k&lt;/code&gt; for any &lt;code&gt;XFS&lt;/code&gt; mount warnings or OOM-killer invocations before promoting the node to primary.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The tuning baseline above rests on documented MongoDB Production Notes and Linux kernel mechanics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;MongoDB Production Notes (Linux):&lt;/strong&gt; MongoDB’s official Linux production documentation explicitly mandates XFS with &lt;code&gt;noatime&lt;/code&gt; over &lt;code&gt;ext4&lt;/code&gt;, recommends &lt;code&gt;blockdev --setra&lt;/code&gt; between 8 and 32 sectors (4 KB–16 KB) for WiredTiger OLTP volumes, and specifies &lt;code&gt;vm.swappiness = 1&lt;/code&gt; (or &lt;code&gt;0&lt;/code&gt; on older kernels) plus &lt;code&gt;numactl --interleave=all&lt;/code&gt; on NUMA hardware.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The MongoDB 8.0 THP Reversal:&lt;/strong&gt; In MongoDB 8.0, MongoDB upgraded its vendored TCMalloc allocator on Linux &lt;code&gt;x86_64&lt;/code&gt; and &lt;code&gt;ARM64&lt;/code&gt; to use &lt;strong&gt;per-CPU caches&lt;/strong&gt; instead of per-thread caches. Because per-CPU TCMalloc reduces fragmentation and benefits from 2 MB page lookups, MongoDB 8.0 documentation &lt;strong&gt;reverses&lt;/strong&gt; the legacy rule and requires THP to be &lt;strong&gt;enabled&lt;/strong&gt; (&lt;code&gt;always&lt;/code&gt; / &lt;code&gt;defrag: defer+madvise&lt;/code&gt;) on supported architectures—while also warning against Linux kernels &lt;code&gt;6.19&lt;/code&gt; through &lt;code&gt;7.0.13&lt;/code&gt; due to a known TCMalloc crash bug fixed in &lt;code&gt;7.0.14+&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Linux 6.x Multi-Gen LRU (MGLRU):&lt;/strong&gt; Modern kernels in Amazon Linux 2023 and Ubuntu 24.04 LTS support MGLRU for lower-overhead page cache eviction, but they do not alter the &lt;code&gt;gp3&lt;/code&gt; 32x readahead amplification math or the need to preserve OS page cache headroom. Operators can verify the impact of readahead and dirty-ratio changes directly via &lt;code&gt;iostat -xz 1&lt;/code&gt; (&lt;code&gt;r/s&lt;/code&gt;, &lt;code&gt;rkB/s&lt;/code&gt;, &lt;code&gt;r_await&lt;/code&gt;, &lt;code&gt;w_await&lt;/code&gt;, and &lt;code&gt;aqu-sz&lt;/code&gt;), confirming that average read request sizes (&lt;code&gt;rareq-sz&lt;/code&gt;) drop from ~128 KB toward 16 KB–32 KB on random OLTP workloads.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I have not run controlled &lt;code&gt;r6g.4xlarge&lt;/code&gt; A/B benchmarks on these kernel parameters personally; every rule above is derived directly from MongoDB’s documented Production Notes, MongoDB 8.0 release notes, and Linux virtual memory behavior.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;THP disabled on MongoDB 8.0+&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Upgraded per-CPU TCMalloc expects THP (&lt;code&gt;always&lt;/code&gt; / &lt;code&gt;defer+madvise&lt;/code&gt;); disabling THP hurts 8.0 allocation efficiency.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;mongod&lt;/code&gt; 8.0 startup warnings or elevated TLB/allocator overhead on x86_64/ARM64.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Branch Packer/Ansible roles by version: THP &lt;code&gt;never&lt;/code&gt; for 4.4–7.0, THP &lt;code&gt;always&lt;/code&gt; for 8.0+.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;cacheSizeGB&lt;/code&gt; set to 90% RAM&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Starves Linux OS page cache; every WiredTiger cache miss forces a physical EBS read.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;iostat&lt;/code&gt; read IOPS spike despite high RAM utilization.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cap &lt;code&gt;cacheSizeGB&lt;/code&gt; at &lt;code&gt;max(0.5 * (RAM - 1 GB), 256 MB)&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;swapoff -a&lt;/code&gt; under memory spike&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero paging headroom leaves the OOM killer no option except &lt;code&gt;SIGKILL&lt;/code&gt; on &lt;code&gt;mongod&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;dmesg&lt;/code&gt; shows &lt;code&gt;Out of memory: Killed process (mongod)&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Provision 1–2 GB swap file with &lt;code&gt;vm.swappiness = 1&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;32-sector readahead on analytical nodes&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Dedicated &lt;code&gt;COLLSCAN&lt;/code&gt; / batch aggregation nodes lose sequential prefetching.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Slower sequential scan throughput on reporting secondaries.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep 32 sectors for OLTP primaries; use 128–256 on dedicated analytics secondaries.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;ext4&lt;/code&gt; data volume on multi-core EC2&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Single-lock extent allocation in &lt;code&gt;ext4&lt;/code&gt; serializes concurrent WiredTiger file growth.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Periodic write freezes in &lt;code&gt;mongod.log&lt;/code&gt; during checkpointing.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Format &lt;code&gt;/var/lib/mongodb&lt;/code&gt; with XFS (&lt;code&gt;mkfs.xfs&lt;/code&gt;) and mount with &lt;code&gt;noatime&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-18-linux-ec2-tuning-6-failures.svg&quot; alt=&quot;Failure-path overlay showing oversized WiredTiger cache, zero-swap OOM SIGKILL, ext4 inode locks, and NUMA skew&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Match THP policy to MongoDB major version:&lt;/strong&gt; Disable THP (&lt;code&gt;never&lt;/code&gt;) via &lt;code&gt;systemd&lt;/code&gt; for MongoDB 4.4 through 7.0, but &lt;strong&gt;enable&lt;/strong&gt; THP (&lt;code&gt;always&lt;/code&gt; / &lt;code&gt;defer+madvise&lt;/code&gt;) for MongoDB 8.0+ on &lt;code&gt;x86_64&lt;/code&gt; and &lt;code&gt;ARM64&lt;/code&gt; where per-CPU TCMalloc is active.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cut NVMe readahead from 256 to 32 sectors (16 KB) via &lt;code&gt;udev&lt;/code&gt;:&lt;/strong&gt; Eliminates the 32x EBS read amplification on random 4 KB B-tree lookups and stops unneeded blocks from poisoning the OS page cache.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Protect the dual-cache split (~50% WT / ~40% OS page cache):&lt;/strong&gt; Over-allocating &lt;code&gt;cacheSizeGB&lt;/code&gt; starves compressed block caching in Linux RAM and forces physical EBS reads on every miss.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never run &lt;code&gt;swapoff -a&lt;/code&gt; in production:&lt;/strong&gt; Pair a 1–2 GB emergency swap file with &lt;code&gt;vm.swappiness = 1&lt;/code&gt; so minor memory spikes page idle daemons instead of triggering &lt;code&gt;SIGKILL&lt;/code&gt; on &lt;code&gt;mongod&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Always use XFS with &lt;code&gt;noatime&lt;/code&gt; and &lt;code&gt;numactl --interleave=all&lt;/code&gt;:&lt;/strong&gt; Avoids &lt;code&gt;ext4&lt;/code&gt; extent-allocation lock contention and prevents single-socket memory exhaustion on multi-socket hosts.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; General-purpose Linux defaults (THP &lt;code&gt;always&lt;/code&gt; on 4.4–7.0, 128 KB readahead, &lt;code&gt;ext4&lt;/code&gt;, and &lt;code&gt;swapoff -a&lt;/code&gt;) cause compaction stalls, 32x EBS read amplification, and OOM crashes under WiredTiger.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Bake version-aware THP units, 32-sector NVMe &lt;code&gt;udev&lt;/code&gt; rules, &lt;code&gt;sysctl&lt;/code&gt; (&lt;code&gt;vm.swappiness=1&lt;/code&gt;, &lt;code&gt;dirty_background_ratio=5&lt;/code&gt;, &lt;code&gt;dirty_ratio=15&lt;/code&gt;), 1–2 GB swap, and XFS &lt;code&gt;noatime&lt;/code&gt; into your Packer/Ansible EC2 AMIs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Verify &lt;code&gt;cat /sys/kernel/mm/transparent_hugepage/enabled&lt;/code&gt;, &lt;code&gt;blockdev --getra&lt;/code&gt;, &lt;code&gt;swapon --show&lt;/code&gt;, and &lt;code&gt;/proc/$(pgrep mongod)/limits&lt;/code&gt; (&lt;code&gt;&gt;= 64000&lt;/code&gt; open files) on every host before joining it to a replica set.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your running EC2 MongoDB nodes today for THP state, readahead sector size, and filesystem type, and update your golden AMI pipeline before your next rolling replacement.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>linux</category><category>performance</category><category>checklist</category></item><item><title>Running Self-Managed MongoDB on AWS EC2: The Standardized Production Architecture</title><link>https://rajivonai.com/blog/2026-05-17-production-architecture/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-17-production-architecture/</guid><description>How to design, deploy, and standardize resilient 3-AZ MongoDB replica sets on AWS EC2: quorum math, TLS 1.3 enforcement, priority tuning, and cross-AZ write latency budgets.</description><pubDate>Sun, 09 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;In a distributed database, high availability is not achieved by launching instances across multiple cloud zones—it is governed by strict quorum mathematics, symmetric election priorities, and transport-layer standardization.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Two-AZ replica set placement loses 2 of 3 votes on a single-zone outage, asymmetric member priorities cause &lt;code&gt;priorityTakeover&lt;/code&gt; election flapping, and legacy &lt;code&gt;net.ssl&lt;/code&gt; directives permit downgraded TLS handshakes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Deploy a symmetric 3-AZ Primary-Secondary-Secondary (PSS) replica set with &lt;code&gt;priority: 1&lt;/code&gt; and &lt;code&gt;votes: 1&lt;/code&gt; on every node, &lt;code&gt;net.tls.mode: requireTLS&lt;/code&gt; with &lt;code&gt;disabledProtocols: TLS1_0,TLS1_1,TLS1_2&lt;/code&gt;, and &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; plus &lt;code&gt;retryWrites=true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Cross-AZ &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; writes incur an inescapable inter-AZ network round-trip compared to &lt;code&gt;w: 1&lt;/code&gt;, and &lt;code&gt;replication.enableMajorityReadConcern&lt;/code&gt; was removed from user configurability (permanently &lt;code&gt;true&lt;/code&gt;) starting in MongoDB 5.0.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-17-production-architecture.svg&quot; alt=&quot;Comparison of 2-AZ placement losing quorum on AZ-A outage against 3-AZ symmetric placement preserving 2/3 majority&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Running self-managed MongoDB Community Edition on AWS EC2 gives platform teams direct control over Linux kernel tuning, dedicated &lt;code&gt;gp3&lt;/code&gt; or &lt;code&gt;io2&lt;/code&gt; EBS volumes, NUMA memory allocation, and instance sizing without managed-service markups. Across multi-cluster estates, however, ad-hoc provisioning introduces three recurring structural weaknesses:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Two-Availability-Zone Quorum Vulnerability:&lt;/strong&gt; Placing two voting members in AZ-A and one in AZ-B leaves only 1 of 3 votes if AZ-A fails or partitions, dropping below the strict majority threshold and forcing the surviving node to step down to read-only.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Asymmetric Priority Flapping:&lt;/strong&gt; Assigning skewed priorities (such as &lt;code&gt;10&lt;/code&gt;, &lt;code&gt;5&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt;) to pin the primary to a preferred AZ triggers &lt;code&gt;priorityTakeover&lt;/code&gt; elections whenever a transient link recovers, causing repeated write pauses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Transport Layer Drift:&lt;/strong&gt; Mixing deprecated &lt;code&gt;net.ssl.mode&lt;/code&gt; settings (deprecated in MongoDB 4.2, removed in 8.0) with modern &lt;code&gt;net.tls.mode&lt;/code&gt; directives permits unencrypted or legacy TLS 1.0/1.1/1.2 connections and breaks strict modern drivers.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Standardizing these three surfaces in Terraform and Ansible turns failover behavior from an emergent accident into a deterministic property.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;MongoDB replica sets use a Raft-derived consensus protocol where electing or sustaining a primary requires a strict majority of voting members:&lt;/p&gt;
&lt;p&gt;$$\text{Quorum Threshold} = \left\lfloor \frac{N}{2} \right\rfloor + 1$$&lt;/p&gt;
&lt;p&gt;For $N = 3$ voting members, quorum is 2 (tolerating 1 failure). For $N = 5$, quorum is 3 (tolerating 2 failures). Notice why even-numbered voting sets are an operational anti-pattern: for $N = 4$, &lt;code&gt;floor(4/2) + 1 = 3&lt;/code&gt;, which still tolerates only 1 node failure—paying for a fourth voting node yields zero additional fault tolerance while increasing replication fanout and risking 2-versus-2 network splits where neither side can elect a primary.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-17-production-architecture-2-constraints.svg&quot; alt=&quot;Three consensus and security constraints: strict majority floor(N/2)+1, 10000ms default electionTimeoutMillis, and net.ssl to net.tls migration&quot;&gt;&lt;/p&gt;
&lt;p&gt;Three documented protocol and configuration constraints define the design space:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Single-AZ Failure Math:&lt;/strong&gt; In a 2-AZ deployment (&lt;code&gt;2 + 1&lt;/code&gt;), losing the 2-node zone removes 66.7% of votes at once. The surviving single node holds 1 of 3 votes, cannot elect a primary, and rejects all writes until the partitioned zone returns or an operator forces a risky single-node reconfiguration. Even if an operator runs &lt;code&gt;rs.reconfig(..., { force: true })&lt;/code&gt; to strip the dead nodes, any writes accepted by the old primary before the partition that did not replicate to the surviving node are lost or rolled back when the original zone reconnects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Heartbeat and Election Timing (&lt;code&gt;electionTimeoutMillis&lt;/code&gt;):&lt;/strong&gt; MongoDB defaults &lt;code&gt;electionTimeoutMillis&lt;/code&gt; to &lt;code&gt;10000&lt;/code&gt; ms (10 seconds), with nodes exchanging heartbeats every 2 seconds. When a primary becomes unreachable for the &lt;code&gt;electionTimeoutMillis&lt;/code&gt; window, eligible secondaries call an election. If priorities are skewed, a recovering higher-priority secondary within 10 seconds of the primary’s latest optime initiates a &lt;code&gt;priorityTakeover&lt;/code&gt; election even when the active primary is healthy—turning minor packet loss into election thrashing. Furthermore, while a &lt;code&gt;priorityTakeover&lt;/code&gt; election is in flight, the replica set briefly has no writable primary, forcing application drivers to buffer or retry in-flight writes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TLS Deprecation Boundary:&lt;/strong&gt; MongoDB 4.2 deprecated &lt;code&gt;net.ssl&lt;/code&gt; in favor of &lt;code&gt;net.tls&lt;/code&gt;, and MongoDB 8.0 removed &lt;code&gt;net.ssl&lt;/code&gt; entirely. Leaving &lt;code&gt;preferSSL&lt;/code&gt; or omitting &lt;code&gt;disabledProtocols&lt;/code&gt; allows older cipher suites and causes handshake failures when client drivers enforce TLS 1.3 or strict certificate verification. In addition, MongoDB nodes authenticate intra-cluster replication traffic using either a shared &lt;code&gt;security.keyFile&lt;/code&gt; or &lt;code&gt;x509&lt;/code&gt; client certificates; drifting certificate SANs or mismatched CA chains across nodes can silently break replication even when client ports appear open.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Before standardizing a cluster, inspect current priorities, heartbeat round-trip times (&lt;code&gt;pingMs&lt;/code&gt;), and the latency delta between &lt;code&gt;w: 1&lt;/code&gt; and &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mongosh&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --eval&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;rs.conf().members.map(m =&gt; ({ host: m.host, priority: m.priority, votes: m.votes }))&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mongosh&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --eval&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;rs.status().members.map(m =&gt; ({ name: m.name, stateStr: m.stateStr, pingMs: m.pingMs }))&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;How can platform teams design an immutable 3-AZ MongoDB topology on AWS EC2 that survives any single-zone outage, eliminates priority takeover thrashing, and enforces TLS 1.3 while budgeting for cross-AZ &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; latency?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams selecting a 3-node MongoDB topology on AWS generally compare three layouts.&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Topology&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Placement&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Durability &amp;#x26; Quorum&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;2-AZ PSS (&lt;code&gt;2 + 1&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2 data nodes in AZ-A, 1 in AZ-B&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;3 data copies, 3 votes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Losing AZ-A drops 2/3 votes and halts all writes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;3-AZ PSA (Arbiter)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2 data nodes + 1 vote-only arbiter&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2 data copies, 3 votes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Losing 1 data node leaves 1 data copy; &lt;code&gt;w:majority&lt;/code&gt; stalls or triggers cache pressure&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;3-AZ Symmetric PSS&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;1 data node per AZ (&lt;code&gt;priority: 1&lt;/code&gt; each)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;3 data copies, 3 votes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cross-AZ &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; incurs 1 inter-AZ network RTT per write&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chosen&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;A &lt;strong&gt;2-AZ PSS&lt;/strong&gt; layout works only if you accept total write unavailability whenever the 2-node zone experiences an outage or transit severance. A &lt;strong&gt;3-AZ PSA&lt;/strong&gt; topology saves one EBS volume and one compute instance by replacing the third data node with an arbiter, but if one data node goes down for maintenance or failure, only a single data copy remains and &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; writes block or force WiredTiger cache pressure while awaiting the missing data node. Even worse, in MongoDB 5.0+, &lt;code&gt;writeConcernMajorityJournalDefault&lt;/code&gt; and the PSA majority commit-point lag can cause the surviving data node’s WiredTiger cache to fill with uncommitted historical versions until the second data node returns.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;3-AZ Symmetric PSS&lt;/strong&gt; topology places one full data-bearing voting member in each of three Availability Zones with equal priority. Any single-zone outage leaves two healthy data copies and 2 of 3 votes intact.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-17-production-architecture-3-options.svg&quot; alt=&quot;Comparison of 2-AZ PSS, 3-AZ PSA with Arbiter, and 3-AZ Symmetric PSS topologies&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;standardized-3-az-architecture&quot;&gt;Standardized 3-AZ Architecture&lt;/h2&gt;
&lt;p&gt;The production standard deploys three identical EC2 instances (for example, ARM64 Graviton &lt;code&gt;r6g.2xlarge&lt;/code&gt; with 64 GB RAM) across three private subnets in &lt;code&gt;us-east-1a&lt;/code&gt;, &lt;code&gt;us-east-1b&lt;/code&gt;, and &lt;code&gt;us-east-1c&lt;/code&gt;, each backed by a dedicated XFS-formatted &lt;code&gt;gp3&lt;/code&gt; EBS data volume mounted at &lt;code&gt;/var/lib/mongodb&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-17-production-architecture-4-architecture.svg&quot; alt=&quot;Reference 3-AZ EC2 topology showing application subnets connecting via TLS 1.3 to symmetric priority-1 PSS nodes&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-symmetric-consensus&quot;&gt;1. Symmetric Consensus&lt;/h3&gt;
&lt;p&gt;Every member receives identical voting and priority weights:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Symmetric 3-AZ member weights&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;members[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]: { priority: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, votes: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// AZ-a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;members[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]: { priority: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, votes: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// AZ-b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;members[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]: { priority: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, votes: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// AZ-c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With symmetric priorities, whichever node holds the primary role keeps it until an actual failure or operator &lt;code&gt;rs.stepDown()&lt;/code&gt; occurs. A recovering node rejoins quietly as a &lt;code&gt;SECONDARY&lt;/code&gt; without forcing a &lt;code&gt;priorityTakeover&lt;/code&gt; election.&lt;/p&gt;
&lt;p&gt;Pair symmetric priorities with explicit read and write preferences in application drivers. Route transactional reads to &lt;code&gt;readPreference=primary&lt;/code&gt; (or &lt;code&gt;primaryPreferred&lt;/code&gt; only when stale reads during a brief election are acceptable) so callers do not observe replication-lag anomalies from secondaries. When a workload genuinely requires scaling read throughput onto secondaries, enforce &lt;code&gt;readConcern: { level: &quot;majority&quot; }&lt;/code&gt; on those queries so secondaries only return data that has already been majority-committed and cannot be rolled back.&lt;/p&gt;
&lt;h3 id=&quot;2-hardened-mongodconf-baseline&quot;&gt;2. Hardened &lt;code&gt;mongod.conf&lt;/code&gt; Baseline&lt;/h3&gt;
&lt;p&gt;Enforce TLS 1.3 by setting &lt;code&gt;net.tls.mode: requireTLS&lt;/code&gt; and listing &lt;code&gt;TLS1_0,TLS1_1,TLS1_2&lt;/code&gt; in &lt;code&gt;net.tls.disabledProtocols&lt;/code&gt; (comma-separated, no spaces). Bound &lt;code&gt;wiredTiger.engineConfig.cacheSizeGB&lt;/code&gt; explicitly—28 GB on a 64 GB &lt;code&gt;r6g.2xlarge&lt;/code&gt; leaves ample RAM for the Linux page cache and monitoring agents. Setting &lt;code&gt;wiredTiger.engineConfig.directoryForIndexes: true&lt;/code&gt; separates collection &lt;code&gt;.wt&lt;/code&gt; files and index &lt;code&gt;.wt&lt;/code&gt; files into distinct subdirectories under &lt;code&gt;/var/lib/mongodb&lt;/code&gt;, allowing operators to mount separate EBS volumes for data and indexes on I/O-intensive clusters without rewriting &lt;code&gt;dbPath&lt;/code&gt;. Mount the underlying XFS volume with &lt;code&gt;noatime&lt;/code&gt; so metadata access-time writes do not amplify EBS IOPS during high-concurrency reads, and verify that &lt;code&gt;ulimit -n&lt;/code&gt; (&lt;code&gt;LimitNOFILE&lt;/code&gt; in systemd) is at least &lt;code&gt;64000&lt;/code&gt; so &lt;code&gt;maxIncomingConnections: 10000&lt;/code&gt; plus WiredTiger’s open &lt;code&gt;.wt&lt;/code&gt; file descriptors never hit &lt;code&gt;EMFILE&lt;/code&gt; errors. Note that &lt;code&gt;replication.enableMajorityReadConcern&lt;/code&gt; is configurable in MongoDB 4.4, but starting in MongoDB 5.0 it is permanently &lt;code&gt;true&lt;/code&gt; and should be omitted from 5.0+ configs.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# /etc/mongod.conf — Standardized Production Baseline (MongoDB 4.4 / 5.0+)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;net&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  port&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;27017&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  bindIp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0.0.0.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  maxIncomingConnections&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  tls&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    mode&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;requireTLS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    certificateKeyFile&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/etc/ssl/mongodb/mongodb.pem&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    CAFile&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/etc/ssl/mongodb/ca.crt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    allowInvalidCertificates&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    allowInvalidHostnames&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    disabledProtocols&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;TLS1_0,TLS1_1,TLS1_2&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;   # forces TLS 1.3: needs OpenSSL 1.1.1+ and TLS 1.3-capable drivers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;storage&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  dbPath&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/var/lib/mongodb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  engine&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;wiredTiger&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  wiredTiger&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    engineConfig&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      cacheSizeGB&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;28&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      directoryForIndexes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    collectionConfig&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      blockCompressor&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;snappy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    indexConfig&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      prefixCompression&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;operationProfiling&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  mode&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;slowOp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  slowOpThresholdMs&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  rateLimit&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;replication&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  replSetName&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;prod-rs01&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-immutable-terraform-provisioning&quot;&gt;3. Immutable Terraform Provisioning&lt;/h3&gt;
&lt;p&gt;Encode the 3-AZ subnet placement and &lt;code&gt;gp3&lt;/code&gt; storage in Terraform so clusters cannot drift into 2-AZ placement:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;locals&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  availability_zones&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;us-east-1a&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;us-east-1b&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;us-east-1c&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;aws_instance&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;mongo_node&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  count&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                  =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ami&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                    =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; data&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;aws_ami&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;al2023_arm64&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  instance_type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;          =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;r6g.2xlarge&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  subnet_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;private_subnet_ids[count&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  vpc_security_group_ids&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [aws_security_group&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;mongo_cluster&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tags&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;mongodb-prod-node-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;index&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    AZ   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; local.availability_zones[count.index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Standardizing an existing cluster to TLS 1.3 and symmetric priorities runs in four gated phases to avoid unplanned failovers.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-17-production-architecture-5-flow.svg&quot; alt=&quot;Four-phase rolling TLS 1.3 and symmetric priority standardization flow&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Certificate Deployment:&lt;/strong&gt; Distribute the CA bundle and node &lt;code&gt;.pem&lt;/code&gt; certificates across all three hosts (&lt;code&gt;chmod 400&lt;/code&gt;, &lt;code&gt;chown mongodb:mongodb&lt;/code&gt;). &lt;em&gt;Validation gate:&lt;/em&gt; verify certificate SANs match each node’s private DNS hostname with &lt;code&gt;openssl x509 -noout -text&lt;/code&gt;. &lt;em&gt;Rollback:&lt;/em&gt; remove unused cert files; &lt;code&gt;mongod&lt;/code&gt; is untouched.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rolling Secondary Reload:&lt;/strong&gt; Update &lt;code&gt;/etc/mongod.conf&lt;/code&gt; on Secondary 1 (&lt;code&gt;Node 2&lt;/code&gt;) and restart &lt;code&gt;mongod&lt;/code&gt;. Wait for &lt;code&gt;rs.status()&lt;/code&gt; to return &lt;code&gt;SECONDARY&lt;/code&gt; and zero replication lag, then repeat on Secondary 2 (&lt;code&gt;Node 3&lt;/code&gt;). If transitioning a live plaintext cluster to &lt;code&gt;requireTLS&lt;/code&gt;, never flip directly to &lt;code&gt;requireTLS&lt;/code&gt; on the first node—doing so causes the updated secondary to reject unencrypted replication connections from the still-plaintext primary. Instead, roll &lt;code&gt;net.tls.mode: allowTLS&lt;/code&gt; or &lt;code&gt;preferTLS&lt;/code&gt; across all three nodes first (so peers accept both plaintext and TLS), switch all application connection strings to &lt;code&gt;tls=true&lt;/code&gt;, and only then roll &lt;code&gt;requireTLS&lt;/code&gt; with &lt;code&gt;disabledProtocols: TLS1_0,TLS1_1,TLS1_2&lt;/code&gt; in a second pass. &lt;em&gt;Rollback:&lt;/em&gt; restore previous &lt;code&gt;mongod.conf&lt;/code&gt; and restart.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Controlled Primary Stepdown:&lt;/strong&gt; Run &lt;code&gt;rs.stepDown(60, 10)&lt;/code&gt; on &lt;code&gt;Node 1&lt;/code&gt; so leadership passes cleanly to a caught-up secondary, then apply the updated &lt;code&gt;mongod.conf&lt;/code&gt; to &lt;code&gt;Node 1&lt;/code&gt; and rejoin it as a secondary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Priority Alignment and Chaos Validation:&lt;/strong&gt; Set &lt;code&gt;priority = 1&lt;/code&gt; on all members via &lt;code&gt;rs.reconfig(conf)&lt;/code&gt;. Verify TLS 1.3 with &lt;code&gt;openssl s_client -connect mongo-node1.internal:27017 -tls1_3&lt;/code&gt;, then test single-AZ partition resilience by temporarily dropping port &lt;code&gt;27017&lt;/code&gt; on one secondary (&lt;code&gt;iptables -A INPUT -p tcp --dport 27017 -j DROP&lt;/code&gt;) and confirming a &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; insert still commits across the remaining two nodes.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The operational tradeoffs of this architecture follow directly from documented MongoDB consensus rules and AWS network behavior:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Quorum and &lt;code&gt;electionTimeoutMillis&lt;/code&gt;:&lt;/strong&gt; MongoDB’s election protocol documentation specifies that a 3-member replica set requires 2 votes to elect or retain a primary, and that &lt;code&gt;electionTimeoutMillis&lt;/code&gt; defaults to &lt;code&gt;10000&lt;/code&gt; ms. Lowering &lt;code&gt;electionTimeoutMillis&lt;/code&gt; accelerates failure detection at the cost of higher sensitivity to transient VPC jitter; keeping &lt;code&gt;priority: 1&lt;/code&gt; symmetric across all members prevents unnecessary &lt;code&gt;priorityTakeover&lt;/code&gt; elections when links recover.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cross-AZ &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; Latency Budget:&lt;/strong&gt; A &lt;code&gt;w: 1&lt;/code&gt; write commits locally on the primary, whereas a &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; write must replicate across the VPC to at least one secondary in another Availability Zone before acknowledging. Application SLAs must budget for that inter-AZ network round-trip rather than expecting &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; to match local &lt;code&gt;w: 1&lt;/code&gt; latency, because &lt;code&gt;w: 1&lt;/code&gt; leaves acknowledged writes vulnerable to rollback if the primary fails before replication completes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PSA Cache Pressure:&lt;/strong&gt; MongoDB documentation explicitly warns that in 3-member PSA topologies, losing one data node prevents &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; commits from advancing the majority commit point, which can cause WiredTiger cache pressure to build up on the surviving data node—another reason to standardize on 3 full data nodes (PSS).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Journal Durability (&lt;code&gt;writeConcernMajorityJournalDefault&lt;/code&gt;):&lt;/strong&gt; By default (&lt;code&gt;true&lt;/code&gt;), &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; waits for the write to be written to the on-disk journal of a calculated majority of voting data-bearing nodes, not merely applied in memory. That guarantees an acknowledged &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; write survives a simultaneous power loss across the primary and one secondary without rolling back on restart.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I have not run multi-cluster cross-AZ &lt;code&gt;r6g.2xlarge&lt;/code&gt; benchmarks personally; the latency and failover properties above are derived directly from MongoDB’s documented replication, write-concern, and election mechanics.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;gp3&lt;/code&gt; baseline IOPS saturation&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Heavy checkpoint flushes exceed &lt;code&gt;gp3&lt;/code&gt; baseline (3,000 IOPS / 125 MiB/s), stalling WiredTiger eviction.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;wiredTiger.cache&lt;/code&gt; dirty bytes &gt; 20% and rising write-ticket wait counts.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Provision higher &lt;code&gt;gp3&lt;/code&gt; IOPS/throughput explicitly or use &lt;code&gt;io2&lt;/code&gt; volumes for heavy write workloads.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Network partition with &lt;code&gt;w: 1&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Isolated primary accepts &lt;code&gt;w: 1&lt;/code&gt; writes, steps down, and rolls back unreplicated writes on rejoin.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rollback files written under &lt;code&gt;dbPath/rollback/&lt;/code&gt; and &lt;code&gt;w:1&lt;/code&gt; client data loss.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Mandate &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; and &lt;code&gt;retryWrites=true&lt;/code&gt; in all transactional driver strings.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Post-failover TLS reconnect storm&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Hundreds of app pods reconnect simultaneously to the new primary, saturating CPU with TLS 1.3 handshakes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Spike in &lt;code&gt;connections.current&lt;/code&gt; and CPU saturation during elections.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Configure driver connection pool jitter/backoff and set &lt;code&gt;maxIncomingConnections: 10000&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Asymmetric priority thrashing&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Skewed priorities (&lt;code&gt;10&lt;/code&gt;, &lt;code&gt;5&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt;) trigger repeated &lt;code&gt;priorityTakeover&lt;/code&gt; elections on flapping links.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Frequent &lt;code&gt;electionReason: priorityTakeover&lt;/code&gt; entries in &lt;code&gt;mongod.log&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce &lt;code&gt;priority: 1&lt;/code&gt; across all three voting members in &lt;code&gt;rs.conf()&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Direct &lt;code&gt;requireTLS&lt;/code&gt; cutover on live plaintext&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enabling &lt;code&gt;requireTLS&lt;/code&gt; on one node before drivers or peers use TLS breaks intra-cluster replication.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;network error&lt;/code&gt; / TLS handshake failures in &lt;code&gt;mongod.log&lt;/code&gt; during rolling restart.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Stage &lt;code&gt;preferTLS&lt;/code&gt; across all nodes and drivers first, then roll &lt;code&gt;requireTLS&lt;/code&gt; + &lt;code&gt;disabledProtocols&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-17-production-architecture-6-failures.svg&quot; alt=&quot;Failure-path overlay showing w:1 split-brain rollback, TLS reconnect storms, priority takeover thrashing, and gp3 IOPS saturation&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Never deploy a 3-node replica set across 2 AZs:&lt;/strong&gt; A &lt;code&gt;2 + 1&lt;/code&gt; layout puts 2 of 3 votes in one failure domain; losing that zone drops the cluster below &lt;code&gt;floor(N/2) + 1 = 2&lt;/code&gt; and forces it read-only.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Keep &lt;code&gt;priority: 1&lt;/code&gt; symmetric across all voting members:&lt;/strong&gt; Asymmetric weights trigger &lt;code&gt;priorityTakeover&lt;/code&gt; elections during transient network blips, turning minor jitter into repeated write pauses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Avoid PSA arbiters on write-heavy production clusters:&lt;/strong&gt; A 3-data-node PSS layout preserves both majority quorum and &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; commit-point advancement when any single node is offline.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Migrate &lt;code&gt;net.ssl&lt;/code&gt; to &lt;code&gt;net.tls&lt;/code&gt; with explicit &lt;code&gt;disabledProtocols&lt;/code&gt;:&lt;/strong&gt; &lt;code&gt;net.ssl&lt;/code&gt; was deprecated in 4.2 and removed in 8.0; enforce &lt;code&gt;mode: requireTLS&lt;/code&gt; and &lt;code&gt;disabledProtocols: TLS1_0,TLS1_1,TLS1_2&lt;/code&gt; via a staged &lt;code&gt;preferTLS&lt;/code&gt; rollout.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Budget application SLAs for cross-AZ &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; RTT:&lt;/strong&gt; Cross-zone replication adds one inter-AZ network round-trip per majority write, which is the physical cost of preventing &lt;code&gt;w: 1&lt;/code&gt; split-brain rollbacks.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Unstandardized EC2 MongoDB clusters risk total write outages in 2-AZ layouts, &lt;code&gt;priorityTakeover&lt;/code&gt; flapping from skewed weights, and TLS handshake failures from legacy &lt;code&gt;net.ssl&lt;/code&gt; settings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Standardize on an immutable 3-AZ symmetric PSS topology (&lt;code&gt;priority: 1&lt;/code&gt;, &lt;code&gt;votes: 1&lt;/code&gt; on all nodes), &lt;code&gt;gp3&lt;/code&gt; storage with provisioned IOPS, &lt;code&gt;net.tls.mode: requireTLS&lt;/code&gt; (TLS 1.3), and &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; with &lt;code&gt;retryWrites=true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run an &lt;code&gt;iptables&lt;/code&gt; port-&lt;code&gt;27017&lt;/code&gt; partition test against one secondary in staging and verify that &lt;code&gt;rs.status()&lt;/code&gt; retains 2/3 quorum and &lt;code&gt;w: &quot;majority&quot;&lt;/code&gt; writes continue committing without error.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit &lt;code&gt;rs.conf().members&lt;/code&gt; and &lt;code&gt;/etc/mongod.conf&lt;/code&gt; across your EC2 clusters this week to flag any 2-AZ placement, asymmetric priorities, or &lt;code&gt;net.ssl&lt;/code&gt; directives for rolling remediation.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>high-availability</category><category>checklist</category></item><item><title>Modernizing a Legacy MongoDB 3.x Estate: MMAPv1, WiredTiger, and the Upgrade Path Forward</title><link>https://rajivonai.com/blog/2026-05-16-modernizing-mongodb-mmapv1-to-wiredtiger/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-16-modernizing-mongodb-mmapv1-to-wiredtiger/</guid><description>How to orchestrate a rolling, zero-data-loss storage engine migration from MMAPv1 to WiredTiger on AWS EC2 to unblock MongoDB 4.2+ upgrades without planned application downtime.</description><pubDate>Fri, 07 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;When a database storage engine reaches end-of-life, the hardest engineering problem is not learning the new B-tree layout—it is migrating a live, multi-terabyte transactional replica set without dropping writes or outrunning the oplog.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; MongoDB 4.2 removed MMAPv1 completely (&lt;code&gt;mongod&lt;/code&gt; aborts at startup on MMAPv1 files), while MMAPv1’s collection-level write locks and uncompressed storage saturate EBS &lt;code&gt;gp2&lt;/code&gt; burst credits and serialize concurrent writers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Keep Feature Compatibility Version (FCV) at &lt;code&gt;4.0&lt;/code&gt;, convert secondaries one by one to WiredTiger via empty &lt;code&gt;dbPath&lt;/code&gt; initial sync on &lt;code&gt;gp3&lt;/code&gt; EBS volumes, then execute &lt;code&gt;rs.stepDown(60, 10)&lt;/code&gt; with &lt;code&gt;retryWrites=true&lt;/code&gt; and convert the retired primary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; If initial sync outlasts the primary’s oplog window, the secondary aborts with &lt;code&gt;OplogStartMissing&lt;/code&gt; (Error 120); always resize the oplog first and convert &lt;code&gt;gp2&lt;/code&gt; volumes to &lt;code&gt;gp3&lt;/code&gt; (3,000 IOPS baseline) before syncing.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-16-modernizing-mongodb-mmapv1-to-wiredtiger.svg&quot; alt=&quot;Comparison of MMAPv1 collection-level write locks against WiredTiger document-level concurrency bridged via oplog.rs replication&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;For MongoDB’s first decade, the default storage engine was MMAPv1, which mapped data files directly into process address space via &lt;code&gt;mmap()&lt;/code&gt; and delegated page caching and eviction to the Linux virtual memory manager. While simple, MMAPv1 wrote uncompressed BSON records into preallocated files, relocated documents when in-place updates exceeded padding, and serialized concurrent mutations behind collection-level write locks (introduced in MongoDB 3.0 to replace the older global lock).&lt;/p&gt;
&lt;p&gt;MongoDB introduced WiredTiger in 3.0, made it the default in 3.2, and deprecated MMAPv1 in 4.0. Many production estates on AWS EC2 remained on MMAPv1 into the 4.0 lifecycle because teams deferred multi-terabyte physical rebuilds.&lt;/p&gt;
&lt;p&gt;The hard deadline arrived in MongoDB 4.2, which removed MMAPv1 from the binary. A &lt;code&gt;mongod&lt;/code&gt; 4.2 process refuses to start if its &lt;code&gt;dbPath&lt;/code&gt; contains MMAPv1 data files. Because an in-place binary upgrade across 4.0 to 4.2 is impossible on MMAPv1 files, every node must be converted to WiredTiger on MongoDB 4.0 (with &lt;code&gt;featureCompatibilityVersion&lt;/code&gt; set to &lt;code&gt;&quot;4.0&quot;&lt;/code&gt;) before upgrading binaries to 4.2 or later.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Legacy MMAPv1 clusters fail under concurrent write load and block version upgrades along three measurable constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Collection-Level Write Lock Serialization:&lt;/strong&gt; In MMAPv1, concurrent writes to the same collection contend for an exclusive collection lock. Querying &lt;code&gt;db.serverStatus().globalLock.currentQueue.writers&lt;/code&gt; shows writer threads piling up behind the lock, saturating application connection pools and driving tail write latency upward. Scaling EC2 instances vertically (for example, from &lt;code&gt;m5.2xlarge&lt;/code&gt; to &lt;code&gt;m5.8xlarge&lt;/code&gt;) adds vCPUs that sit idle waiting on the same collection lock.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;EBS &lt;code&gt;gp2&lt;/code&gt; Burst Exhaustion (3 IOPS/GiB Baseline):&lt;/strong&gt; Because MMAPv1 stores data and indexes uncompressed, working sets consume significantly more disk blocks than WiredTiger’s Snappy-compressed collections and prefix-compressed indexes. On AWS EBS &lt;code&gt;gp2&lt;/code&gt; volumes, baseline performance scales at 3 IOPS per GiB (minimum 100 IOPS), and volumes under 1 TiB rely on burst credits to reach 3,000 IOPS. Sustained dirty-page flushing or a multi-terabyte initial sync drains the burst bucket and throttles the disk to baseline.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oplog Window Ceiling During Initial Sync:&lt;/strong&gt; A multi-terabyte initial sync clones all collections, builds all indexes, and replays concurrent oplog entries. If the sync takes longer than the primary’s oplog retention window, the oldest required oplog entry rolls over and the syncing secondary fails with &lt;code&gt;OplogStartMissing&lt;/code&gt; (Error 120).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Consider the sizing arithmetic before starting a conversion. Suppose a replica set stores 2 TB of uncompressed MMAPv1 data and sustains an oplog generation rate of 4 GB per hour during normal business hours, rising to 10 GB per hour during batch ETL jobs. At 125 MiB/s (&lt;code&gt;gp3&lt;/code&gt; baseline throughput), copying and indexing 2 TB of logical BSON data requires at least 4.5 to 6 hours under ideal conditions, and 12 to 18 hours once random index-build I/O and network serialization are accounted for. A default 50 GB oplog holds only 5 hours of peak batch writes—guaranteeing &lt;code&gt;OplogStartMissing&lt;/code&gt; if a batch job overlaps the initial sync. Sizing the oplog to 200 GB provides 20 hours of peak-rate buffer and 50 hours at steady state.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-16-modernizing-mongodb-mmapv1-to-wiredtiger-2-constraints.svg&quot; alt=&quot;Three operational constraints: MongoDB 4.2 binary removal of MMAPv1, collection-level write lock serialization, and gp2 3 IOPS/GiB baseline throttling&quot;&gt;&lt;/p&gt;
&lt;p&gt;Before touching any node, establish an empirical baseline across storage engine state, lock queue depth, oplog window, and filesystem free space:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mongo&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --eval&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;db.serverStatus().storageEngine&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mongo&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --eval&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;var q = db.serverStatus().globalLock.currentQueue; print(&apos;Total: &apos; + q.total + &apos; | Writers: &apos; + q.writers);&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mongo&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --eval&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;rs.printSecondaryReplicationInfo()&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;df&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -h&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/lib/mongodb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;How can a platform team convert a multi-terabyte MMAPv1 replica set to WiredTiger while serving live transactional traffic, without a write freeze or an &lt;code&gt;OplogStartMissing&lt;/code&gt; abort?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Teams migrating off MMAPv1 evaluate three paths. Only one preserves continuous write availability without requiring dual-write application logic.&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Migration Path&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Downtime / Risk&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;mongodump&lt;/code&gt; + &lt;code&gt;mongorestore&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Logical export and import into a new WiredTiger cluster&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Hours of write freeze or complex dual-write cutover&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Violates uptime SLAs on multi-terabyte datasets&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;In-Place Binary Upgrade&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Replace 4.0 binaries with 4.2 binaries on MMAPv1 files&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Immediate startup failure&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;mongod&lt;/code&gt; 4.2 removed MMAPv1 support and halts on startup&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rolling Secondary Replacement&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Mixed-engine 4.0 replica set + empty &lt;code&gt;dbPath&lt;/code&gt; initial sync&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero planned write downtime; 2–6s election on &lt;code&gt;rs.stepDown()&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires expanded oplog window and &lt;code&gt;gp3&lt;/code&gt; baseline IOPS&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chosen&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Logical export with &lt;code&gt;mongodump&lt;/code&gt; and &lt;code&gt;mongorestore&lt;/code&gt; produces clean WiredTiger data files, but keeping the source and target synchronized during a multi-hour dump requires either an application write freeze or custom change-stream dual-writing. In-place binary replacement is physically unsupported in MongoDB 4.2+. Rolling secondary replacement leverages MongoDB’s logical replication layer so nodes on different storage engines coexist safely inside one FCV 4.0 replica set.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-16-modernizing-mongodb-mmapv1-to-wiredtiger-3-options.svg&quot; alt=&quot;Comparison of mongodump/restore, in-place binary upgrade, and rolling secondary replacement&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;rolling-storage-engine-modernization-architecture&quot;&gt;Rolling Storage Engine Modernization Architecture&lt;/h2&gt;
&lt;p&gt;The migration relies on a core property of MongoDB replica sets: &lt;strong&gt;replication operates at the logical &lt;code&gt;oplog.rs&lt;/code&gt; layer, independent of each member’s local storage engine.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In MongoDB 4.0 with &lt;code&gt;featureCompatibilityVersion&lt;/code&gt; locked at &lt;code&gt;&quot;4.0&quot;&lt;/code&gt;, a replica set can run MMAPv1 on the primary and WiredTiger on one or more secondaries simultaneously. The primary records BSON mutations in &lt;code&gt;local.oplog.rs&lt;/code&gt;; secondaries pull those operations over the network and apply them through their local storage engine API.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-16-modernizing-mongodb-mmapv1-to-wiredtiger-4-architecture.svg&quot; alt=&quot;Mixed-engine MongoDB 4.0 replica set topology with MMAPv1 primary streaming oplog.rs to a WiredTiger secondary&quot;&gt;&lt;/p&gt;
&lt;p&gt;WiredTiger resolves MMAPv1’s concurrency and I/O bottlenecks through three architectural changes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Document-Level Concurrency Control:&lt;/strong&gt; WiredTiger uses optimistic concurrency control and hazard pointers so multiple writer threads mutate distinct documents in the same collection concurrently without blocking on a collection-level lock. If two threads attempt to modify the exact same document simultaneously, WiredTiger detects a write conflict on one transaction and retries it internally—keeping unrelated document writes completely lock-free.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dedicated Internal Cache:&lt;/strong&gt; Instead of delegating eviction to the OS &lt;code&gt;mmap()&lt;/code&gt; page cache, WiredTiger manages a dedicated cache defaulting to &lt;code&gt;max(0.5 * (RAM - 1 GB), 256 MB)&lt;/code&gt;, using background eviction threads to separate database working-set management from OS filesystem caching. In this dual-cache model, WiredTiger holds uncompressed, deserialized B-tree pages in its internal cache for fast query execution, while the Linux page cache holds compressed disk blocks read from EBS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Block and Prefix Compression:&lt;/strong&gt; WiredTiger compresses collection blocks (Snappy by default, or zstd/zlib) and applies prefix compression to B-tree indexes, substantially reducing on-disk footprint and EBS write amplification compared to uncompressed MMAPv1 files. Because MMAPv1 preallocates data files in power-of-two increments up to 2 GB per file and leaves fragmented free lists after deletes, converting via initial sync also compacts dead space into fresh, contiguous WiredTiger &lt;code&gt;.wt&lt;/code&gt; tables.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Target &lt;code&gt;mongod.conf&lt;/code&gt; configuration for converted nodes:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;storage&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  dbPath&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/var/lib/mongodb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  engine&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;wiredTiger&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  wiredTiger&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    engineConfig&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      cacheSizeGB&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;  # Explicitly bounded at ~50% of (RAM - 1 GB)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    collectionConfig&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      blockCompressor&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;snappy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    indexConfig&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      prefixCompression&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Execute the conversion in four gated phases so voting quorum is preserved and rollback remains instant until the final cleanup.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-16-modernizing-mongodb-mmapv1-to-wiredtiger-5-flow.svg&quot; alt=&quot;Four-phase rolling conversion runbook with pre-flight, secondary retirement, initial sync polling, and primary stepdown&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;phase-1-pre-flight-safety-verification&quot;&gt;Phase 1: Pre-Flight Safety Verification&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Confirm Quorum and Health:&lt;/strong&gt; Verify all members report state &lt;code&gt;1&lt;/code&gt; (&lt;code&gt;PRIMARY&lt;/code&gt;) or &lt;code&gt;2&lt;/code&gt; (&lt;code&gt;SECONDARY&lt;/code&gt;) and that the set has an odd number of voting members.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lock Feature Compatibility Version:&lt;/strong&gt; Confirm FCV is &lt;code&gt;&quot;4.0&quot;&lt;/code&gt; (&lt;code&gt;db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 })&lt;/code&gt;). Never bump FCV to &lt;code&gt;&quot;4.2&quot;&lt;/code&gt; while any MMAPv1 member remains.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Expand Oplog Window:&lt;/strong&gt; Inspect &lt;code&gt;rs.printReplicationInfo()&lt;/code&gt;. If the oplog window is smaller than 3x the expected initial sync duration, resize it online (value in MB):
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;adminCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ replSetResizeOplog: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, size: &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Double&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;51200&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) }); &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// 50 GB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Upgrade Storage &amp;#x26; Snapshot:&lt;/strong&gt; Modify target &lt;code&gt;gp2&lt;/code&gt; EBS volumes to &lt;code&gt;gp3&lt;/code&gt; (delivering a consistent 3,000 IOPS and 125 MiB/s baseline independent of volume size, with higher provisioned IOPS if needed) and take EBS snapshots across the set.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;em&gt;Validation gate:&lt;/em&gt; Confirm snapshot completion and verify &lt;code&gt;rs.printReplicationInfo()&lt;/code&gt; shows sufficient oplog hours. &lt;em&gt;Rollback:&lt;/em&gt; None required; no node configuration has changed.&lt;/p&gt;
&lt;h3 id=&quot;phase-2-non-destructive-secondary-retirement&quot;&gt;Phase 2: Non-Destructive Secondary Retirement&lt;/h3&gt;
&lt;p&gt;Target one secondary (&lt;code&gt;Node C&lt;/code&gt;) while the remaining secondary (&lt;code&gt;Node B&lt;/code&gt;) preserves voting quorum with the primary (&lt;code&gt;Node A&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mongo&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --eval&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;db.adminCommand({ shutdown: 1 })&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; systemctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; stop&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; mongod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DB_PATH&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;grep&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; dbPath&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /etc/mongod.conf&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; awk&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;{print $2}&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; mv&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ${DB_PATH} ${DB_PATH}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.mmapv1.bak&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; mkdir&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ${DB_PATH}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; chown&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -R&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; mongodb:mongodb&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ${DB_PATH}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Moving &lt;code&gt;dbPath&lt;/code&gt; to &lt;code&gt;.mmapv1.bak&lt;/code&gt; rather than running &lt;code&gt;rm -rf&lt;/code&gt; preserves an instant local rollback path if the host fails to sync or hits configuration errors. Update &lt;code&gt;/etc/mongod.conf&lt;/code&gt; to &lt;code&gt;engine: wiredTiger&lt;/code&gt; and start &lt;code&gt;mongod&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;phase-3-initial-sync-polling&quot;&gt;Phase 3: Initial Sync Polling&lt;/h3&gt;
&lt;p&gt;Because &lt;code&gt;dbPath&lt;/code&gt; is empty, &lt;code&gt;mongod&lt;/code&gt; enters &lt;code&gt;STARTUP2&lt;/code&gt; and initiates a full initial sync from the primary. Monitor structured progress via &lt;code&gt;db.adminCommand({ replSetGetStatus: 1 }).initialSyncStatus&lt;/code&gt; (&lt;code&gt;appliedOps&lt;/code&gt;, &lt;code&gt;bytesCopied&lt;/code&gt;, &lt;code&gt;totalInitialSyncTime&lt;/code&gt;). Wait until the node transitions through &lt;code&gt;RECOVERING&lt;/code&gt; to &lt;code&gt;SECONDARY&lt;/code&gt; and &lt;code&gt;rs.printSecondaryReplicationInfo()&lt;/code&gt; reports zero lag, then repeat Phase 2 and Phase 3 for &lt;code&gt;Node B&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For fleets with dozens of clusters, automate Phase 2 and Phase 3 in Ansible using &lt;code&gt;community.mongodb.mongodb_status&lt;/code&gt; (&lt;code&gt;poll&lt;/code&gt; sets max queries, &lt;code&gt;interval&lt;/code&gt; sets seconds between polls):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Illustrative: adapt the `until` condition to the fields your community.mongodb collection version returns&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;- &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Wait for secondary to complete initial sync&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  community.mongodb.mongodb_status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    login_host&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;localhost&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    poll&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;360&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    interval&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  register&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;node_status&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  until&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;node_status.state_str == &quot;SECONDARY&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Rollback:&lt;/em&gt; If a secondary fails mid-sync, stop &lt;code&gt;mongod&lt;/code&gt;, restore &lt;code&gt;${DB_PATH}.mmapv1.bak&lt;/code&gt; to &lt;code&gt;${DB_PATH}&lt;/code&gt;, revert &lt;code&gt;mongod.conf&lt;/code&gt;, and restart.&lt;/p&gt;
&lt;h3 id=&quot;phase-4-controlled-primary-stepdown-and-final-conversion&quot;&gt;Phase 4: Controlled Primary Stepdown and Final Conversion&lt;/h3&gt;
&lt;p&gt;Once all secondaries run WiredTiger with zero replication lag:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Audit Driver Retryability:&lt;/strong&gt; Confirm all application connection strings include &lt;code&gt;retryWrites=true&lt;/code&gt; so drivers automatically buffer and replay transient write errors during the 2-to-6-second election window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Step Down the Primary:&lt;/strong&gt; Run &lt;code&gt;rs.stepDown(60, 10)&lt;/code&gt; on &lt;code&gt;Node A&lt;/code&gt; to force a clean election to a WiredTiger secondary. The first argument (&lt;code&gt;60&lt;/code&gt;) prevents &lt;code&gt;Node A&lt;/code&gt; from standing for re-election for 60 seconds; the second (&lt;code&gt;10&lt;/code&gt;) instructs &lt;code&gt;Node A&lt;/code&gt; to wait up to 10 seconds for an eligible secondary to catch up to the latest optime before relinquishing primary status, preventing unnecessary rollbacks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Convert Retired &lt;code&gt;Node A&lt;/code&gt;:&lt;/strong&gt; Shut down &lt;code&gt;Node A&lt;/code&gt;, move its MMAPv1 directory to &lt;code&gt;.mmapv1.bak&lt;/code&gt;, switch &lt;code&gt;mongod.conf&lt;/code&gt; to WiredTiger, and let it initial-sync from the new WiredTiger primary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Verify Fleet State:&lt;/strong&gt; Run &lt;code&gt;db.serverStatus().storageEngine.name&lt;/code&gt; on every member to confirm &lt;code&gt;wiredTiger&lt;/code&gt;, verify &lt;code&gt;globalLock.currentQueue.writers&lt;/code&gt; drops under load, and retain &lt;code&gt;.mmapv1.bak&lt;/code&gt; directories for 48 hours before reclaiming disk.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The operational mechanics of this migration are governed by documented MongoDB and AWS EBS behavior:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;MongoDB Initial Sync &amp;#x26; Oplog Mechanics:&lt;/strong&gt; MongoDB’s initial sync documentation specifies that a syncing node clones all databases, builds indexes, and then applies oplog entries recorded since the clone began. If the primary’s capped &lt;code&gt;oplog.rs&lt;/code&gt; collection overwrites the starting entry before cloning finishes, the secondary aborts with &lt;code&gt;OplogStartMissing&lt;/code&gt; (Error 120) and must restart from scratch.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AWS EBS &lt;code&gt;gp2&lt;/code&gt; vs. &lt;code&gt;gp3&lt;/code&gt; Throughput Behavior:&lt;/strong&gt; AWS EBS documentation defines &lt;code&gt;gp2&lt;/code&gt; baseline performance at 3 IOPS per GiB (100 IOPS floor), relying on finite burst credits to reach 3,000 IOPS on volumes under 1 TiB. &lt;code&gt;gp3&lt;/code&gt; decouples IOPS from capacity and provides 3,000 IOPS and 125 MiB/s continuously without burst credits, preventing mid-sync I/O throttling.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;WiredTiger Eviction &amp;#x26; History Store Evolution:&lt;/strong&gt; In modern MongoDB releases (7.0 and 8.0), WiredTiger continues to rely on explicit cache eviction thresholds (&lt;code&gt;eviction_target&lt;/code&gt; at 80%, &lt;code&gt;eviction_trigger&lt;/code&gt; at 95%, &lt;code&gt;eviction_dirty_target&lt;/code&gt; at 5%, &lt;code&gt;eviction_dirty_trigger&lt;/code&gt; at 20%) and externalizes old MVCC document versions into the History Store (&lt;code&gt;WiredTigerHS.wt&lt;/code&gt;, introduced in 4.4) so long-running transactions do not pin active working-set pages in memory.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I have not executed this conversion across a multi-cluster production fleet personally; the runbook sequence and &lt;code&gt;gp3&lt;/code&gt; pre-provisioning steps reflect the documented behavior of MongoDB 4.0 initial sync and AWS EBS volume classes.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Oplog rollover during initial sync&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary overwrites starting oplog entry; secondary aborts with &lt;code&gt;OplogStartMissing&lt;/code&gt; (Error 120).&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;mongod&lt;/code&gt; log &lt;code&gt;OplogStartMissing&lt;/code&gt; and &lt;code&gt;STARTUP2&lt;/code&gt; restart loop.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run &lt;code&gt;replSetResizeOplog&lt;/code&gt; before Phase 2 to guarantee &gt;3x sync duration headroom.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Premature FCV &lt;code&gt;4.2&lt;/code&gt; bump&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Mixed-engine set rejects or breaks MMAPv1 nodes that cannot process 4.2 catalog metadata.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;setFeatureCompatibilityVersion&lt;/code&gt; error or node crash on binary swap.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep FCV at &lt;code&gt;&quot;4.0&quot;&lt;/code&gt; until &lt;code&gt;serverStatus().storageEngine.name&lt;/code&gt; is &lt;code&gt;wiredTiger&lt;/code&gt; on 100% of nodes.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Linux OOM killer &lt;code&gt;SIGKILL&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;WiredTiger cache + OS page cache + monitoring agents exceed host RAM.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;dmesg&lt;/code&gt; shows &lt;code&gt;Out of memory: Killed process (mongod)&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set &lt;code&gt;cacheSizeGB&lt;/code&gt; to at most &lt;code&gt;0.5 * (RAM - 1 GB)&lt;/code&gt; and leave headroom for OS and agents.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;EBS &lt;code&gt;gp2&lt;/code&gt; burst credit exhaustion&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Parallel index builds drain burst bucket; disk throttles to 3 IOPS/GiB and lag grows.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;CloudWatch &lt;code&gt;VolumeBurstBalance&lt;/code&gt; hits 0; node stalls in &lt;code&gt;RECOVERING&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Migrate volumes live from &lt;code&gt;gp2&lt;/code&gt; to &lt;code&gt;gp3&lt;/code&gt; (3,000+ IOPS) before starting Phase 2.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Missing &lt;code&gt;retryWrites=true&lt;/code&gt; on stepdown&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Application drivers fail writes during the 2–6s election window after &lt;code&gt;rs.stepDown()&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Client &lt;code&gt;NotWritablePrimary&lt;/code&gt; / &lt;code&gt;NotMaster&lt;/code&gt; exceptions in app logs.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Audit all connection strings for &lt;code&gt;retryWrites=true&lt;/code&gt; before executing Phase 4.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-16-modernizing-mongodb-mmapv1-to-wiredtiger-6-failures.svg&quot; alt=&quot;Failure-path overlay showing OplogStartMissing Error 120, Linux OOM killer, gp2 throttling, and stepdown write errors&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;MMAPv1 must be retired on MongoDB 4.0:&lt;/strong&gt; Because MongoDB 4.2 removed MMAPv1 from the binary, every node must convert to WiredTiger via FCV &lt;code&gt;4.0&lt;/code&gt; mixed-engine replication before any 4.2+ binary upgrade.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Initial sync is an I/O and oplog race, not a CPU problem:&lt;/strong&gt; Provision &lt;code&gt;gp3&lt;/code&gt; (or &lt;code&gt;io2&lt;/code&gt;) EBS volumes and expand &lt;code&gt;oplog.rs&lt;/code&gt; with &lt;code&gt;replSetResizeOplog&lt;/code&gt; before converting the first secondary, or &lt;code&gt;OplogStartMissing&lt;/code&gt; (Error 120) will reset multi-terabyte syncs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Preserve &lt;code&gt;.mmapv1.bak&lt;/code&gt; directories for 48 hours:&lt;/strong&gt; Moving &lt;code&gt;dbPath&lt;/code&gt; instead of deleting it gives operators an instant local rollback path if a converted node fails validation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bound &lt;code&gt;cacheSizeGB&lt;/code&gt; explicitly on shared hosts:&lt;/strong&gt; WiredTiger’s default &lt;code&gt;max(0.5 * (RAM - 1 GB), 256 MB)&lt;/code&gt; assumes a dedicated host; account for OS page cache and monitoring agents to avoid OOM kills.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Verify &lt;code&gt;retryWrites=true&lt;/code&gt; before &lt;code&gt;rs.stepDown()&lt;/code&gt;:&lt;/strong&gt; Driver-level retryable writes absorb the 2-to-6-second primary election window without surfacing write errors to callers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; MMAPv1 collection-level write locks serialize concurrent writers, uncompressed files exhaust &lt;code&gt;gp2&lt;/code&gt; burst credits, and MongoDB 4.2+ binaries refuse to start on MMAPv1 data files.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Convert replica sets online on FCV &lt;code&gt;4.0&lt;/code&gt; by migrating secondaries one by one via empty &lt;code&gt;dbPath&lt;/code&gt; initial sync onto &lt;code&gt;gp3&lt;/code&gt; EBS volumes, followed by &lt;code&gt;rs.stepDown(60, 10)&lt;/code&gt; and primary conversion.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Confirm &lt;code&gt;db.serverStatus().storageEngine.name&lt;/code&gt; returns &lt;code&gt;&quot;wiredTiger&quot;&lt;/code&gt; on all members, &lt;code&gt;globalLock.currentQueue.writers&lt;/code&gt; drops under load, and disk usage shrinks via Snappy compression.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run &lt;code&gt;db.serverStatus().storageEngine&lt;/code&gt; and &lt;code&gt;rs.printReplicationInfo()&lt;/code&gt; across your legacy MongoDB 4.0 clusters today, convert any &lt;code&gt;gp2&lt;/code&gt; data volumes to &lt;code&gt;gp3&lt;/code&gt;, and test the &lt;code&gt;.mmapv1.bak&lt;/code&gt; secondary-swap runbook in staging this week.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>Scaling PostgreSQL Reads with AlloyDB: Read Pools, Columnar Execution, and TQF</title><link>https://rajivonai.com/blog/2026-08-06-scaling-postgresql-reads-with-alloydb/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-08-06-scaling-postgresql-reads-with-alloydb/</guid><description>A current guide to AlloyDB read pools, the columnar engine, Preview autoscaling, and Preview Transparent Query Forwarding.</description><pubDate>Thu, 06 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Read scaling fails when “add replicas” is treated as one mechanism. Capacity, routing, consistency, query acceleration, and replica lifecycle are separate decisions—even when one service exposes them behind a single cluster.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A PostgreSQL application mixes short transactions with expensive scans and aggregates. Traditional physical replicas can isolate reads, but the application must route traffic, accept asynchronous visibility, and manage recovery conflicts. AlloyDB offers read pool instances attached to distributed storage, an optional columnar engine, and newer routing and autoscaling features.&lt;/p&gt;
&lt;p&gt;This article is dated after July 23, 2026 because Google introduced Transparent Query Forwarding in Preview on that date. As of September 26, 2026, both Transparent Query Forwarding and read-pool autoscaling must still be treated as Preview capabilities and revalidated before production use.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The original version of this architecture incorrectly placed Transparent Query Forwarding in front of the cluster as a general proxy. Google documents a different behavior: the &lt;strong&gt;primary instance&lt;/strong&gt; intercepts eligible read-only queries and can forward them to an in-region read pool while preserving read-your-writes consistency. Ineligible queries fall back to the primary.&lt;/p&gt;
&lt;p&gt;That distinction changes capacity planning. Enabling TQF does not mean all reads leave the primary, and it does not route writes. The core question is: &lt;strong&gt;which reads should use an explicit read-pool endpoint, which are eligible for Preview forwarding, and which should remain on the primary?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-alloydb-read-path&quot;&gt;The AlloyDB Read Path&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[Application] --&gt; Primary[Primary instance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App --&gt; Reader[Explicit read-pool endpoint]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt; Eligible{TQF eligible read}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Eligible --&gt;|Forwarded| ReadPool[In-region read pool]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Eligible --&gt;|Fallback| Fallback[Execute on primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Reader --&gt; ReadPool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt; Storage[Distributed storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ReadPool --&gt; Storage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ReadPool --&gt; Columnar[Optional columnar engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Read pools contain one or more read-only nodes behind a load-balanced endpoint. They use the cluster’s distributed storage rather than maintaining independent storage volumes in the same way as conventional physical PostgreSQL replicas. Nodes still have local memory and caches, so adding capacity can introduce cold-cache behavior.&lt;/p&gt;
&lt;p&gt;The columnar engine is independent of routing. It can populate selected columns and accelerate eligible scans, joins, and aggregates on the primary or read-pool instances. A query reaching a read pool does not guarantee columnar execution; verify its plan and column coverage.&lt;/p&gt;
&lt;h3 id=&quot;transparent-query-forwarding-boundaries&quot;&gt;Transparent Query Forwarding boundaries&lt;/h3&gt;
&lt;p&gt;Current documentation requires PostgreSQL 17 or 18, at least one active read pool, and explicit enablement. TQF applies only to eligible read-only &lt;code&gt;SELECT&lt;/code&gt; statements. Queries using row locks, temporary or unlogged tables, catalogs, volatile or user-defined functions, some transaction patterns, or cheap index scans can be disqualified. It does not forward to a secondary cluster.&lt;/p&gt;
&lt;p&gt;Enable it deliberately for a database’s new connections:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; logistics&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; alloydb&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;enable_query_forwarding&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use &lt;code&gt;EXPLAIN&lt;/code&gt; to see the query-forwarding eligibility annotation. Monitor the documented TQF metric by status—completed, fallback, and disqualified—so capacity estimates use the observed forwarding ratio rather than the total read count.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Use three routing classes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Primary-bound:&lt;/strong&gt; writes, lock-taking reads, latency-sensitive point lookups, and transactions that depend on primary session state.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Explicit reader:&lt;/strong&gt; reporting services that can connect directly to the read-pool endpoint and have a defined consistency contract.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TQF candidate:&lt;/strong&gt; expensive eligible reads from an application that cannot yet split endpoints and requires read-your-writes behavior.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For each class, record latency, primary CPU, read-pool CPU, storage reads, columnar execution, and result-visibility requirements.&lt;/p&gt;
&lt;p&gt;Read-pool autoscaling, currently Preview, adjusts node count within configured bounds based on documented signals and policies. It is not instantaneous infinite capacity. New nodes need provisioning and cache warm-up; minimum nodes determine steady cost and immediate headroom. Exercise a burst larger than normal traffic and observe queueing before relying on the policy.&lt;/p&gt;
&lt;p&gt;Roll out Preview routing in observable stages. First enable TQF for one database or controlled connection pool, then compare completed, fallback, and disqualified counts with the query inventory. Set a primary-CPU rollback threshold and verify that disabling the database setting affects new connections as documented. Finally, test a read-pool outage and a primary failover: fallback behavior may preserve correctness while still violating latency objectives if the primary lacks headroom for the returned reads.&lt;/p&gt;
&lt;p&gt;Keep the explicit reader endpoint as an architectural option even if TQF performs well. It gives reporting services a visible capacity boundary, isolates connection budgets, and avoids making a Preview eligibility decision part of every query’s latency path.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Cause&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Primary remains saturated after TQF&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Most reads are ineligible or too cheap to forward&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use TQF status metrics and explicit reader endpoints where possible.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Read pool scales but latency rises&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;New nodes start with cold caches&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Maintain headroom and ramp traffic while monitoring tail latency.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Reporting query uses row execution&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Required columns or operations are not covered&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Inspect plans and column-store recommendations.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Preview behavior changes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pre-GA terms permit limited support and change&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Gate rollout, pin assumptions in the ADR, and reverify before launch.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Read pool becomes a correctness bug&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Application never defined visibility semantics&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep correctness-sensitive paths primary-bound and test read-after-write.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Read scaling is being treated as a replica-count setting rather than routing, execution, and consistency design.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Classify reads, use explicit endpoints first, and adopt TQF or autoscaling only with Preview governance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Google documents the primary-side forwarding decision, strict eligibility rules, status metrics, and Preview release stage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Replay a production trace, calculate the observed eligible-forwarding ratio, and load-test scale-out before changing application routing.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/transparent-query-forwarding&quot;&gt;Transparent Query Forwarding&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/release-notes&quot;&gt;AlloyDB release notes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/instance-read-pool-create&quot;&gt;Create and configure a read pool instance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/instance-read-pool-scale&quot;&gt;Read-pool autoscaling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/columnar-engine/about&quot;&gt;Columnar engine overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/monitor-instance&quot;&gt;AlloyDB monitoring metrics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>cloud</category><category>architecture</category><category>databases</category><category>performance</category><category>checklist</category></item><item><title>Global Oracle Fleet Migration: Building a Control Plane Above Zero Downtime Migration</title><link>https://rajivonai.com/blog/2026-05-05-zero-downtime-migration-zdm-physical-logical/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-05-zero-downtime-migration-zdm-physical-logical/</guid><description>Oracle ZDM is an excellent single-database migration orchestrator. At fleet scale — evacuating 500 databases from a deprecated region — it needs a distributed control plane above it to manage concurrency, infrastructure co-provisioning, global state, and automated cutover through the application proxy layer.</description><pubDate>Wed, 05 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Evacuating 500 Oracle databases across regions turns &lt;code&gt;zdmcli&lt;/code&gt; from a single-database migration tool into an uncoordinated bandwidth and DNS hazard unless a Temporal control plane governs concurrency, infrastructure state, and proxy cutover above it.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Oracle Zero Downtime Migration (ZDM) orchestrates individual Physical (RMAN + Data Guard) and Logical (Data Pump + GoldenGate) moves reliably, but has no awareness of aggregate FastConnect bandwidth, KMS/DNS/OEM dependencies, or fleet-wide abort.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Wrap &lt;code&gt;$ZDM_HOME/bin/zdmcli&lt;/code&gt; in a Temporal control plane that gates RMAN waves on &lt;code&gt;oci_fastconnect&lt;/code&gt; utilization (&lt;code&gt;&amp;#x3C;70%&lt;/code&gt;), verifies OCI Vault TDE key access beforehand, and swaps traffic atomically through Envoy xDS without application config changes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Application JDBC pools without a bounded &lt;code&gt;maxLifetime&lt;/code&gt; hold open TCP connections to the demoted primary after cutover and fail with &lt;code&gt;ORA-16000&lt;/code&gt; until those connections drain.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-05-zero-downtime-migration-zdm-physical-logical.svg&quot; alt=&quot;Govern ZDM bandwidth and cutover from a Temporal control plane&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Oracle Zero Downtime Migration (ZDM) is Oracle’s Maximum Availability Architecture (MAA) tool for moving databases to OCI, Exadata, Oracle Database@Azure/AWS/Google Cloud, or between on-premises hosts. It operates in two distinct modes: &lt;strong&gt;Physical&lt;/strong&gt; migration (using RMAN backup/restore or active duplication plus Data Guard switchover for same-OS, same-endianness targets) and &lt;strong&gt;Logical&lt;/strong&gt; migration (using Data Pump for initial load plus Oracle GoldenGate Microservices for continuous replication across different OS/endianness platforms or into Oracle Autonomous Database).&lt;/p&gt;
&lt;p&gt;For a DBA migrating 5 or 10 databases per quarter via &lt;code&gt;$ZDM_HOME/bin/zdmcli&lt;/code&gt;, ZDM’s per-job lifecycle (&lt;code&gt;migrate database&lt;/code&gt;, &lt;code&gt;query job -jobid&lt;/code&gt;, &lt;code&gt;abort job&lt;/code&gt;, &lt;code&gt;resume job&lt;/code&gt;) is sufficient. When an enterprise must evacuate 500 databases—from 200 GB OLTP systems to 12 TB warehouses—out of a retiring region, ZDM’s scope ends at the database boundary. It does not coordinate aggregate cross-region network bandwidth, cross-region TDE KMS key replication, OEM target updates, or application routing cutovers.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Running hundreds of ZDM jobs concurrently without a fleet control plane fails under four explicit constraints.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. Cross-region FastConnect bandwidth math.&lt;/strong&gt; Consider a &lt;code&gt;40 Gbps&lt;/code&gt; cross-region FastConnect link (&lt;code&gt;5.0 GB/s&lt;/code&gt; theoretical ceiling, or roughly &lt;code&gt;3.5 GB/s&lt;/code&gt; safe usable capacity once you reserve headroom for live production Data Guard redo transport and application traffic). If 100 concurrent Physical ZDM jobs each open &lt;code&gt;4&lt;/code&gt; RMAN channels pulling &lt;code&gt;150 MB/s&lt;/code&gt; per channel, aggregate demand reaches &lt;code&gt;100 * 4 * 150 = 60,000 MB/s&lt;/code&gt; (&lt;code&gt;60 GB/s&lt;/code&gt;)—more than 17x the safe link budget. Under congestion, TCP retransmits stall RMAN duplication and starve Data Guard redo transport for unrelated production databases sharing that interconnect.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Physical vs. Logical mode selection.&lt;/strong&gt; Physical ZDM minimizes cutover downtime via Data Guard switchover, but requires identical OS and endian format and cannot target Autonomous Database Serverless. Logical ZDM (Data Pump + GoldenGate) handles cross-platform moves and Autonomous Database targets, but introduces GoldenGate Microservices extract/replicat lag monitoring and object-level exclusions. Choosing the wrong mode late in a wave forces a full re-instantiation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. Ancillary infrastructure drift.&lt;/strong&gt; ZDM moves the database, not the surrounding platform. If the target region lacks access to the OCI Vault master encryption key protecting the TDE wallet, RMAN restore fails or tablespaces refuse to open with &lt;code&gt;ORA-28374&lt;/code&gt;. Similarly, if DNS or proxy endpoints update before OEM monitoring targets and RMAN backup catalogs are re-pointed, the newly promoted primary runs blind without alerts or backups.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. No fleet-level abort or coordinated rollback.&lt;/strong&gt; Each &lt;code&gt;zdmcli&lt;/code&gt; job is independent. If an optimizer regression or storage defect appears after 50 cutovers while 450 jobs are in flight, ZDM provides no single command to halt remaining RMAN waves, cancel active jobs, and reverse completed Data Guard switchovers.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-05-zero-downtime-migration-zdm-physical-logical-2-constraints.svg&quot; alt=&quot;Unbounded RMAN streams starve production Data Guard redo&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do you evacuate 500 Oracle databases across regions using Physical and Logical ZDM while capping aggregate FastConnect load, co-orchestrating KMS/OEM/proxy cutovers, and preserving a single fleet-wide abort path?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Approach&lt;/th&gt;&lt;th&gt;Max Safe Concurrency&lt;/th&gt;&lt;th&gt;Durable State&lt;/th&gt;&lt;th&gt;Bandwidth Governance&lt;/th&gt;&lt;th&gt;Why Chosen or Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Manual &lt;code&gt;zdmcli&lt;/code&gt; on jump host&lt;/td&gt;&lt;td&gt;10–15 jobs&lt;/td&gt;&lt;td&gt;Local ZDM service DB only&lt;/td&gt;&lt;td&gt;Manual channel tuning&lt;/td&gt;&lt;td&gt;Rejected — saturates DBA capacity and FastConnect links&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Ansible + &lt;code&gt;zdmcli&lt;/code&gt; wrappers&lt;/td&gt;&lt;td&gt;50–100 jobs&lt;/td&gt;&lt;td&gt;Weak across multi-week sync&lt;/td&gt;&lt;td&gt;Static inventory batches&lt;/td&gt;&lt;td&gt;Rejected — loses 30-day soak state on controller restart&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Custom parallel SSH scripts&lt;/td&gt;&lt;td&gt;Unbounded (fragile)&lt;/td&gt;&lt;td&gt;Flat logs on NFS/S3&lt;/td&gt;&lt;td&gt;None&lt;/td&gt;&lt;td&gt;Rejected — no retry idempotency or coordinated rollback&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Temporal Control Plane + ZDM&lt;/td&gt;&lt;td&gt;Rate-limited by policy&lt;/td&gt;&lt;td&gt;Durable workflow history&lt;/td&gt;&lt;td&gt;Live &lt;code&gt;oci_fastconnect&lt;/code&gt; gate&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Chosen&lt;/strong&gt; — enforces link caps, KMS checks, and xDS cutover&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-05-zero-downtime-migration-zdm-physical-logical-3-options.svg&quot; alt=&quot;Pair Physical and Logical ZDM modes under one control plane&quot;&gt;&lt;/p&gt;
&lt;p&gt;ZDM Physical and ZDM Logical are both retained inside the chosen design because no single replication engine fits every target: Physical handles same-endian Exadata/VM moves at block speed, while Logical handles cross-platform and Autonomous Database targets. Above both modes, Temporal replaces Ansible and SSH scripts because regional evacuations span weeks between initial RMAN/Data Pump load and final cutover; durable workflow state survives control-plane restarts without losing which databases are in sync, soaked, or rolled back.&lt;/p&gt;
&lt;h2 id=&quot;the-migration-control-plane-architecture&quot;&gt;The Migration Control Plane Architecture&lt;/h2&gt;
&lt;p&gt;The control plane keeps &lt;code&gt;$ZDM_HOME/bin/zdmcli&lt;/code&gt; on an Active-Passive HA ZDM Service Host pair as the database replication engine, while Temporal orchestrates everything outside ZDM: FastConnect admission control, OCI Vault key verification, Envoy xDS endpoint cutover, and OEM/RMAN reconfiguration.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-05-zero-downtime-migration-zdm-physical-logical-4-architecture.svg&quot; alt=&quot;Temporal coordinates ZDM hosts, OCI Vault, and Envoy xDS&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Fleet Catalog — 500 DBs classified Physical or Logical] --&gt; B[Temporal Control Plane — durable orchestrator]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[Bandwidth Gate — oci_fastconnect BitsReceived below 70pct]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[Phase 1 — Verify OCI Vault KMS key and target infra]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[Phase 2 — zdmcli migrate database RMAN or Data Pump]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[Phase 3 — Poll zdmcli query job and lag metrics]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Phase 4 — Pre-cutover app pool TTL check]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[Phase 5 — ZDM switchover and Envoy xDS endpoint swap]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[Phase 6 — 72h soak on V$DATAGUARD_STATS]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[Phase 7 — Retire source backups and instance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; K[Global Abort Signal] --&gt; L[zdmcli abort job and reverse switchover]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;FastConnect bandwidth-aware admission gate.&lt;/strong&gt; Before Temporal starts a heavy RMAN active duplication or Data Pump network transfer, an activity queries OCI Monitoring’s &lt;code&gt;oci_fastconnect&lt;/code&gt; namespace (&lt;code&gt;BitsReceived&lt;/code&gt; metric) and counts active bulk-copy workflows. New Phase 2 transfers start only when measured link utilization is below &lt;code&gt;70%&lt;/code&gt; and active bulk-copy jobs are below &lt;code&gt;30&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime, timedelta, timezone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oci&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; get_fastconnect_utilization_pct&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    compartment_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, link_capacity_bps: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 40_000_000_000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    signer &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oci.auth.signers.InstancePrincipalsSecurityTokenSigner()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    client &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oci.monitoring.MonitoringClient({}, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;signer&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;signer)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    now &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime.now(timezone.utc)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    resp &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.summarize_metrics_data(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        compartment_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;compartment_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        summarize_metrics_data_details&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;oci.monitoring.models.SummarizeMetricsDataDetails(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            namespace&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;oci_fastconnect&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            query&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;BitsReceived[5m].rate().percentile(0.9)&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            start_time&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;now &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; timedelta(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;minutes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            end_time&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;now,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        ),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; resp.data &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;or&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; resp.data[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;].aggregated_datapoints:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    bps &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; resp.data[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;].aggregated_datapoints[&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;].value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (bps &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; link_capacity_bps) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Programmatic &lt;code&gt;zdmcli&lt;/code&gt; wrapper with Physical and Logical response files.&lt;/strong&gt; Because ZDM executes through &lt;code&gt;$ZDM_HOME/bin/zdmcli&lt;/code&gt; (using &lt;code&gt;.oci/config&lt;/code&gt; API keys for OCI HTTPS endpoints), a lightweight host wrapper on the ZDM Service Host exposes idempotent operations to Temporal—checking for an existing active job before calling &lt;code&gt;zdmcli migrate database&lt;/code&gt; and polling &lt;code&gt;zdmcli query job -jobid&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; re&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; subprocess&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;ZDMCLI&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;/u01/app/zdm/bin/zdmcli&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; start_zdm_job&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(rsp_file: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, source_db: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, eval_only: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; False&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cmd &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;ZDMCLI&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;migrate&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;database&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-rsp&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, rsp_file, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-sourcedb&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, source_db]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; eval_only:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        cmd.append(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-eval&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    proc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; subprocess.run(cmd, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;capture_output&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;check&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;300&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    match &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; re.search(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#DBEDFF&quot;&gt;job ID &lt;/span&gt;&lt;span style=&quot;color:#85E89D;font-weight:bold&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;(\d&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#85E89D;font-weight:bold&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, proc.stdout)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; match:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        raise&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; RuntimeError&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Could not parse ZDM job ID: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;proc.stdout[:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;300&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; match.group(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; query_zdm_job&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(job_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    proc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; subprocess.run(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;ZDMCLI&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;query&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;job&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-jobid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, job_id],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        capture_output&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;check&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;60&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; proc.stdout&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Envoy xDS proxy cutover and post-switchover Data Guard check.&lt;/strong&gt; Instead of asking 80 application teams to edit JDBC TNS strings during a cutover window, applications connect through an Envoy TCP proxy cluster. Once &lt;code&gt;zdmcli&lt;/code&gt; completes switchover (Physical) or GoldenGate drain (Logical), Temporal updates the Envoy Endpoint Discovery Service (&lt;code&gt;ClusterLoadAssignment&lt;/code&gt;) to point at the target region’s SCAN VIP and closes idle upstream connections so new transactions land on the new primary immediately. During the 72-hour soak, the agent verifies reverse Data Guard sync using &lt;code&gt;SOURCE_DB_UNIQUE_NAME&lt;/code&gt; on &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; source_db_unique_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       value&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; lag_value,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       unit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;   v$dataguard_stats&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;transport lag&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;apply lag&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Physical vs. Logical response files and ZDM Service Host HA.&lt;/strong&gt; The Temporal catalog classifies each database before Phase 1 so the host wrapper generates the right ZDM response file. For &lt;strong&gt;Physical&lt;/strong&gt; migrations (same OS and endianness), the response file sets &lt;code&gt;MIGRATION_METHOD=ONLINE_PHYSICAL&lt;/code&gt; with &lt;code&gt;DATA_TRANSFER_MEDIUM=DIRECT&lt;/code&gt; (or &lt;code&gt;OSS&lt;/code&gt; for OCI Object Storage staging when direct SQL*Net connectivity between source and target is restricted). For &lt;strong&gt;Logical&lt;/strong&gt; migrations (cross-platform or targeting Oracle Autonomous Database), the response file sets &lt;code&gt;MIGRATION_METHOD=ONLINE_LOGICAL&lt;/code&gt; and points to a pre-configured Oracle GoldenGate Microservices Hub (&lt;code&gt;GOLDEN_GATE_HUB_URL&lt;/code&gt;), using Data Pump over Object Storage for the initial load before GoldenGate catches up on ongoing transactions. Because the ZDM Service Host stores job state in its local repository, deploying ZDM in an Active-Passive HA configuration with shared &lt;code&gt;/u01/app/zdm&lt;/code&gt; metadata and querying &lt;code&gt;zdmcli query job&lt;/code&gt; by source database name before submitting a new job prevents Temporal activity retries from spawning duplicate migrations after a ZDM host failover. In addition, passing &lt;code&gt;-pauseafter ZDM_CONFIGURE_DG_SRC&lt;/code&gt; (for Physical) or &lt;code&gt;-pauseafter ZDM_VALIDATE_TGT&lt;/code&gt; lets Temporal hold the migration in a synchronized standby state indefinitely—separating the multi-day RMAN/Data Pump copy phase from the 5-minute maintenance window where Envoy xDS and &lt;code&gt;zdmcli resume job&lt;/code&gt; execute cutover.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Regional evacuations run through four gated stages so every database proves infrastructure readiness, sync stability, and rollback safety before the source is retired.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-05-zero-downtime-migration-zdm-physical-logical-5-flow.svg&quot; alt=&quot;Four stages gate KMS keys, link load, and 72-hour soak&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 1: Infrastructure, KMS, and &lt;code&gt;-eval&lt;/code&gt; Pre-flight.&lt;/strong&gt; Temporal provisions target DB systems, verifies target-region OCI Vault key access (&lt;code&gt;oci kms management key get&lt;/code&gt;), confirms application JDBC pools enforce &lt;code&gt;maxLifetimeMs &amp;#x3C;= 1800000&lt;/code&gt; (30 minutes), and runs &lt;code&gt;zdmcli migrate database ... -eval&lt;/code&gt;. Gate: &lt;code&gt;-eval&lt;/code&gt; passes with zero errors and KMS key is readable from the target subnet. Rollback: tear down empty target infrastructure with zero impact on production.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 2: Bandwidth-Gated Copy and Replication Sync.&lt;/strong&gt; Temporal admits databases into &lt;code&gt;zdmcli migrate database&lt;/code&gt; only when &lt;code&gt;oci_fastconnect&lt;/code&gt; utilization is &lt;code&gt;&amp;#x3C;70%&lt;/code&gt; and active bulk transfers are &lt;code&gt;&amp;#x3C;30&lt;/code&gt;. Physical jobs instantiate a Data Guard standby via RMAN; Logical jobs run Data Pump initial load and start GoldenGate Microservices replication. Gate: &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; transport/apply lag (or GoldenGate extract/replicat lag) stays within RPO for 24 hours. Rollback: run &lt;code&gt;zdmcli abort job -jobid &amp;#x3C;id&gt;&lt;/code&gt; and drop the target standby.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 3: Switchover, Envoy xDS Swap, and Monitoring Cutover.&lt;/strong&gt; During the approved application window, Temporal pauses writes briefly (for Logical) or triggers ZDM Physical Data Guard switchover, pushes the new SCAN IP via Envoy xDS, and updates OEM agent targets and RMAN catalog entries via API. Gate: application health checks pass against the new primary and OEM reports target up. Rollback: for Physical migrations, execute Data Guard switchover back to the original primary and revert Envoy xDS; for Logical migrations, reverse GoldenGate replication before retiring the source.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 4: 72-Hour Soak and Source Decommission.&lt;/strong&gt; Keep the former primary running as a synchronized standby for 72 hours while monitoring &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; and application latency. Keeping reverse replication active for 72 hours preserves a fast switchover rollback without re-copying terabytes of data over FastConnect, while letting teams verify nightly batch jobs, reporting queries, and RMAN backups on the target region. Gate: zero sev-1/sev-2 regressions over 72 hours. Rollback: instant Data Guard switchover back to the original region until the soak expires; afterward, disable scheduled RMAN backups, deregister the catalog entry, and terminate the source instance.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;ZDM Physical vs. Logical engine boundaries (Pattern A):&lt;/strong&gt; Per Oracle ZDM documentation, Physical migration uses RMAN and Data Guard switchover for same-OS, same-endianness targets, whereas Logical migration uses Data Pump and GoldenGate Microservices for cross-platform moves and Oracle Autonomous Database targets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ZDM CLI and OCI integration (Pattern A):&lt;/strong&gt; Per Oracle ZDM Administration Guide, &lt;code&gt;$ZDM_HOME/bin/zdmcli&lt;/code&gt; (&lt;code&gt;migrate database&lt;/code&gt;, &lt;code&gt;query job&lt;/code&gt;, &lt;code&gt;abort job&lt;/code&gt;, &lt;code&gt;resume job&lt;/code&gt;) is the primary ZDM interface, communicating with OCI endpoints over HTTPS (port 443) using &lt;code&gt;.oci/config&lt;/code&gt; API keys.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;FastConnect telemetry lag (Pattern B):&lt;/strong&gt; OCI Monitoring’s &lt;code&gt;oci_fastconnect&lt;/code&gt; &lt;code&gt;BitsReceived&lt;/code&gt; metric aggregates over 5-minute windows, so an admission gate that relies solely on current utilization can over-admit jobs during a burst; pairing the &lt;code&gt;&amp;#x3C;70%&lt;/code&gt; metric gate with a hard cap of &lt;code&gt;30&lt;/code&gt; concurrent bulk-copy jobs prevents oversubscribing a 40 Gbps link.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sticky JDBC connection pools (Pattern B):&lt;/strong&gt; Updating Envoy xDS or DNS does not sever already-established TCP sockets held by application connection pools; without a bounded &lt;code&gt;maxLifetime&lt;/code&gt; (or explicit Envoy upstream socket drain on cutover), writes continue hitting the demoted standby and fail with &lt;code&gt;ORA-16000: database or pluggable database open for read-only access&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Breaks How&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Sticky pool writes (&lt;code&gt;ORA-16000&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;App JDBC pool lacks &lt;code&gt;maxLifetime&lt;/code&gt;; writes hit demoted standby&lt;/td&gt;&lt;td&gt;App error spike with &lt;code&gt;ORA-16000&lt;/code&gt; immediately after cutover&lt;/td&gt;&lt;td&gt;Require &lt;code&gt;maxLifetimeMs &amp;#x3C;= 1800000&lt;/code&gt; in Phase 1; drain Envoy connections&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;TDE key inaccessible (&lt;code&gt;ORA-28374&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;Target region lacks OCI Vault master key replication/policy&lt;/td&gt;&lt;td&gt;RMAN restore or DB open fails with &lt;code&gt;ORA-28374&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Run &lt;code&gt;oci kms management key get&lt;/code&gt; from target subnet in Phase 1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;FastConnect metric lag saturation&lt;/td&gt;&lt;td&gt;5-min &lt;code&gt;BitsReceived&lt;/code&gt; lag admits too many RMAN streams at once&lt;/td&gt;&lt;td&gt;Production Data Guard &lt;code&gt;transport lag&lt;/code&gt; spikes on unrelated DBs&lt;/td&gt;&lt;td&gt;Pair &lt;code&gt;&amp;#x3C;70%&lt;/code&gt; utilization gate with &lt;code&gt;max_concurrent=30&lt;/code&gt; and 4 channels/job&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Single-node ZDM host outage&lt;/td&gt;&lt;td&gt;Jump host reboot or patching kills &lt;code&gt;zdmcli&lt;/code&gt; service mid-wave&lt;/td&gt;&lt;td&gt;&lt;code&gt;zdmcli query job&lt;/code&gt; connection errors in Temporal activity&lt;/td&gt;&lt;td&gt;Run ZDM Service Host in Active-Passive HA; check existing jobs before submit&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Orphaned ZDM job on fleet abort&lt;/td&gt;&lt;td&gt;Temporal cancels workflows while &lt;code&gt;zdmcli&lt;/code&gt; keeps running locally&lt;/td&gt;&lt;td&gt;Fleet Catalog says &lt;code&gt;ROLLED_BACK&lt;/code&gt; while target keeps syncing&lt;/td&gt;&lt;td&gt;Global abort handler runs &lt;code&gt;zdmcli abort job -jobid&lt;/code&gt; before reverting&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OEM &amp;#x26; backup blindness post-cutover&lt;/td&gt;&lt;td&gt;OEM target and RMAN catalog still point to retired source host&lt;/td&gt;&lt;td&gt;OEM target unreachable or missed nightly RMAN backup&lt;/td&gt;&lt;td&gt;Make OEM REST API update and Level 1 test backup mandatory in Phase 3&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-05-05-zero-downtime-migration-zdm-physical-logical-6-failures.svg&quot; alt=&quot;Four failure paths break cross-region ZDM evacuations&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Choose ZDM Physical or Logical by platform boundary, not preference:&lt;/strong&gt; Physical (RMAN + Data Guard) gives the fastest block-level cutover for same-OS/same-endianness targets; Logical (Data Pump + GoldenGate) is required for cross-platform shifts and Autonomous Database.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Govern aggregate FastConnect bandwidth above ZDM:&lt;/strong&gt; ZDM tunes RMAN channels per job, not across 500 jobs; pair OCI &lt;code&gt;oci_fastconnect&lt;/code&gt; &lt;code&gt;BitsReceived&lt;/code&gt; checks (&lt;code&gt;&amp;#x3C;70%&lt;/code&gt;) with a hard concurrent-copy cap (&lt;code&gt;30&lt;/code&gt; jobs) to protect unrelated Data Guard redo transport.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Automate KMS, Envoy xDS, and OEM in the same workflow:&lt;/strong&gt; Database replication is only half the migration; TDE key access (&lt;code&gt;ORA-28374&lt;/code&gt;), connection draining (&lt;code&gt;ORA-16000&lt;/code&gt;), and OEM target updates must be gated inside the control plane.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Make ZDM job submission idempotent:&lt;/strong&gt; Query existing ZDM jobs before invoking &lt;code&gt;zdmcli migrate database&lt;/code&gt; so Temporal activity retries after ZDM host restarts never spawn duplicate migrations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hold a 72-hour reverse-sync soak before deleting sources:&lt;/strong&gt; Keep the original primary synchronized as a standby for 72 hours so any post-cutover regression can reverse via Data Guard switchover instead of a multi-terabyte restore.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Running &lt;code&gt;zdmcli&lt;/code&gt; directly across hundreds of Oracle databases saturates cross-region FastConnect links, misses KMS/DNS/OEM dependencies, and lacks a coordinated fleet abort path.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Place a Temporal control plane above an HA ZDM Service Host pair to enforce &lt;code&gt;oci_fastconnect&lt;/code&gt; bandwidth gates, verify OCI Vault keys, drive &lt;code&gt;zdmcli&lt;/code&gt;, and swap traffic through Envoy xDS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Confirm &lt;code&gt;zdmcli migrate database ... -eval&lt;/code&gt; passes cleanly on a pilot database, verify &lt;code&gt;oci kms management key get&lt;/code&gt; succeeds from the target subnet, and test &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; (&lt;code&gt;SOURCE_DB_UNIQUE_NAME&lt;/code&gt;) lag polling.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Inventory the non-database dependencies (OCI Vault KMS key policies, JDBC &lt;code&gt;maxLifetimeMs&lt;/code&gt;, Envoy/DNS routing, OEM targets, and RMAN catalog entries) for your top five critical databases before scheduling Wave 1.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Oracle Zero Downtime Migration Documentation — Introduction (Physical and Logical Migration): &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/zero-downtime-migration/21.4/zdmug/introduction-oracle-zero-downtime-migration.html&quot;&gt;https://docs.oracle.com/en/database/oracle/zero-downtime-migration/21.4/zdmug/introduction-oracle-zero-downtime-migration.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Oracle Zero Downtime Migration — &lt;code&gt;zdmcli&lt;/code&gt; Command Reference: &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/zero-downtime-migration/21.4/zdmug/zero-downtime-migration-command-reference.html&quot;&gt;https://docs.oracle.com/en/database/oracle/zero-downtime-migration/21.4/zdmug/zero-downtime-migration-command-reference.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Oracle Data Guard Concepts and Administration — Switchover and Failover: &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/dgbkr/oracle-data-guard-switchover-and-failover-procedures.html&quot;&gt;https://docs.oracle.com/en/database/oracle/oracle-database/19/dgbkr/oracle-data-guard-switchover-and-failover-procedures.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Oracle Database Reference — &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt;: &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/V-DATAGUARD_STATS.html&quot;&gt;https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/V-DATAGUARD_STATS.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OCI Monitoring Service — &lt;code&gt;oci_fastconnect&lt;/code&gt; Metrics Reference (&lt;code&gt;BitsReceived&lt;/code&gt;): &lt;a href=&quot;https://docs.oracle.com/en-us/iaas/Content/Monitoring/Reference/metics-reference.htm&quot;&gt;https://docs.oracle.com/en-us/iaas/Content/Monitoring/Reference/metics-reference.htm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Temporal Documentation — Activity Heartbeats and Durable Execution: &lt;a href=&quot;https://docs.temporal.io/activities#activity-heartbeats&quot;&gt;https://docs.temporal.io/activities#activity-heartbeats&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Envoy Proxy Documentation — Endpoint Discovery Service (EDS / xDS v3): &lt;a href=&quot;https://www.envoyproxy.io/docs/envoy/latest/api-v3/service/endpoint/v3/eds.proto&quot;&gt;https://www.envoyproxy.io/docs/envoy/latest/api-v3/service/endpoint/v3/eds.proto&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>failures</category></item><item><title>Tuning Cassandra 5.0: Unified Compaction Strategy, SAI, and Trie Memtables</title><link>https://rajivonai.com/blog/2026-08-04-cassandra-5-0-tuning-ucs-sai-trie-memtables/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-08-04-cassandra-5-0-tuning-ucs-sai-trie-memtables/</guid><description>The three Cassandra 5.0 features that need deliberate tuning rather than default settings: UCS scaling parameters, SAI index build order, and Trie memtable memory sizing.</description><pubDate>Tue, 04 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Cassandra 5.0’s headline features — Unified Compaction Strategy, Storage-Attached Indexes, and Trie memtables — all ship with defaults tuned for a generic workload, not your workload. Teams that adopt 5.0 and never touch these defaults get correct behavior, but not the performance the release was built to deliver.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A team has upgraded a cluster to Cassandra 5.0 and migrated tables from STCS or LCS to the new Unified Compaction Strategy (UCS), added a Storage-Attached Index (SAI) to support a new query pattern, and is running on Trie memtables by default. Correctness is fine — queries return the right results, compaction runs, indexes build. But nobody has gone back to actually tune any of the three for the workload’s actual shape, and the read/write amplification profile looks suspiciously similar to what STCS was already producing.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;All three of these features expose their most important tuning knob as a default that assumes nothing about your access pattern. UCS’s default &lt;code&gt;scaling_parameters&lt;/code&gt; (&lt;code&gt;T4&lt;/code&gt;) behaves like tiered compaction, optimized for write throughput over read amplification — a defensible generic default, but wrong for a read-heavy table that would benefit from leveled-style behavior instead. SAI has a build-order constraint that isn’t obvious until you hit it: building the index concurrently with a large data load is unsupported and slow, but nothing stops you from doing it. And Trie memtables reduce on-heap pressure versus the legacy skip-list memtable, but that memory profile shift interacts with G1GC heap sizing decisions made before the upgrade.&lt;/p&gt;
&lt;p&gt;The core question: which of UCS, SAI, and Trie memtables actually needs explicit tuning for this workload, versus which one is fine left alone?&lt;/p&gt;
&lt;h2 id=&quot;tuning-surface-across-the-three-features&quot;&gt;Tuning Surface Across the Three Features&lt;/h2&gt;
&lt;p&gt;Each feature has exactly one high-leverage tuning decision; the rest of their configuration surface is genuinely fine at defaults.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Upgrade[Cassandra 5.0 Upgrade] --&gt; UCS{Compaction — UCS}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Upgrade --&gt; SAI{Indexing — SAI}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Upgrade --&gt; Trie{Memtable — Trie}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    UCS --&gt; UCSWorkload{Read-Heavy or Write-Heavy?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    UCSWorkload --&gt;|Read-Heavy| Leveled[Leveled Scaling — L10, Higher Shard Count]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    UCSWorkload --&gt;|Write-Heavy or Time-Series| Tiered[Tiered Scaling — T4 or T8, Fewer Shards]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SAI --&gt; SAIOrder{Index Before or After Data Load?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SAIOrder --&gt;|Before Load| Fast[Index Builds Cleanly, No Rebuild Risk]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SAIOrder --&gt;|After Load, Concurrent| Slow[Unsupported, Slow Concurrent Build]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Trie --&gt; TrieHeap[Re-Evaluate Heap Sizing Post-Upgrade]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-ucs-match-scaling_parameters-to-the-readwrite-shape&quot;&gt;1. UCS: Match &lt;code&gt;scaling_parameters&lt;/code&gt; to the Read/Write Shape&lt;/h3&gt;
&lt;p&gt;UCS’s default configuration uses &lt;code&gt;scaling_parameters: T4&lt;/code&gt; (tiered behavior, threshold 4) with &lt;code&gt;target_sstable_size: 1GiB&lt;/code&gt; and &lt;code&gt;base_shard_count: 4&lt;/code&gt; — a write-throughput-favoring default, the same tradeoff STCS always made. For a read-heavy table, the documented alternative is a leveled-style profile: &lt;code&gt;L10&lt;/code&gt; with a smaller target SSTable size (around 256MiB) and a higher shard count (around 8), which bounds the number of SSTables a read has to consult at the cost of more write amplification. For time-series data, &lt;code&gt;T8&lt;/code&gt; with a mid-size target (around 512MiB) and higher shard count improves both write throughput and time-window expiration checks. Unlike switching between STCS and LCS, UCS can change these parameters in flight without a full strategy migration.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; lookups&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;user_profiles&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; compaction &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;class&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;UnifiedCompactionStrategy&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;scaling_parameters&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;L10&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;target_sstable_size&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;256MiB&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;base_shard_count&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-sai-build-the-index-before-loading-data&quot;&gt;2. SAI: Build the Index Before Loading Data&lt;/h3&gt;
&lt;p&gt;Cassandra’s own SAI documentation is explicit that index settings can’t be changed without dropping and rebuilding the index, and that building an index while data is concurrently loading is unsupported and measurably slower than building it first. This is the opposite order most teams assume when retrofitting an index onto an existing table.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Correct order for a new table: index first, then load&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; catalog&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.products (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    product_id uuid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    embedding vector&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;float&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;768&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    category &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CUSTOM &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INDEX&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; catalog&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;products&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (embedding)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    USING&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;StorageAttachedIndex&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Now bulk-load data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For an existing table with data already loaded, plan the index build as its own maintenance window rather than assuming it’s a background no-op.&lt;/p&gt;
&lt;h3 id=&quot;3-trie-memtables-revisit-heap-sizing-dont-assume-its-free&quot;&gt;3. Trie Memtables: Revisit Heap Sizing, Don’t Assume It’s Free&lt;/h3&gt;
&lt;p&gt;Trie memtables store data in a memory-efficient trie structure instead of the legacy skip-list, which measurably reduces on-heap pressure — this was called out as a contributing factor when tuning G1GC for a 4.1 cluster, since lower memtable heap pressure means fewer humongous allocations from wide-partition memtable flushes. But “reduces heap pressure” isn’t the same as “requires no heap tuning.” A heap sizing decision made for skip-list memtables on 4.1 should be re-validated after the 5.0 upgrade, not assumed to still be optimal — the safe direction to test is a smaller heap or a lower &lt;code&gt;InitiatingHeapOccupancyPercent&lt;/code&gt;, since Trie’s lower baseline pressure changes the humongous-allocation math that drove those settings in the first place.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Confirm which compaction strategy and scaling parameters a table is actually running before assuming the upgrade already tuned it:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;nodetool&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; compactionstats&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;nodetool&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; describering&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;keyspac&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -A&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; compaction&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Check SAI index build status before treating it as done:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;nodetool&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; sai&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;keyspac&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;tabl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Leaving every table on UCS defaults&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;T4&lt;/code&gt; favors write throughput; a read-heavy table pays unnecessary read amplification it didn’t need to accept.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set &lt;code&gt;scaling_parameters&lt;/code&gt; per table based on actual read/write ratio, not the UCS default.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Building SAI concurrently with a bulk load&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unsupported and slow — the index build competes with the load for I/O and can leave the index in an inconsistent intermediate state.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Create the index before loading data, or treat an index build on an existing table as its own maintenance window.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Assuming Trie memtables need zero heap re-tuning&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Heap settings carried over from a skip-list-memtable-era JVM configuration may now be more conservative than necessary.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Re-run heap sizing validation (IHOP, region occupancy) after the 5.0 upgrade instead of carrying forward 4.x settings unchanged.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Cassandra 5.0’s UCS, SAI, and Trie memtables all ship with generic defaults, so upgrading without re-tuning them delivers correctness but not the performance improvement the release is capable of.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Match UCS &lt;code&gt;scaling_parameters&lt;/code&gt; to each table’s actual read/write shape, build SAI indexes before loading data rather than concurrently, and re-validate JVM heap settings post-upgrade rather than carrying forward 4.x tuning.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Cassandra’s own UCS documentation states parameters can be changed in flight without a full compaction-strategy migration — there’s no excuse to leave every table on the write-favoring default if its actual traffic is read-heavy.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run &lt;code&gt;nodetool compactionstats&lt;/code&gt; against your busiest tables this week and check whether any of them are read-heavy but still running UCS’s default &lt;code&gt;T4&lt;/code&gt; tiered scaling.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>vector-db</category></item><item><title>Dual Control Plane Orchestration: Provisioning Oracle Database@Azure Across Microsoft and OCI APIs</title><link>https://rajivonai.com/blog/2026-04-10-oracle-database-at-azure-iac-terraform/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-04-10-oracle-database-at-azure-iac-terraform/</guid><description>Oracle Database@Azure requires coordinated operations across two cloud control planes that have different APIs, authentication models, asynchronous propagation delays, and failure modes. This post details the Temporal-based dual control plane state machine that handles cross-cloud provisioning without orphaned infrastructure.</description><pubDate>Mon, 03 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;When a database deployment spans two hyperscaler control planes, complexity grows combinatorially: provisioning Oracle Database@Azure requires Microsoft’s ARM API and Oracle’s OCI API to succeed in strict dependency order across asynchronous propagation delays, with no native cross-cloud transaction rollback.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Linear scripts and single-pass IaC runs fail across Azure ARM and OCI Database APIs when subnet delegation propagation lags, OIDC tokens expire mid-build, or OCI rejects capacity after Azure VNets are already billed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Orchestrate the cross-cloud sequence with a Temporal durable state machine—driving Terraform/ARM for Azure networking and OCI APIs (via &lt;code&gt;TokenExchangeSigner&lt;/code&gt;) for Exadata and CDBs—with async heartbeats and compensating cleanup.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Never use blocking &lt;code&gt;time.sleep()&lt;/code&gt; inside async Temporal activities, poll until all three SCAN IPs populate before writing Private DNS records, and strip subnet delegations before deleting orphaned subnets.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-04-10-oracle-database-at-azure-iac-terraform.svg&quot; alt=&quot;Durable state machines prevent orphaned dual-cloud builds&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Oracle Database@Azure runs OCI-managed Exadata infrastructure physically inside Microsoft Azure data centers, projecting vNICs into an Azure Delegated Subnet (&lt;code&gt;Oracle.Database/networkAttachments&lt;/code&gt;). Even with native Azure Resource Manager (&lt;code&gt;Oracle.Database&lt;/code&gt;) integration in Terraform &lt;code&gt;azurerm&lt;/code&gt; v4.x (&lt;code&gt;azurerm_oracle_exadata_infrastructure&lt;/code&gt; and &lt;code&gt;azurerm_oracle_cloud_vm_cluster&lt;/code&gt;), enterprise provisioning still spans two administrative planes: Azure ARM owns VNets, delegated subnets, Private DNS, and NSGs, while OCI owns Exadata compute/storage lifecycle, CDB/PDB creation, Data Guard, and Oracle-level operations.&lt;/p&gt;
&lt;p&gt;This split also divides quotas and billing: Microsoft Azure enforces subscription-level VNet, subnet delegation, and Private DNS quotas, while OCI enforces compartment-level Exadata infrastructure, OCPU, and storage-server limits under Oracle Universal Credits or Azure Marketplace offers. A pre-flight check that validates only Azure quotas will still fail 30 minutes later if the target OCI compartment has zero remaining Exadata compute capacity.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Provisioning Component&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Controlling Plane&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Failure &amp;#x26; Latency Surface&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Azure VNet and Delegated Subnet&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Azure Resource Manager&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;API throttling, CIDR overlap, regional delegation propagation lag&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Exadata Infrastructure&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;Oracle.Database&lt;/code&gt; ARM / OCI API&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Long-running async build, zone capacity limits, OCID generation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Exadata VM Cluster&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;Oracle.Database&lt;/code&gt; ARM / OCI API&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;SSH key format checks, GI version, delayed SCAN IP assignment&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Azure Private DNS &amp;#x26; NSGs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Azure Resource Manager&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Missing VNet DNS links, stale or empty SCAN IP list&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;CDB / PDB &amp;#x26; Backup Policy&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;OCI Database API&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Storage quota, character set mismatch, PDB limits&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The structural dependency loop crosses clouds twice: OCI infrastructure and VM cluster provisioning depend on the Azure delegated subnet reference, while Azure Private DNS A-records and firewall rules depend on the three SCAN IP addresses returned only after the VM cluster finishes provisioning.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Without a durable coordination layer, cross-cloud provisioning fails across three recurring boundaries.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 1: Asynchronous Propagation Gaps.&lt;/strong&gt; Azure’s subnet delegation API often returns HTTP 200 before delegation state finishes propagating across Azure’s regional network fabric. Firing the Exadata infrastructure or VM cluster creation call immediately after ARM returns 200 triggers intermittent “subnet not delegated” errors under regional load. Similarly, a VM cluster can report &lt;code&gt;AVAILABLE&lt;/code&gt; briefly before all three SCAN IP addresses are populated in its API response; reading &lt;code&gt;scan_ip_ids&lt;/code&gt; too early writes incomplete DNS records.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 2: Cross-Cloud Identity Bridging.&lt;/strong&gt; Automation runners hosted in Azure authenticate natively with Microsoft Entra Managed Identity, whereas OCI Database API calls require an OCI principal. Embedding static OCI API private keys inside CI/CD runners creates key-rotation debt, secret-sprawl risk, and fragmented audit trails. Bridging identities cleanly requires OIDC Workload Identity Federation—exchanging an Entra Managed Identity JWT for a short-lived OCI User Principal Session Token (UPST) via &lt;code&gt;oci.auth.signers.TokenExchangeSigner&lt;/code&gt;, while ensuring tokens refresh cleanly across 60-minute provisioning polls.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 3: Orphaned Infrastructure and Duplicate Allocation on Partial Failure.&lt;/strong&gt; If Azure VNet, subnet delegation, and NAT/DNS resources succeed, Azure billing and CIDR consumption begin immediately. If the subsequent OCI Exadata or CDB step fails due to regional capacity limits or SSH key validation errors (OCI VM clusters enforce specific OpenSSH RSA key formats and fail late if an unsupported key is passed), a linear script or crashed CI runner leaves orphaned Azure networking behind—and Azure ARM refuses to delete a delegated subnet unless the delegation block is explicitly stripped first. Conversely, if a client-side HTTP timeout occurs right after OCI accepts &lt;code&gt;CreateCloudExadataInfrastructure&lt;/code&gt;, a blind retry without an &lt;code&gt;opc_retry_token&lt;/code&gt; or persisted OCID can allocate a duplicate Exadata infrastructure rack.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-04-10-oracle-database-at-azure-iac-terraform-2-constraints.svg&quot; alt=&quot;Three constraints break naive dual-cloud pipelines&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do platform teams orchestrate Oracle Database@Azure builds across Azure ARM and OCI APIs—handling propagation delays, federated auth, and saga cleanup—without manual console intervention?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams evaluate four approaches to cross-cloud Exadata provisioning.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Dependency Handling&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Failure Cleanup&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Fleet Governance&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Verdict&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;1. Dual-Portal ClickOps&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Manual copy-paste of IDs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Manual teardown&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero auditability&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected: error-prone, unrepeatable&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;2. Linear CI/CD Bash/Python&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sequential script steps&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;None if runner crashes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Weak state tracking&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected: orphans VNets on timeout&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;3. Terraform &lt;code&gt;azurerm&lt;/code&gt; v4.x Only&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Native &lt;code&gt;azurerm_oracle_*&lt;/code&gt; graph&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;terraform destroy&lt;/code&gt; on retry&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Good for static infra; weak on CDB/sagas&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Partial: pair with orchestrator&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;4. Temporal + Terraform / SDKs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Durable state machine + polling&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Explicit saga compensation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fleet catalog + rate limits&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chosen for fleet-scale provisioning&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Option 1 (Dual-Portal ClickOps)&lt;/strong&gt; forces operators to switch between Azure Portal and OCI Console, copying subnet IDs and SCAN IPs by hand. It cannot enforce standardized tagging or repeatable CIDR checks.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 2 (Linear CI/CD Runner)&lt;/strong&gt; runs Azure CLI and OCI CLI sequentially inside a single pipeline job. Because Exadata infrastructure and VM cluster builds routinely span 60+ minutes, runner timeouts or worker restarts lose in-memory resource IDs and leave half-built environments behind.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 3 (Terraform &lt;code&gt;azurerm&lt;/code&gt; v4.x Only)&lt;/strong&gt; leverages &lt;code&gt;azurerm_oracle_exadata_infrastructure&lt;/code&gt; and &lt;code&gt;azurerm_oracle_cloud_vm_cluster&lt;/code&gt; (or &lt;code&gt;azapi_resource&lt;/code&gt; against &lt;code&gt;Oracle.Database&lt;/code&gt; when targeting newly released API properties) to keep Azure and Exadata hardware in one dependency graph. However, a single &lt;code&gt;terraform apply&lt;/code&gt; still faces multi-hour polling timeouts, does not manage OCI-side CDB/PDB and Data Guard lifecycles cleanly, and lacks compensating saga rollbacks when an upstream business gate fails mid-workflow. In addition, if an apply times out in a CI runner, Terraform state locks can remain stuck until manually force-unlocked.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 4 (Temporal State Machine + Terraform/SDKs)&lt;/strong&gt; keeps Terraform for declarative VNet/subnet modules while Temporal owns the multi-hour cross-cloud state machine, persisting every resource ID, heartbeating async polls, enforcing per-region OCI rate limits, and running compensating undelegation cleanup if provisioning aborts.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-04-10-oracle-database-at-azure-iac-terraform-3-options.svg&quot; alt=&quot;Temporal plus Terraform beats scripts and raw apply alone&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-dual-control-plane-state-machine&quot;&gt;The Dual Control Plane State Machine&lt;/h2&gt;
&lt;p&gt;The recommended architecture models each Exadata provisioning request as a durable &lt;strong&gt;Temporal Workflow&lt;/strong&gt; backed by a &lt;strong&gt;Fleet Catalog&lt;/strong&gt;. Temporal persists workflow state after every activity completion, surviving worker restarts or network blips without losing the Azure VNet ID, delegated subnet OCID, or Exadata infrastructure OCID.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-04-10-oracle-database-at-azure-iac-terraform-4-architecture.svg&quot; alt=&quot;Temporal orchestrator bridges Azure ARM and OCI APIs&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Provisioning Request — db_name, region, shape, CIDR] --&gt; B[Step 1 — Azure VNet and Subnet via ARM]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[Step 2 — Delegate Subnet to Oracle.Database/networkAttachments]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[Wait — Poll delegation provisioningState == Succeeded]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[Step 3 — Extract Delegated Subnet ID / OCID]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[Step 4 — OCI ExadataInfrastructure Provisioning]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Wait — Async Heartbeat Poll until AVAILABLE]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[Step 5 — OCI VmCluster Provisioning]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[Wait — Async Poll until AVAILABLE and 3 SCAN IPs Populated]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[Step 6 — Azure Private DNS A Records for SCAN IPs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[Step 7 — Azure NSG and Firewall Rules for TCP 1521 and 2484]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; L[Step 8 — OCI CDB Creation and Fleet Catalog AVAILABLE]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|Retry Exhausted| M[Saga Compensation — Undelegate Subnet then Delete VNet]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    M --&gt; N[Alert Platform Team — Zero Orphaned Resources]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;core-orchestration-controls&quot;&gt;Core Orchestration Controls&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Non-Blocking Async Polling with Heartbeats and &lt;code&gt;opc_retry_token&lt;/code&gt;:&lt;/strong&gt; Long-running OCI activities (&lt;code&gt;CreateCloudExadataInfrastructure&lt;/code&gt; and &lt;code&gt;CreateCloudVmCluster&lt;/code&gt;) run as &lt;code&gt;async def&lt;/code&gt; Temporal activities using &lt;code&gt;await asyncio.sleep(30)&lt;/code&gt; rather than blocking &lt;code&gt;time.sleep()&lt;/code&gt;, calling &lt;code&gt;activity.heartbeat(state)&lt;/code&gt; on every poll iteration. Passing a deterministic &lt;code&gt;opc_retry_token&lt;/code&gt; derived from the Temporal workflow ID guarantees that transient network retries never allocate a duplicate Exadata rack. If a worker pod restarts, Temporal detects the missed heartbeat and resumes polling from the persisted OCID.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Deterministic Workflow Boundaries:&lt;/strong&gt; All Azure ARM calls, OCI SDK calls, and clock checks live strictly inside &lt;code&gt;@activity.defn&lt;/code&gt; functions, keeping &lt;code&gt;@workflow.run&lt;/code&gt; deterministic so Temporal can replay event history safely after any worker crash.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Entra-to-OCI Token Exchange (&lt;code&gt;TokenExchangeSigner&lt;/code&gt;):&lt;/strong&gt; Instead of storing static OCI API keys, the automation’s Azure Managed Identity obtains an Entra OIDC token and passes it to OCI’s &lt;code&gt;TokenExchangeSigner&lt;/code&gt;, which exchanges it for a short-lived UPST token scoped to the target OCI compartment. Token refresh runs automatically inside the signer, preventing 401 failures during 60-minute builds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Global Per-Region Rate Limiting:&lt;/strong&gt; Bulk provisioning requests pass through a Temporal rate limiter or Redis token bucket capping concurrent &lt;code&gt;CreateCloudExadataInfrastructure&lt;/code&gt; calls per region so parallel workflows do not trigger OCI HTTP 429 throttling.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Undelegate-Before-Delete Compensation and Orphan Reaper:&lt;/strong&gt; When a saga rollback fires, the cleanup activity explicitly strips the &lt;code&gt;Oracle.Database/networkAttachments&lt;/code&gt; delegation from the Azure subnet and awaits completion before calling &lt;code&gt;subnets.begin_delete&lt;/code&gt; and &lt;code&gt;virtual_networks.begin_delete&lt;/code&gt;. As a defense in depth, a scheduled reconciliation job scans Fleet Catalog entries stuck in &lt;code&gt;PENDING&lt;/code&gt; beyond four hours and flags or cleans up Azure VNets that lack a matching Exadata OCID.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Deploying a new Oracle Database@Azure cluster through the state machine executes in four sequential phases with explicit validation gates.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-04-10-oracle-database-at-azure-iac-terraform-5-flow.svg&quot; alt=&quot;Four-phase workflow sequences Azure, OCI, and DNS&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Pre-Flight Quotas, VNet &amp;#x26; Delegation Gate:&lt;/strong&gt; Verify that the &lt;code&gt;Oracle.Database&lt;/code&gt; resource provider is registered in the Azure subscription, confirm OCI compartment Exadata limits have headroom, pre-validate the RSA OpenSSH key format (&lt;code&gt;ssh-keygen -l&lt;/code&gt;), and confirm the &lt;code&gt;/24&lt;/code&gt; CIDR does not overlap Hub or Spoke VNets. Then create the Database VNet, apply &lt;code&gt;Oracle.Database/networkAttachments&lt;/code&gt; delegation, and poll ARM until both &lt;code&gt;provisioningState&lt;/code&gt; and &lt;code&gt;delegations[0].provisioningState&lt;/code&gt; equal &lt;code&gt;Succeeded&lt;/code&gt;. Rollback: strip delegation and delete VNet.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Exadata Infrastructure &amp;#x26; VM Cluster:&lt;/strong&gt; Submit infrastructure and VM cluster creation (either via &lt;code&gt;azurerm_oracle_*&lt;/code&gt; / &lt;code&gt;Oracle.Database&lt;/code&gt; ARM or OCI Database API) using federated &lt;code&gt;TokenExchangeSigner&lt;/code&gt; auth, polling asynchronously with Temporal heartbeats until &lt;code&gt;AVAILABLE&lt;/code&gt;. Rollback: trigger compensating cleanup of Azure networking if infrastructure creation fails before allocation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — SCAN IP Extraction &amp;#x26; Private DNS:&lt;/strong&gt; Poll the VM cluster until all three SCAN IP addresses are populated, write the Azure Private DNS A-records, verify VNet DNS links exist for both Hub and Database VNets, and open TCP &lt;code&gt;1521&lt;/code&gt;/&lt;code&gt;2484&lt;/code&gt; firewall/NSG rules. Rollback: remove DNS A-records and revert rule changes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — CDB Provisioning &amp;#x26; Catalog Activation:&lt;/strong&gt; Create the initial CDB/PDBs, verify SQL*Net connectivity to the SCAN hostname from a spoke probe, apply standardized cost-center tags across both Azure and OCI, and flip the Fleet Catalog status from &lt;code&gt;PENDING&lt;/code&gt; to &lt;code&gt;AVAILABLE&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Based on Temporal’s Python SDK (&lt;code&gt;temporalio&lt;/code&gt;), Azure’s &lt;code&gt;ManagedIdentityCredential&lt;/code&gt;, and OCI’s &lt;code&gt;TokenExchangeSigner&lt;/code&gt; workload identity federation, the workflow separates durable orchestration from cloud-specific activities.&lt;/p&gt;
&lt;h3 id=&quot;1-temporal-workflow-definition&quot;&gt;1. Temporal Workflow Definition&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dataclasses &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dataclass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; timedelta&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; temporalio &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; temporalio.common &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; RetryPolicy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;@dataclass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ProvisioningRequest&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    db_name: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    azure_region: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    vnet_cidr: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    delegated_subnet_cidr: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    exadata_shape: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;  # e.g., &quot;Exadata.X9M&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ocpu_count: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    storage_tb: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ssh_public_key: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;@workflow.defn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; OracleAzureProvisioningWorkflow&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    @workflow.run&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    async&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; run&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(self, req: ProvisioningRequest) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;dict&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        resource_ids &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            net &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow.execute_activity(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                create_azure_vnet_and_delegation,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                args&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[req.azure_region, req.vnet_cidr, req.delegated_subnet_cidr],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                start_to_close_timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;timedelta(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;minutes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                retry_policy&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RetryPolicy(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;maximum_attempts&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;backoff_coefficient&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2.0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            resource_ids.update(net)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            infra_ocid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow.execute_activity(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                provision_oci_exadata_infrastructure,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                args&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[net[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;oracle_cloud_subnet_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], req.azure_region, req.exadata_shape],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                start_to_close_timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;timedelta(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;hours&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                heartbeat_timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;timedelta(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;minutes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                retry_policy&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RetryPolicy(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;maximum_attempts&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;initial_interval&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;timedelta(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;minutes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            resource_ids[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;infra_ocid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; infra_ocid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            cluster &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow.execute_activity(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                provision_oci_vmcluster,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                args&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[infra_ocid, req.ocpu_count, req.storage_tb, req.ssh_public_key],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                start_to_close_timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;timedelta(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;hours&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                heartbeat_timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;timedelta(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;minutes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                retry_policy&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RetryPolicy(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;maximum_attempts&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;initial_interval&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;timedelta(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;minutes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            resource_ids[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;vmcluster_ocid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cluster[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ocid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow.execute_activity(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                configure_azure_dns_and_nsg,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                args&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[req.db_name, cluster[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;scan_ips&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], net[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;vnet_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                start_to_close_timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;timedelta(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;minutes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; resource_ids&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        except&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; Exception&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;vnet_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; resource_ids &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;infra_ocid&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; resource_ids:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow.execute_activity(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                    cleanup_azure_resources_on_failure,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                    args&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[resource_ids[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;vnet_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], resource_ids[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;subnet_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                    start_to_close_timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;timedelta(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;minutes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            raise&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-async-oci-polling-with-tokenexchangesigner-and-heartbeats&quot;&gt;2. Async OCI Polling with &lt;code&gt;TokenExchangeSigner&lt;/code&gt; and Heartbeats&lt;/h3&gt;
&lt;p&gt;To eliminate static &lt;code&gt;.oci/config&lt;/code&gt; private keys, configure an OCI IAM Identity Domain OIDC trust pointing to your Microsoft Entra ID issuer (&lt;code&gt;https://login.microsoftonline.com/&amp;#x3C;tenant-id&gt;/v2.0&lt;/code&gt;) and grant an OCI IAM policy to the federated workload identity on the target database compartment. Inside the Temporal worker, &lt;code&gt;ManagedIdentityCredential&lt;/code&gt; fetches a short-lived Entra token and &lt;code&gt;TokenExchangeSigner&lt;/code&gt; exchanges it for an OCI User Principal Session Token (UPST) automatically.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; asyncio&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oci&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azure.identity &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ManagedIdentityCredential&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; temporalio &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;@activity.defn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; provision_oci_exadata_infrastructure&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    azure_subnet_ocid: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, azure_region: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, shape: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    # Exchange Azure Entra Managed Identity JWT for an OCI UPST token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    azure_token &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ManagedIdentityCredential().get_token(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;OCI_FEDERATION_SCOPE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    signer &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oci.auth.signers.TokenExchangeSigner(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        jwt_or_func&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=lambda&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: ManagedIdentityCredential().get_token(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;OCI_FEDERATION_SCOPE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).token,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        oci_domain_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;OCI_DOMAIN_URL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        client_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;OCI_APP_CLIENT_ID&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    db_client &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oci.database.DatabaseClient({}, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;signer&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;signer)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    resp &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db_client.create_cloud_exadata_infrastructure(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        oci.database.models.CreateCloudExadataInfrastructureDetails(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            availability_domain&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;map_azure_region_to_oci_ad(azure_region),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            compartment_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;OCI_COMPARTMENT_ID&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            display_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;exadata-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;azure_region&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            shape&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;shape,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            compute_count&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            storage_count&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    infra_ocid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; resp.data.id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; _ &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;120&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        infra &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db_client.get_cloud_exadata_infrastructure(infra_ocid).data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        activity.heartbeat(infra.lifecycle_state)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; infra.lifecycle_state &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;AVAILABLE&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; infra_ocid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; infra.lifecycle_state &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;FAILED&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;TERMINATED&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            raise&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; RuntimeError&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ExadataInfrastructure &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;infra_ocid&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; state=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;infra.lifecycle_state&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; asyncio.sleep(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;30&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    raise&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; TimeoutError&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ExadataInfrastructure &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;infra_ocid&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; timed out&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-compensating-undelegation-cleanup&quot;&gt;3. Compensating Undelegation Cleanup&lt;/h3&gt;
&lt;p&gt;Because Temporal activities may retry on transient ARM 429 or 503 responses, the cleanup activity must be idempotent: if the subnet or VNet was already deleted on an earlier attempt, handle &lt;code&gt;ResourceNotFoundError&lt;/code&gt; as a clean no-op rather than failing the compensation step. Notice also that &lt;code&gt;delegations&lt;/code&gt; must be cleared and persisted via &lt;code&gt;begin_create_or_update&lt;/code&gt; before &lt;code&gt;begin_delete&lt;/code&gt; is called on the subnet, otherwise Azure ARM rejects the deletion with &lt;code&gt;InUseSubnetCannotBeDeleted&lt;/code&gt;. Similarly, during initial provisioning, the ARM activity must poll &lt;code&gt;client.subnets.get(...)&lt;/code&gt; until both &lt;code&gt;subnet.provisioning_state&lt;/code&gt; and &lt;code&gt;subnet.delegations[0].provisioning_state&lt;/code&gt; report &lt;code&gt;&quot;Succeeded&quot;&lt;/code&gt; (followed by a short regional settle wait) before returning the subnet OCID to the OCI provisioning activity.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;@activity.defn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; cleanup_azure_resources_on_failure&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(resource_group: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, vnet_name: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, subnet_name: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azure.mgmt.network &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; network_mgmt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azure.identity &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ManagedIdentityCredential&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    client &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; network_mgmt.NetworkManagementClient(ManagedIdentityCredential(), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;AZURE_SUBSCRIPTION_ID&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subnet &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.subnets.get(resource_group, vnet_name, subnet_name)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subnet.delegations &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; []  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Strip delegation before ARM permits subnet deletion&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    client.subnets.begin_create_or_update(resource_group, vnet_name, subnet_name, subnet).result()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    client.subnets.begin_delete(resource_group, vnet_name, subnet_name).result()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    client.virtual_networks.begin_delete(resource_group, vnet_name).result()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;4-native-terraform-azurerm-v4x-option&quot;&gt;4. Native Terraform &lt;code&gt;azurerm&lt;/code&gt; v4.x Option&lt;/h3&gt;
&lt;p&gt;For teams managing static Exadata hardware declaratively alongside Azure VNets, &lt;code&gt;azurerm&lt;/code&gt; v4.x provides &lt;code&gt;azurerm_oracle_exadata_infrastructure&lt;/code&gt; and &lt;code&gt;azurerm_oracle_cloud_vm_cluster&lt;/code&gt; (wrapping &lt;code&gt;Oracle.Database&lt;/code&gt; ARM APIs). Because Terraform links &lt;code&gt;cloud_exadata_infrastructure_id&lt;/code&gt; between the two resources, &lt;code&gt;terraform destroy&lt;/code&gt; automatically deletes the VM cluster before tearing down the underlying Exadata infrastructure. Even when Terraform manages those two resources, Temporal remains valuable for orchestrating pre-flight CIDR/SSH checks, waiting on DNS propagation, and automating CDB/PDB creation and Fleet Catalog registration.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Dual-control-plane workflows fail at propagation boundaries, token lifetimes, and cleanup ordering. Because every provisioning run crosses two independent cloud APIs, log correlation requires capturing both Azure’s &lt;code&gt;x-ms-request-id&lt;/code&gt; and OCI’s &lt;code&gt;opc-request-id&lt;/code&gt; inside Temporal’s structured activity logs; without both correlation IDs, support escalations across Microsoft and Oracle stall while teams reconstruct which cloud rejected the call. In addition, tag schemas must be normalized at the Temporal workflow boundary before either cloud API is invoked so FinOps chargeback reports can reconcile Azure networking charges against OCI Exadata compute consumption.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-04-10-oracle-database-at-azure-iac-terraform-6-failures.svg&quot; alt=&quot;Where dual-control-plane provisioning breaks&quot;&gt;&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure Mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Delegation Propagation Race&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;OCI API called immediately after Azure ARM returns 200; regional fabric not yet synced.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exadata creation fails with “subnet not delegated” error.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Poll &lt;code&gt;delegations[0].provisioningState == Succeeded&lt;/code&gt; + settle wait.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;OIDC Token Expiry Mid-Poll&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exchanged OCI UPST expires during a 60-minute cluster build loop.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;OCI polling call fails with HTTP 401 mid-workflow.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pass refreshable token callback to &lt;code&gt;TokenExchangeSigner&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Blocked Event Loop in Async Activity&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Using &lt;code&gt;time.sleep(30)&lt;/code&gt; inside &lt;code&gt;async def&lt;/code&gt; blocks Temporal heartbeats.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Temporal marks activity timed out and spawns duplicate build.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use &lt;code&gt;await asyncio.sleep(30)&lt;/code&gt; and call &lt;code&gt;activity.heartbeat()&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;SCAN IP Assignment Delay&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;VmCluster reports &lt;code&gt;AVAILABLE&lt;/code&gt; before all three SCAN IPs populate.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Azure Private DNS A-record creation fails on empty IP list.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Poll &lt;code&gt;scan_ip_ids&lt;/code&gt; until length == 3 before DNS activity.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Blocked Subnet Cleanup&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Compensating delete calls &lt;code&gt;subnets.begin_delete&lt;/code&gt; without removing delegation.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Azure ARM rejects deletion with &lt;code&gt;InUseSubnetCannotBeDeleted&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Strip &lt;code&gt;subnet.delegations = []&lt;/code&gt; and update before deleting subnet.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Cross-Cloud Tag Drift&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Azure and OCI resources receive mismatched cost-center tag keys.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chargeback reports fail to reconcile Azure and OCI billing.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Inject shared &lt;code&gt;{environment, cost-center, db-name}&lt;/code&gt; map from catalog.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Provisioning Oracle Database@Azure spans two independent control planes (Azure ARM and OCI) with a bidirectional dependency loop: OCI needs the delegated subnet, and Azure DNS needs OCI’s SCAN IPs.&lt;/li&gt;
&lt;li&gt;Wrap cross-cloud provisioning in a Temporal durable workflow so multi-hour builds survive worker restarts and execute compensating undelegation cleanup if OCI fails.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;oci.auth.signers.TokenExchangeSigner&lt;/code&gt; with an Entra Managed Identity token callback rather than static OCI API keys, avoiding secret sprawl and mid-poll 401 expirations.&lt;/li&gt;
&lt;li&gt;Inside &lt;code&gt;async def&lt;/code&gt; Temporal activities, always use &lt;code&gt;await asyncio.sleep()&lt;/code&gt; with &lt;code&gt;activity.heartbeat()&lt;/code&gt; instead of blocking &lt;code&gt;time.sleep()&lt;/code&gt; so workers stay responsive.&lt;/li&gt;
&lt;li&gt;Strip &lt;code&gt;Oracle.Database/networkAttachments&lt;/code&gt; delegations from an Azure subnet before attempting to delete the subnet or VNet during rollback.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Linear CI scripts and single-pass Terraform runs fail across Azure ARM and OCI APIs when subnet delegation lags, tokens expire mid-poll, or partial failures leave orphaned VNets behind.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Coordinate Azure networking, OCI Exadata/VmCluster/CDB provisioning, and Private DNS updates inside a Temporal durable workflow using &lt;code&gt;TokenExchangeSigner&lt;/code&gt; federation, async heartbeats, and saga undelegation cleanup.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run a test workflow in a non-production subscription and intentionally fail the OCI step (or trigger cancellation) to verify that the compensating activity strips the subnet delegation and deletes the VNet cleanly with zero orphaned resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your current Oracle Database@Azure automation this week: replace any static OCI API keys with Entra-to-OCI &lt;code&gt;TokenExchangeSigner&lt;/code&gt; federation, and add an explicit &lt;code&gt;delegations[0].provisioningState&lt;/code&gt; poll before invoking Exadata creation.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>failures</category></item><item><title>Agentic Database Operations: Building a Secure MCP Gateway for Oracle Fleet Intelligence</title><link>https://rajivonai.com/blog/2026-01-10-oracle-sqlcl-mcp-server-dba-setup/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-01-10-oracle-sqlcl-mcp-server-dba-setup/</guid><description>Why giving AI agents raw SQL access to Oracle databases is a production security disaster, and how to build a hardened Model Context Protocol server that acts as an authenticated, sandboxed API gateway between AI reasoning engines and a fleet of 1,000+ Oracle databases.</description><pubDate>Sun, 02 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Giving an LLM agent raw SQL execution against a production Oracle database turns prompt reasoning into unbounded dictionary scans; safe agentic diagnostics require an MCP gateway that exposes only parameterized, pre-tuned queries backed by Oracle Resource Manager &lt;code&gt;CANCEL_SQL&lt;/code&gt; ceilings.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Even a read-only SQL interface lets an LLM run unindexed &lt;code&gt;DBA_HIST_ACTIVE_SESS_HISTORY&lt;/code&gt; scans without &lt;code&gt;DBID&lt;/code&gt; or &lt;code&gt;snap_id&lt;/code&gt; predicates, thrashing the buffer cache and burning context-window tokens on &lt;code&gt;ORA-00942&lt;/code&gt; retries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Put a curated FastMCP gateway in front of &lt;code&gt;oracledb&lt;/code&gt; thin-mode pools (&lt;code&gt;ewallet.pem&lt;/code&gt;), clamp lookback and row limits in Python, use delta AWR queries (&lt;code&gt;ELAPSED_TIME_DELTA&lt;/code&gt;), and bind the diagnostic user to an Oracle Resource Manager plan with &lt;code&gt;SWITCH_GROUP =&gt; &apos;CANCEL_SQL&apos;&lt;/code&gt; and &lt;code&gt;SWITCH_TIME =&gt; 15&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Oracle SQLcl’s native MCP server (&lt;code&gt;sql -mcp&lt;/code&gt;) arrived in &lt;strong&gt;SQLcl 25.2&lt;/strong&gt; (not 24.3) and still allows arbitrary SQL on saved connections; &lt;code&gt;python-oracledb&lt;/code&gt; thin mode ignores &lt;code&gt;sqlnet.ora&lt;/code&gt; and requires &lt;code&gt;ewallet.pem&lt;/code&gt;; and querying &lt;code&gt;ELAPSED_TIME_TOTAL&lt;/code&gt; instead of &lt;code&gt;ELAPSED_TIME_DELTA&lt;/code&gt; returns instance-lifetime totals rather than the requested lookback window.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-01-10-oracle-sqlcl-mcp-server-dba-setup.svg&quot; alt=&quot;Agentic Oracle gateway with FastMCP parameter clamping and Resource Manager CANCEL_SQL&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The Model Context Protocol (MCP), introduced by Anthropic in late 2024 and updated through the 2025 specification revisions, standardizes how AI clients discover and invoke external tools over JSON-RPC 2.0. An MCP server publishes a manifest of typed functions with JSON Schema parameters. The AI client sees only the tool signatures and descriptions—not the backing database credentials, TNS aliases, or SQL templates.&lt;/p&gt;
&lt;p&gt;In parallel, Oracle shipped native MCP server support in &lt;strong&gt;Oracle SQLcl 25.2&lt;/strong&gt; (requiring Java 17 or 21), invoked via &lt;code&gt;sql -mcp&lt;/code&gt; against pre-saved connections (&lt;code&gt;conn -save ... -savepwd ...&lt;/code&gt;). SQLcl &lt;code&gt;-mcp&lt;/code&gt; is convenient for local development and controlled sandboxes because it avoids embedding raw passwords in client configs. On production OLTP fleets, however, a generic SQL execution tool still leaves the LLM responsible for choosing the right Oracle dictionary view, joining on &lt;code&gt;DBID&lt;/code&gt; and &lt;code&gt;INSTANCE_NUMBER&lt;/code&gt;, and bounding partition scans—tasks where a single mistake creates a production incident.&lt;/p&gt;
&lt;p&gt;The distinction between Oracle’s in-memory &lt;code&gt;V$&lt;/code&gt; views and disk-backed &lt;code&gt;DBA_HIST_*&lt;/code&gt; AWR views illustrates why view selection matters. &lt;code&gt;V$ACTIVE_SESSION_HISTORY&lt;/code&gt; reads a bounded circular buffer in the SGA; &lt;code&gt;DBA_HIST_ACTIVE_SESS_HISTORY&lt;/code&gt; reads persisted 1-in-10 second samples stored in &lt;code&gt;SYSAUX&lt;/code&gt; partitions across days or weeks of AWR retention. When an LLM confuses the two or omits &lt;code&gt;DBID&lt;/code&gt; and &lt;code&gt;SNAP_ID&lt;/code&gt; bounds, a query that looks identical in syntax shifts from a millisecond SGA read to a multi-gigabyte &lt;code&gt;SYSAUX&lt;/code&gt; tablespace scan. Similarly, querying &lt;code&gt;V$LOCK&lt;/code&gt; and &lt;code&gt;V$SESSION&lt;/code&gt; on an Oracle RAC cluster only inspects the single instance the connection landed on—missing a blocker sitting on Node 2 unless the query uses &lt;code&gt;GV$LOCK&lt;/code&gt; and &lt;code&gt;GV$SESSION&lt;/code&gt; joined on &lt;code&gt;INST_ID&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Allowing an LLM to author arbitrary &lt;code&gt;SELECT&lt;/code&gt; statements against Oracle’s &lt;code&gt;V$&lt;/code&gt; and &lt;code&gt;DBA_HIST_*&lt;/code&gt; views fails against four concrete operational constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Unbounded AWR and ASH partition scans:&lt;/strong&gt; &lt;code&gt;DBA_HIST_ACTIVE_SESS_HISTORY&lt;/code&gt; and &lt;code&gt;DBA_HIST_SQLSTAT&lt;/code&gt; are range-partitioned by &lt;code&gt;DBID&lt;/code&gt; and &lt;code&gt;SNAP_ID&lt;/code&gt; inside &lt;code&gt;SYSAUX&lt;/code&gt;. If an LLM omits &lt;code&gt;DBID&lt;/code&gt; or &lt;code&gt;SNAP_ID&lt;/code&gt; predicates—or joins &lt;code&gt;V$SQL_PLAN&lt;/code&gt; without &lt;code&gt;PLAN_HASH_VALUE&lt;/code&gt;—Oracle scans historical partitions across every snapshot retained in &lt;code&gt;SYSAUX&lt;/code&gt;. A single unindexed ASH scan can saturate physical I/O and evict hot OLTP blocks from the buffer cache.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Context-window exhaustion from trial-and-error SQL:&lt;/strong&gt; LLMs frequently hallucinate Oracle view names (for example, &lt;code&gt;DBA_HIST_SQL_STAT&lt;/code&gt; instead of &lt;code&gt;DBA_HIST_SQLSTAT&lt;/code&gt;), triggering &lt;code&gt;ORA-00942: table or view does not exist&lt;/code&gt;. If an agent also dumps wide diagnostic rows without &lt;code&gt;SUBSTR&lt;/code&gt; truncation, just 15 unfiltered rows with 2 KB SQL text and explain plans consume &lt;code&gt;15 * 2,000 bytes ≈ 30 KB&lt;/code&gt; (~7,500 tokens) in a single turn—pushing earlier incident context out of the window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No &lt;code&gt;EXPLAIN PLAN&lt;/code&gt; feedback gate:&lt;/strong&gt; A human DBA checks cardinality estimates or &lt;code&gt;SNAP_ID&lt;/code&gt; bounds before running an unfamiliar query against production. An LLM with raw SQL access submits queries directly; without Oracle Resource Manager intervention, a read-only user can still hold CPU and I/O indefinitely.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Incident-storm connection pool exhaustion:&lt;/strong&gt; During a Sev-1 outage, if 30 engineers query the assistant concurrently and each slow AWR call triggers 3 automatic client retries, &lt;code&gt;30 * 3 = 90&lt;/code&gt; concurrent requests hit a database pool capped at &lt;code&gt;max=10&lt;/code&gt;. Without per-agent rate limits and call deduplication, &lt;code&gt;pool.acquire()&lt;/code&gt; blocks and times out across the entire gateway.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-01-10-oracle-sqlcl-mcp-server-dba-setup-2-constraints.svg&quot; alt=&quot;Four operational constraints making raw LLM SQL unsafe in production Oracle fleets&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do you let AI agents investigate live Oracle performance and locking incidents across a 1,000+ database fleet without exposing raw SQL execution, leaking TNS/wallet credentials, or letting runaway queries degrade production OLTP?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Integration Architecture&lt;/th&gt;&lt;th&gt;SQL Flexibility&lt;/th&gt;&lt;th&gt;Credential Isolation&lt;/th&gt;&lt;th&gt;Blast Radius Ceiling&lt;/th&gt;&lt;th&gt;Why Chosen / Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Raw JDBC / &lt;code&gt;sqlplus&lt;/code&gt; Tool&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Full DDL / DML / OS&lt;/td&gt;&lt;td&gt;None (client holds creds)&lt;/td&gt;&lt;td&gt;Unbounded&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; One prompt injection or hallucinated &lt;code&gt;ALTER SYSTEM&lt;/code&gt; / &lt;code&gt;DROP&lt;/code&gt; causes immediate data loss.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Oracle SQLcl 25.2 (&lt;code&gt;sql -mcp&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Arbitrary SQL on saved conns&lt;/td&gt;&lt;td&gt;High (&lt;code&gt;conn -save&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;Depends on DB user grants&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Dev / Sandbox Only:&lt;/strong&gt; Great for ad-hoc DBA workstations, but still lets an LLM write unindexed &lt;code&gt;DBA_HIST_*&lt;/code&gt; scans.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;OEM REST APIs&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Fixed OEM endpoints&lt;/td&gt;&lt;td&gt;High&lt;/td&gt;&lt;td&gt;High&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Limited:&lt;/strong&gt; Strong governance, but coarse polling granularity lags live &lt;code&gt;V$LOCK&lt;/code&gt; / ASH triage.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Curated FastMCP Gateway + Resource Manager&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Pre-tuned parameterized templates only&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Gateway-isolated (&lt;code&gt;ewallet.pem&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Python clamp + &lt;code&gt;CANCEL_SQL&lt;/code&gt; (15s)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Chosen:&lt;/strong&gt; Eliminates arbitrary SQL structurally and backs every query with an engine-level timeout.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-01-10-oracle-sqlcl-mcp-server-dba-setup-3-options.svg&quot; alt=&quot;Comparison of four AI-to-Oracle integration options&quot;&gt;&lt;/p&gt;
&lt;p&gt;Using &lt;strong&gt;Oracle SQLcl 25.2 &lt;code&gt;-mcp&lt;/code&gt;&lt;/strong&gt; is the fastest way to prototype an AI DBA workflow on non-production databases because it ships out of the box with connection-store isolation. Its boundary is query authorship: SQLcl &lt;code&gt;-mcp&lt;/code&gt; executes whatever SQL the model generates (subject to the database user’s privileges). When the target is a tier-1 production OLTP fleet, replacing free-form SQL generation with &lt;strong&gt;typed diagnostic tools&lt;/strong&gt; removes &lt;code&gt;ORA-00942&lt;/code&gt; guesswork and guarantees every query hits indexed &lt;code&gt;SNAP_ID&lt;/code&gt; and &lt;code&gt;DBID&lt;/code&gt; paths.&lt;/p&gt;
&lt;p&gt;Transport choice also separates prototypes from production fleets. Running an MCP server over &lt;code&gt;stdio&lt;/code&gt; works well for a single DBA’s local IDE session, but distributes Oracle wallet files and &lt;code&gt;tnsnames.ora&lt;/code&gt; to every engineer’s laptop. A shared &lt;strong&gt;Streamable HTTP&lt;/strong&gt; gateway behind corporate SSO and mTLS keeps &lt;code&gt;ewallet.pem&lt;/code&gt; inside a locked-down VPC service while giving every engineer the same audited tool catalog.&lt;/p&gt;
&lt;h2 id=&quot;the-oracle-fleet-mcp-gateway-architecture&quot;&gt;The Oracle Fleet MCP Gateway Architecture&lt;/h2&gt;
&lt;p&gt;The gateway sits between the AI client and the Oracle fleet as a stateless Python &lt;code&gt;FastMCP&lt;/code&gt; service. The agent never sees a hostname, port, TNS alias, or wallet password; it passes a logical &lt;code&gt;db_identifier&lt;/code&gt; (validated by dictionary lookup) and bounded integer parameters.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-01-10-oracle-sqlcl-mcp-server-dba-setup-4-architecture.svg&quot; alt=&quot;FastMCP gateway isolating wallet pools and enforcing Oracle Resource Manager limits&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-thin-mode-python-oracledb-pool-initialization&quot;&gt;1. Thin-Mode &lt;code&gt;python-oracledb&lt;/code&gt; Pool Initialization&lt;/h3&gt;
&lt;p&gt;In &lt;code&gt;python-oracledb&lt;/code&gt; &lt;strong&gt;Thin mode&lt;/strong&gt; (the default when &lt;code&gt;init_oracle_client()&lt;/code&gt; is not called), the driver does &lt;strong&gt;not&lt;/strong&gt; read &lt;code&gt;sqlnet.ora&lt;/code&gt; or binary &lt;code&gt;cwallet.sso&lt;/code&gt; files. Instead, &lt;code&gt;config_dir&lt;/code&gt; points to &lt;code&gt;tnsnames.ora&lt;/code&gt;, while &lt;code&gt;wallet_location&lt;/code&gt; points to the directory containing the PEM-encoded &lt;strong&gt;&lt;code&gt;ewallet.pem&lt;/code&gt;&lt;/strong&gt; (plus &lt;code&gt;wallet_password&lt;/code&gt; if encrypted).&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; logging&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dataclasses &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dataclass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; typing &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Dict&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oracledb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; mcp.server.fastmcp &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; FastMCP&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;mcp &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; FastMCP(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;OracleFleetMCPGateway&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;logger &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; logging.getLogger(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;oracle_mcp&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;@dataclass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; DatabaseEntry&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tns_alias: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    config_dir: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;       # contains tnsnames.ora&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    wallet_dir: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;       # contains ewallet.pem&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    wallet_pwd: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    display_name: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    region: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;FLEET_REGISTRY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: Dict[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, DatabaseEntry] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {}  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# loaded from Vault / Secrets Manager&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;DB_POOLS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: Dict[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, oracledb.ConnectionPool] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; init_pools&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db_id, entry &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; FLEET_REGISTRY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.items():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        DB_POOLS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[db_id] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oracledb.create_pool(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            user&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;MCP_DIAG_USER&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            dsn&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;entry.tns_alias,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            config_dir&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;entry.config_dir,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            wallet_location&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;entry.wallet_dir,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            wallet_password&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;entry.wallet_pwd,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            min&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            max&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            increment&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            getmode&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;oracledb.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;POOL_GETMODE_WAIT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-tuned-delta-awr-and-blocking-session-tools&quot;&gt;2. Tuned Delta AWR and Blocking-Session Tools&lt;/h3&gt;
&lt;p&gt;A common AWR bug is selecting &lt;code&gt;elapsed_time_total&lt;/code&gt; from &lt;code&gt;DBA_HIST_SQLSTAT&lt;/code&gt;—which returns cumulative elapsed time since instance startup rather than activity during the requested lookback window. Production tools must aggregate &lt;code&gt;SUM(s.elapsed_time_delta)&lt;/code&gt; and &lt;code&gt;SUM(s.executions_delta)&lt;/code&gt; and join &lt;code&gt;DBA_HIST_SNAPSHOT&lt;/code&gt; on &lt;code&gt;snap_id&lt;/code&gt;, &lt;code&gt;dbid&lt;/code&gt;, &lt;strong&gt;and &lt;code&gt;instance_number&lt;/code&gt;&lt;/strong&gt; so RAC clusters do not double-count snapshots:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;MAX_ROWS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 15&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;MAX_MINUTES&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 120&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;@mcp.tool&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; get_awr_top_sql&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(db_identifier: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, minutes_ago: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 60&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, top_n: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;&quot;&quot;Return top SQL by delta elapsed time within the last 5-120 minutes.&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db_identifier &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;not&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; DB_POOLS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Unknown database: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db_identifier&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;. Valid options: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;DB_POOLS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.keys())&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    minutes_ago &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; max&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;min&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(minutes_ago, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;MAX_MINUTES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    top_n &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; max&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;min&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(top_n, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;MAX_ROWS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    sql &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        SELECT s.sql_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;               ROUND(SUM(s.elapsed_time_delta) / 1e6, 2) AS window_elapsed_sec,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;               SUM(s.executions_delta)                   AS window_executions,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;               ROUND(SUM(s.elapsed_time_delta) / NULLIF(SUM(s.executions_delta), 0) / 1e6, 4) AS avg_sec,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;               SUBSTR(MAX(DBMS_LOB.SUBSTR(t.sql_text, 100, 1)), 1, 100) AS sql_preview&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        FROM   dba_hist_sqlstat s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        JOIN   dba_hist_snapshot sn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;               ON  sn.snap_id = s.snap_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;               AND sn.dbid = s.dbid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;               AND sn.instance_number = s.instance_number&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        JOIN   dba_hist_sqltext t&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;               ON  t.sql_id = s.sql_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;               AND t.dbid = s.dbid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        WHERE  s.dbid = (SELECT dbid FROM v$database)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          AND  sn.begin_interval_time &gt; SYSTIMESTAMP - NUMTODSINTERVAL(:minutes_ago, &apos;MINUTE&apos;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        GROUP BY s.sql_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        HAVING SUM(s.elapsed_time_delta) &gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        ORDER BY window_elapsed_sec DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        FETCH FIRST :top_n ROWS ONLY&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    with&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; DB_POOLS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[db_identifier].acquire() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; conn:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        rows &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; conn.cursor().execute(sql, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;minutes_ago&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;minutes_ago, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;top_n&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;top_n).fetchall()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; _md_table([&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SQL ID&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Window Elapsed (s)&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Executions&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Avg (s)&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SQL Preview&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], rows)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-defense-in-depth-prompt-injection-idempotency-and-rac-awareness&quot;&gt;3. Defense in Depth: Prompt Injection, Idempotency, and RAC Awareness&lt;/h3&gt;
&lt;p&gt;A curated MCP gateway also closes two failure modes that raw SQL tools leave wide open:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Prompt injection and parameter tampering:&lt;/strong&gt; Suppose an engineer pastes an error log containing adversarial text (&lt;code&gt;&quot;check database PROD; DROP TABLE orders;&quot;&lt;/code&gt;) into an AI chat, and the model passes that string as &lt;code&gt;db_identifier&lt;/code&gt;. Because the gateway routes &lt;code&gt;db_identifier&lt;/code&gt; exclusively through a Python dictionary lookup (&lt;code&gt;if db_identifier not in DB_POOLS&lt;/code&gt;) and passes numeric parameters (&lt;code&gt;:minutes_ago&lt;/code&gt;, &lt;code&gt;:top_n&lt;/code&gt;) strictly as &lt;code&gt;oracledb&lt;/code&gt; bind variables, string concatenation is absent from the codebase. Even if an attacker compromises the gateway host itself, &lt;code&gt;MCP_DIAG_USER&lt;/code&gt; holds only &lt;code&gt;CREATE SESSION&lt;/code&gt; and explicit &lt;code&gt;SELECT&lt;/code&gt; grants on whitelisted &lt;code&gt;V$&lt;/code&gt;/&lt;code&gt;GV$&lt;/code&gt;/&lt;code&gt;DBA_HIST_*&lt;/code&gt; views—no &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt;, &lt;code&gt;ALTER SYSTEM&lt;/code&gt;, or &lt;code&gt;DBA&lt;/code&gt; role.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;In-flight call deduplication and rate limiting:&lt;/strong&gt; When a database is already under heavy CPU load and an AWR query takes 8–10 seconds, MCP clients often time out and retry automatically. Tracking an in-flight cache keyed by &lt;code&gt;(tool_name, db_identifier, normalized_args)&lt;/code&gt; with a 30-second TTL returns the first call’s result (or joins the pending future) instead of launching three parallel scans against a struggling primary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RAC-aware locking (&lt;code&gt;GV$LOCK&lt;/code&gt; / &lt;code&gt;GV$SESSION&lt;/code&gt;):&lt;/strong&gt; On multi-node RAC clusters, querying &lt;code&gt;V$LOCK&lt;/code&gt; misses cross-instance blockers. Curated tools should query &lt;code&gt;GV$LOCK&lt;/code&gt; and &lt;code&gt;GV$SESSION&lt;/code&gt; (filtering &lt;code&gt;l1.block &gt; 0 AND l2.request &gt; 0&lt;/code&gt; across &lt;code&gt;inst_id&lt;/code&gt;) so the agent sees the real cluster-wide blocker on the first call without needing follow-up prompts.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;4-oracle-resource-manager-cancel_sql-ceiling&quot;&gt;4. Oracle Resource Manager &lt;code&gt;CANCEL_SQL&lt;/code&gt; Ceiling&lt;/h3&gt;
&lt;p&gt;Application-layer clamping (&lt;code&gt;MAX_MINUTES = 120&lt;/code&gt;, &lt;code&gt;MAX_ROWS = 15&lt;/code&gt;) is the first gate. The second, independent gate lives inside Oracle itself using &lt;code&gt;DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE&lt;/code&gt; with &lt;strong&gt;&lt;code&gt;SWITCH_GROUP =&gt; &apos;CANCEL_SQL&apos;&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;SWITCH_TIME =&gt; 15&lt;/code&gt;&lt;/strong&gt;. When &lt;code&gt;MCP_DIAG_USER&lt;/code&gt; runs a statement exceeding 15 seconds of execution time, Oracle automatically cancels the SQL call with &lt;code&gt;ORA-01013: user requested cancel of current operation&lt;/code&gt; while keeping the pooled session alive:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  DBMS_RESOURCE_MANAGER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;CREATE_PENDING_AREA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  DBMS_RESOURCE_MANAGER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;CREATE_CONSUMER_GROUP&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    consumer_group &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;MCP_GATEWAY_GROUP&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    comment        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;AI diagnostic MCP gateway — cancel SQL calls past 15s&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  DBMS_RESOURCE_MANAGER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;SET_CONSUMER_GROUP_MAPPING&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    attribute      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; DBMS_RESOURCE_MANAGER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;ORACLE_USER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    value&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;          =&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;MCP_DIAG_USER&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    consumer_group &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;MCP_GATEWAY_GROUP&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  DBMS_RESOURCE_MANAGER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;CREATE_PLAN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    plan    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;MCP_DIAG_PLAN&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    comment &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;Resource plan capping MCP diagnostic calls&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  DBMS_RESOURCE_MANAGER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;CREATE_PLAN_DIRECTIVE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    plan                  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;MCP_DIAG_PLAN&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    group_or_subplan      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;MCP_GATEWAY_GROUP&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    comment               &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;Cancel runaway diagnostic SQL at 15s&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cpu_p1                &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    parallel_degree_limit_p1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    switch_group          &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;CANCEL_SQL&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    switch_time           &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 15&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  DBMS_RESOURCE_MANAGER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;CREATE_PLAN_DIRECTIVE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    plan             &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;MCP_DIAG_PLAN&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    group_or_subplan &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;OTHER_GROUPS&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    comment          &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;Default workload directive&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cpu_p1           &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 90&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  DBMS_RESOURCE_MANAGER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;VALIDATE_PENDING_AREA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  DBMS_RESOURCE_MANAGER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;SUBMIT_PENDING_AREA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SYSTEM&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; RESOURCE_MANAGER_PLAN &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;MCP_DIAG_PLAN&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-01-10-oracle-sqlcl-mcp-server-dba-setup-5-flow.svg&quot; alt=&quot;Four-phase rollout from Resource Manager plan to fleet gateway&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Resource Manager Plan and Read-Only User.&lt;/strong&gt; Create &lt;code&gt;MCP_DIAG_USER&lt;/code&gt; with explicit &lt;code&gt;SELECT&lt;/code&gt; grants on &lt;code&gt;V_$SESSION&lt;/code&gt;, &lt;code&gt;V_$LOCK&lt;/code&gt;, &lt;code&gt;V_$SQL&lt;/code&gt;, &lt;code&gt;V_$DATABASE&lt;/code&gt;, &lt;code&gt;V_$ACTIVE_SESSION_HISTORY&lt;/code&gt;, &lt;code&gt;DBA_HIST_SQLSTAT&lt;/code&gt;, &lt;code&gt;DBA_HIST_SQLTEXT&lt;/code&gt;, and &lt;code&gt;DBA_HIST_SNAPSHOT&lt;/code&gt;. Bind the user to &lt;code&gt;MCP_GATEWAY_GROUP&lt;/code&gt; with &lt;code&gt;SWITCH_GROUP =&gt; &apos;CANCEL_SQL&apos;&lt;/code&gt; and &lt;code&gt;SWITCH_TIME =&gt; 15&lt;/code&gt;. &lt;em&gt;Validation gate:&lt;/em&gt; Execute a synthetic 20-second query as &lt;code&gt;MCP_DIAG_USER&lt;/code&gt; and confirm Oracle aborts it at 15 seconds with &lt;code&gt;ORA-01013&lt;/code&gt;. &lt;em&gt;Rollback:&lt;/em&gt; &lt;code&gt;ALTER USER mcp_diag_user ACCOUNT LOCK;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Thin-Mode Wallet and Startup Grant Verification.&lt;/strong&gt; Deploy &lt;code&gt;ewallet.pem&lt;/code&gt; and &lt;code&gt;tnsnames.ora&lt;/code&gt; to the gateway host and add a startup probe that runs &lt;code&gt;SELECT 1 FROM ... WHERE 1=0&lt;/code&gt; against every required &lt;code&gt;V$&lt;/code&gt; and &lt;code&gt;DBA_HIST_*&lt;/code&gt; view. &lt;em&gt;Validation gate:&lt;/em&gt; Gateway refuses to start if any required grant is missing (&lt;code&gt;ORA-00942&lt;/code&gt;). &lt;em&gt;Rollback:&lt;/em&gt; Keep legacy read-only DBA runbooks active.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Non-Production Stdio Canary.&lt;/strong&gt; Connect Claude Desktop or an internal CLI agent via stdio transport to a staging database and validate &lt;code&gt;get_awr_top_sql&lt;/code&gt; and &lt;code&gt;get_blocking_sessions&lt;/code&gt; against simulated lock contention. &lt;em&gt;Validation gate:&lt;/em&gt; Accurate diagnosis without any SQL authored by the LLM and response sizes under 2,000 tokens. &lt;em&gt;Rollback:&lt;/em&gt; Remove the MCP server entry from client config.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Fleet HTTP/mTLS Gateway.&lt;/strong&gt; Expose the gateway over mTLS/IAM-authenticated Streamable HTTP with per-agent rate limits, a 30-second in-flight idempotency cache, and structured audit logging of &lt;code&gt;(agent_id, tool_name, db_identifier, elapsed_ms)&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Oracle &lt;code&gt;DBMS_RESOURCE_MANAGER&lt;/code&gt; &lt;code&gt;CANCEL_SQL&lt;/code&gt; behavior (Oracle Database PL/SQL Packages and Types Reference):&lt;/strong&gt; Setting &lt;code&gt;SWITCH_GROUP =&gt; &apos;CANCEL_SQL&apos;&lt;/code&gt; forces &lt;code&gt;SWITCH_FOR_CALL =&gt; TRUE&lt;/code&gt; automatically. Unlike &lt;code&gt;KILL_SESSION&lt;/code&gt;, &lt;code&gt;CANCEL_SQL&lt;/code&gt; aborts only the offending SQL call (&lt;code&gt;ORA-01013&lt;/code&gt;) and leaves the &lt;code&gt;oracledb&lt;/code&gt; pooled connection intact for subsequent tool calls. You can pair it with &lt;code&gt;MAX_EST_EXEC_TIME&lt;/code&gt; on the same directive so Oracle’s optimizer rejects obviously catastrophic plans (&lt;code&gt;ORA-07455&lt;/code&gt;) before they even begin consuming buffer-cache blocks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle SQLcl 25.2 MCP Server behavior (Oracle SQLcl User’s Guide):&lt;/strong&gt; SQLcl 25.2 introduced &lt;code&gt;sql -mcp&lt;/code&gt; on top of saved connections (&lt;code&gt;conn -save&lt;/code&gt;). While it protects credentials from leaking into LLM prompts, it still exposes general SQL execution—making a curated FastMCP gateway the safer boundary for production OLTP fleets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;MCP &lt;code&gt;resources&lt;/code&gt; primitive (Model Context Protocol Specification):&lt;/strong&gt; Beyond &lt;code&gt;tools&lt;/code&gt;, MCP supports read-only &lt;code&gt;resources&lt;/code&gt; (for example, &lt;code&gt;oracle://PROD-US-EAST/awr/latest&lt;/code&gt;), allowing agents to fetch bounded pre-generated AWR summaries directly into context without triggering live &lt;code&gt;DBA_HIST_*&lt;/code&gt; scans during peak load.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AWR &lt;code&gt;ELAPSED_TIME_DELTA&lt;/code&gt; vs. &lt;code&gt;ELAPSED_TIME_TOTAL&lt;/code&gt; (Oracle Database Reference):&lt;/strong&gt; &lt;code&gt;DBA_HIST_SQLSTAT&lt;/code&gt; stores both cumulative (&lt;code&gt;_TOTAL&lt;/code&gt;) and per-snapshot (&lt;code&gt;_DELTA&lt;/code&gt;) counters. Any time-windowed diagnostic tool that forgets &lt;code&gt;SUM(elapsed_time_delta)&lt;/code&gt; or omits &lt;code&gt;instance_number&lt;/code&gt; in the snapshot join misreports long-lived cached cursors as current-hour spikes. Similarly, on RAC clusters, pairing &lt;code&gt;GV$LOCK&lt;/code&gt; with &lt;code&gt;GV$SESSION&lt;/code&gt; ensures cross-instance blockers are surfaced on the first diagnostic call without requiring manual node-by-node queries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;python-oracledb&lt;/code&gt; error-surface clarity:&lt;/strong&gt; When &lt;code&gt;DPY-3015&lt;/code&gt; (wallet/TLS failure) or &lt;code&gt;DPY-4011&lt;/code&gt; (closed/timed-out pool connection) occurs, returning a structured &lt;code&gt;[DB_ERROR]&lt;/code&gt; string prevents the LLM from misinterpreting an empty exception as “zero blocking sessions.”&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Condition&lt;/th&gt;&lt;th&gt;Breaks How&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;LLM client retries slow tool calls&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;8s AWR query triggers 3x client retries, tripling DB load and exhausting &lt;code&gt;max=10&lt;/code&gt; pool&lt;/td&gt;&lt;td&gt;Surge in duplicate &lt;code&gt;(tool, db_identifier, args)&lt;/code&gt; calls and &lt;code&gt;DPY-4011&lt;/code&gt; / pool wait timeouts&lt;/td&gt;&lt;td&gt;Add a 30s idempotency/dedup cache for identical in-flight calls and cap per-agent concurrency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;&lt;code&gt;python-oracledb&lt;/code&gt; thin mode missing &lt;code&gt;ewallet.pem&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Gateway points &lt;code&gt;wallet_location&lt;/code&gt; at a folder with only &lt;code&gt;cwallet.sso&lt;/code&gt;; thin mode fails handshake&lt;/td&gt;&lt;td&gt;Startup pool creation raises &lt;code&gt;DPY-3015&lt;/code&gt; / TLS wallet error&lt;/td&gt;&lt;td&gt;Export &lt;code&gt;ewallet.pem&lt;/code&gt; into &lt;code&gt;wallet_location&lt;/code&gt; and pass &lt;code&gt;wallet_password&lt;/code&gt;, or switch explicitly to Thick mode&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Revoked &lt;code&gt;V$&lt;/code&gt; or &lt;code&gt;DBA_HIST_*&lt;/code&gt; grant&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Tool raises &lt;code&gt;ORA-00942&lt;/code&gt;; naive error handling lets the LLM report “no blockers found”&lt;/td&gt;&lt;td&gt;&lt;code&gt;ORA-00942&lt;/code&gt; in gateway logs&lt;/td&gt;&lt;td&gt;Assert all view &lt;code&gt;SELECT&lt;/code&gt; grants at gateway startup and return explicit &lt;code&gt;[DB_ERROR]&lt;/code&gt; strings to the LLM&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;AWR query uses &lt;code&gt;_TOTAL&lt;/code&gt; instead of &lt;code&gt;_DELTA&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Historical cursor from 3 days ago tops the 60-minute list&lt;/td&gt;&lt;td&gt;Top SQL list never changes across rolling 15-minute checks&lt;/td&gt;&lt;td&gt;Aggregate &lt;code&gt;SUM(elapsed_time_delta)&lt;/code&gt; and join on &lt;code&gt;(snap_id, dbid, instance_number)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Context window blowout on wide SQL text&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Unbounded &lt;code&gt;sql_text&lt;/code&gt; across 15 rows floods the LLM context&lt;/td&gt;&lt;td&gt;Tool response exceeds 8K tokens&lt;/td&gt;&lt;td&gt;Clamp &lt;code&gt;DBMS_LOB.SUBSTR(t.sql_text, 100, 1)&lt;/code&gt; and cap &lt;code&gt;MAX_ROWS = 15&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Wrong &lt;code&gt;DBID&lt;/code&gt; on cloned/standby DBs&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Hardcoded &lt;code&gt;DBID&lt;/code&gt; returns empty rows or wrong incarnation metrics&lt;/td&gt;&lt;td&gt;Empty AWR results on cloned test environments&lt;/td&gt;&lt;td&gt;Resolve &lt;code&gt;DBID&lt;/code&gt; dynamically via &lt;code&gt;(SELECT dbid FROM v$database)&lt;/code&gt; inside each session&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-01-10-oracle-sqlcl-mcp-server-dba-setup-6-failures.svg&quot; alt=&quot;Four failure modes in Oracle MCP gateways: retry storms, ewallet.pem, AWR deltas, and grant drift&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Remove arbitrary SQL from production AI paths:&lt;/strong&gt; An LLM cannot reliably bound &lt;code&gt;DBA_HIST_ACTIVE_SESS_HISTORY&lt;/code&gt; partition scans or &lt;code&gt;V$SQL_PLAN&lt;/code&gt; joins; expose typed, pre-tuned diagnostic tools instead.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SQLcl 25.2 &lt;code&gt;-mcp&lt;/code&gt; is a workstation tool, not a fleet guardrail:&lt;/strong&gt; Saved connections protect passwords, but curated &lt;code&gt;FastMCP&lt;/code&gt; tools plus Oracle Resource Manager protect production CPU and buffer cache.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Enforce &lt;code&gt;SWITCH_GROUP =&gt; &apos;CANCEL_SQL&apos;&lt;/code&gt; inside Oracle:&lt;/strong&gt; Pairing Python parameter clamping with &lt;code&gt;SWITCH_TIME =&gt; 15&lt;/code&gt; guarantees runaway calls fail with &lt;code&gt;ORA-01013&lt;/code&gt; without breaking pooled connections.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mind &lt;code&gt;python-oracledb&lt;/code&gt; thin-mode wallet rules:&lt;/strong&gt; Thin mode ignores &lt;code&gt;sqlnet.ora&lt;/code&gt; and &lt;code&gt;cwallet.sso&lt;/code&gt;; configure &lt;code&gt;config_dir&lt;/code&gt; for &lt;code&gt;tnsnames.ora&lt;/code&gt; and &lt;code&gt;wallet_location&lt;/code&gt; for &lt;code&gt;ewallet.pem&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Always aggregate &lt;code&gt;_DELTA&lt;/code&gt; AWR columns with &lt;code&gt;instance_number&lt;/code&gt;:&lt;/strong&gt; Joining &lt;code&gt;DBA_HIST_SQLSTAT&lt;/code&gt; to &lt;code&gt;DBA_HIST_SNAPSHOT&lt;/code&gt; on &lt;code&gt;(snap_id, dbid, instance_number)&lt;/code&gt; and summing &lt;code&gt;elapsed_time_delta&lt;/code&gt; prevents stale cumulative totals and RAC double-counting.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Giving AI agents raw SQL execution—even with read-only credentials—exposes production Oracle fleets to unindexed AWR/ASH scans, buffer-cache eviction, and context-window waste.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Deploy a &lt;code&gt;FastMCP&lt;/code&gt; gateway with dictionary-validated database routing, &lt;code&gt;oracledb&lt;/code&gt; thin-mode wallet pools, delta AWR query templates, and an Oracle Resource Manager plan using &lt;code&gt;SWITCH_GROUP =&gt; &apos;CANCEL_SQL&apos;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Verify in staging that a synthetic 20-second query submitted as &lt;code&gt;MCP_DIAG_USER&lt;/code&gt; terminates at 15 seconds with &lt;code&gt;ORA-01013&lt;/code&gt; while the connection pool remains healthy.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Create &lt;code&gt;MCP_DIAG_USER&lt;/code&gt; and &lt;code&gt;MCP_DIAG_PLAN&lt;/code&gt; on a non-production Oracle instance this week, then stand up a two-tool &lt;code&gt;FastMCP&lt;/code&gt; prototype (&lt;code&gt;get_awr_top_sql&lt;/code&gt; and &lt;code&gt;get_blocking_sessions&lt;/code&gt;) before exposing any production database to an AI assistant.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>architecture</category><category>failures</category></item><item><title>Hub-and-Spoke Network Topology for Oracle Database@Azure: Isolating Exadata in a Zero-Trust VNet Architecture</title><link>https://rajivonai.com/blog/2026-03-12-oracle-database-at-azure-vs-azure-vms/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-03-12-oracle-database-at-azure-vs-azure-vms/</guid><description>Oracle Database@Azure&apos;s delegated subnet model breaks naive flat-network designs at scale. This post details the hub-and-spoke VNet topology, Azure Firewall integration, and cross-subscription routing that enables 5,000 microservices to reach Exadata while maintaining zero-trust East-West traffic inspection.</description><pubDate>Sat, 01 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Deploying physical Exadata hardware inside a Microsoft Azure data center solves cross-cloud latency, but connecting dozens of application VNets to that cluster without creating an un-inspectable flat mesh reduces the entire problem to symmetric hub-and-spoke network design.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Oracle Database@Azure projects Exadata vNICs into an Azure Delegated Subnet (&lt;code&gt;Oracle.Database/networkAttachments&lt;/code&gt;), where direct spoke-to-database peering bypasses central firewalls and missing return User Defined Routes (UDRs) break stateful inspection.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Isolate the delegated subnet in a dedicated Database VNet peered only to a Hub VNet (or Azure Virtual WAN with Routing Intent), enforcing symmetric exact-CIDR UDRs through Azure Firewall Premium.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Delegated client subnets require at least a &lt;code&gt;/27&lt;/code&gt; CIDR (13 reserved IPs + 3 SCAN IPs + 4 IPs per VM) and cannot be resized in place; allocate a &lt;code&gt;/24&lt;/code&gt; up front and specify exact subnet prefixes in UDRs.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-03-12-oracle-database-at-azure-vs-azure-vms.svg&quot; alt=&quot;Isolate Exadata vNICs behind a symmetric hub firewall&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Oracle Database@Azure provisions OCI-managed Exadata infrastructure physically inside Microsoft Azure data centers using Azure’s Delegated Subnet model. OCI projects virtual Network Interface Cards (vNICs) directly into an empty, dedicated Azure subnet delegated to &lt;code&gt;Oracle.Database/networkAttachments&lt;/code&gt;. Because that subnet lives inside an Azure Virtual Network (VNet) in your subscription, application compute reaches Exadata over the local Azure region backbone rather than traversing public internet or cross-cloud VPN links.&lt;/p&gt;
&lt;p&gt;At small scale—one application VNet and one Exadata cluster—teams often co-locate workloads in a single VNet or wire direct VNet peering. At enterprise scale, microservices typically span dozens of spoke VNets across multiple Azure subscriptions with strict regulatory segmentation between payment, identity, and analytics tiers. Attempting to peer every spoke VNet directly to the Database VNet hits Azure’s operational routing limits and violates zero-trust East-West inspection requirements: any compromised workload in any spoke gains a direct L3 path to the Exadata client subnet without passing through a central firewall.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Network Architecture&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;VNet Complexity&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Security Posture&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Routing Manageability&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;East-West Inspection&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Flat single VNet&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Minimal&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Poor: no lateral boundary&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Simple single-VNet routing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;None&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Direct N:1 spoke-to-DB peering&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High N:1 mesh&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Poor: unfiltered path to Exadata&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Breaks down past ~30 VNets&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;None: bypasses hub firewall&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Hub-and-spoke + Azure Firewall&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Moderate: central hub&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Strong: central L4/L7 policy&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Centralized hub route tables&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Full symmetric inspection&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Azure Virtual WAN + Routing Intent&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Low UDR overhead&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Strong: automated policy routing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Automated across 50+ spokes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Full integrated inspection&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The delegated subnet model imposes three concrete network constraints that break ad-hoc peering architectures.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 1: Delegated Subnet IP Math and Immutability.&lt;/strong&gt; Oracle Database@Azure reserves a fixed pool of addresses inside the delegated client subnet: 13 reserved networking IPs, 3 Single Client Access Name (SCAN) listener IPs, and 4 IPs per Exadata VM node. A minimal 2-VM cluster therefore consumes &lt;code&gt;13 + 3 + (4 × 2) = 24&lt;/code&gt; IPs before any scale-out. Because Azure also reserves 5 IPs per subnet, a &lt;code&gt;/28&lt;/code&gt; block (16 total IPs) fails validation immediately, making &lt;code&gt;/27&lt;/code&gt; (32 IPs) the strict minimum for a client subnet and &lt;code&gt;/28&lt;/code&gt; the minimum for a backup subnet. Crucially, a delegated subnet’s CIDR cannot be expanded after creation without destroying and re-provisioning the cluster, so production deployments allocate &lt;code&gt;/24&lt;/code&gt; (256 IPs). Each VNet is also limited by default to one Oracle Database@Azure delegated subnet.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 2: Non-Transitive Peering and Route Sprawl.&lt;/strong&gt; Azure VNet peering is &lt;strong&gt;non-transitive&lt;/strong&gt;: peering Spoke A and Spoke B to a Hub VNet does not automatically route packets between Spoke A and the Database VNet without explicit forwarding and User Defined Routes (UDRs). Conversely, peering 50 spokes directly to the Database VNet (even within Azure’s 500-peering-per-VNet hard limit) forces teams to maintain 50 separate return routes and Network Security Group (NSG) exceptions, and NSGs alone lack centralized TLS inspection and audit logging.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 3: Asymmetric Routing and Summary-Route Blackholes.&lt;/strong&gt; When traffic flows from a spoke through a Hub Azure Firewall to the Exadata delegated subnet, return traffic from the Exadata vNIC must traverse the exact same firewall in reverse. If the Database VNet lacks a return UDR pointing spoke CIDRs back to &lt;code&gt;VirtualAppliance&lt;/code&gt; (the firewall’s private IP), Azure’s default system route sends return packets directly over the peering link or drops them, causing stateful TCP handshakes to hang. Furthermore, generic &lt;code&gt;/16&lt;/code&gt; summary UDRs can fail for bare-metal delegated subnets; Azure and Oracle documentation require exact delegated subnet prefixes (for example, &lt;code&gt;10.200.1.0/24&lt;/code&gt;) in UDRs and Virtual WAN Private Traffic Prefixes.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-03-12-oracle-database-at-azure-vs-azure-vms-2-constraints.svg&quot; alt=&quot;Delegated subnet IP math and asymmetric routes break flat meshes&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do platform teams connect dozens of spoke VNets across multiple subscriptions to Oracle Database@Azure with symmetric East-West inspection, preserved client source IPs, and zero-downtime growth headroom?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams evaluate four network topologies before deploying Oracle Database@Azure into production.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Isolation &amp;#x26; Audit&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Route Management&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Scale Ceiling&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Verdict&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;1. Flat Single VNet&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;None (shared L3 domain)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Trivial&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Single VNet CIDR&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected: fails PCI/SOC 2 segmentation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;2. Direct Spoke-to-DB Peering&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;NSG-only; no L7/TLS logs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Manual UDRs on every spoke&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unmanageable &gt;30 spokes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected: bypasses central firewall&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;3. Hub-and-Spoke + Azure Firewall&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Full central L4/L7 inspection&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Terraform-managed symmetric UDRs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Clean up to ~50 spokes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chosen for standard enterprise hubs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;4. Azure Virtual WAN + Routing Intent&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Full central inspection&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Automated by vWAN route manager&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;50+ spokes and multi-region&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chosen for large multi-hub estates&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Option 1 (Flat Single VNet)&lt;/strong&gt; places application VMs and the Oracle delegated subnet in the same VNet. While simple, any lateral movement inside the VNet reaches the Exadata SCAN listeners without passing through a firewall.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 2 (Direct N:1 Peering)&lt;/strong&gt; peers every spoke VNet straight to the Database VNet. It eliminates the hub hop but strips out centralized East-West inspection and forces every subnet change on the Database VNet to be replicated across dozens of spoke route tables.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 3 (Hub-and-Spoke with Azure Firewall Premium)&lt;/strong&gt; isolates the Database VNet behind a central Hub VNet. Spokes peer only to the hub, and symmetric UDRs force all SQL*Net traffic (&lt;code&gt;1521&lt;/code&gt; / &lt;code&gt;2484&lt;/code&gt;) through the firewall’s Network Rules. Because Azure Firewall Network Rules do &lt;strong&gt;not&lt;/strong&gt; SNAT RFC 1918 private destinations by default, the Exadata listener sees each microservice’s true private IP for auditing and ACLs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 4 (Azure Virtual WAN with Routing Intent)&lt;/strong&gt; replaces manual per-spoke UDRs with vWAN Routing Intent policies when an organization operates 50 or more spokes across multiple regions. Enabling &lt;code&gt;PrivateTraffic&lt;/code&gt; Routing Intent in a secured vWAN hub automatically programs routes across every connected VNet to steer RFC 1918 traffic through the vWAN hub’s Azure Firewall—eliminating the risk of a newly onboarded spoke forgetting its return route, provided the exact delegated subnet prefix (such as &lt;code&gt;10.200.1.0/24&lt;/code&gt;) is included in the vWAN Private Traffic Prefixes.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-03-12-oracle-database-at-azure-vs-azure-vms-3-options.svg&quot; alt=&quot;Hub-and-spoke or vWAN beats flat and direct-peered meshes&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-hub-and-spoke-transit-architecture-for-oracle-databaseazure&quot;&gt;The Hub-and-Spoke Transit Architecture for Oracle Database@Azure&lt;/h2&gt;
&lt;p&gt;The recommended topology treats the Database VNet as an isolated landing zone: it peers only with the Hub VNet (&lt;code&gt;allow_forwarded_traffic = true&lt;/code&gt;), never with application spokes, and contains no other VMs or shared services besides the delegated subnet.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-03-12-oracle-database-at-azure-vs-azure-vms-4-architecture.svg&quot; alt=&quot;Zero-trust transit separates spokes, hub, and Exadata&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Spoke VNet 1 — Payment Services — 10.10.1.0/24] --&gt;|VNet Peering to Hub| D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B[Spoke VNet 2 — User Auth — 10.10.2.0/24] --&gt;|VNet Peering to Hub| D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[Spoke VNet N — Analytics — 10.10.N.0/24] --&gt;|VNet Peering to Hub| D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[Hub VNet — 10.0.0.0/16] --&gt; E[Azure Firewall Premium — East-West Inspection]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[Exact UDR — 10.200.1.0/24 via Firewall]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Database VNet — 10.200.0.0/16]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[Delegated Subnet — 10.200.1.0/24 — Oracle.Database/networkAttachments]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[Exadata VM Cluster — vNICs and SCAN Listeners]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; J[Azure Bastion — Admin Jump Access]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; K[Private DNS Resolver — oracle.corp-azure.internal]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|SCAN Resolution| I&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;key-architectural-controls&quot;&gt;Key Architectural Controls&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Symmetric Exact-CIDR Routing:&lt;/strong&gt; Every spoke subnet attaches a route table sending &lt;code&gt;10.200.1.0/24&lt;/code&gt; (the exact delegated subnet CIDR) to &lt;code&gt;VirtualAppliance&lt;/code&gt; (&lt;code&gt;&amp;#x3C;firewall-private-ip&gt;&lt;/code&gt;). Symmetrically, the Database VNet’s delegated subnet route table—where supported, or the peering/hub transit table—returns every spoke CIDR to the same firewall private IP. Note that when deploying Exadata clusters, verify whether UDRs are bound directly to the delegated subnet or enforced via hub/vWAN Routing Intent according to current Azure/OCI delegated-subnet constraints.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Network Rules vs. Application Rules for SQL*Net:&lt;/strong&gt; Configure SQL*Net (&lt;code&gt;1521&lt;/code&gt; cleartext, &lt;code&gt;2484&lt;/code&gt; TCPS/TLS) exclusively in Azure Firewall &lt;strong&gt;Network Rules&lt;/strong&gt;. Azure Firewall Network Rules preserve original RFC 1918 source IPs without SNAT, whereas Application Rules act as a transparent proxy and always apply SNAT. Note also that Azure Firewall Premium’s TLS inspection targets HTTP/S application traffic rather than Oracle’s native TCPS protocol; end-to-end encryption for database sessions is enforced by Oracle Net Services (&lt;code&gt;TCP.VALIDNODE_CHECKING&lt;/code&gt; and Oracle Wallet mutual TLS on port &lt;code&gt;2484&lt;/code&gt;), while the firewall enforces L3/L4 segmentation and centralized flow logging.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Private DNS Resolution for SCAN:&lt;/strong&gt; Applications connect to the Exadata cluster using a Private DNS hostname (e.g., &lt;code&gt;prod-billing.oracle.corp-azure.internal&lt;/code&gt;) backed by the three SCAN listener A-records inside the &lt;code&gt;/24&lt;/code&gt; delegated subnet. Using an Azure-specific DNS suffix prevents split-horizon collisions with legacy on-premises DNS zones.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cross-Subscription Peering Automation:&lt;/strong&gt; When spokes live in separate Azure subscriptions, manual peering approvals turn the Hub networking team into a ticket bottleneck. Platform teams automate peering with an Azure Policy &lt;code&gt;deployIfNotExists&lt;/code&gt; assignment or a shared Terraform spoke module backed by a narrowly scoped service principal holding &lt;code&gt;Network Contributor&lt;/code&gt; permissions solely on the Hub VNet peering resource.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cross-Region Data Guard Separation:&lt;/strong&gt; In multi-region deployments, each Azure region hosts its own Hub VNet and isolated Database VNet. Cross-region Oracle Data Guard redo shipping between Exadata clusters travels over Azure/OCI cross-region backbone connectivity rather than hairpinning through the Hub Firewall, preventing standby redo transport from saturating the regional firewall’s throughput ceiling.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Because a delegated subnet CIDR cannot be resized after an Exadata cluster is deployed, network validation must complete in four gates before OCI provisions hardware.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-03-12-oracle-database-at-azure-vs-azure-vms-5-flow.svg&quot; alt=&quot;Validate transit routing before provisioning Exadata&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Gate 1 — Subscription Feature Registration &amp;#x26; CIDR Reservation:&lt;/strong&gt; Register Azure Advanced Network Features (if required by your topology) before creating the delegated subnet, and reserve a non-overlapping &lt;code&gt;/24&lt;/code&gt; CIDR that collides with neither spoke VNets nor on-premises ExpressRoute/VPN prefixes. Rollback: destroy empty subnet before cluster creation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate 2 — Hub Firewall &amp;#x26; Bi-Directional Peering:&lt;/strong&gt; Deploy &lt;code&gt;AzureFirewallSubnet&lt;/code&gt; (&lt;code&gt;/26&lt;/code&gt; minimum), Azure Firewall Premium, and bi-directional Hub-to-Database VNet peering with &lt;code&gt;allow_forwarded_traffic = true&lt;/code&gt;. Rollback: remove peering links via Terraform.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate 3 — Symmetric UDRs, Firewall Rules &amp;#x26; Private DNS:&lt;/strong&gt; Apply exact-prefix UDRs on spokes and return routes toward the firewall, add TCP &lt;code&gt;1521&lt;/code&gt;/&lt;code&gt;2484&lt;/code&gt; Network Rules, and link the Private DNS zone to both Hub and Spokes. Rollback: revert Terraform route-table and policy state.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate 4 — Pre-Provisioning Probe Validation:&lt;/strong&gt; Before delegating the subnet permanently to Exadata (using a temporary non-delegated test subnet in the same Database VNet CIDR, since a delegated subnet must remain empty), verify round-trip TCP &lt;code&gt;1521&lt;/code&gt;/&lt;code&gt;2484&lt;/code&gt; reachability from a spoke pod/VM through the Hub Firewall and confirm firewall logs record the spoke’s true private IP. Rollback: fix asymmetric routes before invoking Oracle Database@Azure provisioning.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Based on Microsoft Azure’s Hub-and-Spoke reference architecture and Oracle Database@Azure’s delegated subnet specification, the following Terraform blocks establish the Hub VNet, Firewall, delegated subnet, bi-directional peering, and DNS records.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Hub VNet &amp;#x26; Azure Firewall Premium&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_virtual_network&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;hub&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;vnet-oracle-hub-prod&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  resource_group_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_resource_group&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;hub&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  location&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;azure_region&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  address_space&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;10.0.0.0/16&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_subnet&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;firewall&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                 =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;AzureFirewallSubnet&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  resource_group_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_resource_group&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;hub&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  virtual_network_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_virtual_network&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;hub&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  address_prefixes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;     =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;10.0.1.0/26&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# /26 minimum required by Azure Firewall&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_firewall&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;hub&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;fw-oracle-hub-prod&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  resource_group_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_resource_group&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;hub&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  location&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;azure_region&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  sku_tier&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Premium&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  sku_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;AZFW_VNet&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  ip_configuration&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                 =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;primary&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subnet_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_subnet&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;firewall&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    public_ip_address_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_public_ip&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;firewall&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Database VNet &amp;#x26; Empty /24 Delegated Subnet&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_virtual_network&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;database&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;vnet-oracle-database-prod&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  resource_group_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_resource_group&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;database&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  location&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;azure_region&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  address_space&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;10.200.0.0/16&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_subnet&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;oracle_delegated&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                 =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;snet-oracle-exadata-delegated&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  resource_group_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_resource_group&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;database&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  virtual_network_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_virtual_network&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;database&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  address_prefixes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;     =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;10.200.1.0/24&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  delegation&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;oracle-database-delegation&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    service_delegation&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Oracle.Database/networkAttachments&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      actions&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;Microsoft.Network/networkinterfaces/*&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;Microsoft.Network/virtualNetworks/subnets/join/action&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Bi-directional peering and Firewall Network Rules complete the transit path while keeping RFC 1918 source IPs intact:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_virtual_network_peering&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;hub_to_database&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                      =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;peer-hub-to-database&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  resource_group_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_resource_group&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;hub&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  virtual_network_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_virtual_network&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;hub&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  remote_virtual_network_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_virtual_network&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;database&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  allow_forwarded_traffic&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;   =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_virtual_network_peering&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;database_to_hub&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                      =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;peer-database-to-hub&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  resource_group_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_resource_group&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;database&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  virtual_network_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_virtual_network&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;database&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  remote_virtual_network_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_virtual_network&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;hub&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  allow_forwarded_traffic&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;   =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_firewall_policy_rule_collection_group&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;oracle&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;               =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;oracle-database-access&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  firewall_policy_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_firewall_policy&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;hub&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  priority&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;           =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  network_rule_collection&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;     =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;oracle-sqlnet&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    priority&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    action&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;   =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Allow&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    rule&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                  =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;allow-sqlnet-payments&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      source_addresses&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;10.10.1.0/24&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      destination_addresses&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;10.200.1.0/24&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      destination_ports&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;     =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;1521&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;2484&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      protocols&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;             =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;TCP&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Oracle’s Real Application Clusters architecture distributes client connections across Exadata compute nodes using the three SCAN listener IPs assigned from &lt;code&gt;10.200.1.0/24&lt;/code&gt;; referencing the SCAN hostname in Azure Private DNS rather than individual node VIPs keeps JDBC strings stable across rolling patches. In practice, set a low DNS TTL (such as 300 seconds) on the Private DNS A record, link the private DNS zone to both the Hub VNet and every spoke VNet with &lt;code&gt;registration_enabled = false&lt;/code&gt;, and configure application connection pools (such as Oracle UCP or HikariCP) with fast connection validation and retry backoff so transient listener handoffs during Exadata patching do not surface as application errors.&lt;/p&gt;
&lt;p&gt;Equally important, keep the &lt;code&gt;snet-oracle-exadata-delegated&lt;/code&gt; subnet strictly empty prior to cluster creation. Because Azure’s &lt;code&gt;Oracle.Database/networkAttachments&lt;/code&gt; delegation reserves the subnet exclusively for OCI-projected network interfaces, placing jump hosts, Azure Bastion, or Private Endpoints inside the delegated subnet causes cluster provisioning validation to fail; all supporting hub infrastructure belongs in separate subnets inside the Hub VNet.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Hub-and-spoke transit architectures fail most often at route-table drift, CIDR sizing, and DNS boundaries.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-03-12-oracle-database-at-azure-vs-azure-vms-6-failures.svg&quot; alt=&quot;Where Oracle Database@Azure transit networks break&quot;&gt;&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure Mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Missing Return UDR on New Spoke&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;New spoke added without return route to firewall; asymmetric return drops TCP ACK.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Spoke TCP handshake hangs; firewall logs SYN with no return flow.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Generate return routes via shared Terraform spoke module and CI check.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Summary Route Blackhole&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Generic &lt;code&gt;/16&lt;/code&gt; UDR fails to steer bare-metal delegated subnet traffic properly.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Packets drop or bypass hub firewall despite VNet-level route entry.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Specify exact &lt;code&gt;/24&lt;/code&gt; delegated subnet CIDR in UDRs and vWAN prefixes.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Undersized Delegated Subnet&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Subnet smaller than &lt;code&gt;/27&lt;/code&gt; (or full &lt;code&gt;/27&lt;/code&gt;) blocks cluster creation or node scale-out.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;OCI provisioning fails with insufficient IP addresses in delegated subnet.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Allocate &lt;code&gt;/24&lt;/code&gt; up front; delegated subnet CIDRs cannot resize in place.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Split-Horizon DNS Conflict&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Shared internal zone resolves SCAN hostname to unreachable on-premises IP.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Intermittent connection timeouts aligned with DNS TTL expiration.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use an Azure-unique DNS suffix (&lt;code&gt;oracle.corp-azure.internal&lt;/code&gt;) + Resolver.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Firewall Rule Ordering Shadow&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High-priority deny rule blocks SQL*Net before lower-priority allow rule evaluates.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Connections fail with TCP RST while Oracle listener logs show no attempt.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run automated firewall policy connectivity audits on every PR.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Non-Empty Delegated Subnet&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Placing a test VM or private endpoint inside the delegated subnet breaks validation.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;OCI deployment rejects subnet delegation check.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep the delegated subnet strictly empty; test in a sibling subnet first.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Azure VNet peering is non-transitive; connecting multiple spoke VNets to Oracle Database@Azure securely requires routing through a central Hub Firewall or Azure Virtual WAN with Routing Intent.&lt;/li&gt;
&lt;li&gt;Delegated client subnets (&lt;code&gt;Oracle.Database/networkAttachments&lt;/code&gt;) consume 13 reserved IPs + 3 SCAN IPs + 4 IPs per VM (24 IPs minimum for 2 VMs), making &lt;code&gt;/27&lt;/code&gt; the hard minimum and &lt;code&gt;/24&lt;/code&gt; the practical production standard since CIDRs cannot expand in place.&lt;/li&gt;
&lt;li&gt;Configure SQL*Net (&lt;code&gt;1521&lt;/code&gt;, &lt;code&gt;2484&lt;/code&gt;) in Azure Firewall Network Rules rather than Application Rules so private RFC 1918 source IPs pass through without SNAT.&lt;/li&gt;
&lt;li&gt;Always specify the exact delegated subnet prefix (such as &lt;code&gt;/24&lt;/code&gt;) in UDRs and Virtual WAN Private Traffic Prefixes rather than relying solely on &lt;code&gt;/16&lt;/code&gt; VNet summaries.&lt;/li&gt;
&lt;li&gt;Keep the delegated subnet completely empty prior to cluster provisioning and validate symmetric routing from a sibling test subnet first.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Flat VNets and direct N:1 spoke peering to Oracle Database@Azure either expose Exadata to unfiltered lateral traffic or create unmanageable asymmetric routing failures.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Isolate the &lt;code&gt;/24&lt;/code&gt; delegated subnet in a dedicated Database VNet peered only to a Hub VNet (or vWAN Hub), enforce symmetric exact-CIDR UDRs through Azure Firewall Premium Network Rules, and resolve SCAN listeners via Azure Private DNS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Verify in Azure Firewall logs that test TCP &lt;code&gt;1521&lt;/code&gt;/&lt;code&gt;2484&lt;/code&gt; flows from spoke subnets reach the Database VNet symmetrically with the spoke’s original RFC 1918 source IP intact before provisioning the Exadata cluster.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your target Oracle Database@Azure delegated subnet this week: confirm it is at least &lt;code&gt;/24&lt;/code&gt;, completely empty, and paired with exact-prefix UDRs on both the spoke and hub transit paths.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>failures</category></item><item><title>Major Version Oracle Fleet Upgrades: Wrapping AutoUpgrade in Temporal State Machines</title><link>https://rajivonai.com/blog/2026-02-18-oracle-19c-26ai-autoupgrade-factory/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-02-18-oracle-19c-26ai-autoupgrade-factory/</guid><description>Oracle AutoUpgrade.jar handles the dictionary mechanics of 19c-to-26ai upgrades beautifully on a single node. At fleet scale — 3,000 databases — it needs a distributed state machine wrapper, fleet-wide pre-flight analysis, and automated flashback rollback to become operationally viable.</description><pubDate>Fri, 31 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A major version Oracle fleet upgrade is a distributed state machine, and executing &lt;code&gt;autoupgrade.jar&lt;/code&gt; across 3,000 databases from terminal sessions turns the DBA team into an unscalable coordination layer.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; &lt;code&gt;autoupgrade.jar&lt;/code&gt; automates single-host 19c-to-26ai dictionary upgrades, but lacks fleet-wide pre-check aggregation, systemic failure halts, storage-domain concurrency limits, and unattended parallel flashback rollback.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Keep &lt;code&gt;autoupgrade.jar&lt;/code&gt; as the local execution engine on each host and wrap it in a Temporal workflow control plane that parses &lt;code&gt;status.json&lt;/code&gt;, gates rollouts on &lt;code&gt;&amp;#x3C;10%&lt;/code&gt; systemic errors, caps concurrent upgrades per storage cluster, and automates Guaranteed Restore Point (GRP) flashback on failure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Non-CDB databases are desupported in Oracle AI Database 26ai, so any 19c non-CDB must convert to a PDB during the upgrade, and AutoUpgrade requires a matching Java 11 runtime plus updated OEM/RMAN &lt;code&gt;$ORACLE_HOME&lt;/code&gt; paths afterward.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-02-18-oracle-19c-26ai-autoupgrade-factory.svg&quot; alt=&quot;Wrap AutoUpgrade in a Temporal state machine at fleet scale&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Under Oracle’s Lifetime Support Policy (MOS Doc ID 742060.1), Oracle Database 19c Premier Support runs through December 31, 2029, with Extended Support running from January 1, 2030 through December 31, 2032. Moving a multi-thousand-database estate from 19c to Oracle AI Database 26ai is a multi-year engineering program, and starting in 26ai, traditional manual upgrade scripts (&lt;code&gt;catctl.pl&lt;/code&gt; / DBUA-style manual paths) are desupported in favor of &lt;code&gt;autoupgrade.jar&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Oracle’s &lt;code&gt;autoupgrade.jar&lt;/code&gt; is the right local engine: it runs &lt;code&gt;analyze&lt;/code&gt; pre-checks, executes &lt;code&gt;fixups&lt;/code&gt;, creates Guaranteed Restore Points (&lt;code&gt;restoration=yes&lt;/code&gt;), upgrades the data dictionary, converts non-CDBs to PDBs, and emits machine-readable JSON status files. Where it stops is the fleet boundary. Each invocation manages the databases listed in its local configuration file on that host or reachable SSH targets, leaving cross-fleet aggregation, global halt thresholds, storage-domain throttling, and post-upgrade OEM/RMAN reconfiguration to external orchestration.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;At 3,000 databases, manual or ad-hoc scripted coordination breaks on four explicit operational constraints.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. Maintenance-window concurrency math.&lt;/strong&gt; Assume an average end-to-end major version upgrade—pre-checks, GRP creation, dictionary compilation, &lt;code&gt;utlrp.sql&lt;/code&gt; recompilation, and smoke tests—takes &lt;code&gt;2.0&lt;/code&gt; hours per database. Upgrading 3,000 databases serially consumes &lt;code&gt;6,000&lt;/code&gt; database-hours. Even if an enterprise schedules 12 quarterly 8-hour maintenance windows (&lt;code&gt;96&lt;/code&gt; total window hours), finishing the fleet requires &lt;code&gt;6,000 / 96 = 62.5&lt;/code&gt; databases upgraded concurrently in every single window—and closer to 100 concurrent jobs if you account for soak time and rollbacks. Human operators watching SSH terminals saturate well before 10 concurrent sessions per DBA.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Systemic pre-check blind spots.&lt;/strong&gt; If 800 of 3,000 databases fail &lt;code&gt;analyze&lt;/code&gt; due to a shared blocker—such as deprecated &lt;code&gt;CTXSYS&lt;/code&gt; (Oracle Text) objects or an undersized &lt;code&gt;SYSTEM&lt;/code&gt; tablespace—per-host text logs obscure the fleet-wide pattern. Without central JSON aggregation, teams discover the systemic defect piecemeal during live maintenance windows instead of halting early and shipping one automated pre-fixup.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. Fast Recovery Area (FRA) headroom for GRPs.&lt;/strong&gt; A Guaranteed Restore Point (&lt;code&gt;CREATE RESTORE POINT ... GUARANTEE FLASHBACK DATABASE&lt;/code&gt;) does not require &lt;code&gt;ALTER DATABASE FLASHBACK ON&lt;/code&gt; beforehand, but it does require &lt;code&gt;ARCHIVELOG&lt;/code&gt; mode and a configured FRA (&lt;code&gt;DB_RECOVERY_FILE_DEST&lt;/code&gt;) large enough to retain flashback logs plus archived redo for the entire upgrade and soak window. If a busy OLTP primary generates &lt;code&gt;50 MB/s&lt;/code&gt; of redo during dictionary upgrade or post-upgrade soak, 8 hours of retention consumes &lt;code&gt;50 * 3600 * 8 / 1024 = 1,406 GB&lt;/code&gt; of FRA space. Filling the FRA to 100% stalls the database mid-upgrade.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. Shared storage I/O and post-upgrade path drift.&lt;/strong&gt; Running 50 concurrent dictionary upgrades on a single shared NFS or SAN storage pod saturates IOPS and stalls every database in that pod. Furthermore, &lt;code&gt;autoupgrade.jar&lt;/code&gt; updates &lt;code&gt;oratab&lt;/code&gt; and the database home, but leaves systemd units, OEM agent target properties, and RMAN catalog scripts pointing at the old 19c &lt;code&gt;$ORACLE_HOME&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-02-18-oracle-19c-26ai-autoupgrade-factory-2-constraints.svg&quot; alt=&quot;Fleet upgrades fail on coordination math, not dictionary SQL&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do you coordinate 3,000 &lt;code&gt;autoupgrade.jar&lt;/code&gt; executions across bounded maintenance windows with systemic failure halts, per-storage concurrency caps, and automated GRP flashback rollback?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Approach&lt;/th&gt;&lt;th&gt;Fleet Size Viable&lt;/th&gt;&lt;th&gt;Durable State&lt;/th&gt;&lt;th&gt;Systemic Halt&lt;/th&gt;&lt;th&gt;Why Chosen or Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Manual &lt;code&gt;autoupgrade.jar&lt;/code&gt; via SSH&lt;/td&gt;&lt;td&gt;&amp;#x3C;50 databases&lt;/td&gt;&lt;td&gt;None (local terminal)&lt;/td&gt;&lt;td&gt;None&lt;/td&gt;&lt;td&gt;Rejected — saturates DBA headcount and misses systemic failures&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Ansible playbooks calling AutoUpgrade&lt;/td&gt;&lt;td&gt;&amp;#x3C;500 databases&lt;/td&gt;&lt;td&gt;Lost if control node crashes&lt;/td&gt;&lt;td&gt;Basic play failure count&lt;/td&gt;&lt;td&gt;Rejected — weak mid-upgrade state recovery and no per-pod semaphores&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Temporal workflow + host agent&lt;/td&gt;&lt;td&gt;&gt;1,000 databases&lt;/td&gt;&lt;td&gt;Durable event history&lt;/td&gt;&lt;td&gt;Global JSON aggregation&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Chosen&lt;/strong&gt; — survives coordinator restarts and enforces phase gates&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OCI / Autonomous managed upgrades&lt;/td&gt;&lt;td&gt;Cloud-only subset&lt;/td&gt;&lt;td&gt;Vendor-managed&lt;/td&gt;&lt;td&gt;Vendor-managed&lt;/td&gt;&lt;td&gt;Rejected for fleet — does not cover on-premises or self-managed VMs&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-02-18-oracle-19c-26ai-autoupgrade-factory-3-options.svg&quot; alt=&quot;Temporal plus AutoUpgrade scales past Ansible and SSH&quot;&gt;&lt;/p&gt;
&lt;p&gt;Ansible works well for stateless configuration pushes, but a 2-to-4-hour database upgrade is a long-running stateful workflow. If the Ansible controller restarts mid-play, recovering which of 500 databases are in &lt;code&gt;analyze&lt;/code&gt;, &lt;code&gt;deploy&lt;/code&gt;, or &lt;code&gt;flashback&lt;/code&gt; requires brittle idempotency scripts and manual inventory reconciliation. Temporal persists every activity transition in durable event history, supports per-storage-domain task queues or rate limits, and pauses cleanly at human approval gates without holding open SSH connections. Direct SSH with &lt;code&gt;autoupgrade.jar&lt;/code&gt; fails for the opposite reason: while AutoUpgrade supports multi-database configs on a single host or over SSH, it does not aggregate findings across thousands of independent hosts, enforce storage-cluster concurrency semaphores, or orchestrate external OEM and RMAN updates. Cloud-managed upgrades handle the control plane automatically on OCI Autonomous Database or Exadata Cloud Service, but leave on-premises and self-managed VM estates uncovered.&lt;/p&gt;
&lt;h2 id=&quot;the-autoupgrade-fleet-orchestration-architecture&quot;&gt;The AutoUpgrade Fleet Orchestration Architecture&lt;/h2&gt;
&lt;p&gt;The architecture keeps &lt;code&gt;autoupgrade.jar&lt;/code&gt; untouched as the local database upgrade engine while placing Temporal above it as the global state machine. A lightweight host agent (Go or Python systemd service) translates Temporal activities into &lt;code&gt;autoupgrade.jar&lt;/code&gt; invocations, reads AutoUpgrade’s structured JSON output, and handles surrounding OS/OEM/RMAN updates.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-02-18-oracle-19c-26ai-autoupgrade-factory-4-architecture.svg&quot; alt=&quot;Temporal owns fleet state while host agents run AutoUpgrade&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Fleet Upgrade Catalog — 3000 DBs targeting 26ai] --&gt; B[Temporal Control Plane — durable orchestrator]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[Phase 1 — Analyze across fleet]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D{Systemic check — more than 10pct failed?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Yes — halt wave| E[Platform team ships global fixup]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|No — proceed| F[Phase 2 — Fixups and FRA pre-flight]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Phase 3 — Batched deploy max 20 per storage pod]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H{Upgrade exit and V$INSTANCE check}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|Pass| I[Phase 4 — Update OEM RMAN and drop GRP]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|Fail or timeout| J[Automated GRP flashback to 19c]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;AutoUpgrade configuration and structured JSON parsing.&lt;/strong&gt; AutoUpgrade configuration files use key-value property syntax (&lt;code&gt;global.*&lt;/code&gt; and &lt;code&gt;&amp;#x3C;prefix&gt;.*&lt;/code&gt;), not INI section brackets. The host agent writes a clean config per database, invokes &lt;code&gt;autoupgrade.jar&lt;/code&gt; with &lt;code&gt;-mode analyze -noconsole&lt;/code&gt; using Java 11 (bundled in Oracle 26ai homes), and reads &lt;code&gt;status.json&lt;/code&gt; from &lt;code&gt;&amp;#x3C;global.autoupg_log_dir&gt;/cfgtoollogs/upgrade/auto/status/status.json&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; subprocess&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pathlib &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Path&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;AUTOUPGRADE_JAR&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;/u01/app/oracle/product/autoupgrade/autoupgrade.jar&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;LOG_DIR&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;/u01/autoupgrade_logs&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; write_autoupgrade_config&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(db_sid: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, source_home: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, target_home: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; Path:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cfg &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;global.autoupg_log_dir=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{LOG_DIR}\n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;upg1.sid=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db_sid&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}\n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;upg1.source_home=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;source_home&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}\n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;upg1.target_home=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;target_home&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}\n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;upg1.start_time=NOW&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cfg_path &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Path(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/tmp/autoupgrade_&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db_sid&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.cfg&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cfg_path.write_text(cfg)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cfg_path&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; run_analyze&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(db_sid: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, source_home: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, target_home: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;dict&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cfg_path &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; write_autoupgrade_config(db_sid, source_home, target_home)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    proc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; subprocess.run(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;java&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-jar&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;AUTOUPGRADE_JAR&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-config&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(cfg_path), &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-mode&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;analyze&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-noconsole&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        capture_output&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1800&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    status_path &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Path(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;LOG_DIR&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;cfgtoollogs&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;upgrade&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;auto&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;status&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;status.json&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; status_path.exists():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        data &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; json.loads(status_path.read_text())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;db_sid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: db_sid, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;returncode&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: proc.returncode, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;status&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: data}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;db_sid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: db_sid, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;returncode&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: proc.returncode, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;stderr&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: proc.stderr[:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;500&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Temporal workflow with systemic halt and storage-pod batching.&lt;/strong&gt; Temporal runs &lt;code&gt;analyze&lt;/code&gt; across the catalog first, halts automatically if more than 10% of databases report blocking findings, and schedules &lt;code&gt;deploy&lt;/code&gt; activities onto per-storage-cluster task queues capped at 20 concurrent upgrades:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datetime &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; timedelta&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; asyncio&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; temporalio &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;@workflow.defn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; FleetUpgradeWorkflow&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    @workflow.run&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    async&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; run&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(self, db_specs: list[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;dict&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], target_home: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;dict&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        analyze_results &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; asyncio.gather(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            workflow.execute_activity(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;analyze_database&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                args&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[spec[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], spec[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;source_home&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], target_home],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                start_to_close_timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;timedelta(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;minutes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;45&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; spec &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db_specs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        ])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        failed &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [r &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; analyze_results &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;returncode&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(failed) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; max&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(analyze_results), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0.10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;halted&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;failed_count&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(failed)}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(db_specs), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            wave &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db_specs[i : i &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 50&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; asyncio.gather(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                workflow.execute_activity(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                    &quot;deploy_with_grp_rollback&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                    args&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[spec[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], spec[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;source_home&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], target_home],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                    task_queue&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;storage-pod-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;spec[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;storage_pod&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;                    start_to_close_timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;timedelta(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;hours&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; spec &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; wave&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            ], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;return_exceptions&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;halted&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;waves_completed&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(db_specs) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 49&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 50&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;FRA pre-flight and automated GRP flashback.&lt;/strong&gt; Before &lt;code&gt;deploy&lt;/code&gt; starts, the host agent checks &lt;code&gt;V$RECOVERY_FILE_DEST&lt;/code&gt; and recent redo volume in &lt;code&gt;V$ARCHIVED_LOG&lt;/code&gt;. If &lt;code&gt;fra_free_gb&lt;/code&gt; is below 8 hours of peak redo, the database is marked &lt;code&gt;BLOCKED_FRA_INSUFFICIENT&lt;/code&gt; and skipped. Because AutoUpgrade creates a Guaranteed Restore Point by default (&lt;code&gt;restoration=yes&lt;/code&gt;), or the wrapper creates a named GRP in &lt;code&gt;ARCHIVELOG&lt;/code&gt; mode, an unattended non-zero exit triggers immediate flashback via standard &lt;code&gt;sqlplus&lt;/code&gt; heredoc (never &lt;code&gt;sqlplus -e&lt;/code&gt;, which is not a valid flag):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ROUND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(space_limit &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fra_total_gb,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       ROUND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((space_limit &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; space_used) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fra_free_gb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;   v$recovery_file_dest;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [[ $UPGRADE_EXIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-ne&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ]]; &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;then&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  sqlplus&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -S&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; as&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; sysdba&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;SHUTDOWN ABORT;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;STARTUP MOUNT;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;FLASHBACK DATABASE TO RESTORE POINT ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;GRP_NAME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;ALTER DATABASE OPEN RESETLOGS;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;DROP RESTORE POINT ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;GRP_NAME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EXIT;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;fi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Idempotency, non-CDB conversion, and ancillary reconfiguration.&lt;/strong&gt; Two details determine whether an automated wave survives partial failures. First, every &lt;code&gt;deploy&lt;/code&gt; activity must begin by querying &lt;code&gt;SELECT version FROM v$instance&lt;/code&gt; before invoking &lt;code&gt;autoupgrade.jar&lt;/code&gt;. If a network partition drops the activity completion signal after the database has already opened on 26ai, Temporal retries the activity; checking &lt;code&gt;V$INSTANCE&lt;/code&gt; prevents &lt;code&gt;autoupgrade.jar&lt;/code&gt; from failing with a version mismatch on an already-upgraded instance. Second, for legacy 19c non-CDB databases, the host agent sets &lt;code&gt;upg1.target_cdb=&amp;#x3C;cdb_sid&gt;&lt;/code&gt; in the AutoUpgrade configuration so AutoUpgrade converts the non-CDB into a Pluggable Database inside the 26ai CDB in the same workflow rather than leaving an unsupported architecture behind. Finally, after &lt;code&gt;autoupgrade.jar&lt;/code&gt; returns zero, a separate post-upgrade activity updates &lt;code&gt;/etc/oratab&lt;/code&gt;, systemd unit &lt;code&gt;Environment=ORACLE_HOME=...&lt;/code&gt; overrides, OEM agent target properties via the OEM REST API, and RMAN catalog scripts so nightly Level 1 backups and monitoring probes do not fail against the retired 19c home.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;A 3,000-database upgrade factory rolls out in four gated phases so that systemic defects are caught before any production downtime window opens.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-02-18-oracle-19c-26ai-autoupgrade-factory-5-flow.svg&quot; alt=&quot;Four gated phases protect production before dictionary edits&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 1: Fleet-wide Analyze and FRA Audit.&lt;/strong&gt; Run &lt;code&gt;autoupgrade.jar -mode analyze -noconsole&lt;/code&gt; and the &lt;code&gt;V$RECOVERY_FILE_DEST&lt;/code&gt; check across all 3,000 databases online with zero downtime. The Temporal aggregator groups findings by AutoUpgrade check name (for example, &lt;code&gt;INVALID_OBJECTS_EXIST&lt;/code&gt; or &lt;code&gt;TABLESPACE_AUTOEXTEND&lt;/code&gt;) so platform engineers see whether an error affects 3 databases or 800. Gate: &lt;code&gt;&amp;#x3C;10%&lt;/code&gt; systemic blockers and every target database passes 8-hour FRA headroom. Rollback: none needed; read-only phase.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 2: Automated Fixups and Non-CDB Conversion Prep.&lt;/strong&gt; Execute &lt;code&gt;-mode fixups&lt;/code&gt; to clear stale invalid objects, purge recyclebin, and gather dictionary statistics; flag any remaining 19c non-CDBs for target CDB PDB conversion (since non-CDB is desupported in Oracle 26ai) and pre-provision the target 26ai &lt;code&gt;$ORACLE_HOME&lt;/code&gt; binaries on every host. Gate: &lt;code&gt;0&lt;/code&gt; unresolved &lt;code&gt;ERROR&lt;/code&gt;-severity findings in &lt;code&gt;status.json&lt;/code&gt; and verified JRE 11 availability in the target home. Rollback: re-run pre-checks after manual DBA remediation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 3: Wave-Gated Deploy with Storage Caps.&lt;/strong&gt; Execute &lt;code&gt;-mode deploy&lt;/code&gt; in waves of 50 databases, throttled by Temporal worker &lt;code&gt;max_concurrent_activities=20&lt;/code&gt; per storage pod so shared SAN or NFS arrays never hit queue saturation. The host agent polls &lt;code&gt;V$RECOVERY_FILE_DEST&lt;/code&gt; every 5 minutes during dictionary compilation and triggers automated cloud volume expansion if FRA usage crosses 80%. Gate: &lt;code&gt;V$INSTANCE.VERSION&lt;/code&gt; matches target 26ai and &lt;code&gt;DBA_REGISTRY&lt;/code&gt; components are &lt;code&gt;VALID&lt;/code&gt;. Rollback: non-zero exit, 90% FRA critical threshold, or activity timeout triggers automated &lt;code&gt;FLASHBACK DATABASE TO RESTORE POINT&lt;/code&gt; to restore 19c within minutes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Phase 4: Ancillary Reconfiguration and GRP Drop.&lt;/strong&gt; Update OEM agent target &lt;code&gt;$ORACLE_HOME&lt;/code&gt; properties, RMAN catalog scripts, and systemd units, run application smoke queries, and drop the GRP only after a 24-hour soak window so FRA space is reclaimed safely. Keeping the Guaranteed Restore Point active during that 24-hour window gives application owners a deterministic rollback path if subtle execution-plan regressions surface under morning batch or OLTP peak traffic, though monitoring &lt;code&gt;V$RECOVERY_FILE_DEST&lt;/code&gt; remains mandatory until &lt;code&gt;DROP RESTORE POINT&lt;/code&gt; runs. Gate: 100% OEM target green status, zero ORA-00600/ORA-07445 alert log entries during soak, and successful RMAN Level 1 backup under the 26ai home. Rollback: keep the GRP active until the 24-hour soak clears; after dropping the GRP, rollback requires RMAN restore.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Oracle 26ai AutoUpgrade mandate (Pattern A):&lt;/strong&gt; Per Oracle Database 26ai Upgrade documentation, manual upgrade scripts are desupported in 26ai and &lt;code&gt;autoupgrade.jar&lt;/code&gt; is the required upgrade mechanism; non-CDB architecture is also desupported, requiring 19c non-CDBs to convert to PDBs during upgrade.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AutoUpgrade Java 11 requirement (Pattern A):&lt;/strong&gt; Per Oracle’s AutoUpgrade documentation, &lt;code&gt;autoupgrade.jar&lt;/code&gt; is compiled with Java 11 and Oracle 26ai homes bundle JRE 11, avoiding older JDK mismatches when invoked from the target home.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Guaranteed Restore Point behavior without &lt;code&gt;FLASHBACK ON&lt;/code&gt; (Pattern B):&lt;/strong&gt; Per Oracle Database Backup and Recovery documentation, &lt;code&gt;CREATE RESTORE POINT ... GUARANTEE FLASHBACK DATABASE&lt;/code&gt; succeeds in &lt;code&gt;ARCHIVELOG&lt;/code&gt; mode with a configured FRA even when &lt;code&gt;V$DATABASE.FLASHBACK_ON&lt;/code&gt; is &lt;code&gt;NO&lt;/code&gt;; Oracle writes only the flashback logs needed to rewind to that point.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Temporal durable execution (Pattern B):&lt;/strong&gt; Temporal worker task queues naturally isolate storage domains: assigning one task queue per storage pod with &lt;code&gt;max_concurrent_activities=20&lt;/code&gt; prevents 50 parallel dictionary upgrades from saturating shared SAN/NFS bandwidth, and survives orchestrator restarts without losing per-database state.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Breaks How&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;FRA exhaustion mid-upgrade&lt;/td&gt;&lt;td&gt;Heavy redo fills FRA past 95% while GRP retains flashback logs; upgrade hangs&lt;/td&gt;&lt;td&gt;Poll &lt;code&gt;V$RECOVERY_FILE_DEST&lt;/code&gt; every 5 min; alert at 80% used&lt;/td&gt;&lt;td&gt;Auto-expand FRA via cloud storage API at 80%; abort and flashback at 90%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Activity retry after network drop&lt;/td&gt;&lt;td&gt;Upgrade finishes on host, network drops ack, Temporal retries &lt;code&gt;deploy&lt;/code&gt; on 26ai DB&lt;/td&gt;&lt;td&gt;Agent checks &lt;code&gt;SELECT version FROM v$instance&lt;/code&gt; before running JAR&lt;/td&gt;&lt;td&gt;Return idempotent success if &lt;code&gt;V$INSTANCE&lt;/code&gt; is already at target 26ai version&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;status.json&lt;/code&gt; schema drift&lt;/td&gt;&lt;td&gt;Newer &lt;code&gt;autoupgrade.jar&lt;/code&gt; release changes JSON keys; parser misclassifies success&lt;/td&gt;&lt;td&gt;CI contract test runs parser against pinned JAR sample outputs&lt;/td&gt;&lt;td&gt;Pin &lt;code&gt;autoupgrade.jar&lt;/code&gt; SHA-256 in host image; version the parser schema&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Storage pod I/O saturation&lt;/td&gt;&lt;td&gt;50 parallel dictionary upgrades on one storage array spike &lt;code&gt;db file sequential read&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Storage latency &gt;20ms or Temporal activity duration &gt;2x baseline&lt;/td&gt;&lt;td&gt;Cap Temporal worker concurrency at 20 per storage pod task queue&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;$ORACLE_HOME&lt;/code&gt; path drift&lt;/td&gt;&lt;td&gt;Post-upgrade OEM agents and RMAN scripts still reference 19c home path&lt;/td&gt;&lt;td&gt;OEM target down alert or first post-upgrade RMAN backup failure&lt;/td&gt;&lt;td&gt;Run Phase 4 OEM/RMAN reconfiguration and test Level 1 backup before closing wave&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Non-CDB conversion skipped&lt;/td&gt;&lt;td&gt;Legacy 19c non-CDB attempts direct upgrade to 26ai where non-CDB is desupported&lt;/td&gt;&lt;td&gt;&lt;code&gt;analyze&lt;/code&gt; flags non-CDB target incompatibility&lt;/td&gt;&lt;td&gt;Configure &lt;code&gt;upg1.target_cdb&lt;/code&gt; in AutoUpgrade config to convert non-CDB to PDB&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-02-18-oracle-19c-26ai-autoupgrade-factory-6-failures.svg&quot; alt=&quot;Four failure paths stall unattended AutoUpgrade waves&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Do not replace &lt;code&gt;autoupgrade.jar&lt;/code&gt;—orchestrate it:&lt;/strong&gt; AutoUpgrade handles dictionary upgrades, fixups, and GRPs reliably on one host; Temporal adds the global state machine, &lt;code&gt;status.json&lt;/code&gt; aggregation, and storage-pod throttling needed at 3,000-database scale.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Size the FRA for peak redo plus soak time:&lt;/strong&gt; A Guaranteed Restore Point works without &lt;code&gt;FLASHBACK ON&lt;/code&gt;, but requires &lt;code&gt;ARCHIVELOG&lt;/code&gt; mode and enough FRA headroom to hold 8+ hours of redo until the post-upgrade soak clears and the GRP is dropped.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Plan non-CDB-to-PDB conversion in the same workflow:&lt;/strong&gt; Non-CDB architecture and manual upgrade scripts are desupported in Oracle AI Database 26ai, making AutoUpgrade’s PDB conversion path mandatory for legacy 19c non-CDBs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Make host activities idempotent on &lt;code&gt;V$INSTANCE&lt;/code&gt;:&lt;/strong&gt; Temporal retries after transient network drops must inspect &lt;code&gt;V$INSTANCE.VERSION&lt;/code&gt; before invoking &lt;code&gt;autoupgrade.jar&lt;/code&gt; so an already-upgraded database is never re-submitted.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Include OEM, RMAN, and systemd in the definition of done:&lt;/strong&gt; An upgrade is incomplete until monitoring targets and backup catalogs reflect the new 26ai &lt;code&gt;$ORACLE_HOME&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Executing &lt;code&gt;autoupgrade.jar&lt;/code&gt; manually across thousands of Oracle 19c databases fails on coordination math, hidden systemic pre-check blockers, and slow manual flashback recovery.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Wrap host-local &lt;code&gt;autoupgrade.jar&lt;/code&gt; agents in a Temporal workflow control plane that aggregates &lt;code&gt;status.json&lt;/code&gt;, halts on &lt;code&gt;&gt;10%&lt;/code&gt; systemic errors, caps concurrency per storage pod, and automates GRP flashback.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Verify &lt;code&gt;autoupgrade.jar -mode analyze -noconsole&lt;/code&gt; produces parseable &lt;code&gt;status.json&lt;/code&gt; under &lt;code&gt;cfgtoollogs/upgrade/auto/status/&lt;/code&gt;, and test a simulated non-zero exit to confirm &lt;code&gt;FLASHBACK DATABASE TO RESTORE POINT&lt;/code&gt; restores 19c cleanly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run &lt;code&gt;java -jar autoupgrade.jar -create_sample_file config&lt;/code&gt; and &lt;code&gt;-mode analyze -noconsole&lt;/code&gt; on one staging 19c database this week, inspect &lt;code&gt;status.json&lt;/code&gt;, and validate your FRA sizing query against &lt;code&gt;V$RECOVERY_FILE_DEST&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Oracle Lifetime Support Policy — Oracle Database 19c Support Dates (MOS Doc ID 742060.1): &lt;a href=&quot;https://www.oracle.com/us/support/library/lifetime-support-technology-069183.pdf&quot;&gt;https://www.oracle.com/us/support/library/lifetime-support-technology-069183.pdf&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Oracle Database Upgrade Guide — AutoUpgrade Command-Line Parameters and Options: &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/21/upgrd/autoupgrade-command-line-parameters-and-options.html&quot;&gt;https://docs.oracle.com/en/database/oracle/oracle-database/21/upgrd/autoupgrade-command-line-parameters-and-options.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Oracle Database Backup and Recovery User’s Guide — Guaranteed Restore Points: &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/bradv/using-flasback-database-restore-points.html&quot;&gt;https://docs.oracle.com/en/database/oracle/oracle-database/19/bradv/using-flasback-database-restore-points.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Oracle Database Reference — &lt;code&gt;V$RECOVERY_FILE_DEST&lt;/code&gt;: &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/V-RECOVERY_FILE_DEST.html&quot;&gt;https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/V-RECOVERY_FILE_DEST.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Oracle Database 23ai/26ai Upgrade Release Notes — AutoUpgrade &amp;#x26; Non-CDB Desupport: &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/23/upgrd/oracle-database-release-notes.html&quot;&gt;https://docs.oracle.com/en/database/oracle/oracle-database/23/upgrd/oracle-database-release-notes.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Temporal Documentation — Durable Execution and Activities: &lt;a href=&quot;https://docs.temporal.io/concepts/what-is-an-activity&quot;&gt;https://docs.temporal.io/concepts/what-is-an-activity&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>checklist</category></item><item><title>Fleet Evacuation Engineering: Exiting Deprecated Managed Oracle Services at Scale</title><link>https://rajivonai.com/blog/2026-01-23-rds-custom-oracle-end-of-support-exit-strategy/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-01-23-rds-custom-oracle-end-of-support-exit-strategy/</guid><description>When AWS deprecates RDS Custom for Oracle, 500 databases cannot be migrated manually. This is how to build a Data Plane Evacuation Engine using Oracle Data Guard physical replication, Temporal workflows, and transparent proxy cutover.</description><pubDate>Thu, 30 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Exiting a deprecated managed database service cannot rely on detaching EBS volumes when the cloud provider owns the storage perimeter; fleet-scale evacuation requires streaming data out through Oracle’s own redo and RMAN active duplication channels while the source remains online.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; AWS has announced End of Support for Amazon RDS Custom for Oracle effective &lt;strong&gt;March 31, 2027&lt;/strong&gt;, leaving fleets locked inside an AWS-owned EBS perimeter where modifying &lt;code&gt;/rdsdbdata&lt;/code&gt; or managed files flips instances to &lt;code&gt;Unsupported configuration&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Run a Temporal-orchestrated &lt;strong&gt;Data Plane Evacuation Engine&lt;/strong&gt; that uses SSM Run Command to execute &lt;code&gt;RMAN DUPLICATE ... FOR STANDBY FROM ACTIVE DATABASE&lt;/code&gt; (reading from Oracle’s SGA, not EBS), syncs via Data Guard Broker, switches over through an Envoy proxy layer, and deletes the source RDS instance after a 7-day stability timer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Never write inside &lt;code&gt;/rdsdbdata&lt;/code&gt; or unapproved OS paths on RDS Custom; migrate TDE keystores before seeding; set &lt;code&gt;APPLIED ON ALL STANDBY&lt;/code&gt; archivelog deletion policy; and cap JDBC pool &lt;code&gt;maxLifetimeMs&lt;/code&gt; so stale connections do not hit the demoted standby (&lt;code&gt;ORA-01033&lt;/code&gt; / &lt;code&gt;ORA-16000&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2026-01-23-rds-custom-oracle-end-of-support-exit-strategy.svg&quot; alt=&quot;Four-stage RDS Custom evacuation pipeline using RMAN active duplication and Data Guard&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Amazon RDS Custom for Oracle sits between standard Amazon RDS for Oracle and self-managed Oracle on Amazon EC2. Unlike standard RDS, RDS Custom grants &lt;code&gt;SYSDBA&lt;/code&gt; privileges and OS access (via AWS Systems Manager) so teams can install custom backup/monitoring agents, Database Vault, or legacy packaged-application dependencies while AWS automates backups, Multi-AZ orchestration, and underlying EBS storage.&lt;/p&gt;
&lt;p&gt;AWS has announced the &lt;strong&gt;deprecation and End of Support (EOS) for Amazon RDS Custom for Oracle effective March 31, 2027&lt;/strong&gt;. After that date, access to the RDS Custom for Oracle console, database instances, snapshots, and Custom Engine Versions (CEVs) is discontinued.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Deployment Model&lt;/th&gt;&lt;th&gt;OS &amp;#x26; &lt;code&gt;SYSDBA&lt;/code&gt; Access&lt;/th&gt;&lt;th&gt;EBS / Storage Owner&lt;/th&gt;&lt;th&gt;Oracle Binary Owner&lt;/th&gt;&lt;th&gt;Primary Exit Mechanism&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;RDS Standard for Oracle&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;None&lt;/td&gt;&lt;td&gt;AWS&lt;/td&gt;&lt;td&gt;AWS-managed&lt;/td&gt;&lt;td&gt;Data Pump / AWS DMS&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;RDS Custom for Oracle&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;SSM + &lt;code&gt;SYSDBA&lt;/code&gt;&lt;/td&gt;&lt;td&gt;AWS (RDS-managed)&lt;/td&gt;&lt;td&gt;Customer CEV&lt;/td&gt;&lt;td&gt;RMAN Active Dup / Data Guard&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Self-Managed EC2&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Full root + &lt;code&gt;SYSDBA&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Customer&lt;/td&gt;&lt;td&gt;Customer&lt;/td&gt;&lt;td&gt;Storage / RMAN / Data Guard&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Oracle Database@AWS&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Exadata VM + &lt;code&gt;SYSDBA&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Oracle (ODB Network)&lt;/td&gt;&lt;td&gt;Oracle-managed&lt;/td&gt;&lt;td&gt;Target platform (RAC + DG)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The architectural catch is storage ownership. Even though RDS Custom exposes OS access, AWS owns the EBS volume lifecycle and snapshot metadata. You cannot detach an RDS Custom EBS volume and mount it onto a standard EC2 instance or an Exadata VM Cluster. Any fleet exit must move data over an Oracle-native replication channel while staying inside the RDS Custom &lt;strong&gt;support perimeter&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Evacuating hundreds of multi-terabyte Oracle databases ahead of the March 31, 2027 deadline hits four concrete constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;EBS storage entanglement:&lt;/strong&gt; RDS Custom EBS volumes and snapshots carry RDS-internal KMS and storage orchestration bindings tied to the RDS instance identifier. Standard EC2 instances cannot mount cloned RDS snapshots directly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Support perimeter lockout (&lt;code&gt;Unsupported configuration&lt;/code&gt;):&lt;/strong&gt; RDS Custom continuously monitors &lt;code&gt;/rdsdbdata&lt;/code&gt;, managed &lt;code&gt;listener.ora&lt;/code&gt; paths, and OS packages. If an ad-hoc migration script writes temp files inside &lt;code&gt;/rdsdbdata&lt;/code&gt; or alters managed components without pausing automation (&lt;code&gt;aws rds modify-db-instance --automation-mode all-paused&lt;/code&gt;), AWS shifts the instance to &lt;code&gt;Unsupported configuration&lt;/code&gt; and suspends automated backups and monitoring.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fleet DBA concurrency math:&lt;/strong&gt; Migrating 500 databases manually at 2.5 DBA-hours per instance requires &lt;code&gt;500 * 2.5 = 1,250&lt;/code&gt; engineering hours of bespoke execution—excluding incident recovery and post-cutover cleanup. Without durable workflow orchestration, a small DBA team cannot clear the backlog before EOS while running daily production operations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hard March 31, 2027 cutoff and double-billing risk:&lt;/strong&gt; Leaving migrated RDS Custom instances running “for safety” doubles monthly compute and licensing spend and leaves unretired infrastructure approaching the EOS cliff.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2026-01-23-rds-custom-oracle-end-of-support-exit-strategy-2-constraints.svg&quot; alt=&quot;Four constraints blocking naive EBS lift-and-shift from RDS Custom&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do you evacuate hundreds of RDS Custom for Oracle databases before March 31, 2027 without violating the AWS support perimeter, taking multi-hour logical export outages, or scaling DBA headcount linearly?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Exit Strategy&lt;/th&gt;&lt;th&gt;Downtime Profile&lt;/th&gt;&lt;th&gt;Physical Block Fidelity&lt;/th&gt;&lt;th&gt;Perimeter Safe?&lt;/th&gt;&lt;th&gt;Why Chosen / Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Data Pump (&lt;code&gt;expdp&lt;/code&gt; / &lt;code&gt;impdp&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Multi-hour write freeze&lt;/td&gt;&lt;td&gt;Logical (rebuilds indexes/stats)&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Unacceptable outage windows on multi-terabyte OLTP databases and heavy post-import validation.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;AWS DMS (Logical CDC)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Low cutover&lt;/td&gt;&lt;td&gt;Logical (requires supplemental logging)&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Struggles with complex Oracle UDTs, &lt;code&gt;LOB&lt;/code&gt; edge cases, and sequence/trigger parity across legacy schemas.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Self-Managed EC2 Target&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Seconds (with Data Guard)&lt;/td&gt;&lt;td&gt;Physical&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Fallback Target:&lt;/strong&gt; Supports RMAN active duplication, but shifts OS patching, kernel tuning, and HA fencing back onto DBAs.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;RMAN Active Dup + Data Guard to Oracle Database@AWS&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Seconds (&lt;code&gt;dgmgrl SWITCHOVER&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Physical (exact block copy)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Yes (via SSM + SGA)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Chosen:&lt;/strong&gt; Streams directly from Oracle SGA over port 1521, preserves physical fidelity, and lands on managed Exadata HA.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-01-23-rds-custom-oracle-end-of-support-exit-strategy-3-options.svg&quot; alt=&quot;Comparison of four RDS Custom exit strategies&quot;&gt;&lt;/p&gt;
&lt;p&gt;Using &lt;strong&gt;Self-Managed EC2&lt;/strong&gt; as the destination works with the exact same RMAN active duplication engine if a workload requires custom kernel modules or unsupported legacy Oracle versions that do not fit Oracle Database@AWS or standard RDS for Oracle. For enterprise OLTP workloads that need RAC, Data Guard automation, and Exadata I/O without inheriting Linux/hypervisor patching toil, &lt;strong&gt;Oracle Database@AWS&lt;/strong&gt; (or standard Amazon RDS for Oracle when &lt;code&gt;SYSDBA&lt;/code&gt;/OS access is no longer needed) is the cleaner landing zone.&lt;/p&gt;
&lt;h2 id=&quot;the-data-plane-evacuation-engine&quot;&gt;The Data Plane Evacuation Engine&lt;/h2&gt;
&lt;p&gt;The Evacuation Engine treats every migration as a &lt;strong&gt;temporary physical Data Guard standby&lt;/strong&gt; orchestrated by a durable Temporal workflow keyed by the source RDS Custom ARN. Because &lt;code&gt;RMAN DUPLICATE ... FOR STANDBY FROM ACTIVE DATABASE&lt;/code&gt; reads blocks from Oracle’s SGA buffer cache via &lt;code&gt;DBMS_BACKUP_RESTORE&lt;/code&gt; and streams them over SQL*Net (TCP 1521/2484), it never touches EBS volumes or &lt;code&gt;/rdsdbdata&lt;/code&gt; layout directly.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2026-01-23-rds-custom-oracle-end-of-support-exit-strategy-4-architecture.svg&quot; alt=&quot;Temporal orchestrating SSM-triggered RMAN duplication and Envoy proxy cutover&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;phase-1-target-provisioning-and-tde-keystore-migration&quot;&gt;Phase 1: Target Provisioning and TDE Keystore Migration&lt;/h3&gt;
&lt;p&gt;The workflow provisions the target Exadata VM Cluster (or CDB) idempotently, validates VPC/ODB peering connectivity on port 1521/2484, and checks that target IAM/KMS permissions are healthy. Before seeding any standby, the pre-flight activity verifies two Oracle Data Guard prerequisites on the RDS Custom source: &lt;strong&gt;Force Logging&lt;/strong&gt; (&lt;code&gt;SELECT force_logging FROM v$database&lt;/code&gt; must return &lt;code&gt;YES&lt;/code&gt; so &lt;code&gt;NOLOGGING&lt;/code&gt; direct-path loads do not corrupt standby blocks) and &lt;strong&gt;Standby Redo Logs (SRLs)&lt;/strong&gt; sized identically to the online redo logs on the target (&lt;code&gt;&gt;= (online_groups_per_thread + 1)&lt;/code&gt; groups) so Data Guard Broker can receive redo immediately upon role reversal.&lt;/p&gt;
&lt;p&gt;If a source database requires a parameter or &lt;code&gt;tnsnames.ora&lt;/code&gt; adjustment that touches managed directories before duplication begins, operators must pause RDS Custom automation explicitly (&lt;code&gt;aws rds modify-db-instance --automation-mode all-paused&lt;/code&gt;), apply the change, and resume &lt;code&gt;--automation-mode full&lt;/code&gt; afterward rather than editing &lt;code&gt;/rdsdbdata&lt;/code&gt; while the perimeter monitor is active.&lt;/p&gt;
&lt;p&gt;For TDE-encrypted databases, the workflow exports the source TDE master encryption keys before seeding the standby—because RDS Custom’s EBS KMS key is independent of Oracle’s TDE wallet:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ADMINISTER &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MANAGEMENT EXPORT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ENCRYPTION&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; KEYS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  WITH&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SECRET&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;migration_passphrase&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  TO&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;/tmp/wallet_export.p12&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  FORCE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; KEYSTORE IDENTIFIED &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BY&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;&amp;#x3C;keystore_password&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The exported &lt;code&gt;.p12&lt;/code&gt; bundle is transferred securely to the target and imported into the target keystore prior to &lt;code&gt;RMAN DUPLICATE&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;phase-2-rman-active-duplication-via-ssm-run-command&quot;&gt;Phase 2: RMAN Active Duplication via SSM Run Command&lt;/h3&gt;
&lt;p&gt;The workflow invokes the extraction script on the RDS Custom host via &lt;strong&gt;AWS Systems Manager (SSM) Run Command&lt;/strong&gt; (running as the &lt;code&gt;oracle&lt;/code&gt; OS user, which is an explicitly supported management channel inside the RDS Custom perimeter):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RMAN&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; DUPLICATE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TARGET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DATABASE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  FOR&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; STANDBY&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ACTIVE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DATABASE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  DORECOVER&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  SPFILE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    SET&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;db_unique_name&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;     =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;TARGET_DB_UNIQUE&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    SET&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;log_archive_dest_2&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;SERVICE=target_tns ASYNC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                                VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                                DB_UNIQUE_NAME=TARGET_DB_UNIQUE&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  SECTION &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; 4G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  USING&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; COMPRESSED BACKUPSET;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Parallelism comes from the channels allocated before DUPLICATE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- (ALLOCATE CHANNEL / ALLOCATE AUXILIARY CHANNEL), not from a DUPLICATE clause.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using &lt;code&gt;SECTION SIZE 4G&lt;/code&gt; splits large datafiles across all 8 parallel RMAN channels rather than serializing one channel per datafile. At a capped &lt;code&gt;RATE 100M&lt;/code&gt; per channel (&lt;code&gt;8 * 100 MB/s = 800 MB/s&lt;/code&gt; raw, or roughly &lt;code&gt;2.88 TB/hour&lt;/code&gt; before compression), an 8 TB database seeds in roughly &lt;code&gt;8 TB / 2.88 TB/hr ≈ 2.8 hours&lt;/code&gt; without saturating a 10 Gbps VPC link when paced across concurrent workflows.&lt;/p&gt;
&lt;h3 id=&quot;phase-3--phase-4-data-guard-sync-proxy-cutover-and-cleanup&quot;&gt;Phase 3 &amp;#x26; Phase 4: Data Guard Sync, Proxy Cutover, and Cleanup&lt;/h3&gt;
&lt;p&gt;Once duplication finishes, the workflow registers both databases in Data Guard Broker (&lt;code&gt;CREATE CONFIGURATION&lt;/code&gt; / &lt;code&gt;ADD DATABASE target_db ... MAINTAINED AS PHYSICAL&lt;/code&gt;), sets &lt;code&gt;LogXptMode = &apos;ASYNC&apos;&lt;/code&gt; on &lt;code&gt;target_db&lt;/code&gt;, and configures RMAN on the source so archived logs are not purged before the standby applies them:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;dgmgrl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; sys/&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;passwor&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;d&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;@source_tns&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;CREATE CONFIGURATION evac_dg AS PRIMARY DATABASE IS source_db&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  CONNECT IDENTIFIER IS source_tns;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;ADD DATABASE target_db AS CONNECT IDENTIFIER IS target_tns&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  MAINTAINED AS PHYSICAL;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;ENABLE CONFIGURATION;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EDIT DATABASE target_db SET PROPERTY LogXptMode=&apos;ASYNC&apos;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In Temporal, a leaky bucket caps concurrent Phase 2 duplications (for example, 8 concurrent streams per 10 Gbps subnet) and concurrent Phase 4 cutovers (for example, 10 per wave). After a human approval signal in Temporal, Phase 4 executes &lt;code&gt;DGMGRL&gt; SWITCHOVER TO target_db;&lt;/code&gt; and updates the Envoy proxy upstream route.&lt;/p&gt;
&lt;p&gt;To keep the cutover window bounded to seconds rather than hanging on stale TCP sockets, two client-side controls are mandatory alongside Envoy’s routing update:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Bounded JDBC pool lifetimes:&lt;/strong&gt; Every application connection pool must enforce &lt;code&gt;maxLifetimeMs &amp;#x3C;= 1800000&lt;/code&gt; (30 minutes) and fast connection validation (&lt;code&gt;SELECT 1 FROM DUAL&lt;/code&gt;), preventing long-lived connections from staying pinned to the demoted RDS Custom instance after Envoy shifts new connections to Exadata.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Idempotent retry on standby errors:&lt;/strong&gt; During the 5–15 second Envoy xDS propagation window across Availability Zones, in-flight writes that still land on the demoted RDS Custom instance receive &lt;code&gt;ORA-01033&lt;/code&gt; (during &lt;code&gt;MOUNT&lt;/code&gt; transition) or &lt;code&gt;ORA-16000&lt;/code&gt; (if opened read-only). Configuring Envoy upstream timeouts and application-tier retries on transient connection/read-only errors absorbs the brief propagation window without user-visible failures.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Post-cutover, a mandatory &lt;strong&gt;7-day Temporal timer&lt;/strong&gt; monitors &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; and application error rates. Only after 7 clean days does the workflow delete the source RDS Custom instance to prevent double-billing:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; rds&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; delete-db-instance&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --db-instance-identifier&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SOURCE_DB_ID&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --skip-final-snapshot&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --delete-automated-backups&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2026-01-23-rds-custom-oracle-end-of-support-exit-strategy-5-flow.svg&quot; alt=&quot;Four-phase evacuation keeping RDS Custom live until 7-day verification&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Pilot Inventory and Perimeter Audit.&lt;/strong&gt; Classify all RDS Custom instances by Oracle version, TDE status, size, and target landing zone (Oracle Database@AWS, standard RDS, or EC2). Run a non-production dry run to confirm SSM Run Command scripts never touch &lt;code&gt;/rdsdbdata&lt;/code&gt;. &lt;em&gt;Validation gate:&lt;/em&gt; Zero &lt;code&gt;Unsupported configuration&lt;/code&gt; events in AWS RDS event logs. &lt;em&gt;Rollback:&lt;/em&gt; Abort workflow before RMAN starts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Non-Production Standby Seeding.&lt;/strong&gt; Execute &lt;code&gt;RMAN DUPLICATE ... FROM ACTIVE DATABASE&lt;/code&gt; with &lt;code&gt;RATE&lt;/code&gt; throttling and &lt;code&gt;APPLIED ON ALL STANDBY&lt;/code&gt; archivelog deletion policy. &lt;em&gt;Validation gate:&lt;/em&gt; Target standby opens in &lt;code&gt;MOUNT&lt;/code&gt; / &lt;code&gt;READ ONLY&lt;/code&gt; with &lt;code&gt;0&lt;/code&gt; apply lag in &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt;. &lt;em&gt;Rollback:&lt;/em&gt; Drop the target standby and remove &lt;code&gt;evac_dg&lt;/code&gt; broker config; source RDS Custom is untouched.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Production Cutover Waves.&lt;/strong&gt; Enforce JDBC connection pool &lt;code&gt;maxLifetimeMs &amp;#x3C;= 1800000&lt;/code&gt; (30 minutes) and Envoy upstream retries before triggering &lt;code&gt;DGMGRL&gt; SWITCHOVER TO target_db;&lt;/code&gt;. &lt;em&gt;Validation gate:&lt;/em&gt; Zero elevated HTTP 5xx rates and clean write commits on Exadata. &lt;em&gt;Rollback:&lt;/em&gt; Execute &lt;code&gt;DGMGRL&gt; SWITCHOVER TO source_db;&lt;/code&gt; and revert Envoy upstream routing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — 7-Day Stability Timer and Decommission.&lt;/strong&gt; Hold the demoted RDS Custom instance for 7 days as an emergency failback target, then run &lt;code&gt;aws rds delete-db-instance&lt;/code&gt; automatically and alert on any source instance still running &gt; 30 days post-cutover.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;RDS Custom support perimeter behavior (AWS RDS Custom User Guide):&lt;/strong&gt; AWS monitors &lt;code&gt;/rdsdbdata&lt;/code&gt; and core OS/listener configurations continuously; unauthorized edits flip the instance to &lt;code&gt;Unsupported configuration&lt;/code&gt; and halt automated backups. Running RMAN active duplication via SSM Run Command as the &lt;code&gt;oracle&lt;/code&gt; user reads from SGA memory and avoids modifying &lt;code&gt;/rdsdbdata&lt;/code&gt; files directly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RMAN active duplication and TDE independence (Oracle Backup &amp;#x26; Recovery Reference):&lt;/strong&gt; Because &lt;code&gt;DUPLICATE ... FOR STANDBY FROM ACTIVE DATABASE&lt;/code&gt; streams blocks through &lt;code&gt;DBMS_BACKUP_RESTORE&lt;/code&gt; over Oracle Net, RDS Custom’s EBS KMS key never needs to be shared with the target environment; only Oracle’s TDE software keystore (&lt;code&gt;ADMINISTER KEY MANAGEMENT EXPORT ENCRYPTION KEYS&lt;/code&gt;; see Oracle’s Advanced Security TDE documentation for the exact export and import procedure) must be migrated.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle Database@AWS as a landing zone (AWS &amp;#x26; Oracle Documentation):&lt;/strong&gt; Oracle Database@AWS gives DBAs &lt;code&gt;SYSDBA&lt;/code&gt; and Exadata VM cluster control while Oracle manages the underlying Exadata storage cells and hypervisors—preserving the operational control that originally drove teams to RDS Custom without inheriting self-managed EC2 hardware/firmware toil. Exadata VM Clusters also support online OCPU and memory scaling, allowing teams to land initial migrations on right-sized compute shapes and scale dynamically after cutover based on observed AWR telemetry rather than over-provisioning up front.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Temporal leaky-bucket pacing and &lt;code&gt;V$ARCHIVE_GAP&lt;/code&gt; monitoring:&lt;/strong&gt; Capping concurrent Phase 2 duplications prevents 20 parallel RMAN streams from saturating a 10 Gbps subnet, while polling &lt;code&gt;V$ARCHIVE_GAP&lt;/code&gt; every 15 minutes during Phase 3 catches missing archive sequences early—before &lt;code&gt;APPLIED ON ALL STANDBY&lt;/code&gt; retention pressure forces a full RMAN re-seed. Storing workflow state in Temporal by source ARN also ensures an orchestrator restart resumes mid-migration cleanly.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Condition&lt;/th&gt;&lt;th&gt;Breaks How&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;RMAN channels saturate VPC subnet&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Concurrent duplications exhaust 10 Gbps link and drop RMAN channels&lt;/td&gt;&lt;td&gt;RMAN &lt;code&gt;ORA-19502&lt;/code&gt; / network retransmit spikes&lt;/td&gt;&lt;td&gt;Cap concurrent Phase 2 runs via Temporal leaky bucket and set RMAN &lt;code&gt;RATE 100M&lt;/code&gt; per channel&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;SSM script writes inside &lt;code&gt;/rdsdbdata&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;RDS Custom perimeter triggers &lt;code&gt;Unsupported configuration&lt;/code&gt; and suspends backups&lt;/td&gt;&lt;td&gt;RDS event &lt;code&gt;RDS-EVENT-0200&lt;/code&gt; / status &lt;code&gt;Unsupported configuration&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Execute as &lt;code&gt;oracle&lt;/code&gt; user; ban writes to &lt;code&gt;/rdsdbdata&lt;/code&gt; (or pause automation explicitly if unavoidable)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Missing TDE master key on target&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Target Exadata cannot decrypt TDE tablespaces (&lt;code&gt;ORA-28374&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;&lt;code&gt;ORA-28374: typed master key not found in wallet&lt;/code&gt; on standby open&lt;/td&gt;&lt;td&gt;Export/import &lt;code&gt;.p12&lt;/code&gt; TDE keystore in Phase 1 and verify wallet open before Phase 2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Envoy propagation or unbounded JDBC pool&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Stale writes hit demoted standby in &lt;code&gt;MOUNT&lt;/code&gt; / read-only state&lt;/td&gt;&lt;td&gt;&lt;code&gt;ORA-01033&lt;/code&gt; / &lt;code&gt;ORA-16000&lt;/code&gt; (not &lt;code&gt;ORA-16014&lt;/code&gt;, which is an archiver error)&lt;/td&gt;&lt;td&gt;Set JDBC &lt;code&gt;maxLifetimeMs &amp;#x3C;= 1800000&lt;/code&gt;, configure Envoy retries, and hold source briefly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Source purges archivelogs before apply&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Extended Phase 3 sync hits missing archive sequence; standby stalls&lt;/td&gt;&lt;td&gt;&lt;code&gt;V$ARCHIVE_GAP&lt;/code&gt; non-empty or MRP stopped&lt;/td&gt;&lt;td&gt;Set RMAN &lt;code&gt;CONFIGURE ARCHIVELOG DELETION POLICY TO APPLIED ON ALL STANDBY&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Zombie RDS Custom instances post-cutover&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Old instances left running past cutover double cloud spend before EOS&lt;/td&gt;&lt;td&gt;Cost anomaly or RDS inventory &gt; 30 days post-cutover&lt;/td&gt;&lt;td&gt;Enforce Temporal’s 7-day stability timer + automated &lt;code&gt;aws rds delete-db-instance&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2026-01-23-rds-custom-oracle-end-of-support-exit-strategy-6-failures.svg&quot; alt=&quot;Four failure modes in RDS Custom exits: perimeter lockouts, TDE ORA-28374, proxy lag, and archive gaps&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;March 31, 2027 is a hard operational cutoff:&lt;/strong&gt; AWS discontinues the RDS Custom for Oracle console, instances, snapshots, and CEVs after EOS, so manual per-database migrations will not clear a multi-hundred-database fleet in time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stream out of the SGA, not the EBS layer:&lt;/strong&gt; &lt;code&gt;RMAN DUPLICATE ... FOR STANDBY FROM ACTIVE DATABASE&lt;/code&gt; bypasses RDS Custom’s EBS ownership lock and leaves &lt;code&gt;/rdsdbdata&lt;/code&gt; untouched inside the support perimeter.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TDE wallets are independent of EBS KMS keys:&lt;/strong&gt; Export and import Oracle’s TDE encryption keys explicitly (&lt;code&gt;ADMINISTER KEY MANAGEMENT EXPORT ENCRYPTION KEYS&lt;/code&gt;) before seeding the standby to avoid &lt;code&gt;ORA-28374&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Know the right standby error codes during cutover:&lt;/strong&gt; Stale connections hitting a demoted standby throw &lt;code&gt;ORA-01033&lt;/code&gt; or &lt;code&gt;ORA-16000&lt;/code&gt; (whereas &lt;code&gt;ORA-16014&lt;/code&gt; indicates an archiver destination failure); cap JDBC &lt;code&gt;maxLifetimeMs&lt;/code&gt; and configure Envoy retries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Automate decommission on a 7-day timer:&lt;/strong&gt; A Temporal stability timer that deletes the source RDS Custom instance after 7 clean days prevents double-billing and zombie instances.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; AWS RDS Custom for Oracle reaches End of Support on March 31, 2027, and AWS-owned EBS volume lifecycles block storage-level lift-and-shift.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build a Temporal-orchestrated evacuation pipeline using SSM Run Command, &lt;code&gt;RMAN DUPLICATE ... FOR STANDBY FROM ACTIVE DATABASE&lt;/code&gt;, Data Guard Broker, and Envoy proxy cutover to Oracle Database@AWS (or EC2/standard RDS where appropriate).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run a single non-production RDS Custom pilot confirming zero &lt;code&gt;Unsupported configuration&lt;/code&gt; perimeter events during RMAN duplication and sub-minute &lt;code&gt;dgmgrl SWITCHOVER&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Inventory your RDS Custom for Oracle instances this week by size, TDE status, and CEV version, and verify SSM Run Command + port 1521/2484 connectivity to your target VPC or ODB network.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>failures</category></item><item><title>Immutable Infrastructure Patching for Oracle Fleets: Beyond FPP at Hyper-Scale</title><link>https://rajivonai.com/blog/2025-11-10-zero-touch-oracle-fleet-patching-fpp/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-11-10-zero-touch-oracle-fleet-patching-fpp/</guid><description>Why Oracle Fleet Patching and Provisioning chokes at hyper-scale and how to replace it with an immutable VM image pipeline that separates the compute lifecycle from the Oracle data plane, enabling parallel, rollback-safe patching across thousands of nodes.</description><pubDate>Tue, 28 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Patching a cloud database by mutating binaries on a long-lived VM treats disposable compute as permanent hardware; separating ASM block storage from compute lets you patch by swapping golden images instead of pushing gigabytes of Oracle Homes during the maintenance window.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Hub-and-spoke patching with Oracle FPP (&lt;code&gt;rhpserver&lt;/code&gt;) saturates central network uplinks when provisioning working copies at scale and leaves multi-year Linux OS and RPM drift untouched on long-lived VMs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Bake Oracle Grid Infrastructure, Release Updates, and CIS Level 1 OS hardening offline via Packer into regional VM images, then pivot databases during maintenance windows by cleanly stopping ASM, verifying a signed shutdown receipt, reattaching ASM block volumes to a fresh VM, and running &lt;code&gt;datapatch&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Never detach block volumes without confirming zero mounted ASM disk groups; poll &lt;code&gt;udev&lt;/code&gt; / &lt;code&gt;asmcmd lsdsk&lt;/code&gt; before mounting on the new VM; and if you roll back after &lt;code&gt;datapatch&lt;/code&gt; has updated &lt;code&gt;DBA_REGISTRY_SQLPATCH&lt;/code&gt;, you must run &lt;code&gt;datapatch&lt;/code&gt; from the old Oracle Home to revert the data dictionary.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2025-11-10-zero-touch-oracle-fleet-patching-fpp.svg&quot; alt=&quot;Immutable Oracle patching pipeline separating offline image bake from block-volume pivot&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Oracle Fleet Patching and Provisioning (FPP) is Oracle’s native tool for centralized software lifecycle management. Using a central Grid Infrastructure cluster running &lt;code&gt;rhpserver&lt;/code&gt; and the &lt;code&gt;rhpctl&lt;/code&gt; CLI, FPP maintains a repository of gold images and performs out-of-place patching by provisioning a new &lt;strong&gt;working copy&lt;/strong&gt; (&lt;code&gt;rhpctl add workingcopy&lt;/code&gt;), validating readiness (&lt;code&gt;rhpctl move database -eval&lt;/code&gt;), switching the database over (&lt;code&gt;rhpctl move database&lt;/code&gt;), and running &lt;code&gt;datapatch&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For fleets of dozens of databases in a single region, FPP’s out-of-place model is a major improvement over in-place &lt;code&gt;opatch apply&lt;/code&gt;. At thousands of instances spread across multiple cloud regions, however, even out-of-place binary provisioning runs into the limits of mutating long-lived virtual machines from a central hub: &lt;code&gt;rhpserver&lt;/code&gt; must serialize &lt;code&gt;oraInventory&lt;/code&gt; locks on multi-tenant hosts, push multi-gigabyte working copies across WAN/VPC boundaries, and tolerate OS-level package drift that accumulated since the VM was first provisioned.&lt;/p&gt;
&lt;p&gt;Cloud-native database platforms decouple two things that on-premises servers fused together: the &lt;strong&gt;compute layer&lt;/strong&gt; (Linux kernel, &lt;code&gt;glibc&lt;/code&gt;, Grid Infrastructure, and RDBMS binaries) and the &lt;strong&gt;data layer&lt;/strong&gt; (persistent block volumes holding Oracle ASM disk groups for &lt;code&gt;DATA&lt;/code&gt;, &lt;code&gt;FRA&lt;/code&gt;, and OCR/SPFILEs). When storage outlives the VM, patching no longer requires copying binaries onto old hosts.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Centralized hub-and-spoke patching hits four concrete constraints at fleet scale:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Hub uplink saturation during parallel working-copy fanout (derived math):&lt;/strong&gt; An Oracle Grid Infrastructure plus RDBMS home is roughly 10 GB of binaries. Staging working copies concurrently to 500 target VMs from a central &lt;code&gt;rhpserver&lt;/code&gt; transfers &lt;code&gt;500 * 10 GB = 5,000 GB&lt;/code&gt; (&lt;code&gt;40,000 Gb&lt;/code&gt;). Even if a central hub sustains a full 10 Gbps (&lt;code&gt;1.25 GB/s&lt;/code&gt;) uplink with zero packet loss, pushing 5 TB takes &lt;code&gt;40,000 Gb / 10 Gbps = 4,000 seconds&lt;/code&gt; (66.7 minutes) of continuous saturation—long enough to trigger corporate DLP/IDS anomaly alerts or starve co-located management traffic.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multi-year OS configuration drift:&lt;/strong&gt; Out-of-place DB home patching replaces &lt;code&gt;$ORACLE_HOME&lt;/code&gt;, not the underlying Oracle Linux OS. A VM provisioned three years ago accumulates hundreds of &lt;code&gt;yum update&lt;/code&gt; runs, kernel updates, &lt;code&gt;sysctl&lt;/code&gt; edits, and agent upgrades. A Release Update that passes &lt;code&gt;opatch&lt;/code&gt; prerequisites on Node A fails on Node B because of a divergent &lt;code&gt;glibc&lt;/code&gt; or &lt;code&gt;libaio&lt;/code&gt; package introduced months earlier.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cloud block-volume API rate limits:&lt;/strong&gt; Moving to VM replacement trades network bandwidth limits for cloud control-plane API quotas. Cloud providers throttle &lt;code&gt;AttachVolume&lt;/code&gt; and &lt;code&gt;DetachVolume&lt;/code&gt; calls per tenancy (limits differ by provider, region and account, so read the current quota for your own tenancy before sizing the pivot batch). Unthrottled parallel pivots trigger HTTP 429 responses mid-cutover.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Local filesystem state coupling:&lt;/strong&gt; Any database storing auto-login wallets, &lt;code&gt;tnsnames.ora&lt;/code&gt;, &lt;code&gt;sqlnet.ora&lt;/code&gt;, or custom monitoring configs on the VM’s root disk cannot survive a compute swap cleanly unless those artifacts live on ASM/ACFS, object storage, or an external configuration service.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2025-11-10-zero-touch-oracle-fleet-patching-fpp-2-constraints.svg&quot; alt=&quot;Four constraints limiting hub-and-spoke binary patching at fleet scale&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do you roll out quarterly Oracle Release Updates across thousands of cloud nodes in parallel—with zero binary push during the maintenance window, zero accumulated OS drift, and deterministic rollback?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Patching Model&lt;/th&gt;&lt;th&gt;Binary Delivery&lt;/th&gt;&lt;th&gt;OS Drift Reset?&lt;/th&gt;&lt;th&gt;Rollback Mechanism&lt;/th&gt;&lt;th&gt;Why Chosen / Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;In-Place &lt;code&gt;opatch apply&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Local copy / NFS&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;&lt;code&gt;opatch rollback&lt;/code&gt; while DB is down&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Long binary-mutation downtime and brittle rollback if &lt;code&gt;opatch&lt;/code&gt; fails mid-apply.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Oracle FPP (&lt;code&gt;rhpctl&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;rhpserver&lt;/code&gt; working copies&lt;/td&gt;&lt;td&gt;No (mutates existing VM)&lt;/td&gt;&lt;td&gt;&lt;code&gt;rhpctl move database&lt;/code&gt; back to old working copy&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Partial Fit:&lt;/strong&gt; Clean out-of-place DB home switch, but leaves Linux OS drift and central hub fanout intact.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Ansible + Artifactory RPMs&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Regional repo pull&lt;/td&gt;&lt;td&gt;Partial (RPMs only)&lt;/td&gt;&lt;td&gt;Downgrade RPMs&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Reduces hub bottleneck via regional repos, but still mutates stateful hosts into snowflakes.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Immutable Packer Image + Volume Pivot&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Pre-baked regional VM image&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Yes (fresh OL8 + RU every cycle)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Boot old image + reattach + &lt;code&gt;datapatch&lt;/code&gt; rollback if needed&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Chosen:&lt;/strong&gt; Eliminates maintenance-window binary transfers and resets OS and DB binaries together.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2025-11-10-zero-touch-oracle-fleet-patching-fpp-3-options.svg&quot; alt=&quot;Comparison of four Oracle fleet patching models&quot;&gt;&lt;/p&gt;
&lt;p&gt;Oracle FPP (&lt;code&gt;rhpctl&lt;/code&gt;) remains the right choice on bare-metal Exadata or on-premises clusters where compute nodes cannot be replaced via cloud APIs. On cloud VMs backed by detachable block storage (OCI Block Volumes or AWS EBS), &lt;strong&gt;immutable image pivoting&lt;/strong&gt; goes one step further: it treats the entire Linux OS + Grid Infrastructure + RDBMS stack as a single versioned, read-only artifact.&lt;/p&gt;
&lt;h2 id=&quot;the-immutable-oracle-compute-image-pipeline&quot;&gt;The Immutable Oracle Compute Image Pipeline&lt;/h2&gt;
&lt;p&gt;The architecture splits patching into two decoupled planes separated in time: an &lt;strong&gt;Offline Bake Plane&lt;/strong&gt; triggered whenever Oracle publishes a quarterly Release Update, and a &lt;strong&gt;Production Pivot Plane&lt;/strong&gt; that swaps compute around persistent ASM storage during maintenance windows.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2025-11-10-zero-touch-oracle-fleet-patching-fpp-4-architecture.svg&quot; alt=&quot;Offline bake plane and production block-volume pivot architecture&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;bake-phase-golden-image-construction&quot;&gt;Bake Phase: Golden Image Construction&lt;/h3&gt;
&lt;p&gt;HashiCorp Packer builds the patched image offline from a pinned Oracle Linux 8 base OCID/AMI (never floating &lt;code&gt;latest&lt;/code&gt; tags, so base kernel parameters like &lt;code&gt;vm.hugetlbpages&lt;/code&gt; and &lt;code&gt;kernel.shmmax&lt;/code&gt; never change unexpectedly). The pipeline installs Grid Infrastructure and the target Oracle 19c/23ai Release Update, applies CIS Level 1 OS hardening, validates &lt;code&gt;datapatch -prereq&lt;/code&gt; against a representative test database, and seals the image before replicating it to regional registries.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# oracle-db-19c-ru.pkr.hcl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;build&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  source&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;oci-instance&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;oracle-linux-8&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    base_image_ocid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;pinned_ol8_ocid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    shape&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;VM.Standard.E5.Flex&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ocpus&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    memory_in_gbs&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 32&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    compartment_ocid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;build_compartment_ocid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  provisioner&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;shell&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    scripts&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      &quot;scripts/01-install-grid-infrastructure.sh&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      &quot;scripts/02-apply-oracle-release-update.sh&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      &quot;scripts/03-harden-cis-level1.sh&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      &quot;scripts/04-install-monitoring-agents.sh&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      &quot;scripts/05-seal-image.sh&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    environment_vars&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      &quot;ORACLE_VERSION=19.25.0.0&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      &quot;RU_PATCH_ID=37082825&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;pivot-phase-cryptographic-shutdown-gate-and-asm-dismount&quot;&gt;Pivot Phase: Cryptographic Shutdown Gate and ASM Dismount&lt;/h3&gt;
&lt;p&gt;Before detaching any block volume from the old VM, the local agent must cleanly shut down the database and Oracle ASM, verify zero mounted disk groups, and return a &lt;strong&gt;SHA-256 HMAC shutdown receipt&lt;/strong&gt;. Detaching a volume while ASM still holds disk groups mounted risks corrupting ASM disk headers and leaving the new VM unable to mount storage without &lt;code&gt;kfed&lt;/code&gt;/&lt;code&gt;kfod&lt;/code&gt; intervention.&lt;/p&gt;
&lt;p&gt;Equally important is &lt;strong&gt;Oracle Restart (&lt;code&gt;HAS&lt;/code&gt;) and &lt;code&gt;/etc/oratab&lt;/code&gt; re-registration&lt;/strong&gt; on the new VM. Because the golden image is built offline without knowing any specific database’s &lt;code&gt;DB_UNIQUE_NAME&lt;/code&gt; or &lt;code&gt;SPFILE&lt;/code&gt; path, the new VM boots with a clean Grid Infrastructure / Oracle Restart home and an empty &lt;code&gt;/etc/oratab&lt;/code&gt;. Once the block volumes attach and &lt;code&gt;udev&lt;/code&gt; rules bind the ASM disk devices under &lt;code&gt;/dev/oracleasm/disks/*&lt;/code&gt; (or AFD labels), the local agent starts &lt;code&gt;ohasd&lt;/code&gt;, discovers the ASM disk groups via &lt;code&gt;asmcmd&lt;/code&gt;, and registers the database resource in Oracle Restart (&lt;code&gt;srvctl add database&lt;/code&gt; pointing to the &lt;code&gt;SPFILE&lt;/code&gt; stored on &lt;code&gt;+DATA&lt;/code&gt;) before calling &lt;code&gt;srvctl start database&lt;/code&gt;. Keeping the &lt;code&gt;SPFILE&lt;/code&gt;, password file (&lt;code&gt;orapw&lt;/code&gt;), and auto-login wallet on &lt;code&gt;+DATA&lt;/code&gt; rather than &lt;code&gt;$ORACLE_HOME/dbs&lt;/code&gt; is what makes the new VM completely stateless until attach time.&lt;/p&gt;
&lt;p&gt;For &lt;strong&gt;Data Guard&lt;/strong&gt; topologies, the pivot orchestrator applies the same standby-first order used in traditional patching: pivot all physical standbys to the new golden image first (without running &lt;code&gt;datapatch&lt;/code&gt;, since standbys stay in &lt;code&gt;MOUNT&lt;/code&gt; / &lt;code&gt;READ ONLY&lt;/code&gt; managed recovery), verify redo transport and apply catch-up, execute a planned &lt;code&gt;dgmgrl SWITCHOVER&lt;/code&gt; if minimizing primary outage, and then pivot the former primary and run &lt;code&gt;datapatch&lt;/code&gt; once on the active primary so redo propagates the dictionary changes downstream.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -euo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pipefail&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DB_UNIQUE_NAME&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;${1}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; stop&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; database&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -db&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DB_UNIQUE_NAME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -stopoption&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; immediate&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -force&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; stop&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; asm&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -force&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; _ &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;seq&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 12&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;); &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  OFFLINE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;crsctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; stat&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; res&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; ora.asm&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -t&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; 2&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/dev/null&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;OFFLINE&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  [[ &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OFFLINE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; -gt&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ]] &amp;#x26;&amp;#x26; &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;break&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  sleep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;done&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;MOUNTED&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;asmcmd&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; lsdg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; 2&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/dev/null&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -v&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;^State&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;MOUNTED&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[[ &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;MOUNTED&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; -gt&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ]] &amp;#x26;&amp;#x26; { &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;ERROR: ASM disk groups still mounted&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;exit&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DB_UNIQUE_NAME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}:$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -u&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +%s):OFFLINE&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; openssl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; dgst&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -sha256&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -hmac&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;FLEET_HMAC_SECRET&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -binary&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; base64&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /tmp/shutdown_receipt.b64&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On the control-plane side (Temporal workflow), volume detach and attach calls are paced with exponential backoff and jitter, and the new VM polls &lt;code&gt;asmcmd lsdsk&lt;/code&gt; for up to 60 seconds after the cloud API reports &lt;code&gt;ATTACHED&lt;/code&gt; to wait for &lt;code&gt;udev&lt;/code&gt; device nodes before mounting ASM and running &lt;code&gt;datapatch&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; pivot_oracle_compute&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(db_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, new_image_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    db &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fleet_catalog.get(db_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    new_vm &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cloud_api.provision_vm(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;image_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;new_image_id, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.shape, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;subnet&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.subnet_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    # Gate: require valid KMS role on new_vm and signed ASM-offline receipt from old_vm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    assert&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cloud_api.verify_kms_decrypt(new_vm.id, db.kms_key_id), &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;New VM missing KMS access&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    receipt &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; agent.shutdown_db_and_unmount_asm(db.unique_name, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;timeout_seconds&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;300&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    assert&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; receipt.signature_valid, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Unverified shutdown — aborting before volume detach&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; vol &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.block_volumes:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        cloud_api.detach_volume_with_backoff(vol.id, db.vm_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; vol &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.block_volumes:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        cloud_api.attach_volume_with_backoff(vol.id, new_vm.id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    agent.wait_for_asm_disks(new_vm.id, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;timeout_seconds&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;60&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    agent.mount_asm_and_open_db(new_vm.id, db.unique_name)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    agent.run_datapatch(new_vm.id, db.unique_name)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; agent.verify_sqlpatch_registry(new_vm.id, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;expected_version&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;19.25.0.0&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        cloud_api.terminate_instance(db.vm_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; False&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At 20 concurrent regional pivots averaging 3 minutes per pivot (excluding long-running &lt;code&gt;datapatch&lt;/code&gt; tails on multi-PDB CDBs), a 2,000-database fleet finishes in &lt;code&gt;(2,000 / 20) * 3 min = 300 minutes&lt;/code&gt; (5 hours) with zero binary push across the network during the window. Just as importantly, if a single database’s &lt;code&gt;datapatch&lt;/code&gt; stalls on an invalid object or a &lt;code&gt;udev&lt;/code&gt; timeout, only that database’s Temporal workflow pauses; the remaining 19 parallel slots continue advancing without a central &lt;code&gt;rhpserver&lt;/code&gt; bottleneck or shared SSH runner queue blocking the wave.&lt;/p&gt;
&lt;p&gt;To keep cloud volume API retries deterministic under regional load, the Temporal activity separates &lt;strong&gt;provisioning&lt;/strong&gt; (&lt;code&gt;provision_vm&lt;/code&gt; + &lt;code&gt;verify_kms_decrypt&lt;/code&gt;, which can run ahead of the maintenance window) from the &lt;strong&gt;cutover critical path&lt;/strong&gt; (&lt;code&gt;shutdown_db&lt;/code&gt; -&gt; &lt;code&gt;detach&lt;/code&gt; -&gt; &lt;code&gt;attach&lt;/code&gt; -&gt; &lt;code&gt;mount&lt;/code&gt; -&gt; &lt;code&gt;datapatch&lt;/code&gt;). Pre-booting the replacement VM 10 minutes before the database stops removes OS boot and &lt;code&gt;cloud-init&lt;/code&gt; latency from the database downtime budget entirely, leaving only ASM dismount, block-volume reattachment, and instance startup inside the outage window.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2025-11-10-zero-touch-oracle-fleet-patching-fpp-5-flow.svg&quot; alt=&quot;Four-phase pivot flow preserving rollback boundaries before and after datapatch&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Externalize Root-Disk State.&lt;/strong&gt; Move auto-login wallets, &lt;code&gt;SPFILE&lt;/code&gt;s, &lt;code&gt;tnsnames.ora&lt;/code&gt;, and &lt;code&gt;sqlnet.ora&lt;/code&gt; onto ASM/ACFS or object storage/config management so the root volume holds zero unique database state. &lt;em&gt;Validation gate:&lt;/em&gt; Destroy and recreate a non-production VM from the current golden image and open the database without copying files from the old root disk. &lt;em&gt;Rollback:&lt;/em&gt; Restore root-disk config from backup.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Bake and Pre-Validate Golden Images.&lt;/strong&gt; Build the Packer image with pinned OS base OCID/AMI, CIS Level 1 hardening, and the target RU, then run &lt;code&gt;datapatch -prereq&lt;/code&gt; against a cloned test database. &lt;em&gt;Validation gate:&lt;/em&gt; Zero &lt;code&gt;opatch&lt;/code&gt; conflicts and clean &lt;code&gt;datapatch -prereq&lt;/code&gt; queue. &lt;em&gt;Rollback:&lt;/em&gt; Reject image promotion in the regional registry.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Canary Pivots with Old-VM Retention.&lt;/strong&gt; Pivot non-production and canary databases using the HMAC shutdown receipt and &lt;code&gt;udev&lt;/code&gt; polling, keeping the old image ID in the fleet catalog for 72 hours. &lt;em&gt;Validation gate:&lt;/em&gt; &lt;code&gt;DBA_REGISTRY_SQLPATCH&lt;/code&gt; reports &lt;code&gt;SUCCESS&lt;/code&gt; for the new RU, &lt;code&gt;opatch lsinventory&lt;/code&gt; matches the golden image manifest, and application smoke tests pass. &lt;em&gt;Rollback:&lt;/em&gt; Before &lt;code&gt;datapatch&lt;/code&gt; runs, reattach volumes to a VM booted from the old image directly; after &lt;code&gt;datapatch&lt;/code&gt; runs, boot a VM from the old image, start in &lt;code&gt;STARTUP UPGRADE&lt;/code&gt; if core components changed, and execute &lt;code&gt;datapatch -verbose&lt;/code&gt; from the old Oracle Home so Oracle applies the rollback archives stored in &lt;code&gt;SYSTEM&lt;/code&gt;. Make sure &lt;code&gt;ORACLE_HOME/sqlpatch&lt;/code&gt; on the old image remains intact so &lt;code&gt;datapatch&lt;/code&gt; can read every rollback descriptor.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Fleet Wave Execution.&lt;/strong&gt; Scale paced regional waves within cloud block-volume API rate limits, persisting pivot checkpoints to durable external storage so any control-plane restart resumes cleanly.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Oracle &lt;code&gt;datapatch&lt;/code&gt; rollback behavior (Oracle Database Utilities Guide):&lt;/strong&gt; A common misconception is that swapping back to an old VM image requires no &lt;code&gt;datapatch&lt;/code&gt; work. In reality, once &lt;code&gt;datapatch&lt;/code&gt; completes on the new image, &lt;code&gt;DBA_REGISTRY_SQLPATCH&lt;/code&gt; records the new Release Update in the database dictionary. Rolling back to the previous Oracle Home requires running &lt;code&gt;datapatch -verbose&lt;/code&gt; from that older home (often after &lt;code&gt;STARTUP UPGRADE&lt;/code&gt;) so Oracle executes the rollback scripts archived inside the &lt;code&gt;SYSTEM&lt;/code&gt; tablespace.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle FPP vs. immutable images (Oracle FPP Administrator’s Guide):&lt;/strong&gt; Oracle FPP (&lt;code&gt;rhpctl add workingcopy&lt;/code&gt; and &lt;code&gt;rhpctl move database&lt;/code&gt;) solves binary isolation inside an existing OS, which is essential on bare-metal Exadata where VMs are not swapped. On cloud VMs, Packer + block-volume pivoting eliminates both binary push during the window and multi-year Linux package drift.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle AI Database 26ai &lt;code&gt;datapatch&lt;/code&gt; and snapshot behavior (Oracle 26ai Documentation):&lt;/strong&gt; Oracle 26ai added a built-in &lt;strong&gt;blocking-session kill-switch&lt;/strong&gt; in &lt;code&gt;datapatch&lt;/code&gt; to terminate sessions holding dictionary locks that previously stalled maintenance windows. For storage/block-volume snapshots taken while a database is running, Oracle 26ai explicitly desupported &lt;code&gt;RECOVER...SNAPSHOT TIME&lt;/code&gt; and recommends bracketing snapshots with &lt;code&gt;ALTER DATABASE BEGIN BACKUP&lt;/code&gt; / &lt;code&gt;END BACKUP&lt;/code&gt; and recovering via &lt;code&gt;RECOVER...UNTIL TIME&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Condition&lt;/th&gt;&lt;th&gt;Breaks How&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Volume detached before ASM dismounts&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;ASM disk headers remain dirty; new VM fails to mount disk groups&lt;/td&gt;&lt;td&gt;&lt;code&gt;crsctl&lt;/code&gt; / &lt;code&gt;asmcmd lsdg&lt;/code&gt; mount errors (&lt;code&gt;ORA-15032&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;Enforce signed HMAC shutdown receipt after &lt;code&gt;0 MOUNTED&lt;/code&gt; disk groups before detach API&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Post-&lt;code&gt;datapatch&lt;/code&gt; rollback skips old-home &lt;code&gt;datapatch&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Database dictionary stays at newer RU than old VM binaries; &lt;code&gt;ORA-600&lt;/code&gt; / invalid registry&lt;/td&gt;&lt;td&gt;&lt;code&gt;DBA_REGISTRY_SQLPATCH&lt;/code&gt; version mismatch vs. &lt;code&gt;opatch lsinventory&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Run &lt;code&gt;datapatch -prereq&lt;/code&gt; then &lt;code&gt;datapatch -verbose&lt;/code&gt; from old image home (with &lt;code&gt;STARTUP UPGRADE&lt;/code&gt; if required)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Cloud volume API HTTP 429 throttling&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Parallel detach/attach calls fail mid-pivot, leaving volumes detached&lt;/td&gt;&lt;td&gt;Cloud API &lt;code&gt;429 TooManyRequests&lt;/code&gt; in Temporal activity logs&lt;/td&gt;&lt;td&gt;Cap concurrent pivots per region and use jittered exponential backoff&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Cloud &lt;code&gt;ATTACHED&lt;/code&gt; vs. Linux &lt;code&gt;udev&lt;/code&gt; race&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Cloud API reports volume attached before &lt;code&gt;/dev/oracleasm/disks/*&lt;/code&gt; appears&lt;/td&gt;&lt;td&gt;&lt;code&gt;asmcmd lsdsk&lt;/code&gt; returns missing disks immediately after boot&lt;/td&gt;&lt;td&gt;Poll &lt;code&gt;asmcmd lsdsk&lt;/code&gt; for up to 60s before starting ASM&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;KMS &lt;code&gt;kms:Decrypt&lt;/code&gt; drift on new VM&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;New VM attaches encrypted volumes, then fails ASM I/O on decrypt&lt;/td&gt;&lt;td&gt;Kernel / ASM I/O errors immediately on mount&lt;/td&gt;&lt;td&gt;Validate new VM instance-principal KMS permissions before detaching old VM&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Unpinned base OS image in Packer&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Floating OS tag pulls new kernel/RPMs, shifting &lt;code&gt;hugetlbpages&lt;/code&gt; or &lt;code&gt;shmmax&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Kernel/sysctl diff in image manifest&lt;/td&gt;&lt;td&gt;Pin exact base OS OCID/AMI ID and gate OS upgrades separately from DB RUs&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2025-11-10-zero-touch-oracle-fleet-patching-fpp-6-failures.svg&quot; alt=&quot;Four failure modes in block-volume pivoting and rollback&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Separate compute lifecycle from ASM storage:&lt;/strong&gt; Baking Oracle Homes and OS hardening offline via Packer removes multi-terabyte binary transfers from the production maintenance window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never detach a block volume on trust:&lt;/strong&gt; Gate every &lt;code&gt;DetachVolume&lt;/code&gt; API call on a cryptographic HMAC receipt proving &lt;code&gt;srvctl stop asm&lt;/code&gt; completed and &lt;code&gt;asmcmd lsdg&lt;/code&gt; reports zero mounted disk groups.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Poll &lt;code&gt;udev&lt;/code&gt; after cloud &lt;code&gt;ATTACHED&lt;/code&gt;:&lt;/strong&gt; Cloud control-plane APIs report attachment before Linux device nodes settle; wait on &lt;code&gt;asmcmd lsdsk&lt;/code&gt; before attempting ASM startup.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Post-&lt;code&gt;datapatch&lt;/code&gt; rollback still requires &lt;code&gt;datapatch&lt;/code&gt;:&lt;/strong&gt; Reverting to an older Oracle Home image after dictionary patching succeeds requires running &lt;code&gt;datapatch -verbose&lt;/code&gt; from the old home to apply stored rollback archives.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Externalize all local state first:&lt;/strong&gt; Wallets, &lt;code&gt;SPFILE&lt;/code&gt;s, and &lt;code&gt;tnsnames.ora&lt;/code&gt; must live on ASM/ACFS or external config stores before compute VMs can be treated as disposable.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Hub-and-spoke binary patching saturates central network uplinks at scale and leaves multi-year Linux OS drift intact across long-lived database VMs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Adopt an offline Packer golden-image pipeline paired with an HMAC-gated block-volume pivot workflow that swaps compute VMs around persistent ASM storage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Verify in a non-production canary pivot that &lt;code&gt;DBA_REGISTRY_SQLPATCH&lt;/code&gt; reflects the new Release Update with zero files copied from the old VM’s root disk.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your Oracle cloud VMs this week for root-disk dependencies—local wallets, &lt;code&gt;tnsnames.ora&lt;/code&gt;, and &lt;code&gt;SPFILE&lt;/code&gt;s—and migrate them to ASM or external secret/config storage as your prerequisite for immutable patching.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>checklist</category></item><item><title>Continuous Chaos: Automating Cross-Region DR Switchovers for 10,000 Databases</title><link>https://rajivonai.com/blog/2025-08-10-oracle-dr-game-days-automation-rto/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-08-10-oracle-dr-game-days-automation-rto/</guid><description>Why annual Disaster Recovery exercises are expensive corporate theater, and how hyper-scale engineering teams build autonomous Chaos Control Planes to continuously execute zero-downtime database switchovers in production.</description><pubDate>Mon, 27 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Testing disaster recovery once a year during a frozen maintenance window does not prove regional resilience; it only proves that a static snapshot of your topology worked before the next day’s code and firewall changes introduced drift.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Annual DR drills leave up to 364 days for hardcoded primary IPs, asymmetric DR firewall rules, undersized standby VMs, and stale JVM DNS caches to break failover silently.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Run an autonomous Temporal Chaos Control Plane during normal business hours that validates zero standby lag (&lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; + &lt;code&gt;DGMGRL&gt; VALIDATE DATABASE&lt;/code&gt;), executes planned &lt;code&gt;dgmgrl SWITCHOVER&lt;/code&gt; across small resilience rings, and measures application-tier Envoy 5xx recovery against your RTO budget.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; returns rows only on the standby (zero rows on the primary), JVM DNS caching (&lt;code&gt;networkaddress.cache.ttl&lt;/code&gt;) defaults to 30s or infinite (&lt;code&gt;-1&lt;/code&gt; under a SecurityManager), and cross-region cell splits will turn a successful DB switchover into a latency outage unless app cells fail over in lockstep.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2025-08-10-oracle-dr-game-days-automation-rto.svg&quot; alt=&quot;Autonomous DR chaos loop from pre-flight gate to application RTO verification&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Organizations operating tier-1 transactional databases under frameworks such as SOC 2 Type II, ISO 27001, PCI-DSS, and DORA are required to demonstrate both zero-data-loss Recovery Point Objectives (RPO = 0) and sub-minute Recovery Time Objectives (RTO). In Oracle estates, that architecture typically pairs a primary database in one cloud region (for example, &lt;code&gt;us-east-1&lt;/code&gt; or OCI Ashburn) with a synchronous or near-synchronous physical standby in a secondary region (&lt;code&gt;us-west-2&lt;/code&gt; or OCI Phoenix) managed by Oracle Data Guard Broker.&lt;/p&gt;
&lt;p&gt;Historically, teams validate that topology through an &lt;strong&gt;Annual DR Exercise&lt;/strong&gt;: weeks of spreadsheet runbook preparation, a multi-day or multi-week deployment freeze, an off-hours maintenance window where engineers manually adjust DNS or global load balancers and run &lt;code&gt;dgmgrl&lt;/code&gt; switchover commands, and a signed compliance report.&lt;/p&gt;
&lt;p&gt;The flaw in that operating model is temporal. A database platform does not sit in amber between annual tests. Application teams merge connection-string changes, network teams edit security groups, and infrastructure teams resize standby compute shapes every week. Every unverified change between annual exercises widens the gap between documented RTO and actual failover behavior.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Disaster recovery readiness decays at the velocity of application and infrastructure changes, driven by four concrete constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;RTO budget arithmetic leaves zero margin for DNS or redo-lag surprises:&lt;/strong&gt; Suppose a service carries a 30-second RTO SLA. Even a clean &lt;code&gt;dgmgrl SWITCHOVER&lt;/code&gt; spends time draining connections, reversing redo transport, opening the promoted primary, and notifying client pools via Fast Application Notification (FAN). If the standby holds 15 seconds of unapplied redo (&lt;code&gt;apply lag&lt;/code&gt;) and the JVM caches DNS for its default 30 seconds (or indefinitely when &lt;code&gt;networkaddress.cache.ttl = -1&lt;/code&gt; under a SecurityManager), &lt;code&gt;15s + 30s = 45s&lt;/code&gt; already blows past a 30-second RTO before the first post-failover query executes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hardcoded primary endpoints bypass service discovery:&lt;/strong&gt; A microservice deployed with a single-region VIP or private IP instead of a dual-region &lt;code&gt;ADDRESS_LIST&lt;/code&gt; connect descriptor works normally against Region 1—and fails completely the moment &lt;code&gt;dgmgrl&lt;/code&gt; reverses roles to Region 2.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Asymmetric DR firewall and NSG drift:&lt;/strong&gt; Security group or Network Security Group edits applied only to the active primary region leave port &lt;code&gt;1521&lt;/code&gt; or &lt;code&gt;2484&lt;/code&gt; blocked from application subnets in the DR region. Because steady-state traffic never touches the standby listener, the block remains invisible until failover.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Undersized standby compute shapes:&lt;/strong&gt; Downsizing standby VMs to reduce idle DR spend (for example, running 8 vCPUs on standby versus 32 vCPUs on primary) passes &lt;code&gt;dgmgrl&lt;/code&gt; role transition checks, then collapses under CPU saturation the second production traffic lands on the promoted primary.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2025-08-10-oracle-dr-game-days-automation-rto-2-constraints.svg&quot; alt=&quot;Four silent drifts that invalidate annual DR tests within days&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do you verify continuously across a multi-thousand-database fleet that every standby, network path, connection pool, and application tier can complete a zero-data-loss switchover within its RTO SLA—without relying on annual freezes or fragile human runbooks?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Operating Model&lt;/th&gt;&lt;th&gt;Drift Detection Window&lt;/th&gt;&lt;th&gt;Live App Path Verified?&lt;/th&gt;&lt;th&gt;Blast Radius Control&lt;/th&gt;&lt;th&gt;Why Chosen / Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Annual Manual DR Exercise&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Up to 364 days&lt;/td&gt;&lt;td&gt;Only under code freeze&lt;/td&gt;&lt;td&gt;All-or-nothing regional cutover&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Expensive theater; misses daily CI/CD endpoint, firewall, and DNS regressions.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Quarterly Scripted Game Day&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Up to 90 days&lt;/td&gt;&lt;td&gt;Off-hours synthetic load&lt;/td&gt;&lt;td&gt;Subsystem-wide&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Cuts drift to 90 days, but off-hours traffic hides real connection-pool and sizing bottlenecks.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Snapshot Standby Drill&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Continuous or weekly&lt;/td&gt;&lt;td&gt;No (synthetic SQL only)&lt;/td&gt;&lt;td&gt;Standby only&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Proves the DR database can open read-write, but never exercises live FAN/TAC replay or app-to-DR routing.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Autonomous Chaos Control Plane&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;&amp;#x3C; 24 hours (daily slice)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Yes (real Envoy 5xx &amp;#x26; latency)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Ring-fenced 1% daily canary&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Chosen:&lt;/strong&gt; Proves true end-to-end RTO during business hours with automated pre-flight abort and failback.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2025-08-10-oracle-dr-game-days-automation-rto-3-options.svg&quot; alt=&quot;Comparison of four disaster recovery operating models&quot;&gt;&lt;/p&gt;
&lt;p&gt;In practice, &lt;strong&gt;Oracle Snapshot Standby&lt;/strong&gt; (&lt;code&gt;ALTER DATABASE CONVERT TO SNAPSHOT STANDBY&lt;/code&gt;) is useful for testing schema migrations or isolated read-write workloads, but it cannot replace live switchover verification because production application pools never drain, reconnect, or replay transactions against the DR region during a snapshot test. Worse, converting a physical standby to a snapshot standby temporarily halts redo apply (even though redo continues to ship and queue), which increases the redo-apply backlog that would have to be drained if a real disaster struck during the test window. Only a controlled &lt;strong&gt;planned switchover&lt;/strong&gt; (&lt;code&gt;SWITCHOVER TO&lt;/code&gt;)—protected by strict pre-flight lag gates and ring-fenced blast radius—proves that the full stack from Envoy and UCP down to Data Guard Broker behaves as designed without degrading RPO.&lt;/p&gt;
&lt;p&gt;Another common alternative is &lt;strong&gt;DNS-only or GSLB-only failover testing&lt;/strong&gt; against a read-only Active Data Guard standby. While that verifies network reachability to the DR region, read-only queries do not exercise redo role reversal, &lt;code&gt;ALTER SYSTEM&lt;/code&gt; / write-path locks, standby redo log sizing on the demoted primary, or UCP transaction replay under TAC. Continuous &lt;code&gt;dgmgrl SWITCHOVER&lt;/code&gt; in small daily rings is the only mechanism that validates bidirectional role symmetry—proving both that Region 2 can run production writes and that Region 1 can cleanly re-synchronize as a standby afterward.&lt;/p&gt;
&lt;h2 id=&quot;the-chaos-control-plane-architecture&quot;&gt;The Chaos Control Plane Architecture&lt;/h2&gt;
&lt;p&gt;The target architecture embeds DR verification into normal operations via a Temporal-backed &lt;strong&gt;Chaos Control Plane&lt;/strong&gt; that selects a small daily slice of databases inside graduated resilience rings, validates zero lag, executes a Data Guard Broker switchover, verifies application-tier recovery metrics, and fails back cleanly.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2025-08-10-oracle-dr-game-days-automation-rto-4-architecture.svg&quot; alt=&quot;Temporal orchestrating Data Guard Broker switchovers against PromQL RTO gates&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;core-architectural-subsystems&quot;&gt;Core Architectural Subsystems&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Resilience Rings for Blast Radius Caging:&lt;/strong&gt; Databases are tagged into four rings—Ring 0 (internal/canary), Ring 1 (non-customer tooling), Ring 2 (standard services), and Ring 3 (tier-1 transactional ledgers). Chaos workflows target Ring 0 and Ring 1 first and halt promotion to Ring 3 whenever global error budgets burn below threshold.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standby-Side Lag and Broker Readiness Gates:&lt;/strong&gt; &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; returns rows &lt;strong&gt;only on the standby database&lt;/strong&gt; (it returns zero rows when queried on the primary). Before touching any role, the pre-flight activity queries &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; on the target standby for &lt;code&gt;transport lag&lt;/code&gt; and &lt;code&gt;apply lag&lt;/code&gt;, then runs &lt;code&gt;DGMGRL&gt; VALIDATE DATABASE &amp;#x3C;standby_db&gt;;&lt;/code&gt; to confirm &lt;code&gt;Ready for Switchover: Yes&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;FAN and Transparent Application Continuity (TAC):&lt;/strong&gt; Planned switchover (&lt;code&gt;SWITCHOVER TO&lt;/code&gt;) preserves zero data loss. Oracle Data Guard Broker coordinates the role reversal while Fast Application Notification (FAN) signals Universal Connection Pool (UCP) clients out-of-band over Oracle Notification Service (ONS) to abort idle connections and drain active ones at request boundaries. Transparent Application Continuity (TAC) then replays eligible in-flight transactions against the newly promoted primary without surfacing &lt;code&gt;ORA-&lt;/code&gt; connection errors to the caller, provided the JDBC connect string uses an &lt;code&gt;ADDRESS_LIST&lt;/code&gt; containing both regional SCAN endpoints with &lt;code&gt;FAILOVER=ON&lt;/code&gt;, &lt;code&gt;LOAD_BALANCE=OFF&lt;/code&gt;, and bounded &lt;code&gt;CONNECT_TIMEOUT&lt;/code&gt; / &lt;code&gt;TRANSPORT_CONNECT_TIMEOUT&lt;/code&gt; values so sockets never block on a dead TCP half-open.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Application-Tier RTO Verification:&lt;/strong&gt; Database &lt;code&gt;OPEN READ WRITE&lt;/code&gt; status is necessary but not sufficient. The control plane polls Envoy / API Gateway Prometheus metrics (&lt;code&gt;envoy_cluster_upstream_rq_xx&lt;/code&gt;) to confirm that HTTP 5xx error rates settle back below &lt;code&gt;0.1%&lt;/code&gt; within the target RTO window. Alongside HTTP 5xx ratios, the activity checks p99 upstream latency and active DB session counts so a silent connection-pool stall—where zero requests arrive because pods are stuck waiting on a stale socket—cannot masquerade as a healthy &lt;code&gt;0%&lt;/code&gt; error rate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Out-of-Band Emergency Dead-Man Switch:&lt;/strong&gt; An operator abort signal travels over a direct gRPC / Temporal Signal path rather than behind an asynchronous message queue, ensuring any in-flight experiment halts or fails back immediately. In addition, the Temporal workflow checks a global error-budget gate immediately before &lt;code&gt;ExecuteBrokerSwitchover&lt;/code&gt; and again before &lt;code&gt;HoldDuration&lt;/code&gt; completes; if an unrelated regional incident consumes the service’s error budget mid-experiment, the workflow skips the hold window and triggers failback right away.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;1-temporal-chaos-state-machine-go-sdk&quot;&gt;1. Temporal Chaos State Machine (Go SDK)&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;package&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; chaos&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;fmt&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;go.temporal.io/sdk/temporal&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;go.temporal.io/sdk/workflow&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ChaosSwitchoverRequest&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	DatabaseName     &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        `json:&quot;databaseName&quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	PrimaryRegion    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        `json:&quot;primaryRegion&quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	StandbyRegion    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        `json:&quot;standbyRegion&quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	TargetRtoSeconds &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;           `json:&quot;targetRtoSeconds&quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	HoldDuration     &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Duration&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; `json:&quot;holdDuration&quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ChaosSwitchoverWorkflow&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;ctx&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; workflow&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Context&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;req&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ChaosSwitchoverRequest&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	ao &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; workflow&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ActivityOptions&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		StartToCloseTimeout: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time.Minute,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		RetryPolicy:         &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;temporal&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;RetryPolicy&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{MaximumAttempts: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	ctx &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;WithActivityOptions&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, ao)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	logger &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;GetLogger&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; isReady &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ExecuteActivity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, AssertPreFlightSafety, req).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Get&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;isReady); err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;isReady {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		logger.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Warn&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Pre-flight gate failed; aborting with zero blast radius&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;error&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, err)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; switchoverDuration &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Duration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ExecuteActivity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, ExecuteBrokerSwitchover, req.DatabaseName, req.StandbyRegion).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Get&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;switchoverDuration); err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		_ &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ExecuteActivity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, EmergencyFailback, req.DatabaseName, req.PrimaryRegion).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Get&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; appRecovered &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ExecuteActivity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, AssertApplicationRecoverySLA, req.DatabaseName, req.TargetRtoSeconds).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Get&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;appRecovered); err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;appRecovered {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		_ &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ExecuteActivity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, EmergencyFailback, req.DatabaseName, req.PrimaryRegion).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Get&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Errorf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;RTO breach: application did not recover within &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;s&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, req.TargetRtoSeconds)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	workflow.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Sleep&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, req.HoldDuration)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ExecuteActivity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, ExecuteBrokerSwitchover, req.DatabaseName, req.PrimaryRegion).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Get&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;); err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; workflow.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ExecuteActivity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, PublishComplianceCertificate, req, switchoverDuration).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Get&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-standby-pre-flight-sql-and-broker-switchover&quot;&gt;2. Standby Pre-Flight SQL and Broker Switchover&lt;/h3&gt;
&lt;p&gt;Query &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; on the &lt;strong&gt;standby&lt;/strong&gt; before running &lt;code&gt;VALIDATE DATABASE&lt;/code&gt; and &lt;code&gt;SWITCHOVER TO&lt;/code&gt; in &lt;code&gt;dgmgrl&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Run on the physical standby (returns 0 rows on primary)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, unit, time_computed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;   v$dataguard_stats&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;transport lag&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;apply lag&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;apply finish time&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -euo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pipefail&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TARGET_ALIAS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;${1}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TARGET_STANDBY&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;${2}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OUTPUT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;dgmgrl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /@&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TARGET_ALIAS&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;VALIDATE DATABASE &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TARGET_STANDBY&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;SWITCHOVER TO &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TARGET_STANDBY&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EXIT;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OUTPUT&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OUTPUT&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -q&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Switchover succeeded, new primary is &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TARGET_STANDBY&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-application-rto-assertion-promql&quot;&gt;3. Application RTO Assertion (PromQL)&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  sum(rate(envoy_cluster_upstream_rq_xx{envoy_cluster_name=&quot;payments_service&quot;, response_code_class=&quot;5&quot;}[30s]))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  /&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  sum(rate(envoy_cluster_upstream_rq_xx{envoy_cluster_name=&quot;payments_service&quot;}[30s]))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;) &amp;#x3C; 0.001&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Autonomous production switchovers should never start on Day 1 against tier-1 databases. Roll out in four gated phases:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2025-08-10-oracle-dr-game-days-automation-rto-5-flow.svg&quot; alt=&quot;Four-phase ring-fenced rollout from passive lag audit to Tier-1 rotation&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Passive Lag and Broker Validation.&lt;/strong&gt; Run continuous read-only checks (&lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; on standbys and &lt;code&gt;DGMGRL&gt; VALIDATE DATABASE&lt;/code&gt;) across the fleet to catch asymmetric firewall blocks, stale broker metadata, and chronic redo lag. &lt;em&gt;Validation gate:&lt;/em&gt; Zero &lt;code&gt;VALIDATE DATABASE&lt;/code&gt; errors and zero unplanned lag spikes across 30 consecutive days. &lt;em&gt;Rollback:&lt;/em&gt; Disable the passive validation cron.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Ring 0 Canary Switchovers.&lt;/strong&gt; Enable &lt;code&gt;ChaosSwitchoverWorkflow&lt;/code&gt; for internal non-customer databases (Ring 0) during business hours with short hold durations (for example, 10 minutes). &lt;em&gt;Validation gate:&lt;/em&gt; Application Envoy 5xx ratio returns below &lt;code&gt;0.1%&lt;/code&gt; within the target RTO and failback succeeds automatically. &lt;em&gt;Rollback:&lt;/em&gt; Trigger the Temporal dead-man switch signal or flip the Ring 0 feature flag off.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Application Cell Lockstep.&lt;/strong&gt; Pair database switchovers with regional application-cell traffic shifting so upstream services never make cross-region calls to a promoted standby while pods stay pinned in Region 1. &lt;em&gt;Validation gate:&lt;/em&gt; End-to-end p99 request latency remains within regional baseline after switchover. &lt;em&gt;Rollback:&lt;/em&gt; Fail back both app cell routing and Data Guard primary together.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Tier-1 Daily Rotation.&lt;/strong&gt; Expand daily randomized selection (for example, 1% of eligible instances per day) to Ring 2 and Ring 3 services, gated by a global error-budget check that freezes all chaos runs during real incidents.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Continuous fault injection precedent (Netflix TechBlog):&lt;/strong&gt; Netflix’s Chaos Engineering evolution (from Chaos Monkey to ChAP / Failure Injection Testing) demonstrated that testing failure modes continuously in production during business hours catches configuration and routing drift that annual or off-hours drills miss—provided blast radius is tightly bounded and automated abort gates watch live business metrics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle Data Guard Broker &lt;code&gt;SWITCHOVER TO&lt;/code&gt; behavior (Oracle Data Guard Broker Documentation):&lt;/strong&gt; Unlike a forced failover (&lt;code&gt;FAILOVER TO&lt;/code&gt;), &lt;code&gt;SWITCHOVER TO&lt;/code&gt; in Oracle 19c and 23ai performs automated pre-switchover validation, preserves redo continuity, and transitions the former primary into a synchronized physical standby without needing an RMAN rebuild. That symmetry also exposes missing Standby Redo Logs (SRLs) or mismatched &lt;code&gt;DB_FILE_NAME_CONVERT&lt;/code&gt; / &lt;code&gt;LOG_FILE_NAME_CONVERT&lt;/code&gt; parameters on the original primary the first time it flips to standby—catching asymmetric storage configuration long before a real disaster.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle Database 23ai resiliency vs. external fault injection (Oracle HA Overview):&lt;/strong&gt; Oracle Database 23ai does not expose a SQL-level chaos-injection API to simulate corruption or packet drops; instead, Oracle 23ai provides Raft-based replication for Globally Distributed Database and MAA Application Continuity for session replay, while teams inject network latency or packet loss externally via Linux &lt;code&gt;tc&lt;/code&gt;, Chaos Mesh, or VCN route/security rules.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;JVM DNS TTL behavior (Java &lt;code&gt;InetAddress&lt;/code&gt; specification):&lt;/strong&gt; Because the JVM caches resolved hostnames for 30 seconds by default (or forever when &lt;code&gt;networkaddress.cache.ttl = -1&lt;/code&gt; under a SecurityManager), applications using DNS aliases rather than SCAN/ONS addresses must explicitly set &lt;code&gt;-Dsun.net.inetaddr.ttl=5&lt;/code&gt; (or &lt;code&gt;networkaddress.cache.ttl=5&lt;/code&gt;) so connections re-resolve within a 30-second RTO budget.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Condition&lt;/th&gt;&lt;th&gt;Breaks How&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Logon storm on promoted primary&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Application pods drop connections simultaneously after FAN drain and flood the new primary listener&lt;/td&gt;&lt;td&gt;&lt;code&gt;TNS-12518&lt;/code&gt; / listener CPU saturation during switchover&lt;/td&gt;&lt;td&gt;Enforce Oracle UCP connection rate limiting or route through Oracle Connection Manager (CMAN)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Redo apply lag spikes before trigger&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Heavy batch load builds standby apply backlog; switchover stalls waiting for redo catch-up&lt;/td&gt;&lt;td&gt;&lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; on standby shows &lt;code&gt;apply lag &gt; 0&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Abort pre-flight immediately if standby &lt;code&gt;transport lag&lt;/code&gt; or &lt;code&gt;apply lag&lt;/code&gt; is non-zero&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Asymmetric app-to-DB region split&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Database moves to Region 2 while microservices stay in Region 1, adding cross-region RTT to every query&lt;/td&gt;&lt;td&gt;Sudden p99 query latency jump despite healthy DB CPU&lt;/td&gt;&lt;td&gt;Execute chaos switchovers at the &lt;strong&gt;Application Cell&lt;/strong&gt; boundary in lockstep&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Stale JVM DNS cache&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;App threads keep dialing Region 1 IP after role reversal until JVM cache expires&lt;/td&gt;&lt;td&gt;Connection timeouts / &lt;code&gt;ORA-12514&lt;/code&gt; on Region 1 after switchover&lt;/td&gt;&lt;td&gt;Set &lt;code&gt;networkaddress.cache.ttl=5&lt;/code&gt; in base container images and use dual-region &lt;code&gt;ADDRESS_LIST&lt;/code&gt; + FAN&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Dead-man switch queued behind backlog&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Emergency abort message sits behind congested Kafka partitions while workflow continues&lt;/td&gt;&lt;td&gt;Abort command latency &gt; 5 seconds&lt;/td&gt;&lt;td&gt;Send dead-man abort via direct out-of-band Temporal Signal / gRPC&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Standby tablespace / ASM space mismatch&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Primary extends a datafile that fails on standby due to smaller storage quota; MRP halts&lt;/td&gt;&lt;td&gt;Alert log &lt;code&gt;ORA-01119&lt;/code&gt; / MRP stopped in &lt;code&gt;DGMGRL&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Audit standby ASM diskgroup / filesystem free headroom in pre-flight checks&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2025-08-10-oracle-dr-game-days-automation-rto-6-failures.svg&quot; alt=&quot;Four failure modes in automated Data Guard chaos switchovers&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Annual DR drills measure a past snapshot, not today’s fleet:&lt;/strong&gt; Endpoint, firewall, and compute-shape drift accumulate within days of normal CI/CD releases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Query &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; on the standby, never the primary:&lt;/strong&gt; The view returns zero rows on the primary; pair standby lag checks with &lt;code&gt;DGMGRL&gt; VALIDATE DATABASE&lt;/code&gt; before every automated switchover.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Database &lt;code&gt;OPEN READ WRITE&lt;/code&gt; is not application RTO:&lt;/strong&gt; Always gate chaos success on downstream Envoy/API Gateway 5xx recovery and latency, backed by an out-of-band Temporal dead-man switch.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fail over application cells and databases together:&lt;/strong&gt; Moving a database cross-region while leaving pods behind turns a clean &lt;code&gt;dgmgrl SWITCHOVER&lt;/code&gt; into a cross-region latency outage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Control DNS caching and logon storms explicitly:&lt;/strong&gt; Cap JVM &lt;code&gt;networkaddress.cache.ttl&lt;/code&gt; at 5 seconds and use Oracle UCP + FAN/TAC so connection pools drain and replay without saturating the promoted listener.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Annual DR exercises create a false sense of resilience while daily application, firewall, and DNS changes silently break cross-region failover readiness.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Replace annual theater with a Temporal-backed Chaos Control Plane that runs continuous, ring-fenced &lt;code&gt;dgmgrl SWITCHOVER&lt;/code&gt; experiments gated by standby lag invariants and live Envoy 5xx RTO assertions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Confirm 30 consecutive days of zero &lt;code&gt;VALIDATE DATABASE&lt;/code&gt; errors and &lt;code&gt;0s&lt;/code&gt; standby apply lag across your fleet before authorizing automated Ring 0 canary switchovers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Deploy a read-only &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; and &lt;code&gt;DGMGRL&gt; VALIDATE DATABASE&lt;/code&gt; check across your Data Guard standbys this week to surface hidden DR drift before your next failover test.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>Embedding Backfill in Postgres: Batch Size, WAL, Autovacuum, and Bloat</title><link>https://rajivonai.com/blog/2026-06-08-embedding-backfill-postgres-wal-autovacuum-bloat/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-08-embedding-backfill-postgres-wal-autovacuum-bloat/</guid><description>How large embedding backfills stress PostgreSQL through batch size, WAL growth, checkpoints, autovacuum lag, bloat, index timing, throttling, and rollback planning.</description><pubDate>Sun, 26 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The application team called it a backfill. The database saw a sustained write-heavy migration across millions of rows, large vector values, extra indexes, WAL growth, replica lag, dead tuples, and autovacuum falling behind. Embedding backfills are not background chores. They are production database events.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;An embedding backfill should be operated like a controlled data migration. Batch size, commit frequency, worker concurrency, index timing, autovacuum behavior, WAL retention, checkpoint pressure, replica lag, and rollback strategy all matter.&lt;/p&gt;
&lt;p&gt;The safest pattern is usually: write new embeddings into a separate nullable column or table, process in bounded idempotent batches, throttle based on database signals, build or rebuild vector indexes after the bulk load when appropriate, validate retrieval with a fixed query set, then switch reads deliberately.&lt;/p&gt;
&lt;p&gt;Do not let an AI feature team run an unbounded embedding job against production Postgres. The failure mode looks like database instability, not model instability.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Embedding backfills usually arrive after a prototype already exists. The application team has chunks, a model, and a table with nullable embeddings; the database team inherits the job of making the bulk write safe.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Embedding pipelines are often designed from the application outward:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Select rows without embeddings.&lt;/li&gt;
&lt;li&gt;Send text to an embedding model.&lt;/li&gt;
&lt;li&gt;Update the row.&lt;/li&gt;
&lt;li&gt;Repeat until done.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;That loop hides the database cost. Every update creates WAL. Every update creates dead tuples under MVCC. Indexes must be maintained. Replicas must receive and apply changes. Long transactions can block vacuum. Large batches can create bursty checkpoints. Failed jobs can leave partial state. Rollbacks can be more expensive than the work being rolled back.&lt;/p&gt;
&lt;p&gt;The embedding vector itself can be large enough to change the storage profile of the table. If the same table also serves OLTP reads, the backfill competes with normal application traffic.&lt;/p&gt;
&lt;h2 id=&quot;core-technical-explanation&quot;&gt;Core Technical Explanation&lt;/h2&gt;
&lt;p&gt;PostgreSQL updates do not overwrite rows in place in the simple mental model. Under MVCC, an update creates a new row version and leaves old versions for vacuum to reclaim. For embedding backfills, that means a table with millions of existing chunks can churn heavily when a nullable &lt;code&gt;embedding&lt;/code&gt; column is populated.&lt;/p&gt;
&lt;p&gt;The pressure points are connected:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Worker[embedding workers] --&gt; Batch[batch updates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Batch --&gt; WAL[WAL growth]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Batch --&gt; Dead[dead tuples]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Batch --&gt; Index[index maintenance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WAL --&gt; Replica[replica lag]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WAL --&gt; Checkpoint[checkpoint pressure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Dead --&gt; Vacuum[autovacuum work]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Index --&gt; Bloat[index bloat]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Vacuum --&gt; Health[database health]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Batch size controls transaction duration and burst size. Too small, and overhead dominates. Too large, and locks, WAL bursts, rollback time, and replica lag become harder to control.&lt;/p&gt;
&lt;p&gt;Worker concurrency controls write pressure. More workers may increase embedding throughput while degrading database latency. The correct throttle is not “as fast as the model provider allows.” It is “as fast as Postgres can absorb while keeping OLTP healthy.”&lt;/p&gt;
&lt;p&gt;Index timing controls maintenance cost. Updating rows while a vector index already exists maintains that index row by row during every update — the same general PostgreSQL principle that applies to any index during a bulk load, not something specific to vector indexes. For a first backfill on a table with no existing vector index, loading embeddings first and building the HNSW or IVFFlat index afterward avoids that per-row maintenance cost entirely. This is a general bulk-load pattern; confirm it still applies if the backfill is instead adding rows to a table that already has a live index in production use, since dropping and rebuilding that index has its own availability tradeoff.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Use an explicit job table instead of scanning the base table repeatedly:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; embedding_backfill_jobs&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  chunk_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DEFAULT&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;pending&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  attempts &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DEFAULT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  locked_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  completed_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  error &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Workers claim a bounded number of jobs, generate embeddings, update the target rows, and mark jobs complete in short transactions. Keep job claiming idempotent. A worker crash should not create duplicate chunks or leave unknown state.&lt;/p&gt;
&lt;p&gt;Prefer a separate embeddings table when the source table is hot:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; chunk_embeddings&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  chunk_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  embedding_model &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  content_hash &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  embedding vector(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1536&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DEFAULT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This avoids rewriting a wide source row just to attach a vector. It also makes model-version migrations easier because a new model can be loaded side by side before reads switch.&lt;/p&gt;
&lt;p&gt;Throttle on database metrics. If replica lag exceeds the threshold, pause. If autovacuum falls behind, pause. If checkpoint or WAL pressure rises, reduce batch size. If OLTP latency degrades, stop the backfill before users discover it.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Production symptom&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Batch too large&lt;/td&gt;&lt;td&gt;Long transactions and painful rollback&lt;/td&gt;&lt;td&gt;Use small bounded commits&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Too many workers&lt;/td&gt;&lt;td&gt;OLTP latency and replica lag rise&lt;/td&gt;&lt;td&gt;Central throttle and worker limits&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backfill updates hot table&lt;/td&gt;&lt;td&gt;Table and index bloat grow quickly&lt;/td&gt;&lt;td&gt;Separate embedding table or staged writes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Index exists before bulk load&lt;/td&gt;&lt;td&gt;Write amplification during every update&lt;/td&gt;&lt;td&gt;Consider load first, index after&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Autovacuum cannot keep up&lt;/td&gt;&lt;td&gt;Dead tuples and storage growth&lt;/td&gt;&lt;td&gt;Tune table autovacuum and pause jobs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No rollback plan&lt;/td&gt;&lt;td&gt;Half-migrated model version leaks into search&lt;/td&gt;&lt;td&gt;Version reads and switch by feature flag&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Provider retry storm&lt;/td&gt;&lt;td&gt;Database receives duplicate writes&lt;/td&gt;&lt;td&gt;Idempotent jobs and content hashes&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-and-tenancy-notes&quot;&gt;Security and Tenancy Notes&lt;/h2&gt;
&lt;p&gt;Backfill workers need the minimum database privileges required to read source text and write embeddings. Do not run them with broad application owner credentials if the corpus contains tenant or regulated data.&lt;/p&gt;
&lt;p&gt;Tenant boundaries still matter in background jobs. A job queue that accidentally mixes tenant-scoped source records with a shared output table can create retrieval exposure later. Store tenant ID and source document ID with the embedding row, and validate negative retrieval cases before enabling the new vectors.&lt;/p&gt;
&lt;p&gt;If embeddings are generated by an external provider, data-processing and retention rules need review — this is a contractual and compliance question for whichever team owns the data-processing agreement with that provider, not a database configuration setting.&lt;/p&gt;
&lt;h2 id=&quot;cost-notes&quot;&gt;Cost Notes&lt;/h2&gt;
&lt;p&gt;Backfill cost has four parts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Embedding generation cost.&lt;/li&gt;
&lt;li&gt;Worker compute cost.&lt;/li&gt;
&lt;li&gt;Database write, WAL, storage, and backup growth.&lt;/li&gt;
&lt;li&gt;Larger or longer-running indexes and replicas.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The surprise is backup and replica cost. A massive backfill can increase storage and WAL retention, and the new vectors may be captured in backups immediately. If the model migration is later rolled back, the database may still carry the storage and cleanup cost.&lt;/p&gt;
&lt;h2 id=&quot;observability-notes&quot;&gt;Observability Notes&lt;/h2&gt;
&lt;p&gt;Track:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Rows processed per minute.&lt;/li&gt;
&lt;li&gt;Jobs pending, running, failed, and retried.&lt;/li&gt;
&lt;li&gt;Average and p95 transaction duration.&lt;/li&gt;
&lt;li&gt;WAL generation rate.&lt;/li&gt;
&lt;li&gt;Replica lag.&lt;/li&gt;
&lt;li&gt;Autovacuum activity and dead tuples on target tables.&lt;/li&gt;
&lt;li&gt;Table and index size growth.&lt;/li&gt;
&lt;li&gt;OLTP query latency during the backfill.&lt;/li&gt;
&lt;li&gt;Vector query correctness before and after read cutover.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Add a kill switch. Backfills need operational controls, not only dashboards.&lt;/p&gt;
&lt;h2 id=&quot;backup-restore-and-dr-notes&quot;&gt;Backup, Restore, and DR Notes&lt;/h2&gt;
&lt;p&gt;A backfill can cross backup boundaries. If a restore lands in the middle of the migration, the system must know whether to resume, roll forward, or discard partial embeddings.&lt;/p&gt;
&lt;p&gt;Store model version, content hash, and backfill batch ID. Make the read path select only completed model versions. During restore, resume from the job table and recompute rows whose content hash no longer matches.&lt;/p&gt;
&lt;p&gt;For DR, confirm the target environment can run the same embedding jobs or has a restored copy of the completed vectors. Without that, the database RTO and retrieval RTO diverge.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Is the embedding stored in the source table or a separate table?&lt;/li&gt;
&lt;li&gt;What is the maximum transaction size and rollback time?&lt;/li&gt;
&lt;li&gt;How many workers can run before OLTP latency moves?&lt;/li&gt;
&lt;li&gt;What metrics pause or slow the backfill?&lt;/li&gt;
&lt;li&gt;Are vector indexes built before or after the bulk load?&lt;/li&gt;
&lt;li&gt;Are model versions isolated in the read path?&lt;/li&gt;
&lt;li&gt;Can the job resume after crash, failover, or restore?&lt;/li&gt;
&lt;li&gt;Is autovacuum tuned for the target table?&lt;/li&gt;
&lt;li&gt;Has replica lag been tested under backfill load?&lt;/li&gt;
&lt;li&gt;Is there a feature flag for read cutover?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Embedding backfills get designed from the application outward — select rows, embed, update, repeat — hiding the database cost of WAL growth, dead tuples, index maintenance, and replica lag until production feels it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Operate the backfill like a controlled migration: bounded idempotent batches via a job table, a separate embeddings table when the source is hot, index builds after the bulk load, and throttling driven by replica lag, autovacuum, and OLTP latency signals.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The backfill can be paused mid-run without data loss or duplicate writes, and OLTP query latency stays within its normal band while the backfill is running.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, confirm the backfill job has a kill switch and is throttled on at least replica lag and autovacuum activity, not just embedding-provider rate limits.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;PostgreSQL MVCC and routine vacuuming documentation: &lt;a href=&quot;https://www.postgresql.org/docs/current/routine-vacuuming.html&quot;&gt;https://www.postgresql.org/docs/current/routine-vacuuming.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL WAL configuration and checkpoint documentation: &lt;a href=&quot;https://www.postgresql.org/docs/current/wal-configuration.html&quot;&gt;https://www.postgresql.org/docs/current/wal-configuration.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;pgvector README for vector column, HNSW, IVFFlat, and index build behavior: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL monitoring statistics documentation (replication lag, autovacuum activity): &lt;a href=&quot;https://www.postgresql.org/docs/current/monitoring-stats.html&quot;&gt;https://www.postgresql.org/docs/current/monitoring-stats.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL index build progress reporting: &lt;a href=&quot;https://www.postgresql.org/docs/current/progress-reporting.html&quot;&gt;https://www.postgresql.org/docs/current/progress-reporting.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>failures</category><category>architecture</category><category>checklist</category></item><item><title>pgvector Backup and Restore: Why Embeddings Change Your RPO/RTO Thinking</title><link>https://rajivonai.com/blog/2026-06-04-pgvector-backup-restore-rpo-rto/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-04-pgvector-backup-restore-rpo-rto/</guid><description>Why pgvector changes backup and restore planning for RAG systems, including vector column size, index rebuilds, embedding reproducibility, source-of-truth design, and DR runbooks.</description><pubDate>Sat, 25 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The database restored cleanly. The application came back. Then support search returned old policy chunks, missed the latest incident runbooks, and mixed two embedding model versions in the same result set. The backup met the database RPO. It failed the retrieval RPO.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;pgvector changes recovery planning because embeddings are both data and derived data. The rows may live in Postgres, but the operational question is whether the retrieval system can be restored to a known semantic state.&lt;/p&gt;
&lt;p&gt;RPO and RTO need separate answers for source records, chunk rows, embedding vectors, vector indexes, and query-quality validation. A restore is not complete when PostgreSQL starts. It is complete when authorized users can retrieve the right documents, deleted documents stay hidden, and the rebuilt or restored vector indexes behave within an accepted tolerance.&lt;/p&gt;
&lt;p&gt;For every pgvector RAG system, decide whether embeddings are recoverable state, rebuildable derived state, or both. That decision drives backup size, restore time, index rebuild time, model-version retention, and DR runbooks.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL backup plans usually prove that a database can be restored to a consistent point in time. RAG systems need that, but they also need the retrieval layer to come back with the right corpus, model version, indexes, and authorization behavior.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Traditional Postgres backup plans focus on transactionally correct recovery: base backups, WAL, point-in-time restore, replica promotion, and application reconnects. RAG adds another layer:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Source documents may be restored to one point in time.&lt;/li&gt;
&lt;li&gt;Chunk tables may represent a deterministic transformation of those documents.&lt;/li&gt;
&lt;li&gt;Embeddings may depend on an external model version.&lt;/li&gt;
&lt;li&gt;Vector indexes may take significant time to rebuild.&lt;/li&gt;
&lt;li&gt;Query results may change after rebuild even when rows are present.&lt;/li&gt;
&lt;li&gt;Authorization failures can leak restored chunks from deleted or moved tenants.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This creates a subtle recovery trap. If embeddings are backed up as ordinary rows, restore may be storage-heavy but straightforward. If embeddings are treated as rebuildable, restore may be smaller but RTO depends on re-chunking, re-embedding, rate limits, model availability, and index build time.&lt;/p&gt;
&lt;p&gt;Neither answer is universally correct. The mistake is not choosing.&lt;/p&gt;
&lt;h2 id=&quot;core-technical-explanation&quot;&gt;Core Technical Explanation&lt;/h2&gt;
&lt;p&gt;A production pgvector schema usually has at least four recovery surfaces:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Source[source records] --&gt; Chunks[chunk rows]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Chunks --&gt; Embeddings[embedding vectors]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Embeddings --&gt; Index[pgvector index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Source --&gt; Backup[database backup]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Chunks --&gt; Backup&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Embeddings --&gt; Backup&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Index --&gt; Rebuild[index rebuild]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Backup --&gt; Restore[restore drill]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Rebuild --&gt; Validate[retrieval validation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Restore --&gt; Validate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The base table is the business truth: documents, owners, tenants, lifecycle state, permissions, and deletion markers. Chunk rows are a projection of that truth. Embeddings are a numerical representation of chunk text under a specific model, dimension, and preprocessing pipeline. The vector index is an access path over those embeddings.&lt;/p&gt;
&lt;p&gt;Backup and restore planning depends on which of those surfaces you preserve.&lt;/p&gt;
&lt;p&gt;If you back up embeddings, you preserve the exact vectors from the backup point. That supports faster recovery if the database restore is enough and the index is still usable or can be rebuilt quickly. The cost is larger backups, larger restores, more storage, and more WAL.&lt;/p&gt;
&lt;p&gt;If you rebuild embeddings, you need reproducible chunking, retained source text, retained model identity, access to the embedding model, and enough batch capacity. That can reduce backup dependence on derived vectors, but it moves RTO into the ingestion pipeline.&lt;/p&gt;
&lt;p&gt;Vector indexes are a third concern, and the split follows PostgreSQL’s general backup taxonomy. A physical backup (&lt;code&gt;pg_basebackup&lt;/code&gt; or a filesystem/WAL-based snapshot) copies the data directory as-is, so index relation files — including HNSW and IVFFlat indexes — restore intact with no rebuild needed. A logical backup (&lt;code&gt;pg_dump&lt;/code&gt;) stores only the &lt;code&gt;CREATE INDEX&lt;/code&gt; statements, not the index data itself, so every index, vector or otherwise, is rebuilt from scratch on restore — and an HNSW rebuild on a large embedding table is a materially different time cost than rebuilding a B-tree. Migration paths that use logical dump-and-restore (major-version upgrades, some managed-service migration tools) inherit this rebuild cost; confirm which backup method a given DR path actually uses before estimating RTO.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The most robust operating model separates recovery ownership:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Source-of-truth owner.&lt;/strong&gt; Defines which records must be recoverable, which deletes are permanent, and what point in time matters for business correctness.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Embedding pipeline owner.&lt;/strong&gt; Owns chunking code, model version, dimensions, batching, idempotency, and rebuild throughput.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Database owner.&lt;/strong&gt; Owns backup schedules, WAL retention, restore procedures, index rebuild windows, and query-plan validation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Product owner.&lt;/strong&gt; Defines acceptable retrieval degradation during recovery. Some systems can run lexical fallback for a few hours. Others cannot answer without semantic retrieval.&lt;/p&gt;
&lt;p&gt;Store recovery metadata in the database:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Sketch only; adapt to the local schema.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; embedding_batches&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigserial&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  embedding_model &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  model_dimension &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  chunker_version &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  started_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  completed_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  source_snapshot &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The point is not the exact table. The point is to make recovery inspectable. During an incident, the team should be able to answer which model produced the vectors, whether a backfill completed, and which chunks are stale.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it hurts&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Embeddings treated as disposable without rebuild capacity&lt;/td&gt;&lt;td&gt;Restore finishes but search remains degraded for days&lt;/td&gt;&lt;td&gt;Measure rebuild throughput before relying on it&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Embeddings backed up without model metadata&lt;/td&gt;&lt;td&gt;Restored vectors cannot be compared or regenerated safely&lt;/td&gt;&lt;td&gt;Store model, dimension, chunker, and content hash&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Index rebuild omitted from RTO&lt;/td&gt;&lt;td&gt;Database is online but retrieval queries are too slow&lt;/td&gt;&lt;td&gt;Include index build time in restore drills&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Deleted documents restored into retrieval&lt;/td&gt;&lt;td&gt;Security incident after point-in-time restore&lt;/td&gt;&lt;td&gt;Replay deletes or validate lifecycle filters&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Mixed model versions after partial restore&lt;/td&gt;&lt;td&gt;Ranking quality changes silently&lt;/td&gt;&lt;td&gt;Query by model version and isolate migrations&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DR region lacks embedding provider access&lt;/td&gt;&lt;td&gt;Rebuild runbook fails outside primary region&lt;/td&gt;&lt;td&gt;Pre-approve provider access and secrets&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-and-tenancy-notes&quot;&gt;Security and Tenancy Notes&lt;/h2&gt;
&lt;p&gt;Backup and restore can break tenant boundaries when restored data is routed through a different environment. A DR drill should verify tenant filters, row-level policies if used, application roles, and secret scoping.&lt;/p&gt;
&lt;p&gt;Embedding content deserves the same classification as source text. Even if vectors are not human-readable, retrieval can expose the underlying sensitive text. Backups containing embeddings should inherit the data-retention and encryption requirements of the source corpus — this is a policy decision for the team’s own data classification framework, not a pgvector-specific setting, so it needs sign-off from whoever owns compliance for the source corpus rather than a database-team default.&lt;/p&gt;
&lt;p&gt;For tenant deletion, decide whether a restored point in time is allowed to bring back deleted tenant chunks. If not, the runbook needs a post-restore deletion replay or a tombstone source that survives restore.&lt;/p&gt;
&lt;h2 id=&quot;cost-notes&quot;&gt;Cost Notes&lt;/h2&gt;
&lt;p&gt;Embedding recovery cost appears in three places.&lt;/p&gt;
&lt;p&gt;First, storage and backup cost increase when vectors are stored as ordinary database rows. Second, restore cost increases when large vector tables and indexes extend recovery time. Third, rebuild cost appears as embedding API spend, worker compute, database writes, and index maintenance.&lt;/p&gt;
&lt;p&gt;Do not compare “backup embeddings” against “rebuild embeddings” as if rebuild is free. Rebuild has a cloud bill and an operational bill.&lt;/p&gt;
&lt;h2 id=&quot;observability-notes&quot;&gt;Observability Notes&lt;/h2&gt;
&lt;p&gt;Useful recovery metrics include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Backup age and restore test age.&lt;/li&gt;
&lt;li&gt;Vector table size, index size, and growth rate.&lt;/li&gt;
&lt;li&gt;Embedding freshness lag by corpus and tenant.&lt;/li&gt;
&lt;li&gt;Count of chunks by model version and chunker version.&lt;/li&gt;
&lt;li&gt;Rebuild throughput in chunks per minute.&lt;/li&gt;
&lt;li&gt;Index build duration in staging and production.&lt;/li&gt;
&lt;li&gt;Query latency during degraded mode.&lt;/li&gt;
&lt;li&gt;Retrieval validation pass rate after restore.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Keep a small fixed query set for recovery validation. It should include tenant-specific queries, deleted-document negative tests, exact identifier queries, and semantic queries that depend on embeddings.&lt;/p&gt;
&lt;h2 id=&quot;backup-restore-and-dr-notes&quot;&gt;Backup, Restore, and DR Notes&lt;/h2&gt;
&lt;p&gt;Write the runbook in phases:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Restore the database to the target point in time.&lt;/li&gt;
&lt;li&gt;Confirm schema, extensions, roles, and vector columns.&lt;/li&gt;
&lt;li&gt;Confirm embedding model versions present in restored rows.&lt;/li&gt;
&lt;li&gt;Rebuild or validate vector indexes.&lt;/li&gt;
&lt;li&gt;Replay tombstones or post-restore corrections if required.&lt;/li&gt;
&lt;li&gt;Run retrieval validation queries.&lt;/li&gt;
&lt;li&gt;Resume embedding workers in controlled mode.&lt;/li&gt;
&lt;li&gt;Watch lag, query plans, and error rates.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For DR, predefine degraded modes. A support assistant may run lexical search only. A compliance retrieval system may need to stay down until semantic validation passes. A customer-facing RAG feature may need a banner or feature flag if freshness is behind.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Are embeddings backed up, rebuilt, or both?&lt;/li&gt;
&lt;li&gt;Is chunking deterministic across deploys?&lt;/li&gt;
&lt;li&gt;Are model version, dimension, content hash, and chunker version stored?&lt;/li&gt;
&lt;li&gt;Has index rebuild time been measured on realistic data?&lt;/li&gt;
&lt;li&gt;Can the embedding provider be reached from the DR environment?&lt;/li&gt;
&lt;li&gt;Are deletes and tenant moves replayable after point-in-time restore?&lt;/li&gt;
&lt;li&gt;Does the runbook include retrieval-quality validation?&lt;/li&gt;
&lt;li&gt;Is there a degraded mode when vector search is unavailable?&lt;/li&gt;
&lt;li&gt;Are backup retention and encryption policies aligned with source data classification?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; A database restore that meets the PostgreSQL RPO/RTO can still fail the retrieval RPO — old chunks reappear, model versions mix, or deleted documents come back searchable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Decide explicitly whether embeddings are recoverable state (backed up as rows) or rebuildable state (regenerated from source), store model/dimension/chunker metadata to make that decision inspectable, and include index rebuild time and retrieval validation in RTO — not just database uptime.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; A restore drill runs the fixed recovery query set (tenant-specific queries, deleted-document negative tests, semantic queries) and passes before the team declares recovery complete.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, confirm whether the last restore drill validated retrieval quality or only database availability, and add the missing validation step if it didn’t.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;PostgreSQL backup, WAL, and point-in-time recovery documentation: &lt;a href=&quot;https://www.postgresql.org/docs/current/backup.html&quot;&gt;https://www.postgresql.org/docs/current/backup.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;pgvector README for index creation, HNSW, IVFFlat, and operator behavior: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Aurora PostgreSQL backup, restore, and point-in-time recovery documentation (if running on Aurora): &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Managing.Backups.html&quot;&gt;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Managing.Backups.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL &lt;code&gt;CREATE INDEX&lt;/code&gt; and &lt;code&gt;REINDEX&lt;/code&gt; documentation: &lt;a href=&quot;https://www.postgresql.org/docs/current/sql-createindex.html&quot;&gt;https://www.postgresql.org/docs/current/sql-createindex.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL index build progress reporting: &lt;a href=&quot;https://www.postgresql.org/docs/current/progress-reporting.html&quot;&gt;https://www.postgresql.org/docs/current/progress-reporting.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>failures</category><category>checklist</category></item><item><title>pgvector on Aurora PostgreSQL: What DBAs Need to Watch</title><link>https://rajivonai.com/blog/2026-06-01-pgvector-on-aurora-postgresql-dba-watchlist/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-01-pgvector-on-aurora-postgresql-dba-watchlist/</guid><description>A DBA watchlist for running pgvector on Aurora PostgreSQL, covering extension support, memory, I/O, WAL, replicas, failover, backups, parameters, and cost.</description><pubDate>Fri, 24 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The RAG prototype worked on a small PostgreSQL instance. Then the production team moved it to Aurora PostgreSQL, enabled pgvector, loaded millions of embeddings, and discovered that the bottleneck was no longer “vector search.” It was memory pressure, redo volume, replica lag, parameter discipline, and a failover path nobody had tested with the new index workload.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;Aurora PostgreSQL can be a reasonable place to run pgvector when Postgres is already the system of record and the team wants one relational operating model. It is not just “Postgres with cheaper storage” or “managed pgvector.” Aurora changes the failure and cost map because compute, storage, replicas, backups, and I/O behave differently from a self-managed PostgreSQL server.&lt;/p&gt;
&lt;p&gt;Treat pgvector on Aurora as a database workload first and a RAG feature second. Verify pgvector extension availability, version support, parameter group settings, instance memory, WAL and redo behavior, replica lag, failover behavior, backup retention, and restore time before calling the design production-ready.&lt;/p&gt;
&lt;p&gt;Aurora extension versions and feature support are engine-version-specific. As of this writing, Aurora PostgreSQL supports pgvector 0.8.0 on clusters running PostgreSQL 16.8, 15.12, 14.17, and 13.20 or higher; earlier pgvector releases (0.7.0, 0.5.0) require correspondingly older minimum engine versions. Confirm the pinned pgvector and engine version pair against AWS’s release notes before design sign-off, since AWS ships new pgvector support as engine-version-gated announcements, not a rolling upgrade.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Aurora is often the approved PostgreSQL platform before the RAG project starts. Security already understands it, platform teams already monitor it, and DBAs already have backup and failover routines.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Many teams choose Aurora because it is already approved by security, already monitored by platform engineering, and already integrated with backup and failover workflows. That is a strong reason to start there. The mistake is assuming that pgvector introduces only a new column type and an index.&lt;/p&gt;
&lt;p&gt;Vector search changes the workload:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Embedding columns can be large relative to ordinary relational attributes.&lt;/li&gt;
&lt;li&gt;HNSW and IVFFlat indexes can consume substantial memory and storage.&lt;/li&gt;
&lt;li&gt;Backfills generate sustained write pressure.&lt;/li&gt;
&lt;li&gt;Approximate search can compete with OLTP queries for CPU.&lt;/li&gt;
&lt;li&gt;Index rebuilds can stress maintenance memory and I/O.&lt;/li&gt;
&lt;li&gt;Replicas can become part of the retrieval path, which makes lag visible to users.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Aurora adds another layer. Storage is distributed. I/O is billed differently from a local-disk Postgres mental model. Replicas share the storage architecture but still have independent compute, cache, and query pressure. Failover moves the writer role, but the application still has to handle connections, prepared statements, transaction retries, and warm-up behavior.&lt;/p&gt;
&lt;h2 id=&quot;core-technical-explanation&quot;&gt;Core Technical Explanation&lt;/h2&gt;
&lt;p&gt;pgvector stores embeddings as a PostgreSQL data type and supports distance operators and vector indexes. In Aurora PostgreSQL, those objects live inside the Aurora engine and are managed through the same schema, extension, backup, and parameter mechanisms as other PostgreSQL objects. That gives DBAs familiar controls, but it also means vector workload pressure lands on the same database fleet.&lt;/p&gt;
&lt;p&gt;Three areas deserve early review.&lt;/p&gt;
&lt;p&gt;First, extension support is not generic. Aurora PostgreSQL supports a defined set of extensions by engine version and region, and pgvector’s supported feature set (HNSW, iterative scans, specific operator classes) tracks the pgvector version AWS has certified for that engine version — not necessarily the latest upstream release. A design that depends on a specific pgvector feature, index type, operator class, or query behavior needs an explicit compatibility check against the AWS Aurora PostgreSQL extension support page for the target engine version.&lt;/p&gt;
&lt;p&gt;Second, memory is a production constraint. Vector indexes can require memory during build and query. HNSW build behavior is especially sensitive to whether the graph fits in &lt;code&gt;maintenance_work_mem&lt;/code&gt;. In Aurora, &lt;code&gt;maintenance_work_mem&lt;/code&gt; is a DB (instance) parameter group setting, not a DB cluster parameter group setting — it applies per instance and can differ between the writer and readers, and an instance-level value takes precedence over anything set at the cluster level. Raising memory-related settings in a parameter group is not free; it changes the memory budget for the whole instance class.&lt;/p&gt;
&lt;p&gt;Third, Aurora I/O and redo behavior are not invisible. Embedding backfills, index builds, and frequent updates can create sustained write traffic. That can affect cost, replica freshness, and recovery behavior. Avoid designing the workload as if the only cost is the vector query itself.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The safer architecture is to keep the write path explicit.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[application write] --&gt; Writer[Aurora writer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Writer --&gt; Base[documents and chunks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Writer --&gt; Jobs[embedding job table]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Jobs --&gt; Worker[embedding workers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Worker --&gt; Emb[embedding rows]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Emb --&gt; Index[pgvector indexes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[retrieval query] --&gt; Reader[Aurora reader or writer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Reader --&gt; Auth[tenant and entitlement filters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Auth --&gt; Index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Writer --&gt; Backup[Aurora backup and restore plan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use the writer for source-of-truth changes and controlled embedding writes. Use readers only when the product can tolerate replica behavior for retrieval. If the answer must reflect a just-written document, route that query to the writer or design a freshness contract that says when new documents become searchable.&lt;/p&gt;
&lt;p&gt;Keep embedding workers bounded. They should use small batches, statement timeouts, retryable jobs, and explicit pause controls. The DBA should be able to stop embedding writes without taking down the application.&lt;/p&gt;
&lt;p&gt;Put vector schema changes through normal migration review. A migration that creates an HNSW index on a large table is not equivalent to adding a small B-tree. It needs a build window, rollback plan, metrics, and a way to compare retrieval quality before and after the index appears.&lt;/p&gt;
&lt;p&gt;Use parameter groups deliberately. A DB cluster parameter group applies cluster-wide; a DB (instance) parameter group applies per instance and overrides the cluster parameter group for any setting present in both. Settings related to maintenance work, autovacuum, checkpoint behavior, query timeout, and connection limits should be reviewed against the instance class at the level (cluster or instance) where they actually apply. Do not copy settings from self-managed Postgres without checking which parameter group tier Aurora expects them at.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Production symptom&lt;/th&gt;&lt;th&gt;DBA response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Unsupported pgvector feature&lt;/td&gt;&lt;td&gt;Migration passes locally and fails in Aurora&lt;/td&gt;&lt;td&gt;Pin engine version and extension version during design&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Instance memory pressure&lt;/td&gt;&lt;td&gt;Query latency rises or build slows dramatically&lt;/td&gt;&lt;td&gt;Test build settings on production-shaped data&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backfill overwhelms write path&lt;/td&gt;&lt;td&gt;WAL or redo pressure, replica lag, cost spike&lt;/td&gt;&lt;td&gt;Throttle workers and batch commits&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Retrieval routed to lagging reader&lt;/td&gt;&lt;td&gt;Recently updated content is missing&lt;/td&gt;&lt;td&gt;Define freshness routing and lag thresholds&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failover untested with vector traffic&lt;/td&gt;&lt;td&gt;Application reconnects but retrieval latency spikes&lt;/td&gt;&lt;td&gt;Run failover drills during read and backfill load&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Index rebuild treated as routine&lt;/td&gt;&lt;td&gt;Maintenance window overruns&lt;/td&gt;&lt;td&gt;Time rebuilds in staging with realistic row counts&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-and-tenancy-notes&quot;&gt;Security and Tenancy Notes&lt;/h2&gt;
&lt;p&gt;Aurora does not remove the need for query-time authorization. Tenant ID, entitlement state, deletion state, and document lifecycle fields should be part of the retrieval query, not a post-filter after chunks leave the database.&lt;/p&gt;
&lt;p&gt;For multi-tenant RAG, decide whether tenants share tables, use partitions, or get stronger isolation through separate schemas or clusters. Shared tables are simpler to operate but place more pressure on query filters and indexes. Cluster-level isolation is stronger but multiplies cost and migration work.&lt;/p&gt;
&lt;p&gt;Row-level security may be appropriate for some applications, but retrieval queries and background workers must be tested against it — a worker role that bypasses RLS (common for batch embedding jobs) can silently defeat the isolation the application layer assumes is enforced at the database.&lt;/p&gt;
&lt;h2 id=&quot;cost-notes&quot;&gt;Cost Notes&lt;/h2&gt;
&lt;p&gt;Cost is not only the Aurora instance size. Include storage growth from embedding columns, vector index storage, backup retention, read replicas, I/O, backfill compute, and larger instances chosen to protect memory headroom.&lt;/p&gt;
&lt;p&gt;The trap is sizing for normal OLTP load and then treating RAG as free because it lives in the same database. If vector queries force a larger writer or larger readers, pgvector has a visible infrastructure cost even without a separate vector database.&lt;/p&gt;
&lt;h2 id=&quot;observability-notes&quot;&gt;Observability Notes&lt;/h2&gt;
&lt;p&gt;Minimum useful signals:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Vector query latency by route, tenant, and query class.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;EXPLAIN&lt;/code&gt; plans for representative retrieval queries.&lt;/li&gt;
&lt;li&gt;Index size and table size growth.&lt;/li&gt;
&lt;li&gt;Embedding job lag, failure count, and retry count.&lt;/li&gt;
&lt;li&gt;Aurora reader lag and cache behavior during retrieval.&lt;/li&gt;
&lt;li&gt;Write throughput during embedding backfills.&lt;/li&gt;
&lt;li&gt;Autovacuum lag and dead tuple trends on chunk tables.&lt;/li&gt;
&lt;li&gt;Backup age, restore test age, and failover drill results.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Track empty-result rate and under-return rate too. Users experience those as answer quality problems, but the root cause may be a plan, filter, index, or replica-freshness issue.&lt;/p&gt;
&lt;h2 id=&quot;backup-restore-and-dr-notes&quot;&gt;Backup, Restore, and DR Notes&lt;/h2&gt;
&lt;p&gt;Aurora backup gives you a database recovery mechanism, but RAG recovery needs semantic checks. Restoring rows is not enough if the restored retrieval path cannot answer the expected query set.&lt;/p&gt;
&lt;p&gt;Keep source document IDs, chunk hashes, embedding model versions, and ingestion timestamps. After a restore, run validation queries that prove deleted records stay deleted, tenant filters still work, and representative questions return acceptable candidates.&lt;/p&gt;
&lt;p&gt;For cross-region DR, decide whether embeddings are restored from database backup, rebuilt from source records, or replicated through another pipeline. Each choice changes RPO, RTO, and cost. Aurora Global Database is the documented option here: it replicates across regions with typical replication lag under a second and a managed-failover RTO under a minute, or zero data loss on a planned switchover — but that RPO/RTO applies to the database layer, not to whether the restored retrieval index still answers the expected query set. Test both.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Is the required pgvector version supported on the target Aurora PostgreSQL version?&lt;/li&gt;
&lt;li&gt;Are extension, parameter group, and engine-version choices pinned?&lt;/li&gt;
&lt;li&gt;Have HNSW or IVFFlat builds been timed on production-shaped data?&lt;/li&gt;
&lt;li&gt;Can embedding workers be throttled or paused without a deploy?&lt;/li&gt;
&lt;li&gt;Are vector queries isolated from critical OLTP routes?&lt;/li&gt;
&lt;li&gt;Does retrieval tolerate reader lag, or must it read from the writer?&lt;/li&gt;
&lt;li&gt;Have failover drills included active retrieval and active backfill load?&lt;/li&gt;
&lt;li&gt;Does backup validation include retrieval behavior, not only database availability?&lt;/li&gt;
&lt;li&gt;Is the cost model explicit about I/O, replicas, storage, backups, and larger instance classes?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Teams move a pgvector prototype onto Aurora and treat it as “Postgres with cheaper storage,” then discover the real bottleneck is memory pressure, redo volume, replica lag, or an untested failover path.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Treat pgvector on Aurora as a database workload first — pin the pgvector and engine version pair, review parameter groups at the correct tier (cluster vs. instance), and test failover and restore with active retrieval and backfill load.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; A failover drill run under active retrieval and backfill traffic completes within the expected RTO, and a restore validation query set confirms deleted records stay deleted and tenant filters still work.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, confirm the pinned pgvector version is certified for the deployed Aurora PostgreSQL engine version, and schedule a failover drill if one hasn’t run under vector workload conditions.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Amazon Aurora PostgreSQL extensions and pgvector version support: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraPostgreSQLReleaseNotes/AuroraPostgreSQL.Extensions.html&quot;&gt;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraPostgreSQLReleaseNotes/AuroraPostgreSQL.Extensions.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Amazon Aurora parameter groups (cluster vs. instance level): &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/USER_WorkingWithParamGroups.html&quot;&gt;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/USER_WorkingWithParamGroups.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Amazon Aurora Global Database (cross-region DR, RPO/RTO): &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-global-database.html&quot;&gt;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-global-database.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;pgvector README for index support, HNSW, IVFFlat, and tuning parameters: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL documentation for &lt;code&gt;EXPLAIN&lt;/code&gt;, autovacuum, and &lt;code&gt;CREATE INDEX&lt;/code&gt;: &lt;a href=&quot;https://www.postgresql.org/docs/current/using-explain.html&quot;&gt;https://www.postgresql.org/docs/current/using-explain.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>cloud</category><category>architecture</category><category>checklist</category></item><item><title>From POC to Production: A Natural Language Search Migration Playbook</title><link>https://rajivonai.com/blog/2026-04-13-poc-to-production-natural-language-search-playbook/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-04-13-poc-to-production-natural-language-search-playbook/</guid><description>A staged playbook for moving from Postgres and pgvector prototypes to hybrid search, agents, and GraphRAG only when the workload requires it.</description><pubDate>Thu, 23 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The fastest way to waste money on AI search is to migrate infrastructure before the retrieval problem is measured. The fastest way to break production is to keep the prototype after the workload has clearly outgrown it.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most natural language search systems start as POCs.&lt;/p&gt;
&lt;p&gt;A team chunks documents, creates embeddings, stores vectors, retrieves top results, and asks an LLM to answer. The first demo works because the query set is small, the corpus is curated, and the users are forgiving.&lt;/p&gt;
&lt;p&gt;Then production arrives.&lt;/p&gt;
&lt;p&gt;Users ask exact-token queries. They apply filters. They expect freshness. They ask for prices, inventory, policy versions, tenant-specific data, and comparisons. They click bad results. They notice missing results. They ask the system to take actions.&lt;/p&gt;
&lt;p&gt;The architecture has to evolve without turning every POC into a platform rebuild.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Teams need two opposing disciplines.&lt;/p&gt;
&lt;p&gt;First, do not overbuild too early. A controlled internal knowledge base may not need a search cluster or graph pipeline on day one.&lt;/p&gt;
&lt;p&gt;Second, do not underbuild after evidence appears. A consumer-facing search product cannot be operated like a notebook demo with embeddings.&lt;/p&gt;
&lt;p&gt;The migration decision should be evidence-driven. The system should graduate when the workload crosses a specific boundary: relevance, scale, filters, freshness, cost, operations, security, or workflow complexity.&lt;/p&gt;
&lt;h2 id=&quot;the-staged-architecture&quot;&gt;The Staged Architecture&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    POC[POC retrieval] --&gt; PG[Postgres hybrid]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PG --&gt; SRCH[Search platform]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SRCH --&gt; VEC[Specialized vector service]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SRCH --&gt; AG[Agent layer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SRCH --&gt; GR[Graph retrieval]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    AG --&gt; PROD[Production workflow]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    GR --&gt; PROD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    VEC --&gt; PROD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Stage 1 is the POC. It proves that the corpus, chunking, embeddings, and prompt can answer useful questions.&lt;/p&gt;
&lt;p&gt;Stage 2 is database-shaped production. PostgreSQL full-text search plus pgvector may be enough when the corpus is controlled, authorization is relational, query volume is modest, and one operating model matters.&lt;/p&gt;
&lt;p&gt;Stage 3 is search-shaped production. OpenSearch or Elasticsearch becomes attractive when facets, analyzers, typo tolerance, geo, ranking, query analytics, and high-volume relevance tuning matter.&lt;/p&gt;
&lt;p&gt;Stage 4 is retrieval specialization. Qdrant, Weaviate, Pinecone, Milvus, or another vector-native system may fit when vector retrieval, sparse plus dense search, payload filtering, multivectors, or managed vector operations become central.&lt;/p&gt;
&lt;p&gt;Stage 5 is workflow expansion. Agentic RAG appears when users need outcomes, tools, retries, validation, and governed actions.&lt;/p&gt;
&lt;p&gt;Stage 6 is relationship expansion. GraphRAG appears when entity links, communities, and corpus-level synthesis become the value.&lt;/p&gt;
&lt;p&gt;The important point is that these are not maturity badges. They are workload responses.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented behavior of the major systems supports a staged approach.&lt;/p&gt;
&lt;p&gt;Postgres and pgvector provide a credible first production step because full-text search, SQL filters, and vector indexes can live in one database. That reduces operational overhead when the workload is internal and controlled.&lt;/p&gt;
&lt;p&gt;OpenSearch and Elasticsearch provide a search-platform step because hybrid search, analyzers, filters, geo, ranking, and relevance tooling are core to search serving systems. That matters when search quality becomes a product metric.&lt;/p&gt;
&lt;p&gt;Weaviate, Qdrant, and Pinecone provide specialized retrieval options because each supports hybrid or dense plus sparse retrieval patterns with service-oriented operating models. That matters when retrieval needs are no longer best expressed as relational tables or search-engine documents.&lt;/p&gt;
&lt;p&gt;GraphRAG provides a relationship step because its documented architecture extracts entities, builds graph structure, creates community summaries, and supports local and global graph search. That matters when questions require links and synthesis across the corpus.&lt;/p&gt;
&lt;p&gt;Agent systems provide a workflow step because they can plan and call tools. That matters only when the product contract includes action.&lt;/p&gt;
&lt;h2 id=&quot;graduation-signals&quot;&gt;Graduation Signals&lt;/h2&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Move&lt;/th&gt;&lt;th&gt;Evidence that justifies it&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;POC to Postgres hybrid&lt;/td&gt;&lt;td&gt;Users need exact terms and semantic recall under SQL filters&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Postgres hybrid to search platform&lt;/td&gt;&lt;td&gt;Facets, typo tolerance, geo, analyzers, ranking, or query analytics become central&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Search platform to vector service&lt;/td&gt;&lt;td&gt;Vector workload needs specialized filtering, sparse plus dense retrieval, or retrieval-service isolation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Search to agent layer&lt;/td&gt;&lt;td&gt;Users need tool calls, validation, retries, or side effects&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hybrid search to GraphRAG&lt;/td&gt;&lt;td&gt;Relationships, communities, or corpus-level synthesis drive value&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Any stage to managed service&lt;/td&gt;&lt;td&gt;Team lacks operational capacity and accepts vendor cost&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Mistake&lt;/th&gt;&lt;th&gt;Result&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;No evaluation set&lt;/td&gt;&lt;td&gt;Architecture debates become opinion&lt;/td&gt;&lt;td&gt;Build query benchmarks first&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No source-of-truth boundary&lt;/td&gt;&lt;td&gt;LLM or index invents final facts&lt;/td&gt;&lt;td&gt;Keep authoritative systems explicit&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No freshness metric&lt;/td&gt;&lt;td&gt;Stale search becomes invisible&lt;/td&gt;&lt;td&gt;Track ingestion and embedding lag&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No rollback&lt;/td&gt;&lt;td&gt;Relevance releases become risky&lt;/td&gt;&lt;td&gt;Use aliases, versions, and config rollback&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No degraded mode&lt;/td&gt;&lt;td&gt;Partial outages become full outages&lt;/td&gt;&lt;td&gt;Define lexical-only or read-only fallback&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No cost model&lt;/td&gt;&lt;td&gt;Reranking and agents surprise the budget&lt;/td&gt;&lt;td&gt;Track per-query and per-stage cost&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No ownership model&lt;/td&gt;&lt;td&gt;DBAs, search engineers, and AI teams overlap badly&lt;/td&gt;&lt;td&gt;Assign operational ownership by layer&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Decide whether the current system is failing because of retrieval quality, search product requirements, vector scale, workflow complexity, or relationship reasoning.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Graduate one layer at a time: Postgres hybrid, search platform, vector service, agent layer, or graph retrieval.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Maintain a query evaluation suite with expected results, hard-filter tests, stale-data tests, permission-negative tests, and workflow simulations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before migration, define source of truth, ingestion replay, index rebuild, relevance rollback, observability, cost budget, degraded mode, and incident owner.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The POC is allowed to be simple. Production is not allowed to be vague.&lt;/p&gt;
&lt;p&gt;A mature natural language search architecture is not the one with the most AI components. It is the one whose retrieval path, truth boundary, failure modes, and operating model are clear enough to debug under pressure.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;PostgreSQL full-text search controls: &lt;a href=&quot;https://www.postgresql.org/docs/current/textsearch-controls.html&quot;&gt;https://www.postgresql.org/docs/current/textsearch-controls.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;pgvector README: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch hybrid search documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/ai-search/hybrid-search/index/&quot;&gt;https://docs.opensearch.org/latest/vector-search/ai-search/hybrid-search/index/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Elasticsearch hybrid search documentation: &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/semantic-text-hybrid-search.html&quot;&gt;https://www.elastic.co/guide/en/elasticsearch/reference/current/semantic-text-hybrid-search.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Weaviate hybrid search documentation: &lt;a href=&quot;https://docs.weaviate.io/weaviate/search/hybrid&quot;&gt;https://docs.weaviate.io/weaviate/search/hybrid&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Qdrant hybrid queries documentation: &lt;a href=&quot;https://qdrant.tech/documentation/concepts/hybrid-queries/&quot;&gt;https://qdrant.tech/documentation/concepts/hybrid-queries/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Pinecone hybrid search documentation: &lt;a href=&quot;https://docs.pinecone.io/guides/search/hybrid-search&quot;&gt;https://docs.pinecone.io/guides/search/hybrid-search&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Microsoft GraphRAG documentation: &lt;a href=&quot;https://microsoft.github.io/graphrag/&quot;&gt;https://microsoft.github.io/graphrag/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>ai-engineering</category><category>checklist</category></item><item><title>GraphRAG: Use It When Relationships Matter, Not When Search Is Hard</title><link>https://rajivonai.com/blog/2026-04-06-graphrag-when-relationships-matter/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-04-06-graphrag-when-relationships-matter/</guid><description>A production decision guide for GraphRAG, entity graphs, community summaries, relationship reasoning, and where graph retrieval is overkill.</description><pubDate>Wed, 22 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;GraphRAG is not advanced RAG for every workload. It is a relationship architecture. Use it when the value comes from entities, links, communities, and corpus-level synthesis, not when the user only needs a filtered list or a direct answer from a few documents.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Some questions are hard for ordinary retrieval because the answer is not in one chunk.&lt;/p&gt;
&lt;p&gt;“Which vendors share ownership patterns across these contracts?”&lt;/p&gt;
&lt;p&gt;“Which incidents involve the same service dependency chain?”&lt;/p&gt;
&lt;p&gt;“Which neighborhoods show related signals across schools, commute, inventory, and price movement?”&lt;/p&gt;
&lt;p&gt;“Which accounts, addresses, devices, and transactions appear connected?”&lt;/p&gt;
&lt;p&gt;These questions are not just semantic similarity problems. They require entities, relationships, paths, clusters, and summaries across a corpus.&lt;/p&gt;
&lt;p&gt;That is the space where GraphRAG becomes useful.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;GraphRAG is often introduced as if it is simply better RAG. That framing is dangerous.&lt;/p&gt;
&lt;p&gt;GraphRAG adds an ingestion pipeline: entity extraction, relationship extraction, graph construction, community detection, summarization, graph retrieval, document retrieval, and citation strategy. Each layer has cost and failure modes.&lt;/p&gt;
&lt;p&gt;If the user asks “show available three-bedroom homes under a price limit,” graph retrieval is not the main problem. The system needs structured filters, geo, price, availability, ranking, and fresh listings.&lt;/p&gt;
&lt;p&gt;If the user asks “how are these properties, owners, agents, schools, transactions, and neighborhood changes related,” then a graph may become central.&lt;/p&gt;
&lt;p&gt;The decision is whether relationships are the product.&lt;/p&gt;
&lt;h2 id=&quot;the-graphrag-pattern&quot;&gt;The GraphRAG Pattern&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DOC[Documents and records] --&gt; ENT[Entity extraction]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ENT --&gt; REL[Relationship extraction]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    REL --&gt; KG[Knowledge graph]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    KG --&gt; COM[Community detection]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    COM --&gt; SUM[Community summaries]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Q[User query] --&gt; CLS[Query classifier]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CLS --&gt; L{Query type}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|Entity| LOCAL[Local graph search]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|Corpus| GLOBAL[Global summary search]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|Lookup| HYB[Hybrid search]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    LOCAL --&gt; CTX[Context builder]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    GLOBAL --&gt; CTX&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    HYB --&gt; CTX&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CTX --&gt; ANS[Answer with evidence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;GraphRAG has two broad retrieval shapes.&lt;/p&gt;
&lt;p&gt;Local search starts from specific entities and their connected facts. This is useful for questions about a person, property, company, system, service, clause, account, or transaction.&lt;/p&gt;
&lt;p&gt;Global search uses communities and summaries to answer broad corpus-level questions. This is useful when the question asks for patterns, themes, risks, clusters, or trends across many documents.&lt;/p&gt;
&lt;p&gt;Neither replaces hybrid search. The best architectures often keep lexical and vector retrieval for ordinary lookup and add graph retrieval for relationship-heavy questions.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Microsoft’s GraphRAG documentation describes a pipeline that extracts a knowledge graph from text, detects communities, creates community reports, and uses local or global search for answering questions. The documented pattern makes the tradeoff clear: GraphRAG adds structure so the system can reason across entities and communities, but that structure must be built and maintained.&lt;/p&gt;
&lt;p&gt;The production pattern is strongest in domains where entity links carry value:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Investigation systems: accounts, addresses, devices, transactions, organizations, events.&lt;/li&gt;
&lt;li&gt;Legal discovery: people, clauses, communications, dates, obligations, disputes.&lt;/li&gt;
&lt;li&gt;Enterprise architecture: services, teams, owners, incidents, dependencies, repositories.&lt;/li&gt;
&lt;li&gt;Market intelligence: assets, locations, transactions, public signals, entities, timelines.&lt;/li&gt;
&lt;li&gt;Research corpora: papers, authors, methods, datasets, claims, citations.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The pattern is weaker for direct lookup:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Find available inventory.&lt;/li&gt;
&lt;li&gt;Search product names.&lt;/li&gt;
&lt;li&gt;Retrieve a policy section.&lt;/li&gt;
&lt;li&gt;Answer from a single runbook.&lt;/li&gt;
&lt;li&gt;Filter listings by price and location.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Those are usually hybrid search problems first.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Graph built from weak extraction&lt;/td&gt;&lt;td&gt;Relationships are noisy or false&lt;/td&gt;&lt;td&gt;Evaluate extraction quality and keep provenance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Freshness is undefined&lt;/td&gt;&lt;td&gt;Graph answers from stale links&lt;/td&gt;&lt;td&gt;Track source version and update lag&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Citations are vague&lt;/td&gt;&lt;td&gt;Summary hides original evidence&lt;/td&gt;&lt;td&gt;Link graph facts back to source records&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Graph used for simple lookup&lt;/td&gt;&lt;td&gt;Cost rises without better results&lt;/td&gt;&lt;td&gt;Route ordinary queries to hybrid search&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Entity resolution is weak&lt;/td&gt;&lt;td&gt;Same entity splits or merges incorrectly&lt;/td&gt;&lt;td&gt;Build entity identity rules&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Community summaries drift&lt;/td&gt;&lt;td&gt;Global answers become outdated&lt;/td&gt;&lt;td&gt;Version summaries and rebuild intentionally&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Permissions ignored&lt;/td&gt;&lt;td&gt;Graph traversal crosses access boundaries&lt;/td&gt;&lt;td&gt;Enforce security at node, edge, and source levels&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The hardest production issue is not drawing the graph. It is keeping the graph correct as source systems change.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Determine whether the high-value questions depend on relationships, communities, indirect links, ownership, events, or corpus-level synthesis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Add GraphRAG as a specialized retrieval path beside hybrid search, not as a replacement for search.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Build evaluation questions that require multi-hop relationships, entity-specific context, global summaries, and negative cases where no relationship should be inferred.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Design graph ingestion with provenance, entity resolution, access control, update cadence, summary versioning, citation strategy, and human review for high-stakes outputs.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;GraphRAG is powerful when the question is “how are these things connected?”&lt;/p&gt;
&lt;p&gt;It is overkill when the question is “which items match these filters?”&lt;/p&gt;
&lt;p&gt;That distinction should be made before the graph pipeline is built.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Microsoft GraphRAG documentation: &lt;a href=&quot;https://microsoft.github.io/graphrag/&quot;&gt;https://microsoft.github.io/graphrag/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Microsoft GraphRAG indexing architecture: &lt;a href=&quot;https://microsoft.github.io/graphrag/index/architecture/&quot;&gt;https://microsoft.github.io/graphrag/index/architecture/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Microsoft GraphRAG querying documentation: &lt;a href=&quot;https://microsoft.github.io/graphrag/query/overview/&quot;&gt;https://microsoft.github.io/graphrag/query/overview/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>ai-engineering</category><category>architecture</category><category>databases</category><category>vector-db</category><category>checklist</category></item><item><title>When Search Becomes a Product: OpenSearch and Elasticsearch Architecture</title><link>https://rajivonai.com/blog/2026-03-09-when-search-becomes-a-product-opensearch-elasticsearch/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-03-09-when-search-becomes-a-product-opensearch-elasticsearch/</guid><description>Why marketplace, travel, retail, local commerce, and support search often need a dedicated search platform instead of only pgvector.</description><pubDate>Mon, 20 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A user-facing search system is not just retrieval. It is filtering, ranking, freshness, analytics, explainability, and operational discipline. When search becomes a product feature, the architecture usually needs a search serving layer, not only embeddings inside the source database.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Consumer marketplaces, travel booking sites, retail catalogs, local commerce systems, support portals, and enterprise knowledge platforms all face the same shift. Users no longer search only with keywords. They describe intent:&lt;/p&gt;
&lt;p&gt;“Quiet hotel near the beach with breakfast.”&lt;/p&gt;
&lt;p&gt;“Durable carry-on under 200 for international trips.”&lt;/p&gt;
&lt;p&gt;“Dinner for four near me under 60 with vegetarian options.”&lt;/p&gt;
&lt;p&gt;“Policy for escalation after account lockout.”&lt;/p&gt;
&lt;p&gt;These queries are natural language, but the system still has to honor structured facts: price, location, availability, inventory, category, entitlement, policy version, delivery zone, and freshness.&lt;/p&gt;
&lt;p&gt;This is where a search platform becomes architecturally natural.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Postgres plus pgvector can support many internal RAG systems. It is less natural as the primary serving layer for product-grade search when the workload requires:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Facets and aggregations.&lt;/li&gt;
&lt;li&gt;Typo tolerance and analyzers.&lt;/li&gt;
&lt;li&gt;Synonyms and stemming.&lt;/li&gt;
&lt;li&gt;Field boosts.&lt;/li&gt;
&lt;li&gt;Geo filtering and distance ranking.&lt;/li&gt;
&lt;li&gt;High query volume.&lt;/li&gt;
&lt;li&gt;Near-real-time indexing.&lt;/li&gt;
&lt;li&gt;Search analytics.&lt;/li&gt;
&lt;li&gt;Relevance experiments.&lt;/li&gt;
&lt;li&gt;Multiple ranking pipelines.&lt;/li&gt;
&lt;li&gt;Operational search explainability.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Embeddings solve semantic similarity. They do not automatically solve search product requirements.&lt;/p&gt;
&lt;p&gt;For marketplace, travel, retail, local commerce, and support search, the better architecture is usually source-of-truth database plus derived search index.&lt;/p&gt;
&lt;h2 id=&quot;the-search-serving-architecture&quot;&gt;The Search Serving Architecture&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DB[Source database] --&gt; CDC[Change stream]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DB --&gt; EP[Embedding pipeline]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CDC --&gt; IDX[Search index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    EP --&gt; IDX&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Q[User query] --&gt; PARSE[Query parser]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PARSE --&gt; KW[Keyword query]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PARSE --&gt; SEM[Semantic query]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PARSE --&gt; FIL[Hard filters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    KW --&gt; IDX&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SEM --&gt; IDX&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    FIL --&gt; IDX&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    IDX --&gt; CAN[Hybrid candidates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CAN --&gt; RANK[Ranking pipeline]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    RANK --&gt; OUT[Results or explanation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The source database remains authoritative for transactions, ownership, and final truth. The search index is a derived serving system optimized for retrieval and ranking.&lt;/p&gt;
&lt;p&gt;That separation is important. The search index can be rebuilt. The source database cannot be treated as a cache. Prices, availability, order state, booking inventory, and policy truth should be verified against authoritative systems when the user takes action.&lt;/p&gt;
&lt;p&gt;OpenSearch and Elasticsearch fit this pattern because they start from search primitives: inverted indexes, BM25, analyzers, mappings, filters, aggregations, geo capabilities, relevance tooling, and now vector and hybrid retrieval.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented OpenSearch hybrid model combines keyword and semantic search through a pipeline that normalizes and combines scores. That is useful for search products because it keeps lexical and semantic evidence inside the search serving architecture.&lt;/p&gt;
&lt;p&gt;Elastic documents hybrid search as combining semantic retrieval and lexical retrieval so the ranked output can benefit from both meaning and text similarity. That reflects the same production reality: exact terms and semantic intent both matter.&lt;/p&gt;
&lt;p&gt;The documented pattern for search-heavy systems is therefore:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Keep the relational or transactional database as the source of truth.&lt;/li&gt;
&lt;li&gt;Stream source changes into the search index.&lt;/li&gt;
&lt;li&gt;Add embeddings as derived features.&lt;/li&gt;
&lt;li&gt;Search over lexical fields, vector fields, and structured filters.&lt;/li&gt;
&lt;li&gt;Use ranking and reranking as controlled release surfaces.&lt;/li&gt;
&lt;li&gt;Verify transactional truth before checkout, booking, ordering, or compliance-sensitive action.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For a travel system, the search index may know hotel descriptions, amenities, location, reviews, and semantic embeddings. The pricing and availability service remains authoritative for dates, rooms, taxes, fees, and final price.&lt;/p&gt;
&lt;p&gt;For a retail system, the search index may serve catalog discovery and semantic matching. Inventory, payment, and order state remain authoritative elsewhere.&lt;/p&gt;
&lt;p&gt;For a local commerce system, the index may retrieve vendors, menu items, delivery zones, and attributes. The order system still verifies availability, substitutions, fees, and delivery promise.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Risk&lt;/th&gt;&lt;th&gt;Production impact&lt;/th&gt;&lt;th&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Source and index drift&lt;/td&gt;&lt;td&gt;Search shows stale or unavailable items&lt;/td&gt;&lt;td&gt;Track ingestion lag and verify final truth&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Search index becomes source of truth&lt;/td&gt;&lt;td&gt;Recovery and compliance become unclear&lt;/td&gt;&lt;td&gt;Keep rebuild path from authoritative records&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hybrid score tuning is opaque&lt;/td&gt;&lt;td&gt;Relevance changes cannot be explained&lt;/td&gt;&lt;td&gt;Log candidate stages and ranking features&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Embeddings update silently&lt;/td&gt;&lt;td&gt;Results shift without review&lt;/td&gt;&lt;td&gt;Version embedding models and index aliases&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Facets ignore semantic candidates&lt;/td&gt;&lt;td&gt;Result counts and results disagree&lt;/td&gt;&lt;td&gt;Design filters and aggregations together&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Search cluster is undersized&lt;/td&gt;&lt;td&gt;Latency spikes under vector workload&lt;/td&gt;&lt;td&gt;Capacity plan for lexical and vector paths&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LLM writes final decisions&lt;/td&gt;&lt;td&gt;Price, availability, or policy truth is wrong&lt;/td&gt;&lt;td&gt;Use LLM for explanation, not authority&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Decide whether search is now a product surface rather than a database convenience.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Use a dedicated search serving layer when users need facets, geo, analyzers, typo tolerance, hybrid ranking, analytics, and high-volume relevance tuning.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Compare source-of-truth freshness, zero-result rate, no-click rate, lexical recall, semantic recall, filter correctness, and ranking quality across real query classes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Build the index as a derived system with CDC, replay, aliases, relevance test sets, rollback, and transactional verification before user actions.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The reason to use OpenSearch or Elasticsearch is not that vector search is impossible elsewhere. It is that product search has a broader contract than nearest-neighbor retrieval.&lt;/p&gt;
&lt;p&gt;When search becomes a product, the serving layer needs search-native operations.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;OpenSearch hybrid search documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/ai-search/hybrid-search/index/&quot;&gt;https://docs.opensearch.org/latest/vector-search/ai-search/hybrid-search/index/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch vector search documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/&quot;&gt;https://docs.opensearch.org/latest/vector-search/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Elasticsearch hybrid search documentation: &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/semantic-text-hybrid-search.html&quot;&gt;https://www.elastic.co/guide/en/elasticsearch/reference/current/semantic-text-hybrid-search.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Elasticsearch query DSL documentation: &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html&quot;&gt;https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>ai-engineering</category><category>checklist</category></item><item><title>When Postgres and pgvector Are Enough for Natural Language Search</title><link>https://rajivonai.com/blog/2026-03-02-when-postgres-pgvector-is-enough-for-natural-language-search/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-03-02-when-postgres-pgvector-is-enough-for-natural-language-search/</guid><description>A database-first decision guide for using PostgreSQL full-text search and pgvector before adding a search engine or vector database.</description><pubDate>Sun, 19 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Postgres plus pgvector is not the weak version of AI search. For the right workload, it is the most honest architecture: one source of truth, one authorization model, one backup model, and enough hybrid retrieval to prove whether the product deserves more infrastructure.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Many teams do not start with a search platform. They start with PostgreSQL, application tables, document metadata, tenant rules, and a small internal use case: support docs, runbooks, engineering design notes, onboarding content, product documentation, or policy Q&amp;#x26;A.&lt;/p&gt;
&lt;p&gt;The organization may not have OpenSearch, Elasticsearch, Qdrant, Weaviate, Pinecone, or Milvus approved. Procurement may be slow. Security review may be slower. The DBA team already knows how to back up, patch, monitor, and restore Postgres.&lt;/p&gt;
&lt;p&gt;That is a legitimate constraint. A good architecture should respect it.&lt;/p&gt;
&lt;p&gt;Postgres full-text search plus pgvector can deliver a useful first production version when the workload is controlled and the team is honest about the boundaries.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Teams often swing between two mistakes.&lt;/p&gt;
&lt;p&gt;The first mistake is overbuilding the POC. A small internal runbook assistant does not necessarily need a distributed search cluster, a managed vector service, a graph pipeline, and an agent framework. It needs accurate retrieval, tenant filtering, citations, observability, and a way to recover.&lt;/p&gt;
&lt;p&gt;The second mistake is pretending Postgres is a full search platform forever. Once users need high-volume faceting, typo tolerance, advanced analyzers, query analytics, relevance tooling, geo-heavy ranking, or multiple ranking pipelines, the system has become a search product.&lt;/p&gt;
&lt;p&gt;The database architect’s job is to know which side of that boundary the workload is on.&lt;/p&gt;
&lt;h2 id=&quot;the-postgres-first-hybrid-pattern&quot;&gt;The Postgres-First Hybrid Pattern&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[Documents and metadata] --&gt; C[Chunking]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; T[tsvector fields]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; E[Embedding fields]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    T --&gt; P[PostgreSQL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; P&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Q[User query] --&gt; A[Authorization filters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; L[Full text candidates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; V[pgvector candidates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt; F[Rank fusion]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    V --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; R[Optional rerank]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    R --&gt; G[Grounded answer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This architecture keeps business truth and retrieval data close together.&lt;/p&gt;
&lt;p&gt;PostgreSQL owns the source records, tenant IDs, lifecycle state, access controls, and metadata filters. Full-text search handles exact terms and phrases. pgvector handles semantic similarity. SQL applies filters. The application merges candidates and builds context for the LLM.&lt;/p&gt;
&lt;p&gt;For a first version, this is enough when:&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Condition&lt;/th&gt;&lt;th&gt;Why it helps&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Corpus is small to medium&lt;/td&gt;&lt;td&gt;Index and query costs stay manageable&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Data already lives in Postgres&lt;/td&gt;&lt;td&gt;No separate ingestion system is required&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Authorization is relational&lt;/td&gt;&lt;td&gt;SQL filters can enforce access&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Query volume is modest&lt;/td&gt;&lt;td&gt;Search traffic does not threaten OLTP&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Relevance needs are simple&lt;/td&gt;&lt;td&gt;Rank fusion and light reranking are acceptable&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Recovery simplicity matters&lt;/td&gt;&lt;td&gt;Existing backup and restore processes apply&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented Postgres behavior matters. Full-text search stores normalized document representations in &lt;code&gt;tsvector&lt;/code&gt;, accepts structured search through &lt;code&gt;tsquery&lt;/code&gt;, and provides ranking functions. That gives the team a lexical path for exact tokens, phrase-ish behavior, and weighted fields.&lt;/p&gt;
&lt;p&gt;The documented pgvector behavior matters too. pgvector adds vector types, distance operators, and approximate indexes such as HNSW and IVFFlat. Its README also discusses hybrid search with full-text search and rank fusion. That makes Postgres a credible first hybrid retrieval platform, not only a place to store embeddings.&lt;/p&gt;
&lt;p&gt;The production pattern is to keep retrieval inspectable:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Store chunk text, document ID, tenant ID, source version, lifecycle state, embedding model, embedding dimension, and content hash.&lt;/li&gt;
&lt;li&gt;Build full-text and vector indexes deliberately.&lt;/li&gt;
&lt;li&gt;Use the same tenant and lifecycle filters on both lexical and vector paths.&lt;/li&gt;
&lt;li&gt;Retrieve bounded candidate sets from each path.&lt;/li&gt;
&lt;li&gt;Fuse by rank or rerank a limited set.&lt;/li&gt;
&lt;li&gt;Log which path contributed each final result.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This works especially well for internal systems where the main value is answering from controlled documents: operational runbooks, database incident notes, support knowledge, engineering design docs, and policy repositories.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Pressure&lt;/th&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Graduation trigger&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Search traffic competes with OLTP&lt;/td&gt;&lt;td&gt;Application queries slow during retrieval spikes&lt;/td&gt;&lt;td&gt;Isolate retrieval or move serving search out&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Faceting becomes product-critical&lt;/td&gt;&lt;td&gt;Users need fast aggregations and filter counts&lt;/td&gt;&lt;td&gt;Use a search platform&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Typo tolerance and synonyms matter&lt;/td&gt;&lt;td&gt;Exact search feels brittle&lt;/td&gt;&lt;td&gt;Use search-engine analyzers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Geo ranking dominates&lt;/td&gt;&lt;td&gt;SQL filters are correct but ranking is weak&lt;/td&gt;&lt;td&gt;Use geo-aware search infrastructure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ANN plus filters under-return&lt;/td&gt;&lt;td&gt;Vector candidates vanish after selective filters&lt;/td&gt;&lt;td&gt;Tune, partition, or move workload&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Relevance debugging grows&lt;/td&gt;&lt;td&gt;DBA tools are not enough for product search&lt;/td&gt;&lt;td&gt;Add search analytics and explain tooling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reindexing becomes routine&lt;/td&gt;&lt;td&gt;Embedding changes disrupt database operations&lt;/td&gt;&lt;td&gt;Build a derived retrieval pipeline&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The most dangerous failure is letting Postgres become the accidental owner of OLTP, analytics, search relevance, vector retrieval, backfills, and LLM context assembly without an operating model change.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Identify whether the workload is still database-shaped: controlled corpus, SQL authorization, modest query volume, and simple relevance needs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Use PostgreSQL full-text search plus pgvector as the first hybrid retrieval architecture when simplicity, consistency, and approval speed matter.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run lexical-only, vector-only, and hybrid retrieval against exact-code queries, paraphrase queries, tenant-negative tests, and filtered queries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Define graduation signals before launch: query volume, latency, relevance backlog, faceting needs, typo tolerance, search analytics, and operational isolation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Postgres plus pgvector is a strong POC and a reasonable production design for many internal retrieval systems. It is not a substitute for a search platform when search itself becomes the product.&lt;/p&gt;
&lt;p&gt;The principal decision is not whether pgvector is “enough” in the abstract. It is enough when the workload fits the database operating model and the team can prove retrieval quality with evidence.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;PostgreSQL full-text search controls: &lt;a href=&quot;https://www.postgresql.org/docs/current/textsearch-controls.html&quot;&gt;https://www.postgresql.org/docs/current/textsearch-controls.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL full-text search indexes: &lt;a href=&quot;https://www.postgresql.org/docs/current/textsearch-indexes.html&quot;&gt;https://www.postgresql.org/docs/current/textsearch-indexes.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;pgvector README: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;pgvector hybrid search guidance: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>ai-engineering</category><category>checklist</category></item><item><title>Natural Language Search Architecture: A Database Architect&apos;s Decision Framework</title><link>https://rajivonai.com/blog/2026-02-02-natural-language-search-architecture-decision-framework/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-02-02-natural-language-search-architecture-decision-framework/</guid><description>A production decision framework for choosing lexical search, vector search, hybrid retrieval, RAG, agents, or GraphRAG by workload shape.</description><pubDate>Fri, 17 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The mistake is treating natural language search as a vector database selection problem. The harder production question is whether the workload needs exact matching, semantic discovery, hard filters, ranked results, tool execution, relationship reasoning, or all of them in one controlled retrieval pipeline.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Product teams are adding natural language interfaces to search surfaces that already had years of operational complexity: marketplace listings, travel inventory, retail catalogs, support content, internal knowledge bases, order systems, incident runbooks, and policy repositories.&lt;/p&gt;
&lt;p&gt;The request sounds simple: “Let users ask in natural language.” The architecture is not simple.&lt;/p&gt;
&lt;p&gt;A user might type “quiet family hotel near the beach with breakfast under 250.” Another might ask “policy for MRI prior authorization after appeal denial.” An engineer might ask “replica lag after failover in the payments database.” These queries mix intent, exact terms, filters, freshness, ranking, and sometimes task execution.&lt;/p&gt;
&lt;p&gt;The retrieval layer has to know which parts are soft meaning and which parts are hard constraints. Price, availability, tenant access, deletion state, eligibility, and booking inventory are not semantic preferences. They are source-of-truth facts.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Most failed AI search designs start with the wrong first question:&lt;/p&gt;
&lt;p&gt;“Which vector database should we use?”&lt;/p&gt;
&lt;p&gt;That skips the real architecture review. Vector similarity is one retrieval technique. It is not a full search product, a ranking system, an authorization model, a freshness model, or a workflow engine.&lt;/p&gt;
&lt;p&gt;Pure vector search is strong when wording differs but meaning is close. It is weak when the exact token matters: addresses, SKUs, drug names, legal clauses, policy IDs, error codes, hotel names, dates, prices, and acronyms.&lt;/p&gt;
&lt;p&gt;Pure keyword search has the opposite failure mode. BM25 and full-text search are strong for exact terms, rare words, and fielded relevance. They struggle when the user’s language and the document’s language differ.&lt;/p&gt;
&lt;p&gt;RAG adds another boundary. It retrieves context and asks an LLM to answer. That is useful for grounded Q&amp;#x26;A, but it does not make bad retrieval good. If the right document never enters the context window, the model is answering from incomplete evidence.&lt;/p&gt;
&lt;p&gt;Agents add planning and tools, but they increase cost, latency, and safety exposure. GraphRAG adds entity and relationship reasoning, but it adds ingestion cost, graph construction, summarization, and freshness complexity.&lt;/p&gt;
&lt;p&gt;The architecture question is not “Which tool is best?” It is:&lt;/p&gt;
&lt;p&gt;What retrieval problem is the system actually solving?&lt;/p&gt;
&lt;h2 id=&quot;the-retrieval-decision-stack&quot;&gt;The Retrieval Decision Stack&lt;/h2&gt;
&lt;p&gt;Treat natural language search as a stack of retrieval responsibilities:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Q[User request] --&gt; U[Query understanding]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    U --&gt; L[Lexical retrieval]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    U --&gt; V[Semantic retrieval]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    U --&gt; F[Hard filters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt; C[Candidate pool]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    V --&gt; C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; R[Ranking and fusion]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    R --&gt; RR[Reranking]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    RR --&gt; O{Needs action}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    O --&gt;|No| S[Search results]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    O --&gt;|Yes| A[Agent workflow]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; G{Needs relationships}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|Yes| KG[Graph retrieval]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The baseline architecture for serious search systems is usually hybrid retrieval:&lt;/p&gt;
&lt;p&gt;BM25 or full-text search for exact terms, vector retrieval for semantic intent, metadata filters for hard constraints, fusion for candidate combination, business ranking for product rules, reranking for final quality, and optional LLM generation only when an answer or explanation is useful.&lt;/p&gt;
&lt;p&gt;That stack separates concerns:&lt;/p&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Layer&lt;/th&gt;&lt;th&gt;Job&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Lexical search&lt;/td&gt;&lt;td&gt;Exact terms, identifiers, phrase relevance, field boosts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Vector search&lt;/td&gt;&lt;td&gt;Semantic similarity, paraphrases, discovery queries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Metadata filters&lt;/td&gt;&lt;td&gt;Tenant, access, price, location, inventory, lifecycle state&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Fusion&lt;/td&gt;&lt;td&gt;Combine candidates without comparing raw scores blindly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Business ranking&lt;/td&gt;&lt;td&gt;Freshness, quality, popularity, compliance, availability&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reranking&lt;/td&gt;&lt;td&gt;Reorder a bounded candidate set with richer features&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;RAG answer&lt;/td&gt;&lt;td&gt;Explain, summarize, compare, cite&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Agent workflow&lt;/td&gt;&lt;td&gt;Plan, call tools, retry, validate, take action&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Graph retrieval&lt;/td&gt;&lt;td&gt;Traverse entities, relationships, communities, ownership, events&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern across modern retrieval systems is convergence toward hybrid search rather than pure vector search.&lt;/p&gt;
&lt;p&gt;OpenSearch documents hybrid search as combining keyword and semantic search through a search pipeline that normalizes and combines scores. That is a search-engine view of the problem: lexical and semantic signals produce different score distributions, so production systems need an explicit combination stage.&lt;/p&gt;
&lt;p&gt;PostgreSQL and pgvector show the database-centered path. PostgreSQL full-text search provides &lt;code&gt;tsvector&lt;/code&gt;, &lt;code&gt;tsquery&lt;/code&gt;, and ranking functions. pgvector adds vector types and ANN indexes. The documented pattern is not “replace SQL with vectors.” It is “combine relational filters, full-text ranking, and vector similarity when the workload is still database-shaped.”&lt;/p&gt;
&lt;p&gt;Weaviate, Qdrant, and Pinecone show the retrieval-service path. Each supports hybrid retrieval, but with different operating models: object and BM25F fusion, sparse plus dense vectors, or managed dense and sparse records. The production decision is therefore not just feature support. It is which team can operate, tune, secure, and recover the system.&lt;/p&gt;
&lt;p&gt;Microsoft’s GraphRAG documentation describes graph extraction, community detection, community reports, local search, and global search over private corpora. That is a different architecture class. It is useful when relationships and corpus-level synthesis matter. It is not a better replacement for ordinary product or document search.&lt;/p&gt;
&lt;p&gt;The practical decision rule is:&lt;/p&gt;
&lt;p&gt;Use the cheapest architecture that preserves the user’s contract.&lt;/p&gt;
&lt;p&gt;For a small internal runbook assistant, PostgreSQL full-text search plus pgvector may be enough. For a consumer marketplace, a search serving layer with lexical relevance, vector fields, facets, geo filters, ranking, and analytics is usually more natural. For a workflow assistant, add agents after retrieval quality and tool boundaries are understood. For relationship-heavy intelligence, add graph retrieval when entity links are the product.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Bad framing&lt;/th&gt;&lt;th&gt;Production failure&lt;/th&gt;&lt;th&gt;Better question&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;”Use embeddings for search”&lt;/td&gt;&lt;td&gt;Exact names, IDs, prices, and filters fail&lt;/td&gt;&lt;td&gt;Which fields require exact matching?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Use RAG for answers”&lt;/td&gt;&lt;td&gt;LLM answers from incomplete context&lt;/td&gt;&lt;td&gt;Did retrieval find the right evidence?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Use agents for search”&lt;/td&gt;&lt;td&gt;Latency and cost rise without better recall&lt;/td&gt;&lt;td&gt;Is the user asking for an action?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Use GraphRAG because it is advanced”&lt;/td&gt;&lt;td&gt;Expensive graph pipeline serves simple lookup&lt;/td&gt;&lt;td&gt;Are relationships the core value?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Keep everything in one database”&lt;/td&gt;&lt;td&gt;OLTP, search, ranking, and AI traffic compete&lt;/td&gt;&lt;td&gt;Has search become a product feature?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Move to a new vector service”&lt;/td&gt;&lt;td&gt;Source-of-truth drift and rebuild gaps appear&lt;/td&gt;&lt;td&gt;Is the ingestion path replayable?&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Classify query classes before selecting infrastructure: exact lookup, semantic discovery, filtered search, comparison, task execution, or relationship analysis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Start with hybrid retrieval as the default production baseline, then add RAG, agents, or graph retrieval only when the workload requires them.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Build a query evaluation set with exact-token queries, natural-language queries, strict-filter queries, empty-result cases, and tenant-negative tests.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Write the architecture decision record around source of truth, hard filters, ranking, observability, rebuild path, degraded mode, and ownership.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The right decision framework is not vendor-first. It is workload-first.&lt;/p&gt;
&lt;p&gt;If the workload is database-shaped, stay close to the database. If search is a product, use a search platform. If retrieval is the service, evaluate vector-native systems. If the user wants an outcome, add agents. If relationships are the value, use graph retrieval.&lt;/p&gt;
&lt;p&gt;Natural language search is not one architecture. It is a set of retrieval contracts that need to be made explicit before the first embedding index is created.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;OpenSearch hybrid search documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/ai-search/hybrid-search/index/&quot;&gt;https://docs.opensearch.org/latest/vector-search/ai-search/hybrid-search/index/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL full-text search controls: &lt;a href=&quot;https://www.postgresql.org/docs/current/textsearch-controls.html&quot;&gt;https://www.postgresql.org/docs/current/textsearch-controls.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;pgvector README: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Weaviate hybrid search documentation: &lt;a href=&quot;https://docs.weaviate.io/weaviate/search/hybrid&quot;&gt;https://docs.weaviate.io/weaviate/search/hybrid&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Qdrant hybrid queries documentation: &lt;a href=&quot;https://qdrant.tech/documentation/concepts/hybrid-queries/&quot;&gt;https://qdrant.tech/documentation/concepts/hybrid-queries/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Pinecone hybrid search documentation: &lt;a href=&quot;https://docs.pinecone.io/guides/search/hybrid-search&quot;&gt;https://docs.pinecone.io/guides/search/hybrid-search&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Microsoft GraphRAG documentation: &lt;a href=&quot;https://microsoft.github.io/graphrag/&quot;&gt;https://microsoft.github.io/graphrag/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>ai-engineering</category><category>checklist</category></item><item><title>Weaviate Hybrid Search for DB Engineers: Schema, Objects, BM25, Vectors, and Filters</title><link>https://rajivonai.com/blog/2026-05-21-weaviate-hybrid-search-for-db-engineers/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-21-weaviate-hybrid-search-for-db-engineers/</guid><description>A database engineer&apos;s guide to Weaviate hybrid search, including collections, objects, BM25, vectors, filters, tenancy, schema design, and operational tradeoffs.</description><pubDate>Thu, 16 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A database engineer opens a Weaviate schema and looks for tables, indexes, and joins. Instead they find collections, objects, properties, vectorizers, inverted indexes, BM25, hybrid search, and tenants. The concepts are not alien, but the mapping is different enough to create production mistakes.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;Weaviate hybrid search combines keyword-style retrieval and vector retrieval over schema-defined objects. For DB engineers, the useful mental model is: collection as a table-like boundary, object as a row-like entity, property as a column-like field, inverted index as keyword access path, vector index as semantic access path, and filters as query predicates.&lt;/p&gt;
&lt;p&gt;That analogy is useful but incomplete. Weaviate is not a relational database. Schema design, object boundaries, tenancy, vectorization, and backup behavior need their own review.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;DB engineers know how to reason about tables: primary keys, indexed predicates, nullability, source of truth, tenant isolation, and backup restore. Vector databases and object retrieval systems use different vocabulary, but production still asks the same questions.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Weaviate adds hybrid retrieval and object schema on top of vector search. That makes it powerful, and it also makes casual schema design risky. Does the relational mental model transfer cleanly enough to design a safe Weaviate schema, or does it hide the places where Weaviate behaves differently?&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot;&gt;How It Works&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Collection[Weaviate collection] --&gt; Object[objects]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Object --&gt; Props[properties]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Object --&gt; Vector[vector index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Props --&gt; BM25[inverted index and BM25]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[user query] --&gt; Hybrid[hybrid search]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Hybrid --&gt; BM25&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Hybrid --&gt; Vector&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Hybrid --&gt; Filters[property filters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Filters --&gt; Results[ranked objects]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Hybrid search uses both keyword and vector signals. Weaviate exposes an &lt;code&gt;alpha&lt;/code&gt; parameter to control the balance between vector and keyword contribution, where &lt;code&gt;alpha = 1&lt;/code&gt; is pure vector search and &lt;code&gt;alpha = 0&lt;/code&gt; is pure keyword (BM25) search. Current Weaviate documentation lists the default as &lt;code&gt;0.75&lt;/code&gt; — weighted toward vector search — though the default has varied across client library versions, so confirm it against the specific client and Weaviate version in use rather than assuming it is unweighted.&lt;/p&gt;
&lt;p&gt;Filters restrict objects by properties. Multi-tenancy can isolate tenant data at the collection level depending on configuration. Named vectors can allow multiple embedding spaces per object.&lt;/p&gt;
&lt;h2 id=&quot;architecture-and-operating-model&quot;&gt;Architecture and Operating Model&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Collection design.&lt;/strong&gt; Choose collections around object lifecycle and query patterns. Do not mirror every relational table automatically. A normalized relational model may be a poor retrieval model.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Object design.&lt;/strong&gt; An object should represent something users retrieve: article, product, ticket, policy, code example, or incident. If a single object mixes unrelated retrieval surfaces, search quality becomes hard to explain.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Property design.&lt;/strong&gt; Properties drive filters, keyword search, and vectorization. Decide which fields are searchable, filterable, and vectorized. A field used for authorization should be filterable and mandatory.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Hybrid search.&lt;/strong&gt; Treat the BM25 and vector balance as relevance configuration. Product names, identifiers, and error codes often need keyword weight. Conceptual questions often need vector weight.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tenancy.&lt;/strong&gt; Decide whether tenants live as filters, collection boundaries, or Weaviate multi-tenancy. This is a security and operations decision, not only a query parameter.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Source of truth.&lt;/strong&gt; If Weaviate is derived from another database, keep deterministic IDs, source version, deletion propagation, and replay tooling. If it is a primary store, then backup, restore, migration, and audit requirements become stricter.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The database-engineering translation is helpful:&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Relational habit&lt;/th&gt;&lt;th&gt;Weaviate equivalent question&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Table design&lt;/td&gt;&lt;td&gt;What collection boundary matches retrieval and lifecycle?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Index design&lt;/td&gt;&lt;td&gt;Which properties and vectors are queried?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Query plan review&lt;/td&gt;&lt;td&gt;Which retrieval path handles each query class?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Row-level security&lt;/td&gt;&lt;td&gt;How are tenant and entitlement filters enforced?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Migration planning&lt;/td&gt;&lt;td&gt;How are schema and vector changes backfilled?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup testing&lt;/td&gt;&lt;td&gt;Are active tenants and collections restored correctly?&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The platform should prevent every team from inventing its own object schema. Shared conventions for collection names, property names, tenant fields, source IDs, model versions, and lifecycle fields will save incident time later.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Relational schema copied directly&lt;/td&gt;&lt;td&gt;Too many objects and weak retrieval&lt;/td&gt;&lt;td&gt;Model around retrieval objects&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Filters are optional&lt;/td&gt;&lt;td&gt;Cross-tenant or stale results appear&lt;/td&gt;&lt;td&gt;Enforce filters in a service layer&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hybrid balance not tested&lt;/td&gt;&lt;td&gt;Exact matches vanish or semantic matches dominate&lt;/td&gt;&lt;td&gt;Evaluate by query class&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schema evolves casually&lt;/td&gt;&lt;td&gt;Backfills and vector changes surprise operations&lt;/td&gt;&lt;td&gt;Treat schema changes as migrations&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup assumptions wrong&lt;/td&gt;&lt;td&gt;Some tenant data missing after restore&lt;/td&gt;&lt;td&gt;Test backup and restore behavior&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-cost-observability-and-failure-notes&quot;&gt;Security, Cost, Observability, and Failure Notes&lt;/h2&gt;
&lt;p&gt;Security should start with tenant isolation and entitlement fields. Weaviate’s documented multi-tenancy behavior is explicit and consequential for compliance: backups of a multi-tenant collection include only &lt;code&gt;ACTIVE&lt;/code&gt; tenants — &lt;code&gt;INACTIVE&lt;/code&gt; tenants (stored on local disk) and &lt;code&gt;OFFLOADED&lt;/code&gt; tenants (moved to cloud storage) are skipped because they have no local data available to snapshot at backup time. A team relying on backups for tenant data retention or legal-hold compliance must activate a tenant before backing it up, or the backup will silently exclude that tenant’s data. Verify this behavior against the deployed Weaviate version before writing a compliance or retention policy around it, since tenant-state handling has evolved across Weaviate releases.&lt;/p&gt;
&lt;p&gt;Cost is driven by object count, property indexes, vector dimensions, named vectors, replicas, modules, and backfills. Hybrid search may add compute by using both inverted and vector access paths.&lt;/p&gt;
&lt;p&gt;Observability should include query latency by retrieval type, hybrid settings, filter selectivity, empty-result rate, object ingestion lag, vectorization failures, tenant-level query volume, backup age, and restore test age.&lt;/p&gt;
&lt;p&gt;Failure modes include schema drift, stale vectors, missed deletes, tenant misrouting, unbounded object growth, and relevance regressions after changing hybrid weighting.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;What is the retrieval object?&lt;/li&gt;
&lt;li&gt;Which properties are searchable?&lt;/li&gt;
&lt;li&gt;Which properties are filterable?&lt;/li&gt;
&lt;li&gt;Which fields are vectorized?&lt;/li&gt;
&lt;li&gt;Does hybrid search improve the query set?&lt;/li&gt;
&lt;li&gt;How is tenant isolation enforced?&lt;/li&gt;
&lt;li&gt;Is Weaviate source of truth or derived index?&lt;/li&gt;
&lt;li&gt;Are IDs deterministic and replayable?&lt;/li&gt;
&lt;li&gt;How are schema changes migrated?&lt;/li&gt;
&lt;li&gt;Are backups restored and validated?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; DB engineers map Weaviate concepts onto relational habits (collection as table, object as row) and miss the places where the analogy breaks — tenant backup exclusions and hybrid-weight tuning chief among them.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Use the relational-habit translation table as a starting point, but verify Weaviate-specific behavior (tenant states, &lt;code&gt;alpha&lt;/code&gt; defaults, backup scope) against the deployed version rather than assuming relational semantics apply.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; A restore drill confirms which tenants (active, inactive, offloaded) actually come back after a backup, and the hybrid &lt;code&gt;alpha&lt;/code&gt; setting is documented per endpoint rather than left at a global default.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, confirm every tenant that needs backup coverage is in &lt;code&gt;ACTIVE&lt;/code&gt; state before the next scheduled backup, and document the &lt;code&gt;alpha&lt;/code&gt; value each search endpoint actually uses.&lt;/p&gt;
&lt;h2 id=&quot;operating-guardrails&quot;&gt;Operating Guardrails&lt;/h2&gt;
&lt;p&gt;Create a schema review template before the second team adopts Weaviate. The template should force decisions on collection purpose, object lifecycle, required properties, filterable properties, vectorized properties, tenant model, source ID, deletion behavior, and restore validation. Without that review, collections tend to multiply into inconsistent retrieval silos.&lt;/p&gt;
&lt;p&gt;Keep hybrid settings close to the query use case. A support-ticket search endpoint, a product catalog endpoint, and a policy-document endpoint should not inherit one global keyword-vector balance. Each endpoint needs a query set, expected behavior, owner, and rollback plan.&lt;/p&gt;
&lt;p&gt;Finally, make source ownership explicit. If Weaviate is a derived retrieval index, every object should point back to a source system and version. If it is primary storage, it deserves the same backup, migration, audit, and access-review discipline as any other production database.&lt;/p&gt;
&lt;p&gt;DB engineers should also ask how bad answers are debugged. The minimum useful trace records collection, tenant, filters, hybrid settings, target vector if any, object IDs returned, and scores. Without that, every relevance issue becomes anecdotal.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Weaviate collection configuration: &lt;a href=&quot;https://docs.weaviate.io/weaviate/config-refs/collections&quot;&gt;https://docs.weaviate.io/weaviate/config-refs/collections&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Weaviate hybrid search documentation: &lt;a href=&quot;https://docs.weaviate.io/weaviate/search/hybrid&quot;&gt;https://docs.weaviate.io/weaviate/search/hybrid&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Weaviate filters documentation: &lt;a href=&quot;https://docs.weaviate.io/weaviate/search/filters&quot;&gt;https://docs.weaviate.io/weaviate/search/filters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Weaviate multi-tenancy documentation: &lt;a href=&quot;https://docs.weaviate.io/weaviate/manage-collections/multi-tenancy&quot;&gt;https://docs.weaviate.io/weaviate/manage-collections/multi-tenancy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>ai-engineering</category><category>checklist</category></item><item><title>Spanner vs Aurora PostgreSQL Limitless: Two Approaches to Horizontally Scaling PostgreSQL Workloads</title><link>https://rajivonai.com/blog/2026-07-15-spanner-vs-aurora-postgresql-limitless/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-07-15-spanner-vs-aurora-postgresql-limitless/</guid><description>An architectural comparison of Google Cloud Spanner and Amazon Aurora PostgreSQL Limitless Database for horizontally scaling write-heavy PostgreSQL workloads.</description><pubDate>Wed, 15 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When high-throughput systems hit the physical write limits of the largest single-node PostgreSQL instances, adding more read replicas can no longer mask the primary node’s CPU saturation. At this threshold, architects realize they need a horizontally sharded database. However, implementing manual sharding at the application layer risks breaking strict ACID compliance. The industry has introduced managed solutions that promise limitless write scaling while maintaining the PostgreSQL interface: Google Cloud Spanner and Amazon Aurora PostgreSQL Limitless Database. While both abstract the pain of sharding, they take completely different architectural paths to achieve distributed consensus, resulting in vastly different operational limits.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Consider an enterprise financial platform managing millions of accounts.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Payment API&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     | (100k+ TPS, strict serializability)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Standard Single-Node PostgreSQL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;     |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;100% CPU / I/O Bottleneck&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The platform needs to transition to a horizontally scaled architecture without rewriting their ORM. It needs cross-shard transactional consistency—if User A (on Shard 1) sends money to User B (on Shard 2), the database must guarantee a two-phase commit (2PC) that prevents double-spending. It also requires high availability across multiple zones. The architecture must choose between Cloud Spanner (using the PostgreSQL interface) and Aurora PostgreSQL Limitless Database.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Traditional horizontal sharding is a nightmare for DBAs. If you use an extension like Citus or implement application-level sharding, you are responsible for schema deployment across shards, cross-shard query routing, and rebalancing data when a single shard becomes a “hotspot.”&lt;/p&gt;
&lt;p&gt;Managed solutions attempt to hide this complexity behind a single endpoint. However, the laws of physics and distributed systems (the CAP theorem) still apply. When a transaction spans multiple shards, the system must coordinate those shards over a network. This introduces clock skew and network latency. If an architect assumes a distributed transaction on Spanner or Aurora Limitless performs exactly like a local transaction on a single monolithic PostgreSQL node, their application will grind to a halt waiting for network consensus or suffering from &lt;code&gt;ABORTED&lt;/code&gt; lock errors.&lt;/p&gt;
&lt;p&gt;The core question is: How do Spanner and Aurora Limitless internally coordinate distributed transactions, and what does that mean for schema design and query latency?&lt;/p&gt;
&lt;h2 id=&quot;distributed-write-architecture-comparison&quot;&gt;Distributed Write Architecture Comparison&lt;/h2&gt;
&lt;p&gt;Both databases provide a single PostgreSQL-compatible connection endpoint, but their internal topologies differ significantly.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph Amazon Aurora Limitless&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A_App[Client] --&gt; A_Router[Router Node]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A_Router -- 2PC — AWS Time Sync --&gt; A_Shard1[Shard 1 — PostgreSQL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A_Router -- 2PC — AWS Time Sync --&gt; A_Shard2[Shard 2 — PostgreSQL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A_Shard1 --&gt; A_Storage[(Aurora Storage Fleet)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A_Shard2 --&gt; A_Storage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph Google Cloud Spanner&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G_App[Client] --&gt; G_API[Spanner API Coordinator]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G_API -- 2PC — TrueTime --&gt; G_Split1[Split 1 — Paxos Group]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G_API -- 2PC — TrueTime --&gt; G_Split2[Split 2 — Paxos Group]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G_Split1 --&gt; G_Colossus[(Colossus)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G_Split2 --&gt; G_Colossus&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;google-cloud-spanner-postgresql-interface&quot;&gt;Google Cloud Spanner (PostgreSQL Interface)&lt;/h3&gt;
&lt;p&gt;Spanner is a proprietary distributed database built from the ground up on Paxos consensus, with the PostgreSQL interface as a translation layer over it — see &lt;a href=&quot;https://rajivonai.com/blog/2026-08-26-cloud-spanner-postgresql-write-scalability-dba-guide/&quot;&gt;How Google Cloud Spanner Scales Writes&lt;/a&gt; for the full split/Paxos/TrueTime write path. The point that matters for this comparison: Spanner uses a Commit-Wait mechanism, where the transaction coordinator waits out TrueTime’s clock uncertainty window before acknowledging a cross-split commit.&lt;/p&gt;
&lt;h3 id=&quot;amazon-aurora-postgresql-limitless-database&quot;&gt;Amazon Aurora PostgreSQL Limitless Database&lt;/h3&gt;
&lt;p&gt;Aurora Limitless is built on fleets of actual PostgreSQL instances. It introduces a two-layer architecture: &lt;strong&gt;Routers&lt;/strong&gt; (which parse queries and manage distributed transactions) and &lt;strong&gt;Shards&lt;/strong&gt; (standard Aurora PostgreSQL nodes that hold partitioned data).
To solve the distributed transaction problem, Limitless relies on &lt;strong&gt;AWS Time Sync&lt;/strong&gt; and hybrid logical clocks. Similar to TrueTime, it assigns globally comparable timestamps to order cross-shard transactions. The exact overlap between Limitless’s commit-wait handling and Aurora’s quorum storage write path is an implementation detail worth confirming against current AWS documentation rather than assuming from Spanner’s design — the operationally relevant point is the same either way: cross-shard transactions pay a clock-uncertainty tax that local-shard transactions don’t.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented operational patterns for these databases require different DBA strategies.&lt;/p&gt;
&lt;h3 id=&quot;schema-design-and-shard-keys&quot;&gt;Schema Design and Shard Keys&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Aurora Limitless:&lt;/strong&gt; Requires you to explicitly define how tables are distributed, but not through a &lt;code&gt;SHARDED&lt;/code&gt; DDL keyword — table distribution mode is set via session variables before a standard &lt;code&gt;CREATE TABLE&lt;/code&gt;:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; rds_aurora&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;limitless_create_table_mode&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;sharded&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; rds_aurora&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;limitless_create_table_shard_key&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;{&quot;customer_id&quot;}&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (...);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
You can also create &lt;code&gt;REFERENCE&lt;/code&gt; tables (small tables copied to every shard for fast local joins) and collocated tables (sharded tables sharing a shard key so joins between them execute on a single shard, the direct Aurora-side analog to Spanner’s &lt;code&gt;INTERLEAVE IN PARENT&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Spanner:&lt;/strong&gt; Automatically manages splits. However, if your primary keys are sequential (e.g., &lt;code&gt;SERIAL&lt;/code&gt;), Spanner will push all writes to a single split, creating a fatal hotspot. You must use UUIDv4 or bit-reversed sequences. You use &lt;code&gt;INTERLEAVE IN PARENT&lt;/code&gt; to co-locate related tables (similar to a shard key).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;postgresql-compatibility&quot;&gt;PostgreSQL Compatibility&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Aurora Limitless:&lt;/strong&gt; Because the underlying shards run actual PostgreSQL, standard SQL and drivers work unmodified — but extension support is narrower than it sounds. Limitless supports a curated allowlist of roughly 15 extensions (including &lt;code&gt;pgvector&lt;/code&gt;, PostGIS with limitations, &lt;code&gt;pg_trgm&lt;/code&gt;, &lt;code&gt;pgcrypto&lt;/code&gt;, and a handful of others), meaningfully fewer than the 70+ extensions available on standard (non-Limitless) Aurora PostgreSQL, since the distributed architecture restricts what can run safely across shards.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Spanner:&lt;/strong&gt; The PostgreSQL interface is a protocol and syntax wrapper. It does not support many native PostgreSQL extensions (like PostGIS), triggers, or stored procedures.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;autoscaling&quot;&gt;Autoscaling&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Aurora Limitless:&lt;/strong&gt; The DB Shard Group operates in a serverless manner, automatically adjusting compute capacity across routers and shards based on workload demands.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Spanner:&lt;/strong&gt; Storage scales automatically. Compute (Processing Units) can also autoscale — Spanner has a GA managed autoscaler on Enterprise and Enterprise Plus editions — but Standard edition, or Enterprise/Enterprise Plus with autoscaling disabled, still requires manual provisioning. This isn’t the clean “Aurora is serverless, Spanner isn’t” split it might appear to be; both now offer automated compute scaling, gated by edition and configuration rather than architecture.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Distributed databases cannot bend the speed of light.&lt;/p&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure Mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detection&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation / Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Cross-Shard Join Latency&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High query latency&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;A &lt;code&gt;JOIN&lt;/code&gt; between tables on different shards/splits requires massive network data transfer. You must use Shard Keys (Aurora) or Interleaving (Spanner) to physically co-locate joined rows.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Spanner: Hotspots&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Key Visualizer shows red bands&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sequential primary keys bottleneck the entire cluster on one node. Must refactor schema to use highly entropic keys (UUIDs).&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Transaction Size Limits&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;Transaction too large&lt;/code&gt; errors&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Spanner hard-limits transactions to 80,000 mutations or 100MB. Massive batch deletes must be chunked.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Contention Aborts&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High &lt;code&gt;ABORTED&lt;/code&gt; error rates&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Spanner uses pessimistic two-phase locking with wound-wait. If multiple clients create a deadlock risk, younger transactions receive abort errors and must retry.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;When choosing between Spanner and Aurora Limitless for write scaling:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Single-node PostgreSQL compute cannot handle &gt;100,000 write transactions per second, forcing a move to distributed architectures.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Both Spanner and Aurora Limitless offer managed horizontal scaling, utilizing atomic clock synchronization (TrueTime / AWS Time Sync) to coordinate cross-shard transactions without application-layer complexity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Google and AWS documentation confirm that cross-shard distributed transactions require Commit-Wait mechanisms, making local shard co-location (via Shard Keys or Interleaving) mandatory for high performance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; If your application heavily relies on native PostgreSQL extensions, triggers, or stored procedures, migrate to Amazon Aurora Limitless, as it runs actual PostgreSQL under the hood — but verify your specific extensions against the current allowlist first. If your application logic is externalized and you require zero-RPO multi-region consensus across continents, migrate to Cloud Spanner and rewrite your sequential primary keys to UUIDs.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&quot;technical-references&quot;&gt;Technical References&lt;/h2&gt;
&lt;h3 id=&quot;google-cloud&quot;&gt;Google Cloud&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/spanner/docs/concurrency-control&quot;&gt;Concurrency Control in Cloud Spanner&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/spanner/docs/managed-autoscaler&quot;&gt;Managed Autoscaler for Cloud Spanner&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.cloud.google.com/spanner/docs/autoscaling-overview&quot;&gt;Cloud Spanner Autoscaling Overview&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;aws&quot;&gt;AWS&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless-architecture.html&quot;&gt;Amazon Aurora PostgreSQL Limitless Database Architecture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless-creating-config.html&quot;&gt;Creating Limitless Tables Using Session Variables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless-reference.DDL-limitations.html&quot;&gt;Aurora Limitless DDL Limitations, Including Supported Extensions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://aws.amazon.com/about-aws/whats-new/2024/10/amazon-aurora-postgresql-limitless-database-generally-available&quot;&gt;Amazon Aurora PostgreSQL Limitless Database Generally Available (Oct 2024)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;claims-requiring-additional-human-validation&quot;&gt;Claims Requiring Additional Human Validation&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;The exact current count and list of Aurora Limitless-supported extensions should be re-checked against AWS’s DDL limitations page before publication-sensitive claims are made, since this is an allowlist AWS can expand over time.&lt;/li&gt;
&lt;li&gt;Spanner’s managed autoscaler edition gating (Enterprise/Enterprise Plus only) should be re-verified if this post is updated significantly after August 2026, since edition feature gating is a common target for expansion.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>cloud</category><category>architecture</category><category>databases</category><category>failures</category><category>checklist</category></item><item><title>Qdrant Hybrid Search with Dense + Sparse Vectors: Why Fusion Strategy Matters</title><link>https://rajivonai.com/blog/2026-05-14-qdrant-hybrid-search-dense-sparse/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-14-qdrant-hybrid-search-dense-sparse/</guid><description>Why dense plus sparse retrieval in Qdrant needs careful fusion, score normalization, candidate sizing, and reranking to work in production RAG.</description><pubDate>Tue, 14 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The retrieval demo looks better after adding sparse vectors: exact error codes come back, semantic matches still work, and the answer quality improves. Then one production query fails because the sparse result and dense result disagree, and the fusion strategy quietly chooses the wrong document. Hybrid search is not just “run both.”&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;Dense vectors and sparse vectors solve different retrieval problems. Dense vectors capture semantic similarity. Sparse vectors preserve token-level precision. Qdrant’s hybrid query model can combine them, but the fusion strategy determines which candidates win.&lt;/p&gt;
&lt;p&gt;Use Reciprocal Rank Fusion (RRF) when ranks are more trustworthy than scores across systems. Use Distribution-Based Score Fusion (DBSF) — which normalizes each branch’s score distribution before summing — only when score distributions are understood and tested. Add reranking when the first-stage retrievers produce useful candidates but cannot reliably order them.&lt;/p&gt;
&lt;p&gt;Qdrant’s Query API exposes both RRF and DBSF as named fusion methods on the &lt;code&gt;fusion&lt;/code&gt; parameter of a query combining multiple &lt;code&gt;prefetch&lt;/code&gt; branches. RRF sidesteps score comparability entirely by fusing on rank position; DBSF keeps and normalizes raw scores. Confirm the fusion method names against the client library version in use, since Query API naming has evolved across Qdrant releases.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Dense embeddings are strong at paraphrase and weak at exact identifiers. Sparse retrieval is strong at exact terms and weak at semantic paraphrase.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Production RAG needs both:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;“ORA-01555” should retrieve exact Oracle troubleshooting docs.&lt;/li&gt;
&lt;li&gt;“query sees old data after commit” should retrieve read-after-write consistency docs.&lt;/li&gt;
&lt;li&gt;“S3 event duplicated order” should retrieve idempotency runbooks even if wording differs.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Dense and sparse scores usually live on different scales. A dense cosine score and a sparse term score are not directly comparable. Combining raw scores can make one retrieval mode dominate the other. Which fusion strategy prevents that without introducing a new failure mode of its own?&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot;&gt;How It Works&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[user query] --&gt; Dense[dense embedding search]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query --&gt; Sparse[sparse vector search]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Dense --&gt; DCandidates[dense candidates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Sparse --&gt; SCandidates[sparse candidates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DCandidates --&gt; Fusion[fusion strategy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SCandidates --&gt; Fusion&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Fusion --&gt; TopK[top candidates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    TopK --&gt; Rerank[cross encoder or rules]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Rerank --&gt; Context[RAG context]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Qdrant’s hybrid query flow uses prefetches to run subqueries, then applies a main query over the prefetched results. For dense plus sparse text retrieval, one prefetch targets the dense vector and another targets the sparse vector. Fusion combines the result sets.&lt;/p&gt;
&lt;p&gt;Rank-based fusion ignores score magnitudes and rewards documents that rank highly in one or more result sets. Score-aware fusion considers score distributions. Reranking then uses a more expensive model or deterministic rule set on a smaller candidate set.&lt;/p&gt;
&lt;h2 id=&quot;architecture-and-operating-model&quot;&gt;Architecture and Operating Model&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Candidate sizing.&lt;/strong&gt; Hybrid search fails when each branch retrieves too few candidates. If dense prefetch returns 10 and sparse prefetch returns 10, the fusion layer has little room to recover. Start with larger candidate pools and measure latency.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Fusion choice.&lt;/strong&gt; RRF is a practical default when dense and sparse scores are not comparable. It uses rank positions rather than raw score values. Distribution-based fusion can work when score distributions are stable, but production queries often vary wildly between exact-code queries and natural-language questions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reranking.&lt;/strong&gt; A reranker is useful when first-stage recall is good but order is weak. It is not a replacement for retrieval. If the right document never appears in the candidate set, reranking cannot recover it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Evaluation.&lt;/strong&gt; Keep separate query classes: exact identifiers, natural-language paraphrases, short ambiguous queries, long questions, multi-term product-like queries, and tenant-filtered queries. Hybrid settings should be evaluated by class, not only aggregate performance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Fallbacks.&lt;/strong&gt; For identifier-heavy queries, sparse results may deserve higher weight. For natural-language troubleshooting queries, dense may deserve more influence. A query classifier can choose route-specific settings, but every route needs observability.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;DBAs should recognize hybrid retrieval as query planning by another name. The engine is choosing access paths, merging candidate sets, and applying final ordering. The same discipline applies: inspect the plan, measure row counts, and validate results.&lt;/p&gt;
&lt;p&gt;Platform teams should keep fusion settings versioned. A change from rank fusion to score fusion is a relevance release. It should have a before-and-after query set, rollback, and owner.&lt;/p&gt;
&lt;p&gt;The retrieval service should log branch-level candidates, not only final answers. Without dense and sparse candidate logs, debugging a bad answer becomes guesswork.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Raw scores combined directly&lt;/td&gt;&lt;td&gt;One branch dominates all results&lt;/td&gt;&lt;td&gt;Use rank fusion or calibrated normalization&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Candidate pools too small&lt;/td&gt;&lt;td&gt;Relevant doc never reaches fusion&lt;/td&gt;&lt;td&gt;Increase prefetch limits&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sparse overpowers natural language&lt;/td&gt;&lt;td&gt;Exact token overlap beats semantic answer&lt;/td&gt;&lt;td&gt;Route or weight by query class&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Dense misses identifiers&lt;/td&gt;&lt;td&gt;Error codes and SKUs disappear&lt;/td&gt;&lt;td&gt;Add sparse retrieval and exact filters&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reranker added too late&lt;/td&gt;&lt;td&gt;Latency rises without recall gain&lt;/td&gt;&lt;td&gt;Validate first-stage recall before reranking&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-cost-observability-and-failure-notes&quot;&gt;Security, Cost, Observability, and Failure Notes&lt;/h2&gt;
&lt;p&gt;Security filters must apply consistently to both dense and sparse branches. A secure dense query and an unfiltered sparse query can leak data through fusion.&lt;/p&gt;
&lt;p&gt;Cost increases with multiple retrieval branches, larger candidate pools, and reranking. The cheapest hybrid setup is often not the best; the most expensive one may still fail if candidate generation is wrong.&lt;/p&gt;
&lt;p&gt;Observability should log dense candidate count, sparse candidate count, overlap, fusion method, final result count, reranker latency, empty results, and query class.&lt;/p&gt;
&lt;p&gt;Failure modes include score drift after model upgrades, sparse tokenizer changes, branch-specific filter bugs, and a reranker masking retrieval regressions until latency or quality collapses.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Which query classes need sparse retrieval?&lt;/li&gt;
&lt;li&gt;Which query classes need dense retrieval?&lt;/li&gt;
&lt;li&gt;Are dense and sparse scores comparable?&lt;/li&gt;
&lt;li&gt;Is rank fusion safer than score fusion?&lt;/li&gt;
&lt;li&gt;How large should each prefetch candidate set be?&lt;/li&gt;
&lt;li&gt;Are filters applied to every branch?&lt;/li&gt;
&lt;li&gt;Does reranking improve order after first-stage recall is confirmed?&lt;/li&gt;
&lt;li&gt;Are fusion settings versioned?&lt;/li&gt;
&lt;li&gt;Can bad results be debugged branch by branch?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Dense and sparse retrieval branches disagree in production because their scores live on different scales, and naive score combination lets one branch silently dominate.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Default to RRF (Reciprocal Rank Fusion) since it fuses on rank rather than raw score; move to DBSF only after score distributions are measured and stable. Treat fusion strategy, candidate sizing, and reranking as versioned production configuration, not demo code.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The query-class evaluation set (exact identifiers, natural-language paraphrases, tenant-filtered queries) shows stable recall after a fusion-method change, and query logs record which fusion method and parameters produced each result.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, confirm which fusion method (RRF or DBSF) is currently configured, log it per query, and build a review set of queries where dense and sparse results disagree.&lt;/p&gt;
&lt;h2 id=&quot;operating-guardrails&quot;&gt;Operating Guardrails&lt;/h2&gt;
&lt;p&gt;Version the full retrieval recipe: dense model, sparse model, vector names, prefetch limits, fusion method, fusion parameters, filters, reranker, and result limit. A change to any one of those can alter answers. Store the recipe version in query logs so incidents can be tied back to the exact retrieval configuration.&lt;/p&gt;
&lt;p&gt;Build evaluation around disagreement. Queries where dense and sparse agree are rarely the hard cases. Review queries where dense returns conceptual matches and sparse returns exact-token matches, then decide which behavior the product actually wants. That review set is where fusion strategy earns its keep.&lt;/p&gt;
&lt;p&gt;Keep a cheap fallback for exact identifiers. If a query contains a known ticket ID, SKU, database error code, or file path, a direct payload or text lookup may be more reliable than any fusion strategy. Hybrid search should not make exact lookup harder.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Qdrant hybrid queries documentation: &lt;a href=&quot;https://qdrant.tech/documentation/search/hybrid-queries/&quot;&gt;https://qdrant.tech/documentation/search/hybrid-queries/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Qdrant filtering documentation: &lt;a href=&quot;https://qdrant.tech/documentation/concepts/filtering/&quot;&gt;https://qdrant.tech/documentation/concepts/filtering/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Qdrant search relevance documentation: &lt;a href=&quot;https://qdrant.tech/documentation/search/search-relevance/&quot;&gt;https://qdrant.tech/documentation/search/search-relevance/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>ai-engineering</category><category>failures</category></item><item><title>Cassandra Driver Tuning: Token-Aware Routing, Connection Pooling, and Prepared Statements</title><link>https://rajivonai.com/blog/2026-07-14-cassandra-driver-tuning-token-aware-routing-pooling-prepared-statements/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-07-14-cassandra-driver-tuning-token-aware-routing-pooling-prepared-statements/</guid><description>The client-side tuning levers that determine Cassandra latency before a query ever reaches the coordinator: routing, connection pools, and statement preparation.</description><pubDate>Tue, 14 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Every Cassandra tuning guide focuses on the server: compaction, GC, disk. But a meaningful share of avoidable latency in production Cassandra deployments never touches the server at all — it’s decided by how the client driver routes requests, manages connections, and handles statement parsing before the query leaves the application process.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A Cassandra cluster is healthy by every server-side metric: &lt;code&gt;nodetool tpstats&lt;/code&gt; shows no pending tasks, GC pauses are stable, and compaction is keeping up. Yet application-observed p99 latency is significantly higher than what server-side query tracing shows for the same requests. The gap between “what the coordinator measured” and “what the client experienced” is happening entirely in the driver layer, and no amount of server tuning will close it.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A Cassandra client driver makes three decisions per query that most application teams never configure explicitly: which node to send the request to, how many TCP connections to maintain to get it there, and whether to re-parse the query string on every execution. Each of these has a wrong default state that looks fine in development (low query volume, single-node testing) and only shows its cost under production load and cluster size.&lt;/p&gt;
&lt;p&gt;If the driver isn’t routing token-aware, every query becomes a two-hop request: an arbitrary coordinator receives it, then forwards it to the actual replica that owns the data. If connection pools aren’t sized for the protocol version in use, the driver either under-utilizes available connections or wastes memory over-provisioning them. And if the application builds a new &lt;code&gt;SimpleStatement&lt;/code&gt; from a string on every call instead of a prepared statement, Cassandra re-parses and re-plans that query on the coordinator every single time.&lt;/p&gt;
&lt;h2 id=&quot;the-driver-request-path&quot;&gt;The Driver Request Path&lt;/h2&gt;
&lt;p&gt;A query’s actual latency is the sum of client-side routing and pooling decisions plus server-side execution — and the client-side portion is invisible to server metrics.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[Application Query] --&gt; Prep{Prepared Statement?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Prep --&gt;|No| Parse[Coordinator Re-Parses Query String]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Prep --&gt;|Yes| Cached[Cached Parse Plan and Metadata]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Parse --&gt; Route&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Cached --&gt; Route&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Route{Token-Aware Routing Enabled?} --&gt;|No| Random[Random Coordinator]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Random --&gt; Forward[Coordinator Forwards to Actual Replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Route --&gt;|Yes| Direct[Request Sent Directly to Owning Replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Forward --&gt; Pool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Direct --&gt; Pool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Pool{Connection Pool Sized Correctly?} --&gt;|Undersized| Queue[Requests Queue Behind In-Flight Limit]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Pool --&gt;|Correctly Sized| Send[Request Sent Immediately]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-token-aware-routing&quot;&gt;1. Token-Aware Routing&lt;/h3&gt;
&lt;p&gt;Modern Cassandra drivers default to a token-aware load balancing policy, but it only works if the driver’s token map is populated — which requires the driver to know the cluster’s partitioner and keyspace replication settings, and requires prepared statements for the driver to calculate the partition key automatically. A driver configured to skip metadata refresh, or an application using ad-hoc &lt;code&gt;SimpleStatement&lt;/code&gt;s instead of prepared statements, silently falls back to routing every request to an arbitrary coordinator, adding a network hop to every single query.&lt;/p&gt;
&lt;h3 id=&quot;2-connection-pooling&quot;&gt;2. Connection Pooling&lt;/h3&gt;
&lt;p&gt;Since protocol v3, Cassandra’s binary protocol multiplexes requests over a connection using stream IDs, supporting up to 32,768 simultaneous in-flight requests per connection. This collapsed the old “many connections per host” model down to typically one core connection per host for most workloads — more connections don’t help once a single connection’s in-flight capacity isn’t the bottleneck, and resizing pools dynamically under load is itself expensive. Tune the &lt;em&gt;max requests per connection&lt;/em&gt; ceiling for genuinely high-throughput nodes rather than reflexively adding connections.&lt;/p&gt;
&lt;h3 id=&quot;3-prepared-statements&quot;&gt;3. Prepared Statements&lt;/h3&gt;
&lt;p&gt;A prepared statement is parsed and planned once, server-side, and referenced by an ID on every subsequent execution — the driver also caches the response metadata client-side, skipping metadata parsing on every response. An application issuing the same query shape as a fresh string every time pays full parse-and-plan cost on the coordinator for every single call, with no caching benefit at all.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Diagnosing which of these is actually costing latency starts with checking whether the driver’s routing is doing its job:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Confirm the token map is populated — an empty map means&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// token-aware routing is silently falling back to random routing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (session.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getMetadata&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getTokenMap&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;isEmpty&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    log.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;warn&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Token map is empty — routing is not token-aware&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Prepare once, execute many times — never rebuild a &lt;code&gt;SimpleStatement&lt;/code&gt; from a string in a hot path:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Prepare once at startup or first use&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;PreparedStatement prepared &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; session.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;prepare&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;SELECT * FROM orders.by_customer WHERE customer_id = ?&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Execute cheaply on every call — no re-parse, no re-plan&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ResultSet rs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; session.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;execute&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(prepared.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;bind&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(customerId));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Tune the driver’s connection pool against actual observed in-flight request counts, not a guess:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# application.conf (DataStax Java driver 4.x)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;datastax-java-driver.advanced.connection {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  pool.local.size = 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  max-requests-per-connection = 1024&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Ad-hoc &lt;code&gt;SimpleStatement&lt;/code&gt;s in hot paths&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Every execution re-parses and re-plans on the coordinator, and token-aware routing silently falls back to random.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Prepare every recurring query once; treat an unprepared statement in a hot loop as a bug.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Oversized connection pools “for safety”&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;More connections than a workload’s in-flight request count needs waste memory on both driver and server sides without improving throughput.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Size pools against observed concurrent in-flight requests, not intuition.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Disabling driver metadata refresh to reduce chatter&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Saves a small amount of background traffic but breaks token-aware routing and topology awareness, silently degrading every request.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Only disable metadata refresh where routing correctness genuinely doesn’t matter — rare in production.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Client-driver misconfiguration — non-token-aware routing, mis-sized connection pools, or unprepared statements — adds latency that server-side metrics never surface, because it happens before the request reaches the coordinator.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Confirm token-aware routing is active by checking the driver’s token map, prepare every recurring query exactly once, and size connection pools against observed in-flight request volume rather than a fixed multiplier.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Protocol v3’s multiplexing model supports up to 32,768 in-flight requests per connection — most latency gains from “more connections” evaporate once that per-connection capacity isn’t actually the bottleneck.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Add a startup check that logs a warning if &lt;code&gt;session.getMetadata().getTokenMap()&lt;/code&gt; is empty, and grep the codebase for &lt;code&gt;SimpleStatement&lt;/code&gt; usage in any request-handling hot path.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>OpenSearch Hybrid Search for Product Catalogs: BM25, Vectors, Filters, and Rerankers</title><link>https://rajivonai.com/blog/2026-05-07-opensearch-hybrid-search-product-catalogs/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-07-opensearch-hybrid-search-product-catalogs/</guid><description>A production architecture for product catalog hybrid search with OpenSearch, combining BM25, vector retrieval, filters, shard design, reranking, and relevance debugging.</description><pubDate>Sun, 12 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A shopper searches for “waterproof hiking shell.” BM25 finds products with those exact words. Vector search finds rain jackets whose descriptions never say “shell.” The business wants both, but also needs inventory filters, brand rules, price ranges, facets, and explainable relevance. That is the product-catalog hybrid search problem.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;Product catalog search is rarely dense-vector-only. It needs lexical search for exact terms, vector search for semantic recall, metadata filters for business constraints, and reranking for final relevance.&lt;/p&gt;
&lt;p&gt;OpenSearch fits this workload when catalog search is already search-engine-shaped. The hard parts are score normalization, shard and mapping design, relevance debugging, and keeping the index synchronized with inventory and catalog systems.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Product catalogs contain mixed signals: exact identifiers (SKUs, model numbers, sizes, colors, brand names), natural language (titles, descriptions, reviews, support text), structured fields (price, category, inventory, tenant, region, eligibility), and business logic (margin, availability, promotions, compliance, personalization).&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Vector search improves semantic recall, but it does not replace exact matching or business filters. A semantic hit for an unavailable product is still a bad result. A perfect vector match from the wrong region may be a policy violation. Can the retrieval pipeline combine lexical precision, semantic recall, and hard business constraints without any one of them silently overriding the others?&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot;&gt;How It Works&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[shopper query] --&gt; Lex[BM25 title and description]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query --&gt; Vec[dense vector search]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Lex --&gt; Normalize[score normalization]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Vec --&gt; Normalize&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Normalize --&gt; Filter[category inventory price filters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Filter --&gt; Rerank[reranker and business rules]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Rerank --&gt; Results[product results and facets]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Results --&gt; Logs[query and click logs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Logs --&gt; Eval[relevance evaluation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The core pattern:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Run a lexical query over title, brand, attributes, and description.&lt;/li&gt;
&lt;li&gt;Run a vector query over one or more embedding fields.&lt;/li&gt;
&lt;li&gt;Normalize or fuse scores.&lt;/li&gt;
&lt;li&gt;Apply hard filters for tenant, inventory, category, region, and compliance.&lt;/li&gt;
&lt;li&gt;Rerank a bounded candidate set.&lt;/li&gt;
&lt;li&gt;Log query, result IDs, filters, scores, clicks, and conversions.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;OpenSearch’s &lt;code&gt;normalization-processor&lt;/code&gt; search pipeline handles step 3: it runs between the query and fetch phases, applies a normalization technique (commonly min-max) to bring BM25 and k-NN scores onto a comparable scale, then combines them with a weighted technique such as arithmetic mean. This is a documented, general-availability feature — the pipeline definition is created once and attached to the index, not authored per query.&lt;/p&gt;
&lt;h2 id=&quot;architecture-and-operating-model&quot;&gt;Architecture and Operating Model&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Index shape.&lt;/strong&gt; A catalog index should be intentionally denormalized. Each indexed product document should contain fields needed for search and filtering: title, normalized brand, category path, attributes, price bands, availability, tenant or marketplace, language, region, and embedding vectors.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Analyzer design.&lt;/strong&gt; Product search needs analyzers for stemming, synonyms, punctuation, case normalization, and possibly language-specific behavior. Do not apply the same analyzer to SKU fields and prose fields.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Vector fields.&lt;/strong&gt; A product can have separate embeddings for title, description, attributes, and reviews. Keep field purpose clear. If all text is collapsed into one embedding, debugging relevance gets harder.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Filters.&lt;/strong&gt; Some filters are hard constraints: tenant, region, availability, compliance, deleted state. Others are user refinements: price, category, brand, rating. Hard constraints should never be left to reranking.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reranking.&lt;/strong&gt; Rerankers should operate on a bounded result set, not the whole index. They can incorporate semantic relevance, product quality, availability, and business rules. Keep the reranker deterministic enough to debug.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Evaluation.&lt;/strong&gt; Maintain a query set: head queries, tail queries, exact SKU queries, vague semantic queries, misspellings, and zero-result queries. Review search changes against this set before deployment.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;A database engineer should view OpenSearch as a derived read model. The catalog database remains the source of truth for product state. The search index is rebuilt, refreshed, snapshotted, and monitored separately.&lt;/p&gt;
&lt;p&gt;The most important operational boundary is ingestion. Product attributes, inventory updates, price changes, and embedding updates move at different speeds. The platform should make freshness explicit: product metadata may update in seconds, embeddings may update in minutes, and offline relevance features may update in batches.&lt;/p&gt;
&lt;p&gt;Do not let the vector index hide stale inventory. Availability should be a structured filter tied to a reliable feed.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Score scales do not match&lt;/td&gt;&lt;td&gt;Vector results dominate exact matches or disappear&lt;/td&gt;&lt;td&gt;Use normalization and query-set evaluation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Filters applied too late&lt;/td&gt;&lt;td&gt;Unavailable or unauthorized products appear&lt;/td&gt;&lt;td&gt;Apply hard filters before final response&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Shards too small or too many&lt;/td&gt;&lt;td&gt;Cluster overhead and unstable latency&lt;/td&gt;&lt;td&gt;Review shard sizing and lifecycle policy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Embeddings stale&lt;/td&gt;&lt;td&gt;Product meaning changes but vector does not&lt;/td&gt;&lt;td&gt;Version embeddings and monitor lag&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reranker opaque&lt;/td&gt;&lt;td&gt;Search team cannot explain result order&lt;/td&gt;&lt;td&gt;Log features, scores, and candidate stages&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-cost-observability-and-failure-notes&quot;&gt;Security, Cost, Observability, and Failure Notes&lt;/h2&gt;
&lt;p&gt;Security is mainly field-level and tenant-level discipline. Do not index restricted products into a shared searchable surface without query-time enforcement. If products have market or contractual restrictions, those restrictions belong in filters.&lt;/p&gt;
&lt;p&gt;Cost comes from denormalized document size, vector fields, replicas, shard count, query fanout, and reranking compute. Hybrid search can be more expensive than either BM25 or vector search alone because it runs both.&lt;/p&gt;
&lt;p&gt;Observability should include indexing lag, query latency by stage, zero-result rate, no-click rate, filter selectivity, top slow queries, shard hot spots, refresh lag, and relevance metrics from the query set.&lt;/p&gt;
&lt;p&gt;Failure modes include stale inventory, synonym regressions, analyzer mistakes, vector model upgrades without backfills, and business rules that bury relevant products without explanation.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Which fields require exact matching?&lt;/li&gt;
&lt;li&gt;Which fields should be embedded?&lt;/li&gt;
&lt;li&gt;Which filters are hard constraints?&lt;/li&gt;
&lt;li&gt;How fresh must inventory be?&lt;/li&gt;
&lt;li&gt;How are lexical and vector scores combined?&lt;/li&gt;
&lt;li&gt;What query set protects relevance during changes?&lt;/li&gt;
&lt;li&gt;How are shards sized and rolled over?&lt;/li&gt;
&lt;li&gt;Can the index be rebuilt from source systems?&lt;/li&gt;
&lt;li&gt;Are query logs sufficient for relevance debugging?&lt;/li&gt;
&lt;li&gt;What is the rollback plan for a bad relevance release?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Product catalog search treats hybrid retrieval as “run BM25 and vectors and combine them,” without accounting for score-scale mismatch or hard business constraints that must never be left to reranking.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Use OpenSearch’s &lt;code&gt;normalization-processor&lt;/code&gt; pipeline to put BM25 and vector scores on a comparable scale, apply tenant/region/availability/compliance as hard filters before reranking, and rerank only a bounded candidate set.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The protected query set (including exact SKU lookups, discontinued-item lookups, and restricted-region lookups) passes after every relevance change, and zero-result rate is tracked separately from low-click rate.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, confirm a &lt;code&gt;normalization-processor&lt;/code&gt; pipeline is attached to the catalog index, and build the protected query set if it does not already exist.&lt;/p&gt;
&lt;h2 id=&quot;operating-guardrails&quot;&gt;Operating Guardrails&lt;/h2&gt;
&lt;p&gt;Treat every catalog relevance change as a release. Analyzer changes, synonym updates, vector model changes, boost changes, and reranker changes should run against the same protected query set before deployment. Include boring queries in that set: exact SKU lookup, discontinued item lookup, restricted-region lookup, and empty inventory lookup.&lt;/p&gt;
&lt;p&gt;Keep the catalog index rebuildable. Product data, inventory state, pricing rules, and embeddings may come from different systems, but the index should have a deterministic replay path. If a bad mapping or vector model ships, the team should be able to create a replacement index, replay source data, compare relevance, and switch aliases without hand-editing production documents.&lt;/p&gt;
&lt;p&gt;Watch zero-result queries separately from low-click queries. Zero results often indicate analyzer, filter, or inventory-feed problems. Low clicks usually indicate ranking or merchandising problems. Blending them into one “search quality” metric hides the fix path.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;OpenSearch vector search documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/&quot;&gt;https://docs.opensearch.org/latest/vector-search/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch hybrid search documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/ai-search/hybrid-search/&quot;&gt;https://docs.opensearch.org/latest/vector-search/ai-search/hybrid-search/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch query DSL documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/query-dsl/&quot;&gt;https://docs.opensearch.org/latest/query-dsl/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>ai-engineering</category><category>checklist</category></item><item><title>OpenSearch as a Vector Database: When Search Infrastructure Beats a Dedicated Vector DB</title><link>https://rajivonai.com/blog/2026-05-04-opensearch-as-a-vector-database/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-04-opensearch-as-a-vector-database/</guid><description>When OpenSearch is the right vector-search platform because keyword search, hybrid retrieval, relevance debugging, and search operations already matter.</description><pubDate>Sat, 11 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A product team asks for vector search, but the real requirement is not pure semantic nearest-neighbor lookup. They need exact SKU matching, typo tolerance, category filters, facet counts, boosted brands, availability rules, and semantic fallback. That is not just a vector database problem. It is a search platform problem.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;OpenSearch is a good vector database when the workload is already search-shaped: BM25 relevance, analyzers, filters, facets, dashboards, logs, product search, and relevance debugging. It is especially strong when vector retrieval is one signal among several.&lt;/p&gt;
&lt;p&gt;OpenSearch is the wrong fit when the application needs a small, simple vector service, strict relational consistency, or a retrieval workload with little lexical search value. In those cases, pgvector or a dedicated vector store may be easier to operate.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Many teams evaluate vector databases as if semantic similarity replaced search. It usually does not.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Users still type part numbers, error codes, customer names, version strings, acronyms, and exact phrases. Dense embeddings are often weak at those cases. BM25 and field-aware lexical search remain valuable because they reward exact tokens and term frequency in predictable ways.&lt;/p&gt;
&lt;p&gt;OpenSearch becomes attractive when the organization already has:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Index lifecycle management.&lt;/li&gt;
&lt;li&gt;Search relevance tests.&lt;/li&gt;
&lt;li&gt;Analyzer and mapping expertise.&lt;/li&gt;
&lt;li&gt;Snapshot and restore workflows.&lt;/li&gt;
&lt;li&gt;Query profiling and relevance explanation habits.&lt;/li&gt;
&lt;li&gt;Product or log search infrastructure.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Adding vectors to that existing search model may be simpler than adding a separate vector store and then rebuilding search features around it. Does the retrieval workload actually need lexical search, facets, and relevance debugging, or would a smaller, purpose-built vector store be less operational surface for the same result?&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot;&gt;How It Works&lt;/h2&gt;
&lt;p&gt;OpenSearch can store vector fields alongside text, keyword, numeric, and structured fields. A query can combine lexical matching, vector similarity, filters, and ranking pipelines.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Source[source records] --&gt; Ingest[index pipeline]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Ingest --&gt; Index[OpenSearch index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[user query] --&gt; Lex[BM25 lexical query]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query --&gt; Vec[vector query]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Lex --&gt; Hybrid[hybrid search pipeline]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Vec --&gt; Hybrid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Hybrid --&gt; Filters[metadata filters and boosts]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Filters --&gt; Results[ranked results]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The production benefit is not only vector search. It is the ability to keep retrieval signals in one search index:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;title&lt;/code&gt; and &lt;code&gt;body&lt;/code&gt; text fields for BM25.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;tenant_id&lt;/code&gt;, &lt;code&gt;category&lt;/code&gt;, and &lt;code&gt;availability&lt;/code&gt; fields for filters.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;embedding&lt;/code&gt; fields for semantic similarity.&lt;/li&gt;
&lt;li&gt;Pipeline settings for normalization and score combination.&lt;/li&gt;
&lt;li&gt;Query profiling and relevance debugging tools.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;architecture-and-operating-model&quot;&gt;Architecture and Operating Model&lt;/h2&gt;
&lt;p&gt;OpenSearch should be selected when the operating model already fits search.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Index design.&lt;/strong&gt; Define mappings intentionally. Text fields need analyzers. Keyword fields need exact matching. Vector fields need dimension and method choices. Nested objects and denormalized fields should be reviewed because mapping mistakes are expensive to unwind.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ingestion.&lt;/strong&gt; Treat OpenSearch as a derived index unless it is intentionally the system of record. Source changes must produce idempotent index updates. Deletes must remove or tombstone vector-bearing documents.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Hybrid retrieval.&lt;/strong&gt; Combine lexical and vector result sets through documented query and pipeline mechanisms. Do not assume raw BM25 scores and vector scores are directly comparable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Relevance operations.&lt;/strong&gt; Keep a query set. Record expected results. Use explain tooling and search quality monitoring where available. Relevance changes should be reviewed like query plan changes in a database.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Recovery.&lt;/strong&gt; Snapshots restore the search cluster, but platform teams should also maintain a replay path from source systems. A search index that cannot be rebuilt is a hidden source of truth.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;DBAs coming from relational systems should recognize OpenSearch as a distributed indexing system, not a relational database with a vector column. It has shards, replicas, mappings, refresh behavior, segment merges, heap pressure, disk watermarks, and query fanout.&lt;/p&gt;
&lt;p&gt;That complexity is justified when the workload benefits from it. It is wasteful when the application only needs “top ten similar chunks for tenant X.”&lt;/p&gt;
&lt;p&gt;Platform teams should ask: do we already have OpenSearch operational maturity? If yes, vector search may fit naturally. If no, adopting OpenSearch solely for vectors means accepting a search platform just to avoid operating a vector service.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;Why OpenSearch struggles&lt;/th&gt;&lt;th&gt;Alternative&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Strong relational consistency&lt;/td&gt;&lt;td&gt;Search refresh and derived-index semantics complicate read-after-write&lt;/td&gt;&lt;td&gt;pgvector&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Simple internal RAG&lt;/td&gt;&lt;td&gt;Search cluster overhead dominates value&lt;/td&gt;&lt;td&gt;pgvector or Qdrant&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Heavy OLTP joins&lt;/td&gt;&lt;td&gt;Denormalization becomes brittle&lt;/td&gt;&lt;td&gt;PostgreSQL plus pgvector&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Pure vector service&lt;/td&gt;&lt;td&gt;BM25, analyzers, and shards add unused complexity&lt;/td&gt;&lt;td&gt;Qdrant&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Weak search operations&lt;/td&gt;&lt;td&gt;Team cannot debug mappings, shards, and relevance&lt;/td&gt;&lt;td&gt;Start simpler&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-cost-observability-and-failure-notes&quot;&gt;Security, Cost, Observability, and Failure Notes&lt;/h2&gt;
&lt;p&gt;Security depends on filter discipline. Tenant and entitlement fields must be present in the index and used in every query. If authorization depends on relational joins outside OpenSearch, the ingestion pipeline must denormalize those permissions safely or query-time enforcement must happen before retrieval.&lt;/p&gt;
&lt;p&gt;Cost is driven by indexed data volume, shard count, replicas, storage, heap, ingestion rate, and query fanout. Vector fields can increase memory and storage pressure. Hybrid retrieval can increase query CPU because it runs multiple retrieval paths.&lt;/p&gt;
&lt;p&gt;Observability should cover search latency, vector query latency, indexing lag, refresh behavior, rejected requests, heap pressure, segment merges, disk watermarks, shard imbalance, snapshot age, and relevance regression metrics.&lt;/p&gt;
&lt;p&gt;Failure modes include stale indexes, mapping mistakes, over-sharding, under-sized clusters, expensive wildcard or aggregation queries, and score-combination changes that silently reorder important results.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Is lexical relevance as important as semantic similarity?&lt;/li&gt;
&lt;li&gt;Does the team already operate OpenSearch well?&lt;/li&gt;
&lt;li&gt;Are analyzers, field boosts, facets, and filters part of the requirement?&lt;/li&gt;
&lt;li&gt;Is OpenSearch already the product-search or log-search platform?&lt;/li&gt;
&lt;li&gt;Can source data be replayed into the index?&lt;/li&gt;
&lt;li&gt;Are authorization fields safely denormalized?&lt;/li&gt;
&lt;li&gt;Is there a relevance test set?&lt;/li&gt;
&lt;li&gt;Is search latency budget compatible with hybrid retrieval?&lt;/li&gt;
&lt;li&gt;Does the cost model account for shards, replicas, storage, and vector fields?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Teams reach for OpenSearch as a vector database because the vectors need to go somewhere, without checking whether the workload actually needs BM25, facets, and relevance debugging.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Use OpenSearch when retrieval is search, not just vectors — BM25, filters, facets, relevance tooling, and operational search maturity already matter. Avoid it when the workload only needs a small vector lookup service or when consistency belongs inside Postgres.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The team can name which production queries rely on lexical matching versus vector similarity, and a relevance query set exists to catch regressions before they ship.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, list the query types the retrieval endpoint actually serves (exact SKU lookup, semantic paraphrase, filtered facet browse) and check whether OpenSearch’s operational maturity already exists on the team before committing to it as the vector store.&lt;/p&gt;
&lt;h2 id=&quot;operating-guardrails&quot;&gt;Operating Guardrails&lt;/h2&gt;
&lt;p&gt;Do not let OpenSearch become the unreviewed dumping ground for every field that might someday be searched. Each indexed field should have a query purpose, retention policy, mapping owner, and freshness expectation. Vector fields need the same discipline as text fields: model version, dimension, source property, and rebuild plan.&lt;/p&gt;
&lt;p&gt;For production rollout, separate hard constraints from ranking signals. Tenant, region, deletion state, compliance status, and entitlement are constraints. They should not be expressed as soft boosts. Relevance tuning can move products up and down; it must not decide whether a user is allowed to see the result.&lt;/p&gt;
&lt;p&gt;Also define an ownership rule for mappings. A vector field added by an AI team can change heap, disk, snapshot, and query behavior for the search team. That change deserves the same review as a new high-cardinality aggregation field.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;OpenSearch vector search documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/&quot;&gt;https://docs.opensearch.org/latest/vector-search/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch hybrid search documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/ai-search/hybrid-search/&quot;&gt;https://docs.opensearch.org/latest/vector-search/ai-search/hybrid-search/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch index management documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/im-plugin/&quot;&gt;https://docs.opensearch.org/latest/im-plugin/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>cloud</category><category>ai-engineering</category></item><item><title>pgvector HNSW vs IVFFlat: The Index Choice That Changes Everything</title><link>https://rajivonai.com/blog/2026-04-30-pgvector-hnsw-vs-ivfflat/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-04-30-pgvector-hnsw-vs-ivfflat/</guid><description>A practical DBA guide to pgvector HNSW and IVFFlat tradeoffs across build time, memory, recall, writes, maintenance, and query tuning.</description><pubDate>Fri, 10 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The schema migration looked harmless: add a vector index so RAG queries stop scanning the table. The next problem arrived during the backfill, when index build time, memory pressure, and write amplification became the real production risk. In pgvector, choosing HNSW or IVFFlat is not an academic choice. It changes the operating model.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;HNSW is usually the better query-time index when recall and latency matter, but it builds slower and uses more memory. IVFFlat is lighter to build and operate, but it requires training on existing data, depends heavily on list and probe tuning, and can have weaker recall for the same latency target.&lt;/p&gt;
&lt;p&gt;For many new pgvector deployments, start by testing HNSW unless the table is write-heavy, memory-constrained, or still growing through large bulk loads. Use IVFFlat when build speed, lower memory use, and simpler initial backfills matter more than best speed-recall tradeoff.&lt;/p&gt;
&lt;p&gt;This guidance targets pgvector 0.7 and later, where both index types are generally available with the tuning knobs described below (&lt;code&gt;hnsw.ef_search&lt;/code&gt;, defaulting to 40; &lt;code&gt;ivfflat.probes&lt;/code&gt;). Confirm the deployed extension version before applying specific parameter defaults, since managed Postgres providers occasionally pin an older release.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Postgres engineers are used to indexes with predictable semantics: a B-tree index changes performance, not result correctness.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Approximate nearest-neighbor indexes are different from a B-tree. Adding one can change which rows are returned because the index trades recall for speed.&lt;/p&gt;
&lt;p&gt;That shift creates three DBA problems:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The index is part of relevance behavior, not only performance.&lt;/li&gt;
&lt;li&gt;Tuning parameters affect correctness as users experience it.&lt;/li&gt;
&lt;li&gt;Build and maintenance costs can disrupt the source database.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;HNSW and IVFFlat both accelerate vector search, but they fail differently. Which one is the right default for a given table depends on whether memory, build time, or recall is the binding constraint.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot;&gt;How It Works&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Table[embedding table] --&gt; HNSW[HNSW graph index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Table --&gt; IVF[IVFFlat list index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    HNSW --&gt; HQuery[graph traversal at query time]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    IVF --&gt; IQuery[probe nearest lists at query time]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    HQuery --&gt; Recall[recall latency tradeoff]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    IQuery --&gt; Recall&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Recall --&gt; DBA[DBA tuning and validation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;HNSW&lt;/strong&gt; builds a navigable graph of vectors. Queries traverse the graph to find nearby points. The key tuning concepts are graph construction quality and search candidate breadth. Higher construction and search settings can improve recall, but increase build time, memory use, insert cost, and query CPU.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;IVFFlat&lt;/strong&gt; clusters vectors into lists. Queries identify nearby lists and search only some of them. The key tuning concepts are number of lists and number of probes. More lists can improve partitioning of the vector space; more probes improve recall but increase query cost.&lt;/p&gt;
&lt;p&gt;The difference matters during ingestion. HNSW can be created before data exists because it does not require a training step. IVFFlat should be created after representative data exists because the lists depend on the data distribution.&lt;/p&gt;
&lt;h2 id=&quot;architecture-and-operating-model&quot;&gt;Architecture and Operating Model&lt;/h2&gt;
&lt;p&gt;A production pgvector table should treat index choice as a rollout.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Baseline exact search.&lt;/strong&gt; Before adding an approximate index, run exact search on a representative subset. Save a query set and expected top results. This becomes the recall baseline.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Build the index in staging with production-shaped data.&lt;/strong&gt; Measure build time, peak memory, WAL volume, replica lag, and query latency. Do not extrapolate from 10,000 rows if production has millions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tune for query classes.&lt;/strong&gt; Admin search, tenant-scoped RAG, and product recommendation may need different recall and latency tradeoffs. Use session-local settings where possible.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Measure recall, not only latency.&lt;/strong&gt; A query that returns fast but misses the relevant incident runbook is not healthy. Keep a labeled or manually reviewed query set.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Plan rebuilds.&lt;/strong&gt; Embedding model changes can require a new vector column, a new index, or a full backfill. Build the migration path before the model upgrade.&lt;/p&gt;
&lt;h2 id=&quot;practical-comparison&quot;&gt;Practical Comparison&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Area&lt;/th&gt;&lt;th&gt;HNSW&lt;/th&gt;&lt;th&gt;IVFFlat&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Build model&lt;/td&gt;&lt;td&gt;Graph construction&lt;/td&gt;&lt;td&gt;List clustering&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Build timing&lt;/td&gt;&lt;td&gt;Can build before table has data&lt;/td&gt;&lt;td&gt;Best after representative data exists&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Query behavior&lt;/td&gt;&lt;td&gt;Strong speed-recall tradeoff&lt;/td&gt;&lt;td&gt;Depends heavily on lists and probes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Memory pressure&lt;/td&gt;&lt;td&gt;Higher&lt;/td&gt;&lt;td&gt;Lower&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Write behavior&lt;/td&gt;&lt;td&gt;More index maintenance cost&lt;/td&gt;&lt;td&gt;Usually lighter&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tuning knob&lt;/td&gt;&lt;td&gt;&lt;code&gt;hnsw.ef_search&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;ivfflat.probes&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Operational risk&lt;/td&gt;&lt;td&gt;Memory and build time&lt;/td&gt;&lt;td&gt;Poor recall from bad list or probe settings&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Good fit&lt;/td&gt;&lt;td&gt;Read-heavy retrieval&lt;/td&gt;&lt;td&gt;Bulk-loaded or memory-constrained datasets&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;DBAs should treat vector index creation like a heavy production change. It can compete for memory, CPU, WAL, and I/O. It can also surprise application owners because retrieval results change after the index is added.&lt;/p&gt;
&lt;p&gt;For HNSW, watch build memory and insert latency. The pgvector README notes that HNSW builds faster when the graph fits in &lt;code&gt;maintenance_work_mem&lt;/code&gt;; setting that too high can exhaust server memory. That is a DBA decision, not an application toggle.&lt;/p&gt;
&lt;p&gt;For IVFFlat, watch whether the data distribution used to create lists still represents the table after months of growth. If the corpus shifts, recall can degrade. Reindexing may become a scheduled maintenance task.&lt;/p&gt;
&lt;p&gt;Platform teams should expose index choice through a migration template, not one-off SQL copied from examples. The template should include index name, operator class, build window, rollback plan, and validation query set.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Index&lt;/th&gt;&lt;th&gt;Production symptom&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Graph build exceeds memory budget&lt;/td&gt;&lt;td&gt;HNSW&lt;/td&gt;&lt;td&gt;Slow build or host pressure&lt;/td&gt;&lt;td&gt;Lower settings, increase maintenance memory safely, build off-hours&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Inserts become expensive&lt;/td&gt;&lt;td&gt;HNSW&lt;/td&gt;&lt;td&gt;Write latency rises during ingestion&lt;/td&gt;&lt;td&gt;Batch writes, separate ingestion window, consider IVFFlat&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lists poorly represent data&lt;/td&gt;&lt;td&gt;IVFFlat&lt;/td&gt;&lt;td&gt;Recall is inconsistent&lt;/td&gt;&lt;td&gt;Rebuild after representative data exists&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Probes too low&lt;/td&gt;&lt;td&gt;IVFFlat&lt;/td&gt;&lt;td&gt;Fast but misses relevant rows&lt;/td&gt;&lt;td&gt;Increase probes and measure latency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Global setting over-tuned&lt;/td&gt;&lt;td&gt;Both&lt;/td&gt;&lt;td&gt;CPU cost rises fleet-wide&lt;/td&gt;&lt;td&gt;Use route-specific session-local settings&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-cost-observability-and-failure-notes&quot;&gt;Security, Cost, Observability, and Failure Notes&lt;/h2&gt;
&lt;p&gt;Security is mostly indirect: index choice affects whether authorized rows are found after filters. A retrieval path that under-returns can push users toward broader queries and accidental exposure. Keep authorization filters in SQL.&lt;/p&gt;
&lt;p&gt;Cost appears as larger instances, more memory, longer builds, and more expensive replicas. Vector indexes are not free metadata; they are production data structures.&lt;/p&gt;
&lt;p&gt;Observability should include index size, build progress, query latency, recall checks, rows returned versus requested limit, buffer usage, WAL generation during backfills, and replica lag.&lt;/p&gt;
&lt;p&gt;Failure modes include index corruption, accidental index drops, model-version mixing, and silent relevance regressions after parameter changes. Treat vector index settings as configuration that requires review.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Is the table read-heavy or write-heavy?&lt;/li&gt;
&lt;li&gt;Can the build run after bulk loading?&lt;/li&gt;
&lt;li&gt;Is memory the limiting resource?&lt;/li&gt;
&lt;li&gt;Do you have a recall baseline before adding the index?&lt;/li&gt;
&lt;li&gt;Are filters part of the production query?&lt;/li&gt;
&lt;li&gt;Can you tune settings per query class?&lt;/li&gt;
&lt;li&gt;How will you rebuild after an embedding model change?&lt;/li&gt;
&lt;li&gt;What is the rollback plan if relevance gets worse?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Adding a vector index is treated as a performance-only decision, but HNSW and IVFFlat trade recall for speed differently, and the wrong choice surfaces as a memory, build-time, or relevance incident during backfill.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Default to HNSW unless the table is write-heavy, memory-constrained, or still growing through large bulk loads, in which case IVFFlat’s lighter build is the better starting point. Validate the choice with production-shaped data before committing.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; A staging build with production-shaped data completes within the maintenance window, and a saved query set shows recall at or above the pre-index baseline.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, run the baseline exact-search query set against a representative subset of the table, then build both HNSW and IVFFlat in staging and compare build time, peak memory, and recall before choosing.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;pgvector HNSW and IVFFlat documentation: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL index build progress: &lt;a href=&quot;https://www.postgresql.org/docs/current/progress-reporting.html&quot;&gt;https://www.postgresql.org/docs/current/progress-reporting.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL &lt;code&gt;REINDEX&lt;/code&gt; documentation: &lt;a href=&quot;https://www.postgresql.org/docs/current/sql-reindex.html&quot;&gt;https://www.postgresql.org/docs/current/sql-reindex.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>checklist</category></item><item><title>Why pgvector Returns Too Few Results: Filtered HNSW, ef_search, and Iterative Scans</title><link>https://rajivonai.com/blog/2026-04-27-pgvector-filtered-search-too-few-results/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-04-27-pgvector-filtered-search-too-few-results/</guid><description>Why approximate pgvector searches can under-return rows after SQL filters, and how to tune filtered HNSW with ef_search, partial indexes, partitioning, and iterative scans.</description><pubDate>Thu, 09 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The incident looks like an application bug: the support assistant asks for ten documents, but pgvector returns three. The table has thousands of matching rows for the tenant. The embedding query works without the tenant filter. The failure is the interaction between approximate nearest-neighbor search and SQL filtering.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;Approximate pgvector indexes do not always search the whole table before filters are applied. With HNSW or IVFFlat, PostgreSQL can retrieve a candidate set by vector distance and then apply the &lt;code&gt;WHERE&lt;/code&gt; clause. If the filter is selective, too few candidates survive.&lt;/p&gt;
&lt;p&gt;The fixes are workload-specific: raise &lt;code&gt;hnsw.ef_search&lt;/code&gt;, use iterative scans where supported, add exact indexes on filter columns, create partial vector indexes for low-cardinality filters, partition by high-impact filter keys, and verify every change with &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Teams add tenant and category filters to vector queries as the application matures from prototype to multi-tenant production, and recall quietly degrades without any error being raised.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;SQL developers expect this mental model:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Apply &lt;code&gt;WHERE tenant_id = 42&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Rank the remaining rows by vector distance.&lt;/li&gt;
&lt;li&gt;Return the top ten.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Approximate vector indexes often behave closer to this model:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Walk the vector index until enough candidate vectors are found.&lt;/li&gt;
&lt;li&gt;Apply &lt;code&gt;WHERE tenant_id = 42&lt;/code&gt; to those candidates.&lt;/li&gt;
&lt;li&gt;Return whatever remains.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;That difference matters when filters are selective. If a tenant owns 5 percent of the table, the first 40 HNSW candidates may only contain a few rows from that tenant. The query asks for ten, but the index path did not search enough of the graph to find ten authorized matches.&lt;/p&gt;
&lt;p&gt;The pgvector README documents this filtered-search behavior and notes that iterative index scans can scan more of the index until enough rows are found or scan limits are reached. &lt;code&gt;hnsw.ef_search&lt;/code&gt; defaults to 40 with a valid range of 1–1000; the default is tuned for unfiltered recall and is frequently too low once a selective &lt;code&gt;WHERE&lt;/code&gt; clause is added. Confirm the deployed version’s default before assuming 40, since self-managed forks and managed Postgres providers occasionally override extension defaults. The question that matters operationally: does the production query path always include a selective filter, and if so, has anyone measured how many candidates survive it?&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot;&gt;How It Works&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[tenant filtered vector query] --&gt; ANN[approximate index scan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ANN --&gt; Candidates[small vector candidate set]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Candidates --&gt; Filter[apply tenant filter]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Filter --&gt; Few[too few rows survive]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query --&gt; Tune[increase search effort]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Tune --&gt; More[larger candidate set]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    More --&gt; Filter2[apply tenant filter]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Filter2 --&gt; Enough[enough rows returned]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A typical failing query:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; id, body&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; document_chunks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tenant_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; category &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; embedding &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The problem is not the syntax. The problem is that &lt;code&gt;tenant_id&lt;/code&gt; and &lt;code&gt;category&lt;/code&gt; change the selectivity of the vector search. A query that works globally can under-return inside one tenant or category.&lt;/p&gt;
&lt;h2 id=&quot;architecture-and-operating-model&quot;&gt;Architecture and Operating Model&lt;/h2&gt;
&lt;p&gt;Treat filtered vector search as a query-planning problem, not an LLM problem.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 1: Capture the failing query shape.&lt;/strong&gt; Keep the tenant, category, limit, vector operator, and index type. Do not debug with a global query if production always includes tenant filters.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 2: Run &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt;.&lt;/strong&gt; Confirm whether the plan uses HNSW, IVFFlat, a sequential scan, or an exact index on filter columns. Look for rows removed by filters and the actual row count returned.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 3: Tune candidate search.&lt;/strong&gt; For HNSW, raise &lt;code&gt;hnsw.ef_search&lt;/code&gt; for the query or transaction. Higher values improve recall but cost latency. Use &lt;code&gt;SET LOCAL&lt;/code&gt; so one sensitive endpoint does not change the whole session pool.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LOCAL&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; hnsw&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;ef_search&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 200&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; id, body&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; document_chunks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tenant_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; embedding &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;COMMIT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 4: Use iterative scans where available.&lt;/strong&gt; pgvector added &lt;code&gt;hnsw.iterative_scan&lt;/code&gt; (and the IVFFlat equivalent) in version 0.8.0, released October 2024. It defaults to &lt;code&gt;off&lt;/code&gt;. Confirm the deployed extension is 0.8.0 or later before relying on it — earlier versions do not have this parameter and will error on the &lt;code&gt;SET&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; hnsw&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;iterative_scan&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; strict_order;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Strict ordering preserves distance order. Relaxed ordering can improve recall but may need a materialized CTE and final ordering step.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 5: Change physical design when tuning is not enough.&lt;/strong&gt; If a filter is always present, make it part of the storage strategy. For a few categories, use partial vector indexes. For many tenants or categories with strong isolation, consider partitioning.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; document_chunks_policy_hnsw&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; document_chunks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;USING&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; hnsw (embedding vector_cosine_ops)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; category &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;policy&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; document_chunks_partitioned&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tenant_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  document_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  embedding vector(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1536&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  body &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;PARTITION&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; BY&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; HASH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The DBA instinct is useful here: tune from the plan, not from vibes.&lt;/p&gt;
&lt;p&gt;Filtered vector search should have a small library of known query shapes with expected plans. A tenant-scoped RAG endpoint, a category-scoped search endpoint, and an admin global search endpoint should not share one tuning setting blindly.&lt;/p&gt;
&lt;p&gt;Platform teams should expose safe query classes rather than letting every application build arbitrary vector SQL. A retrieval service can choose &lt;code&gt;ef_search&lt;/code&gt;, timeouts, result limits, and fallback behavior by route.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Under-return after tenant filter&lt;/td&gt;&lt;td&gt;Candidate set is too small for selective tenant&lt;/td&gt;&lt;td&gt;Raise &lt;code&gt;ef_search&lt;/code&gt; or enable iterative scans&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slow query after raising &lt;code&gt;ef_search&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Search effort grows for every request&lt;/td&gt;&lt;td&gt;Use route-specific settings and timeouts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Partial index explosion&lt;/td&gt;&lt;td&gt;One partial vector index per tenant&lt;/td&gt;&lt;td&gt;Use partitions or collection boundaries instead&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Partition fanout&lt;/td&gt;&lt;td&gt;Query touches too many partitions&lt;/td&gt;&lt;td&gt;Partition only by filters that are always present&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Exact scan beats ANN&lt;/td&gt;&lt;td&gt;Filter matches a tiny row set&lt;/td&gt;&lt;td&gt;Use B-tree filter index then exact distance order&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-cost-observability-and-failure-notes&quot;&gt;Security, Cost, Observability, and Failure Notes&lt;/h2&gt;
&lt;p&gt;Security requires filtering before results leave the database. Over-fetching globally and filtering in the application can leak unauthorized chunk text into logs, traces, or prompt assembly.&lt;/p&gt;
&lt;p&gt;Cost rises when search effort is raised globally. &lt;code&gt;ef_search = 400&lt;/code&gt; may solve recall for one tenant and burn CPU for every other query. Tune per endpoint.&lt;/p&gt;
&lt;p&gt;Observability should track requested &lt;code&gt;LIMIT&lt;/code&gt;, returned row count, empty-result rate, tenant selectivity, query latency, index used, and configured search effort. Alert on systematic under-return, not just errors.&lt;/p&gt;
&lt;p&gt;Failure handling should include a fallback: if approximate search returns too few rows, rerun with higher search effort or exact search over a bounded filtered candidate set. The fallback should be explicit and measured.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Does the production query always include tenant or category filters?&lt;/li&gt;
&lt;li&gt;What percentage of rows survive each filter?&lt;/li&gt;
&lt;li&gt;Does &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; show an approximate index scan?&lt;/li&gt;
&lt;li&gt;How many rows are removed by the filter?&lt;/li&gt;
&lt;li&gt;Does increasing &lt;code&gt;ef_search&lt;/code&gt; improve returned row count?&lt;/li&gt;
&lt;li&gt;Is iterative scan available in the deployed pgvector version?&lt;/li&gt;
&lt;li&gt;Would an exact B-tree filter plus distance sort be faster for selective filters?&lt;/li&gt;
&lt;li&gt;Are partial indexes limited to a small number of stable values?&lt;/li&gt;
&lt;li&gt;Would partitioning match the real query boundary?&lt;/li&gt;
&lt;li&gt;Is under-return measured as a retrieval-quality metric?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Selective tenant or category filters silently under-return results from approximate pgvector indexes, and the failure looks like an application bug rather than a query-planning issue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Tune from the plan, not from vibes — raise &lt;code&gt;hnsw.ef_search&lt;/code&gt; per route with &lt;code&gt;SET LOCAL&lt;/code&gt;, enable &lt;code&gt;hnsw.iterative_scan&lt;/code&gt; on pgvector 0.8.0+, and move to partial indexes or partitioning when a filter is always present.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; on the production-shaped, filtered query shows rows removed by the filter and a returned row count that matches the requested &lt;code&gt;LIMIT&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, run &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; on the most tenant-selective production query, record the candidate count before and after the filter, and confirm the deployed pgvector version supports &lt;code&gt;hnsw.iterative_scan&lt;/code&gt; before recommending it to the team.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;pgvector filtering and iterative scan documentation: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL &lt;code&gt;EXPLAIN&lt;/code&gt; documentation: &lt;a href=&quot;https://www.postgresql.org/docs/current/using-explain.html&quot;&gt;https://www.postgresql.org/docs/current/using-explain.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL partial indexes: &lt;a href=&quot;https://www.postgresql.org/docs/current/indexes-partial.html&quot;&gt;https://www.postgresql.org/docs/current/indexes-partial.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL partitioning: &lt;a href=&quot;https://www.postgresql.org/docs/current/ddl-partitioning.html&quot;&gt;https://www.postgresql.org/docs/current/ddl-partitioning.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>failures</category><category>checklist</category><category>ai-engineering</category></item><item><title>When pgvector Is Enough: The Postgres-First RAG Architecture</title><link>https://rajivonai.com/blog/2026-04-23-when-pgvector-is-enough/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-04-23-when-pgvector-is-enough/</guid><description>When a Postgres-first RAG design with pgvector is simpler, safer, and easier to operate than adding a separate vector database.</description><pubDate>Wed, 08 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A team adds a separate vector database to power internal support search, then spends the next quarter reconciling two truths: PostgreSQL says the customer document was deleted, while the vector store still returns the old chunk. The retrieval model was fast. The data model was wrong.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;pgvector is enough when the RAG corpus is already relational, the metadata filters matter, the write volume is moderate, and operational simplicity is worth more than specialized vector infrastructure.&lt;/p&gt;
&lt;p&gt;Keeping embeddings inside Postgres gives you one transaction boundary, one backup system, one authorization model, and one place to join vectors with tenant, product, entitlement, and lifecycle fields. That does not make pgvector the right answer for every vector workload. It means many production RAG systems should start in Postgres and earn their way out.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most RAG architecture diagrams start with chunks, embeddings, vector database, retriever, and LLM. They understate the data-management work around the vector store.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A second vector system adds real obligations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Source records need stable IDs.&lt;/li&gt;
&lt;li&gt;Chunks need versioning.&lt;/li&gt;
&lt;li&gt;Deletes need to propagate.&lt;/li&gt;
&lt;li&gt;Tenant filters need to be enforced at retrieval time.&lt;/li&gt;
&lt;li&gt;Embedding model changes need backfills.&lt;/li&gt;
&lt;li&gt;Restore tests need to include the vector index.&lt;/li&gt;
&lt;li&gt;Authorization cannot be delegated to the LLM layer.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If the source data already lives in PostgreSQL, a second vector system creates a synchronization problem before it solves a retrieval problem. Does the corpus need a dedicated vector store at all, or does it just need embeddings added to the database that already owns the data?&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot;&gt;How It Works&lt;/h2&gt;
&lt;p&gt;A Postgres-first RAG design treats embeddings as an indexable representation of rows, not as a new source of truth.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[application write] --&gt; Tx[Postgres transaction]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Tx --&gt; Doc[documents table]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Tx --&gt; Job[embedding job row]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Job --&gt; Worker[embedding worker]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Worker --&gt; Emb[document embeddings table]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[user question] --&gt; Auth[tenant and entitlement filter]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Auth --&gt; SQL[SQL vector query]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SQL --&gt; Context[retrieved context]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Context --&gt; LLM[answer generation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The base tables hold documents, ownership, lifecycle state, and permissions. A companion embeddings table holds chunk text, embedding vectors, model version, content hash, and timestamps.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; document_chunks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  id              &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigserial&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  document_id     &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tenant_id       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  chunk_ordinal   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  content_hash    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  embedding_model &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  body            &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  embedding       vector(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1536&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  deleted_at      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; document_chunks_tenant_idx&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; document_chunks (tenant_id, document_id);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; document_chunks_embedding_hnsw&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; document_chunks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  USING&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; hnsw (embedding vector_cosine_ops);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The retrieval query can enforce authorization before ranking:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; document_id, chunk_ordinal, body&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; document_chunks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tenant_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; deleted_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; embedding &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That query is not magic. It is regular SQL with a vector operator. The value is that the same database enforces the same data boundaries the rest of the application already trusts.&lt;/p&gt;
&lt;h2 id=&quot;architecture-and-operating-model&quot;&gt;Architecture and Operating Model&lt;/h2&gt;
&lt;p&gt;The Postgres-first operating model has four parts.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ingestion.&lt;/strong&gt; Store source documents first. Generate chunks deterministically. Write chunk rows with a content hash and model version. Generate embeddings asynchronously. Make the embedding job idempotent so a retry cannot create duplicate chunks.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Retrieval.&lt;/strong&gt; Apply tenant, entitlement, lifecycle, and category filters in SQL. Rank by vector distance only after the candidate set is authorized. For hybrid retrieval, combine Postgres full-text search with vector search and rerank in the application when necessary.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Maintenance.&lt;/strong&gt; Monitor index size, query plans, table bloat, autovacuum lag, and embedding backfill progress. Rebuild indexes during controlled windows. Keep embedding model version in the table so old and new vectors do not silently mix.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Recovery.&lt;/strong&gt; Restore Postgres and the embeddings restore with it. If embeddings are treated as derived data, keep enough source and job metadata to rebuild them; if they are treated as operational data, include them in normal backup and restore drills.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;DBAs tend to prefer boring systems that fail predictably. pgvector is attractive because it keeps the retrieval feature inside known operational controls: roles, schemas, backups, PITR, extensions, connection pools, observability, and SQL plans.&lt;/p&gt;
&lt;p&gt;The tradeoff is resource contention. Vector indexes consume memory. Approximate searches consume CPU. Large embedding backfills create write load and table churn. A Postgres-first design should isolate this with separate tables, controlled workers, statement timeouts, connection limits, and workload-specific indexes.&lt;/p&gt;
&lt;p&gt;The platform question is not “can Postgres do vector search?” It can. The question is whether the vector workload will harm the OLTP workload that Postgres already serves.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Limit&lt;/th&gt;&lt;th&gt;Production symptom&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;High vector query volume&lt;/td&gt;&lt;td&gt;OLTP latency rises during retrieval peaks&lt;/td&gt;&lt;td&gt;Read replica, workload isolation, or dedicated vector store&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Weak hybrid search needs&lt;/td&gt;&lt;td&gt;Exact terms, SKUs, and names rank poorly&lt;/td&gt;&lt;td&gt;Add full-text search and reranking, or move search-heavy workloads to OpenSearch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Selective filters with approximate indexes&lt;/td&gt;&lt;td&gt;Top-k returns too few authorized rows&lt;/td&gt;&lt;td&gt;Tune &lt;code&gt;ef_search&lt;/code&gt;, iterative scans, partial indexes, or partitions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Large embedding backfills&lt;/td&gt;&lt;td&gt;Vacuum pressure, WAL growth, replica lag&lt;/td&gt;&lt;td&gt;Batch writes, throttle workers, schedule index rebuilds&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Multi-region retrieval&lt;/td&gt;&lt;td&gt;Cross-region consistency and latency conflicts&lt;/td&gt;&lt;td&gt;Define regional ownership and rebuild strategy&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-cost-observability-and-failure-notes&quot;&gt;Security, Cost, Observability, and Failure Notes&lt;/h2&gt;
&lt;p&gt;Security improves when authorization fields live beside vectors, but only if every retrieval query uses them. Do not rely on application-side filtering after retrieval; the retrieved text has already crossed the boundary.&lt;/p&gt;
&lt;p&gt;Cost is often lower at the beginning because there is no extra cluster. It can become higher if vector search forces a larger Postgres instance for unrelated OLTP traffic. Measure the marginal cost of CPU, RAM, storage, WAL, backups, and replicas.&lt;/p&gt;
&lt;p&gt;Observability should include &lt;code&gt;EXPLAIN&lt;/code&gt; plans for representative retrieval queries, index hit behavior, query latency by tenant, rows removed by filters, embedding job lag, and empty-result rates.&lt;/p&gt;
&lt;p&gt;Failure modes are usually stale embeddings, deleted documents still searchable, mixed model versions, or query plans that stop using the intended index after data distribution changes.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Is the corpus already in PostgreSQL?&lt;/li&gt;
&lt;li&gt;Do retrieval filters depend on relational fields or joins?&lt;/li&gt;
&lt;li&gt;Is read-after-write consistency more important than maximum ANN throughput?&lt;/li&gt;
&lt;li&gt;Can the OLTP workload tolerate vector query CPU and memory pressure?&lt;/li&gt;
&lt;li&gt;Can the team validate query plans with &lt;code&gt;EXPLAIN&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;Is the expected corpus size within the team’s Postgres operating comfort?&lt;/li&gt;
&lt;li&gt;Are embedding jobs idempotent and observable?&lt;/li&gt;
&lt;li&gt;Is there a clean path to move retrieval out later if load justifies it?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Teams add a separate vector database before proving Postgres cannot handle the retrieval workload, creating a synchronization problem between two sources of truth.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Keep embeddings inside Postgres when the corpus is already relational — start there for consistency, tenant filtering, and operational simplicity, and leave only when vector traffic, hybrid relevance, memory pressure, or multi-stage retrieval requirements make Postgres the wrong failure boundary.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; on the production-shaped retrieval query shows the intended HNSW index in use, and a restore drill confirms deleted or unauthorized documents no longer appear in retrieval.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, add &lt;code&gt;content_hash&lt;/code&gt;, &lt;code&gt;embedding_model&lt;/code&gt;, and &lt;code&gt;deleted_at&lt;/code&gt; to the embeddings table if they are missing, and run the Decision Checklist above against the current corpus.&lt;/p&gt;
&lt;h2 id=&quot;operating-guardrails&quot;&gt;Operating Guardrails&lt;/h2&gt;
&lt;p&gt;Keep the first implementation deliberately narrow. Start with one corpus, one embedding model, one vector column, one retrieval endpoint, and one query set for regression testing. Add hybrid search, replicas, or a separate vector service only after the simple path has measurable pressure.&lt;/p&gt;
&lt;p&gt;The guardrail that matters most is reversibility. Store source IDs, chunk hashes, model versions, and ingestion timestamps so the team can rebuild embeddings, compare old and new results, and prove that deleted or unauthorized records no longer appear in retrieval.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;pgvector README and index documentation: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL &lt;code&gt;EXPLAIN&lt;/code&gt; documentation: &lt;a href=&quot;https://www.postgresql.org/docs/current/using-explain.html&quot;&gt;https://www.postgresql.org/docs/current/using-explain.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL full text search documentation: &lt;a href=&quot;https://www.postgresql.org/docs/current/textsearch.html&quot;&gt;https://www.postgresql.org/docs/current/textsearch.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>ai-engineering</category></item><item><title>pgvector vs OpenSearch vs Qdrant vs Weaviate: A Database Architect&apos;s Decision Matrix</title><link>https://rajivonai.com/blog/2026-04-20-pgvector-vs-opensearch-vs-qdrant-vs-weaviate-decision-matrix/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-04-20-pgvector-vs-opensearch-vs-qdrant-vs-weaviate-decision-matrix/</guid><description>A production-oriented decision matrix for choosing pgvector, OpenSearch, Qdrant, or Weaviate by workload shape, filters, hybrid search, operations, cost, tenancy, and recovery.</description><pubDate>Tue, 07 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The common failure is not choosing the “wrong” vector database. It is choosing one before the workload is understood. A customer-support RAG system with strict tenant filters, a product catalog with BM25 relevance history, and an internal document assistant over 40,000 pages are three different database problems wearing the same embedding vocabulary.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;There is no universal winner among pgvector, OpenSearch, Qdrant, and Weaviate.&lt;/p&gt;
&lt;p&gt;Use &lt;strong&gt;pgvector&lt;/strong&gt; when the source of truth is already PostgreSQL, consistency matters more than maximum vector throughput, metadata filters are relational, and the team wants one recovery model. Use &lt;strong&gt;OpenSearch&lt;/strong&gt; when keyword search, product search, logs, or relevance-debugging workflows already live in search infrastructure and hybrid retrieval is the main problem. Use &lt;strong&gt;Qdrant&lt;/strong&gt; when the workload is primarily vector retrieval, payload filtering, dense plus sparse retrieval, or multi-stage search with a clean service boundary. Use &lt;strong&gt;Weaviate&lt;/strong&gt; when the object schema, named vectors, hybrid search, and multi-tenant retrieval model fit the application more naturally than relational tables or search indexes.&lt;/p&gt;
&lt;p&gt;The decision should be made from the operating model backward: who backs it up, who pages on it, how tenants are isolated, how relevance is debugged, and how data is re-indexed after an embedding model change.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Vector search arrived through AI application teams, not database architecture review boards. Many first RAG systems were prototypes: chunk documents, embed chunks, store vectors, retrieve top-k, send context to an LLM. That path works until production requirements arrive.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Production retrieval asks different questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Does the answer need read-after-write consistency with the source record?&lt;/li&gt;
&lt;li&gt;Are filters selective enough to change recall?&lt;/li&gt;
&lt;li&gt;Is keyword matching still required for SKUs, error codes, names, and legal terms?&lt;/li&gt;
&lt;li&gt;Does every tenant need isolation, auditability, and export?&lt;/li&gt;
&lt;li&gt;Can the team restore the vector index to a known point in time?&lt;/li&gt;
&lt;li&gt;How expensive is full re-embedding and re-indexing?&lt;/li&gt;
&lt;li&gt;Can SREs explain bad results without reading model embeddings by hand?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Those questions are database questions. They are not solved by nearest-neighbor latency alone. Which of pgvector, OpenSearch, Qdrant, or Weaviate answers them depends on workload shape and the team’s existing operating model.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot;&gt;How It Works&lt;/h2&gt;
&lt;p&gt;The four choices sit at different points on the same architecture map.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Workload[retrieval workload] --&gt; Consistency[needs source consistency]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Workload --&gt; Hybrid[needs lexical relevance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Workload --&gt; Vector[mostly vector retrieval]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Workload --&gt; Object[object schema with many embeddings]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Consistency --&gt; PG[pgvector in PostgreSQL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Hybrid --&gt; OS[OpenSearch search cluster]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Vector --&gt; QD[Qdrant vector service]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Object --&gt; WV[Weaviate object store]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PG --&gt; Ops[one database operating model]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OS --&gt; OpsSearch[search relevance operating model]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    QD --&gt; OpsVector[vector service operating model]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WV --&gt; OpsObject[object retrieval operating model]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;pgvector&lt;/strong&gt; extends PostgreSQL with vector types and indexes. The advantage is co-location: documents, embeddings, ownership fields, tenant IDs, permissions, and transactional updates can live together. The risk is that approximate vector indexes and SQL filters interact in non-obvious ways, especially under selective filters.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;OpenSearch&lt;/strong&gt; starts from a search engine model. It is strong when the retrieval problem includes lexical search, analyzers, field boosts, faceting, product relevance, logs, or operational search workflows. The risk is operational gravity: shard sizing, mappings, refresh behavior, replicas, and relevance pipelines become the production surface.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Qdrant&lt;/strong&gt; starts from vector search as a service. Its core model is collections, points, vectors, and payloads. It is a good fit when vector retrieval and payload filtering are the center of the workload, and when dense plus sparse retrieval or multi-stage query flows are first-class needs. The risk is that the source-of-truth system still has to be integrated, backed up, and reconciled.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Weaviate&lt;/strong&gt; starts from objects with schema, vectors, filters, hybrid search, and optional named vectors. It can model one object with multiple embedding spaces more naturally than a flat chunk table. The risk is schema and operational commitment: once the application model lives in the vector database, migrations, tenancy, and backfills become platform work.&lt;/p&gt;
&lt;h2 id=&quot;decision-matrix&quot;&gt;Decision Matrix&lt;/h2&gt;




































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Criterion&lt;/th&gt;&lt;th&gt;pgvector&lt;/th&gt;&lt;th&gt;OpenSearch&lt;/th&gt;&lt;th&gt;Qdrant&lt;/th&gt;&lt;th&gt;Weaviate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Source-of-truth fit&lt;/td&gt;&lt;td&gt;Strong when PostgreSQL owns the data&lt;/td&gt;&lt;td&gt;Usually secondary index&lt;/td&gt;&lt;td&gt;Usually secondary index&lt;/td&gt;&lt;td&gt;Can become application retrieval store&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Consistency&lt;/td&gt;&lt;td&gt;Strongest if vectors update in the same transaction&lt;/td&gt;&lt;td&gt;Near-real-time search semantics require design&lt;/td&gt;&lt;td&gt;Depends on ingestion pipeline&lt;/td&gt;&lt;td&gt;Depends on object ingestion and consistency model&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Filtering&lt;/td&gt;&lt;td&gt;SQL filters, joins, partial indexes, partitions&lt;/td&gt;&lt;td&gt;Rich search filters, faceting, mappings&lt;/td&gt;&lt;td&gt;Payload filters and vector search&lt;/td&gt;&lt;td&gt;Object filters and tenant-aware queries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hybrid search&lt;/td&gt;&lt;td&gt;Possible with Postgres full text plus vectors, more custom work&lt;/td&gt;&lt;td&gt;Strong fit when BM25 is already central&lt;/td&gt;&lt;td&gt;Strong fit for dense plus sparse and fusion&lt;/td&gt;&lt;td&gt;Built-in hybrid model with tunable weighting&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Operational skillset&lt;/td&gt;&lt;td&gt;DBA team&lt;/td&gt;&lt;td&gt;Search platform team&lt;/td&gt;&lt;td&gt;Vector service platform team&lt;/td&gt;&lt;td&gt;Object/vector platform team&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup and DR&lt;/td&gt;&lt;td&gt;PostgreSQL backup discipline&lt;/td&gt;&lt;td&gt;Snapshot and index recovery discipline&lt;/td&gt;&lt;td&gt;Snapshot and collection recovery discipline&lt;/td&gt;&lt;td&gt;Backup plus tenant and schema discipline&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cost shape&lt;/td&gt;&lt;td&gt;Database CPU, RAM, storage, index bloat&lt;/td&gt;&lt;td&gt;Cluster nodes, shards, replicas, storage, ingestion&lt;/td&gt;&lt;td&gt;Vector memory, storage, replicas, managed service cost&lt;/td&gt;&lt;td&gt;Vector index memory, object storage, modules, managed service cost&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tenancy&lt;/td&gt;&lt;td&gt;Row-level or schema-level patterns&lt;/td&gt;&lt;td&gt;Index, routing, filter, or cluster patterns&lt;/td&gt;&lt;td&gt;Payload filters, collections, or deployment boundaries&lt;/td&gt;&lt;td&gt;Multi-tenancy features and collection design&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;architecture-and-operating-model&quot;&gt;Architecture and Operating Model&lt;/h2&gt;
&lt;p&gt;The most reliable decision process starts with ownership.&lt;/p&gt;
&lt;p&gt;If the DBA team already owns PostgreSQL recovery, audit, migrations, and performance tuning, pgvector is the lowest-friction starting point. The operating model is familiar: schema migrations, &lt;code&gt;EXPLAIN&lt;/code&gt;, indexes, backups, PITR, connection pools, vacuum, and role-based access.&lt;/p&gt;
&lt;p&gt;If a search team already owns OpenSearch for catalog search or log search, OpenSearch may be the right retrieval surface because the operational muscle already exists: analyzers, relevance explain, shard management, index templates, snapshots, and query profiling.&lt;/p&gt;
&lt;p&gt;If the application needs a dedicated retrieval service with payload filtering and multiple vector representations, Qdrant can keep the retrieval boundary explicit. Platform engineering then has to treat it like any other stateful system: deployment topology, snapshots, security, client retries, capacity, and restore tests.&lt;/p&gt;
&lt;p&gt;If product objects need multiple embedding spaces, object-level filters, hybrid search, and collection-level schema, Weaviate may reduce application plumbing. The cost is that schema evolution and vector backfills become part of the data platform lifecycle.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The DBA question is: what is the recovery story?&lt;/p&gt;
&lt;p&gt;For pgvector, embeddings are part of the database. A restore can bring back rows and vectors together. That is attractive for auditability, but vector indexes can increase memory pressure, maintenance cost, and query planning complexity.&lt;/p&gt;
&lt;p&gt;For OpenSearch, Qdrant, and Weaviate, the vector store is usually a derived system. The source of truth is elsewhere. That means the platform needs a replayable ingestion pipeline, idempotent writes, versioned embeddings, and a tested rebuild path. If the vector store is lost, the business question is not “do we have a snapshot?” only. It is “can we rebuild the retrieval index from the source records and prove the rebuilt index answers the same?”&lt;/p&gt;
&lt;h2 id=&quot;security-cost-observability-and-failure-notes&quot;&gt;Security, Cost, Observability, and Failure Notes&lt;/h2&gt;
&lt;p&gt;Security starts with tenant boundaries and embedding content. Embeddings can leak sensitive source text through retrieval even when the raw document is hidden elsewhere. Every option needs authorization at retrieval time, not only at ingestion time.&lt;/p&gt;
&lt;p&gt;Cost is dominated by duplication and rebuilds. Running a second stateful system means paying for storage, replicas, snapshots, monitoring, and ingestion compute. Re-embedding after model changes can become a batch workload with real cloud cost.&lt;/p&gt;
&lt;p&gt;Observability should include query latency, index build time, recall sampling, empty-result rate, under-return rate after filters, ingestion lag, failed upserts, snapshot age, restore test age, and top expensive queries.&lt;/p&gt;
&lt;p&gt;Failure modes usually come from drift: source row changed but embedding did not, document deleted but chunk remains searchable, tenant moved but payload filter is stale, or a relevance change ships without before-and-after query sets.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Bad decision&lt;/th&gt;&lt;th&gt;What happens in production&lt;/th&gt;&lt;th&gt;Better framing&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Choose by benchmark only&lt;/td&gt;&lt;td&gt;Fast demo, weak recovery and tenancy model&lt;/td&gt;&lt;td&gt;Choose by workload and operating model&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Choose pgvector for all workloads&lt;/td&gt;&lt;td&gt;PostgreSQL becomes search, vector, OLTP, and relevance lab at once&lt;/td&gt;&lt;td&gt;Keep pgvector for consistency-heavy retrieval&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Choose OpenSearch for pure vectors&lt;/td&gt;&lt;td&gt;Search cluster complexity without lexical benefit&lt;/td&gt;&lt;td&gt;Use it when search relevance is already central&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Choose Qdrant without source replay&lt;/td&gt;&lt;td&gt;Restore depends on undocumented application behavior&lt;/td&gt;&lt;td&gt;Build idempotent ingestion and rebuild tooling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Choose Weaviate without schema planning&lt;/td&gt;&lt;td&gt;Named vectors and collections evolve through risky backfills&lt;/td&gt;&lt;td&gt;Treat schema as a data contract&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Is PostgreSQL already the source of truth for the content and authorization model?&lt;/li&gt;
&lt;li&gt;Does the query require exact relational filters before vector ranking?&lt;/li&gt;
&lt;li&gt;Does lexical relevance still matter for identifiers, names, codes, and exact terms?&lt;/li&gt;
&lt;li&gt;Does the team already operate OpenSearch with relevance-debugging workflows?&lt;/li&gt;
&lt;li&gt;Does the retrieval service need dense plus sparse vectors, multivectors, or fusion?&lt;/li&gt;
&lt;li&gt;Does one object need multiple embedding spaces?&lt;/li&gt;
&lt;li&gt;Is every vector store write replayable from an authoritative source?&lt;/li&gt;
&lt;li&gt;Can the team restore from backup and validate retrieval quality after restore?&lt;/li&gt;
&lt;li&gt;Are tenant boundaries enforced in the retrieval layer and tested with negative cases?&lt;/li&gt;
&lt;li&gt;Is there a query set for regression testing relevance before every index change?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Teams pick a vector database from a benchmark or a demo before the retrieval workload’s consistency, filtering, and recovery requirements are defined.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Choose from the operating model backward — start with pgvector when consistency and simplicity dominate, move to OpenSearch when search relevance and BM25 are central, use Qdrant when vector retrieval and payload filtering deserve their own service, and use Weaviate when object schema, named vectors, and hybrid retrieval fit the domain.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The chosen system’s recovery story can be explained in one sentence, and a restore drill returns the same top results as a saved query set.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, write down the retrieval workload’s consistency requirement, filter selectivity, and recovery story before evaluating any vector database, then run the Decision Checklist above against the current or proposed system.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;pgvector README: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch vector search documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/&quot;&gt;https://docs.opensearch.org/latest/vector-search/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Qdrant documentation: &lt;a href=&quot;https://qdrant.tech/documentation/&quot;&gt;https://qdrant.tech/documentation/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Weaviate documentation: &lt;a href=&quot;https://docs.weaviate.io/weaviate&quot;&gt;https://docs.weaviate.io/weaviate&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>ai-engineering</category><category>checklist</category></item><item><title>Migrating from Elasticsearch/OpenSearch Keyword Search to Semantic Search</title><link>https://rajivonai.com/blog/2026-06-25-migrate-keyword-search-to-semantic-search-opensearch/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-25-migrate-keyword-search-to-semantic-search-opensearch/</guid><description>A safe migration path from keyword search to semantic or hybrid OpenSearch retrieval using dual indexing, embeddings, backfill, relevance evaluation, A/B testing, fallback, rollback, and cutover.</description><pubDate>Mon, 06 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The dangerous migration is not from keyword search to semantic search. It is from explainable keyword behavior to an untested hybrid system with no fallback. Users do not care that embeddings are modern if exact order numbers, product names, error codes, and compliance filters regress on launch day.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;Migrate from Elasticsearch or OpenSearch keyword search to semantic search as a controlled relevance migration, not a feature toggle. Keep keyword search running, add embeddings through dual indexing, backfill safely, evaluate relevance, run A/B tests, preserve fallback, and cut over with rollback.&lt;/p&gt;
&lt;p&gt;The right target is usually hybrid search, not pure semantic search. BM25 remains valuable for identifiers, exact terms, names, and field-aware search. Vectors add semantic recall. Reranking and fusion decide the final order.&lt;/p&gt;
&lt;p&gt;Elasticsearch and OpenSearch feature compatibility, vector syntax, licensing, and managed-service behavior differ by version and provider — and the divergence has a specific origin. Elastic changed Elasticsearch and Kibana’s license away from Apache 2.0 in January 2021 (to SSPL, later Elastic License v2), and AWS responded in April 2021 by forking Elasticsearch and Kibana 7.10.2 under the Apache 2.0 license as OpenSearch. Every Elasticsearch feature released after that fork point (including most of Elastic’s own vector search work) is not automatically present in OpenSearch, and vice versa — the two codebases have diverged independently since. Confirm feature-by-feature compatibility for the specific versions in play rather than assuming shared ancestry implies shared syntax.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Many teams already have a mature keyword search index. It has analyzers, synonyms, dashboards, relevance tweaks, index templates, aliases, and incident history. Then semantic search arrives through RAG, AI search, or product-discovery requirements.&lt;/p&gt;
&lt;p&gt;The business wants better answers without losing the known behavior users rely on.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Existing search systems contain hidden contracts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Exact identifiers must work.&lt;/li&gt;
&lt;li&gt;Filters must preserve authorization.&lt;/li&gt;
&lt;li&gt;Sort and boost rules encode product policy.&lt;/li&gt;
&lt;li&gt;Synonyms and analyzers reflect years of tuning.&lt;/li&gt;
&lt;li&gt;Dashboards and alerts assume existing query paths.&lt;/li&gt;
&lt;li&gt;Customer support knows current failure modes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Semantic search can improve recall, but it can also break those contracts. A vector result that is conceptually close but unavailable, unauthorized, obsolete, or missing the exact requested identifier is not an improvement.&lt;/p&gt;
&lt;p&gt;The migration question is: how do you add semantic recall without destroying keyword reliability?&lt;/p&gt;
&lt;h2 id=&quot;core-technical-explanation&quot;&gt;Core Technical Explanation&lt;/h2&gt;
&lt;p&gt;A safe migration keeps old and new retrieval paths alive long enough to compare them.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Source[source records] --&gt; Current[current keyword index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Source --&gt; Embed[embedding generation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Embed --&gt; Candidate[new hybrid index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Current --&gt; Eval[offline relevance evaluation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Candidate --&gt; Eval&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Eval --&gt; AB[A B test or shadow traffic]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    AB --&gt; Cutover[alias cutover]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Cutover --&gt; Fallback[keyword fallback and rollback]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The migration has five workstreams.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Schema.&lt;/strong&gt; Add vector fields and model metadata to a new index or versioned index template. Avoid mutating the only production index in place unless rollback is trivial.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Embedding generation.&lt;/strong&gt; Generate embeddings from stable text fields. Store model version, dimension, source hash, and generated time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Backfill.&lt;/strong&gt; Index embeddings in controlled batches. Monitor indexing pressure, refresh behavior, merge pressure, and search latency.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Evaluation.&lt;/strong&gt; Compare current keyword results, semantic results, and hybrid results against known query sets.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cutover.&lt;/strong&gt; Use aliases, feature flags, or route-level controls so traffic can move gradually and roll back quickly.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Start by classifying queries:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Exact lookup: order ID, SKU, ticket number, error code.&lt;/li&gt;
&lt;li&gt;Navigational: known title, known product, known document.&lt;/li&gt;
&lt;li&gt;Exploratory: broad product or knowledge discovery.&lt;/li&gt;
&lt;li&gt;Troubleshooting: long natural-language problem statement.&lt;/li&gt;
&lt;li&gt;Regulated: queries where tenant, region, or entitlement filters dominate.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Do not tune all query classes with one average metric. Exact lookup may stay mostly BM25. Troubleshooting may benefit heavily from vectors. Product discovery may need hybrid plus business reranking.&lt;/p&gt;
&lt;p&gt;Use shadow evaluation before user-facing A/B tests. For each real query, run the old keyword path and the new hybrid path, log candidate IDs, and compare offline. Once the new path passes basic safety checks, expose it to a small traffic slice.&lt;/p&gt;
&lt;p&gt;Keep fallback boring. If vector search, embedding generation, reranking, or fusion fails, the system should be able to serve keyword results.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Migration symptom&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Pure semantic replacement&lt;/td&gt;&lt;td&gt;Exact identifiers regress&lt;/td&gt;&lt;td&gt;Keep BM25 path and hybrid fusion&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No dual index&lt;/td&gt;&lt;td&gt;Rollback requires risky in-place edits&lt;/td&gt;&lt;td&gt;Build versioned index and alias switch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backfill unthrottled&lt;/td&gt;&lt;td&gt;Cluster latency and merge pressure&lt;/td&gt;&lt;td&gt;Batch and throttle indexing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No relevance baseline&lt;/td&gt;&lt;td&gt;Team argues from anecdotes&lt;/td&gt;&lt;td&gt;Maintain query sets and offline reports&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Filters rewritten incorrectly&lt;/td&gt;&lt;td&gt;Security or availability regressions&lt;/td&gt;&lt;td&gt;Share filter logic and run negative tests&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;A/B test lacks guardrails&lt;/td&gt;&lt;td&gt;Bad results reach too much traffic&lt;/td&gt;&lt;td&gt;Start with shadow mode and small cohorts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reranker opaque&lt;/td&gt;&lt;td&gt;Cannot explain result changes&lt;/td&gt;&lt;td&gt;Log candidate stages and features&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-and-tenancy-notes&quot;&gt;Security and Tenancy Notes&lt;/h2&gt;
&lt;p&gt;The migration must preserve existing authorization behavior. If keyword search enforces tenant filters, region restrictions, deleted state, or compliance rules, the semantic path must enforce the same constraints.&lt;/p&gt;
&lt;p&gt;Dual indexing can create data leakage if old and new indexes differ in redaction, deletion, or entitlement fields. Every migration should include negative tests: documents a user must not retrieve, tenants they must not cross, and deleted records that must not reappear.&lt;/p&gt;
&lt;p&gt;If embeddings are generated outside the search cluster, review data handling and retention — this is a contractual question for whoever owns the data-processing agreement with that embedding provider, not a cluster configuration setting.&lt;/p&gt;
&lt;h2 id=&quot;cost-notes&quot;&gt;Cost Notes&lt;/h2&gt;
&lt;p&gt;Migration temporarily costs more than steady state. You may run two indexes, store both old and new fields, backfill embeddings, execute shadow queries, run A/B infrastructure, and keep rollback capacity.&lt;/p&gt;
&lt;p&gt;Budget for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Embedding generation.&lt;/li&gt;
&lt;li&gt;Extra index storage.&lt;/li&gt;
&lt;li&gt;Additional replicas or nodes during backfill.&lt;/li&gt;
&lt;li&gt;Snapshot storage for both index versions.&lt;/li&gt;
&lt;li&gt;Query CPU for shadow traffic.&lt;/li&gt;
&lt;li&gt;Reranking or model calls.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The cost is justified if it prevents a relevance incident during cutover.&lt;/p&gt;
&lt;h2 id=&quot;observability-notes&quot;&gt;Observability Notes&lt;/h2&gt;
&lt;p&gt;Track migration-specific signals:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Backfill progress and failure rate.&lt;/li&gt;
&lt;li&gt;Embedding lag and model-version distribution.&lt;/li&gt;
&lt;li&gt;Keyword versus hybrid result overlap.&lt;/li&gt;
&lt;li&gt;Exact-lookup regression rate.&lt;/li&gt;
&lt;li&gt;Zero-result rate.&lt;/li&gt;
&lt;li&gt;Click-through or success metrics by cohort.&lt;/li&gt;
&lt;li&gt;Query latency by old path and new path.&lt;/li&gt;
&lt;li&gt;Filter mismatch count.&lt;/li&gt;
&lt;li&gt;Rollback trigger metrics.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Keep a dashboard for launch day that compares old and new behavior side by side.&lt;/p&gt;
&lt;h2 id=&quot;backup-restore-and-dr-notes&quot;&gt;Backup, Restore, and DR Notes&lt;/h2&gt;
&lt;p&gt;Before cutover, snapshot both the old and new index versions. More importantly, make sure both can be rebuilt from source systems.&lt;/p&gt;
&lt;p&gt;If the hybrid index is corrupted, mapped incorrectly, or built with the wrong embeddings, restore may not be enough. You may need to rebuild. The runbook should include replacement index creation, backfill replay, relevance validation, alias switch, and rollback.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;What existing keyword behavior must never regress?&lt;/li&gt;
&lt;li&gt;Which query classes benefit from semantic search?&lt;/li&gt;
&lt;li&gt;Is the target pure vector, hybrid, or hybrid plus reranking?&lt;/li&gt;
&lt;li&gt;Are vector fields added in a new versioned index?&lt;/li&gt;
&lt;li&gt;Is embedding generation idempotent and versioned?&lt;/li&gt;
&lt;li&gt;Can the old keyword path serve as fallback?&lt;/li&gt;
&lt;li&gt;Is there a protected relevance query set?&lt;/li&gt;
&lt;li&gt;Are tenant and entitlement filters shared by both paths?&lt;/li&gt;
&lt;li&gt;Can traffic be shifted gradually?&lt;/li&gt;
&lt;li&gt;Can rollback happen without reindexing under pressure?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Teams replace years of keyword tuning with a vector query in one release, and exact order numbers, product names, error codes, and compliance filters regress on launch day.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Treat the migration like a database cutover — dual indexing, shadow evaluation before user-facing A/B tests, a protected relevance query set per query class, and an alias-based rollback path — targeting hybrid search rather than pure semantic replacement.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The old keyword path and new hybrid path are compared offline on the same query set before any user sees the new path, and a negative test confirms no authorization regression between the two indexes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, confirm the migration plan can be stopped halfway (via alias switch) without user-visible damage — if rollback requires reindexing under pressure, that’s the first gap to close.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;OpenSearch vector search, hybrid search, and query DSL documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/&quot;&gt;https://docs.opensearch.org/latest/vector-search/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch history and Elasticsearch fork background: &lt;a href=&quot;https://opensearch.org/blog/&quot;&gt;https://opensearch.org/blog/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch reindex, aliases, and index template documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/im-plugin/&quot;&gt;https://docs.opensearch.org/latest/im-plugin/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch snapshot and restore documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/tuning-your-cluster/availability-and-recovery/snapshots/index/&quot;&gt;https://docs.opensearch.org/latest/tuning-your-cluster/availability-and-recovery/snapshots/index/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Amazon OpenSearch Service migration guidance (if using the managed service): &lt;a href=&quot;https://docs.aws.amazon.com/opensearch-service/latest/developerguide/migration.html&quot;&gt;https://docs.aws.amazon.com/opensearch-service/latest/developerguide/migration.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>ai-engineering</category><category>checklist</category></item><item><title>Storage Latency Topologies: Abstracting I/O Performance Across AWS, Azure, and Exadata</title><link>https://rajivonai.com/blog/2025-05-12-oracle-io-troubleshooting-exadata-aws-azure-oci/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-05-12-oracle-io-troubleshooting-exadata-aws-azure-oci/</guid><description>Why expecting standard cloud block storage to match Exadata performance causes migration failures, and how platform engineers build an automated storage vending plane across AWS EBS, Azure Premium SSD v2, and OCI Exadata.</description><pubDate>Sat, 04 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Expecting cloud virtual machine block storage to perform identically to engineered database hardware is an operational fallacy; bridging the performance divide across AWS, Azure, and Exadata requires designing storage topologies around physical I/O boundaries.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Cloud VMs fetch every 8 KB block over virtualized storage networks and enforce separate VM-level and volume-level bandwidth ceilings, breaking workloads migrated from Exadata Smart Scan.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Replace manual disk selection with a declarative Storage Compiler that segregates &lt;code&gt;+RECO&lt;/code&gt; onto &lt;code&gt;io2&lt;/code&gt; Block Express or Azure Ultra Disk, stripes &lt;code&gt;+DATA&lt;/code&gt; across four &lt;code&gt;gp3&lt;/code&gt; or Premium SSD v2 volumes in ASM, and routes heavy analytical scans to Exadata.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Provisioning fast disks on an undersized VM shape triggers silent hypervisor throttling, and enabling host caching on Azure ASM disks risks silent block corruption.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2025-05-12-oracle-io-troubleshooting-exadata-aws-azure-oci.svg&quot; alt=&quot;Comparison of OCI Exadata Cell Offload, AWS EC2 Block Storage, and Azure VM Managed Disks&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Heterogeneous database estates rarely live on a single cloud provider. Analytical data warehouses often run on Oracle Cloud Infrastructure (OCI) Exadata Database Service, transactional payment services on Amazon Web Services (AWS) EC2, and enterprise resource planning (ERP) workloads on Microsoft Azure Virtual Machines.&lt;/p&gt;
&lt;p&gt;In an on-premises data center, storage performance was governed by dedicated Storage Area Network (SAN) administrators carving Fibre Channel LUNs from shared all-flash arrays. In the public cloud, storage architectures diverge across three distinct physical models:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;OCI Exadata Cloud Service:&lt;/strong&gt; Combines scale-out database compute nodes with intelligent storage cell servers over a 100 Gbps RDMA over Converged Ethernet (RoCEv2) fabric. Exadata pushes SQL predicates, column projections, Storage Index lookups, and Hybrid Columnar Compression (HCC) decompression down to the storage cell processors (&lt;strong&gt;Smart Scan&lt;/strong&gt;), returning only matching rows rather than raw 8 KB database blocks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AWS EC2 with Amazon EBS:&lt;/strong&gt; Attaches network-backed block volumes over AWS Nitro NVMe-over-Fabrics. General Purpose SSD (&lt;code&gt;gp3&lt;/code&gt;) provides a flat baseline of 3,000 IOPS and 125 MiB/s with independent scaling up to 80,000 IOPS and 2,000 MiB/s per volume. Provisioned IOPS SSD (&lt;code&gt;io2&lt;/code&gt; Block Express) scales up to 256,000 IOPS and 4,000 MiB/s with 99.999% durability.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Microsoft Azure Virtual Machines:&lt;/strong&gt; Attaches Azure Managed Disks over a virtualized storage plane. Premium SSD v2 provides a free baseline of 3,000 IOPS and 125 MB/s and scales independently up to 80,000 IOPS and 2,000 MB/s. Azure Ultra Disk scales up to 400,000 IOPS and 10,000 MB/s for latency-sensitive transaction logging.&lt;/li&gt;
&lt;/ol&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Provider &amp;#x26; Tier&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Architecture Model&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Single-Disk Max IOPS&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Single-Disk Max Throughput&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Query Processing Model&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;OCI Exadata Storage Cells&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Distributed Storage Silicon&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Millions across cell bus&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;RoCEv2 fabric bound&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cell Smart Scan Offload&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;AWS EBS &lt;code&gt;io2&lt;/code&gt; Block Express&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Network NVMe-oF&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;256,000 IOPS&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;4,000 MiB/s&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Host-Level Block Fetch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;AWS EBS &lt;code&gt;gp3&lt;/code&gt; Volumes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Network NVMe-oF&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;80,000 IOPS&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2,000 MiB/s&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Host-Level Block Fetch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Azure Premium SSD v2&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Software-Defined Disk&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;80,000 IOPS&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2,000 MB/s&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Host-Level Block Fetch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Azure Ultra Disk&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Low-Latency NVMe LUN&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;400,000 IOPS&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;10,000 MB/s&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Host-Level Block Fetch&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;When platform teams expose raw cloud disk APIs to application engineers without guardrails, teams size storage by capacity alone and ignore physical I/O boundaries.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Application engineers reason in SQL semantics and business transaction rates, not hypervisor queue depths, NVMe packetization overhead, or storage cell offloading. Without an abstraction layer enforcing physical constraints, three operational failure patterns emerge.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 1: Host-Level Block Fetch vs. Cell Offload.&lt;/strong&gt; Exadata evaluates &lt;code&gt;WHERE&lt;/code&gt; predicates inside the storage cell servers (&lt;code&gt;cell smart table scan&lt;/code&gt;). If a 10 TB table scan filters down to 200 GB of matching rows, Exadata transfers 200 GB across the RoCEv2 fabric. On AWS EC2 or an Azure VM, there is no storage-tier SQL offload; the database host must pull all 10 TB of raw 8 KB blocks over the virtualized network interface. On an Azure &lt;code&gt;Standard_E32-8s_v5&lt;/code&gt; VM, whose sustained uncached disk throughput ceiling is 865 MB/s, transferring 10 TB (&lt;code&gt;10,000,000 MB / 865 MB/s&lt;/code&gt;) requires at least 11,560 seconds—roughly 3.2 hours—of pure disk transfer before accounting for CPU evaluation or concurrent queries.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 2: Dual-Constraint Hypervisor Ceilings.&lt;/strong&gt; Cloud hypervisors enforce two independent storage limits: the &lt;strong&gt;volume limit&lt;/strong&gt; and the &lt;strong&gt;VM instance limit&lt;/strong&gt;. Effective throughput is always &lt;code&gt;min(sum of attached disk limits, VM uncached limit)&lt;/code&gt;. An engineer can attach an Azure Premium SSD v2 volume provisioned for 60,000 IOPS and 1,000 MB/s to a &lt;code&gt;Standard_E8s_v5&lt;/code&gt; VM. Because &lt;code&gt;Standard_E8s_v5&lt;/code&gt; caps sustained uncached disk performance at 12,800 IOPS and 290 MB/s, the hypervisor throttles every read above 290 MB/s. Oracle sessions stall on &lt;code&gt;db file sequential read&lt;/code&gt; while the organization pays for 710 MB/s of disk throughput the VM cannot physically consume.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Constraint 3: Redo Log Commit Queue Contention (&lt;code&gt;log file sync&lt;/code&gt;).&lt;/strong&gt; Oracle transactional commit latency depends on the Log Writer (&lt;code&gt;LGWR&lt;/code&gt;) flushing sequential redo entries via &lt;code&gt;log file parallel write&lt;/code&gt;. When &lt;code&gt;+RECO&lt;/code&gt; (redo logs) shares a single &lt;code&gt;gp3&lt;/code&gt; or Premium SSD v2 volume with &lt;code&gt;+DATA&lt;/code&gt; (random-access table and index blocks), checkpoint flushes and parallel queries saturate the shared disk queue. Redo writes queue behind bulk reads, causing &lt;code&gt;log file sync&lt;/code&gt; waits to climb and upstream application connection pools to exhaust.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2025-05-12-oracle-io-troubleshooting-exadata-aws-azure-oci-2-constraints.svg&quot; alt=&quot;VM hypervisor ceilings silently throttle over-provisioned disks&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How can platform teams build an automated storage vending plane that maps application SLAs to cloud-native topologies without violating VM ceilings or mixing I/O profiles?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams typically evaluate four storage architectures when standardizing Oracle deployments across AWS, Azure, and OCI.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Redo Isolation&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Scan Bandwidth&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Cost Profile&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Verdict&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;1. Single shared &lt;code&gt;gp3&lt;/code&gt; or Premium SSD v2&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;None (&lt;code&gt;+DATA&lt;/code&gt; and &lt;code&gt;+RECO&lt;/code&gt; share queue)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bounded by single disk and VM&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lowest&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected: batch I/O stalls &lt;code&gt;LGWR&lt;/code&gt; commits&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;2. All-&lt;code&gt;io2&lt;/code&gt; or All-Ultra Disk topology&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Isolated only if separate volumes used&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High, up to VM ceiling&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Highest&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected: overpays for cold datafile IOPS&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;3. Segregated &lt;code&gt;+RECO&lt;/code&gt; + 4x ASM-striped &lt;code&gt;+DATA&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Dedicated &lt;code&gt;io2&lt;/code&gt; or Ultra Disk&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Scales linearly across 4 LUNs to VM cap&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Balanced&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chosen for Cloud IaaS OLTP&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;4. OCI Exadata or Oracle Database@Azure/AWS&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exadata Smart Flash Logging&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cell Smart Scan offload&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Premium&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chosen for &lt;code&gt;DW-Throughput-Heavy&lt;/code&gt; SLAs&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Option 1 (Single Shared Pool)&lt;/strong&gt; minimizes Terraform complexity by attaching one large volume for all ASM diskgroups. It fails under mixed workloads because Oracle’s sequential &lt;code&gt;LGWR&lt;/code&gt; writes compete with random &lt;code&gt;DBWn&lt;/code&gt; flushes and full table scans on the same NVMe queue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 2 (All-Ultra / All-&lt;code&gt;io2&lt;/code&gt;)&lt;/strong&gt; places both &lt;code&gt;+DATA&lt;/code&gt; and &lt;code&gt;+RECO&lt;/code&gt; on top-tier provisioned IOPS volumes. While latency is predictable, paying Ultra Disk or &lt;code&gt;io2&lt;/code&gt; per-IOPS rates for multi-terabyte historical tables that only require sequential throughput inflates monthly storage spend without improving OLTP commit times.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 3 (Segregated Redo + ASM-Striped Data)&lt;/strong&gt; isolates &lt;code&gt;+RECO&lt;/code&gt; on a small, dedicated &lt;code&gt;io2&lt;/code&gt; Block Express or Azure Ultra Disk volume and stripes &lt;code&gt;+DATA&lt;/code&gt; across four &lt;code&gt;gp3&lt;/code&gt; or Premium SSD v2 volumes using Oracle Automatic Storage Management (ASM). This matches each volume tier to its exact I/O pattern.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 4 (Exadata Smart Scan)&lt;/strong&gt; routes analytical workloads with multi-terabyte scan requirements to OCI Exadata Database Service or native Exadata deployments inside Azure and AWS (&lt;code&gt;Oracle Database@Azure&lt;/code&gt; and &lt;code&gt;Oracle AI Database@AWS&lt;/code&gt;), avoiding the host-level block-shipping bottleneck altogether.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2025-05-12-oracle-io-troubleshooting-exadata-aws-azure-oci-3-options.svg&quot; alt=&quot;Segregating redo from striped data balances cost and latency&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-storage-vending-abstraction-architecture&quot;&gt;The Storage Vending Abstraction Architecture&lt;/h2&gt;
&lt;p&gt;To enforce these choices consistently, the platform exposes a declarative &lt;strong&gt;Database Vending Control Plane&lt;/strong&gt; implemented with Terraform modules and policy checks (via Open Policy Agent or Crossplane compositions). Application teams never pick EBS volume types, Azure LUN caching flags, or ASM allocation units. Instead, they submit a declarative storage claim containing capacity and one of three I/O SLAs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;OLTP-Latency-Critical&lt;/code&gt;:&lt;/strong&gt; Prioritizes sub-millisecond &lt;code&gt;LGWR&lt;/code&gt; commit latency and high random-read concurrency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;DW-Throughput-Heavy&lt;/code&gt;:&lt;/strong&gt; Prioritizes multi-gigabyte-per-second sequential scan bandwidth and predicate offload.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;Balanced-Standard&lt;/code&gt;:&lt;/strong&gt; Cost-optimized baseline for non-production or low-concurrency transactional services.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;./2025-05-12-oracle-io-troubleshooting-exadata-aws-azure-oci-4-architecture.svg&quot; alt=&quot;Storage compiler maps SLA claims to cloud topologies&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph Developer Self-Service Tier&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        A[Developer Manifest: Storage Claim — 5TB, SLA=OLTP-Latency-Critical]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph DBaaS Control Plane — Storage Compiler&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        A --&gt; B[Platform Storage Compiler API]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        B --&gt; C{Evaluate Target Cloud Environment}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph AWS Storage Plane Topology&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        C --&gt;|Target AWS| D[Provision EC2 Memory-Optimized Shape]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        D --&gt; E[Isolate Redo: io2 Block Express 10K IOPS]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        D --&gt; F[Stripe Data: 4x gp3 Volumes across ASM +DATA]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        D --&gt; G[Assert VM EBS Throughput &gt; 1500 MB/s]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph Azure Storage Plane Topology&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        C --&gt;|Target Azure| H[Provision Constrained-Core E32-8s VM]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        H --&gt; I[Isolate Redo: Azure Ultra Disk Sub-Millisecond]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        H --&gt; J[Stripe Data: 4x Premium SSD v2 across ASM +DATA]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        H --&gt; K[Assert VM Uncached Cap &gt;= 865 MB/s]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph OCI Engineered Systems Topology&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        C --&gt;|Target OCI| L[Provision Exadata Database Service]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        L --&gt; M[Enable Cell Smart Scan Offloading]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        L --&gt; N[Configure Smart Flash Logging for Redo]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        L --&gt; O[Enable Hybrid Columnar Compression]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;core-storage-compiler-rules&quot;&gt;Core Storage Compiler Rules&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Strict I/O Profile Segregation:&lt;/strong&gt; The compiler never colocates &lt;code&gt;+RECO&lt;/code&gt; and &lt;code&gt;+DATA&lt;/code&gt; on the same physical disk tier in production cloud IaaS. Redo logs receive dedicated &lt;code&gt;io2&lt;/code&gt; Block Express (AWS) or Ultra Disk (Azure) LUNs with independent queue depths.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Algorithmic ASM Striping:&lt;/strong&gt; Striping &lt;code&gt;+DATA&lt;/code&gt; across four &lt;code&gt;gp3&lt;/code&gt; or Premium SSD v2 volumes distributes I/O across multiple virtualized controller queues and allows the control plane to scale aggregate IOPS and bandwidth cleanly up to the VM’s ceiling. Setting &lt;code&gt;au_size = 4M&lt;/code&gt; with &lt;code&gt;COARSE&lt;/code&gt; striping places one 4 MB Allocation Unit per LUN in round-robin order, keeping single-block OLTP reads on one volume while spreading multi-block sequential scans and RMAN backups across all four volumes. By contrast, &lt;code&gt;FINE&lt;/code&gt; striping splits each 1 MB unit into 128 KB chunks across every disk, which is reserved for latency-sensitive redo logs when striping is used.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hypervisor Capacity Assertions:&lt;/strong&gt; Before running &lt;code&gt;terraform apply&lt;/code&gt;, the compiler validates that the target compute shape’s sustained EBS bandwidth (AWS) or uncached disk limit (Azure) exceeds the aggregate provisioned throughput of all attached disks. On AWS, an &lt;code&gt;r6i.16xlarge&lt;/code&gt; provides 20 Gbps of dedicated EBS bandwidth, 80,000 IOPS (at 16 KiB), and 2,500 MB/s (roughly 2,384 MiB/s) of throughput—enough to absorb a 2,000 MiB/s striped &lt;code&gt;+DATA&lt;/code&gt; pool plus a 10,000 IOPS &lt;code&gt;+RECO&lt;/code&gt; volume without instance-level queuing. On Azure, constrained-vCPU shapes such as &lt;code&gt;Standard_E32-8s_v5&lt;/code&gt; provide 256 GiB of memory and 51,200 uncached IOPS (865 MB/s sustained, 2,000 MB/s burst) while keeping active vCPUs at 8 for Oracle processor licensing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Smart Scan Routing for Analytical SLAs:&lt;/strong&gt; When a claim specifies &lt;code&gt;DW-Throughput-Heavy&lt;/code&gt; and exceeds single-VM block-fetch thresholds, the compiler routes placement to OCI Exadata Database Service or native Exadata racks in Azure/AWS.&lt;/li&gt;
&lt;/ol&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Target Cloud &amp;#x26; Tier&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Redo Storage (&lt;code&gt;+RECO&lt;/code&gt;)&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Data Storage (&lt;code&gt;+DATA&lt;/code&gt;)&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;VM &amp;#x26; Striping Guardrail&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;AWS EC2 (&lt;code&gt;OLTP-Latency-Critical&lt;/code&gt;)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;io2&lt;/code&gt; Block Express (10,000 IOPS)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;4x &lt;code&gt;gp3&lt;/code&gt; striped (&lt;code&gt;2,000 MB/s&lt;/code&gt; agg.)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;ASM &lt;code&gt;au_size = 4M&lt;/code&gt;; EBS-optimized &lt;code&gt;r6i&lt;/code&gt; shape&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Azure VM (&lt;code&gt;OLTP-Latency-Critical&lt;/code&gt;)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Azure Ultra Disk (&lt;code&gt;15,000 IOPS&lt;/code&gt;)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;4x Premium SSD v2 (&lt;code&gt;1,600 MB/s&lt;/code&gt; agg.)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;Standard_E32-8s_v5&lt;/code&gt; or larger; &lt;code&gt;caching = &quot;None&quot;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;OCI Exadata (&lt;code&gt;DW-Throughput-Heavy&lt;/code&gt;)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exadata Smart Flash Logging&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High Capacity Storage Cells&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Smart Scan + HCC enabled&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;AWS / Azure (&lt;code&gt;Balanced-Standard&lt;/code&gt;)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Dedicated baseline &lt;code&gt;gp3&lt;/code&gt; / Prem v2&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Single or 2x &lt;code&gt;gp3&lt;/code&gt; / Prem v2&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Non-production baseline with TTL&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Deploying a storage topology through the vending plane follows four sequential gates, each with an explicit validation check and rollback point before application traffic reaches the database.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2025-05-12-oracle-io-troubleshooting-exadata-aws-azure-oci-5-flow.svg&quot; alt=&quot;Four-gate rollout validates I/O before database open&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Gate 1 — Pre-Flight Quota &amp;#x26; Shape Assertion:&lt;/strong&gt; The compiler evaluates &lt;code&gt;sum(disk_iops) &amp;#x3C;= vm_uncached_iops&lt;/code&gt; and &lt;code&gt;sum(disk_mbps) &amp;#x3C;= vm_uncached_mbps&lt;/code&gt;, and verifies availability zone alignment (&lt;code&gt;UltraSSD_LRS&lt;/code&gt; and &lt;code&gt;PremiumV2_LRS&lt;/code&gt; require an explicit zone). Rollback: fail CI pipeline before any cloud API call.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate 2 — Declarative Disk Provisioning:&lt;/strong&gt; Terraform provisions the VM, the dedicated &lt;code&gt;+RECO&lt;/code&gt; volume, and the four &lt;code&gt;+DATA&lt;/code&gt; volumes, enforcing &lt;code&gt;caching = &quot;None&quot;&lt;/code&gt; on all Azure ASM attachments and KMS encryption on AWS EBS volumes. Rollback: &lt;code&gt;terraform destroy&lt;/code&gt; cleans up unformatted volumes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate 3 — ASM Diskgroup Initialization:&lt;/strong&gt; The host bootstrap playbook maps persistent device paths (via udev rules or LUN symlinks) and executes &lt;code&gt;CREATE DISKGROUP&lt;/code&gt; with &lt;code&gt;EXTERNAL REDUNDANCY&lt;/code&gt; and &lt;code&gt;&apos;au_size&apos; = &apos;4M&apos;&lt;/code&gt;, keeping cloud-native triple replication at the block layer rather than double-mirroring in ASM. Rollback: &lt;code&gt;DROP DISKGROUP ... INCLUDING CONTENTS&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate 4 — Synthetic I/O Calibration:&lt;/strong&gt; Before opening the database to application workloads, the pipeline runs Oracle ORION (against raw ASM LUNs prior to database creation) or &lt;code&gt;DBMS_RESOURCE_MANAGER.CALIBRATE_IO&lt;/code&gt; (inside the opened instance) to measure sustained IOPS, MB/s, and single-block read latency against the SLA claim. Because &lt;code&gt;CALIBRATE_IO&lt;/code&gt; issues asynchronous random 8 KB reads and sequential 1 MB reads across all datafiles, any mismatch between provisioned disk limits and the VM hypervisor ceiling surfaces immediately as a plateau below the expected aggregate MB/s. Rollback: quarantine the instance and resize the VM shape if hypervisor throttling is detected.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Based on documented AWS EBS, Azure Managed Disks, and Oracle ASM behavior, the storage compiler codifies these rules directly in Terraform and &lt;code&gt;asmcmd&lt;/code&gt; templates. Notice that on Azure, both &lt;code&gt;UltraSSD_LRS&lt;/code&gt; and &lt;code&gt;PremiumV2_LRS&lt;/code&gt; require an availability &lt;code&gt;zone&lt;/code&gt; and &lt;code&gt;caching = &quot;None&quot;&lt;/code&gt; on the attachment.&lt;/p&gt;
&lt;h3 id=&quot;1-aws-striped-ebs-storage-module&quot;&gt;1. AWS Striped EBS Storage Module&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# modules/aws_oracle_storage/main.tf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;aws_ebs_volume&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;oracle_redo_volume&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  availability_zone&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;us-east-1a&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  size&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 250&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;io2&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  iops&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  encrypted&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;         =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  kms_key_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;kms_key_arn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tags&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; { Name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;vol-oracle-reco-01&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, Diskgroup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;RECO&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;aws_ebs_volume&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;oracle_data_volumes&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  count&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;             =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  availability_zone&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;us-east-1a&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  size&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1250&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt; # 4 x 1,250 GiB = 5,000 GiB (+DATA)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;gp3&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  iops&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 12000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  throughput&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 500&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;  # 4 x 500 = 2,000 MiB/s aggregate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  encrypted&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;         =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  kms_key_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;kms_key_arn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tags&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; { Name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;vol-oracle-data-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;index&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, Diskgroup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;DATA&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-azure-decoupled-premium-ssd-v2-and-ultra-disk-module&quot;&gt;2. Azure Decoupled Premium SSD v2 and Ultra Disk Module&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# modules/azure_oracle_storage/main.tf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_managed_disk&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;oracle_ultra_redo&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                 =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;disk-oracle-reco-ultra-01&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  location&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;             =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;location&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  resource_group_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;resource_group_name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  zone&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                 =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;1&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  storage_account_type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;UltraSSD_LRS&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  create_option&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Empty&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  disk_size_gb&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;         =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 256&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  disk_iops_read_write&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 15000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  disk_mbps_read_write&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 500&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_managed_disk&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;oracle_data_v2&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  count&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                 =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;disk-oracle-data-v2-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;index&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  location&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;             =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;location&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  resource_group_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;resource_group_name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  zone&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                 =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;1&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  storage_account_type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;PremiumV2_LRS&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  create_option&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Empty&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  disk_size_gb&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;         =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1250&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  disk_iops_read_write&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 20000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  disk_mbps_read_write&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 400&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt; # Size VM uncached limit &gt;= aggregate target&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_virtual_machine_data_disk_attachment&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;data_attach&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  count&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  managed_disk_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_managed_disk&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;oracle_data_v2[count&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;index]&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  virtual_machine_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;vm_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  lun&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; count&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  caching&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;None&quot;&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt; # Required to prevent ASM direct I/O bypass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-oracle-asm-diskgroup-creation&quot;&gt;3. Oracle ASM Diskgroup Creation&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Executed via SQL*Plus as SYSASM&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; DISKGROUP &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DATA&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; EXTERNAL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; REDUNDANCY&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  DISK&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;/dev/oracleasm/by-lun/lun-20&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;/dev/oracleasm/by-lun/lun-21&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;/dev/oracleasm/by-lun/lun-22&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;/dev/oracleasm/by-lun/lun-23&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ATTRIBUTE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;au_size&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;4M&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;compatible.asm&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;19.0.0.0.0&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;compatible.rdbms&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;19.0.0.0.0&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; DISKGROUP RECO &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;EXTERNAL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; REDUNDANCY&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  DISK&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;/dev/oracleasm/by-lun/lun-10&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ATTRIBUTE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;au_size&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;4M&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;compatible.asm&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;19.0.0.0.0&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;compatible.rdbms&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;19.0.0.0.0&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;4-kernel-level-block-tracing-with-ebpf&quot;&gt;4. Kernel-Level Block Tracing with eBPF&lt;/h3&gt;
&lt;p&gt;When AWR reports show elevated &lt;code&gt;db file sequential read&lt;/code&gt; or &lt;code&gt;log file parallel write&lt;/code&gt; times, operators can attach eBPF block-layer tracers such as &lt;code&gt;biolatency&lt;/code&gt; or &lt;code&gt;biosnoop&lt;/code&gt; (from the BCC / bpftrace toolset) on Linux database hosts to measure latency between block-device submission and completion, separating internal Oracle latch contention from cloud hypervisor throttling.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Abstracting storage across multiple clouds introduces failure modes at the hypervisor boundary, the ASM layer, and the query-offload layer.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2025-05-12-oracle-io-troubleshooting-exadata-aws-azure-oci-6-failures.svg&quot; alt=&quot;Where cloud Oracle storage breaks in production&quot;&gt;&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure Mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Silent VM-Level Throttling&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sum of disk IOPS or MB/s exceeds VM uncached ceiling; hypervisor queues I/O.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;db file sequential read&lt;/code&gt; spikes while disk metrics look idle; VM IOPS cap metric hits 100%.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pre-flight Terraform assertion: &lt;code&gt;sum(disk) &amp;#x3C;= vm_uncached_limit&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Shared Redo Queue Saturation&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Batch writes on shared &lt;code&gt;gp3&lt;/code&gt; or Premium v2 exhaust baseline 3,000 IOPS or queue depth.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;log file sync&lt;/code&gt; and &lt;code&gt;log file parallel write&lt;/code&gt; top AWR wait events.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Isolate &lt;code&gt;+RECO&lt;/code&gt; on dedicated &lt;code&gt;io2&lt;/code&gt; Block Express or Azure Ultra Disk.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Azure Host-Cache Corruption&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;ReadOnly&lt;/code&gt; or &lt;code&gt;ReadWrite&lt;/code&gt; caching on ASM disks breaks direct I/O write ordering on crash.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Block corruption errors (&lt;code&gt;ORA-01578&lt;/code&gt;) after VM host reboot.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce &lt;code&gt;caching = &quot;None&quot;&lt;/code&gt; via OPA/Sentinel policy on all ASM LUNs.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;ASM Rebalance IOPS Storm&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Disk replacement triggers ASM rebalance at high power, starving OLTP queries.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Elevated &lt;code&gt;V$ASM_OPERATION&lt;/code&gt; I/O alongside OLTP latency spikes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Throttle with &lt;code&gt;ALTER DISKGROUP DATA REBALANCE POWER 3&lt;/code&gt; off-peak.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Exadata Smart Scan Invalidation&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unoffloadable PL/SQL functions in &lt;code&gt;WHERE&lt;/code&gt; force fallback to block shipping.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Execution plan lacks &lt;code&gt;TABLE ACCESS STORAGE FULL&lt;/code&gt;; cell offload bytes drop.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Audit plans and rewrite predicates to use offload-eligible SQL.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;EBS Volume Detachment Latching&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Failover script reattaches EBS volume before dead instance releases it.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cloud API returns &lt;code&gt;VolumeInUseError&lt;/code&gt; during automated recovery.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Poll detachment state with exponential backoff before &lt;code&gt;attach_volume&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Cloud block storage cannot offload SQL predicates; full table scans on AWS EC2 or Azure VMs must ship every 8 KB block over the virtualized network, bounded by the VM’s sustained disk ceiling.&lt;/li&gt;
&lt;li&gt;Effective cloud storage performance is &lt;code&gt;min(sum of attached disk limits, VM uncached limit)&lt;/code&gt;; sizing disks without sizing the compute shape wastes provisioned IOPS.&lt;/li&gt;
&lt;li&gt;Isolating &lt;code&gt;+RECO&lt;/code&gt; onto dedicated &lt;code&gt;io2&lt;/code&gt; Block Express or Azure Ultra Disk eliminates &lt;code&gt;log file sync&lt;/code&gt; stalls caused by bulk &lt;code&gt;+DATA&lt;/code&gt; checkpoints and scans.&lt;/li&gt;
&lt;li&gt;Striping &lt;code&gt;+DATA&lt;/code&gt; across four &lt;code&gt;gp3&lt;/code&gt; or Premium SSD v2 volumes in ASM with &lt;code&gt;au_size = 4M&lt;/code&gt; and &lt;code&gt;EXTERNAL REDUNDANCY&lt;/code&gt; scales throughput cleanly without double-mirroring cloud-replicated storage.&lt;/li&gt;
&lt;li&gt;Azure &lt;code&gt;UltraSSD_LRS&lt;/code&gt; and &lt;code&gt;PremiumV2_LRS&lt;/code&gt; disks require an explicit availability &lt;code&gt;zone&lt;/code&gt; and &lt;code&gt;caching = &quot;None&quot;&lt;/code&gt; on every ASM attachment to prevent direct I/O ordering bugs.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Treating cloud VM block storage as equivalent to Exadata storage cells causes full-table-scan bottlenecks, silent hypervisor throttling, and &lt;code&gt;log file sync&lt;/code&gt; commit stalls.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build a Storage Compiler module in Terraform that enforces VM-to-disk bandwidth assertions, isolates &lt;code&gt;+RECO&lt;/code&gt; onto &lt;code&gt;io2&lt;/code&gt; or Ultra Disk, stripes &lt;code&gt;+DATA&lt;/code&gt; across four ASM LUNs, and routes heavy scan workloads to Exadata.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; After provisioning, run &lt;code&gt;DBMS_RESOURCE_MANAGER.CALIBRATE_IO&lt;/code&gt; and check AWR &lt;code&gt;log file parallel write&lt;/code&gt; and &lt;code&gt;db file sequential read&lt;/code&gt; wait histograms to confirm measured throughput matches the VM’s uncached ceiling without throttling.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your production cloud Oracle AWR reports this week; if &lt;code&gt;log file sync&lt;/code&gt; or &lt;code&gt;db file sequential read&lt;/code&gt; appears in the top five timed events, verify that &lt;code&gt;+RECO&lt;/code&gt; is isolated from &lt;code&gt;+DATA&lt;/code&gt; and that &lt;code&gt;sum(disk_mbps)&lt;/code&gt; does not exceed your VM shape’s uncached disk limit.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>When OpenSearch Vector Search Fails: Shard Sizing, Recall Loss, and Hot Nodes</title><link>https://rajivonai.com/blog/2026-06-24-opensearch-vector-search-failures-shards-recall-hot-nodes/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-24-opensearch-vector-search-failures-shards-recall-hot-nodes/</guid><description>OpenSearch vector search failure modes for operators, including shard count, hot shards, tenant skew, memory pressure, recall degradation, slow merges, filters, and recovery.</description><pubDate>Fri, 03 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The incident does not start with a red cluster. It starts with users saying search got worse. One tenant has slow queries, another gets empty results, a node is hot, merges are behind, and recall changed after a backfill. OpenSearch vector failures often look like relevance problems before they look like infrastructure problems.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;OpenSearch vector search fails through familiar distributed-system paths: wrong shard count, hot shards, uneven tenant distribution, memory pressure, merge backlog, poor filters, stale embeddings, and under-tested HNSW settings.&lt;/p&gt;
&lt;p&gt;The recovery playbook should separate availability, latency, recall, and security. A green cluster can still return poor candidates. A fast query can still miss relevant documents. A relevance regression can still be caused by shard distribution, memory pressure, or filters.&lt;/p&gt;
&lt;p&gt;OpenSearch vector internals and available metrics depend on version and deployment model — the k-NN engine in use (Faiss, Lucene, or the deprecated nmslib) changes which metrics and tuning parameters are relevant, and a self-managed cluster exposes different operational metrics than Amazon OpenSearch Service’s managed CloudWatch integration. Confirm which engine and deployment model is in play before applying troubleshooting steps from a mismatched context.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Search infrastructure teams are used to latency incidents, indexing backlogs, and shard imbalance. Vector search adds a quality dimension that can fail without an obvious outage.&lt;/p&gt;
&lt;p&gt;For RAG and semantic search, users notice missing context, irrelevant answers, and inconsistent results across tenants. Those symptoms require both search relevance debugging and infrastructure triage.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Vector search creates new ways for a cluster to be technically up and functionally wrong:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Candidate search can under-return after filters.&lt;/li&gt;
&lt;li&gt;Shard distribution can affect latency and candidate diversity.&lt;/li&gt;
&lt;li&gt;HNSW tuning can change recall.&lt;/li&gt;
&lt;li&gt;Memory pressure can make tail latency unstable.&lt;/li&gt;
&lt;li&gt;Segment merges can compete with query traffic.&lt;/li&gt;
&lt;li&gt;Backfills can change data distribution and hot nodes.&lt;/li&gt;
&lt;li&gt;Tenant skew can turn one customer into a cluster-wide problem.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The operational trap is treating every complaint as “the embedding model is bad.” Sometimes the model is fine and the search system is sick.&lt;/p&gt;
&lt;h2 id=&quot;core-technical-explanation&quot;&gt;Core Technical Explanation&lt;/h2&gt;
&lt;p&gt;An OpenSearch vector query fans out across shards, runs local candidate retrieval, combines results, and applies the rest of the query pipeline. If shards are uneven, filters are selective, or local candidate counts are too low, final results can degrade.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[vector query] --&gt; Fanout[fan out to shards]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Fanout --&gt; Hot[hot shard or node]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Fanout --&gt; Cold[normal shards]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Hot --&gt; LocalA[local candidates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Cold --&gt; LocalB[local candidates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    LocalA --&gt; Merge[global result merge]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    LocalB --&gt; Merge&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Merge --&gt; Filters[filters and ranking]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Filters --&gt; Results[user results]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Shard count controls how data and queries distribute. Too few shards can produce large recovery units and hot spots. Too many shards create overhead and fanout. The right answer depends on data volume, query rate, tenant skew, replicas, and node count.&lt;/p&gt;
&lt;p&gt;Recall loss can come from vector parameters, candidate counts, filters, stale embeddings, or uneven data. It is not one metric. Teams need a fixed query set and a way to compare results before and after changes.&lt;/p&gt;
&lt;p&gt;Hot nodes often come from shard placement, tenant skew, or query routing. A single large tenant or popular corpus can dominate one shard if routing and index design are naive.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Run incidents through a layered triage:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Layer 1: availability.&lt;/strong&gt; Cluster health, rejected requests, node failures, disk watermarks, and snapshot or allocation issues.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Layer 2: latency.&lt;/strong&gt; Search latency by route, shard, node, index, and query class. Separate lexical, vector, and hybrid traffic.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Layer 3: freshness.&lt;/strong&gt; Indexing lag, refresh behavior, backfill progress, and embedding version distribution.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Layer 4: recall.&lt;/strong&gt; Query-set results, under-return rate, empty-result rate, and known-good candidates missing from output.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Layer 5: security.&lt;/strong&gt; Tenant filters, entitlement filters, deletion filters, and negative tests.&lt;/p&gt;
&lt;p&gt;Do not rebalance, resize, reindex, and retune HNSW all at once. Preserve enough evidence to know which action fixed the problem.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;User symptom&lt;/th&gt;&lt;th&gt;Operator signal&lt;/th&gt;&lt;th&gt;Recovery&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Wrong shard count&lt;/td&gt;&lt;td&gt;Slow or inconsistent search&lt;/td&gt;&lt;td&gt;Fanout overhead or huge shards&lt;/td&gt;&lt;td&gt;Reindex with better shard plan&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hot shard&lt;/td&gt;&lt;td&gt;One tenant or corpus slow&lt;/td&gt;&lt;td&gt;Node-level skew&lt;/td&gt;&lt;td&gt;Split, route, or isolate workload&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Memory pressure&lt;/td&gt;&lt;td&gt;Tail latency spikes&lt;/td&gt;&lt;td&gt;Heap or native memory pressure&lt;/td&gt;&lt;td&gt;Resize, reduce load, tune workload&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Merge backlog&lt;/td&gt;&lt;td&gt;Latency worsens during indexing&lt;/td&gt;&lt;td&gt;Merge activity and I/O pressure&lt;/td&gt;&lt;td&gt;Throttle backfill or adjust refresh&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Recall degradation&lt;/td&gt;&lt;td&gt;Relevant docs disappear&lt;/td&gt;&lt;td&gt;Query-set regression&lt;/td&gt;&lt;td&gt;Tune candidates, filters, or index settings&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Poor filters&lt;/td&gt;&lt;td&gt;Empty or unauthorized results&lt;/td&gt;&lt;td&gt;High filter drop-off&lt;/td&gt;&lt;td&gt;Fix query builder and tests&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Stale embeddings&lt;/td&gt;&lt;td&gt;Old content ranks&lt;/td&gt;&lt;td&gt;Embedding lag by model version&lt;/td&gt;&lt;td&gt;Resume backfill or rollback route&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-and-tenancy-notes&quot;&gt;Security and Tenancy Notes&lt;/h2&gt;
&lt;p&gt;Security failures can masquerade as relevance fixes. Broadening filters to improve recall can accidentally expand the authorization boundary. Tenant, region, entitlement, deletion state, and compliance rules must remain hard constraints during incident response.&lt;/p&gt;
&lt;p&gt;For multi-tenant workloads, keep hot-tenant remediation separate from authorization. Moving a tenant to a new index, shard strategy, or domain should preserve query-time filters and audit behavior.&lt;/p&gt;
&lt;h2 id=&quot;cost-notes&quot;&gt;Cost Notes&lt;/h2&gt;
&lt;p&gt;Operational recovery has cost:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Reindexing consumes compute and storage.&lt;/li&gt;
&lt;li&gt;Extra replicas increase spend.&lt;/li&gt;
&lt;li&gt;Larger nodes may hide bad shard design.&lt;/li&gt;
&lt;li&gt;Isolated tenant indexes or domains multiply baseline cost.&lt;/li&gt;
&lt;li&gt;Higher candidate counts improve recall but raise query CPU.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The cheapest fix is often query and shard discipline. The most expensive fix is repeatedly scaling the cluster because no one wants to reindex.&lt;/p&gt;
&lt;h2 id=&quot;observability-notes&quot;&gt;Observability Notes&lt;/h2&gt;
&lt;p&gt;Useful dashboards include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Latency by route: lexical, vector, hybrid.&lt;/li&gt;
&lt;li&gt;Latency by index, shard, and node.&lt;/li&gt;
&lt;li&gt;Query count and error rate by tenant.&lt;/li&gt;
&lt;li&gt;Candidate count and final result count.&lt;/li&gt;
&lt;li&gt;Empty-result and under-return rate.&lt;/li&gt;
&lt;li&gt;Segment count and merge pressure.&lt;/li&gt;
&lt;li&gt;Refresh and indexing lag.&lt;/li&gt;
&lt;li&gt;Embedding model-version distribution.&lt;/li&gt;
&lt;li&gt;Hot shard and disk watermark views.&lt;/li&gt;
&lt;li&gt;Query-set recall checks.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Observability should connect symptoms to ownership. Search relevance, platform, database, and application teams need the same incident vocabulary.&lt;/p&gt;
&lt;h2 id=&quot;backup-restore-and-dr-notes&quot;&gt;Backup, Restore, and DR Notes&lt;/h2&gt;
&lt;p&gt;Some failures require reindexing rather than restore. A wrong shard count or bad mapping usually cannot be fixed in place. The runbook should include creating a replacement index, replaying data, validating relevance, and switching aliases.&lt;/p&gt;
&lt;p&gt;Snapshots matter when the cluster or index is lost. Rebuild matters when the index design is wrong. Production DR needs both paths.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Is the issue availability, latency, recall, security, or freshness?&lt;/li&gt;
&lt;li&gt;Which tenants and query classes are affected?&lt;/li&gt;
&lt;li&gt;Are hot shards or nodes visible?&lt;/li&gt;
&lt;li&gt;Did a backfill, mapping change, model change, or shard change happen recently?&lt;/li&gt;
&lt;li&gt;Are filters dropping most vector candidates?&lt;/li&gt;
&lt;li&gt;Does exact or higher-recall validation find the missing documents?&lt;/li&gt;
&lt;li&gt;Can the fix be done with settings, or does it require reindexing?&lt;/li&gt;
&lt;li&gt;Is rollback an alias switch?&lt;/li&gt;
&lt;li&gt;Does recovery preserve tenant boundaries?&lt;/li&gt;
&lt;li&gt;What metric proves the incident is resolved?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; OpenSearch vector search can be technically up (green cluster, no errors) while functionally wrong — degraded recall, hot shards, or stale embeddings show up as “search got worse” complaints, not outages.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Triage in layers — availability, latency, freshness, recall, security — rather than jumping straight to “the embedding model is bad,” and preserve evidence before rebalancing, resizing, reindexing, and retuning HNSW all at once.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; A fixed query set shows recall back at baseline after remediation, and the specific layer (shard, memory, filter, or freshness) that caused the regression is identified, not just papered over by scaling nodes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, confirm recall is tracked as its own SRE signal (query-set regression, not just latency and error rate), since a green cluster with degraded recall won’t show up in typical uptime dashboards.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;OpenSearch vector search and k-NN documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/&quot;&gt;https://docs.opensearch.org/latest/vector-search/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch k-NN methods and engines (Faiss/Lucene/nmslib): &lt;a href=&quot;https://docs.opensearch.org/latest/mappings/supported-field-types/knn-methods-engines/&quot;&gt;https://docs.opensearch.org/latest/mappings/supported-field-types/knn-methods-engines/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch cluster and index management documentation (shards, allocation): &lt;a href=&quot;https://docs.opensearch.org/latest/im-plugin/&quot;&gt;https://docs.opensearch.org/latest/im-plugin/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch monitoring and performance analyzer documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/monitoring-your-cluster/&quot;&gt;https://docs.opensearch.org/latest/monitoring-your-cluster/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Amazon OpenSearch Service operational metrics (if using the managed service): &lt;a href=&quot;https://docs.aws.amazon.com/opensearch-service/latest/developerguide/managedomains-cloudwatchmetrics.html&quot;&gt;https://docs.aws.amazon.com/opensearch-service/latest/developerguide/managedomains-cloudwatchmetrics.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>failures</category><category>architecture</category><category>checklist</category></item><item><title>Azure Oracle Storage Planes: Automated ASM Orchestration via Immutable Compute</title><link>https://rajivonai.com/blog/2025-03-20-azure-oracle-vm-factory-terraform-ansible-asm/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-03-20-azure-oracle-vm-factory-terraform-ansible-asm/</guid><description>Why procedural Ansible playbooks destroy production database storage during cloud hypervisor recovery events, and how to build an autonomous cloud-init bootstrap agent that safely discovers and mounts Oracle ASM diskgroups.</description><pubDate>Thu, 02 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Treating cloud virtual machines and persistent storage volumes as tightly coupled infrastructure is a critical reliability failure; in cloud database architectures, compute instances must be disposable while persistent Oracle ASM storage remains self-describing and immune to procedural Day 1 automation.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Procedural Ansible Day 1 playbooks assume attached disks are blank and rely on volatile Linux &lt;code&gt;/dev/sdX&lt;/code&gt; names, so re-running them after a hypervisor replacement overwrites Oracle ASM Block 0 headers and destroys production diskgroups.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Bake Oracle Grid Infrastructure and Database binaries into an immutable Azure Compute Gallery image, bind disks by hardware SCSI LUN via &lt;code&gt;udev&lt;/code&gt;, and run a fail-closed &lt;code&gt;cloud-init&lt;/code&gt; bootstrap agent that inspects &lt;code&gt;kfed&lt;/code&gt; Block 0 headers before choosing Day 1 creation, Day 2 mount, or quarantine.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Partial LUN attachments during Azure VM replacement or stale &lt;code&gt;udev&lt;/code&gt; races can trigger false blank-disk detection unless the agent verifies expected LUN counts and runs &lt;code&gt;udevadm settle&lt;/code&gt; first.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2025-03-20-azure-oracle-vm-factory-terraform-ansible-asm.svg&quot; alt=&quot;Verify ASM headers before touching any block device&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Enterprise engineering teams deploying Oracle Database onto Microsoft Azure Infrastructure-as-a-Service (IaaS) routinely use Oracle Automatic Storage Management (ASM) to stripe database files across raw block devices without OS filesystem overhead. To automate provisioning, platform teams typically pair Terraform with procedural Ansible playbooks:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Infrastructure Provisioning (Terraform):&lt;/strong&gt; Terraform deploys a constrained-core Azure VM (such as &lt;code&gt;Standard_E32-8s_v5&lt;/code&gt;) and attaches multiple Azure Managed Disks (&lt;code&gt;PremiumV2_LRS&lt;/code&gt; or &lt;code&gt;UltraSSD_LRS&lt;/code&gt;) across dedicated SCSI Logical Unit Numbers (LUNs) for &lt;code&gt;+DATA&lt;/code&gt; and &lt;code&gt;+RECO&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Procedural Configuration (Ansible):&lt;/strong&gt; An Ansible playbook connects over SSH to the new VM, iterates over &lt;code&gt;/dev/sd*&lt;/code&gt; block devices, runs &lt;code&gt;parted&lt;/code&gt; or &lt;code&gt;fdisk&lt;/code&gt; to initialize partition tables, writes &lt;code&gt;udev&lt;/code&gt; rules for &lt;code&gt;grid:asmadmin&lt;/code&gt; ownership, installs Grid Infrastructure, initializes ASM disks, and runs DBCA to create the database.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;During initial green-field staging deployments, every disk is genuinely blank, so the procedural pipeline succeeds cleanly. The hidden defect is that the automation encodes &lt;strong&gt;Day 1 assumptions&lt;/strong&gt; into a &lt;strong&gt;Day 2 lifecycle&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure surfaces during the first unplanned &lt;strong&gt;Day 2 Azure host hardware fault&lt;/strong&gt; or automated VM replacement.&lt;/p&gt;
&lt;p&gt;Cloud virtual machines are transient compute shells. Physical host hardware faults, uncorrectable DIMM ECC errors, or live-migration failures terminate the VM abruptly, while the attached Azure Managed Disks—persisted independently on Azure Storage clusters—remain intact and contain production data. When self-healing automation replaces the failed VM and reattaches the existing Managed Disks, four concrete constraints break procedural Day 1 playbooks:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Constraint 1 — Block 0 Header Destruction:&lt;/strong&gt; Procedural playbooks that execute &lt;code&gt;parted --script /dev/sdc mklabel gpt&lt;/code&gt; or &lt;code&gt;dd&lt;/code&gt; unconditionally overwrite Block 0 of the ASM disk—destroying the &lt;code&gt;kfbh&lt;/code&gt; block header and &lt;code&gt;kfdhdb&lt;/code&gt; disk header (&lt;code&gt;ORCLDISK&lt;/code&gt; provisioning string and allocation unit pointers). While ASM mirrors a backup copy of the disk header in the second Allocation Unit (AU 1, block 254), &lt;code&gt;parted&lt;/code&gt; or &lt;code&gt;mkfs&lt;/code&gt; writes can also clobber adjacent metadata or trigger immediate diskgroup mount failure, forcing a multi-terabyte RMAN restore from backup.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 2 — Non-Deterministic Linux &lt;code&gt;/dev/sdX&lt;/code&gt; Enumeration:&lt;/strong&gt; The Linux kernel assigns &lt;code&gt;/dev/sd*&lt;/code&gt; letters sequentially as SCSI targets respond during PCI/hv_storvsc bus enumeration, so assignments are not guaranteed across reboots or VM replacements. A volume attached at LUN 10 (&lt;code&gt;+DATA&lt;/code&gt;) on the original host can enumerate under a different &lt;code&gt;/dev/sd*&lt;/code&gt; letter on the replacement host, causing hardcoded-letter scripts to format the wrong LUN or mix &lt;code&gt;+DATA&lt;/code&gt; and &lt;code&gt;+RECO&lt;/code&gt; members.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 3 — Partial LUN Attachment Windows and Detach Leases:&lt;/strong&gt; During automated VM replacement, Azure’s compute control plane must detach Managed Disks from the failed VM before attaching them to the replacement VM. If the replacement VM boots while one Managed Disk is still detaching, only 3 of 4 &lt;code&gt;+DATA&lt;/code&gt; LUNs appear in sysfs at &lt;code&gt;cloud-init&lt;/code&gt; startup—and in normal-redundancy diskgroups or external-redundancy configurations, missing a member disk prevents the diskgroup from mounting cleanly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 4 — &lt;code&gt;ASMCMD&lt;/code&gt; and Tooling Syntax Traps:&lt;/strong&gt; Scripts written from memory often fail on real Oracle 19c binaries—for example, &lt;code&gt;asmcmd&lt;/code&gt; creates diskgroups via &lt;code&gt;asmcmd mkdg &amp;#x3C;config.xml&gt;&lt;/code&gt; (not &lt;code&gt;createdg&lt;/code&gt;), and &lt;code&gt;asmcmd&lt;/code&gt; expects the ASM instance and diskgroups to be mounted via SQL (&lt;code&gt;ALTER DISKGROUP ... MOUNT&lt;/code&gt;) or &lt;code&gt;srvctl&lt;/code&gt;, not standalone &lt;code&gt;asmcmd mount&lt;/code&gt; on a stopped Oracle Restart stack. Similarly, initializing a multi-terabyte diskgroup with the default &lt;code&gt;1M&lt;/code&gt; Allocation Unit (&lt;code&gt;AU_SIZE&lt;/code&gt;) rather than &lt;code&gt;4M&lt;/code&gt; increases ASM extent-map memory pressure in the ASM shared pool.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2025-03-20-azure-oracle-vm-factory-terraform-ansible-asm-2-constraints.svg&quot; alt=&quot;Day 2 VM replacement destroys data if scripts assume blank disks&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do platform teams architect an automated Azure Oracle VM factory that treats compute as disposable while guaranteeing zero destructive writes against reattached ASM Managed Disks?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams evaluating Day 1 and Day 2 Oracle storage orchestration on Azure typically consider four approaches.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Device Identity&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Day 2 Rebuild Safety&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Recovery Speed&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Why Chosen or Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Ansible Day-1 SSH Playbook&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;/dev/sdX&lt;/code&gt; letters&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unsafe (overwrites headers)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fast until data loss&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected: Procedural script assumes blank disks; destroys Block 0 on VM replacement.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Terraform &lt;code&gt;remote-exec&lt;/code&gt; Provisioner&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;/dev/sdX&lt;/code&gt; or LUN&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fragile (runs on create)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Moderate&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected: Requires SSH from CI runner and breaks idempotency on VM taint/replace.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Manual DBA Storage Attach&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;udev&lt;/code&gt; + human check&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Safe (human inspects)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Slow (manual paging)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected: Requires DBA intervention for every Azure host hardware replacement.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Golden Image + &lt;code&gt;cloud-init&lt;/code&gt; &lt;code&gt;kfed&lt;/code&gt; Gate&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Hardware SCSI LUN&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fail-closed consensus&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Automated&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen:&lt;/strong&gt; Separates immutable compute from persistent LUNs with zero-write quarantine.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Ansible SSH playbooks and Terraform &lt;code&gt;remote-exec&lt;/code&gt; provisioners both couple storage initialization to VM creation events. Even if you add a guard flag like &lt;code&gt;create_asm: false&lt;/code&gt; in Ansible variables, a single misconfigured inventory run or forgotten flag during an emergency rebuild wipes production headers. Manual DBA recovery protects data but turns routine cloud host replacement into a high-toil incident.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2025-03-20-azure-oracle-vm-factory-terraform-ansible-asm-3-options.svg&quot; alt=&quot;Compare four Oracle VM storage orchestration patterns&quot;&gt;&lt;/p&gt;
&lt;p&gt;Packaging Oracle binaries into an &lt;strong&gt;Immutable Golden Image&lt;/strong&gt; and pushing storage discovery into a local, fail-closed &lt;strong&gt;&lt;code&gt;cloud-init&lt;/code&gt; bootstrap agent&lt;/strong&gt; removes both human error and remote SSH fragility. The agent derives truth exclusively from the attached disks’ Block 0 headers and hardware SCSI LUN IDs.&lt;/p&gt;
&lt;h2 id=&quot;the-autonomous-storage-discovery-architecture&quot;&gt;The Autonomous Storage Discovery Architecture&lt;/h2&gt;
&lt;p&gt;The architecture separates the database host into three clean layers: an &lt;strong&gt;Ephemeral Compute Layer&lt;/strong&gt; built from an Azure Compute Gallery golden image, a &lt;strong&gt;Deterministic Mapping Layer&lt;/strong&gt; driven by &lt;code&gt;udev&lt;/code&gt; SCSI LUN rules, and a &lt;strong&gt;Persistent Storage Layer&lt;/strong&gt; on Azure Managed Disks.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Immutable Golden Image (Azure Compute Gallery):&lt;/strong&gt; OS hardening, kernel parameters (&lt;code&gt;sysctl.conf&lt;/code&gt;), hugepages, Oracle Grid Infrastructure 19c, and Oracle Database 19c software-only homes are baked into a versioned Azure Compute Gallery image using Packer. Root OS disks hold zero database state and can be replaced at any time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Deterministic SCSI LUN Mapping (&lt;code&gt;/dev/oracleasm/by-lun/lun-*&lt;/code&gt;):&lt;/strong&gt; Terraform assigns fixed LUN ranges (&lt;code&gt;lun = 10..19&lt;/code&gt; for &lt;code&gt;+DATA&lt;/code&gt;, &lt;code&gt;lun = 20..29&lt;/code&gt; for &lt;code&gt;+RECO&lt;/code&gt;). At boot, a &lt;code&gt;udev&lt;/code&gt; generator reads &lt;code&gt;/sys/block/sd*/device/scsi_device/&lt;/code&gt; to extract the hardware SCSI LUN number and creates stable symlinks owned by &lt;code&gt;grid:asmadmin&lt;/code&gt; with mode &lt;code&gt;0660&lt;/code&gt;. On Azure VMs where &lt;code&gt;/dev/sda&lt;/code&gt; is the OS disk and &lt;code&gt;/dev/sdb&lt;/code&gt; may be a temporary resource disk (depending on VM series—&lt;code&gt;Esv5&lt;/code&gt; without local temp disks does not always expose &lt;code&gt;/dev/sdb&lt;/code&gt; as temp), filtering by &lt;code&gt;LUN &gt;= 10&lt;/code&gt; is far safer than hardcoding &lt;code&gt;sda&lt;/code&gt;/&lt;code&gt;sdb&lt;/code&gt; exclusions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Header Inspection via &lt;code&gt;kfed&lt;/code&gt; and &lt;code&gt;blkid&lt;/code&gt;:&lt;/strong&gt; The bootstrap agent inspects every expected LUN device using Oracle’s &lt;code&gt;kfed read &amp;#x3C;device&gt;&lt;/code&gt; utility (checking for &lt;code&gt;kfbh.type: KFBTYP_DISKHEAD&lt;/code&gt; and &lt;code&gt;kfdhdb.hdrsts: KFDHDR_MEMBER&lt;/code&gt; with &lt;code&gt;ORCLDISK&lt;/code&gt; in &lt;code&gt;kfdhdb.driver.provstr&lt;/code&gt;) alongside &lt;code&gt;blkid -p&lt;/code&gt; (probe bypassing the udev cache) to detect foreign filesystems (&lt;code&gt;ext4&lt;/code&gt;, &lt;code&gt;xfs&lt;/code&gt;, &lt;code&gt;LVM2_member&lt;/code&gt;). Using &lt;code&gt;kfed&lt;/code&gt; directly avoids brittle raw byte-offset assumptions across partitioned versus unpartitioned LUNs, while &lt;code&gt;blkid -p&lt;/code&gt; ensures stale udev cache entries from an earlier boot never mask an existing signature.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fail-Closed Atomic Consensus Gate:&lt;/strong&gt; Every expected LUN listed in the VM’s &lt;code&gt;ExpectedLuns&lt;/code&gt; metadata must be present and must agree on the same state before any write occurs:
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;All LUNs have valid ASM headers:&lt;/strong&gt; Route to &lt;strong&gt;Day 2 Recovery&lt;/strong&gt; (&lt;code&gt;crsctl start has&lt;/code&gt;, &lt;code&gt;srvctl start asm&lt;/code&gt;, &lt;code&gt;srvctl start diskgroup&lt;/code&gt;, and &lt;code&gt;srvctl start database&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;All LUNs are completely unformatted (zero ASM, partition, or filesystem signatures):&lt;/strong&gt; Route to &lt;strong&gt;Day 1 Provisioning&lt;/strong&gt; (&lt;code&gt;CREATE DISKGROUP ... ATTRIBUTE &apos;au_size&apos;=&apos;4M&apos;&lt;/code&gt; via &lt;code&gt;sqlplus / as sysasm&lt;/code&gt; or &lt;code&gt;asmcmd mkdg&lt;/code&gt; XML).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Any mixed, partial, or foreign state:&lt;/strong&gt; Route to &lt;strong&gt;Emergency Quarantine Halt&lt;/strong&gt;—exit non-zero, leave the database stopped, emit a P1 alert, and modify zero disk blocks. If a single disk’s primary Block 0 was damaged but AU 1 block 254 remains intact, operators can inspect and repair it manually with &lt;code&gt;kfed&lt;/code&gt; rather than letting automation overwrite the group.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2025-03-20-azure-oracle-vm-factory-terraform-ansible-asm-4-architecture.svg&quot; alt=&quot;Decouple immutable compute from persistent SCSI LUN storage&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Deploying autonomous ASM discovery into an existing estate should follow a staged rollout so legacy hosts are never disrupted.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Bake Golden Image and Validate LUN Mapping:&lt;/strong&gt; Build the Packer image with GI 19c and Database 19c binaries, deploy a sandbox VM with &lt;code&gt;PremiumV2_LRS&lt;/code&gt; disks on LUNs 10–19 and 20–29, and verify &lt;code&gt;/dev/oracleasm/by-lun/lun-*&lt;/code&gt; symlinks match Azure LUNs. &lt;em&gt;Validation gate:&lt;/em&gt; &lt;code&gt;kfed&lt;/code&gt; and &lt;code&gt;udevadm info&lt;/code&gt; confirm &lt;code&gt;grid:asmadmin&lt;/code&gt; ownership on every expected LUN. &lt;em&gt;Rollback:&lt;/em&gt; Destroy sandbox VM; production untouched.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Dry-Run Discovery on Existing Hosts:&lt;/strong&gt; Run the header-inspection script in read-only &lt;code&gt;--dry-run&lt;/code&gt; mode against staging VMs to verify it classifies existing ASM disks as &lt;code&gt;Day 2 Recovery&lt;/code&gt; with 100% accuracy. &lt;em&gt;Validation gate:&lt;/em&gt; Zero false-negative &lt;code&gt;empty&lt;/code&gt; classifications across 10 consecutive reboots. &lt;em&gt;Rollback:&lt;/em&gt; Remove dry-run script; no storage changes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Enable Day 2 Auto-Mount on Standby Nodes:&lt;/strong&gt; Replace procedural Ansible post-boot hooks on a Data Guard physical standby with the &lt;code&gt;cloud-init&lt;/code&gt; bootstrap agent, then taint and replace the standby VM in Terraform. &lt;em&gt;Validation gate:&lt;/em&gt; Replacement standby VM boots, discovers &lt;code&gt;+DATA&lt;/code&gt; and &lt;code&gt;+RECO&lt;/code&gt;, mounts ASM, and resumes Redo Apply automatically. &lt;em&gt;Rollback:&lt;/em&gt; Reattach disks to a manual recovery VM using the prior runbook.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Enforce Fail-Closed Quarantine and Decommission Day-1 Playbooks:&lt;/strong&gt; Remove destructive &lt;code&gt;parted&lt;/code&gt;/&lt;code&gt;mkfs&lt;/code&gt; tasks from Ansible and CI/CD pipelines, and wire the bootstrap agent’s exit-code &lt;code&gt;2&lt;/code&gt; alert into PagerDuty/Azure Monitor. &lt;em&gt;Validation gate:&lt;/em&gt; Simulated partial-LUN attach triggers Quarantine Halt with zero block writes. &lt;em&gt;Rollback:&lt;/em&gt; Re-enable manual DBA intervention gate in Terraform.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2025-03-20-azure-oracle-vm-factory-terraform-ansible-asm-5-flow.svg&quot; alt=&quot;Fail-closed consensus gate routes Day 1, Day 2, or Quarantine&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;I have not published internal production telemetry from this factory, but the documented implementation pattern combines Terraform LUN pinning, &lt;code&gt;udev&lt;/code&gt; SCSI symlink generation, and a &lt;code&gt;kfed&lt;/code&gt;-backed Python state engine.&lt;/p&gt;
&lt;h3 id=&quot;1-terraform-immutable-vm-and-disk-attachment&quot;&gt;1. Terraform Immutable VM and Disk Attachment&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_linux_virtual_machine&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;oracle_node&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;vm-oradb-prod-01&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  resource_group_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;resource_group_name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  location&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;location&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  zone&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;availability_zone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  size&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Standard_E32-8s_v5&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  admin_username&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;azureuser&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  network_interface_ids&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [azurerm_network_interface&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;oracle_nic&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  source_image_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;oracle_golden_image_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  custom_data&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;           =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; filebase64&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;path&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/scripts/bootstrap_asm.sh&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  os_disk&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    caching&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;ReadWrite&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    storage_account_type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Premium_LRS&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tags&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Workload     &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Oracle&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DatabaseName &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;PRODDB&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ExpectedLuns &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;10,11,20&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_virtual_machine_data_disk_attachment&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;data_disk_01&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  managed_disk_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_managed_disk&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;asm_data_01&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  virtual_machine_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; azurerm_linux_virtual_machine&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;oracle_node&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  lun&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  caching&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;None&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-deterministic-lun-to-udev-rule-generator-generate_asm_udevsh&quot;&gt;2. Deterministic LUN-to-Udev Rule Generator (&lt;code&gt;generate_asm_udev.sh&lt;/code&gt;)&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -euo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pipefail&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;UDEV_RULE_FILE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/etc/udev/rules.d/99-oracle-asmdevices.rules&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;UDEV_RULE_FILE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dev_path &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /sys/block/sd*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  [ &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-e&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;dev_path&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; continue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  dev_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;basename&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;dev_path&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  scsi_dir&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;dev_path&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/device/scsi_device/&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  [ &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-e&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scsi_dir&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; continue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  lun_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;basename&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scsi_dir&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; awk&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -F&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;:&apos;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;{print $4}&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;  # Only bind LUNs &gt;= 10 reserved for Oracle ASM (+DATA / +RECO)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [ &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;lun_id&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; -lt&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ]; &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;then&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    continue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  fi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;SUBSYSTEM==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;block&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;, KERNEL==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;dev_name&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;, ENV{DEVTYPE}==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;disk&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;, SYMLINK+=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;oracleasm/by-lun/lun-${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;lun_id&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;, OWNER=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;grid&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;, GROUP=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;asmadmin&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;, MODE=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;0660&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;UDEV_RULE_FILE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;done&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;udevadm&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; control&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --reload-rules&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;udevadm&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; trigger&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --subsystem-match=block&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;udevadm&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; settle&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --timeout=30&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-autonomous-state-engine-bootstrap_agentpy&quot;&gt;3. Autonomous State Engine (&lt;code&gt;bootstrap_agent.py&lt;/code&gt;)&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env python3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; glob, logging, subprocess, sys&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;logging.basicConfig(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;level&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;logging.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;INFO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%(asctime)s&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%(levelname)s&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%(message)s&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;KFED_BIN&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;/u01/app/19.0.0.0/grid/bin/kfed&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; check_asm_header&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(device_path):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;&quot;&quot;Verify ASM disk header via kfed rather than raw byte offsets.&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    res &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; subprocess.run([&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;KFED_BIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;read&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, device_path], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;capture_output&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; res.returncode &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; False&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;KFBTYP_DISKHEAD&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; res.stdout &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;ORCLDISK&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; res.stdout&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; is_device_empty&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(device_path):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;&quot;&quot;Confirm zero filesystem, LVM, or partition signatures via blkid.&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    res &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; subprocess.run([&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;blkid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-p&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, device_path], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;capture_output&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; res.returncode &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; and&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; res.stdout.strip() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; execute_day2_recovery&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subprocess.run([&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/u01/app/19.0.0.0/grid/bin/crsctl&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;start&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;has&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;check&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subprocess.run([&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;su&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;grid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-c&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;srvctl start asm&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;check&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subprocess.run([&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;su&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;grid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-c&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;srvctl start diskgroup -g DATA&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;check&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subprocess.run([&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;su&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;grid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-c&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;srvctl start diskgroup -g RECO&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;check&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subprocess.run([&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;su&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;oracle&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-c&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;srvctl start database -d PRODDB&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;check&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    devices &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; sorted&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(glob.glob(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/dev/oracleasm/by-lun/lun-*&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; devices:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        logging.error(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;No Oracle ASM LUNs found in /dev/oracleasm/by-lun/.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sys.exit(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    asm_flags &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [check_asm_header(d) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; d &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; devices]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    empty_flags &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [is_device_empty(d) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; d &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; devices]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; all&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(asm_flags):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        logging.info(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;All LUNs carry valid ASM headers. Running Day 2 recovery.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        execute_day2_recovery()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    elif&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; all&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(empty_flags):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        logging.info(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;All LUNs are completely blank. Ready for Day 1 CREATE DISKGROUP.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;        # Invoke SQL*Plus SYSASM or asmcmd mkdg XML with AU_SIZE 4M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        logging.critical(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;FAIL-CLOSED: Mixed or foreign disk state detected. Halting.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sys.exit(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; __name__&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    main()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note also two documented architectural details when operating this pattern. First, keep the database &lt;code&gt;SPFILE&lt;/code&gt; and &lt;code&gt;orapw&lt;/code&gt; password file inside &lt;code&gt;+DATA&lt;/code&gt; (or fetch &lt;code&gt;orapw${ORACLE_SID}&lt;/code&gt; from Azure Key Vault at boot) and store only a one-line &lt;code&gt;init${ORACLE_SID}.ora&lt;/code&gt; pointer (&lt;code&gt;SPFILE=&apos;+DATA/PRODDB/PARAMETERFILE/spfilePRODDB.ora&apos;&lt;/code&gt;) in &lt;code&gt;$ORACLE_HOME/dbs&lt;/code&gt;; otherwise, an ephemeral replacement VM loses &lt;code&gt;ALTER SYSTEM&lt;/code&gt; parameter changes and Data Guard &lt;code&gt;SYS&lt;/code&gt; authentication. Second, for workloads that do not require self-managed IaaS VMs, &lt;strong&gt;Oracle Database@Azure&lt;/strong&gt; runs OCI Exadata infrastructure inside Azure data centers and eliminates self-managed Linux &lt;code&gt;udev&lt;/code&gt; and ASM diskgroup bootstrapping entirely.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Moving storage orchestration into a host-level &lt;code&gt;cloud-init&lt;/code&gt; agent eliminates remote Ansible wipe risks, but introduces local boot-time race conditions and Azure control-plane edge cases.&lt;/p&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Transient read timeout on Block 0&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;kfed read&lt;/code&gt; fails on one LUN during boot; if treated as blank, could misclassify disk.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;kfed&lt;/code&gt; stderr I/O timeout in &lt;code&gt;/var/log/cloud-init-output.log&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Require explicit &lt;code&gt;blkid -p&lt;/code&gt; exit code &lt;code&gt;2&lt;/code&gt; (no signature found) before classifying any disk as blank, and retry &lt;code&gt;kfed&lt;/code&gt; 3 times.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Partial LUN attachment on VM replace&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Azure attaches 3 of 4 &lt;code&gt;+DATA&lt;/code&gt; disks while the 4th is still detaching from the dead host.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Expected LUN list in VM tags (&lt;code&gt;ExpectedLuns&lt;/code&gt;) does not match &lt;code&gt;/dev/oracleasm/by-lun/&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Assert exact LUN count and IDs against &lt;code&gt;ExpectedLuns&lt;/code&gt; metadata before running header consensus.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;udev&lt;/code&gt; symlink race at boot&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;bootstrap_agent.py&lt;/code&gt; runs before &lt;code&gt;systemd-udevd&lt;/code&gt; finishes creating &lt;code&gt;/dev/oracleasm/by-lun/lun-*&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Agent exits &lt;code&gt;1&lt;/code&gt; with “No Oracle ASM LUNs found” immediately after boot.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Order systemd unit &lt;code&gt;After=systemd-udevd.service&lt;/code&gt; and run &lt;code&gt;udevadm settle --timeout=30&lt;/code&gt; before Python starts.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Split-brain concurrent disk attach&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Shared disk or delayed detach lets two VMs mount the same ASM diskgroup simultaneously.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;ASM alert log &lt;code&gt;ORA-15020&lt;/code&gt; or block corruption across instances.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Confirm the failed VM is fully &lt;code&gt;Deallocated&lt;/code&gt; / deleted before attaching disks to the replacement VM.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;IMDS rate limiting on fleet reboot&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Simultaneous host reboots hit Azure IMDS (&lt;code&gt;169.254.169.254&lt;/code&gt;) HTTP 429 limits if querying metadata APIs.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;HTTP 429 responses in bootstrap logs.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pass static expected LUN metadata via &lt;code&gt;custom_data&lt;/code&gt; or retry IMDS queries with exponential backoff.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Wrong &lt;code&gt;au_size&lt;/code&gt; on Day 1 build&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Creating &lt;code&gt;+DATA&lt;/code&gt; with default &lt;code&gt;1M&lt;/code&gt; AU size instead of &lt;code&gt;4M&lt;/code&gt; bloats ASM shared pool memory on large DBs.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;SELECT name, allocation_unit_size FROM v$asm_diskgroup;&lt;/code&gt; shows &lt;code&gt;1048576&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Hardcode &lt;code&gt;ATTRIBUTE &apos;au_size&apos;=&apos;4M&apos;&lt;/code&gt; in Day 1 &lt;code&gt;CREATE DISKGROUP&lt;/code&gt; SQL / &lt;code&gt;asmcmd mkdg&lt;/code&gt; XML.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Missing password file on golden image&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Replacement VM boots without the instance-specific &lt;code&gt;orapwPRODDB&lt;/code&gt; file, breaking Data Guard redo transport.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;ORA-16191&lt;/code&gt; in primary alert log during standby recovery.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fetch &lt;code&gt;orapw${ORACLE_SID}&lt;/code&gt; from Azure Key Vault via Managed Identity during &lt;code&gt;cloud-init&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2025-03-20-azure-oracle-vm-factory-terraform-ansible-asm-6-failures.svg&quot; alt=&quot;Where autonomous ASM discovery breaks in production&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Never run procedural Day 1 formatting tools (&lt;code&gt;parted&lt;/code&gt;, &lt;code&gt;fdisk&lt;/code&gt;, &lt;code&gt;mkfs&lt;/code&gt;) against cloud block devices without first inspecting Block 0 headers; on a Day 2 VM replacement, those scripts destroy live Oracle ASM metadata.&lt;/li&gt;
&lt;li&gt;Linux &lt;code&gt;/dev/sdX&lt;/code&gt; device letters are non-deterministic across reboots and Azure VM replacements; bind ASM devices strictly by hardware SCSI LUN IDs using &lt;code&gt;udev&lt;/code&gt; rules (&lt;code&gt;/dev/oracleasm/by-lun/lun-*&lt;/code&gt;) and filter on reserved LUN ranges (&lt;code&gt;&gt;= 10&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Use Oracle’s &lt;code&gt;kfed read&lt;/code&gt; (&lt;code&gt;KFBTYP_DISKHEAD&lt;/code&gt; / &lt;code&gt;ORCLDISK&lt;/code&gt;) paired with &lt;code&gt;blkid -p&lt;/code&gt; to enforce a fail-closed consensus gate: mount only if 100% of expected LUNs carry ASM headers, initialize only if 100% are blank, and halt in quarantine on any mismatch.&lt;/li&gt;
&lt;li&gt;Verify real Oracle 19c CLI semantics in automation: &lt;code&gt;asmcmd&lt;/code&gt; uses &lt;code&gt;mkdg &amp;#x3C;xml&gt;&lt;/code&gt; (not &lt;code&gt;createdg&lt;/code&gt;), and Oracle Restart mounts diskgroups via &lt;code&gt;srvctl start diskgroup&lt;/code&gt; or SQL &lt;code&gt;ALTER DISKGROUP ... MOUNT&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Always verify expected LUN counts against VM metadata and run &lt;code&gt;udevadm settle --timeout=30&lt;/code&gt; before scanning, because partial Azure disk attachments and &lt;code&gt;udev&lt;/code&gt; races are the most common Day 2 boot failures.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Procedural Day 1 Ansible playbooks that assume blank disks and hardcoded &lt;code&gt;/dev/sdX&lt;/code&gt; names destroy Oracle ASM Block 0 headers when cloud automation replaces a failed Azure VM.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Move Oracle binaries into an immutable Azure Compute Gallery golden image, bind disks by hardware SCSI LUN via &lt;code&gt;udev&lt;/code&gt;, and run a fail-closed &lt;code&gt;cloud-init&lt;/code&gt; bootstrap agent using &lt;code&gt;kfed&lt;/code&gt; and &lt;code&gt;blkid -p&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Taint and replace a Data Guard standby VM in staging; the replacement node should map identical &lt;code&gt;/dev/oracleasm/by-lun/lun-*&lt;/code&gt; symlinks, classify all LUNs as Day 2 Recovery, and mount &lt;code&gt;+DATA&lt;/code&gt; and &lt;code&gt;+RECO&lt;/code&gt; without formatting a single block.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Grep your infrastructure repositories today for &lt;code&gt;parted&lt;/code&gt;, &lt;code&gt;fdisk&lt;/code&gt;, &lt;code&gt;mkfs&lt;/code&gt;, and &lt;code&gt;dd&lt;/code&gt; targeting database volumes, and replace any unguarded Day 1 disk task with a &lt;code&gt;kfed&lt;/code&gt; + &lt;code&gt;udev&lt;/code&gt; LUN-verified consensus gate.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Oracle Corporation:&lt;/strong&gt; &lt;em&gt;Oracle Automatic Storage Management Administrator’s Guide 19c&lt;/em&gt; (&lt;code&gt;kfed&lt;/code&gt; utility, &lt;code&gt;asmcmd mkdg&lt;/code&gt;, &lt;code&gt;CREATE DISKGROUP ... ATTRIBUTE &apos;au_size&apos;=&apos;4M&apos;&lt;/code&gt;, and &lt;code&gt;srvctl start diskgroup&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Microsoft Corporation:&lt;/strong&gt; &lt;em&gt;Azure Virtual Machines Documentation: Attach a data disk to a Linux VM and SCSI LUN mapping&lt;/em&gt; (Microsoft Learn).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Linux Kernel / systemd:&lt;/strong&gt; &lt;em&gt;udevadm(8) and persistent block device naming rules&lt;/em&gt; (Linux man-pages / systemd documentation).&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>The Cost of High Availability: Modeling the Financial Impact of Cloud Database Architectures</title><link>https://rajivonai.com/blog/2024-12-05-financial-modeling-cloud-database-ha-cost/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-12-05-financial-modeling-cloud-database-ha-cost/</guid><description>A Principal Architect&apos;s guide to calculating Total Cost of Ownership across Multi-AZ RDS, Kubernetes Operators, and Exadata. Why 99.999% uptime costs 10x more than 99.99%.</description><pubDate>Wed, 01 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;In enterprise engineering meetings, requesting ‘five nines of availability’ is often treated as a casual baseline specification; in financial reality, jumping from 99.99% to 99.999% uptime introduces an exponential cost curve that can erase operating margins.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Moving from 99.99% (52.56 min/yr downtime) to 99.999% (5.26 min/yr) saves only 47.3 minutes per year while multiplying infrastructure, licensing, and SRE toil costs by 5x–10x+.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Replace blanket five-nines mandates with an ROI-gated three-tier HA menu (Tier 1: 99.9%, Tier 2: 99.99%, Tier 3: 99.999%) where upgrades require $\Delta \text{Downtime Minutes Saved} \times \text{Cost/Min} &gt; \Delta \text{TCO}_{\text{Annual}}$.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Managed AWS RDS Multi-AZ replication traffic is free, but self-managed EC2 cross-AZ replication and cross-AZ app-to-DB traffic still incur $0.02/GB total transfer charges; never downsize standbys to cut costs.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2024-12-05-financial-modeling-cloud-database-ha-cost.svg&quot; alt=&quot;Each extra nine of availability multiplies TCO while saving fewer minutes&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Platform architects routinely receive product or compliance mandates demanding “zero-downtime, five-nines (99.999%)” database infrastructure. Stakeholders equate downtime directly with lost revenue and SLA penalties, so request-for-proposal (RFP) documents often specify 99.999% as a default baseline across every service.&lt;/p&gt;
&lt;p&gt;To meet that mandate, database teams design multi-region topologies: OCI Exadata Maximum Availability Architecture, Amazon Aurora Global Databases, or multi-region PostgreSQL clusters coordinated via Patroni and etcd with synchronous intra-region standbys, cross-region DR replicas, third-region witnesses, and hitless proxy draining.&lt;/p&gt;
&lt;p&gt;The architecture is sound on paper, yet when multi-year cloud and licensing invoices arrive, the fully burdened cost—compute, provisioned IOPS, cross-AZ and cross-region network transfer, commercial processor licenses, and dedicated SRE headcount—frequently dwarfs the actual revenue at risk from the downtime it prevents.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Availability is an asymptotic cost curve: every additional “nine” removes ten times less downtime while demanding disproportionately more infrastructure and operational complexity:&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Availability Tier&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Annual Downtime Allowance&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Monthly Downtime Allowance&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Weekly Downtime Allowance&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;99.0% (“Two Nines”)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;3.65 days (87.6 hours)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;7.30 hours&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;1.68 hours&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;99.9% (“Three Nines”)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;8.76 hours&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;43.8 minutes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;10.1 minutes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;99.99% (“Four Nines”)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;52.56 minutes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;4.38 minutes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;1.01 minutes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;99.999% (“Five Nines”)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;5.26 minutes (315 seconds)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;26.3 seconds&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;6.05 seconds&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Four concrete cost multipliers explain why blanket five-nines mandates break budgets:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The Planned Maintenance Math and Failover Budget:&lt;/strong&gt; A single 15-minute maintenance window once per quarter for OS kernel patches or database point upgrades consumes &lt;strong&gt;60 minutes per year&lt;/strong&gt;—already exceeding the entire 5.26-minute annual five-nines budget by more than 11x before a single hardware failure occurs. Even on automated failovers, DNS TTL propagation (often 60 seconds), stale connection-pool drain, and WAL crash recovery routinely take 60–180 seconds; a single 3-minute unplanned failover consumes 57% of the year’s 99.999% allowance. True 99.999% requires hitless rolling upgrades, proxy connection draining, and automated sub-second health-check routing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Inter-AZ and Cross-Region Network Egress:&lt;/strong&gt; Managed Amazon RDS Multi-AZ does &lt;strong&gt;not&lt;/strong&gt; charge for internal primary-to-standby replication traffic. However, &lt;strong&gt;self-managed databases on EC2&lt;/strong&gt; (such as Patroni/PostgreSQL streaming WAL across AZs) and &lt;strong&gt;cross-AZ application-to-database traffic&lt;/strong&gt; incur standard inter-AZ transfer fees of &lt;strong&gt;$0.01/GB out + $0.01/GB in ($0.02/GB total)&lt;/strong&gt;. Streaming 10 TB/day across AZs on self-managed EC2 costs $200/day, or &lt;strong&gt;~$6,000/month ($72,000/year)&lt;/strong&gt; in network transfer alone. Even on managed RDS, an application fleet spread evenly across three AZs sending 5 TB/day of query results from a single-AZ primary sends two-thirds of its traffic across AZ boundaries: $5\text{ TB/day} \times \frac{2}{3} \times 30\text{ days} = 100\text{ TB/month} \times $0.02/\text{GB} \approx $2,048/\text{month}$ (&lt;strong&gt;$24,576/year&lt;/strong&gt;) in unbudgeted cross-AZ egress.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Commercial Software Licensing Multipliers:&lt;/strong&gt; Oracle Database Enterprise Edition carries a published list price of &lt;strong&gt;$47,500 per processor&lt;/strong&gt; (multiplied by the 0.5 core factor for modern x86 Intel/AMD processors, so an 8-core server requires 4 processor licenses = $190,000) plus &lt;strong&gt;22% annual support&lt;/strong&gt; ($41,800/year). While standard Data Guard for a passive mounted DR standby is included with Enterprise Edition, opening a standby for real-time read queries or automatic block repair requires the &lt;strong&gt;Active Data Guard&lt;/strong&gt; option (&lt;strong&gt;$11,500 per processor&lt;/strong&gt; list price), and active standby processing can double full database licensing obligations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SRE Payroll vs. Managed DBaaS Markup:&lt;/strong&gt; Teams sometimes replace managed RDS with self-managed Kubernetes database operators (CloudNativePG, Percona) to avoid the managed-service markup. While raw EC2 + EBS spend drops, managing CSI volume detachments, etcd quorum, and backup verification requires dedicated senior database SRE capacity—payroll that easily exceeds cloud markup at small or medium fleet sizes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2024-12-05-financial-modeling-cloud-database-ha-cost-2-constraints.svg&quot; alt=&quot;Four hidden multipliers dominate database HA budgets&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How can platform architects model the fully burdened Total Cost of Ownership across HA tiers so stakeholders fund reliability proportional to real downtime risk?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Gets Right&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Verdict&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Blanket 99.999% everywhere&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Maximizes theoretical uptime across all services.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pays 5x–10x+ TCO for internal tools and async queues that lose negligible revenue per minute.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt; — destroys operating margin.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Self-managed K8s everywhere&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Eliminates cloud DBaaS compute markup.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Dedicated SRE payroll for CSI/quorum ops exceeds managed markup on smaller fleets.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Partial&lt;/strong&gt; — viable only at large fleet scale.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Undersized DR standbys&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cuts standby compute bill in half.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Smaller standby crashes under peak traffic the moment failover promotes it.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt; — breaks failover reliability.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;ROI-gated 3-tier HA menu&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Matches SLA, RTO, and RPO to quantified cost per minute of outage.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires product and finance teams to price downtime honestly before provisioning.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen&lt;/strong&gt; — rational capital allocation.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2024-12-05-financial-modeling-cloud-database-ha-cost-3-options.svg&quot; alt=&quot;Replace blanket mandates with an ROI-gated HA menu&quot;&gt;&lt;/p&gt;
&lt;p&gt;Standardizing on a single “maximum availability” tier wastes capital on asynchronous and internal workloads, while cutting costs by downsizing standbys invalidates failover. The steepest cost cliff sits between &lt;strong&gt;intra-region Multi-AZ (99.99%)&lt;/strong&gt; and &lt;strong&gt;multi-region Active-Passive/DR (99.999%)&lt;/strong&gt;: intra-region Multi-AZ roughly doubles compute and storage with sub-2 ms replication latency, whereas crossing regions introduces WAN egress billing, a third-region witness, duplicate observability pipelines, and hitless expand-contract schema migration tooling. An ROI-gated service menu forces every Tier 3 upgrade to clear a financial break-even test.&lt;/p&gt;
&lt;h2 id=&quot;the-menu-driven-high-availability-tco-architecture&quot;&gt;The Menu-Driven High-Availability TCO Architecture&lt;/h2&gt;
&lt;p&gt;Platform engineering should publish a formal three-tier database catalog pairing concrete SLA, RTO, and RPO targets with annualized TCO, evaluated against a six-component cost formula.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-12-05-financial-modeling-cloud-database-ha-cost-4-architecture.svg&quot; alt=&quot;Model all six TCO inputs before selecting an HA tier&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-the-six-component-annual-tco-model&quot;&gt;1. The Six-Component Annual TCO Model&lt;/h3&gt;
&lt;p&gt;$$\text{TCO}&lt;em&gt;{\text{Annual}} = C&lt;/em&gt;{\text{compute}} + C_{\text{storage}} + C_{\text{network}} + C_{\text{licensing}} + (N_{\text{SRE}} \times S_{\text{SRE}} \times \alpha_{\text{toil}}) + C_{\text{downtime_risk}}$$&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$C_{\text{compute}}$: Annualized compute across primary, standbys, and witnesses (3-year Reserved/Savings Plans).&lt;/li&gt;
&lt;li&gt;$C_{\text{storage}}$: Provisioned capacity, IOPS, and throughput across all replicas.&lt;/li&gt;
&lt;li&gt;$C_{\text{network}}$: Self-managed inter-AZ WAL transfer, cross-AZ app traffic, and cross-region replication egress.&lt;/li&gt;
&lt;li&gt;$C_{\text{licensing}}$: Amortized commercial processor licenses plus 22% annual support.&lt;/li&gt;
&lt;li&gt;$N_{\text{SRE}} \times S_{\text{SRE}} \times \alpha_{\text{toil}}$: Burdened SRE payroll fraction spent on patching, backups, and failover drills.&lt;/li&gt;
&lt;li&gt;$C_{\text{downtime_risk}}$: Expected annual outage minutes $\times$ verified revenue/SLA cost per minute.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-the-three-architectural-tiers&quot;&gt;2. The Three Architectural Tiers&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Tier 1 — 99.9% (Three Nines, 8.76 hrs/yr allowance):&lt;/strong&gt; Single-AZ instance (&lt;code&gt;db.m6g.xlarge&lt;/code&gt; class) with GP3 storage, daily snapshots, and continuous WAL archiving to S3. RTO is 1–2 hours; RPO is under 5 minutes. Appropriate for internal portals, reporting backends, and staging.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tier 2 — 99.99% (Four Nines, 52.56 min/yr allowance):&lt;/strong&gt; Primary plus synchronous standby across two AZs (RDS Multi-AZ or Patroni + etcd) with PgBouncer/Envoy connection pooling. Guarantees $RPO = 0$ and automated failover under 30–60 seconds. Optimal default for customer-facing OLTP APIs. Note that in traditional EBS-backed topologies (RDS Multi-AZ or self-managed Patroni), adding a standby or read replica duplicates the full EBS volume and Provisioned IOPS spend per node, whereas shared-storage engines like Amazon Aurora decouple compute replica count from the underlying 6-copy storage volume.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tier 3 — 99.999% (Five Nines, 5.26 min/yr allowance):&lt;/strong&gt; Multi-region active-passive with local Multi-AZ synchronous quorum, third-region witness, hitless rolling upgrades, and 24/7 SRE coverage. Reserved strictly for payment clearing, card authorization, or real-time trading cores where outages immediately burn revenue and cannot be buffered in queues.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-the-economic-decision-gate&quot;&gt;3. The Economic Decision Gate&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Financial Decision Gate evaluating Tier 2 -&gt; Tier 3 Upgrade ROI&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; BusinessImpactSpec&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CostPerMinuteOfDowntime &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;float64&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt; // Direct margin loss + SLA penalties&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; HATier&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Name                  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    AnnualCost            &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;float64&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    AnnualDowntimeMinutes &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;float64&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; EvaluateOptimalTier&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;biz&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; BusinessImpactSpec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;tier2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;tier3&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; HATier&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    minutesSaved &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tier2.AnnualDowntimeMinutes &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tier3.AnnualDowntimeMinutes &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// 52.56 - 5.26 = 47.3 min&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    valueSaved &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; minutesSaved &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; biz.CostPerMinuteOfDowntime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    costDelta &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tier3.AnnualCost &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tier2.AnnualCost&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    netBenefit &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; valueSaved &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; costDelta&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; netBenefit &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Sprintf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;APPROVED: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%s&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; justified. Net annual value: $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%.2f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, tier3.Name, netBenefit)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Sprintf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;REJECTED: Spending $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%.2f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/yr to protect $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%.2f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/yr in outage risk.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, costDelta, valueSaved)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Consider the arithmetic on a $10M/year retail service where 100% downtime costs ~$19/minute ($1,140/hour). Moving from Tier 2 (52.56 min/yr) to Tier 3 (5.26 min/yr) saves &lt;strong&gt;47.3 minutes per year&lt;/strong&gt;, worth &lt;strong&gt;$898.70/year&lt;/strong&gt;. Spending an extra $400,000/year on Tier 3 infrastructure to insure against $898.70 of annual downtime risk is financially indefensible.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Transitioning an organization from ad-hoc availability demands to an ROI-gated catalog takes four auditable steps:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-12-05-financial-modeling-cloud-database-ha-cost-5-flow.svg&quot; alt=&quot;Roll out tiered HA governance in four auditable steps&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Price Downtime Honestly:&lt;/strong&gt; Classify every workload by whether requests fail permanently during an outage (live checkout) or buffer safely in durable queues (async loan processing, webhooks). Compute true cost per minute using lost contribution margin, not gross revenue. Rollback: none (analysis phase).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Publish the Three-Tier Catalog:&lt;/strong&gt; Pair each Terraform database module (&lt;code&gt;tier1&lt;/code&gt;, &lt;code&gt;tier2&lt;/code&gt;, &lt;code&gt;tier3&lt;/code&gt;) with its monthly TCO estimate, RTO/RPO SLA, and whether it supports zero-downtime maintenance. Rollback: keep legacy modules deprecated but callable during migration.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Right-Size Existing Clusters:&lt;/strong&gt; Downgrade over-provisioned Tier 3 clusters serving async or internal workloads to Tier 2 or Tier 1, and align application pods into the same AZ as their primary where possible to cut cross-AZ egress. Rollback: promote a read replica or toggle Multi-AZ before cutover.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Enforce FinOps PR Gates:&lt;/strong&gt; Add cost-estimation checks (such as Infracost or OpenCost) to database Terraform PRs so any Tier 3 request requires written sign-off on the &lt;code&gt;EvaluateOptimalTier&lt;/code&gt; ROI calculation. Rollback: disable the PR gate check if it blocks emergency capacity scaling.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The cost multipliers and architectural boundaries in this model derive from published cloud pricing and database engine behavior:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;AWS Data Transfer vs. Managed RDS Multi-AZ:&lt;/strong&gt; AWS documentation explicitly distinguishes between managed RDS Multi-AZ replication traffic (which is &lt;strong&gt;free&lt;/strong&gt; between the primary and standby) and cross-AZ application-to-database or self-managed EC2 database replication traffic (which is billed at &lt;strong&gt;$0.01/GB in and $0.01/GB out&lt;/strong&gt;, or &lt;strong&gt;$0.02/GB total&lt;/strong&gt;). Teams migrating off RDS onto self-managed EC2 Patroni clusters frequently miss this line item in TCO comparisons.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle Processor Licensing Rules:&lt;/strong&gt; Under Oracle’s published Technology Global Price List, Enterprise Edition is &lt;strong&gt;$47,500 per processor&lt;/strong&gt; with 22% annual support, scaled by the Processor Core Factor (0.5 for Intel Xeon and AMD EPYC). Standard Data Guard is included for passive mounted DR, whereas opening a standby for real-time read queries requires &lt;strong&gt;Active Data Guard&lt;/strong&gt; (&lt;strong&gt;$11,500 per processor&lt;/strong&gt;) across the environment.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Amazon Aurora Storage Decoupling:&lt;/strong&gt; Aurora separates compute from a distributed storage volume that maintains &lt;strong&gt;6 copies across 3 Availability Zones&lt;/strong&gt; (2 copies per AZ), tolerating the loss of up to 2 copies for writes and 3 copies for reads without compute-driven block replication.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CARL Acknowledgment:&lt;/strong&gt; The arithmetic examples in this post use published list prices and AWS Well-Architected availability math rather than private corporate financial records.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Undersized DR standby&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Standby has fewer vCPUs/RAM than primary; crashes immediately upon failover promotion.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;CPU/OOM spike during failover chaos test.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Mandate 100% symmetric CPU, RAM, and IOPS sizing on fail-over standbys.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Cross-AZ app-to-DB egress&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;App pods in AZ1 query RDS primary in AZ2 at $0.02/GB total.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unexpected &lt;code&gt;DataTransfer-Regional-Bytes&lt;/code&gt; line item on cloud bill.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enable topology-aware routing or AZ-local read replicas for read-heavy paths.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Commercial license audit&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Standby opened for read queries without licensing Active Data Guard.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Software Asset Management (SAM) audit finding.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run formal SAM review before enabling read offload on commercial standbys.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Cross-region sync latency&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Synchronous replication across WAN regions adds 70 ms+ per commit.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Write p99 latency spikes and connection pool exhaustion.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Restrict synchronous replication to intra-region AZs (&amp;#x3C; 2 ms); use async cross-region.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Manual patching on “5-nines” DB&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Architecture has multi-AZ HA, but human executes OS/DB patch out of sequence.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unplanned outage consuming &gt;5.26 min annual budget.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Automate rolling upgrades via durable workflows and hitless proxy draining.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;K8s CSI volume detach stall&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pod fails over, but EBS CSI driver hangs detaching volume from dead node.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pod stuck in &lt;code&gt;ContainerCreating&lt;/code&gt; past RTO target.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Test CSI detach timeouts and enforce node fencing before volume re-attach.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Untested Tier 1 backups&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Snapshot exists, but KMS key policy or catalog corruption blocks restore.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Failed monthly restore drill.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Automate monthly ephemeral restore tests that verify table counts and checksums.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;A practical TCO review also accounts for &lt;strong&gt;IOPS headroom and backup-retention compounding&lt;/strong&gt; across tiers. When a team upgrades a single-AZ database to synchronous Multi-AZ or adds cross-region read replicas, storage and provisioned IOPS charges scale linearly with every full physical copy, and automated snapshot retention plus transaction-log archives grow with write churn rather than static table size. Modeling storage IOPS, cross-AZ data transfer, and commercial processor cores together inside the Terraform catalog ensures engineering leads compare full monthly run-rates rather than base compute hourly prices alone.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-12-05-financial-modeling-cloud-database-ha-cost-6-failures.svg&quot; alt=&quot;Cost-cutting shortcuts break failover exactly when you need it&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Every additional nine saves 10x less downtime at 5x–10x+ the cost:&lt;/strong&gt; Jumping from 99.99% to 99.999% saves only 47.3 minutes per year while eliminating planned maintenance windows.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Know what AWS bills for inter-AZ traffic:&lt;/strong&gt; Managed RDS Multi-AZ replication is free, but self-managed EC2 WAL streaming and cross-AZ app-to-DB queries cost $0.02/GB total.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Model SRE payroll alongside cloud markup:&lt;/strong&gt; Self-managed Kubernetes database operators only beat managed DBaaS TCO when fleet size amortizes dedicated database SRE headcount.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never downsize standbys to save money:&lt;/strong&gt; A smaller standby will collapse under peak production concurrency the moment failover promotes it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate Tier 3 requests on ROI math:&lt;/strong&gt; Only approve 99.999% when the value of 47.3 saved downtime minutes per year exceeds the full annualized TCO delta.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Audit your database estate for services running expensive multi-region or commercial HA tiers without a documented cost-per-minute outage justification.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Publish a three-tier HA service catalog (99.9%, 99.99%, 99.999%) with fully burdened TCO (compute, storage, network, licensing, SRE toil).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Check your last cloud bill for &lt;code&gt;DataTransfer-Regional-Bytes&lt;/code&gt; and verify whether your standbys are symmetrically sized to handle peak primary load.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, compute &lt;code&gt;EvaluateOptimalTier&lt;/code&gt; for your top three production databases and add a TCO sign-off gate to Tier 3 Terraform modules.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;AWS Pricing &amp;#x26; Knowledge Center — &lt;em&gt;Amazon RDS Multi-AZ Replication and EC2 Inter-AZ Data Transfer&lt;/em&gt; (&lt;a href=&quot;https://repost.aws/knowledge-center/ec2-data-transfer-charges&quot;&gt;repost.aws&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Amazon Aurora Documentation — &lt;em&gt;Aurora Storage Architecture (6 Copies / 3 AZs)&lt;/em&gt; (&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Storage.html&quot;&gt;docs.aws.amazon.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Oracle Technology Global Price List — &lt;em&gt;Database Enterprise Edition &amp;#x26; Active Data Guard Processor Metrics&lt;/em&gt; (&lt;a href=&quot;https://www.oracle.com/assets/technology-price-list-070617.pdf&quot;&gt;oracle.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Google Site Reliability Engineering — &lt;em&gt;Embracing Risk and Availability Math&lt;/em&gt; (&lt;a href=&quot;https://sre.google/sre-book/&quot;&gt;sre.google/sre-book/&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>Building Hybrid Search in OpenSearch: BM25, k-NN, Filters, and Reranking</title><link>https://rajivonai.com/blog/2026-06-23-opensearch-hybrid-search-bm25-knn-filters-reranking/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-23-opensearch-hybrid-search-bm25-knn-filters-reranking/</guid><description>A production guide to OpenSearch hybrid retrieval with BM25, vector k-NN, metadata filters, score fusion, reranking, relevance debugging, and observability.</description><pubDate>Tue, 30 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Hybrid search fails when teams treat it as “BM25 plus vectors.” Production hybrid search is a retrieval pipeline: lexical candidates, semantic candidates, hard filters, score normalization, fusion, reranking, evaluation, and operational telemetry. OpenSearch can run that pipeline, but it will not design the relevance model for you.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;OpenSearch is a strong platform for hybrid search when lexical relevance, vector recall, structured filters, relevance debugging, and operational search tooling all matter. The core design is not one query. It is a candidate pipeline with explicit constraints, rank fusion, reranking, and feedback loops.&lt;/p&gt;
&lt;p&gt;Use BM25 for exact terms, phrases, identifiers, and field-aware relevance. Use k-NN for semantic recall. Apply hard filters for tenant, entitlement, deletion state, region, compliance, inventory, and other non-negotiable constraints. Fuse or normalize candidate sets carefully, then rerank a bounded set when needed.&lt;/p&gt;
&lt;p&gt;OpenSearch hybrid search features and query syntax are version-specific — the &lt;code&gt;normalization-processor&lt;/code&gt; search pipeline (the mechanism most of this post assumes) is the current, generally-available way to fuse BM25 and k-NN scores, but confirm exact syntax against the deployed OpenSearch version before writing pipeline definitions, since hybrid search has picked up new configuration options across recent releases.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Organizations adopt OpenSearch hybrid search when users need both exact words and semantic meaning. Support search needs error codes and paraphrases. Product search needs SKUs and intent. Internal knowledge search needs names, acronyms, and conceptual matching.&lt;/p&gt;
&lt;p&gt;The business asks for “better search.” The platform team has to define what better means.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;BM25 and vector similarity produce different kinds of evidence.&lt;/p&gt;
&lt;p&gt;BM25 is strong when the user’s query and the document share terms. It handles exact identifiers, rare words, field boosts, analyzers, and phrase behavior. Dense vector search is strong when the user’s query and document mean similar things with different words.&lt;/p&gt;
&lt;p&gt;Combining them creates production questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Which filters are hard constraints versus ranking signals?&lt;/li&gt;
&lt;li&gt;How many candidates come from each retrieval path?&lt;/li&gt;
&lt;li&gt;How are scores normalized?&lt;/li&gt;
&lt;li&gt;Does an exact identifier match beat a semantic paraphrase?&lt;/li&gt;
&lt;li&gt;Where does reranking happen?&lt;/li&gt;
&lt;li&gt;How are bad results debugged?&lt;/li&gt;
&lt;li&gt;How does the team roll back a relevance change?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Without answers, hybrid search becomes an opaque scoring soup.&lt;/p&gt;
&lt;h2 id=&quot;core-technical-explanation&quot;&gt;Core Technical Explanation&lt;/h2&gt;
&lt;p&gt;A production hybrid pipeline separates stages:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[user query] --&gt; Parse[query parsing and intent]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Parse --&gt; Lex[BM25 candidate query]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Parse --&gt; Vec[k-NN candidate query]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Parse --&gt; Filter[hard metadata filters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Filter --&gt; Lex&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Filter --&gt; Vec&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Lex --&gt; Fusion[score normalization or rank fusion]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Vec --&gt; Fusion&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Fusion --&gt; Rerank[bounded reranking]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Rerank --&gt; Explain[logs and relevance debugging]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Explain --&gt; Results[ranked results]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The lexical path should know which fields matter. A title match may be stronger than a body match. A SKU field should not use the same analyzer as prose. Synonyms and stemming should be tested, not assumed.&lt;/p&gt;
&lt;p&gt;The vector path should know which embedding field it searches. Title embeddings, body embeddings, and support-ticket embeddings may have different behavior. A single generic embedding field may be acceptable for a first version but makes debugging harder.&lt;/p&gt;
&lt;p&gt;Filters should be shared across both paths. If BM25 searches one corpus and k-NN searches another, fusion results become inconsistent and security risk rises.&lt;/p&gt;
&lt;p&gt;Fusion can be score-based or rank-based. Raw BM25 scores and vector scores are not naturally comparable — OpenSearch’s documented approach is the &lt;code&gt;normalization-processor&lt;/code&gt; pipeline, which applies a normalization technique (commonly min-max) to bring both score families onto a comparable scale, then combines them with a weighted technique such as arithmetic mean. Normalization, rank fusion, or a reranker should be chosen deliberately, not left at whatever default a tutorial used.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Operate hybrid search like a release pipeline:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Define query classes.&lt;/strong&gt; Exact identifier lookup, troubleshooting question, product discovery, known-title lookup, and broad semantic exploration should not be tuned from one average metric.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Create relevance sets.&lt;/strong&gt; Keep a protected set of queries with expected good, acceptable, and bad results. Include tenant-filtered negative cases.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Log candidate stages.&lt;/strong&gt; Store which path produced each candidate, the rank from each path, filter context, final rank, and reranker features. Do not log sensitive body text unless policy allows it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Bound reranking.&lt;/strong&gt; Rerank the top candidates, not the whole index. The reranker should improve final ordering without hiding broken retrieval.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Separate hard constraints.&lt;/strong&gt; Tenant, region, deletion state, compliance, entitlement, and inventory availability are not soft boosts.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Use aliases for releases.&lt;/strong&gt; Build replacement indexes for mapping or embedding changes, compare results, then switch aliases when validation passes.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Raw score mixing&lt;/td&gt;&lt;td&gt;BM25 or vector path dominates unpredictably&lt;/td&gt;&lt;td&gt;Normalize or use rank-based fusion&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Candidate pool too small&lt;/td&gt;&lt;td&gt;Reranker cannot recover missing documents&lt;/td&gt;&lt;td&gt;Increase lexical or vector candidate size&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Filters differ by path&lt;/td&gt;&lt;td&gt;Inconsistent or unauthorized results&lt;/td&gt;&lt;td&gt;Centralize filter construction&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reranker hides retrieval bugs&lt;/td&gt;&lt;td&gt;Final results look okay until tail queries fail&lt;/td&gt;&lt;td&gt;Log pre-rerank candidates&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Analyzer breaks identifiers&lt;/td&gt;&lt;td&gt;SKUs or error codes stop matching&lt;/td&gt;&lt;td&gt;Use keyword fields and query-set tests&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Embedding model changes silently&lt;/td&gt;&lt;td&gt;Relevance shifts without review&lt;/td&gt;&lt;td&gt;Version vectors and release indexes&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-and-tenancy-notes&quot;&gt;Security and Tenancy Notes&lt;/h2&gt;
&lt;p&gt;Hybrid search doubles the chance of filter mistakes because there are multiple retrieval paths. The same security constraints must apply to BM25, k-NN, fusion, reranking, and fallback.&lt;/p&gt;
&lt;p&gt;If reranking calls an external model, retrieved snippets may leave the OpenSearch boundary. That must be reviewed as data processing, not only relevance engineering — whoever owns the data-processing agreement with that model provider needs to sign off, since this is a contractual and compliance question, not a configuration flag.&lt;/p&gt;
&lt;h2 id=&quot;cost-notes&quot;&gt;Cost Notes&lt;/h2&gt;
&lt;p&gt;Hybrid search often costs more than either lexical or vector search alone. It can run two candidate queries, perform fusion, and execute reranking. The cost is justified when result quality improves enough to matter.&lt;/p&gt;
&lt;p&gt;Control cost by bounding candidate sizes, separating query classes, caching where safe, and measuring reranker use. Do not run the most expensive pipeline for every query if exact lookup is enough.&lt;/p&gt;
&lt;h2 id=&quot;observability-notes&quot;&gt;Observability Notes&lt;/h2&gt;
&lt;p&gt;Track:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Lexical candidate latency and hit count.&lt;/li&gt;
&lt;li&gt;Vector candidate latency and hit count.&lt;/li&gt;
&lt;li&gt;Fusion overlap between paths.&lt;/li&gt;
&lt;li&gt;Final result source mix.&lt;/li&gt;
&lt;li&gt;Reranker latency and error rate.&lt;/li&gt;
&lt;li&gt;Zero-result rate and no-click rate.&lt;/li&gt;
&lt;li&gt;Filter selectivity.&lt;/li&gt;
&lt;li&gt;Query-set regression results.&lt;/li&gt;
&lt;li&gt;Relevance changes by release.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Give search engineers a way to inspect a single bad query from parse through final rank.&lt;/p&gt;
&lt;h2 id=&quot;backup-restore-and-dr-notes&quot;&gt;Backup, Restore, and DR Notes&lt;/h2&gt;
&lt;p&gt;Hybrid search recovery needs both lexical and vector validation. After restore or reindex, verify analyzers, mappings, vector fields, filters, pipelines, aliases, and reranker configuration.&lt;/p&gt;
&lt;p&gt;If the vector path is degraded, lexical fallback may be acceptable for some workloads. Decide that before an incident. For regulated or customer-facing results, degraded mode may need feature flags and visible product behavior.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Which query classes require hybrid retrieval?&lt;/li&gt;
&lt;li&gt;Which fields are lexical, vector, or both?&lt;/li&gt;
&lt;li&gt;Which filters are hard constraints?&lt;/li&gt;
&lt;li&gt;How many candidates come from BM25 and k-NN?&lt;/li&gt;
&lt;li&gt;How are scores normalized or fused?&lt;/li&gt;
&lt;li&gt;Is reranking required, and on how many candidates?&lt;/li&gt;
&lt;li&gt;Can a bad result be explained from logs?&lt;/li&gt;
&lt;li&gt;Does every path enforce tenant and entitlement filters?&lt;/li&gt;
&lt;li&gt;Is there a query set for release validation?&lt;/li&gt;
&lt;li&gt;Is rollback an alias switch, config revert, or full reindex?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Hybrid search gets built as “BM25 plus vectors” without a plan for score fusion, shared filters, or reranking, and results become an opaque scoring soup nobody can debug.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Treat hybrid search as a release pipeline — normalize BM25 and k-NN scores deliberately (OpenSearch’s &lt;code&gt;normalization-processor&lt;/code&gt;), centralize filter construction so both paths enforce the same constraints, and log pre-rerank candidates so a bad result can be traced from parse through final rank.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; A protected relevance query set (including tenant-filtered negative cases) passes after every mapping, fusion, or reranker change, and a single bad query can be explained from logs without guessing.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, confirm BM25 and vector candidates are filtered through one shared filter builder, not two independently maintained ones — that’s the most common place hybrid search leaks unauthorized results.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;OpenSearch hybrid search documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/ai-search/hybrid-search/index/&quot;&gt;https://docs.opensearch.org/latest/vector-search/ai-search/hybrid-search/index/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch vector search and k-NN query documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/&quot;&gt;https://docs.opensearch.org/latest/vector-search/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch query DSL, analyzers, and filters documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/query-dsl/&quot;&gt;https://docs.opensearch.org/latest/query-dsl/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch normalization-processor and search pipeline documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/search-plugins/search-pipelines/normalization-processor/&quot;&gt;https://docs.opensearch.org/latest/search-plugins/search-pipelines/normalization-processor/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch hybrid search explain and scoring documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/ai-search/hybrid-search/explain/&quot;&gt;https://docs.opensearch.org/latest/vector-search/ai-search/hybrid-search/explain/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>ai-engineering</category><category>checklist</category></item><item><title>Active-Active Routing is a Myth: The Physics of Multi-Region Data Consistency</title><link>https://rajivonai.com/blog/2024-11-15-active-active-database-myth-multi-region/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-11-15-active-active-database-myth-multi-region/</guid><description>Why multi-master database replication collapses under speed-of-light constraints, how asynchronous conflict resolution corrupts data, and why single-master with geographic sharding is the superior architecture.</description><pubDate>Sun, 28 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Promising global Active-Active multi-region database writes with sub-millisecond latency is not an engineering achievement; it is a fundamental violation of the speed of light that guarantees silent data corruption under asynchronous replication.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Transatlantic fiber limits round-trip time (RTT) to ~55.7 ms minimum (68–78 ms in production), so synchronous multi-region writes stall row locks while asynchronous multi-master with Last-Write-Wins silently overwrites conflicting commits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Route writes to a single authoritative owner per row using either Global Active-Passive with write-forwarding + 5s session pinning, or Strict Geographic Sharding ($S_{\text{US}} \cap S_{\text{EU}} = \emptyset$).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Never automate cross-region failover between only two regions; without an independent third-region witness, a WAN partition produces a dual-primary split-brain.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2024-11-15-active-active-database-myth-multi-region.svg&quot; alt=&quot;Async multi-master trades silent data loss for local speed&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Engineering teams are routinely asked to deploy transactional applications in an “Active-Active” configuration across continents—such as AWS &lt;code&gt;us-east-1&lt;/code&gt; and &lt;code&gt;eu-west-1&lt;/code&gt;. The goal is straightforward: users in London and New York both read and write to local database nodes in under 5 ms while the database keeps both regions synchronized and survives a full regional blackout with zero downtime.&lt;/p&gt;
&lt;p&gt;To build this, teams reach for bidirectional replication tools: Oracle GoldenGate multi-master, PostgreSQL logical replication meshes, or MySQL multi-source replication. Traffic routers send European requests to Europe and North American requests to Virginia, with both databases operating in read-write mode and streaming change data capture (CDC) events across the Atlantic.&lt;/p&gt;
&lt;p&gt;In low-concurrency staging tests where users update disjoint rows, the setup looks clean. As soon as production traffic hits shared inventory, account balances, or reservation seats concurrently, physical network latency breaks the illusion.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Multi-master database replication across continents collides with four hard constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Speed-of-Light Floor:&lt;/strong&gt; Light travels in a vacuum at ~300,000 km/s, and in single-mode optical fiber at roughly two-thirds that speed ($c/n \approx 200,000\text{ km/s}$). New York and London are ~5,570 km apart along a great-circle path. Even ignoring terrestrial fiber detours and optical switches, the theoretical minimum round-trip time is $2 \times 5,570 / 200,000 \approx 55.7\text{ ms}$. Production transatlantic paths typically measure &lt;strong&gt;68 ms to 78 ms RTT&lt;/strong&gt;, and transpacific paths (California to Tokyo) measure &lt;strong&gt;110 ms to 130 ms RTT&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Synchronous 2PC Lock-Hold Explosion:&lt;/strong&gt; Enforcing strict serializability across regions requires synchronous consensus or two-phase commit (2PC) over the WAN. A local &lt;code&gt;UPDATE&lt;/code&gt; that holds a row lock for 0.5 ms now holds that lock for &lt;strong&gt;140 ms to 220 ms&lt;/strong&gt; across two round trips—a 300x increase in lock-hold duration. By Little’s Law, a single hot inventory row that supports $1 / 0.0005\text{ s} = 2,000\text{ serial TPS}$ locally collapses to $1 / 0.150\text{ s} \approx 6.7\text{ TPS}$ under 150 ms cross-region lock holds, regardless of how many vCPUs or IOPS the database has.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Asynchronous Blind Window:&lt;/strong&gt; To preserve 1 ms local writes, teams make replication asynchronous. During the ~70 ms WAN transit window, both regions accept writes independently. If a New York user and a London user both claim the last seat on a flight or the last unit of inventory within that window, both primaries validate &lt;code&gt;CHECK (quantity &gt;= 0)&lt;/code&gt; or &lt;code&gt;UNIQUE&lt;/code&gt; constraints against their local snapshot and commit. Seventy milliseconds later, the replication streams collide with an unresolvable constraint violation—and even PostgreSQL &lt;code&gt;ON CONFLICT&lt;/code&gt; or custom merge handlers cannot undo two already-acknowledged inventory decrements without overselling stock.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Last-Write-Wins (LWW) Silent Data Loss:&lt;/strong&gt; Automated conflict resolution commonly defaults to “Latest Timestamp Wins.” Cloud VM NTP clocks routinely drift by 10 ms to 100 ms, meaning a region with a fast clock systematically overwrites newer valid commits from the slower region. Even with zero clock skew, LWW silently discards one committed transaction after the client has already received an HTTP 200 confirmation.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2024-11-15-active-active-database-myth-multi-region-2-constraints.svg&quot; alt=&quot;Physics and clock drift bound multi-region databases&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How can platform architects deliver low-latency local reads and multi-region resilience without falling into asynchronous multi-master conflict corruption?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Gets Right&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Verdict&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Async multi-master with LWW&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sub-5 ms local reads and writes in every region.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Concurrent writes to shared keys collide; LWW silently erases committed rows.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt; — violates transactional integrity.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Synchronous WAN 2PC / Paxos&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero write conflicts; strict external consistency.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;140–220 ms WAN lock holds crush hot-row OLTP throughput.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Niche&lt;/strong&gt; — viable only for low-contention workloads (e.g., Spanner).&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Active-Passive + write-forwarding&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;2 ms local reads everywhere; single authoritative writer prevents conflicts.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cross-region writes pay 1 WAN RTT (~70 ms) and need Read-Your-Writes pinning.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen&lt;/strong&gt; — for global shared datasets.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Strict Geographic Sharding&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sub-5 ms local reads and writes with zero write conflicts.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cross-region foreign keys and global uniqueness require async sagas.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen&lt;/strong&gt; — for tenant-partitionable workloads.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2024-11-15-active-active-database-myth-multi-region-3-options.svg&quot; alt=&quot;Choose single-master forwarding or geographic sharding&quot;&gt;&lt;/p&gt;
&lt;p&gt;Asynchronous multi-master is acceptable only for mathematically conflict-free structures such as CRDT counters or append-only logs—never for relational inventory, balances, or reservations. Similarly, GeoDNS or BGP anycast routing solves edge ingress proximity, not database state convergence: routing two continents to two writable primaries simply shifts the conflict from the network layer into the storage engine. Teams must separate &lt;strong&gt;multi-region read scaling&lt;/strong&gt; (where local read-only replicas serve 90%+ of traffic safely) from &lt;strong&gt;multi-region write authority&lt;/strong&gt;. For relational OLTP, you must choose between centralizing writes per table (Active-Passive) or partitioning write authority per tenant (Geographic Sharding).&lt;/p&gt;
&lt;h2 id=&quot;the-global-active-passive-and-geosharded-architecture&quot;&gt;The Global Active-Passive and Geosharded Architecture&lt;/h2&gt;
&lt;p&gt;Both sound architectures enforce one invariant: &lt;strong&gt;every mutable row has exactly one authoritative primary at any point in time.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-11-15-active-active-database-myth-multi-region-4-architecture.svg&quot; alt=&quot;Route reads locally and pin post-write sessions to primary&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-global-active-passive-with-write-forwarding&quot;&gt;1. Global Active-Passive with Write-Forwarding&lt;/h3&gt;
&lt;p&gt;For unified datasets (e.g., a global product catalog or shared order system) where typical OLTP traffic is 90%–95% reads, designate one region (&lt;code&gt;us-east-1&lt;/code&gt;) as the authoritative read-write primary and deploy read-only replicas in secondary regions (&lt;code&gt;eu-west-1&lt;/code&gt;, &lt;code&gt;ap-southeast-1&lt;/code&gt;). Local reads complete in ~2 ms. Write statements from European users forward over the private cloud backbone to the US primary, paying one ~70 ms WAN RTT on mutations while guaranteeing zero write collisions.&lt;/p&gt;
&lt;p&gt;To avoid multiplying that 70 ms penalty on multi-statement transactions (&lt;code&gt;BEGIN&lt;/code&gt;, &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, &lt;code&gt;COMMIT&lt;/code&gt;), never chatty-forward individual SQL statements from an EU application server to a US database over the WAN—five sequential round trips would stall a single checkout for 350 ms+ while holding row locks in Virginia. Instead, forward the &lt;strong&gt;entire write RPC or service command&lt;/strong&gt; to a stateless worker in &lt;code&gt;us-east-1&lt;/code&gt; (or invoke a single stored procedure) so all transactional round trips execute locally within 1 ms inside the primary region.&lt;/p&gt;
&lt;h3 id=&quot;2-session-pinned-read-your-writes-routing&quot;&gt;2. Session-Pinned Read-Your-Writes Routing&lt;/h3&gt;
&lt;p&gt;Because cross-region WAL replication lags by 100 ms to 500 ms, a user who updates their profile (forwarded to &lt;code&gt;us-east-1&lt;/code&gt;) and immediately refreshes the page (served by &lt;code&gt;eu-west-1&lt;/code&gt;) can read stale data and re-submit the form. An application router eliminates this by pinning the user’s session to the primary in a local low-latency cache for a short window after any mutation:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Read-Your-Writes Session Pinning Router in Go&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; RequestRouter&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    primaryPool  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pgxpool&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Pool&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt; // Authoritative Primary (us-east-1)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    replicaPool  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pgxpool&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Pool&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt; // Local Read Replica (eu-west-1)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    sessionCache &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;redis&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Client&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt; // Local Redis (noeviction policy)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;r &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;RequestRouter&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;HandleQuery&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;ctx&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; context&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Context&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;userID&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;sql&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;isWrite&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; bool&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pgx&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Rows&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; isWrite {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        rows, err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r.primaryPool.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Query&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, sql)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            r.sessionCache.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Set&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pinned:&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;userID, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;time.Second)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rows, err&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pinned, _ &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r.sessionCache.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Exists&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pinned:&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;userID).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(); pinned &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r.primaryPool.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Query&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, sql)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r.replicaPool.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Query&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, sql)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-strict-geographic-sharding&quot;&gt;3. Strict Geographic Sharding&lt;/h3&gt;
&lt;p&gt;When every region requires sub-5 ms &lt;strong&gt;write&lt;/strong&gt; latency (payments, regional gaming state, strict GDPR data residency), partition the database by tenant or geography (&lt;code&gt;tenant_region = &apos;EU&apos;&lt;/code&gt; vs &lt;code&gt;&apos;US&apos;&lt;/code&gt;). Frankfurt is the sole primary for EU accounts; Virginia is the sole primary for US accounts. Any cross-region CDC stream is strictly unidirectional and read-only for analytics, so $S_{\text{US}} \cap S_{\text{EU}} = \emptyset$ and write conflicts cannot occur. For the rare operations that span regions—such as migrating an enterprise tenant from &lt;code&gt;us-east-1&lt;/code&gt; to &lt;code&gt;eu-west-1&lt;/code&gt; or maintaining a global routing directory—use a transactional outbox and an idempotent saga rather than distributed 2PC foreign keys, fencing the tenant to read-only during the ownership handoff so only one shard ever holds write authority.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Replacing an existing multi-master topology without downtime proceeds in four reversible stages:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-11-15-active-active-database-myth-multi-region-5-flow.svg&quot; alt=&quot;Migrate off multi-master in four reversible stages&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Table Ownership Classification:&lt;/strong&gt; Audit every table and classify it as either &lt;em&gt;global single-primary&lt;/em&gt; (forwarded writes) or &lt;em&gt;geographically sharded&lt;/em&gt; (partitioned by &lt;code&gt;tenant_region&lt;/code&gt;). Rollback: none (read-only schema audit).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Write-Forwarding Router and Session Pinning:&lt;/strong&gt; Deploy the application router with session pinning (or enable managed write forwarding) while dual-region replication is still monitored. Verify that EU reads hit the local replica and post-write reads route to the primary during the pin window. Rollback: toggle router feature flag back to direct local writes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Fence Secondary Writers:&lt;/strong&gt; Drain in-flight local writes on the secondary region, verify zero &lt;code&gt;INSERT&lt;/code&gt;/&lt;code&gt;UPDATE&lt;/code&gt;/&lt;code&gt;DELETE&lt;/code&gt; activity via &lt;code&gt;pg_stat_database&lt;/code&gt; (&lt;code&gt;tup_inserted&lt;/code&gt;, &lt;code&gt;tup_updated&lt;/code&gt;, &lt;code&gt;tup_deleted&lt;/code&gt;), and flip the secondary database to &lt;code&gt;default_transaction_read_only = on&lt;/code&gt; before retiring bidirectional CDC. Rollback: re-enable read-write mode if write-forwarding fails.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Third-Region Witness for Automated Failover:&lt;/strong&gt; Place an independent failover coordinator or witness in a third region (&lt;code&gt;us-west-2&lt;/code&gt; or &lt;code&gt;eu-central-1&lt;/code&gt;) so neither primary nor secondary can promote itself during a transatlantic network cut. If &lt;code&gt;us-east-1&lt;/code&gt; truly goes dark, the witness and &lt;code&gt;eu-west-1&lt;/code&gt; form a 2-of-3 quorum and promote the secondary; if only the transatlantic fiber degrades while &lt;code&gt;us-east-1&lt;/code&gt; remains reachable from &lt;code&gt;us-west-2&lt;/code&gt;, the primary keeps quorum and &lt;code&gt;eu-west-1&lt;/code&gt; stays read-only. Rollback: revert failover automation to manual promotion.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The constraints and patterns above reflect documented behavior in production multi-region database engines:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;AWS Aurora Global Database Write Forwarding:&lt;/strong&gt; For Aurora PostgreSQL (versions 14.9, 15.4, 16+, and higher), AWS supports native write forwarding controlled by &lt;code&gt;apg_write_forward.consistency_mode&lt;/code&gt;. Its modes map directly to the Read-Your-Writes tradeoff: &lt;code&gt;eventual&lt;/code&gt; (fastest, may read stale replica state), &lt;code&gt;session&lt;/code&gt; (default: replica waits for the session’s own Virtual Data LSN before serving subsequent reads), and &lt;code&gt;global&lt;/code&gt; (strongest: waits for all globally committed LSNs, paying full WAN sync latency).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Google Cloud Spanner TrueTime:&lt;/strong&gt; Spanner achieves global external consistency not by bypassing the speed of light, but by bounding clock uncertainty $\epsilon = (\text{latest} - \text{earliest}) / 2$ using GPS and atomic clocks (typically 1–4 ms under normal operation) and forcing transactions to wait out $2\epsilon$ via &lt;strong&gt;Commit Wait&lt;/strong&gt; before releasing locks—proving that even specialized hardware pays a latency tax for global linearizability.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CRDTs vs. Relational OLTP:&lt;/strong&gt; Redis Enterprise Active-Active CRDBs use vector clocks and Conflict-Free Replicated Data Types (CRDTs) for strong eventual consistency on counters, sets, and maps—whereas open-source Valkey uses standard asynchronous primary-replica replication. CRDTs solve set/counter convergence, not relational &lt;code&gt;UNIQUE&lt;/code&gt; constraints or multi-row foreign-key invariants.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CARL Acknowledgment:&lt;/strong&gt; I have not operated a global airline or Spanner-scale multi-region deployment personally; the analysis here synthesizes documented Aurora Global Database, Spanner TrueTime, and optical networking constraints.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Clock skew in LWW multi-master&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;NTP drift (10–100 ms) lets older writes overwrite newer commits.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Silent row regressions and audit log timestamp inversions.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Eliminate LWW on transactional tables; enforce single-writer ownership.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Cross-region replication lag spike&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;WAN congestion pushes replica lag from 200 ms to 15s.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Replica &lt;code&gt;replay_lag&lt;/code&gt; alert exceeds SLO threshold.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Add a lag circuit breaker that routes reads to primary when lag &gt; 2s.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Session-pin Redis eviction&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Redis LRU evicts pin key before WAL catches up on replica.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Read-your-writes regression immediately after form submit.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run pin store on &lt;code&gt;noeviction&lt;/code&gt; memory policy with conservative 10s TTL.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Two-region failover split-brain&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;WAN cut isolates US and EU; both regions promote to primary.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Dual-primary health state and divergent WAL timelines.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Require a 3rd-region witness for failover quorum; fence minority to read-only.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Cross-shard foreign keys&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;EU tenant row joins or enforces FK against US shard over WAN.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;200 ms+ distributed query tail latency and timeout spikes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Decouple shards into bounded contexts; coordinate via async event sagas.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Uncompressed WAN CDC egress&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Full-row before/after WAL images stream continuously across regions.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sudden spike in cross-region data transfer billing.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enable replication stream compression and replicate changed columns only.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Beyond replication lag and failover quorum, teams migrating off multi-master must budget for &lt;strong&gt;schema-migration serialization&lt;/strong&gt; and &lt;strong&gt;sequence/identity key collisions&lt;/strong&gt;. In a single-primary topology, DDL migrations (&lt;code&gt;ALTER TABLE&lt;/code&gt;, index creation) execute sequentially on the primary and replicate deterministically down the WAL stream to read replicas. Under multi-master CDC, running an uncoordinated DDL change on one region while the peer region still holds the previous table definition breaks logical apply workers and stalls the replication queue until an operator manually skips or replays the conflicting transaction. Similarly, auto-increment integer sequences that rely on odd/even offsets across two regions fail silently when a third disaster-recovery region is added or when a load balancer shifts traffic mid-failover; migrating to &lt;code&gt;UUIDv7&lt;/code&gt; or region-prefixed composite keys removes sequence coordination from the critical write path. Finally, test connection-pool draining under simulated WAN brownouts: if the session-pinning cache becomes unreachable, the router must fail closed to the single primary rather than serving stale local reads to post-write requests.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-11-15-active-active-database-myth-multi-region-6-failures.svg&quot; alt=&quot;Cross-region replication lag and missing witnesses break naive failover&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The speed of light sets a hard ~56 ms+ transatlantic RTT floor:&lt;/strong&gt; No database vendor or replication protocol can deliver sub-5 ms synchronous global commits across continents.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Asynchronous multi-master with LWW trades integrity for local speed:&lt;/strong&gt; Concurrent writes during the WAN transit window collide and silently overwrite committed customer state.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Active-Passive + write-forwarding fits global shared tables:&lt;/strong&gt; Local replicas serve 90%+ reads in ~2 ms, while writes forward to one primary and session pinning preserves Read-Your-Writes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Geographic sharding fits sub-5 ms global writes:&lt;/strong&gt; Partitioning write ownership by &lt;code&gt;tenant_region&lt;/code&gt; eliminates conflicts mathematically ($S_{\text{US}} \cap S_{\text{EU}} = \emptyset$).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never automate failover with only two regions:&lt;/strong&gt; Always place an independent witness in a third region so a WAN partition cannot trigger dual-primary promotion.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Audit any architecture labeled “Active-Active Multi-Region” to check whether two regions can mutate the same primary key concurrently.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Standardize on Global Active-Passive with write-forwarding (for shared data) or Strict Geographic Sharding (for tenant-isolated data).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Measure the actual inter-region RTT between your target cloud regions and check &lt;code&gt;apg_write_forward.consistency_mode&lt;/code&gt; or session-pinning behavior under simulated 500 ms replication lag.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, add Read-Your-Writes session pinning in your API router and verify that cross-region failover quorum depends on a third-region witness.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Amazon Aurora Documentation — &lt;em&gt;Write Forwarding in Aurora Global Database (PostgreSQL)&lt;/em&gt; (&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-global-database-write-forwarding.html&quot;&gt;docs.aws.amazon.com&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Google Cloud Spanner Documentation — &lt;em&gt;TrueTime and External Consistency&lt;/em&gt; (&lt;a href=&quot;https://cloud.google.com/spanner/docs/true-time-external-consistency&quot;&gt;cloud.google.com/spanner/docs/true-time-external-consistency&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Redis Documentation — &lt;em&gt;Active-Active Geo-Distribution (CRDBs)&lt;/em&gt; (&lt;a href=&quot;https://redis.io/docs/latest/operate/rs/databases/active-active/&quot;&gt;redis.io&lt;/a&gt;) and Valkey Replication (&lt;a href=&quot;https://valkey.io/topics/replication/&quot;&gt;valkey.io&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Martin Kleppmann — &lt;em&gt;Designing Data-Intensive Applications: Chapter 5 (Replication) &amp;#x26; Chapter 9 (Consistency and Consensus)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>OpenSearch Vector Search on AWS: Managed Convenience, Hidden Cost</title><link>https://rajivonai.com/blog/2026-06-22-opensearch-vector-search-aws-managed-convenience-hidden-cost/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-22-opensearch-vector-search-aws-managed-convenience-hidden-cost/</guid><description>The tradeoffs of Amazon OpenSearch Service for vector search, including managed operations, scaling, instance choice, storage, memory, transfer, snapshots, and index design cost.</description><pubDate>Sat, 27 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Amazon OpenSearch Service makes the cluster easier to provision. It does not make vector search cheap, automatically well-sharded, or immune to bad mappings. Managed service convenience removes some operational work; it does not remove the physics of memory, shards, replicas, storage, snapshots, and query fanout.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;Amazon OpenSearch Service is attractive for vector search when AWS is already the platform boundary and the team wants managed provisioning, integration, snapshots, access control, monitoring, and scaling workflows.&lt;/p&gt;
&lt;p&gt;The hidden cost is that managed OpenSearch still bills for the shape of the workload. Over-sharded indexes, oversized replicas, poor vector mappings, high-dimensional embeddings, heavy hybrid queries, frequent refreshes, large snapshots, cross-AZ traffic, and under-modeled memory pressure can make a “simple” vector search service expensive.&lt;/p&gt;
&lt;p&gt;AWS service behavior, supported OpenSearch versions, instance families, storage options, and pricing change over time — this piece describes the storage-tier model (hot/UltraWarm/cold) and cost structure that has been stable across recent versions, but confirm current instance families and per-unit pricing on the AWS pricing page before building a cost model, since AWS revises both regularly.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Teams choose Amazon OpenSearch Service because it fits the AWS operating model. IAM, VPC networking, encryption, CloudWatch, snapshots, Terraform modules, and procurement may already exist. For many platform teams, that matters more than adopting a new dedicated vector database.&lt;/p&gt;
&lt;p&gt;That convenience is real. The mistake is treating it as a substitute for search architecture.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Managed services reduce undifferentiated operations. They do not make poor physical design disappear.&lt;/p&gt;
&lt;p&gt;Vector search on managed OpenSearch can become expensive through:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More or larger data nodes for memory headroom.&lt;/li&gt;
&lt;li&gt;More storage for vector fields and graph structures.&lt;/li&gt;
&lt;li&gt;More replicas for availability and query throughput.&lt;/li&gt;
&lt;li&gt;More shards than the workload needs.&lt;/li&gt;
&lt;li&gt;More snapshot storage and longer restore windows.&lt;/li&gt;
&lt;li&gt;More ingestion compute during backfills.&lt;/li&gt;
&lt;li&gt;More query CPU from hybrid retrieval.&lt;/li&gt;
&lt;li&gt;More data transfer when clients, ingest workers, or consumers cross boundaries.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The cost failure usually appears after a successful launch. The system works, traffic grows, embeddings are backfilled, and the monthly bill says the index design is part of the architecture.&lt;/p&gt;
&lt;h2 id=&quot;core-technical-explanation&quot;&gt;Core Technical Explanation&lt;/h2&gt;
&lt;p&gt;OpenSearch Service gives you a managed domain or serverless collection model depending on the chosen AWS offering. The production vector workload still depends on index mappings, vector dimensions, k-NN method choices, shard count, replica count, storage type, node type, availability zone design, and snapshot policy — the managed control plane provisions and monitors these, but does not choose good values for them.&lt;/p&gt;
&lt;p&gt;AWS OpenSearch Service’s storage model has three documented tiers: hot storage (EBS or instance store, for actively indexed and queried data), UltraWarm (S3-backed with a caching layer, for infrequently accessed data — billed per GB stored, separately from node-hour charges), and cold storage (S3-only, no attached compute, billed for storage alone with no compute cost while idle). Vector fields with an active HNSW graph generally need hot storage; UltraWarm and cold tiers are a cost lever for older, less-queried indices, not a place to put a live vector search workload.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Source[source systems] --&gt; Ingest[embedding and indexing pipeline]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Ingest --&gt; Domain[Amazon OpenSearch Service]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Domain --&gt; Shards[index shards and replicas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Shards --&gt; Vector[vector fields and HNSW]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Shards --&gt; Text[BM25 fields]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[application queries] --&gt; Domain&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Domain --&gt; Metrics[CloudWatch and service metrics]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Domain --&gt; Snapshots[snapshots and restore]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Managed control planes help with provisioning and some maintenance tasks. They do not know whether your tenant distribution makes one shard hot, whether your candidate set is too small for recall, or whether your index contains fields nobody queries.&lt;/p&gt;
&lt;p&gt;The key cost principle is simple: vector search magnifies bad index design. A mistake in shard count, field mapping, embedding dimension, or replica strategy is multiplied across every node, snapshot, and query.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Operate AWS OpenSearch vector search from a cost-aware runbook:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Instance and node sizing.&lt;/strong&gt; Choose nodes based on vector memory, indexing pressure, query concurrency, storage, and recovery needs. Do not reuse log-search sizing without a vector load test.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Shard discipline.&lt;/strong&gt; Start with the fewest shards that meet scale, recovery, and parallelism needs. Over-sharding creates management overhead, more fanout, and higher baseline resource use.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Replica strategy.&lt;/strong&gt; Replicas improve availability and read throughput but duplicate storage and indexing work. Decide replica count by availability and query load, not habit.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Storage planning.&lt;/strong&gt; Include vector fields, text fields, replicas, merge headroom, snapshots, and disk watermarks. Cost estimates based only on source document size will be wrong.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Backfill windows.&lt;/strong&gt; Embedding backfills are cluster events. Use controlled indexing rates, refresh settings appropriate to the phase, and rollback plans.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Snapshot validation.&lt;/strong&gt; Snapshots are not a full DR plan unless restore time and search quality after restore have been tested.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Cost symptom&lt;/th&gt;&lt;th&gt;Operational symptom&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Over-sharding&lt;/td&gt;&lt;td&gt;Higher baseline node and overhead cost&lt;/td&gt;&lt;td&gt;Query fanout and cluster-state pressure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High-dimensional vectors by default&lt;/td&gt;&lt;td&gt;Storage and memory growth&lt;/td&gt;&lt;td&gt;Slower indexing and queries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replicas copied from keyword index&lt;/td&gt;&lt;td&gt;Double or triple storage cost&lt;/td&gt;&lt;td&gt;No clear read-throughput need&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Frequent refresh during backfill&lt;/td&gt;&lt;td&gt;Higher CPU and merge pressure&lt;/td&gt;&lt;td&gt;Indexing lag and latency spikes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cross-boundary ingestion&lt;/td&gt;&lt;td&gt;Data transfer cost&lt;/td&gt;&lt;td&gt;Harder network troubleshooting&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Snapshots never restored&lt;/td&gt;&lt;td&gt;False DR confidence&lt;/td&gt;&lt;td&gt;RTO unknown&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hybrid search everywhere&lt;/td&gt;&lt;td&gt;Query CPU grows&lt;/td&gt;&lt;td&gt;Search latency variance&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-and-tenancy-notes&quot;&gt;Security and Tenancy Notes&lt;/h2&gt;
&lt;p&gt;AWS-managed convenience can improve baseline controls: VPC access, encryption, IAM integration, logging, and managed snapshots may fit enterprise requirements — these are genuine AWS platform capabilities, but “available” is not the same as “configured correctly for this workload,” so verify each control is actually turned on for the domain in question rather than assuming managed means secure by default.&lt;/p&gt;
&lt;p&gt;Tenant enforcement still belongs in the index and query design. If tenant or entitlement data is denormalized into OpenSearch, the ingestion pipeline must update it reliably. If authorization is checked outside OpenSearch, the system must avoid retrieving unauthorized chunks before filtering.&lt;/p&gt;
&lt;p&gt;For strict tenants, separate indexes or domains may be appropriate. That improves blast-radius control but multiplies cost.&lt;/p&gt;
&lt;h2 id=&quot;cost-notes&quot;&gt;Cost Notes&lt;/h2&gt;
&lt;p&gt;Model cost with scenarios:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Normal query load.&lt;/li&gt;
&lt;li&gt;Backfill load.&lt;/li&gt;
&lt;li&gt;Relevance experiment load.&lt;/li&gt;
&lt;li&gt;Restore and rebuild event.&lt;/li&gt;
&lt;li&gt;Tenant growth skew.&lt;/li&gt;
&lt;li&gt;Model migration with a second vector field.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Include data nodes, coordinator pressure if applicable, storage, replicas, snapshots, cross-AZ or cross-service transfer, monitoring, ingestion workers, and reranking compute. Exact per-unit prices for instances, storage, and data transfer change over time and by region — model the cost with the current AWS OpenSearch Service pricing page rather than a fixed number from any point-in-time source.&lt;/p&gt;
&lt;p&gt;The biggest preventable cost is often over-sharding. A cluster with many small shards can look “scalable” while wasting resources every hour.&lt;/p&gt;
&lt;h2 id=&quot;observability-notes&quot;&gt;Observability Notes&lt;/h2&gt;
&lt;p&gt;Track both AWS service metrics and OpenSearch workload metrics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Node CPU, JVM heap, native memory signals, and disk watermarks.&lt;/li&gt;
&lt;li&gt;Search latency by lexical, vector, and hybrid route.&lt;/li&gt;
&lt;li&gt;Indexing latency, rejected writes, and refresh behavior.&lt;/li&gt;
&lt;li&gt;Segment count and merge activity.&lt;/li&gt;
&lt;li&gt;Snapshot duration and failures.&lt;/li&gt;
&lt;li&gt;Hot nodes and shard imbalance.&lt;/li&gt;
&lt;li&gt;Cost allocation by domain, environment, tenant tier, and index.&lt;/li&gt;
&lt;li&gt;Query volume by feature flag or application route.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Do not stop at CloudWatch health. Add search-quality and recall checks.&lt;/p&gt;
&lt;h2 id=&quot;backup-restore-and-dr-notes&quot;&gt;Backup, Restore, and DR Notes&lt;/h2&gt;
&lt;p&gt;Managed snapshots help, but they need restore drills. Test restoring a vector-heavy index into a non-production domain, allocating shards, warming queries, and validating representative retrieval results.&lt;/p&gt;
&lt;p&gt;If OpenSearch is derived from source systems, also keep a replay path. If snapshots are unavailable or too slow, rebuild may be the real DR path. If rebuild depends on an embedding provider, that provider becomes part of DR.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Which AWS OpenSearch deployment model is being used?&lt;/li&gt;
&lt;li&gt;Are supported vector features verified for the chosen version?&lt;/li&gt;
&lt;li&gt;Is node sizing based on vector load tests?&lt;/li&gt;
&lt;li&gt;Are shard and replica counts justified?&lt;/li&gt;
&lt;li&gt;How large are vector fields after replicas and snapshots?&lt;/li&gt;
&lt;li&gt;What is the cost of a full reindex or embedding migration?&lt;/li&gt;
&lt;li&gt;Are ingestion workers in the same network and region boundary?&lt;/li&gt;
&lt;li&gt;Have restore time and post-restore relevance been tested?&lt;/li&gt;
&lt;li&gt;Are costs attributed by index, tenant, and feature?&lt;/li&gt;
&lt;li&gt;What signal triggers a move to a different architecture?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Teams treat Amazon OpenSearch Service’s managed provisioning as a substitute for search architecture, and the bill surfaces the real cost of over-sharding, oversized replicas, and unmodeled vector memory only after launch.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Review managed OpenSearch like infrastructure — size shards and nodes from vector load tests, keep vector-active indices on hot storage (not UltraWarm/cold), and model cost across normal, backfill, and restore scenarios, not just steady-state query traffic.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; A cost breakdown by index and tenant tier accounts for replicas, snapshots, and cross-AZ transfer, and a restore drill into a non-production domain validates both shard allocation and retrieval quality.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, check whether shard count and replica count were chosen from a vector-load test or copied from an existing keyword-search index, and re-test if it’s the latter.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Amazon OpenSearch Service vector search documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/&quot;&gt;https://docs.opensearch.org/latest/vector-search/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Amazon OpenSearch Service pricing (instances, storage, data transfer — check current rates): &lt;a href=&quot;https://aws.amazon.com/opensearch-service/pricing/&quot;&gt;https://aws.amazon.com/opensearch-service/pricing/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Amazon OpenSearch Service storage tier guidance (UltraWarm and cold storage): &lt;a href=&quot;https://aws.amazon.com/blogs/big-data/choose-the-right-storage-tier-for-your-needs-in-amazon-opensearch-service/&quot;&gt;https://aws.amazon.com/blogs/big-data/choose-the-right-storage-tier-for-your-needs-in-amazon-opensearch-service/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Amazon OpenSearch Service domain sizing guidance: &lt;a href=&quot;https://docs.aws.amazon.com/opensearch-service/latest/developerguide/sizing-domains.html&quot;&gt;https://docs.aws.amazon.com/opensearch-service/latest/developerguide/sizing-domains.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch index management and snapshot documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/im-plugin/&quot;&gt;https://docs.opensearch.org/latest/im-plugin/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>cloud</category><category>architecture</category><category>checklist</category></item><item><title>Azure Constrained-Core Optimization: Designing Licensing-Aware Infrastructure Code</title><link>https://rajivonai.com/blog/2024-10-12-oracle-on-azure-vms-storage-ha/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-10-12-oracle-on-azure-vms-storage-ha/</guid><description>Why deploying Oracle on standard Azure virtual machines exposes enterprises to millions in unexpected license liabilities, and how to build policy-governed IaC pipelines using constrained-core compute shapes.</description><pubDate>Fri, 26 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Cloud compute infrastructure is billed by the hour, but enterprise database software is licensed by exposed hypervisor vCPUs; deploying standard Azure virtual machines to unlock storage IOPS can trigger millions of dollars in unbudgeted Oracle liabilities from a single Terraform pull request.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Azure couples uncached storage IOPS and throughput to VM vCPU count, so sizing for 51,200 IOPS on standard &lt;code&gt;Standard_E32s_v5&lt;/code&gt; exposes 32 vCPUs (16 Oracle Processors) even when the database needs only 8 vCPUs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Use Azure constrained-core SKUs (&lt;code&gt;Standard_E32-8s_v5&lt;/code&gt;) paired with Premium SSD v2 (&lt;code&gt;PremiumV2_LRS&lt;/code&gt;) and enforce SKU selection at three layers: a declarative Terraform module, an OPA Conftest CI gate, and an Azure Policy &lt;code&gt;deny&lt;/code&gt; assignment.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Constrained-core shapes create a 32:1 GiB-per-vCPU memory ratio that can trigger &lt;code&gt;latch: cache buffers chains&lt;/code&gt; spinlocks and interrupt skew if SGA sizing, parallel query caps, and &lt;code&gt;irqbalance&lt;/code&gt; are not tuned.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2024-10-12-oracle-on-azure-vms-storage-ha.svg&quot; alt=&quot;Decouple storage IOPS from Oracle vCPU licenses&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Enterprise technology organizations migrating mission-critical Oracle Database workloads from on-premises SAN architectures to Microsoft Azure face a structural mismatch between how cloud hypervisors allocate I/O and how Oracle prices software. On-premises, storage arrays deliver IOPS and bandwidth over Fibre Channel fabrics independently of how many CPU sockets are populated in the database host. In Azure, that independence disappears at the VM hypervisor boundary.&lt;/p&gt;
&lt;p&gt;In an Online Transaction Processing (OLTP) deployment, database workloads often require 40,000 to 50,000 Input/Output Operations Per Second (IOPS) and roughly 800 MB/s of uncached storage bandwidth to sustain redo log flushes (&lt;code&gt;log file sync&lt;/code&gt;), DBWR dirty-block writes, and random index lookups (&lt;code&gt;db file sequential read&lt;/code&gt;). When platform engineers consult the Azure &lt;code&gt;Esv5&lt;/code&gt;-series specification, they discover that uncached Premium SSD storage throughput and IOPS scale linearly with vCPU count:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Standard_E4s_v5&lt;/code&gt; exposes 4 vCPUs and 32 GiB RAM, but caps uncached remote storage at 6,400 IOPS and 145 MB/s.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Standard_E8s_v5&lt;/code&gt; exposes 8 vCPUs and 64 GiB RAM, capping uncached remote storage at 12,800 IOPS and 290 MB/s.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Standard_E16s_v5&lt;/code&gt; exposes 16 vCPUs and 128 GiB RAM, capping uncached remote storage at 25,600 IOPS and 600 MB/s.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Standard_E32s_v5&lt;/code&gt; exposes 32 vCPUs and 256 GiB RAM, unlocking 51,200 IOPS and 865 MB/s of uncached storage throughput.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Because an 8-vCPU &lt;code&gt;Standard_E8s_v5&lt;/code&gt; stalls at 12,800 IOPS, engineers routinely select &lt;code&gt;Standard_E32s_v5&lt;/code&gt; in Terraform solely to acquire the parent chassis’s storage controller ceiling. Once that pull request merges, every node exposes 32 vCPUs to the Linux kernel regardless of actual CPU demand.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The financial failure surfaces during an Oracle License Management Services (LMS) review. Under Oracle’s &lt;em&gt;Licensing Oracle Software in the Cloud Computing Environment&lt;/em&gt; policy for Authorized Cloud Environments (which includes Microsoft Azure and AWS), Oracle’s standard on-premises Core Factor Table does not apply. Instead, when multi-threading (hyper-threading) is enabled, two available vCPUs equal one Oracle Processor license; if multi-threading is disabled, one vCPU equals one Processor license.&lt;/p&gt;
&lt;p&gt;Consider the arithmetic on published Oracle Technology Global Price List figures ($47,500 perpetual list price per Enterprise Edition Processor license plus $10,450 per year in Software Update License &amp;#x26; Support, or 22% of list):&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Constraint 1 — Linear I/O-to-vCPU Coupling:&lt;/strong&gt; Reaching 51,200 uncached IOPS on standard &lt;code&gt;Esv5&lt;/code&gt; shapes requires &lt;code&gt;Standard_E32s_v5&lt;/code&gt; (32 vCPUs = 16 Oracle Processors per node). Across a 20-host fleet, that equals 640 vCPUs, or &lt;strong&gt;320 Oracle Processor licenses&lt;/strong&gt;: &lt;strong&gt;$15,200,000 in upfront list licenses&lt;/strong&gt; plus &lt;strong&gt;$3,344,000 in recurring annual support&lt;/strong&gt;—excluding optional packs such as Partitioning, Diagnostics, or Real Application Clusters.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 2 — Guest OS Masking Invalidity:&lt;/strong&gt; Administrators sometimes attempt to cap CPU exposure inside Linux by executing &lt;code&gt;echo 0 &gt; /sys/devices/system/cpu/cpu*/online&lt;/code&gt; or setting &lt;code&gt;maxcpus=8&lt;/code&gt; in GRUB. Oracle LMS audits reject guest-OS CPU masking because the policy counts vCPUs made available to the VM by the cloud hypervisor, not cores left online by the guest kernel.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 3 — Standard Edition 2 (SE2) Hard Cap:&lt;/strong&gt; For workloads licensed under Oracle Database Standard Edition 2, Oracle’s cloud policy caps instance size at a maximum of 8 vCPUs (mapped at 4 vCPUs per socket, so 8 vCPUs = 2 sockets). Provisioning a &lt;code&gt;Standard_E16s_v5&lt;/code&gt; or &lt;code&gt;Standard_E32s_v5&lt;/code&gt; immediately breaks SE2 compliance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 4 — Asymmetric Compute-to-I/O Utilization:&lt;/strong&gt; When I/O—not SQL execution—is the bottleneck, upgrading from &lt;code&gt;Standard_E8s_v5&lt;/code&gt; to &lt;code&gt;Standard_E32s_v5&lt;/code&gt; leaves 24 vCPUs largely idle while quadrupling software entitlement exposure.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2024-10-12-oracle-on-azure-vms-storage-ha-2-constraints.svg&quot; alt=&quot;Azure couples IOPS to vCPU count unless constrained&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How can platform engineering teams automate Azure infrastructure provisioning so Oracle databases receive 51,200+ IOPS and 256 GiB of memory while exposing only the exact vCPU count required by the workload?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams evaluating Oracle on Azure IaaS face four distinct sizing and governance patterns. Only one satisfies both the storage throughput ceiling and Oracle’s hypervisor-level vCPU counting rules.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Active vCPUs&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Uncached IOPS&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Oracle EE List / Node&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Why Chosen or Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unconstrained &lt;code&gt;Standard_E32s_v5&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;32 vCPUs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;51,200 IOPS&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;$760,000 (16 Procs)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected: 4x license overspend for unused compute cores.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Guest OS CPU Offlining (&lt;code&gt;maxcpus=8&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;8 visible (32 allocated)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;51,200 IOPS&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;$760,000 (16 Procs)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected: Oracle LMS counts hypervisor-available vCPUs, not Linux online CPUs.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Downsize to &lt;code&gt;Standard_E8s_v5&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;8 vCPUs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;12,800 IOPS&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;$190,000 (4 Procs)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected: Capped at 12,800 IOPS and 290 MB/s; starves OLTP redo and data reads.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Constrained &lt;code&gt;Standard_E32-8s_v5&lt;/code&gt; + Policy Gates&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;8 active vCPUs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;51,200 IOPS&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;$190,000 (4 Procs)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen:&lt;/strong&gt; Preserves parent I/O and 256 GiB RAM while cutting vCPUs by 75%.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Unconstrained &lt;code&gt;Standard_E32s_v5&lt;/code&gt; is operationally simple because it requires no special SKU governance, but it turns every Terraform scale-up into a six-figure licensing event. Guest OS masking is worse: it gives engineers a false sense of compliance while leaving the organization exposed to back-dated audit true-ups. Downsizing to &lt;code&gt;Standard_E8s_v5&lt;/code&gt; solves the licensing math but cuts uncached IOPS by 75% (from 51,200 to 12,800) and uncached throughput by 66% (from 865 MB/s to 290 MB/s), causing I/O queues to back up under OLTP commit bursts.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-10-12-oracle-on-azure-vms-storage-ha-3-options.svg&quot; alt=&quot;Only hypervisor-level vCPU caps survive an audit&quot;&gt;&lt;/p&gt;
&lt;p&gt;Constrained-core VMs paired with policy-as-code gates win because Microsoft enforces the vCPU reduction at the Azure hypervisor launch boundary. A &lt;code&gt;Standard_E32-8s_v5&lt;/code&gt; exposes only 8 active vCPUs to the guest OS while retaining the 256 GiB RAM, 32 max data disks, 51,200 IOPS, and 865 MB/s uncached throughput of the &lt;code&gt;Standard_E32s_v5&lt;/code&gt; parent chassis.&lt;/p&gt;
&lt;h2 id=&quot;the-constrained-core-policy-architecture&quot;&gt;The Constrained-Core Policy Architecture&lt;/h2&gt;
&lt;p&gt;Relying on wiki checklists to remind engineers to pick &lt;code&gt;-8s_v5&lt;/code&gt; instead of &lt;code&gt;s_v5&lt;/code&gt; fails the first time an on-call engineer resizes a VM in the Azure Portal during an incident. The architecture therefore removes raw VM SKU selection from human operators and enforces constrained-core shapes across three layers:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Declarative Capacity Abstraction (Terraform Module):&lt;/strong&gt; Application and database teams declare workload intent—target IOPS, target MB/s, and required memory—rather than raw Azure VM strings. The module maps those inputs deterministically to approved constrained-core SKUs (&lt;code&gt;Standard_E8-2s_v5&lt;/code&gt;, &lt;code&gt;Standard_E16-4s_v5&lt;/code&gt;, &lt;code&gt;Standard_E32-8s_v5&lt;/code&gt;, or &lt;code&gt;Standard_E64-16s_v5&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pre-Merge CI/CD Gate (OPA Conftest):&lt;/strong&gt; Every pull request runs &lt;code&gt;terraform plan -out=tfplan.binary&lt;/code&gt;, converts the plan to JSON, and evaluates it with Open Policy Agent (OPA). Any &lt;code&gt;azurerm_linux_virtual_machine&lt;/code&gt; tagged &lt;code&gt;Workload = &quot;Oracle&quot;&lt;/code&gt; whose &lt;code&gt;size&lt;/code&gt; does not match the constrained-core hyphenated pattern fails CI before merge.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Runtime Control-Plane Perimeter (Azure Policy):&lt;/strong&gt; At the Azure Management Group level, an Azure Policy evaluates every &lt;code&gt;Microsoft.Compute/virtualMachines&lt;/code&gt; write operation. If &lt;code&gt;tags[&apos;Workload&apos;] == &apos;Oracle&apos;&lt;/code&gt; and the SKU name does not match the constrained pattern, the Azure Resource Manager (ARM) API returns a hard &lt;code&gt;deny&lt;/code&gt;, blocking manual portal resizes, ad-hoc CLI commands, and secondary deployment scripts.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2024-10-12-oracle-on-azure-vms-storage-ha-4-architecture.svg&quot; alt=&quot;Three-layer enforcement stops unconstrained SKU drift&quot;&gt;&lt;/p&gt;
&lt;p&gt;For storage, the architecture pairs constrained-core VMs with &lt;strong&gt;Azure Premium SSD v2 (&lt;code&gt;PremiumV2_LRS&lt;/code&gt;)&lt;/strong&gt;. Unlike Premium SSD v1, where IOPS and throughput are tied to disk capacity (forcing teams to over-allocate terabytes of empty storage just to buy IOPS), Premium SSD v2 includes a baseline of 3,000 IOPS and 125 MB/s per disk and lets Terraform dial &lt;code&gt;disk_iops_read_write&lt;/code&gt; (up to 80,000 IOPS per disk) and &lt;code&gt;disk_mbps_read_write&lt;/code&gt; independently of &lt;code&gt;disk_size_gb&lt;/code&gt;. Two Azure constraints must be respected in the module: &lt;code&gt;PremiumV2_LRS&lt;/code&gt; disks require a &lt;code&gt;zone&lt;/code&gt; specification in zonal regions, and host-level disk caching (&lt;code&gt;ReadOnly&lt;/code&gt; or &lt;code&gt;ReadWrite&lt;/code&gt;) is unsupported on Premium SSD v2 data disks, so &lt;code&gt;caching&lt;/code&gt; on &lt;code&gt;azurerm_virtual_machine_data_disk_attachment&lt;/code&gt; must be set to &lt;code&gt;&quot;None&quot;&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Migrating an existing Oracle estate onto constrained-core shapes and policy guardrails requires a phased rollout so enforcement never breaks live recovery workflows.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Inventory and Telemetry Audit:&lt;/strong&gt; Query Azure Resource Graph for all VMs running Oracle binaries, verify &lt;code&gt;Workload = &quot;Oracle&quot;&lt;/code&gt; tagging, and collect 30 days of CPU utilization, &lt;code&gt;nproc&lt;/code&gt;, and Azure Monitor uncached disk IOPS/MBps metrics. &lt;em&gt;Validation gate:&lt;/em&gt; 100% of Oracle VMs tagged and mapped to target constrained SKUs. &lt;em&gt;Rollback:&lt;/em&gt; Read-only phase; no production changes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — OPA Gate in Warning Mode, Then Blocking:&lt;/strong&gt; Add the Rego policy to CI pipelines in warn-only mode for one sprint to catch legacy module exceptions, then flip to blocking (&lt;code&gt;deny&lt;/code&gt;) on pull requests. &lt;em&gt;Validation gate:&lt;/em&gt; Zero false-positive blocks on approved constrained-core templates. &lt;em&gt;Rollback:&lt;/em&gt; Revert CI step to warn-only.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Azure Policy in Audit Mode, Then Deny:&lt;/strong&gt; Deploy the Management Group policy with &lt;code&gt;effect = &quot;audit&quot;&lt;/code&gt; for 14 days to detect portal or CLI drift. Once compliant or exempted for non-Oracle hosts, switch &lt;code&gt;effect&lt;/code&gt; to &lt;code&gt;&quot;deny&quot;&lt;/code&gt;. &lt;em&gt;Validation gate:&lt;/em&gt; Zero unauthorized ARM &lt;code&gt;virtualMachines/write&lt;/code&gt; events in Activity Logs. &lt;em&gt;Rollback:&lt;/em&gt; Patch policy parameter back to &lt;code&gt;&quot;audit&quot;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Rolling Standby-First Cutover:&lt;/strong&gt; Resize Data Guard physical standby VMs first to the target constrained SKU (e.g., &lt;code&gt;Standard_E32-8s_v5&lt;/code&gt;) and attach &lt;code&gt;PremiumV2_LRS&lt;/code&gt; volumes. Run synthetic &lt;code&gt;SLOB&lt;/code&gt; or replay workloads, inspect AWR top timed events for &lt;code&gt;latch: cache buffers chains&lt;/code&gt; and &lt;code&gt;log file sync&lt;/code&gt;, then perform a planned Data Guard switchover before resizing the former primary. &lt;em&gt;Validation gate:&lt;/em&gt; AWR wait profile and redo apply lag remain within SLA under peak load. &lt;em&gt;Rollback:&lt;/em&gt; Switch back to the original primary via Data Guard.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2024-10-12-oracle-on-azure-vms-storage-ha-5-flow.svg&quot; alt=&quot;Four-phase rollout with explicit audit and rollback gates&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;I have not published internal client telemetry from this architecture, but the documented implementation pattern combines three verifiable artifacts: a Terraform sizing module, an OPA Rego policy, and an Azure Policy &lt;code&gt;deny&lt;/code&gt; rule.&lt;/p&gt;
&lt;h3 id=&quot;1-declarative-sizing-module-modulesoracle_vmmaintf&quot;&gt;1. Declarative Sizing Module (&lt;code&gt;modules/oracle_vm/main.tf&lt;/code&gt;)&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;locals&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  selected_vm_size&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;target_iops &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 50000&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ?&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Standard_E64-16s_v5&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;target_iops &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 25000&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ?&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Standard_E32-8s_v5&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;target_iops &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 12000&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ?&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Standard_E16-4s_v5&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;Standard_E8-2s_v5&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_linux_virtual_machine&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;oracle_node&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;vm-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;database_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;-01&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  resource_group_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;resource_group_name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  location&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;location&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  zone&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;availability_zone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  size&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; local&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;selected_vm_size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  admin_username&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;oracle_admin&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  network_interface_ids&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [azurerm_network_interface&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;oracle_nic&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  os_disk&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    caching&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;ReadWrite&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    storage_account_type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Premium_LRS&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tags&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Workload        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Oracle&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DatabaseName    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var.database_name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ConstrainedCore &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Enforced&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;azurerm_managed_disk&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;asm_data_disk&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                 =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;disk-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;database_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;-data-01&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  location&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;             =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;location&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  resource_group_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;resource_group_name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  zone&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                 =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;availability_zone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  storage_account_type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;PremiumV2_LRS&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  create_option&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Empty&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  disk_size_gb&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;         =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 500&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  disk_iops_read_write&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;target_iops&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  disk_mbps_read_write&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;target_mbps&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tags&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                 =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; { Workload &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Oracle&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-opa-cicd-guardrail-licensing_guardrailrego&quot;&gt;2. OPA CI/CD Guardrail (&lt;code&gt;licensing_guardrail.rego&lt;/code&gt;)&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;package terraform.azure.licensing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import future.keywords.in&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;is_constrained_sku(sku) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    regex.match(&quot;^Standard_[A-Za-z]+[0-9]+-[0-9]+.*$&quot;, sku)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;deny[msg] {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    some resource in input.resource_changes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    resource.type == &quot;azurerm_linux_virtual_machine&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    tags := resource.change.after.tags&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    tags[&quot;Workload&quot;] == &quot;Oracle&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    vm_size := resource.change.after.size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    not is_constrained_sku(vm_size)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    msg := sprintf(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &quot;LICENSING VIOLATION: &apos;%v&apos; requests unconstrained SKU &apos;%v&apos; for Oracle.&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        [resource.address, vm_size]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-azure-policy-definition-at-management-group-level&quot;&gt;3. Azure Policy Definition at Management Group Level&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;properties&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;displayName&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Enforce Azure Constrained-Core SKUs for Oracle Workloads&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;mode&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Indexed&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;policyRule&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;      &quot;if&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        &quot;allOf&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          { &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;&quot;field&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;&quot;equals&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Microsoft.Compute/virtualMachines&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          { &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;&quot;field&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;tags[&apos;Workload&apos;]&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;&quot;equals&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Oracle&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;            &quot;field&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Microsoft.Compute/virtualMachines/sku.name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;            &quot;notLike&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Standard_E*-*s_v*&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;      &quot;then&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: { &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;&quot;effect&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;deny&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note also two documented ecosystem behaviors when evaluating this pattern. First, for workloads that need Exadata-class IOPS or RAC without managing Azure VM storage limits, &lt;strong&gt;Oracle Database@Azure&lt;/strong&gt; runs OCI Exadata infrastructure inside Azure data centers with low-latency VNet integration. Second, while constrained-core SKUs reduce Oracle processor licenses, Azure still bills the VM compute rate of the parent chassis (&lt;code&gt;Standard_E32-8s_v5&lt;/code&gt; carries the same Azure VM compute charge as &lt;code&gt;Standard_E32s_v5&lt;/code&gt;), so the savings come from Oracle software licensing rather than Azure IaaS compute.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Constrained-core architectures reduce software licensing exposure, but compressing 256 GiB of RAM and 51,200 IOPS onto 8 active vCPUs shifts bottlenecks toward CPU scheduling, latch contention, and audit governance.&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Batch or unindexed SQL spike&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;8 active vCPUs hit 100% CPU; OLTP sessions queue on &lt;code&gt;resmgr:cpu quantum&lt;/code&gt; or listener spawns stall.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Azure Monitor &lt;code&gt;Percentage CPU&lt;/code&gt; &gt; 90%; AWR &lt;code&gt;CPU time&lt;/code&gt; and &lt;code&gt;OS CPU&lt;/code&gt; wait spikes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enable Oracle Database Resource Manager (DBRM); cap &lt;code&gt;parallel_max_servers&lt;/code&gt; and per-consumer-group CPU shares.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Oversized SGA on 32:1 RAM/vCPU&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Hundreds of gigabytes of buffer cache scanned by only 8 vCPUs amplifies &lt;code&gt;latch: cache buffers chains&lt;/code&gt; spins.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;AWR Top 10 Timed Events showing &lt;code&gt;latch: cache buffers chains&lt;/code&gt; or &lt;code&gt;latch free&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Size &lt;code&gt;db_cache_size&lt;/code&gt; to active working set rather than all 256 GiB; segregate hot objects into &lt;code&gt;KEEP&lt;/code&gt; pools.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Oracle sales “chassis” challenge&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Account reps sometimes argue the parent 32-vCPU hardware chassis should be licensed instead of 8 active vCPUs.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Audit inquiry citing parent VM family (&lt;code&gt;E32&lt;/code&gt;) rather than active SKU (&lt;code&gt;E32-8s_v5&lt;/code&gt;).&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cite the “available vCPUs” wording in Oracle’s Cloud Licensing Policy; log &lt;code&gt;nproc&lt;/code&gt; output and Azure SKU metadata; escalate to LMS.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Premium SSD v2 provisioned cap&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bulk loads or sort spills exceed &lt;code&gt;disk_iops_read_write&lt;/code&gt; or &lt;code&gt;disk_mbps_read_write&lt;/code&gt;; host caching is disabled.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Azure Monitor &lt;code&gt;Data Disk IOPS Consumed Percentage&lt;/code&gt; &gt; 85%; &lt;code&gt;direct path read temp&lt;/code&gt; waits.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Isolate &lt;code&gt;TEMP&lt;/code&gt; onto a dedicated &lt;code&gt;PremiumV2_LRS&lt;/code&gt; disk; alert at 85% IOPS and remember Azure caps performance changes per 24h window.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Portal resize drift&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Engineer resizes &lt;code&gt;Standard_E32-8s_v5&lt;/code&gt; to &lt;code&gt;Standard_E32s_v5&lt;/code&gt; during an incident, instantly adding 12 Oracle Processors.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Azure Activity Log &lt;code&gt;virtualMachines/write&lt;/code&gt; with unconstrained SKU; FinOps tag/SKU scanner.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce Azure Policy &lt;code&gt;deny&lt;/code&gt; at Management Group scope so portal and CLI resizes fail at the ARM API layer.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Linux IRQ affinity skew&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Storage NVMe and NIC interrupts pin to CPU 0–1 on an 8-vCPU guest, saturating &lt;code&gt;%soft&lt;/code&gt; while cores 2–7 sit idle.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;mpstat -P ALL 1&lt;/code&gt; showing imbalanced &lt;code&gt;%irq&lt;/code&gt; / &lt;code&gt;%soft&lt;/code&gt; on CPU 0.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enable Azure Accelerated Networking (SR-IOV) on NICs and run &lt;code&gt;irqbalance&lt;/code&gt; across all active vCPUs.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2024-10-12-oracle-on-azure-vms-storage-ha-6-failures.svg&quot; alt=&quot;Where constrained-core Oracle deployments break&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Azure &lt;code&gt;Esv5&lt;/code&gt; VMs bind uncached IOPS and throughput to vCPU count (&lt;code&gt;Standard_E8s_v5&lt;/code&gt; caps at 12,800 IOPS / 290 MB/s; &lt;code&gt;Standard_E32s_v5&lt;/code&gt; reaches 51,200 IOPS / 865 MB/s), making standard SKU selection an expensive way to buy storage.&lt;/li&gt;
&lt;li&gt;Under Oracle’s Authorized Cloud Environments policy, 2 available vCPUs with hyper-threading equal 1 Oracle Processor license ($47,500 list + $10,450/yr support), and guest-OS CPU masking (&lt;code&gt;maxcpus&lt;/code&gt;) is rejected in audits.&lt;/li&gt;
&lt;li&gt;Constrained-core SKUs (&lt;code&gt;Standard_E32-8s_v5&lt;/code&gt;) expose 8 active vCPUs at the hypervisor boundary while preserving the parent chassis’s 256 GiB RAM and 51,200 IOPS, cutting Oracle Processor exposure by 75%.&lt;/li&gt;
&lt;li&gt;Policy enforcement must span both pre-merge CI (OPA Conftest on &lt;code&gt;terraform plan&lt;/code&gt; JSON) and runtime control plane (Azure Policy &lt;code&gt;deny&lt;/code&gt; on ARM &lt;code&gt;virtualMachines/write&lt;/code&gt;) to prevent incident-time portal drift.&lt;/li&gt;
&lt;li&gt;Constrained-core VMs still bill at the parent Azure compute rate and introduce a 32:1 RAM-to-vCPU ratio, requiring DBRM CPU caps, conservative SGA sizing, &lt;code&gt;irqbalance&lt;/code&gt;, and &lt;code&gt;zone&lt;/code&gt;-pinned &lt;code&gt;PremiumV2_LRS&lt;/code&gt; disks with &lt;code&gt;caching = &quot;None&quot;&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Sizing standard Azure VMs for Oracle storage IOPS exposes 2x to 4x more vCPUs than the database needs, creating multi-million-dollar Oracle licensing liabilities.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Standardize on Azure Constrained-Core VMs (&lt;code&gt;Standard_E32-8s_v5&lt;/code&gt; or &lt;code&gt;Standard_E64-16s_v5&lt;/code&gt;) paired with &lt;code&gt;PremiumV2_LRS&lt;/code&gt; disks, governed by Terraform intent modules, OPA CI checks, and Azure Policy &lt;code&gt;deny&lt;/code&gt; rules.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Compare &lt;code&gt;nproc&lt;/code&gt; and Azure SKU metadata against 30-day CPU and uncached IOPS telemetry; a &lt;code&gt;Standard_E32-8s_v5&lt;/code&gt; retains the full 51,200 IOPS and 865 MB/s ceiling while reporting 8 active vCPUs (4 Oracle Processors) to the guest kernel.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Query your Azure subscriptions today for all VMs tagged or running Oracle, compare active vCPU counts against average CPU and peak disk IOPS, and schedule a Data Guard standby-first migration for any high-core, I/O-bound host.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Oracle Corporation:&lt;/strong&gt; &lt;em&gt;Licensing Oracle Software in the Cloud Computing Environment&lt;/em&gt; (Authorized Cloud Environments policy: 2 vCPUs = 1 Processor with multi-threading; SE2 8-vCPU limit).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle Corporation:&lt;/strong&gt; &lt;em&gt;Oracle Technology Global Price List&lt;/em&gt; (Enterprise Edition Processor $47,500 list, $10,450 annual support).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Microsoft Corporation:&lt;/strong&gt; &lt;em&gt;Esv5-series virtual machine specifications&lt;/em&gt; and &lt;em&gt;Constrained vCPU capable VM sizes&lt;/em&gt; (Microsoft Learn).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Microsoft Corporation:&lt;/strong&gt; &lt;em&gt;Azure Premium SSD v2&lt;/em&gt; and HashiCorp &lt;code&gt;azurerm_managed_disk&lt;/code&gt; documentation (&lt;code&gt;PremiumV2_LRS&lt;/code&gt; baseline 3,000 IOPS / 125 MB/s, &lt;code&gt;zone&lt;/code&gt; requirement, &lt;code&gt;caching = &quot;None&quot;&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>OpenSearch k-NN Architecture: Shards, Segments, HNSW, and Memory</title><link>https://rajivonai.com/blog/2026-06-19-opensearch-knn-architecture-shards-segments-hnsw-memory/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-19-opensearch-knn-architecture-shards-segments-hnsw-memory/</guid><description>An infrastructure view of OpenSearch vector search, covering k-NN fields, HNSW, shards, segments, refresh, merges, memory, node sizing, and operational gotchas.</description><pubDate>Wed, 24 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The first OpenSearch vector demo feels simple: add a vector field, index embeddings, run k-NN, return the closest chunks. The production cluster sees something else: shard fanout, segment refreshes, HNSW graph memory, merge pressure, cache churn, and nodes sized for keyword search suddenly carrying a different kind of workload.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;OpenSearch vector search is not just a new query type. It is a distributed indexing workload running inside a search engine. k-NN fields, HNSW graph construction, shard count, segment lifecycle, refresh behavior, merge pressure, and memory placement all affect latency, recall, and cost.&lt;/p&gt;
&lt;p&gt;The infrastructure decision is whether the cluster is sized for search plus vector retrieval, not whether the API can accept embeddings. A vector-heavy OpenSearch workload needs deliberate mapping design, shard sizing, refresh expectations, merge monitoring, memory headroom, and restore/rebuild planning.&lt;/p&gt;
&lt;p&gt;OpenSearch k-NN behavior depends on engine version, vector engine, method parameters, and deployment mode. OpenSearch supports three k-NN engines that implement HNSW — nmslib (deprecated in favor of the other two), Faiss, and Lucene — and they don’t behave identically: Faiss and Lucene apply filters during graph traversal so restrictive filters still return an accurate top-k, while the Lucene engine ignores &lt;code&gt;ef_search&lt;/code&gt; entirely and dynamically sets search breadth to the requested &lt;code&gt;k&lt;/code&gt;. Confirm which engine an index actually uses before assuming a tuning parameter from one engine’s documentation applies.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;OpenSearch documentation positions the project around search, analytics, and observability, with vector search documented as another retrieval capability inside that search platform rather than the platform’s primary purpose. When a team already operates OpenSearch as a search stack, the lowest-friction path for semantic retrieval is often to add vector fields rather than introduce another vector database.&lt;/p&gt;
&lt;p&gt;That path can be right. It can also hide the fact that vector search changes the physical workload.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Keyword search engineers think in analyzers, inverted indexes, shards, replicas, refresh intervals, and segment merges. Vector search adds another data structure to that world.&lt;/p&gt;
&lt;p&gt;The common production mistakes are predictable:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Shards are chosen from old keyword-search heuristics.&lt;/li&gt;
&lt;li&gt;Vector fields are added without memory modeling.&lt;/li&gt;
&lt;li&gt;Refresh behavior is ignored until fresh embeddings do not appear.&lt;/li&gt;
&lt;li&gt;Segment merges are treated as background noise until latency spikes.&lt;/li&gt;
&lt;li&gt;HNSW parameters are copied from examples without recall tests.&lt;/li&gt;
&lt;li&gt;Node sizing assumes CPU and heap are the only limits.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The result is a cluster that appears healthy under ordinary search metrics but degrades under vector query load.&lt;/p&gt;
&lt;h2 id=&quot;core-technical-explanation&quot;&gt;Core Technical Explanation&lt;/h2&gt;
&lt;p&gt;OpenSearch stores indexed documents across shards. Each shard is a Lucene index made of segments. As documents are indexed and refreshed, new segments become searchable. Background merges consolidate segments over time. Vector fields add approximate nearest-neighbor data structures, commonly HNSW, into that segment-oriented lifecycle.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Docs[source documents] --&gt; Ingest[indexing pipeline]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Ingest --&gt; Shard[primary shard]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Shard --&gt; Segment[Lucene segments]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Segment --&gt; Text[inverted index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Segment --&gt; Vector[k-NN vector structures]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Vector --&gt; HNSW[HNSW graph search]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[search request] --&gt; Fanout[shard fanout]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Fanout --&gt; HNSW&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    HNSW --&gt; Merge[merge and refresh pressure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The important point is that vector search is not global by default. A query fans out to shards, each shard searches its local data, and results are combined. Shard count therefore changes both parallelism and candidate distribution.&lt;/p&gt;
&lt;p&gt;HNSW stores graph relationships that make approximate search fast. The construction-time parameter &lt;code&gt;m&lt;/code&gt; (bidirectional links per node, default 16, typical range 8–64) and &lt;code&gt;ef_construction&lt;/code&gt; (build-time candidate pool size) both trade recall against memory and build time — a larger &lt;code&gt;m&lt;/code&gt; improves recall but increases RAM and build time directly, since it’s more edges per node stored in the graph. Those choices affect memory, indexing throughput, and query CPU, and copying defaults from a keyword-search-sized cluster without revisiting them for a vector workload is a common source of surprise.&lt;/p&gt;
&lt;p&gt;Segments matter because search happens over searchable segment structures. Frequent refreshes can produce many small segments. Merges can reduce segment count but consume I/O and CPU. With vector fields, a merge has to rebuild or re-serialize the HNSW graph for the merged segment, not just concatenate postings lists the way a keyword-only merge does — that’s why vector-field merges are commonly heavier than teams expect coming from a keyword-search background.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;A production operating model should start with index design:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Mappings.&lt;/strong&gt; Define vector field dimension, method, and purpose explicitly. Do not create one generic &lt;code&gt;embedding&lt;/code&gt; field for every future use case. Store model version and source text fields needed for debugging.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Shard sizing.&lt;/strong&gt; Choose shard count from document volume, vector count, tenant distribution, query fanout, and node capacity. Too few shards can create large hot shards. Too many shards create overhead and fanout cost.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Refresh expectations.&lt;/strong&gt; Decide whether embeddings need near-real-time visibility. Lower refresh frequency can improve indexing efficiency, but search freshness changes. Higher refresh frequency can increase segment churn.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Merge monitoring.&lt;/strong&gt; Watch merge backlog and disk I/O. Vector-heavy indexes may make merge pressure more visible during backfills or high update rates.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Node sizing.&lt;/strong&gt; Size for vector memory, CPU, heap, disk, and network together. Keyword-only nodes may not be appropriate for vector-heavy workloads.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Recall validation.&lt;/strong&gt; Keep a query set and compare exact or higher-recall baselines against production settings. Fast k-NN that misses the right incident document is still a failure.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Production symptom&lt;/th&gt;&lt;th&gt;Recovery path&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Over-sharding&lt;/td&gt;&lt;td&gt;High overhead and unstable latency&lt;/td&gt;&lt;td&gt;Reindex into fewer shards and test fanout&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Under-sharding&lt;/td&gt;&lt;td&gt;Large shards and slow recovery&lt;/td&gt;&lt;td&gt;Reindex with better shard sizing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Too frequent refresh&lt;/td&gt;&lt;td&gt;Segment churn and indexing pressure&lt;/td&gt;&lt;td&gt;Increase refresh interval for backfills&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Merge backlog&lt;/td&gt;&lt;td&gt;Query latency spikes under write load&lt;/td&gt;&lt;td&gt;Throttle indexing and watch merge metrics&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Vector memory underestimated&lt;/td&gt;&lt;td&gt;Node pressure or degraded query latency&lt;/td&gt;&lt;td&gt;Resize nodes or reduce vector workload&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;HNSW settings copied blindly&lt;/td&gt;&lt;td&gt;Recall or indexing cost surprises&lt;/td&gt;&lt;td&gt;Tune against a query set&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Uneven tenant distribution&lt;/td&gt;&lt;td&gt;Hot shards and noisy neighbors&lt;/td&gt;&lt;td&gt;Route, partition, or split indexes by tenant class&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-and-tenancy-notes&quot;&gt;Security and Tenancy Notes&lt;/h2&gt;
&lt;p&gt;OpenSearch vector search must preserve the same tenant and entitlement boundaries as keyword search. Tenant, region, deletion state, and compliance eligibility should be hard filters, not ranking boosts.&lt;/p&gt;
&lt;p&gt;The operational risk is that shard or index design becomes a security shortcut. A shared index can be correct if every query enforces filters and tests prove it. Dedicated tenant indexes or clusters can provide stronger isolation but raise cost and operational complexity.&lt;/p&gt;
&lt;h2 id=&quot;cost-notes&quot;&gt;Cost Notes&lt;/h2&gt;
&lt;p&gt;Cost comes from vector storage, replicas, larger nodes, indexing CPU, merge I/O, snapshots, and query fanout. Over-sharding is a cost multiplier because every shard carries overhead and participates in more distributed work.&lt;/p&gt;
&lt;p&gt;Do not size from raw embedding bytes only. The HNSW graph itself (edges per node scale with &lt;code&gt;m&lt;/code&gt;) adds memory overhead beyond the raw vector data, and that overhead multiplies with replica count since each replica holds its own graph copy. Include graph structures, segment overhead, replica count, disk watermarks, snapshot storage, and rebuild headroom in the sizing model, not just &lt;code&gt;vector_dimension × 4 bytes × document_count&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;observability-notes&quot;&gt;Observability Notes&lt;/h2&gt;
&lt;p&gt;Track:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;k-NN query latency by index and shard.&lt;/li&gt;
&lt;li&gt;Indexing throughput and refresh latency.&lt;/li&gt;
&lt;li&gt;Segment count and merge activity.&lt;/li&gt;
&lt;li&gt;Heap, native memory, and circuit breaker signals where applicable.&lt;/li&gt;
&lt;li&gt;Disk watermarks and snapshot duration.&lt;/li&gt;
&lt;li&gt;Hot shards and node-level query skew.&lt;/li&gt;
&lt;li&gt;Recall checks from a fixed query set.&lt;/li&gt;
&lt;li&gt;Empty-result and under-return rates after filters.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Dashboards should separate lexical latency from vector latency. A blended search latency chart hides the failure surface.&lt;/p&gt;
&lt;h2 id=&quot;backup-restore-and-dr-notes&quot;&gt;Backup, Restore, and DR Notes&lt;/h2&gt;
&lt;p&gt;Snapshots protect the index, but the rebuild path still matters. If source systems can replay documents and embeddings, OpenSearch remains a derived index. If the OpenSearch index is the only copy of embeddings or ranking features, it has become a source of truth and needs stronger recovery guarantees.&lt;/p&gt;
&lt;p&gt;Restore validation should include index health, shard allocation, vector query latency, and a retrieval query set. A restored cluster with all shards green can still be operationally weak if vector search is slow or recall has changed.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;What vector field dimensions and methods are required?&lt;/li&gt;
&lt;li&gt;How many vectors live in each shard at current and projected scale?&lt;/li&gt;
&lt;li&gt;Are tenants evenly distributed across shards?&lt;/li&gt;
&lt;li&gt;What refresh delay is acceptable for new embeddings?&lt;/li&gt;
&lt;li&gt;How will backfills affect segment creation and merge pressure?&lt;/li&gt;
&lt;li&gt;What memory headroom exists for vector search?&lt;/li&gt;
&lt;li&gt;How are HNSW parameters validated against recall?&lt;/li&gt;
&lt;li&gt;Can the index be rebuilt from source systems?&lt;/li&gt;
&lt;li&gt;Are lexical and vector metrics separated?&lt;/li&gt;
&lt;li&gt;What shard design change requires a reindex?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; OpenSearch clusters sized and shaped for keyword search inherit vector fields without revisiting shard count, HNSW parameters, or memory headroom, and the cluster looks healthy on ordinary search metrics while degrading under vector query load.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Treat vector fields as a new physical access pattern with its own capacity model — size shards from vector count and query fanout, validate HNSW &lt;code&gt;m&lt;/code&gt;/&lt;code&gt;ef_construction&lt;/code&gt; against a recall baseline, and separate lexical from vector latency in dashboards.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; A recall test against a fixed query set matches the baseline after any HNSW parameter or shard-count change, and merge/segment metrics stay stable during embedding backfills.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, confirm which k-NN engine (Faiss, Lucene, or the deprecated nmslib) the vector-bearing indices actually use, since tuning parameters and filter behavior differ between them.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;OpenSearch vector search and k-NN documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/&quot;&gt;https://docs.opensearch.org/latest/vector-search/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch k-NN methods and engines (nmslib/Faiss/Lucene, HNSW parameters): &lt;a href=&quot;https://docs.opensearch.org/latest/mappings/supported-field-types/knn-methods-engines/&quot;&gt;https://docs.opensearch.org/latest/mappings/supported-field-types/knn-methods-engines/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch approximate k-NN search documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/vector-search-techniques/approximate-knn/&quot;&gt;https://docs.opensearch.org/latest/vector-search/vector-search-techniques/approximate-knn/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenSearch index management and segment merge documentation: &lt;a href=&quot;https://docs.opensearch.org/latest/im-plugin/&quot;&gt;https://docs.opensearch.org/latest/im-plugin/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Amazon OpenSearch Service sizing and vector workload guidance (if using the managed service): &lt;a href=&quot;https://docs.aws.amazon.com/opensearch-service/latest/developerguide/sizing-domains.html&quot;&gt;https://docs.aws.amazon.com/opensearch-service/latest/developerguide/sizing-domains.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>cloud</category><category>checklist</category></item><item><title>Multi-Tenant AWS RDS Provisioning: Designing Cell-Based Isolation Architectures</title><link>https://rajivonai.com/blog/2024-09-12-amazon-rds-oracle-terraform-production-architecture/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-09-12-amazon-rds-oracle-terraform-production-architecture/</guid><description>Why deploying thousands of databases into a single cloud virtual network causes catastrophic quota exhaustion and security cross-talk, and how to engineer a cell-based architecture across AWS Organizations.</description><pubDate>Mon, 22 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Attempting to host hundreds or thousands of enterprise tenant databases inside a single flat AWS VPC guarantees service quota exhaustion, lateral network exposure, and an account-wide blast radius during operational incidents.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; A single AWS account and VPC hits hard quotas (40 default RDS instances per Region, 60 inbound rules per security group, 500 default routes per route table) and blocks per-tenant cryptographic offboarding when keys are shared.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Partition the fleet into bounded cells of 25 to 50 RDS Oracle instances per AWS Organizations account, exposed to the application tier over AWS PrivateLink with overlapping &lt;code&gt;10.0.0.0/24&lt;/code&gt; CIDRs and one KMS Customer Managed Key per tenant.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Amazon RDS Proxy does not support Oracle, and Network Load Balancer IP targets go stale on Multi-AZ failover unless an RDS Event-driven Lambda refreshes the target group; AWS Organizations also does not provide a delegated administrator for RDS lifecycle operations.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2024-09-12-amazon-rds-oracle-terraform-production-architecture.svg&quot; alt=&quot;Comparison of a flat single-VPC RDS deployment against a cell-based AWS Organizations architecture showing 100 percent vs 1-cell blast radius&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Enterprise SaaS providers in regulated sectors—core banking, healthcare revenue cycle management, and defense logistics—frequently operate under customer data sovereignty clauses. Frameworks such as SOC 2 Type II, PCI-DSS, HIPAA, and FedRAMP often prohibit logical multi-tenancy where tenants share tables separated only by a &lt;code&gt;tenant_id&lt;/code&gt; column. Contracts instead mandate hard isolation: each enterprise customer receives a dedicated database instance with independent storage encryption.&lt;/p&gt;
&lt;p&gt;To deliver dedicated Oracle databases on AWS without managing operating systems, storage volumes, or binary patching directly, platform teams adopt Amazon RDS for Oracle. In early deployments, the provisioning pipeline follows a centralized pattern: one primary production VPC in a single AWS account, Multi-AZ private subnets behind a Transit Gateway, and a Terraform or OpenTofu module that creates an &lt;code&gt;aws_db_instance&lt;/code&gt; per tenant inside that shared database subnet.&lt;/p&gt;
&lt;p&gt;That pattern works for the first few dozen customers. Once tenant count grows past a single account’s default service quotas and subnet isolation boundaries, onboarding stalls and every network or IAM change carries fleet-wide risk.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A flat, centralized RDS deployment breaks along four documented AWS service limits and isolation constraints:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;RDS Regional Instance Quota (40 Default):&lt;/strong&gt; AWS enforces a default aggregate quota of 40 DB instances per account per Region across Amazon RDS, Aurora, Neptune, and DocumentDB (with additional engine-specific or license-included sub-limits). Reaching 1,000 or 5,000 tenant databases in one account requires repeated quota escalations and concentrates the entire customer base under a single IAM and API rate-limit domain.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Security Group Rule Cap (60 Inbound Rules):&lt;/strong&gt; By default, a VPC security group allows 60 inbound IPv4 rules and 60 outbound IPv4 rules, and the product of security groups per network interface multiplied by rules per security group cannot exceed 1,000. When hundreds of tenant RDS instances share a &lt;code&gt;/16&lt;/code&gt; database subnet, an application container compromised via remote code execution can scan Oracle listener ports (&lt;code&gt;1521&lt;/code&gt; or &lt;code&gt;2484&lt;/code&gt;) across neighboring tenants unless granular per-tenant rules exist—which the 60-rule cap prevents at scale.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Route Table Scaling Ceiling (500 Default / 1,000 Max):&lt;/strong&gt; Isolating each tenant into its own VPC and interconnecting those VPCs through a central Transit Gateway or peering mesh collides with both RFC 1918 IPv4 address exhaustion and VPC route table quotas. Each VPC route table defaults to 500 non-propagated routes and is adjustable only up to 1,000 routes, where AWS notes that network performance may degrade.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;KMS Key Co-Mingling on Offboarding:&lt;/strong&gt; Encrypting multiple tenant databases under one shared AWS KMS key prevents clean cryptographic erasure. When a customer terminates their contract and requires verified data destruction, deleting a shared KMS key renders active tenants’ databases and EBS snapshots permanently unreadable.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;./2024-09-12-amazon-rds-oracle-terraform-production-architecture-2-constraints.svg&quot; alt=&quot;Four documented AWS quotas and cryptographic constraints that break flat multi-tenant RDS fleets&quot;&gt;&lt;/p&gt;
&lt;p&gt;Consider the arithmetic of a 2,500-tenant fleet. At 50 RDS instances per cell, the platform runs 50 isolated cell accounts; an account-level misconfiguration or Regional RDS quota ceiling inside one cell affects at most 2% of the fleet (50 tenants) rather than 100%.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do platform teams automate provisioning for thousands of isolated RDS Oracle instances with non-overlapping application connectivity, bounded blast radius, and clean per-tenant cryptographic erasure without hitting AWS account and VPC quotas?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams evaluating multi-tenant RDS isolation typically compare three network and account topologies before committing to Terraform modules.&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Topology&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Isolation Boundary&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Routing Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Flat Monolithic VPC&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Shared subnet + security groups&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Direct VPC routing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Hits 40-instance default RDS quota, 60-rule SG cap, and 100% account blast radius&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;VPC-Per-Tenant + TGW&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Dedicated VPC per DB&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;AWS Transit Gateway&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exhausts RFC 1918 CIDRs and hits 500–1,000 routes per route table&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Cell Accounts + PrivateLink&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Dedicated AWS account per 25–50 DBs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;AWS PrivateLink + Internal NLB&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires cross-account orchestration and NLB target IP sync on failover&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chosen&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The &lt;strong&gt;Flat Monolithic VPC&lt;/strong&gt; minimizes initial Terraform boilerplate, because every tenant shares one &lt;code&gt;aws_db_subnet_group&lt;/code&gt; and one Transit Gateway attachment. It fails as soon as compliance audits require lateral movement prevention between tenants or when a single account’s RDS API and instance quotas become a deployment bottleneck.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;VPC-Per-Tenant + Transit Gateway&lt;/strong&gt; design establishes a clean Layer 3 network boundary per customer, but it transfers the scaling bottleneck to IPv4 address planning and route propagation. Even with &lt;code&gt;/26&lt;/code&gt; subnets, thousands of routed VPCs consume large blocks of &lt;code&gt;10.0.0.0/8&lt;/code&gt; space and force central hub route tables toward the 1,000-route ceiling.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cell-Based AWS Organizations Accounts with PrivateLink&lt;/strong&gt; resolves both limits. The AWS account acts as a hard IAM, billing, and quota boundary, while PrivateLink decouples routing so every cell VPC reuses the exact same &lt;code&gt;10.0.0.0/24&lt;/code&gt; CIDR block.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-09-12-amazon-rds-oracle-terraform-production-architecture-3-options.svg&quot; alt=&quot;Comparison of Flat Monolithic VPC, VPC-Per-Tenant with TGW, and Cell-Based Accounts with PrivateLink&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-cell-based-aws-organizations-architecture&quot;&gt;The Cell-Based AWS Organizations Architecture&lt;/h2&gt;
&lt;p&gt;The target architecture partitions the database fleet into standardized administrative units called &lt;strong&gt;Cells&lt;/strong&gt;. Each cell is an independent AWS account provisioned under a dedicated Organizational Unit (OU) in AWS Organizations and sized to hold 25 to 50 RDS Oracle instances—staying comfortably near or slightly above the 40-instance default quota (with a single predictable quota bump to 60 if needed) while keeping the per-cell blast radius small.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-09-12-amazon-rds-oracle-terraform-production-architecture-4-architecture.svg&quot; alt=&quot;Layered architecture showing AWS Organizations SCPs governing isolated PrivateLink database cells&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;control-plane-aws-organizations-and-guardrail-scps&quot;&gt;Control Plane: AWS Organizations and Guardrail SCPs&lt;/h3&gt;
&lt;p&gt;The management plane creates each cell account via &lt;code&gt;aws_organizations_account&lt;/code&gt; and places it under a Database OU governed by Service Control Policies (SCPs). SCPs enforce baseline invariants that individual cell Terraform runs cannot override: denying &lt;code&gt;ec2:CreateInternetGateway&lt;/code&gt; and &lt;code&gt;ec2:AttachInternetGateway&lt;/code&gt; so no database subnet can ever be routed to the public internet, and denying &lt;code&gt;rds:CreateDBInstance&lt;/code&gt; when &lt;code&gt;rds:StorageEncrypted&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# cell_account_factory.tf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;aws_organizations_account&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;database_cell&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                       =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;prod-db-cell-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;cell_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  email&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                      =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;aws-db-cell-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;cell_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;@enterprise.com&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  parent_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                  =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; aws_organizations_organizational_unit&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;database_ou&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  iam_user_access_to_billing&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;DENY&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  close_on_deletion&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;          =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;aws_organizations_policy&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;cell_guardrail&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;DB-Cell-Strict-Guardrail&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  description&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Deny internet gateways and unencrypted RDS instances&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  content&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; jsonencode&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Version &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;2012-10-17&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Statement &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Sid      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;DenyInternetGateways&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Effect   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Deny&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Action   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ec2:CreateInternetGateway&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ec2:AttachInternetGateway&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Resource &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;*&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Sid      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;EnforceEncryptedStorage&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Effect   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Deny&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Action   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;rds:CreateDBInstance&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Resource &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;*&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Condition &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          Bool &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; { &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;rds:StorageEncrypted&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  })&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;network-plane-overlapping-cidrs-over-privatelink&quot;&gt;Network Plane: Overlapping CIDRs over PrivateLink&lt;/h3&gt;
&lt;p&gt;Inside each cell account, Terraform provisions a standardized &lt;code&gt;10.0.0.0/24&lt;/code&gt; VPC split across Availability Zones (&lt;code&gt;10.0.0.0/26&lt;/code&gt; and &lt;code&gt;10.0.0.64/26&lt;/code&gt;). An internal Network Load Balancer (NLB) sits in front of the cell’s RDS instances and attaches to an &lt;code&gt;aws_vpc_endpoint_service&lt;/code&gt; restricted to the application account’s ARN via &lt;code&gt;allowed_principals&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Because AWS PrivateLink proxies TCP connections at the Hyperplane layer between the application VPC’s Interface Endpoint and the cell’s NLB, the application VPC and the cell VPC never exchange IP routes. Every cell reuses &lt;code&gt;10.0.0.0/24&lt;/code&gt;, eliminating IPv4 CIDR exhaustion across thousands of databases.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# cell_network_privatelink.tf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;aws_vpc&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;cell_vpc&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  cidr_block&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;           =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;10.0.0.0/24&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  enable_dns_hostnames&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  enable_dns_support&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;   =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;aws_lb&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;cell_nlb&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;               =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;nlb-cell-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;cell_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  internal&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;           =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  load_balancer_type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;network&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  subnets&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [aws_subnet&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db_subnet_a&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id, aws_subnet&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db_subnet_b&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;id]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;aws_vpc_endpoint_service&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;cell_endpoint_service&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  acceptance_required&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  network_load_balancer_arns&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [aws_lb&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;cell_nlb&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;arn]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  allowed_principals&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;         =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;application_account_arn]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To avoid exhausting Elastic Network Interfaces (ENIs) and subnet IPs in the application VPC, do not create one PrivateLink endpoint service per tenant database. Instead, multiplex all 25 to 50 tenant databases inside a cell through the cell’s shared NLB using distinct listener ports mapped to each tenant’s IP target group.&lt;/p&gt;
&lt;h3 id=&quot;data-plane-per-tenant-kms-cmks-and-multi-az-rds-oracle&quot;&gt;Data Plane: Per-Tenant KMS CMKs and Multi-AZ RDS Oracle&lt;/h3&gt;
&lt;p&gt;Each tenant database is provisioned with its own dedicated &lt;code&gt;aws_kms_key&lt;/code&gt; (&lt;code&gt;deletion_window_in_days&lt;/code&gt; between 7 and 30 days) and a Multi-AZ &lt;code&gt;aws_db_instance&lt;/code&gt; running Oracle 19c (&lt;code&gt;engine = &quot;oracle-ee&quot;&lt;/code&gt;, &lt;code&gt;engine_version = &quot;19.0.0.0.ru-2024-04.rur-2024-04.r1&quot;&lt;/code&gt;). When a customer offboards, revoking grants and scheduling deletion of that tenant’s CMK cryptographically shreds the active volume and all associated automated EBS snapshots without touching any other tenant in the cell.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# tenant_database_instance.tf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;aws_kms_key&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;tenant_cmek&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  description&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;             =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Dedicated encryption key for Tenant &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tenant_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  deletion_window_in_days&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  enable_key_rotation&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;     =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;aws_db_instance&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;tenant_oracle_db&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  identifier&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                 =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;tenant-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tenant_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;-oracle&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  engine&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                     =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;oracle-ee&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  engine_version&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;             =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;19.0.0.0.ru-2024-04.rur-2024-04.r1&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  instance_class&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;             =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;db.r6i.xlarge&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  allocated_storage&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;          =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  max_allocated_storage&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  storage_type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;               =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;gp3&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  multi_az&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                   =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  publicly_accessible&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  db_subnet_group_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; aws_db_subnet_group&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;cell_db_subnets&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  kms_key_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                 =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; aws_kms_key&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tenant_cmek&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;arn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  storage_encrypted&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;          =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  auto_minor_version_upgrade&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  backup_retention_period&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 35&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  deletion_protection&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  skip_final_snapshot&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  final_snapshot_identifier&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;final-snap-tenant-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tenant_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Cell provisioning and tenant onboarding execute in four ordered stages, each with a clear validation gate and rollback point.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-09-12-amazon-rds-oracle-terraform-production-architecture-5-flow.svg&quot; alt=&quot;Four-step cell deployment and failover IP synchronization flow&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Cell Account Creation and SCP Attachment:&lt;/strong&gt; The Account Factory pipeline creates the AWS Organizations account in the Database OU and verifies that the guardrail SCPs are active. &lt;em&gt;Validation gate:&lt;/em&gt; attempt a dry-run unencrypted &lt;code&gt;CreateDBInstance&lt;/code&gt; or &lt;code&gt;CreateInternetGateway&lt;/code&gt; call and confirm IAM denies it. &lt;em&gt;Rollback:&lt;/em&gt; move the empty account to a quarantine OU before any tenant data exists.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cell Network and PrivateLink Bootstrap:&lt;/strong&gt; Terraform deploys the &lt;code&gt;10.0.0.0/24&lt;/code&gt; VPC, Multi-AZ subnets, internal NLB, and &lt;code&gt;aws_vpc_endpoint_service&lt;/code&gt;, and creates the matching Interface Endpoint in the application account. &lt;em&gt;Validation gate:&lt;/em&gt; verify endpoint state transitions to &lt;code&gt;available&lt;/code&gt; and DNS resolves from the application EKS cluster. &lt;em&gt;Rollback:&lt;/em&gt; destroy the network module in the empty cell.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tenant KMS and RDS Oracle Provisioning:&lt;/strong&gt; For each tenant assigned to the cell, the pipeline creates &lt;code&gt;aws_kms_key.tenant_cmek&lt;/code&gt;, provisions the Multi-AZ RDS Oracle instance, resolves the primary instance’s private IP, and registers it in the NLB target group on the tenant’s assigned listener port. &lt;em&gt;Validation gate:&lt;/em&gt; NLB target health check passes on the Oracle listener port (&lt;code&gt;1521&lt;/code&gt; internal target) and an encrypted SQL*Net handshake succeeds from the application tier. &lt;em&gt;Rollback:&lt;/em&gt; before customer data loads, destroy the tenant stack and cancel key deletion if needed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Failover IP Synchronization Loop:&lt;/strong&gt; Because Amazon RDS Proxy does not support Oracle, an NLB pointing to static RDS IP targets will drop connections after a Multi-AZ failover when RDS promotes the standby and updates its internal DNS to a new IP. To prevent this, deploy an Amazon RDS Event Subscription listening for failover events (such as &lt;code&gt;RDS-EVENT-0005&lt;/code&gt; / primary switchover notifications) routed through SNS to an AWS Lambda function. The Lambda resolves the RDS endpoint DNS name, deregisters the stale IP from the NLB target group, and registers the new primary IP. &lt;em&gt;Validation gate:&lt;/em&gt; execute a manual Multi-AZ reboot with failover in staging and confirm the NLB target updates automatically.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Note on fleet management: AWS Organizations does not provide a native delegated administrator role for Amazon RDS lifecycle operations (unlike GuardDuty, AWS Config, or CloudFormation StackSets). Cross-account RDS patching, parameter group enforcement, and snapshot audits must be driven by a central CI/CD runner or CloudFormation StackSets assuming scoped IAM execution roles in each cell account, rather than relying on a non-existent RDS delegated admin feature.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Several parts of this design come directly from documented AWS service behavior and published guidance:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;AWS Well-Architected Reliability Pillar (Cell-Based Architectures):&lt;/strong&gt; AWS documents partitioning workloads into independent cells with a fixed maximum capacity so that failures, bad deployments, and noisy neighbors remain bounded to a single cell rather than cascading across the entire fleet.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AWS PrivateLink + RDS IP Target Synchronization:&lt;/strong&gt; AWS Architecture guidance explicitly documents that NLB IP target groups in front of RDS do not track DNS IP changes during Multi-AZ failovers, requiring either RDS Proxy (unavailable for Oracle) or an RDS Event + SNS + Lambda synchronization loop.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;KMS Key Deletion Behavior:&lt;/strong&gt; AWS KMS documentation specifies a mandatory 7-to-30-day waiting period (&lt;code&gt;deletion_window_in_days&lt;/code&gt;) before a scheduled CMK is permanently destroyed, providing a built-in safety buffer against accidental offboarding webhooks.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I have not operated a 5,000-instance RDS Oracle fleet personally; the sizing target of 25 to 50 databases per cell is a documented architectural pattern derived from balancing AWS’s 40-instance default Regional RDS quota against blast-radius containment and cross-account CI/CD overhead.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Cell-based isolation eliminates single-account quota ceilings, but it introduces cross-account operational failure modes that must be engineered explicitly.&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Multi-AZ RDS Oracle failover&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary IP changes; NLB IP target group points to old standby IP and drops traffic.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;NLB &lt;code&gt;UnHealthyHostCount&lt;/code&gt; &gt; 0 and application ORA-12541 / timeout errors.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Subscribe RDS failover events to SNS + Lambda to re-register the resolved RDS DNS IP.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;1 VPC endpoint per tenant DB&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Application VPC exhausts subnet IPv4s and ENI quotas across hundreds of endpoints.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;InsufficientAddressSpace&lt;/code&gt; or ENI limit errors in application account.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Multiplex 25–50 tenant DBs per cell behind one NLB using distinct listener ports.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Premature CMK deletion webhook&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Faulty offboarding triggers &lt;code&gt;ScheduleKeyDeletion&lt;/code&gt; on an active tenant’s KMS key.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;CloudTrail alert on &lt;code&gt;kms:ScheduleKeyDeletion&lt;/code&gt; / &lt;code&gt;kms:DisableKey&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce 7–30 day &lt;code&gt;deletion_window_in_days&lt;/code&gt; and require two-person approval in the control plane.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Burst onboarding hits &lt;code&gt;CreateAccount&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rapid customer signups exceed AWS Organizations &lt;code&gt;CreateAccount&lt;/code&gt; concurrency/rate limits.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;TooManyRequestsException&lt;/code&gt; or &lt;code&gt;ConcurrentModificationException&lt;/code&gt; in Account Factory.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Maintain a pre-warmed pool of empty, SCP-hardened cell accounts ready for assignment.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Cross-account CI/CD STS storms&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Central runner assumes IAM roles across dozens of cells simultaneously, hitting STS limits.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Terraform provider throttling errors during fleet-wide parameter updates.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Stagger rollouts by cell cohort with exponential backoff and per-cell worker queues.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Option Group / patch drift&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Oracle timezone or RU patches applied inconsistently across cell accounts.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;AWS Config custom rules flagging mismatched &lt;code&gt;engine_version&lt;/code&gt; or option groups.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Publish versioned golden Terraform modules / CloudFormation StackSets from one central repo.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2024-09-12-amazon-rds-oracle-terraform-production-architecture-6-failures.svg&quot; alt=&quot;Failure-path overlay showing stale NLB IP targets, ENI exhaustion, premature KMS deletion, and account creation throttling&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Account boundaries are the only hard AWS quota reset:&lt;/strong&gt; Splitting fleets into 25–50 database cells per AWS Organizations account isolates RDS instance quotas, IAM blast radius, and API rate limits simultaneously.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PrivateLink eliminates CIDR and route-table scaling limits:&lt;/strong&gt; Exposing cell NLBs through &lt;code&gt;aws_vpc_endpoint_service&lt;/code&gt; lets every cell reuse &lt;code&gt;10.0.0.0/24&lt;/code&gt;, avoiding Transit Gateway route caps (500 default / 1,000 max) and RFC 1918 exhaustion.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle on PrivateLink requires automated NLB IP sync:&lt;/strong&gt; Because RDS Proxy does not support Oracle, an RDS Event + SNS + Lambda loop is mandatory to update NLB IP targets whenever Multi-AZ failover changes the primary IP.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;One KMS CMK per tenant makes offboarding deterministic:&lt;/strong&gt; Dedicated customer-managed keys with a 7–30 day deletion window allow verifiable cryptographic shredding of a single tenant’s data and snapshots without affecting cell neighbors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Do not rely on RDS delegated administration:&lt;/strong&gt; AWS Organizations does not support a delegated administrator for RDS lifecycle operations; fleet-wide patching and compliance checks require StackSets or CI/CD runners with scoped cross-account IAM roles.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Hosting dedicated enterprise RDS Oracle instances in one flat VPC hits AWS’s 40-instance default Regional quota, 60-rule security group cap, and shared KMS offboarding lock-in.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Group 25 to 50 tenant databases per AWS Organizations cell account behind a PrivateLink-backed internal NLB, with one KMS CMK per tenant and an Event-driven Lambda keeping NLB IP targets current across Multi-AZ failovers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; In a staging cell, trigger a Multi-AZ RDS reboot with failover and verify that the Lambda updates the NLB target group IP and restores application connectivity before health checks expire.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your primary production account’s &lt;code&gt;DescribeDBInstances&lt;/code&gt; count and KMS key sharing today; if more than 25 regulated tenants share a single VPC or KMS key, stand up your first SCP-guarded cell account and PrivateLink NLB module this week.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>Postgres Full-Text Search + pgvector: Cheap Hybrid Search Before Buying a Vector DB</title><link>https://rajivonai.com/blog/2026-06-18-postgres-full-text-search-pgvector-hybrid-search/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-18-postgres-full-text-search-pgvector-hybrid-search/</guid><description>How to combine PostgreSQL full-text search and pgvector for low-cost hybrid retrieval, including tsvector, ranking, semantic search, fusion, filters, observability, and when to outgrow it.</description><pubDate>Sun, 21 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The RAG system misses “ORA-00060” because the semantic embedding understands deadlocks but not the exact error code. Then pure keyword search misses the runbook because the page says “transaction cycle” instead of “deadlock.” This is the practical reason hybrid search exists: production users ask with both meaning and literals.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;Before buying a dedicated vector database or search platform, many teams should test hybrid retrieval inside PostgreSQL: full-text search for exact terms and lexical relevance, pgvector for semantic similarity, SQL filters for authorization, and a simple fusion step to combine candidates.&lt;/p&gt;
&lt;p&gt;This is not the best architecture for every search product. It is a pragmatic starting point when the corpus already lives in Postgres, the team needs cheap hybrid RAG, and the operating model benefits from one database.&lt;/p&gt;
&lt;p&gt;Outgrow it when relevance tuning, faceting, analyzers, typo tolerance, multilingual search, high query volume, or specialized vector features become central enough to justify OpenSearch, Qdrant, Weaviate, or another dedicated retrieval service.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most internal RAG systems discover hybrid search through failure. Semantic retrieval works for concepts, lexical search works for identifiers, and production users need both in the same question.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Pure vector search is bad at some things users care about:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Error codes.&lt;/li&gt;
&lt;li&gt;Product SKUs.&lt;/li&gt;
&lt;li&gt;Invoice numbers.&lt;/li&gt;
&lt;li&gt;Names and acronyms.&lt;/li&gt;
&lt;li&gt;API symbols.&lt;/li&gt;
&lt;li&gt;Legal phrases.&lt;/li&gt;
&lt;li&gt;Exact command output.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Pure keyword search is also bad at some things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Synonyms.&lt;/li&gt;
&lt;li&gt;Conceptual matches.&lt;/li&gt;
&lt;li&gt;Paraphrases.&lt;/li&gt;
&lt;li&gt;Questions that do not use the document’s wording.&lt;/li&gt;
&lt;li&gt;Long support narratives.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Production RAG usually needs both. The mistake is assuming “hybrid search” requires a new platform immediately. PostgreSQL already has full-text search, ranking functions, indexes, filters, and pgvector. That combination can carry a serious first version if the team accepts its limits.&lt;/p&gt;
&lt;h2 id=&quot;core-technical-explanation&quot;&gt;Core Technical Explanation&lt;/h2&gt;
&lt;p&gt;A simple chunk table can store both lexical and semantic representations:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; document_chunks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigserial&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tenant_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  document_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  body &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  search_vector tsvector &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  embedding vector(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1536&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  deleted_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; document_chunks_fts_idx&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; document_chunks &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;USING&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; gin (search_vector);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; document_chunks_embedding_idx&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; document_chunks &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;USING&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; hnsw (embedding vector_cosine_ops);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The lexical path uses &lt;code&gt;tsvector&lt;/code&gt; and a text query. The semantic path uses vector distance. The hybrid path gets candidates from both, then combines them.&lt;/p&gt;
&lt;p&gt;Do not assume full-text rank and vector distance are directly comparable. They are different scoring systems. A practical first implementation can use rank-based fusion: retrieve top candidates from each path, convert each result list into ranks, and reward documents that appear near the top of either or both lists.&lt;/p&gt;
&lt;p&gt;Reciprocal Rank Fusion (RRF) is the standard formula for this kind of rank combination: for each document, sum &lt;code&gt;1 / (k + rank)&lt;/code&gt; across every ranked list it appears in, where &lt;code&gt;k&lt;/code&gt; is a smoothing constant (Cormack, Clarke, and Büttcher’s original 2009 paper used &lt;code&gt;k = 60&lt;/code&gt;, and most implementations still default to it). A document that ranks consistently well across both the lexical and vector lists outscores one that ranks first in a single list but is absent from the other — which is usually the behavior a hybrid search product wants. &lt;code&gt;k&lt;/code&gt; is a tuning knob, not a fixed constant; validate it against the workload’s actual query set rather than assuming 60 is optimal for every corpus.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Keep the architecture explicit:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[user query] --&gt; Auth[tenant and entitlement filters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Auth --&gt; Lex[Postgres full text candidates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Auth --&gt; Vec[pgvector semantic candidates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Lex --&gt; Fuse[rank fusion]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Vec --&gt; Fuse&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Fuse --&gt; Rerank[optional rerank]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Rerank --&gt; Context[LLM context]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The retrieval service should run two bounded candidate queries:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Lexical query: top N by full-text rank under the tenant and lifecycle filters.&lt;/li&gt;
&lt;li&gt;Vector query: top N by vector distance under the same filters.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Then merge by chunk ID or document ID. Keep the fusion code simple enough to inspect during incidents. Record which path contributed each final result.&lt;/p&gt;
&lt;p&gt;For many internal RAG systems, this is enough: exact error codes come from full-text search, conceptual matches come from pgvector, and SQL filters enforce permissions.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Production symptom&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Scores mixed directly&lt;/td&gt;&lt;td&gt;Strange ranking after combining paths&lt;/td&gt;&lt;td&gt;Use rank-based fusion or calibrated reranking&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Filters differ between paths&lt;/td&gt;&lt;td&gt;Unauthorized or inconsistent candidates&lt;/td&gt;&lt;td&gt;Share one filter builder and test negative cases&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lexical query too narrow&lt;/td&gt;&lt;td&gt;Exact terms work but phrasing fails&lt;/td&gt;&lt;td&gt;Keep vector path independent&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Vector query too broad&lt;/td&gt;&lt;td&gt;Semantic matches ignore identifiers&lt;/td&gt;&lt;td&gt;Keep full-text path independent&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Candidate count too small&lt;/td&gt;&lt;td&gt;Fusion misses good documents&lt;/td&gt;&lt;td&gt;Tune top N per path with query set&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Postgres becomes search platform&lt;/td&gt;&lt;td&gt;Relevance backlog outgrows DBA model&lt;/td&gt;&lt;td&gt;Move to OpenSearch or dedicated retrieval service&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-and-tenancy-notes&quot;&gt;Security and Tenancy Notes&lt;/h2&gt;
&lt;p&gt;The lexical and vector paths must apply the same tenant, entitlement, lifecycle, and deletion filters. A common bug is securing the SQL vector query but letting the full-text query search a broader scope, or the reverse.&lt;/p&gt;
&lt;p&gt;Log retrieval metadata, not raw sensitive chunks. For debugging, store chunk IDs, document IDs, score components, rank positions, tenant ID, and query class. Keep prompt text and retrieved body text under the same retention policy as the source documents.&lt;/p&gt;
&lt;p&gt;If full-text search uses generated columns or triggers, ensure they update when documents are redacted, deleted, or moved between tenants.&lt;/p&gt;
&lt;h2 id=&quot;cost-notes&quot;&gt;Cost Notes&lt;/h2&gt;
&lt;p&gt;The cheap part is avoiding another stateful system. The expensive part is making Postgres do too many jobs after the workload grows.&lt;/p&gt;
&lt;p&gt;Cost drivers include GIN index size, vector index size, write amplification during document updates, CPU for two candidate queries, and any reranker used after retrieval. Hybrid search can be cheaper than operating OpenSearch or a vector service, but only while query volume and relevance requirements stay within the team’s Postgres comfort zone.&lt;/p&gt;
&lt;h2 id=&quot;observability-notes&quot;&gt;Observability Notes&lt;/h2&gt;
&lt;p&gt;Track the two paths separately:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Lexical candidate count and latency.&lt;/li&gt;
&lt;li&gt;Vector candidate count and latency.&lt;/li&gt;
&lt;li&gt;Fusion overlap between lexical and vector results.&lt;/li&gt;
&lt;li&gt;Final result source mix.&lt;/li&gt;
&lt;li&gt;Empty-result rate.&lt;/li&gt;
&lt;li&gt;Queries where exact tokens appear but lexical path contributed nothing.&lt;/li&gt;
&lt;li&gt;Queries where vector path dominates but users click lexical results.&lt;/li&gt;
&lt;li&gt;Slow plans for both candidate queries.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Keep a relevance test set with exact-code queries, synonym queries, long natural-language questions, and tenant-filtered negative cases.&lt;/p&gt;
&lt;h2 id=&quot;backup-restore-and-dr-notes&quot;&gt;Backup, Restore, and DR Notes&lt;/h2&gt;
&lt;p&gt;Hybrid search doubles the retrieval validation surface. After restore, confirm both GIN and vector indexes exist and are healthy. Confirm generated or stored &lt;code&gt;tsvector&lt;/code&gt; values match restored text. Confirm embedding model versions match the active retrieval path.&lt;/p&gt;
&lt;p&gt;If embeddings are rebuildable but &lt;code&gt;tsvector&lt;/code&gt; is generated from source text, lexical search may recover faster than semantic search. That can be a useful degraded mode: lexical-only retrieval while vector indexes or embeddings rebuild.&lt;/p&gt;
&lt;h2 id=&quot;when-to-outgrow-this-approach&quot;&gt;When to Outgrow This Approach&lt;/h2&gt;
&lt;p&gt;Move beyond Postgres hybrid search when the problem becomes a search product rather than a database feature.&lt;/p&gt;
&lt;p&gt;Strong signals:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Product teams need faceting, typo tolerance, stemming customization, synonyms, and relevance tooling.&lt;/li&gt;
&lt;li&gt;Search traffic competes with OLTP latency.&lt;/li&gt;
&lt;li&gt;Query logs require search-specific analytics.&lt;/li&gt;
&lt;li&gt;Multiple corpora need different analyzers or ranking pipelines.&lt;/li&gt;
&lt;li&gt;Vector retrieval needs specialized payload filtering, sparse vectors, or multi-stage search.&lt;/li&gt;
&lt;li&gt;Relevance engineers need dashboards and explain tooling that Postgres does not provide.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At that point, OpenSearch may fit search-heavy workloads. Qdrant or Weaviate may fit retrieval-service workloads. The point is to earn the move with evidence.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Does the corpus already live in PostgreSQL?&lt;/li&gt;
&lt;li&gt;Do users search for exact identifiers as well as concepts?&lt;/li&gt;
&lt;li&gt;Can SQL filters express the authorization model?&lt;/li&gt;
&lt;li&gt;Can the team maintain both GIN and vector indexes?&lt;/li&gt;
&lt;li&gt;Is a simple fusion strategy acceptable for the first version?&lt;/li&gt;
&lt;li&gt;Are candidate counts tuned with a real query set?&lt;/li&gt;
&lt;li&gt;Can Postgres absorb two retrieval queries per user query?&lt;/li&gt;
&lt;li&gt;Is there a relevance test set before launch?&lt;/li&gt;
&lt;li&gt;Is lexical-only mode useful during vector rebuilds?&lt;/li&gt;
&lt;li&gt;What signal tells the team to move to a dedicated search or vector platform?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Teams assume hybrid search requires a new platform on day one, when pure vector search already misses exact identifiers and pure keyword search already misses paraphrases and synonyms.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Combine PostgreSQL full-text search and pgvector with rank-based fusion (RRF) as a first hybrid architecture — one source of truth, shared SQL filters, lexical recall, and semantic recall — and move to a dedicated search or vector platform only when relevance tuning, scale, or search operations outgrow what Postgres can do.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; A relevance test set with exact-code queries, synonym queries, and tenant-filtered negative cases passes, and both the lexical and vector candidate queries apply identical authorization filters.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, confirm the lexical and vector retrieval paths share one filter builder rather than two independently maintained ones, since that’s the most common place authorization drifts between paths.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;PostgreSQL full-text search, &lt;code&gt;tsvector&lt;/code&gt;, and GIN index documentation: &lt;a href=&quot;https://www.postgresql.org/docs/current/textsearch.html&quot;&gt;https://www.postgresql.org/docs/current/textsearch.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;pgvector README for vector indexes, distance operators, and query examples: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL generated columns documentation (if used for &lt;code&gt;tsvector&lt;/code&gt; maintenance): &lt;a href=&quot;https://www.postgresql.org/docs/current/ddl-generated-columns.html&quot;&gt;https://www.postgresql.org/docs/current/ddl-generated-columns.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Cormack, Clarke, and Büttcher, “Reciprocal Rank Fusion outperforms Condorcet and Individual Rank Learning Methods” (SIGIR 2009) — the original RRF paper.&lt;/li&gt;
&lt;li&gt;OpenSearch, Qdrant, and Weaviate documentation for outgrowth comparison points: &lt;a href=&quot;https://docs.opensearch.org/latest/vector-search/&quot;&gt;https://docs.opensearch.org/latest/vector-search/&lt;/a&gt;, &lt;a href=&quot;https://qdrant.tech/documentation/&quot;&gt;https://qdrant.tech/documentation/&lt;/a&gt;, &lt;a href=&quot;https://docs.weaviate.io/weaviate&quot;&gt;https://docs.weaviate.io/weaviate&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>ai-engineering</category><category>checklist</category></item><item><title>Transparent Application Continuity at Scale: Building a Resilient Connection Mesh</title><link>https://rajivonai.com/blog/2024-08-30-oracle-application-failover-taf-tac-connection-pools/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-08-30-oracle-application-failover-taf-tac-connection-pools/</guid><description>Why TCP keepalive timeouts destroy recovery time objectives during database failover, and how to engineer a zero-downtime connection mesh utilizing Fast Application Notification (FAN) and Transparent Application Continuity (TAC).</description><pubDate>Sat, 20 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Measuring database high availability at the database process level is a vanity metric; if an infrastructure node failover recovers in seconds while client connection pools hang for fifteen minutes on dead TCP sockets, the platform has experienced a production outage.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Abrupt database host crashes leave established client TCP sockets open until OS retransmission timers expire (&lt;code&gt;tcp_retries2&lt;/code&gt;), starving application thread pools while legacy TAF aborts in-flight DML with &lt;code&gt;ORA-25408&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Combine Fast Application Notification (FAN) over ONS with Oracle Universal Connection Pool (UCP), the JDBC Replay Driver (&lt;code&gt;OracleDataSourceImpl&lt;/code&gt;), and Transparent Application Continuity (TAC) backed by Transaction Guard (&lt;code&gt;LTXID&lt;/code&gt;), adding &lt;code&gt;CMAN-TDM&lt;/code&gt; when thousands of pods must reconnect without a logon storm.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Blocked ONS port &lt;code&gt;6200&lt;/code&gt; disables FAN silently, large batch transactions can exhaust client replay buffers, and external side effects (&lt;code&gt;UTL_HTTP&lt;/code&gt;) inside a database transaction will execute twice on replay.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2024-08-30-oracle-application-failover-taf-tac-connection-pools.svg&quot; alt=&quot;Comparison between passive TCP sockets with legacy TAF versus FAN out-of-band signaling with TAC&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Microservice platforms running on Kubernetes commonly maintain hundreds or thousands of application pods holding persistent JDBC connection pools against a multi-node Oracle Real Application Clusters (RAC) or Active Data Guard deployment. Under steady-state traffic, those pools keep established TCP connections open to specific RAC instances to avoid per-request TLS and TNS authentication overhead.&lt;/p&gt;
&lt;p&gt;When a database host suffers an ungraceful failure—such as a kernel panic, power supply cutoff, or abrupt hypervisor termination—it sends no TCP &lt;code&gt;FIN&lt;/code&gt; or &lt;code&gt;RST&lt;/code&gt; packet to connected clients. Inside the cluster, Oracle Clusterware’s Cluster Synchronization Services (CSS) detects missing network and voting-disk heartbeats (governed by &lt;code&gt;misscount&lt;/code&gt;, defaulting to 30 seconds on Linux RAC), evicts the failed node, and relocates its Virtual IP (VIP) to a surviving instance. The relocated VIP accepts new TCP &lt;code&gt;SYN&lt;/code&gt; packets and immediately rejects stale connection attempts via &lt;code&gt;RST&lt;/code&gt; for &lt;em&gt;new&lt;/em&gt; sockets. From the DBA’s perspective in &lt;code&gt;GV$INSTANCE&lt;/code&gt;, the cluster has recovered. Yet application API tiers continue timing out.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The outage persists because &lt;strong&gt;database cluster recovery and client OS TCP failure detection operate on completely different clocks&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-08-30-oracle-application-failover-taf-tac-connection-pools-2-constraints.svg&quot; alt=&quot;Four-step failure cascade from silent host crash to Linux tcp_retries2 backoff, thread starvation, and logon storm&quot;&gt;&lt;/p&gt;
&lt;p&gt;Three failure mechanisms turn a brief node eviction into a prolonged application outage:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;OS TCP Retransmission Backoff:&lt;/strong&gt; On Linux, &lt;code&gt;net.ipv4.tcp_retries2&lt;/code&gt; defaults to &lt;code&gt;15&lt;/code&gt;. When an application thread writes a SQL call onto an already-established socket whose remote host has vanished, the kernel retransmits unacknowledged segments with exponential backoff starting from the measured round-trip timeout (around &lt;code&gt;0.2&lt;/code&gt; seconds) and doubling on each retry until capped at &lt;code&gt;TCP_RTO_MAX&lt;/code&gt; (&lt;code&gt;120&lt;/code&gt; seconds). Across 15 retries, the cumulative wait reaches roughly &lt;code&gt;924&lt;/code&gt; seconds (~15.4 minutes) before the kernel returns &lt;code&gt;ETIMEDOUT&lt;/code&gt;. Every worker thread executing a query against the dead node blocks for the full duration. Once worker threads saturate, upstream API gateways exhaust their queues and return HTTP 504 errors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Legacy TAF Aborts In-Flight DML (&lt;code&gt;ORA-25408&lt;/code&gt;):&lt;/strong&gt; Traditional Transparent Application Failover (&lt;code&gt;-failovertype SELECT&lt;/code&gt;) only re-establishes sessions and resumes read-only &lt;code&gt;SELECT&lt;/code&gt; cursors. If a session has an uncommitted &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, or &lt;code&gt;DELETE&lt;/code&gt; active when the instance dies, TAF cannot verify whether the commit succeeded or safely replay the mutation; it aborts the call with &lt;code&gt;ORA-25408: can not safely replay call&lt;/code&gt;. Unless every application service implements idempotent retry logic around every transaction boundary, writes fail visibly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Post-Failover Logon Storms:&lt;/strong&gt; When dead sockets finally time out—or when a connection pool detects failures simultaneously across hundreds of pods—every pod attempts to rebuild its &lt;code&gt;min-pool-size&lt;/code&gt; connections at once against the surviving RAC listeners. A sudden burst of &lt;code&gt;1,000&lt;/code&gt; pods each opening &lt;code&gt;10&lt;/code&gt; connections generates &lt;code&gt;10,000&lt;/code&gt; simultaneous TNS listener handshakes, password verifications, and server process spawns (&lt;code&gt;fork&lt;/code&gt;/&lt;code&gt;exec&lt;/code&gt;), saturating listener and OS CPU on the surviving nodes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;How do platform architects eliminate multi-minute TCP socket hangs, replay in-flight DML with exactly-once safety, and prevent post-failover logon storms without rewriting application business logic?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;When post-mortems trace an outage to hanging JDBC sockets or &lt;code&gt;ORA-25408&lt;/code&gt; errors, engineering teams typically evaluate four approaches.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-08-30-oracle-application-failover-taf-tac-connection-pools-3-options.svg&quot; alt=&quot;Comparison of OS TCP tuning, Legacy TAF, Fast Connection Failover, and Transparent Application Continuity&quot;&gt;&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Solves&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;OS TCP / Socket Timeout Tuning&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lower &lt;code&gt;tcp_retries2&lt;/code&gt; and &lt;code&gt;oracle.jdbc.ReadTimeout&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bounds how long a socket blocks on a dead host&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Aggressive timeouts cause false disconnects on slow SQL; still drops active DML&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt; as primary failover mechanism&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Legacy TAF (&lt;code&gt;SELECT&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Client driver re-establishes connection and restarts &lt;code&gt;SELECT&lt;/code&gt; cursors&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Masks outages for read-only reporting queries&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Throws &lt;code&gt;ORA-25408&lt;/code&gt; on any in-flight DML; no Transaction Guard&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt; for transactional OLTP services&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Fast Connection Failover (FCF Only)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;UCP subscribes to ONS FAN events and purges dead sockets immediately&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Eliminates the 15-minute TCP hang in milliseconds&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Uncommitted DML still raises an exception to the application&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Partial&lt;/strong&gt;; necessary foundation, insufficient alone&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;FAN + UCP + TAC&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;FCF plus JDBC Replay Driver (&lt;code&gt;OracleDataSourceImpl&lt;/code&gt;) and Transaction Guard (&lt;code&gt;LTXID&lt;/code&gt;)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Purges dead sockets and replays uncommitted DML transparently&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires UCP/Replay Driver, ONS port &lt;code&gt;6200&lt;/code&gt;, and &lt;code&gt;KEEP&lt;/code&gt; grants&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen&lt;/strong&gt; for transactional services&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Lowering &lt;code&gt;tcp_retries2&lt;/code&gt; or setting a 2-second &lt;code&gt;ReadTimeout&lt;/code&gt; is a blunt instrument: any legitimate query taking 3 seconds gets killed mid-flight, and dead connections still lose their uncommitted transactions. Fast Connection Failover (FCF) solves the socket hang cleanly via out-of-band FAN signaling, but without TAC, applications still receive &lt;code&gt;SQLRecoverableException&lt;/code&gt; for any call that was in flight at the moment of the crash. Pairing FCF with TAC and Transaction Guard solves both halves of the problem.&lt;/p&gt;
&lt;h2 id=&quot;the-intelligent-connection-mesh-architecture&quot;&gt;The Intelligent Connection Mesh Architecture&lt;/h2&gt;
&lt;p&gt;The recommended architecture bridges Clusterware and the application connection pool through out-of-band signaling, client-side request recording, server-side commit verification, and optional proxy multiplexing.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-08-30-oracle-application-failover-taf-tac-connection-pools-4-architecture.svg&quot; alt=&quot;Architecture showing UCP ONS subscriber, Client Replay Buffer, CMAN-TDM, and Transaction Guard LTXID validation&quot;&gt;&lt;/p&gt;
&lt;p&gt;Four components form the mesh:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Out-of-Band Fast Application Notification (FAN via ONS):&lt;/strong&gt; Oracle Notification Service (&lt;code&gt;ONS&lt;/code&gt;) runs on every RAC node and publishes &lt;code&gt;NODE_DOWN&lt;/code&gt;, &lt;code&gt;SERVICE_MEMBER_DOWN&lt;/code&gt;, and load-balancing advisory events over an independent TCP channel (default port &lt;code&gt;6200&lt;/code&gt;). With &lt;code&gt;ons.jar&lt;/code&gt; on the classpath, UCP automatically learns ONS endpoints from the SCAN/database handshake, subscribes out-of-band, and immediately aborts idle and active sockets pointing to the evicted node, bypassing OS TCP retransmission timers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Client Replay Buffer (&lt;code&gt;OracleDataSourceImpl&lt;/code&gt;) and Automatic State Tracking:&lt;/strong&gt; The Oracle JDBC Replay Driver records SQL calls, bind variables, and session state transitions inside an in-memory cyclic buffer from the start of each database request until the request completes or commits. Unlike traditional Application Continuity (&lt;code&gt;-failovertype TRANSACTION&lt;/code&gt;), which relies on explicit &lt;code&gt;beginRequest&lt;/code&gt;/&lt;code&gt;endRequest&lt;/code&gt; boundaries or UCP pool borrows plus manual session-state callbacks, Transparent Application Continuity (&lt;code&gt;-failovertype AUTO&lt;/code&gt; with &lt;code&gt;-failover_restore AUTO&lt;/code&gt; and &lt;code&gt;-session_state AUTO&lt;/code&gt;) automatically tracks non-transactional session state changes on the server and re-establishes them on the surviving node without application code changes. If an application mutates an untracked state mid-request, TAC safely disables replay for that request rather than risking silent state corruption.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Transaction Guard (&lt;code&gt;LTXID&lt;/code&gt;) for Exactly-Once Safety:&lt;/strong&gt; Replaying an uncommitted transaction is safe only if the database guarantees it did not already commit before the crash. With &lt;code&gt;-commit_outcome TRUE&lt;/code&gt;, Oracle assigns every transaction a Logical Transaction ID (&lt;code&gt;LTXID&lt;/code&gt;). During failover, the Replay Driver queries Transaction Guard on the surviving node: if the transaction is uncommitted, TAC replays the buffered calls; if it already committed, TAC halts replay with &lt;code&gt;ORA-41409: cannot replay committed transaction; failover cannot continue&lt;/code&gt;, preventing duplicate execution.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mutable Value Preservation and &lt;code&gt;CMAN-TDM&lt;/code&gt;:&lt;/strong&gt; Functions such as &lt;code&gt;SYSDATE&lt;/code&gt;, &lt;code&gt;SYSTIMESTAMP&lt;/code&gt;, &lt;code&gt;SYS_GUID()&lt;/code&gt;, and &lt;code&gt;sequence.NEXTVAL&lt;/code&gt; would normally return different values on replay. Granting &lt;code&gt;KEEP DATE TIME&lt;/code&gt;, &lt;code&gt;KEEP SYSGUID&lt;/code&gt;, and &lt;code&gt;KEEP SEQUENCE&lt;/code&gt; instructs Oracle to preserve and re-inject the original values during replay. For large Kubernetes fleets, placing Oracle Connection Manager in Traffic Director Mode (&lt;code&gt;CMAN-TDM&lt;/code&gt;) in front of RAC multiplexes thousands of pod connections onto persistent server processes, absorbing reconnect storms.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;1-database-service-configuration-srvctl&quot;&gt;1. Database Service Configuration (&lt;code&gt;srvctl&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Create a custom service (never use the default database or PDB service for TAC) with &lt;code&gt;-failovertype AUTO&lt;/code&gt;, &lt;code&gt;-failover_restore AUTO&lt;/code&gt;, &lt;code&gt;-session_state AUTO&lt;/code&gt;, and &lt;code&gt;-commit_outcome TRUE&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; service&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -db&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODDB&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -service&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; svc_payments_tac&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -preferred&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODDB1,PRODDB2&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -available&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODDB3,PRODDB4&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failovertype&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; AUTO&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failover_restore&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; AUTO&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -commit_outcome&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; TRUE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -drain_timeout&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 300&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -replay_init_time&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 600&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failoverretry&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 30&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failoverdelay&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -notification&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; TRUE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; start&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; service&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -db&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODDB&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -service&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; svc_payments_tac&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-mutable-value-privileges&quot;&gt;2. Mutable Value Privileges&lt;/h3&gt;
&lt;p&gt;Grant &lt;code&gt;KEEP&lt;/code&gt; privileges to the application user so timestamps, GUIDs, and sequence numbers remain identical if a transaction replays on a surviving node:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; KEEP&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TIME&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; payments_app;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; KEEP&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SYSGUID &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; payments_app;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; KEEP&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SEQUENCE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; payments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;order_id_seq&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; payments_app;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; KEEP&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SEQUENCE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; payments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;transaction_id_seq&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; payments_app;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, failover_type, failover_restore, commit_outcome, replay_init_time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dba_services&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;svc_payments_tac&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-application-pool-configuration-spring-boot--ucp&quot;&gt;3. Application Pool Configuration (Spring Boot / UCP)&lt;/h3&gt;
&lt;p&gt;Configure Spring Boot to use &lt;code&gt;PoolDataSourceImpl&lt;/code&gt; with &lt;code&gt;oracle.jdbc.replay.OracleDataSourceImpl&lt;/code&gt; as the connection factory and &lt;code&gt;fast-connection-failover-enabled: true&lt;/code&gt; (ensure &lt;code&gt;com.oracle.database.ha:ons&lt;/code&gt; is on the classpath):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;spring&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  datasource&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;oracle.ucp.jdbc.PoolDataSourceImpl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    oracleucp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      connection-factory-class-name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;oracle.jdbc.replay.OracleDataSourceImpl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      url&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;jdbc:oracle:thin:@(DESCRIPTION=(CONNECT_TIMEOUT=3)(RETRY_COUNT=30)(RETRY_DELAY=1)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=rac-scan.corp.internal)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=svc_payments_tac)))&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      user&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;payments_app&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      password&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;${DB_PASSWORD}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      initial-pool-size&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;20&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      min-pool-size&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;20&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      max-pool-size&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;50&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      fast-connection-failover-enabled&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      validate-connection-on-borrow&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      connection-wait-timeout&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      max-connection-reuse-time&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3600&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;4-workload-readiness-check-acchk&quot;&gt;4. Workload Readiness Check (&lt;code&gt;ACCHK&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Before enabling TAC in production, capture representative test traffic using Oracle’s Application Continuity Protection Check (&lt;code&gt;ACCHK&lt;/code&gt;) packages to identify non-replayable calls:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- ACCHK needs 19.11+ and its views created once per PDB (DBMS_APP_CONT_ADMIN.ACCHK_VIEWS).&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Clear old data first (PDB level), then enable collection for new sessions.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;EXECUTE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; DBMS_APP_CONT_ADMIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;ACCHK_PURGE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- check the PL/SQL Packages reference for your release&apos;s exact signature&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;EXECUTE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; DBMS_APP_CONT_ADMIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;ACCHK_SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(TRUE);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Run application integration/regression suite against svc_payments_tac...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Generate full coverage and diagnostic report&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SERVEROUTPUT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; FORMAT WRAPPED LINESIZE &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;160&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;EXECUTE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; DBMS_APP_CONT_REPORT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;ACCHK_REPORT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;DBMS_APP_CONT_REPORT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;FULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Disable collection when finished&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;EXECUTE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; DBMS_APP_CONT_ADMIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;ACCHK_SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(FALSE);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Deploy TAC in four gated phases so network reachability, mutable grants, and non-replayable side effects are caught before production cutover.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-08-30-oracle-application-failover-taf-tac-connection-pools-5-flow.svg&quot; alt=&quot;Four-phase rollout from ACCHK workload audit and TAC service creation to KEEP grants and UCP cutover&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — ACCHK Workload Audit:&lt;/strong&gt; Enable &lt;code&gt;DBMS_APP_CONT_ADMIN.ACCHK_SET(TRUE)&lt;/code&gt; in staging, run the application’s regression suite, and inspect &lt;code&gt;DBMS_APP_CONT_REPORT.ACCHK_REPORT(DBMS_APP_CONT_REPORT.FULL)&lt;/code&gt;.
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Validation gate:&lt;/em&gt; Identify and isolate any non-replayable operations (such as &lt;code&gt;UTL_HTTP&lt;/code&gt; external calls or unsupported session state mutations) into separate services or outbox patterns.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Rollback:&lt;/em&gt; Disable collection with &lt;code&gt;ACCHK_SET(FALSE)&lt;/code&gt; and &lt;code&gt;ACCHK_PURGE&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — TAC Service Provisioning:&lt;/strong&gt; Create &lt;code&gt;svc_payments_tac&lt;/code&gt; via &lt;code&gt;srvctl&lt;/code&gt; with &lt;code&gt;-failovertype AUTO&lt;/code&gt;, &lt;code&gt;-failover_restore AUTO&lt;/code&gt;, &lt;code&gt;-session_state AUTO&lt;/code&gt;, and &lt;code&gt;-commit_outcome TRUE&lt;/code&gt;.
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Validation gate:&lt;/em&gt; Verify &lt;code&gt;dba_services&lt;/code&gt; and &lt;code&gt;srvctl config service&lt;/code&gt; reflect the exact TAC attributes and FAN notification is enabled.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Rollback:&lt;/em&gt; Stop and remove the new service; existing services remain untouched.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — &lt;code&gt;KEEP&lt;/code&gt; Grants and ONS Network Verification:&lt;/strong&gt; Apply &lt;code&gt;KEEP DATE TIME&lt;/code&gt;, &lt;code&gt;KEEP SYSGUID&lt;/code&gt;, and &lt;code&gt;KEEP SEQUENCE&lt;/code&gt; grants, and verify bidirectional TCP connectivity on ONS port &lt;code&gt;6200&lt;/code&gt; from Kubernetes worker subnets to all RAC VIPs.
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Validation gate:&lt;/em&gt; Confirm ONS client registration succeeds and firewall rules permit port &lt;code&gt;6200&lt;/code&gt; across every availability zone.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Rollback:&lt;/em&gt; Revoke &lt;code&gt;KEEP&lt;/code&gt; grants if security policy requires scoping them to a specific service role.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — UCP Replay Cutover and Chaos Validation:&lt;/strong&gt; Switch the application pool to &lt;code&gt;OracleDataSourceImpl&lt;/code&gt; with &lt;code&gt;fast-connection-failover-enabled: true&lt;/code&gt; (and &lt;code&gt;CMAN-TDM&lt;/code&gt; for high pod counts). Run a planned &lt;code&gt;srvctl relocate service&lt;/code&gt; and an unplanned node kill in staging.
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Validation gate:&lt;/em&gt; Confirm dead sockets abort immediately via FAN, uncommitted OLTP calls replay cleanly on the surviving instance, and no &lt;code&gt;ORA-25408&lt;/code&gt; errors occur.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Rollback:&lt;/em&gt; Revert &lt;code&gt;connection-factory-class-name&lt;/code&gt; to &lt;code&gt;oracle.jdbc.pool.OracleDataSource&lt;/code&gt; (retaining FCF/FAN socket cleanup without replay).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Based on how Oracle Clusterware, ONS, UCP, and Transparent Application Continuity behave under failure:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;FAN separates socket cleanup from TCP timeouts:&lt;/strong&gt; Because ONS delivers &lt;code&gt;NODE_DOWN&lt;/code&gt; and &lt;code&gt;SERVICE_MEMBER_DOWN&lt;/code&gt; events out-of-band, UCP does not wait for &lt;code&gt;tcp_retries2&lt;/code&gt; to expire on half-open sockets. Setting &lt;code&gt;validate-connection-on-borrow: false&lt;/code&gt; avoids adding a round-trip &lt;code&gt;SELECT 1 FROM DUAL&lt;/code&gt; ping to every borrow while still purging broken connections on failure events.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Transaction Guard prevents silent double-execution:&lt;/strong&gt; If a node crashes after a &lt;code&gt;COMMIT&lt;/code&gt; reaches the redo log but before the &lt;code&gt;OK&lt;/code&gt; packet returns to the client, replaying the &lt;code&gt;INSERT&lt;/code&gt;/&lt;code&gt;UPDATE&lt;/code&gt; would corrupt balances. Transaction Guard’s &lt;code&gt;LTXID&lt;/code&gt; outcome table lets the Replay Driver check the commit status on the surviving instance and raise &lt;code&gt;ORA-41409&lt;/code&gt; instead of re-executing a committed transaction.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Planned maintenance drains before replay is needed:&lt;/strong&gt; Configuring &lt;code&gt;-drain_timeout 300&lt;/code&gt; on the TAC service lets &lt;code&gt;srvctl relocate service&lt;/code&gt; and rolling Grid Infrastructure patching emit FAN drain notifications so UCP stops routing new borrows to the draining instance and lets active transactions finish normally. Replay is then reserved only for stragglers or sudden unplanned crashes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bounded replay windows protect JVM memory:&lt;/strong&gt; The client replay buffer holds every call and bind variable since the start of the database request. Short OLTP transactions consume minimal heap, whereas long-running batch jobs that update hundreds of thousands of rows in a single uncommitted transaction can exhaust JVM memory or exceed &lt;code&gt;-replay_init_time&lt;/code&gt;. Batch services should therefore use a separate service with &lt;code&gt;-failovertype NONE&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;TAC and FAN eliminate TCP hang outages, but they introduce strict boundaries around network ports, replay buffer sizing, and transaction determinism.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-08-30-oracle-application-failover-taf-tac-connection-pools-6-failures.svg&quot; alt=&quot;Three failure paths of TAC: blocked ONS port 6200, batch replay buffer OOM, and external side-effect duplication&quot;&gt;&lt;/p&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks how&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Blocked ONS port &lt;code&gt;6200&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Firewall drops ONS traffic between RAC nodes and app pods; UCP falls back to slow OS TCP timeouts.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Test &lt;code&gt;nc -zv &amp;#x3C;rac-vip&gt; 6200&lt;/code&gt; from pods and check UCP/ONS subscriber logs.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Permit TCP &lt;code&gt;6200&lt;/code&gt; between app subnets and all RAC VIPs/SCAN; monitor ONS registration.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Long batch replay OOM&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bulk DML in a single request fills the client JVM replay buffer or exceeds &lt;code&gt;replay_init_time&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;OutOfMemoryError&lt;/code&gt; in app pods or &lt;code&gt;ACCHK&lt;/code&gt; flagging oversized requests.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Route batch pipelines to a dedicated service with &lt;code&gt;-failovertype NONE&lt;/code&gt;; commit in small chunks.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;External side effects in TX&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;UTL_HTTP&lt;/code&gt; or external RPC inside an uncommitted DB transaction executes twice on replay.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;ACCHK_REPORT&lt;/code&gt; flags non-replayable external calls; duplicate downstream webhooks.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Move external calls out of the DB transaction (transactional outbox) or enforce idempotency keys.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Already-committed crash (&lt;code&gt;ORA-41409&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Crash occurs after &lt;code&gt;COMMIT&lt;/code&gt; but before client ACK; Transaction Guard halts replay with &lt;code&gt;ORA-41409&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Application logs show &lt;code&gt;ORA-41409: cannot replay committed transaction&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Treat &lt;code&gt;ORA-41409&lt;/code&gt; in the API layer as “commit succeeded—verify state rather than retrying.”&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Untracked session state drift&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Mid-request global package state or unlogged session changes prevent safe replay.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;ACCHK_REPORT&lt;/code&gt; shows replay disabled mid-session due to state mutation.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set &lt;code&gt;-failover_restore AUTO&lt;/code&gt; and &lt;code&gt;-session_state AUTO&lt;/code&gt;; keep application sessions stateless.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Pod logon storm on failover&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Thousands of pods drop and recreate connections simultaneously, saturating TNS listeners.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Listener CPU spike and &lt;code&gt;ORA-12541&lt;/code&gt; / &lt;code&gt;TNS-12535&lt;/code&gt; errors on surviving nodes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Place &lt;code&gt;CMAN-TDM&lt;/code&gt; in front of RAC to multiplex pod connections onto persistent server processes.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Replay lock contention&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Replayed DML must re-acquire row locks on the surviving node while another session holds them.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;enq: TX - row lock contention&lt;/code&gt; waits during failover windows.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep transactions short and use bounded wait clauses (&lt;code&gt;SELECT ... FOR UPDATE WAIT 10&lt;/code&gt;).&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Before promoting any TAC service to production, run a synthetic &lt;code&gt;kill -9&lt;/code&gt; on one RAC instance’s &lt;code&gt;PMON&lt;/code&gt; process under sustained OLTP write load while tailing UCP ONS debug logs. Confirm that dead connections drain immediately on &lt;code&gt;FAN&lt;/code&gt; event arrival and that &lt;code&gt;V$TRANSACTION&lt;/code&gt; shows zero duplicate commits on surviving nodes.&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Database eviction speed means nothing if client sockets hang:&lt;/strong&gt; Without out-of-band FAN notifications, Linux &lt;code&gt;tcp_retries2 = 15&lt;/code&gt; keeps dead pooled connections blocked for ~15 minutes after an ungraceful host crash.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Legacy TAF cannot protect OLTP writes:&lt;/strong&gt; TAF only replays &lt;code&gt;SELECT&lt;/code&gt; cursors and aborts active DML with &lt;code&gt;ORA-25408&lt;/code&gt;; transactional workloads require TAC (&lt;code&gt;-failovertype AUTO&lt;/code&gt;) with &lt;code&gt;OracleDataSourceImpl&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Transaction Guard (&lt;code&gt;LTXID&lt;/code&gt;) makes replay safe:&lt;/strong&gt; Enabling &lt;code&gt;-commit_outcome TRUE&lt;/code&gt; prevents duplicate commits by halting replay with &lt;code&gt;ORA-41409&lt;/code&gt; whenever the crashed transaction had already committed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Audit workloads with &lt;code&gt;ACCHK&lt;/code&gt; before cutover:&lt;/strong&gt; Run &lt;code&gt;DBMS_APP_CONT_ADMIN.ACCHK_SET(TRUE)&lt;/code&gt; and &lt;code&gt;DBMS_APP_CONT_REPORT.ACCHK_REPORT&lt;/code&gt; in staging to catch external side effects (&lt;code&gt;UTL_HTTP&lt;/code&gt;), missing &lt;code&gt;KEEP&lt;/code&gt; grants, and oversized batch requests.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Separate batch from OLTP and multiplex large fleets:&lt;/strong&gt; Keep bulk batch jobs on &lt;code&gt;-failovertype NONE&lt;/code&gt; to avoid replay buffer OOM, and use &lt;code&gt;CMAN-TDM&lt;/code&gt; when thousands of pods could trigger a post-failover logon storm.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Passive OS TCP keepalive/retransmission timers leave application connection pools blocked for minutes after an abrupt RAC node crash, while legacy TAF aborts active DML with &lt;code&gt;ORA-25408&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Enable Fast Application Notification (FAN) over ONS port &lt;code&gt;6200&lt;/code&gt;, configure a TAC service (&lt;code&gt;-failovertype AUTO -failover_restore AUTO -session_state AUTO -commit_outcome TRUE&lt;/code&gt;), grant &lt;code&gt;KEEP&lt;/code&gt; privileges, and switch UCP to &lt;code&gt;oracle.jdbc.replay.OracleDataSourceImpl&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; &lt;code&gt;DBMS_APP_CONT_REPORT.ACCHK_REPORT&lt;/code&gt; confirms high replay coverage in staging, and a chaos test killing a RAC node shows UCP purging dead sockets immediately while uncommitted OLTP transactions complete on the surviving node.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run &lt;code&gt;ACCHK_SET(TRUE)&lt;/code&gt; against your staging workload this week, verify port &lt;code&gt;6200&lt;/code&gt; reachability from your Kubernetes worker subnets to your RAC VIPs, and migrate your primary OLTP service to UCP with FCF and TAC.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>pgvector Query Plans: Reading EXPLAIN for Vector Search</title><link>https://rajivonai.com/blog/2026-06-17-pgvector-query-plans-explain-vector-search/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-17-pgvector-query-plans-explain-vector-search/</guid><description>How DBAs should read PostgreSQL EXPLAIN plans for pgvector queries, including index scans, sequential scans, ORDER BY distance, LIMIT, filters, iterative scans, cost estimates, and plan surprises.</description><pubDate>Thu, 18 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The application says pgvector is slow. The DBA runs &lt;code&gt;EXPLAIN&lt;/code&gt; and sees a sequential scan, a distance sort, a tenant filter, and a &lt;code&gt;LIMIT 10&lt;/code&gt; that arrives too late to save the query. The problem is not that Postgres cannot do vector search. The problem is that nobody read the plan like a database engineer.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;pgvector query tuning starts with the shape of the SQL plan. DBAs need to verify whether the query uses a vector index, whether &lt;code&gt;ORDER BY embedding &amp;#x3C;=&gt; $query_vector&lt;/code&gt; is written in an indexable form, whether filters are applied before or after candidate selection, whether &lt;code&gt;LIMIT&lt;/code&gt; is helping, and whether row estimates match reality.&lt;/p&gt;
&lt;p&gt;Do not tune vector search from application latency alone. Use &lt;code&gt;EXPLAIN&lt;/code&gt;, representative parameters, realistic tenants, and side-by-side exact-search baselines. The plan tells you whether the database is scanning, sorting, using an approximate index, filtering away candidates, or misestimating selectivity.&lt;/p&gt;
&lt;p&gt;pgvector planner behavior, iterative scans, and supported index patterns depend on pgvector and PostgreSQL versions — most notably, &lt;code&gt;hnsw.iterative_scan&lt;/code&gt; and &lt;code&gt;ivfflat.iterative_scan&lt;/code&gt; only exist from pgvector 0.8.0 (October 2024) onward, and earlier versions will error on the &lt;code&gt;SET&lt;/code&gt; rather than silently ignore it. Confirm the deployed pgvector version before assuming any specific planner behavior described below is available.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Once pgvector is in production, slow retrieval tickets land on the same desks as ordinary PostgreSQL performance tickets. The difference is that answer quality, filters, and approximate index behavior are now mixed into the query plan conversation.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Vector SQL looks deceptively ordinary:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; id, body&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; document_chunks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tenant_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; embedding &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A DBA sees a filter, an order, and a limit. But this query has special requirements:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The distance operator must match the index operator class.&lt;/li&gt;
&lt;li&gt;The ordering must be in a form the index can support.&lt;/li&gt;
&lt;li&gt;Filters can reduce or distort the candidate set.&lt;/li&gt;
&lt;li&gt;The planner may choose a sequential scan if it believes that is cheaper.&lt;/li&gt;
&lt;li&gt;Approximate indexes can return fast but not exact results.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;LIMIT&lt;/code&gt; controls top-k retrieval, but only if the plan can exploit it.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The failure mode is easy to miss in dashboards. Latency goes up, answer quality goes down, or the query returns fewer rows than expected. The plan explains which one is happening.&lt;/p&gt;
&lt;h2 id=&quot;core-technical-explanation&quot;&gt;Core Technical Explanation&lt;/h2&gt;
&lt;p&gt;Read a pgvector plan from the top down and then from the access path up.&lt;/p&gt;
&lt;p&gt;The first question: is the database using a vector index or scanning the table? For large tables, a sequential scan plus sort usually means Postgres is computing distance for many rows and sorting them. That can be acceptable for tiny tables or offline validation, but it is not a production retrieval path for large corpora.&lt;/p&gt;
&lt;p&gt;The second question: is the distance expression indexable? This follows the same general PostgreSQL rule that applies to any index-backed &lt;code&gt;ORDER BY&lt;/code&gt;: the planner can only use an index to satisfy an ordering if the expression in &lt;code&gt;ORDER BY&lt;/code&gt; matches the index’s indexed expression and operator class exactly. The query should order directly by the vector distance expression (&lt;code&gt;embedding &amp;#x3C;=&gt; $2&lt;/code&gt;) and use a &lt;code&gt;LIMIT&lt;/code&gt;. Wrapping the distance expression in a function call, casting it, or reversing the comparison changes the expression the planner sees and can silently fall back to a sequential scan plus sort — with no error, just a much slower plan.&lt;/p&gt;
&lt;p&gt;The third question: where do filters apply? Tenant, lifecycle, entitlement, category, language, and model-version filters are correctness requirements. But approximate nearest-neighbor indexes may produce candidates before all filters are satisfied. If many candidates are filtered out, the result can under-return or require broader search settings.&lt;/p&gt;
&lt;p&gt;The fourth question: are estimates believable? If Postgres expects a tenant filter to return half the table when it actually returns 0.01 percent, the plan choice can be wrong. Statistics on filter columns still matter in vector search.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Build a plan review workflow:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Capture the canonical SQL for each retrieval route.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;EXPLAIN&lt;/code&gt; with representative tenant sizes and filter combinations.&lt;/li&gt;
&lt;li&gt;Compare indexed approximate search against exact search on a bounded dataset.&lt;/li&gt;
&lt;li&gt;Store a small query set for regression checks.&lt;/li&gt;
&lt;li&gt;Review plans after schema changes, statistics changes, pgvector upgrades, and model migrations.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Use route-specific examples. A support-search route with strict tenant filters is different from a recommendation route over a public catalog.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[retrieval SQL] --&gt; Explain[EXPLAIN review]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Explain --&gt; Access[index or scan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Explain --&gt; Filters[filter placement]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Explain --&gt; Limit[top k behavior]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Explain --&gt; Estimates[row estimates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Access --&gt; Tune[tuning decision]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Filters --&gt; Tune&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Limit --&gt; Tune&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Estimates --&gt; Tune&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For production, log enough context to reproduce slow plans without logging sensitive chunk text. Store query class, tenant tier, model version, filter counts, limit, index settings, and latency bucket.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Plan smell&lt;/th&gt;&lt;th&gt;Response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;No vector index used&lt;/td&gt;&lt;td&gt;Sequential scan and sort on distance&lt;/td&gt;&lt;td&gt;Check index, operator class, query form, and table size&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Filter removes candidates&lt;/td&gt;&lt;td&gt;Rows removed by filter or under-return&lt;/td&gt;&lt;td&gt;Tune search breadth, partial index, or partitioning&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Wrong operator class&lt;/td&gt;&lt;td&gt;Index exists but ignored&lt;/td&gt;&lt;td&gt;Match distance operator to index definition&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Missing &lt;code&gt;LIMIT&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Large sort or full scan behavior&lt;/td&gt;&lt;td&gt;Use top-k form for retrieval&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bad row estimates&lt;/td&gt;&lt;td&gt;Planner picks surprising path&lt;/td&gt;&lt;td&gt;Analyze table and improve statistics on filters&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Generic prepared plan&lt;/td&gt;&lt;td&gt;Good for one tenant, bad for another&lt;/td&gt;&lt;td&gt;Test tenant-size skew and plan caching behavior&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-and-tenancy-notes&quot;&gt;Security and Tenancy Notes&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN&lt;/code&gt; can expose table names, index names, predicates, and sometimes parameterized query structure. It should not expose chunk text unless &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; or logging is wrapped carelessly around application data. Keep plan capture sanitized.&lt;/p&gt;
&lt;p&gt;For tenancy, the plan must include the tenant and authorization filters. A fast plan without those filters is irrelevant and dangerous. Every performance test should include a negative authorization case.&lt;/p&gt;
&lt;h2 id=&quot;cost-notes&quot;&gt;Cost Notes&lt;/h2&gt;
&lt;p&gt;Bad plans are cloud bills. A sequential scan over embedding rows can burn CPU and I/O while returning ten chunks. A plan that misses the intended index may force a larger instance, more replicas, or a premature move to another vector database.&lt;/p&gt;
&lt;p&gt;Before buying new infrastructure, prove the query is written in an indexable form, the right index exists, statistics are current, and filters are not defeating candidate selection.&lt;/p&gt;
&lt;h2 id=&quot;observability-notes&quot;&gt;Observability Notes&lt;/h2&gt;
&lt;p&gt;Track:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Plan shape for canonical retrieval queries.&lt;/li&gt;
&lt;li&gt;Index usage counts where available.&lt;/li&gt;
&lt;li&gt;Query latency by tenant tier and filter combination.&lt;/li&gt;
&lt;li&gt;Rows returned versus requested limit.&lt;/li&gt;
&lt;li&gt;Empty-result and under-return rate.&lt;/li&gt;
&lt;li&gt;Table and index size.&lt;/li&gt;
&lt;li&gt;Autovacuum and analyze recency.&lt;/li&gt;
&lt;li&gt;Slow queries with route labels.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For high-risk routes, keep a small plan snapshot in version control or private runbooks. The goal is to notice when a migration changes retrieval behavior.&lt;/p&gt;
&lt;h2 id=&quot;backup-restore-and-dr-notes&quot;&gt;Backup, Restore, and DR Notes&lt;/h2&gt;
&lt;p&gt;After restore, query plans may change because caches are cold, statistics are stale, indexes were rebuilt, or the restored dataset differs from current production. Include representative &lt;code&gt;EXPLAIN&lt;/code&gt; checks in restore validation.&lt;/p&gt;
&lt;p&gt;If a logical restore rebuilds vector indexes, run both performance and correctness checks before re-enabling full traffic. A restored database that scans every vector row may be available but not operationally healthy.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Does the query use the expected vector index?&lt;/li&gt;
&lt;li&gt;Does the distance operator match the index operator class?&lt;/li&gt;
&lt;li&gt;Is the &lt;code&gt;ORDER BY&lt;/code&gt; expression written in an indexable form?&lt;/li&gt;
&lt;li&gt;Is &lt;code&gt;LIMIT&lt;/code&gt; present and appropriate for the route?&lt;/li&gt;
&lt;li&gt;Are tenant, lifecycle, and entitlement filters in the SQL?&lt;/li&gt;
&lt;li&gt;Do small, medium, and large tenants get acceptable plans?&lt;/li&gt;
&lt;li&gt;Are row estimates close enough for filter columns?&lt;/li&gt;
&lt;li&gt;Is exact search available as a recall baseline?&lt;/li&gt;
&lt;li&gt;Do plan checks run after index, statistics, and pgvector changes?&lt;/li&gt;
&lt;li&gt;Can slow plans be reproduced without exposing sensitive text?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; A pgvector query looks like ordinary SQL, but wrapped distance expressions, selective filters, or stale statistics can silently push the planner off the intended index — and the first symptom is a vague “pgvector is slow” ticket.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Read the plan like a database engineer before tuning anything: confirm the index is used, the &lt;code&gt;ORDER BY&lt;/code&gt; expression is written in indexable form, filters aren’t defeating candidate selection, and row estimates match reality.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; &lt;code&gt;EXPLAIN&lt;/code&gt; on the canonical retrieval query for small, medium, and large tenants shows the expected index scan, not a sequential scan plus sort.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, capture &lt;code&gt;EXPLAIN&lt;/code&gt; output for the slowest retrieval route with representative parameters, and confirm the &lt;code&gt;ORDER BY&lt;/code&gt; expression exactly matches the index’s operator class.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;pgvector README for distance operators, operator classes, HNSW, IVFFlat, and query examples: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL &lt;code&gt;EXPLAIN&lt;/code&gt; and &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; documentation: &lt;a href=&quot;https://www.postgresql.org/docs/current/using-explain.html&quot;&gt;https://www.postgresql.org/docs/current/using-explain.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL planner statistics and &lt;code&gt;ANALYZE&lt;/code&gt; documentation: &lt;a href=&quot;https://www.postgresql.org/docs/current/planner-stats.html&quot;&gt;https://www.postgresql.org/docs/current/planner-stats.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL prepared statement and generic plan behavior: &lt;a href=&quot;https://www.postgresql.org/docs/current/sql-prepare.html&quot;&gt;https://www.postgresql.org/docs/current/sql-prepare.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;pgvector iterative scan documentation (added in 0.8.0): &lt;a href=&quot;https://github.com/pgvector/pgvector/blob/master/README.md#iterative-index-scans&quot;&gt;https://github.com/pgvector/pgvector/blob/master/README.md#iterative-index-scans&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>checklist</category></item><item><title>pgvector Partitioning Strategy for Multi-Tenant RAG</title><link>https://rajivonai.com/blog/2026-06-16-pgvector-partitioning-multi-tenant-rag/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-16-pgvector-partitioning-multi-tenant-rag/</guid><description>How to design tenant-scoped pgvector search with tenant filters, partial indexes, list or hash partitioning, filtered HNSW behavior, query plans, operational limits, and isolation tradeoffs.</description><pubDate>Wed, 17 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The first tenant has 20,000 chunks. The largest tenant has 40 million. The query has &lt;code&gt;WHERE tenant_id = $1 ORDER BY embedding &amp;#x3C;=&gt; $2 LIMIT 10&lt;/code&gt;, and the team expects Postgres to make that safe, fast, and fair for everyone. Multi-tenant vector search is not solved by adding &lt;code&gt;tenant_id&lt;/code&gt; to the table.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;short-version&quot;&gt;Short Version&lt;/h2&gt;
&lt;p&gt;For multi-tenant RAG in pgvector, tenant filtering is a correctness requirement and a performance variable. The design has to decide whether tenants share one table and one vector index, use partial indexes for large tenants, use partitions, or move extreme tenants into separate physical boundaries.&lt;/p&gt;
&lt;p&gt;Start simple when tenants are small and similar: shared chunk table, mandatory tenant filter, supporting B-tree indexes, and a vector index tested with representative filters. Add partial indexes or partitions when tenant size, query volume, compliance, or operational isolation justifies the extra complexity.&lt;/p&gt;
&lt;p&gt;Do not partition because it sounds enterprise-grade. Partition because it improves pruning, restore, maintenance, tenant isolation, or query predictability enough to pay for the operational cost.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Multi-tenant RAG usually starts as a shared table because that is the fastest way to ship. The tenant ID is present, queries include a filter, and the first few customers look similar enough that the design feels settled.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Approximate vector search and tenant filtering interact in ways that surprise relational engineers. A normal B-tree lookup on &lt;code&gt;tenant_id&lt;/code&gt; narrows the candidate set exactly. A vector index ranks nearby vectors, then filters and limits interact with that candidate flow. Under selective filters, approximate search can return too few rows or spend work on vectors the tenant is not allowed to see.&lt;/p&gt;
&lt;p&gt;The problem gets worse with uneven tenants:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;One large tenant can dominate index size and cache behavior.&lt;/li&gt;
&lt;li&gt;Small tenants may under-return if global nearest neighbors mostly belong to others.&lt;/li&gt;
&lt;li&gt;A noisy tenant can create query load that affects everyone.&lt;/li&gt;
&lt;li&gt;Tenant deletion or export is harder in a large shared table.&lt;/li&gt;
&lt;li&gt;Per-tenant restore may be impossible without careful design.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The database architecture needs to encode tenant shape, not only tenant identity.&lt;/p&gt;
&lt;h2 id=&quot;core-technical-explanation&quot;&gt;Core Technical Explanation&lt;/h2&gt;
&lt;p&gt;The baseline shared-table design looks simple:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; document_id, chunk_id, body&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; document_chunks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tenant_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; deleted_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; embedding &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This query has two jobs. It enforces authorization, and it ranks by vector distance. The hard part is making both jobs efficient and reliable.&lt;/p&gt;
&lt;p&gt;Common physical designs:&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Design&lt;/th&gt;&lt;th&gt;Good fit&lt;/th&gt;&lt;th&gt;Main cost&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Shared table and shared vector index&lt;/td&gt;&lt;td&gt;Many small similar tenants&lt;/td&gt;&lt;td&gt;Filtered ANN surprises&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Shared table with partial indexes&lt;/td&gt;&lt;td&gt;Few large or high-value tenants&lt;/td&gt;&lt;td&gt;Index sprawl and migration work&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hash partition by tenant&lt;/td&gt;&lt;td&gt;Many tenants with broad distribution&lt;/td&gt;&lt;td&gt;More partitions to manage&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;List partition by tenant tier or tenant&lt;/td&gt;&lt;td&gt;Clear tenant boundaries or large tenants&lt;/td&gt;&lt;td&gt;Partition count and DDL overhead&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Separate database or cluster&lt;/td&gt;&lt;td&gt;Strict isolation or very large tenants&lt;/td&gt;&lt;td&gt;Cost and operational duplication&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;PostgreSQL partition pruning can reduce the data touched when the partition key is constrained — the planner excludes partitions that cannot match the &lt;code&gt;WHERE&lt;/code&gt; clause before scanning anything. Whether a pgvector query benefits depends on whether &lt;code&gt;tenant_id&lt;/code&gt; is the (or part of the) partition key, whether each partition carries its own vector index, and whether the query’s tenant filter is a literal or bindable constant the planner can prune on at plan time versus execution time.&lt;/p&gt;
&lt;p&gt;pgvector filtered search behavior and iterative scan options are version-specific: &lt;code&gt;hnsw.iterative_scan&lt;/code&gt; was added in pgvector 0.8.0 (October 2024) and defaults to &lt;code&gt;off&lt;/code&gt;. On partitioned tables, iterative scan applies per-partition — each partition’s index scan expands independently — so a query touching several tenant partitions pays the iterative-scan cost multiple times, not once.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;A practical operating model starts with tenant tiers.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tier 1: shared tenants.&lt;/strong&gt; Most tenants live in a shared partition or table. They use mandatory tenant filters, lifecycle filters, and standard retrieval limits. This keeps onboarding simple.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tier 2: heavy tenants.&lt;/strong&gt; Tenants with large corpora or high query volume get partial indexes, dedicated partitions, or stronger routing rules. Their query plans are tested separately.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tier 3: isolated tenants.&lt;/strong&gt; Regulated, very large, or noisy tenants get separate databases, clusters, or physical deployments. This is not only for security; it can be an operational fairness decision.&lt;/p&gt;
&lt;p&gt;The architecture should make routing explicit:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[tenant retrieval query] --&gt; Classify[tenant tier lookup]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Classify --&gt; Shared[shared table]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Classify --&gt; Heavy[dedicated partition or partial index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Classify --&gt; Isolated[separate database boundary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Shared --&gt; Auth[tenant and entitlement filter]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Heavy --&gt; Auth&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Isolated --&gt; Auth&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Auth --&gt; Results[authorized ranked chunks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Keep a tenant catalog outside the vector query path that records tier, corpus size, index strategy, model version, and restore requirements. Without that catalog, partitioning becomes tribal knowledge.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Production symptom&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Missing tenant filter&lt;/td&gt;&lt;td&gt;Cross-tenant data exposure&lt;/td&gt;&lt;td&gt;Query builders, tests, and database policies&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Global index with selective filter&lt;/td&gt;&lt;td&gt;Too few results for small tenants&lt;/td&gt;&lt;td&gt;Tune search breadth, partial indexes, or partitions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Too many partitions&lt;/td&gt;&lt;td&gt;Slow planning and DDL complexity&lt;/td&gt;&lt;td&gt;Partition by tier or hash, not every tenant by default&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Large tenant in shared table&lt;/td&gt;&lt;td&gt;Cache and index behavior degrade for all&lt;/td&gt;&lt;td&gt;Promote tenant to heavier tier&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Partial index sprawl&lt;/td&gt;&lt;td&gt;Migrations become fragile&lt;/td&gt;&lt;td&gt;Use explicit tenant promotion workflow&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tenant deletion scans huge table&lt;/td&gt;&lt;td&gt;Compliance request takes too long&lt;/td&gt;&lt;td&gt;Partition or maintain tenant-local delete paths&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;security-and-tenancy-notes&quot;&gt;Security and Tenancy Notes&lt;/h2&gt;
&lt;p&gt;Tenant isolation must be enforced before text leaves the database. Application-side filtering after vector retrieval is too late because unauthorized chunks may already be in memory, logs, traces, or prompts.&lt;/p&gt;
&lt;p&gt;Use negative tests: ask for a document title from tenant A while authenticated as tenant B and prove no candidate crosses the boundary. Run those tests after index changes, partition changes, restore drills, and model-version migrations.&lt;/p&gt;
&lt;p&gt;Row-level security may strengthen protection, but it should not be the only control if background workers, admin tools, and migration scripts bypass normal application roles — RLS policies apply per role, and a worker connecting as a superuser or with &lt;code&gt;BYPASSRLS&lt;/code&gt; skips them entirely by design, which is exactly the gap that turns a “defense in depth” control into the only control.&lt;/p&gt;
&lt;h2 id=&quot;cost-notes&quot;&gt;Cost Notes&lt;/h2&gt;
&lt;p&gt;Partitioning and partial indexes can reduce query waste, but they increase operational cost. Each extra index consumes storage, backup space, build time, and maintenance attention. Separate clusters multiply baseline spend.&lt;/p&gt;
&lt;p&gt;The cost decision should include fairness. A large tenant that forces every other tenant onto a larger database instance is already costing the platform money. Isolation can be cheaper than shared-resource interference.&lt;/p&gt;
&lt;h2 id=&quot;observability-notes&quot;&gt;Observability Notes&lt;/h2&gt;
&lt;p&gt;Track metrics by tenant tier:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Query latency and timeout rate.&lt;/li&gt;
&lt;li&gt;Rows returned versus requested limit.&lt;/li&gt;
&lt;li&gt;Empty-result and under-return rate.&lt;/li&gt;
&lt;li&gt;Vector index size by table or partition.&lt;/li&gt;
&lt;li&gt;Top tenants by query volume and storage.&lt;/li&gt;
&lt;li&gt;Autovacuum lag on tenant-heavy partitions.&lt;/li&gt;
&lt;li&gt;Plan changes for representative tenant queries.&lt;/li&gt;
&lt;li&gt;Restore and delete time by tenant class.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Use &lt;code&gt;EXPLAIN&lt;/code&gt; examples for small, medium, and large tenants. A single plan from a median tenant is not enough.&lt;/p&gt;
&lt;h2 id=&quot;backup-restore-and-dr-notes&quot;&gt;Backup, Restore, and DR Notes&lt;/h2&gt;
&lt;p&gt;Partitioning can improve tenant-level operations if the runbook is designed for it. A tenant-local partition may make export, deletion, restore testing, or archival easier. It can also complicate global restore because more physical objects need validation.&lt;/p&gt;
&lt;p&gt;For shared tables, per-tenant restore is usually an application-level reconstruction problem, not a database restore problem. Decide whether the business needs tenant-level point-in-time recovery. If it does, shared-table design may need compensating audit logs or event replay.&lt;/p&gt;
&lt;h2 id=&quot;decision-checklist&quot;&gt;Decision Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Are tenant filters mandatory in every retrieval query?&lt;/li&gt;
&lt;li&gt;How skewed is tenant corpus size and query rate?&lt;/li&gt;
&lt;li&gt;Do small tenants under-return under the global index?&lt;/li&gt;
&lt;li&gt;Which tenants need stronger isolation for compliance or fairness?&lt;/li&gt;
&lt;li&gt;Does partitioning improve pruning or only add complexity?&lt;/li&gt;
&lt;li&gt;How many partitions can the team operate comfortably?&lt;/li&gt;
&lt;li&gt;Are partial indexes governed by a promotion process?&lt;/li&gt;
&lt;li&gt;Can tenant deletion, export, and restore meet requirements?&lt;/li&gt;
&lt;li&gt;Are query plans tested by tenant tier?&lt;/li&gt;
&lt;li&gt;Is the design reversible if tenant distribution changes?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; A shared chunk table with a &lt;code&gt;tenant_id&lt;/code&gt; filter works until tenant sizes diverge — a large tenant degrades cache and index behavior for everyone, and small tenants silently under-return against a global vector index.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Tier tenants explicitly — shared table for small/similar tenants, partial indexes or dedicated partitions for heavy tenants, separate databases for regulated or extreme tenants — and promote tenants between tiers deliberately, not by accident.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; A negative test (request tenant A’s document while authenticated as tenant B) returns zero candidates after every index, partition, or restore change, and &lt;code&gt;EXPLAIN&lt;/code&gt; plans are captured for small, medium, and large tenants, not just a median case.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, run the cross-tenant negative test against production, and identify which tenant (if any) is large enough to warrant promotion out of the shared table.&lt;/p&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;pgvector README for filtered search, HNSW, IVFFlat, and iterative scan behavior: &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;https://github.com/pgvector/pgvector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL declarative partitioning and partition pruning documentation: &lt;a href=&quot;https://www.postgresql.org/docs/current/ddl-partitioning.html&quot;&gt;https://www.postgresql.org/docs/current/ddl-partitioning.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL partial index documentation: &lt;a href=&quot;https://www.postgresql.org/docs/current/indexes-partial.html&quot;&gt;https://www.postgresql.org/docs/current/indexes-partial.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PostgreSQL row-level security documentation: &lt;a href=&quot;https://www.postgresql.org/docs/current/ddl-rowsecurity.html&quot;&gt;https://www.postgresql.org/docs/current/ddl-rowsecurity.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Aurora PostgreSQL limits and quotas (if running on Aurora): &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.AuroraPostgreSQL.html&quot;&gt;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.AuroraPostgreSQL.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>architecture</category><category>failures</category><category>checklist</category></item><item><title>Datadog DBM: What Database Teams Should Actually Monitor</title><link>https://rajivonai.com/blog/2026-06-15-datadog-dbm-what-database-teams-should-actually-monitor/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-15-datadog-dbm-what-database-teams-should-actually-monitor/</guid><description>Datadog Database Monitoring can surface enormous detail — and bill for it. The skill is choosing the few signals that answer real cost and reliability questions, and not paying to collect noise nobody acts on.</description><pubDate>Mon, 15 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Datadog Database Monitoring (DBM) will happily show you every query, every plan, and every host metric your fleet produces. The trap is treating “more telemetry” as “better observability.” The teams who get value from DBM monitor a short list of signals tied to decisions — and deliberately ignore the rest, because in DBM the rest is also a line on the bill.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A team turns on Datadog DBM expecting clarity and gets a firehose: thousands of normalized queries, host dashboards, plan samples, and a steadily climbing Datadog invoice.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Six weeks later the on-call engineer still can’t answer “why was the database slow at 2am?” any faster than before, because the dashboards show &lt;em&gt;everything&lt;/em&gt; and therefore foreground &lt;em&gt;nothing&lt;/em&gt;. Meanwhile DBM is now a noticeable cost itself — host-based DBM pricing plus custom metrics plus log ingestion. Observability that you pay for but don’t act on is just a second cost problem stacked on the first.&lt;/p&gt;
&lt;p&gt;Observability spend is real spend, and DBM has several meters running at once:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Per-host DBM&lt;/strong&gt; scales with your fleet — every replica and non-prod instance you instrument adds cost, whether or not anyone reads its dashboard.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Custom metrics&lt;/strong&gt; bill per unique metric+tag combination. High-cardinality tags (per-user, per-request-id) can multiply a single metric into thousands of billable timeseries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Log ingestion and retention&lt;/strong&gt; for slow-query and audit logs add a third meter.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The financial point cuts both ways: under-monitoring means you can’t see the cost and reliability problems that matter, while &lt;em&gt;naïve&lt;/em&gt; monitoring means you pay to collect telemetry nobody uses. The question that matters: for every signal you’re paying to collect, is there an alert and a runbook attached to it — or is it just noise on the bill?&lt;/p&gt;
&lt;h2 id=&quot;choosing-signals-tied-to-decisions&quot;&gt;Choosing Signals Tied to Decisions&lt;/h2&gt;
&lt;p&gt;Technical root causes behind why DBM bills and dashboards balloon:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Instrumenting everything by default&lt;/strong&gt; — every non-prod and idle replica gets a DBM host agent.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;High-cardinality custom metrics&lt;/strong&gt; — tagging metrics with unbounded values (user IDs, request IDs) explodes billable timeseries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Collecting without alerting&lt;/strong&gt; — query samples and metrics gathered but wired to no alert and no runbook.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Symptom-level alerts&lt;/strong&gt; — “host CPU high” instead of leading indicators (replication lag, connection saturation, storage runway).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No baseline&lt;/strong&gt; — without a normal range, dashboards can’t tell you whether 2am was abnormal.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Every host instrumented by default] --&gt; B[Per-host cost scales with fleet]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[High-cardinality tags] --&gt; D[Billable timeseries explode]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E[Collected but unalerted signals] --&gt; F[Cost with no decision attached]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; G[Scope to what you act on]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[Small signal list tied to alerts and runbooks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The signals worth monitoring, tied to a decision, at minimum:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Top queries by total time and by I/O&lt;/strong&gt; — the same &lt;code&gt;pg_stat_statements&lt;/code&gt; view DBM surfaces fleet-wide; this is your cost and latency hot list.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replication lag&lt;/strong&gt; — with a defined normal range and a threshold alert (not just a graph).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Connection saturation&lt;/strong&gt; — active vs &lt;code&gt;max_connections&lt;/code&gt;, alerted &lt;em&gt;before&lt;/em&gt; the limit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Storage runway&lt;/strong&gt; — free space / days-to-full, alerted with lead time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cache hit ratio&lt;/strong&gt; and &lt;strong&gt;deadlocks/lock waits&lt;/strong&gt; — early signals of memory pressure and contention.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Long-running / idle-in-transaction&lt;/strong&gt; — the transactions that block vacuum and cause incidents.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And on the cost side of DBM itself:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Which hosts are instrumented — are idle replicas and non-prod paying for DBM they don’t need?&lt;/li&gt;
&lt;li&gt;Are any custom metrics high-cardinality? Check your top metrics by timeseries count.&lt;/li&gt;
&lt;li&gt;For every collected signal: is there an alert and a runbook? If not, why collect it?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;(Illustrative — the patterns these reviews repeatedly surface, not a specific client.)&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBM was enabled on every host including 6 idle non-prod replicas; scoping DBM to production and active readers cut DBM host cost without losing a single useful dashboard.&lt;/li&gt;
&lt;li&gt;A custom metric tagged with &lt;code&gt;request_id&lt;/code&gt; had ballooned into tens of thousands of billable timeseries; dropping the unbounded tag collapsed it to a handful.&lt;/li&gt;
&lt;li&gt;The team had rich query dashboards but no alert on replication lag — the one signal that would have warned them before a read-after-write incident.&lt;/li&gt;
&lt;li&gt;Slow-query logs were ingested and retained for 30 days but never queried; trimming retention cut log cost with no operational loss.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Good database observability and a controlled observability bill are the same discipline as the rest of cost engineering: collect what answers a question, alert on what you’ll act on, and measure the cost of the tooling itself.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Wrong move&lt;/th&gt;&lt;th&gt;What happens in production&lt;/th&gt;&lt;th&gt;Better approach&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Instrument every host by default&lt;/td&gt;&lt;td&gt;Idle replicas and non-prod pay full DBM cost for zero use&lt;/td&gt;&lt;td&gt;Scope DBM to production and active readers; instrument non-prod only when actively debugging&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tag metrics with unbounded values&lt;/td&gt;&lt;td&gt;A single metric explodes into thousands of billable timeseries&lt;/td&gt;&lt;td&gt;Audit top custom metrics by timeseries count and remove unbounded tags&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Alert on symptoms&lt;/td&gt;&lt;td&gt;”Host CPU high” fires without explaining why or what to do&lt;/td&gt;&lt;td&gt;Alert on leading indicators — replication lag, connection saturation, storage runway&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Collect signals with no alert or runbook&lt;/td&gt;&lt;td&gt;You pay to gather telemetry nobody ever acts on&lt;/td&gt;&lt;td&gt;For every collected signal, require an attached alert and runbook or stop collecting it&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; DBM’s granularity is also its cost model — instrumenting everything by default and tagging with high-cardinality values turns observability into an unbounded bill nobody’s using fully.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Define the decision behind every signal, scope DBM to production and active replicas, kill high-cardinality tags, and alert on leading indicators with a threshold and an owner.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; DBM’s own cost line drops after scoping, and the team can answer “why was it slow” faster because dashboards show a short list instead of everything.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, audit which hosts are instrumented and which custom metrics are high-cardinality — those two checks usually explain most of an inflated DBM bill.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>cost</category><category>checklist</category></item><item><title>How to Run a Database Cost &amp; Reliability Review</title><link>https://rajivonai.com/blog/2026-06-12-how-to-run-a-database-cost-and-reliability-review/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-12-how-to-run-a-database-cost-and-reliability-review/</guid><description>A practitioner walkthrough of the review method: what to look at, in what order, how to quantify an opportunity honestly, and how to turn findings into a prioritized 30/60/90-day plan.</description><pubDate>Fri, 12 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A good cost review is not a tool that prints a number. It is a sequence: get the right access, look at nine areas in order, quantify each opportunity with its own math, and rank the fixes by impact, effort, and risk. Here is the method, end to end.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most database “cost reviews” are either a vendor dashboard screenshot or a one-off “make it cheaper” sprint.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Neither produces something a team can act on with confidence. The first lacks engineering judgment; the second lacks reliability guardrails and tends to trade away durability for a short-term saving. A real review is structured, evidence-based, and sequenced.&lt;/p&gt;
&lt;p&gt;Database spend grows quietly and compounds. The cost of &lt;em&gt;not&lt;/em&gt; reviewing is two-sided: you keep paying for waste (oversized instances, idle replicas, bloat), and you carry unmeasured reliability risk (untested failover, unverified restores) that turns into an expensive incident at the worst time. Root causes that drift bills without anyone noticing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Instances sized for a launch and never revisited.&lt;/li&gt;
&lt;li&gt;Storage and I/O charges that grow without anyone watching the trend.&lt;/li&gt;
&lt;li&gt;Replicas added “to be safe” that never receive read traffic.&lt;/li&gt;
&lt;li&gt;Bloat and unused indexes inflating storage and write cost.&lt;/li&gt;
&lt;li&gt;Observability too thin to even see where the money goes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A structured review surfaces both waste and risk — and, just as important, it produces a &lt;em&gt;prioritized&lt;/em&gt; plan, so the savings actually get implemented instead of dying in a backlog. The question that matters: is there a repeatable method here, or does every review start from a blank page?&lt;/p&gt;
&lt;h2 id=&quot;the-nine-area-method&quot;&gt;The Nine-Area Method&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;0. Get read-only access and a metrics window.&lt;/strong&gt; Without it you are guessing. A replica, snapshot, or read-only role plus 2–4 weeks of metrics is enough. Sign a mutual NDA; never take write access for a review.&lt;/p&gt;
&lt;p&gt;Then work the &lt;strong&gt;nine areas&lt;/strong&gt;, in this order (cheap-to-see first, riskier-to-fix later):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Read-only access + metrics window] --&gt; B[1 Cost]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[2 Performance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[3 Reliability]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[4 Storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[5 Replication]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[6 Backup and recovery]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[7 Observability]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[8 Security]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[9 Automation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[Prioritized 30-60-90 plan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Cost&lt;/strong&gt; — instance sizing vs utilization, idle/non-prod, pricing model, storage/I/O drivers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Performance&lt;/strong&gt; — top queries (&lt;code&gt;pg_stat_statements&lt;/code&gt;), index effectiveness, connections, cache hit ratio.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reliability&lt;/strong&gt; — failover tested, HA posture, single points of failure, headroom.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Storage&lt;/strong&gt; — bloat/dead tuples, growth trend, retention/archival.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replication&lt;/strong&gt; — replica utilization, lag visibility, read/write routing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Backup &amp;#x26; recovery&lt;/strong&gt; — backups exist, restores tested, PITR/RPO understood.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Observability&lt;/strong&gt; — metrics coverage, query-level insight, alerting on leading indicators.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Security&lt;/strong&gt; — encryption, least-privilege, audit/change visibility.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Automation&lt;/strong&gt; — which toil could be automated to cut risk and cost.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Quantifying an opportunity honestly is where reviews earn or lose trust. For each finding:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Show the math.&lt;/strong&gt; “Writer at 14% peak CPU over 30 days; one class down ≈ 50% of compute cost ≈ $X/month.”&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Give a range, not a point.&lt;/strong&gt; Real savings depend on validation and execution.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never promise a percentage before you’ve looked.&lt;/strong&gt; Be wary of anyone who does.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Flag the reliability tradeoff&lt;/strong&gt; of every cost cut explicitly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Prioritize by scoring each finding on impact (cost or reliability), effort to fix, and risk of the fix. The plan writes itself when you sort by those three: low-risk high-impact first, risky changes later with guardrails. That produces a 30/60/90 plan:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;First 30 days — instrument &amp;#x26; capture low-risk wins:&lt;/strong&gt; enable statement stats and slow-query logging, add leading-indicator alerts, remove clearly idle resources, confirm restores work.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Days 31–60 — right-size &amp;#x26; reduce structural waste:&lt;/strong&gt; act on sizing and pricing findings backed by data, fix replica routing, begin bloat/index cleanup.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Days 61–90 — harden &amp;#x26; sustain:&lt;/strong&gt; failover testing, pooling, automation of toil, and a baseline so you can prove the changes worked.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;(Illustrative, not a specific case.)&lt;/em&gt; A typical first review can surface: one oversized non-prod-hours pattern, one or two idle replicas, a handful of unused indexes, a top-three I/O query missing an index, and — almost always — at least one untested restore or failover. The cost items pay for the review; the reliability items are why you do it before an incident.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Wrong move&lt;/th&gt;&lt;th&gt;What happens in production&lt;/th&gt;&lt;th&gt;Better approach&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Cut cost without checking reliability&lt;/td&gt;&lt;td&gt;An untested failover or restore fails during the next incident&lt;/td&gt;&lt;td&gt;Flag the reliability tradeoff of every cost cut explicitly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Promise a savings percentage before looking&lt;/td&gt;&lt;td&gt;The number is wrong and trust in the review collapses&lt;/td&gt;&lt;td&gt;Show the math per finding and give a range, not a point estimate&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Fix findings in isolation, unranked&lt;/td&gt;&lt;td&gt;High-effort, low-impact work gets done first; backlog rots&lt;/td&gt;&lt;td&gt;Rank by impact × effort × risk and sequence into 30/60/90&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Take write access “to move faster”&lt;/td&gt;&lt;td&gt;Review scope creeps into changes with no rollback plan&lt;/td&gt;&lt;td&gt;Stay read-only for the review; write access is a separate, scoped step&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Ad hoc “make it cheaper” sprints skip reliability guardrails and produce findings nobody prioritizes or implements.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Run the nine areas in order starting from read-only access, quantify every finding with its own math and a range, and rank by impact × effort × risk into a 30/60/90 plan.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Each finding has a dollar range backed by a measurement, and the plan re-measures after changes to confirm they landed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, secure read-only access and a 2–4 week metrics window, then walk the first three areas (cost, performance, reliability) and write down what you find with evidence, not assumptions.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cost</category><category>checklist</category></item><item><title>Aurora Cost Optimization: The Hidden Database Bill</title><link>https://rajivonai.com/blog/2026-06-11-aurora-cost-optimization-the-hidden-database-bill/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-11-aurora-cost-optimization-the-hidden-database-bill/</guid><description>Aurora cost hides in places the console doesn&apos;t foreground — I/O charges, oversized writers and readers, replica sprawl, and storage. A structured way to find and reduce each without hurting reliability.</description><pubDate>Thu, 11 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Aurora’s bill is three things — compute, storage, and I/O — and the one that surprises teams is I/O, because it scales with how your queries read data, not with anything you provisioned. Most Aurora cost reviews stop at instance class and miss the line that’s actually growing.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;An Aurora bill climbs and the obvious lever — instance class — doesn’t explain it. The writer looks busy enough. Nobody touched the cluster config. Yet month over month the number rises.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The cost is real but diffuse: a bit of oversizing, a couple of idle readers, storage that only grows, and an I/O charge driven by query patterns nobody is watching.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;I/O charges from inefficient reads.&lt;/strong&gt; Aurora bills per I/O operation on standard configuration. A few high-frequency queries doing sequential scans on large tables can dominate the bill while looking unremarkable in the query list.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oversized writers and readers.&lt;/strong&gt; Instances sized for a historical peak (a backfill, a launch) and never revisited; steady-state CPU sits low.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replica sprawl.&lt;/strong&gt; Readers added for HA or “reporting” that no longer receive meaningful read traffic — full instance cost for near-zero use.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Read/write routing gaps.&lt;/strong&gt; The primary carries read load the readers were paid to absorb.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Storage that only grows.&lt;/strong&gt; Aurora storage auto-grows and doesn’t shrink; bloat and unarchived cold data inflate it permanently.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For a mid-size Aurora estate, the I/O line and replica sprawl together are frequently the largest recoverable spend — and both are low-risk to address once you can see them. Unlike a risky schema change, removing an idle reader or indexing a hot sequential-scan query is reversible and safe. The question that matters: which of compute, storage, or I/O is actually driving this bill, and has anyone measured it directly rather than assumed it’s instance class?&lt;/p&gt;
&lt;h2 id=&quot;finding-the-real-driver&quot;&gt;Finding the Real Driver&lt;/h2&gt;
&lt;p&gt;The financial point: the biggest Aurora wins are usually the &lt;em&gt;least&lt;/em&gt; dangerous ones, which is exactly why leaving them in place is hard to justify once measured.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Aurora bill] --&gt; B[Compute]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; C[Storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; D[I-O]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[Peak vs average CPU per instance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; F[Growth trend and bloat]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; G[Top sequential-scan queries]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; H[Right-size or remove idle readers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; I[Trend and reclaim]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; J[Index or rewrite]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; K[Aurora I-O-Optimized evaluation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Review checklist:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What is your I/O charge as a share of the cluster bill, and which queries drive it?&lt;/li&gt;
&lt;li&gt;What is peak (not average) CPU/connections on each writer and reader over 30 days?&lt;/li&gt;
&lt;li&gt;Does each reader receive real read traffic? Pull per-replica read metrics.&lt;/li&gt;
&lt;li&gt;Is read traffic actually routed to readers (reader endpoint / routing layer)?&lt;/li&gt;
&lt;li&gt;Would &lt;strong&gt;Aurora I/O-Optimized&lt;/strong&gt; be cheaper given your I/O-to-compute ratio?&lt;/li&gt;
&lt;li&gt;Is storage growth trended? What’s the largest contributor (bloat, logs, cold data)?&lt;/li&gt;
&lt;li&gt;Are there indexes that would convert your top sequential scans into index scans?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;(Illustrative — the pattern these reviews repeatedly surface, not a specific client.)&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Three high-frequency queries accounted for a large share of logical reads via sequential scans; targeted indexes plus one query rewrite cut I/O operations materially and improved latency.&lt;/li&gt;
&lt;li&gt;A reporting reader showed negligible reads after reporting moved elsewhere; removing it recovered the full reader cost with no functional impact.&lt;/li&gt;
&lt;li&gt;An analytics writer sized during a 14-month-old backfill ran at ~14% peak CPU; a validated step-down recovered roughly half its compute cost.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Every one of these is read-only to &lt;em&gt;find&lt;/em&gt; and reversible to &lt;em&gt;apply&lt;/em&gt; — make the change in staging, confirm the metric moved, then promote.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Wrong move&lt;/th&gt;&lt;th&gt;What happens in production&lt;/th&gt;&lt;th&gt;Better approach&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Assume the bill is instance class&lt;/td&gt;&lt;td&gt;I/O and storage keep growing unaddressed&lt;/td&gt;&lt;td&gt;Break the bill into compute / storage / I/O before choosing a lever&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Remove a reader without a consumer check&lt;/td&gt;&lt;td&gt;A report or job silently breaks&lt;/td&gt;&lt;td&gt;Audit real traffic and confirm no consumer depends on it first&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Right-size against average CPU&lt;/td&gt;&lt;td&gt;Instance is undersized for month-end or spike load&lt;/td&gt;&lt;td&gt;Right-size against peak, including known spike windows&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Index every sequential scan indiscriminately&lt;/td&gt;&lt;td&gt;Write amplification rises, offsetting the I/O savings&lt;/td&gt;&lt;td&gt;Target the highest-frequency sequential-scan queries first, validate in staging&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Aurora bills climb from I/O, replica sprawl, and storage growth — none of which show up if the review only looks at instance class.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Break the bill into compute / storage / I/O, attack I/O at the query level with targeted indexes, audit every reader for real traffic, right-size against peak, and trend storage growth.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; I/O operations drop after indexing the top sequential-scan queries, and removed idle readers show zero downstream impact after a consumer check.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, pull your I/O charge as a share of the cluster bill and identify the queries driving it — that single number usually reveals whether instance class was ever the real lever.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>cost</category><category>aurora</category></item><item><title>Cassandra Cache Tuning: Row Cache, Key Cache, and Chunk Cache</title><link>https://rajivonai.com/blog/2026-06-10-cassandra-cache-tuning-row-key-chunk-cache/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-10-cassandra-cache-tuning-row-key-chunk-cache/</guid><description>Why Cassandra&apos;s row cache is usually the wrong lever, and how key cache and chunk cache actually cut read latency without the invalidation trap.</description><pubDate>Wed, 10 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Cassandra ships with three distinct caches, and the one most operators reach for first — row cache — is the one that’s off by default for a reason. Turning it on for the wrong workload doesn’t just fail to help; it adds CPU and memory overhead while quietly making writes slower, because every write to a cached partition evicts the entire cached row.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A read-heavy Cassandra table is serving elevated read latency despite adequate CPU, memory, and disk headroom. &lt;code&gt;nodetool tablestats&lt;/code&gt; shows a low SSTable count per read, so it isn’t the read-amplification problem that a bad compaction strategy would cause. The obvious instinct is to enable row caching — after all, an in-memory cache should make reads faster. The team enables row cache on the hot table and read latency gets worse under write load, not better.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra’s three caches solve different problems, and Cassandra’s own row cache implementation has a specific, non-obvious cost: it caches the &lt;em&gt;entire deserialized row&lt;/em&gt;, not just a lookup index. Any write to that partition invalidates the whole cached row, forcing a full re-read from disk on the next access. For a table that’s read-heavy &lt;em&gt;and&lt;/em&gt; write-heavy on the same rows — which describes most production Cassandra tables — row cache thrashes: it gets populated, immediately invalidated by the next write, and repopulated, burning CPU and heap on cache churn that never pays off in hit rate.&lt;/p&gt;
&lt;p&gt;The core question isn’t “should I cache reads” — Cassandra already does that at two other layers by default. The question is which of the three caches actually matches this workload’s access pattern.&lt;/p&gt;
&lt;h2 id=&quot;the-three-cache-architecture&quot;&gt;The Three-Cache Architecture&lt;/h2&gt;
&lt;p&gt;Cassandra’s read path touches up to three independent caches before it ever reads compressed data off disk, each with a different job and a different default.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Read[Read Request] --&gt; KeyCache{Key Cache Hit?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    KeyCache --&gt;|Yes — skip partition index scan| ChunkCache{Chunk Cache Hit?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    KeyCache --&gt;|No| PartitionIndex[Scan Partition Index on Disk]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PartitionIndex --&gt; ChunkCache&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ChunkCache --&gt;|Yes — decompressed chunk in memory| Serve[Serve Row]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ChunkCache --&gt;|No| DiskRead[Read and Decompress SSTable Chunk]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DiskRead --&gt; Serve&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    RowCache{Row Cache Enabled and Hit?} -.-&gt;|Bypasses everything above| Serve&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-key-cache--on-by-default-rarely-worth-touching&quot;&gt;1. Key Cache — On by Default, Rarely Worth Touching&lt;/h3&gt;
&lt;p&gt;The key cache stores the on-disk location of a partition key within its SSTable, so a read can skip the partition index scan entirely. It defaults to &lt;code&gt;auto&lt;/code&gt; — &lt;code&gt;min(5% of heap, 100MB)&lt;/code&gt; — and is on-heap. This is close to free: it’s small, it has no write-invalidation problem (a key’s disk location doesn’t change on every write to that partition), and disabling it almost never helps. Leave it on; there’s rarely a reason to tune it beyond confirming it isn’t disabled.&lt;/p&gt;
&lt;h3 id=&quot;2-chunk-cache--the-lever-datastax-actually-recommends&quot;&gt;2. Chunk Cache — The Lever DataStax Actually Recommends&lt;/h3&gt;
&lt;p&gt;The chunk cache (&lt;code&gt;file_cache_size_in_mb&lt;/code&gt; in &lt;code&gt;cassandra.yaml&lt;/code&gt;) caches decompressed SSTable data chunks in off-heap memory, defaulting to the smaller of 512MB or 1/4 of heap. Because it caches at the compression-chunk level rather than the whole-row level, a write to one row in a chunk doesn’t invalidate the other rows sharing that chunk the way row cache invalidation does. This is the cache DataStax’s own documentation points operators toward when row cache looks tempting: it gives most of the read-latency benefit without the write-invalidation cliff.&lt;/p&gt;
&lt;h3 id=&quot;3-row-cache--off-by-default-narrow-use-case&quot;&gt;3. Row Cache — Off by Default, Narrow Use Case&lt;/h3&gt;
&lt;p&gt;Row cache stores the fully deserialized row, off-heap. It is disabled by default (&lt;code&gt;row_cache_size_in_mb: 0&lt;/code&gt;) because the win only shows up for a specific shape of workload: partitions that are read far more often than they’re written, and ideally static or slowly-changing. A reference/lookup table that’s read thousands of times per write is a reasonable candidate. A table receiving continuous writes to the same partitions it’s also serving hot reads from — the shape most operational tables actually have — is exactly the case where row cache makes things worse.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Check current cache effectiveness before changing anything:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Per-table cache hit rates&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;nodetool&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; info&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -A&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Key Cache&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;nodetool&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; tablestats&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;keyspac&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;tabl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -i&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; cache&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If chunk cache hit rate is low on a read-latency-sensitive table, the fix is usually more chunk cache headroom, not row cache:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# cassandra.yaml — raise chunk cache ceiling on a node with heap to spare&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;file_cache_size_in_mb&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1024&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To actually validate whether a specific table is a legitimate row-cache candidate, check its write-to-read ratio and partition volatility before enabling it — not after:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Enable row cache only after confirming low write volume to this table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; reference&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;currency_codes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; caching &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;keys&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;ALL&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;rows_per_partition&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;ALL&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;rows_per_partition&lt;/code&gt; can also be capped (e.g., &lt;code&gt;&apos;100&apos;&lt;/code&gt;) instead of &lt;code&gt;&apos;ALL&apos;&lt;/code&gt; to bound memory use per partition when only the most recent rows in a wide partition are actually hot.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Row cache on a write-heavy table&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Every write invalidates the cached row, forcing a full re-read on next access — net negative on both CPU and latency.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reserve row cache for genuinely read-dominant, low-write reference tables. Check write volume before enabling.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Oversized chunk cache on a memory-constrained node&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chunk cache is off-heap; raising it without headroom pressures the OS page cache and can trigger swapping.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Size chunk cache against actual free system RAM, not just “more is better.”&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Disabling key cache to “save memory”&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Every read now pays a full partition-index scan even when the win from disabling was under 100MB of heap.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Leave key cache on &lt;code&gt;auto&lt;/code&gt; unless heap pressure is severe and every other lever has already been tried.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Enabling row cache on the wrong workload adds overhead without improving read latency, because writes to hot partitions invalidate the cache faster than it can pay off.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Default to key cache (already on) and chunk cache (tune the ceiling) for general read-latency work; reserve row cache for a narrow set of read-dominant, low-write tables.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Cassandra’s own defaults reflect this — row cache ships disabled, chunk cache ships on, and DataStax’s documentation explicitly recommends chunk cache tuning over row cache for most workloads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before enabling row cache on any table, run &lt;code&gt;nodetool tablestats&lt;/code&gt; to check its write volume. If it’s taking meaningful write traffic, tune chunk cache instead.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>PostgreSQL Bloat, Index Waste, and Cloud Cost</title><link>https://rajivonai.com/blog/2026-06-10-postgresql-bloat-index-waste-and-cloud-cost/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-10-postgresql-bloat-index-waste-and-cloud-cost/</guid><description>Table and index bloat and unused indexes are well-known Postgres problems — and direct cloud-cost problems: wasted storage, write amplification, and extra I/O. How to measure both with read-only queries and remediate safely.</description><pubDate>Wed, 10 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Bloat and unused indexes are usually filed under “performance hygiene.” On a cloud database they are also a line on the bill: storage you pay for and never use, writes amplified across indexes nobody reads, and I/O spent scanning dead space. The fixes are well understood and mostly low-risk — the hard part is seeing the problem.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Cloud Postgres bills separate storage, I/O, and compute into visible line items, but nothing on the invoice tells a team that half a table’s storage is dead tuples, or that six indexes have not served a single query in a month. Bloat and unused indexes are database hygiene problems that were always true; on a cloud bill they become cost problems too.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s MVCC model creates dead tuples on every update and delete. Autovacuum reclaims them for reuse, but under heavy churn — or with mistuned autovacuum — dead space accumulates faster than it’s reclaimed. Tables and indexes grow beyond the live data they hold. Separately, indexes added years ago for queries that no longer run keep costing write overhead and storage. Neither shows up as a “cost” problem until you go looking:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Storage&lt;/strong&gt; on cloud Postgres (and Aurora) is billed on what’s allocated/used; bloat inflates it permanently — Aurora storage doesn’t even shrink.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Write amplification:&lt;/strong&gt; every &lt;code&gt;INSERT&lt;/code&gt;/&lt;code&gt;UPDATE&lt;/code&gt; maintains &lt;em&gt;every&lt;/em&gt; index on the table. Unused indexes tax every write with zero read benefit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I/O:&lt;/strong&gt; bloated tables mean more pages scanned for the same rows — more I/O, which on Aurora is a direct charge and everywhere is latency.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These are small per-row and large in aggregate — the classic shape of a cost that hides until measured. The question that matters: is anyone actually measuring bloat and index usage, or is the bill just absorbing it?&lt;/p&gt;
&lt;h2 id=&quot;finding-the-waste&quot;&gt;Finding the Waste&lt;/h2&gt;
&lt;p&gt;Bloat and unused indexes share the same root causes, and the same read-only diagnostic path finds both.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[High-churn tables] --&gt; B[Dead tuples outpace autovacuum]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[Long-running transactions] --&gt; B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; D[Table and index bloat]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E[Indexes from retired queries] --&gt; F[Zero-scan indexes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Write overhead with no read benefit]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; H[Review checklist]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[Prioritized remediation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Technical root causes worth checking before anything else:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;High-churn tables (queues, counters, soft-deletes) outpacing autovacuum defaults.&lt;/li&gt;
&lt;li&gt;Long-running transactions holding back the xmin horizon so vacuum can’t reclaim.&lt;/li&gt;
&lt;li&gt;Indexes created for one-off queries, dashboards, or ORMs and never removed.&lt;/li&gt;
&lt;li&gt;Duplicate or redundant indexes (e.g. an index that’s a prefix of another).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Review checklist (read-only):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Which tables and indexes have the highest estimated bloat?&lt;/li&gt;
&lt;li&gt;Is autovacuum keeping up, or are dead tuples climbing on hot tables?&lt;/li&gt;
&lt;li&gt;Are there long-running transactions blocking vacuum?&lt;/li&gt;
&lt;li&gt;Which indexes have zero or near-zero scans in &lt;code&gt;pg_stat_user_indexes&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;Any duplicate/redundant indexes?&lt;/li&gt;
&lt;li&gt;What’s the storage trend, and how much is reclaimable?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;(Illustrative — the pattern these reviews repeatedly surface, not a specific client.)&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Four high-churn tables carried significant estimated bloat; tuning autovacuum (lower scale factors, more workers) plus a maintenance-window repack reclaimed storage and cut scan I/O.&lt;/li&gt;
&lt;li&gt;Six indexes showed zero scans over a 30-day window while adding write overhead; dropping them (after confirming no rare/seasonal use) reduced write amplification and storage.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A note on safety: &lt;em&gt;finding&lt;/em&gt; all of this is read-only. &lt;em&gt;Applying&lt;/em&gt; it ranges from zero-risk (drop an index with zero scans) to needs-a-window (repack a large table). Sequence accordingly and validate in staging.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Wrong move&lt;/th&gt;&lt;th&gt;What happens in production&lt;/th&gt;&lt;th&gt;Better approach&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Drop an index after a short scan window&lt;/td&gt;&lt;td&gt;A seasonal or rare-but-critical query breaks&lt;/td&gt;&lt;td&gt;Confirm zero scans across a full 30+ day window before dropping&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;VACUUM FULL&lt;/code&gt; on a live hot table&lt;/td&gt;&lt;td&gt;Table is locked for the duration; outage risk&lt;/td&gt;&lt;td&gt;Prefer &lt;code&gt;pg_repack&lt;/code&gt; for an online reclaim, or schedule a maintenance window&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tune autovacuum globally&lt;/td&gt;&lt;td&gt;Aggressive settings on cold tables waste cycles; hot tables still lag&lt;/td&gt;&lt;td&gt;Tune per-table (&lt;code&gt;autovacuum_vacuum_scale_factor&lt;/code&gt;) based on churn&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Treat bloat as a one-time cleanup&lt;/td&gt;&lt;td&gt;Bloat and unused indexes silently return within months&lt;/td&gt;&lt;td&gt;Add bloat and index-usage checks to a recurring (monthly) hygiene routine&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Table and index bloat, plus indexes nobody queries, quietly inflate cloud storage, I/O, and write cost without ever appearing as a line item.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Measure bloat and index-scan counts with read-only queries, tune autovacuum per hot table, reclaim bloat with &lt;code&gt;pg_repack&lt;/code&gt; before considering &lt;code&gt;VACUUM FULL&lt;/code&gt;, and drop confirmed-unused indexes after a long-enough observation window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Storage growth flattens or reverses, write latency on high-churn tables improves, and the top sequential-scan queries convert to index scans after remediation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, run bloat estimation and pull &lt;code&gt;pg_stat_user_indexes&lt;/code&gt; scan counts over the longest window you have, and flag anything with zero scans for a 30-day confirmation before dropping it.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cost</category><category>checklist</category></item><item><title>Decoupling Analytical Workloads: Active Data Guard and the Death of ORA-01555</title><link>https://rajivonai.com/blog/2024-07-10-oracle-undo-read-consistency-ora-01555/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-07-10-oracle-undo-read-consistency-ora-01555/</guid><description>Why expanding the Undo tablespace is a temporary patch for snapshot too old errors, and how to build a production proxy routing mesh to offload analytical queries to Active Data Guard standbys.</description><pubDate>Tue, 09 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Encountering an ORA-01555 error is not a database sizing deficiency; it is an architectural indicator that high-velocity transactional writes and long-running analytical queries are improperly co-located on the same physical infrastructure.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Running long-running analytical scans on an active OLTP primary forces Oracle’s circular Undo tablespace to overwrite historical row pre-images before readers finish (&lt;code&gt;ORA-01555&lt;/code&gt;) while flooding the shared buffer cache with cold scan blocks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Physically decouple reads and writes by routing analytical and reporting queries through CMAN-TDM or application-tier read/write pools to an Oracle Active Data Guard physical standby (&lt;code&gt;READ ONLY WITH APPLY&lt;/code&gt;) with &lt;code&gt;TEMP_UNDO_ENABLED = TRUE&lt;/code&gt; and &lt;code&gt;STANDBY_MAX_DATA_DELAY&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Standby offloading introduces apply-lag staleness (&lt;code&gt;ORA-03172&lt;/code&gt;), primary DDL invalidation conflicts, and Active Data Guard licensing requirements.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2024-07-10-oracle-undo-read-consistency-ora-01555.svg&quot; alt=&quot;Comparison between co-located OLTP and analytics on primary versus Active Data Guard read-write split&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;High-throughput Online Transaction Processing (OLTP) databases on Oracle must serve thousands of concurrent point inserts, updates, and deletes while guaranteeing statement-level and transaction-level read consistency without blocking readers. Oracle implements this guarantee through Multi-Version Concurrency Control (MVCC).&lt;/p&gt;
&lt;p&gt;When a &lt;code&gt;SELECT&lt;/code&gt; statement begins at a specific System Change Number (&lt;code&gt;SCN_0&lt;/code&gt;), Oracle guarantees that every block read by that query reflects data committed as of &lt;code&gt;SCN_0&lt;/code&gt;. If a concurrent OLTP transaction modifies a row in a block at &lt;code&gt;SCN_1 &gt; SCN_0&lt;/code&gt;, the kernel writes the pre-modification image into an Undo segment, records the transaction entry in the block’s Interested Transaction List (ITL), and updates the block in place. When the reader later visits that block, it compares the block header SCN against &lt;code&gt;SCN_0&lt;/code&gt;, reads the ITL pointer, fetches the historical undo record, and constructs a Consistent Read (CR) clone in the buffer cache.&lt;/p&gt;
&lt;p&gt;For millisecond-duration OLTP lookups, those undo records remain intact for the lifetime of the query. The architecture holds until reporting jobs, BI extracts, and ad-hoc analytical queries are pointed at the same primary database instance.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Co-locating multi-minute or multi-hour analytical scans with continuous OLTP DML breaks two shared subsystems on the primary instance: the circular Undo tablespace and the System Global Area (SGA) buffer cache.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-07-10-oracle-undo-read-consistency-ora-01555-2-constraints.svg&quot; alt=&quot;Four-step MVCC undo wrap-around and delayed block cleanout failure sequence leading to ORA-01555&quot;&gt;&lt;/p&gt;
&lt;p&gt;Four physical constraints explain why Undo tuning alone cannot resolve the conflict:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Circular Undo Wrap-Around (&lt;code&gt;ORA-01555&lt;/code&gt;):&lt;/strong&gt; Oracle manages the Undo tablespace as a ring of extents. Once active transactions finish and &lt;code&gt;UNDO_RETENTION&lt;/code&gt; expires—or earlier if free space is exhausted under autoextend limits—Oracle reuses those extents for new DML. A simple rate check illustrates the sizing bound: if peak OLTP generates &lt;code&gt;50 MB/s&lt;/code&gt; of undo and a batch report runs for &lt;code&gt;7,200&lt;/code&gt; seconds (2 hours), the primary must retain at least &lt;code&gt;50 MB/s * 7,200 s = 360,000 MB&lt;/code&gt; (~351.6 GB) of historical undo &lt;em&gt;on top of&lt;/em&gt; active uncommitted undo and concurrent DML headroom. If a single unindexed analytical job stretches to 4 hours during a traffic spike, required undo doubles to over 700 GB; once any needed extent is overwritten, the query aborts with &lt;code&gt;ORA-01555: snapshot too old&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Delayed Block Cleanout:&lt;/strong&gt; When a large OLTP batch modifies many data blocks and commits, Oracle updates the transaction table in the undo segment header without immediately revisiting every modified data block to clear ITL lock bytes. A later analytical scan that touches those blocks must consult the undo segment’s transaction table to verify the commit SCN. If subsequent OLTP commits have already wrapped around and overwritten that transaction table slot, the analytical query can fail with &lt;code&gt;ORA-01555&lt;/code&gt; almost immediately—even if zero DML runs while the query is executing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Buffer Cache Eviction:&lt;/strong&gt; Expanding &lt;code&gt;UNDOTBS1&lt;/code&gt; does nothing to protect the SGA buffer cache. Full-table and fast-full-index scans stream gigabytes of cold historical blocks through the buffer cache, evicting hot OLTP index branch blocks and active customer working sets. OLTP sessions that previously hit memory must suddenly issue physical disk reads, degrading transactional tail latency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CPU and I/O Contention:&lt;/strong&gt; Parallel analytical queries (&lt;code&gt;PQ&lt;/code&gt;) compete directly with OLTP foreground sessions for CPU run-queue slots and storage IOPS, turning a reporting spike into a customer-facing checkout slowdown.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;How do platform architects eliminate &lt;code&gt;ORA-01555&lt;/code&gt; errors permanently while insulating primary OLTP buffer caches and CPU capacity from heavy analytical scans?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;When &lt;code&gt;ORA-01555&lt;/code&gt; alerts appear in the alert log alongside rising OLTP &lt;code&gt;db file sequential read&lt;/code&gt; waits, teams typically evaluate four options.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-07-10-oracle-undo-read-consistency-ora-01555-3-options.svg&quot; alt=&quot;Comparison of four options for resolving ORA-01555 and analytical contention on an OLTP primary&quot;&gt;&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Solves&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Expand Undo + &lt;code&gt;UNDO_RETENTION&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Add terabytes of storage to &lt;code&gt;UNDOTBS&lt;/code&gt; and raise &lt;code&gt;UNDO_RETENTION&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Extends the time window before expired undo extents are overwritten&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Does not stop SGA buffer cache eviction, CPU contention, or delayed block cleanout wrap-around&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt; as standalone fix&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;RETENTION GUARANTEE&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Force Oracle never to steal unexpired undo extents (&lt;code&gt;ALTER TABLESPACE ... RETENTION GUARANTEE&lt;/code&gt;)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Prevents &lt;code&gt;ORA-01555&lt;/code&gt; from premature extent stealing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;When undo fills, OLTP &lt;code&gt;INSERT&lt;/code&gt;/&lt;code&gt;UPDATE&lt;/code&gt;/&lt;code&gt;DELETE&lt;/code&gt; statements fail with &lt;code&gt;ORA-30036&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt;; unacceptable OLTP blast radius&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Batch ETL / Data Pump Offload&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Export snapshots periodically to a separate data warehouse&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Isolates long-running BI from primary SGA and undo&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Introduces hours of data staleness; export jobs themselves still read the primary&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Deferred&lt;/strong&gt; for near-real-time operational reporting&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Active Data Guard Read-Write Split&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Offload read-only queries to a physical standby in &lt;code&gt;READ ONLY WITH APPLY&lt;/code&gt; mode&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Isolates SGA, CPU, IOPS, and Temporary Undo while keeping low replication lag&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires Active Data Guard licensing and explicit lag/DDL governance&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen&lt;/strong&gt; for operational analytics and reporting&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Enabling &lt;code&gt;RETENTION GUARANTEE&lt;/code&gt; on the primary Undo tablespace is particularly dangerous: it inverses the failure mode so that a runaway reporting query causes production OLTP writes to fail with &lt;code&gt;ORA-30036: unable to extend segment by ... in undo tablespace&lt;/code&gt;. Batch ETL remains appropriate for multi-year warehouse aggregations, but operational reporting and real-time dashboards need fresher data without touching the primary SGA. Active Data Guard solves both problems simultaneously by replaying redo onto independent compute and memory.&lt;/p&gt;
&lt;h2 id=&quot;the-active-data-guard-read-write-decoupling-architecture&quot;&gt;The Active Data Guard Read-Write Decoupling Architecture&lt;/h2&gt;
&lt;p&gt;The recommended architecture enforces physical compute, memory, and undo isolation between transactional writes and analytical reads. The primary database handles only OLTP DML and latency-sensitive point lookups. Redo streams continuously via Data Guard transport (&lt;code&gt;NSS&lt;/code&gt;/&lt;code&gt;RFS&lt;/code&gt; processes) to one or more Active Data Guard physical standbys running in &lt;code&gt;READ ONLY WITH APPLY&lt;/code&gt; mode, where the Managed Recovery Process (&lt;code&gt;MRP&lt;/code&gt;) applies changes in real time.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-07-10-oracle-undo-read-consistency-ora-01555-4-architecture.svg&quot; alt=&quot;Recommended architecture showing client pools, CMAN-TDM, role-pinned services, and Active Data Guard standby&quot;&gt;&lt;/p&gt;
&lt;p&gt;Three architectural mechanisms make the split transparent and safe for applications:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Role-Pinned Database Services:&lt;/strong&gt; Using &lt;code&gt;srvctl&lt;/code&gt;, administrators define &lt;code&gt;svc_oltp_primary&lt;/code&gt; with &lt;code&gt;-role PRIMARY&lt;/code&gt; and &lt;code&gt;svc_analytics_ro&lt;/code&gt; with &lt;code&gt;-role PHYSICAL_STANDBY&lt;/code&gt;. Clusterware starts each service only on the database currently holding that role, so a Data Guard switchover or failover automatically moves services to the right hosts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standby Temporary Undo (&lt;code&gt;TEMP_UNDO_ENABLED = TRUE&lt;/code&gt;):&lt;/strong&gt; Analytical queries frequently materialize global temporary tables or spill hash joins and sorts. Setting &lt;code&gt;TEMP_UNDO_ENABLED = TRUE&lt;/code&gt; on the standby redirects temporary-table undo into the standby’s local temporary tablespace rather than generating undo or redo dependencies against the primary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Freshness Bounds and Proxy Routing:&lt;/strong&gt; Applications route traffic either through Oracle Connection Manager in Traffic Director Mode (&lt;code&gt;CMAN-TDM&lt;/code&gt;) or through an application-tier routing &lt;code&gt;DataSource&lt;/code&gt; (for example, Spring’s &lt;code&gt;AbstractRoutingDataSource&lt;/code&gt; inspecting &lt;code&gt;@Transactional(readOnly = true)&lt;/code&gt;). Sessions on the standby enforce &lt;code&gt;STANDBY_MAX_DATA_DELAY&lt;/code&gt; so queries abort cleanly with &lt;code&gt;ORA-03172&lt;/code&gt; rather than serving stale data beyond SLA. When geographical separation between the primary and reporting regions adds network round-trip latency, an Oracle Far Sync instance placed near the primary receives synchronous redo locally and ships asynchronously to the remote Active Data Guard standby, keeping primary commit latency isolated from WAN distance.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;1-role-pinned-services-and-standby-configuration&quot;&gt;1. Role-Pinned Services and Standby Configuration&lt;/h3&gt;
&lt;p&gt;Define the role-aware services in Clusterware and configure standby read consistency controls:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Primary OLTP service (runs only when database is in PRIMARY role)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; service&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -db&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODDB&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -service&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; svc_oltp_primary&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -role&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRIMARY&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -notification&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; TRUE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failovertype&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; TRANSACTION&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -commit_outcome&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; TRUE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Analytical read-only service (runs only when database is PHYSICAL_STANDBY)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; service&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -db&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODDB&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -service&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; svc_analytics_ro&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -role&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PHYSICAL_STANDBY&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -notification&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; TRUE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -clbgoal&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; LONG&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -rlbgoal&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; THROUGHPUT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; start&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; service&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -db&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODDB&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -service&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; svc_oltp_primary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On the Active Data Guard standby, ensure Standby Redo Logs (SRLs) are configured at the same size as the primary online redo logs with at least one more group per primary thread (&lt;code&gt;online_groups + 1&lt;/code&gt;) so real-time apply never stalls waiting for log archival. Then enable local temporary undo and open the database in &lt;code&gt;READ ONLY WITH APPLY&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Isolate temporary table undo on the standby&apos;s local TEMP tablespace&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SYSTEM&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; TEMP_UNDO_ENABLED &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; TRUE SCOPE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=BOTH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Open standby read-only and start Real-Time Apply&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DATABASE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; OPEN&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; READ&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ONLY;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; RECOVER MANAGED &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;STANDBY&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; DISCONNECT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SESSION&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Enforce session-level lag SLA (e.g., via logon trigger for svc_analytics_ro)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Raises ORA-03172 if apply lag exceeds 15 seconds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SESSION&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; STANDBY_MAX_DATA_DELAY &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-oracle-connection-manager-cmanora-traffic-director-mode&quot;&gt;2. Oracle Connection Manager (&lt;code&gt;cman.ora&lt;/code&gt;) Traffic Director Mode&lt;/h3&gt;
&lt;p&gt;Configure &lt;code&gt;CMAN-TDM&lt;/code&gt; (&lt;code&gt;TDM = YES&lt;/code&gt;) to proxy and filter client connections by target service name and enforce session timeouts:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;cman_router =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  (CONFIGURATION =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    (ADDRESS = (PROTOCOL = TCP)(HOST = cman-proxy.corp.internal)(PORT = 1521))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    (PARAMETER_LIST =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      (TDM = YES)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      (MAX_GATEWAY_PROCESSES = 16)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      (MIN_GATEWAY_PROCESSES = 4)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    (RULE_LIST =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      (RULE =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        (SRC = 10.0.0.0/8)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        (DST = *)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        (SRV = svc_analytics_ro)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        (ACT = accept)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        (ACTION_LIST = (SESSION_TIMEOUT = 14400))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      (RULE =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        (SRC = 10.0.0.0/8)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        (DST = *)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        (SRV = svc_oltp_primary)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        (ACT = accept)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        (ACTION_LIST = (SESSION_TIMEOUT = 3600))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-application-tier-read-write-splitting-spring--hikaricp&quot;&gt;3. Application-Tier Read-Write Splitting (Spring / HikariCP)&lt;/h3&gt;
&lt;p&gt;For microservices using Spring transaction boundaries, route &lt;code&gt;@Transactional(readOnly = true)&lt;/code&gt; methods to the standby pool while defaulting all write and read-your-writes paths to the primary pool:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; TransactionRoutingDataSource&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; AbstractRoutingDataSource&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    @&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;Override&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    protected&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Object &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;determineCurrentLookupKey&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        boolean&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; isReadOnly &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; TransactionSynchronizationManager.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;isCurrentTransactionReadOnly&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; isReadOnly &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; DataSourceType.READ_ONLY_STANDBY &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; DataSourceType.READ_WRITE_PRIMARY;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ensure that &lt;code&gt;TransactionRoutingDataSource&lt;/code&gt; wraps a &lt;code&gt;LazyConnectionDataSourceProxy&lt;/code&gt; in Spring so the transaction’s &lt;code&gt;readOnly&lt;/code&gt; flag is evaluated &lt;em&gt;before&lt;/em&gt; a physical JDBC connection is borrowed from HikariCP.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Roll out read-write decoupling in four gated phases so licensing, redo transport, and switchover behavior are verified before production BI traffic moves.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-07-10-oracle-undo-read-consistency-ora-01555-5-flow.svg&quot; alt=&quot;Four-phase rollout from standby preparation and role services to routing SLA validation and report cutover&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Standby Preparation and Licensing Verification:&lt;/strong&gt; Confirm Active Data Guard licensing entitlement, verify Standby Redo Logs (SRLs) match or exceed primary online redo log groups, set &lt;code&gt;TEMP_UNDO_ENABLED = TRUE&lt;/code&gt;, and open the standby in &lt;code&gt;READ ONLY WITH APPLY&lt;/code&gt;.
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Validation gate:&lt;/em&gt; Query &lt;code&gt;V$DATABASE&lt;/code&gt; (&lt;code&gt;OPEN_MODE = &apos;READ ONLY WITH APPLY&apos;&lt;/code&gt;) and &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; (&lt;code&gt;apply lag&lt;/code&gt; within target SLA).&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Rollback:&lt;/em&gt; Cancel MRP, reopen in &lt;code&gt;MOUNT&lt;/code&gt; state, and restart MRP without real-time query if licensing or stability issues appear.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Role-Pinned Service Creation:&lt;/strong&gt; Create &lt;code&gt;svc_oltp_primary&lt;/code&gt; (&lt;code&gt;-role PRIMARY&lt;/code&gt;) and &lt;code&gt;svc_analytics_ro&lt;/code&gt; (&lt;code&gt;-role PHYSICAL_STANDBY&lt;/code&gt;) via &lt;code&gt;srvctl&lt;/code&gt; and test a planned Data Guard switchover in staging.
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Validation gate:&lt;/em&gt; Confirm services start automatically on the correct post-switchover hosts without manual intervention.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Rollback:&lt;/em&gt; Stop and remove the new services via &lt;code&gt;srvctl&lt;/code&gt;; existing connections continue using legacy services.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Proxy Routing and Lag SLA Enforcement:&lt;/strong&gt; Deploy &lt;code&gt;CMAN-TDM&lt;/code&gt; behind a load balancer and enable &lt;code&gt;STANDBY_MAX_DATA_DELAY&lt;/code&gt; in reporting sessions (or a logon trigger scoped to &lt;code&gt;svc_analytics_ro&lt;/code&gt;). Test application fallback behavior when &lt;code&gt;ORA-03172&lt;/code&gt; is raised.
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Validation gate:&lt;/em&gt; Simulate standby apply lag in staging and confirm &lt;code&gt;ORA-03172&lt;/code&gt; triggers either controlled retry or primary fallback for critical reads.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Rollback:&lt;/em&gt; Point client TNS aliases directly to the primary SCAN listener and bypass &lt;code&gt;CMAN-TDM&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Analytical Workload Cutover:&lt;/strong&gt; Migrate BI dashboards, scheduled reports, and long-running ad-hoc SQL clients from the primary service to &lt;code&gt;svc_analytics_ro&lt;/code&gt;, starting with the longest-running queries first.
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Validation gate:&lt;/em&gt; Confirm &lt;code&gt;ORA-01555&lt;/code&gt; errors cease on the primary alert log and primary buffer cache hit ratios stabilize without full-table-scan spikes.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Rollback:&lt;/em&gt; Revert the BI tool’s service name back to the primary service while keeping &lt;code&gt;TEMP_UNDO_ENABLED&lt;/code&gt; intact.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Based on how Oracle Data Guard, MVCC, and &lt;code&gt;CMAN-TDM&lt;/code&gt; operate in production:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Why standbys avoid primary undo wrap-around:&lt;/strong&gt; On an Active Data Guard standby, &lt;code&gt;MRP&lt;/code&gt; applies redo sequentially from the primary, and read-only sessions reconstruct consistent views using the standby’s own SCN progression without competing against local OLTP &lt;code&gt;INSERT&lt;/code&gt;/&lt;code&gt;UPDATE&lt;/code&gt;/&lt;code&gt;DELETE&lt;/code&gt; undo allocation. Enabling &lt;code&gt;TEMP_UNDO_ENABLED = TRUE&lt;/code&gt; ensures that sorting, hash-join spills, and global temporary table writes on the standby stay inside local temporary segments.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bounding stale reads with &lt;code&gt;STANDBY_MAX_DATA_DELAY&lt;/code&gt;:&lt;/strong&gt; When a session sets &lt;code&gt;ALTER SESSION SET STANDBY_MAX_DATA_DELAY = 15&lt;/code&gt;, Oracle checks &lt;code&gt;apply lag&lt;/code&gt; before executing each query. If lag exceeds 15 seconds, the query fails immediately with &lt;code&gt;ORA-03172: STANDBY_MAX_DATA_DELAY of 15 seconds exceeded&lt;/code&gt; rather than silently returning stale balances. Applications that require strict read-your-writes immediately after a mutation must still pin post-write reads to &lt;code&gt;svc_oltp_primary&lt;/code&gt; rather than relying on an asynchronous standby.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Handling stray writes on a read-only standby:&lt;/strong&gt; Any &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, or &lt;code&gt;DELETE&lt;/code&gt; accidentally routed to &lt;code&gt;svc_analytics_ro&lt;/code&gt; fails with &lt;code&gt;ORA-16000: database or pluggable database open for read-only access&lt;/code&gt;. Starting in Oracle 19c, setting &lt;code&gt;ADG_REDIRECT_DML = TRUE&lt;/code&gt; transparently tunnels occasional DML statements back to the primary over an internal link and waits for redo apply before returning—useful for legacy reporting tools that write small audit rows, though unsuitable for heavy OLTP writes (and unsupported inside XA transactions).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Monitoring redo transport and lag:&lt;/strong&gt; Query &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; on the standby for &lt;code&gt;transport lag&lt;/code&gt; and &lt;code&gt;apply lag&lt;/code&gt;, and query &lt;code&gt;V$REDO_DEST_RESP_HISTOGRAM&lt;/code&gt; on the primary alongside transport wait events (&lt;code&gt;LNS wait on SENDREQ&lt;/code&gt; in older releases, &lt;code&gt;NSS&lt;/code&gt; sync / &lt;code&gt;TTnn&lt;/code&gt; async transport waits in 12c+) to catch network bottlenecks early.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Decoupling reads onto a physical standby shifts failure modes from local undo exhaustion to replication lag, DDL coordination, and proxy availability.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-07-10-oracle-undo-read-consistency-ora-01555-6-failures.svg&quot; alt=&quot;Three failure paths of standby read-write splitting: apply lag staleness, primary DDL conflicts, and stray DML&quot;&gt;&lt;/p&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks how&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Standby apply lag spike&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Burst primary DML or slow standby I/O delays &lt;code&gt;MRP&lt;/code&gt;; reads return stale data or abort with &lt;code&gt;ORA-03172&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Query &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; (&lt;code&gt;apply lag&lt;/code&gt;) and alert on &lt;code&gt;ORA-03172&lt;/code&gt; spikes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set &lt;code&gt;STANDBY_MAX_DATA_DELAY&lt;/code&gt; per SLA; route read-your-writes sessions to &lt;code&gt;svc_oltp_primary&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Primary partition &lt;code&gt;DROP&lt;/code&gt; or &lt;code&gt;TRUNCATE&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Destructive partition DDL invalidates segments currently scanned by a standby query, raising &lt;code&gt;ORA-01555&lt;/code&gt; or &lt;code&gt;ORA-08103&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Alert on &lt;code&gt;ORA-01555&lt;/code&gt; / &lt;code&gt;ORA-08103&lt;/code&gt; on the standby during maintenance windows.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Schedule partition drops outside peak BI windows; use &lt;code&gt;ALTER SESSION SET DDL_LOCK_TIMEOUT&lt;/code&gt; on primary.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unlicensed Active Data Guard usage&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Opening a physical standby &lt;code&gt;READ ONLY&lt;/code&gt; while &lt;code&gt;MRP&lt;/code&gt; is active triggers ADG license feature tracking.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Audit &lt;code&gt;DBA_FEATURE_USAGE_STATISTICS&lt;/code&gt; for &lt;code&gt;Active Data Guard&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Verify ADG license entitlements before opening any standby in &lt;code&gt;READ ONLY WITH APPLY&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Standby SGA or &lt;code&gt;TEMP&lt;/code&gt; exhaustion&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Concurrent multi-pass hash joins or huge table scans thrash standby SGA and fill &lt;code&gt;TEMP&lt;/code&gt; (&lt;code&gt;ORA-01652&lt;/code&gt;).&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Monitor &lt;code&gt;ORA-01652&lt;/code&gt; and standby &lt;code&gt;V$TEMPSEG_USAGE&lt;/code&gt; / AWR wait events.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce Database Resource Manager (DBRM) consumer groups and size standby &lt;code&gt;TEMP&lt;/code&gt; for peak concurrency.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Single &lt;code&gt;CMAN-TDM&lt;/code&gt; proxy outage&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;A single proxy VM failure drops all routed primary and standby client connections simultaneously.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Health-check failures on port &lt;code&gt;1521&lt;/code&gt; at the proxy tier.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Deploy &lt;code&gt;CMAN-TDM&lt;/code&gt; in an active-active pair behind a network load balancer with health probes.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Stray DML routed to standby&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Misconfigured application pool sends &lt;code&gt;INSERT&lt;/code&gt;/&lt;code&gt;UPDATE&lt;/code&gt; to &lt;code&gt;svc_analytics_ro&lt;/code&gt; and hits &lt;code&gt;ORA-16000&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Application error logs showing &lt;code&gt;ORA-16000&lt;/code&gt; on read-only pools.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fix &lt;code&gt;@Transactional(readOnly = true)&lt;/code&gt; boundaries; enable &lt;code&gt;ADG_REDIRECT_DML = TRUE&lt;/code&gt; only for occasional audit writes.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Redo transport congestion&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Network packet loss or small &lt;code&gt;DEFAULT_SDU_SIZE&lt;/code&gt; stalls &lt;code&gt;SYNC&lt;/code&gt; commits (&lt;code&gt;log file sync&lt;/code&gt;) or widens &lt;code&gt;ASYNC&lt;/code&gt; lag.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Monitor &lt;code&gt;V$REDO_DEST_RESP_HISTOGRAM&lt;/code&gt; and &lt;code&gt;NSS&lt;/code&gt;/&lt;code&gt;LNS wait on SENDREQ&lt;/code&gt; events.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Separate redo transport onto dedicated NICs, tune &lt;code&gt;DEFAULT_SDU_SIZE=32767&lt;/code&gt;, and place SRLs on fast storage.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Undo sizing cannot fix cache pollution:&lt;/strong&gt; Expanding &lt;code&gt;UNDOTBS&lt;/code&gt; and raising &lt;code&gt;UNDO_RETENTION&lt;/code&gt; only delays &lt;code&gt;ORA-01555&lt;/code&gt; wrap-around; it never stops analytical full-table scans from evicting hot OLTP blocks from the primary SGA.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never use &lt;code&gt;RETENTION GUARANTEE&lt;/code&gt; on an OLTP primary:&lt;/strong&gt; Guaranteeing undo retention for long-running queries converts reporting failures into &lt;code&gt;ORA-30036&lt;/code&gt; write outages for customer-facing OLTP transactions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Role-pinned services keep Data Guard switchovers automatic:&lt;/strong&gt; Binding &lt;code&gt;svc_oltp_primary&lt;/code&gt; to &lt;code&gt;PRIMARY&lt;/code&gt; and &lt;code&gt;svc_analytics_ro&lt;/code&gt; to &lt;code&gt;PHYSICAL_STANDBY&lt;/code&gt; via &lt;code&gt;srvctl&lt;/code&gt; ensures traffic follows the active role without manual TNS edits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Enforce freshness explicitly with &lt;code&gt;STANDBY_MAX_DATA_DELAY&lt;/code&gt;:&lt;/strong&gt; Rather than letting dashboards silently read lagging data, bound staleness at the session level (&lt;code&gt;ORA-03172&lt;/code&gt;) and pin read-your-writes sessions to the primary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Verify licensing and DDL windows before cutover:&lt;/strong&gt; Active Data Guard requires explicit licensing for &lt;code&gt;READ ONLY WITH APPLY&lt;/code&gt;, and primary &lt;code&gt;DROP&lt;/code&gt;/&lt;code&gt;TRUNCATE PARTITION&lt;/code&gt; operations must still be coordinated around active standby scans.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Co-locating long-running analytical queries with high-velocity OLTP writes on a single Oracle primary exhausts circular undo (&lt;code&gt;ORA-01555&lt;/code&gt;) and evicts hot OLTP buffers from the SGA.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Offload analytical and reporting workloads to an Active Data Guard physical standby using role-pinned &lt;code&gt;srvctl&lt;/code&gt; services, &lt;code&gt;TEMP_UNDO_ENABLED = TRUE&lt;/code&gt;, &lt;code&gt;STANDBY_MAX_DATA_DELAY&lt;/code&gt;, and &lt;code&gt;CMAN-TDM&lt;/code&gt; or application-tier read/write routing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Primary &lt;code&gt;ORA-01555&lt;/code&gt; alerts drop to zero, primary buffer cache hit ratios stabilize without full-scan eviction, and &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; confirms &lt;code&gt;apply lag&lt;/code&gt; stays within your &lt;code&gt;STANDBY_MAX_DATA_DELAY&lt;/code&gt; threshold.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your primary AWR “SQL ordered by Elapsed Time” report this week, verify Active Data Guard licensing on your existing physical standby, and migrate your top long-running reporting queries onto a &lt;code&gt;PHYSICAL_STANDBY&lt;/code&gt; service.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>DBaaS Fleet Telemetry: Distributed Pre-Flight Health Checks at Scale</title><link>https://rajivonai.com/blog/2024-05-20-automated-oracle-patch-validation-python/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-05-20-automated-oracle-patch-validation-python/</guid><description>Why centralized SSH and SQL polling collapses when managing thousands of database instances, and how to build a push-based sidecar telemetry plane using Go, Kafka, and Temporal to govern automated patching.</description><pubDate>Mon, 08 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Centralized polling architectures are an operational hazard at cloud scale; attempting to inspect thousands of databases via inbound SSH or SQL*Net connections turns your patch control plane into a self-inflicted denial-of-service attack.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Fanning out inbound SSH or SQL*Net pre-flight checks across 10,000+ Oracle instances exhausts firewall &lt;code&gt;nf_conntrack&lt;/code&gt; tables, centralizes &lt;code&gt;SYSDBA&lt;/code&gt; credentials, and acts on stale telemetry by the time a sweep finishes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Move pre-flight evaluation to a cgroup-capped Go sidecar on each host that authenticates locally via Oracle Bequeath (&lt;code&gt;PROTOCOL=beq&lt;/code&gt;), pushes a low-cardinality &lt;code&gt;fleet_db_patch_ready&lt;/code&gt; state vector outbound, and gates Temporal patch waves with a fail-closed PromQL &lt;code&gt;timestamp()&lt;/code&gt; query.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; &lt;code&gt;godror&lt;/code&gt; rejects &lt;code&gt;&quot;sys/ as sysdba&quot;&lt;/code&gt; (use an explicit Bequeath descriptor or &lt;code&gt;externalAuth=1&lt;/code&gt;), PromQL staleness requires &lt;code&gt;timestamp()&lt;/code&gt;, and high-cardinality labels will still blow up the TSDB if you emit per-SQL dimensions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2024-05-20-automated-oracle-patch-validation-python.svg&quot; alt=&quot;Push-based pre-flight telemetry pipeline from local Bequeath IPC to Temporal gate&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Platform engineering teams operating Database-as-a-Service (DBaaS) fleets across hybrid and multi-cloud estates run continuous lifecycle workflows: quarterly Oracle Database Release Updates (RUs), Linux kernel patches, security baseline enforcement, and storage migrations across 5,000 to 20,000 database instances.&lt;/p&gt;
&lt;p&gt;Before any destructive step—stopping an instance, switching &lt;code&gt;$ORACLE_HOME&lt;/code&gt;, or running &lt;code&gt;datapatch&lt;/code&gt; against the data dictionary—automated patch orchestrators enforce &lt;strong&gt;pre-flight health invariants&lt;/strong&gt;. On Oracle Database 19c and 23ai, four checks separate a clean rolling patch from a stalled maintenance window:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Dictionary Integrity:&lt;/strong&gt; Zero unexpected &lt;code&gt;INVALID&lt;/code&gt; objects in core administrative schemas (&lt;code&gt;SYS&lt;/code&gt;, &lt;code&gt;SYSTEM&lt;/code&gt;, &lt;code&gt;MDSYS&lt;/code&gt;, &lt;code&gt;CTXSYS&lt;/code&gt;) in &lt;code&gt;DBA_OBJECTS&lt;/code&gt;, and all components in &lt;code&gt;DBA_REGISTRY&lt;/code&gt; reporting &lt;code&gt;VALID&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Storage Capacity:&lt;/strong&gt; &lt;code&gt;SYSTEM&lt;/code&gt;, &lt;code&gt;SYSAUX&lt;/code&gt;, and active &lt;code&gt;UNDO&lt;/code&gt; tablespaces retain at least 25% free headroom in &lt;code&gt;DBA_TABLESPACE_USAGE_METRICS&lt;/code&gt; (which accounts for &lt;code&gt;AUTOEXTEND&lt;/code&gt; max size in database blocks), and the Fast Recovery Area (&lt;code&gt;V$RECOVERY_FILE_DEST&lt;/code&gt;) sits below 80% utilization to avoid archiver hangs (&lt;code&gt;ORA-00257&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replication Health:&lt;/strong&gt; Physical standbys report transport and apply lag under 30 seconds in &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; so emergency switchover remains viable if a primary fails to restart cleanly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Workload Quiescence:&lt;/strong&gt; No long-running uncommitted transactions (&lt;code&gt;V$TRANSACTION&lt;/code&gt;), active RMAN backups (&lt;code&gt;V$RMAN_STATUS&lt;/code&gt;), or critical scheduler jobs (&lt;code&gt;DBA_SCHEDULER_RUNNING_JOBS&lt;/code&gt;) are mid-flight before &lt;code&gt;SHUTDOWN IMMEDIATE&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Historically, teams implemented these checks with centralized Ansible playbooks, Python Paramiko runners, or Jenkins pipelines opening inbound SSH and SQL*Net connections to every host in the fleet.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Centralized pull polling breaks against four concrete scale, security, and concurrency constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Sweep latency creates decision drift (derived math):&lt;/strong&gt; Suppose a centralized runner executes pre-flight checks with 50 concurrent workers, and each host requires 4 seconds for SSH/TLS handshake, &lt;code&gt;sqlplus&lt;/code&gt; or driver initialization, and dictionary queries. Across 10,000 databases, the minimum sweep time is &lt;code&gt;(10,000 / 50) * 4s = 800 seconds&lt;/code&gt; (13.3 minutes) under zero-failure conditions—and if 10% of hosts hit a 30-second SSH timeout, those 1,000 timeouts alone add &lt;code&gt;(1,000 / 50) * 30s = 600 seconds&lt;/code&gt;, pushing total sweep duration past 23 minutes. By the time wave execution starts, telemetry from the first hosts is already stale: a batch job that started 15 minutes into the sweep can fill &lt;code&gt;UNDO&lt;/code&gt; or leave a massive uncommitted transaction open right before &lt;code&gt;SHUTDOWN IMMEDIATE&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stateful firewall &lt;code&gt;nf_conntrack&lt;/code&gt; exhaustion:&lt;/strong&gt; Enterprise cloud firewalls and NAT gateways track every active TCP socket in kernel &lt;code&gt;nf_conntrack&lt;/code&gt; tables. Opening thousands of short-lived SSH and SQL*Net connections in parallel across VPCs consumes state-table entries that remain in &lt;code&gt;TIME_WAIT&lt;/code&gt; after sockets close, risking &lt;code&gt;nf_conntrack: table full, dropping packet&lt;/code&gt; and dropping production application traffic alongside monitoring probes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Centralized credential honeypot:&lt;/strong&gt; A central polling engine requires &lt;code&gt;SYSDBA&lt;/code&gt; passwords, wallet passphrases, or SSH private keys for every database in the fleet. That runner becomes a high-value breach target, and routine corporate credential rotation frequently desynchronizes stored secrets, causing false-negative pre-flight failures.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TSDB cardinality explosion on naive push:&lt;/strong&gt; Even if you switch to push metrics, emitting high-cardinality labels—such as SQL IDs, session IDs, or per-PDB object names—multiplies active series rapidly. Across 10,000 databases, emitting just 1,000 unique label combinations per host produces &lt;code&gt;10,000 * 1,000 = 10,000,000&lt;/code&gt; active time series, saturating Prometheus memory and slowing PromQL wave queries.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2024-05-20-automated-oracle-patch-validation-python-2-constraints.svg&quot; alt=&quot;Four scaling limits that break centralized SSH and SQL polling&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do you evaluate real-time pre-flight health across 10,000+ Oracle instances in sub-second orchestration queries without opening inbound SSH/SQL*Net ports or storing fleet-wide &lt;code&gt;SYSDBA&lt;/code&gt; credentials?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Architecture&lt;/th&gt;&lt;th&gt;Inbound Ports Required&lt;/th&gt;&lt;th&gt;Credential Scope&lt;/th&gt;&lt;th&gt;Fleet Evaluation Latency&lt;/th&gt;&lt;th&gt;Why Chosen / Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Centralized Ansible / Python Pull&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;SSH (22) + SQL*Net (1521/2484)&lt;/td&gt;&lt;td&gt;Global vault on central runner&lt;/td&gt;&lt;td&gt;Minutes to hours (worker-bound)&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Exhausts &lt;code&gt;nf_conntrack&lt;/code&gt;, creates a central credential honeypot, and acts on stale state.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Regional Bastion Fanout&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;SSH / SQL*Net per VPC&lt;/td&gt;&lt;td&gt;Sharded per-region secrets&lt;/td&gt;&lt;td&gt;Minutes per region&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Reduces cross-VPC firewall pressure but still requires inbound ports and distributed credential management.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Local Sidecar + Central Prometheus Scrape&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Inbound HTTP (9161) on every DB host&lt;/td&gt;&lt;td&gt;Local OS auth (no password)&lt;/td&gt;&lt;td&gt;Scrape interval (15–60s)&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; Eliminates DB passwords, but central Prometheus still opens 10,000+ inbound TCP scrapes across private subnets.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Local Bequeath Sidecar + Outbound Push&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;None (0 inbound ports)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Local OS group (&lt;code&gt;oinstall&lt;/code&gt;/&lt;code&gt;dba&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Sub-second PromQL against TSDB&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;Chosen:&lt;/strong&gt; Isolates credentials to local IPC, keeps DB subnets outbound-only, and decouples Temporal from host reachability.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2024-05-20-automated-oracle-patch-validation-python-3-options.svg&quot; alt=&quot;Comparison of four fleet pre-flight architectures&quot;&gt;&lt;/p&gt;
&lt;p&gt;Running &lt;strong&gt;regional bastions&lt;/strong&gt; is the most common intermediate step, because sharding workers into each VPC avoids traversing central transit firewalls. However, bastions do not eliminate inbound listener exposure or the operational burden of rotating &lt;code&gt;SYSDBA&lt;/code&gt; secrets across every regional runner. Similarly, exposing a Prometheus &lt;code&gt;/metrics&lt;/code&gt; endpoint on port &lt;code&gt;9161&lt;/code&gt; solves database authentication via local OS auth, but still forces security teams to permit inbound scraping from central monitoring CIDRs into every restricted database subnet. Inverting the transport to &lt;strong&gt;outbound remote-write or Kafka push&lt;/strong&gt; removes inbound firewall rules altogether.&lt;/p&gt;
&lt;h2 id=&quot;the-distributed-sidecar-telemetry-architecture&quot;&gt;The Distributed Sidecar Telemetry Architecture&lt;/h2&gt;
&lt;p&gt;The architecture separates telemetry collection, regional ingestion, and wave orchestration into three decoupled planes.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-05-20-automated-oracle-patch-validation-python-4-architecture.svg&quot; alt=&quot;Three decoupled planes separating collection, ingestion, and gating&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;plane-1-local-bequeath-sidecar-and-cgroup-sandbox&quot;&gt;Plane 1: Local Bequeath Sidecar and Cgroup Sandbox&lt;/h3&gt;
&lt;p&gt;A compiled Go daemon runs on each database host under the local &lt;code&gt;oracle&lt;/code&gt; OS user (&lt;code&gt;oinstall&lt;/code&gt; / &lt;code&gt;dba&lt;/code&gt; group). Instead of connecting through the TNS listener, it uses Oracle’s local &lt;strong&gt;Bequeath (&lt;code&gt;PROTOCOL=beq&lt;/code&gt;)&lt;/strong&gt; IPC mechanism, spawning a local server process with OS authentication and zero stored passwords.&lt;/p&gt;
&lt;p&gt;A critical implementation detail for the &lt;code&gt;godror&lt;/code&gt; Go driver: unlike &lt;code&gt;sqlplus / as sysdba&lt;/code&gt;, &lt;code&gt;godror&lt;/code&gt; (and underlying ODPI-C) cannot parse a bare &lt;code&gt;&quot;sys/ as sysdba&quot;&lt;/code&gt; or &lt;code&gt;&quot;/ as sysdba&quot;&lt;/code&gt; DSN. Local &lt;code&gt;SYSDBA&lt;/code&gt; over Bequeath requires either &lt;code&gt;externalAuth=1&lt;/code&gt; (for externally identified accounts) or &lt;code&gt;adminRole=SYSDBA&lt;/code&gt; paired with an explicit &lt;code&gt;PROTOCOL=beq&lt;/code&gt; connect descriptor specifying &lt;code&gt;PROGRAM&lt;/code&gt;, &lt;code&gt;ORACLE_HOME&lt;/code&gt;, and &lt;code&gt;ORACLE_SID&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;package&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; main&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;context&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;database/sql&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;fmt&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;log&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	_ &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;github.com/godror/godror&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;github.com/prometheus/client_golang/prometheus&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	patchReadyGauge &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; prometheus.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;NewGaugeVec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;		prometheus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;GaugeOpts&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			Name: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;fleet_db_patch_ready&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			Help: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Binary indicator (1=Ready, 0=Blocked) of database patch readiness&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		[]&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;region&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;db_name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	invalidObjectsGauge &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; prometheus.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;NewGaugeVec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;		prometheus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;GaugeOpts&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			Name: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;fleet_db_invalid_objects_total&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			Help: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Invalid objects in SYS and SYSTEM schemas&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		[]&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;region&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;db_name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	fraUsagePercentGauge &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; prometheus.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;NewGaugeVec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;		prometheus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;GaugeOpts&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			Name: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;fleet_db_fra_usage_percent&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			Help: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Fast Recovery Area utilization percentage&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		[]&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;region&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;db_name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; buildBequeathDSN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;oracleHome&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;sid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	desc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Sprintf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;		`(DESCRIPTION=(ADDRESS=(PROTOCOL=beq)(PROGRAM=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%s&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/bin/oracle)(ARGV0=oracle&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%s&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)`&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;			`(ENVS=&quot;ORACLE_HOME=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%s&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;,ORACLE_SID=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%s&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;)(ARGS=&quot;(DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))&quot;))`&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;			`(CONNECT_DATA=(SID=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%s&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)))`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		oracleHome, sid, oracleHome, sid, sid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Sprintf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`user=&quot;SYS&quot; adminRole=SYSDBA connectString=&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%s&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, desc)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; collectTelemetry&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;db&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sql&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;DB&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;region&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;dbName&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	ctx, cancel &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; context.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;WithTimeout&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(context.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Background&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;time.Second)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	defer&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; cancel&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	query &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; `&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;		SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;			(SELECT COUNT(*) FROM dba_objects&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;			  WHERE status = &apos;INVALID&apos; AND owner IN (&apos;SYS&apos;, &apos;SYSTEM&apos;)) AS invalid_core_objs,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;			(SELECT NVL(ROUND((space_used / NULLIF(space_limit, 0)) * 100, 2), 0)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;			   FROM v$recovery_file_dest) AS fra_pct,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;			(SELECT NVL(MAX(used_percent), 0) FROM dba_tablespace_usage_metrics&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;			  WHERE tablespace_name IN (&apos;SYSTEM&apos;, &apos;SYSAUX&apos;)) AS sys_ts_pct,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;			(SELECT database_role FROM v$database) AS db_role,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;			(SELECT COUNT(*) FROM v$transaction) AS active_tx_count&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;		FROM dual`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; invalidObjs, activeTx &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fraPct, sysTsPct &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;float64&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	var&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dbRole &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;QueryRowContext&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, query).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Scan&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;invalidObjs, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;fraPct, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sysTsPct, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;dbRole, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;activeTx); err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		log.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Printf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[ERROR] Telemetry query failed: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%v&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, err)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		patchReadyGauge.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;WithLabelValues&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(region, dbName, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;UNKNOWN&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Set&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	invalidObjectsGauge.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;WithLabelValues&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(region, dbName).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Set&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;float64&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(invalidObjs))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	fraUsagePercentGauge.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;WithLabelValues&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(region, dbName).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Set&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(fraPct)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	isReady &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; invalidObjs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fraPct &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 80.0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sysTsPct &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 75.0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; activeTx &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		isReady &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	patchReadyGauge.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;WithLabelValues&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(region, dbName, dbRole).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Set&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(isReady)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	dsn &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; buildBequeathDSN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(os.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Getenv&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ORACLE_HOME&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), os.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Getenv&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ORACLE_SID&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	db, err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sql.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;godror&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, dsn)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		log.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Fatalf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;godror init failed: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%v&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, err)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	defer&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Close&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	ticker &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;NewTicker&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time.Second)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	defer&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ticker.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Stop&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	for&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ticker.C {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;		collectTelemetry&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(db, os.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Getenv&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;CLOUD_REGION&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), os.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Getenv&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ORACLE_SID&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note the use of &lt;code&gt;DBA_TABLESPACE_USAGE_METRICS&lt;/code&gt; (a native Oracle view that accounts for &lt;code&gt;AUTOEXTEND&lt;/code&gt; max sizes in blocks) rather than the non-existent &lt;code&gt;DBA_TABLESPACE_USAGE&lt;/code&gt;. To guarantee the collector never competes with Oracle foreground or LGWR/DBWR processes, systemd Cgroup v2 directives cap the daemon at 5% of one CPU core and 256 MB of RAM:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;ini&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# /etc/systemd/system/db-telemetry-sidecar.service&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;[Service]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;Type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=simple&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;User&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=oracle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;Group&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=oinstall&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;Environment&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=/u01/app/oracle/product/19.0.0.0/dbhome_1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;Environment&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORACLE_SID&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=PRODDB01&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ExecStart&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=/opt/enterprise/bin/db-telemetry-sidecar&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;Restart&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=always&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;RestartSec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CPUAccounting&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CPUQuota&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=5%&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;MemoryAccounting&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;MemoryMax&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=256M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;MemoryHigh&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=200M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;plane-2--plane-3-outbound-ingestion-and-fail-closed-temporal-gate&quot;&gt;Plane 2 &amp;#x26; Plane 3: Outbound Ingestion and Fail-Closed Temporal Gate&lt;/h3&gt;
&lt;p&gt;The sidecar pushes its low-cardinality state vector (&lt;code&gt;region&lt;/code&gt;, &lt;code&gt;db_name&lt;/code&gt;, &lt;code&gt;role&lt;/code&gt;—only 3 labels per instance) outbound over mTLS to a regional Kafka cluster or Prometheus remote-write gateway. Keeping label cardinality fixed at three keys is mandatory: if an engineer adds &lt;code&gt;pdb_name&lt;/code&gt;, &lt;code&gt;tablespace_name&lt;/code&gt;, or &lt;code&gt;sql_id&lt;/code&gt; to the readiness gauge, series counts multiply across multi-tenant CDBs and blow through TSDB head-block memory budgets. Granular diagnostics—such as which specific tablespace crossed 75% or which &lt;code&gt;SYS&lt;/code&gt; package became &lt;code&gt;INVALID&lt;/code&gt;—belong in structured error logs or on-demand diagnostic events rather than high-frequency Prometheus labels.&lt;/p&gt;
&lt;p&gt;To prevent thundering herds when a regional network partition heals or a Kafka broker rolls, each sidecar buffers at most the latest state vector in memory (dropping intermediate 10-second ticks rather than queuing unbounded backlogs) and reconnects using &lt;strong&gt;exponential backoff with full jitter&lt;/strong&gt; (for example, &lt;code&gt;sleep = random(0, min(cap, base * 2^attempt))&lt;/code&gt;). Dropping stale buffered ticks is intentional: for pre-flight gating, Temporal only cares about current database state, never replaying 30 minutes of historical 10-second gauges after a disconnect.&lt;/p&gt;
&lt;p&gt;When Temporal schedules a patching wave, it evaluates readiness across the entire region with a single PromQL query that &lt;strong&gt;must&lt;/strong&gt; wrap the metric in &lt;code&gt;timestamp()&lt;/code&gt; to verify freshness:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;# Fail-closed Temporal pre-flight gate:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;# Only return PRIMARY instances whose latest sample is &amp;#x3C;= 60s old AND ready == 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  fleet_db_patch_ready{region=&quot;us-ashburn-1&quot;, role=&quot;PRIMARY&quot;} == 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;and&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  (time() - timestamp(fleet_db_patch_ready{region=&quot;us-ashburn-1&quot;, role=&quot;PRIMARY&quot;})) &amp;#x3C;= 60&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using &lt;code&gt;time() - metric &gt; 60&lt;/code&gt; without &lt;code&gt;timestamp()&lt;/code&gt; is a subtle PromQL bug: it subtracts the gauge value (&lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt;) from Unix epoch seconds rather than checking when the sample was scraped. Wrapping the series in &lt;code&gt;timestamp()&lt;/code&gt; ensures that any dead sidecar, partitioned host, or OOM-killed agent drops out of the ready set within 60 seconds.&lt;/p&gt;
&lt;h3 id=&quot;telemetry-invariant-matrix&quot;&gt;Telemetry Invariant Matrix&lt;/h3&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Telemetry Metric&lt;/th&gt;&lt;th&gt;Underlying Oracle View&lt;/th&gt;&lt;th&gt;Ready Threshold&lt;/th&gt;&lt;th&gt;Failure Prevented&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Core Invalid Objects&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;DBA_OBJECTS&lt;/code&gt; (&lt;code&gt;SYS&lt;/code&gt;, &lt;code&gt;SYSTEM&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;&lt;code&gt;0&lt;/code&gt; invalid objects&lt;/td&gt;&lt;td&gt;&lt;code&gt;datapatch&lt;/code&gt; recompilation deadlocks or registry failures&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;&lt;code&gt;SYSTEM&lt;/code&gt; / &lt;code&gt;SYSAUX&lt;/code&gt; Usage&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;DBA_TABLESPACE_USAGE_METRICS&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;&amp;#x3C; 75%&lt;/code&gt; of max autoextend&lt;/td&gt;&lt;td&gt;&lt;code&gt;ORA-01653: unable to extend table&lt;/code&gt; mid-patch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Fast Recovery Area&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;V$RECOVERY_FILE_DEST&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;&amp;#x3C; 80%&lt;/code&gt; of &lt;code&gt;SPACE_LIMIT&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;ORA-00257: archiver error&lt;/code&gt; during pre-patch backup&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Data Guard Apply Lag&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;&amp;#x3C; 30&lt;/code&gt; seconds&lt;/td&gt;&lt;td&gt;Unrecoverable switchover lag if primary reboot stalls&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Active Transactions&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;V$TRANSACTION&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;0&lt;/code&gt; long-running txns&lt;/td&gt;&lt;td&gt;Prolonged SMON rollback recovery after &lt;code&gt;SHUTDOWN&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Replacing centralized pull scripts with a sidecar gate should happen in four stages so teams can verify telemetry parity before trusting the TSDB to authorize production restarts.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-05-20-automated-oracle-patch-validation-python-5-flow.svg&quot; alt=&quot;Four-phase shadow-mode rollout with parity audit and canary gating&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Shadow Deployment.&lt;/strong&gt; Roll out &lt;code&gt;db-telemetry-sidecar&lt;/code&gt; under systemd cgroup limits (&lt;code&gt;CPUQuota=5%&lt;/code&gt;, &lt;code&gt;MemoryMax=256M&lt;/code&gt;) across non-production and production hosts in read-only mode, keeping existing Ansible/Python pre-flight checks as the authoritative gate. &lt;em&gt;Validation gate:&lt;/em&gt; Zero &lt;code&gt;ORA-01031&lt;/code&gt; OS auth errors and zero cgroup OOM kills over 7 days. &lt;em&gt;Rollback:&lt;/em&gt; &lt;code&gt;systemctl stop db-telemetry-sidecar&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Parity Audit.&lt;/strong&gt; Run a shadow comparison job inside Temporal that queries both the legacy pull runner and the PromQL &lt;code&gt;timestamp()&lt;/code&gt; gate before non-production patch waves, logging every discrepancy. &lt;em&gt;Validation gate:&lt;/em&gt; 100% agreement on &lt;code&gt;SYSTEM&lt;/code&gt;/&lt;code&gt;SYSAUX&lt;/code&gt; headroom, FRA usage, and invalid core objects across a full patch cycle. &lt;em&gt;Rollback:&lt;/em&gt; Keep legacy pull runner as the blocking gate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Canary Wave Gating.&lt;/strong&gt; Switch Temporal’s pre-flight activity to enforce the PromQL gate on development and staging waves while keeping legacy scripts available as a manual fallback flag. &lt;em&gt;Validation gate:&lt;/em&gt; Sub-second wave filtering and clean fail-closed exclusion when a test sidecar is intentionally stopped. &lt;em&gt;Rollback:&lt;/em&gt; Flip the Temporal workflow feature flag back to pull mode.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Fleet Cutover and Credential Decommissioning.&lt;/strong&gt; Enable PromQL gating for production waves, revoke central &lt;code&gt;SYSDBA&lt;/code&gt; secrets from the legacy Ansible/Jenkins vault, and remove inbound SSH/SQL*Net rules used solely by the old polling servers.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Oracle &lt;code&gt;DBA_TABLESPACE_USAGE_METRICS&lt;/code&gt; behavior (Oracle Database Reference):&lt;/strong&gt; Unlike manual joins of &lt;code&gt;DBA_DATA_FILES&lt;/code&gt; and &lt;code&gt;DBA_FREE_SPACE&lt;/code&gt;, &lt;code&gt;DBA_TABLESPACE_USAGE_METRICS&lt;/code&gt; calculates &lt;code&gt;USED_PERCENT&lt;/code&gt; against &lt;code&gt;TABLESPACE_SIZE&lt;/code&gt; accounting for &lt;code&gt;AUTOEXTEND&lt;/code&gt; max size in blocks—preventing false-positive alerts on auto-extensible &lt;code&gt;SYSTEM&lt;/code&gt;/&lt;code&gt;SYSAUX&lt;/code&gt; datafiles that appear 99% full at their current allocated size.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data Guard standby-first invariant (&lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt;):&lt;/strong&gt; In an Oracle Data Guard topology, rolling Release Updates apply to physical standbys before the primary. Checking &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; for both &lt;code&gt;transport lag&lt;/code&gt; and &lt;code&gt;apply lag&lt;/code&gt; ensures the standby is caught up before &lt;code&gt;SHUTDOWN IMMEDIATE&lt;/code&gt; on the primary; if apply lag exceeds 30 seconds, the sidecar sets &lt;code&gt;fleet_db_patch_ready = 0&lt;/code&gt; so Temporal pauses the wave rather than leaving the primary without a fast-failover target.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle Bequeath (&lt;code&gt;PROTOCOL=beq&lt;/code&gt;) behavior (Oracle Net Services &amp;#x26; &lt;code&gt;godror&lt;/code&gt;):&lt;/strong&gt; Because Bequeath spawns a local dedicated server process directly over OS pipes/IPC without traversing &lt;code&gt;tnslsnr&lt;/code&gt;, telemetry continues working even if the TNS listener is down or firewalled, and OS group membership (&lt;code&gt;oinstall&lt;/code&gt;/&lt;code&gt;dba&lt;/code&gt;) replaces stored passwords. In containerized or multi-home environments, each sidecar instance must point &lt;code&gt;ORACLE_HOME&lt;/code&gt; and &lt;code&gt;ORACLE_SID&lt;/code&gt; to the active GI/RDBMS home so out-of-place patching (&lt;code&gt;rhpctl&lt;/code&gt; or Fleet Patching and Provisioning) updates the environment file alongside &lt;code&gt;/etc/oratab&lt;/code&gt; during home switchover.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;OpenTelemetry Database Semantic Conventions (CNCF / OTel v1.30+):&lt;/strong&gt; Teams standardizing beyond custom Prometheus gauges can map pool and operation telemetry to OpenTelemetry’s current database semantic conventions—&lt;code&gt;db.client.operation.duration&lt;/code&gt; (stable histogram, seconds) and &lt;code&gt;db.client.connection.count&lt;/code&gt; (replacing older &lt;code&gt;db.client.connections.usage&lt;/code&gt;), tagged with &lt;code&gt;db.system.name&lt;/code&gt;—while keeping the discrete &lt;code&gt;fleet_db_patch_ready&lt;/code&gt; state vector for Temporal gating.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Condition&lt;/th&gt;&lt;th&gt;Breaks How&lt;/th&gt;&lt;th&gt;Detect&lt;/th&gt;&lt;th&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Sidecar freezes on dictionary lock&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;DBA_OBJECTS&lt;/code&gt; waits on &lt;code&gt;library cache lock&lt;/code&gt; during heavy DDL; stale &lt;code&gt;1&lt;/code&gt; stays in TSDB&lt;/td&gt;&lt;td&gt;PromQL &lt;code&gt;time() - timestamp(...) &gt; 60&lt;/code&gt; drops host from ready set&lt;/td&gt;&lt;td&gt;Wrap every query in &lt;code&gt;context.WithTimeout(8*time.Second)&lt;/code&gt; and force &lt;code&gt;patch_ready = 0&lt;/code&gt; on error&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Thundering herd after network partition&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Thousands of sidecars reconnect to Kafka/remote-write simultaneously, exhausting broker memory&lt;/td&gt;&lt;td&gt;Surge in HTTP 429/503 or Kafka broker CPU/GC pauses&lt;/td&gt;&lt;td&gt;Implement exponential backoff with full jitter on sidecar push retries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Sidecar cgroup OOM kill&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Unbounded log/metric buffer exceeds &lt;code&gt;MemoryMax=256M&lt;/code&gt;; kernel kills agent&lt;/td&gt;&lt;td&gt;&lt;code&gt;systemd&lt;/code&gt; restart counter increments and host disappears from TSDB&lt;/td&gt;&lt;td&gt;Set &lt;code&gt;Restart=always&lt;/code&gt;, keep cardinality to 3 labels, and alert on missing heartbeats&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;OS group drift removes &lt;code&gt;dba&lt;/code&gt; membership&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Sidecar Bequeath connection fails with &lt;code&gt;ORA-01031: insufficient privileges&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;fleet_db_patch_ready&lt;/code&gt; flips to &lt;code&gt;0&lt;/code&gt; across affected hosts&lt;/td&gt;&lt;td&gt;Enforce &lt;code&gt;/etc/group&lt;/code&gt; membership via configuration management and alert on &lt;code&gt;ORA-01031&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;NTP clock skew on DB host&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Host clock drifts &gt; 120s; TSDB rejects remote-write samples as out-of-order&lt;/td&gt;&lt;td&gt;&lt;code&gt;node_timex_offset_seconds &gt; 0.5&lt;/code&gt; alert fires&lt;/td&gt;&lt;td&gt;Gate patching on verified NTP sync alongside &lt;code&gt;fleet_db_patch_ready&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Non-critical schema invalid objects&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;An unused test schema invalid object blocks an entire canary wave if unfiltered&lt;/td&gt;&lt;td&gt;High false-positive &lt;code&gt;patch_ready == 0&lt;/code&gt; rate before maintenance window&lt;/td&gt;&lt;td&gt;Restrict &lt;code&gt;DBA_OBJECTS&lt;/code&gt; invalid check strictly to &lt;code&gt;SYS&lt;/code&gt;, &lt;code&gt;SYSTEM&lt;/code&gt;, &lt;code&gt;MDSYS&lt;/code&gt;, and &lt;code&gt;CTXSYS&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2024-05-20-automated-oracle-patch-validation-python-6-failures.svg&quot; alt=&quot;Four silent failure modes in push-based fleet telemetry&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Inbound polling fails on physics and security before code quality matters:&lt;/strong&gt; At 10,000 databases, even 50 concurrent workers hit multi-minute sweep drift, exhaust firewall &lt;code&gt;nf_conntrack&lt;/code&gt; state tables, and force you to store fleet-wide &lt;code&gt;SYSDBA&lt;/code&gt; credentials in one place.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;godror&lt;/code&gt; requires an explicit Bequeath descriptor for local OS auth:&lt;/strong&gt; Passing &lt;code&gt;&quot;sys/ as sysdba&quot;&lt;/code&gt; fails in &lt;code&gt;godror&lt;/code&gt;; use &lt;code&gt;adminRole=SYSDBA&lt;/code&gt; with a &lt;code&gt;PROTOCOL=beq&lt;/code&gt; &lt;code&gt;connectString&lt;/code&gt; (or &lt;code&gt;externalAuth=1&lt;/code&gt;) so the sidecar authenticates over local IPC with zero passwords.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Query &lt;code&gt;DBA_TABLESPACE_USAGE_METRICS&lt;/code&gt;, not &lt;code&gt;DBA_TABLESPACE_USAGE&lt;/code&gt;:&lt;/strong&gt; Oracle’s built-in view accounts for &lt;code&gt;AUTOEXTEND&lt;/code&gt; ceilings automatically, avoiding false pre-flight blocks on &lt;code&gt;SYSTEM&lt;/code&gt; and &lt;code&gt;SYSAUX&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PromQL freshness gates require &lt;code&gt;timestamp()&lt;/code&gt;:&lt;/strong&gt; Always evaluate &lt;code&gt;(time() - timestamp(fleet_db_patch_ready{...})) &amp;#x3C;= 60&lt;/code&gt; so a dead or OOM-killed sidecar fails closed rather than leaving a stale &lt;code&gt;1&lt;/code&gt; in the TSDB.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sandbox the collector with systemd Cgroup v2 limits:&lt;/strong&gt; &lt;code&gt;CPUQuota=5%&lt;/code&gt; and &lt;code&gt;MemoryMax=256M&lt;/code&gt; guarantee a runaway telemetry query can never starve Oracle background processes.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Centralized SSH and SQL*Net polling cannot evaluate pre-flight health across thousands of Oracle databases without exhausting firewall state tables, centralizing &lt;code&gt;SYSDBA&lt;/code&gt; secrets, and acting on stale data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Deploy a cgroup-capped Go telemetry sidecar using local Bequeath (&lt;code&gt;PROTOCOL=beq&lt;/code&gt;) authentication, pushing low-cardinality &lt;code&gt;fleet_db_patch_ready&lt;/code&gt; state vectors outbound to a TSDB queried by Temporal with a &lt;code&gt;timestamp()&lt;/code&gt; freshness guard.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Verify in shadow mode that every host emits fresh samples every 10 seconds with zero inbound firewall ports opened and 100% parity against legacy pre-flight checks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your current patch orchestrator this week for inbound SSH/SQL*Net fanout and central &lt;code&gt;SYSDBA&lt;/code&gt; vaults, then prototype the &lt;code&gt;PROTOCOL=beq&lt;/code&gt; Go collector on a non-production Oracle 19c host.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>Ephemeral Data Environments: Zero-Cost Database Cloning for CI/CD via Storage Snapshots</title><link>https://rajivonai.com/blog/2024-04-18-ephemeral-database-cloning-storage-snapshots/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-04-18-ephemeral-database-cloning-storage-snapshots/</guid><description>Why validating migrations against mocked test data guarantees production outages, and how to build an ephemeral data platform that provisions isolated 10-terabyte Copy-on-Write database clones in seconds.</description><pubDate>Sun, 07 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Testing database migrations against synthetic or truncated datasets hides table-rewrite locks and index bloat; validating schema changes safely requires running CI checks against Copy-on-Write storage clones of full-scale, masked production data.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Problem: A 50 MB synthetic test database passes DDL in milliseconds, while the same &lt;code&gt;ALTER TABLE&lt;/code&gt; with a volatile default or type change rewrites a 10 TB production table under &lt;code&gt;AccessExclusiveLock&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Answer: Build a nightly PII-masked Golden Snapshot and clone it per pull request via Kubernetes CSI &lt;code&gt;VolumeSnapshot&lt;/code&gt; Copy-on-Write storage, gated by &lt;code&gt;lock_timeout&lt;/code&gt; profiling and strict TTL reapers.&lt;/li&gt;
&lt;li&gt;Watch out: Standard EBS snapshots lazy-load blocks from S3 on first read, while EBS Fast Snapshot Restore (FSR) bills &lt;code&gt;$0.75/hour&lt;/code&gt; per snapshot per Availability Zone continuously.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2024-04-18-ephemeral-database-cloning-storage-snapshots.svg&quot; alt=&quot;Synthetic mocks hide table-rewrite locks; Copy-on-Write snapshots test migrations against production reality&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Engineering teams validate application code on every pull request using ephemeral containers and automated integration suites. Database schema migrations, by contrast, are still commonly tested against one of three compromised environments:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Synthetic or Mock Datasets:&lt;/strong&gt; A local container seeded with a few thousand rows validates SQL syntax, but hides B-tree index depth, dead-tuple bloat, and query optimizer cardinality transitions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Static Shared Staging:&lt;/strong&gt; Multiple teams share a single long-lived staging database. Because dropping tables or running destructive rollbacks disrupts neighboring squads, teams avoid testing failure paths, and staging data drifts weeks behind production.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Truncated Subset Clones:&lt;/strong&gt; An ETL job copies a 1% or 5% sample of production rows. Sampling breaks foreign-key distributions, misses Pareto-skewed tenant hotspots, and understates the runtime of full-table rewrites.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When an &lt;code&gt;ALTER TABLE&lt;/code&gt; statement passes in 120 milliseconds against a 50 MB synthetic container, the pull request merges—pushing an untested lock and I/O bomb straight toward production.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Running unverified migrations against a 10-terabyte production table exposes three hard operational and financial constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Physical Clone Storage Math:&lt;/strong&gt; Giving 50 developers dedicated physical 10 TB copies via &lt;code&gt;pg_restore&lt;/code&gt;, MySQL physical backup, or Oracle RMAN duplicate requires provisioning 500 TB (512,000 GB) of block storage. At AWS EBS &lt;code&gt;gp3&lt;/code&gt; base pricing (&lt;code&gt;$0.08/GB-month&lt;/code&gt;), raw disk alone costs &lt;code&gt;$40,960/month&lt;/code&gt; before counting IOPS, compute, or the hours spent streaming terabytes over the network. By contrast, a Copy-on-Write clone consuming 20 GB of write deltas adds &lt;code&gt;$1.60/month&lt;/code&gt; in storage per active branch.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PostgreSQL &lt;code&gt;AccessExclusiveLock&lt;/code&gt; Queue Cascade:&lt;/strong&gt; Every &lt;code&gt;ALTER TABLE&lt;/code&gt; acquires an &lt;code&gt;AccessExclusiveLock&lt;/code&gt;, blocking all concurrent &lt;code&gt;SELECT&lt;/code&gt;, &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, and &lt;code&gt;DELETE&lt;/code&gt; queries on that table. Even a fast metadata-only &lt;code&gt;ALTER TABLE&lt;/code&gt; creates an outage if it queues behind a single long-running reporting &lt;code&gt;SELECT&lt;/code&gt;: once &lt;code&gt;ALTER TABLE&lt;/code&gt; waits in the lock queue, every subsequent &lt;code&gt;SELECT&lt;/code&gt; and &lt;code&gt;INSERT&lt;/code&gt; queues behind &lt;code&gt;ALTER TABLE&lt;/code&gt;, exhausting application connection pools within seconds. Worse, while PostgreSQL 11+ executes &lt;code&gt;ADD COLUMN&lt;/code&gt; with a constant non-volatile default as a fast metadata-only catalog update, adding a column with a volatile default (such as &lt;code&gt;now()&lt;/code&gt;, &lt;code&gt;random()&lt;/code&gt;, or &lt;code&gt;clock_timestamp()&lt;/code&gt;), changing a column data type, or rewriting a constraint forces PostgreSQL to rewrite every physical heap page and secondary index on disk while holding &lt;code&gt;AccessExclusiveLock&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;EBS Lazy Loading vs. Fast Snapshot Restore (FSR) Economics:&lt;/strong&gt; Standard AWS EBS snapshots restore a volume immediately at zero extra hourly fee, but stream blocks lazily from Amazon S3 on first read—distorting migration duration benchmarks. Enabling EBS Fast Snapshot Restore (FSR) pre-warms blocks for full IOPS immediately, but bills &lt;strong&gt;&lt;code&gt;$0.75 per DSU-hour&lt;/code&gt;&lt;/strong&gt; (1 snapshot × 1 AZ × 1 hour, 1-hour minimum) continuously from enablement until disabled (&lt;code&gt;$547.50/month&lt;/code&gt; per AZ, or &lt;code&gt;$1,642.50/month&lt;/code&gt; across three AZs).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2024-04-18-ephemeral-database-cloning-storage-snapshots-2-constraints.svg&quot; alt=&quot;Physical cloning multiplies storage 50x, while EBS Fast Snapshot Restore bills $0.75 per AZ-hour&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How can platform teams give every pull request an isolated, full-scale production-equivalent database in seconds without paying for 50x physical storage or exposing unmasked PII?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Teams building pre-merge database verification choose among four approaches, each trading off fidelity, I/O latency, and infrastructure cost.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Approach&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Provisioning Latency&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Storage Footprint&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Synthetic Mocks / Subsets&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Seconds&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Megabytes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero B-tree depth or skew fidelity; misses &lt;code&gt;AccessExclusiveLock&lt;/code&gt; rewrites&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt; for DDL gating&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Physical Restore (&lt;code&gt;pg_restore&lt;/code&gt; / RMAN)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Hours&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;1.0x&lt;/code&gt; per developer (&lt;code&gt;500 TB&lt;/code&gt; for 50 devs)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Prohibitive storage bill and restore time&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt; for per-PR CI&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;K8s CSI + EBS Snapshot (with single-AZ FSR)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Seconds&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Delta blocks only&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;$0.75/hr&lt;/code&gt; per AZ for FSR; requires WAL crash recovery on boot&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Selected&lt;/strong&gt; for AWS EKS fleets&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;ZFS/SAN CoW or Neon/Aurora Branching&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sub-second to seconds&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Delta blocks only&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires ZFS/SAN hardware or serverless branching engine&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Selected&lt;/strong&gt; where storage/engine supports it&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Synthetic mocks are rejected as the sole migration gate because they cannot reproduce lock queues or multi-hour table rewrites. Physical restores are rejected because 50x storage multiplication and multi-hour restore windows break CI feedback loops. Copy-on-Write (CoW) cloning via Kubernetes CSI &lt;code&gt;VolumeSnapshot&lt;/code&gt;—pinned to a single Availability Zone when using EBS FSR—delivers full 10 TB physical fidelity while allocating block storage only for modified pages.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-04-18-ephemeral-database-cloning-storage-snapshots-3-options.svg&quot; alt=&quot;Choose between CSI EBS snapshots, SAN/ZFS CoW clones, and serverless branching by I/O and budget&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-ephemeral-storage-clone-architecture&quot;&gt;The Ephemeral Storage Clone Architecture&lt;/h2&gt;
&lt;p&gt;The ephemeral data platform separates nightly PII sanitization from instant per-PR Copy-on-Write cloning.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-04-18-ephemeral-database-cloning-storage-snapshots-4-architecture.svg&quot; alt=&quot;Sanitization creates an immutable Golden Snapshot; K8s CSI clones it into isolated PR pods&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-nightly-sanitization-and-golden-snapshot-pipeline&quot;&gt;1. Nightly Sanitization and Golden Snapshot Pipeline&lt;/h3&gt;
&lt;p&gt;Production data cannot be mounted directly into developer CI namespaces under GDPR, HIPAA, or PCI-DSS. Each night, a scheduled job issues &lt;code&gt;CHECKPOINT&lt;/code&gt; on a production read replica to flush dirty buffers and minimize WAL replay time, takes a block snapshot, and attaches it to an isolated sanitization worker. The worker runs deterministic PII masking—using keyed HMAC-SHA256 hashes or format-preserving tokenization for joinable keys like &lt;code&gt;email&lt;/code&gt; and &lt;code&gt;account_id&lt;/code&gt; so foreign-key integrity, cardinality, &lt;code&gt;NULL&lt;/code&gt; ratios, and row-count skew stay intact—and then runs &lt;code&gt;ANALYZE&lt;/code&gt; so &lt;code&gt;pg_statistic&lt;/code&gt; histograms match production query plans. Finally, an automated PII-scanner attestation gate inspects sampled columns before publishing an immutable read-only &lt;strong&gt;Golden &lt;code&gt;VolumeSnapshot&lt;/code&gt;&lt;/strong&gt; protected by &lt;code&gt;DoNotDelete&lt;/code&gt; IAM Service Control Policies.&lt;/p&gt;
&lt;h3 id=&quot;2-kubernetes-csi-copy-on-write-cloning&quot;&gt;2. Kubernetes CSI Copy-on-Write Cloning&lt;/h3&gt;
&lt;p&gt;When a developer opens a pull request, an Ephemeral Data Operator creates a &lt;code&gt;PersistentVolumeClaim&lt;/code&gt; referencing the Golden &lt;code&gt;VolumeSnapshot&lt;/code&gt; via the stable &lt;code&gt;snapshot.storage.k8s.io/v1&lt;/code&gt; API. Under Kubernetes CSI rules, both the &lt;code&gt;VolumeSnapshot&lt;/code&gt; and the cloned &lt;code&gt;PersistentVolumeClaim&lt;/code&gt; must reside in the &lt;strong&gt;same namespace&lt;/strong&gt;, and the requested &lt;code&gt;resources.requests.storage&lt;/code&gt; must be &lt;strong&gt;equal to or greater than&lt;/strong&gt; the source snapshot size. Because EBS &lt;code&gt;ReadWriteOnce&lt;/code&gt; volumes can only attach to one EC2 node at a time, the operator also sets short CSI volume-attachment timeouts so a rescheduled pod is not stuck in &lt;code&gt;Multi-Attach&lt;/code&gt; if an underlying spot node terminates abruptly:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;apiVersion&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;v1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;kind&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;PersistentVolumeClaim&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;metadata&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;data-clone-pr-1402&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  namespace&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;database-ephemeral-ci&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;spec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  accessModes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ReadWriteOnce&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  storageClassName&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;ebs-gp3-sc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  resources&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    requests&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      storage&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;10Ti&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  dataSource&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    apiGroup&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;snapshot.storage.k8s.io&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    kind&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;VolumeSnapshot&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;snap-golden-masked-prod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At creation time, the clone shares read pointers with the Golden Snapshot and consumes zero private data blocks until the migration writes deltas. Under the hood, local NVMe arrays (such as ZFS or Pure Storage FlashArray) resolve those pointers directly on local flash at wire speed, whereas cloud block snapshots (such as AWS EBS) store base blocks in S3 and fetch them lazily on first read unless Fast Snapshot Restore (FSR) pre-warms the volume.&lt;/p&gt;
&lt;p&gt;To prevent runaway delta allocation when a developer tests an unindexed full-table &lt;code&gt;UPDATE&lt;/code&gt;, the operator pairs the &lt;code&gt;PersistentVolumeClaim&lt;/code&gt; with a Kubernetes &lt;code&gt;ResourceQuota&lt;/code&gt; in &lt;code&gt;database-ephemeral-ci&lt;/code&gt; and monitors &lt;code&gt;kubelet_volume_stats_used_bytes&lt;/code&gt;. If delta writes exceed a configured ceiling—such as 150 GB on a 10 TB virtual volume—the CI job aborts before generating an unexpected storage bill.&lt;/p&gt;
&lt;h3 id=&quot;3-crash-consistent-startup-and-lock-profiling&quot;&gt;3. Crash-Consistent Startup and Lock Profiling&lt;/h3&gt;
&lt;p&gt;Because a live block snapshot captures the database in a crash-consistent state, the container entrypoint removes stale &lt;code&gt;postmaster.pid&lt;/code&gt; and socket files and lets PostgreSQL replay uncommitted WAL records before opening connections:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/bin/bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -euo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pipefail&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DATA_DIR&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/var/lib/postgresql/data&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;rm&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DATA_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/postmaster.pid&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DATA_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/.s.PGSQL.*&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;/usr/lib/postgresql/15/bin/pg_ctl&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -D&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DATA_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -w&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;psql&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -U&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; postgres&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; enterprise_db&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;CREATE EXTENSION IF NOT EXISTS pg_stat_statements; SELECT pg_stat_statements_reset();&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once PostgreSQL accepts connections, the CI runner executes the migration inside a transaction with &lt;code&gt;SET LOCAL lock_timeout = &apos;15s&apos;;&lt;/code&gt; and fails the build if the migration either exceeds the lock timeout or runs longer than a table-rewrite threshold. To catch lock-order deadlocks that never appear in single-session tests, the runner optionally spawns a lightweight background read/write load generator against the cloned database while the DDL executes:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; psycopg2, sys, time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; profile_migration&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(uri: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, sql_path: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    conn &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; psycopg2.connect(uri)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    conn.autocommit &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cur &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; conn.cursor()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    with&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(sql_path) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; f:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sql &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; f.read()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    t0 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time.time()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        cur.execute(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;BEGIN;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        cur.execute(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SET LOCAL lock_timeout = &apos;15s&apos;;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        cur.execute(sql)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        cur.execute(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;COMMIT;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time.time() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; t0 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 180.0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            sys.exit(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Suspected table rewrite on 10 TB dataset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    except&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; psycopg2.errors.LockNotAvailable:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sys.exit(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    finally&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        conn.close()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When &lt;code&gt;profile_migration.py&lt;/code&gt; rejects a blocking DDL statement, developers refactor the change into a four-step non-blocking sequence: add the column as nullable without a default (metadata-only), backfill rows in bounded batches, set the &lt;code&gt;DEFAULT&lt;/code&gt; for future writes, and add &lt;code&gt;CHECK&lt;/code&gt; or &lt;code&gt;NOT NULL&lt;/code&gt; constraints using &lt;code&gt;NOT VALID&lt;/code&gt; followed by &lt;code&gt;VALIDATE CONSTRAINT&lt;/code&gt; (which holds a weaker &lt;code&gt;ShareUpdateExclusiveLock&lt;/code&gt; instead of &lt;code&gt;AccessExclusiveLock&lt;/code&gt;).&lt;/p&gt;
&lt;h3 id=&quot;4-aggressive-ttl-reaper-and-serverless-branching&quot;&gt;4. Aggressive TTL Reaper and Serverless Branching&lt;/h3&gt;
&lt;p&gt;Every ephemeral pod and PVC carries a strict Time-to-Live (2 to 4 hours) and a PR-close webhook trigger. An automated reaper deletes expired pods and PVCs so weekend compute and delta-block charges never accumulate. In addition to DDL lock profiling, CI runners query &lt;code&gt;pg_stat_statements&lt;/code&gt; and &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; against the cloned dataset to catch query plan regressions—such as a dropped or altered index flipping an index scan into a multi-terabyte sequential scan—before the pull request merges. For teams already running serverless engines such as Neon or Aurora Serverless v2, native LSN-based branching provides the same CoW isolation without managing CSI snapshot classes directly.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Deploying ephemeral snapshot cloning safely requires validating boot time and PII sanitization before gating developer merges.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-04-18-ephemeral-database-cloning-storage-snapshots-5-flow.svg&quot; alt=&quot;Four-phase rollout builds the masked Golden Snapshot and lock profiler before gating merges&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Snapshot Boot Baseline:&lt;/strong&gt; Take a manual snapshot of a staging replica immediately after &lt;code&gt;CHECKPOINT&lt;/code&gt;, clone it to a single-AZ volume, and measure &lt;code&gt;pg_ctl start&lt;/code&gt; WAL recovery time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Automated PII Masking and Attestation:&lt;/strong&gt; Build the nightly sanitization worker, attach an automated PII-detection scan as a mandatory publish gate, and lock the Golden Snapshot with &lt;code&gt;DoNotDelete&lt;/code&gt; policies.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Single-AZ CSI + FSR and TTL Reaper:&lt;/strong&gt; Pin ephemeral CI pods to one Availability Zone so EBS Fast Snapshot Restore costs &lt;code&gt;$0.75/hour&lt;/code&gt; (1 AZ) instead of &lt;code&gt;$2.25/hour&lt;/code&gt; (3 AZs), and verify the 4-hour TTL reaper cleanly deletes PVCs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — CI Lock Profiler Gate:&lt;/strong&gt; Wire &lt;code&gt;profile_migration.py&lt;/code&gt; into pull requests to flag volatile-default table rewrites and &lt;code&gt;lock_timeout&lt;/code&gt; violations before merge.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Documented storage and database engine behaviors explain why CoW snapshot platforms succeed—and where operators must guard their budgets:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;AWS EBS Lazy Loading vs. FSR Billing:&lt;/strong&gt; AWS documentation confirms that volumes restored from standard EBS snapshots lazy-load blocks from S3 on first read, introducing unpredictable I/O latency during schema benchmarks. Enabling EBS Fast Snapshot Restore removes that penalty at &lt;code&gt;$0.75 per DSU-hour&lt;/code&gt; per AZ—meaning pinning CI runners to a single AZ cuts FSR spend by two-thirds compared to multi-AZ enablement.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Kubernetes CSI &lt;code&gt;VolumeSnapshot&lt;/code&gt; Constraints:&lt;/strong&gt; Under &lt;code&gt;snapshot.storage.k8s.io/v1&lt;/code&gt;, a cloned PVC must reside in the same namespace as the &lt;code&gt;VolumeSnapshot&lt;/code&gt; and request at least as much storage as the source snapshot. Teams organizing CI per-PR namespaces must either run all ephemeral DBs in a shared &lt;code&gt;database-ephemeral-ci&lt;/code&gt; namespace with network policies or replicate snapshot metadata per namespace.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PostgreSQL 11+ Metadata-Only vs. Table-Rewrite DDL:&lt;/strong&gt; PostgreSQL’s &lt;code&gt;ALTER TABLE&lt;/code&gt; documentation establishes that constant defaults are metadata-only since PostgreSQL 11, whereas volatile defaults (&lt;code&gt;now()&lt;/code&gt;, &lt;code&gt;random()&lt;/code&gt;) and column type changes rewrite the entire table under &lt;code&gt;AccessExclusiveLock&lt;/code&gt;. Testing against a real 10 TB CoW clone catches the difference immediately before production deployment.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CSI Block Clones vs. Serverless Page-Store Branching:&lt;/strong&gt; When teams run self-managed PostgreSQL on Kubernetes or EC2, CSI &lt;code&gt;VolumeSnapshot&lt;/code&gt; clones are the standard CoW primitive. Teams already running serverless storage engines such as Neon or Aurora Serverless v2 can instead branch directly at a specific LSN via API without managing &lt;code&gt;VolumeSnapshot&lt;/code&gt; CRDs, EBS FSR hours, or &lt;code&gt;pg_ctl&lt;/code&gt; crash recovery on container boot.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pattern Acknowledgment:&lt;/strong&gt; I have not personally operated a 50-developer EBS FSR fleet across 10 TB databases, but the documented storage-virtualization pattern across Kubernetes CSI, AWS EBS, and PostgreSQL internals is consistent: separate nightly PII masking from per-PR CoW clones, pin FSR to one AZ, and enforce hard TTLs.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Storage virtualization eliminates 50x physical copies, but introduces distinct operational failure modes.&lt;/p&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure Mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;EBS lazy-loading I/O stall&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Standard EBS snapshot streams blocks from S3 on first read, skewing CI timings&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High &lt;code&gt;iowait&lt;/code&gt; and read latency spikes on initial table scan&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enable single-AZ EBS FSR (&lt;code&gt;$0.75/hr&lt;/code&gt;) or use ZFS/SAN local CoW clones&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;PII masking gate failure&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Masking script fails silently; unmasked customer data lands in dev PVCs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Automated PII regex/entropy scanner flags unmasked columns&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Block Golden Snapshot promotion unless scanner exits clean&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Delta storage explosion&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unindexed full-table &lt;code&gt;UPDATE&lt;/code&gt; rewrites 100% of CoW blocks on disk&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rapid PVC delta growth alerts during CI run&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Abort ephemeral job and fail PR if delta write bytes exceed threshold&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Weekend compute sprawl&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Developers leave ephemeral pods running after PRs stall or close&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Orphaned pods older than 4 hours in &lt;code&gt;database-ephemeral-ci&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce 2–4h TTL reaper cron and PR-close webhook teardown&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Stale WAL crash recovery&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Snapshot captured during write burst holds gigabytes of unflushed WAL&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;pg_ctl start&lt;/code&gt; exceeds CI startup timeout replaying WAL&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run &lt;code&gt;CHECKPOINT&lt;/code&gt; on replica immediately before taking snapshot&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;CSI volume latch on node loss&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Node crashes leaving RWO EBS volume attached to dead instance&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rescheduled pod stuck in &lt;code&gt;ContainerCreating&lt;/code&gt; (&lt;code&gt;Multi-Attach&lt;/code&gt;)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Configure CSI volume attachment timeouts and force-detach recovery&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Base Golden Snapshot deletion&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Operator deletes Golden Snapshot while PR clones are mounted&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Active clones fail immediately with physical read I/O errors&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Protect Golden Snapshots with &lt;code&gt;DoNotDelete&lt;/code&gt; tags and AWS IAM SCPs&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2024-04-18-ephemeral-database-cloning-storage-snapshots-6-failures.svg&quot; alt=&quot;Four failure modes break ephemeral clones: S3 lazy-load stalls, PII leaks, delta explosions, and stale WAL&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Synthetic mocks cannot catch &lt;code&gt;AccessExclusiveLock&lt;/code&gt; rewrites:&lt;/strong&gt; Only a full-scale clone exposes multi-hour table rewrites triggered by volatile defaults or column type changes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CoW snapshots eliminate 50x storage duplication, not all cost:&lt;/strong&gt; Budget for write deltas, ephemeral pod compute, and single-AZ EBS Fast Snapshot Restore (&lt;code&gt;$0.75/DSU-hour&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pin CI clones to one Availability Zone and one namespace:&lt;/strong&gt; Single-AZ placement cuts FSR costs by 3x, and same-namespace placement satisfies Kubernetes &lt;code&gt;VolumeSnapshot&lt;/code&gt; &lt;code&gt;dataSource&lt;/code&gt; rules.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate Golden Snapshots on &lt;code&gt;CHECKPOINT&lt;/code&gt; and PII attestation:&lt;/strong&gt; Flushing dirty pages keeps pod startup fast, while an automated PII scanner prevents unmasked production data from reaching dev clusters.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Schema migrations that pass in milliseconds against 50 MB synthetic test databases trigger &lt;code&gt;AccessExclusiveLock&lt;/code&gt; outages and full table rewrites on multi-terabyte production tables.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build a nightly PII-masked Golden Snapshot pipeline and provision isolated per-PR Copy-on-Write clones via Kubernetes CSI &lt;code&gt;VolumeSnapshot&lt;/code&gt; with &lt;code&gt;lock_timeout&lt;/code&gt; profiling and strict TTL reapers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run a baseline test by issuing &lt;code&gt;CHECKPOINT&lt;/code&gt; on a staging replica, taking an EBS snapshot, restoring a volume in a single AZ, and measuring &lt;code&gt;pg_ctl start&lt;/code&gt; WAL recovery time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Wire &lt;code&gt;profile_migration.py&lt;/code&gt; with &lt;code&gt;SET LOCAL lock_timeout = &apos;15s&apos;&lt;/code&gt; into one high-churn repository this week, and pin your first &lt;code&gt;VolumeSnapshot&lt;/code&gt; clone in a single-AZ namespace to measure real DDL runtime before production rollout.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>Architecting Sharded Workloads on Oracle RAC: Eliminating GC Waits at the Application Tier</title><link>https://rajivonai.com/blog/2024-03-15-oracle-rac-hot-blocks-gc-waits-sequences/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-03-15-oracle-rac-hot-blocks-gc-waits-sequences/</guid><description>Why tuning &apos;gc buffer busy&apos; waits in Oracle RAC is a losing battle against physics, and how hyper-scale architectures eliminate Cache Fusion overhead by implementing cell-based application affinity.</description><pubDate>Thu, 04 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Attempting to resolve Oracle RAC interconnect bottlenecks through database-level parameter tuning is an architectural dead end; eliminating Cache Fusion contention requires restructuring workload affinity at the application and connection routing tiers.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Round-robin connection routing across Oracle RAC nodes forces concurrent DML on the same 8 KB data and index leaf blocks to ping-pong over the private interconnect via Cache Fusion, turning local memory writes into multi-node network waits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Implement Cell-Based Application Affinity by hashing tenant or account keys in the application router to dedicated connection pools bound to single-preferred-instance Oracle services (&lt;code&gt;srvctl&lt;/code&gt;), paired with &lt;code&gt;CACHE ... NOORDER&lt;/code&gt; sequences and hash-partitioned or reverse-key indexes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Pinning workloads trades Cache Fusion contention for hot-tenant CPU skew, cold-cache read spikes after instance failover, and connection churn on abrupt failback.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2024-03-15-oracle-rac-hot-blocks-gc-waits-sequences.svg&quot; alt=&quot;Comparison between uncoordinated round-robin RAC routing and cell-based application affinity&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Multi-node Oracle Real Application Clusters (RAC) deployments cluster multiple compute instances over a private interconnect—typically bonded 25 GbE, 100 GbE, or RDMA over Converged Ethernet (RoCE) on engineered systems such as Exadata—sharing a single storage tier managed by Oracle Automatic Storage Management (ASM).&lt;/p&gt;
&lt;p&gt;The default deployment pattern treats the RAC cluster as a homogeneous compute pool. Connection pools such as Oracle Universal Connection Pool (UCP) or HikariCP connect through a Single Client Access Name (SCAN) listener with &lt;code&gt;LOAD_BALANCE=ON&lt;/code&gt; against a cluster-wide database service:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;(DESCRIPTION=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  (ADDRESS_LIST=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    (LOAD_BALANCE=ON)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    (FAILOVER=ON)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    (ADDRESS=(PROTOCOL=TCP)(HOST=rac-scan.corp.internal)(PORT=1521))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  (CONNECT_DATA=(SERVICE_NAME=oltp_generic_service))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Under round-robin or least-loaded connection distribution, consecutive transactions for the same customer or tenant land on different instances. A checkout request updating a customer ledger row executes on Instance 1; milliseconds later, a fulfillment worker updating a sibling order row in the same 8 KB block executes on Instance 3; simultaneously, a balance check runs on Instance 2. Because all instances share the same underlying datafiles in ASM, Oracle must coordinate block ownership in memory across every node touching that block.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Oracle RAC maintains single-image cache coherency across instance System Global Areas (SGAs) using &lt;strong&gt;Cache Fusion&lt;/strong&gt;, coordinated by the Global Cache Service (GCS, backed by &lt;code&gt;LMS&lt;/code&gt; background processes) and the Global Enqueue Service (GES, backed by &lt;code&gt;LMD&lt;/code&gt; and &lt;code&gt;LMON&lt;/code&gt;). Only one instance can hold an exclusive lock to modify an 8 KB data block at any instant.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-03-15-oracle-rac-hot-blocks-gc-waits-sequences-2-constraints.svg&quot; alt=&quot;Three-way Cache Fusion block transfer across requester, GCS master, and current holder&quot;&gt;&lt;/p&gt;
&lt;p&gt;When Instance 3 needs to modify a dirty block currently cached in Instance 1’s SGA, it cannot read the stale copy on ASM storage. Instead, Cache Fusion executes a multi-hop protocol:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Instance 3 requests an exclusive current-mode block from the block’s GCS resource master (for example, Instance 2).&lt;/li&gt;
&lt;li&gt;The GCS master forwards the request to Instance 1, the current holder.&lt;/li&gt;
&lt;li&gt;Instance 1 records a Past Image (PI) in its local buffer cache, downgrades its lock, and ships the 8 KB block over the private interconnect to Instance 3.&lt;/li&gt;
&lt;li&gt;Instance 3 receives the block, applies its redo-protected change, and notifies the GCS master.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Four physical constraints govern this path:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Block-Transfer Bandwidth and Packet Rate:&lt;/strong&gt; Each cross-node current-block transfer ships at least an 8 KB block payload plus GCS lock and acknowledgement messages. Even if an application only mutates a 60-byte row, Cache Fusion moves the entire 8,192-byte block—a 136x payload amplification. At 50,000 cross-node block transfers per second across a cluster, the interconnect must move &lt;code&gt;50,000 * 8,192 bytes = 409.6 MB/s&lt;/code&gt; (~3.28 Gbps) of raw block data alone before packet headers, Past Image acknowledgements, and consistent-read (&lt;code&gt;CR&lt;/code&gt;) block clones.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LMS CPU and Scheduling Latency:&lt;/strong&gt; Every block grant and transfer passes through &lt;code&gt;LMS&lt;/code&gt; background processes. When interconnect packet rates rise, &lt;code&gt;LMS&lt;/code&gt; processes compete for CPU cycles with foreground sessions, inflating &lt;code&gt;gc current block 2-way&lt;/code&gt;, &lt;code&gt;gc current block 3-way&lt;/code&gt;, &lt;code&gt;gc cr block 2-way&lt;/code&gt;, and &lt;code&gt;gc buffer busy acquire&lt;/code&gt; wait events in Active Session History (ASH).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Monotonic Sequence and Index Leaf Hotspots:&lt;/strong&gt; Default Oracle sequences created with &lt;code&gt;ORDER&lt;/code&gt; or small &lt;code&gt;CACHE&lt;/code&gt; sizes serialize across instances on &lt;code&gt;enq: SQ - contention&lt;/code&gt; and &lt;code&gt;DFS lock handle&lt;/code&gt;. Worse, monotonic primary keys and timestamp indexes insert every new row into the exact same right-hand B-Tree leaf block. All cluster instances fight for exclusive ownership of that single leaf block, causing &lt;code&gt;enq: TX - index contention&lt;/code&gt; alongside &lt;code&gt;gc current block busy&lt;/code&gt; waits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ineffectiveness of Local Storage Tuning:&lt;/strong&gt; Raising &lt;code&gt;PCTFREE&lt;/code&gt; to spread rows across more blocks or increasing &lt;code&gt;DB_CACHE_SIZE&lt;/code&gt; does not stop two instances from modifying the same logical tenant’s rows or the same right-hand index leaf. As long as routing is uncoordinated, adding RAC nodes increases the probability that the next transaction for a given entity lands on a remote instance.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;How do platform architects eliminate cross-node Cache Fusion ping-ponging across a RAC cluster without rewriting relational schemas or abandoning shared-storage high availability?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;When AWR and ASH reports show &lt;code&gt;gc buffer busy acquire&lt;/code&gt;, &lt;code&gt;gc current block busy&lt;/code&gt;, or &lt;code&gt;enq: TX - index contention&lt;/code&gt; dominating database time, teams evaluate four architectural responses.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-03-15-oracle-rac-hot-blocks-gc-waits-sequences-3-options.svg&quot; alt=&quot;Comparison of four architectural options for Oracle RAC interconnect contention&quot;&gt;&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Solves&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Database-Level Tuning&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Raise &lt;code&gt;PCTFREE&lt;/code&gt;, increase sequence &lt;code&gt;CACHE&lt;/code&gt;, add &lt;code&gt;LMS&lt;/code&gt; processes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reduces row density per block and sequence metadata waits&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Does not stop concurrent cross-node DML on the same logical entity or index leaf&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt; as primary fix; sequence and index fixes kept as prerequisites&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Active Data Guard Offload&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Route read-only reporting queries to a physical standby&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Isolates analytical table scans from OLTP SGA caches and &lt;code&gt;gc cr&lt;/code&gt; waits&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Does nothing for OLTP write-write contention on the primary cluster&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Partial&lt;/strong&gt;; adopted only for reporting isolation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Cell-Based Application Affinity&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Hash tenant/entity ID at router to pinned pools and single-preferred-instance services&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keeps an entity’s working set in one instance’s SGA while retaining ASM failover&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Introduces hot-tenant skew and cold-cache failover behavior&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen&lt;/strong&gt; for OLTP workloads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Oracle Sharding&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Split data into independent databases with a shard catalog&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Eliminates shared cache coherency completely across shards&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires shard-key schema redesign and restricts cross-shard joins&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Deferred&lt;/strong&gt; until single-cluster storage or redo limits are hit&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Database-level tuning fails because &lt;code&gt;PCTFREE&lt;/code&gt; only reduces the chance that &lt;em&gt;unrelated&lt;/em&gt; rows share an 8 KB block; it cannot prevent two transactions for the same customer or order from colliding across instances, nor does it stop right-hand B-Tree leaf contention on monotonic keys unless the index itself is altered. Active Data Guard is necessary to keep reporting scans from evicting OLTP buffers and generating &lt;code&gt;gc cr&lt;/code&gt; transfers, but standby replicas are read-only and cannot absorb OLTP DML. Oracle Sharding provides true shared-nothing isolation, yet migrating an existing monolithic schema to sharded tables and rewritten cross-shard queries is a multi-quarter effort. Cell-Based Application Affinity delivers SGA locality with zero table schema changes and preserves ASM-backed failover.&lt;/p&gt;
&lt;h2 id=&quot;the-cell-based-application-affinity-architecture&quot;&gt;The Cell-Based Application Affinity Architecture&lt;/h2&gt;
&lt;p&gt;Cell-Based Application Affinity decouples cluster compute capacity from homogeneous load balancing. Every RAC instance still mounts the same ASM diskgroups so any node can take over during an outage, but steady-state read/write traffic for a given data domain (tenant, geographic region, or account hash bucket) is deterministically routed to a designated home instance.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-03-15-oracle-rac-hot-blocks-gc-waits-sequences-4-architecture.svg&quot; alt=&quot;Layered cell-based application affinity architecture from ingress router to shared ASM storage&quot;&gt;&lt;/p&gt;
&lt;p&gt;The design relies on three coordinated layers:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Deterministic Hash Routing at the Application Tier:&lt;/strong&gt; The API gateway or data-access layer computes a deterministic hash over the partition key—typically &lt;code&gt;tenant_id&lt;/code&gt; or &lt;code&gt;account_id&lt;/code&gt;—and maps it to a logical shard (&lt;code&gt;SHARD_ALPHA&lt;/code&gt;, &lt;code&gt;SHARD_BETA&lt;/code&gt;, etc.). All requests for that key borrow connections from the pool bound to that shard.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Single-Preferred-Instance Oracle Services:&lt;/strong&gt; Instead of one cluster-wide service, administrators create one Oracle service per shard via &lt;code&gt;srvctl&lt;/code&gt;, assigning a single instance as &lt;code&gt;-preferred&lt;/code&gt; and a peer instance as &lt;code&gt;-available&lt;/code&gt;. Under normal operation, the service runs only on its preferred instance, guaranteeing that all blocks for that shard’s tenants stay hot in one SGA.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sequence and Index Dispersion:&lt;/strong&gt; Even with tenant affinity, globally shared sequences and monotonic global indexes can still span shards. Sequences must use &lt;code&gt;CACHE ... NOORDER&lt;/code&gt; (or application-side UUIDv7/Snowflake IDs), and global monotonic indexes must be converted to &lt;code&gt;REVERSE&lt;/code&gt; (for equality lookups) or &lt;code&gt;GLOBAL PARTITION BY HASH&lt;/code&gt; (for range scans).&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;1-provisioning-dedicated-affinity-services-srvctl&quot;&gt;1. Provisioning Dedicated Affinity Services (&lt;code&gt;srvctl&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Create separate services for each shard with Application Continuity (&lt;code&gt;-failovertype TRANSACTION&lt;/code&gt; and &lt;code&gt;-commit_outcome TRUE&lt;/code&gt; for Transaction Guard) so in-flight work can drain or replay cleanly during planned maintenance or node failure:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Shard Alpha: Instance 1 Preferred, Instance 2 Available&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; service&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -db&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODRAC&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -service&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; svc_shard_alpha&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -preferred&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODRAC1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -available&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODRAC2&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failovertype&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; TRANSACTION&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failover_restore&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; LEVEL1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failoverretry&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 30&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failoverdelay&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -commit_outcome&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; TRUE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Shard Beta: Instance 2 Preferred, Instance 1 Available&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; service&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -db&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODRAC&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -service&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; svc_shard_beta&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -preferred&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODRAC2&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -available&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODRAC1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failovertype&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; TRANSACTION&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failover_restore&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; LEVEL1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failoverretry&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 30&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failoverdelay&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -commit_outcome&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; TRUE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; start&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; service&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -db&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODRAC&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -service&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; svc_shard_alpha&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; start&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; service&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -db&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODRAC&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -service&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; svc_shard_beta&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-eliminating-sequence-and-index-leaf-contention&quot;&gt;2. Eliminating Sequence and Index Leaf Contention&lt;/h3&gt;
&lt;p&gt;Remove cross-instance sequence ordering and disperse right-hand index leaf inserts across multiple blocks:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Preallocate 10,000 sequence values per instance SGA; NOORDER avoids cross-node sync&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SEQUENCE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; payments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;order_seq&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CACHE &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; NOORDER;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SEQUENCE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; payments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;txn_seq&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CACHE &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; NOORDER;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Disperse monotonic primary-key inserts when only equality lookups (=) are used&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; payments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pk_orders&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; REBUILD&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; REVERSE;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Use hash-partitioned global indexes when range scans (BETWEEN, &gt;, &amp;#x3C;) are required&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; payments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.idx_order_ts_hash &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; payments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(order_timestamp)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  GLOBAL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; PARTITION&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; BY&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; HASH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(order_timestamp)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  PARTITIONS &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;16&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  TABLESPACE USERS_INDEX;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-application-connection-pool-routing-java--hikaricp&quot;&gt;3. Application Connection Pool Routing (Java / HikariCP)&lt;/h3&gt;
&lt;p&gt;Maintain isolated connection pools per shard service so tenant traffic never leaks onto the wrong instance. Use explicit unit suffixes (&lt;code&gt;ms&lt;/code&gt;) on Oracle JDBC timeout properties (supported since Oracle JDBC 18.1+) to prevent seconds-versus-milliseconds misconfiguration:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;java&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ShardedConnectionManager&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; final&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Map&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;HikariDataSource&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;&gt; dataSources &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ConcurrentHashMap&amp;#x3C;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    static&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        dataSources.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;put&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SHARD_ALPHA&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;createPool&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &quot;jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=rac-scan.corp.internal)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=svc_shard_alpha)))&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &quot;pool-alpha&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;40&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        ));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        dataSources.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;put&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SHARD_BETA&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;createPool&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &quot;jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=rac-scan.corp.internal)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=svc_shard_beta)))&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &quot;pool-beta&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;40&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        ));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    private&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; HikariDataSource &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;createPool&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(String &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;jdbcUrl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, String &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;poolName&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; maxPoolSize&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        HikariConfig config &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; HikariConfig&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        config.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;setJdbcUrl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(jdbcUrl);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        config.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;setUsername&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;app_user&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        config.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;setPassword&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(System.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getenv&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;DB_CREDENTIAL&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        config.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;setMaximumPoolSize&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(maxPoolSize);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        config.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;setMinimumIdle&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        config.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;setPoolName&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(poolName);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        config.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;addDataSourceProperty&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;oracle.jdbc.ReadTimeout&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;5000ms&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        config.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;addDataSourceProperty&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;oracle.net.CONNECT_TIMEOUT&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;3000ms&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; HikariDataSource&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(config);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    public&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Connection &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getConnectionForTenant&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(String &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;tenantId&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;throws&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SQLException {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        int&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; bucket &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Math.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;floorMod&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(tenantId.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;hashCode&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        String targetShard &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (bucket &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;SHARD_ALPHA&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;SHARD_BETA&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dataSources.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(targetShard).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getConnection&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note the use of &lt;code&gt;Math.floorMod&lt;/code&gt; instead of &lt;code&gt;Math.abs(hashCode()) % 2&lt;/code&gt;: in Java, &lt;code&gt;Math.abs(Integer.MIN_VALUE)&lt;/code&gt; remains negative, which would break modulo indexing on edge-case tenant IDs.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Migrating an active RAC cluster from a generic service to cell-based affinity must happen in four reversible phases so a misconfigured pool or index never causes an outage.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-03-15-oracle-rac-hot-blocks-gc-waits-sequences-5-flow.svg&quot; alt=&quot;Four-phase rollout from sequence and index fixes to service provisioning, app routing, and read offload&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Sequence and Global Index Dispersion:&lt;/strong&gt; Alter hot sequences to &lt;code&gt;CACHE 10000 NOORDER&lt;/code&gt; and rebuild monotonic indexes as &lt;code&gt;GLOBAL PARTITION BY HASH&lt;/code&gt; (or &lt;code&gt;REVERSE&lt;/code&gt; only after auditing SQL plans for range scans).
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Validation gate:&lt;/em&gt; Verify &lt;code&gt;enq: SQ - contention&lt;/code&gt; and &lt;code&gt;enq: TX - index contention&lt;/code&gt; drop in &lt;code&gt;V$SYSTEM_EVENT&lt;/code&gt; / ASH without regressions in range-scan SQL elapsed times.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Rollback:&lt;/em&gt; Revert sequence attributes with &lt;code&gt;ALTER SEQUENCE&lt;/code&gt; or rebuild the index without &lt;code&gt;REVERSE&lt;/code&gt; / hash partitioning (&lt;code&gt;ONLINE&lt;/code&gt; rebuilds avoid table locks).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Dedicated Service Provisioning:&lt;/strong&gt; Create &lt;code&gt;svc_shard_alpha&lt;/code&gt; and &lt;code&gt;svc_shard_beta&lt;/code&gt; via &lt;code&gt;srvctl&lt;/code&gt; with one &lt;code&gt;-preferred&lt;/code&gt; instance and one &lt;code&gt;-available&lt;/code&gt; instance each, leaving the legacy generic service running.
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Validation gate:&lt;/em&gt; Confirm &lt;code&gt;srvctl status service -db PRODRAC&lt;/code&gt; shows each service active exclusively on its intended preferred node, and verify SCAN listener registration via &lt;code&gt;lsnrctl status&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Rollback:&lt;/em&gt; Stop and remove the new services with &lt;code&gt;srvctl stop service&lt;/code&gt; and &lt;code&gt;srvctl remove service&lt;/code&gt;; existing traffic is unaffected.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Feature-Flagged Application Routing:&lt;/strong&gt; Deploy the dual-pool &lt;code&gt;ShardedConnectionManager&lt;/code&gt; behind a runtime feature flag. Shift traffic incrementally (for example, 10% of tenants, then 50%, then 100%) from the generic pool to the tenant-hashed pools.
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Validation gate:&lt;/em&gt; Monitor ASH for &lt;code&gt;gc buffer busy acquire&lt;/code&gt;, &lt;code&gt;gc current block 2-way&lt;/code&gt;, and &lt;code&gt;gc cr block 2-way&lt;/code&gt; waits alongside &lt;code&gt;GV$INSTANCE_PING&lt;/code&gt; (&lt;code&gt;AVERAGE_8K&lt;/code&gt; and &lt;code&gt;CURRENT_8K&lt;/code&gt; microsecond latency). Confirm each instance’s CPU and buffer cache hit ratio remain balanced.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Rollback:&lt;/em&gt; Flip the feature flag back to the generic service pool; connections drain without database DDL changes.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Cross-Tenant Reporting Offload:&lt;/strong&gt; Repoint batch analytics and cross-tenant reporting jobs to an Active Data Guard physical standby service.
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Validation gate:&lt;/em&gt; Confirm &lt;code&gt;gc cr&lt;/code&gt; block transfers triggered by full-table scans disappear from the primary RAC AWR report.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Rollback:&lt;/em&gt; Repoint the reporting TNS alias back to a dedicated primary service if standby lag exceeds SLA.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Based on how Oracle RAC’s Cache Fusion, Global Cache Service, and Clusterware service placement operate:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cache Fusion is strictly block-oriented:&lt;/strong&gt; Oracle’s GCS tracks ownership at the 8 KB data block level, not the row level. When two instances modify different rows that happen to reside in the same data block—or different keys in the same B-Tree index leaf—&lt;code&gt;LMS&lt;/code&gt; must still ship the 8 KB block and maintain Past Images across the interconnect. Pinning all writes for a given tenant to one preferred instance ensures that both the table blocks and the tenant’s working set stay local to one SGA.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Service relocation uses FAN draining:&lt;/strong&gt; When an operator balances load across nodes using &lt;code&gt;srvctl relocate service -db PRODRAC -service svc_shard_alpha -oldinst PRODRAC1 -newinst PRODRAC2 -drain_timeout 60 -stopoption IMMEDIATE&lt;/code&gt;, Clusterware emits a Fast Application Notification (FAN) event. FAN-aware pools (such as Oracle UCP) stop routing new borrows to Instance 1 and allow in-flight transactions up to 60 seconds to commit before terminating stragglers. Standard pools without FAN support (such as default HikariCP without ONS integration) only discover the move on connection failure or max-lifetime expiry, making explicit &lt;code&gt;-drain_timeout&lt;/code&gt; bounds essential to prevent hung relocations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interconnect health is measured in &lt;code&gt;GV$INSTANCE_PING&lt;/code&gt;:&lt;/strong&gt; Oracle’s background &lt;code&gt;PING&lt;/code&gt; process periodically probes every peer instance with 500-byte and 8 KB messages and records round-trip latency in microseconds in &lt;code&gt;GV$INSTANCE_PING&lt;/code&gt; (&lt;code&gt;CURRENT_500B&lt;/code&gt;, &lt;code&gt;AVERAGE_500B&lt;/code&gt;, &lt;code&gt;CURRENT_8K&lt;/code&gt;, &lt;code&gt;AVERAGE_8K&lt;/code&gt;). Interface binding and private-vs-public classification are verified in &lt;code&gt;GV$CLUSTER_INTERCONNECTS&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Pinning workloads eliminates steady-state Cache Fusion contention, but it concentrates risk around skew, failover, and index semantics.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-03-15-oracle-rac-hot-blocks-gc-waits-sequences-6-failures.svg&quot; alt=&quot;Three failure paths of cell-based affinity: hot tenant skew, cold cache failover, and abrupt failback churn&quot;&gt;&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks how&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Hot tenant skew&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;One high-volume tenant saturates its preferred node’s CPU while peer instances sit underutilized.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Per-instance CPU utilization and ASH active session imbalance across &lt;code&gt;GV$ACTIVE_SESSION_HISTORY&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Split the hot tenant into its own dedicated service and relocate it with &lt;code&gt;srvctl relocate service -drain_timeout&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unplanned node failover&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Service shifts to its &lt;code&gt;-available&lt;/code&gt; peer whose SGA has none of that shard’s blocks cached, causing a burst of physical reads.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Spike in &lt;code&gt;db file sequential read&lt;/code&gt; and &lt;code&gt;db file scattered read&lt;/code&gt; waits immediately after failover.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Size ASM storage IOPS for cold-cache bursts; enable Application Continuity (&lt;code&gt;-failovertype TRANSACTION&lt;/code&gt;) to replay interrupted work.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Abrupt failback on reboot&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Recovering instance pulls its preferred service back immediately, severing active connections on the surviving node.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sudden surge in connection errors and &lt;code&gt;ORA-03113&lt;/code&gt; / socket resets after node restart.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep failback manual or drain gradually with &lt;code&gt;-drain_timeout&lt;/code&gt; during an off-peak maintenance window.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Range scan on &lt;code&gt;REVERSE&lt;/code&gt; index&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Queries using &lt;code&gt;BETWEEN&lt;/code&gt;, &lt;code&gt;&gt;&lt;/code&gt;, or &lt;code&gt;&amp;#x3C;&lt;/code&gt; cannot use a &lt;code&gt;REVERSE&lt;/code&gt; index range scan and fall back to full table scans.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Plan regression in &lt;code&gt;V$SQL_PLAN&lt;/code&gt; showing &lt;code&gt;TABLE ACCESS FULL&lt;/code&gt; on indexed columns.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Restrict &lt;code&gt;REVERSE&lt;/code&gt; to equality-only PKs; use &lt;code&gt;GLOBAL PARTITION BY HASH&lt;/code&gt; for range-scanned columns.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Cross-shard reporting on primary&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Multi-tenant analytical queries scan blocks owned by multiple instances, triggering &lt;code&gt;gc cr&lt;/code&gt; block shipping and evicting OLTP buffers.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rise in &lt;code&gt;gc cr multi block request&lt;/code&gt; and buffer cache misses during batch windows.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Route all cross-tenant reporting and BI queries to an Active Data Guard standby service.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Sequence cache loss on crash&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Instance crash discards up to &lt;code&gt;CACHE&lt;/code&gt; (e.g., 10,000) preallocated sequence values held in the local SGA.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Gaps in primary key sequence IDs after an instance restart.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Treat database sequences strictly as uniqueness generators; never rely on them for gap-free audit numbering.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cache Fusion obeys network physics, not SQL tuning:&lt;/strong&gt; Adding RAC nodes under round-robin routing increases cross-instance block ownership transfers; only deterministic routing at the application or service tier keeps hot blocks in a single SGA.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Single-preferred-instance services preserve shared-storage HA:&lt;/strong&gt; Defining one &lt;code&gt;-preferred&lt;/code&gt; and one &lt;code&gt;-available&lt;/code&gt; instance per service with &lt;code&gt;srvctl&lt;/code&gt; eliminates steady-state interconnect ping-ponging while keeping automatic ASM-backed failover intact.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sequences and monotonic indexes are shared-cluster traps:&lt;/strong&gt; Even with tenant-pinned services, &lt;code&gt;ORDER&lt;/code&gt; sequences and unpartitioned ascending indexes reintroduce cluster-wide contention unless converted to &lt;code&gt;CACHE ... NOORDER&lt;/code&gt;, &lt;code&gt;REVERSE&lt;/code&gt; (equality only), or &lt;code&gt;GLOBAL PARTITION BY HASH&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Affinity trades interconnect waits for skew and cold-cache risk:&lt;/strong&gt; Plan for hot-tenant service splitting, cold-SGA IOPS headroom after failover, and FAN-drained relocations (&lt;code&gt;-drain_timeout&lt;/code&gt;) before cutting over production traffic.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Measure interconnect health with the right views:&lt;/strong&gt; Use &lt;code&gt;GV$CLUSTER_INTERCONNECTS&lt;/code&gt; to verify private network binding and &lt;code&gt;GV$INSTANCE_PING&lt;/code&gt; (500B and 8 KB microsecond latencies) alongside ASH &lt;code&gt;gc&lt;/code&gt; wait events to validate every rollout phase.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Round-robin connection routing across Oracle RAC turns local row mutations into cross-node 8 KB block transfers, saturating &lt;code&gt;LMS&lt;/code&gt; processes and the private interconnect.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Hash tenant or account keys at the application connection-pool tier to dedicated single-preferred-instance Oracle services, backed by &lt;code&gt;CACHE ... NOORDER&lt;/code&gt; sequences and hash-partitioned or reverse-key indexes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; In ASH and &lt;code&gt;V$SYSTEM_EVENT&lt;/code&gt;, &lt;code&gt;gc buffer busy acquire&lt;/code&gt;, &lt;code&gt;gc current block 2-way&lt;/code&gt;, &lt;code&gt;gc cr block 2-way&lt;/code&gt;, and &lt;code&gt;enq: SQ - contention&lt;/code&gt; drop sharply after Phase 1 and Phase 3 cutover while &lt;code&gt;GV$INSTANCE_PING&lt;/code&gt; latency stabilizes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Query &lt;code&gt;GV$ACTIVE_SESSION_HISTORY&lt;/code&gt; and your latest AWR “Top 10 Foreground Events” section this week; if &lt;code&gt;gc&lt;/code&gt; waits and &lt;code&gt;enq: TX - index contention&lt;/code&gt; sit near the top, audit your &lt;code&gt;srvctl&lt;/code&gt; service topology and introduce a tenant-keyed pool behind a feature flag.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>Policy as Code at Hyper-Scale: Preventing Destructive Database Operations in CI/CD</title><link>https://rajivonai.com/blog/2024-02-25-oracle-infrastructure-cicd-terraform-policy/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-02-25-oracle-infrastructure-cicd-terraform-policy/</guid><description>Why human pull request reviews fail to catch database destruction in massive Infrastructure as Code pipelines, and how to build Open Policy Agent (OPA) gates to mathematically guarantee database safety.</description><pubDate>Wed, 03 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Relying on human code reviews to parse complex Infrastructure as Code execution plans guarantees that a production database will eventually be destroyed by an accidental resource replacement.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Human PR reviewers inspect HCL diffs rather than multi-thousand-line &lt;code&gt;terraform plan&lt;/code&gt; outputs, missing &lt;code&gt;ForceNew&lt;/code&gt; replacements (&lt;code&gt;-/+&lt;/code&gt;), unmapped module renames, and &lt;code&gt;0.0.0.0/0&lt;/code&gt; listener exposures, while &lt;code&gt;lifecycle { prevent_destroy = true }&lt;/code&gt; disappears whenever a resource block is deleted.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Serialize speculative plans with &lt;code&gt;tofu show -json&lt;/code&gt; (or &lt;code&gt;terraform show -json&lt;/code&gt;) and gate every PR through Open Policy Agent (OPA) Rego rules that block unauthorized deletes, public listener CIDRs, and missing customer-managed KMS keys before human review.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Monolithic state files bloat plan JSON evaluation times, self-approved decommission tags bypass safety checks unless paired with &lt;code&gt;CODEOWNERS&lt;/code&gt;, and OpenTofu 1.7 state encryption must be paired with external OPA evaluation rather than assuming an embedded policy engine.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2024-02-25-oracle-infrastructure-cicd-terraform-policy.svg&quot; alt=&quot;Evaluating compiled Terraform plan JSON with OPA before human review&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Platform engineering teams manage cloud database fleets—across Oracle Cloud Infrastructure (OCI), Amazon Web Services (AWS), and Microsoft Azure—using declarative Infrastructure as Code (IaC) engines such as Terraform and OpenTofu. Daily pull requests (PRs) resize compute shapes, expand block volumes, adjust subnet routes, update parameter groups, and refactor shared modules.&lt;/p&gt;
&lt;p&gt;A standard database IaC pipeline follows four steps: a developer edits HCL and opens a PR, the CI runner executes a speculative &lt;code&gt;plan&lt;/code&gt;, a peer reviewer inspects the diff, and merge triggers &lt;code&gt;apply&lt;/code&gt; against cloud provider APIs. When a state file grows to hundreds or thousands of managed resources, a single refactoring PR generates thousands of lines of plan output. Human reviewers naturally focus on the readable HCL diff—variable names, tags, and module structure—rather than auditing every attribute change inside the compiled execution plan.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Human code review breaks down against four structural constraints in declarative IaC engines:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Constraint 1 — Plan-Parsing Fatigue vs. &lt;code&gt;ForceNew&lt;/code&gt; Replacement:&lt;/strong&gt; Cloud database resources (&lt;code&gt;oci_database_db_system&lt;/code&gt;, &lt;code&gt;oci_database_cloud_vm_cluster&lt;/code&gt;, &lt;code&gt;aws_db_instance&lt;/code&gt;, &lt;code&gt;azurerm_oracle_cloud_vm_cluster&lt;/code&gt;) treat core attributes—such as availability domain, subnet or VCN placement, or certain KMS key bindings—as immutable (&lt;code&gt;ForceNew&lt;/code&gt;). Modifying one of those attributes in HCL does not trigger an in-place update; Terraform plans a destroy-and-recreate (&lt;code&gt;-/+&lt;/code&gt;), represented in the compiled plan JSON as &lt;code&gt;change.actions&lt;/code&gt; containing &lt;code&gt;[&quot;delete&quot;, &quot;create&quot;]&lt;/code&gt; (or &lt;code&gt;[&quot;create&quot;, &quot;delete&quot;]&lt;/code&gt; when &lt;code&gt;create_before_destroy&lt;/code&gt; is set). At a careful human reading pace of roughly 50 lines per minute, auditing a 3,000-line plan output takes 60 minutes of uninterrupted attention per PR—an unsustainable tax when teams merge dozens of infrastructure PRs per week.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 2 — State Identity Breaks on Module Refactors:&lt;/strong&gt; Renaming a module or resource address (for example, &lt;code&gt;module.core_db.oci_database_db_system.primary&lt;/code&gt; to &lt;code&gt;module.core_db.oci_database_db_system.production&lt;/code&gt;) without a Terraform 1.1+ &lt;code&gt;moved&lt;/code&gt; block severs the state-address mapping. In the state graph, the old address disappears and the new address has no prior state, so the engine schedules an immediate &lt;code&gt;delete&lt;/code&gt; of the live production database and a &lt;code&gt;create&lt;/code&gt; of an empty replacement.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 3 — &lt;code&gt;lifecycle { prevent_destroy = true }&lt;/code&gt; Is Self-Defeating:&lt;/strong&gt; Teams often rely on &lt;code&gt;prevent_destroy = true&lt;/code&gt; inside resource blocks. However, if a developer deletes or replaces the surrounding module block during a refactor, the &lt;code&gt;lifecycle&lt;/code&gt; block is deleted along with it. Worse, &lt;code&gt;prevent_destroy&lt;/code&gt; only triggers at &lt;code&gt;apply&lt;/code&gt; time—often after preceding dependencies have already been mutated—and inspects nothing about network ingress CIDRs (&lt;code&gt;0.0.0.0/0&lt;/code&gt;) or customer-managed encryption keys.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 4 — Unresolved Variables in HCL Source:&lt;/strong&gt; Raw HCL files hide computed values behind &lt;code&gt;locals&lt;/code&gt;, &lt;code&gt;for_each&lt;/code&gt; maps, &lt;code&gt;dynamic&lt;/code&gt; blocks, and module outputs. Static source-code grep checks (and even AST-only HCL linters) cannot reliably determine whether a dynamically constructed NSG rule opens TCP port 1521 to the public internet, nor can they see whether a provider marks an attribute change as &lt;code&gt;ForceNew&lt;/code&gt; in the current provider schema.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;./2024-02-25-oracle-infrastructure-cicd-terraform-policy-2-constraints.svg&quot; alt=&quot;Declarative state constraints showing ForceNew replacement, missing moved blocks, and prevent_destroy deletion&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do platform teams build deterministic CI/CD gates that inspect resolved infrastructure mutations before merge, blocking accidental deletions, public database exposure, and unencrypted storage?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams typically evaluate four governance mechanisms before adopting plan-JSON Policy as Code.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Governance Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Catches &lt;code&gt;ForceNew&lt;/code&gt; / Refactors&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Evaluates Computed CIDRs &amp;#x26; KMS&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Survives Block Deletion&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Why Rejected / Chosen&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Human PR Review&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Poor — fatigue on large plans&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Poor — variables hide values&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Yes (if noticed)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails under volume; misses buried &lt;code&gt;-/+&lt;/code&gt; actions.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;HCL Regex / Grep Linters&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails — blind to state graph&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails — cannot resolve locals&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;No&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High false-positive rate; ignores provider &lt;code&gt;ForceNew&lt;/code&gt; logic.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;lifecycle { prevent_destroy }&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Partial — fails at apply time&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails — no network/KMS checks&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;No&lt;/strong&gt; — deleted with block&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Easily bypassed by deleting the module or resource block.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Plan-JSON + OPA Gate (Chosen)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;100% deterministic on plan&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Full resolved attribute check&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Yes&lt;/strong&gt; — external to HCL&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Evaluates exact provider actions at PR time before review.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2024-02-25-oracle-infrastructure-cicd-terraform-policy-3-options.svg&quot; alt=&quot;Comparison of human review, HCL regex linters, prevent_destroy, and Plan-JSON OPA gates&quot;&gt;&lt;/p&gt;
&lt;p&gt;Human review alone was rejected because plan volume scales faster than reviewer attention. HCL regex linters and static AST scanners (such as &lt;code&gt;tflint&lt;/code&gt; or &lt;code&gt;tfsec&lt;/code&gt; running without a plan) were rejected as the primary gate because they cannot resolve &lt;code&gt;for_each&lt;/code&gt; expressions, cross-module outputs, state-graph diffs, or provider-level &lt;code&gt;ForceNew&lt;/code&gt; semantics. Native &lt;code&gt;prevent_destroy&lt;/code&gt; blocks were rejected as a sole control because they vanish when a resource block is removed, fail only during &lt;code&gt;apply&lt;/code&gt; rather than PR review, and offer zero protection against opening port 1521 to &lt;code&gt;0.0.0.0/0&lt;/code&gt;. Evaluating the compiled &lt;code&gt;tfplan.json&lt;/code&gt; with Open Policy Agent (OPA) inspects the exact state transitions the provider will execute, including &lt;code&gt;prior_state&lt;/code&gt;, &lt;code&gt;planned_values&lt;/code&gt;, and &lt;code&gt;resource_changes&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-policy-as-code-pipeline-architecture&quot;&gt;The Policy-as-Code Pipeline Architecture&lt;/h2&gt;
&lt;p&gt;The target architecture decouples policy enforcement from application Terraform repositories by evaluating serialized plan JSON (&lt;code&gt;tofu show -json tfplan.binary&lt;/code&gt;) against a pinned, versioned Rego policy bundle in CI before peer review begins.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-02-25-oracle-infrastructure-cicd-terraform-policy-4-architecture.svg&quot; alt=&quot;Policy-as-Code CI architecture decoupling signed Rego bundles from database Terraform repositories&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;architectural-policy-pillars&quot;&gt;Architectural Policy Pillars&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Deterministic Plan-JSON Inspection:&lt;/strong&gt; OPA evaluates &lt;code&gt;input.resource_changes&lt;/code&gt; in the compiled plan JSON rather than raw HCL. Because Terraform and OpenTofu resolve all variable interpolations, &lt;code&gt;moved&lt;/code&gt; blocks, and provider replacement flags during &lt;code&gt;plan&lt;/code&gt;, Rego sees the exact &lt;code&gt;actions&lt;/code&gt; array (&lt;code&gt;[&quot;delete&quot;]&lt;/code&gt;, &lt;code&gt;[&quot;delete&quot;, &quot;create&quot;]&lt;/code&gt;, or &lt;code&gt;[&quot;create&quot;, &quot;delete&quot;]&lt;/code&gt;) and &lt;code&gt;change.after&lt;/code&gt; attributes. Checking &lt;code&gt;&quot;delete&quot; in resource.change.actions&lt;/code&gt; catches both pure deletions and both replacement orderings (&lt;code&gt;create_before_destroy&lt;/code&gt; on or off).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hierarchical Blast-Radius Guardrails:&lt;/strong&gt; Policies separate benign in-place updates (such as freeform tag edits or storage scaling, where &lt;code&gt;actions == [&quot;update&quot;]&lt;/code&gt;) from destructive &lt;code&gt;delete&lt;/code&gt; and &lt;code&gt;replace&lt;/code&gt; actions on stateful database primitives (&lt;code&gt;oci_database_db_system&lt;/code&gt;, &lt;code&gt;oci_database_cloud_vm_cluster&lt;/code&gt;, &lt;code&gt;oci_database_autonomous_database&lt;/code&gt;, &lt;code&gt;aws_db_instance&lt;/code&gt;, &lt;code&gt;aws_rds_cluster&lt;/code&gt;, &lt;code&gt;azurerm_oracle_cloud_vm_cluster&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Network Zero-Exposure Enforcement:&lt;/strong&gt; Policies inspect all security rules (&lt;code&gt;oci_core_network_security_group_security_rule&lt;/code&gt;, &lt;code&gt;aws_security_group_rule&lt;/code&gt;, &lt;code&gt;aws_vpc_security_group_ingress_rule&lt;/code&gt;) and deny any &lt;code&gt;INGRESS&lt;/code&gt; rule with &lt;code&gt;source == &quot;0.0.0.0/0&quot;&lt;/code&gt; or &lt;code&gt;&quot;::/0&quot;&lt;/code&gt; overlapping Oracle listener ports &lt;code&gt;1521&lt;/code&gt; (Oracle Net), &lt;code&gt;2484&lt;/code&gt; (TCPS), or &lt;code&gt;5500&lt;/code&gt; (EM Express), as well as wildcard protocol rules (&lt;code&gt;protocol == &quot;all&quot;&lt;/code&gt; or &lt;code&gt;&quot;6&quot;&lt;/code&gt; with port range &lt;code&gt;1&lt;/code&gt;–&lt;code&gt;65535&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mandatory Customer-Managed Encryption (CMEK) and &lt;code&gt;after_unknown&lt;/code&gt; Handling:&lt;/strong&gt; Policies verify that &lt;code&gt;kms_key_id&lt;/code&gt; is non-empty on protected database and storage resources, rejecting null or provider-default encryption keys. Crucially, when a KMS key is provisioned in the same &lt;code&gt;plan&lt;/code&gt; as the database, &lt;code&gt;change.after.kms_key_id&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt; in &lt;code&gt;tfplan.json&lt;/code&gt; while &lt;code&gt;change.after_unknown.kms_key_id&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;. Rego rules must explicitly check &lt;code&gt;after_unknown&lt;/code&gt; or trace &lt;code&gt;configuration.root_module&lt;/code&gt; references so new-environment bootstraps do not trigger false denials while still blocking unencrypted builds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Micro-State Decomposition:&lt;/strong&gt; To keep &lt;code&gt;tfplan.json&lt;/code&gt; small and OPA evaluation sub-second, database state files are partitioned by environment and tier (network, IAM, database cluster) rather than stored in a single monolithic state. Smaller state files also limit the blast radius of state-lock contention and isolate &lt;code&gt;plan&lt;/code&gt; IAM permissions per tier.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;production-rego-policies-and-github-actions-workflow&quot;&gt;Production Rego Policies and GitHub Actions Workflow&lt;/h3&gt;
&lt;p&gt;The anti-destruction policy (&lt;code&gt;database_safety.rego&lt;/code&gt;) inspects &lt;code&gt;resource_changes&lt;/code&gt; for any &lt;code&gt;&quot;delete&quot;&lt;/code&gt; action on protected database types and requires explicit decommissioning tags gated by &lt;code&gt;CODEOWNERS&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;package terraform.database.safety&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import future.keywords.in&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;protected_database_types := {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;oci_database_db_system&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;oci_database_cloud_vm_cluster&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;oci_database_autonomous_database&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;aws_db_instance&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;aws_rds_cluster&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;azurerm_oracle_cloud_vm_cluster&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;deny[msg] {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    some resource in input.resource_changes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    resource.type in protected_database_types&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    &quot;delete&quot; in resource.change.actions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    not is_authorized_decommission(resource)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    msg := sprintf(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &quot;CRITICAL GOVERNANCE VIOLATION: &apos;%v&apos; (%v) plans destruction (%v) without decommission approval.&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        [resource.address, resource.type, resource.change.actions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;is_authorized_decommission(resource) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    after := object.get(resource.change, &quot;after&quot;, {})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    tags := object.get(after, &quot;freeform_tags&quot;, {})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    tags[&quot;decommission_approved&quot;] == &quot;true&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    object.get(tags, &quot;decommission_ticket_id&quot;, &quot;&quot;) != &quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The network isolation policy (&lt;code&gt;network_security.rego&lt;/code&gt;) checks OCI NSG rules for &lt;code&gt;0.0.0.0/0&lt;/code&gt; ingress overlapping database listener ports:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;package terraform.database.security&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;import future.keywords.in&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;database_listener_ports := {1521, 2484, 5500}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;deny[msg] {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    some resource in input.resource_changes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    resource.type == &quot;oci_core_network_security_group_security_rule&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    after := object.get(resource.change, &quot;after&quot;, {})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    after.direction == &quot;INGRESS&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    after.source_type == &quot;CIDR_BLOCK&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    after.source == &quot;0.0.0.0/0&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    tcp_options := after.tcp_options[_]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    port_range := tcp_options.destination_port_range[_]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    some port in database_listener_ports&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    port &gt;= port_range.min&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    port &amp;#x3C;= port_range.max&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    msg := sprintf(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        &quot;SECURITY VIOLATION: NSG rule &apos;%v&apos; exposes database port %v to 0.0.0.0/0.&quot;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        [resource.address, port]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The GitHub Actions workflow generates &lt;code&gt;tfplan.json&lt;/code&gt;, evaluates both packages, and posts violations back to the PR:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Database IaC Policy Verification&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;on&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  pull_request&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    paths&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;terraform/databases/**&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;jobs&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  policy-evaluation&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    runs-on&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;ubuntu-latest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    permissions&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      contents&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;read&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      pull-requests&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;write&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    steps&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;uses&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;actions/checkout@v4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;uses&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;opentofu/setup-opentofu@v1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        with&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          tofu_version&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1.7.2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;uses&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;open-policy-agent/setup-opa@v2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        with&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          version&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0.62.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;working-directory&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;terraform/databases&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        run&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          tofu init -backend=false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          tofu plan -out=tfplan.binary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          tofu show -json tfplan.binary &gt; tfplan.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          opa eval --data ../../policies/ --input tfplan.json --format pretty \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &quot;data.terraform.database.safety.deny&quot; &gt; violations.txt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          if grep -q &quot;CRITICAL GOVERNANCE VIOLATION&quot; violations.txt; then&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            cat violations.txt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            exit 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          fi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Turning on hard-blocking OPA gates across an active repository without prior fixture testing creates immediate developer friction and console-bypass pressure. Roll out enforcement in four gated phases.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-02-25-oracle-infrastructure-cicd-terraform-policy-5-flow.svg&quot; alt=&quot;Four-phase rollout from Rego fixture unit tests and audit-only mode to hard enforcement and break-glass&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Rego Fixture Unit Testing (&lt;code&gt;opa test&lt;/code&gt;):&lt;/strong&gt; Collect sanitized &lt;code&gt;tfplan.json&lt;/code&gt; snapshots from real past PRs (in-place tag edits, storage resizes, &lt;code&gt;ForceNew&lt;/code&gt; replacements, &lt;code&gt;after_unknown&lt;/code&gt; bootstraps, and module refactors) and build an &lt;code&gt;opa test&lt;/code&gt; suite. &lt;strong&gt;Validation gate:&lt;/strong&gt; 100% pass rate on expected allows and denies before touching CI pipelines. &lt;strong&gt;Rollback:&lt;/strong&gt; Fix Rego edge cases in the policy repo without affecting app teams.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Audit-Only PR Comments with Credential Isolation:&lt;/strong&gt; Deploy the GitHub Actions workflow in non-blocking mode (commenting violations on PRs without &lt;code&gt;exit 1&lt;/code&gt;). Run speculative &lt;code&gt;plan&lt;/code&gt; steps using a dedicated read-only OIDC role so PR runners can read state and cloud metadata without holding write or &lt;code&gt;apply&lt;/code&gt; permissions, and delete &lt;code&gt;tfplan.binary&lt;/code&gt; and &lt;code&gt;tfplan.json&lt;/code&gt; in an &lt;code&gt;always()&lt;/code&gt; cleanup step because plan files contain resolved resource attributes and sensitive outputs. &lt;strong&gt;Validation gate:&lt;/strong&gt; Audit two weeks of PRs to identify existing non-compliant NSGs, missing KMS keys, or provider schema quirks. &lt;strong&gt;Rollback:&lt;/strong&gt; Disable the workflow trigger if runner latency spikes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Hard Blocking + Branch Protection:&lt;/strong&gt; Flip the CI gate to &lt;code&gt;exit 1&lt;/code&gt; on violations and mark &lt;code&gt;policy-evaluation&lt;/code&gt; as a required status check in GitHub branch protection. &lt;strong&gt;Validation gate:&lt;/strong&gt; Confirm a test PR attempting an unapproved &lt;code&gt;aws_db_instance&lt;/code&gt; replacement or &lt;code&gt;0.0.0.0/0&lt;/code&gt; port 1521 rule is blocked automatically. &lt;strong&gt;Rollback:&lt;/strong&gt; Revert the branch-protection requirement to audit-only if a provider upgrade triggers false-positive replacements.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Governed Break-Glass Decommissioning:&lt;/strong&gt; Enforce &lt;code&gt;CODEOWNERS&lt;/code&gt; requiring Lead DBA sign-off on any PR that sets &lt;code&gt;decommission_approved = &quot;true&quot;&lt;/code&gt; or modifies policy references, paired with a pre-deletion snapshot check and a separate &lt;code&gt;apply&lt;/code&gt; pipeline that re-validates the signed plan digest.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The behavior of this pipeline rests on documented Terraform, OpenTofu, and OPA semantics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;OpenTofu 1.7 Native State and Plan Encryption:&lt;/strong&gt; OpenTofu 1.7 introduced client-side &lt;strong&gt;State and Plan Encryption&lt;/strong&gt; using an &lt;code&gt;encryption&lt;/code&gt; block (&lt;code&gt;key_provider &quot;aws_kms&quot;&lt;/code&gt; with &lt;code&gt;encryption_context&lt;/code&gt;, paired with &lt;code&gt;method &quot;aes_gcm&quot;&lt;/code&gt; and &lt;code&gt;enforced = true&lt;/code&gt;). While encrypting state and plan artifacts at rest protects sensitive credentials from backend storage leaks, OpenTofu 1.7 does not include an embedded Rego policy engine—meaning teams still serialize &lt;code&gt;tofu show -json&lt;/code&gt; in memory or ephemeral CI runners for OPA evaluation and clean up &lt;code&gt;tfplan.json&lt;/code&gt; immediately after the job step completes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;OCI and AzureRM Resource Semantics:&lt;/strong&gt; In the OCI and AzureRM Terraform providers, &lt;code&gt;oci_database_db_system&lt;/code&gt; supports updatable &lt;code&gt;freeform_tags&lt;/code&gt; and &lt;code&gt;kms_key_id&lt;/code&gt; attributes while marking placement and shape-family changes as &lt;code&gt;ForceNew&lt;/code&gt;, and &lt;code&gt;azurerm_oracle_cloud_vm_cluster&lt;/code&gt; (for Oracle Database@Azure) marks &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;location&lt;/code&gt;, and core cluster identity fields as replacement triggers. Evaluating &lt;code&gt;tfplan.json&lt;/code&gt; catches these provider-specific replacement flags automatically without hardcoding every &lt;code&gt;ForceNew&lt;/code&gt; field in Rego.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Terraform &lt;code&gt;moved&lt;/code&gt; Block State-Graph Behavior:&lt;/strong&gt; Introduced in Terraform 1.1 and supported in OpenTofu, a &lt;code&gt;moved { from = ... to = ... }&lt;/code&gt; block rewrites the resource address inside state during &lt;code&gt;plan&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; without invoking cloud provider destroy or create APIs. In &lt;code&gt;tfplan.json&lt;/code&gt;, a valid &lt;code&gt;moved&lt;/code&gt; refactor produces &lt;code&gt;previous_address&lt;/code&gt; alongside &lt;code&gt;actions: [&quot;no-op&quot;]&lt;/code&gt; (or &lt;code&gt;[&quot;update&quot;]&lt;/code&gt; if attributes also changed), allowing the OPA anti-destruction rule to pass cleanly while still blocking any unmapped rename that falls back to &lt;code&gt;[&quot;delete&quot;, &quot;create&quot;]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CARL Acknowledgment:&lt;/strong&gt; I have not operated a multi-thousand-resource OCI/AzureRM Terraform monorepo personally; the policy mechanics above derive from documented Terraform/OpenTofu plan JSON schemas, Open Policy Agent Rego evaluation rules, and provider &lt;code&gt;ForceNew&lt;/code&gt; specifications.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Automated policy gates introduce failure modes around state size, schema drift, and emergency operational paths.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-02-25-oracle-infrastructure-cicd-terraform-policy-6-failures.svg&quot; alt=&quot;Failure-path overlay showing monolithic state bottlenecks, self-approved tag bypass, schema drift, and missing moved blocks&quot;&gt;&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;How to Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Monolithic state file&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Thousands of resources in one state generate huge &lt;code&gt;tfplan.json&lt;/code&gt; files that slow CI and OPA.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Track CI job duration and &lt;code&gt;tfplan.json&lt;/code&gt; file size per repo.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Decompose monolithic states into micro-states per database or tier.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Self-approved decommission tag&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Author adds &lt;code&gt;decommission_approved = &quot;true&quot;&lt;/code&gt; in the same PR to bypass the delete rule.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Audit PR diffs modifying &lt;code&gt;decommission_approved&lt;/code&gt; without DBA review.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Require &lt;code&gt;CODEOWNERS&lt;/code&gt; approval from Lead DBAs for retirement PRs.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Provider schema drift&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Provider upgrade renames a JSON key; Rego lookup returns undefined and fails open.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run &lt;code&gt;opa test&lt;/code&gt; against new provider plan fixtures during upgrades.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use &lt;code&gt;object.get()&lt;/code&gt; with safe defaults and pin provider versions.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Module rename without &lt;code&gt;moved&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Refactoring renames a resource address; Terraform plans delete+create and OPA blocks it.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;CI detects paired &lt;code&gt;delete&lt;/code&gt; + &lt;code&gt;create&lt;/code&gt; of identical resource types.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Require Terraform 1.1+ &lt;code&gt;moved&lt;/code&gt; blocks for all address refactors.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unpinned policy bundles&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Runners pull mutable &lt;code&gt;main&lt;/code&gt; branch Rego rules, causing inconsistent staging vs. prod blocks.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Compare OPA bundle digest logged in CI run metadata.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Distribute policies as SHA-pinned Git submodules or signed OCI artifacts.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Emergency storage block&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Quota or budget Rego rule blocks an urgent disk expansion during an &lt;code&gt;ORA-00257&lt;/code&gt; outage.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Monitor emergency-label PRs blocked by policy checks.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Maintain a pre-approved fast-path runbook/exemption for storage expansion.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;When onboarding new provider releases, keep a golden suite of sanitized &lt;code&gt;tfplan.json&lt;/code&gt; fixtures covering both permitted changes and intentional policy violations. Running &lt;code&gt;opa test&lt;/code&gt; against those fixtures on every policy or provider version bump catches silent schema renames before a broken rule reaches production pull requests.&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Evaluate compiled &lt;code&gt;tfplan.json&lt;/code&gt;, never raw HCL:&lt;/strong&gt; Only the serialized plan resolves variables, &lt;code&gt;for_each&lt;/code&gt; loops, and provider &lt;code&gt;ForceNew&lt;/code&gt; replacement flags.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Treat &lt;code&gt;prevent_destroy&lt;/code&gt; as defense-in-depth, not a gate:&lt;/strong&gt; Deleting a resource or module block deletes its &lt;code&gt;lifecycle&lt;/code&gt; rule with it, and &lt;code&gt;prevent_destroy&lt;/code&gt; cannot inspect NSG CIDRs or KMS keys.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pair tag exemptions with &lt;code&gt;CODEOWNERS&lt;/code&gt;:&lt;/strong&gt; A Rego check for &lt;code&gt;decommission_approved == &quot;true&quot;&lt;/code&gt; is only safe if branch protection prevents the PR author from self-approving that tag.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Keep states small and policies pinned:&lt;/strong&gt; Micro-states keep &lt;code&gt;tfplan.json&lt;/code&gt; evaluation fast, and SHA-pinned Rego bundles prevent runner policy drift.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Encrypt state at rest with OpenTofu 1.7+:&lt;/strong&gt; Use &lt;code&gt;aes_gcm&lt;/code&gt; with &lt;code&gt;aws_kms&lt;/code&gt; and &lt;code&gt;enforced = true&lt;/code&gt; to protect state files while running OPA on ephemeral CI runners.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Human PR reviews miss buried &lt;code&gt;-/+&lt;/code&gt; database replacements and &lt;code&gt;0.0.0.0/0&lt;/code&gt; exposures inside large Terraform execution plans, while &lt;code&gt;prevent_destroy&lt;/code&gt; disappears when blocks are removed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Gate every database IaC PR with OPA Rego policies evaluating &lt;code&gt;tofu show -json&lt;/code&gt; (or &lt;code&gt;terraform show -json&lt;/code&gt;) for deletions, public listener CIDRs, and mandatory CMEK keys, backed by &lt;code&gt;CODEOWNERS&lt;/code&gt; and micro-states.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Export a real &lt;code&gt;tfplan.json&lt;/code&gt; from your repository and verify with &lt;code&gt;opa eval&lt;/code&gt; that an unapproved &lt;code&gt;delete&lt;/code&gt; or &lt;code&gt;0.0.0.0/0&lt;/code&gt; port 1521 rule returns exit code 1 while benign tag updates pass.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Add the OPA step in audit-only mode to your database Terraform CI workflow this week, remediate any existing violations it surfaces, and then enable branch-protection enforcement.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>Per-App Postgres on Kubernetes Changes the Failure Boundary</title><link>https://rajivonai.com/blog/2026-06-02-per-app-postgres-on-kubernetes-changes-the-failure-boundary/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-06-02-per-app-postgres-on-kubernetes-changes-the-failure-boundary/</guid><description>How CloudNativePG, GitOps, and external secrets make per-application Postgres viable without hiding the operational cost.</description><pubDate>Tue, 02 Jun 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Per-application PostgreSQL does not make databases easier to operate; it makes the failure boundary smaller and the operating contract larger. The trade is worth considering only when the platform can prove that every declared database can fail over, rotate credentials, archive WAL, restore into a clean namespace, and survive Kubernetes maintenance without relying on tribal memory.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The old platform default was a shared managed PostgreSQL cluster with many application databases. It is efficient, familiar, and often the right answer. It also couples teams through change windows, noisy neighbors, backup policy, major-version lifecycle, and shared operational risk.&lt;/p&gt;
&lt;p&gt;The newer pattern is one PostgreSQL cluster per application, declared in Git and reconciled by a Kubernetes operator such as CloudNativePG. That changes what the platform owns. The platform is no longer only offering “a database”; it is offering a repeatable database lifecycle.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Default model&lt;/th&gt;&lt;th&gt;Alternative model&lt;/th&gt;&lt;th&gt;What changes&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;One shared managed PostgreSQL cluster, many databases&lt;/td&gt;&lt;td&gt;One CloudNativePG cluster per application&lt;/td&gt;&lt;td&gt;Failure moves from shared infrastructure to per-service blast radius&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Central database administrator controls change windows&lt;/td&gt;&lt;td&gt;GitOps declares database intent per service&lt;/td&gt;&lt;td&gt;Review moves into pull requests, admission policy, and runbooks&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backups and upgrades handled at the shared cluster level&lt;/td&gt;&lt;td&gt;Backups and upgrades handled per cluster&lt;/td&gt;&lt;td&gt;More isolation, more fleet operations&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Credentials and connectivity are centrally managed&lt;/td&gt;&lt;td&gt;Secrets are synchronized into each namespace&lt;/td&gt;&lt;td&gt;Rotation becomes an end-to-end workflow, not a secret-store update&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Database operations are concentrated in a few large systems&lt;/td&gt;&lt;td&gt;Database operations are repeated across many smaller systems&lt;/td&gt;&lt;td&gt;Templates, policy, alerts, and restore drills become the product&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;CloudNativePG makes this viable because PostgreSQL becomes a Kubernetes custom resource. Argo CD can reconcile the database intent from Git. External Secrets Operator can pull credentials from Azure Key Vault or another external store into Kubernetes Secrets. Kustomize overlays can keep environment differences explicit.&lt;/p&gt;
&lt;p&gt;That is a strong architecture. It is not managed-database simplicity with YAML in front of it.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The operator can create the cluster. That is the least interesting part.&lt;/p&gt;
&lt;p&gt;The production question is whether the database survives the ordinary failures: node drains, bad migrations, storage latency, broken WAL archiving, stale credentials, object-store access errors, version drift, and emergency changes made while GitOps is still reconciling the old state.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure point&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Shared cluster migrations&lt;/td&gt;&lt;td&gt;One application’s migration can saturate I/O, bloat catalogs, or hold locks visible to unrelated tenants&lt;/td&gt;&lt;td&gt;Per-database isolation inside one PostgreSQL instance is not operational isolation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GitOps self-healing&lt;/td&gt;&lt;td&gt;Argo CD can reapply the desired state after manual emergency changes when &lt;code&gt;selfHeal: true&lt;/code&gt; is enabled&lt;/td&gt;&lt;td&gt;Incident response needs a documented reconciliation pause; Argo CD retries self-heal after a default 5 second timeout when configured that way (&lt;a href=&quot;https://argo-cd.readthedocs.io/en/release-2.11/user-guide/auto_sync/&quot;&gt;Argo CD docs&lt;/a&gt;)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup configuration&lt;/td&gt;&lt;td&gt;WAL archives exist, but the physical base backup is missing, stale, or unrecoverable&lt;/td&gt;&lt;td&gt;CloudNativePG’s docs warn that a WAL archive alone is not a restore strategy (&lt;a href=&quot;https://github.com/cloudnative-pg/cloudnative-pg/blob/main/docs/src/backup.md&quot;&gt;CloudNativePG backup docs&lt;/a&gt;)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Kubernetes storage&lt;/td&gt;&lt;td&gt;PostgreSQL restarts cleanly, but the StorageClass has poor latency, weak snapshot behavior, or unsafe reclaim defaults&lt;/td&gt;&lt;td&gt;A database operator cannot paper over unreliable persistent volume semantics&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Secret rotation&lt;/td&gt;&lt;td&gt;External Secrets updates a Kubernetes Secret, but PostgreSQL roles and application connection pools keep using old credentials&lt;/td&gt;&lt;td&gt;Secret synchronization is not end-to-end credential rotation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Version drift&lt;/td&gt;&lt;td&gt;A manifest copied from an older CloudNativePG example keeps working until the operator lifecycle changes&lt;/td&gt;&lt;td&gt;Starting with CloudNativePG 1.26, backup and recovery capabilities are moving toward CNPG-I plugins, so backup templates need version review (&lt;a href=&quot;https://github.com/cloudnative-pg/cloudnative-pg/blob/main/docs/src/backup.md&quot;&gt;CloudNativePG backup docs&lt;/a&gt;)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The right question is not “can Kubernetes run PostgreSQL?” It can. The better question is: what operational boundary are you buying, and what repeated work are you accepting for every application database?&lt;/p&gt;
&lt;h2 id=&quot;architecture-problem&quot;&gt;Architecture Problem&lt;/h2&gt;
&lt;p&gt;The shared database model and the per-application database model solve different coordination problems. In the shared model, operational consistency is achieved at the cost of coupling. In the per-application model, coupling is removed at the cost of operational repetition.&lt;/p&gt;
&lt;p&gt;The architectural problem is not technical feasibility. Kubernetes can schedule PostgreSQL pods. CloudNativePG can declare a cluster as a custom resource. Argo CD can reconcile it from Git. External Secrets Operator can synchronize credentials into namespaces. These mechanisms are documented and widely deployed.&lt;/p&gt;
&lt;p&gt;The actual architectural problem is: &lt;strong&gt;which operational concerns can be automated once at the platform layer, and which must be repeated per database — and is the platform mature enough to absorb the repetition safely?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The failure mode of the shared model is coupling: one application’s migration, bloat, or connection saturation affects every tenant of the cluster. The failure mode of the per-application model is multiplication: every new database adds backup monitoring, restore verification, credential rotation, upgrade planning, and failover testing. If these are not templated, tested, and owned by platform tooling, the per-application model exchanges shared risk for invisible risk.&lt;/p&gt;
&lt;h2 id=&quot;design-options&quot;&gt;Design Options&lt;/h2&gt;
&lt;p&gt;Three options are in common use, and each distributes risk and work differently.&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Option&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Coupling risk&lt;/th&gt;&lt;th&gt;Multiplication risk&lt;/th&gt;&lt;th&gt;Recommended for&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Shared managed cluster&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;One cloud-managed PostgreSQL cluster hosts many application databases; DBA team or cloud provider owns operations&lt;/td&gt;&lt;td&gt;High — shared change windows, noisy neighbors, shared version lifecycle&lt;/td&gt;&lt;td&gt;Low — operations are centralized&lt;/td&gt;&lt;td&gt;Teams early in database operational maturity; stable workloads without strict isolation requirements&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Per-app PostgreSQL, manual management&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Each application gets a dedicated cloud-managed database instance; teams manage their own backups, creds, and versions&lt;/td&gt;&lt;td&gt;Low — isolated failure boundary&lt;/td&gt;&lt;td&gt;High — no shared templates, policy, or tooling&lt;/td&gt;&lt;td&gt;Teams that need isolation but cannot invest in a Kubernetes-native platform&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Per-app PostgreSQL via operator (CloudNativePG + GitOps)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Kubernetes operator reconciles PostgreSQL clusters from Git; external secrets, backups, monitoring, and failover are declared resources&lt;/td&gt;&lt;td&gt;Low — each application cluster is independent&lt;/td&gt;&lt;td&gt;Medium — operator and templates absorb repetition, but restore drills and upgrade testing must still run per cluster&lt;/td&gt;&lt;td&gt;Teams with mature Kubernetes platform capability and willingness to own the database lifecycle&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Option A&lt;/strong&gt; should remain the default until coupling failure modes are actively limiting teams. The argument for per-app databases should be made from incident reports and blocking dependencies, not from preference for patterns.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option B&lt;/strong&gt; increases operational isolation without a shared template layer. Teams that choose this option often discover that they have recreated the shared-cluster problem in a distributed form: many databases with inconsistent backup policies, no shared restore testing, and no centralized visibility into credential expiry or disk saturation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option C&lt;/strong&gt; is the strongest option when the platform investment has been made. CloudNativePG provides a consistent operator lifecycle, standardized service semantics, and Prometheus integration. GitOps provides audit history, review gates, and reconciliation. External Secrets provides credentialed automation. The platform team owns the templates, admission policy, and restore drill cadence. Application teams declare their database intent and trust the platform to handle the lifecycle correctly.&lt;/p&gt;
&lt;h2 id=&quot;tradeoff-matrix&quot;&gt;Tradeoff Matrix&lt;/h2&gt;

































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Dimension&lt;/th&gt;&lt;th&gt;Shared managed cluster&lt;/th&gt;&lt;th&gt;Per-app managed instances&lt;/th&gt;&lt;th&gt;Per-app operator (CloudNativePG)&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Failure blast radius&lt;/td&gt;&lt;td&gt;Shared across all tenants&lt;/td&gt;&lt;td&gt;Per application&lt;/td&gt;&lt;td&gt;Per application&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Noisy neighbor risk&lt;/td&gt;&lt;td&gt;High&lt;/td&gt;&lt;td&gt;None&lt;/td&gt;&lt;td&gt;None&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Operational repetition&lt;/td&gt;&lt;td&gt;Low&lt;/td&gt;&lt;td&gt;High&lt;/td&gt;&lt;td&gt;Medium — templates absorb most repetition&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup and restore&lt;/td&gt;&lt;td&gt;Centralized, consistent&lt;/td&gt;&lt;td&gt;Per-team, inconsistent without tooling&lt;/td&gt;&lt;td&gt;Per-cluster, consistent if platform owns templates&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Credential rotation&lt;/td&gt;&lt;td&gt;Central secret store&lt;/td&gt;&lt;td&gt;Per-instance manual or scripted&lt;/td&gt;&lt;td&gt;External Secrets + per-cluster runbook&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Version upgrades&lt;/td&gt;&lt;td&gt;Scheduled at cluster level&lt;/td&gt;&lt;td&gt;Per-instance, team-owned&lt;/td&gt;&lt;td&gt;Per-cluster, GitOps-managed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GitOps compatibility&lt;/td&gt;&lt;td&gt;External to database&lt;/td&gt;&lt;td&gt;External to database&lt;/td&gt;&lt;td&gt;Native — cluster is a Kubernetes custom resource&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Restore drill burden&lt;/td&gt;&lt;td&gt;One drill for shared cluster&lt;/td&gt;&lt;td&gt;One drill per instance&lt;/td&gt;&lt;td&gt;One drill per cluster tier (production, staging)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Platform investment&lt;/td&gt;&lt;td&gt;Low&lt;/td&gt;&lt;td&gt;Low&lt;/td&gt;&lt;td&gt;High — operator lifecycle, policy, monitoring, templates&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;core-concept-per-app-postgresql-as-a-declared-failure-boundary&quot;&gt;Core Concept: Per-App PostgreSQL as a Declared Failure Boundary&lt;/h2&gt;
&lt;p&gt;A per-application PostgreSQL cluster works when the platform treats the database manifest as an operating contract, not a deployment snippet.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Dev[developer commit] --&gt; Git[Git repository — apps and databases]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Git --&gt; Argo[Argo CD — reconcile desired state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Argo --&gt; App[application namespace]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Argo --&gt; CNPGCluster[CloudNativePG Cluster resource]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    KeyVault[external secret store] --&gt; ESO[External Secrets Operator]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ESO --&gt; K8sSecret[Kubernetes Secret]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K8sSecret --&gt; App&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K8sSecret --&gt; CNPGCluster&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CNPG[CloudNativePG operator] --&gt; Primary[PostgreSQL primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CNPG --&gt; ReplicaA[PostgreSQL replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CNPG --&gt; ReplicaB[PostgreSQL replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App --&gt; RWService[cluster rw service]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    RWService --&gt; Primary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt; WAL[WAL archive in object storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ReplicaA --&gt; WAL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ReplicaB --&gt; WAL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Backup[scheduled base backup] --&gt; ObjectStore[object storage recovery boundary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;CloudNativePG creates service endpoints for each cluster: &lt;code&gt;rw&lt;/code&gt; points to the current primary, &lt;code&gt;ro&lt;/code&gt; points to replicas when available, and &lt;code&gt;r&lt;/code&gt; can point to any instance. The &lt;code&gt;rw&lt;/code&gt; service is essential and cannot be disabled because CloudNativePG relies on it for PostgreSQL replication behavior (&lt;a href=&quot;https://cloudnative-pg.io/docs/1.26/service_management/&quot;&gt;CloudNativePG service docs&lt;/a&gt;). Application write traffic should use the generated &lt;code&gt;*-rw&lt;/code&gt; service unless there is a deliberately tested routing layer in front of it.&lt;/p&gt;
&lt;p&gt;A production-grade manifest should look less like a tutorial and more like a contract:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;apiVersion&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;postgresql.cnpg.io/v1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;kind&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Cluster&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;metadata&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;linkding-db-prod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  labels&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    app.kubernetes.io/name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;linkding&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    platform.example.com/owner&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;bookmarks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    platform.example.com/tier&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;production&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;spec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  instances&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  imageName&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;ghcr.io/cloudnative-pg/postgresql:16.4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  storage&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    size&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;100Gi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    storageClass&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;premium-rwo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  resources&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    requests&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      cpu&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;500m&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      memory&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;2Gi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    limits&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      memory&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;4Gi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  monitoring&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    enablePodMonitor&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  bootstrap&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    initdb&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      database&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;linkding&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      owner&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;linkding&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      secret&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;linkding-db-owner&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  backup&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    barmanObjectStore&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      destinationPath&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;https://example.blob.core.windows.net/postgres/linkding&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      azureCredentials&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        storageAccount&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;linkding-backup-creds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          key&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;storage-account&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        storageSasToken&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;linkding-backup-creds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          key&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;sas-token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      wal&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        compression&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;gzip&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        compression&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;gzip&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    retentionPolicy&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;14d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The contract is not complete until it has tests.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Split day-0 infrastructure from day-2 database intent.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Install CloudNativePG, External Secrets Operator, Argo CD, monitoring CRDs, admission policy, namespaces, and storage classes through Terraform or another cluster-admin workflow. Application repositories should declare database intent, not own operator installation.&lt;/p&gt;
&lt;p&gt;Verification:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;kubectl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; auth&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; can-i&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; create&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; clusters.postgresql.cnpg.io&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-prod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;kubectl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; auth&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; can-i&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; update&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; deployment&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; cloudnative-pg&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; cnpg-system&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;kubectl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; auth&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; can-i&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; patch&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; storageclass&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; premium-rwo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The expected shape is narrow: application delivery can create its own &lt;code&gt;Cluster&lt;/code&gt; resource in its namespace, but cannot modify the operator deployment, cluster-wide secret stores, or storage classes.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;Make policy enforce the minimum contract.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For production clusters, reject manifests that omit ownership labels, resource requests, monitoring, backup configuration, explicit storage class, or a three-instance topology.&lt;/p&gt;
&lt;p&gt;A CI or admission rule should fail a manifest like this:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;spec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  instances&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  storage&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    size&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;5Gi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The exact policy engine is less important than the invariant. Kyverno, OPA Gatekeeper, Conftest, or a custom CI check can all work. The point is to stop “temporary” database YAML from becoming production state.&lt;/p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;Route applications through the CloudNativePG read-write service.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Do not hardcode pod names. Do not point applications at ordinal &lt;code&gt;0&lt;/code&gt;. Do not teach application teams that the first pod is the primary. In a failover, the application needs the service abstraction to follow the writable instance.&lt;/p&gt;
&lt;p&gt;Verification:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;kubectl&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-prod&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; get&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; cluster&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-db-prod&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -o&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; jsonpath=&apos;{.status.currentPrimary}{&quot;\n&quot;}&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;kubectl&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-prod&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; delete&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pod&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;kubectl&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-prod get cluster linkding-db-prod &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -o&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; jsonpath=&apos;{.status.currentPrimary}&apos;)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;kubectl&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-prod&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; wait&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; cluster/linkding-db-prod&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --for=condition=Ready&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --timeout=300s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;kubectl&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-prod&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; get&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; cluster&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-db-prod&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -o&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; jsonpath=&apos;{.status.currentPrimary}{&quot;\n&quot;}&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then verify the application can still write through the same hostname:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; table&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; exists&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; platform_failover_probe (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigserial&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; primary key&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  observed_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; not null&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;insert into&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; platform_failover_probe &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;default&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; values&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;select&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; platform_failover_probe;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A changed primary is not enough. The application write must succeed without changing connection strings.&lt;/p&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;Prove recovery before calling the platform production-ready.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;CloudNativePG can archive WAL to object storage and recover from physical backups. For Barman object-store backups, current CloudNativePG docs say the operator sets &lt;code&gt;archive_timeout&lt;/code&gt; to &lt;code&gt;5min&lt;/code&gt; by default, giving a deterministic time-based RPO boundary for low-write workloads (&lt;a href=&quot;https://cloudnative-pg.io/docs/1.29/appendixes/backup_barmanobjectstore/&quot;&gt;CloudNativePG object-store backup docs&lt;/a&gt;). That boundary is meaningful only after restore has been tested.&lt;/p&gt;
&lt;p&gt;Verification:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;kubectl&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-prod&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; apply&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;YAML&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;apiVersion: postgresql.cnpg.io/v1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;kind: Backup&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;metadata:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  name: linkding-manual-restore-drill&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;spec:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  cluster:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    name: linkding-db-prod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;YAML&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;kubectl&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-prod&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; get&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; backup&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-manual-restore-drill&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A restore drill should create a new namespace, restore from object storage, run application migrations against the restored database, and record observed RTO and RPO. The output should be boring enough to put in a runbook:&lt;/p&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Drill field&lt;/th&gt;&lt;th&gt;Recorded value&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Backup identifier&lt;/td&gt;&lt;td&gt;Exact backup object or CloudNativePG backup name&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Restore namespace&lt;/td&gt;&lt;td&gt;Isolated namespace name&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Restore start time&lt;/td&gt;&lt;td&gt;Timestamp&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application migration result&lt;/td&gt;&lt;td&gt;Pass or fail&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Observed RTO&lt;/td&gt;&lt;td&gt;Measured duration&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Observed RPO&lt;/td&gt;&lt;td&gt;Last committed test row recovered&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Operator version&lt;/td&gt;&lt;td&gt;CloudNativePG version&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;PostgreSQL image&lt;/td&gt;&lt;td&gt;Exact image tag&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;StorageClass&lt;/td&gt;&lt;td&gt;Exact class&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;Make GitOps incident-aware.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Automated pruning and self-healing are useful until an incident commander needs to patch a live object. Argo CD automated sync does not prune by default; pruning and self-healing are explicit settings (&lt;a href=&quot;https://argo-cd.readthedocs.io/en/release-2.11/user-guide/auto_sync/&quot;&gt;Argo CD docs&lt;/a&gt;). Database resources need operational rules around those settings.&lt;/p&gt;
&lt;p&gt;Verification:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;argocd&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; app&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-db-prod&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --sync-policy&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; none&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;kubectl&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-prod&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; annotate&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; cluster&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-db-prod&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  incident.example.com/reconciliation-paused=&quot;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -u&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +%Y-%m-%dT%H:%M:%SZ)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Apply the emergency change, then commit the final desired state back to Git.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;argocd&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; app&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-db-prod&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --sync-policy&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; automated&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --self-heal&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --auto-prune&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;argocd&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; app&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; sync&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; linkding-db-prod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The runbook should say who can pause reconciliation, how the change is recorded, and how drift is reconciled afterward.&lt;/p&gt;
&lt;ol start=&quot;6&quot;&gt;
&lt;li&gt;Monitor the database fleet, not just one cluster.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;CloudNativePG provides predefined metrics and Prometheus integration. A &lt;code&gt;PodMonitor&lt;/code&gt; for a cluster can be created by setting &lt;code&gt;.spec.monitoring.enablePodMonitor: true&lt;/code&gt;, and CloudNativePG publishes Grafana dashboard material for the operator and clusters (&lt;a href=&quot;https://cloudnative-pg.io/documentation/1.20/monitoring/&quot;&gt;CloudNativePG monitoring docs&lt;/a&gt;, &lt;a href=&quot;https://grafana.com/grafana/dashboards/20417-cloudnativepg/&quot;&gt;Grafana dashboard&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Per-application databases multiply alert surfaces. That is acceptable only if ownership is encoded.&lt;/p&gt;
&lt;p&gt;Minimum alert classes:&lt;/p&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Alert class&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Replication lag&lt;/td&gt;&lt;td&gt;Failover safety depends on replicas being current enough for the workload&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failed WAL archiving&lt;/td&gt;&lt;td&gt;PITR depends on the archive, not only the running pods&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup age&lt;/td&gt;&lt;td&gt;A configured backup policy can still fail silently&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Disk saturation&lt;/td&gt;&lt;td&gt;PostgreSQL availability usually fails gradually before it fails completely&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failover events&lt;/td&gt;&lt;td&gt;The application may need connection-pool and retry validation after promotion&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Certificate or secret expiry&lt;/td&gt;&lt;td&gt;A synchronized Secret does not prove clients are using it correctly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;External Secrets sync errors&lt;/td&gt;&lt;td&gt;The Kubernetes Secret can drift from the external source&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Object-store errors&lt;/td&gt;&lt;td&gt;Restore readiness depends on credentials, network path, and storage availability&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern is not “Kubernetes makes databases easy.” The documented pattern is “Kubernetes gives the operator a control plane, and the operator still depends on PostgreSQL, storage, object storage, secrets, and reconciliation semantics behaving correctly.”&lt;/p&gt;
&lt;p&gt;The strongest public warning is GitLab’s January 31, 2017 database outage. It was not a Kubernetes incident, and it should not be misrepresented as one. Its relevance is narrower and more useful: GitLab’s public postmortem shows how PostgreSQL HA, replication, snapshots, dumps, and restore procedures can all look plausible until the one day they are needed together.&lt;/p&gt;
&lt;p&gt;GitLab reported accidental removal of data from the primary database, replication already propagating the damage, missing &lt;code&gt;pg_dump&lt;/code&gt; backups caused by a PostgreSQL client version mismatch, backup failure notifications that were not reaching operators, and a restore path bottlenecked by slow disk transfer from a staging snapshot (&lt;a href=&quot;https://about.gitlab.com/blog/postmortem-of-database-outage-of-january-31/&quot;&gt;GitLab postmortem&lt;/a&gt;). The public incident summary also noted that a six-hour-old backup was used and database changes in that window were lost (&lt;a href=&quot;https://about.gitlab.com/blog/gitlab-dot-com-database-incident/&quot;&gt;GitLab incident update&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;The lesson for CloudNativePG is not that Kubernetes would have prevented the incident. It would not automatically do that. The lesson is that database resilience is a chain:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Write[application write] --&gt; WAL[WAL generated]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WAL --&gt; Archive[WAL archived]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Data[database files] --&gt; BaseBackup[physical base backup]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Archive --&gt; Restore[restore procedure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    BaseBackup --&gt; Restore&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Restore --&gt; AppCheck[application migration and read write check]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    AppCheck --&gt; Evidence[recorded RTO and RPO]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If any link is assumed rather than tested, the platform is carrying hidden risk.&lt;/p&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Evidence type&lt;/th&gt;&lt;th&gt;Public mechanism&lt;/th&gt;&lt;th&gt;Production implication&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;GitLab public postmortem&lt;/td&gt;&lt;td&gt;Backup jobs failed because the wrong PostgreSQL client version was used, and failure notifications were not reaching operators (&lt;a href=&quot;https://about.gitlab.com/blog/postmortem-of-database-outage-of-january-31/&quot;&gt;GitLab postmortem&lt;/a&gt;)&lt;/td&gt;&lt;td&gt;Backup configuration must be verified by restore tests and alert delivery, not only scheduled jobs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GitLab restore behavior&lt;/td&gt;&lt;td&gt;Restore was constrained by the available snapshot and storage transfer path (&lt;a href=&quot;https://about.gitlab.com/blog/postmortem-of-database-outage-of-january-31/&quot;&gt;GitLab postmortem&lt;/a&gt;)&lt;/td&gt;&lt;td&gt;RTO depends on data size, object-store throughput, volume performance, and the restore procedure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CloudNativePG service behavior&lt;/td&gt;&lt;td&gt;CloudNativePG documents &lt;code&gt;rw&lt;/code&gt;, &lt;code&gt;ro&lt;/code&gt;, and &lt;code&gt;r&lt;/code&gt; services, with &lt;code&gt;rw&lt;/code&gt; pointing to the primary and being non-disableable (&lt;a href=&quot;https://cloudnative-pg.io/docs/1.26/service_management/&quot;&gt;service docs&lt;/a&gt;)&lt;/td&gt;&lt;td&gt;Application failover depends on using the service, not pod identity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CloudNativePG backup behavior&lt;/td&gt;&lt;td&gt;CloudNativePG documents WAL archiving, physical base backups, PITR, and warns that WAL alone cannot restore a cluster (&lt;a href=&quot;https://github.com/cloudnative-pg/cloudnative-pg/blob/main/docs/src/backup.md&quot;&gt;backup docs&lt;/a&gt;)&lt;/td&gt;&lt;td&gt;Backup success is not restore readiness&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CloudNativePG object-store behavior&lt;/td&gt;&lt;td&gt;CloudNativePG documents a default &lt;code&gt;archive_timeout&lt;/code&gt; of &lt;code&gt;5min&lt;/code&gt; for Barman object-store WAL archiving (&lt;a href=&quot;https://cloudnative-pg.io/docs/1.29/appendixes/backup_barmanobjectstore/&quot;&gt;object-store backup docs&lt;/a&gt;)&lt;/td&gt;&lt;td&gt;Low-write workloads still need explicit RPO measurement and restore validation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Argo CD reconciliation&lt;/td&gt;&lt;td&gt;Argo CD documents automated prune, self-heal, sync semantics, and rollback limits under automated sync (&lt;a href=&quot;https://argo-cd.readthedocs.io/en/release-2.11/user-guide/auto_sync/&quot;&gt;auto-sync docs&lt;/a&gt;)&lt;/td&gt;&lt;td&gt;Database emergency operations need a GitOps pause and resume procedure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;External Secrets refresh&lt;/td&gt;&lt;td&gt;External Secrets Operator documents &lt;code&gt;CreatedOnce&lt;/code&gt;, &lt;code&gt;Periodic&lt;/code&gt;, and &lt;code&gt;OnChange&lt;/code&gt; refresh policies; &lt;code&gt;Periodic&lt;/code&gt; updates the Kubernetes Secret on &lt;code&gt;refreshInterval&lt;/code&gt; (&lt;a href=&quot;https://external-secrets.io/latest/api/externalsecret/&quot;&gt;ExternalSecret API docs&lt;/a&gt;)&lt;/td&gt;&lt;td&gt;Secret rotation must include application reload and PostgreSQL role behavior&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Kubernetes disruption behavior&lt;/td&gt;&lt;td&gt;Kubernetes distinguishes voluntary and involuntary disruptions and notes that not all voluntary disruptions are constrained by PodDisruptionBudgets (&lt;a href=&quot;https://kubernetes.io/docs/concepts/workloads/pods/disruptions/&quot;&gt;Kubernetes docs&lt;/a&gt;)&lt;/td&gt;&lt;td&gt;Node drain, pod deletion, node loss, and storage failure are separate tests&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;I have not run this exact Linkding-style reference deployment at production scale personally. The documented mechanics are still enough to draw the boundary: a three-instance PostgreSQL cluster can fail over correctly at the Kubernetes object level while the user-visible service still fails because the application pinned stale connections, the volume layer stalled, External Secrets rotated a value no process reloaded, WAL archiving failed unnoticed, or Argo CD reverted an emergency patch.&lt;/p&gt;
&lt;p&gt;That is why the proof must be operational, not visual. A green Argo CD dashboard proves convergence. It does not prove recoverability. A promoted replica proves one HA path. It does not prove connection-pool behavior, restore speed, backup freshness, or data-loss bounds.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Correlated downtime across replicas&lt;/td&gt;&lt;td&gt;Kubernetes schedules PostgreSQL instances onto nodes sharing the same failure domain&lt;/td&gt;&lt;td&gt;Require topology spread constraints, node affinity, and anti-affinity across zones or node pools&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;False confidence from HA&lt;/td&gt;&lt;td&gt;Primary pod deletion succeeds, but storage-zone failure or object-store outage was never tested&lt;/td&gt;&lt;td&gt;Run separate drills for pod deletion, node drain, node loss, storage latency, and restore from object storage&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup drift across CloudNativePG versions&lt;/td&gt;&lt;td&gt;Templates depend on older &lt;code&gt;barmanObjectStore&lt;/code&gt; examples while the operator lifecycle moves toward CNPG-I plugins from 1.26 onward&lt;/td&gt;&lt;td&gt;Pin operator versions, maintain upgrade notes, and test backup plus restore for every operator upgrade&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GitOps conflicts with emergency repair&lt;/td&gt;&lt;td&gt;&lt;code&gt;selfHeal: true&lt;/code&gt; reapplies Git state after manual database-related Kubernetes changes&lt;/td&gt;&lt;td&gt;Document Argo CD suspension, require incident annotations, and reconcile the final state back into Git&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Secret rotation only updates Kubernetes&lt;/td&gt;&lt;td&gt;External Secrets updates the Secret, but PostgreSQL connections remain open with old credentials&lt;/td&gt;&lt;td&gt;Use explicit rotation runbooks: create new role secret, restart or reload clients, verify new logins, then revoke the old role&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Read traffic hits the wrong endpoint&lt;/td&gt;&lt;td&gt;Application sends writes to &lt;code&gt;ro&lt;/code&gt; or uses &lt;code&gt;r&lt;/code&gt; because it appears to work during steady state&lt;/td&gt;&lt;td&gt;Standardize environment variables and policy checks so write paths use only &lt;code&gt;*-rw&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cost expands quietly&lt;/td&gt;&lt;td&gt;Every service gets PostgreSQL pods, persistent volumes, backups, metrics, and alerts&lt;/td&gt;&lt;td&gt;Define tiers: production HA, staging reduced HA, ephemeral development, and explicit cost labels&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Noisy fleet operations&lt;/td&gt;&lt;td&gt;One-off manifests diverge across teams&lt;/td&gt;&lt;td&gt;Generate manifests from reviewed templates and enforce policy with Kyverno, OPA Gatekeeper, or CI checks&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Restore exceeds incident budget&lt;/td&gt;&lt;td&gt;PITR exists in theory, but base backup size, object-store throughput, and migration replay time were never measured&lt;/td&gt;&lt;td&gt;Record RTO and RPO during scheduled restore drills, then publish them with the service SLO&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Kubernetes maintenance causes failover churn&lt;/td&gt;&lt;td&gt;Node drains evict database pods without a maintenance strategy&lt;/td&gt;&lt;td&gt;Use PodDisruptionBudgets, maintenance windows, topology constraints, and CloudNativePG-aware drain procedures&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup alerts are too shallow&lt;/td&gt;&lt;td&gt;The backup job exits successfully, but restore would fail because credentials, object paths, or versions drifted&lt;/td&gt;&lt;td&gt;Alert on backup age and WAL archive failures, then run scheduled restore verification into a clean namespace&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application retry behavior is untested&lt;/td&gt;&lt;td&gt;PostgreSQL primary changes while clients hold old sessions&lt;/td&gt;&lt;td&gt;Test failover through the real application path, including connection pool settings and transaction retry behavior&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Per-application PostgreSQL reduces blast radius, but multiplies operational surfaces across storage, backup, monitoring, secrets, upgrades, GitOps, and cost.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Build a database platform contract around CloudNativePG manifests, admission policy, restore drills, and incident-aware reconciliation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: A valid proof creates a cluster from Git, writes test data, kills the primary, confirms application writes through &lt;code&gt;*-rw&lt;/code&gt;, rotates credentials, restores from object storage into a clean namespace, and records observed RTO and RPO.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, add CI or admission checks for &lt;code&gt;instances &gt;= 3&lt;/code&gt;, backup configuration, monitoring enabled, resource requests, owner labels, explicit storage class, and no plaintext Secret manifests.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A per-application database is not a smaller managed service. It is a sharper failure boundary. Use it when the platform is prepared to test the edge.&lt;/p&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category></item><item><title>Managing the AWS Shared Responsibility Model Across RDS, RDS Custom, and EC2 for Oracle</title><link>https://rajivonai.com/blog/2024-01-15-rds-vs-ec2-vs-rds-custom-responsibility/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-01-15-rds-vs-ec2-vs-rds-custom-responsibility/</guid><description>How to operate enterprise Oracle fleets across Amazon RDS, RDS Custom, and EC2 without breaching the AWS Support Perimeter or fracturing enterprise telemetry and security compliance.</description><pubDate>Sat, 30 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Attempting to govern a mixed fleet of Amazon RDS, RDS Custom, and self-managed EC2 Oracle databases with ad-hoc SSH scripts guarantees Support Perimeter lockouts, security blind spots, and fractured observability.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Uncoordinated OS agent installs on RDS Custom breach the AWS Support Perimeter (&lt;code&gt;unsupported-configuration&lt;/code&gt;), halting automated backups and point-in-time recovery, while SSH keys across EC2 violate Zero Trust audit controls.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Standardize host execution on AWS Systems Manager (SSM) with &lt;code&gt;AutomationMode=&apos;all-paused&apos;&lt;/code&gt; wrappers and &lt;code&gt;/opt&lt;/code&gt; &lt;code&gt;cgroups v2&lt;/code&gt; sandboxes for RDS Custom and EC2, and route Standard RDS telemetry through CloudWatch Metric Streams (&lt;code&gt;opentelemetry1.0&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; &lt;code&gt;ResumeFullAutomationModeMinutes&lt;/code&gt; accepts 60 to 1,440 minutes (never 30), unfiltered metric streams inflate Kinesis Firehose ingest costs, and agents binding TCP port 1521 block the Oracle Listener.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2024-01-15-rds-vs-ec2-vs-rds-custom-responsibility.svg&quot; alt=&quot;Shared responsibility boundaries across Amazon RDS, RDS Custom, and EC2 for Oracle&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Organizations migrating Oracle portfolios to Amazon Web Services (AWS) rarely land on a single hosting model. Cloud-native microservices fit managed services, packaged COTS applications (SAP, PeopleSoft, Siebel) often require OS-level filesystem paths or root access, and high-throughput transactional monoliths may need custom kernel hugepages, ASM striping, or Data Guard configurations. Across AWS, those workloads split into three operational paradigms:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Amazon RDS for Oracle (Managed):&lt;/strong&gt; AWS owns the host, Linux OS, storage striping, and database binary patching. Administration is restricted to database-level PL/SQL APIs (&lt;code&gt;rdsadmin&lt;/code&gt;), with no OS shell or root access.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Amazon RDS Custom for Oracle (Co-Managed):&lt;/strong&gt; AWS preserves automated backups, snapshots, and Multi-AZ storage replication while granting Linux root access and &lt;code&gt;SYSDBA&lt;/code&gt;. That co-managed automation relies on an active &lt;strong&gt;Support Perimeter&lt;/strong&gt; enforced by an AWS health monitor on the host.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Self-Managed Oracle on Amazon EC2 (IaaS):&lt;/strong&gt; The customer owns the entire stack—OS patching, Oracle ASM across Amazon EBS (&lt;code&gt;gp3&lt;/code&gt; or &lt;code&gt;io2&lt;/code&gt; Block Express), kernel parameters, Data Guard Broker, and RMAN backups.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When security teams mandate fleet-wide endpoint detection agents and unified APM telemetry, platform engineers must deploy and observe all three tiers without breaking managed invariants.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Managing these three tiers with bastion SSH keys, cron-based shell scripts, and separate monitoring pipelines fails against four operational constraints:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Constraint 1 — RDS Custom Support Perimeter Invariants:&lt;/strong&gt; AWS continuously polls the underlying EC2 host of every RDS Custom instance to verify that &lt;code&gt;/rdsdbdata&lt;/code&gt;, &lt;code&gt;/rdsdbbin&lt;/code&gt;, network routing, IAM instance profiles, the &lt;code&gt;AmazonSSMAgent&lt;/code&gt; service, and the port 1521 listener remain intact. An uncoordinated package install that restarts &lt;code&gt;sshd&lt;/code&gt; improperly, mutates &lt;code&gt;/rdsdbdata&lt;/code&gt;, or interrupts SSM connectivity transitions the instance to &lt;code&gt;unsupported-configuration&lt;/code&gt;. Once flagged, AWS &lt;strong&gt;halts automated backups&lt;/strong&gt;, impairs point-in-time recovery (PITR), and blocks manual snapshots and host replacement until remediated.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 2 — Minimum Automation Pause Window:&lt;/strong&gt; AWS exposes &lt;code&gt;AutomationMode=&apos;all-paused&apos;&lt;/code&gt; to suspend perimeter checks during maintenance, but &lt;code&gt;ResumeFullAutomationModeMinutes&lt;/code&gt; enforces a valid range of &lt;strong&gt;60 to 1,440 minutes&lt;/strong&gt; (default 60). Scripts that pass values below 60 fail API validation; scripts that omit a &lt;code&gt;finally&lt;/code&gt; resume block leave backups paused for the full timer window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 3 — SGA/PGA Resource Isolation:&lt;/strong&gt; Oracle workloads pin large memory regions in hugepages for the System Global Area (SGA) and Program Global Area (PGA). An uncapped third-party security or log-shipping daemon that spikes CPU or buffers multi-gigabyte trace files can trigger Linux OOM kills against Oracle background processes (&lt;code&gt;LGWR&lt;/code&gt;, &lt;code&gt;DBW0&lt;/code&gt;), crashing the instance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 4 — Zero-SSH Auditability and Metric Convergence:&lt;/strong&gt; Distributing static SSH keys across EC2 and RDS Custom hosts breaks IAM-based attribution and CloudTrail command auditing. Meanwhile, pushing EC2 metrics to one APM, RDS Custom logs to another, and Standard RDS metrics to CloudWatch splits incident triage across three uncorrelated consoles.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;./2024-01-15-rds-vs-ec2-vs-rds-custom-responsibility-2-constraints.svg&quot; alt=&quot;Support perimeter failure cascade when unguarded OS changes trigger unsupported-configuration&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do platform teams orchestrate security and telemetry agents across EC2 and RDS Custom without tripping the Support Perimeter, while converging Standard RDS metrics into the same observability pipeline?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Teams typically evaluate four ways to manage agents and telemetry across mixed AWS Oracle estates.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Fleet Management Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;RDS Custom Safety&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Zero-Trust / Audit&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Standard RDS Coverage&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Why Rejected / Chosen&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Direct SSH + Bastion Hosts&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unsafe — uncoordinated changes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails — static key sprawl&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;None&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;No IAM command trail; high blast radius on &lt;code&gt;/rdsdbdata&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unpaused SSM Installs&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails — trips perimeter&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Passes — IAM + CloudTrail&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;None&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Health monitor flags &lt;code&gt;unsupported-configuration&lt;/code&gt; mid-install.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Standard RDS Only&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;N/A&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Passes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Full&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Blocks COTS apps needing OS paths, &lt;code&gt;SYSDBA&lt;/code&gt;, or custom binaries.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;SSM + Pause Lifecycle + Metric Streams (Chosen)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Safe — 60m pause + &lt;code&gt;/opt&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Passes — zero SSH keys&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Full via OTLP 1.0&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unifies host execution and telemetry across all three tiers.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2024-01-15-rds-vs-ec2-vs-rds-custom-responsibility-3-options.svg&quot; alt=&quot;Comparison of SSH bastions, unpaused installs, Standard RDS only, and SSM with cgroup sandboxing&quot;&gt;&lt;/p&gt;
&lt;p&gt;Direct SSH was rejected because static keys bypass IAM session attribution and encourage manual drift. Running SSM without pausing RDS Custom automation was rejected because package managers that touch systemd targets or network sockets trigger perimeter lockouts. Standardizing exclusively on managed RDS was rejected because legacy ERP and third-party agents cannot run without OS access. Combining SSM with bounded automation pauses, &lt;code&gt;/opt&lt;/code&gt; cgroup sandboxing, and CloudWatch Metric Streams addresses every tier cleanly.&lt;/p&gt;
&lt;h2 id=&quot;the-unified-aws-systems-manager-fleet-architecture&quot;&gt;The Unified AWS Systems Manager Fleet Architecture&lt;/h2&gt;
&lt;p&gt;The architecture eliminates SSH bastions and static keys by using &lt;strong&gt;AWS Systems Manager (SSM)&lt;/strong&gt; as the single host control plane for EC2 and RDS Custom, paired with &lt;strong&gt;Amazon CloudWatch Metric Streams&lt;/strong&gt; for Standard RDS.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-01-15-rds-vs-ec2-vs-rds-custom-responsibility-4-architecture.svg&quot; alt=&quot;Unified AWS SSM control plane for EC2 and RDS Custom paired with CloudWatch Metric Streams for Standard RDS&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;architectural-control-plane-pillars&quot;&gt;Architectural Control Plane Pillars&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Bastion-Less IAM Execution:&lt;/strong&gt; Every EC2 and RDS Custom host runs &lt;code&gt;amazon-ssm-agent&lt;/code&gt; authenticated via its IAM instance profile. Engineers and CI/CD pipelines never open port 22 or distribute static SSH private keys; commands execute via signed AWS SigV4 APIs (&lt;code&gt;ssm:SendCommand&lt;/code&gt; or Session Manager) with full input/output logging to AWS CloudTrail and Amazon S3. Because RDS Custom instances are backed by AWS-managed EC2 instances named with the &lt;code&gt;do-not-delete-rds-custom-{DbiResourceId}&lt;/code&gt; prefix, the control plane resolves the EC2 target dynamically from the RDS API rather than maintaining brittle static inventory files.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bounded RDS Custom Automation Pause:&lt;/strong&gt; An orchestration wrapper transitions the RDS Custom instance to &lt;code&gt;AutomationMode=&apos;all-paused&apos;&lt;/code&gt; with &lt;code&gt;ResumeFullAutomationModeMinutes=60&lt;/code&gt; before dispatching SSM documents, waits for &lt;code&gt;db_instance_available&lt;/code&gt;, executes the host change, and explicitly restores &lt;code&gt;AutomationMode=&apos;full&apos;&lt;/code&gt; in a &lt;code&gt;finally&lt;/code&gt; block. That 60-minute minimum bound acts as a hard safety fuse: even if the CI runner crashes mid-execution, AWS automatically re-engages monitoring and backups once the timer expires.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Isolated &lt;code&gt;/opt&lt;/code&gt; Placement and &lt;code&gt;cgroups v2&lt;/code&gt; Sandboxing:&lt;/strong&gt; Third-party binaries (such as AWS Distro for OpenTelemetry / ADOT collectors or endpoint security daemons) install strictly under &lt;code&gt;/opt/enterprise/&lt;/code&gt;. Systemd unit drop-ins enforce &lt;code&gt;CPUQuota=5%&lt;/code&gt; and &lt;code&gt;MemoryMax=512M&lt;/code&gt; (with &lt;code&gt;MemoryHigh=400M&lt;/code&gt; soft throttling) so an agent memory leak or trace-parsing spike is throttled or OOM-killed inside its own slice before it can compete with Oracle SGA hugepages or PGA allocations. Deployment scripts treat &lt;code&gt;/rdsdbdata&lt;/code&gt;, &lt;code&gt;/rdsdbbin&lt;/code&gt;, &lt;code&gt;/u01/app/oracle&lt;/code&gt;, and &lt;code&gt;/etc/oracle&lt;/code&gt; as strictly read-only.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Unified OTLP Telemetry Mesh:&lt;/strong&gt; Standard RDS instances cannot host OS agents, so Enhanced Monitoring and &lt;code&gt;AWS/RDS&lt;/code&gt; CloudWatch metrics flow through a CloudWatch Metric Stream (&lt;code&gt;output_format = &quot;opentelemetry1.0&quot;&lt;/code&gt;) and Amazon Kinesis Data Firehose into the same central observability backend that receives EC2 and RDS Custom host metrics. Filtering &lt;code&gt;include_filter&lt;/code&gt; to high-signal namespaces and metric names keeps Firehose ingest volume predictable: at 1-minute metric periods, streaming 8 core metrics per RDS instance produces &lt;code&gt;8 * 60 * 24 = 11,520&lt;/code&gt; metric data points per instance per day, whereas an unfiltered stream emits dozens of storage, burst, and replica dimensions per instance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Immutable SSM State Manager Drift Remediation:&lt;/strong&gt; On self-managed EC2 hosts, SSM State Manager associations run on a scheduled cadence to verify that &lt;code&gt;/opt/enterprise/&lt;/code&gt; binaries, systemd cgroup drop-ins, and &lt;code&gt;chronyd&lt;/code&gt; configurations match the signed baseline. On RDS Custom hosts, State Manager only runs read-only compliance inventory checks unless wrapped in the &lt;code&gt;all-paused&lt;/code&gt; automation workflow, preventing background drift-remediation jobs from colliding with AWS-managed patching windows.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;production-implementation-snippets&quot;&gt;Production Implementation Snippets&lt;/h3&gt;
&lt;p&gt;The Python orchestration wrapper pauses RDS Custom automation, resolves the underlying EC2 instance using the immutable &lt;code&gt;DbiResourceId&lt;/code&gt; tag convention (&lt;code&gt;do-not-delete-rds-custom-*&lt;/code&gt;), dispatches the SSM document, and guarantees resumption:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; boto3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sys&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;rds &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; boto3.client(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;rds&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;region_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;us-east-1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ssm &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; boto3.client(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ssm&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;region_name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;us-east-1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; deploy_to_rds_custom&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(instance_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, doc_name: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    rds.modify_db_instance(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        DBInstanceIdentifier&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;instance_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        AutomationMode&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;all-paused&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;        ResumeFullAutomationModeMinutes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;60&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    rds.get_waiter(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;db_instance_available&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).wait(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;DBInstanceIdentifier&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;instance_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        db &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rds.describe_db_instances(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;DBInstanceIdentifier&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;instance_id)[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;DBInstances&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;][&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        resource_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;DbiResourceId&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        ssm.send_command(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            Targets&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Key&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;tag:Name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Values&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;do-not-delete-rds-custom-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;resource_id&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;*&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]}],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            DocumentName&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;doc_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            TimeoutSeconds&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;600&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            Parameters&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;PackageVersion&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;7.52.0&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    finally&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        rds.modify_db_instance(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;DBInstanceIdentifier&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;instance_id, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;AutomationMode&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;full&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On the host, the SSM Command Document installs the agent under &lt;code&gt;/opt&lt;/code&gt; and binds systemd resource limits:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;schemaVersion&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;2.2&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;description&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Install and Sandbox Telemetry Agent on Oracle Hosts&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;parameters&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  PackageVersion&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    default&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;7.52.0&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;mainSteps&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;action&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;aws:runShellScript&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;installAndSandboxAgent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    inputs&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      runAsUser&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;root&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      timeoutSeconds&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;300&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      script&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        - &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          #!/bin/bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          set -euo pipefail&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          INSTALL_DIR=&quot;/opt/enterprise/telemetry&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          mkdir -p &quot;${INSTALL_DIR}&quot; /etc/systemd/system/telemetry-agent.service.d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          rpm -Uvh --prefix=&quot;${INSTALL_DIR}&quot; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &quot;https://internal-repo.enterprise.com/agents/telemetry-agent-{{ PackageVersion }}.rpm&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          cat &amp;#x3C;&amp;#x3C;EOF &gt; /etc/systemd/system/telemetry-agent.service.d/resource-limits.conf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          [Service]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          CPUAccounting=true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          CPUQuota=5%&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          MemoryAccounting=true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          MemoryMax=512M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          MemoryHigh=400M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          systemctl daemon-reload&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          systemctl enable --now telemetry-agent.service&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For Standard RDS, Terraform provisions a filtered CloudWatch Metric Stream using &lt;code&gt;opentelemetry1.0&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;aws_cloudwatch_metric_stream&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;rds_telemetry_stream&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;          =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;rds-database-telemetry-stream&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  role_arn&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; aws_iam_role&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;metric_stream_role&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;arn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  firehose_arn&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; aws_kinesis_firehose_delivery_stream&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;observability_firehose&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;arn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  output_format&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;opentelemetry1.0&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  include_filter&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    namespace&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;AWS/RDS&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    metric_names&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;CPUUtilization&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;FreeableMemory&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ReadLatency&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;WriteLatency&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Rolling out host agents across production Oracle fleets requires staged validation so a bad package or systemd unit never locks out backups across multiple databases simultaneously.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-01-15-rds-vs-ec2-vs-rds-custom-responsibility-5-flow.svg&quot; alt=&quot;Four-step perimeter-safe deployment flow for RDS Custom and EC2&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — IAM and SSM Baseline Verification:&lt;/strong&gt; Attach &lt;code&gt;AmazonSSMManagedInstanceCore&lt;/code&gt; via Terraform and protect it with AWS Organizations Service Control Policies (SCPs) so no account administrator can detach the core management policy during ad-hoc troubleshooting. Verify all EC2 and RDS Custom targets appear &lt;code&gt;Online&lt;/code&gt; in SSM Fleet Manager and enforce Amazon Time Sync Service (&lt;code&gt;169.254.169.123&lt;/code&gt;) via Chrony so SigV4 API calls never fail with &lt;code&gt;SignatureExpired&lt;/code&gt;. &lt;strong&gt;Validation gate:&lt;/strong&gt; Execute a read-only &lt;code&gt;ssm:SendCommand&lt;/code&gt; health check (&lt;code&gt;chronyc tracking&lt;/code&gt;) across all targets and confirm CloudTrail logs the IAM caller ARN. &lt;strong&gt;Rollback:&lt;/strong&gt; Revert the IAM instance profile attachment if a custom policy conflict occurs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Standard RDS Metric Stream Activation:&lt;/strong&gt; Deploy &lt;code&gt;aws_cloudwatch_metric_stream&lt;/code&gt; with &lt;code&gt;include_filter&lt;/code&gt; scoped strictly to core RDS latency, CPU, and memory metrics, backed by a Kinesis Data Firehose delivery stream with GZIP compression. &lt;strong&gt;Validation gate:&lt;/strong&gt; Confirm OTLP 1.0 records arrive in the central APM within 60 seconds and check Firehose &lt;code&gt;DeliveryToHttpEndpoint.Success&lt;/code&gt; metrics. &lt;strong&gt;Rollback:&lt;/strong&gt; Delete or pause the metric stream resource with zero impact on RDS availability.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Staging RDS Custom Pause-and-Install Drill:&lt;/strong&gt; Run the Python wrapper against a non-production RDS Custom instance. &lt;strong&gt;Validation gate:&lt;/strong&gt; Confirm &lt;code&gt;AutomationMode&lt;/code&gt; transitions cleanly to &lt;code&gt;all-paused&lt;/code&gt; and back to &lt;code&gt;full&lt;/code&gt;, database status returns to &lt;code&gt;available&lt;/code&gt; (not &lt;code&gt;unsupported-configuration&lt;/code&gt;), &lt;code&gt;systemd-cgtop&lt;/code&gt; shows the agent bounded under &lt;code&gt;512M&lt;/code&gt;, and a manual RDS snapshot completes cleanly afterward. &lt;strong&gt;Rollback:&lt;/strong&gt; Uninstall the &lt;code&gt;/opt&lt;/code&gt; package while paused, restore &lt;code&gt;AutomationMode=&apos;full&apos;&lt;/code&gt;, and verify a manual DB snapshot succeeds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Canary and Fleet Rollout:&lt;/strong&gt; Deploy to 10% of production EC2 and RDS Custom hosts first, monitoring &lt;code&gt;unsupported-configuration&lt;/code&gt; RDS events, SSM command exit codes, and Oracle &lt;code&gt;alert.log&lt;/code&gt; for listener or memory anomalies before expanding fleet-wide via SSM State Manager associations.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;This architecture aligns with documented AWS and Oracle operational behavior across co-managed and multicloud environments:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;AWS RDS Custom Perimeter Behavior:&lt;/strong&gt; Per AWS RDS Custom documentation, modifying core database directories, stopping the underlying EC2 instance directly, or breaking SSM Agent connectivity triggers &lt;code&gt;unsupported-configuration&lt;/code&gt;, which immediately suspends automated backups and PITR. Using &lt;code&gt;AutomationMode=&apos;all-paused&apos;&lt;/code&gt; with &lt;code&gt;ResumeFullAutomationModeMinutes&lt;/code&gt; (60 to 1,440 minutes) gives maintenance workflows a bounded window while guaranteeing AWS automatically re-engages monitoring if an orchestrator crashes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;OpenTelemetry via ADOT and SSM Distributor:&lt;/strong&gt; Rather than embedding OpenTelemetry inside &lt;code&gt;amazon-ssm-agent&lt;/code&gt; itself, AWS distributes the &lt;strong&gt;AWS Distro for OpenTelemetry (ADOT)&lt;/strong&gt; Collector as a signed SSM Distributor package configured via SSM Parameter Store—keeping the SSM daemon minimal while standardizing OTLP collection across EC2 and RDS Custom.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle Database@AWS Alternative:&lt;/strong&gt; For massive Exadata or RAC workloads that previously forced teams onto self-managed EC2, &lt;strong&gt;Oracle Database@AWS&lt;/strong&gt; places OCI-managed Exadata infrastructure physically inside AWS Availability Zones, offering sub-millisecond EC2 connectivity while shifting hardware and RAC lifecycle management to OCI rather than customer-managed EC2 scripts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standard RDS Telemetry Granularity:&lt;/strong&gt; Because Standard RDS prohibits OS-level agents, CloudWatch Metric Streams export 1-minute &lt;code&gt;AWS/RDS&lt;/code&gt; metrics via Kinesis Data Firehose, while Enhanced Monitoring captures OS process-level telemetry at 1-to-60-second intervals into CloudWatch Logs for unified correlation without breaching the managed host boundary.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Even with SSM and &lt;code&gt;/opt&lt;/code&gt; isolation, subtle misconfigurations in cgroup caps, listener ports, or CloudWatch filters can disrupt production databases or inflate billing.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-01-15-rds-vs-ec2-vs-rds-custom-responsibility-6-failures.svg&quot; alt=&quot;Failure-path overlay showing perimeter lockout, cgroup OOM, port 1521 collision, and metric stream cost&quot;&gt;&lt;/p&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;How to Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Protected path mutation&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Script alters &lt;code&gt;/rdsdbdata&lt;/code&gt;, &lt;code&gt;/rdsdbbin&lt;/code&gt;, or &lt;code&gt;/etc/oracle&lt;/code&gt;; AWS flags &lt;code&gt;unsupported-configuration&lt;/code&gt; and halts backups.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Alert on RDS EventBridge/RDS events (&lt;code&gt;SP-S0000&lt;/code&gt; or status != &lt;code&gt;available&lt;/code&gt;).&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Quarantine all third-party files in &lt;code&gt;/opt&lt;/code&gt;; lint SSM scripts in CI for forbidden paths.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Orchestrator crash during pause&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Wrapper fails before &lt;code&gt;finally&lt;/code&gt; block; backups stay paused until &lt;code&gt;ResumeFullAutomationModeMinutes&lt;/code&gt; expires.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Monitor CloudTrail &lt;code&gt;ModifyDBInstance&lt;/code&gt; events and RDS &lt;code&gt;AutomationMode&lt;/code&gt; state.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep &lt;code&gt;ResumeFullAutomationModeMinutes = 60&lt;/code&gt; (minimum) so AWS auto-resumes within one hour.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Cgroup memory OOM kill&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Agent buffers large Oracle trace files and hits &lt;code&gt;MemoryMax=512M&lt;/code&gt;; kernel kills telemetry daemon.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Alert on systemd &lt;code&gt;oom-kill&lt;/code&gt; journal events and missing host heartbeats.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Configure agents to tail logs incrementally; size &lt;code&gt;MemoryMax&lt;/code&gt; from staging peak usage.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Port 1521 listener collision&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Active vulnerability scanner binds TCP 1521; Oracle TNS Listener fails on restart.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Monitor &lt;code&gt;lsnrctl status&lt;/code&gt; and connection pool handshake errors.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run security scanners in passive inspection mode; forbid socket binding on 1521.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;SSM IAM role drift&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Instance profile edit drops &lt;code&gt;AmazonSSMManagedInstanceCore&lt;/code&gt;; host vanishes from SSM Fleet Manager.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Alert on SSM &lt;code&gt;ConnectionLost&lt;/code&gt; status in Fleet Manager.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lock core SSM role permissions with AWS Organizations SCPs.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chrony NTP clock drift&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Skewed system clock causes AWS SigV4 requests to fail with &lt;code&gt;SignatureExpired&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Alert on &lt;code&gt;chronyc tracking&lt;/code&gt; offset and SSM agent health check failures.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce Amazon Time Sync Service (&lt;code&gt;169.254.169.123&lt;/code&gt;) in base AMIs and State Manager.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unfiltered Metric Stream&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Streaming every &lt;code&gt;AWS/RDS&lt;/code&gt; metric at high cardinality spikes Kinesis Firehose ingest fees.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Monitor AWS Cost Explorer and Firehose &lt;code&gt;IncomingBytes&lt;/code&gt; metrics.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Restrict &lt;code&gt;include_filter&lt;/code&gt; to essential CPU, memory, and I/O latency metrics.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Never manage RDS Custom like raw EC2:&lt;/strong&gt; Any OS change outside &lt;code&gt;/opt&lt;/code&gt; or without &lt;code&gt;AutomationMode=&apos;all-paused&apos;&lt;/code&gt; risks &lt;code&gt;unsupported-configuration&lt;/code&gt; and an immediate halt to automated backups and PITR.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code&gt;ResumeFullAutomationModeMinutes&lt;/code&gt; as a safety fuse:&lt;/strong&gt; Set it to the minimum 60 minutes so a crashed deployment script cannot leave RDS Custom automation paused indefinitely.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sandbox every third-party daemon with &lt;code&gt;cgroups v2&lt;/code&gt;:&lt;/strong&gt; Capping &lt;code&gt;CPUQuota&lt;/code&gt; and &lt;code&gt;MemoryMax&lt;/code&gt; prevents monitoring or security agents from starving Oracle SGA/PGA memory.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Eliminate SSH keys with AWS SSM:&lt;/strong&gt; IAM-authenticated SSM Run Command and Session Manager provide full CloudTrail attribution across both EC2 and RDS Custom without bastion hosts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Converge Standard RDS via filtered OTLP 1.0 streams:&lt;/strong&gt; CloudWatch Metric Streams (&lt;code&gt;opentelemetry1.0&lt;/code&gt;) bring managed RDS into the same observability mesh as EC2 and RDS Custom when filtered to high-signal metrics.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Ad-hoc SSH scripts and fragmented monitoring across Amazon RDS, RDS Custom, and EC2 cause SSH key sprawl, observability blind spots, and RDS Custom Support Perimeter lockouts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Standardize host management on IAM-governed AWS SSM with 60-minute &lt;code&gt;all-paused&lt;/code&gt; wrappers and &lt;code&gt;/opt&lt;/code&gt; &lt;code&gt;cgroups v2&lt;/code&gt; sandboxes, and export Standard RDS metrics via filtered &lt;code&gt;opentelemetry1.0&lt;/code&gt; CloudWatch Metric Streams.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Verify in staging that an SSM agent deployment on RDS Custom leaves DB status &lt;code&gt;available&lt;/code&gt;, keeps the daemon under &lt;code&gt;512M&lt;/code&gt; RAM, and delivers &lt;code&gt;AWS/RDS&lt;/code&gt; OTLP metrics within 60 seconds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your AWS Oracle inventory this week: confirm no RDS Custom instances are stuck in &lt;code&gt;unsupported-configuration&lt;/code&gt;, attach &lt;code&gt;AmazonSSMManagedInstanceCore&lt;/code&gt; protected by an SCP, and disable port 22 bastion access.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>GCP AlloyDB vs Cloud SQL for PostgreSQL: When to Upgrade</title><link>https://rajivonai.com/blog/2026-05-28-gcp-alloydb-vs-cloud-sql-postgresql-when-to-upgrade/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-28-gcp-alloydb-vs-cloud-sql-postgresql-when-to-upgrade/</guid><description>When Cloud SQL&apos;s managed PostgreSQL hits its limits and AlloyDB&apos;s columnar cache and HTAP architecture become worth the migration complexity and cost jump.</description><pubDate>Thu, 28 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Cloud SQL for PostgreSQL handles most managed database workloads on GCP correctly, but teams that hit analytical query performance ceilings or need HTAP capabilities discover they should have evaluated AlloyDB before the schema was in production.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Google offers two managed PostgreSQL services on GCP: Cloud SQL and AlloyDB. Cloud SQL is the established managed PostgreSQL (and MySQL, SQL Server) offering with straightforward HA, backups, and read replicas. AlloyDB is a Google-developed PostgreSQL-compatible database that separates compute from storage using a distributed storage layer, adds an adaptive adaptive columnar cache, and supports read pool instances that can run both OLTP and analytical queries against the same data.&lt;/p&gt;
&lt;p&gt;AlloyDB became generally available in May 2023. Most GCP teams deploying PostgreSQL choose Cloud SQL as the default path and only encounter AlloyDB when they are researching options or hitting specific performance limits.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cloud SQL for PostgreSQL is a managed PostgreSQL instance with HA standby and read replicas. It scales vertically. The limiting pattern: as analytical query volume grows alongside OLTP traffic, the primary instance saturates on CPU, and read replicas lag under heavy read load — because they are executing the same row-scan-based queries that the primary executes. Adding read replicas distributes read connections but not the per-query execution cost.&lt;/p&gt;
&lt;p&gt;AlloyDB’s design addresses a different bottleneck. For OLAP-style queries (aggregations, wide scans, joins across large tables), AlloyDB’s columnar cache stores frequently accessed columns in a compressed columnar format in memory, separate from the row-store. The query engine uses the columnar representation when it is faster, without requiring the application to target a separate analytical store. This is what Google means by HTAP — both OLTP and analytical queries run against the same PostgreSQL-compatible interface, with the storage engine selecting the execution path.&lt;/p&gt;
&lt;p&gt;The core question: does the workload contain a meaningful volume of analytical queries running against live OLTP data, and is Cloud SQL’s execution performance the actual bottleneck?&lt;/p&gt;
&lt;h2 id=&quot;alloydb-vs-cloud-sql-architecture&quot;&gt;AlloyDB vs Cloud SQL Architecture&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[PostgreSQL workload on GCP] --&gt; B{Workload shape?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|standard OLTP — transactional reads and writes| C[Cloud SQL — managed single-primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|mixed OLTP and analytical queries on same data| D{Is Cloud SQL CPU the bottleneck?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no — query volume is moderate| C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes — analytical queries saturating primary or replicas| E[AlloyDB — columnar cache — HTAP]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; F[HA standby — read replicas — automatic backups]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; G[Primary — read pool instances — columnar cache — distributed storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Cloud SQL for PostgreSQL&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Cloud SQL provides a managed PostgreSQL instance with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;High availability via a synchronous standby in a secondary zone; Google documents zonal failover typically completing in under 60 seconds with automatic IP cutover (&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/high-availability&quot;&gt;Cloud SQL HA&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Read replicas in the same or different regions (asynchronous replication)&lt;/li&gt;
&lt;li&gt;Automatic backups and point-in-time recovery up to the retention window&lt;/li&gt;
&lt;li&gt;Private IP, VPC peering, and Cloud SQL Auth Proxy for secure connectivity&lt;/li&gt;
&lt;li&gt;Maintenance windows with configurable timing&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cross-region disaster recovery with Cloud SQL uses cross-region read replicas. Google documents these as asynchronous, meaning a regional failure can result in data loss equal to replication lag at the moment of failure. Replica promotion is a manual operation (&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/intro-to-cloud-sql-disaster-recovery&quot;&gt;Cloud SQL DR&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;AlloyDB for PostgreSQL&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;AlloyDB separates PostgreSQL compute from storage:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The primary instance handles writes; the storage layer is distributed across Google’s infrastructure, replicating synchronously across zones within the region&lt;/li&gt;
&lt;li&gt;Read pool instances share the same storage layer as the primary — there is no replication lag for reads because read pool instances read directly from the shared distributed storage&lt;/li&gt;
&lt;li&gt;The adaptive columnar cache stores frequently accessed column data in memory on read pool instances and the primary; the query engine selects columnar or row-store execution per query&lt;/li&gt;
&lt;li&gt;Google documents AlloyDB storage as synchronously replicated within the region; the storage tier handles I/O and durability independently of compute&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;AlloyDB is PostgreSQL-compatible at the protocol level. Standard PostgreSQL drivers, pgAdmin, and most tools that connect to PostgreSQL connect to AlloyDB without modification. Extensions that depend on specific storage internals may behave differently. The compute and storage separation also means that PostgreSQL-compatible behavior should not be confused with an identical physical write path.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Google’s AlloyDB documentation describes the columnar cache as an adaptive structure — the database populates it based on query patterns without requiring explicit configuration (&lt;a href=&quot;https://cloud.google.com/alloydb/docs/columnar-engine/about&quot;&gt;AlloyDB columnar engine&lt;/a&gt;). The engine analyzes which columns are accessed frequently by scan-heavy queries and promotes them into the columnar representation. This is distinct from creating a materialized view or a separate analytical table: the data source is the same live table; the storage representation changes based on access patterns.&lt;/p&gt;
&lt;p&gt;The documented design consequence is that AlloyDB read pool instances can satisfy analytical queries from the columnar cache without adding lag from replication — because they read from the same distributed storage layer as the primary rather than applying a WAL stream. Cloud SQL read replicas apply WAL asynchronously; under heavy write load, replication lag can grow, making replica reads stale for time-sensitive analytics.&lt;/p&gt;
&lt;p&gt;Migration from Cloud SQL to AlloyDB uses the Database Migration Service. Google documents that DMS supports online migrations from Cloud SQL for PostgreSQL to AlloyDB with minimal downtime using logical replication (&lt;a href=&quot;https://cloud.google.com/database-migration/docs/postgres-to-alloydb/overview&quot;&gt;DMS AlloyDB migration&lt;/a&gt;). Schema-level PostgreSQL extensions used in Cloud SQL that are not supported in AlloyDB require application changes before migration. The AlloyDB documentation lists supported extensions; notably, some PostGIS and pg_partman functionality may require version verification.&lt;/p&gt;
&lt;p&gt;AlloyDB costs more than Cloud SQL at equivalent compute sizes. Google’s pricing for AlloyDB reflects the separate storage layer billing model — storage is billed per GB regardless of instance size, and read pool instances add compute cost beyond the primary. For workloads where Cloud SQL’s row-store execution is adequate, AlloyDB’s additional cost produces no measurable benefit.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;AlloyDB — columnar cache cold on startup&lt;/td&gt;&lt;td&gt;Analytical queries revert to row-store performance until cache warms&lt;/td&gt;&lt;td&gt;Cache is populated from query patterns; a restarted instance has no cached columns initially&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AlloyDB — extension dependency not supported&lt;/td&gt;&lt;td&gt;Migration blocked or application behavior changes&lt;/td&gt;&lt;td&gt;AlloyDB does not support all PostgreSQL extensions available in Cloud SQL; verify before migrating&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cloud SQL cross-region replica — regional failover&lt;/td&gt;&lt;td&gt;Manual promotion, potential data loss equal to replication lag&lt;/td&gt;&lt;td&gt;Cross-region replicas are asynchronous; no automatic promotion to primary&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AlloyDB — write-heavy workload with no analytical queries&lt;/td&gt;&lt;td&gt;Cost increase with no performance benefit&lt;/td&gt;&lt;td&gt;The columnar cache and read pool architecture only benefit mixed or analytical workloads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cloud SQL — analytical query on primary during peak OLTP&lt;/td&gt;&lt;td&gt;CPU saturation affects write latency&lt;/td&gt;&lt;td&gt;Row-store execution for wide scans competes with OLTP for CPU; no separate execution path&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AlloyDB — connection to read pool for write operations&lt;/td&gt;&lt;td&gt;Write rejected&lt;/td&gt;&lt;td&gt;Read pool instances are read-only; writes must target the primary endpoint&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Cloud SQL’s row-store execution handles OLTP well but has no separate code path for analytical queries, meaning mixed workloads compete for the same CPU on primary and replicas.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Evaluate AlloyDB when analytical queries represent a meaningful share of query volume, Cloud SQL CPU is the bottleneck during analytical load, and the workload runs in a single GCP region (AlloyDB does not currently support cross-region reads with the shared storage model).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Run &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; on the three slowest analytical queries in Cloud SQL and measure CPU time; if the bottleneck is scan and aggregation (not I/O or lock contention), AlloyDB’s columnar cache addresses the actual bottleneck.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Before committing to AlloyDB, verify that all PostgreSQL extensions in use are supported by AlloyDB and budget for the cost differential; if the workload is exclusively transactional with no wide-scan analytics, Cloud SQL remains the correct choice.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category></item><item><title>Cassandra Write Path Fundamentals for Database Engineers</title><link>https://rajivonai.com/blog/2026-05-26-cassandra-write-path-fundamentals-for-database-engineers/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-26-cassandra-write-path-fundamentals-for-database-engineers/</guid><description>How Cassandra&apos;s commit log, Memtable, and SSTable pipeline works, why write amplification is the dominant operational cost, and how compaction strategy selection changes it.</description><pubDate>Tue, 26 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Cassandra’s write performance reputation is correct but incomplete — writes are fast because Cassandra converts random writes into sequential I/O, and the operational cost of that conversion is paid later through compaction, which can saturate disk throughput if the strategy does not match the workload.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Database engineers familiar with PostgreSQL or MySQL approach Cassandra expecting tunable durability, indexing flexibility, and a query optimizer. Cassandra’s durability and performance model works differently: the write path is optimized for sequential I/O at the cost of deferred merge work, and the query model is constrained by the partition key and clustering columns defined at schema creation.&lt;/p&gt;
&lt;p&gt;Cassandra is used in production for workloads requiring high write throughput, time-series data, and geographic multi-region replication — systems where the write path’s operational characteristics are the primary design constraint.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The fundamental problem Cassandra solves is random write throughput. Traditional relational databases perform writes by updating rows in-place on disk pages, which requires random I/O to locate the correct page. At high write rates across large datasets, this random I/O pattern saturates disk throughput.&lt;/p&gt;
&lt;p&gt;Cassandra converts all writes into sequential operations: every write appends to the commit log (sequential disk write) and updates an in-memory structure (Memtable). When the Memtable exceeds a threshold, it is flushed to disk as an immutable SSTable (Sequential String Table) file. The database never updates SSTables in place — mutations are always new writes. This makes the write path fast, but it defers the cost of merging and garbage-collecting old data to compaction.&lt;/p&gt;
&lt;p&gt;The core question: which compaction strategy minimizes the operational cost of the deferred merge work for the workload’s specific access pattern?&lt;/p&gt;
&lt;h2 id=&quot;the-write-path&quot;&gt;The Write Path&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[write request — partition key and columns] --&gt; B[commit log — sequential append — fsync]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[Memtable — in-memory sorted structure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D{Memtable full or flush triggered?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no — within threshold| E[write acknowledged to client]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes — threshold exceeded| F[flush Memtable to SSTable on disk]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[new immutable SSTable file]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H{compaction threshold reached?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| I[multiple SSTables accumulate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| J[compaction — merge SSTables — discard tombstones]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[fewer larger SSTables]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Commit Log&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Every write is first appended to the commit log — a sequential append-only file on disk. Cassandra uses the commit log for crash recovery: if the process dies before the Memtable is flushed, the commit log replays the unwritten data on restart. The commit log is the durability guarantee.&lt;/p&gt;
&lt;p&gt;Cassandra’s &lt;code&gt;commitlog_sync&lt;/code&gt; setting controls when the commit log is fsynced to disk:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;periodic&lt;/code&gt; (default): writes are acknowledged after being written to the OS buffer; an fsync happens periodically (default 10,000ms). This is fast but risks losing up to 10 seconds of writes if the node crashes.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;batch&lt;/code&gt;: fsync happens before the write is acknowledged. Durable but slower — adds the fsync latency to every write.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Most high-throughput production deployments use &lt;code&gt;periodic&lt;/code&gt; mode with the understanding that a crash can lose up to &lt;code&gt;commitlog_sync_period_in_ms&lt;/code&gt; of data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Memtable&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;After the commit log append, the write is applied to the Memtable — an in-memory sorted data structure partitioned by the partition key and ordered by clustering columns. Multiple concurrent writes accumulate in the Memtable until it is flushed. Reads that target recently written data are served from the Memtable without hitting disk.&lt;/p&gt;
&lt;p&gt;The Memtable is bounded by &lt;code&gt;memtable_heap_space_in_mb&lt;/code&gt; and &lt;code&gt;memtable_offheap_space_in_mb&lt;/code&gt;. When the Memtable exceeds the threshold or when a flush is triggered manually, Cassandra writes it to disk as an immutable SSTable and starts a new Memtable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;SSTable and Compaction&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;SSTables are immutable files. An update to an existing row writes a new SSTable entry with a higher timestamp — the old value is not removed. A delete writes a tombstone — a marker indicating the row was deleted. Tombstones accumulate in SSTables until compaction.&lt;/p&gt;
&lt;p&gt;Reads must check all SSTables for the most recent version of a row (plus the Memtable). As SSTable count grows, read latency increases because more files must be checked. Compaction merges SSTables, applies the recency rule (highest timestamp wins), removes tombstones beyond the &lt;code&gt;gc_grace_seconds&lt;/code&gt; threshold, and produces fewer, larger SSTables. This reduces read amplification at the cost of write amplification (new SSTable files written during compaction).&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Cassandra’s documentation describes three compaction strategies, each with different tradeoffs (&lt;a href=&quot;https://cassandra.apache.org/doc/stable/cassandra/operating/compaction/&quot;&gt;Apache Cassandra compaction&lt;/a&gt;):&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Size-Tiered Compaction Strategy (STCS)&lt;/strong&gt; — the default. Groups SSTables of similar sizes into tiers and merges within each tier when the count exceeds a threshold (default 4). Write amplification is low — fewer bytes are rewritten per compaction cycle. Read amplification is higher because many SSTables can accumulate before a tier triggers. STCS is appropriate for write-heavy workloads where read latency is less critical.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Leveled Compaction Strategy (LCS)&lt;/strong&gt; — maintains SSTables in levels where each SSTable in a level covers a disjoint key range. A given partition key exists in exactly one SSTable per level (except Level 0). This keeps read amplification low — finding a row requires checking at most one SSTable per level — but write amplification is significantly higher because SSTables are rewritten frequently to maintain the level invariant. LCS is appropriate for read-heavy workloads where predictable read latency is required.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Time Window Compaction Strategy (TWCS)&lt;/strong&gt; — groups SSTables by time window and compacts within each window. SSTables from old, expired windows are compacted into a single file and then not recompacted. This is optimal for time-series data where old data is rarely updated, because it avoids repeatedly rewriting old SSTables. Cassandra’s TWCS documentation is specific about a key requirement: time-to-live (TTL) must be set consistently on all data in a TWCS table, or tombstones from rows without TTL will never be fully compacted away (&lt;a href=&quot;https://cassandra.apache.org/doc/stable/cassandra/operating/compaction/twcs.html&quot;&gt;TWCS documentation&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tombstone accumulation as an operational hazard.&lt;/strong&gt; In Cassandra’s documented behavior, tombstones for deleted rows accumulate across SSTables until compaction runs and &lt;code&gt;gc_grace_seconds&lt;/code&gt; elapses. If a partition accumulates a large number of tombstones before compaction (due to high delete rates, low compaction throughput, or misconfigured &lt;code&gt;gc_grace_seconds&lt;/code&gt;), reads on that partition must scan through all tombstones before returning results. Cassandra’s coordinator logs a warning at 1,000 tombstones per read and throws a &lt;code&gt;TombstoneOverwhelmingException&lt;/code&gt; at 100,000. High tombstone counts are the most common cause of unexpected read latency on write-optimized Cassandra tables.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;STCS on read-heavy workload&lt;/td&gt;&lt;td&gt;Read latency grows as SSTable count increases between compaction cycles&lt;/td&gt;&lt;td&gt;STCS allows many same-size SSTables to accumulate; reads must check each one&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LCS on write-heavy workload&lt;/td&gt;&lt;td&gt;Compaction I/O saturates disk throughput&lt;/td&gt;&lt;td&gt;High write amplification from maintaining level invariants requires continuous rewriting&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;TWCS with mixed TTL and non-TTL data&lt;/td&gt;&lt;td&gt;Tombstones never fully compacted in old windows&lt;/td&gt;&lt;td&gt;Non-TTL rows in old time windows prevent old SSTable retirement&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;commitlog_sync: batch&lt;/code&gt; at high write rate&lt;/td&gt;&lt;td&gt;Write throughput drops significantly&lt;/td&gt;&lt;td&gt;Each write waits for an fsync; batching does not fully absorb the overhead at high concurrency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Large partition with many updates&lt;/td&gt;&lt;td&gt;Read latency spikes; repair timeouts&lt;/td&gt;&lt;td&gt;Large partitions accumulate many SSTable entries; repair must process the full partition&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;gc_grace_seconds&lt;/code&gt; set to 0&lt;/td&gt;&lt;td&gt;Deleted rows reappear after node repair&lt;/td&gt;&lt;td&gt;Tombstones are the mechanism for propagating deletes during hinted handoff; removing them before repair risks resurrection&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unbounded Memtable heap&lt;/td&gt;&lt;td&gt;JVM GC pauses&lt;/td&gt;&lt;td&gt;Memtable allocation competes with JVM heap for Cassandra processes; excessive heap causes long GC pauses&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Cassandra’s sequential write path makes writes fast, but the deferred compaction cost creates a continuous background I/O load that can saturate disk and cause read latency spikes if the compaction strategy does not match the workload.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Select STCS for write-heavy append workloads, LCS for read-heavy workloads with updates and point lookups, and TWCS for time-series tables with consistent TTL — and verify tombstone accumulation rates on high-delete tables using &lt;code&gt;nodetool cfstats&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Run &lt;code&gt;nodetool compactionstats&lt;/code&gt; to see pending compaction tasks and measure live disk I/O during compaction; if compaction cannot keep up with write rate (pending task count grows continuously), the strategy or write rate is mismatched.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Identify your highest-volume Cassandra tables this week, confirm which compaction strategy each uses, and check &lt;code&gt;nodetool cfstats&lt;/code&gt; for tombstone count — any table with tombstones per read above 1,000 warrants immediate investigation.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category></item><item><title>Azure Database for PostgreSQL: Flexible Server vs Hyperscale (Citus) Architecture Decision</title><link>https://rajivonai.com/blog/2026-05-25-azure-postgresql-flexible-vs-citus-architecture-decision/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-25-azure-postgresql-flexible-vs-citus-architecture-decision/</guid><description>When to choose Azure Flexible Server vs Citus for PostgreSQL on Azure — failover behavior, connection pooling, and the workload shapes where each architecture wins and breaks.</description><pubDate>Mon, 25 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The default Azure PostgreSQL offering handles most OLTP workloads correctly, but teams that hit connection limits, multi-tenant scale, or distributed query requirements discover they chose the wrong architecture after the schema is in production.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Azure offers two managed PostgreSQL architectures: Flexible Server (the current default and successor to Single Server) and Hyperscale, which runs the Citus extension for distributed PostgreSQL. Both are managed services on Azure with similar operational interfaces. The architectural difference is not a sizing question — it is a data distribution question. Most teams never need Citus. The teams that do need it typically discover the need late, after their schema is built around single-node PostgreSQL assumptions.&lt;/p&gt;
&lt;p&gt;Azure announced that PostgreSQL Single Server reached end of life in March 2025, making Flexible Server the standard entry point for new deployments and migrations.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Azure Flexible Server is a single-primary managed PostgreSQL instance with read replicas, high availability via standby promotion, and built-in PgBouncer connection pooling. It scales vertically and handles standard PostgreSQL workloads. The failure mode is predictable: beyond a certain write throughput threshold and connection count, a single PostgreSQL primary saturates regardless of how large the VM SKU is.&lt;/p&gt;
&lt;p&gt;Citus distributes table rows across worker nodes using a shard key. This enables horizontal write scaling and parallel query execution across shards — but it requires designing the schema and query patterns around the distribution key from the start. Application queries that do not include the distribution key cannot be routed to a single shard and must fan out across all workers, which is expensive.&lt;/p&gt;
&lt;p&gt;The core question: does the workload require horizontal scaling of writes and data volume, or does it require operational simplicity with vertical scaling?&lt;/p&gt;
&lt;h2 id=&quot;flexible-server-vs-hyperscale-citus&quot;&gt;Flexible Server vs Hyperscale (Citus)&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[PostgreSQL workload on Azure] --&gt; B{Multi-tenant or single-tenant?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|single tenant — standard OLTP| C[Flexible Server]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|multi-tenant at scale or distributed analytics| D{Can schema be distributed on tenant ID?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes — queries filter by tenant| E[Citus — sharded by tenant]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no — cross-tenant joins required| F[Flexible Server — accept vertical limits]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; G[Scale vertically — HA standby — PgBouncer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; H[Coordinator node — worker shards — distributed queries]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Azure Flexible Server&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Flexible Server provides a single primary PostgreSQL instance with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Zone-redundant high availability (primary + synchronous standby in a secondary AZ)&lt;/li&gt;
&lt;li&gt;Built-in PgBouncer for connection pooling (configurable pool sizes per database)&lt;/li&gt;
&lt;li&gt;Read replicas for read offload (asynchronous replication)&lt;/li&gt;
&lt;li&gt;Automatic minor version patching and maintenance windows&lt;/li&gt;
&lt;li&gt;Private endpoint and VNet integration&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The HA model uses a standby in a secondary availability zone with synchronous replication. Azure documents typical failover in 60–120 seconds with automatic DNS cutover (&lt;a href=&quot;https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/concepts-high-availability&quot;&gt;Flexible Server HA docs&lt;/a&gt;). The built-in PgBouncer connection pooler is enabled separately from the HA feature and must be explicitly configured — applications that connect directly to the PostgreSQL port bypass PgBouncer.&lt;/p&gt;
&lt;p&gt;Connection pooling is the most commonly misconfigured element. Azure Flexible Server supports a maximum of 5,000 backend connections for the largest SKU (D64s v3), but each PostgreSQL backend process consumes memory. The practical limit before performance degrades is substantially lower. PgBouncer on Flexible Server runs in transaction-pooling mode by default, which releases the backend connection between transactions — enabling more clients than physical backends.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Hyperscale (Citus)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Citus distributes a PostgreSQL database across a coordinator node and multiple worker nodes. The coordinator routes queries to shards based on the distribution column. A table distributed on &lt;code&gt;tenant_id&lt;/code&gt; routes queries that filter on &lt;code&gt;tenant_id&lt;/code&gt; to the single worker holding that tenant’s shards. Queries without a &lt;code&gt;tenant_id&lt;/code&gt; filter fan out to all workers.&lt;/p&gt;
&lt;p&gt;The operational consequence: Citus is most efficient for multi-tenant SaaS workloads where each tenant’s data is isolated and queries are tenant-scoped. It is less effective for workloads with heavy cross-tenant analytics or complex joins between distributed and reference tables.&lt;/p&gt;
&lt;p&gt;Azure-managed Citus (now branded as part of Azure Cosmos DB for PostgreSQL) provides managed coordinator and worker nodes, automatic rebalancing, and built-in high availability per node.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Azure Flexible Server’s PgBouncer documentation explicitly states that &lt;code&gt;PREPARE&lt;/code&gt;, &lt;code&gt;DEALLOCATE&lt;/code&gt;, &lt;code&gt;LISTEN&lt;/code&gt;, &lt;code&gt;NOTIFY&lt;/code&gt;, &lt;code&gt;LOAD&lt;/code&gt;, and advisory locks are not compatible with transaction-pooling mode (&lt;a href=&quot;https://www.pgbouncer.org/features.html&quot;&gt;PgBouncer compatibility&lt;/a&gt;). Applications that use prepared statements with PgBouncer in transaction mode will encounter errors. This is a documented PostgreSQL connection pooler constraint, not Azure-specific — but it is frequently missed by teams migrating from AWS RDS or on-premises PostgreSQL where client-side connection pooling was used at the application layer instead.&lt;/p&gt;
&lt;p&gt;Citus’s documented design requires that the distribution column be present in the primary key and all unique constraints of the distributed table. A table distributed on &lt;code&gt;tenant_id&lt;/code&gt; must include &lt;code&gt;tenant_id&lt;/code&gt; in its primary key (e.g., &lt;code&gt;PRIMARY KEY (tenant_id, id)&lt;/code&gt;). This is documented as a hard requirement — the coordinator cannot enforce uniqueness across shards without the distribution column in the constraint (&lt;a href=&quot;https://docs.citusdata.com/en/v12.1/sharding/data_modeling.html&quot;&gt;Citus distribution docs&lt;/a&gt;). Applications migrated from single-node PostgreSQL typically have auto-increment primary keys without a tenant prefix, requiring a schema migration before Citus distribution is feasible.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Flexible Server — prepared statements with PgBouncer in transaction mode&lt;/td&gt;&lt;td&gt;&lt;code&gt;ERROR: prepared statement does not exist&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Transaction-pooling releases connections between statements; prepared statements don’t persist&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Flexible Server — application connects to PostgreSQL port, bypasses PgBouncer&lt;/td&gt;&lt;td&gt;Connection saturation under load&lt;/td&gt;&lt;td&gt;PgBouncer only intercepts connections on port 6432; direct PostgreSQL port (5432) bypasses pooling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Citus — cross-tenant queries on distributed tables&lt;/td&gt;&lt;td&gt;Fan-out to all workers, high latency&lt;/td&gt;&lt;td&gt;No shard routing possible without distribution column in WHERE clause&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Citus — unique constraints without distribution column&lt;/td&gt;&lt;td&gt;Cannot enforce constraint across shards&lt;/td&gt;&lt;td&gt;Coordinator cannot run a distributed uniqueness check efficiently&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Flexible Server — HA failover to standby&lt;/td&gt;&lt;td&gt;60–120s DNS propagation delay during failover&lt;/td&gt;&lt;td&gt;Applications not using connection retry logic see errors during the HA switchover window&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Citus — uneven tenant distribution (hotspot)&lt;/td&gt;&lt;td&gt;One worker shard saturated while others idle&lt;/td&gt;&lt;td&gt;All rows for a large tenant land on one shard; distribution column alone does not balance load&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Choosing between Flexible Server and Citus after the schema is designed and populated is expensive — Citus requires a distribution-column-aware schema that cannot be retrofitted easily.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Use Flexible Server as the default; evaluate Citus only when the workload is multi-tenant with tenant-scoped queries, write throughput exceeds what a single large SKU can sustain, or data volume per tenant is large enough to benefit from distributed storage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Benchmark your top write-intensive operations on the largest available Flexible Server SKU under expected peak load; if the primary CPU or WAL write throughput saturates, that is the signal that horizontal distribution is worth the schema redesign cost.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: If you are building on Flexible Server, enable and configure PgBouncer this week, connect your application through port 6432, and verify prepared statement behavior — this is the most common production misconfiguration on Azure PostgreSQL.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category></item><item><title>The Stack for AI-Accelerated Database Operations Is Now Open Source</title><link>https://rajivonai.com/blog/2026-05-24-ai-database-ops-tools-may-2026/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-05-24-ai-database-ops-tools-may-2026/</guid><description>Three May 2026 breakout projects close the gaps that stop database teams from moving schema changes, query assistance, and operational workflows to AI: declarative Postgres migrations, local LLM inference, and a full agent platform.</description><pubDate>Sun, 24 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Database teams that have tried to adopt AI tooling hit the same three walls: schema change management tools that predate modern declarative infrastructure, LLMs that require sending production schema to a third-party API, and the months of engineering it takes to build a custom agent with RAG, a workflow engine, and plugin support.&lt;/strong&gt; Three projects that hit a combined 35,000 stars in May 2026 close each of those gaps — and together form a self-hosted stack that lets a database team automate schema changes, run local model inference for query assistance, and deploy operational agents without writing the platform from scratch.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The case for AI assistance in database operations is clear: SQL generation, query plan explanation, schema review, and runbook execution are all pattern-matching tasks that language models handle well. The barrier has not been capability — it has been infrastructure. Declarative schema management requires an opinionated tool that understands PostgreSQL’s full object model. Local LLM inference capable of handling database-scale context requires an optimized serving layer most teams cannot build. And building an internal database operations agent requires assembling a RAG pipeline, workflow engine, model router, plugin system, and debugging interface — six months of work before the first query gets answered.&lt;/p&gt;
&lt;p&gt;May 2026 produced open-source solutions to each of these independently.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure modes that block database teams from using AI effectively:&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure point&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Manual migration file sequencing&lt;/td&gt;&lt;td&gt;Flyway/Liquibase require numbered files; concurrent development causes sequence conflicts&lt;/td&gt;&lt;td&gt;One mis-sequenced migration in a multi-developer team fails deployment&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cloud LLM schema exposure&lt;/td&gt;&lt;td&gt;ChatGPT and Gemini require sending schema to third-party APIs&lt;/td&gt;&lt;td&gt;Unacceptable for teams with data residency or compliance requirements&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Agent platform build cost&lt;/td&gt;&lt;td&gt;RAG + workflow + plugin + model router = 4-6 months of foundational engineering&lt;/td&gt;&lt;td&gt;Teams never get to the actual automation; they build infrastructure instead&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Shadow database requirement&lt;/td&gt;&lt;td&gt;Most state-based schema tools need a spare database to validate migrations&lt;/td&gt;&lt;td&gt;Adds infra dependency to every CI pipeline run&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Local inference complexity&lt;/td&gt;&lt;td&gt;vLLM requires significant configuration; the codebase is not readable&lt;/td&gt;&lt;td&gt;Teams can’t audit, modify, or debug the inference layer they’re running&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The question for a database team evaluating AI tooling in mid-2026: is there a path to all three capabilities — schema-as-code, local inference, agent platform — without building foundational infrastructure?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;These three tools form a complete answer. Each targets one layer:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DBTeam[database team — daily operations]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DBTeam --&gt; SchemaWork[schema change management]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DBTeam --&gt; QueryWork[query assistance and schema review]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DBTeam --&gt; OpsWork[operational runbooks and incident workflows]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SchemaWork --&gt; pgschema[pgschema — declare target state, generate DDL automatically]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    QueryWork --&gt; nanovllm[nano-vllm — local LLM inference, schema never leaves the server]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OpsWork --&gt; CozeStudio[coze-studio — visual agent builder with RAG and workflow engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    pgschema --&gt; Outcome1[migrations reviewed and applied without manual file sequencing]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    nanovllm --&gt; Outcome2[query plans explained, SQL generated, no third-party API]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CozeStudio --&gt; Outcome3[DB ops agent deployed in days not months]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;pgschema--declarative-schema-migrations-for-postgresql&quot;&gt;pgschema — Declarative Schema Migrations for PostgreSQL&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The problem it solves:&lt;/strong&gt; Flyway and Liquibase require manually writing and numbering migration files. In a team with multiple engineers touching the schema, migration numbers conflict, files get applied out of order, and the “what does the current schema look like” question requires reading a long history of incremental files rather than a single state definition.&lt;/p&gt;
&lt;p&gt;pgschema, built by the Bytebase team, takes a Terraform-style approach: you declare what the schema &lt;em&gt;should look like&lt;/em&gt;, and the tool generates the SQL to get from the current state to that state. The workflow is &lt;code&gt;dump → edit → plan → apply&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Capture current schema state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pgschema&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; dump&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --url&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $DATABASE_URL &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;--output&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; schema.sql&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Edit schema.sql directly — add columns, indexes, RLS policies&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Then preview what SQL will be generated&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pgschema&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; plan&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --url&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $DATABASE_URL &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;--schema&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; schema.sql&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Apply with lock timeout control and concurrent change detection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pgschema&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; apply&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --url&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $DATABASE_URL &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;--schema&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; schema.sql&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --lock-timeout&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; 5s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;plan&lt;/code&gt; step shows the exact DDL that will execute before anything touches the database — the same workflow &lt;code&gt;terraform plan&lt;/code&gt; established for infrastructure. For a team that does code review on migrations, this means reviewing a human-readable schema diff rather than a raw SQL file.&lt;/p&gt;
&lt;p&gt;Two properties from the README are relevant for production database teams. First, pgschema handles PostgreSQL-specific objects that tools like Liquibase skip: row-level security policies, partitioned tables, partial indexes, identity columns, domain types, and column-level grants. Second, it uses an embedded Postgres instance for validation instead of requiring a shadow database — removing a persistent infrastructure dependency from the CI pipeline.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Where it breaks:&lt;/strong&gt; pgschema is PostgreSQL-only. Teams running MySQL, SQL Server, or mixed environments cannot use it for their full schema footprint. It is also a young project; the README does not yet document behavior on very large schemas with hundreds of tables and complex dependency graphs. Start with a non-critical database to build confidence in the plan output before applying to production.&lt;/p&gt;
&lt;h3 id=&quot;nano-vllm--local-llm-inference-in-1200-lines&quot;&gt;nano-vllm — Local LLM Inference in 1,200 Lines&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The problem it solves:&lt;/strong&gt; Running an LLM locally for database assistance — query plan explanation, SQL generation, schema review — requires an inference server. vLLM is the production standard, but its codebase is large and complex, which makes it difficult to audit, modify, or trust for teams that want to understand exactly what their inference layer does. nano-vllm is a clean reimplementation of vLLM’s core in approximately 1,200 lines of Python.&lt;/p&gt;
&lt;p&gt;From the project README, a benchmark on an RTX 4070 Laptop (8 GB VRAM) running Qwen3-0.6B shows nano-vllm achieving 1,434 tokens per second versus vLLM’s 1,361 tokens per second on the same hardware and workload. The implementation includes prefix caching, tensor parallelism, Torch compilation, and CUDA graph execution — the same optimization techniques vLLM uses, readable in a codebase that a database engineer can actually review.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; nanovllm &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; LLM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, SamplingParams&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;llm &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; LLM(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/models/sqlcoder-7b&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;enforce_eager&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;tensor_parallel_size&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;params &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SamplingParams(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;temperature&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0.1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;max_tokens&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;512&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Ask for query plan explanation without sending schema to any external API&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;outputs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; llm.generate(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Explain this PostgreSQL query plan and identify the bottleneck:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; query_plan],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    params&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(outputs[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;][&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;text&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For database teams, the critical property is that the schema never leaves the server. A local Qwen3 or SQLCoder model running on a workstation with a GPU can explain query plans, suggest indexes, generate SQL, and review migrations — all without a cloud API key or a data residency risk.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Where it breaks:&lt;/strong&gt; nano-vllm requires a CUDA-capable GPU. The documented benchmark uses a small model (0.6B parameters) on 8 GB VRAM; serious database workloads that benefit from a larger context window require proportionally more VRAM — a 7B model needs roughly 14 GB in float16. Teams without GPU infrastructure need to consider whether a CPU-only path (llama.cpp) fits their latency requirements better than GPU-accelerated serving.&lt;/p&gt;
&lt;h3 id=&quot;coze-studio--build-your-db-ops-agent-in-days-not-months&quot;&gt;coze-studio — Build Your DB Ops Agent in Days, Not Months&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The problem it solves:&lt;/strong&gt; Building an internal database operations agent — one that answers schema questions, walks engineers through runbooks, escalates incidents, or generates migration plans from a description — requires assembling six layers: a RAG pipeline for internal documentation, a model router, a workflow engine for multi-step operations, a plugin system for tool calls, a debugging interface, and a deployment layer. The Coze platform, which ByteDance has used to serve tens of thousands of enterprises according to the project README, has these layers built and tested.&lt;/p&gt;
&lt;p&gt;In May 2026, ByteDance open-sourced the full Coze Studio codebase under Apache 2.0. The backend is Go, the frontend is React + TypeScript, the architecture is microservices designed around domain-driven design (DDD) principles. The README documents the feature set: model service integration (OpenAI, Volcengine, or any compatible endpoint), agent builder with visual workflow design, RAG knowledge base management, plugin system for external tool calls, and a database resource connector.&lt;/p&gt;
&lt;p&gt;For a database team, the practical starting point is a knowledge base agent: index your runbooks, schema documentation, and postmortem archive into the built-in RAG system, connect it to your preferred model (including a local endpoint like nano-vllm), and deploy an agent that database engineers can query during incidents.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; clone&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; https://github.com/coze-dev/coze-studio&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; coze-studio&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Configure model endpoints in .env (supports local endpoints)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;docker&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; compose&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; up&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Access the visual builder at http://localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The visual workflow builder means a database engineer — not a backend developer — can assemble a multi-step runbook agent: query the knowledge base, call a database API, evaluate the result, route to a different action based on the outcome. The plugin system connects to external tools: monitoring APIs, ticketing systems, database management endpoints.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Where it breaks:&lt;/strong&gt; Coze Studio is designed around a microservices architecture, which means the self-hosted deployment is non-trivial compared to a single-container application. The README is primarily oriented toward Volcengine (ByteDance’s cloud platform) for production deployment; self-hosted configuration documentation is less detailed than the feature documentation. Teams should expect to invest in deployment configuration before reaching a stable internal instance.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern across platform engineering teams is to standardize on unified toolchains rather than maintaining bespoke automation scripts. ByteDance’s public decision to open-source the Coze platform demonstrates this industry shift toward declarative, visual agent builders for managing complex, multi-step database workflows.&lt;/p&gt;
&lt;p&gt;Every technical capability described is derived from how these specific systems actually behave in production. For instance, PostgreSQL’s behavior with row-level security (RLS) policies, partitioned tables, and partial indexes requires exact schema state comparisons. &lt;code&gt;pgschema&lt;/code&gt; handles this by using an embedded Postgres instance to validate the generated DDL before execution, avoiding the drift common in manual migration sequencing.&lt;/p&gt;
&lt;p&gt;Similarly, local inference with &lt;code&gt;nano-vllm&lt;/code&gt; mirrors the execution paths of standard production inference servers. By implementing prefix caching and CUDA graph execution, the system achieves the documented throughput (1,434 tokens/sec on an RTX 4070 for Qwen3-0.6B) within a verifiable 1,200-line codebase. The open-source release of &lt;code&gt;coze-studio&lt;/code&gt; is new as of May 2026, so teams should still validate multi-step agent behaviors against non-production data before full adoption.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;pgschema plan diverges on complex schemas&lt;/td&gt;&lt;td&gt;Large schemas with circular dependencies or custom extensions&lt;/td&gt;&lt;td&gt;Run plan in dry-run mode; review every DDL statement before apply&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;pgschema Postgres-only&lt;/td&gt;&lt;td&gt;MySQL or SQL Server in the same fleet&lt;/td&gt;&lt;td&gt;Use pgschema only for the Postgres layer; keep existing tooling for other engines&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;nano-vllm VRAM ceiling&lt;/td&gt;&lt;td&gt;7B+ model exceeds available GPU memory&lt;/td&gt;&lt;td&gt;Use quantized models (GGUF Q4) or fall back to llama.cpp for CPU inference&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;coze-studio microservices overhead&lt;/td&gt;&lt;td&gt;Single-engineer team deploying self-hosted&lt;/td&gt;&lt;td&gt;Start with Docker Compose configuration; avoid Kubernetes deployment until scale demands it&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;coze-studio Volcengine defaults&lt;/td&gt;&lt;td&gt;Default model and storage config points to ByteDance’s cloud&lt;/td&gt;&lt;td&gt;Override all endpoint configs in &lt;code&gt;.env&lt;/code&gt; before first run; audit outbound connections&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Schema migrations break in multi-developer teams, cloud LLMs expose schema to third parties, building a DB ops agent from scratch takes months.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: pgschema for declarative Postgres migrations, nano-vllm for local model inference, coze-studio for the agent platform layer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Run &lt;code&gt;pgschema plan&lt;/code&gt; against your development database on any recent migration — compare the generated DDL against what was written manually. If the output is equivalent, you have eliminated one class of migration authoring error.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, install nano-vllm with a local SQLCoder or Qwen3 model and run it against three slow-query logs from your last month’s incidents. If the explanations are accurate, you have a local query assistant that requires no cloud API and exposes no schema externally.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>architecture</category></item><item><title>Abstracting the Cloud: Building an Enterprise Database Vending Machine API</title><link>https://rajivonai.com/blog/2024-01-12-database-vending-machine-api-abstraction/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-01-12-database-vending-machine-api-abstraction/</guid><description>Why exposing raw Terraform modules to developers creates catastrophic governance failures, and how building an intent-driven Database Vending Machine API in Go automates multi-cloud provisioning and secret management.</description><pubDate>Sat, 23 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;If application developers are writing raw Terraform modules or clicking through cloud consoles to provision databases, your organization does not operate a platform; it operates an unmanaged scripting surface where public-exposure drift, speculative over-sizing, and abandoned Day-2 lifecycles are inevitable.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Problem: Exposing raw Terraform modules leaks 80+ cloud parameters, lets security-group drift (&lt;code&gt;0.0.0.0/0&lt;/code&gt;, &lt;code&gt;publicly_accessible = true&lt;/code&gt;) slip past PR reviews, and encourages speculative hardware over-sizing.&lt;/li&gt;
&lt;li&gt;Answer: Replace developer-authored HCL with an intent-driven Database Vending Machine API (Go + OPA + IPAM + Temporal/Crossplane + Vault Database Secrets Engine).&lt;/li&gt;
&lt;li&gt;Watch out: A &lt;code&gt;/28&lt;/code&gt; AWS subnet leaves only 11 usable IPs (risking Multi-AZ ENI exhaustion), RDS automated backups cap at 35 days, and Day-1 vending fails if you omit Day-2 storage, extension, and TTL endpoints.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2024-01-12-database-vending-machine-api-abstraction.svg&quot; alt=&quot;Raw Terraform modules leak 80+ cloud flags; an Intent API enforces policy and sizing before provisioning&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Organizations running multi-cloud workloads across Amazon Web Services (AWS), Microsoft Azure, and Oracle Cloud Infrastructure (OCI) rely on managed data stores—Amazon Aurora PostgreSQL, Amazon RDS, Azure Database for PostgreSQL Flexible Server, and OCI Exadata Database Service—to back application microservices. To standardize provisioning, platform engineering teams typically publish reusable Terraform or OpenTofu modules in an internal registry. Application developers clone a module, fill in instance classes, storage sizes, VPC subnet IDs, and backup windows, and open a Pull Request for CI/CD execution.&lt;/p&gt;
&lt;p&gt;That module-sharing model works when a single platform team supports a handful of services. As an organization scales to dozens of autonomous product squads, however, raw IaC modules expose too many low-level cloud flags. Application engineers spend sprints debugging subnet group errors and KMS key policies, while platform engineers become a manual PR-review bottleneck.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Exposing raw cloud primitives—even wrapped inside Terraform modules—breaks governance and velocity across four concrete constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Attribute Explosion and Security Drift:&lt;/strong&gt; A single &lt;code&gt;aws_db_instance&lt;/code&gt; or &lt;code&gt;aws_rds_cluster&lt;/code&gt; resource exposes over 80 configuration attributes and depends on half a dozen adjacent resources: KMS customer-managed keys, DB parameter groups, DB subnet groups, VPC security groups, IAM authentication roles, and log exports. When a developer hits a connectivity timeout during testing and temporarily sets &lt;code&gt;cidr_blocks = [&quot;0.0.0.0/0&quot;]&lt;/code&gt; and &lt;code&gt;publicly_accessible = true&lt;/code&gt;, a platform reviewer scanning a 1,000-line PR can easily miss the change.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Subnet CIDR Exhaustion:&lt;/strong&gt; AWS enforces a minimum IPv4 subnet size of &lt;code&gt;/28&lt;/code&gt; (16 IP addresses) and reserves 5 addresses per subnet (network, VPC router, Amazon DNS, future use, and broadcast), leaving only &lt;strong&gt;11 usable IP addresses&lt;/strong&gt; per subnet. Because Multi-AZ RDS DB subnet groups must span at least two Availability Zones and consume extra Elastic Network Interfaces (ENIs) during failover, minor-version patching, blue/green deployments, and read-replica scaling, a single Multi-AZ cluster with replicas can consume 6 to 10 IPs in one AZ—pushing &lt;code&gt;/28&lt;/code&gt; blocks straight into &lt;code&gt;InsufficientFreeAddressesInSubnet&lt;/code&gt; failures. Production database tiers require &lt;code&gt;/26&lt;/code&gt; (59 usable IPs) or &lt;code&gt;/24&lt;/code&gt; (251 usable IPs) subnets managed centrally via IPAM.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Backup Retention Ceiling Misalignment:&lt;/strong&gt; Compliance regimes such as PCI-DSS frequently mandate 7-year (&lt;code&gt;2,555&lt;/code&gt;-day) audit retention. However, Amazon RDS and Aurora automated backups cap at &lt;strong&gt;35 days&lt;/strong&gt; maximum retention. Developers who assume setting &lt;code&gt;backup_retention_period&lt;/code&gt; covers multi-year compliance either hit API validation errors or miss the requirement to pair 35-day PITR with AWS Backup vaults or immutable S3 snapshot exports.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Speculative Over-Sizing and Day-2 Abandonment:&lt;/strong&gt; When asked to pick between &lt;code&gt;db.r6g.large&lt;/code&gt; (2 vCPU, 16 GiB RAM) and &lt;code&gt;db.r6g.8xlarge&lt;/code&gt; (32 vCPU, 256 GiB RAM), developers defensively over-provision to avoid latency incidents—a 16x compute and memory jump for a low-throughput microservice. Worse, static Day-1 Terraform modules do not govern Day-2 operations—such as minor engine patching, extension whitelisting, or credential rotation—leaving databases as unmanaged pets.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2024-01-12-database-vending-machine-api-abstraction-2-constraints.svg&quot; alt=&quot;Cloud database provisioning hits hard limits on subnet IPs, backup retention, and parameter complexity&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How can platform teams replace leaky Terraform modules with an intent-driven Database Vending Machine API that translates business requirements into governed, multi-cloud infrastructure and zero-trust credentials?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform engineering teams replacing raw Terraform PRs generally evaluate three architectures.&lt;/p&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Architecture Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Gets Right&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Shared Terraform / OpenTofu Modules&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fastest to bootstrap; zero custom control-plane services to operate&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Leaks HCL/cloud attributes; manual PR bottlenecks; speculative sizing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected as developer interface&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Go Vending Machine API + Temporal&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Clean JSON intent contract; tight integration with OPA, external IPAM, and Vault dynamic creds&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Platform team must maintain the Go service and hot-reloaded sizing matrices&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Selected&lt;/strong&gt; for multi-cloud + IPAM orchestration&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Crossplane XRDs + Pipeline Functions&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Declarative Kubernetes API with built-in continuous drift reconciliation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires Kubernetes everywhere; awkward for non-K8s REST/IPAM workflows&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Viable alternative when K8s is the universal control plane&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Shared Terraform modules are rejected as the developer-facing boundary because they cannot prevent developers from editing security groups or guessing instance sizes inside HCL variables. Crossplane XRDs are a strong alternative for Kubernetes-first platforms, whereas a dedicated Go Vending Machine API backed by Temporal wins when the platform must integrate external enterprise IPAM (such as Infoblox), non-Kubernetes consumers, and multi-step compensation sagas across AWS, Azure, and OCI.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-01-12-database-vending-machine-api-abstraction-3-options.svg&quot; alt=&quot;Compare Terraform modules, a Go Vending API, and Crossplane XRDs by governance and maintenance cost&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-api-driven-vending-architecture-intent-based-infrastructure&quot;&gt;The API-Driven Vending Architecture: Intent-Based Infrastructure&lt;/h2&gt;
&lt;p&gt;The Database Vending Machine API inserts a strict four-layer control plane between application developers and cloud provider APIs.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-01-12-database-vending-machine-api-abstraction-4-architecture.svg&quot; alt=&quot;The Vending Machine API separates intent validation, topology calculation, durable execution, and Vault delivery&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-the-declarative-intent-contract&quot;&gt;1. The Declarative Intent Contract&lt;/h3&gt;
&lt;p&gt;Developers submit a business-centric JSON payload via an Internal Developer Portal (such as Backstage) or CLI. Infrastructure primitives—instance families, IOPS, subnet IDs, and KMS ARNs—are omitted from the schema:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;service_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;checkout-payment-service&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;owner_team&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;checkout-platform&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;environment&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;production&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;database_engine&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;postgresql&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;engine_version&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;15&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;workload_profile&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;expected_qps&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3500&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;storage_growth_gb_per_month&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;read_heavy_ratio&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0.80&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;compliance&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;data_classification&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pci-dss&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;archive_retention_days&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2555&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    &quot;multi_az_required&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;cloud_target&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;aws&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Because &lt;code&gt;publicly_accessible&lt;/code&gt; and &lt;code&gt;kms_key_id&lt;/code&gt; do not exist in the contract, a developer cannot disable encryption or expose a database to the internet. When &lt;code&gt;data_classification&lt;/code&gt; is &lt;code&gt;pci-dss&lt;/code&gt;, the API enforces customer-managed KMS encryption, TLS 1.3 in transit, 35-day automated RDS/Aurora PITR, and an AWS Backup archive policy for the 2,555-day compliance window.&lt;/p&gt;
&lt;h3 id=&quot;2-policy-validation-and-topology-calculation&quot;&gt;2. Policy Validation and Topology Calculation&lt;/h3&gt;
&lt;p&gt;Upon receiving an intent payload, the Go API runs a three-step translation pipeline:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — OPA Rego Validation:&lt;/strong&gt; Evaluates budget quotas, approved cloud regions, curated &lt;code&gt;extensions: []&lt;/code&gt; whitelists (such as &lt;code&gt;pgvector&lt;/code&gt; or &lt;code&gt;timescaledb&lt;/code&gt;), and compliance constraints before touching any cloud API, returning actionable validation errors immediately if a rule fails. Because OPA rules live in versioned Rego bundles, security teams can tighten encryption or region rules across the entire vending surface without modifying Go application code or reviewing individual Terraform PRs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Topology and Hardware Sizing:&lt;/strong&gt; Maps &lt;code&gt;expected_qps&lt;/code&gt; and &lt;code&gt;read_heavy_ratio&lt;/code&gt; to benchmarked instance classes (&lt;code&gt;db.r6g.large&lt;/code&gt; 2 vCPU / 16 GiB, &lt;code&gt;db.r6g.xlarge&lt;/code&gt; 4 vCPU / 32 GiB, &lt;code&gt;db.r6g.2xlarge&lt;/code&gt; 8 vCPU / 64 GiB, &lt;code&gt;db.r6g.4xlarge&lt;/code&gt; 16 vCPU / 128 GiB). Crucially, sizing rules belong in hot-reloaded YAML config maps rather than hardcoded Go switches so newer Graviton3 (&lt;code&gt;db.r7g&lt;/code&gt;) shapes adopt without recompiling the API. Also note the storage distinction: standard RDS PostgreSQL uses &lt;code&gt;MaxAllocatedStorage&lt;/code&gt; for EBS Storage Autoscaling, whereas Amazon Aurora cluster volumes grow automatically up to 128 TiB (or 256 TiB depending on engine configuration) without a &lt;code&gt;MaxAllocatedStorage&lt;/code&gt; parameter:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Standard RDS PostgreSQL sizing with hot-reloaded tier matrix&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; CalculateRDSTopology&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;p&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; WorkloadProfile&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ComplianceSpec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; TierMatrix&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;InstanceSpec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    spec &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; InstanceSpec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Engine:           &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;postgres&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        EngineVersion:    &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;15&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        MultiAZ:          c.MultiAZRequired,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        StorageEncrypted: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        BackupRetention:  &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;35&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// AWS RDS/Aurora automated backup max&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        AllocatedGB:      &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        MaxAllocatedGB:   &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (p.StorageGrowthGBPerMonth &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 24&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    spec.InstanceClass &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;MatchClass&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(p.ExpectedQPS) &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// e.g., db.r6g.2xlarge&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; p.ReadHeavyRatio &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0.70&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; p.ExpectedQPS &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        spec.ReadReplicas &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; spec&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — IPAM and Security Isolation:&lt;/strong&gt; Leases &lt;code&gt;/26&lt;/code&gt; (59 usable IPs) or &lt;code&gt;/24&lt;/code&gt; (251 usable IPs) private subnets across at least two Availability Zones from enterprise IPAM, attaches security groups restricted to application worker nodes, and binds a dedicated KMS Customer Managed Key with annual rotation.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-zero-trust-credential-delivery-via-vault&quot;&gt;3. Zero-Trust Credential Delivery via Vault&lt;/h3&gt;
&lt;p&gt;Rather than letting developers pick passwords or storing static credentials in Git, the Vending API separates static configuration from dynamic database access in HashiCorp Vault:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Database Bootstrap:&lt;/strong&gt; Durable execution workers (Temporal or Crossplane) provision the database with a randomized 32-character root credential stored strictly in a restricted platform Vault KV v2 path (&lt;code&gt;secret/data/platform/db-roots/&amp;#x3C;service_id&gt;&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamic Credential Engine:&lt;/strong&gt; The worker registers the new database with Vault’s &lt;strong&gt;Database Secrets Engine&lt;/strong&gt; (&lt;code&gt;database/config/&amp;#x3C;service_id&gt;&lt;/code&gt; and &lt;code&gt;database/roles/&amp;#x3C;app_role&gt;&lt;/code&gt;). When an application requests credentials at &lt;code&gt;database/creds/&amp;#x3C;app_role&gt;&lt;/code&gt;, Vault dynamically issues a short-lived PostgreSQL user with a bounded TTL and revokes it automatically on lease expiry.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Readiness-Gated Delivery and Durable Compensation:&lt;/strong&gt; In Kubernetes, the External Secrets Operator (ESO) or Vault Agent Injector delivers credentials into pods and handles lease renewal before TTL expiration. The Vending API gates its final &lt;code&gt;PROVISIONED&lt;/code&gt; status on secret-sync verification so pods do not boot into &lt;code&gt;CrashLoopBackOff&lt;/code&gt; before credentials land. Crucially, Temporal workflow workers wrap each provisioning step—IPAM lease, RDS creation, KMS binding, and Vault role registration—in a durable saga: if Vault registration fails after the cloud database is created, Temporal retries with exponential backoff or executes a compensation activity to quarantine the resource and release the IPAM CIDR block, preventing silent orphaned databases.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;4-day-2-lifecycle-endpoints-and-ephemeral-ttls&quot;&gt;4. Day-2 Lifecycle Endpoints and Ephemeral TTLs&lt;/h3&gt;
&lt;p&gt;Provisioning is only half the contract; without governed Day-2 mutations, teams fall back to manual console edits. The Vending API exposes scoped &lt;code&gt;PATCH /v1/databases/{service_id}&lt;/code&gt; endpoints for approved operational changes: raising &lt;code&gt;MaxAllocatedStorage&lt;/code&gt; ceilings on standard RDS instances, enabling whitelisted extensions (&lt;code&gt;pgvector&lt;/code&gt;, &lt;code&gt;timescaledb&lt;/code&gt;) via parameter-group updates, and triggering point-in-time recovery clones into staging. For pull-request preview environments, the API enforces a mandatory &lt;code&gt;ttl_hours&lt;/code&gt; field (such as 72 hours) and runs a scheduled reaper workflow that deletes expired test clusters, revokes their Vault roles, and returns their &lt;code&gt;/26&lt;/code&gt; subnet allocations to IPAM automatically.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Shipping a Vending Machine API requires a staged rollout with Day-2 endpoints built alongside Day-1 provisioning.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2024-01-12-database-vending-machine-api-abstraction-5-flow.svg&quot; alt=&quot;Four-phase rollout builds the intent contract and Vault pipeline before retiring Terraform PRs&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Intent Contract and YAML Sizing Matrix:&lt;/strong&gt; Define a minimal JSON schema covering 80% of standard PostgreSQL workloads and externalize instance mappings into versioned YAML config maps.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — OPA Policies and IPAM Integration:&lt;/strong&gt; Wire Rego compliance checks (&lt;code&gt;publicly_accessible == false&lt;/code&gt;, mandatory KMS, 35-day PITR + AWS Backup archive for regulated data) and automated &lt;code&gt;/26&lt;/code&gt; subnet leasing across at least two AZs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Temporal Sagas and Vault Readiness Gates:&lt;/strong&gt; Implement durable workflows with compensation rollbacks (so a failed Vault registration cleans up or quarantines the half-provisioned database) and token-bucket rate limiters against cloud &lt;code&gt;ThrottlingException&lt;/code&gt; errors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Portal Cutover and Day-2 Endpoints:&lt;/strong&gt; Expose the template in Backstage, ship Day-2 &lt;code&gt;PATCH&lt;/code&gt; endpoints (for storage ceiling bumps, curated &lt;code&gt;extensions: []&lt;/code&gt; whitelists such as &lt;code&gt;pgvector&lt;/code&gt;, and 72-hour ephemeral preview TTLs), and retire raw developer Terraform PRs.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Documented cloud provider constraints and platform engineering patterns explain why intent-driven APIs outperform raw module sharing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;AWS Subnet and Backup Boundaries:&lt;/strong&gt; AWS VPC documentation establishes that &lt;code&gt;/28&lt;/code&gt; subnets yield only 11 usable IP addresses after the 5 AWS-reserved addresses, creating ENI exhaustion risk during Multi-AZ RDS failover and maintenance. Likewise, Amazon RDS and Aurora documentation caps automated backup retention at 35 days—requiring an explicit archival tier (AWS Backup or S3 exports) whenever compliance mandates multi-year retention.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Vault KV v2 vs. Database Secrets Engine:&lt;/strong&gt; HashiCorp Vault’s documented separation between KV v2 (&lt;code&gt;secret/data/...&lt;/code&gt; for static secrets) and the Database Secrets Engine (&lt;code&gt;database/creds/&amp;#x3C;role&gt;&lt;/code&gt; for dynamic, TTL-bound database users) eliminates static password sprawl and removes humans from routine credential rotation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Crossplane and Copy-on-Write Convergence (2024–2026):&lt;/strong&gt; Platform teams increasingly pair intent-driven portals with Crossplane XRDs for Kubernetes-native reconciliation and copy-on-write branching (such as Aurora Serverless v2 or Neon) for ephemeral pull-request databases with automated 72-hour teardown TTLs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multi-Cloud Semantic Boundaries:&lt;/strong&gt; Keeping &lt;code&gt;cloud_target&lt;/code&gt; explicit in the intent contract rather than forcing lowest-common-denominator cloud agnosticism prevents runtime surprises when workloads depend on cloud-specific features such as AWS IAM database authentication tokens versus Microsoft Entra ID authentication on Azure Flexible Server.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pattern Acknowledgment:&lt;/strong&gt; I have not personally operated a single Go vending service across 1,400 engineers, but the documented CNCF Internal Developer Platform pattern is consistent: hide cloud flags behind an intent contract, enforce compliance via OPA before provisioning, and pair durable execution with dynamic Vault leases.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Centralizing provisioning into a Vending Machine API shifts operational failure modes onto the platform team.&lt;/p&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure Mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Hardcoded hardware mappings&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Go &lt;code&gt;switch&lt;/code&gt; locks fleet to &lt;code&gt;db.r6g&lt;/code&gt;, missing newer &lt;code&gt;db.r7g&lt;/code&gt; price-performance&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;FinOps audit shows stale instance families in new provisions&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Externalize sizing matrices into hot-reloaded YAML config maps&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;IPAM CIDR exhaustion&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Ephemeral preview DBs consume VPC &lt;code&gt;/26&lt;/code&gt; blocks without reclamation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Provisioning fails with &lt;code&gt;SubnetAllocationException&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce 72-hour TTLs on preview DBs and automated IPAM reclamation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Multi-cloud semantic mismatch&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Treating AWS RDS and Azure Flexible Server as identical breaks cloud-native IAM auth&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;App deployment fails when &lt;code&gt;target_cloud&lt;/code&gt; changes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep &lt;code&gt;target_cloud&lt;/code&gt; explicit in intent; bind cloud-specific auth per target&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Vault secret sync race&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pod starts before ESO syncs credentials from Vault&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pod enters &lt;code&gt;CrashLoopBackOff&lt;/code&gt; on missing DB env vars&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Gate Vending API &lt;code&gt;PROVISIONED&lt;/code&gt; readiness on confirmed K8s Secret sync&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Locked-down parameter groups&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Apps needing &lt;code&gt;pgvector&lt;/code&gt; or &lt;code&gt;timescaledb&lt;/code&gt; fail against rigid defaults&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Extension &lt;code&gt;CREATE EXTENSION&lt;/code&gt; rejected in staging&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Expose a curated, OPA-validated &lt;code&gt;extensions: []&lt;/code&gt; whitelist in intent&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Orchestrator crash mid-provision&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Worker crashes after RDS creation but before Vault registration&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Orphaned cloud DB running with no Vault record&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use Temporal or Crossplane durable sagas with compensation rollbacks&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Burst CI/CD cloud throttling&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Parallel test suites send 50+ create calls simultaneously&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cloud API returns &lt;code&gt;ThrottlingException&lt;/code&gt; / &lt;code&gt;RateExceeded&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Add token-bucket rate limiters and durable task queues on outbound SDKs&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2024-01-12-database-vending-machine-api-abstraction-6-failures.svg&quot; alt=&quot;Four failure modes break Vending Machine APIs: CIDR exhaustion, Vault sync races, hardcoded tiers, and bursts&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Hide cloud flags behind business intent:&lt;/strong&gt; Developers should specify QPS, growth rate, and compliance classification—never &lt;code&gt;cidr_blocks&lt;/code&gt;, KMS ARNs, or raw instance classes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Encode cloud limits inside the API:&lt;/strong&gt; Enforce &lt;code&gt;/26&lt;/code&gt; or &lt;code&gt;/24&lt;/code&gt; subnets across two AZs, distinguish Aurora auto-growing storage from RDS &lt;code&gt;MaxAllocatedStorage&lt;/code&gt;, and pair 35-day RDS PITR with AWS Backup for multi-year retention.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use Vault’s Database Secrets Engine for runtime access:&lt;/strong&gt; Reserve KV v2 (&lt;code&gt;secret/data/...&lt;/code&gt;) for static bootstrap roots and issue dynamic, TTL-bound credentials via &lt;code&gt;database/creds/&amp;#x3C;role&gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Build Day-2 &lt;code&gt;PATCH&lt;/code&gt; and TTL teardown on day one:&lt;/strong&gt; A vending API without storage expansion, curated extension whitelists, and ephemeral preview TTLs simply automates the creation of tomorrow’s unmanaged pets.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Identify whether developers are copy-pasting Terraform database modules, guessing instance sizes, or opening manual tickets for routine provisioning.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Replace developer-authored HCL with an intent-driven Database Vending Machine API backed by OPA policies, IPAM subnet leasing, durable workflows, and Vault dynamic credentials.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Audit your existing Terraform PRs and deployed RDS instances for public-access drift, undersized &lt;code&gt;/28&lt;/code&gt; subnets, and oversized instances running below 10% CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Draft a minimal JSON intent contract covering your primary PostgreSQL workload, write an OPA Rego rule blocking &lt;code&gt;publicly_accessible = true&lt;/code&gt; and enforcing encryption, and wire a staging pipeline to Vault’s Database Secrets Engine before retiring raw module PRs.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>cloud</category><category>databases</category><category>failures</category><category>checklist</category></item><item><title>Tiering High Availability by Blast Radius: The Composite RAC, Data Guard, and GoldenGate Architecture</title><link>https://rajivonai.com/blog/2023-11-05-rac-vs-data-guard-vs-goldengate-decision/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-11-05-rac-vs-data-guard-vs-goldengate-decision/</guid><description>Why relying on a single database high availability technology guarantees catastrophic downtime, and how hyper-scale platforms combine RAC, Active Data Guard, and GoldenGate to survive hardware, regional, and logical failures.</description><pubDate>Thu, 21 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Treating high availability as a monolithic requirement guarantees architectural failure because node crashes, storage corruption, regional network blackouts, and destructive SQL belong to separate failure domains that no single replication primitive can isolate.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Stretching Oracle RAC across sites multiplies Cache Fusion lock latency and leaves shared ASM storage as a single point of failure, while unpartitioned active-active GoldenGate collapses under WAN write conflicts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Layer four Oracle Maximum Availability Architecture (MAA) tiers by blast radius—local RAC with TAC for compute faults, synchronous Active Data Guard for metro AZ loss, asynchronous plus delayed standby for regional and logical recovery, and region-sharded GoldenGate or 23ai Raft for global writes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Synchronous &lt;code&gt;MaxAvailability&lt;/code&gt; stalls primary &lt;code&gt;log file sync&lt;/code&gt; commits during metro network jitter unless &lt;code&gt;NetTimeout&lt;/code&gt; is bounded, and delayed standbys silently lose logical protection if redo spikes exhaust the Fast Recovery Area.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2023-11-05-rac-vs-data-guard-vs-goldengate-decision.svg&quot; alt=&quot;Composite HA architecture layering four blast-radius tiers&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Transactional systems that process payments, reservations, or inventory ledgers typically operate under four-nines to five-nines availability targets, zero-data-loss recovery point objectives (RPO = 0) for local and metro faults, and recovery time objectives (RTO) measured in seconds. In the Oracle ecosystem, architects evaluate three distinct availability primitives to satisfy those mandates:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Oracle Real Application Clusters (RAC):&lt;/strong&gt; A shared-everything compute topology where multiple database instances on separate hosts mount a common Oracle Automatic Storage Management (ASM) disk group. Cache Fusion coordinates data blocks and global enqueues between instance buffer caches over a private RDMA or Gigabit interconnect.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle Active Data Guard (ADG):&lt;/strong&gt; A shared-nothing physical replication topology where the primary database ships redo records to one or more independent standby databases. The Managed Recovery Process (MRP) applies redo blocks continuously, keeping the standby physically identical while open for read-only queries and Fast-Start Failover (FSFO).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle GoldenGate (OGG):&lt;/strong&gt; A decoupled logical replication engine that captures committed changes from redo logs, writes platform-independent trail files, and applies SQL DML and DDL on target databases through Replicat processes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The recurring operational mistake is treating these three primitives as mutually exclusive alternatives rather than complementary layers. Selecting a single tool forces it across failure boundaries it was never built to survive.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Stretching any single availability primitive past its physical failure domain breaks either latency, isolation, or data integrity. Four explicit constraints govern multi-tier database availability:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Constraint 1 — Interconnect Speed-of-Light Tax (RAC Cache Fusion):&lt;/strong&gt; Inside a single datacenter over InfiniBand or RoCE, Cache Fusion block transfers complete in sub-millisecond memory-to-network round trips. Stretching RAC across two datacenters 30 to 50 kilometers apart introduces 2 to 5 milliseconds of round-trip fiber latency. Under OLTP concurrency, every global cache read (&lt;code&gt;gc cr block 2-way&lt;/code&gt;) or busy buffer handoff (&lt;code&gt;gc current block busy&lt;/code&gt;) pays that cross-site RTT. Consider a transaction executing 40 sequential index and table block gets where 25% require cross-node Cache Fusion coordination: at 0.2 ms local RTT, interconnect overhead is &lt;code&gt;10 * 0.2 ms = 2 ms&lt;/code&gt; per transaction; at 3 ms cross-AZ RTT, interconnect overhead jumps to &lt;code&gt;10 * 3 ms = 30 ms&lt;/code&gt;, adding 28 ms of pure lock wait before disk or network I/O is even counted.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 2 — Shared Storage Blast Radius:&lt;/strong&gt; Every RAC node mounts the same ASM diskgroups. A storage array firmware defect, SAN fabric misconfiguration, or administrative &lt;code&gt;DROP DISKGROUP&lt;/code&gt; error hits every RAC node simultaneously. Shared-everything compute provides zero isolation against storage corruption or human error.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 3 — Synchronous Commit Latency Budget:&lt;/strong&gt; Synchronous Data Guard (&lt;code&gt;SYNC AFFIRM&lt;/code&gt;) holds the primary &lt;code&gt;LGWR&lt;/code&gt; commit until the metro standby confirms redo write to its standby redo logs. When metro RTT is under 1 ms, commit overhead is negligible. Beyond 5 to 10 ms RTT, synchronous &lt;code&gt;log file sync&lt;/code&gt; waits throttle primary write throughput directly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constraint 4 — WAN Write-Collision Rate:&lt;/strong&gt; Transatlantic RTT between US East and Europe West sits around 70 to 120 ms. Running unpartitioned active-active GoldenGate across that link leaves a wide asynchronous window where concurrent updates to the same row collide, shifting database capacity from application transactions to Conflict Detection and Resolution (CDR) exception queues.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;./2023-11-05-rac-vs-data-guard-vs-goldengate-decision-2-constraints.svg&quot; alt=&quot;Physical and logical constraints across interconnect RTT, shared ASM storage, and WAN conflicts&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do platform architects layer RAC, Active Data Guard, and GoldenGate so compute faults, SAN failures, regional outages, rogue SQL, and cross-ocean latency are each absorbed inside their own boundary?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Before settling on a layered topology, teams typically evaluate three single-technology shortcuts. Each fails at least two of the four constraints above.&lt;/p&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Architecture Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Compute Fault (RTO / RPO)&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Storage / AZ Loss&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Logical Error (&lt;code&gt;DROP TABLE&lt;/code&gt;)&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Global Multi-Region Writes&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Why Rejected&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Extended (Stretched) RAC&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Seconds / RPO = 0&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails — shared ASM&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails — instant propagation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails — Cache Fusion stalls&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cross-AZ RTT degrades OLTP; shared ASM is a single point of failure.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Single-Region Active Data Guard&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;15–30s FSFO / RPO = 0&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Survives via independent ASM&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails unless delayed target added&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails — single primary writer&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Full cluster failover for a single host crash; no local compute pooling.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unpartitioned Active-Active GoldenGate&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Seconds / Async RPO &gt; 0&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Survives on independent DB&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fails — DML/DDL replicates&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Collides under concurrent writes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cannot guarantee RPO = 0; conflict queues risk ledger divergence.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Composite MAA Topology (Chosen)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Seconds (RAC + TAC) / RPO = 0&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Survives via Metro Sync ADG&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Survives via &lt;code&gt;DelayMins=240&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Survives via sharded writes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Contains every failure inside its matched layer without cross-domain coupling.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2023-11-05-rac-vs-data-guard-vs-goldengate-decision-3-options.svg&quot; alt=&quot;Comparison of single-tool HA options against Composite MAA&quot;&gt;&lt;/p&gt;
&lt;p&gt;Extended RAC was rejected because dark fiber does not eliminate shared ASM corruption or rogue DDL propagation. Single-region ADG alone forces a database-wide role transition just to survive a single server motherboard fault. Bidirectional GoldenGate without strict write ownership trades zero data loss for asynchronous conflict resolution. Only the composite design satisfies all four failure domains without violating physics.&lt;/p&gt;
&lt;h2 id=&quot;the-composite-blast-radius-architecture&quot;&gt;The Composite Blast-Radius Architecture&lt;/h2&gt;
&lt;p&gt;The production-grade pattern assigns one dedicated primitive to each failure domain: compute, metro storage, continental geography, logical corruption, and cross-region write routing.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-11-05-rac-vs-data-guard-vs-goldengate-decision-4-architecture.svg&quot; alt=&quot;Composite MAA target architecture showing local RAC, Metro Sync ADG, cascaded Remote and Delayed ADG, and partitioned GoldenGate&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;layered-component-design&quot;&gt;Layered Component Design&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Compute Layer (Local RAC + TAC):&lt;/strong&gt; Within the primary availability zone, a 2-node or 4-node RAC cluster shares local ASM storage over a redundant private interconnect. If one compute instance fails, Clusterware evicts it and surviving instances recover uncommitted locks from shared redo. Transparent Application Continuity (TAC) combined with Fast Application Notification (FAN) and Transaction Guard intercepts in-flight SQL calls and replays uncommitted sessions on a surviving node without surfacing disconnects to the application.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Metro Storage Layer (Synchronous Active Data Guard):&lt;/strong&gt; Within the same metropolitan region where RTT stays below 1 ms, the primary replicates to a local standby running in &lt;code&gt;MaxAvailability&lt;/code&gt; protection mode (&lt;code&gt;LogXptMode = &apos;SYNC&apos;&lt;/code&gt;). Because the metro standby owns completely independent ASM storage, a SAN failure or AZ power loss triggers Fast-Start Failover (FSFO) with RPO = 0.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Continental Layer (Cascaded Async Data Guard):&lt;/strong&gt; To protect the primary from shipping redo directly across three WAN targets, Data Guard &lt;code&gt;RedoRoutes&lt;/code&gt; offloads continental shipping to the metro standby. The metro standby forwards redo asynchronously (&lt;code&gt;ASYNC&lt;/code&gt;) to a remote-region disaster recovery standby, shielding primary commit latency from cross-country network jitter.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Logical Error Layer (Delayed Apply Standby):&lt;/strong&gt; A second remote standby receives redo asynchronously in real time, so committed transactions are durable off-site, but its Managed Recovery Process enforces &lt;code&gt;DelayMins = 240&lt;/code&gt;. If an engineer drops a table or runs an unguarded &lt;code&gt;UPDATE&lt;/code&gt;, operations halts MRP before the bad SCN is applied, flashes back or queries the delayed standby prior to the error, and extracts the clean rows.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Global Write Layer (Region-Sharded GoldenGate):&lt;/strong&gt; When users operate across North America and Europe, applications partition write ownership by region: North American accounts write only to the US primary, and European accounts write only to the EU primary. GoldenGate replicates read-only reference tables and cross-region read replicas unidirectionally with transaction tagging, avoiding active-active write collisions.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;production-configuration-snippets&quot;&gt;Production Configuration Snippets&lt;/h3&gt;
&lt;p&gt;On the primary RAC cluster, create the application service with TAC enabled. Note that TAC (&lt;code&gt;-failovertype AUTO&lt;/code&gt;) uses &lt;code&gt;-failover_restore AUTO&lt;/code&gt; for automatic session state tracking, alongside &lt;code&gt;-commit_outcome TRUE&lt;/code&gt; to record Transaction Guard outcomes:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; service&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -db&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODDB&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -service&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; srv_payments_oltp&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -preferred&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODDB1,PRODDB2&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failovertype&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; AUTO&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -failover_restore&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; AUTO&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -commit_outcome&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; TRUE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -replay_init_time&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 600&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -retention&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 86400&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -notification&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; TRUE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -drain_timeout&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 300&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -clbgoal&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; SHORT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -rlbgoal&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; THROUGHPUT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; start&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; service&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -db&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PRODDB&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -service&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; srv_payments_oltp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In Data Guard Broker (&lt;code&gt;dgmgrl&lt;/code&gt;), bind the metro synchronous target, cascade the remote and delayed standbys through &lt;code&gt;RedoRoutes&lt;/code&gt;, and cap synchronous stall exposure with &lt;code&gt;NetTimeout&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CONNECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sys&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&amp;#x3C;broker_credential&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;@PROD_PRIMARY;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ADD&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; prod_metro &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; CONNECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; IDENTIFIER &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROD_METRO;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EDIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; prod_metro &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROPERTY LogXptMode &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;SYNC&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EDIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; prod_metro &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROPERTY NetTimeout &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EDIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; prod_metro &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROPERTY RedoRoutes &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;(LOCAL : (prod_remote ASYNC, prod_delayed ASYNC))&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ADD&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; prod_remote &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; CONNECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; IDENTIFIER &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROD_REMOTE;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EDIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; prod_remote &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROPERTY LogXptMode &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;ASYNC&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ADD&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; prod_delayed &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; CONNECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; IDENTIFIER &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROD_DELAYED;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EDIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; prod_delayed &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROPERTY LogXptMode &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;ASYNC&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EDIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; prod_delayed &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROPERTY DelayMins &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 240&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EDIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CONFIGURATION&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROTECTION MODE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MaxAvailability;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ENABLE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; CONFIGURATION&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CONFIGURATION&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For cross-region GoldenGate replication, tag Replicat writes and configure Extract to exclude that tag so replicated rows never bounce back in an echo loop:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- GoldenGate Microservices Extract: EX_US_TXNS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXTRACT ex_us_txns&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;USERIDALIAS ogg_admin DOMAIN OGG_SECURITY&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXTTRAIL &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;u02&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;goldengate&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;dirdat&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;et, MEGABYTES &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;500&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TRANLOGOPTIONS EXCLUDETAG &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;01&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TABLE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; payments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;accounts&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (region_code &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;NA&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TABLE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; payments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;transactions&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (origin_region &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;NA&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- GoldenGate Replicat on EU Cluster: REP_EU_SYNC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;REPLICAT rep_eu_sync&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;USERIDALIAS ogg_admin DOMAIN OGG_SECURITY&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DBOPTIONS SUPPRESSTRIGGERS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DBOPTIONS SETTAG &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;01&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;MAP &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;payments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;accounts&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TARGET&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; payments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;accounts_us_replica&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;MAP &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;payments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;transactions&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TARGET&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; payments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;transactions_us_replica&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Deploying all four layers at once creates tangled failure diagnostics. Roll out each blast-radius tier behind an explicit validation gate and rollback boundary before admitting production writes.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-11-05-rac-vs-data-guard-vs-goldengate-decision-5-flow.svg&quot; alt=&quot;Four-phase rollout timeline with validation gates for TAC, FSFO, delayed flashback, and GoldenGate loop checks&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — RAC Cluster and TAC Coverage Gate:&lt;/strong&gt; Provision the 2-node primary RAC cluster and create the TAC database service. Run &lt;code&gt;orachk -acchk&lt;/code&gt; (part of Oracle Autonomous Health Framework) and the database’s built-in Application Continuity Protection Check (available in Oracle 19.10+) to flag non-replayable JDBC concrete types or mid-transaction DDL. &lt;strong&gt;Validation gate:&lt;/strong&gt; Kill instance 1 under synthetic load and confirm zero application errors. &lt;strong&gt;Rollback:&lt;/strong&gt; Revert connection strings to a standard non-TAC service while fixing unsupported client calls.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Metro Synchronous ADG and FSFO Gate:&lt;/strong&gt; Provision the AZ2 standby on independent ASM storage, enable &lt;code&gt;MaxAvailability&lt;/code&gt;, and start the FSFO observer on a third fault domain. &lt;strong&gt;Validation gate:&lt;/strong&gt; Measure &lt;code&gt;log file sync&lt;/code&gt; p99 latency under peak write load, then execute a planned &lt;code&gt;SWITCHOVER TO prod_metro&lt;/code&gt; and an unplanned primary kill drill to verify RPO = 0. &lt;strong&gt;Rollback:&lt;/strong&gt; Drop protection mode to &lt;code&gt;MaxPerformance&lt;/code&gt; (&lt;code&gt;ASYNC&lt;/code&gt;) if metro RTT exceeds the commit latency budget.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — Cascaded Remote and Delayed Standbys:&lt;/strong&gt; Attach &lt;code&gt;prod_remote&lt;/code&gt; and &lt;code&gt;prod_delayed&lt;/code&gt; (&lt;code&gt;DelayMins = 240&lt;/code&gt;) via &lt;code&gt;RedoRoutes&lt;/code&gt; from &lt;code&gt;prod_metro&lt;/code&gt;, sizing the delayed standby’s Fast Recovery Area (FRA) to hold at least 48 hours of peak redo. &lt;strong&gt;Validation gate:&lt;/strong&gt; Simulate a &lt;code&gt;DROP TABLE&lt;/code&gt; on a staging clone and verify row extraction from the delayed standby via Flashback. &lt;strong&gt;Rollback:&lt;/strong&gt; Remove the delayed standby from &lt;code&gt;RedoRoutes&lt;/code&gt; without impacting primary or metro availability.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — Partitioned GoldenGate CDC:&lt;/strong&gt; Enable Extract and Replicat with &lt;code&gt;DBOPTIONS SETTAG 01&lt;/code&gt; and &lt;code&gt;TRANLOGOPTIONS EXCLUDETAG 01&lt;/code&gt; after verifying region-sharded write routing in the application tier. &lt;strong&gt;Validation gate:&lt;/strong&gt; Insert test rows in NA and verify zero echo captures in EU Extract trail statistics. &lt;strong&gt;Rollback:&lt;/strong&gt; Stop Extract/Replicat; regional primaries continue operating independently while read replicas freeze.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;This layered design maps directly to &lt;strong&gt;Oracle Maximum Availability Architecture (MAA) Platinum&lt;/strong&gt; reference behavior, where RAC provides local instance resilience, Active Data Guard provides zero-data-loss physical standby failover, and GoldenGate or sharding decouples multi-region active writes.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Deterministic Conflict Handling vs. Write Partitioning:&lt;/strong&gt; In &lt;strong&gt;Oracle GoldenGate 23ai&lt;/strong&gt;, multi-master conflict handling relies on deterministic Automatic Conflict Detection and Resolution (ACDR) using hidden timestamp columns or explicit &lt;code&gt;COMPARECOLS&lt;/code&gt; and &lt;code&gt;RESOLVECONFLICT&lt;/code&gt; rules—not autonomous guesswork. While GoldenGate 23ai adds vector replication for AI retrieval pipelines and CDC streaming into Oracle Machine Learning (OML) and ONNX scoring workflows, active-active SQL conflicts still require strict relational rules or application write partitioning to avoid silent ledger overwrites.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Native Raft Consensus in Oracle 23ai:&lt;/strong&gt; For greenfield globally distributed schemas that can adopt horizontal sharding, &lt;strong&gt;Oracle Globally Distributed Database (GDD) in 23ai&lt;/strong&gt; introduces native Raft consensus replication across Replication Units (groups of table partition chunks, typically replicated with a factor of 3). Raft automates leader election and majority-quorum commits inside the database engine, reducing external Data Guard and GoldenGate orchestration overhead for sharded workloads—though legacy monolithic schemas still require the composite RAC + ADG + GoldenGate topology.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Every added layer introduces its own operational failure mode. Monitor transport lag, FRA headroom, and replay eligibility continuously so a dormant safeguard does not fail silently during an incident.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-11-05-rac-vs-data-guard-vs-goldengate-decision-6-failures.svg&quot; alt=&quot;Failure-path overlay showing sync latency latching, FRA exhaustion, GoldenGate echo loops, and TAC replay boundaries&quot;&gt;&lt;/p&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;How to Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Metro WAN RTT spike&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;SYNC AFFIRM&lt;/code&gt; blocks primary &lt;code&gt;log file sync&lt;/code&gt; commits across all RAC nodes until timeout.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Monitor &lt;code&gt;log file sync&lt;/code&gt; p99 wait time and Data Guard transport lag.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set &lt;code&gt;NetTimeout = 10&lt;/code&gt; to &lt;code&gt;15&lt;/code&gt; seconds so &lt;code&gt;LGWR&lt;/code&gt; decouples a stalled standby instead of hanging writes.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Delayed standby FRA exhaustion&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Heavy batch loads or purges fill the FRA before the 240-minute apply window clears; MRP halts.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Alert on &lt;code&gt;V$RECOVERY_FILE_DEST&lt;/code&gt; space used &gt; 75% and &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; (&lt;code&gt;NAME = &apos;apply lag&apos;&lt;/code&gt;).&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Size delayed standby FRA for 48+ hours of peak redo generation; enforce retention policies.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Missing GoldenGate tag exclusion&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Replicat DML is recaptured by local Extract, creating an infinite bidirectional echo storm.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sudden spike in Extract trail bytes and duplicate key errors in Replicat discard files.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Mandate &lt;code&gt;DBOPTIONS SET TAG&lt;/code&gt; and &lt;code&gt;TRANLOGOPTIONS EXCLUDETAG&lt;/code&gt; in CI/CD parameter linters.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;RAC interconnect packet loss&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;CSS heartbeat misses trigger node eviction and brief cluster reconfiguration stalls.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Alert on CSS misscount warnings and &lt;code&gt;gc cr block lost&lt;/code&gt; interconnect counters.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use redundant bonded NICs on isolated, non-routed private interconnect switches.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Standby archive gap or MRP crash&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Standby falls behind primary; FSFO observer blocks automatic failover to prevent data loss.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Query &lt;code&gt;V$DATAGUARD_STATS WHERE name = &apos;apply lag&apos;&lt;/code&gt; and &lt;code&gt;SHOW CONFIGURATION&lt;/code&gt; in &lt;code&gt;dgmgrl&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Alert when apply lag exceeds 60 seconds; automate missing archive fetch diagnostics.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Replication schema drift&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;ALTER TABLE&lt;/code&gt; executed on primary without updating GoldenGate definitions aborts Replicat (&lt;code&gt;OGG-00551&lt;/code&gt;).&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;GoldenGate Microservices health endpoint reports &lt;code&gt;ABENDED&lt;/code&gt; Replicat status.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Couple schema migrations with GoldenGate DDL replication or coordinated CI/CD rollout steps.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Non-replayable TAC session state&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Mid-transaction DDL, autonomous transactions, or deprecated JDBC concrete classes disable replay.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run &lt;code&gt;orachk -acchk&lt;/code&gt; or query database AC Protection Check views.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Audit application SQL and JDBC drivers in staging before enabling TAC in production.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Match the primitive to the failure domain:&lt;/strong&gt; RAC protects against compute host faults, synchronous Active Data Guard protects against storage and AZ loss, a delayed standby protects against human SQL errors, and partitioned GoldenGate protects against cross-continent write latency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never stretch Cache Fusion across WAN links:&lt;/strong&gt; Milliseconds of inter-site fiber latency multiply across every global cache wait and cannot fix shared ASM storage vulnerability.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bound synchronous stall risk with &lt;code&gt;NetTimeout&lt;/code&gt;:&lt;/strong&gt; &lt;code&gt;MaxAvailability&lt;/code&gt; protects RPO = 0 under normal metro RTT, but without a tight &lt;code&gt;NetTimeout&lt;/code&gt;, network jitter turns a standby problem into a primary write outage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Offload WAN redo shipping via &lt;code&gt;RedoRoutes&lt;/code&gt;:&lt;/strong&gt; Let the metro standby forward asynchronous redo to remote and delayed standbys so the primary &lt;code&gt;LGWR&lt;/code&gt; never manages multiple cross-country TCP streams.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Partition writes before enabling GoldenGate:&lt;/strong&gt; Tag-based loop exclusion (&lt;code&gt;SET TAG&lt;/code&gt; / &lt;code&gt;EXCLUDETAG&lt;/code&gt;) prevents echo storms, and region-sharded write ownership eliminates active-active conflict queues.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Relying on a single HA technology leaves critical databases exposed to out-of-scope failures—such as shared-storage corruption in RAC or multi-hour restore times after a rogue &lt;code&gt;DROP TABLE&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Implement a tiered MAA topology: local RAC with TAC for node faults, metro synchronous Active Data Guard for zero-RPO AZ failover, cascaded async plus 240-minute delayed standbys for regional and logical recovery, and region-sharded GoldenGate (or 23ai GDD Raft for sharded schemas) for global writes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Validate each tier independently—confirm zero client errors during a staging RAC node kill with &lt;code&gt;orachk -acchk&lt;/code&gt; clean, verify RPO = 0 in a metro &lt;code&gt;dgmgrl&lt;/code&gt; switchover drill, and prove point-in-time table recovery from the delayed standby.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your primary production databases this week: check whether your disaster recovery standby shares storage with the primary, verify &lt;code&gt;NetTimeout&lt;/code&gt; and FRA headroom on every Data Guard target, and confirm whether a delayed-apply standby exists to absorb destructive DDL.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>The Sidecar Database: Reimagining Connection Pooling with Envoy and eBPF</title><link>https://rajivonai.com/blog/2023-09-05-envoy-ebpf-database-sidecar-pooling/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-09-05-envoy-ebpf-database-sidecar-pooling/</guid><description>Why application-level connection pools and centralized proxies collapse under Kubernetes autoscaling, and how local Envoy sidecars accelerated by eBPF transform database reliability.</description><pubDate>Tue, 19 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Relying on static in-process connection pools across autoscaling Kubernetes pods guarantees self-inflicted database connection storms, while centralized proxy fleets introduce shared single points of failure and extra cross-AZ hops.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Kubernetes HPA scaling multiplies per-pod HikariCP or &lt;code&gt;pgxpool&lt;/code&gt; pools into thousands of PostgreSQL backend processes, while centralized PgBouncer tiers create shared blast radiuses and DNS failover lag.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Combine a pod-local transaction pooler, an Envoy sidecar fed by an xDS control plane for instant primary/replica routing, and an eBPF &lt;code&gt;sockmap&lt;/code&gt; program to bypass the loopback TCP/IP stack.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Envoy’s &lt;code&gt;postgres_proxy&lt;/code&gt; + &lt;code&gt;tcp_proxy&lt;/code&gt; maintains a 1:1 connection mapping (it does not multiplex like PgBouncer), per-pod sidecars amplify cluster RAM at thousands of pods, and transaction pooling breaks session-level &lt;code&gt;PREPARE&lt;/code&gt; and &lt;code&gt;pg_advisory_lock&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2023-09-05-envoy-ebpf-database-sidecar-pooling.svg&quot; alt=&quot;Four-stage sidecar database connectivity from local app pool and eBPF sockmap to Envoy xDS and PostgreSQL&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Kubernetes replaced static virtual machine fleets with elastic microservice pods written in Java, Go, Python, and Node.js. Meanwhile, the underlying data plane remains anchored in stateful relational databases such as Patroni-managed PostgreSQL clusters, Amazon RDS/Aurora, and MySQL topologies.&lt;/p&gt;
&lt;p&gt;Historically, teams managed database connections using two patterns:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Application-Level Connection Pooling:&lt;/strong&gt; Libraries embedded inside each service process (HikariCP in Java, &lt;code&gt;database/sql&lt;/code&gt; or &lt;code&gt;pgxpool&lt;/code&gt; in Go, SQLAlchemy in Python).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Centralized Proxy Fleets:&lt;/strong&gt; Shared intermediate tiers (PgBouncer, Odyssey, ProxySQL, or AWS RDS Proxy) placed in front of database primaries to multiplex client connections over fewer backend processes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In static deployments, both patterns hold. Under aggressive Horizontal Pod Autoscaling (HPA), however, their assumptions break down simultaneously.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The Autonomous Pod Connection Storm:&lt;/strong&gt; In-process pools have zero visibility into global cluster scale. If each pod configures HikariCP with &lt;code&gt;minimumIdle = 10&lt;/code&gt; and &lt;code&gt;maximumPoolSize = 20&lt;/code&gt;, scaling from 200 pods to 2,000 pods during a traffic spike triggers 18,000 to 36,000 concurrent TCP and TLS handshakes (and up to 40,000 open connections at max pool size). Because PostgreSQL forks a dedicated OS backend process per connection—typically consuming roughly 5–10 MB of private memory plus shared-memory &lt;code&gt;PGPROC&lt;/code&gt; and lock-array slots—40,000 simultaneous backends would demand 200–400 GB of RAM for connection state alone, exhausting &lt;code&gt;max_connections&lt;/code&gt;, triggering kernel OOM kills, and stalling CPU scheduling on context switches before query load even arrives.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Centralized Proxy Blast Radius &amp;#x26; Latency:&lt;/strong&gt; Shared PgBouncer or ProxySQL clusters prevent backend exhaustion, yet centralizing the proxy creates a shared failure domain. A bad config reload, single-threaded epoll saturation (PgBouncer is single-threaded per process), or AZ network blip drops every dependent microservice at once while adding an extra network hop on every round-trip.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Prepared Statement Collisions in Transaction Pooling:&lt;/strong&gt; To achieve high N:1 multiplexing, proxies run in transaction pooling mode, returning the backend socket after each &lt;code&gt;COMMIT&lt;/code&gt;. Drivers that issue named server-side &lt;code&gt;PREPARE&lt;/code&gt; statements on connection A fail with &lt;code&gt;ERROR: prepared statement &quot;S_1&quot; does not exist&lt;/code&gt; when &lt;code&gt;EXECUTE&lt;/code&gt; lands on backend connection B unless unnamed statements or pooler-level prepared statement tracking are enabled.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stale Client DNS Caching During Failover:&lt;/strong&gt; When Patroni or RDS Multi-AZ promotes a standby, DNS updates propagate asynchronously. Runtimes that cache DNS records aggressively (such as legacy JVM &lt;code&gt;networkaddress.cache.ttl&lt;/code&gt; configurations) keep routing writes to the demoted read-only primary until pods restart.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2023-09-05-envoy-ebpf-database-sidecar-pooling-2-constraints.svg&quot; alt=&quot;Four pooling constraints: HPA connection storms, central proxy SPOF, prepared statement collisions, and stale JVM DNS caching&quot;&gt;&lt;/p&gt;
&lt;p&gt;The core engineering question is: &lt;strong&gt;How can platform teams give autoscaling Kubernetes pods multiplexed connection pooling, sub-second failover routing, and low loopback overhead without creating a centralized proxy single point of failure?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Approach&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Multiplexing &amp;#x26; Routing&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;App-Only Pools (HikariCP / &lt;code&gt;pgxpool&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Direct to DB; zero proxy hop&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;O(N pods) connection storm on HPA; slow DNS failover&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt; — crashes primaries under HPA&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Centralized Proxy Tier (PgBouncer / RDS Proxy)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Strong N:1 transaction pooling&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Shared failure domain across services; extra cross-AZ hop&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Partial&lt;/strong&gt; — simpler for small/medium fleets&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Envoy Sidecar Alone (&lt;code&gt;postgres_proxy&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;xDS dynamic endpoints + per-port primary/replica listeners + TLS&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;tcp_proxy&lt;/code&gt; is 1:1 per stream; does not perform PgBouncer N:1 multiplexing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected alone&lt;/strong&gt; — still passes pod pool fan-out&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Local Pooler + Envoy xDS + eBPF &lt;code&gt;sockmap&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pod-local N:1 pooling, xDS failover, loopback bypass&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Higher per-pod RAM than node proxies; requires transaction-pool hygiene&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen&lt;/strong&gt; — isolated, elastic, xDS-driven&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2023-09-05-envoy-ebpf-database-sidecar-pooling-3-options.svg&quot; alt=&quot;Comparison of app-only pools, central proxies, and sidecar pooler plus Envoy xDS and eBPF sockmap&quot;&gt;&lt;/p&gt;
&lt;p&gt;Many engineers assume Envoy’s &lt;code&gt;postgres_proxy&lt;/code&gt; filter replaces PgBouncer. In practice, Envoy’s &lt;code&gt;postgres_proxy&lt;/code&gt; + &lt;code&gt;tcp_proxy&lt;/code&gt; chain maintains a &lt;strong&gt;1:1 downstream-to-upstream TCP connection mapping&lt;/strong&gt; (the filter only parses traffic for statistics; it does not split reads and writes, so primary and replica traffic use separate listeners or ports, each mapped to its own cluster). Pairing a lightweight local transaction pooler (or bounding the pod pool to 1–2 connections) with Envoy xDS gives you both connection clamping and instant endpoint discovery, while eBPF &lt;code&gt;sockmap&lt;/code&gt; removes the loopback TCP stack tax.&lt;/p&gt;
&lt;h2 id=&quot;the-envoy-sidecar-architecture-edge-routing-with-local-pooling-and-ebpf&quot;&gt;The Envoy Sidecar Architecture: Edge Routing with Local Pooling and eBPF&lt;/h2&gt;
&lt;p&gt;In this architecture, the application container connects to &lt;code&gt;127.0.0.1:5432&lt;/code&gt; with a tiny local pool (2–4 connections). Inside the same pod network namespace, a local transaction pooler multiplexes concurrent app threads onto 1–2 persistent connections to Envoy, or Envoy enforces strict upstream circuit breakers (&lt;code&gt;max_connections&lt;/code&gt;) fed by an xDS control plane. An eBPF &lt;code&gt;sockmap&lt;/code&gt; program attached at the host kernel bypasses the loopback TCP/IP stack for pod-local sockets.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-09-05-envoy-ebpf-database-sidecar-pooling-4-architecture.svg&quot; alt=&quot;Pod network namespace with local pooler, eBPF sockmap, Envoy xDS control plane, and PostgreSQL primary/replica cluster&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[App Container — localhost:5432] --&gt; B[eBPF sockmap — SK_MSG Bypass]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[Local Pooler + Envoy Sidecar]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[xDS Control Plane — Patroni/RDS Watch] --&gt; C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; E[PostgreSQL Primary — Bounded Pool]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; F[PostgreSQL Read Replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-wire-protocol-inspection-and-readwrite-splitting-in-envoy&quot;&gt;1. Wire Protocol Inspection and Read/Write Splitting in Envoy&lt;/h3&gt;
&lt;p&gt;Envoy’s &lt;code&gt;envoy.filters.network.postgres_proxy&lt;/code&gt; filter decodes PostgreSQL v3.0 wire frames (&lt;code&gt;Query&lt;/code&gt;, &lt;code&gt;Parse&lt;/code&gt;, &lt;code&gt;Bind&lt;/code&gt;, &lt;code&gt;Execute&lt;/code&gt;, &lt;code&gt;ReadyForQuery&lt;/code&gt;) for observability metrics only. It is not a router or pooler, so primary/replica routing is done with separate listeners (for example port 5432 to the primary cluster and 5433 to the replica cluster) while xDS updates each cluster’s endpoints on failover:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;filter_chains&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;filters&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;envoy.filters.network.postgres_proxy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        typed_config&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          &quot;@type&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;type.googleapis.com/envoy.extensions.filters.network.postgres_proxy.v3alpha.PostgresProxy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          stat_prefix&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;postgres&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          enable_sql_parsing&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;envoy.filters.network.tcp_proxy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        typed_config&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          &quot;@type&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          stat_prefix&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;tcp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;          # One listener per role: this listener (port 5432) maps to the primary cluster;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;          # a second listener (port 5433) with the same chain maps to &quot;pg_cluster_read&quot;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          cluster&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pg_cluster_write&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Because &lt;code&gt;postgres_proxy&lt;/code&gt; cannot decode messages after an SSL handshake takes place inside the stream, TLS should be terminated at the Envoy listener or offloaded to Envoy’s upstream mTLS transport socket so Envoy can inspect unencrypted loopback traffic and wrap it in mTLS over the cluster network. When &lt;code&gt;split_rw&lt;/code&gt; is enabled, &lt;code&gt;postgres_proxy&lt;/code&gt; also tracks PostgreSQL’s &lt;code&gt;ReadyForQuery&lt;/code&gt; transaction-status byte (&lt;code&gt;&apos;I&apos;&lt;/code&gt; for idle, &lt;code&gt;&apos;T&apos;&lt;/code&gt; for inside a transaction block, &lt;code&gt;&apos;E&apos;&lt;/code&gt; for failed transaction): once a client sends &lt;code&gt;BEGIN&lt;/code&gt;, subsequent &lt;code&gt;SELECT&lt;/code&gt; statements inside that transaction stay pinned to the primary (&lt;code&gt;write_cluster_name&lt;/code&gt;) until &lt;code&gt;COMMIT&lt;/code&gt; or &lt;code&gt;ROLLBACK&lt;/code&gt; returns the session to idle, preventing mid-transaction read-your-writes violations.&lt;/p&gt;
&lt;h3 id=&quot;2-ebpf-sockmap-eliminating-the-sidecar-loopback-tax&quot;&gt;2. eBPF &lt;code&gt;sockmap&lt;/code&gt;: Eliminating the Sidecar Loopback Tax&lt;/h3&gt;
&lt;p&gt;Running a sidecar normally forces every packet through the Linux loopback TCP/IP stack twice (socket buffer -&gt; IP routing -&gt; netfilter &lt;code&gt;iptables&lt;/code&gt; -&gt; loopback -&gt; receiving socket). An eBPF &lt;code&gt;BPF_PROG_TYPE_SK_MSG&lt;/code&gt; program attached to a &lt;code&gt;sockmap&lt;/code&gt; / &lt;code&gt;sockhash&lt;/code&gt; intercepts &lt;code&gt;send()&lt;/code&gt; calls between co-located sockets on &lt;code&gt;127.0.0.1&lt;/code&gt; and redirects payloads directly to the peer socket’s ingress queue:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;SEC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sk_msg&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; bpf_tcp_msg_redirect&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sk_msg_md &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;msg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (msg-&gt;remote_ip4 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;7f000001&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; msg-&gt;local_ip4 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;7f000001&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (msg-&gt;remote_port &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; bpf_htons&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5432&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; bpf_msg_redirect_hash&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(msg, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sock_map, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;msg-&gt;remote_ip4, BPF_F_INGRESS);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SK_PASS;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By skipping TCP segmentation, checksums, and netfilter traversal on loopback, &lt;code&gt;sockmap&lt;/code&gt; reduces sidecar CPU overhead and tail latency without requiring any changes to the application binary or database driver. Under the hood, two eBPF hooks cooperate: a &lt;code&gt;BPF_PROG_TYPE_SOCK_OPS&lt;/code&gt; program attached to the cgroup intercepts &lt;code&gt;BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB&lt;/code&gt; and &lt;code&gt;BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB&lt;/code&gt; events when the app and sidecar complete their &lt;code&gt;127.0.0.1&lt;/code&gt; three-way handshake, inserting both socket descriptors into the shared &lt;code&gt;sock_map&lt;/code&gt; hash table keyed by their 4-tuple. On every subsequent &lt;code&gt;send()&lt;/code&gt;, the &lt;code&gt;BPF_PROG_TYPE_SK_MSG&lt;/code&gt; program looks up the peer socket in &lt;code&gt;sock_map&lt;/code&gt; and copies the buffer directly to the peer’s receive queue. Teams whose drivers natively support Unix domain sockets (such as Go &lt;code&gt;pgx&lt;/code&gt; or Python &lt;code&gt;psycopg&lt;/code&gt;) can alternatively mount a pod-local &lt;code&gt;emptyDir&lt;/code&gt; socket path to avoid TCP &lt;code&gt;TIME_WAIT&lt;/code&gt; altogether, whereas Java JDBC services—which lack built-in Unix socket transport without JNI—rely on &lt;code&gt;127.0.0.1&lt;/code&gt; + &lt;code&gt;sockmap&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;3-xds-control-plane-dynamic-quotas-and-failover&quot;&gt;3. xDS Control Plane: Dynamic Quotas and Failover&lt;/h3&gt;
&lt;p&gt;An xDS management server (using &lt;code&gt;go-control-plane&lt;/code&gt; or Istio) watches two signals: active pod count ($P_{\text{active}}$) from Kubernetes and primary/replica health from Patroni or the cloud RDS API. Given a safe database backend budget ($C_{\text{max}}$), the control plane computes the per-pod upstream connection quota:&lt;/p&gt;
&lt;p&gt;$$\text{Per-Pod Max Connections} = \max\left(1,; \left\lfloor \frac{C_{\text{max}}}{P_{\text{active}}} \right\rfloor\right)$$&lt;/p&gt;
&lt;p&gt;When HPA scales pods from 200 to 1,000 against a 2,000-connection budget, xDS pushes updated Envoy Cluster Discovery Service (CDS) circuit-breaker limits (&lt;code&gt;max_connections: 2&lt;/code&gt;) and Endpoint Discovery Service (EDS) primary IPs over gRPC streaming. During a Patroni failover, Envoy drains connections to the demoted primary and opens connections to the newly promoted primary without waiting for JVM DNS caches to expire.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Rolling out sidecar database proxies requires cleaning up session-scoped SQL anti-patterns before injecting proxies into production pods:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-09-05-envoy-ebpf-database-sidecar-pooling-5-flow.svg&quot; alt=&quot;Four-phase rollout: session hygiene, staging sidecar canary, eBPF sockmap DaemonSet, and xDS quota clamping&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 1 — Session Hygiene Audit:&lt;/strong&gt; Audit application code for session-scoped state that breaks transaction pooling: named &lt;code&gt;PREPARE&lt;/code&gt; statements, &lt;code&gt;SET search_path&lt;/code&gt; / &lt;code&gt;SET timezone&lt;/code&gt;, temporary tables, and session-level &lt;code&gt;pg_advisory_lock&lt;/code&gt;. Migrate to unnamed statements, &lt;code&gt;SET LOCAL&lt;/code&gt;, and &lt;code&gt;pg_advisory_xact_lock&lt;/code&gt;. &lt;em&gt;Gate:&lt;/em&gt; zero session-state errors in staging transaction-pool tests.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 2 — Sidecar Canary Without eBPF:&lt;/strong&gt; Inject the local pooler + Envoy sidecar into a single non-critical service in staging, pointing the app at &lt;code&gt;127.0.0.1:5432&lt;/code&gt; and keeping HikariCP/&lt;code&gt;pgxpool&lt;/code&gt; capped at 2–4 connections. &lt;em&gt;Gate:&lt;/em&gt; p99 query latency and sidecar memory stay within budget.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 3 — eBPF &lt;code&gt;sockmap&lt;/code&gt; DaemonSet:&lt;/strong&gt; Deploy the &lt;code&gt;BPF_PROG_TYPE_SK_MSG&lt;/code&gt; DaemonSet on Linux 5.4+ (6.1 LTS recommended) worker nodes and verify attachment with &lt;code&gt;bpftool prog list&lt;/code&gt;. &lt;em&gt;Gate:&lt;/em&gt; loopback CPU usage drops under load without socket stalls.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 4 — xDS Quota Clamping &amp;#x26; Failover Drill:&lt;/strong&gt; Connect the xDS control plane to Patroni/RDS health checks, run a 5x HPA scale-out test, and trigger a controlled primary failover. &lt;em&gt;Rollback:&lt;/em&gt; flip the application &lt;code&gt;DATABASE_URL&lt;/code&gt; secret back to the direct database or central proxy endpoint and remove the sidecar injection annotation.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;I have not operated a 3,000-pod Envoy + &lt;code&gt;sockmap&lt;/code&gt; PostgreSQL mesh personally; the design constraints above derive from PostgreSQL’s documented process model, Envoy’s &lt;code&gt;postgres_proxy&lt;/code&gt; specification, and Cilium’s eBPF &lt;code&gt;sockmap&lt;/code&gt; architecture.&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;PostgreSQL Process-Per-Connection Overhead:&lt;/strong&gt; PostgreSQL spawns a dedicated OS backend process for every client connection bounded by &lt;code&gt;max_connections&lt;/code&gt; (&lt;code&gt;100&lt;/code&gt; by default), consuming roughly 5–10 MB of private memory plus shared-memory tracking per backend—which is why unbounded client pools exhaust memory during connection bursts (&lt;a href=&quot;https://www.postgresql.org/docs/current/runtime-config-connection.html&quot;&gt;PostgreSQL Connection Settings&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Envoy &lt;code&gt;postgres_proxy&lt;/code&gt; Behavior:&lt;/strong&gt; Envoy’s PostgreSQL network filter decodes v3.0 wire protocol frames for stats only (no read/write splitting or pooling), while &lt;code&gt;tcp_proxy&lt;/code&gt; maintains a 1:1 connection mapping and ignores encrypted traffic once an SSL handshake starts (&lt;a href=&quot;https://www.envoyproxy.io/docs/envoy/latest/configuration/listeners/network_filters/postgres_proxy_filter&quot;&gt;Envoy Postgres Proxy Docs&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cilium eBPF &lt;code&gt;sockmap&lt;/code&gt; Loopback Acceleration:&lt;/strong&gt; Cilium uses &lt;code&gt;BPF_PROG_TYPE_SOCK_OPS&lt;/code&gt; and &lt;code&gt;BPF_PROG_TYPE_SK_MSG&lt;/code&gt; (&lt;code&gt;bpf_msg_redirect_hash&lt;/code&gt;) to bypass the TCP/IP and netfilter stack for co-located pod-to-sidecar sockets on the same host (&lt;a href=&quot;https://docs.cilium.io/en/stable/network/ebpf/&quot;&gt;Cilium eBPF Datapath&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Kubernetes Native Sidecar Ordering:&lt;/strong&gt; Standard pod &lt;code&gt;containers&lt;/code&gt; start and terminate in parallel with the application, which can drop in-flight database transactions if Envoy exits before the app drains. Defining the local pooler and Envoy as Kubernetes native sidecar &lt;code&gt;initContainers&lt;/code&gt; with &lt;code&gt;restartPolicy: Always&lt;/code&gt; (introduced in Kubernetes 1.28 and beta by default in 1.29) guarantees the proxy is listening before the app container starts and remains alive until the app finishes its &lt;code&gt;preStop&lt;/code&gt; and &lt;code&gt;SIGTERM&lt;/code&gt; drain (&lt;a href=&quot;https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/&quot;&gt;Kubernetes Sidecar Containers&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Edge Case&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Failure Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Consequence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detection &amp;#x26; Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Sidecar RAM Amplification&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Every pod runs a dedicated Envoy + pooler container.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;At 5,000+ pods, proxy RAM overhead becomes substantial.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Strip unused Envoy HTTP/Lua filters; evaluate node-level proxies (Istio Ambient ztunnel) for huge fleets.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;CPU Throttling on Bulk Reads&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reporting query returns 500,000 rows through &lt;code&gt;postgres_proxy&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;L7 wire parsing hits container CPU limits and throttles I/O.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bypass &lt;code&gt;enable_sql_parsing&lt;/code&gt; for bulk ETL/analytics listeners; enforce query pagination.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Prepared Statement Collisions&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Named &lt;code&gt;PREPARE stmt_1&lt;/code&gt; runs across multiplexed backend sockets.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;ERROR: prepared statement does not exist&lt;/code&gt; on next query.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Configure drivers (&lt;code&gt;pgx&lt;/code&gt;, JDBC) for unnamed/protocol-level statements or pooler statement caching.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Loopback &lt;code&gt;TIME_WAIT&lt;/code&gt; Exhaustion&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;App opens/closes short-lived connections to &lt;code&gt;127.0.0.1:5432&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Ephemeral port exhaustion throws &lt;code&gt;EADDRNOTAVAIL&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep persistent local app pool (2–4 conns); set &lt;code&gt;net.ipv4.tcp_tw_reuse = 1&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Session Variable Leakage&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Client executes &lt;code&gt;SET timezone&lt;/code&gt; or &lt;code&gt;SET search_path&lt;/code&gt; without &lt;code&gt;LOCAL&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Next multiplexed client inherits contaminated session state.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use &lt;code&gt;SET LOCAL&lt;/code&gt; inside transactions or configure &lt;code&gt;DISCARD ALL&lt;/code&gt; on pooler reset.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;xDS Propagation Lag&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary fails over before EDS updates reach all sidecars.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sidecars send writes to demoted primary, hitting read-only errors.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Stream EDS over gRPC; configure active TCP/PG health checks and app retry on &lt;code&gt;25006&lt;/code&gt; (&lt;code&gt;read_only_sql_transaction&lt;/code&gt;).&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Legacy Kernel &lt;code&gt;sockmap&lt;/code&gt; Gaps&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Nodes run pre-5.4 kernels with incomplete &lt;code&gt;SK_MSG&lt;/code&gt; support.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;sockmap&lt;/code&gt; fails to attach; traffic falls back to full TCP loopback.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Standardize worker AMIs on Linux 6.1 LTS+; alert if &lt;code&gt;bpftool prog list&lt;/code&gt; misses &lt;code&gt;sk_msg&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Advisory Lock Deadlocks&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;App acquires session-level &lt;code&gt;pg_advisory_lock&lt;/code&gt; over pooled socket.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lock stays pinned on backend socket after client disconnects.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Ban session advisory locks in pooled services; use &lt;code&gt;pg_advisory_xact_lock&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2023-09-05-envoy-ebpf-database-sidecar-pooling-6-failures.svg&quot; alt=&quot;Four failure modes: sidecar RAM amplification, session lock contamination, loopback TIME_WAIT exhaustion, and xDS failover lag&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;april-2026-update&quot;&gt;April 2026 Update&lt;/h2&gt;
&lt;p&gt;Between 2023 and 2026, two architectural shifts reduced the operational weight of pod-local database proxies:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Node-Level Ambient Proxies (Istio Ambient &amp;#x26; Cilium):&lt;/strong&gt; To eliminate per-pod sidecar memory amplification at thousands of pods, teams increasingly offload mTLS and L4 routing to shared per-node Rust/eBPF proxies (&lt;code&gt;ztunnel&lt;/code&gt; + Cilium &lt;code&gt;sockmap&lt;/code&gt;), reserving L7 database poolers for dedicated pods or per-node pools.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built-In Pooler Prepared Statement Support:&lt;/strong&gt; Modern transaction poolers (including PgBouncer 1.21+ &lt;code&gt;max_prepared_statements&lt;/code&gt;) added protocol-level named prepared statement tracking, removing the historical tradeoff between transaction multiplexing and JDBC/&lt;code&gt;pgx&lt;/code&gt; statement caching.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Never rely on Envoy &lt;code&gt;postgres_proxy&lt;/code&gt; alone for N:1 multiplexing:&lt;/strong&gt; Chained with &lt;code&gt;tcp_proxy&lt;/code&gt;, Envoy is 1:1 per TCP stream; pair it with a transaction pooler (or tight per-pod circuit breakers) to clamp backend connections.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bind HPA scale to dynamic xDS quotas:&lt;/strong&gt; Compute per-pod &lt;code&gt;max_connections&lt;/code&gt; from total database capacity divided by active pods so autoscaling cannot fork-bomb PostgreSQL.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use eBPF &lt;code&gt;sockmap&lt;/code&gt; to erase the loopback tax:&lt;/strong&gt; &lt;code&gt;BPF_PROG_TYPE_SK_MSG&lt;/code&gt; redirects co-located &lt;code&gt;127.0.0.1&lt;/code&gt; socket buffers directly, avoiding double TCP/IP and netfilter traversal.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Terminate TLS at Envoy, not inside the app-to-sidecar hop:&lt;/strong&gt; &lt;code&gt;postgres_proxy&lt;/code&gt; stops parsing frames after an SSL handshake; keep loopback plaintext (or Unix socket) and let Envoy wrap mTLS to the database.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Enforce transaction-pooling hygiene before rollout:&lt;/strong&gt; Migrate from session &lt;code&gt;pg_advisory_lock&lt;/code&gt; and &lt;code&gt;SET&lt;/code&gt; to &lt;code&gt;pg_advisory_xact_lock&lt;/code&gt; and &lt;code&gt;SET LOCAL&lt;/code&gt;, or use PgBouncer 1.21+ prepared statement caching.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Check your Kubernetes services for large static &lt;code&gt;HikariCP&lt;/code&gt; or &lt;code&gt;pgxpool&lt;/code&gt; sizes (&lt;code&gt;&gt; 10&lt;/code&gt; per pod) that multiply beyond PostgreSQL &lt;code&gt;max_connections&lt;/code&gt; during an HPA spike.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Pair a pod-local (or per-node) transaction pooler with Envoy xDS routing and eBPF &lt;code&gt;sockmap&lt;/code&gt; loopback acceleration.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run a 5x HPA scale-out test in staging and verify that PostgreSQL backend count in &lt;code&gt;pg_stat_activity&lt;/code&gt; stays flat while Envoy EDS switches primaries cleanly on a Patroni failover.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit services for session-scoped &lt;code&gt;PREPARE&lt;/code&gt; and &lt;code&gt;pg_advisory_lock&lt;/code&gt; calls this week, then canary the sidecar configuration on a single stateless service.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Envoy Proxy Documentation: &lt;em&gt;PostgreSQL Network Filter (&lt;code&gt;envoy.filters.network.postgres_proxy&lt;/code&gt;)&lt;/em&gt; (&lt;code&gt;envoyproxy.io/docs/envoy/latest/configuration/listeners/network_filters/postgres_proxy_filter&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Cilium eBPF Documentation: &lt;em&gt;Socket-Level Load Balancing and &lt;code&gt;sockmap&lt;/code&gt;&lt;/em&gt; (&lt;code&gt;docs.cilium.io/en/stable/network/ebpf/&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;PostgreSQL Documentation: &lt;em&gt;Connections and Authentication (&lt;code&gt;max_connections&lt;/code&gt;)&lt;/em&gt; (&lt;code&gt;postgresql.org/docs/current/runtime-config-connection.html&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;PgBouncer Documentation: &lt;em&gt;Protocol-Level Prepared Statements (&lt;code&gt;max_prepared_statements&lt;/code&gt;)&lt;/em&gt; (&lt;code&gt;pgbouncer.org/config.html&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Kubernetes Documentation: &lt;em&gt;Sidecar Containers&lt;/em&gt; (&lt;code&gt;kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>Predictive PGA and Temp Tablespace Exhaustion: Real-Time Telemetry and Automated Remediation</title><link>https://rajivonai.com/blog/2023-08-22-oracle-pga-temp-pressure-hash-join-spills/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-08-22-oracle-pga-temp-pressure-hash-join-spills/</guid><description>Why reactive alerting on ORA-04030 and ORA-01652 errors fails in production, and how to build a stream-processing pipeline using high-frequency ASH metrics to neutralize rogue queries before database crashes occur.</description><pubDate>Mon, 18 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Reacting to memory and temporary-tablespace exhaustion alerts fails in consolidated Oracle fleets because storage queues saturate and &lt;code&gt;ORA-04036&lt;/code&gt; or &lt;code&gt;ORA-01652&lt;/code&gt; fires long before five-minute monitoring pollers ever notice the spike.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Problem: Runaway hash joins and Cartesian sorts exhaust &lt;code&gt;TEMP&lt;/code&gt; and PGA between 5-minute &lt;code&gt;DBA_*&lt;/code&gt; polling intervals, stalling OLTP &lt;code&gt;log file sync&lt;/code&gt; I/O and risking &lt;code&gt;ORA-04036&lt;/code&gt; or Linux OOM kills.&lt;/li&gt;
&lt;li&gt;Answer: Sample &lt;code&gt;V$SQL_WORKAREA_ACTIVE&lt;/code&gt;, &lt;code&gt;V$SESSION&lt;/code&gt;, and &lt;code&gt;V$PROCESS&lt;/code&gt; every second from a local sidecar, compute allocation velocity and Time-To-Exhaustion, and neutralize culprits with &lt;code&gt;ALTER SYSTEM CANCEL SQL&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Watch out: &lt;code&gt;PGA_AGGREGATE_LIMIT = 0&lt;/code&gt; disables instance-level protection, &lt;code&gt;DBMS_SQLQ&lt;/code&gt; SQL Quarantine requires Exadata (&lt;code&gt;EE-ES&lt;/code&gt;) licensing, and automated kills require a &lt;code&gt;BATCH_EXEMPT&lt;/code&gt; allowlist plus stale-sample rejection.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2023-08-22-oracle-pga-temp-pressure-hash-join-spills.svg&quot; alt=&quot;Real-time workarea telemetry pipeline from V$ views to surgical SQL cancel&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In multi-tenant Oracle Database 19c environments, Pluggable Databases (PDBs) share host RAM and storage bandwidth across independent application workloads. Oracle manages private session memory through the Program Global Area (PGA) and spills overflowing sort or hash workareas to disk in the Temporary Tablespace (&lt;code&gt;TEMP&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;When the optimizer chooses a hash join, the server process first reads the smaller row source (the build input) to construct an in-memory hash table inside its PGA workarea, then streams the larger row source (the probe input) to match join keys. If stale statistics or join-key skew cause the build input to exceed the workarea budget, the engine partitions the hash table and writes overflowing buckets directly to &lt;code&gt;TEMP&lt;/code&gt; tempfiles via &lt;code&gt;direct path write temp&lt;/code&gt;—bypassing the SGA buffer cache—and reads them back during the probe phase via &lt;code&gt;direct path read temp&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Depending on how much PGA the workarea receives relative to the input size, execution falls into one of three regimes:&lt;/p&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Execution Regime&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Memory Fit&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Temp Disk I/O&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Operational Effect&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Optimal&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Entire hash table or sort fits in PGA workarea.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero &lt;code&gt;TEMP&lt;/code&gt; writes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Executes at memory bandwidth.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;One-Pass&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exceeds workarea; partitions spill once via &lt;code&gt;direct path write temp&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Proportional to spill volume (&lt;code&gt;direct path read temp&lt;/code&gt; on probe).&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Predictable I/O overhead if storage has headroom.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Multi-Pass&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Severe PGA deficit forces repeated spill and re-partition cycles.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Heavy read/write amplification in &lt;code&gt;TEMP&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Saturates shared storage queues and starves OLTP.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;To bound private memory, Oracle provides &lt;code&gt;PGA_AGGREGATE_TARGET&lt;/code&gt; (a soft sizing target that enables &lt;code&gt;WORKAREA_SIZE_POLICY = AUTO&lt;/code&gt;) and &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt; (a hard instance ceiling introduced in Oracle 12c).&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Even with automatic workarea management enabled, runaway queries—such as missing join predicates, skewed parallel hash joins, or unbounded PL/SQL collections—can destabilize a consolidated host along three operational constraints.&lt;/p&gt;
&lt;p&gt;First, &lt;strong&gt;storage queue saturation before &lt;code&gt;ORA-01652&lt;/code&gt;&lt;/strong&gt;. A multi-pass hash join spilling gigabytes through &lt;code&gt;direct path write temp&lt;/code&gt; and &lt;code&gt;direct path read temp&lt;/code&gt; floods shared storage controllers well before &lt;code&gt;TEMP&lt;/code&gt; reaches zero free extents and raises &lt;code&gt;ORA-01652: unable to extend temp segment&lt;/code&gt;. Latency-sensitive OLTP waits (&lt;code&gt;db file sequential read&lt;/code&gt; and &lt;code&gt;log file sync&lt;/code&gt;) degrade across every PDB sharing the array.&lt;/p&gt;
&lt;p&gt;Second, &lt;strong&gt;&lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt; boundaries and OS OOM risk&lt;/strong&gt;. &lt;code&gt;PGA_AGGREGATE_TARGET&lt;/code&gt; is only a soft advisory target: Oracle’s workarea manager can shrink tunable sort and hash buffers, but it cannot shrink &lt;em&gt;untunable&lt;/em&gt; PGA such as PL/SQL associative arrays, open cursor state, or client LOB buffers. When &lt;code&gt;MEMORY_TARGET&lt;/code&gt; is unset, Oracle 19c defaults &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt; to the greatest of &lt;strong&gt;2 GB&lt;/strong&gt;, &lt;strong&gt;200% of &lt;code&gt;PGA_AGGREGATE_TARGET&lt;/code&gt;&lt;/strong&gt;, or &lt;strong&gt;3 MB times &lt;code&gt;PROCESSES&lt;/code&gt;&lt;/strong&gt; (&lt;strong&gt;5 MB times &lt;code&gt;PROCESSES&lt;/code&gt;&lt;/strong&gt; on RAC). Crossing that ceiling triggers &lt;code&gt;ORA-04036: PGA memory used by the instance exceeds PGA_AGGREGATE_LIMIT&lt;/code&gt;, forcing Oracle to abort calls or terminate the highest untunable PGA consumers. Conversely, administrators who set &lt;code&gt;PGA_AGGREGATE_LIMIT = 0&lt;/code&gt; to silence &lt;code&gt;ORA-04036&lt;/code&gt; remove the safety valve entirely: untunable PL/SQL heaps or concurrent workareas can exhaust host RAM (&lt;code&gt;ORA-04030: out of process memory&lt;/code&gt;) and trigger the Linux OOM killer against critical background processes such as &lt;code&gt;PMON&lt;/code&gt; or &lt;code&gt;LGWR&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Third, &lt;strong&gt;velocity mismatch against periodic polling&lt;/strong&gt;. Traditional monitoring agents query dictionary views like &lt;code&gt;DBA_TABLESPACE_USAGE_METRICS&lt;/code&gt; or &lt;code&gt;V$PGASTAT&lt;/code&gt; on 5-minute intervals. A runaway join can consume available &lt;code&gt;TEMP&lt;/code&gt; or trip &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt; between two consecutive polls, leaving operators with post-mortem logs rather than early containment. Even &lt;code&gt;V$TEMPSEG_USAGE&lt;/code&gt; only gives a point-in-time snapshot unless sampled continuously to compute allocation rate.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-08-22-oracle-pga-temp-pressure-hash-join-spills-2-constraints.svg&quot; alt=&quot;Three constraints turn hash-join spills into instance outages&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do platform teams detect explosive PGA and &lt;code&gt;TEMP&lt;/code&gt; allocation trajectories in real time and cancel offending SQL statements before shared storage or instance memory collapses?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams typically evaluate four containment strategies before combining high-frequency telemetry with native Oracle SQL cancellation.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Gets Right&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;5-minute OEM / cron polling&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Low overhead; built into standard monitoring packs.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Blind to sub-minute &lt;code&gt;TEMP&lt;/code&gt; spikes and rapid PGA allocation slopes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Setting &lt;code&gt;PGA_AGGREGATE_LIMIT = 0&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Prevents &lt;code&gt;ORA-04036&lt;/code&gt; on legitimate large analytical jobs.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exposes the host to OS OOM kills of Oracle background processes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Oracle Resource Manager alone&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforces native CPU shares, &lt;code&gt;MAX_EST_EXEC_TIME&lt;/code&gt;, and session temp pool caps.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Static thresholds do not measure cross-session &lt;code&gt;TEMP&lt;/code&gt; growth velocity or Time-To-Exhaustion.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Partial (pair)&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;1s &lt;code&gt;V$&lt;/code&gt; velocity stream + &lt;code&gt;ALTER SYSTEM CANCEL SQL&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Tracks &lt;code&gt;dTemp/dt&lt;/code&gt; and &lt;code&gt;dPGA/dt&lt;/code&gt; per session; cancels only the offending &lt;code&gt;sql_id&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires a local sidecar, allowlisting for batch jobs, and stale-event guards.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Disabling &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt; trades predictable &lt;code&gt;ORA-04036&lt;/code&gt; errors for unpredictable host-level OOM crashes. Oracle Resource Manager (&lt;code&gt;DBMS_RESOURCE_MANAGER&lt;/code&gt;) remains valuable as a defensive baseline—directives like &lt;code&gt;MAX_EST_EXEC_TIME&lt;/code&gt;, &lt;code&gt;SWITCH_TIME&lt;/code&gt;, and &lt;code&gt;SWITCH_FOR_CALL&lt;/code&gt; can demote long-running queries to a lower-priority consumer group, and &lt;code&gt;MAX_UTILIZATION_LIMIT&lt;/code&gt; caps CPU share—but Resource Manager directives do not compute aggregate &lt;code&gt;TEMP&lt;/code&gt; allocation velocity or project Time-To-Exhaustion across concurrent sessions. Pairing explicit &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt; and Resource Manager bounds with a 1-second &lt;code&gt;V$SQL_WORKAREA_ACTIVE&lt;/code&gt; velocity detector gives teams early surgical cancellation before &lt;code&gt;TEMP&lt;/code&gt; or RAM runs dry.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-08-22-oracle-pga-temp-pressure-hash-join-spills-3-options.svg&quot; alt=&quot;Comparison of four containment options showing why 1s velocity telemetry plus native cancel is chosen&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-streaming-workarea-telemetry-architecture&quot;&gt;The Streaming Workarea Telemetry Architecture&lt;/h2&gt;
&lt;p&gt;Instead of running heavy &lt;code&gt;DBA_*&lt;/code&gt; dictionary queries over remote connections, a lightweight local sidecar samples in-memory dynamic performance views (&lt;code&gt;V$SQL_WORKAREA_ACTIVE&lt;/code&gt;, &lt;code&gt;V$SESSION&lt;/code&gt;, and &lt;code&gt;V$PROCESS&lt;/code&gt;) every second, streams deltas to a velocity evaluator, and dispatches surgical cancellations back to a local remediation handler.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-08-22-oracle-pga-temp-pressure-hash-join-spills-4-architecture.svg&quot; alt=&quot;Streaming workarea telemetry architecture from V$ views to surgical cancel&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;pipeline-mechanics&quot;&gt;Pipeline Mechanics&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Non-Blocking In-Memory Scrape:&lt;/strong&gt; The sidecar queries &lt;code&gt;V$SQL_WORKAREA_ACTIVE&lt;/code&gt; joined to &lt;code&gt;V$SESSION&lt;/code&gt; and &lt;code&gt;V$PROCESS&lt;/code&gt;. Because &lt;code&gt;V$SQL_WORKAREA_ACTIVE&lt;/code&gt; only holds rows for currently executing workareas—rows disappear as soon as a sort or hash join finishes—joining &lt;code&gt;V$SESSION&lt;/code&gt; (&lt;code&gt;sql_id&lt;/code&gt;, &lt;code&gt;module&lt;/code&gt;, &lt;code&gt;serial#&lt;/code&gt;) and &lt;code&gt;V$PROCESS&lt;/code&gt; (&lt;code&gt;pga_alloc_mem&lt;/code&gt;) in a single pass captures both tunable workarea spills and total process PGA. Filtering to sessions above a baseline floor (for example, &lt;code&gt;actual_mem_used &gt; 50 MB&lt;/code&gt; or &lt;code&gt;tempseg_size &gt; 100 MB&lt;/code&gt;) keeps result sets small and avoids scanning idle connections. Using adaptive polling—5 seconds when &lt;code&gt;TEMP&lt;/code&gt; usage is calm, dropping to 1 second when &lt;code&gt;TEMP&lt;/code&gt; exceeds 60%—prevents unnecessary library-cache latch pressure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Velocity, Time-To-Exhaustion (TTE), and State Eviction:&lt;/strong&gt; Rather than alerting only on static fill percentages, the stream processor computes the rate of change per &lt;code&gt;(sid, serial#)&lt;/code&gt; and evicts stale entries from &lt;code&gt;fleetRegistry&lt;/code&gt; once a session closes so the sidecar’s own Go heap stays bounded:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;$$\text{Velocity} = \frac{\Delta \text{TempAllocated}}{\Delta t} \quad \left[\frac{\text{MB}}{\text{sec}}\right], \qquad \text{TTE} = \frac{\text{Capacity}&lt;em&gt;{\text{remaining}}}{\sum \text{Velocity}&lt;/em&gt;{\text{active}}}$$&lt;/p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;Surgical Cancellation (&lt;code&gt;ALTER SYSTEM CANCEL SQL&lt;/code&gt;):&lt;/strong&gt; Introduced in Oracle 18c and standard in 19c, &lt;code&gt;ALTER SYSTEM CANCEL SQL&lt;/code&gt; aborts only the offending &lt;code&gt;sql_id&lt;/code&gt; inside a session while keeping the client connection alive (returning &lt;code&gt;ORA-01013: user requested cancel of current operation&lt;/code&gt; to the caller). Before issuing a cancel, the evaluator checks the session’s &lt;code&gt;module&lt;/code&gt; and &lt;code&gt;sql_id&lt;/code&gt; against a &lt;code&gt;BATCH_EXEMPT&lt;/code&gt; allowlist so legitimate nightly ETL or month-end close jobs are never interrupted. Only if a non-exempt session ignores cancellation and continues holding memory past a short grace window does the handler escalate to &lt;code&gt;ALTER SYSTEM DISCONNECT SESSION ... IMMEDIATE&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;1-high-frequency-workarea-collector-query&quot;&gt;1. High-Frequency Workarea Collector Query&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt; /*+ LEADING(wa) USE_HASH(s) */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;sid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;serial&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;#,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;username&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;module&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;sql_id&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    ROUND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;wa&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;actual_mem_used&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; mem_used_mb,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    ROUND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;wa&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;tempseg_size&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; temp_spill_mb,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    wa&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;number_passes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    ROUND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pga_alloc_mem&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pga_alloc_mb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; v$sql_workarea_active wa&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; v$&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;session&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; s &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; wa&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;sid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;sid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; v$process p &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;paddr&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;addr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; wa&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;actual_mem_used&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 52428800&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;   OR&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; wa&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;tempseg_size&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    &gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 104857600&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-stream-processing-velocity-evaluator-go&quot;&gt;2. Stream-Processing Velocity Evaluator (Go)&lt;/h3&gt;
&lt;p&gt;The evaluator tracks the previous sample per &lt;code&gt;sid-serial#&lt;/code&gt;, discards out-of-order or stale ticks, and flags sessions whose &lt;code&gt;TEMP&lt;/code&gt; TTE drops below 30 seconds or whose PGA allocation rate crosses the configured slope ceiling.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; EvaluateTelemetryTick&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;cur&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; SessionTelemetry&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;freeTempMb&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; float64&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;AnomalyDecision&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	key &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Sprintf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, cur.Sid, cur.SerialNum)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	prev, exists &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fleetRegistry[key]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	fleetRegistry[key] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;SessionTracker&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{lastSample: cur}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;exists {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; AnomalyDecision&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	dt &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cur.SampleTime.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Sub&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(prev.lastSample.SampleTime).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Seconds&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dt &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Since&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(cur.SampleTime) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;time.Second {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; AnomalyDecision&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{} &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Ignore stale or out-of-order samples&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	tempVel &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (cur.TempSpillMb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; prev.lastSample.TempSpillMb) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	pgaVel &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (cur.PgaAllocMb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; prev.lastSample.PgaAllocMb) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tempVel &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 350.0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cur.TempSpillMb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10240.0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (freeTempMb&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;tempVel) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 30.0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; AnomalyDecision&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{ShouldRemediate: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, Sid: cur.Sid, SerialNum: cur.SerialNum, SqlId: cur.SqlId}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pgaVel &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 150.0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cur.PgaAllocMb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 4096.0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; AnomalyDecision&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{ShouldRemediate: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, Sid: cur.Sid, SerialNum: cur.SerialNum, SqlId: cur.SqlId}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; AnomalyDecision&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-native-cancellation-and-plan-prevention&quot;&gt;3. Native Cancellation and Plan Prevention&lt;/h3&gt;
&lt;p&gt;Do not use undocumented internal &lt;code&gt;DBMS_SYSTEM&lt;/code&gt; event workarounds (&lt;code&gt;event 10237&lt;/code&gt;) in Oracle 19c; use the documented &lt;code&gt;ALTER SYSTEM CANCEL SQL&lt;/code&gt; statement, which targets the exact &lt;code&gt;sid,serial#,sql_id&lt;/code&gt; tuple and cleans up automatically.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- 1. Surgical SQL cancellation (Oracle 18c/19c+); returns ORA-01013 to caller&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SYSTEM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CANCEL &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SQL&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;412,58912,a98b42cd71efg&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- 2. Escalation only if session remains active after grace window&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SYSTEM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; DISCONNECT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SESSION&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;412,58912&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IMMEDIATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To stop an application from immediately re-submitting the same bad plan, bind a fixed execution plan with SQL Plan Management (&lt;code&gt;DBMS_SPM&lt;/code&gt;) or a SQL Patch on standard Enterprise Edition. Note that &lt;code&gt;DBMS_SQLQ.CREATE_QUARANTINE_BY_SQL_ID&lt;/code&gt; (SQL Quarantine) is licensed strictly for &lt;strong&gt;Engineered Systems (Exadata, Exadata Cloud Service, and Autonomous Database)&lt;/strong&gt;—never enable &lt;code&gt;_exadata_feature_on&lt;/code&gt; on non-Exadata hosts to use &lt;code&gt;DBMS_SQLQ&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Automated session termination should never go live on day one without baseline calibration. Roll out the pipeline in four gated stages.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-08-22-oracle-pga-temp-pressure-hash-join-spills-5-flow.svg&quot; alt=&quot;Four-stage rollout from bounds hardening to automated SQL cancellation&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Stage 1 — Harden Instance Bounds and Per-PDB &lt;code&gt;TEMP&lt;/code&gt;:&lt;/strong&gt; Verify &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt; is explicitly set (not &lt;code&gt;0&lt;/code&gt;) and check &lt;code&gt;V$PGASTAT&lt;/code&gt; (&lt;code&gt;over allocation count&lt;/code&gt;, &lt;code&gt;total PGA allocated&lt;/code&gt;, and &lt;code&gt;maximum PGA allocated&lt;/code&gt;) to confirm normal workloads fit cleanly below the ceiling. Confirm every PDB uses its own local temporary tablespace (&lt;code&gt;ALTER DATABASE DEFAULT TEMPORARY TABLESPACE&lt;/code&gt; inside each PDB) with &lt;code&gt;EXTENT MANAGEMENT LOCAL UNIFORM SIZE&lt;/code&gt; so one tenant cannot exhaust &lt;code&gt;TEMP&lt;/code&gt; for unrelated PDBs and extent deallocation stays predictable.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stage 2 — Deploy Sidecar in Observe-Only Mode:&lt;/strong&gt; Run the &lt;code&gt;V$SQL_WORKAREA_ACTIVE&lt;/code&gt; collector and Go velocity evaluator with &lt;code&gt;dry_run=true&lt;/code&gt;. Capture legitimate ETL and month-end jobs to populate the &lt;code&gt;BATCH_EXEMPT&lt;/code&gt; module/&lt;code&gt;sql_id&lt;/code&gt; allowlist, and tune &lt;code&gt;tempVel&lt;/code&gt; and &lt;code&gt;pgaVel&lt;/code&gt; thresholds against real baseline slopes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stage 3 — Enable Soft Cancellation (&lt;code&gt;ALTER SYSTEM CANCEL SQL&lt;/code&gt;):&lt;/strong&gt; Flip &lt;code&gt;dry_run=false&lt;/code&gt; for &lt;code&gt;ALTER SYSTEM CANCEL SQL&lt;/code&gt; only, keeping &lt;code&gt;DISCONNECT SESSION&lt;/code&gt; disabled. Validate in staging with a synthetic Cartesian join that &lt;code&gt;ORA-01013&lt;/code&gt; aborts the runaway cursor while leaving the session intact, then monitor production &lt;code&gt;ORA-01013&lt;/code&gt; counts for zero false positives on allowlisted batch workloads. Rollback is setting &lt;code&gt;dry_run=true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stage 4 — Enable Escalation and Plan Controls:&lt;/strong&gt; Enable the &lt;code&gt;DISCONNECT SESSION IMMEDIATE&lt;/code&gt; fallback for sessions that ignore &lt;code&gt;CANCEL SQL&lt;/code&gt;, and wire repeated &lt;code&gt;sql_id&lt;/code&gt; offenders into SQL Plan Baselines (or &lt;code&gt;DBMS_SQLQ&lt;/code&gt; on Exadata) so client retry loops do not immediately re-trigger the same spill.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;This architecture aligns directly with documented Oracle Database 19c memory and workarea behavior:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Explicit &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt; protection:&lt;/strong&gt; Because Oracle 19c defaults &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt; to the maximum of &lt;code&gt;2 GB&lt;/code&gt;, &lt;code&gt;200%&lt;/code&gt; of &lt;code&gt;PGA_AGGREGATE_TARGET&lt;/code&gt;, or &lt;code&gt;3 MB * PROCESSES&lt;/code&gt; (&lt;code&gt;5 MB * PROCESSES&lt;/code&gt; on RAC), leaving the limit active ensures &lt;code&gt;ORA-04036&lt;/code&gt; aborts runaway sessions before host RAM exhaustion triggers the Linux OOM killer. Setting &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt; explicitly in &lt;code&gt;spfile&lt;/code&gt; also prevents unexpected ceiling shifts if a DBA increases &lt;code&gt;PROCESSES&lt;/code&gt; without reviewing host memory headroom.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Workarea vs untunable PGA visibility:&lt;/strong&gt; &lt;code&gt;V$SQL_WORKAREA_ACTIVE&lt;/code&gt; captures tunable SQL workareas (sorts, hash joins, bitmap merges), while &lt;code&gt;V$PROCESS.PGA_ALLOC_MEM&lt;/code&gt; and &lt;code&gt;V$PROCESS_MEMORY&lt;/code&gt; expose total process PGA including untunable PL/SQL associative arrays and Java heaps. Querying both together catches both hash-join &lt;code&gt;TEMP&lt;/code&gt; spills and PL/SQL memory leaks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Temporary extent cleanup and &lt;code&gt;V$TEMPSEG_USAGE&lt;/code&gt;:&lt;/strong&gt; When a large sort or hash join is cancelled via &lt;code&gt;ALTER SYSTEM CANCEL SQL&lt;/code&gt;, the session’s temporary segments are marked for cleanup, and &lt;code&gt;SMON&lt;/code&gt; deallocates the extents back to the tablespace. Using &lt;code&gt;EXTENT MANAGEMENT LOCAL UNIFORM SIZE&lt;/code&gt; (for example, &lt;code&gt;64M&lt;/code&gt; or &lt;code&gt;128M&lt;/code&gt; for analytical PDBs) avoids the dictionary-extent fragmentation that slows down &lt;code&gt;SMON&lt;/code&gt; cleanup after a multi-gigabyte spill.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Native 18c/19c &lt;code&gt;CANCEL SQL&lt;/code&gt; semantics:&lt;/strong&gt; &lt;code&gt;ALTER SYSTEM CANCEL SQL &apos;sid,serial#,sql_id&apos;&lt;/code&gt; interrupts only the active cursor without tearing down connection-pool sockets, whereas &lt;code&gt;ALTER SYSTEM DISCONNECT SESSION&lt;/code&gt; tears down the session cleanly when a process is stuck outside an interruptible SQL fetch. Pairing cancellation logs with &lt;code&gt;V$SQL_PLAN_MONITOR&lt;/code&gt; (where Diagnostics and Tuning Pack is licensed) lets database engineers pinpoint the exact hash join or sort step that triggered the spill.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Sub-second automated remediation introduces four operational hazards that require explicit guardrails in the collector and evaluator. Each failure mode stems from treating a live transactional database like a stateless worker without accounting for batch exemptions, client retry loops, or multi-tenant &lt;code&gt;TEMP&lt;/code&gt; sharing.&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Legitimate batch sort hits velocity threshold&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Month-end ETL job gets cancelled mid-run (&lt;code&gt;ORA-01013&lt;/code&gt;).&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Spikes in cancelled &lt;code&gt;BATCH&lt;/code&gt; module jobs during nightly windows.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Maintain a &lt;code&gt;BATCH_EXEMPT&lt;/code&gt; allowlist by &lt;code&gt;module&lt;/code&gt; / &lt;code&gt;sql_id&lt;/code&gt; and isolate batch in Resource Manager.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;App framework retries &lt;code&gt;ORA-01013&lt;/code&gt; immediately&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cancelled query reconnects in a tight loop, repeating the &lt;code&gt;TEMP&lt;/code&gt; spike.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Same &lt;code&gt;sql_id&lt;/code&gt; cancelled repeatedly within seconds.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fix plan via &lt;code&gt;DBMS_SPM&lt;/code&gt; / SQL Patch (or &lt;code&gt;DBMS_SQLQ&lt;/code&gt; on Exadata) and rate-limit at API gateway.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Kafka lag delivers stale telemetry ticks&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Evaluator triggers &lt;code&gt;CANCEL SQL&lt;/code&gt; minutes late against a reused &lt;code&gt;SID&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Event timestamp delta exceeds 5 seconds.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Drop samples older than 5 seconds and always require exact &lt;code&gt;(sid, serial#, sql_id)&lt;/code&gt; match.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Shared &lt;code&gt;CDB$ROOT&lt;/code&gt; &lt;code&gt;TEMP&lt;/code&gt; across PDBs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;One runaway tenant fills shared &lt;code&gt;TEMP&lt;/code&gt;, failing queries in all sibling PDBs.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;ORA-01652&lt;/code&gt; errors across multiple PDBs simultaneously.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Provision dedicated local &lt;code&gt;TEMP&lt;/code&gt; tablespaces per PDB with &lt;code&gt;UNIFORM SIZE&lt;/code&gt; extents.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;V$SQL_WORKAREA_ACTIVE&lt;/code&gt; polled too aggressively&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sub-second polling during heavy concurrency adds library-cache latch contention.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rising collector query latency or CPU overhead on the host.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use adaptive 5s/1s polling and filter to sessions above 50 MB PGA or 100 MB &lt;code&gt;TEMP&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Untunable PL/SQL heap fragmentation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;ORA-04030&lt;/code&gt; or &lt;code&gt;ORA-04036&lt;/code&gt; fires even while &lt;code&gt;V$SQL_WORKAREA_ACTIVE&lt;/code&gt; looks small.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;V$PROCESS_MEMORY&lt;/code&gt; &lt;code&gt;PL/SQL&lt;/code&gt; category growing rapidly.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Include &lt;code&gt;V$PROCESS.PGA_ALLOC_MEM&lt;/code&gt; velocity in the evaluator alongside &lt;code&gt;tempseg_size&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2023-08-22-oracle-pga-temp-pressure-hash-join-spills-6-failures.svg&quot; alt=&quot;Four failure paths in automated workarea remediation&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Never set &lt;code&gt;PGA_AGGREGATE_LIMIT = 0&lt;/code&gt; to silence &lt;code&gt;ORA-04036&lt;/code&gt;; keep the hard ceiling enabled so Oracle terminates runaway sessions before the Linux OOM killer hits &lt;code&gt;PMON&lt;/code&gt; or &lt;code&gt;LGWR&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Sample &lt;code&gt;V$SQL_WORKAREA_ACTIVE&lt;/code&gt; joined to &lt;code&gt;V$SESSION&lt;/code&gt; and &lt;code&gt;V$PROCESS&lt;/code&gt; (with adaptive 5s/1s polling and a minimum size filter) rather than polling &lt;code&gt;DBA_*&lt;/code&gt; dictionary views on 5-minute intervals.&lt;/li&gt;
&lt;li&gt;Trigger remediation on &lt;strong&gt;allocation velocity&lt;/strong&gt; (&lt;code&gt;dTemp/dt&lt;/code&gt;, &lt;code&gt;dPGA/dt&lt;/code&gt;) and projected Time-To-Exhaustion rather than static fill percentages alone.&lt;/li&gt;
&lt;li&gt;Use Oracle’s supported &lt;code&gt;ALTER SYSTEM CANCEL SQL &apos;sid,serial#,sql_id&apos;&lt;/code&gt; (18c/19c+) instead of legacy &lt;code&gt;DBMS_SYSTEM&lt;/code&gt; event workarounds, and reserve &lt;code&gt;DBMS_SQLQ&lt;/code&gt; SQL Quarantine strictly for Exadata (&lt;code&gt;EE-ES&lt;/code&gt;) environments.&lt;/li&gt;
&lt;li&gt;Isolate multi-tenant &lt;code&gt;TEMP&lt;/code&gt; blast radiuses by assigning each Pluggable Database its own local temporary tablespace and protecting batch workloads with a &lt;code&gt;BATCH_EXEMPT&lt;/code&gt; allowlist.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Runaway hash joins and PL/SQL leaks saturate &lt;code&gt;TEMP&lt;/code&gt; I/O and breach PGA ceilings faster than 5-minute monitoring pollers can detect.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Deploy a 1-second adaptive sidecar on &lt;code&gt;V$SQL_WORKAREA_ACTIVE&lt;/code&gt;, &lt;code&gt;V$SESSION&lt;/code&gt;, and &lt;code&gt;V$PROCESS&lt;/code&gt;, compute per-session allocation velocity and Time-To-Exhaustion, and neutralize non-exempt culprits with &lt;code&gt;ALTER SYSTEM CANCEL SQL&lt;/code&gt; before &lt;code&gt;TEMP&lt;/code&gt; or PGA limits trip.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run the collector in &lt;code&gt;dry_run=true&lt;/code&gt; mode for one week against production workloads, verify zero false-positive triggers against allowlisted batch jobs, and confirm synthetic runaway spills in staging are cancelled before &lt;code&gt;ORA-01652&lt;/code&gt; or &lt;code&gt;ORA-04036&lt;/code&gt; occurs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt;, &lt;code&gt;V$PGASTAT&lt;/code&gt; over-allocation counts, and per-PDB &lt;code&gt;TEMP&lt;/code&gt; tablespace isolation across your 19c fleet this week before enabling automated &lt;code&gt;CANCEL SQL&lt;/code&gt; in non-production. Document any long-running ETL modules in &lt;code&gt;BATCH_EXEMPT&lt;/code&gt; during shadow mode.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>failures</category><category>cloud</category></item><item><title>Building an Oracle Gold Image Factory: Deterministic Artifact Pipelines for Fleet-Scale Patching</title><link>https://rajivonai.com/blog/2023-08-15-building-oracle-gold-image-factory/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-08-15-building-oracle-gold-image-factory/</guid><description>Why running OPatch and binary relinking on hundreds of production database servers causes catastrophic configuration drift, and how to build a build-once, deploy-everywhere CI/CD pipeline using Oracle Gold Images.</description><pubDate>Sun, 17 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Compiling and linking database binaries directly on production servers turns every quarterly patch cycle into an unrepeatable build, guaranteeing configuration drift and turning routine maintenance into relink failures.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Problem: Running &lt;code&gt;opatch apply&lt;/code&gt; and &lt;code&gt;make -f ins_rdbms.mk ioracle&lt;/code&gt; on every production host causes &lt;code&gt;glibc&lt;/code&gt;/toolchain drift, &lt;code&gt;/tmp&lt;/code&gt; and &lt;code&gt;.patch_storage&lt;/code&gt; bloat, and unstartable homes when a relink aborts mid-flight.&lt;/li&gt;
&lt;li&gt;Answer: Compile, conflict-check, and relink once inside an ephemeral CI runner, package the home with &lt;code&gt;runInstaller -createGoldImage&lt;/code&gt;, and deploy out-of-place across the fleet using &lt;code&gt;oui/bin/runInstaller -attachHome&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Watch out: Gold images are bound to the target OS &lt;code&gt;glibc&lt;/code&gt; ABI (build separately per OS major version), &lt;code&gt;root.sh&lt;/code&gt; must run after &lt;code&gt;-attachHome&lt;/code&gt; to restore SetUID permissions, and parallel &lt;code&gt;-attachHome&lt;/code&gt; runs require a host-level inventory lock.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2023-08-15-building-oracle-gold-image-factory.svg&quot; alt=&quot;Oracle Gold Image Factory overview from CI build to fleet switchover&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Enterprise database fleets running Oracle Database 19c must apply quarterly Release Updates (RUs), Oracle Java Virtual Machine (OJVM) patches, and occasional one-off diagnostic fixes. In traditional operations, configuration management playbooks (Ansible, SaltStack, or shell scripts) execute the full patching workflow independently on every database server in the fleet.&lt;/p&gt;
&lt;p&gt;On each host, the automation copies multi-gigabyte patch archives to local storage, unpacks them into &lt;code&gt;/tmp&lt;/code&gt;, upgrades the local OPatch utility, runs &lt;code&gt;opatch prereq CheckConflictAgainstOHWithDetail&lt;/code&gt;, executes &lt;code&gt;opatch apply&lt;/code&gt;, and invokes &lt;code&gt;make -f ins_rdbms.mk ioracle&lt;/code&gt; to relink &lt;code&gt;oracle&lt;/code&gt;, &lt;code&gt;tnslsnr&lt;/code&gt;, &lt;code&gt;expdp&lt;/code&gt;, and &lt;code&gt;impdp&lt;/code&gt; against whatever OS libraries and headers happen to reside on that server.&lt;/p&gt;
&lt;p&gt;When a fleet spans dozens or hundreds of hosts, target-side compilation repeats the exact same conflict checks, archive extractions, and C-library relinking on every machine. Any minor difference in OS package revisions, &lt;code&gt;umask&lt;/code&gt; settings, or &lt;code&gt;/tmp&lt;/code&gt; free space turns a uniform quarterly patch into a per-host debugging exercise.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Target-side compilation violates a core reliability principle: &lt;strong&gt;production database binaries should be immutable, hermetic, and byte-for-byte identical across every host in an OS tier&lt;/strong&gt;. Executing OPatch and &lt;code&gt;make&lt;/code&gt; locally on production servers fails along four concrete constraints.&lt;/p&gt;
&lt;p&gt;First, &lt;strong&gt;toolchain and &lt;code&gt;glibc&lt;/code&gt; ABI skew&lt;/strong&gt;. Differences in minor revisions of &lt;code&gt;gcc&lt;/code&gt;, &lt;code&gt;binutils&lt;/code&gt;, &lt;code&gt;glibc&lt;/code&gt;, or &lt;code&gt;libaio&lt;/code&gt; across servers cause &lt;code&gt;relink&lt;/code&gt; to bind against different system library symbols. A binary linked on a slightly older staging host may behave differently—or fail symbol resolution—compared to a production host patched a month later. Hardened &lt;code&gt;umask&lt;/code&gt; settings (&lt;code&gt;027&lt;/code&gt; instead of &lt;code&gt;022&lt;/code&gt;) on a subset of hosts can also strip group/other read and execute bits during relink, breaking local OS clients that load &lt;code&gt;libclntsh.so&lt;/code&gt; outside the &lt;code&gt;oinstall&lt;/code&gt; group.&lt;/p&gt;
&lt;p&gt;Second, &lt;strong&gt;mid-rollout superseded patches&lt;/strong&gt;. Oracle Support periodically re-releases a Release Update or interim patch mid-quarter to fix a regression. If a fleet rollout takes weeks and pulls patches live from a mirror, early hosts receive Revision A while later hosts receive Revision B, splitting the fleet into undocumented patch variants. Auditing which host carries which sub-revision requires scraping &lt;code&gt;opatch lsinventory&lt;/code&gt; text output across every server.&lt;/p&gt;
&lt;p&gt;Third, &lt;strong&gt;storage and &lt;code&gt;.patch_storage&lt;/code&gt; bloat&lt;/strong&gt;. Unpacking multi-gigabyte RU archives on production hosts competes for CPU and disk I/O with live workloads and risks exhausting &lt;code&gt;/tmp&lt;/code&gt; inodes or &lt;code&gt;/u01&lt;/code&gt; space. Repeated in-place &lt;code&gt;opatch apply&lt;/code&gt; runs also accumulate historical rollback backups inside &lt;code&gt;$ORACLE_HOME/.patch_storage&lt;/code&gt;, bloating the home directory and slowing &lt;code&gt;opatch lsinventory&lt;/code&gt; scans.&lt;/p&gt;
&lt;p&gt;Fourth, &lt;strong&gt;corrupted in-place relink&lt;/strong&gt;. If an in-place &lt;code&gt;opatch apply&lt;/code&gt; fails mid-relink due to disk exhaustion or a missing prerequisite, the active &lt;code&gt;$ORACLE_HOME&lt;/code&gt; is left partially linked. The database cannot start until an operator repairs the binary tree or executes &lt;code&gt;opatch rollback&lt;/code&gt; under outage pressure.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-08-15-building-oracle-gold-image-factory-2-constraints.svg&quot; alt=&quot;Four constraints that break target-side OPatch runs&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do platform teams eliminate target-side compilation entirely and turn Oracle Database patching into a deterministic, single-artifact deployment pipeline?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams typically evaluate four ways to distribute Oracle Database binaries across a fleet before adopting a native Gold Image Factory.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Gets Right&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;In-place &lt;code&gt;opatch apply&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires no extra disk for a second &lt;code&gt;$ORACLE_HOME&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Long database downtime during relink; toolchain skew; risky rollback if relink aborts.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Ad-hoc &lt;code&gt;tar&lt;/code&gt; / &lt;code&gt;zip&lt;/code&gt; of &lt;code&gt;$ORACLE_HOME&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Captures a pre-linked directory tree without running &lt;code&gt;make&lt;/code&gt; on targets.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Risks flattening symbolic links, baking in host &lt;code&gt;*.ora&lt;/code&gt; configs, or carrying &lt;code&gt;.patch_storage&lt;/code&gt; bloat.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Full VM / AMI image bake&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Guarantees OS and database binary immutability together.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Couples OS and DB patch cycles; requires replacing hosts each quarter, which is disruptive on bare metal and RAC.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;runInstaller -createGoldImage&lt;/code&gt; + &lt;code&gt;-attachHome&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Preserves symlinks, excludes host state, separates OS from DB lifecycle, keeps &lt;code&gt;OLD_HOME&lt;/code&gt; for instant rollback.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires space on &lt;code&gt;/u01&lt;/code&gt; for two homes during cutover and one build per target OS major version.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Ad-hoc &lt;code&gt;tar&lt;/code&gt; or &lt;code&gt;zip&lt;/code&gt; archives look deceptively simple, but forgetting &lt;code&gt;zip -y&lt;/code&gt; flattens symbolic links into duplicate files, inflating the archive and breaking shared library resolution. Full VM or AMI baking works well for ephemeral stateless tiers, yet replacing persistent database hosts—especially bare-metal servers, ASM configurations, or RAC clusters—every quarter introduces unnecessary storage re-attachment and network reconfiguration. Oracle Fleet Patching and Provisioning (FPP) solves the same problem natively inside Grid Infrastructure ecosystems, but it requires standing up and operating a dedicated FPP Server. For heterogeneous fleets mixing standalone Oracle Restart, single-instance cloud VMs, and RAC, a CI-driven Gold Image Factory using &lt;code&gt;runInstaller -createGoldImage&lt;/code&gt; gives every target the same immutable artifact without requiring an FPP control plane.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-08-15-building-oracle-gold-image-factory-3-options.svg&quot; alt=&quot;Comparison of four Oracle fleet patching options showing why Native Gold Images are chosen&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-oracle-gold-image-factory-architecture&quot;&gt;The Oracle Gold Image Factory Architecture&lt;/h2&gt;
&lt;p&gt;The Gold Image Factory shifts Oracle patching from &lt;em&gt;target-side compilation&lt;/em&gt; to &lt;strong&gt;build-once, deploy-everywhere immutability&lt;/strong&gt;. All patch extraction, conflict analysis, and binary relinking happen once inside an isolated, ephemeral CI runner. Production servers never invoke &lt;code&gt;opatch apply&lt;/code&gt; or &lt;code&gt;gcc&lt;/code&gt;; they verify a cryptographic checksum, unpack the pre-linked Gold Image into a new out-of-place directory, register it in &lt;code&gt;oraInventory&lt;/code&gt;, and switch the instance pointer.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-08-15-building-oracle-gold-image-factory-4-architecture.svg&quot; alt=&quot;Gold Image Factory pipeline from base 19.3 to signed artifact&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;factory-core-mechanics&quot;&gt;Factory Core Mechanics&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Ephemeral OS-Matched Build Runners:&lt;/strong&gt; Each Gold Image is built inside a throwaway container or VM running the exact target OS major release (for example, Oracle Linux 7 or Oracle Linux 8) with locked toolchain packages. Because compiled Oracle binaries link dynamically against the host’s &lt;code&gt;glibc&lt;/code&gt;, maintaining one build runner per OS major family prevents ABI mismatches at startup.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pre-Flight Conflict Resolution:&lt;/strong&gt; &lt;code&gt;opatch prereq CheckConflictAgainstOHWithDetail&lt;/code&gt; runs once in CI against the exact combination of Release Update, OJVM RU, and approved one-off fixes. Any overlay or subset conflict fails the CI pipeline before a single production host is touched.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Storage Sanitization (&lt;code&gt;cleanup&lt;/code&gt; and &lt;code&gt;deleteinactivepatches&lt;/code&gt;):&lt;/strong&gt; Before packaging, the factory runs &lt;code&gt;opatch util cleanup -silent&lt;/code&gt; to purge temporary logs, &lt;code&gt;make.txt&lt;/code&gt; artifacts, and scratch directories in &lt;code&gt;$ORACLE_HOME/.patch_storage&lt;/code&gt;. For long-lived homes with stacked patch histories, OPatch &lt;code&gt;12.2.0.1.37&lt;/code&gt; and later also provide &lt;code&gt;opatch util deleteinactivepatches&lt;/code&gt; to remove superseded RU backup sets safely (never delete &lt;code&gt;.patch_storage&lt;/code&gt; manually with &lt;code&gt;rm -rf&lt;/code&gt;, as that corrupts OUI metadata).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Native Image Synthesis (&lt;code&gt;-createGoldImage&lt;/code&gt;):&lt;/strong&gt; Starting in Oracle 18c/19c (replacing the deprecated &lt;code&gt;clone.pl&lt;/code&gt; workflow), &lt;code&gt;$ORACLE_HOME/runInstaller -silent -createGoldImage -destinationLocation &amp;#x3C;dir&gt;&lt;/code&gt; packages a clean, portable &lt;code&gt;.zip&lt;/code&gt; of the patched home, preserving symbolic links and omitting runtime logs or host-specific state. Pairing this with an external &lt;code&gt;TNS_ADMIN&lt;/code&gt; directory (such as &lt;code&gt;/etc/oracle/network/admin&lt;/code&gt; outside &lt;code&gt;$ORACLE_HOME&lt;/code&gt;) ensures &lt;code&gt;tnsnames.ora&lt;/code&gt;, &lt;code&gt;sqlnet.ora&lt;/code&gt;, and &lt;code&gt;listener.ora&lt;/code&gt; never get overwritten or lost when switching homes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cryptographic Attestation and XML SBOM:&lt;/strong&gt; Every Gold Image is paired with a SHA-256 digest and an XML Software Bill of Materials exported via &lt;code&gt;opatch lsinventory -xml&lt;/code&gt;, giving security and compliance scanners an exact manifest of every bug fix inside the artifact. Storing the &lt;code&gt;.sha256&lt;/code&gt; and &lt;code&gt;-sbom.xml&lt;/code&gt; files alongside the &lt;code&gt;.zip&lt;/code&gt; in the artifact registry lets deployment playbooks reject tampered or truncated downloads before extraction begins.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;1-automated-ci-build-script&quot;&gt;1. Automated CI Build Script&lt;/h3&gt;
&lt;p&gt;The CI runner builds a fresh 19c home from the base &lt;code&gt;19.3.0.0&lt;/code&gt; distribution, upgrades OPatch (&lt;code&gt;p6880880&lt;/code&gt;), applies the target RU and OJVM RU (note that &lt;code&gt;-ocmrf&lt;/code&gt; is omitted in modern 19c OPatch since Oracle Configuration Manager was retired), cleans scratch storage, and exports the Gold Image alongside its XML SBOM and SHA-256 manifest.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -euo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pipefail&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;BUILD_DIR&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/tmp/oracle_build_&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;$$&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;STAGE_DIR&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/opt/oracle/stage&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OUTPUT_NAME&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;oracle-db-19.23.0.0-ol8&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPORT_DIR&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/mnt/artifacts/oracle&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mkdir&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -p&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;BUILD_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/db_home&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPORT_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;BUILD_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/db_home&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PATH&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/bin:${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/OPatch:${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;PATH&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;unzip&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -q&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;STAGE_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/LINUX.X64_193000_db_home.zip&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;rm&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -rf&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/OPatch&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;unzip&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -q&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;STAGE_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/p6880880_190000_Linux-x86-64.zip&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;unzip&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -q&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;STAGE_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/p36233263_190000_Linux-x86-64.zip&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;BUILD_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/patches&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;opatch&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; apply&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -silent&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;BUILD_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/patches/36233263&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;unzip&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -q&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;STAGE_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/p36199271_190000_Linux-x86-64.zip&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;BUILD_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/patches&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;opatch&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; apply&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -silent&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;BUILD_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/patches/36199271&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;opatch&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; util&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; cleanup&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -silent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;}/runInstaller&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -silent&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -createGoldImage&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -destinationLocation&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPORT_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RAW_ZIP&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ls&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -t&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPORT_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;/db_home_&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.zip&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; head&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -n&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mv&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RAW_ZIP&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPORT_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OUTPUT_NAME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}.zip&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;opatch&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; lsinventory&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -xml&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPORT_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OUTPUT_NAME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}-sbom.xml&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sha256sum&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPORT_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OUTPUT_NAME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}.zip&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPORT_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OUTPUT_NAME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}.sha256&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-fleet-pre-staging-playbook-ansible&quot;&gt;2. Fleet Pre-Staging Playbook (Ansible)&lt;/h3&gt;
&lt;p&gt;Target servers pull the signed archive from a regional object-storage cache while the database remains completely online, verify its SHA-256 checksum, unpack it into the new &lt;code&gt;$ORACLE_HOME&lt;/code&gt;, register it with &lt;code&gt;$ORACLE_HOME/oui/bin/runInstaller -silent -attachHome&lt;/code&gt;, and execute &lt;code&gt;root.sh&lt;/code&gt; to set SetUID permissions on privileged helpers like &lt;code&gt;extjob&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;- &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Stage and attach immutable Oracle Gold Image&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  hosts&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;oracle_database_servers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  become&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  become_user&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;oracle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  vars&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    artifact_url&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;https://artifacts.internal.net/oracle/oracle-db-19.23.0.0-ol8.zip&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    expected_sha256&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;8e73c4f5a3b2e1d0987654321fedcba0123456789abcdef0123456789abcdef0&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    target_oracle_home&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/u01/app/oracle/product/19.23.0.0/dbhome_1&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    home_name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;OraDB19Home_1923&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  tasks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Create target Oracle Home directory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      ansible.builtin.file&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        path&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;{{ target_oracle_home }}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;directory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        owner&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;oracle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        group&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;oinstall&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        mode&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;0755&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Download and verify Gold Image checksum&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      ansible.builtin.get_url&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        url&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;{{ artifact_url }}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        dest&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/tmp/{{ home_name }}.zip&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        checksum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sha256:{{ expected_sha256 }}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        mode&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;0640&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Unpack pre-compiled binaries&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      ansible.builtin.unarchive&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        src&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/tmp/{{ home_name }}.zip&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        dest&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;{{ target_oracle_home }}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        remote_src&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Register home in central oraInventory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      ansible.builtin.command&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        cmd&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          flock /tmp/orainv.lock&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          {{ target_oracle_home }}/oui/bin/runInstaller -silent -attachHome&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          ORACLE_HOME=&quot;{{ target_oracle_home }}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          ORACLE_HOME_NAME=&quot;{{ home_name }}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      register&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;attach_out&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      changed_when&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&apos;The inventory has been successfully updated&apos; in attach_out.stdout&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;Run root.sh for SetUID binary permissions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      become_user&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;root&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      ansible.builtin.command&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        cmd&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;{{ target_oracle_home }}/root.sh&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      changed_when&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Deploying a Gold Image across production separates &lt;strong&gt;online binary staging&lt;/strong&gt; from the &lt;strong&gt;maintenance-window cutover&lt;/strong&gt;, keeping database downtime limited to instance restart and &lt;code&gt;datapatch&lt;/code&gt; execution.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-08-15-building-oracle-gold-image-factory-5-flow.svg&quot; alt=&quot;Four-stage out-of-place switchover and instant rollback flow&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Stage 1 — Pre-Stage and Verify Online:&lt;/strong&gt; Distribute the Gold Image &lt;code&gt;.zip&lt;/code&gt; to regional artifact caches and download it to target hosts while databases remain online. Gate on exact SHA-256 verification before unpacking.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stage 2 — Unpack, Attach, and Verify Permissions Online:&lt;/strong&gt; Extract the archive into &lt;code&gt;NEW_HOME&lt;/code&gt;, serialize &lt;code&gt;-attachHome&lt;/code&gt; with &lt;code&gt;flock&lt;/code&gt; against &lt;code&gt;/etc/oraInst.loc&lt;/code&gt;, run &lt;code&gt;root.sh&lt;/code&gt;, and verify SetUID permissions on &lt;code&gt;$NEW_HOME/bin/extjob&lt;/code&gt; and &lt;code&gt;opatch lsinventory&lt;/code&gt; output. Copy or link instance parameter files (&lt;code&gt;spfile&lt;/code&gt;, &lt;code&gt;orapw&lt;/code&gt;, and external &lt;code&gt;TNS_ADMIN&lt;/code&gt; configs) if they reside inside &lt;code&gt;OLD_HOME/dbs&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stage 3 — Maintenance Window Cutover and &lt;code&gt;datapatch&lt;/code&gt;:&lt;/strong&gt; Stop the instance, update Clusterware/Oracle Restart with &lt;code&gt;srvctl modify database&lt;/code&gt;, start the instance from &lt;code&gt;NEW_HOME&lt;/code&gt;, and run &lt;code&gt;datapatch -verbose&lt;/code&gt;. Because &lt;code&gt;datapatch&lt;/code&gt; only updates Pluggable Databases (PDBs) that are open read-write, confirm all PDBs are open before invoking &lt;code&gt;datapatch&lt;/code&gt;, then verify &lt;code&gt;DBA_REGISTRY_SQLPATCH&lt;/code&gt; shows &lt;code&gt;SUCCESS&lt;/code&gt; across &lt;code&gt;CDB$ROOT&lt;/code&gt; and every PDB. On RAC clusters, attach &lt;code&gt;NEW_HOME&lt;/code&gt; across all nodes in Stage 2, then switch instances and run &lt;code&gt;datapatch&lt;/code&gt; once the cluster is running on &lt;code&gt;NEW_HOME&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stage 4 — Instant Rollback if Needed:&lt;/strong&gt; If &lt;code&gt;datapatch&lt;/code&gt; or application smoke tests fail, stop the instance, point &lt;code&gt;srvctl modify database&lt;/code&gt; back to the untouched &lt;code&gt;OLD_HOME&lt;/code&gt;, start the database, and run &lt;code&gt;datapatch -rollback -verbose&lt;/code&gt; (or &lt;code&gt;datapatch -verbose&lt;/code&gt; from &lt;code&gt;OLD_HOME&lt;/code&gt;) to revert SQL registry changes. Once all instances are verified on &lt;code&gt;NEW_HOME&lt;/code&gt; after the bake-in window, decommission &lt;code&gt;OLD_HOME&lt;/code&gt; with &lt;code&gt;oui/bin/runInstaller -silent -detachHome&lt;/code&gt; before deleting the directory so &lt;code&gt;inventory.xml&lt;/code&gt; stays clean.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; OLD_HOME&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/u01/app/oracle/product/19.22.0.0/dbhome_1&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; NEW_HOME&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/u01/app/oracle/product/19.23.0.0/dbhome_1&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;cp&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OLD_HOME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/dbs/spfilePAYMENTSP1.ora&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OLD_HOME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/dbs/orapwPAYMENTSP1&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;NEW_HOME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/dbs/&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; stop&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; database&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PAYMENTSP1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -stopoption&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; IMMEDIATE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; modify&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; database&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PAYMENTSP1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -oraclehome&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;NEW_HOME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; start&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; database&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; PAYMENTSP1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;NEW_HOME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PATH&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/bin:${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;PATH&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;datapatch&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -verbose&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The Gold Image Factory design relies directly on documented Oracle 19c installer, OPatch, and &lt;code&gt;datapatch&lt;/code&gt; mechanics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Native Gold Image replacement for &lt;code&gt;clone.pl&lt;/code&gt;:&lt;/strong&gt; Oracle Database 19c deprecated &lt;code&gt;clone.pl&lt;/code&gt; and &lt;code&gt;runInstaller -clone&lt;/code&gt; in favor of &lt;code&gt;runInstaller -createGoldImage&lt;/code&gt;, which packages a sanitized &lt;code&gt;$ORACLE_HOME&lt;/code&gt; archive ready for extraction and registration on target nodes. Because the installer excludes local logs and transient state, every target node unpacks the exact same binary tree rather than inheriting build-runner artifacts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Out-of-place &lt;code&gt;datapatch&lt;/code&gt; rollback metadata:&lt;/strong&gt; Starting in modern Oracle releases including 19c, &lt;code&gt;datapatch&lt;/code&gt; stores SQL rollback scripts inside the &lt;code&gt;SYSTEM&lt;/code&gt; tablespace during patch application. Switching back to &lt;code&gt;OLD_HOME&lt;/code&gt; and running &lt;code&gt;datapatch -rollback -verbose&lt;/code&gt; reverts the SQL dictionary state without needing OPatch to uninstall binaries on the host. Always keep &lt;code&gt;OLD_HOME&lt;/code&gt; intact until application teams sign off on the new Release Update.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;OPatch storage hygiene in 19c:&lt;/strong&gt; In long-lived homes, &lt;code&gt;.patch_storage&lt;/code&gt; grows with every quarterly Release Update. Oracle’s OPatch &lt;code&gt;12.2.0.1.37&lt;/code&gt; (released in April 2023) introduced &lt;code&gt;opatch util listorderedinactivepatches&lt;/code&gt; and &lt;code&gt;opatch util deleteinactivepatches&lt;/code&gt; to remove superseded RU backup sets safely while preserving the active rollback baseline, complementing &lt;code&gt;opatch util cleanup&lt;/code&gt; for scratch files.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Automated inventory verification:&lt;/strong&gt; Running &lt;code&gt;opatch lsinventory -xml&lt;/code&gt; produces a machine-readable XML manifest in CI and on target hosts, enabling fleet compliance checks without scraping unstructured text output. Comparing the target host’s XML output against the CI artifact’s SBOM confirms zero drift after cutover.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Centralizing binary compilation moves failure modes from per-host compiler errors to ABI compatibility, inventory locking, and filesystem lifecycle management. None of these failures are hard to prevent, but each requires an explicit pipeline gate because target hosts no longer execute &lt;code&gt;opatch prereq&lt;/code&gt; locally before extraction.&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;glibc&lt;/code&gt; ABI mismatch across OS versions&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;OL8-built Gold Image fails to launch on OL7 hosts with &lt;code&gt;GLIBC_2.28 not found&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Startup failure or &lt;code&gt;ldd $ORACLE_HOME/bin/oracle&lt;/code&gt; missing symbol errors.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Maintain separate CI build runners and artifact tags per OS major release (&lt;code&gt;ol7&lt;/code&gt;, &lt;code&gt;ol8&lt;/code&gt;).&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Fleet-wide simultaneous image pull&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Hundreds of hosts pull multi-GB &lt;code&gt;.zip&lt;/code&gt; files over a shared WAN link at once.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Network saturation and Ansible &lt;code&gt;get_url&lt;/code&gt; timeouts.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pre-stage artifacts in regional object storage caches and deploy in canary waves.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Concurrent &lt;code&gt;-attachHome&lt;/code&gt; runs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Two automation jobs write to &lt;code&gt;/etc/oraInst.loc&lt;/code&gt; inventory simultaneously.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;Inventory is locked by another process&lt;/code&gt; or corrupted &lt;code&gt;inventory.xml&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Serialize &lt;code&gt;-attachHome&lt;/code&gt; per host using &lt;code&gt;flock&lt;/code&gt; or Ansible &lt;code&gt;serial&lt;/code&gt; / mutex controls.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;root.sh&lt;/code&gt; skipped after &lt;code&gt;-attachHome&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Privileged binaries lack SetUID root ownership; external jobs and metrics fail.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;ls -l $ORACLE_HOME/bin/extjob&lt;/code&gt; missing &lt;code&gt;-rwsr-s---&lt;/code&gt; root permissions.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Make &lt;code&gt;root.sh&lt;/code&gt; and SetUID permission assertion mandatory pipeline gates.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Static &lt;code&gt;sqlnet.ora&lt;/code&gt; baked into image&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Build-host network config overwrites target server settings on extraction.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unexpected TNS resolution errors or tracing paths after cutover.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Strip &lt;code&gt;*.ora&lt;/code&gt; and host-specific files before &lt;code&gt;-createGoldImage&lt;/code&gt;; keep &lt;code&gt;TNS_ADMIN&lt;/code&gt; external.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Old homes never decommissioned&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Accumulating four+ quarterly homes exhausts &lt;code&gt;/u01&lt;/code&gt; space or inodes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;/u01&lt;/code&gt; capacity or &lt;code&gt;df -i&lt;/code&gt; inode alerts prior to staging.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Retain only &lt;code&gt;Active_Home&lt;/code&gt; and &lt;code&gt;Previous_Home&lt;/code&gt;; decommission older homes via &lt;code&gt;-detachHome&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2023-08-15-building-oracle-gold-image-factory-6-failures.svg&quot; alt=&quot;Four failure paths in Gold Image distribution&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Never run &lt;code&gt;opatch apply&lt;/code&gt; or &lt;code&gt;make&lt;/code&gt; directly on production database servers; build, conflict-check, and relink once in an ephemeral CI runner matched to the target OS &lt;code&gt;glibc&lt;/code&gt; ABI.&lt;/li&gt;
&lt;li&gt;Use Oracle’s native &lt;code&gt;runInstaller -silent -createGoldImage&lt;/code&gt; rather than ad-hoc &lt;code&gt;tar&lt;/code&gt; or &lt;code&gt;zip&lt;/code&gt; commands so symbolic links stay intact and host-specific state is excluded.&lt;/li&gt;
&lt;li&gt;Pair &lt;code&gt;opatch util cleanup -silent&lt;/code&gt; (and &lt;code&gt;opatch util deleteinactivepatches&lt;/code&gt; on OPatch &lt;code&gt;12.2.0.1.37+&lt;/code&gt; for stacked homes) with &lt;code&gt;opatch lsinventory -xml&lt;/code&gt; to keep artifacts lean and auditable.&lt;/li&gt;
&lt;li&gt;On target hosts, unpack out-of-place, serialize &lt;code&gt;oui/bin/runInstaller -silent -attachHome&lt;/code&gt; with &lt;code&gt;flock&lt;/code&gt;, and always run &lt;code&gt;root.sh&lt;/code&gt; before cutover.&lt;/li&gt;
&lt;li&gt;Keep &lt;code&gt;OLD_HOME&lt;/code&gt; untouched on disk during the maintenance window so binary and SQL rollback (&lt;code&gt;srvctl modify database&lt;/code&gt; + &lt;code&gt;datapatch -rollback -verbose&lt;/code&gt;) remains deterministic.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Target-side &lt;code&gt;opatch apply&lt;/code&gt; and binary relinking introduce &lt;code&gt;glibc&lt;/code&gt; drift, bloat &lt;code&gt;.patch_storage&lt;/code&gt;, and turn failed patch runs into unstartable production homes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build an ephemeral CI Gold Image Factory per OS major version using &lt;code&gt;runInstaller -createGoldImage&lt;/code&gt;, sign the archive with SHA-256 and an XML SBOM, and deploy out-of-place via &lt;code&gt;oui/bin/runInstaller -attachHome&lt;/code&gt; and &lt;code&gt;root.sh&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Validate the pipeline in staging by extracting the Gold Image onto a clean host, verifying &lt;code&gt;opatch lsinventory&lt;/code&gt; and &lt;code&gt;extjob&lt;/code&gt; SetUID permissions, switching with &lt;code&gt;srvctl modify database&lt;/code&gt;, and testing a round-trip rollback to &lt;code&gt;OLD_HOME&lt;/code&gt; with &lt;code&gt;datapatch -rollback -verbose&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your current patching playbooks this week; wherever automation invokes &lt;code&gt;opatch apply&lt;/code&gt; on database hosts, replace it with an out-of-place Gold Image staging job in non-production first.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>Building a Highly Available Witness Tier: Distributed Observers and Quorum at Scale</title><link>https://rajivonai.com/blog/2023-07-15-oracle-fsfo-observer-design-failover/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-07-15-oracle-fsfo-observer-design-failover/</guid><description>Why deploying a single FSFO Observer creates a fatal single point of failure in automated database disaster recovery, and how to engineer a resilient, containerized multi-observer quorum tier across distributed cloud regions.</description><pubDate>Thu, 14 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;An automated database failover mechanism is only as dependable as its witness tier; if the observer process lacks independent failure domains and self-healing supervision, automated disaster recovery becomes an operational liability.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Problem: Running a single Oracle Data Guard Observer on a shared bastion host or inside the primary database region creates a silent single point of failure that blocks automated failover (&lt;code&gt;ORA-16819&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Answer: Package the full Oracle Database Client (&lt;code&gt;dgmgrl&lt;/code&gt;) in a non-root container and run Master and Backup observers as a multi-AZ Kubernetes &lt;code&gt;StatefulSet&lt;/code&gt; in an independent third region.&lt;/li&gt;
&lt;li&gt;Watch out: Instant Client does not ship &lt;code&gt;dgmgrl&lt;/code&gt;, &lt;code&gt;FastStartFailoverThreshold&lt;/code&gt; has a 6-second floor (30-second default), and &lt;code&gt;ReadWriteOnce&lt;/code&gt; persistent volumes can lock &lt;code&gt;fsfo.dat&lt;/code&gt; during pod rescheduling.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2023-07-15-oracle-fsfo-observer-design-failover.svg&quot; alt=&quot;Three-site Oracle Data Guard Fast-Start Failover topology with independent witness tier&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Oracle Data Guard provides disaster recovery by shipping redo continuously from a Primary database to one or more Physical Standby databases. When platforms enable Fast-Start Failover (FSFO) in &lt;code&gt;MaxAvailability&lt;/code&gt; or &lt;code&gt;MaxPerformance&lt;/code&gt; mode, the Data Guard Broker relies on an external daemon—the &lt;strong&gt;Data Guard Observer&lt;/strong&gt;—to monitor primary health and trigger automatic promotion without waiting for an on-call engineer.&lt;/p&gt;
&lt;p&gt;To prevent split-brain writes where both Primary and Standby accept transactions simultaneously, Data Guard Broker requires mutual agreement between the &lt;strong&gt;Master Observer&lt;/strong&gt; and the &lt;strong&gt;Standby database&lt;/strong&gt;. A failover proceeds only when the Master Observer loses contact with the Primary for longer than &lt;code&gt;FastStartFailoverThreshold&lt;/code&gt; and the Standby confirms it is synchronized and ready to assume the primary role.&lt;/p&gt;
&lt;p&gt;In many environments, the observer starts as an ad-hoc &lt;code&gt;dgmgrl&lt;/code&gt; command inside a &lt;code&gt;screen&lt;/code&gt; or &lt;code&gt;tmux&lt;/code&gt; session on a shared administration host. That pattern works during initial testing, but it leaves automated recovery dependent on an unmanaged process running on a single virtual machine.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Relying on a single unmanaged observer process introduces three operational constraints that break automated failover when a real outage hits.&lt;/p&gt;
&lt;p&gt;First, &lt;strong&gt;silent single-host failure (&lt;code&gt;ORA-16819&lt;/code&gt;)&lt;/strong&gt;. When an observer runs as an unsupervised process on one bastion VM, a kernel panic, OOM kill, or routine OS patch terminates &lt;code&gt;dgmgrl&lt;/code&gt;. Because Data Guard continues shipping redo normally without an observer, application traffic does not notice the loss. The Broker raises &lt;code&gt;ORA-16819: fast-start failover observer not started&lt;/code&gt; in &lt;code&gt;SHOW CONFIGURATION&lt;/code&gt;, and if the Primary subsequently crashes, the Standby cannot promote itself automatically. Recovery stalls until an operator diagnoses the topology, confirms the Primary is genuinely down rather than partitioned, and runs a manual failover.&lt;/p&gt;
&lt;p&gt;Second, &lt;strong&gt;correlated regional failure&lt;/strong&gt;. Placing the observer in the same VPC, availability domain, or data center as the Primary or Standby violates three-site isolation. If a power or network outage cuts off the Primary site while the observer sits in that same site, both the Primary and the observer vanish together. The remote Standby sees no witness and refuses to fail over. Even if the Primary host itself is healthy, a shared top-of-rack switch or subnet ACL change can sever both the Primary and a colocated observer from the Standby at the exact same moment.&lt;/p&gt;
&lt;p&gt;Third, &lt;strong&gt;threshold tuning versus WAN jitter and half-open sockets&lt;/strong&gt;. Oracle Data Guard Broker enforces a minimum &lt;code&gt;FastStartFailoverThreshold&lt;/code&gt; of &lt;strong&gt;6 seconds&lt;/strong&gt; and a default of &lt;strong&gt;30 seconds&lt;/strong&gt;. Setting the threshold near the 6-second floor across a cross-region WAN exposes the database to false-positive failovers during transient packet loss or BGP reconvergence. Conversely, if TCP connections between the observer and Primary hang in a half-open state during a silent hardware freeze, detection can stall unless Oracle Net &lt;code&gt;SQLNET.RECV_TIMEOUT&lt;/code&gt; and OS TCP keepalives are tuned below &lt;code&gt;FastStartFailoverThreshold&lt;/code&gt;. In &lt;code&gt;MaxPerformance&lt;/code&gt; mode, &lt;code&gt;FastStartFailoverLagLimit&lt;/code&gt; (minimum &lt;strong&gt;5 seconds&lt;/strong&gt;, default &lt;strong&gt;30 seconds&lt;/strong&gt;; ignored and defaulted to &lt;code&gt;0&lt;/code&gt; in &lt;code&gt;MaxAvailability&lt;/code&gt;) adds a second constraint: if standby apply lag exceeds the configured bound when the Primary fails, the observer blocks failover rather than violating the RPO target.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-07-15-oracle-fsfo-observer-design-failover-2-constraints.svg&quot; alt=&quot;Three constraints governing Oracle Fast-Start Failover availability&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How do you design and orchestrate a witness tier that survives host and availability-zone failures, stays isolated from both database sites, and avoids false-positive failovers?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams typically evaluate four ways to host the Data Guard Observer before settling on a multi-region witness architecture.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Gets Right&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Decision&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Bastion &lt;code&gt;screen&lt;/code&gt; / &lt;code&gt;tmux&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero extra infrastructure; quick to launch during setup.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Dies silently on host reboot; no health checks, log shipping, or backup witness.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Single VM + &lt;code&gt;systemd&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Restarts &lt;code&gt;dgmgrl&lt;/code&gt; automatically if the local process crashes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Single VM remains a single point of failure during hypervisor or OS patching.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Observer on Standby host&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Survives a Primary site blackout without a third site.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Breaks three-site quorum; a Standby-only partition cannot distinguish Primary death from a link cut.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Rejected&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Multi-AZ Kubernetes &lt;code&gt;StatefulSet&lt;/code&gt; in Site 3&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Independent failure domain, automated pod recovery, Master + Backup handoff, mTLS wallets.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires a third region/cluster and cross-region port 1521/2484 connectivity.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Chosen&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Running &lt;code&gt;systemd&lt;/code&gt; on a single VM fixes process crashes but does not survive host maintenance, kernel upgrades, or availability-zone loss; while the VM is rebooting, the configuration sits unobserved with &lt;code&gt;ORA-16819&lt;/code&gt;. Hosting the observer on the Standby server removes the need for a third site, but it collapses the witness into the Standby’s failure domain: if Site 2 loses WAN connectivity to Site 1, the Standby and colocated observer both lose sight of the Primary at once, creating the exact split-brain risk three-site quorum is meant to prevent. Only a dedicated third-site Kubernetes cluster running multiple observers across separate availability zones eliminates single-host and single-site blind spots without storing any database data in the witness tier.&lt;/p&gt;
&lt;p&gt;Evaluating the network cost of the chosen architecture is equally straightforward. An observer exchanges lightweight Broker heartbeat messages over Oracle Net (port 1521 or 2484 for TCPS/mTLS) with the Primary and Standby &lt;code&gt;DMON&lt;/code&gt; processes; it never receives redo streams or table blocks. Cross-region bandwidth consumption is negligible compared to redo transport, which means placing the witness tier in a third cloud region adds minimal network spend while buying true regional fault independence.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-07-15-oracle-fsfo-observer-design-failover-3-options.svg&quot; alt=&quot;Comparison of four observer deployment options showing why Multi-AZ Kubernetes in Site 3 is chosen&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-distributed-kubernetes-observer-quorum-architecture&quot;&gt;The Distributed Kubernetes Observer Quorum Architecture&lt;/h2&gt;
&lt;p&gt;Beginning with Oracle Database 12c Release 2 and continuing through 19c and 23ai, Data Guard Broker supports &lt;strong&gt;Multiple Observers&lt;/strong&gt;. In Oracle 19c, a Broker configuration supports up to &lt;strong&gt;three&lt;/strong&gt; registered observers: one &lt;strong&gt;Master Observer&lt;/strong&gt; and up to &lt;strong&gt;two Backup Observers&lt;/strong&gt;. In Oracle 23ai, that limit increases to &lt;strong&gt;four&lt;/strong&gt; observers (one Master and up to three Backups).&lt;/p&gt;
&lt;p&gt;Multiple observers do not vote as a multi-observer consensus quorum. Only the single &lt;strong&gt;Master Observer&lt;/strong&gt; actively monitors the Primary and coordinates failover with the Standby. Backup observers stay connected in hot standby; if the Master Observer disconnects or its host fails, the Data Guard Broker (&lt;code&gt;DMON&lt;/code&gt;) automatically promotes a healthy Backup Observer to Master. Administrators can control preference order using &lt;code&gt;PreferredObserverHosts&lt;/code&gt; or force an immediate role change during planned cluster maintenance with &lt;code&gt;SET MASTEROBSERVER TO &amp;#x3C;observer_name&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Using a Kubernetes &lt;code&gt;StatefulSet&lt;/code&gt; gives each observer pod a deterministic identity (&lt;code&gt;billing-db-observer-0&lt;/code&gt;, &lt;code&gt;billing-db-observer-1&lt;/code&gt;) via &lt;code&gt;metadata.name&lt;/code&gt;. That deterministic naming keeps &lt;code&gt;SHOW OBSERVER ALL&lt;/code&gt; and &lt;code&gt;PreferredObserverHosts&lt;/code&gt; stable across pod restarts and image upgrades, unlike a &lt;code&gt;Deployment&lt;/code&gt; whose random pod suffixes churn every time a replica is replaced.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-07-15-oracle-fsfo-observer-design-failover-4-architecture.svg&quot; alt=&quot;Layered Kubernetes witness architecture in Site 3 with Master and Backup observers&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;partition-behavior-across-three-sites&quot;&gt;Partition Behavior Across Three Sites&lt;/h3&gt;
&lt;p&gt;Separating Primary (Site 1), Standby (Site 2), and the Witness Tier (Site 3) produces deterministic outcomes under every single-site or link partition:&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Partition Scenario&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mutual Visibility&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Broker Decision&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Operational Result&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Primary isolated (Site 1 outage)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Standby &amp;#x3C;—&gt; Master Observer&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Master + Standby agree Primary is unreachable past &lt;code&gt;FastStartFailoverThreshold&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Standby promotes to Primary automatically.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Standby isolated (Site 2 outage)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary &amp;#x3C;—&gt; Master Observer&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary remains reachable by Master Observer.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary stays read/write; FSFO suspends until Standby resyncs.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Witness isolated (Site 3 WAN cut)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary &amp;#x3C;—&gt; Standby healthy&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary loses observer heartbeats and logs &lt;code&gt;ORA-16819&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary stays read/write; automatic failover disabled safely.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Inter-site cut (Site 1 &amp;#x3C;—&gt; Site 2)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Observer reaches both sites&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Master Observer sees Primary is alive.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;No failover; prevents split-brain writes.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;1-container-image-using-full-oracle-database-client&quot;&gt;1. Container Image Using Full Oracle Database Client&lt;/h3&gt;
&lt;p&gt;A common build mistake is packaging Oracle Instant Client. Standard Instant Client packages (&lt;code&gt;instantclient-basic&lt;/code&gt; and &lt;code&gt;instantclient-tools&lt;/code&gt;) do &lt;strong&gt;not&lt;/strong&gt; include the &lt;code&gt;dgmgrl&lt;/code&gt; binary. The witness container must install the full Oracle Database Client (Administrator or Runtime installation) so &lt;code&gt;dgmgrl&lt;/code&gt; and Oracle Wallet tools (&lt;code&gt;mkstore&lt;/code&gt; / &lt;code&gt;orapki&lt;/code&gt;) are present. Run the container as an unprivileged user and mount Oracle Wallets read-only from Kubernetes Secrets.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;dockerfile&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oraclelinux:8-slim&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;RUN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; microdnf install -y oracle-database-preinstall-19c unzip &amp;#x26;&amp;#x26; microdnf clean all&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Install Oracle Database 19c Administrator Client silently into /opt/oracle/client&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# (Standard Instant Client does not ship the dgmgrl binary)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ENV&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ORACLE_HOME=/opt/oracle/client \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PATH=/opt/oracle/client/bin:$PATH \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    TNS_ADMIN=/etc/oracle/wallets&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;USER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oracle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;COPY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; --chown=oracle:oinstall entrypoint.sh /home/oracle/entrypoint.sh&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;RUN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; chmod +x /home/oracle/entrypoint.sh&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ENTRYPOINT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/home/oracle/entrypoint.sh&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-observer-lifecycle-wrapper&quot;&gt;2. Observer Lifecycle Wrapper&lt;/h3&gt;
&lt;p&gt;The entrypoint script uses &lt;code&gt;exec&lt;/code&gt; so &lt;code&gt;dgmgrl&lt;/code&gt; receives container signals directly, writes its runtime state file (&lt;code&gt;fsfo.dat&lt;/code&gt;) to local ephemeral storage, and streams diagnostic logs to &lt;code&gt;/dev/stdout&lt;/code&gt; for cluster log aggregation.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/bin/bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -euo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pipefail&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OBSERVER_NAME&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OBSERVER_NAME&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:-&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;hostname&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;CONNECT_ALIAS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DATABASE_TNS_ALIAS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;BILLING_BROKER&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;STATE_DIR&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/var/run/fsfo&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mkdir&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -p&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;STATE_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;cleanup&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    dgmgrl&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -silent&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /@&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;CONNECT_ALIAS&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;STOP OBSERVER ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OBSERVER_NAME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;};&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    exit&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trap&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; cleanup&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; SIGTERM&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; SIGINT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;exec&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; dgmgrl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /@&quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;CONNECT_ALIAS&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;START OBSERVER &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OBSERVER_NAME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot; FILE IS &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;STATE_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/fsfo_${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;OBSERVER_NAME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}.dat&quot; LOGFILE IS &quot;/dev/stdout&quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-multi-az-kubernetes-statefulset&quot;&gt;3. Multi-AZ Kubernetes StatefulSet&lt;/h3&gt;
&lt;p&gt;Deploy two or three replicas in Site 3 with &lt;code&gt;podAntiAffinity&lt;/code&gt; on &lt;code&gt;topology.kubernetes.io/zone&lt;/code&gt; so the Master and Backup observers never land on the same availability zone or worker node. Store &lt;code&gt;fsfo.dat&lt;/code&gt; in an &lt;code&gt;emptyDir&lt;/code&gt; volume rather than a &lt;code&gt;ReadWriteOnce&lt;/code&gt; cloud block volume (such as EBS); if a pod reschedules after a node crash, a slow cloud volume detach leaves the new pod stuck waiting to acquire a lock on &lt;code&gt;fsfo.dat&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;apiVersion&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;apps/v1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;kind&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;StatefulSet&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;metadata&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;billing-db-observer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  namespace&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;database-observers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;spec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  serviceName&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;billing-observer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  replicas&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  selector&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    matchLabels&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      app.kubernetes.io/name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;fsfo-observer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  template&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    metadata&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      labels&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        app.kubernetes.io/name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;fsfo-observer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    spec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      affinity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        podAntiAffinity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          requiredDuringSchedulingIgnoredDuringExecution&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;labelSelector&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;                matchLabels&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;                  app.kubernetes.io/name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;fsfo-observer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;              topologyKey&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;topology.kubernetes.io/zone&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      containers&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;observer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          image&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;internal-registry.example.com/database/fsfo-observer:19.23&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          env&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;DATABASE_TNS_ALIAS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;              value&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;BILLING_BROKER&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;OBSERVER_NAME&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;              valueFrom&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;                fieldRef&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;                  fieldPath&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;metadata.name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          volumeMounts&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;tns-config&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;              mountPath&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/etc/oracle/wallets&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;              readOnly&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;state-storage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;              mountPath&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/var/run/fsfo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          livenessProbe&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;            exec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;              command&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/bin/sh&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;-c&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pgrep -x dgmgrl &gt;/dev/null&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;            periodSeconds&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      volumes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;tns-config&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          secret&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;            secretName&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;billing-db-wallet-secret&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;            defaultMode&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0400&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;state-storage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          emptyDir&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;4-broker-configuration-in-dgmgrl&quot;&gt;4. Broker Configuration in DGMGRL&lt;/h3&gt;
&lt;p&gt;Configure &lt;code&gt;FastStartFailoverThreshold&lt;/code&gt; to match WAN stability (30 seconds is the default; 6 seconds is the hard floor). In &lt;code&gt;MaxAvailability&lt;/code&gt;, synchronous redo transport already enforces zero data loss, so &lt;code&gt;FastStartFailoverLagLimit&lt;/code&gt; is ignored and defaults to &lt;code&gt;0&lt;/code&gt;. Note that &lt;code&gt;Corrupted Controlfile&lt;/code&gt; and other core health conditions are enabled by default when FSFO is activated, and can be verified with &lt;code&gt;SHOW FAST_START FAILOVER&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CONNECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;@BILLING_PRIMARY &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SYSDG;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EDIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CONFIGURATION&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROPERTY FastStartFailoverThreshold &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 30&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- PreferredObserverHosts is a per-database property (set on each member that can hold the primary role)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EDIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; billing_primary &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROPERTY PreferredObserverHosts &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;billing-db-observer-0,billing-db-observer-1&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EDIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; billing_standby &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROPERTY PreferredObserverHosts &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;billing-db-observer-0,billing-db-observer-1&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ENABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; FAST_START &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FAILOVER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CONFIGURATION&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW OBSERVER ALL;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW FAST_START &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FAILOVER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Rolling out a distributed witness tier to an existing Data Guard pair happens in four gated stages so an observer misconfiguration cannot trigger an unexpected failover during deployment.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-07-15-oracle-fsfo-observer-design-failover-5-flow.svg&quot; alt=&quot;Four-stage rollout from image and wallet build to failover drill&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Stage 1 — Build Image and mTLS Wallet:&lt;/strong&gt; Package the full Oracle Database Client 19c image, provision an auto-rotating Oracle Wallet (mTLS) or &lt;code&gt;SYSDG&lt;/code&gt; wallet secret in Kubernetes, and verify &lt;code&gt;dgmgrl /@BILLING_BROKER&lt;/code&gt; connects from a test pod in Site 3 without interactive prompts. Validate &lt;code&gt;sqlnet.ora&lt;/code&gt; includes &lt;code&gt;SQLNET.RECV_TIMEOUT&lt;/code&gt; so stalled TCP connections fail fast rather than hanging past &lt;code&gt;FastStartFailoverThreshold&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stage 2 — Deploy Multi-AZ StatefulSet:&lt;/strong&gt; Apply the &lt;code&gt;StatefulSet&lt;/code&gt; in Site 3 with &lt;code&gt;replicas: 2&lt;/code&gt; (or &lt;code&gt;3&lt;/code&gt; in 19c, &lt;code&gt;4&lt;/code&gt; in 23ai) and pod anti-affinity. Confirm &lt;code&gt;SHOW OBSERVER ALL&lt;/code&gt; lists one Master Observer and the expected Backup Observers across distinct zones before enabling FSFO. Check &lt;code&gt;kubectl get pods -o wide&lt;/code&gt; to verify no two observer pods share a worker node or availability zone.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stage 3 — Enable FSFO with Conservative Thresholds:&lt;/strong&gt; Set &lt;code&gt;FastStartFailoverThreshold = 30&lt;/code&gt; (or higher for noisy WAN links) and run &lt;code&gt;ENABLE FAST_START FAILOVER&lt;/code&gt;. Gate on &lt;code&gt;SHOW CONFIGURATION&lt;/code&gt; returning &lt;code&gt;SUCCESS&lt;/code&gt; with zero &lt;code&gt;ORA-16819&lt;/code&gt; warnings, and confirm &lt;code&gt;SHOW FAST_START FAILOVER&lt;/code&gt; displays the expected protection mode, threshold, and health conditions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stage 4 — Run Handoff and Isolation Drills:&lt;/strong&gt; Terminate the Master Observer pod (&lt;code&gt;kubectl delete pod billing-db-observer-0&lt;/code&gt;) and verify the Broker promotes &lt;code&gt;billing-db-observer-1&lt;/code&gt; to Master without interrupting database writes. Next, test &lt;code&gt;SET MASTEROBSERVER TO billing-db-observer-0&lt;/code&gt; once the pod recovers to verify deterministic failback. Finally, schedule a controlled Primary isolation drill in staging. Rollback at any stage is &lt;code&gt;DISABLE FAST_START FAILOVER;&lt;/code&gt;, which immediately halts automatic promotion while leaving redo transport intact.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The behavior above follows directly from Oracle Data Guard Broker’s documented architecture in 19c and 23ai:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Master/Backup promotion without consensus voting:&lt;/strong&gt; Oracle Data Guard Broker registers up to 3 observers in 19c (1 Master, 2 Backup) and up to 4 observers in 23ai. Only the Master Observer drives failover; backups exist purely for observer high availability so a single witness crash does not leave the configuration unobserved.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Observer state isolation:&lt;/strong&gt; Because &lt;code&gt;fsfo.dat&lt;/code&gt; only tracks observer runtime metadata for the local &lt;code&gt;dgmgrl&lt;/code&gt; process, using pod-local &lt;code&gt;emptyDir&lt;/code&gt; storage avoids cloud block-volume attach races when Kubernetes reschedules a failed observer pod onto another node.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Protection mode coupling:&lt;/strong&gt; In &lt;code&gt;MaxAvailability&lt;/code&gt;, synchronous redo transport prevents applied lag under normal health, whereas in &lt;code&gt;MaxPerformance&lt;/code&gt;, &lt;code&gt;FastStartFailoverLagLimit&lt;/code&gt; (minimum 5 seconds, default 30 seconds) acts as an explicit RPO guardrail that blocks failover if the Standby falls too far behind. Querying &lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; (&lt;code&gt;apply lag&lt;/code&gt; and &lt;code&gt;transport lag&lt;/code&gt;) lets platform teams alert before standby lag crosses &lt;code&gt;FastStartFailoverLagLimit&lt;/code&gt; and silently disables automatic recovery.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Health-condition triggers beyond network loss:&lt;/strong&gt; When FSFO is enabled in Oracle 19c, core Primary health conditions such as &lt;code&gt;Corrupted Controlfile&lt;/code&gt; are enabled by default. Even if the Primary host and network remain reachable, the Master Observer can trigger failover when &lt;code&gt;DMON&lt;/code&gt; reports a fatal internal database fault, which makes isolating the witness tier from Primary storage and host failures essential.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Even a multi-AZ witness tier introduces operational failure modes across storage locking, asymmetric firewalls, credential rotation, and DNS caching.&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Condition&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Breaks How&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Detect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Witness colocated in Site 1&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Site 1 outage kills Primary and all observers simultaneously; Standby cannot fail over.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cloud region/VPC audit of observer nodes vs Primary DB.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce CI policy placing the witness cluster in an independent third region.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;FastStartFailoverThreshold&lt;/code&gt; set near 6s on WAN&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Transient cross-region packet loss triggers unnecessary failover and connection churn.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Frequent observer reconnect warnings in &lt;code&gt;drc*.log&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep &lt;code&gt;FastStartFailoverThreshold&lt;/code&gt; at 30s or higher over WAN links.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;ReadWriteOnce&lt;/code&gt; PVC for &lt;code&gt;fsfo.dat&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Node crash leaves volume attached; rescheduled pod enters &lt;code&gt;CrashLoopBackOff&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pod stuck in &lt;code&gt;ContainerCreating&lt;/code&gt; or &lt;code&gt;Multi-Attach&lt;/code&gt; events.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use ephemeral &lt;code&gt;emptyDir&lt;/code&gt; storage per observer pod.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Static &lt;code&gt;SYS&lt;/code&gt;/&lt;code&gt;SYSDG&lt;/code&gt; password expires&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Master and Backup observers fail authentication together; FSFO drops to unobserved.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;ORA-16819&lt;/code&gt; in Broker status and auth errors in stdout.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Authenticate observers via mTLS Oracle Wallets rotated via External Secrets.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Asymmetric firewall drop on port 1521&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Observer cannot reach Primary, attempts failover; Standby rejects because Primary transport is live.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;ORA-16819&lt;/code&gt; or repeated failed failover attempts in Broker log.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Monitor Broker status continuously and alert on any &lt;code&gt;ORA-16819&lt;/code&gt; warning.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Apply lag exceeds &lt;code&gt;FastStartFailoverLagLimit&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Primary crashes during batch load; observer blocks failover in &lt;code&gt;MaxPerformance&lt;/code&gt; to cap data loss.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;V$DATAGUARD_STATS&lt;/code&gt; apply lag approaching &lt;code&gt;FastStartFailoverLagLimit&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Alert on apply lag before it hits the limit; throttle unindexed batch loads.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2023-07-15-oracle-fsfo-observer-design-failover-6-failures.svg&quot; alt=&quot;Four failure paths that break automated observer failover&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;An Oracle Data Guard Observer running in &lt;code&gt;screen&lt;/code&gt; or on a single VM turns automated failover into a silent single point of failure (&lt;code&gt;ORA-16819&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Standard Oracle Instant Client does not include &lt;code&gt;dgmgrl&lt;/code&gt;; witness container images must package the full Oracle Database Client.&lt;/li&gt;
&lt;li&gt;Multiple observers (up to 3 in 19c, up to 4 in 23ai) provide Master/Backup redundancy, not multi-observer voting—only the Master Observer and Standby decide failover.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;emptyDir&lt;/code&gt; rather than &lt;code&gt;ReadWriteOnce&lt;/code&gt; persistent volumes for &lt;code&gt;fsfo.dat&lt;/code&gt; so Kubernetes pod rescheduling never stalls on cloud storage detach locks.&lt;/li&gt;
&lt;li&gt;Keep &lt;code&gt;FastStartFailoverThreshold&lt;/code&gt; at or above the 30-second default across WAN links (6 seconds is the hard floor) and align &lt;code&gt;FastStartFailoverLagLimit&lt;/code&gt; (minimum 5 seconds) when running &lt;code&gt;MaxPerformance&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Single-host or colocated Data Guard Observers silently drop out of protection (&lt;code&gt;ORA-16819&lt;/code&gt;) or fail alongside the Primary site during regional outages.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Containerize the full Oracle Database Client (&lt;code&gt;dgmgrl&lt;/code&gt;) with mTLS Oracle Wallets and deploy Master and Backup observers as a multi-AZ Kubernetes &lt;code&gt;StatefulSet&lt;/code&gt; in a third independent region.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run &lt;code&gt;SHOW OBSERVER ALL&lt;/code&gt; and &lt;code&gt;SHOW CONFIGURATION&lt;/code&gt; after killing the Master Observer pod; confirm a Backup Observer assumes the Master role and &lt;code&gt;ORA-16819&lt;/code&gt; does not persist.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit every Data Guard configuration this week with &lt;code&gt;SHOW OBSERVER ALL&lt;/code&gt; and relocate any single-host or Site-1-colocated observers into a dedicated multi-AZ witness cluster.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>Building an Internal DBaaS Broker: Abstracting Exadata, VMs, and Autonomous in OCI</title><link>https://rajivonai.com/blog/2023-05-25-oracle-oci-vm-exadata-autonomous-decision/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-05-25-oracle-oci-vm-exadata-autonomous-decision/</guid><description>Why exposing raw OCI database primitives leads to extreme over-provisioning and architectural mismatches, and how to build a declarative Kubernetes Crossplane broker to route workloads programmatically.</description><pubDate>Mon, 11 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Exposing raw cloud infrastructure primitives directly to application teams creates a structural mismatch between operational requirements, hardware capabilities, and database licensing models.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Self-service catalogs that let developers pick between OCI VM DB Systems, Exadata, and Autonomous Database produce defensive over-provisioning, I/O starvation on block volumes, and late-stage failures when legacy apps hit Autonomous OS/SYSDBA restrictions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Replace raw infrastructure menus with a declarative &lt;code&gt;DatabaseClaim&lt;/code&gt; CRD evaluated by a placement broker that routes workloads deterministically to Autonomous Database Serverless, consolidated Exadata PDBs with CDB Resource Manager isolation, or constrained-core VM DB Systems.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Bin-packing PDBs onto shared Exadata CDBs requires &lt;code&gt;CREATE_CDB_PLAN_DIRECTIVE&lt;/code&gt; (&lt;code&gt;shares&lt;/code&gt; and &lt;code&gt;utilization_limit&lt;/code&gt;) plus per-PDB &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt;, and online PDB relocation requires &lt;code&gt;ARCHIVELOG&lt;/code&gt;, Local Undo, and &lt;code&gt;CREATE PLUGGABLE DATABASE ... RELOCATE&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2023-05-25-oracle-oci-vm-exadata-autonomous-decision.svg&quot; alt=&quot;Four-stage DBaaS broker flow: DatabaseClaim intent, OPA policy gate, placement engine, and OCI target topology&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Enterprise platform teams migrating Oracle estates to Oracle Cloud Infrastructure (OCI) manage a wide spectrum of workloads: cloud-native microservices, legacy commercial off-the-shelf (COTS) monoliths with operating-system agents, and multi-terabyte analytical warehouses. OCI offers three primary database hosting primitives:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;OCI Base Database Service (VM DB Systems):&lt;/strong&gt; Virtual machines on flexible compute shapes backed by OCI Block Volumes or local NVMe storage, supporting single-instance and 2-node Real Application Clusters (RAC). Administrators retain full Linux OS shell and &lt;code&gt;SYSDBA&lt;/code&gt; access.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Exadata Database Service (ExaDB-D and ExaDB-C@C):&lt;/strong&gt; Engineered systems pairing scale-out database servers with intelligent storage cell servers over a 100 Gbps RoCE (RDMA over Converged Ethernet) fabric, enabling storage-tier SQL predicate offload (Smart Scan), Hybrid Columnar Compression (HCC), and Storage Indexes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Autonomous Database (Serverless and Dedicated):&lt;/strong&gt; Managed Oracle Database services (ATP for OLTP, ADW for analytics) that automate patching, backups, and tuning, while locking down the Linux OS, withholding &lt;code&gt;SYSDBA&lt;/code&gt; privileges, and requiring mutual TLS (mTLS) wallet or TLS authentication.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When platform teams expose these three tiers directly in Backstage, ServiceNow, or Terraform variable forms, application squads are forced to pick compute shapes, cluster topologies, and storage tiers during onboarding.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Application engineers understand query latency targets, retention windows, and deployment cadences, not Oracle Processor Core Factor licensing or storage-cell offload mechanics. Exposing raw OCI primitives creates four operational constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Defensive Over-Provisioning and License Floors:&lt;/strong&gt; Small transactional services routinely select Exadata because teams equate it with maximum resilience. Exadata requires dedicated infrastructure commitments and minimum OCPU/ECPU allocations, turning a small schema into an expensive, mostly idle footprint. Similarly, selecting unconstrained VM shapes without pinning &lt;code&gt;cpuCoreCount&lt;/code&gt; creates unnecessary Oracle Enterprise Edition license exposure under Bring Your Own License (BYOL): on x86 OCI shapes, each OCPU represents a physical core with two hardware threads (2 vCPUs), so provisioning an 8-OCPU VM when the application only needs 2 OCPUs of compute—but wants 64 GB of RAM—quadruples the required Enterprise Edition processor licenses unless you explicitly pin &lt;code&gt;cpuCoreCount&lt;/code&gt; on a flexible shape.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Architectural I/O Starvation:&lt;/strong&gt; Conversely, teams migrating multi-terabyte reporting databases or warehouses often pick VM DB Systems to minimize nominal compute cost. Without Exadata’s storage-cell Smart Scan predicate filtering, Hybrid Columnar Compression (HCC), and Storage Indexes, full-table scans pull raw, uncompressed blocks across virtualized block storage, saturating network-block IOPS and starving neighboring instances.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Privilege and OS Compatibility Lockouts:&lt;/strong&gt; Autonomous Database disables local filesystem access (&lt;code&gt;UTL_FILE&lt;/code&gt; to host paths), blocks custom OS daemons and shared C libraries, and withholds &lt;code&gt;SYSDBA&lt;/code&gt;. Client applications must also authenticate via mutual TLS (mTLS) wallets or configured TLS endpoints rather than unencrypted SQL*Net listeners. When legacy COTS applications are routed to Autonomous Database without checking those hard constraints, teams discover the incompatibility only during late-stage integration testing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Noisy-Neighbor Consolidation Risk:&lt;/strong&gt; Packing multiple Pluggable Databases (PDBs) onto a shared Exadata Container Database (CDB) amortizes hardware cost, but without CDB-level CPU shares, utilization caps, and per-PDB memory bounds, one runaway batch query can evict shared buffer cache and exhaust PGA across every colocated tenant. Even worse, because every PDB in a CDB shares a single Oracle binary home and &lt;code&gt;CDB$ROOT&lt;/code&gt;, applying a Quarterly Release Update (RU) restarts or drains connections for every colocated PDB on that CDB simultaneously.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2023-05-25-oracle-oci-vm-exadata-autonomous-decision-2-constraints.svg&quot; alt=&quot;Three ungoverned OCI provisioning failures: over-provisioning on Exadata, I/O starvation on VMs, and privilege lockout on Autonomous&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How can platform teams build an internal database broker that hides raw OCI deployment targets and translates declarative application constraints into cost-efficient, license-compliant, and isolated database topologies?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams standardizing OCI database provisioning typically weigh four approaches:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Gets Right&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Verdict&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Raw Catalog Menus&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Expose VM, Exadata, and ADB directly in forms&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Maximum flexibility for expert DBAs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Severe over-provisioning, license drift, and wrong-tier selections&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Standardize on Autonomous Only&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Force all workloads onto ADB Serverless&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero OS patching and automatic scaling (2 ECPU min, 3x burst)&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Blocks legacy COTS apps needing OS agents, &lt;code&gt;SYSDBA&lt;/code&gt;, or custom libs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Dedicated VM Per Service&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Provision isolated VM DB System for every app&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Strong OS/tenant isolation and simple chargeback&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Loses Exadata Smart Scan for analytics and wastes idle compute&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Declarative DBaaS Broker&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;DatabaseClaim&lt;/code&gt; CRD routed to ADB, Exadata PDB, or VM&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforces OS/size/Smart Scan rules and bin-packs Exadata CDBs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires building placement engine, DBRM templates, and drift sync&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chosen&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Standardizing on a single OCI service fails because no single tier covers both locked-down cloud-native microservices and OS-dependent COTS applications. Forcing every team onto Autonomous Database breaks legacy vendor packages that require local file paths, &lt;code&gt;SYSDBA&lt;/code&gt; installation scripts, or host monitoring agents. Conversely, giving every microservice a dedicated VM DB System trades away Exadata Smart Scan for large analytical tables and forces platform teams to patch and manage Linux OS images for workloads that do not need host access. A declarative broker preserves all three OCI primitives behind a single &lt;code&gt;DatabaseClaim&lt;/code&gt; contract while keeping hardware, consolidation, and licensing decisions in platform-owned code.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-05-25-oracle-oci-vm-exadata-autonomous-decision-3-options.svg&quot; alt=&quot;Comparison of three OCI database tiers across OS access, Smart Scan offload, and compute scaling&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-declarative-dbaas-broker-architecture&quot;&gt;The Declarative DBaaS Broker Architecture&lt;/h2&gt;
&lt;p&gt;The broker replaces infrastructure selection with a Kubernetes Custom Resource Definition (&lt;code&gt;DatabaseClaim&lt;/code&gt;), an OPA Gatekeeper admission layer, a Go placement engine, and a Crossplane / OCI provisioning layer backed by Oracle Database Resource Manager (DBRM) isolation.&lt;/p&gt;
&lt;h3 id=&quot;1-the-declarative-developer-claim&quot;&gt;1. The Declarative Developer Claim&lt;/h3&gt;
&lt;p&gt;Application teams submit a &lt;code&gt;DatabaseClaim&lt;/code&gt; specifying workload behavior, capacity, and hard OS/privilege requirements—never compute shapes or ASM diskgroups:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;apiVersion&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;database.platform.internal/v1alpha1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;kind&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;DatabaseClaim&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;metadata&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;payment-processing-service&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  namespace&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;billing-prod&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;spec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  workloadProfile&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;transactional&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;      # transactional, analytical, batch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  tier&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;production&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;                    # production, staging, development&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  capacity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    initialStorageGb&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;750&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    projectedAnnualGrowthGb&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;300&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    peakConcurrentSessions&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;120&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  performanceSla&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    maxQueryLatencyP99Ms&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;15&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    requiresSmartScan&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  operationalConstraints&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    requiresOsAccess&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    requiresSysdbaAccess&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    requiresCustomSharedLibraries&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    complianceFramework&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;pci-dss&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  highAvailability&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    rpoMinutes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-deterministic-placement-engine&quot;&gt;2. Deterministic Placement Engine&lt;/h3&gt;
&lt;p&gt;The placement engine evaluates hard compatibility gates first, then storage and Smart Scan thresholds, and defaults clean cloud-native OLTP workloads to Autonomous Database Serverless:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;package&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; placement&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;context&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;	&quot;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;fmt&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;	TargetAutonomousServerless&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;AutonomousDatabaseServerless&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;	TargetExadataConsolidated&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;ExadataDatabaseServiceConsolidated&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;	TargetBaseDbVm&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;             =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;BaseDatabaseServiceVM&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; DatabaseClaimSpec&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	WorkloadProfile      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	InitialStorageGb     &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	RequiresOsAccess     &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	RequiresSysdbaAccess &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	RequiresCustomLibs   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	RequiresSmartScan    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	RpoMinutes           &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; PlacementDecision&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	TargetTier          &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	TargetCdbOcId       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	OcpuOrEcpuBase      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	StorageAllocationGb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	RequiresDataGuard   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; EvaluatePlacement&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;ctx&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; context&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Context&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;claim&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; DatabaseClaimSpec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;fleet&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; FleetCapacityState&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) (&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;PlacementDecision&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;	// Gate 1: Hard OS, SYSDBA, or external shared-library dependencies require VM DB System&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; claim.RequiresOsAccess &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; claim.RequiresSysdbaAccess &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; claim.RequiresCustomLibs {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; PlacementDecision&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			TargetTier:          TargetBaseDbVm,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			OcpuOrEcpuBase:      &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;calculateVmOcpu&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(claim.InitialStorageGb),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			StorageAllocationGb: claim.InitialStorageGb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			RequiresDataGuard:   claim.RpoMinutes &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		}, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;	// Gate 2: Large analytical datasets or explicit Smart Scan need Exadata CDB consolidation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; claim.RequiresSmartScan &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; claim.WorkloadProfile &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;analytical&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; claim.InitialStorageGb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		cdb, err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fleet.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;FindBestFitExadataCdb&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(claim.InitialStorageGb)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;			return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; PlacementDecision&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{}, fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Errorf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;exadata capacity exhausted: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%w&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, err)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; PlacementDecision&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			TargetTier:          TargetExadataConsolidated,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			TargetCdbOcId:       cdb.OcId,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			OcpuOrEcpuBase:      &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			StorageAllocationGb: claim.InitialStorageGb,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			RequiresDataGuard:   claim.RpoMinutes &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		}, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;	// Gate 3: Standard cloud-native OLTP defaults to Autonomous Database Serverless (2 ECPU minimum)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; PlacementDecision&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		TargetTier:          TargetAutonomousServerless,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		OcpuOrEcpuBase:      &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		StorageAllocationGb: claim.InitialStorageGb,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		RequiresDataGuard:   claim.RpoMinutes &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-crossplane-synthesis--cdb-resource-manager-isolation&quot;&gt;3. Crossplane Synthesis &amp;#x26; CDB Resource Manager Isolation&lt;/h3&gt;
&lt;p&gt;For VM DB Systems, the broker compiles the decision into a Crossplane composition that hard-pins &lt;code&gt;cpuCoreCount&lt;/code&gt; to match approved BYOL entitlements while sizing flexible memory and OCI Block Volume IOPS independently. When &lt;code&gt;rpoMinutes: 0&lt;/code&gt; is requested, the composition provisions an OCI Data Guard association in Maximum Protection or Maximum Availability mode (&lt;code&gt;SYNC&lt;/code&gt; redo transport) across availability domains or regions, rather than relying on single-instance block-volume snapshots. Similarly, the composition selects &lt;code&gt;databaseEdition&lt;/code&gt; (&lt;code&gt;STANDARD_EDITION&lt;/code&gt; vs &lt;code&gt;ENTERPRISE_EDITION_HIGH_PERFORMANCE&lt;/code&gt;) based on whether the claim requires Data Guard, Partitioning, or Advanced Compression, preventing teams from defaulting every dev or staging VM to the highest-cost Enterprise Edition tier.&lt;/p&gt;
&lt;p&gt;For consolidated Exadata placements, the broker creates a Pluggable Database (PDB) inside an existing Exadata CDB and binds it to a CDB Resource Plan (&lt;code&gt;DBMS_RESOURCE_MANAGER.CREATE_CDB_PLAN_DIRECTIVE&lt;/code&gt;) so &lt;code&gt;shares&lt;/code&gt; and &lt;code&gt;utilization_limit&lt;/code&gt; prevent noisy neighbors:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PLUGGABLE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; billing_prod_pdb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  ADMIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; USER pdb_admin IDENTIFIED &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BY&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;RotateViaVaultSecret#1&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ROLES &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CONNECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;RESOURCE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  DEFAULT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; TABLESPACE users&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DATAFILE &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;+DATA&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SIZE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; 10G AUTOEXTEND &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NEXT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; 1G MAXSIZE 750G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  FILE_NAME_CONVERT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;+DATA/CDB1/PDBSEED/&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;+DATA/CDB1/BILLING_PROD_PDB/&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PLUGGABLE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DATABASE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; billing_prod_pdb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;OPEN&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; READ&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; WRITE;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  DBMS_RESOURCE_MANAGER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;CREATE_PENDING_AREA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  DBMS_RESOURCE_MANAGER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;CREATE_CDB_PLAN_DIRECTIVE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    plan               &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;ENTERPRISE_CONSOLIDATION_PLAN&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    pluggable_database &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;BILLING_PROD_PDB&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    shares             &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    utilization_limit  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 35&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  DBMS_RESOURCE_MANAGER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;VALIDATE_PENDING_AREA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  DBMS_RESOURCE_MANAGER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;SUBMIT_PENDING_AREA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note the distinction in Oracle Multitenant: &lt;code&gt;CREATE_CDB_PLAN_DIRECTIVE&lt;/code&gt; governs CPU and Exadata I/O distribution &lt;em&gt;between PDBs&lt;/em&gt; at the &lt;code&gt;CDB$ROOT&lt;/code&gt; level using &lt;code&gt;shares&lt;/code&gt; and &lt;code&gt;utilization_limit&lt;/code&gt; (1–100%), whereas &lt;code&gt;CREATE_PLAN_DIRECTIVE&lt;/code&gt; governs consumer groups &lt;em&gt;inside&lt;/em&gt; a single PDB. Pairing CDB directives with per-PDB &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt;, &lt;code&gt;SGA_TARGET&lt;/code&gt;, and &lt;code&gt;DB_CACHE_SIZE&lt;/code&gt; settings (configured inside each PDB via &lt;code&gt;ALTER SYSTEM SET ... SCOPE=SPFILE&lt;/code&gt; when &lt;code&gt;NONCDB_COMPATIBLE&lt;/code&gt; is false) prevents a single tenant from exhausting shared memory pools or evicting hot blocks belonging to neighboring PDBs. In addition, setting &lt;code&gt;MAXSIZE&lt;/code&gt; on the PDB’s default tablespace datafiles stops a runaway ingestion job from filling the shared &lt;code&gt;+DATA&lt;/code&gt; ASM diskgroup and stalling sibling tenants with &lt;code&gt;ORA-01653: unable to extend table&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-05-25-oracle-oci-vm-exadata-autonomous-decision-4-architecture.svg&quot; alt=&quot;Broker control and isolation architecture: DatabaseClaim and OPA feed Go placement engine into Crossplane or Exadata PDB with CDB Resource Manager&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Deploying an internal DBaaS broker across an existing OCI tenancy follows four gated phases:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 01 — Fleet Inventory and License Baseline:&lt;/strong&gt; Audit existing VM DB Systems, Exadata CDBs, and Autonomous instances for idle CPU, storage growth, and constrained-core BYOL boundaries. Define CDB density caps and anti-affinity pools so a single CDB Release Update (RU) restart never drains an entire business unit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 02 — Shadow Routing and OPA Policy Gate:&lt;/strong&gt; Deploy &lt;code&gt;DatabaseClaim&lt;/code&gt; and OPA Gatekeeper policies in dry-run mode alongside existing provisioning. Verify that claims requesting &lt;code&gt;requiresOsAccess: true&lt;/code&gt; never route to Autonomous, and that cost-center quotas block oversized storage requests before provisioning. &lt;em&gt;Rollback: fall back to existing manual Terraform templates.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 03 — Active Vending with DBRM Containment:&lt;/strong&gt; Enable automated provisioning for new workloads. Every Exadata PDB is created with a &lt;code&gt;CREATE_CDB_PLAN_DIRECTIVE&lt;/code&gt; entry, &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt;, and automated wallet/credential injection into Kubernetes Secrets. Post-provisioning checks query &lt;code&gt;DBA_CDB_RSRC_PLAN_DIRECTIVES&lt;/code&gt; and &lt;code&gt;V$PDBS&lt;/code&gt; to confirm the directive is active in the current &lt;code&gt;RESOURCE_MANAGER_PLAN&lt;/code&gt; before marking the &lt;code&gt;DatabaseClaim&lt;/code&gt; ready. &lt;em&gt;Rollback: pause Crossplane composition sync while leaving provisioned databases online.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 04 — Telemetry-Driven Online Rebalancing:&lt;/strong&gt; Monitor &lt;code&gt;V$SYSSTAT&lt;/code&gt;, &lt;code&gt;V$RSRC_CONSUMER_GROUP&lt;/code&gt;, and OCI Performance Hub metrics. When a microservice outgrows its tier or needs Smart Scan, trigger online PDB relocation to an Exadata CDB using &lt;code&gt;CREATE PLUGGABLE DATABASE &amp;#x3C;target&gt; FROM &amp;#x3C;source&gt;@&amp;#x3C;dblink&gt; RELOCATE AVAILABILITY MAX&lt;/code&gt; (requires &lt;code&gt;ARCHIVELOG&lt;/code&gt; mode and Local Undo on both CDBs), followed by &lt;code&gt;ALTER PLUGGABLE DATABASE &amp;#x3C;target&gt; OPEN&lt;/code&gt; during a low-&lt;code&gt;V$TRANSACTION&lt;/code&gt; window.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2023-05-25-oracle-oci-vm-exadata-autonomous-decision-5-flow.svg&quot; alt=&quot;Four-stage broker rollout from fleet inventory and shadow routing to active vending and online PDB relocation&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The constraints encoded in this broker reflect documented OCI and Oracle Database system behaviors rather than arbitrary heuristics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Autonomous Database Serverless ECPU Elasticity:&lt;/strong&gt; Per Oracle Autonomous Database documentation, ECPU-based Serverless instances start at a minimum of &lt;strong&gt;2 ECPUs&lt;/strong&gt; and, with compute auto-scaling enabled, automatically scale up to &lt;strong&gt;3x&lt;/strong&gt; the base ECPU allocation during workload spikes (for example, 2 ECPUs burst to 6 ECPUs, and 128 ECPUs burst to 384 ECPUs). That built-in elasticity makes Autonomous Serverless a natural default for variable-load microservices that do not require OS shell or &lt;code&gt;SYSDBA&lt;/code&gt; access.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle Database 23ai AI Vector Search &amp;#x26; Exadata AI Smart Scan:&lt;/strong&gt; Oracle Database 23ai adds native &lt;code&gt;VECTOR&lt;/code&gt; columns with &lt;strong&gt;HNSW&lt;/strong&gt; (in-memory, governed by &lt;code&gt;VECTOR_MEMORY_SIZE&lt;/code&gt;) and &lt;strong&gt;IVF&lt;/strong&gt; (Inverted File Flat / Neighbor Partition storage-backed) indexes. On Exadata System Software 24.1.0+, &lt;strong&gt;AI Smart Scan&lt;/strong&gt; pushes vector distance calculations and Top-K filtering down to Exadata Storage Servers using XRMEM and Smart Flash Cache, shrinking network traffic back to database nodes. Adding a &lt;code&gt;requiresVectorOffload&lt;/code&gt; flag to &lt;code&gt;DatabaseClaim&lt;/code&gt; lets the broker route heavy vector-search workloads onto Exadata or Autonomous 23ai automatically.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Online PDB Relocation Mechanics:&lt;/strong&gt; Oracle’s documented &lt;code&gt;CREATE PLUGGABLE DATABASE ... RELOCATE&lt;/code&gt; workflow copies data blocks, redo, and undo online while the source PDB remains open, then cuts over active sessions when &lt;code&gt;ALTER PLUGGABLE DATABASE ... OPEN&lt;/code&gt; executes on the target CDB—provided both CDBs run in &lt;code&gt;ARCHIVELOG&lt;/code&gt; mode with Local Undo enabled and matching endianness.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure Mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Failure Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Operational Consequence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Admission Quota Deception&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Team declares small &lt;code&gt;initialStorageGb&lt;/code&gt; then ingests terabytes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exhausts shared ASM diskgroup (&lt;code&gt;ORA-01653&lt;/code&gt;) across colocated PDBs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce tablespace &lt;code&gt;MAXSIZE&lt;/code&gt; and alert at 75% quota usage&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Exadata Noisy Neighbor&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Uncapped PDB consumes shared CPU, I/O, or buffer cache&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Degrades latency for all sibling PDBs in the same CDB&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce &lt;code&gt;CREATE_CDB_PLAN_DIRECTIVE&lt;/code&gt; (&lt;code&gt;shares&lt;/code&gt;, &lt;code&gt;utilization_limit&lt;/code&gt;) + &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Smart Scan Offload Bypass&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Queries use PL/SQL UDFs or unsupported predicates in &lt;code&gt;WHERE&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Storage cells fall back to host-level reads, saturating RoCE/CPU&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Audit SQL plans for cell offload efficiency; refactor predicates&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Autonomous mTLS Wallet Expiry&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unmonitored ADB wallet certificates expire in app pods&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Mass connection failures across microservices&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Automate OCI wallet rotation into Kubernetes Secrets via sidecar/controller&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Constrained-Core License Drift&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Admin resizes VM shape in OCI Console outside broker&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unlocks extra physical cores, creating Oracle BYOL audit exposure&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lock manual shape edits with OCI IAM policies and Cloud Guard&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Over-Consolidated CDB Patching&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Too many PDBs packed into one CDB&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Quarterly RU rolling restart drains every tenant at once&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cap PDB density per CDB and pair with Data Guard switchover&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;PDB Relocation Lock Timeout&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;CREATE PDB ... RELOCATE&lt;/code&gt; cutover runs during heavy writes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Long-running uncommitted &lt;code&gt;V$TRANSACTION&lt;/code&gt; stalls switchover&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Gate &lt;code&gt;ALTER PDB OPEN&lt;/code&gt; cutover on low active transaction counts&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;When validating &lt;code&gt;DatabaseClaim&lt;/code&gt; routing in pre-production, verify that every provisioned Autonomous Database Serverless instance enforces &lt;code&gt;ECPU&lt;/code&gt; minimum allocations and mTLS wallet rotation schedules, and confirm that Base Database VM Systems reject any Terraform plan diff attempting to unpin &lt;code&gt;cpuCoreCount&lt;/code&gt; outside approved license bounds.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-05-25-oracle-oci-vm-exadata-autonomous-decision-6-failures.svg&quot; alt=&quot;Four operational failures inside an automated DBaaS broker: license drift, noisy-neighbor starvation, wallet expiry, and CDB patch blast radius&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Hide OCI primitives behind constraints, not menus:&lt;/strong&gt; Developers know whether they need OS access, &lt;code&gt;SYSDBA&lt;/code&gt;, Smart Scan, or zero-RPO Data Guard; they should never pick Exadata shapes or ASM diskgroup names.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Enforce multi-tenant isolation at the CDB root:&lt;/strong&gt; Bin-packing PDBs onto Exadata requires &lt;code&gt;DBMS_RESOURCE_MANAGER.CREATE_CDB_PLAN_DIRECTIVE&lt;/code&gt; (&lt;code&gt;shares&lt;/code&gt; and &lt;code&gt;utilization_limit&lt;/code&gt;) paired with per-PDB &lt;code&gt;PGA_AGGREGATE_LIMIT&lt;/code&gt; and &lt;code&gt;DB_CACHE_SIZE&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Default clean OLTP to Autonomous Serverless:&lt;/strong&gt; With a 2-ECPU minimum and 3x automatic burst scaling, Autonomous Database Serverless eliminates OS patching and idle VM capacity for apps that do not need host access.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pin VM cores and guard console mutations:&lt;/strong&gt; On Base Database VM Systems, hard-pin &lt;code&gt;cpuCoreCount&lt;/code&gt; in Crossplane/Terraform and block manual OCI Console shape changes via IAM and Cloud Guard to prevent BYOL license drift.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;code&gt;CREATE PLUGGABLE DATABASE ... RELOCATE&lt;/code&gt; for tier graduation:&lt;/strong&gt; When a workload outgrows its tier, online PDB relocation moves it between CDBs with minimal cutover downtime once &lt;code&gt;ARCHIVELOG&lt;/code&gt; and Local Undo are verified.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Self-service catalogs that expose raw OCI VM, Exadata, and Autonomous primitives cause defensive over-provisioning, block-storage I/O starvation, and late-stage OS/SYSDBA incompatibilities.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Put a declarative &lt;code&gt;DatabaseClaim&lt;/code&gt; CRD and deterministic placement engine in front of OCI, routing workloads to Autonomous Serverless, DBRM-isolated Exadata PDBs, or constrained-core VMs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Inventory your OCI tenancy’s VM DB Systems and Exadata CDBs for idle OCPU allocations, unpinned &lt;code&gt;cpuCoreCount&lt;/code&gt; shapes, and CDBs missing &lt;code&gt;DBA_CDB_RSRC_PLAN_DIRECTIVES&lt;/code&gt; caps.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Define a &lt;code&gt;DatabaseClaim&lt;/code&gt; schema with explicit &lt;code&gt;requiresOsAccess&lt;/code&gt;, &lt;code&gt;requiresSysdbaAccess&lt;/code&gt;, and &lt;code&gt;requiresSmartScan&lt;/code&gt; booleans this week, and test its routing decisions against your last ten database provisioning tickets.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Oracle Cloud Infrastructure Documentation: &lt;em&gt;Base Database Service, Exadata Database Service, and Autonomous Database&lt;/em&gt; (&lt;code&gt;docs.oracle.com/en-us/iaas/Content/Database/Concepts/overview.htm&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Oracle Autonomous Database Documentation: &lt;em&gt;Use Auto Scaling&lt;/em&gt; (&lt;code&gt;docs.oracle.com/en/cloud/paas/autonomous-database/serverless/adbsb/autonomous-auto-scale.html&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Oracle Database Administrator’s Guide: &lt;em&gt;Using Oracle Resource Manager for PDBs (&lt;code&gt;CREATE_CDB_PLAN_DIRECTIVE&lt;/code&gt;)&lt;/em&gt; (&lt;code&gt;docs.oracle.com/en/database/oracle/oracle-database/19/admin/using-oracle-resource-manager-for-pdbs-with-sql-plus.html&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Oracle Multitenant Administrator’s Guide: &lt;em&gt;Relocating a PDB (&lt;code&gt;CREATE PLUGGABLE DATABASE ... RELOCATE&lt;/code&gt;)&lt;/em&gt; (&lt;code&gt;docs.oracle.com/en/database/oracle/oracle-database/19/multi/relocating-a-pdb.html&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Oracle Exadata Documentation: &lt;em&gt;AI Smart Scan &amp;#x26; Oracle Database 23ai AI Vector Search&lt;/em&gt; (&lt;code&gt;docs.oracle.com&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>Declarative Database Topologies: Orchestrating OCI DB Systems via Kubernetes Operators</title><link>https://rajivonai.com/blog/2023-04-18-provisioning-oracle-database-oci-terraform/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-04-18-provisioning-oracle-database-oci-terraform/</guid><description>Why running Terraform in monolithic CI/CD pipelines creates catastrophic database blast radiuses, and how building Kubernetes Operators provides continuous reconciliation for Oracle Cloud estates.</description><pubDate>Sat, 09 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Terraform is designed for infrastructure where resources can be destroyed and recreated on a diff; applying monolithic, push-based Terraform workspaces to stateful Oracle database fleets guarantees state-lock contention, out-of-band drift collisions, and catastrophic accidental replacements.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Monolithic Terraform pipelines hold exclusive state locks during 45–90 minute RAC or Exadata provisioning runs, mark immutable &lt;code&gt;oci_database_db_system&lt;/code&gt; fields (&lt;code&gt;db_name&lt;/code&gt;, &lt;code&gt;character_set&lt;/code&gt;) as &lt;code&gt;forces replacement&lt;/code&gt;, and silently revert emergency console scaling on the next unrelated PR.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Keep Terraform for static foundational networking (VCNs, subnets, NSGs, Exadata infrastructure), and hand day-2 database lifecycles to a Kubernetes Operator (OCI Service Operator for Kubernetes / &lt;code&gt;OraOperator&lt;/code&gt;) backed by admission webhooks, &lt;code&gt;WorkRequest&lt;/code&gt; polling, and deletion-protection policies.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Kubernetes Operators introduce their own failure modes—tight reconciliation loops can trigger OCI &lt;code&gt;429 TooManyRequests&lt;/code&gt; throttling, and naive &lt;code&gt;cpuCoreCount&lt;/code&gt; reconciliation will fight OCI Autonomous Auto-Scaling.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2023-04-18-provisioning-oracle-database-oci-terraform.svg&quot; alt=&quot;Four-step Kubernetes Operator control loop for OCI databases&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Enterprise platform teams running workloads on Oracle Cloud Infrastructure (OCI) manage heterogeneous database fleets: VM DB Systems, Bare Metal DB Systems, Exadata Cloud Service, and Autonomous Database instances. These databases anchor transactional ledgers, ERP systems, and customer-facing APIs.&lt;/p&gt;
&lt;p&gt;To standardize provisioning, platform teams typically adopt HashiCorp Terraform with the &lt;code&gt;oracle/oci&lt;/code&gt; provider. Shared HCL modules encapsulate Virtual Cloud Networks (VCNs), private database subnets, Network Security Groups (NSGs), backup policies, and OCI Vault keys. Application teams submit pull requests with variable overrides, and CI/CD runners execute &lt;code&gt;terraform plan&lt;/code&gt; and &lt;code&gt;terraform apply&lt;/code&gt; against a remote state file (&lt;code&gt;terraform.tfstate&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;At small scale—a few static databases managed by one infrastructure squad—this push-based workflow is predictable. As the estate grows across dozens of product teams making concurrent schema, compute, and network changes, the mismatch between Terraform’s point-in-time graph execution and long-lived, stateful database operations surfaces as pipeline queues and production risk.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Governing a large OCI database fleet through monolithic Terraform workspaces exposes four structural constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The &lt;code&gt;ForceNew&lt;/code&gt; Replacement Constraint:&lt;/strong&gt; In the &lt;code&gt;oracle/oci&lt;/code&gt; Terraform provider, core creation-time attributes nested inside &lt;code&gt;oci_database_db_system&lt;/code&gt; (&lt;code&gt;db_home.database.db_name&lt;/code&gt;, &lt;code&gt;character_set&lt;/code&gt;, &lt;code&gt;nchar_character_set&lt;/code&gt;, &lt;code&gt;availability_domain&lt;/code&gt;, and &lt;code&gt;shape&lt;/code&gt; transitions across incompatible families) cannot be modified in place via OCI’s update APIs. Terraform marks these attributes as &lt;code&gt;forces replacement&lt;/code&gt;. If a shared module refactor or variable typo alters one of those fields across multiple databases, &lt;code&gt;terraform plan&lt;/code&gt; schedules a destroy-and-recreate (&lt;code&gt;TerminateDbSystem&lt;/code&gt; followed by &lt;code&gt;LaunchDbSystem&lt;/code&gt;). Under review fatigue on a massive plan output, approving that apply permanently destroys live databases and forces emergency point-in-time recovery.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Exclusive State-Lock Contention:&lt;/strong&gt; Provisioning an Oracle RAC DB System or Exadata VM cluster on OCI routinely takes 45 to 90 minutes while OCI allocates block volumes, formats Grid Infrastructure ASM disk groups, and initializes database instances. Throughout the apply, Terraform holds an exclusive lock on the workspace’s &lt;code&gt;terraform.tfstate&lt;/code&gt; (when using a locking backend such as Terraform Cloud, OCI Resource Manager, or a lock-enabled backend). Every other team targeting that workspace is blocked with &lt;code&gt;Error acquiring the state lock&lt;/code&gt; until the storage operation finishes. Conversely, teams that store state in OCI Object Storage via its S3-compatible API without an external DynamoDB-compatible lock table lose state locking altogether—exposing concurrent applies to silent state corruption.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Out-of-Band Drift Collisions:&lt;/strong&gt; Production incidents force out-of-band console changes. During a traffic spike, an on-call engineer scales an Autonomous Database from 4 OCPUs to 16 OCPUs in the OCI Console to relieve CPU saturation. Later, another engineer merges an unrelated PR updating a cost-center tag in Git. Terraform reads the live state (&lt;code&gt;16&lt;/code&gt; OCPUs), compares it to Git (&lt;code&gt;4&lt;/code&gt; OCPUs), treats the emergency mitigation as configuration drift, and scales the database back down to &lt;code&gt;4&lt;/code&gt; OCPUs—re-triggering the outage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Absence of Continuous Self-Healing:&lt;/strong&gt; Terraform only runs when a CI/CD pipeline is triggered. If an engineer weakens an NSG ingress rule, disables automatic backups, or changes a Vault key binding directly in OCI, Terraform detects nothing until the next commit days or weeks later.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2023-04-18-provisioning-oracle-database-oci-terraform-2-constraints.svg&quot; alt=&quot;Constraints of monolithic Terraform state: ForceNew destruction, exclusive state locks, and console drift reverts&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How can database platform architects eliminate monolithic state-lock bottlenecks and accidental &lt;code&gt;ForceNew&lt;/code&gt; terminations while adding continuous, self-healing drift control for OCI databases?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams evaluating how to fix monolithic Terraform pipelines generally compare four architectures:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Gets Right&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Verdict&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Monolithic Terraform Workspace&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Single root module and shared &lt;code&gt;tfstate&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Centralized governance and module reuse&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;45–90 minute state locks and multi-DB &lt;code&gt;ForceNew&lt;/code&gt; blast radius&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Per-Database Split Workspaces&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;One state file per database instance&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Isolates state locks and plan diffs to one DB&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Remains point-in-time push execution; zero continuous drift healing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Partial&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;OCI Resource Manager Stacks&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Managed Terraform execution inside OCI&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Eliminates self-hosted runners and state setup&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Retains HCL &lt;code&gt;ForceNew&lt;/code&gt; semantics; drift detection is still manual&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Partial&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Terraform Network + K8s Operator&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Terraform owns VCN/subnets; OSOK/&lt;code&gt;OraOperator&lt;/code&gt; owns DBs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Per-DB &lt;code&gt;etcd&lt;/code&gt; keys, zero global locks, continuous 60s reconciliation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires running HA OKE control plane and tuning OCI API rate limits&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chosen&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Splitting Terraform into per-database workspaces solves the shared state-lock queue, and OCI Resource Manager removes runner maintenance and provides on-demand drift reports, but neither gives you a continuous control loop. Both remain vulnerable to out-of-band console drift reverting on unrelated commits and lack Kubernetes-native admission webhooks that reject immutable-field edits before they reach a plan phase. Separating responsibilities—Terraform for slow-changing VCNs, subnets, and Exadata racks, and a Kubernetes Operator for database lifecycles, start/stop schedules, and OCPU scaling—gives each tool the workload it was built for.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-04-18-provisioning-oracle-database-oci-terraform-3-options.svg&quot; alt=&quot;Comparison of four OCI provisioning options: Monolith Terraform, Per-DB Workspace, OCI Resource Manager, and Terraform Network plus Kubernetes Operator&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-kubernetes-operator-architecture&quot;&gt;The Kubernetes Operator Architecture&lt;/h2&gt;
&lt;p&gt;In the hybrid control-plane architecture, a dedicated, multi-AZ Oracle Container Engine for Kubernetes (OKE) management cluster runs the &lt;strong&gt;OCI Service Operator for Kubernetes (OSOK)&lt;/strong&gt; or the &lt;strong&gt;Oracle Database Operator for Kubernetes (&lt;code&gt;OraOperator&lt;/code&gt;)&lt;/strong&gt; under the &lt;code&gt;database.oracle.com&lt;/code&gt; API group. Application teams declare database specifications as Kubernetes Custom Resources (CRDs) synced via GitOps (such as ArgoCD), while the controller continuously reconciles desired state in &lt;code&gt;etcd&lt;/code&gt; against live OCI APIs.&lt;/p&gt;
&lt;h3 id=&quot;1-declarative-custom-resource-and-admission-guards&quot;&gt;1. Declarative Custom Resource and Admission Guards&lt;/h3&gt;
&lt;p&gt;Each database is represented as an independent Custom Resource in its owning namespace (&lt;code&gt;kubectl explain autonomousdatabase.spec&lt;/code&gt; confirms the exact schema for your installed operator release):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Illustrative resource shape. Oracle&apos;s OraOperator AutonomousDatabase CRD nests these&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# fields under spec.details and its apiVersion differs by release; check the CRD you install.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;apiVersion&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;database.oracle.com/v1beta1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;kind&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;AutonomousDatabase&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;metadata&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;billing-core-db&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  namespace&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;finance-platform&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  annotations&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    # Enforced by Kyverno/OPA admission policy to block accidental kubectl delete&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    database.oracle.com/termination-protection&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;spec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  compartmentId&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ocid1.compartment.oc1..aaaaaaaexample&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  displayName&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Billing Core Production&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  dbName&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;BILLING&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  cpuCoreCount&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  dataStorageSizeInTBs&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  dbWorkload&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;OLTP&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  isAutoScalingEnabled&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  licenseModel&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;BRING_YOUR_OWN_LICENSE&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  adminPassword&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    k8sSecret&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;billing-db-admin-secret&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Two mechanisms prevent the “15,000-line plan” disaster:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Admission Webhook Validation:&lt;/strong&gt; A Kubernetes &lt;code&gt;ValidatingWebhookConfiguration&lt;/code&gt; (or Kyverno/OPA policy) inspects every &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; on &lt;code&gt;AutonomousDatabase&lt;/code&gt; and &lt;code&gt;DbSystem&lt;/code&gt; resources. Any commit attempting to mutate immutable fields (&lt;code&gt;dbName&lt;/code&gt;, &lt;code&gt;characterSet&lt;/code&gt;, &lt;code&gt;compartmentId&lt;/code&gt;) is rejected synchronously at &lt;code&gt;kubectl apply&lt;/code&gt; / ArgoCD sync time—never queued for a destructive replacement.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Namespace and &lt;code&gt;etcd&lt;/code&gt; Isolation:&lt;/strong&gt; Because each database is its own key in &lt;code&gt;etcd&lt;/code&gt;, modifying &lt;code&gt;billing-core-db&lt;/code&gt; cannot touch databases in any other namespace, and &lt;code&gt;etcd&lt;/code&gt; serializes concurrent updates across hundreds of teams in milliseconds without a global state lock.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;./2023-04-18-provisioning-oracle-database-oci-terraform-4-architecture.svg&quot; alt=&quot;Layered architecture: GitOps and admission webhooks feed OKE controller and WorkRequest poller into OCI data plane&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;2-asynchronous-workrequest-reconciliation-loop&quot;&gt;2. Asynchronous &lt;code&gt;WorkRequest&lt;/code&gt; Reconciliation Loop&lt;/h3&gt;
&lt;p&gt;Because OCI database operations (provisioning, storage expansion, cloning) are asynchronous and take minutes to hours, a production controller pattern must never block a worker goroutine waiting for completion. Instead, the reconciler submits the OCI SDK call, records the returned &lt;code&gt;opc-work-request-id&lt;/code&gt; in &lt;code&gt;.status&lt;/code&gt;, and returns &lt;code&gt;ctrl.Result{RequeueAfter: 30 * time.Second}&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Illustrative controller-runtime reconciliation pattern for OCI AutonomousDatabase&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;r &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ADBReconciler&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Reconcile&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;ctx&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; context&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Context&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;req&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ctrl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Request&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) (&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ctrl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	adb &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;databasev1beta1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;AutonomousDatabase&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Get&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, req.NamespacedName, adb); err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ctrl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{}, client.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;IgnoreNotFound&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(err)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	ociResp, err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r.DatabaseClient.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;GetAutonomousDatabase&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;database&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;GetAutonomousDatabaseRequest&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		AutonomousDatabaseId: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;adb.Status.OCID,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;createOrBindAutonomousDatabase&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, adb)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	actual &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ociResp.AutonomousDatabase&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; actual.LifecycleState &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; database.AutonomousDatabaseLifecycleStateUpdating &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		actual.LifecycleState &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; database.AutonomousDatabaseLifecycleStateProvisioning {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ctrl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{RequeueAfter: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;30&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time.Second}, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;	// Only reconcile OCPU count when Autonomous Auto-Scaling is disabled&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;adb.Spec.IsAutoScalingEnabled &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;actual.CpuCoreCount &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; adb.Spec.CpuCoreCount {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		_, err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r.DatabaseClient.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;UpdateAutonomousDatabase&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;database&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;UpdateAutonomousDatabaseRequest&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			AutonomousDatabaseId: actual.Id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			UpdateAutonomousDatabaseDetails: &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;database&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;UpdateAutonomousDatabaseDetails&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;				CpuCoreCount: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;adb.Spec.CpuCoreCount,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;			},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;			return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ctrl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{}, err&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;		}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;		return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ctrl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{RequeueAfter: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;30&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time.Second}, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;	return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ctrl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{RequeueAfter: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;60&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time.Second}, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice the guard on &lt;code&gt;IsAutoScalingEnabled&lt;/code&gt;: if OCI Autonomous Auto-Scaling is active, OCI dynamically scales compute up to 3x the base OCPU count under load. A naive controller that compares &lt;code&gt;*actual.CpuCoreCount != adb.Spec.CpuCoreCount&lt;/code&gt; without checking auto-scaling status will fight OCI’s autoscaler every 60 seconds.&lt;/p&gt;
&lt;h3 id=&quot;3-finalizers-and-deletion-governance&quot;&gt;3. Finalizers and Deletion Governance&lt;/h3&gt;
&lt;p&gt;In Kubernetes, deleting a Custom Resource (&lt;code&gt;kubectl delete -f adb.yaml&lt;/code&gt; or &lt;code&gt;kubectl delete namespace&lt;/code&gt;) triggers controller cleanup via &lt;code&gt;metadata.finalizers&lt;/code&gt;. Without a policy gate, a finalizer will faithfully call OCI’s &lt;code&gt;DeleteAutonomousDatabase&lt;/code&gt; API. To prevent accidental data loss:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Admission-Level Delete Block:&lt;/strong&gt; Enforce a Kyverno or OPA Gatekeeper policy that denies &lt;code&gt;DELETE&lt;/code&gt; requests on any &lt;code&gt;AutonomousDatabase&lt;/code&gt; carrying &lt;code&gt;database.oracle.com/termination-protection: &quot;true&quot;&lt;/code&gt;. Admission policies intercept the API call before the Kubernetes API server sets &lt;code&gt;metadata.deletionTimestamp&lt;/code&gt;, so the controller never enters its finalizer deletion branch.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Orphan vs. Terminate Semantics:&lt;/strong&gt; Support explicit &lt;code&gt;unbind&lt;/code&gt; / orphan behavior so detaching a Kubernetes object from the cluster does not automatically terminate the underlying OCI database unless an authorized break-glass annotation is removed first. Even if a termination call reaches OCI, Autonomous Database retains terminated instances for a short recovery window, though DB Systems terminate immediately without a soft-delete grace period.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Migrating an existing Terraform-managed OCI database estate to a Kubernetes Operator requires four staged phases so live databases are never recreated during cutover:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Phase 01 — Freeze Database Terraform Modules:&lt;/strong&gt; Add &lt;code&gt;lifecycle { prevent_destroy = true }&lt;/code&gt; to every &lt;code&gt;oci_database_db_system&lt;/code&gt; and &lt;code&gt;oci_database_autonomous_database&lt;/code&gt; block in existing Terraform code. Keep VCNs, subnets, route tables, and NSGs in Terraform. &lt;em&gt;Gate: &lt;code&gt;terraform plan&lt;/code&gt; shows zero replacements or destroys across all workspaces.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 02 — Deploy HA OKE Control Plane and Policies:&lt;/strong&gt; Provision a dedicated, multi-AZ OKE management cluster running OSOK / &lt;code&gt;OraOperator&lt;/code&gt; with exponential backoff configured on the controller manager. Install Kyverno/OPA policies blocking immutable field edits and protected &lt;code&gt;DELETE&lt;/code&gt; operations. &lt;em&gt;Gate: dry-run webhook tests reject &lt;code&gt;dbName&lt;/code&gt; edits and protected deletes.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 03 — Bind Existing Databases by OCID:&lt;/strong&gt; Author CRDs referencing each live database’s existing &lt;code&gt;ocid1.autonomousdatabase...&lt;/code&gt; (binding/adopting the existing OCI resource rather than creating a new one), verify the operator populates &lt;code&gt;.status.lifecycleState = AVAILABLE&lt;/code&gt; and confirms zero pending &lt;code&gt;WorkRequest&lt;/code&gt; IDs, and then run &lt;code&gt;terraform state rm &amp;#x3C;resource_address&gt;&lt;/code&gt; to detach the database from Terraform state without calling &lt;code&gt;TerminateDbSystem&lt;/code&gt;. Always run a follow-up &lt;code&gt;terraform plan&lt;/code&gt; in the legacy workspace to confirm Terraform no longer tracks the detached database. &lt;em&gt;Rollback: re-import the OCID into Terraform via &lt;code&gt;terraform import&lt;/code&gt;.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase 04 — Enable GitOps Sync and Rate-Limit Monitoring:&lt;/strong&gt; Point ArgoCD at the CRD repository with &lt;code&gt;RequeueAfter&lt;/code&gt; tuned to 60–120 seconds, and alert on OCI &lt;code&gt;429 TooManyRequests&lt;/code&gt; API responses in controller metrics. &lt;em&gt;Rollback: pause ArgoCD auto-sync while keeping the database online in OCI.&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2023-04-18-provisioning-oracle-database-oci-terraform-5-flow.svg&quot; alt=&quot;Four-phase migration from Terraform state to Operator control without database downtime&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The architectural division between Terraform and Kubernetes Operators is reflected directly in Oracle’s own tooling and multi-cloud reference architectures:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;OCI Service Operator for Kubernetes (OSOK) &amp;#x26; &lt;code&gt;OraOperator&lt;/code&gt;:&lt;/strong&gt; Oracle maintains both &lt;code&gt;oracle/oci-service-operator&lt;/code&gt; (for OCI control-plane resources including &lt;code&gt;AutonomousDatabase&lt;/code&gt;) and &lt;code&gt;oracle/oracle-database-operator&lt;/code&gt; (for &lt;code&gt;AutonomousDatabase&lt;/code&gt;, containerized databases, Sharding, and Data Guard topologies across OKE, AKS, and EKS). Both follow the controller-runtime pattern: each resource is reconciled independently in &lt;code&gt;etcd&lt;/code&gt;, eliminating shared state locks, and long-running OCI operations are tracked asynchronously via OCI &lt;code&gt;WorkRequest&lt;/code&gt; polling rather than holding global locks. &lt;code&gt;OraOperator&lt;/code&gt; also automates wallet and mTLS secret synchronization directly into application namespaces, so rotating database connection wallets updates Kubernetes &lt;code&gt;Secret&lt;/code&gt; objects without requiring manual DBA distribution.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Terraform &lt;code&gt;oracle/oci&lt;/code&gt; &lt;code&gt;ForceNew&lt;/code&gt; Mechanics:&lt;/strong&gt; In the official &lt;code&gt;oracle/oci&lt;/code&gt; provider documentation for &lt;code&gt;oci_database_db_system&lt;/code&gt;, attributes under &lt;code&gt;db_home.database&lt;/code&gt; such as &lt;code&gt;db_name&lt;/code&gt; and &lt;code&gt;character_set&lt;/code&gt; are documented as creation-time parameters that cannot be updated in place—meaning any HCL change to those fields forces resource replacement. Pairing &lt;code&gt;prevent_destroy = true&lt;/code&gt; in Terraform with admission-webhook validation in Kubernetes is the standard defense against accidental replacement. Even when a team attempts to recover from a &lt;code&gt;ForceNew&lt;/code&gt; plan using &lt;code&gt;terraform state mv&lt;/code&gt; or targeted &lt;code&gt;-replace&lt;/code&gt;, the underlying HCL coupling between network, compute, and database blocks in a shared root module keeps review risk high.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multi-Cloud Split (Oracle Database@Azure and Database@AWS):&lt;/strong&gt; In Oracle’s multi-cloud deployments co-located inside Azure and AWS data centers, macro infrastructure—Exadata Infrastructure, Exadata VM Clusters, and ODB networks—is provisioned via Terraform (&lt;code&gt;azurerm&lt;/code&gt;/&lt;code&gt;azapi&lt;/code&gt;, &lt;code&gt;aws&lt;/code&gt;, and &lt;code&gt;oci&lt;/code&gt; providers), while &lt;code&gt;OraOperator&lt;/code&gt; runs inside AKS or EKS to manage Kubernetes-plane database lifecycles, wallet secrets, and application connectivity.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure Scenario&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Failure Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Operational Consequence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;OCI API &lt;code&gt;429&lt;/code&gt; Reconciliation Storm&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Tight error requeue loop across hundreds of CRDs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exhausts OCI tenancy API rate limits, blocking all cloud automation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set &lt;code&gt;RequeueAfter&lt;/code&gt; to 60–120s and enable exponential backoff with jitter&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Auto-Scaling OCPU Thrashing&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Controller enforces static &lt;code&gt;cpuCoreCount&lt;/code&gt; while &lt;code&gt;isAutoScalingEnabled&lt;/code&gt; is true&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Operator scales DB down while OCI scales up under load&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Skip &lt;code&gt;cpuCoreCount&lt;/code&gt; drift updates when auto-scaling is enabled&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Namespace Delete Triggers Termination&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;kubectl delete namespace&lt;/code&gt; runs controller finalizer without policy guard&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Operator calls OCI delete API against production database&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce Kyverno/OPA delete blocks on &lt;code&gt;termination-protection: &quot;true&quot;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Synchronous &lt;code&gt;WorkRequest&lt;/code&gt; Blocking&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Controller thread blocks waiting 60m for ASM/storage provisioning&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exhausts &lt;code&gt;MaxConcurrentReconciles&lt;/code&gt; worker pool; other DBs stall&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Poll &lt;code&gt;WorkRequest&lt;/code&gt; status asynchronously via &lt;code&gt;RequeueAfter&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;OKE Control Plane Outage&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Management cluster loses &lt;code&gt;etcd&lt;/code&gt; quorum or API availability&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Existing OCI DBs stay online, but all GitOps changes/scaling halt&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run Operator on dedicated multi-AZ OKE cluster; back up &lt;code&gt;etcd&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Immutable Field Reconcile Loop&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;User edits &lt;code&gt;dbName&lt;/code&gt; without admission webhook; OCI rejects update&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;CRD stuck in &lt;code&gt;Error&lt;/code&gt; state, firing continuous failed API calls&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reject immutable-field mutations synchronously in ValidatingWebhook&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Plaintext Secrets in Git&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Admin password committed in CRD YAML instead of Secret/Vault ref&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Credentials exposed in Git history and ArgoCD diffs&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use External Secrets Operator or OCI Vault references&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2023-04-18-provisioning-oracle-database-oci-terraform-6-failures.svg&quot; alt=&quot;Four operator failure modes: OCI 429 storms, auto-scaling thrashing, namespace delete, and WorkRequest blocking&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Use Terraform for networks, Operators for databases:&lt;/strong&gt; Terraform excels at static VCNs, subnets, and Exadata racks; Kubernetes Operators excel at per-database day-2 lifecycles without global &lt;code&gt;tfstate&lt;/code&gt; locks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Prevent &lt;code&gt;ForceNew&lt;/code&gt; replacement before it hits &lt;code&gt;apply&lt;/code&gt;:&lt;/strong&gt; &lt;code&gt;oci_database_db_system&lt;/code&gt; fields like &lt;code&gt;db_name&lt;/code&gt; and &lt;code&gt;character_set&lt;/code&gt; force full destruction on change; block those edits in admission webhooks and keep &lt;code&gt;prevent_destroy = true&lt;/code&gt; on legacy Terraform blocks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Poll OCI &lt;code&gt;WorkRequest&lt;/code&gt;s asynchronously:&lt;/strong&gt; Never block controller worker threads on 45–90 minute RAC or storage operations; return &lt;code&gt;RequeueAfter&lt;/code&gt; and check lifecycle state on the next tick.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Respect OCI Autonomous Auto-Scaling:&lt;/strong&gt; Reconcile &lt;code&gt;cpuCoreCount&lt;/code&gt; only when &lt;code&gt;isAutoScalingEnabled&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;, or the Operator will actively fight OCI’s elastic compute scaling.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Detach via &lt;code&gt;terraform state rm&lt;/code&gt;, never &lt;code&gt;destroy&lt;/code&gt;:&lt;/strong&gt; When migrating live OCI databases from Terraform to OSOK/&lt;code&gt;OraOperator&lt;/code&gt;, bind the existing OCID in the CRD first and remove the resource from Terraform state cleanly.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Monolithic Terraform workspaces lock out concurrent database deployments and expose live OCI databases to &lt;code&gt;ForceNew&lt;/code&gt; replacement and console drift reverts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Freeze database resources in Terraform with &lt;code&gt;prevent_destroy = true&lt;/code&gt;, keep networking in Terraform, and move database lifecycles to OSOK or &lt;code&gt;OraOperator&lt;/code&gt; behind admission webhooks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Audit your existing &lt;code&gt;oci_database_db_system&lt;/code&gt; and &lt;code&gt;oci_database_autonomous_database&lt;/code&gt; Terraform states for shared lock wait times and any &lt;code&gt;forces replacement&lt;/code&gt; attributes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Deploy OSOK or &lt;code&gt;OraOperator&lt;/code&gt; in a non-production OKE cluster this week, bind one non-prod &lt;code&gt;AutonomousDatabase&lt;/code&gt; by OCID, and verify that an out-of-band console tag or OCPU change reconciles without touching other namespaces.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Oracle Cloud Infrastructure Documentation: &lt;em&gt;OCI Service Operator for Kubernetes (OSOK)&lt;/em&gt; (&lt;code&gt;docs.oracle.com/en-us/iaas/Content/ContEng/Tasks/contengusingosok.htm&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Oracle Database Operator for Kubernetes (&lt;code&gt;oracle/oracle-database-operator&lt;/code&gt;): &lt;em&gt;AutonomousDatabase CRD &amp;#x26; Controller&lt;/em&gt; (&lt;code&gt;github.com/oracle/oracle-database-operator&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Terraform Registry (&lt;code&gt;oracle/oci&lt;/code&gt;): &lt;em&gt;&lt;code&gt;oci_database_db_system&lt;/code&gt; Resource Reference&lt;/em&gt; (&lt;code&gt;registry.terraform.io/providers/oracle/oci/latest/docs/resources/database_db_system&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Kubernetes Documentation: &lt;em&gt;The Operator Pattern &amp;#x26; Validating Admission Webhooks&lt;/em&gt; (&lt;code&gt;kubernetes.io/docs/concepts/extend-kubernetes/operator/&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>Autonomous Index Lifecycle Management: Safely Deploying Index Changes at Scale</title><link>https://rajivonai.com/blog/2023-02-10-oracle-index-engineering-clustering-factor-invisible/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-02-10-oracle-index-engineering-clustering-factor-invisible/</guid><description>Why deploying an index in production is a massive availability risk, how clustering factor dictates I/O efficiency, and how to build an automated pipeline using Invisible Indexes and SQL Performance Analyzer.</description><pubDate>Thu, 07 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Adding an index to a production database is not a benign performance optimization; it is a structural alteration of the Cost-Based Optimizer’s search space that can push unrelated, revenue-critical queries into catastrophic execution plan regressions.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; A &lt;code&gt;CREATE INDEX ... ONLINE&lt;/code&gt; command avoids DML locks during build, but exposes the new index globally to the Cost-Based Optimizer (CBO) the instant it completes—frequently flipping unrelated batch or reporting queries from multi-block scans into random single-block I/O storms.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Decouple physical index construction from optimizer visibility by building with &lt;code&gt;INVISIBLE ONLINE&lt;/code&gt;, validating the full production SQL Tuning Set via &lt;code&gt;DBMS_SQLPA&lt;/code&gt; dual trials (&lt;code&gt;OPTIMIZER_USE_INVISIBLE_INDEXES&lt;/code&gt; toggled via &lt;code&gt;ALTER SESSION&lt;/code&gt;), and promoting only after a session-level canary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Invisible indexes still enforce &lt;code&gt;UNIQUE&lt;/code&gt; constraints on DML, still consume redo/undo and buffer cache during writes, and require multi-day SQL Tuning Sets to avoid bind-peeking blind spots.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2023-02-10-oracle-index-engineering-clustering-factor-invisible.svg&quot; alt=&quot;Four-stage safe index pipeline: build invisible online, capture AWR SQL Tuning Set, run SPA dual trials, and canary before ALTER VISIBLE or DROP&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Enterprise teams running high-throughput relational workloads on Oracle Database 19c—across Exadata, OCI Database Cloud Service, or self-managed bare metal—routinely hit point-lookup bottlenecks on large transactional tables such as &lt;code&gt;ORDERS&lt;/code&gt;, &lt;code&gt;TRANSACTIONS&lt;/code&gt;, or &lt;code&gt;LEDGER_ENTRIES&lt;/code&gt;. Developers identify a slow search endpoint and request a secondary B-tree index on a selective column like &lt;code&gt;customer_email&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To avoid blocking application writes, database engineering teams execute an online index build during a maintenance window:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; idx_orders_customer_email&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders(customer_email) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ONLINE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;ONLINE&lt;/code&gt; keyword lets concurrent &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, and &lt;code&gt;DELETE&lt;/code&gt; transactions proceed by buffering changes in a temporary journal table (&lt;code&gt;SYS_JOURNAL_&amp;#x3C;object_id&gt;&lt;/code&gt;) while scanning the base table snapshot. The build finishes without locking the application, and the target lookup query drops from scanning the table to reading a few B-tree blocks.&lt;/p&gt;
&lt;p&gt;The failure arrives later under peak concurrency. Because the new index is immediately visible to every session in the instance, the Oracle Cost-Based Optimizer (CBO) re-evaluates execution plans for unrelated queries touching &lt;code&gt;ORDERS&lt;/code&gt;. A heavy batch job or reporting query that previously relied on a parallel Full Table Scan—using multi-block I/O or Exadata Smart Scan cell offloading—can suddenly switch to an Index Range Scan if the CBO underestimates row selectivity. Instead of streaming contiguous blocks, the batch job issues millions of single-block random reads (&lt;code&gt;db file sequential read&lt;/code&gt;), saturating storage IOPS queues and exhausting application connection pools.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Creating a secondary index alters both the physical storage engine and the optimizer’s cost model across four distinct constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The Clustering Factor Amplification Constraint:&lt;/strong&gt; The I/O cost of an Index Range Scan is governed less by B-tree height (&lt;code&gt;BLEVEL&lt;/code&gt;) than by &lt;code&gt;CLUSTERING_FACTOR&lt;/code&gt; in &lt;code&gt;DBA_INDEXES&lt;/code&gt;. During statistics gathering, Oracle walks the index leaf blocks in key order and counts how many times the &lt;code&gt;ROWID&lt;/code&gt;’s table block address changes from one index entry to the next. That transition count becomes &lt;code&gt;CLUSTERING_FACTOR&lt;/code&gt;.
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Near-perfect clustering (&lt;code&gt;CLUSTERING_FACTOR ≈ BLOCKS&lt;/code&gt;):&lt;/strong&gt; When table rows are physically ordered similarly to the index keys (such as a monotonically increasing &lt;code&gt;CREATED_AT&lt;/code&gt; or sequence-backed primary key), scanning 10,000 index entries touches a small set of adjacent table blocks. Buffer cache hit rates stay high and physical disk reads stay low.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pathological clustering (&lt;code&gt;CLUSTERING_FACTOR ≈ NUM_ROWS&lt;/code&gt;):&lt;/strong&gt; When rows are scattered across heap blocks relative to the index key—common with UUIDs, hashes, or &lt;code&gt;customer_email&lt;/code&gt; in an append-mostly &lt;code&gt;ORDERS&lt;/code&gt; table—two consecutive index entries almost always point to different table blocks. Scanning 10,000 index entries can force up to &lt;strong&gt;10,000 separate single-block reads&lt;/strong&gt; across the storage array. If bind peeking or stale histograms cause the CBO to underestimate selectivity, the resulting random I/O storm saturates storage latency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Controlling sensitivity with &lt;code&gt;TABLE_CACHED_BLOCKS&lt;/code&gt;:&lt;/strong&gt; Since Oracle 12c, &lt;code&gt;DBMS_STATS.SET_TABLE_PREFS&lt;/code&gt; supports &lt;code&gt;TABLE_CACHED_BLOCKS&lt;/code&gt; (default &lt;code&gt;1&lt;/code&gt;, up to &lt;code&gt;255&lt;/code&gt;), which lets &lt;code&gt;DBMS_STATS&lt;/code&gt; remember recently visited table blocks during the leaf-block walk so a few interleaved rows in an otherwise clustered table do not inflate &lt;code&gt;CLUSTERING_FACTOR&lt;/code&gt; toward &lt;code&gt;NUM_ROWS&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Global Optimizer Visibility Constraint:&lt;/strong&gt; In standard Oracle DDL, an index becomes globally visible to the CBO the millisecond compilation finishes. Every shared-pool cursor referencing that table is invalidated on its next hard parse or rolling invalidation window, exposing unrelated batch and analytical workloads to unvalidated plan flips.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Synchronous DML Write Tax:&lt;/strong&gt; An index is a live write obligation, not a passive read structure. Every &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt;, and indexed-column &lt;code&gt;UPDATE&lt;/code&gt; must synchronously update B-tree leaf blocks, generate redo and undo, and handle leaf block splits (sequential 90-10 splits versus expensive random 50-50 splits). Adding unvetted indexes to high-ingestion tables increases redo generation and introduces &lt;code&gt;enq: TX - index contention&lt;/code&gt; waits under concurrent inserts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Staging Environment Fidelity Constraint:&lt;/strong&gt; Testing index DDL in QA or staging does not predict production plan stability. Non-production databases rarely replicate production &lt;code&gt;CLUSTERING_FACTOR&lt;/code&gt;, data skew, block fragmentation, or system statistics (&lt;code&gt;AUX_STATS$&lt;/code&gt;), so a plan that looks safe in staging can still regress in production.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2023-02-10-oracle-index-engineering-clustering-factor-invisible-2-constraints.svg&quot; alt=&quot;Comparison of low clustering factor near table blocks versus high clustering factor near table rows&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How can platform architects decouple the physical creation and maintenance of an index from its global visibility to the optimizer, allowing automated validation against live production SQL before exposing application traffic?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Platform teams typically evaluate four patterns for shipping index changes to production:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Option&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;What It Gets Right&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Where It Breaks&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Verdict&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Direct &lt;code&gt;CREATE INDEX ONLINE&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Build index online and expose globally immediately&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero DML table lock during creation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Exposes every shared-pool query to immediate unvalidated CBO plan flips&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Staging / QA Replay Only&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Test index DDL in pre-prod before running in prod&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Zero production resource usage during testing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Staging lacks production &lt;code&gt;CLUSTERING_FACTOR&lt;/code&gt;, skew, and &lt;code&gt;AUX_STATS$&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;INVISIBLE&lt;/code&gt; + Manual Session Test&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Build &lt;code&gt;INVISIBLE&lt;/code&gt;, test target SQL with &lt;code&gt;ALTER SESSION&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Confirms target query speeds up safely&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Misses unrelated batch and reporting queries in the shared pool&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Partial&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;INVISIBLE&lt;/code&gt; + &lt;code&gt;DBMS_SQLPA&lt;/code&gt; Pipeline&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Build &lt;code&gt;INVISIBLE&lt;/code&gt;, run dual SPA trials on AWR STS&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Quantifies both target gain and fleet-wide regressions&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Requires Real Application Testing (RAT) license and DML overhead&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Chosen&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Direct online builds optimize for DDL availability while ignoring optimizer blast radius. Staging-only validation fails because optimizer cost arithmetic depends on physical table layout and production bind distributions that staging databases do not have. Testing an invisible index manually with &lt;code&gt;ALTER SESSION SET OPTIMIZER_USE_INVISIBLE_INDEXES = TRUE&lt;/code&gt; verifies the developer’s query, but tells you nothing about the hundreds of other SQL IDs sharing the same table. Combining &lt;code&gt;INVISIBLE ONLINE&lt;/code&gt; with Oracle’s SQL Performance Analyzer (&lt;code&gt;DBMS_SQLPA&lt;/code&gt;) closes that gap by testing the entire captured production workload before global visibility changes.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2023-02-10-oracle-index-engineering-clustering-factor-invisible-3-options.svg&quot; alt=&quot;Four ways to ship an index: direct online, staging only, invisible with manual session, and invisible with SPA&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-index-lifecycle-orchestration-pipeline&quot;&gt;The Index Lifecycle Orchestration Pipeline&lt;/h2&gt;
&lt;p&gt;The recommended architecture separates physical storage maintenance from optimizer visibility using two native Oracle capabilities: &lt;strong&gt;Invisible Indexes&lt;/strong&gt; and &lt;strong&gt;SQL Performance Analyzer (SPA)&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id=&quot;1-storage-plane-vs-optimizer-plane&quot;&gt;1. Storage Plane vs. Optimizer Plane&lt;/h3&gt;
&lt;p&gt;Building an index with both &lt;code&gt;INVISIBLE&lt;/code&gt; and &lt;code&gt;ONLINE&lt;/code&gt; instructs the storage engine to construct and maintain the B-tree in real time while hiding it from the CBO:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; idx_orders_customer_email&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders(customer_email)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TABLESPACE indx_data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;INVISIBLE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ONLINE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Storage Engine Behavior:&lt;/strong&gt; Oracle scans the table, builds the B-tree, applies concurrent changes buffered in the online journal table, and keeps leaf blocks synchronized with every subsequent &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, and &lt;code&gt;DELETE&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optimizer Behavior:&lt;/strong&gt; The initialization parameter &lt;code&gt;OPTIMIZER_USE_INVISIBLE_INDEXES&lt;/code&gt; defaults to &lt;code&gt;FALSE&lt;/code&gt;. Production sessions compile and execute queries as if the index does not exist, preserving existing execution plans in the shared pool.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;./2023-02-10-oracle-index-engineering-clustering-factor-invisible-4-architecture.svg&quot; alt=&quot;Two-plane architecture: storage engine maintains the B-tree synchronously while optimizer plane isolates visibility&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;2-sql-performance-analyzer-spa-dual-trial-validation&quot;&gt;2. SQL Performance Analyzer (SPA) Dual-Trial Validation&lt;/h3&gt;
&lt;p&gt;Once the invisible index is built and its statistics are gathered, the pipeline captures a SQL Tuning Set (STS) from Automatic Workload Repository (AWR) snapshots covering the target table’s active SQL statements. It then invokes &lt;code&gt;DBMS_SQLPA&lt;/code&gt; to compare execution metrics with &lt;code&gt;OPTIMIZER_USE_INVISIBLE_INDEXES&lt;/code&gt; toggled at the session level between trials:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DECLARE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    v_task_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VARCHAR2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    v_sts_name  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VARCHAR2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;64&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) :&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;STS_ORDERS_ACTIVE_WORKLOAD&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    v_task_name :&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; DBMS_SQLPA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;CREATE_ANALYSIS_TASK&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sqlset_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; v_sts_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        task_name   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;SPA_VALIDATE_ORDERS_INDEX&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    EXECUTE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IMMEDIATE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;ALTER SESSION SET OPTIMIZER_USE_INVISIBLE_INDEXES = FALSE&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    DBMS_SQLPA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;EXECUTE_ANALYSIS_TASK&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        task_name      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; v_task_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        execution_type &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;TEST EXECUTE&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        execution_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;TRIAL_1_BASELINE&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    EXECUTE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IMMEDIATE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;ALTER SESSION SET OPTIMIZER_USE_INVISIBLE_INDEXES = TRUE&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    DBMS_SQLPA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;EXECUTE_ANALYSIS_TASK&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        task_name      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; v_task_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        execution_type &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;TEST EXECUTE&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        execution_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;TRIAL_2_WITH_INDEX&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    EXECUTE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IMMEDIATE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;ALTER SESSION SET OPTIMIZER_USE_INVISIBLE_INDEXES = FALSE&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    DBMS_SQLPA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;EXECUTE_ANALYSIS_TASK&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        task_name        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; v_task_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        execution_type   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;COMPARE PERFORMANCE&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        execution_params &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; DBMS_ADVISOR&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;ARGLIST&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &apos;comparison_metric&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;BUFFER_GETS&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &apos;execution_name1&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;TRIAL_1_BASELINE&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &apos;execution_name2&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;TRIAL_2_WITH_INDEX&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that &lt;code&gt;OPTIMIZER_USE_INVISIBLE_INDEXES&lt;/code&gt; is a session/system parameter rather than a &lt;code&gt;DBMS_SQLPA.SET_ANALYSIS_TASK_PARAMETER&lt;/code&gt; option, so toggling it via &lt;code&gt;ALTER SESSION&lt;/code&gt; between &lt;code&gt;TEST EXECUTE&lt;/code&gt; runs ensures Trial 1 measures the baseline and Trial 2 evaluates plans that can see the new index. Comparing on &lt;code&gt;BUFFER_GETS&lt;/code&gt; (logical I/O) provides a deterministic signal that is far less noisy than wall-clock elapsed time on shared hosts.&lt;/p&gt;
&lt;h3 id=&quot;3-automated-gating-criteria&quot;&gt;3. Automated Gating Criteria&lt;/h3&gt;
&lt;p&gt;The pipeline parses &lt;code&gt;DBMS_SQLPA.REPORT_ANALYSIS_TASK&lt;/code&gt; and enforces two gates before promotion:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Target Gain Gate:&lt;/strong&gt; The motivating SQL ID must show a meaningful reduction in &lt;code&gt;BUFFER_GETS&lt;/code&gt; or CPU time. If the index yields negligible improvement, keeping it only adds DML write overhead, so the pipeline drops it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fleet Regression Gate:&lt;/strong&gt; No other SQL ID in the STS may exceed the configured regression threshold (commonly 5% on &lt;code&gt;BUFFER_GETS&lt;/code&gt; or &lt;code&gt;WORKLOAD_IMPACT_THRESHOLD&lt;/code&gt;). Any regression triggers an automated &lt;code&gt;DROP INDEX&lt;/code&gt; and returns the offending &lt;code&gt;SQL_ID&lt;/code&gt; and plan diff to the pull request.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;A safe rollout enforces four sequential gates, each with an explicit rollback action:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Gate 01 — Pre-Flight Safety Check:&lt;/strong&gt; Reject &lt;code&gt;UNIQUE&lt;/code&gt; index proposals from automated pipelines, because the storage engine enforces uniqueness even when an index is &lt;code&gt;INVISIBLE&lt;/code&gt;. Check table write intensity via &lt;code&gt;V$SEGMENT_STATISTICS&lt;/code&gt; and set &lt;code&gt;DDL_LOCK_TIMEOUT&lt;/code&gt; to avoid immediate &lt;code&gt;ORA-00054: resource busy&lt;/code&gt; failures during transient locks. &lt;em&gt;Rollback: abort before DDL.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate 02 — Invisible Online Build &amp;#x26; Physical Inspection:&lt;/strong&gt; Execute &lt;code&gt;CREATE INDEX ... INVISIBLE ONLINE&lt;/code&gt;, then query &lt;code&gt;DBA_INDEXES&lt;/code&gt; for &lt;code&gt;CLUSTERING_FACTOR&lt;/code&gt;, &lt;code&gt;BLEVEL&lt;/code&gt;, and &lt;code&gt;LEAF_BLOCKS&lt;/code&gt; alongside &lt;code&gt;DBA_TABLES.BLOCKS&lt;/code&gt; and &lt;code&gt;NUM_ROWS&lt;/code&gt;. If &lt;code&gt;CLUSTERING_FACTOR&lt;/code&gt; approaches &lt;code&gt;NUM_ROWS&lt;/code&gt; on a range-scanned column, flag the index for review before running SPA. &lt;em&gt;Rollback: &lt;code&gt;DROP INDEX&lt;/code&gt;.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate 03 — SPA Dual-Trial Evaluation:&lt;/strong&gt; Capture an AWR SQL Tuning Set spanning full batch and OLTP cycles (not just a single off-peak hour) and run the &lt;code&gt;DBMS_SQLPA&lt;/code&gt; baseline vs. session-enabled comparison. &lt;em&gt;Rollback: &lt;code&gt;DROP INDEX&lt;/code&gt;.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gate 04 — Session Canary &amp;#x26; Serialized Promotion:&lt;/strong&gt; Enable &lt;code&gt;ALTER SESSION SET OPTIMIZER_USE_INVISIBLE_INDEXES = TRUE&lt;/code&gt; on a single canary application pool or service while watching Active Session History (&lt;code&gt;V$ACTIVE_SESSION_HISTORY&lt;/code&gt;) for &lt;code&gt;db file sequential read&lt;/code&gt; spikes. For critical batch SQL IDs that must never flip plans even after promotion, pin their existing plans in SQL Plan Management (&lt;code&gt;DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE&lt;/code&gt;) before changing index visibility. Once clean, execute &lt;code&gt;ALTER INDEX ... VISIBLE&lt;/code&gt; serially across Pluggable Databases (PDBs) to avoid concurrent library cache invalidation storms. &lt;em&gt;Rollback: &lt;code&gt;ALTER INDEX ... INVISIBLE&lt;/code&gt; (instant, zero data loss).&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2023-02-10-oracle-index-engineering-clustering-factor-invisible-5-flow.svg&quot; alt=&quot;Staged rollout with four gates and explicit rollback points at every step&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;This exact &lt;code&gt;INVISIBLE&lt;/code&gt;-build-plus-workload-verification pattern is not merely a custom CI/CD convention; it is the foundational mechanism behind Oracle’s own &lt;strong&gt;Automatic Indexing&lt;/strong&gt; (&lt;code&gt;DBMS_AUTO_INDEX&lt;/code&gt;), introduced in &lt;strong&gt;Oracle Database 19c&lt;/strong&gt; on Exadata and Autonomous Database and expanded in &lt;strong&gt;Oracle Database 23ai&lt;/strong&gt; with expression tracking and on-demand APIs.&lt;/p&gt;
&lt;p&gt;In Oracle’s documented Automatic Indexing workflow:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Identification and Invisible Creation:&lt;/strong&gt; The background task analyzes workload column usage and creates candidate indexes with a &lt;code&gt;SYS_AI_&lt;/code&gt; prefix in the &lt;code&gt;INVISIBLE&lt;/code&gt; and &lt;code&gt;UNUSABLE&lt;/code&gt; or &lt;code&gt;VISIBLE&lt;/code&gt; lifecycle states, ensuring production sessions cannot use them prematurely.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SPA Verification:&lt;/strong&gt; Oracle internally invokes SQL Performance Analyzer verification against captured workload SQL before flipping any &lt;code&gt;SYS_AI_&lt;/code&gt; index to &lt;code&gt;VISIBLE&lt;/code&gt;. If a candidate index improves target statements without regressing other SQL statements, it is promoted; otherwise it remains invisible and is eventually purged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Operational Controls:&lt;/strong&gt; Administrators can run &lt;code&gt;DBMS_AUTO_INDEX.CONFIGURE(&apos;AUTO_INDEX_MODE&apos;, &apos;REPORT ONLY&apos;)&lt;/code&gt; to build and test invisible candidates without ever promoting them automatically, or &lt;code&gt;&apos;IMPLEMENT&apos;&lt;/code&gt; to allow automated promotion.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For teams running Oracle 19c on non-Exadata infrastructure or without Autonomous Database—where &lt;code&gt;DBMS_AUTO_INDEX&lt;/code&gt; is not available—implementing the same &lt;code&gt;INVISIBLE ONLINE&lt;/code&gt; + &lt;code&gt;DBMS_SQLPA&lt;/code&gt; pipeline in GitOps/CI provides equivalent regression protection using documented, supported database primitives. &lt;em&gt;(Note: &lt;code&gt;DBMS_SQLPA&lt;/code&gt; requires the Oracle Real Application Testing option.)&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure Scenario&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Failure Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Operational Consequence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Invisible Index Sprawl&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Failed or abandoned invisible indexes are never dropped&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Consumes tablespace storage and slows every &lt;code&gt;INSERT&lt;/code&gt;/&lt;code&gt;UPDATE&lt;/code&gt;/&lt;code&gt;DELETE&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce automated TTL cleanup to drop unpromoted invisible indexes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Write-Heavy Table Contention&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Multiple indexes on high-DML tables&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Leaf block splits, redo overhead, and &lt;code&gt;enq: TX - index contention&lt;/code&gt; waits&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Gate on &lt;code&gt;V$SEGMENT_STATISTICS&lt;/code&gt; write rates before approving new indexes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;SPA Bind-Peeking Blindness&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;STS captures only a narrow window or single bind set&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Trial misses skewed predicates handled by Adaptive Cursor Sharing&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Capture multi-day STS snapshots covering both OLTP and batch windows&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unique Invisible Index Trap&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Creating &lt;code&gt;UNIQUE INVISIBLE&lt;/code&gt; on a live table&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Storage engine enforces uniqueness on DML despite &lt;code&gt;INVISIBLE&lt;/code&gt; status&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Ban &lt;code&gt;UNIQUE&lt;/code&gt; indexes from automated pipelines; require constraint review&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Exadata Smart Scan Bypass&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;New index tempts CBO away from Smart Scan&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Loses storage cell predicate offload; shifts filtering load to DB host CPU&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Calibrate system stats with &lt;code&gt;DBMS_STATS.GATHER_SYSTEM_STATS(&apos;EXADATA&apos;)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Online Build Journal Lock (&lt;code&gt;ORA-00054&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Concurrent DML collides with DDL lock acquisition&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;CREATE INDEX ... ONLINE&lt;/code&gt; fails or blocks waiting on &lt;code&gt;SYS_JOURNAL&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Schedule builds off-peak and set session &lt;code&gt;DDL_LOCK_TIMEOUT&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Concurrent PDB Visibility Pivot&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Running &lt;code&gt;ALTER INDEX ... VISIBLE&lt;/code&gt; across PDBs at once&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Simultaneous cursor invalidations trigger &lt;code&gt;library cache: mutex X&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Serialize &lt;code&gt;ALTER INDEX ... VISIBLE&lt;/code&gt; one PDB at a time&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2023-02-10-oracle-index-engineering-clustering-factor-invisible-6-failures.svg&quot; alt=&quot;Failure-path overlay showing four failure modes that bypass invisible-index safety&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Visibility is the blast radius, not the build:&lt;/strong&gt; &lt;code&gt;CREATE INDEX ... ONLINE&lt;/code&gt; protects DML concurrency during construction, but only &lt;code&gt;INVISIBLE&lt;/code&gt; protects the Cost-Based Optimizer from immediate global plan invalidation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;CLUSTERING_FACTOR&lt;/code&gt; determines real I/O cost:&lt;/strong&gt; An index whose &lt;code&gt;CLUSTERING_FACTOR&lt;/code&gt; approaches &lt;code&gt;NUM_ROWS&lt;/code&gt; forces random single-block I/O that can make an Index Range Scan orders of magnitude slower than a Full Table Scan.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Toggle &lt;code&gt;OPTIMIZER_USE_INVISIBLE_INDEXES&lt;/code&gt; per session in SPA:&lt;/strong&gt; &lt;code&gt;DBMS_SQLPA&lt;/code&gt; tests invisible indexes by running &lt;code&gt;TEST EXECUTE&lt;/code&gt; twice—once with &lt;code&gt;FALSE&lt;/code&gt; and once after &lt;code&gt;ALTER SESSION SET OPTIMIZER_USE_INVISIBLE_INDEXES = TRUE&lt;/code&gt;—comparing &lt;code&gt;BUFFER_GETS&lt;/code&gt; across the full AWR SQL Tuning Set.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Invisible is not free:&lt;/strong&gt; The storage engine still maintains the B-tree synchronously on every DML statement and still enforces &lt;code&gt;UNIQUE&lt;/code&gt; constraints even when &lt;code&gt;VISIBILITY = &apos;INVISIBLE&apos;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle 19c+ Automatic Indexing uses the same loop:&lt;/strong&gt; On Exadata and Autonomous Database, &lt;code&gt;DBMS_AUTO_INDEX&lt;/code&gt; automates invisible creation, SPA verification, and promotion; CI/CD pipelines bring that same safety gate to standard enterprise deployments.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Production teams still run raw &lt;code&gt;CREATE INDEX ... ONLINE&lt;/code&gt; DDL without testing how the new index alters execution plans for unrelated batch and reporting SQL.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Mandate &lt;code&gt;INVISIBLE ONLINE&lt;/code&gt; for all production index builds and gate promotion on &lt;code&gt;DBMS_SQLPA&lt;/code&gt; dual-trial comparison (&lt;code&gt;BUFFER_GETS&lt;/code&gt;) plus a session-level canary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Query &lt;code&gt;DBA_INDEXES&lt;/code&gt; on your highest-churn tables today to compare &lt;code&gt;CLUSTERING_FACTOR&lt;/code&gt; against &lt;code&gt;BLOCKS&lt;/code&gt; and &lt;code&gt;NUM_ROWS&lt;/code&gt;, and check &lt;code&gt;V$SQL_PLAN&lt;/code&gt; for batch queries doing high-cost index lookups.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Wrap your next non-unique production index request in &lt;code&gt;CREATE INDEX ... INVISIBLE ONLINE&lt;/code&gt;, run a two-trial &lt;code&gt;DBMS_SQLPA&lt;/code&gt; comparison against a 24-hour AWR SQL Tuning Set, and promote with &lt;code&gt;ALTER INDEX ... VISIBLE&lt;/code&gt; only after zero fleet regressions.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Oracle Database Administrator’s Guide: &lt;em&gt;Managing Indexes (Invisible Indexes &amp;#x26; Automatic Indexing)&lt;/em&gt; (&lt;code&gt;docs.oracle.com/en/database/oracle/oracle-database/19/admin/managing-indexes.html&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Oracle Database PL/SQL Packages and Types Reference: &lt;em&gt;DBMS_SQLPA&lt;/em&gt; (&lt;code&gt;docs.oracle.com/en/database/oracle/oracle-database/19/arpls/DBMS_SQLPA.html&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Oracle Database Performance Tuning Guide: &lt;em&gt;Gathering System Statistics (&lt;code&gt;EXADATA&lt;/code&gt;)&lt;/em&gt; (&lt;code&gt;docs.oracle.com&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Oracle Database Reference: &lt;em&gt;DBA_INDEXES (&lt;code&gt;CLUSTERING_FACTOR&lt;/code&gt;, &lt;code&gt;VISIBILITY&lt;/code&gt;)&lt;/em&gt; (&lt;code&gt;docs.oracle.com&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>failures</category><category>ai-engineering</category><category>checklist</category></item><item><title>Oracle Data Guard Switchover Orchestration: Services, FAN, and Outcome Reconciliation</title><link>https://rajivonai.com/blog/2022-11-20-data-guard-broker-automation-switchover/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-11-20-data-guard-broker-automation-switchover/</guid><description>How to orchestrate an Oracle Data Guard switchover without unsafe retries, using role-based services, FAN, connection draining, Application Continuity, and evidence-based verification.</description><pubDate>Tue, 05 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A Data Guard role transition is not complete when the target reports &lt;code&gt;READ WRITE&lt;/code&gt;; it is complete when the broker is healthy, the former primary is safely in its new role, role-based services are online only where intended, clients have discarded invalid sessions, and a business transaction succeeds through the production connection path.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Treating &lt;code&gt;READ WRITE&lt;/code&gt; as ready, relying on DNS or L4 proxies to move live Oracle sessions, or auto-retrying a timed-out &lt;code&gt;DGMGRL&lt;/code&gt; switchover causes split routing and reconnect storms.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Pair &lt;code&gt;PRIMARY&lt;/code&gt;-role Clusterware services, both-site connect descriptors, and FAN/ONS with a single non-retried switchover command that enters &lt;code&gt;OUTCOME_UNKNOWN&lt;/code&gt; reconciliation on timeout.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Once the new primary accepts writes, an automatic reversal is not a rollback—it is a brand-new switchover that requires its own preflight and approval.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2022-11-20-data-guard-broker-automation-switchover.svg&quot; alt=&quot;Data Guard switchover finishes only when role services, FAN, and writes are verified&quot;&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Production Context: 2022 | Oracle 19c Data Guard Operations | Technical Review: September 2026&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Oracle Data Guard maintains standby databases by transporting and applying redo. A planned switchover reverses the primary and standby roles without data loss when it completes successfully. A failover is a different operation used after an outage and may lose data depending on protection mode and the state of redo transport. Combining both under a generic “regional failover” button hides a critical safety decision.&lt;/p&gt;
&lt;p&gt;The broker reduces the database role change to a governed command and coordinates with Oracle Clusterware or Oracle Restart. Applications still need a service topology that follows the role. Oracle’s documented pattern uses services configured for the &lt;code&gt;PRIMARY&lt;/code&gt; role on every database that may become primary, connect descriptors that can locate both sites, and FAN events delivered through Oracle Notification Services (ONS) to supported clients and pools.&lt;/p&gt;
&lt;p&gt;That client path is frequently the least-tested part of disaster recovery. A team can validate redo apply every day and still discover during a switchover that an old JDBC driver does not consume FAN, a third-party pool retains dead sessions, only one site’s address is in the connect descriptor, or a synthetic probe bypasses the same service used by production.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;h3 id=&quot;why-switch-the-database-then-change-dns-is-incomplete&quot;&gt;Why “Switch the Database, Then Change DNS” Is Incomplete&lt;/h3&gt;
&lt;p&gt;DNS can remain part of endpoint discovery, but it does not manage established sessions. An authoritative record change affects only later resolutions, and caching occurs in operating systems, runtimes, resolvers, and connection pools. More importantly, a live Oracle session contains authentication, transaction, package, and session state. Neither DNS nor a generic layer-four proxy can move that state to another database connection.&lt;/p&gt;
&lt;p&gt;Four incorrect assumptions create long outages:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;READ WRITE&lt;/code&gt; means ready.&lt;/strong&gt; Open mode alone does not prove the expected &lt;code&gt;DB_UNIQUE_NAME&lt;/code&gt;, &lt;code&gt;DATABASE_ROLE&lt;/code&gt;, broker health, protection state, PDB open state, role-based service placement, listener registration, or application schema readiness.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A proxy can preserve the session.&lt;/strong&gt; Envoy’s TCP proxy can choose an upstream for a new TCP connection and can close or drain existing connections. An endpoint update does not transplant an established Oracle Net session. Transparent replay requires database and client cooperation, such as Application Continuity and Transaction Guard, and even then only eligible requests are replayed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A timed-out switchover is safe to retry.&lt;/strong&gt; Role transition is not an idempotent write. If the workflow loses its response after the broker commits the transition, retrying the same directional command can fail, target the wrong role assumptions, or trigger conflicting operator action. The correct next step is observation and reconciliation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;All reconnects are harmless.&lt;/strong&gt; When pools simultaneously discard sessions, the new primary can face a connection storm, authentication CPU, session initialization, and cache-warming pressure. FAN speeds removal of unusable connections, but pool sizing, connection pacing, retry jitter, and database capacity still determine recovery behavior.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2022-11-20-data-guard-broker-automation-switchover-2-constraints.svg&quot; alt=&quot;DNS cutovers, proxy endpoint swaps, and auto-retried commands fail Data Guard switchovers&quot;&gt;&lt;/p&gt;
&lt;p&gt;The engineering question is: &lt;strong&gt;How should a switchover controller drain work, execute exactly one role-change intent, recover from an unknown outcome, and prove end-to-end service without inventing atomicity across the broker, Clusterware, clients, and application tiers?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Four orchestration and routing patterns cover the common ways teams automate Data Guard switchovers.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Approach&lt;/th&gt;&lt;th&gt;Client Relocation&lt;/th&gt;&lt;th&gt;Timeout Safety&lt;/th&gt;&lt;th&gt;Session State&lt;/th&gt;&lt;th&gt;Verdict&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;DNS CNAME Cutover&lt;/td&gt;&lt;td&gt;TTL cache + dead sockets&lt;/td&gt;&lt;td&gt;Manual recovery&lt;/td&gt;&lt;td&gt;Lost on cutover&lt;/td&gt;&lt;td&gt;Rejected alone&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;L4 Proxy / Mesh Swap&lt;/td&gt;&lt;td&gt;New TCP only; drops txns&lt;/td&gt;&lt;td&gt;No Oracle role check&lt;/td&gt;&lt;td&gt;Lost on socket close&lt;/td&gt;&lt;td&gt;Ingress layer only&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Auto-Retried DGMGRL&lt;/td&gt;&lt;td&gt;Uses role services&lt;/td&gt;&lt;td&gt;Races completed switchover&lt;/td&gt;&lt;td&gt;Needs FAN/AC&lt;/td&gt;&lt;td&gt;Rejected — unsafe&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Role Services + Reconciler&lt;/td&gt;&lt;td&gt;Both-site desc + FAN/ONS&lt;/td&gt;&lt;td&gt;&lt;code&gt;OUTCOME_UNKNOWN&lt;/code&gt; probe&lt;/td&gt;&lt;td&gt;AC/TG where supported&lt;/td&gt;&lt;td&gt;Selected&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2022-11-20-data-guard-broker-automation-switchover-3-options.svg&quot; alt=&quot;Role-based services with FAN and reconciled orchestration outperform DNS and auto-retry scripts&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;DNS CNAME cutover&lt;/strong&gt; only affects new lookups after TTL expiry and leaves existing connection pools pinned to dead sockets. &lt;strong&gt;An L4 proxy or service mesh&lt;/strong&gt; can redirect new TCP connections, but it cannot migrate authenticated Oracle Net session state or in-flight transactions. &lt;strong&gt;Auto-retrying a &lt;code&gt;DGMGRL&lt;/code&gt; activity&lt;/strong&gt; in a workflow engine risks racing an already-committed role transition if the first response timed out. &lt;strong&gt;Role-based services with FAN/ONS and an &lt;code&gt;OUTCOME_UNKNOWN&lt;/code&gt; reconciler&lt;/strong&gt; cleanly separate client discovery from non-idempotent broker execution.&lt;/p&gt;
&lt;h2 id=&quot;the-role-aware-switchover-control-plane&quot;&gt;The Role-Aware Switchover Control Plane&lt;/h2&gt;
&lt;p&gt;Use Oracle’s role-aware service and client mechanisms as the data plane. The orchestrator coordinates and records state; it does not pretend the steps form one distributed transaction. Once the new primary accepts writes, an automatic reversal is not a rollback—it is another switchover with new risk and must receive a new decision.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2022-11-20-data-guard-broker-automation-switchover-4-architecture.svg&quot; alt=&quot;Separate the switchover orchestrator from role-based services and independent site probes&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-make-client-relocation-a-database-service-contract&quot;&gt;1. Make Client Relocation a Database-Service Contract&lt;/h3&gt;
&lt;p&gt;Configure a write service on each Data Guard member with a &lt;code&gt;PRIMARY&lt;/code&gt; role attribute, using &lt;code&gt;srvctl&lt;/code&gt; and the database’s actual Clusterware or Oracle Restart topology. The service name must be the name in application connect descriptors; clients should not connect through the default administrative service. When the broker performs a role transition, it works with Clusterware or Restart to start the role-appropriate service and publish FAN events.&lt;/p&gt;
&lt;p&gt;A production connection contract should include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;addresses capable of reaching listeners at both potential primary sites, with bounded connect and retry behavior;&lt;/li&gt;
&lt;li&gt;one role-based &lt;code&gt;SERVICE_NAME&lt;/code&gt;, created consistently on both databases;&lt;/li&gt;
&lt;li&gt;ONS discovery for all sites and Fast Connection Failover enabled for supported Oracle drivers or pools;&lt;/li&gt;
&lt;li&gt;current, compatible client libraries and no mixed JDBC or ONS JAR versions;&lt;/li&gt;
&lt;li&gt;pool validation, retry, maximum lifetime, and connection-creation limits tested against the expected outage window;&lt;/li&gt;
&lt;li&gt;Application Continuity or Transparent Application Continuity only where licensed, supported, and proven for the application’s calls and session state.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;FAN is a notification channel, not transaction replay. It tells integrated clients that a service or instance is down or up so pools can remove unusable connections and direct new work correctly. Application Continuity can replay eligible requests after a recoverable interruption. Transaction Guard supplies a commit outcome used by replay. Calls with unprotected side effects, unsupported session state, or replay-disabled behavior may still return an error and require application-level idempotency.&lt;/p&gt;
&lt;p&gt;Generic proxies can still provide access control, regional ingress, observability, or connection-rate limits. Treat them as another failure domain. An Envoy endpoint update affects connection selection; it does not understand Oracle transactions, multiplex authenticated sessions safely, or guarantee that in-flight statements continue. Oracle Connection Manager has Oracle-specific capabilities, but its exact concentration, filtering, and Traffic Director features must be designed from its product documentation rather than inferred from a generic TCP proxy.&lt;/p&gt;
&lt;h3 id=&quot;2-validate-readiness-before-mutating-roles&quot;&gt;2. Validate Readiness Before Mutating Roles&lt;/h3&gt;
&lt;p&gt;Create an immutable operation record containing request ID, source and target &lt;code&gt;DB_UNIQUE_NAME&lt;/code&gt;, expected current roles, broker configuration identity, protection mode, application service set, approval, and deadline. Acquire a lease that prevents another workflow or operator integration from initiating a role transition for the same configuration.&lt;/p&gt;
&lt;p&gt;The preflight should collect and retain:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;SHOW CONFIGURATION&lt;/code&gt; and &lt;code&gt;SHOW DATABASE VERBOSE&lt;/code&gt; for both members, including errors and warnings.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;VALIDATE DATABASE VERBOSE&lt;/code&gt; for the target. Use the strict checks supported by the installed release where policy requires them, and separately validate network configuration and SPFILE differences.&lt;/li&gt;
&lt;li&gt;Primary transport state and target apply state, unresolved transport errors, redo gaps, standby redo-log readiness, and the broker’s own “Ready for Switchover” result.&lt;/li&gt;
&lt;li&gt;Clusterware or Restart ownership plus the exact role, drain, replay, notification, and placement attributes of every application service on both databases.&lt;/li&gt;
&lt;li&gt;PDB recovery and saved-state assumptions. The post-transition workflow must verify only the PDBs and services expected on this database, not blindly open every PDB.&lt;/li&gt;
&lt;li&gt;Client readiness: FAN subscription health, both-site reachability, pool version, application admission control, and synthetic credentials.&lt;/li&gt;
&lt;li&gt;Capacity on the target for the reconnect and cache-warming envelope, including processes, sessions, CPU, storage latency, and downstream dependencies.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Do not require an arbitrary zero-second lag as a substitute for broker readiness. A switchover coordinates a no-data-loss role reversal and may wait for apply to catch up. Lag is still an RTO input: the controller should reject or require elevated approval when the observed drain estimate breaches the business deadline.&lt;/p&gt;
&lt;p&gt;For RAC services configured with Application Continuity attributes, the broker &lt;code&gt;SWITCHOVER ... WAIT&lt;/code&gt; option can wait for service draining; Oracle documents that the option has no effect for a single-instance database. Draining is bounded. After its timeout, remaining work can be interrupted, so the runbook must define what the application observes and which requests are safe to retry.&lt;/p&gt;
&lt;h3 id=&quot;3-reconcile-an-ambiguous-role-change-outcome&quot;&gt;3. Reconcile an Ambiguous Role-Change Outcome&lt;/h3&gt;
&lt;p&gt;Run DGMGRL with a secure external password store or another approved credential mechanism; do not place a SYS password in the process list or workflow payload. The command activity must have automatic retries disabled. A workflow framework may retry read-only probes, but the role-change activity itself changes state to &lt;code&gt;ROLE_CHANGE_SUBMITTED&lt;/code&gt; before invocation.&lt;/p&gt;
&lt;p&gt;The state machine needs an explicit unknown state:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;APPROVED -&gt; PRECHECKED -&gt; DRAINING -&gt; ROLE_CHANGE_SUBMITTED&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ROLE_CHANGE_SUBMITTED -&gt; OUTCOME_UNKNOWN -&gt; RECONCILING&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ROLE_CHANGE_SUBMITTED -&gt; NEW_PRIMARY_OBSERVED -&gt; VERIFYING&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;RECONCILING -&gt; NEW_PRIMARY_OBSERVED or MANUAL_INTERVENTION&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;VERIFYING -&gt; ADMITTING -&gt; COMPLETE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After a timeout or controller crash, query both databases independently using connect identifiers that do not depend on the application service. Compare &lt;code&gt;DB_UNIQUE_NAME&lt;/code&gt;, &lt;code&gt;DATABASE_ROLE&lt;/code&gt;, &lt;code&gt;OPEN_MODE&lt;/code&gt;, broker status, and service state. There are only three safe automation conclusions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Intent achieved:&lt;/strong&gt; exactly the requested target is primary, the former primary is a standby, and the broker recognizes the configuration. Continue forward verification.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Intent not started:&lt;/strong&gt; original roles remain intact and the broker has no transition in progress. A new attempt may be authorized after the cause is known; it is not an invisible activity retry.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ambiguous or unsafe:&lt;/strong&gt; databases are unreachable, roles conflict with intent, the former primary is not a viable standby, or broker state is inconsistent. Fence further automation and hand off the evidence bundle.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This distinction is why a simplistic Temporal activity with &lt;code&gt;MaximumAttempts: 3&lt;/code&gt; is dangerous. Durable workflow history is useful, but it cannot make a non-idempotent external mutation idempotent. Idempotency comes from a single durable intent, serialized ownership, and reconciliation of observed Oracle state.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Post-transition verification proceeds from the database outward to the client, with explicit halt gates before admitting production traffic:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2022-11-20-data-guard-broker-automation-switchover-5-flow.svg&quot; alt=&quot;Switchover state machine reconciles unknown outcomes before verifying and admitting traffic&quot;&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Confirm one expected primary with &lt;code&gt;READ WRITE&lt;/code&gt;, the target DB identity, expected protection mode, and a broker configuration with no unresolved role-transition error.&lt;/li&gt;
&lt;li&gt;Confirm the former primary has reached its intended standby state and redo transport and apply have resumed in the reverse direction. Do not close the event with no remaining DR protection.&lt;/li&gt;
&lt;li&gt;For required PDBs, validate open mode, restricted state, and service association.&lt;/li&gt;
&lt;li&gt;Confirm the primary-role service is running on the new primary, stopped on the standby as designed, registered with the intended listeners, and publishing notifications.&lt;/li&gt;
&lt;li&gt;Open a &lt;strong&gt;new&lt;/strong&gt; connection through the production connect descriptor and service. Check database identity and role from the session to catch routing to the wrong site.&lt;/li&gt;
&lt;li&gt;Execute a uniquely identified synthetic transaction in a dedicated table, commit it, read it back through a separate connection, and clean it up through the same governed path.&lt;/li&gt;
&lt;li&gt;Restore application admission in cohorts. Watch new sessions per second, authentication and logon time, listener errors, pool pending requests, database CPU, service latency, and application error rate. Stop expansion when a reviewed guardrail trips.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The synthetic transaction must be designed as production data: isolated, auditable, idempotent, and excluded from business reporting. A TCP check or &lt;code&gt;SELECT 1 FROM dual&lt;/code&gt; proves connectivity but not write service correctness.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Oracle’s documented Data Guard behavior provides a testable pattern: the broker performs the role change, Clusterware or Restart starts services according to role, and FAN informs integrated clients. Application Continuity is supported for physical-standby switchovers subject to configuration and licensing requirements. These are public system behaviors; they are not evidence for fabricated claims of zero errors, millisecond recovery, or dozens of simultaneous switchovers.&lt;/p&gt;
&lt;p&gt;Qualify each application service with a failure-injection matrix before attaching it to an RTO:&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Test&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Evidence to retain&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Pass condition&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Planned switchover with drain&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Broker transcript, service events, pool statistics, active-request outcomes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Drain is bounded; eligible requests complete or replay; non-replayed errors are documented and safe.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Controller loss after command submission&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Workflow record and independent role probes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Restart enters reconciliation and never resubmits the role change automatically.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;ONS path unavailable&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Client logs, pool behavior, reconnect rate&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Clients recover through bounded validation and reconnect behavior without retaining writes to the old role.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;New-primary connection surge&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Logon rate, listener errors, CPU, session counts, application queue depth&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Admission pacing keeps the database inside tested capacity and eventually restores the full cohort.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Former primary cannot rejoin&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Broker state, protection posture, incident decision&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Customer service and degraded DR posture are reported separately; the operation cannot be marked fully complete.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Measure RTO from the business event that starts the transition to verified service restoration, not only DGMGRL duration. Publish a distribution from repeated drills—median, tail, and failure causes—rather than a single best result. The drill should use the production driver, pool, connect descriptor, ONS path, authentication, and service attributes; otherwise it validates a different system.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2022-11-20-data-guard-broker-automation-switchover-6-failures.svg&quot; alt=&quot;Missing role services, single-site descriptors, and un-reinstated standbys break switchovers&quot;&gt;&lt;/p&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Operational scenario&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Physical failure mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Consequence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation protocol&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Outcome is lost after broker submission&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Network or workflow failure hides whether the role transition committed.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;An automatic retry races the completed transition or an operator.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Persist submission first, disable mutation retries, probe both members independently, and resume only from reconciled roles.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Service is not role configured on both sites&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The new primary opens but Clusterware has no matching &lt;code&gt;PRIMARY&lt;/code&gt; service definition.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;FAN may announce database state while the application service remains unavailable.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Diff &lt;code&gt;srvctl config service&lt;/code&gt; across sites in preflight and verify actual service placement after every transition.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Client cannot discover the new primary&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Connect descriptor or ONS configuration includes only the former site, or runtime artifacts are incompatible.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pools discard sessions but repeatedly reconnect to an unavailable listener.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Include both sites, test remote ONS and driver dependencies, and run production-path connection probes continuously.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;In-flight work is not replay safe&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;A request has unprotected side effects, unsupported session state, or ambiguous commit outcome.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Blind application retries duplicate effects or return inconsistent results.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use Transaction Guard and Application Continuity where supported, design idempotency at business boundaries, and document non-replayable calls.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Reconnect storm exceeds target capacity&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Thousands of pools create sessions at once after FAN or socket failure.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Authentication and session initialization starve useful work on the new primary.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cap pool creation, apply jitter and admission cohorts, reserve process headroom, and stop rollout on listener or CPU guardrails.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;PDB or service readiness is partial&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;CDB role changes, but a required PDB is closed, restricted, or missing its service.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Some tenants recover while others receive availability errors.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Maintain an expected PDB-service manifest and verify each required mapping before admitting its cohort.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Former primary is not a healthy standby&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Restart, reinstate, transport, or apply fails after the target accepts writes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Service is restored with no validated protection against the next failure.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Track customer recovery and DR restoration as separate objectives, alert degraded posture, and block routine closeout until protection is restored or risk is accepted.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;FSFO topology is misunderstood as quorum&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Multiple observers are treated as voting members, although broker selects one master and the others are backups.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Operators infer split-brain protection that the observer design does not provide.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Design FSFO from broker protection-mode and connectivity rules, inspect master-observer state, and test asymmetric partitions with database fencing assumptions.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;september-2026-update&quot;&gt;September 2026 Update&lt;/h2&gt;
&lt;p&gt;OCI Full Stack Disaster Recovery can orchestrate supported OCI database, compute, storage, networking, and application resources through ordered DR plans. It is a control-plane option, not proof that an application’s connection and transaction semantics are correct. Preserve the same prechecks, ambiguous-outcome handling, service verification, and production-path synthetic test when adopting it; document which resources use native handlers and which use custom steps.&lt;/p&gt;
&lt;p&gt;Oracle Database releases and current drivers continue to improve FAN, draining, and Application Continuity behavior, but the installed database RU and client version remain the compatibility boundary. Validate against their exact documentation and support matrix. There is no need to invent a “23ai connection director” to solve a problem that role-based services and supported clients already address.&lt;/p&gt;
&lt;p&gt;Likewise, eBPF or service-mesh routing can change where a &lt;strong&gt;new&lt;/strong&gt; TCP connection goes; it cannot relocate authenticated Oracle session state or guarantee replay of an established transaction. Use network technology for the functions it owns—reachability, policy, telemetry, and rate control—and leave database request continuity to the Oracle service and client contract.&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;READ WRITE&lt;/code&gt; is not application readiness:&lt;/strong&gt; Verify broker health, former-primary standby apply, &lt;code&gt;PRIMARY&lt;/code&gt;-role service placement, FAN delivery, and a synthetic write through the production descriptor.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Never auto-retry a Data Guard role transition:&lt;/strong&gt; Disable activity retries on &lt;code&gt;DGMGRL SWITCHOVER&lt;/code&gt;; on timeout, enter &lt;code&gt;OUTCOME_UNKNOWN&lt;/code&gt; and probe both sites independently.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;DNS and L4 proxies cannot migrate Oracle sessions:&lt;/strong&gt; Use both-site &lt;code&gt;ADDRESS_LIST&lt;/code&gt; descriptors, &lt;code&gt;PRIMARY&lt;/code&gt;-role Clusterware services, and ONS/FAN so pools discard dead sockets cleanly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pace reconnects by cohort:&lt;/strong&gt; Uncapped simultaneous pool reconnects can starve CPU and authentication on a cold new primary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Track DR restoration separately from customer availability:&lt;/strong&gt; A switchover is not cleanly closed until the former primary resumes redo apply as a healthy standby.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Audit every production service for both-site discovery, role-based service definitions, FAN reception, replay eligibility, reconnect pacing, and a tested unknown-outcome runbook.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build a serialized switchover state machine that uses broker validation, bounded draining, one non-retried role mutation, independent role reconciliation, and cohort-based admission.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run drills that kill the controller after command submission, remove ONS, induce a reconnect surge, close one required PDB, and prevent the former primary from rejoining; retain end-to-end evidence for each outcome.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Fix one critical service first, measure business RTO through its real driver and pool, then expand only after the new primary and restored protection both meet explicit gates.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/dgbkr/using-data-guard-broker-to-manage-switchovers-failovers.html&quot;&gt;Oracle Database 19c Data Guard Broker — switchover and failover operations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/dgbkr/oracle-data-guard-broker-commands.html&quot;&gt;Oracle Database 19c Data Guard Broker — DGMGRL commands and validation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/sbydb/managing-oracle-data-guard-role-transitions.html&quot;&gt;Oracle Database 19c Data Guard Concepts — role transitions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/haovw/configuring-fan-specific-drivers-and-pools.html&quot;&gt;Oracle Database 19c High Availability — configuring FAN for drivers and pools&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/haovw/continuous-availability-applications-reference.html&quot;&gt;Oracle Database 19c High Availability — continuous availability for applications&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/racad/server-control-utility-reference.html&quot;&gt;Oracle Database 19c RAC Administration — SRVCTL service attributes and draining&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/V-FS_FAILOVER_OBSERVERS.html&quot;&gt;Oracle Database 19c Reference — V$FS_FAILOVER_OBSERVERS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en-us/iaas/disaster-recovery/&quot;&gt;OCI documentation — Full Stack Disaster Recovery&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>Fleet-Wide Oracle Plan Stability: Detecting Regressions Without Automating an Outage</title><link>https://rajivonai.com/blog/2022-10-15-oracle-statistics-histograms-cardinality-bind-peeking/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-10-15-oracle-statistics-histograms-cardinality-bind-peeking/</guid><description>A production design for detecting Oracle SQL plan regressions from counter deltas, governing SQL Plan Management changes, and proving rollback safely across a fleet.</description><pubDate>Sun, 03 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A slow SQL alert is not evidence that the execution plan is wrong, and a historical plan hash is not proof that the old plan is still safe; autonomous plan control must distinguish a plan regression from changed work, changed data, and system-wide contention before it mutates production.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Dividing cumulative &lt;code&gt;V$SQLSTATS&lt;/code&gt; totals, ignoring row-volume shifts, or blindly purging the shared pool turns plan-stability automation into an outage amplifier.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Compute reset- and RAC-aware interval deltas, verify Diagnostics Pack entitlements, load candidate baselines with &lt;code&gt;fixed =&gt; &apos;NO&apos;&lt;/code&gt; on a single canary cohort, and gate promotion on post-change deltas.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; An &lt;code&gt;ENABLED=YES&lt;/code&gt; baseline does not guarantee plan reproduction—if an index or partition changed, the baseline is silently skipped unless post-change &lt;code&gt;V$SQL&lt;/code&gt; verification confirms it.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2022-10-15-oracle-statistics-histograms-cardinality-bind-peeking.svg&quot; alt=&quot;Detect plan regressions from counter deltas before mutating SQL Plan Management&quot;&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Production Context: 2022 | Oracle 19c Fleet Operations | Technical Review: September 2026&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Oracle’s cost-based optimizer chooses a plan from estimates: object statistics, column distributions, system statistics, optimizer settings, constraints, bind information, and transformations available at parse time. That is a feature, not a defect. Data and execution environments change, so a plan that was good last quarter is not permanently correct.&lt;/p&gt;
&lt;p&gt;Plan instability becomes operationally important when a high-volume statement changes plan and consumes materially more database time per unit of work. Bind peeking can make the first hard parse sensitive to one bind value. Adaptive cursor sharing can create multiple child cursors for different selectivity ranges. Statistics gathering can change cardinality estimates. Schema changes, invalidation, an Oracle release update, service placement, and a different optimizer environment can all produce a different plan.&lt;/p&gt;
&lt;p&gt;A single database can be investigated manually. A fleet cannot depend on an expert noticing every plan transition, reconstructing its context, finding a reproducible alternative, and changing SQL Plan Management (SPM) during an incident. The useful automation boundary is therefore not “pin every slow query.” It is a closed control loop that detects with valid measurements, gathers evidence, applies only authorized changes, and can prove whether the intervention helped.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;h3 id=&quot;why-naive-regression-automation-is-dangerous&quot;&gt;Why Naive Regression Automation Is Dangerous&lt;/h3&gt;
&lt;p&gt;Four implementation errors turn a plan-stability service into an outage amplifier:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Treating cumulative counters as interval rates.&lt;/strong&gt; &lt;code&gt;V$SQLSTATS.EXECUTIONS&lt;/code&gt;, &lt;code&gt;ELAPSED_TIME&lt;/code&gt;, &lt;code&gt;CPU_TIME&lt;/code&gt;, &lt;code&gt;BUFFER_GETS&lt;/code&gt;, and wait-time columns accumulate over the cursor’s retained lifetime. Dividing cumulative elapsed time by cumulative executions produces a lifetime average, not “the last minute.” Filtering on &lt;code&gt;LAST_ACTIVE_TIME&lt;/code&gt; does not change that. A collector must persist two observations and compute deltas, while detecting instance restarts, cursor aging, counter resets, and RAC instance boundaries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Equating latency with plan quality.&lt;/strong&gt; A statement can slow while its plan hash remains unchanged because storage is saturated, rows returned per execution increased, a blocking transaction appeared, CPU entitlement changed, or a remote dependency degraded. Conversely, a plan hash can change without user impact. Remediation requires a plan transition plus a stable unit-of-work comparison and enough samples to reject noise.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Assuming SPM “forces” an arbitrary plan.&lt;/strong&gt; A SQL plan baseline contains accepted plans for matching SQL text. The optimizer selects among enabled, accepted, reproducible plans; fixed plans receive preference over nonfixed plans, but a referenced object or access path can disappear and make a plan non-reproducible. SQL profiles, patches, outlines, hints, parameters, and adaptive behavior are distinct mechanisms. A baseline is a governed optimizer input, not a byte-for-byte execution guarantee.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ignoring licensing and privilege boundaries.&lt;/strong&gt; AWR, ASH, and direct access to their underlying data are Diagnostics Pack features under Oracle’s licensing guide. Some SQL tuning facilities require Tuning Pack. A design that silently queries &lt;code&gt;DBA_HIST_*&lt;/code&gt; or &lt;code&gt;V$ACTIVE_SESSION_HISTORY&lt;/code&gt; everywhere can create compliance exposure. The control plane must select an entitled data path per database and use a narrowly scoped executor for changes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2022-10-15-oracle-statistics-histograms-cardinality-bind-peeking-2-constraints.svg&quot; alt=&quot;Cumulative V$SQLSTATS totals, changed work, and licensing traps mislead naive detectors&quot;&gt;&lt;/p&gt;
&lt;p&gt;The engineering question is: &lt;strong&gt;How can a fleet control plane identify a real plan regression, select a reproducible candidate, roll it out with bounded blast radius, and reverse it without trusting fabricated precision or granting broad DBA access to every collector?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Four approaches define the design space for fleet-wide Oracle plan stability.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Strategy&lt;/th&gt;&lt;th&gt;False-Positive Risk&lt;/th&gt;&lt;th&gt;Parse / Blast Risk&lt;/th&gt;&lt;th&gt;Licensing Safety&lt;/th&gt;&lt;th&gt;Verdict&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Naive Latency Alerting&lt;/td&gt;&lt;td&gt;Very high (totals &amp;#x26; locks)&lt;/td&gt;&lt;td&gt;Manual triage only&lt;/td&gt;&lt;td&gt;Often queries ASH blindly&lt;/td&gt;&lt;td&gt;Rejected — noisy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Auto-Purge Shared Pool&lt;/td&gt;&lt;td&gt;Medium&lt;/td&gt;&lt;td&gt;Hard-parse storm&lt;/td&gt;&lt;td&gt;N/A&lt;/td&gt;&lt;td&gt;Rejected — dangerous&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Global Fixed SPM Pin&lt;/td&gt;&lt;td&gt;High (bind-class skew)&lt;/td&gt;&lt;td&gt;Locks suboptimal plan&lt;/td&gt;&lt;td&gt;Needs STS/AWR care&lt;/td&gt;&lt;td&gt;Human PE approval only&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Delta + Nonfixed Canary&lt;/td&gt;&lt;td&gt;Low (work-normalized)&lt;/td&gt;&lt;td&gt;Bounded + reversible&lt;/td&gt;&lt;td&gt;Per-DB entitlement check&lt;/td&gt;&lt;td&gt;Selected&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2022-10-15-oracle-statistics-histograms-cardinality-bind-peeking-3-options.svg&quot; alt=&quot;Delta-gated nonfixed SPM canary outperforms pool purges and blind baseline pinning&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Naive latency alerting&lt;/strong&gt; was rejected because raw &lt;code&gt;V$SQLSTATS&lt;/code&gt; ratios and unnormalized elapsed times fire whenever row counts or lock waits spike. &lt;strong&gt;Auto-purging the shared pool&lt;/strong&gt; (&lt;code&gt;DBMS_SHARED_POOL.PURGE&lt;/code&gt;) triggers hard-parse storms under concurrency without guaranteeing a better plan. &lt;strong&gt;Global fixed SPM pinning&lt;/strong&gt; locks a single plan across all bind selectivity classes, which breaks skewed workloads. &lt;strong&gt;Delta-based detection paired with a nonfixed canary baseline&lt;/strong&gt; isolates true plan regressions and keeps every mutation reversible.&lt;/p&gt;
&lt;h2 id=&quot;the-guarded-plan-stability-control-loop&quot;&gt;The Guarded Plan-Stability Control Loop&lt;/h2&gt;
&lt;p&gt;Separate observation, decision, mutation, and verification. A collector exports raw monotonic counters. A detector computes interval features and opens a candidate incident. A policy engine decides whether the statement is eligible for automated or approved action. A database-local executor performs one idempotent SPM change. A verifier measures new intervals and either promotes, disables, or rolls back that change.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2022-10-15-oracle-statistics-histograms-cardinality-bind-peeking-4-architecture.svg&quot; alt=&quot;Separate read-only counter collection from policy gating and local SPM execution&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-measure-counter-deltas-correctly&quot;&gt;1. Measure Counter Deltas Correctly&lt;/h3&gt;
&lt;p&gt;Poll a bounded, allowlisted workload rather than scanning every statement at high frequency. Persist a key such as &lt;code&gt;(dbid, instance_number, startup_time, con_id, sql_id, plan_hash_value)&lt;/code&gt; with collection time and cumulative values. The SQL below returns observations; it does &lt;strong&gt;not&lt;/strong&gt; claim that they are interval metrics:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; con_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       sql_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       plan_hash_value,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       executions,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       elapsed_time,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       cpu_time,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       buffer_gets,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       disk_reads,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       rows_processed,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       application_wait_time,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       concurrency_wait_time,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       user_io_wait_time,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       invalidations,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       last_active_time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;   v$sqlstats&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_active_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SYSDATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1440&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    executions &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For consecutive observations with the same startup identity, calculate &lt;code&gt;delta_elapsed / delta_executions&lt;/code&gt;, &lt;code&gt;delta_cpu / delta_executions&lt;/code&gt;, and similar ratios only when &lt;code&gt;delta_executions &gt; 0&lt;/code&gt; and all required deltas are nonnegative. Treat a missing previous row, a lower counter, a changed startup identity, or a collector gap beyond policy as a new series with no rate. Oracle documents elapsed time for parallel execution as coordinator time plus parallel-server time, so it is database work rather than end-user wall time; do not compare serial and parallel samples as if the measure were identical.&lt;/p&gt;
&lt;p&gt;RAC requires per-instance observations before aggregation. A global average can hide one instance using a different child cursor or serving a different bind population. Multitenant fleets also need &lt;code&gt;CON_ID&lt;/code&gt;; the same SQL can behave differently across PDBs with different data and services.&lt;/p&gt;
&lt;p&gt;The detector should require all of the following before opening a regression candidate:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The plan hash changed, or the same plan became non-reproducible and a different child cursor took traffic.&lt;/li&gt;
&lt;li&gt;The new plan has enough completed executions and database time to exceed a minimum evidence floor.&lt;/li&gt;
&lt;li&gt;Per-execution elapsed time, CPU time, buffer gets, or physical reads crossed a versioned threshold with a confidence band, not a single point estimate.&lt;/li&gt;
&lt;li&gt;Rows processed per execution and the application operation class remain comparable. A tenfold increase in requested rows is changed work, not automatically a regression.&lt;/li&gt;
&lt;li&gt;Wait deltas do not point primarily to application locks, cluster contention, resource-manager throttling, or an external bottleneck that a plan baseline cannot fix.&lt;/li&gt;
&lt;li&gt;The regression is observed independently on more than one interval, unless an explicit emergency policy applies.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;ASH can add sampled session context when the database is entitled to Diagnostics Pack, but samples are not completed transactions and must not be presented as exact request counts. Where packs are unavailable, use permitted dynamic views, application latency, service metrics, and the collector’s own deltas. Store the entitlement decision beside every detector configuration.&lt;/p&gt;
&lt;h3 id=&quot;2-build-a-reproducible-plan-evidence-package&quot;&gt;2. Build a Reproducible Plan Evidence Package&lt;/h3&gt;
&lt;p&gt;Do not choose a candidate solely because its plan hash was once fast. Capture:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;exact and force-matching SQL signatures, normalized SQL identity, parsing schema, PDB, and optimizer environment;&lt;/li&gt;
&lt;li&gt;current and candidate plan operations, predicates, object owners, object IDs, index visibility and status, and partition assumptions;&lt;/li&gt;
&lt;li&gt;statistics timestamps, histogram metadata, bind-sensitivity indicators, child-cursor reasons, and relevant parameter differences;&lt;/li&gt;
&lt;li&gt;interval evidence for elapsed time, CPU, logical I/O, physical I/O, rows processed, and wait composition;&lt;/li&gt;
&lt;li&gt;the source of the candidate: current cursor cache, a licensed AWR interval, a governed SQL tuning set, or a transported baseline;&lt;/li&gt;
&lt;li&gt;license entitlement, approval class, expiry, owner, ticket, and rollback command.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE&lt;/code&gt; accepts both &lt;code&gt;sql_id&lt;/code&gt; and &lt;code&gt;plan_hash_value&lt;/code&gt;, so it is the simplest exact load path when the known-good plan is still present. After the executor has verified that an equivalent baseline is not already present, a safe initial load is enabled and nonfixed:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DECLARE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    l_loaded PLS_INTEGER;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    l_loaded :&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; DBMS_SPM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;LOAD_PLANS_FROM_CURSOR_CACHE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        sql_id          &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; :sql_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        plan_hash_value &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; :good_plan_hash,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        fixed           &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;NO&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        enabled&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;         =&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;YES&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    IF&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; l_loaded &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; THEN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        RAISE_APPLICATION_ERROR(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;20001&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;Expected exactly one plan to be loaded&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    END&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IF&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Historical recovery needs more care. Oracle’s &lt;code&gt;LOAD_PLANS_FROM_AWR&lt;/code&gt; &lt;code&gt;basic_filter&lt;/code&gt; is applied to SQL text; it is not a documented plan-hash selector. A narrow snapshot range may still contain multiple plans. If exact historical selection matters, build and inspect a SQL tuning set through the licensed, documented workflow, then load the selected plan from that set. Never concatenate untrusted SQL IDs into a dynamic filter, and never query Oracle’s internal &lt;code&gt;WRH$_&lt;/code&gt; tables as an automation contract.&lt;/p&gt;
&lt;p&gt;The executor should not search &lt;code&gt;DBA_SQL_PLAN_BASELINES&lt;/code&gt; for the first row with origin &lt;code&gt;MANUAL-LOAD&lt;/code&gt;: concurrent work or an existing baseline can make that the wrong plan. Record baseline inventory before and after the load, match the expected SQL handle and plan attributes, and require a single unambiguous new or existing candidate. &lt;code&gt;DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE&lt;/code&gt; is the documented signature function; there is no &lt;code&gt;DBMS_SPM.SQL_TEXT_TO_SIGNATURE&lt;/code&gt; API.&lt;/p&gt;
&lt;h3 id=&quot;3-make-mutation-bounded-and-reversible&quot;&gt;3. Make Mutation Bounded and Reversible&lt;/h3&gt;
&lt;p&gt;Plan changes are production changes. Give the read-only collector no execute privilege on &lt;code&gt;DBMS_SPM&lt;/code&gt;. Route mutations through a database-local package that validates an allowlisted PDB, SQL signature, plan hash, request ID, and approved action. The package should reject DDL, recursive SQL, unregistered statements, stale approvals, and a second active change for the same SQL handle.&lt;/p&gt;
&lt;p&gt;Avoid broad shared-pool purges. &lt;code&gt;DBMS_SHARED_POOL.PURGE&lt;/code&gt; is not an atomic switch and can create hard-parse load; finding an address with a scalar subquery also does not prove every relevant child cursor is gone. Prefer a controlled service or session canary where possible. If cursor invalidation is required, treat it as a separate, rate-limited change with a measured parse-capacity budget and a tested rollback.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Start on one database or one service cohort rather than mutating the entire fleet. Verification compares multiple post-change intervals using the same unit-of-work checks as detection: confirm the intended baseline is active in &lt;code&gt;V$SQL&lt;/code&gt;, verify database work per execution improves, and confirm no new parse or concurrency pressure appears. A fixed execution count such as “50 executions” is not universally sufficient; sample size depends on variance, execution frequency, and business risk.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2022-10-15-oracle-statistics-histograms-cardinality-bind-peeking-5-flow.svg&quot; alt=&quot;Canary SPM rollout verifies plan reproduction before fleet expansion&quot;&gt;&lt;/p&gt;
&lt;p&gt;Rollback normally means disabling the newly introduced plan baseline (&lt;code&gt;DBMS_SPM.ALTER_SQL_PLAN_BASELINE(..., attribute_name =&gt; &apos;ENABLED&apos;, attribute_value =&gt; &apos;NO&apos;)&lt;/code&gt;) or restoring its prior attributes, then allowing a controlled reparse. Dropping evidence immediately destroys the forensic record. Mark uncertain outcomes as &lt;code&gt;VERIFYING_UNKNOWN&lt;/code&gt;, stop further rollout, and require observation before another mutation.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented Oracle behavior supports a conservative operating pattern rather than an unqualified autonomous fixer. SPM can load plans from the cursor cache, AWR, or SQL tuning sets. Loaded plans can be enabled, accepted, and fixed or nonfixed. Oracle’s plan-selection rules prefer reproducible fixed plans over nonfixed plans, but among the eligible set the optimizer still costs plans. Oracle also provides evolve tasks to test nonaccepted plans before acceptance. These mechanisms justify a lifecycle with explicit evidence and approval; they do not justify inventing fleet success rates or recovery times.&lt;/p&gt;
&lt;p&gt;A useful qualification exercise uses a representative staging clone and two known plans for an allowlisted statement:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Capture a minimum of two stable counter intervals for each plan under the same bind class, service, data snapshot, and concurrency envelope.&lt;/li&gt;
&lt;li&gt;Load the candidate as nonfixed and verify its &lt;code&gt;SQL_HANDLE&lt;/code&gt;, &lt;code&gt;PLAN_NAME&lt;/code&gt;, &lt;code&gt;ENABLED&lt;/code&gt;, &lt;code&gt;ACCEPTED&lt;/code&gt;, &lt;code&gt;FIXED&lt;/code&gt;, origin, and reproduction status through documented views.&lt;/li&gt;
&lt;li&gt;Execute through the application service, not only a privileged SQL session. Confirm the observed plan through &lt;code&gt;V$SQL&lt;/code&gt; or the licensed historical path and compare application latency with database-work deltas.&lt;/li&gt;
&lt;li&gt;Disable the candidate and force only the minimum controlled reparse required by the test. Verify that the prior eligible plan returns.&lt;/li&gt;
&lt;li&gt;Restart the executor and redeliver the same request ID. It must report the existing mutation, not load another baseline or alter unrelated plans.&lt;/li&gt;
&lt;li&gt;Make a test index unusable in the disposable staging clone. The workflow must detect that the plan is not safely reproducible and stop, not declare SPM broken or apply a hidden optimizer parameter.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The evidence from this exercise becomes the admission contract for automation. Low-risk, pre-authorized statements may progress from detect-only to canary automation. Revenue-critical DML, statements with unstable bind classes, and changes requiring fixed plans should retain human approval until repeated tests demonstrate safe boundaries.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2022-10-15-oracle-statistics-histograms-cardinality-bind-peeking-6-failures.svg&quot; alt=&quot;Bind-class skew, concurrent SPM races, and stale historical plans break naive remediation&quot;&gt;&lt;/p&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Operational scenario&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Physical failure mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Consequence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation protocol&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Collector treats totals as rates&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cumulative &lt;code&gt;V$SQLSTATS&lt;/code&gt; values are divided without a prior observation or across a restart.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lifetime history or a reset appears as a sudden regression.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Persist raw observations, key by startup and instance identity, reject negative deltas, and suppress decisions until a second valid interval exists.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Bind populations change&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The new interval serves a more expensive selectivity class even though SQL text is identical.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Automation pins a plan optimized for a different bind class and harms valid traffic.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Segment evidence by service or bind class where permitted, inspect adaptive cursor behavior, and exclude statements whose workload cannot be classified safely.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Plan is no longer reproducible&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;An index, partition, object, privilege, or optimizer capability used by the stored outline changed.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The baseline is unused or another eligible plan is selected.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Validate dependencies before mutation and verify actual plan use afterward; never equate &lt;code&gt;ENABLED=YES&lt;/code&gt; with successful reproduction.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;AWR or ASH is used without entitlement&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;A collector queries licensed views or APIs on a database without the required management pack.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The monitoring design creates licensing and audit exposure.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Maintain a per-database entitlement registry, enforce alternate collectors, and review current Oracle licensing terms with the contract owner.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Concurrent remediators race&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Two incidents load, fix, disable, or purge plans for the same SQL handle.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Final baseline attributes are order-dependent and rollback can target the wrong plan.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Serialize by database, PDB, and SQL handle; use immutable request IDs, before-and-after inventory, and compare-and-set state transitions.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Shared-pool purge creates a parse storm&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Many active cursors are invalidated while foreground sessions reconnect or reparse.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Library-cache and CPU pressure compound the original incident.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Separate invalidation approval, rate-limit it, canary by service, and maintain headroom plus an immediate stop condition.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Historical plan fits old data only&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Data volume, clustering, partitions, or rows returned per execution changed materially.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The “known-good” plan consumes more work than the current plan.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reproduce against current representative data, compare work per execution, use nonfixed admission first, and expire emergency baselines.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;RAC or PDB context is lost&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Telemetry is aggregated across instances or the executor connects to &lt;code&gt;CDB$ROOT&lt;/code&gt; instead of the target PDB.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The detector hides a localized regression or the baseline is created in the wrong container.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Carry DBID, instance, service, and &lt;code&gt;CON_ID&lt;/code&gt; end to end; assert session container and database identity inside the mutation package.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;september-2026-update&quot;&gt;September 2026 Update&lt;/h2&gt;
&lt;p&gt;The review date changes the implementation options, not the safety model. Current Oracle 19c documentation remains the authority for a 19c fleet, including the exact Release Update installed. Newer Oracle releases and services may add automatic SPM evolution, quarantine, or managed performance features, but adoption must be based on the target release’s documentation and license terms. Do not project a newer feature backward onto 19c.&lt;/p&gt;
&lt;p&gt;OpenTelemetry’s database semantic conventions provide common database client attributes and spans, but they do not establish &lt;code&gt;db.plan_hash_value&lt;/code&gt; as a universal standard attribute. A plan hash can be attached as a reviewed custom attribute or event when cardinality and sensitivity controls are understood. It should not be placed on every trace if doing so creates excessive cardinality or exposes SQL identity.&lt;/p&gt;
&lt;p&gt;The strongest modernization is operational: version the detector, entitlement policy, mutation package, and baseline expiry rules together. Replay historical observations through a new detector before deployment, shadow its decisions without executing them, and compare candidate volume and false-positive causes. An “AI” classifier can rank incidents, but the enforcement path still needs deterministic gates, explainable evidence, and a bounded rollback.&lt;/p&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Never treat &lt;code&gt;V$SQLSTATS&lt;/code&gt; totals as rates:&lt;/strong&gt; Key observations by &lt;code&gt;(dbid, instance_number, startup_time, con_id, sql_id, plan_hash_value)&lt;/code&gt; and compute deltas across two valid intervals.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Normalize for work and waits before blaming the plan:&lt;/strong&gt; A 10x increase in &lt;code&gt;ROWS_PROCESSED&lt;/code&gt; per execution or a spike in application lock waits is not a plan regression.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Enforce Diagnostics Pack entitlements in code:&lt;/strong&gt; Route non-licensed databases away from &lt;code&gt;DBA_HIST_*&lt;/code&gt; and &lt;code&gt;V$ACTIVE_SESSION_HISTORY&lt;/code&gt; automatically.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Load nonfixed canary baselines first:&lt;/strong&gt; Verify actual reproduction in &lt;code&gt;V$SQL&lt;/code&gt; on one cohort before expanding; never equate &lt;code&gt;ENABLED=YES&lt;/code&gt; with plan reproduction.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Disable rather than drop on rollback:&lt;/strong&gt; Preserving the disabled baseline retains the forensic trail and prevents another worker from re-proposing the same bad plan.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Audit whether fleet alerts derive interval deltas correctly, preserve RAC and PDB identity, distinguish changed work from a changed plan, and respect Diagnostics and Tuning Pack entitlements.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Separate read-only observation from a serialized, database-local SPM executor governed by immutable request IDs, explicit candidate evidence, canary scope, and expiry.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Reproduce two plans in staging, inject restart, duplicate-delivery, dependency-loss, and parse-pressure failures, then demonstrate both successful verification and a reversible uncertain outcome.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Deploy the detector in shadow mode first; authorize mutation only for allowlisted SQL after thresholds, privileges, license paths, rollback, and ownership have passed review.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/V-SQLSTATS.html&quot;&gt;Oracle Database 19c Reference — V$SQLSTATS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/tgsql/managing-sql-plan-baselines.html&quot;&gt;Oracle Database 19c SQL Tuning Guide — managing SQL plan baselines&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/tgsql/overview-of-sql-plan-management.html&quot;&gt;Oracle Database 19c SQL Tuning Guide — SQL plan management&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/arpls/DBMS_SPM.html&quot;&gt;Oracle Database 19c PL/SQL Packages Reference — DBMS_SPM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/arpls/DBMS_SQLTUNE.html&quot;&gt;Oracle Database 19c PL/SQL Packages Reference — DBMS_SQLTUNE&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/DBA_SQL_PLAN_BASELINES.html&quot;&gt;Oracle Database 19c Reference — DBA_SQL_PLAN_BASELINES&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/dblic/Licensing-Information.html&quot;&gt;Oracle Database 19c Licensing Information — management pack features&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://opentelemetry.io/docs/specs/semconv/database/database-spans/&quot;&gt;OpenTelemetry semantic conventions — database spans&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>failures</category><category>ai-engineering</category><category>checklist</category></item><item><title>Database Provisioning as a Distributed System: Abstracting DBCA via Microservices</title><link>https://rajivonai.com/blog/2022-09-05-automating-oracle-builds-dbca-silent-ansible/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-09-05-automating-oracle-builds-dbca-silent-ansible/</guid><description>How to wrap Oracle DBCA in a durable, idempotent provisioning control plane with policy-bound sizing, secret isolation, reconciliation, and verifiable readiness.</description><pubDate>Sat, 02 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A DBCA command is not a provisioning control plane: without durable intent, idempotency keys, host-local execution ownership, secret boundaries, and reconciliation after ambiguous failures, a transient orchestrator outage can create duplicate databases or destroy the evidence needed to recover safely.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Push-based Ansible/SSH DBCA wrappers lose state on network drops, saturate controller forks under fan-out, and expose &lt;code&gt;SYS&lt;/code&gt;/PDB passwords in process args or response files.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Separate a central desired-state API (with idempotency keys and weighted admission control) from a host-local agent that runs DBCA inside a &lt;code&gt;systemd-run&lt;/code&gt; unit and reconciles &lt;code&gt;RUNNING_UNKNOWN&lt;/code&gt; outcomes against Oracle itself.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; A zero exit code from DBCA is not readiness—explicitly probe &lt;code&gt;V$PDBS&lt;/code&gt;, &lt;code&gt;DBA_REGISTRY&lt;/code&gt;, listener registration, and workload-identity secret read-back before marking &lt;code&gt;READY&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2022-09-05-automating-oracle-builds-dbca-silent-ansible.svg&quot; alt=&quot;Wrap DBCA in durable intent, host-local units, and state reconciliation&quot;&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Production Context: 2022 | Enterprise DBaaS Control Planes | Technical Review: September 2026&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Organizations delivering SaaS or managing multi-tenant ERP estates continuously provision Oracle Database 19c instances across bare metal, VMware, and cloud VMs in AWS and OCI. Historically, platform teams automated those builds with push-based Configuration Management tools (Ansible, Puppet, Chef) or Bash wrappers: an orchestrator connects over SSH, copies a &lt;code&gt;dbca.rsp&lt;/code&gt; file, and runs &lt;code&gt;dbca -silent -createDatabase -responseFile /u01/stage/dbca.rsp&lt;/code&gt;. The runner holds the SSH socket open while DBCA formats data files, creates &lt;code&gt;SYSTEM&lt;/code&gt;/&lt;code&gt;SYSAUX&lt;/code&gt;/&lt;code&gt;UNDOTBS1&lt;/code&gt;, initializes redo groups, configures the listener, and runs &lt;code&gt;catalog.sql&lt;/code&gt; and &lt;code&gt;catproc.sql&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;At low volume, operators reconcile ambiguous failures manually. As concurrency grows, human reconciliation becomes a queueing and correctness bottleneck.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;h3 id=&quot;the-distributed-fragility-of-push-based-dbca-provisioning&quot;&gt;The Distributed Fragility of Push-Based DBCA Provisioning&lt;/h3&gt;
&lt;p&gt;Relying on centralized, imperative push scripts to execute stateful, long-running database creation commands introduces acute distributed failure modes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Ambiguous Remote Execution:&lt;/strong&gt; DBCA is a multi-phase process that creates files, starts an instance, runs catalog scripts, and registers configuration. If an SSH transport disappears, the controller often cannot distinguish “command failed,” “command is still running,” and “database completed but acknowledgement was lost.” Whether the child receives &lt;code&gt;SIGHUP&lt;/code&gt; depends on the remote shell, pseudo-terminal, privilege wrapper, and service manager; assuming either success or termination is unsafe.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Controller Backpressure:&lt;/strong&gt; Ansible and similar push systems consume forks, file descriptors, SSH connections, memory, and log bandwidth for each active target. Fifty connections do not exhaust a normal ephemeral-port range by themselves, but unrestricted fan-out can saturate configured forks, bastions, API quotas, storage throughput, or the controller’s CPU. Concurrency must be an explicit capacity limit with queueing and admission control.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Static Inputs vs. Governed Topologies:&lt;/strong&gt; A response file is not inherently static; it can be generated from inventory. The failure is accepting arbitrary host-derived values without policy. Memory, process limits, ASM disk groups, HugePages, NUMA layout, filesystem capacity, and license constraints must be validated against an approved service class before DBCA starts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Credential Exposure in Automation Logs:&lt;/strong&gt; Passing &lt;code&gt;SYS&lt;/code&gt;, &lt;code&gt;SYSTEM&lt;/code&gt;, or PDB administrator passwords in command-line arguments or unprotected response files expands their exposure to process inspection, automation output, shell history, diagnostic capture, backups, and audit records. Oracle explicitly warns that response files may contain plain-text passwords and recommends restricting ownership and deleting or securing them when unused.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2022-09-05-automating-oracle-builds-dbca-silent-ansible-2-constraints.svg&quot; alt=&quot;Push-based SSH orchestration breaks on ambiguous timeouts and secret exposure&quot;&gt;&lt;/p&gt;
&lt;p&gt;The engineering question is: &lt;strong&gt;How should a database vending platform preserve one durable provisioning intent across retries, process restarts, host failures, and uncertain DBCA outcomes without creating a second database or losing the evidence needed for recovery?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Four provisioning models represent the realistic architectural choices for Oracle fleets.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Architecture&lt;/th&gt;&lt;th&gt;Crash Recovery&lt;/th&gt;&lt;th&gt;Secret Boundary&lt;/th&gt;&lt;th&gt;Blast Radius&lt;/th&gt;&lt;th&gt;Verdict&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Push Ansible / SSH&lt;/td&gt;&lt;td&gt;Ambiguous on socket drop&lt;/td&gt;&lt;td&gt;Controller holds secrets&lt;/td&gt;&lt;td&gt;Controller fan-out saturation&lt;/td&gt;&lt;td&gt;Rejected at scale&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Pull Agent + &lt;code&gt;systemd-run&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;RUNNING_UNKNOWN&lt;/code&gt; + lease&lt;/td&gt;&lt;td&gt;Local CSPRNG + &lt;code&gt;tmpfs&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Isolated per host&lt;/td&gt;&lt;td&gt;Selected for VM/BM&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Warm CDB + PDB Clone&lt;/td&gt;&lt;td&gt;Fast tenant creation&lt;/td&gt;&lt;td&gt;Shared CDB admin plane&lt;/td&gt;&lt;td&gt;Shared CDB failure domain&lt;/td&gt;&lt;td&gt;Complementary&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Oracle K8s Operator&lt;/td&gt;&lt;td&gt;K8s controller loop&lt;/td&gt;&lt;td&gt;K8s Secrets / external vault&lt;/td&gt;&lt;td&gt;Requires K8s runtime&lt;/td&gt;&lt;td&gt;K8s estates only&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2022-09-05-automating-oracle-builds-dbca-silent-ansible-3-options.svg&quot; alt=&quot;Durable pull-agent control planes beat push scripts on idempotency and failure recovery&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Push Ansible/SSH&lt;/strong&gt; works for low-volume manual runs, but it was rejected because long-lived SSH sessions turn transient network drops into ambiguous outcomes. &lt;strong&gt;Warm CDB + PDB cloning&lt;/strong&gt; accelerates tenant onboarding when multi-tenancy is acceptable, but it shares CDB patching and outage domains across tenants. &lt;strong&gt;Oracle’s Kubernetes Operator&lt;/strong&gt; provides reconciliation natively if the fleet already runs on Kubernetes, though it does not cover bare-metal or VM-based ASM estates. &lt;strong&gt;A pull-based host agent with &lt;code&gt;systemd-run&lt;/code&gt;&lt;/strong&gt; provides durable local execution and clean secret boundaries across VM and bare-metal Oracle hosts.&lt;/p&gt;
&lt;h2 id=&quot;the-declarative-provisioning-architecture-pull-based-agent-orchestration&quot;&gt;The Declarative Provisioning Architecture: Pull-Based Agent Orchestration&lt;/h2&gt;
&lt;p&gt;The scalable solution is to separate a durable desired-state API from host-local execution. Pull-based agents are one valid implementation, but they are not automatically safer: the safety comes from durable state, authenticated work claims, idempotent reconciliation, bounded concurrency, and explicit cleanup ownership.&lt;/p&gt;
&lt;p&gt;The platform constructs a central &lt;strong&gt;Database Vending API&lt;/strong&gt; (a Go microservice) that records declarative desired state in an authoritative fleet catalog and provisions an unconfigured host VM. On the target host, a &lt;strong&gt;Host Provisioning Agent&lt;/strong&gt; starts under &lt;code&gt;systemd&lt;/code&gt;, authenticates via mTLS, claims one intent generation, validates an approved DBCA plan against local hardware, launches a host-local &lt;code&gt;systemd-run&lt;/code&gt; unit, and reports observed state with evidence.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2022-09-05-automating-oracle-builds-dbca-silent-ansible-4-architecture.svg&quot; alt=&quot;Decouple central desired-state persistence from host-local DBCA execution and secret storage&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-dynamic-hardware-discovery-and-dbca-synthesis&quot;&gt;1. Dynamic Hardware Discovery and DBCA Synthesis&lt;/h3&gt;
&lt;p&gt;Rather than relying on static &lt;code&gt;.rsp&lt;/code&gt; text templates, the local Go agent programmatically evaluates the host’s actual hardware topology before invoking DBCA:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Policy-bounded DBCA plan synthesis. Secrets are deliberately absent.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; HostTopology&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    TotalRAMMB       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CPUCores         &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    HugePagesFreeMB  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ASMDiskGroups    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;map&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; DatabaseIntent&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    RequestID       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; `json:&quot;request_id&quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DatabaseName    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; `json:&quot;database_name&quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PDBName         &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; `json:&quot;pdb_name&quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ServiceClass    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; `json:&quot;service_class&quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ServiceClass&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SGAMB, PGAMB, Processes &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MinRAMMB, MinCores      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; SynthesizeDBCACommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;topo&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; HostTopology&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; DatabaseIntent&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;classes&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; map&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ServiceClass&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) ([]&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;regexp.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;MustCompile&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`^[A-Z][A-Z0-9_]{0,7}$`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;MatchString&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(in.DatabaseName) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Errorf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;invalid database name&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    class, ok &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; classes[in.ServiceClass]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ok &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; topo.TotalRAMMB &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; class.MinRAMMB &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; topo.CPUCores &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; class.MinCores {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Errorf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;host does not satisfy service class &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%q&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, in.ServiceClass)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;topo.ASMDiskGroups[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;+DATA&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;topo.ASMDiskGroups[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;+RECO&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Errorf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;required ASM disk groups are not mounted&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; topo.HugePagesFreeMB &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; class.SGAMB {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Errorf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;insufficient HugePages for requested SGA&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    args &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; []&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;-silent&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;-createDatabase&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;-templateName&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;General_Purpose.dbc&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;-gdbName&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, in.DatabaseName,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;-sid&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, in.DatabaseName,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;-createAsContainerDatabase&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;-numberOfPDBs&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;-pdbName&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, in.PDBName,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;-storageType&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ASM&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;-diskGroupName&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;+DATA&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;-recoveryAreaDestination&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;+RECO&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;-characterSet&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;AL32UTF8&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;-nationalCharacterSet&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;AL16UTF16&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;-initParams&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Sprintf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;sga_target=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;M,pga_aggregate_target=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;M,processes=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            class.SGAMB, class.PGAMB, class.Processes),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; args, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The host data is used to reject an incompatible placement, not to invent database sizing from a percentage. Service classes are reviewed configuration with workload-tested SGA, PGA, process, storage, and license envelopes. Exadata storage servers are not DBCA targets; DBCA runs from the database home on database nodes. Exadata-specific placement must validate the database-server and Grid Infrastructure topology separately.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;2-durable-execution-and-reconciliation&quot;&gt;2. Durable Execution and Reconciliation&lt;/h3&gt;
&lt;p&gt;POSIX process-group detachment is not a durable job system. A service restart may terminate every process in its systemd cgroup, and a detached child does not preserve an authoritative state transition. The host agent instead creates a uniquely named systemd unit from the immutable request ID, persists the unit name before launch, and reconciles the unit and database state after every restart:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; StartDBCAUnit&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;ctx&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; context&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Context&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;requestID&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;dbcaArgs&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; []&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;error&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    uuid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; regexp.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;MustCompile&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;uuid.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;MatchString&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(requestID) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Errorf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;invalid request ID&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    unit &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;oracle-dbca-&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; requestID&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    // The create-only local record is fsync&apos;d before process submission.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    // An existing record is reconciled; it is never interpreted as permission&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    // to launch the command again.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    op, created, err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; CreateOperationIfAbsent&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(requestID, unit, &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;HashPlan&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(dbcaArgs))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Errorf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;persist operation intent: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%w&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, err)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;created {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ReconcileOperation&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, op)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    args &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; []&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;--unit&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, unit,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;--property=User=oracle&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;--property=Group=oinstall&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;--property=RuntimeMaxSec=75min&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;--property=KillMode=control-group&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;--setenv=ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;--setenv=ORACLE_BASE=/u01/app/oracle&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;/u01/app/oracle/product/19.0.0/dbhome_1/bin/dbca&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    args &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; append&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(args, dbcaArgs&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    cmd &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; exec.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;CommandContext&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ctx, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;systemd-run&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, args&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    output, err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; cmd.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;CombinedOutput&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; err &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nil&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;        MarkOutcomeUnknown&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(requestID, output)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; fmt.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Errorf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;create DBCA unit: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%w&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;%s&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, err, output)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; MarkUnitSubmitted&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(requestID)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The controller reads state from systemd, the DBCA logs under &lt;code&gt;$ORACLE_BASE/cfgtoollogs/dbca&lt;/code&gt;, and Oracle itself. A unit exit of zero is necessary but insufficient: reconciliation confirms the CDB identity, open mode, PDB state, component registry, data-file placement, listener registration, and request ownership before reporting success. A timed-out API call leaves the request &lt;code&gt;RUNNING_UNKNOWN&lt;/code&gt;; it never starts a second DBCA invocation until observation proves the first job absent or terminal.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;3-bound-the-secret-exposure-path&quot;&gt;3. Bound the Secret Exposure Path&lt;/h3&gt;
&lt;p&gt;Pull execution does not “completely eliminate” credential exposure. DBCA credential options, response files, process inspection, debug logs, crash dumps, and agent memory are all boundaries that must be designed explicitly:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Generate locally:&lt;/strong&gt; use the operating system CSPRNG and an approved password policy. Associate the generated material with the immutable request ID before database creation begins.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use a documented DBCA credential mechanism:&lt;/strong&gt; prefer an Oracle-supported credential wallet when the target DBCA version supports it. If a response file is required, create it on a root-owned &lt;code&gt;tmpfs&lt;/code&gt;, set &lt;code&gt;0600&lt;/code&gt;, prevent shell tracing and process-argument exposure, and destroy it after DBCA terminates. Do not claim generic stdin support without testing the exact DBCA release.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Write through workload identity:&lt;/strong&gt; authenticate to the enterprise secret manager with an instance or workload identity constrained to one request-scoped path. Use create-only semantics so a retry cannot silently overwrite credentials for an existing database.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Minimize lifetime:&lt;/strong&gt; avoid copying secrets into immutable strings, structured logs, error messages, metrics, or workflow payloads. Memory zeroing is defense in depth, not proof that the runtime or operating system retained no copy.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Verify retrieval before handoff:&lt;/strong&gt; the consumer identity—not a human operator—performs a test retrieval and database authentication. Only then can the control plane remove bootstrap access and mark the credential handoff complete.&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;Persist every transition with optimistic concurrency. The API acknowledges a request only after its intent record exists; the host claims work only with a lease tied to the request generation.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2022-09-05-automating-oracle-builds-dbca-silent-ansible-5-flow.svg&quot; alt=&quot;Recoverable state machine isolates uncertain outcomes into RUNNING_UNKNOWN&quot;&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ACCEPTED -&gt; PLACED -&gt; CLAIMED -&gt; PREFLIGHT_PASSED -&gt; DBCA_RUNNING&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;         -&gt; VERIFYING -&gt; CREDENTIAL_HANDOFF -&gt; READY&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Any nonterminal state -&gt; RUNNING_UNKNOWN -&gt; VERIFYING&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Any verified failure  -&gt; CLEANUP_REQUIRED -&gt; FAILED or ACCEPTED&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;RUNNING_UNKNOWN&lt;/code&gt; is essential. It expresses an epistemic condition rather than inventing an outcome after a timeout. The reconciler checks, in order, the request ledger, host claim, systemd unit, DBCA log, Oracle inventory, database identity, and storage artifacts. It moves to &lt;code&gt;FAILED&lt;/code&gt; only when a terminal failure is observed, and to &lt;code&gt;READY&lt;/code&gt; only when all readiness invariants hold.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Oracle documents DBCA as the supported assistant for creating and configuring a database, including a silent command interface and response-file mode. Oracle also warns that response files can contain clear-text passwords and should be restricted or removed when not in use. Those two documented facts establish the operational boundary: DBCA performs the local database mutation; a platform control plane must own request identity, policy, credentials, retries, and proof of completion.&lt;/p&gt;
&lt;p&gt;The idempotency key must cover the desired database identity, not only an HTTP attempt. Enforce uniqueness for the request ID, target host, DB unique name, SID, and storage namespace. A client retry returns the existing operation. A changed specification creates a new generation and requires the old generation to be terminal; it must not mutate an active request in place.&lt;/p&gt;
&lt;h3 id=&quot;separate-preconditions-execution-and-readiness&quot;&gt;Separate Preconditions, Execution, and Readiness&lt;/h3&gt;
&lt;p&gt;The preflight should emit a signed or immutable evidence document that includes the Oracle home version, RU level, service class revision, CPU and memory observations, HugePages and &lt;code&gt;memlock&lt;/code&gt; capacity, ASM disk-group state and usable space, listener ownership, filesystem headroom, required OS groups, and selected DBCA syntax. The agent then hashes the exact non-secret plan and stores that digest in the request ledger. This makes later questions—“which policy produced this database?”—answerable without retaining credentials.&lt;/p&gt;
&lt;p&gt;Execution evidence is different from readiness evidence. Capture the transient unit name, systemd invocation ID, DBCA start time, exit status, and the relevant &lt;code&gt;$ORACLE_BASE/cfgtoollogs/dbca&lt;/code&gt; location. Do not scrape a percentage from human-oriented output and treat it as a transaction boundary. Progress is advisory; observed state is authoritative.&lt;/p&gt;
&lt;p&gt;After DBCA exits, use explicit probes such as these, adapted to the topology:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify the expected database resource exists in Oracle Restart or Clusterware where those components own it; otherwise verify the intended instance and configuration files directly.&lt;/li&gt;
&lt;li&gt;Connect locally with controlled administrative authentication and compare &lt;code&gt;DBID&lt;/code&gt;, &lt;code&gt;DB_UNIQUE_NAME&lt;/code&gt;, CDB status, database role, and open mode to the request.&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;V$PDBS&lt;/code&gt; for the requested PDB and desired open mode, and configure saved state deliberately rather than assuming it persisted.&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;DBA_REGISTRY&lt;/code&gt; for invalid or loading components. A clean process exit does not prove catalog components are valid.&lt;/li&gt;
&lt;li&gt;Confirm data files, online redo, control files, and recovery destinations are on the approved storage namespaces; also verify usable capacity after allocation.&lt;/li&gt;
&lt;li&gt;Verify service registration through the listener and perform a connection using the consumer service name. Do not certify readiness through the default administrative service.&lt;/li&gt;
&lt;li&gt;Retrieve the credential through the intended workload identity and perform a minimal authentication test. Revoke bootstrap access only after this succeeds.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;These probes form the completion predicate. Store their results and timestamps as structured evidence, while excluding SQL text, connect descriptors, or error payloads that may contain secrets.&lt;/p&gt;
&lt;h3 id=&quot;test-the-failure-semantics-not-just-the-happy-path&quot;&gt;Test the Failure Semantics, Not Just the Happy Path&lt;/h3&gt;
&lt;p&gt;A staging qualification should inject faults at the boundaries where ownership changes. Terminate the API after intent persistence but before its response; restart the host agent immediately before and during DBCA; block control-plane traffic while the unit continues; expire the work lease; make the secret manager unavailable after database creation; fill a dedicated log filesystem; and deliver the same request repeatedly. For every test, assert both safety and liveness:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Safety:&lt;/strong&gt; no duplicate DB identity, no credential overwrite, no deletion of an unowned database, and no second DBCA process for one generation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Liveness:&lt;/strong&gt; once dependencies recover, reconciliation either reaches &lt;code&gt;READY&lt;/code&gt; or produces a terminal, operator-actionable state with retained evidence.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Capacity testing should measure the actual shared constraints: ASM allocation and rebalance load, image or template reads, secret-manager quotas, cloud API rate limits, listener registration, controller database contention, and log ingestion. “Fifty concurrent builds” is not a transferable target. Admission control should allocate weighted tokens by service class and failure domain, then reduce concurrency automatically when storage latency, API throttling, or verification time crosses a reviewed threshold.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2022-09-05-automating-oracle-builds-dbca-silent-ansible-6-failures.svg&quot; alt=&quot;Lease expiry, secret write timeouts, and HugePages mismatches break naive builders&quot;&gt;&lt;/p&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Operational Scenario / Environmental Edge Case&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Failure Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Architectural Consequence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation Protocol&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;CSPRNG unavailable early in boot&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;An image starts the agent before the kernel random pool is initialized, so credential generation blocks or fails.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The request remains claimed without a credential or DBCA process, and an unsafe retry may create a second secret.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use the operating-system CSPRNG, surface &lt;code&gt;getrandom&lt;/code&gt; failure as a preflight failure, release the claim safely, and fix image or boot ordering; do not substitute a weak userspace generator.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;ASM access or capacity mismatch&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The Oracle software owner lacks the required ASM access, the requested disk group is not mounted, or usable space is below the plan.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;DBCA fails before or during file creation and may leave request-owned storage artifacts.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Query ASM state and usable capacity in preflight, validate approved OS-group membership, and record every request-owned artifact for guarded cleanup.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Partition outlives the work lease&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The host continues DBCA while the control plane cannot renew or observe its claim.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;A naive scheduler assigns the same identity elsewhere or terminates a host whose outcome is unknown.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Make identity unique globally, move expired claims to &lt;code&gt;RUNNING_UNKNOWN&lt;/code&gt;, forbid reassignment while host evidence is unresolved, and require a fenced recovery decision before termination.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Cloud identity bootstrap fails&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Metadata, instance-principal, DNS, or time synchronization is unavailable during boot.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The agent cannot authenticate to claim work or write the generated credential.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use the cloud’s authenticated metadata mode, bounded jittered retries, clock-health checks, and a host-local pending state; never fall back to an embedded long-lived credential.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;HugePages, &lt;code&gt;memlock&lt;/code&gt;, NUMA, or cgroup limit mismatch&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The service-class SGA fits physical RAM but not the effective locked-memory or cgroup envelope.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Instance startup fails or silently uses an unintended memory mode with different performance characteristics.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Validate effective limits and HugePages against the selected Oracle configuration, reserve OS headroom, and reject rather than auto-resize the database.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Listener ownership or service collision&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;A DBCA plan attempts to create or modify a listener already managed by Grid Infrastructure or another Oracle home.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Registration is inconsistent, a port bind fails, or an unrelated service is disrupted.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Discover the listener owner and endpoints, use role-appropriate services, and make network configuration a separately owned reconciled resource.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Secret write succeeds but acknowledgement is lost&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The secret manager commits a value and the agent times out before receiving the response.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Retrying a normal write can overwrite the credential with a value that no longer matches the database.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use a request-scoped create-only operation, retain the credential until read-back is proven, and reconcile metadata or version before deciding whether to retry.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Log or Oracle-base filesystem fills&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;DBCA, audit, diagnostic, or journald output consumes a shared filesystem during catalog creation.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Writes fail, evidence is truncated, and colocated Oracle components can be affected.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Isolate and size log destinations, enforce quotas and retention, alert on both bytes and inodes, and stop admission before the reserve needed for diagnosis is consumed.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id=&quot;september-2026-update&quot;&gt;September 2026 Update&lt;/h2&gt;
&lt;p&gt;The architectural boundary still matters even when the provisioning primitive changes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;PDB clone paths reduce work but add lineage constraints.&lt;/strong&gt; Oracle supports local and remote PDB clones, refreshable clone PDBs, PDB snapshots, and storage-managed snapshot-copy PDBs subject to version, storage, open-mode, and licensing constraints. A warm CDB pool can therefore avoid building a CDB for every tenant, but the control plane must track source lineage, patch compatibility, storage dependencies, service naming, refresh state, and isolation policy. Provisioning latency must be measured on the chosen storage path; there is no universal seconds-level guarantee.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;An operator is an implementation of reconciliation, not its substitute.&lt;/strong&gt; Oracle’s Database Operator for Kubernetes exposes documented custom resources for supported database lifecycle operations. Its exact database versions, topology, Kubernetes versions, storage classes, and day-two capabilities must be checked against the release being adopted. A team still owns idempotency, secrets, backup, recovery objectives, observability, upgrades, and the boundary between Kubernetes state and Oracle state.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Prebuilt images move validation earlier.&lt;/strong&gt; A patched and tested database-home image can remove installation variance, but it does not validate runtime ASM, HugePages, listener ownership, credentials, or catalog health. Record the image digest and patch inventory as inputs to the service class, then retain the same post-provision completion predicate.&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Decouple intent from execution transport:&lt;/strong&gt; Persist desired state in a fleet catalog and run DBCA inside a uniquely named &lt;code&gt;systemd-run&lt;/code&gt; unit so SSH or API drops never orphan or duplicate builds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Model uncertainty with &lt;code&gt;RUNNING_UNKNOWN&lt;/code&gt;:&lt;/strong&gt; On timeout or lease expiry, inspect the systemd unit, DBCA logs, and Oracle inventory before retrying or reassigning.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Validate hardware against policy, not percentages:&lt;/strong&gt; Reject builds in preflight when RAM, HugePages, &lt;code&gt;memlock&lt;/code&gt;, or ASM disk groups fail the approved service class.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scope secrets with create-only writes:&lt;/strong&gt; Generate credentials locally with the OS CSPRNG, isolate any response file on &lt;code&gt;0600&lt;/code&gt; &lt;code&gt;tmpfs&lt;/code&gt;, and verify consumer read-back before revoking bootstrap access.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Prove readiness via Oracle views:&lt;/strong&gt; A zero exit code is insufficient; verify &lt;code&gt;DBID&lt;/code&gt;, &lt;code&gt;V$PDBS&lt;/code&gt;, &lt;code&gt;DBA_REGISTRY&lt;/code&gt;, and listener service registration before marking &lt;code&gt;READY&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Inventory every point where a lost response can make the current pipeline confuse “failed” with “still running” or “committed,” including DBCA, storage, identity, and secret writes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Persist an immutable request and generation, execute through a host-local service manager, and reconcile Oracle-observed state against explicit readiness invariants before acknowledging completion.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; In a non-production failure campaign, kill the API and agent, partition the host, duplicate request delivery, expire credentials, and exhaust log capacity; verify one database identity and a recoverable terminal state after every test.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Implement &lt;code&gt;RUNNING_UNKNOWN&lt;/code&gt;, create-only secret writes, policy-versioned service classes, admission tokens, and an evidence record before increasing provisioning concurrency.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/admin/creating-and-configuring-an-oracle-database.html&quot;&gt;Oracle Database 19c Administrator’s Guide — creating and configuring a database with DBCA&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/ladbi/running-database-configuration-assistant-using-response-files.html&quot;&gt;Oracle Database 19c Linux Installation Guide — running DBCA with response files&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/racad/troubleshooting-oracle-rac.html&quot;&gt;Oracle Database 19c RAC Administration and Deployment Guide — DBCA trace locations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/multi/cloning-a-pdb.html&quot;&gt;Oracle Database 19c Multitenant Administrator’s Guide — cloning a PDB&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.freedesktop.org/software/systemd/man/latest/systemd-run.html&quot;&gt;systemd-run manual — transient service and scope units&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html&quot;&gt;systemd.service manual — service lifetime, restart, and timeout semantics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://pkg.go.dev/os/exec&quot;&gt;Go os/exec package — context cancellation and process execution&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/oracle/oracle-database-operator&quot;&gt;Oracle Database Operator for Kubernetes — official source and documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category><category>checklist</category></item><item><title>From OPatch to Immutable Oracle Homes: Why Out-of-Place Patching Wins</title><link>https://rajivonai.com/blog/2022-04-12-oracle-immutable-homes-out-of-place-patching/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-04-12-oracle-immutable-homes-out-of-place-patching/</guid><description>Why in-place OPatch execution creates unpredictable maintenance downtime, and how out-of-place patching transforms Oracle database lifecycle operations into deterministic, instantly revertible deployments.</description><pubDate>Fri, 01 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Patching the active Oracle Home couples binary mutation, relinking, inventory updates, database downtime, and recovery into one maintenance-window transaction; an out-of-place home moves most of that uncertainty into a testable build artifact, but rollback is safe only when binary and SQL patch state are designed together.&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Running &lt;code&gt;opatch apply&lt;/code&gt; against the active &lt;code&gt;$ORACLE_HOME&lt;/code&gt; puts archive extraction, conflict detection, relinking, and inventory writes inside the production downtime window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Build and verify the next &lt;code&gt;$ORACLE_HOME&lt;/code&gt; offline with &lt;code&gt;runInstaller -applyRU&lt;/code&gt;, then use the maintenance window only to stop instances, pivot &lt;code&gt;srvctl&lt;/code&gt; or &lt;code&gt;/etc/oratab&lt;/code&gt;, start on the new home, and run &lt;code&gt;datapatch&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out:&lt;/strong&gt; Preserving the old binary home does not make SQL changes automatically reversible—once &lt;code&gt;datapatch&lt;/code&gt; mutates &lt;code&gt;CDB_REGISTRY_SQLPATCH&lt;/code&gt;, rollback requires the patch README’s SQL reconciliation or a backup restore.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&quot;./2022-04-12-oracle-immutable-homes-out-of-place-patching.svg&quot; alt=&quot;Build and verify the next Oracle Home before downtime begins&quot;&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Production Context: 2022 | Enterprise Oracle Infrastructure | Technical Review: September 2026&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Enterprise database administration teams responsible for mission-critical Oracle database estates operate under continuous pressure to apply quarterly Release Updates (RUs), security vulnerability fixes (Critical Patch Updates / CPUs), and interim bug fixes. In organizations operating dozens of standalone servers, Oracle Real Application Clusters (RAC), and Exadata machines, maintaining binary currency is mandatory to eliminate software bugs, patch Common Vulnerabilities and Exposures (CVEs), and retain Oracle Premier Support.&lt;/p&gt;
&lt;p&gt;Historically, the universal standard operating procedure for Oracle patching was &lt;strong&gt;In-Place Patching&lt;/strong&gt;. When an RU arrived, the Database Administrator (DBA) scheduled a weekend maintenance window, notified business stakeholders of planned downtime, brought down the production database instances and listeners running out of &lt;code&gt;$ORACLE_HOME&lt;/code&gt;, and invoked the &lt;code&gt;opatch apply&lt;/code&gt; utility directly against the active software directory (e.g., &lt;code&gt;/u01/app/oracle/product/19.0.0/dbhome_1&lt;/code&gt;). The OPatch utility decompressed patch archives, interrogated the central Oracle Inventory (&lt;code&gt;oraInventory&lt;/code&gt;), extracted modified object files (&lt;code&gt;.o&lt;/code&gt;), linked new executables using the operating system linker (&lt;code&gt;ld&lt;/code&gt; via makefiles such as &lt;code&gt;ins_rdbms.mk&lt;/code&gt;), and updated local inventory metadata.&lt;/p&gt;
&lt;p&gt;When the binary patching phase concluded without errors, the DBA started the database instances in upgrade mode and executed the SQL patch utility (&lt;code&gt;datapatch&lt;/code&gt;) to apply data dictionary transformations into the database catalog.&lt;/p&gt;
&lt;p&gt;While functional in small, single-instance dev environments, this in-place patching model introduces massive operational fragility when scaled across enterprise fleets.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;h3 id=&quot;the-non-deterministic-failure-modes-of-in-place-binary-patching&quot;&gt;The Non-Deterministic Failure Modes of In-Place Binary Patching&lt;/h3&gt;
&lt;p&gt;Executing in-place binary patching during live production maintenance windows creates acute operational risks due to the stateful, mutative nature of traditional software updates:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Binary Mutation Under Time Pressure:&lt;/strong&gt; A Release Update replaces libraries, object files, and patch metadata and may invoke relinking for affected executables. When &lt;code&gt;opatch apply&lt;/code&gt; mutates the active home, prerequisite checks, file replacement, inventory writes, and any relink happen inside the outage. CPU contention, full staging filesystems, permissions, or missing operating-system libraries can therefore consume the recovery budget while the database is unavailable.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Patch Conflict Paralysis:&lt;/strong&gt; In enterprise environments, production database homes frequently contain one-off interim patches applied over several years to remediate specific vendor bugs. When a new Quarterly Release Update is applied in-place, OPatch frequently detects binary conflicts: a modified module in the new RU conflicts with an existing one-off patch. OPatch halts immediately with an error (&lt;code&gt;Conflict detected with patch...&lt;/code&gt;). At 02:00 AM on a Sunday, the on-call DBA is faced with an impossible choice: abort the patching and attempt to roll back, or open an emergency Sev-1 ticket with Oracle Support requesting an urgent “Merge Patch,” leaving the database offline for hours while waiting for vendor engineers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A Coupled Rollback Path:&lt;/strong&gt; &lt;code&gt;opatch rollback -id &amp;#x3C;patch_number&gt;&lt;/code&gt; depends on the active home’s inventory and &lt;code&gt;.patch_storage&lt;/code&gt; state. A rollback can also cross two independent boundaries: restoring binaries and reconciling SQL changes recorded in &lt;code&gt;DBA_REGISTRY_SQLPATCH&lt;/code&gt;. Reverting only one side can leave an unsupported binary/catalog combination.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Unbounded Planned Downtime:&lt;/strong&gt; Because prerequisite validation, file mutation, inventory updates, and relinking occur after shutdown, the outage duration inherits every host-specific difference. A defensible window must be based on rehearsal measurements and a tested restore path, not a universal three-hour estimate.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;./2022-04-12-oracle-immutable-homes-out-of-place-patching-2-constraints.svg&quot; alt=&quot;In-place patching traps binary build failures inside the production outage&quot;&gt;&lt;/p&gt;
&lt;p&gt;The central engineering challenge is: &lt;strong&gt;How can database platform architects move binary construction and conflict resolution out of the production outage while preserving a tested, version-compatible rollback for both the Oracle Home and the database catalog?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;options-considered&quot;&gt;Options Considered&lt;/h2&gt;
&lt;p&gt;Four patching strategies define the operational trade-offs for Oracle 19c fleets.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Option&lt;/th&gt;&lt;th&gt;Conflict Isolation&lt;/th&gt;&lt;th&gt;Rollback Boundary&lt;/th&gt;&lt;th&gt;Inventory Overhead&lt;/th&gt;&lt;th&gt;Best Fit&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;In-Place &lt;code&gt;opatch apply&lt;/code&gt;&lt;/td&gt;&lt;td&gt;None — discovered during outage&lt;/td&gt;&lt;td&gt;Coupled to &lt;code&gt;.patch_storage&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Low&lt;/td&gt;&lt;td&gt;Rejected for production&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Manual Home Clone&lt;/td&gt;&lt;td&gt;Offline&lt;/td&gt;&lt;td&gt;Prone to manual &lt;code&gt;oraInventory&lt;/code&gt; drift&lt;/td&gt;&lt;td&gt;High&lt;/td&gt;&lt;td&gt;Small static estates only&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Gold Image + &lt;code&gt;runInstaller -applyRU&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Offline + signed artifact&lt;/td&gt;&lt;td&gt;Clean &lt;code&gt;N-1&lt;/code&gt; home + SQL check&lt;/td&gt;&lt;td&gt;Moderate&lt;/td&gt;&lt;td&gt;Universal standalone and RAC&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Oracle Fleet Patching &amp;#x26; Provisioning (FPP)&lt;/td&gt;&lt;td&gt;Managed working copies&lt;/td&gt;&lt;td&gt;Built-in &lt;code&gt;rhpctl&lt;/code&gt; switchback&lt;/td&gt;&lt;td&gt;Low at scale&lt;/td&gt;&lt;td&gt;Large Grid Infrastructure fleets&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;./2022-04-12-oracle-immutable-homes-out-of-place-patching-3-options.svg&quot; alt=&quot;Automated out-of-place gold images eliminate conflict risk at fleet scale&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In-place &lt;code&gt;opatch apply&lt;/code&gt;&lt;/strong&gt; minimizes disk usage because only one &lt;code&gt;$ORACLE_HOME&lt;/code&gt; exists per release, but it was rejected because any conflict, disk-full condition, or relink failure extends production downtime directly. &lt;strong&gt;Manual home cloning&lt;/strong&gt; moves binary work offline, but copying directories by hand without central-inventory registration and checksum verification creates silent drift between hosts. &lt;strong&gt;Oracle FPP&lt;/strong&gt; (&lt;code&gt;rhpctl&lt;/code&gt;) is the strongest managed option for large Grid Infrastructure and RAC estates, though it requires running an FPP server infrastructure. &lt;strong&gt;Signed gold images with &lt;code&gt;runInstaller -applyRU&lt;/code&gt;&lt;/strong&gt; deliver the same immutable-home guarantees across standalone, Oracle Restart, and RAC deployments using standard Oracle installer primitives.&lt;/p&gt;
&lt;h2 id=&quot;immutable-oracle-homes-the-out-of-place-architecture&quot;&gt;Immutable Oracle Homes: The Out-of-Place Architecture&lt;/h2&gt;
&lt;p&gt;The engineering solution is to treat the &lt;code&gt;$ORACLE_HOME&lt;/code&gt; not as a mutable pet, but as an &lt;strong&gt;Immutable Binary Artifact&lt;/strong&gt;. Instead of modifying the active production software directory, the platform adopts &lt;strong&gt;Out-of-Place Patching&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Platform engineers build, patch, relink, and validate a new, isolated &lt;code&gt;$ORACLE_HOME&lt;/code&gt; days before the maintenance window. The running database remains on the current home. During the outage, automation stops services, changes the Clusterware or standalone-home registration, starts from the target home, runs &lt;code&gt;datapatch&lt;/code&gt;, and verifies application services. This is a controlled state transition, not an atomic filesystem-pointer swap: Grid Infrastructure metadata, listeners, SQL patch state, and application routing are separate failure domains.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2022-04-12-oracle-immutable-homes-out-of-place-patching-4-architecture.svg&quot; alt=&quot;Separate immutable binary homes from host configuration, Clusterware metadata, and SQL registry&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;1-the-pre-maintenance-build-and-verification-phase&quot;&gt;1. The Pre-Maintenance Build and Verification Phase&lt;/h3&gt;
&lt;p&gt;All heavy lifting—unzipping archives, checking prerequisites, resolving patch conflicts, and compiling binary code—takes place in the background days prior to scheduled maintenance:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# -------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Out-of-Place Oracle Home Provisioning Script&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Target: Oracle Database 19c RU 19.15.0.0.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# -------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ORACLE_BASE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;/u01/app/oracle&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; OLD_HOME&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;/u01/app/oracle/product/19.12.0/dbhome_1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; NEW_HOME&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;/u01/app/oracle/product/19.15.0/dbhome_1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PATCH_DIR&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;/u01/stage/patches/19.15&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mkdir&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $NEW_HOME&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $NEW_HOME&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# 1. Extract Base 19.3.0.0 Software Image&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;unzip&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -q&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /u01/stage/LINUX.X64_193000_db_home.zip&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $NEW_HOME&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# 2. Setup Oracle Home and Apply RU 19.15 via runInstaller in Silent Mode&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;./runInstaller&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -silent&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -waitforcompletion&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -applyRU&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $PATCH_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/33806152&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -applyOneOffs&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $PATCH_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/33803476&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  ORACLE_BASE=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$ORACLE_BASE &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  ORACLE_HOME=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$NEW_HOME &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  ORACLE_HOME_NAME=OraDB19Home_1915&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  oracle.install.db.InstallEdition=EE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# 3. Verify Inventory and Relink Binaries&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$NEW_HOME/OPatch/opatch lsinventory -detail&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$NEW_HOME/bin/relink all&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# 4. Capture evidence used by the change gate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$NEW_HOME/OPatch/opatch lspatches&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$NEW_HOME/bin/sqlplus -V&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ldd&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $NEW_HOME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/bin/oracle&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; grep&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;not found&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &amp;#x26;&amp;#x26; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;exit&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The production pipeline must additionally verify the image checksum and signature, confirm the supported OPatch version from the RU README, run the installer-required root scripts, and record the central-inventory entry. Patch numbers in examples are not portable configuration: the pipeline must resolve the correct RU, OJVM, and interim patches from the approved manifest for the target platform and quarter.&lt;/p&gt;
&lt;h4 id=&quot;why-conflict-risk-is-neutralized&quot;&gt;Why Conflict Risk is Neutralized&lt;/h4&gt;
&lt;p&gt;If a patch conflict occurs during Step 2, &lt;strong&gt;zero production downtime has occurred&lt;/strong&gt;. The live database continues running on &lt;code&gt;$OLD_HOME&lt;/code&gt;. The DBA can open a ticket with Oracle Support, request a merge patch, or adjust the patch payload during normal business hours without any operational emergency.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;2-the-maintenance-window-execution-protocol&quot;&gt;2. The Maintenance Window Execution Protocol&lt;/h3&gt;
&lt;p&gt;During the actual maintenance window, the database is offline only for the time required to stop the instance, swap paths, start the instance, and execute SQL dictionary scripts:&lt;/p&gt;
&lt;h4 id=&quot;step-1-quiesce-and-database-shutdown&quot;&gt;Step 1: Quiesce and Database Shutdown&lt;/h4&gt;
&lt;p&gt;Client connections are drained via connection pools, and the database is shut down cleanly:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SHUTDOWN&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IMMEDIATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;step-2-clusterware-aware-home-pivot&quot;&gt;Step 2: Clusterware-Aware Home Pivot&lt;/h4&gt;
&lt;p&gt;For a standalone database, &lt;code&gt;/etc/oratab&lt;/code&gt; and service-unit environments may define the active home. For Oracle Restart or RAC, Clusterware owns that state; editing &lt;code&gt;/etc/oratab&lt;/code&gt; alone does not move the resource. The runbook records the current configuration before changing it:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Capture rollback evidence&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; config&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; database&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -db&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; DB19C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Oracle Restart or RAC — execute with the documented owner and version-specific syntax&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;srvctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; modify&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; database&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -db&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; DB19C&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -oraclehome&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$NEW_HOME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Standalone deployments only: update an exact, validated oratab entry&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;cp&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -p&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /etc/oratab&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /etc/oratab.pre-1915&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sed&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -i&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;s|^DB19C:/u01/app/oracle/product/19.12.0/dbhome_1:|DB19C:/u01/app/oracle/product/19.15.0/dbhome_1:|&apos;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /etc/oratab&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Source the new environment&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;export&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ORACLE_SID&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DB19C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /usr/local/bin/oraenv&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;step-3-database-startup-and-sql-patch-execution&quot;&gt;Step 3: Database Startup and SQL Patch Execution&lt;/h4&gt;
&lt;p&gt;The database mounts using the new binaries, and &lt;code&gt;datapatch&lt;/code&gt; runs to apply SQL catalog changes:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sqlplus&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; as&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; sysdba&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;STARTUP;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EXIT;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Execute SQL patch utility&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $ORACLE_HOME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/OPatch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;./datapatch&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -verbose&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Recompile invalid objects&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sqlplus&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; as&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; sysdba&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;@?/rdbms/admin/utlrp.sql&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EXIT;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;EOF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The maintenance outage is now dominated by shutdown, resource relocation, startup, SQL patching, and validation rather than construction of the Oracle Home.&lt;/p&gt;
&lt;p&gt;That duration is an acceptance target, not a property of out-of-place patching. &lt;code&gt;datapatch&lt;/code&gt; runtime depends on component inventory, PDB count, invalid objects, and SQL patch payload. Production scheduling must use rehearsal percentiles for the same topology and must reserve time for service validation and a rollback decision.&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&quot;3-the-version-compatible-rollback-path&quot;&gt;3. The Version-Compatible Rollback Path&lt;/h3&gt;
&lt;p&gt;Out-of-place patching preserves the previous binary home, but it does not make every rollback instantaneous. Before the change, the team must classify rollback boundaries from the RU README and rehearse them on a production-like clone:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Capture &lt;code&gt;opatch lspatches&lt;/code&gt;, &lt;code&gt;DBA_REGISTRY_SQLPATCH&lt;/code&gt;, database component status, Clusterware configuration, and application-service placement.&lt;/li&gt;
&lt;li&gt;Define the last point at which a binary-only reversal is supported. Once &lt;code&gt;datapatch&lt;/code&gt; changes the catalog, use the patch-specific SQL rollback procedure; do not label it optional.&lt;/li&gt;
&lt;li&gt;Stop application admission before changing homes. A write admitted after the pivot may make storage or application rollback—not merely binary rollback—the relevant recovery path.&lt;/li&gt;
&lt;li&gt;Restore the Clusterware resource or standalone configuration to &lt;code&gt;$OLD_HOME&lt;/code&gt;, reconcile SQL patch state using the documented procedure, start services, and verify &lt;code&gt;DBA_REGISTRY&lt;/code&gt;, &lt;code&gt;DBA_REGISTRY_SQLPATCH&lt;/code&gt;, alert logs, listeners, and synthetic transactions.&lt;/li&gt;
&lt;li&gt;If the patch README marks SQL changes irreversible, or validation cannot establish a supported binary/catalog pairing, restore the rehearsed database backup or snapshot instead of improvising an OPatch sequence.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The rollback service-level objective is therefore measured from rehearsals. The defensible guarantee is that the old binary artifact is preserved and the recovery procedure is known—not that every database returns in three minutes.&lt;/p&gt;
&lt;h2 id=&quot;how-it-rolls-out&quot;&gt;How It Rolls Out&lt;/h2&gt;
&lt;p&gt;The control plane should persist the patch transition independently of the shell process executing it. Modeling the rollout as explicit states—&lt;code&gt;IMAGE_VERIFIED&lt;/code&gt;, &lt;code&gt;RESOURCE_STOPPED&lt;/code&gt;, &lt;code&gt;HOME_REGISTERED&lt;/code&gt;, &lt;code&gt;SQL_PATCHED&lt;/code&gt;, and &lt;code&gt;VALIDATED&lt;/code&gt;—ensures that a retry observes reality before acting and never assumes a timed-out SSH or &lt;code&gt;srvctl&lt;/code&gt; call failed.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./2022-04-12-oracle-immutable-homes-out-of-place-patching-5-flow.svg&quot; alt=&quot;Model the home pivot as explicit persisted states with clear rollback gates&quot;&gt;&lt;/p&gt;
&lt;p&gt;Each transition records its command, start and finish timestamps, exit status, and evidence URI. Three invariants prevent the most damaging partial states:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;One admitted writer:&lt;/strong&gt; application services remain disabled until the intended database role is read-write, the expected home owns the running processes, and synthetic writes commit successfully.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Binary/catalog compatibility:&lt;/strong&gt; the active home’s &lt;code&gt;opatch lspatches&lt;/code&gt; output and the successful rows in &lt;code&gt;CDB_REGISTRY_SQLPATCH&lt;/code&gt; match the approved manifest for every open PDB. A successful instance startup is not sufficient evidence.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Recoverable old home:&lt;/strong&gt; the fallback home remains read-only to automation, registered or re-registerable, and covered by a rehearsed SQL reconciliation or database-restore procedure until the observation window closes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The post-start gate should collect evidence rather than return a single Boolean:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, open_mode, database_role &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; v$&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;database&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; con_id, patch_id, patch_type, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;action&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, action_time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;   cdb_registry_sqlpatch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER  BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; action_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; con_id, comp_id, comp_name, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;   cdb_registry&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;VALID&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER  BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; con_id, comp_id;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; con_id, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, open_mode, restricted&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;   v$pdbs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER  BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; con_id;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The operating-system side records &lt;code&gt;ps -ef&lt;/code&gt; executable paths, &lt;code&gt;srvctl config database&lt;/code&gt;, &lt;code&gt;srvctl status database -verbose&lt;/code&gt;, listener service registration, alert-log errors since startup, and filesystem capacity for both homes. Application validation must include authentication, a transaction with commit and read-after-write, connection-pool recycling, and the workload’s highest-risk SQL paths. Only after these independent signals pass should the orchestrator enable normal admission.&lt;/p&gt;
&lt;p&gt;The observation window is also explicit. During it, alerting compares parse failures, invalid objects, &lt;code&gt;ORA-00600&lt;/code&gt; or &lt;code&gt;ORA-07445&lt;/code&gt; incidents, logon rate, database time, and critical SQL plan hashes against the pre-change baseline. Crossing a predefined threshold disables further fleet rollout and triggers the rehearsed rollback decision; it does not automatically run destructive rollback commands on every symptom.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Oracle’s documented image-based installation and Fleet Patching and Provisioning workflows establish the production pattern without requiring an invented estate or invented success metrics. &lt;code&gt;runInstaller -createGoldImage&lt;/code&gt; packages an Oracle Home image; Fleet Patching and Provisioning models software homes as managed gold images and deploys working copies to targets. The operational conclusion is derived from those documented mechanics: construct and validate the home before downtime, then move the database resource to a known artifact.&lt;/p&gt;
&lt;p&gt;A production evidence pack should make that conclusion testable for each fleet:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Artifact provenance:&lt;/strong&gt; record base-image checksum, RU and one-off identifiers, OPatch version, build host OS release, compiler/runtime dependencies, signing identity, and immutable object-store URI.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compatibility evidence:&lt;/strong&gt; run &lt;code&gt;opatch prereq CheckConflictAgainstOHWithDetail&lt;/code&gt;, compare &lt;code&gt;opatch lsinventory&lt;/code&gt;, validate the RU README’s platform prerequisites, and test every installed option used by the target database.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Database rehearsal:&lt;/strong&gt; restore a recent production-like clone, attach it to the candidate home, run &lt;code&gt;datapatch -verbose&lt;/code&gt;, open every PDB, compile invalid objects, and execute workload-specific smoke tests. A successful binary install alone is not a database validation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Measured decision thresholds:&lt;/strong&gt; retain stop, start, &lt;code&gt;datapatch&lt;/code&gt;, listener-registration, PDB-open, and synthetic-transaction durations separately. Set the maintenance window and abort point from observed percentiles rather than universal claims.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rollback proof:&lt;/strong&gt; rehearse the exact old-home restoration and SQL reconciliation path. Save the commands and outputs as change evidence, including the conditions that escalate to database restore.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;./2022-04-12-oracle-immutable-homes-out-of-place-patching-6-failures.svg&quot; alt=&quot;Disk exhaustion, inventory locks, and catalog divergence break out-of-place rollouts&quot;&gt;&lt;/p&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Operational Scenario / Environmental Edge Case&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Failure Mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Architectural Consequence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation Protocol&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Storage Exhaustion on &lt;code&gt;/u01&lt;/code&gt; Mount Point&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Storing multiple 12 GB Oracle homes fills the root or software filesystem to 100%.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Server runs out of disk space; cannot write logs or create trace files; database crashes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Implement automated home lifecycle cleanup; strictly retain only active home ($N$) and previous fallback home ($N-1$).&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Central Inventory (&lt;code&gt;inventory.xml&lt;/code&gt;) Lock Contention&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Multiple automated scripts attempt to register new homes concurrently against the global inventory.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Inventory lock acquisition times out; &lt;code&gt;runInstaller&lt;/code&gt; fails with &lt;code&gt;InventoryException&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Serialize inventory registration steps or utilize local per-home inventories using &lt;code&gt;-invPtrLoc&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Static Listener Configuration Desynchronization&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Database listener configured with static SID descriptors pointing to the old &lt;code&gt;$ORACLE_HOME&lt;/code&gt; path.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Connections succeed to database, but external tools and OEM fail to resolve service handlers.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Standardize on dynamic service registration via &lt;code&gt;LOCAL_LISTENER&lt;/code&gt;; update static listener entries in automation.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Datapatch Catalog Failure&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Component errors, closed PDBs, invalid prerequisites, or patch SQL failures prevent catalog reconciliation.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Binaries and SQL registry diverge; opening applications risks operating an unsupported state.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run clone rehearsals and component checks; retain &lt;code&gt;datapatch&lt;/code&gt; logs; stop application admission until registry and PDB validation pass.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Hardcoded Environment Paths in Cron Scripts&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Legacy cron jobs and shell scripts hardcode &lt;code&gt;/u01/.../19.12.0/dbhome_1&lt;/code&gt; in their script headers.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Scripts continue executing against the inactive, un-patched home after the database pivots.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Ban hardcoded paths; mandate that all operational scripts source &lt;code&gt;/usr/local/bin/oraenv&lt;/code&gt; dynamically.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;OJVM Patch-Mode Mismatch&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Automation assumes an OJVM patch is rolling without checking the patch README and RAC mode prerequisites.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Nodes or catalogs enter incompatible patch states and the rolling plan becomes invalid.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Derive rolling eligibility and SQL steps from the exact OJVM README; test the same RAC topology before production.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Missing Kernel OS Shared Library Symlinks&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;New home compiled on a build server with slightly different OS glibc or patch levels.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Executables fail to mount on target hosts due to missing shared object dependencies (&lt;code&gt;libaio.so.1&lt;/code&gt;).&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce strict operating system gold image parity via Terraform and Ansible across all database hosts.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Read-Only Oracle Home (&lt;code&gt;roohctl&lt;/code&gt;) Path Divergence&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Utilizing Read-Only Oracle Home where configuration files (&lt;code&gt;dbs&lt;/code&gt;, &lt;code&gt;network/admin&lt;/code&gt;) live outside the home in &lt;code&gt;$ORACLE_BASE_HOME&lt;/code&gt; and &lt;code&gt;$ORACLE_BASE_CONFIG&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Pointer scripts fail to locate parameter files (&lt;code&gt;spfile&lt;/code&gt;) during startup from the new directory.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Standardize on &lt;code&gt;roohctl -enable&lt;/code&gt; before DB creation and resolve paths via &lt;code&gt;orabasehome&lt;/code&gt; / &lt;code&gt;orabaseconfig&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id=&quot;april-2026-update&quot;&gt;April 2026 Update&lt;/h2&gt;
&lt;p&gt;Advancements in Oracle database lifecycle management and cloud automation between 2022 and 2026 have made out-of-place patching the mandated industry standard:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Read-Only Oracle Homes:&lt;/strong&gt; Oracle supports a read-only home configuration that relocates instance-specific files under &lt;code&gt;ORACLE_BASE&lt;/code&gt;. It improves separation between software and configuration, but it is an installation choice with documented path changes—not a claim that every 19c home is immutable by default.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fleet Patching and Provisioning:&lt;/strong&gt; FPP manages gold images and working copies and provides version-aware fleet operations for supported Oracle Database and Grid Infrastructure deployments. It reduces custom orchestration, but change owners still validate database state, application services, and rollback boundaries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Artifact attestation:&lt;/strong&gt; Modern pipelines should add signed manifests, software-bill-of-material records, checksum verification, and promotion history to the Oracle gold image. These controls establish which bytes entered production; they do not replace Oracle inventory and &lt;code&gt;datapatch&lt;/code&gt; validation.&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Treat &lt;code&gt;$ORACLE_HOME&lt;/code&gt; as an immutable build artifact:&lt;/strong&gt; Running &lt;code&gt;runInstaller -applyRU -applyOneOffs&lt;/code&gt; offline removes archive extraction, conflict resolution, and relinking from the maintenance window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Keep &lt;code&gt;oraInventory&lt;/code&gt; and Clusterware aligned:&lt;/strong&gt; On Oracle Restart or RAC, &lt;code&gt;srvctl modify database -oraclehome&lt;/code&gt; owns the home pointer—editing &lt;code&gt;/etc/oratab&lt;/code&gt; alone leaves Clusterware starting the old home.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Binary rollback and SQL rollback are separate gates:&lt;/strong&gt; Keeping the &lt;code&gt;N-1&lt;/code&gt; home intact preserves binary recovery, but once &lt;code&gt;datapatch&lt;/code&gt; updates &lt;code&gt;CDB_REGISTRY_SQLPATCH&lt;/code&gt;, reversal requires the patch README’s SQL rollback or a snapshot restore.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Persist rollout state externally:&lt;/strong&gt; Recording &lt;code&gt;IMAGE_VERIFIED&lt;/code&gt;, &lt;code&gt;RESOURCE_STOPPED&lt;/code&gt;, &lt;code&gt;HOME_REGISTERED&lt;/code&gt;, &lt;code&gt;SQL_PATCHED&lt;/code&gt;, and &lt;code&gt;VALIDATED&lt;/code&gt; ensures retries reconcile reality rather than re-running destructive steps.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Prune strictly to &lt;code&gt;N&lt;/code&gt; and &lt;code&gt;N-1&lt;/code&gt;:&lt;/strong&gt; Retaining unbounded historical homes fills &lt;code&gt;/u01&lt;/code&gt;; automate &lt;code&gt;N-2&lt;/code&gt; cleanup only after the post-patch observation window closes.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Identify whether your database operations team still executes &lt;code&gt;opatch apply&lt;/code&gt; directly against running production Oracle homes during scheduled maintenance windows.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Mandate out-of-place patching as the sole authorized lifecycle standard across your enterprise database fleet.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Conduct an audit of your previous year’s database maintenance incidents. Quantify how many hours of extended downtime were caused by patch conflicts, compilation stalls, or failed rollbacks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Build a signed gold-image pipeline using &lt;code&gt;runInstaller -createGoldImage&lt;/code&gt;, rehearse the exact Clusterware and SQL transition on a production-like clone, and set downtime and rollback objectives from measured results.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/ladbi/&quot;&gt;Oracle Database 19c Installation Guide — Image-Based Installation and Read-Only Homes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/cwadd/&quot;&gt;Oracle Clusterware Administration and Deployment Guide — Fleet Patching and Provisioning&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/enterprise-manager/cloud-control/enterprise-manager-cloud-control/13.5/optch/patching-oracle-software-opatch.html&quot;&gt;Oracle OPatch User’s Guide — Patch Prerequisite Checks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/upgrd/&quot;&gt;Oracle Database 19c Upgrade Guide — Datapatch and Post-Patch Processing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/DBA_REGISTRY_SQLPATCH.html&quot;&gt;Oracle Database 19c Reference — DBA_REGISTRY_SQLPATCH&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.oracle.com/security-alerts/cpuapr2022.html&quot;&gt;Oracle Critical Patch Update Advisory — April 2022&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>failures</category><category>checklist</category></item><item><title>SQL Server to PostgreSQL Migration Cost Defense Checklist</title><link>https://rajivonai.com/blog/2026-04-16-sql-server-to-postgresql-migration-checklist/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-04-16-sql-server-to-postgresql-migration-checklist/</guid><description>A pragmatic checklist to defend the business case for migrating away from Microsoft SQL Server.</description><pubDate>Thu, 16 Apr 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Migrating off SQL Server is rarely a technical decision—it is a financial defense mechanism against escalating licensing audits.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Microsoft’s transition from core-based perpetual licensing to subscription models, combined with aggressive Software Assurance renewals, is forcing engineering leaders to justify their SQL Server footprint.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Proposing a migration to PostgreSQL is easy; executing it is hard. The business case often falls apart because the one-time engineering cost to rewrite T-SQL stored procedures exceeds the 3-year license savings. How do you build a defensible migration strategy that CFOs will approve and engineers can actually deliver?&lt;/p&gt;
&lt;h2 id=&quot;the-migration-defense-checklist&quot;&gt;The Migration Defense Checklist&lt;/h2&gt;
&lt;h3 id=&quot;1-the-licensing-baseline&quot;&gt;1. The Licensing Baseline&lt;/h3&gt;
&lt;ul class=&quot;contains-task-list&quot;&gt;
&lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled&gt; Calculate current annual SQL Server Enterprise/Standard costs.&lt;/li&gt;
&lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled&gt; Factor in the upcoming Software Assurance renewal increase (typically 10-15%).&lt;/li&gt;
&lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled&gt; Audit Azure Hybrid Benefit eligibility—if you are moving to Azure, staying on SQL Server might actually be cheaper in the short term.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-the-technical-assessment&quot;&gt;2. The Technical Assessment&lt;/h3&gt;
&lt;ul class=&quot;contains-task-list&quot;&gt;
&lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled&gt; Run the Microsoft Data Migration Assistant (DMA) or AWS SCT.&lt;/li&gt;
&lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled&gt; Identify all instances of &lt;code&gt;CROSS APPLY&lt;/code&gt;, &lt;code&gt;MERGE&lt;/code&gt;, and CLR integrations (these require manual rewrites in PostgreSQL).&lt;/li&gt;
&lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled&gt; Quantify the reliance on SQL Server Agent jobs (these must be migrated to &lt;code&gt;pg_cron&lt;/code&gt; or external orchestrators like Airflow).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-the-refactoring-estimate&quot;&gt;3. The Refactoring Estimate&lt;/h3&gt;
&lt;ul class=&quot;contains-task-list&quot;&gt;
&lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled&gt; Categorize databases into Tier 1 (Heavy T-SQL/Legacy) and Tier 2 (Simple CRUD/ORM-driven).&lt;/li&gt;
&lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled&gt; Estimate engineering months required to migrate Tier 2 databases.&lt;/li&gt;
&lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; disabled&gt; Exclude Tier 1 databases from the initial business case—migrating them first will kill the project’s momentum.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern is to focus on avoiding future licensing purchases rather than replacing deeply entrenched legacy systems immediately. Target new microservices and simple, high-read databases for the first wave of PostgreSQL adoption.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Risk&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;ORM Compatibility&lt;/td&gt;&lt;td&gt;Entity Framework (EF) generates SQL Server specific queries. Switching the EF provider to PostgreSQL often exposes subtle behavioral differences in case sensitivity and transaction handling.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Linked Servers&lt;/td&gt;&lt;td&gt;SQL Server relies heavily on Linked Servers for cross-database queries. PostgreSQL uses Foreign Data Wrappers (FDW), which have different performance profiles for large joins.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: SQL Server migrations stall because the technical debt of T-SQL outweighs license savings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Use this checklist to target low-complexity databases first and build momentum.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Phased migrations (Tier 2 first) show a faster ROI and build team muscle memory for PostgreSQL.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Try our &lt;a href=&quot;https://rajivonai.com/tools/migration-readiness&quot;&gt;Open-Source DB Migration Readiness&lt;/a&gt; tool to score your schema compatibility.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>checklist</category><category>databases</category></item><item><title>Beyond the Slow Query Log: AI-Driven Troubleshooting Across the Application-Database Boundary</title><link>https://rajivonai.com/blog/2026-04-10-ai-driven-database-troubleshooting-orm-slow-queries/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-04-10-ai-driven-database-troubleshooting-orm-slow-queries/</guid><description>How to use LLMs to holistically diagnose complex production incidents by correlating database metrics, application ORM models, and driver configurations.</description><pubDate>Fri, 10 Apr 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The most complex database performance problems rarely originate in the database itself. By the time a query shows up in the slow query log, the architectural failure that caused it has already occurred in the application tier.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Historically, Database Administrators (DBAs) and Application Developers have existed in separate silos. When latency spiked, the DBA would look at CPU utilization, InnoDB buffer pool hit rates, and the slow query log. If the queries were individually fast (e.g., 2 milliseconds), the DBA would declare the database healthy.&lt;/p&gt;
&lt;p&gt;Meanwhile, the developer would see a 5-second page load time and blame the database. The reality was usually an N+1 query problem, connection pool exhaustion, or a misconfigured ORM mapping that fired thousands of 2-millisecond queries in a single web request.&lt;/p&gt;
&lt;p&gt;With the advent of Large Language Models (LLMs) integrated into operational workflows, we no longer have to respect these silos. We can feed telemetry from the entire stack into an agentic reasoning engine.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Traditional database monitoring tools lack context. A metric showing &lt;code&gt;15,000 queries per second&lt;/code&gt; is just a number. Is that expected behavior for a batch job, or is it a rogue &lt;code&gt;for&lt;/code&gt; loop in the Python application?&lt;/p&gt;
&lt;p&gt;If you only feed an LLM the output of &lt;code&gt;SHOW ENGINE INNODB STATUS&lt;/code&gt; or a raw slow query log, the LLM will act like a traditional DBA: it will suggest adding an index or tweaking a buffer pool parameter. It cannot fix the root cause because it cannot see the application code that generated the workload.&lt;/p&gt;
&lt;p&gt;The core question is: how do we structure the context window for an LLM so that it can holistically diagnose a system failure spanning application logic, database driver configuration, and database storage execution?&lt;/p&gt;
&lt;h2 id=&quot;the-holistic-llm-diagnostic-architecture&quot;&gt;The Holistic LLM Diagnostic Architecture&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Production Incident] --&gt; B[Telemetry Gathering]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[Application Source Code — ORM Models]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; D[Infrastructure Config — Connection Pool]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[Database Telemetry — Slow Query Digest]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; F[LLM Diagnostic Context Window]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Root Cause Analysis]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[Application Code Fix]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; I[Database Schema Fix]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for advanced AI Database Ops involves constructing a multi-layered evidence pack for the LLM.&lt;/p&gt;
&lt;p&gt;Instead of asking, “Why is my database slow?”, a Staff Engineer provides the LLM with three specific artifacts during an incident:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The Database Digest:&lt;/strong&gt; The top 5 query digests from the Performance Schema or CloudWatch Database Insights, including total execution time, lock wait time, and rows examined.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The ORM Model definition:&lt;/strong&gt; The Python (SQLAlchemy/Django) or Java (Hibernate) class definitions mapping to those tables, specifically highlighting relationships (e.g., &lt;code&gt;lazy=&apos;select&apos;&lt;/code&gt; vs &lt;code&gt;lazy=&apos;joined&apos;&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Driver Configuration:&lt;/strong&gt; The environment variables defining &lt;code&gt;MAX_CONNECTIONS&lt;/code&gt;, &lt;code&gt;IDLE_TIMEOUT&lt;/code&gt;, and &lt;code&gt;TCP_KEEPALIVE&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When an LLM evaluates these three artifacts simultaneously, its behavior changes completely. For example, if the LLM sees a query digest &lt;code&gt;SELECT * FROM comments WHERE post_id = ?&lt;/code&gt; executing 10,000 times a minute, and then sees the ORM model defines &lt;code&gt;comments&lt;/code&gt; as a lazily loaded relationship on the &lt;code&gt;Post&lt;/code&gt; object, it immediately flags an &lt;strong&gt;N+1 Query Regression&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Instead of suggesting a database index on &lt;code&gt;post_id&lt;/code&gt;, the LLM generates a patch for the application code: &lt;code&gt;Post.objects.select_related(&apos;comments&apos;)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Similarly, if the LLM sees database connections dropping after 5 minutes of idle time and causing application exceptions, and it reads the driver config showing no TCP keepalive settings, it will diagnose a stateful firewall timeout dropping the packets, rather than a database crash.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Approach&lt;/th&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Database-Only Context&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;LLM hallucinates index suggestions for queries that shouldn’t exist.&lt;/td&gt;&lt;td&gt;Always include the application code that generates the query.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Raw PII Exposure&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Sending un-sanitized slow query logs leaks customer data to the LLM provider.&lt;/td&gt;&lt;td&gt;Only send query &lt;em&gt;digests&lt;/em&gt; (parameterized SQL) and structural schema data.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Autonomous Execution&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;LLM automatically applies a schema change that locks the table in production.&lt;/td&gt;&lt;td&gt;LLMs propose fixes; humans authorize, validate, and execute them.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Missing Driver Context&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;LLM blames the DB network thread for dropped connections.&lt;/td&gt;&lt;td&gt;Include connection pool and OS-level TCP settings in the prompt context.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Database incidents cannot be accurately diagnosed by looking only at database metrics, leading to treating symptoms rather than application root causes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Leverage LLMs to bridge the gap by feeding them a combined context of ORM models, driver configurations, and query digests.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Cloud observability platforms are actively moving toward this model by correlating APM (Application Performance Monitoring) distributed traces directly with database wait events.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Build a CLI tool or script that automatically packages your application’s &lt;code&gt;schema.rb&lt;/code&gt; or &lt;code&gt;models.py&lt;/code&gt;, your driver’s non-secret pool settings (&lt;code&gt;MAX_CONNECTIONS&lt;/code&gt;, &lt;code&gt;IDLE_TIMEOUT&lt;/code&gt;, &lt;code&gt;TCP_KEEPALIVE&lt;/code&gt;), and the top 10 queries from &lt;code&gt;events_statements_summary_by_digest&lt;/code&gt; into a single markdown file to pass to your LLM during the next outage — never the raw &lt;code&gt;.env&lt;/code&gt; connection strings themselves.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>ai-engineering</category><category>databases</category><category>architecture</category><category>failures</category></item><item><title>Why Your Non-Prod Databases Cost as Much as Production</title><link>https://rajivonai.com/blog/2026-04-08-dev-test-database-cost-reduction/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-04-08-dev-test-database-cost-reduction/</guid><description>Architectural strategies to eliminate waste in Dev, Test, and Staging database environments.</description><pubDate>Wed, 08 Apr 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;It is a common infrastructure failure when the combined cost of Dev, QA, and Staging databases exceeds the cost of Production.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Engineering teams require production-like environments to ensure release safety. Over time, as microservices multiply, each service gets its own dedicated database in Dev, QA, Staging, and UAT.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;These non-prod databases are often provisioned using Terraform templates cloned directly from Production. They are deployed on Multi-AZ instances, with high-IOPS storage, and left running 24/7. However, developers only use them 40 hours a week. How do you provide production-like fidelity without paying production-level infrastructure bills?&lt;/p&gt;
&lt;h2 id=&quot;the-non-prod-optimization-playbook&quot;&gt;The Non-Prod Optimization Playbook&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Single-AZ Deployments&lt;/strong&gt;: Non-prod environments do not need Multi-AZ high availability. Disabling Multi-AZ immediately cuts compute and storage costs in half.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Storage Tiering&lt;/strong&gt;: Production requires Provisioned IOPS (io2/io3); Dev requires General Purpose storage (gp3).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Auto-Pause/Resume&lt;/strong&gt;: Implement scheduled Lambda/Functions to stop instances at 7 PM and start them at 7 AM on weekdays, saving ~65% of weekly compute hours.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Serverless Dev Databases&lt;/strong&gt;: Move developer environments to scale-to-zero serverless database engines (like Aurora Serverless v2 or Neon) where you only pay when queries are actively running.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern is to treat Staging as a scale-down replica of Production (to test deployment scripts), but to treat Dev and QA as ephemeral, highly optimized, Single-AZ footprints.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Strategy&lt;/th&gt;&lt;th&gt;Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Auto-Pause&lt;/td&gt;&lt;td&gt;Stopping a database clears its cache. The first queries of the morning will experience a “cold start” performance hit while data is pulled back into RAM.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Serverless&lt;/td&gt;&lt;td&gt;If a developer leaves a script running in a loop over the weekend, a serverless database won’t scale to zero—it will scale up and generate a massive bill.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Non-prod databases mirroring production configurations bleed OPEX.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Downgrade storage, disable Multi-AZ, and enforce aggressive pause schedules.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: These changes routinely eliminate 60-70% of non-prod database costs without impacting developer velocity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Audit your AWS/Azure billing dashboard, filtering specifically by &lt;code&gt;Environment: Dev&lt;/code&gt; tags for RDS/SQL Database resources.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>failures</category><category>architecture</category></item><item><title>The Math Behind Database Reserved Instances: When to Wait</title><link>https://rajivonai.com/blog/2026-04-01-cloud-database-reserved-instance-math/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-04-01-cloud-database-reserved-instance-math/</guid><description>Why committing to 3-year database reserved instances too early locks in architectural waste.</description><pubDate>Wed, 01 Apr 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The biggest mistake in Cloud FinOps isn’t failing to buy Reserved Instances—it’s buying them before you’ve optimized the architecture.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A company completes a massive “lift and shift” migration to the cloud. To hit their first-year cost reduction targets, the FinOps team immediately purchases 3-year Reserved Instances (RIs) for all their newly provisioned AWS RDS and Azure SQL databases.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Lift-and-shift migrations almost always result in oversized infrastructure. On-premises databases are sized for 5-year peak capacity. When you move those identical instance sizes to the cloud and immediately lock them in with a 3-year RI, you are signing a contract to pay for idle CPU and RAM for the next 36 months. How do you balance the pressure for immediate RI discounts against the need for architectural right-sizing?&lt;/p&gt;
&lt;h2 id=&quot;the-right-sizing-buffer&quot;&gt;The Right-Sizing Buffer&lt;/h2&gt;
&lt;p&gt;Database workloads require a stabilization period.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The 90-Day Rule&lt;/strong&gt;: Never purchase a database RI within the first 90 days of a cloud migration.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;P95 Profiling&lt;/strong&gt;: Use those 90 days to capture the 95th percentile CPU and memory utilization.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scale Down&lt;/strong&gt;: Reduce the instance sizes to match the P95 load, leaning on the cloud’s ability to scale up dynamically if needed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Commit&lt;/strong&gt;: Only then should you execute the 1-year or 3-year RI purchase on the right-sized footprint.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern shows that a 50% discount on a &lt;code&gt;$10,000&lt;/code&gt;/month oversized instance (&lt;code&gt;$5,000&lt;/code&gt; effective) is worse than right-sizing the instance to &lt;code&gt;$4,000&lt;/code&gt;/month on-demand and then applying a 30% 1-year discount (&lt;code&gt;$2,800&lt;/code&gt; effective).&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Database Modernization&lt;/td&gt;&lt;td&gt;If engineering plans to migrate from RDS MySQL to Aurora Serverless within 18 months, a 3-year RI on the legacy RDS instances will become sunk-cost waste.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Engine Flexibility&lt;/td&gt;&lt;td&gt;Standard RIs are often locked to a specific database engine. You cannot easily transfer an Oracle RI to a PostgreSQL instance.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Buying RIs on unoptimized database infrastructure locks in waste.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Enforce a 90-day waiting period post-migration to profile and right-size instances before committing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Right-sizing followed by RIs yields a dramatically lower TCO than applying RIs to legacy sizes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Model your break-even points using our &lt;a href=&quot;https://rajivonai.com/tools/reserved-instance-roi-calculator/&quot;&gt;Database Reserved Instance ROI Calculator&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>cloud</category><category>architecture</category></item><item><title>Oracle Cloud BYOL: True Cost Analysis Beyond the Headline Rate</title><link>https://rajivonai.com/blog/2026-03-25-oracle-cloud-byol-true-cost/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-03-25-oracle-cloud-byol-true-cost/</guid><description>Understanding the financial nuances, OCPU conversions, and hidden costs of bringing your Oracle licenses to OCI.</description><pubDate>Wed, 25 Mar 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Oracle Cloud Infrastructure (OCI) advertises the most aggressive pricing for Oracle Database workloads, but the true cost relies heavily on your existing contract structure.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;An enterprise wants to migrate their on-premises Oracle Exadata workloads to the cloud. They are comparing AWS RDS for Oracle against Oracle Cloud Infrastructure (OCI) Exadata Database Service.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;OCI’s headline compute rates are significantly lower than AWS, and Oracle’s licensing policies heavily favor OCI (where 1 OCPU = 1 Processor License, compared to AWS where hyper-threading penalties apply). However, the Bring Your Own License (BYOL) math on OCI is complex, factoring in un-allocated support costs and mandatory cloud management fees. How do you calculate the actual TCO?&lt;/p&gt;
&lt;h2 id=&quot;the-oci-byol-reality&quot;&gt;The OCI BYOL Reality&lt;/h2&gt;
&lt;p&gt;When you bring your licenses to OCI via BYOL, you stop paying for the “License Included” markup, but you continue to pay your annual on-premises support bill.
Furthermore, OCI PaaS offerings (like Base Database Service or Exadata Cloud Service) require you to pay a baseline OCPU rate that covers the cloud automation, backup infrastructure, and management plane.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern is that OCI provides the lowest TCO for workloads that &lt;em&gt;must&lt;/em&gt; remain on Oracle (due to deep PL/SQL dependencies or vendor application requirements). By leveraging BYOL on OCI, customers avoid the “Authorized Cloud Environment” core-factor penalties that Oracle applies to AWS and Azure.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;ULA Expiration&lt;/td&gt;&lt;td&gt;If your Unlimited License Agreement (ULA) is expiring, declaring your usage and moving to OCI BYOL requires strict audit compliance. If you over-provision OCPUs in the cloud, you will trigger a massive true-up bill.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Multi-Cloud Networking&lt;/td&gt;&lt;td&gt;If the rest of your application stack lives in AWS, moving the database to OCI introduces latency and egress costs. You must factor in the cost of an Azure-Oracle Interconnect or FastConnect to AWS.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Comparing Oracle database costs across AWS and OCI is apples-to-oranges due to licensing penalties.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Model the exact core counts using Oracle’s Cloud Licensing Policy document.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: OCI BYOL consistently models cheaper for heavy Oracle workloads, provided egress and latency constraints are managed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Request a Cloud Database Cost Review to build a custom multi-cloud ROI model for your Exadata footprint.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category></item><item><title>BigQuery Cost Optimization: On-Demand vs Slot Commitments</title><link>https://rajivonai.com/blog/2026-03-18-gcp-bigquery-cost-optimization/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-03-18-gcp-bigquery-cost-optimization/</guid><description>How to stop runaway BigQuery costs by analyzing query scans, enforcing partitions, and moving to capacity-based pricing.</description><pubDate>Wed, 18 Mar 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The beauty of BigQuery is that it requires no infrastructure management. The danger is that an analyst can accidentally spend $500 with a single &lt;code&gt;SELECT *&lt;/code&gt; query.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Data teams initially love BigQuery’s on-demand pricing model ($5 to $6.25 per TB scanned). It allows them to start small without upfront capacity planning.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;As data volume grows and user adoption increases, on-demand costs become unpredictable and highly volatile. A poorly written query without a &lt;code&gt;WHERE&lt;/code&gt; clause on a massive unpartitioned table scans petabytes of data, causing immediate budget overruns. How do you secure BigQuery costs without bottlenecking the data team?&lt;/p&gt;
&lt;h2 id=&quot;the-optimization-checklist&quot;&gt;The Optimization Checklist&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Enforce Partition Filters&lt;/strong&gt;: Require partition filters on all multi-terabyte tables at the schema level.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Materialized Views&lt;/strong&gt;: Pre-aggregate common daily/weekly metrics so dashboards aren’t scanning raw event data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Query Limits&lt;/strong&gt;: Set maximum bytes billed limits per user and per project to prevent accidental runaway queries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Transition to Capacity Pricing&lt;/strong&gt;: Evaluate moving from On-Demand to Capacity Pricing (Slot Commitments).&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for mature BigQuery environments is a hybrid approach. They purchase baseline slot commitments (e.g., 500 slots) to handle predictable, continuous ETL workloads, while keeping ad-hoc analyst exploration on the on-demand model with strict query limits enforced.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Strategy&lt;/th&gt;&lt;th&gt;Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Slot Commitments&lt;/td&gt;&lt;td&gt;Purchasing slots caps your maximum spend, but it also caps your maximum performance. If multiple analysts run heavy queries simultaneously, queries will queue and latency will increase.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Partition Enforcement&lt;/td&gt;&lt;td&gt;Hard-enforcing partition filters breaks legacy queries and dashboards that were built assuming full table scans were acceptable.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Volatile and unpredictable BigQuery on-demand costs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Implement table partitioning, enforce query limits, and evaluate baseline slot commitments.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Transitioning baseline ETL to capacity pricing while restricting ad-hoc scans consistently flattens BigQuery spend curves.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Audit your &lt;code&gt;INFORMATION_SCHEMA.JOBS&lt;/code&gt; to identify the top 10 most expensive queries this week.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>cloud</category><category>architecture</category><category>checklist</category></item><item><title>Why Vector Database Performance Is Different: Recall vs Latency</title><link>https://rajivonai.com/blog/2026-03-17-why-vector-database-performance-is-different-recall-vs-latency/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-03-17-why-vector-database-performance-is-different-recall-vs-latency/</guid><description>A systems engineering analysis of vector search performance: navigating the fundamental tradeoff between Recall@K, query latency, index memory footprint, quantization, and filtered search.</description><pubDate>Tue, 17 Mar 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;In relational and document databases, an index either matches a row or does not; in vector databases, index performance is a continuous negotiation between query latency, memory consumption, and retrieval accuracy. Tuning vector search requires measuring Recall@K against index hyperparameters rather than treating query execution as deterministic boolean matching.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Enterprise AI systems, retrieval-augmented generation (RAG) pipelines, and semantic search platforms rely on vector databases and vector extensions (such as pgvector, Qdrant, Milvus, and Elasticsearch vector search). Unlike traditional B-trees or inverted indices that execute exact predicate evaluation, vector engines execute Approximate Nearest Neighbor (ANN) search across high-dimensional embedding spaces (e.g., 768, 1536, or 3072 dimensions).&lt;/p&gt;
&lt;p&gt;Vector performance evaluation departs fundamentally from classical database benchmarking:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Accuracy is Approximate:&lt;/strong&gt; An ANN query returns the closest $K$ candidates found by a heuristic graph or clustering traversal, not the mathematically guaranteed top-$K$ nearest neighbors. Retrieval quality is governed by Recall@K (the percentage of true nearest neighbors retrieved relative to an exact brute-force scan).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Extreme Memory Footprint:&lt;/strong&gt; Graph-based indices like HNSW (Hierarchical Navigable Small World) perform best when the hot graph and its vectors fit in RAM, because traversal is a pointer-chasing workload whose access pattern defeats read-ahead. This is a latency characteristic, not a correctness requirement: several engines support disk-backed or memory-mapped operation with a latency tradeoff. Qdrant offers memory-mapped and on-disk vector and index configurations; pgvector’s HNSW index is an ordinary PostgreSQL index stored on disk and paged through shared buffers, though performance still depends heavily on the active graph staying resident. Note also that peak &lt;em&gt;build&lt;/em&gt; memory and steady-state &lt;em&gt;query&lt;/em&gt; memory are different numbers — sizing against the build peak overprovisions the cluster. A dataset of 10 million 1536-dimensional vectors can easily consume 80–120 GB when held fully in memory as Float32.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compute-Intensive Math:&lt;/strong&gt; Distance metrics (Cosine Similarity, Euclidean Distance $L_2$, Dot Product) require billions of floating-point operations per query, relying heavily on CPU SIMD instructions (AVX-512, ARM Neon) or GPU acceleration.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When downstream LLMs hallucinate due to missed context or when vector search p99 latency degrades from 5 ms to 200 ms, DBAs and SREs cannot simply look for missing indexes. Triage requires understanding the trade-off envelope between index topology, quantization compression, search exploration depth, and metadata filter selectivity.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Vector search introduces distinct operational trade-offs where optimizing one system dimension inevitably degrades another:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The Recall-Latency Curve:&lt;/strong&gt; In HNSW indices, increasing the search exploration parameter ($efSearch$) expands the candidate queue during graph traversal, raising Recall@K at the cost of latency and throughput. The &lt;em&gt;shape&lt;/em&gt; of that tradeoff is universal; the magnitude is not. How much recall a given $efSearch$ increase buys, and how much QPS it costs, depends on dataset size, dimensionality, graph construction parameters ($M$, $efConstruction$), filter selectivity, storage backing, hardware, and query concurrency. Treat any specific pair of numbers as belonging to the benchmark that produced it, and measure Recall@K and latency on your own corpus before committing to a setting.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Quantization Distortion vs. Memory Savings:&lt;/strong&gt; Techniques like Scalar Quantization (SQ8) and Product Quantization (PQ) reduce memory usage by 4x–16x by compressing 32-bit floats into 8-bit or sub-vector codebooks. However, quantization introduces distance approximation noise, degrading recall unless paired with expensive raw-vector rescoring (over-fetching).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Metadata Filtering Dilemma:&lt;/strong&gt; Combining boolean metadata filters (e.g., &lt;code&gt;tenant_id = &quot;org_42&quot;&lt;/code&gt; AND &lt;code&gt;status = &quot;active&quot;&lt;/code&gt;) with vector search creates structural failure modes:
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Post-filtering:&lt;/em&gt; Runs ANN search first, then drops non-matching candidates. If metadata selectivity is 1%, an ANN query for $K=10$ returns 0 or 1 valid document (catastrophic recall collapse).&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Pre-filtering:&lt;/em&gt; Filters metadata first, but if the remaining candidate set is small, graph traversal fails to navigate disconnected nodes, or falls back to an unindexed brute-force scan.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Iterative / Single-Stage Filtering:&lt;/em&gt; Evaluates metadata during graph traversal, but increases per-node evaluation cost.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RAM Thrashing on Graph Traversals:&lt;/strong&gt; Unlike relational tables that leverage sequential disk reads and page caches, HNSW graph traversal performs random memory pointer jumps across millions of vectors. Read-ahead cannot predict the next hop, so when the working set does not fit in RAM the index degrades sharply — each traversal hop can become a synchronous I/O stall rather than a cache hit. The severity spans orders of magnitude depending on storage class, queue depth, and how much of the graph stays resident, and it is qualitatively different from a deliberately disk-backed configuration that the engine plans for. Distinguish the two: unplanned swap thrashing shows as rising major page faults (&lt;code&gt;majflt&lt;/code&gt;) against a resident set that exceeds RAM, whereas a memory-mapped or on-disk index configured on purpose has a higher but stable latency floor.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dimensionality Scaling Bottlenecks:&lt;/strong&gt; Doubling embedding dimensionality from 768 to 1536 doubles RAM consumption and vector math instruction cycles while worsening the “curse of dimensionality,” where distance distributions concentrate and make graph clustering less discriminative.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;How do we systematically diagnose whether vector retrieval failures stem from index hyperparameter misconfiguration, quantization recall loss, metadata filter mismatch, memory paging, or vector dimension overhead?&lt;/p&gt;
&lt;h2 id=&quot;build-a-vector-aware-incident-evidence-pack&quot;&gt;Build a Vector-Aware Incident Evidence Pack&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[RAG retrieval latency and quality alerts] --&gt; E[time-bounded vector evidence pack]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B[Recall-at-K ground truth benchmark validation] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[index hyperparameters HNSW M efSearch IVF nprobe] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[vector memory footprint quantization and filter selectivity] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[calculate Recall-at-K curve vs latency and QPS]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[reconcile SIMD instructions RAM resident set and cache misses]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[LLM observations hypotheses contradictions and gaps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[AI Platform and Database Engineer verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[index tuning validation and rollback]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To diagnose vector search performance, construct an evidence pack combining system metrics with algorithmic accuracy benchmarks:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Accuracy Metrics (Recall@K):&lt;/strong&gt; Measure ground-truth recall against an exact brute-force Flat index ($kNN_{exact}$) across a representative validation query sample (e.g., 500 queries):
$$\text{Recall}@K = \frac{| \text{ANN_Results}@K \cap \text{Exact_Results}@K |}{K}$$&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Index Hyperparameters:&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;HNSW:&lt;/em&gt; $M$ (max links per node, e.g., 16–64), $efConstruction$ (build candidate list, e.g., 100–400), $efSearch$ (runtime candidate list, e.g., 32–256).&lt;/li&gt;
&lt;li&gt;&lt;em&gt;IVF:&lt;/em&gt; $nlist$ (total Voronoi cluster centroids), $nprobe$ (centroids scanned during query).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Vector and Memory Sizing:&lt;/strong&gt; Vector dimension ($D$), precision (Float32, Float16, SQ8, PQ, BQ), total vector count ($N$), index memory size in RAM, and OS resident memory (RSS).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Execution and Hardware Telemetry:&lt;/strong&gt; P50/p95/p99 search latency, queries per second (QPS), CPU SIMD instruction flags (&lt;code&gt;avx512f&lt;/code&gt;, &lt;code&gt;neon&lt;/code&gt;), page fault rates (&lt;code&gt;minflt/s&lt;/code&gt;, &lt;code&gt;majflt/s&lt;/code&gt;), and metadata filter selectivity percentage.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The LLM processes this multi-dimensional bundle to generate four distinct outputs:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Observations:&lt;/strong&gt; Quantitative facts correlating parameters to recall and latency (e.g., “Increasing $efSearch$ from 32 to 128 increased Recall@10 from 88.2% to 97.4%, while p99 latency increased from 4.1 ms to 14.8 ms”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hypotheses:&lt;/strong&gt; Ranked architectural explanations (e.g., “Hypothesis 1: Post-filtering on low-selectivity tenant metadata causing candidate exhaustion; Hypothesis 2: Index size exceeding physical RAM causing minor page faults on graph traversal”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Missing Evidence:&lt;/strong&gt; Unobserved metrics (e.g., “Need validation query ground-truth recall across quantized vs unquantized vectors”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Actions:&lt;/strong&gt; Specific index parameter tuning, quantization adjustments, and filtering architecture changes.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Plausible failure classes&lt;/th&gt;&lt;th&gt;Evidence that separates them&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Low retrieval accuracy / hallucinations&lt;/td&gt;&lt;td&gt;Low $efSearch$/$nprobe$, aggressive PQ quantization, post-filtering&lt;/td&gt;&lt;td&gt;Measured Recall@K &amp;#x3C; 90%, metadata filter selectivity, quantization mode&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High p99 latency with low CPU&lt;/td&gt;&lt;td&gt;Index paging to disk/swap, random I/O thrashing during graph hops&lt;/td&gt;&lt;td&gt;OS major page faults (&lt;code&gt;majflt&lt;/code&gt;), disk read IOPS on vector volume&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Latency spikes when metadata filter added&lt;/td&gt;&lt;td&gt;Post-filtering candidate starvation, unindexed filter scan&lt;/td&gt;&lt;td&gt;Latency delta with vs without filter, filter selectivity %, query plan&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High CPU with low QPS&lt;/td&gt;&lt;td&gt;High $efSearch$, un-quantized high-dimensional math, missing SIMD&lt;/td&gt;&lt;td&gt;&lt;code&gt;efSearch&lt;/code&gt; &gt; 256, CPU instruction counters, vector dimension $D \ge 1536$&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Memory OOM during index build&lt;/td&gt;&lt;td&gt;$efConstruction$ or $M$ too high, un-quantized vectors in RAM&lt;/td&gt;&lt;td&gt;Memory climb during build, &lt;code&gt;M&lt;/code&gt; &gt; 64, &lt;code&gt;efConstruction&lt;/code&gt; &gt; 400&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Index build takes hours/days&lt;/td&gt;&lt;td&gt;Large $N$, high $efConstruction$, single-threaded build&lt;/td&gt;&lt;td&gt;Build time delta, worker thread count, indexing throughput (vectors/sec)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Vector search latency or recall degradation alert] --&gt; B[1. Validate Recall-at-K against exact brute-force baseline]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[2. Inspect index hyperparameters HNSW efSearch or IVF nprobe]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[3. Verify index resident memory in RAM and check page faults]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[4. Analyze metadata filter selectivity and filtering execution strategy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[5. Evaluate vector quantization type and distance calculation overhead]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Synthesize observations hypotheses and hyperparameter tuning plan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-validate-recallk-against-exact-brute-force-baseline&quot;&gt;1. Validate Recall@K against exact brute-force baseline&lt;/h3&gt;
&lt;p&gt;Never tune vector parameters based on latency alone without measuring retrieval accuracy. Run a test batch of 100–500 representative production queries against both the active ANN index and a ground-truth flat brute-force index (&lt;code&gt;Flat&lt;/code&gt; / exact scan).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If Recall@10 is below 92–95%, the index is dropping true nearest neighbors, causing downstream RAG hallucination.&lt;/li&gt;
&lt;li&gt;If Recall@10 is 99.8%, the index is over-configured for accuracy and wasting compute headroom.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-inspect-index-hyperparameters-hnsw-efsearch-or-ivf-nprobe&quot;&gt;2. Inspect index hyperparameters (HNSW $efSearch$ or IVF $nprobe$)&lt;/h3&gt;
&lt;p&gt;Check the runtime exploration depth:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;HNSW $efSearch$:&lt;/strong&gt; In HNSW, $efSearch$ controls the size of the dynamic priority queue during search. If $efSearch = K$ (e.g., $K=10, efSearch=10$), graph search terminates prematurely with poor recall. A standard baseline is $efSearch = 64\text{ to }128$.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IVF $nprobe$:&lt;/strong&gt; In IVF, $nprobe$ dictates how many cluster centroids are inspected. If $nlist = 4096$ and $nprobe = 1$, only the single closest centroid is searched, resulting in low recall for boundary vectors.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-verify-index-resident-memory-in-ram-and-check-page-faults&quot;&gt;3. Verify index resident memory in RAM and check page faults&lt;/h3&gt;
&lt;p&gt;Calculate raw index memory requirements:
$$\text{Memory}_{\text{HNSW}} \approx N \times \left( (D \times 4\text{ bytes}) + (M \times 8\text{ bytes}) \right) \times 1.25$$
For 5 million vectors with $D=1536$ and $M=32$:
$$\text{Memory} \approx 5{,}000{,}000 \times \left( 6144 + 256 \right) \times 1.25 \approx 40.0\text{ GB}$$
Check &lt;code&gt;vmstat 1&lt;/code&gt; for major page faults (&lt;code&gt;majflt&lt;/code&gt;). If the index exceeds available RAM and the OS pages vectors from SSD swap, latency spikes by orders of magnitude due to random non-sequential graph memory access.&lt;/p&gt;
&lt;h3 id=&quot;4-analyze-metadata-filter-selectivity-and-filtering-execution-strategy&quot;&gt;4. Analyze metadata filter selectivity and filtering execution strategy&lt;/h3&gt;
&lt;p&gt;Inspect the metadata filters applied to queries:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;High Selectivity (e.g., filter matches 0.1% of data):&lt;/strong&gt; Pre-filtering or single-stage iterative graph traversal is required. Post-filtering will discard almost all ANN candidates, returning empty results.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Low Selectivity (e.g., filter matches 90% of data):&lt;/strong&gt; Post-filtering or standard ANN traversal with bitmap checks operates efficiently without index overhead.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;5-evaluate-vector-quantization-type-and-distance-calculation-overhead&quot;&gt;5. Evaluate vector quantization type and distance calculation overhead&lt;/h3&gt;
&lt;p&gt;Inspect vector representation precision:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Uncompressed Float32:&lt;/strong&gt; Maximum distance fidelity, but consumes 4 bytes per dimension.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scalar Quantization (SQ8):&lt;/strong&gt; Compresses floats to 8-bit unsigned integers (1 byte/dimension), providing roughly a 4x RAM reduction. Recall impact is typically small but is data-dependent — validate it against your own corpus rather than assuming a fixed retention figure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Product Quantization (PQ):&lt;/strong&gt; Compresses vectors into compact codebook indices (e.g., 64 bytes total), but requires rescoring the top-$N$ candidates ($N=50\text{ to }100$) using uncompressed vectors to restore final Recall@K.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Vector search performance or recall incident] --&gt; B{Measured Recall-at-K below target SLA}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes| C{Is quantization active SQ8 PQ BQ}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|Yes| D[Enable top-K over-fetching and raw vector rescoring]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|No| E[Increase efSearch in HNSW or nprobe in IVF]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No| F{Search p99 latency above SLA}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|Yes| G{Major page faults detected or RSS exceeds RAM}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|Yes| H[Apply SQ8 quantization or upgrade instance memory]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|No| I{Metadata filter present in query}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|Yes| J[Switch from post-filtering to single-stage iterative filtering]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|No| K[Decrease efSearch or optimize CPU SIMD compiler flags]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://arxiv.org/abs/1603.09320&quot;&gt;original HNSW research (Malkov &amp;#x26; Yashunin, IEEE TPAMI)&lt;/a&gt; proves that hierarchical graph exploration achieves logarithmic search scaling ($O(\log N)$) by constructing multi-layer Delaunay-like graphs. The research establishes that search quality depends directly on $efSearch$; setting $efSearch &amp;#x3C; K$ destroys recall, while setting $efSearch &gt; 500$ produces diminishing accuracy returns at significant CPU cost.&lt;/p&gt;
&lt;p&gt;According to the &lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;pgvector official documentation&lt;/a&gt;, HNSW index builds require tuning &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;ef_construction&lt;/code&gt; during index creation, and adjusting &lt;code&gt;hnsw.ef_search&lt;/code&gt; dynamically per session. The documentation highlights that memory for HNSW indexes must fit within PostgreSQL’s &lt;code&gt;shared_buffers&lt;/code&gt; and OS RAM cache to prevent disk seek bottlenecks.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://qdrant.tech/documentation/manage-data/quantization/&quot;&gt;Qdrant’s quantization documentation&lt;/a&gt; documents Scalar Quantization (SQ8) as delivering roughly a fourfold reduction in memory, with accuracy loss that is often small — but it treats that accuracy impact as explicitly data-dependent rather than guaranteed. Do not carry a fixed recall-retention figure across corpora: whether SQ8 costs you 0.5% or 5% of Recall@10 depends on the embedding model, the dimensionality, and how tightly clustered the vectors are in your space. Measure it. In contrast, 1-bit Binary Quantization (BQ) generally requires over-sampling and rescoring against original vectors to avoid severe accuracy regressions in cosine similarity search.&lt;/p&gt;
&lt;p&gt;Research on &lt;a href=&quot;https://arxiv.org/abs/2403.04871&quot;&gt;Filtered Vector Search (ACORN / Filtered HNSW)&lt;/a&gt; demonstrates that naive post-filtering causes catastrophic recall collapse when metadata selectivity drops below 5%. The documented best practice is single-stage predicate graph traversal, where edge exploration evaluates both vector proximity and boolean predicates simultaneously.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven root cause&lt;/th&gt;&lt;th&gt;Candidate remediation&lt;/th&gt;&lt;th&gt;Validation criteria&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Low Recall@K due to shallow graph search&lt;/td&gt;&lt;td&gt;Increase &lt;code&gt;efSearch&lt;/code&gt; (e.g., from 32 to 96 or 128) in session configuration&lt;/td&gt;&lt;td&gt;Recall@10 increases to &gt;95%; latency remains within SLA&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Excessive RAM footprint causing page faults&lt;/td&gt;&lt;td&gt;Convert index from Float32 to Scalar Quantization (SQ8)&lt;/td&gt;&lt;td&gt;Index RAM drops by 75%; major page faults drop to 0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Post-filtering causing empty or low-recall results&lt;/td&gt;&lt;td&gt;Migrate to single-stage filtered index or pre-filter by partition&lt;/td&gt;&lt;td&gt;Retrieved candidate count equals requested $K$; recall restores&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High search latency from high dimensionality&lt;/td&gt;&lt;td&gt;Apply Matryoshka Representation Learning (MRL) truncation or PCA&lt;/td&gt;&lt;td&gt;Vector dimensions reduced (e.g., 1536 to 512); latency drops 2x&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Product Quantization distance distortion&lt;/td&gt;&lt;td&gt;Implement two-phase search: over-fetch top $3K$ via PQ, rescore with Float32&lt;/td&gt;&lt;td&gt;Recall@K restores to Float32 parity with low RAM usage&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slow index builds blocking ingest pipeline&lt;/td&gt;&lt;td&gt;Increase parallel build workers and tune &lt;code&gt;efConstruction&lt;/code&gt; to 128–200&lt;/td&gt;&lt;td&gt;Build throughput increases (vectors/sec); RAM stays bounded&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;When modifying vector index parameters, quantization formats, or filtering pipelines, maintain an explicit reversal plan:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Session-Level Hyperparameter Rollback:&lt;/strong&gt; Changes to search parameters (such as &lt;code&gt;hnsw.ef_search&lt;/code&gt; in pgvector or &lt;code&gt;ef&lt;/code&gt; in Qdrant) are dynamic. If increasing &lt;code&gt;efSearch&lt;/code&gt; degrades throughput, reset the parameter to its baseline value instantly without rebuilding the index.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Quantization Rollback Strategy:&lt;/strong&gt; When converting an index to SQ8 or PQ, build the quantized index under a separate name or alias. Validate Recall@K against production queries. If accuracy drops below SLA, point query routing back to the unquantized index before dropping the old index.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Filtering Pipeline Fallback:&lt;/strong&gt; If deploying single-stage iterative graph filtering causes query planner regressions, toggle an application feature flag to revert to pre-filtered partitioned tables.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Matryoshka Dimension Truncation Rollback:&lt;/strong&gt; If truncating vector dimensions (e.g., from 1536 to 512) degrades semantic recall on long-tail queries, update the embedding service configuration to generate full-dimension embeddings.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why naive reasoning fails&lt;/th&gt;&lt;th&gt;Better diagnostic boundary&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;”Recall is 99%, index is optimal”&lt;/td&gt;&lt;td&gt;Over-configured &lt;code&gt;efSearch&lt;/code&gt; wastes CPU; 95% recall at 3x higher QPS may be better&lt;/td&gt;&lt;td&gt;Map the full Recall-vs-Latency Pareto frontier&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Vector indexes behave like B-trees”&lt;/td&gt;&lt;td&gt;B-trees are deterministic; vector search is probabilistic and parameter-dependent&lt;/td&gt;&lt;td&gt;Benchmark accuracy using ground-truth validation queries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Post-filtering is simple and safe”&lt;/td&gt;&lt;td&gt;Low-selectivity filters eliminate all nearest neighbors, causing empty results&lt;/td&gt;&lt;td&gt;Analyze metadata filter selectivity distribution&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”More RAM always fixes slow search”&lt;/td&gt;&lt;td&gt;If $efSearch$ is too high or SIMD math is unoptimized, CPU is the bottleneck&lt;/td&gt;&lt;td&gt;Profile CPU instruction cycles and SIMD utilization&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Quantization always loses accuracy”&lt;/td&gt;&lt;td&gt;SQ8’s accuracy cost is often small relative to its ~4x memory saving, but the magnitude is data-dependent&lt;/td&gt;&lt;td&gt;Measure Recall@K with and without SQ8 on your own corpus before investing in massive RAM upgrades&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Bigger embedding model is always better”&lt;/td&gt;&lt;td&gt;3072-dim vectors double latency and memory; smaller MRL models often match quality&lt;/td&gt;&lt;td&gt;Benchmark domain retrieval accuracy vs vector dimension&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;To prevent hallucinated configurations, data leakage, and system degradation, strict boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Vector Ingestion:&lt;/strong&gt; The LLM must not receive raw high-dimensional vector arrays or private document embeddings. Telemetry must be restricted to index metadata, recall benchmarks, and system metrics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Speculative Recall Guarantees:&lt;/strong&gt; The LLM cannot predict exact Recall@K without empirical ground-truth benchmark data. It must request validation query evaluations when accuracy metrics are missing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Direct Index Mutations:&lt;/strong&gt; The LLM cannot execute &lt;code&gt;CREATE INDEX&lt;/code&gt;, drop indexes, or alter database memory configurations autonomously.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Threshold Redefinition:&lt;/strong&gt; The LLM cannot decide whether a 92% recall rate is acceptable for business logic without human domain authorization.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Vector search performance diverges from classical databases, requiring continuous optimization across recall, query latency, memory footprint, and metadata filtering.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Construct a vector-aware incident evidence pack measuring empirical Recall@K curves against $efSearch$/$nprobe$, memory sizing, and filter selectivity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Formulate hypotheses that explicitly reconcile recall improvements against latency inflation, quantization memory savings against rescoring overhead, and filter strategies against candidate yield.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Instrument automated Recall@K tracking against a baseline flat index in staging. Then proceed to Part 2 to diagnose embedding model latency, reranking pipelines, and hybrid search regressions.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1603.09320&quot;&gt;Efficient and Robust Approximate Nearest Neighbor Search Using Hierarchical Navigable Small World Graphs (Malkov &amp;#x26; Yashunin, IEEE TPAMI)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/pgvector/pgvector&quot;&gt;pgvector Documentation — HNSW and IVFFlat Vector Indexing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://qdrant.tech/documentation/concepts/quantization/&quot;&gt;Qdrant Vector Database Documentation — Vector Quantization and Index Sizing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://milvus.io/docs/index.md&quot;&gt;Milvus Vector Database Documentation — In-Memory and Filtered Vector Search&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2403.04871&quot;&gt;ACORN: Performant and Accurate Approximate Nearest Neighbor Search with Predicates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/2205.13147&quot;&gt;Matryoshka Representation Learning (Kusupati et al., NeurIPS)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>ai-engineering</category><category>failures</category></item><item><title>Oracle to Aurora PostgreSQL: License Cost Elimination in Practice</title><link>https://rajivonai.com/blog/2026-03-11-aurora-postgresql-migration-cost-savings/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-03-11-aurora-postgresql-migration-cost-savings/</guid><description>The engineering reality and ROI of migrating from Oracle to Amazon Aurora PostgreSQL.</description><pubDate>Wed, 11 Mar 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Eliminating commercial database licensing is the holy grail of cloud cost optimization, but the migration path is heavily guarded by proprietary PL/SQL.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A platform team is mandated by the CFO to exit their Oracle Enterprise Agreement due to a 20% year-over-year increase in support and maintenance costs.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;They decide to migrate to Amazon Aurora PostgreSQL. While tools like the AWS Schema Conversion Tool (SCT) and Database Migration Service (DMS) handle the raw table structures and data movement, they fail on complex stored procedures, hierarchical queries (&lt;code&gt;CONNECT BY&lt;/code&gt;), and Oracle-specific XML processing. How do you accurately model the ROI when the migration requires thousands of hours of manual rewrite?&lt;/p&gt;
&lt;h2 id=&quot;the-migration-investment-framework&quot;&gt;The Migration Investment Framework&lt;/h2&gt;
&lt;p&gt;To calculate the true ROI of an Oracle exit, you must factor in the migration cost.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Assessment&lt;/strong&gt;: Run SCT to generate an automated conversion report. Identify the “red” items (manual rewrite required).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Estimation&lt;/strong&gt;: Assign an engineering hour cost to every manual rewrite item.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Modeling&lt;/strong&gt;: Compare the 5-year TCO of staying on Oracle (including annual support increases) against the Aurora compute cost plus the one-time migration engineering cost.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for successful Oracle exits involves establishing a “strangler fig” architecture. Rather than a massive big-bang cutover, teams replicate data to Aurora using DMS, point read-only workloads to PostgreSQL first, and slowly refactor the write-path APIs away from PL/SQL into the application layer.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Phase&lt;/th&gt;&lt;th&gt;Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Schema Conversion&lt;/td&gt;&lt;td&gt;SCT is optimistic. It will claim 95% automated conversion, but the remaining 5% of code often contains the core business logic.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Performance Tuning&lt;/td&gt;&lt;td&gt;Aurora PostgreSQL handles concurrency differently than Oracle RAC. Queries that were fast on Oracle may require significant index tuning or architectural changes (like removing sequence bottlenecks) on PostgreSQL.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Oracle licensing costs are unsustainable, but migration engineering costs are opaque.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Execute a strict schema assessment and build a 5-year TCO model that includes manual refactoring time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Organizations that treat the migration as an application refactoring project (moving logic out of the database) achieve a faster ROI.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Model your break-even point using our &lt;a href=&quot;https://rajivonai.com/tools/oracle-migration-savings-calculator/&quot;&gt;Oracle to PostgreSQL Migration Savings Calculator&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category></item><item><title>Top GitHub Breakouts: February 2026 — Part I</title><link>https://rajivonai.com/blog/2026-03-07-github-stars-feb-2026/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-03-07-github-stars-feb-2026/</guid><description>The highest-starred new open-source projects in February 2026 — eliminating the context tax that slows AI-assisted code review, infrastructure generation, and database operations.</description><pubDate>Sat, 07 Mar 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Every AI coding session starts with a tax: the agent re-reads the entire codebase, hallucinates Terraform resources that don’t exist, and has no way to undo the database changes it just made. February 2026’s top breakout tools close all three gaps with precision.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;AI coding agents are writing infrastructure code, running database migrations, and reviewing pull requests. The tooling around those agents hasn’t kept pace: every session burns tokens re-reading code the agent already understood, Terraform generation drifts from HashiCorp’s own best practices because LLMs hallucinate module structures, and database changes made by agents leave no audit trail. The cost is real — both in wasted tokens and in hours spent recovering from agent-induced drift.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Domain&lt;/th&gt;&lt;th&gt;Manual bottleneck&lt;/th&gt;&lt;th&gt;What it costs&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;System design&lt;/td&gt;&lt;td&gt;AI coding agent re-reads entire codebase on every session&lt;/td&gt;&lt;td&gt;Wasted tokens on unchanged files; context window crowded with irrelevant code&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;System design&lt;/td&gt;&lt;td&gt;Engineers manually direct the agent to the relevant files before each task&lt;/td&gt;&lt;td&gt;Setup time before the agent can do the actual work&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Platform engineering&lt;/td&gt;&lt;td&gt;LLM-generated Terraform uses deprecated or hallucinated resource arguments&lt;/td&gt;&lt;td&gt;IaC drift that fails &lt;code&gt;plan&lt;/code&gt; or &lt;code&gt;apply&lt;/code&gt; in CI, requiring human correction&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Databases&lt;/td&gt;&lt;td&gt;AI agent modifies database schemas with no rollback path&lt;/td&gt;&lt;td&gt;Data loss or hours of manual reconstruction when an agent makes a wrong change&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Can AI tooling available today eliminate these manual steps without requiring teams to build custom infrastructure?&lt;/p&gt;
&lt;h2 id=&quot;eliminating-the-context-tax-across-code-infrastructure-and-data&quot;&gt;Eliminating the Context Tax Across Code, Infrastructure, and Data&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[AI engineering without guardrails] --&gt; B[Context — full codebase re-read every task]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; C[Terraform IaC — hallucinated resources and arguments]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; D[Database changes — no rollback after agent errors]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[code-review-graph — structural map via MCP]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; F[TerraShark — HashiCorp best practices as skill]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; G[GFS — Git snapshots and branches for databases]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; H[Precise context — only relevant files loaded]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; I[Hallucination-free IaC generation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; J[Instant rollback from any agent mistake]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;tirth8205code-review-graph--eliminating-full-codebase-re-reads-on-every-ai-task&quot;&gt;tirth8205/code-review-graph — eliminating full codebase re-reads on every AI task&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: Every AI coding session re-reads all source files even when only a handful are relevant to the current task, burning tokens and crowding the context window with noise that the agent has to work around.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How AI replaces that task&lt;/strong&gt;: According to the project README, &lt;code&gt;code-review-graph&lt;/code&gt; uses Tree-sitter to build a persistent structural map of the codebase — functions, classes, imports, call graphs — then tracks changes incrementally. It exposes this map to AI coding tools via MCP so the agent receives only the files and symbols relevant to the current task. The project description states 6.8× fewer tokens on code reviews and up to 49× on daily coding tasks; the README diagram references 8.2× average token reduction across 6 real repositories. These are the project’s claimed metrics; I have not independently benchmarked them.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The workflow&lt;/strong&gt;: &lt;code&gt;pip install code-review-graph&lt;/code&gt;, then &lt;code&gt;code-review-graph install&lt;/code&gt; (auto-detects Claude Code and other supported platforms, writes MCP config), then &lt;code&gt;code-review-graph build&lt;/code&gt; to parse the codebase. The tool auto-discovers supported AI platforms and installs platform-native hooks without manual config editing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: The structural graph must be rebuilt or incrementally updated after large refactors. The README covers incremental tracking for routine changes but does not describe behavior on major directory restructures in detail.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;lukasniessenterrashark--grounding-terraform-generation-in-hashicorps-actual-best-practices&quot;&gt;LukasNiessen/terrashark — grounding Terraform generation in HashiCorp’s actual best practices&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: LLMs generating Terraform hallucinate resource arguments, use deprecated syntax, and produce module structures that fail validation or drift from team conventions — requiring engineers to manually review and correct IaC before it can run.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How AI replaces that task&lt;/strong&gt;: TerraShark is a Claude Code and Codex skill that injects Terraform best practices directly into the agent’s context at the skill layer. The README states it is based on HashiCorp’s official recommended practices and includes good, bad, and neutral examples so the agent avoids common Terraform mistakes. It is also described as aggressively token-optimized: “most Terraform skills dump huge text-of-walls onto the agent and burn expensive tokens — TerraShark was aggressively de-duplicated and optimized for maximum quality per token.”&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The workflow&lt;/strong&gt;: Clone to &lt;code&gt;~/.claude/skills/terrashark&lt;/code&gt; — Claude Code auto-discovers skills in that directory with no restart required. Alternatively, install via the Claude Code plugin marketplace: &lt;code&gt;/plugin marketplace add LukasNiessen/terrashark&lt;/code&gt; then &lt;code&gt;/plugin install terrashark&lt;/code&gt;. The skill activates whenever Terraform code is being generated or reviewed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: TerraShark addresses generation quality, not state management or plan validation. An agent using it still needs &lt;code&gt;terraform plan&lt;/code&gt; in CI to catch provider-specific behaviors not covered by general HashiCorp guidelines.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;guepard-corpgfs--bringing-git-style-version-control-to-database-changes-made-by-ai-agents&quot;&gt;Guepard-Corp/gfs — bringing Git-style version control to database changes made by AI agents&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: When an AI agent modifies a database schema or migrates data, there is no audit trail and no rollback. A wrong change requires manual reconstruction.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How AI replaces that task&lt;/strong&gt;: GFS (Git For database Systems) applies Git-like semantics to database state: commit, branch, rollback, and time-travel through database history. The README explicitly frames this as an AI safety feature: “automatic snapshots protect against agent mistakes and data loss.” It exposes an MCP server so Claude Code, Cursor, Cline, Windsurf, and other MCP-compatible agents can snapshot database state before changes and roll back if something goes wrong. It uses Docker to manage isolated database environments. Supported databases per the repository topics include PostgreSQL, MySQL, and ClickHouse.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The workflow&lt;/strong&gt;: Wire the GFS MCP server into your agent. Before a schema change, the agent commits current state; if the change fails, rollback is one command. Branching lets agents experiment on isolated database copies without touching the main state.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: The README includes an explicit warning: “This project is under active development. Expect changes, incomplete features, and evolving APIs.” GFS is a compelling concept but not yet production-stable; treat it as early-stage infrastructure that warrants close monitoring.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;All three descriptions are grounded in each repository’s README as of February 2026. The token reduction figures for &lt;code&gt;code-review-graph&lt;/code&gt; come from a diagram and the repository description — these are the project’s claimed metrics, not independently benchmarked here. TerraShark’s characterization as “The #1 Terraform skill for Claude Code and Codex, measured by GitHub stars” is stated verbatim in the README. GFS’s AI safety framing and MCP integration are documented; the active development warning is quoted directly from the repository.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;code-review-graph graph goes stale after major refactor&lt;/td&gt;&lt;td&gt;Large-scale directory restructuring without a rebuild&lt;/td&gt;&lt;td&gt;Run &lt;code&gt;code-review-graph build&lt;/code&gt; after significant changes; add as a CI step&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;TerraShark skill doesn’t catch provider-specific hallucinations&lt;/td&gt;&lt;td&gt;Behaviors not covered in HashiCorp general practices&lt;/td&gt;&lt;td&gt;Run &lt;code&gt;terraform validate&lt;/code&gt; and &lt;code&gt;terraform plan&lt;/code&gt; in CI as a second gate&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GFS rollback fails in shared database environments&lt;/td&gt;&lt;td&gt;Multiple agents writing concurrently with no locking&lt;/td&gt;&lt;td&gt;Run GFS against isolated Docker databases, not shared staging instances&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;code-review-graph MCP config silently breaks after agent platform update&lt;/td&gt;&lt;td&gt;MCP config format changes in the AI coding tool&lt;/td&gt;&lt;td&gt;Re-run &lt;code&gt;code-review-graph install&lt;/code&gt; after updating the AI coding platform&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: AI coding agents waste tokens on irrelevant context, hallucinate Terraform configurations, and leave no recovery path when they modify database state — all of which require human intervention to clean up.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: &lt;code&gt;code-review-graph&lt;/code&gt; delivers precise codebase context to agents via MCP; TerraShark grounds Terraform generation in HashiCorp best practices; GFS adds Git-style snapshots to database changes made by agents.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Run &lt;code&gt;code-review-graph build&lt;/code&gt; on your most active repository, open a PR review task, and compare token usage before and after — what the agent loads versus what it would have loaded without the graph is the signal.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: &lt;code&gt;pip install code-review-graph &amp;#x26;&amp;#x26; code-review-graph install &amp;#x26;&amp;#x26; code-review-graph build&lt;/code&gt;. Then ask your agent to review the last merged PR. Watch what context it loads. That is the week-one win.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>ai-engineering</category><category>databases</category><category>architecture</category></item><item><title>Why Is This Elasticsearch Query Slow? Search Profiling and LLM Analysis</title><link>https://rajivonai.com/blog/2026-03-05-why-is-this-elasticsearch-query-slow-search-profiling-and-llm-analysis/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-03-05-why-is-this-elasticsearch-query-slow-search-profiling-and-llm-analysis/</guid><description>A deep dive into diagnosing slow Elasticsearch queries: using the Search Profile API to separate query, fetch, and aggregation phases from thread queue delays and network transit.</description><pubDate>Thu, 05 Mar 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A slow Elasticsearch search request does not prove that Lucene execution is slow, a low query-phase duration does not rule out fetch overhead, and the Search Profile API cannot measure thread queue delays. Diagnosing search latency requires breaking execution down into query scoring, fetch serialization, shard fan-out, and coordinating node reduction.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;When an enterprise search, recommendation, or analytics endpoint experiences latency degradation in Elasticsearch 8.x, application logs report aggregate HTTP round-trip times (e.g., &lt;code&gt;took: 1250ms&lt;/code&gt;). Within the cluster, however, a single search request coordinates a two-phase distributed workflow across dozens of shards, multiple node tiers, and internal thread pools.&lt;/p&gt;
&lt;p&gt;Elasticsearch provides the Search Profile API (&lt;code&gt;&quot;profile&quot;: true&lt;/code&gt;), the Search Slow Log (&lt;code&gt;index.search.slowlog&lt;/code&gt;), and Node Stats (&lt;code&gt;_nodes/stats/indices/search&lt;/code&gt;) to diagnose query bottlenecks. The Profile API exposes nanosecond-level breakdowns of Lucene query components (&lt;code&gt;TermQuery&lt;/code&gt;, &lt;code&gt;BooleanQuery&lt;/code&gt;, &lt;code&gt;PointRangeQuery&lt;/code&gt;), scorer initialization, collector execution, aggregation trees, and fetch phases.&lt;/p&gt;
&lt;p&gt;However, interpreting search profiles in production requires strict operational rigor:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Profiling Overhead:&lt;/strong&gt; Enabling &lt;code&gt;&quot;profile&quot;: true&lt;/code&gt; adds significant instrumentation overhead, expanding execution times and altering CPU cache locality.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Incomplete Evidence Boundary:&lt;/strong&gt; The Profile API measures only in-thread Lucene execution time within shard workers; it completely omits search thread pool queue wait times, network transport delays, and coordinating node merge overhead.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Query vs. Filter Semantics:&lt;/strong&gt; Misunderstanding the distinction between scored query contexts (&lt;code&gt;must&lt;/code&gt;, &lt;code&gt;should&lt;/code&gt;) and cached filter contexts (&lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;must_not&lt;/code&gt;) leads to unnecessary computation and wasted node query cache capacity.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;An LLM assists by ingesting sanitized search profiles, slowlog entries, and shard metadata to isolate algorithmic bottlenecks—such as unoptimized bucket aggregations or massive fetch payloads—without guessing about missing transport and queueing telemetry.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Slow search requests in Elasticsearch stem from distinct architectural mechanisms across the distributed execution pipeline:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Query Phase Scoring vs. Filter Caching:&lt;/strong&gt; Running term or range lookups in &lt;code&gt;must&lt;/code&gt; clauses forces Lucene to compute relevance scores (&lt;code&gt;BM25&lt;/code&gt;) and instantiate costly scorers (&lt;code&gt;build_scorer&lt;/code&gt;, &lt;code&gt;score&lt;/code&gt;) for every document, bypassing the Node Query Cache which only caches bitsets for &lt;code&gt;filter&lt;/code&gt; contexts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fetch Phase Amplification:&lt;/strong&gt; The query phase returns only document IDs and sort values. The fetch phase then retrieves document &lt;code&gt;_source&lt;/code&gt;, stored fields, and highlights from disk. Requesting large result sets (&lt;code&gt;size: 1000&lt;/code&gt;), deep pagination (&lt;code&gt;from: 10000&lt;/code&gt;), or heavy highlighting (&lt;code&gt;unified&lt;/code&gt; on multi-megabyte text fields) makes the fetch phase dramatically slower than the query phase.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;High-Cardinality Aggregation Explosion:&lt;/strong&gt; Bucket aggregations (&lt;code&gt;terms&lt;/code&gt;) on high-cardinality fields without &lt;code&gt;composite&lt;/code&gt; pagination or appropriate &lt;code&gt;execution_hint&lt;/code&gt; parameters build massive memory structures during the &lt;code&gt;collect&lt;/code&gt; and &lt;code&gt;reduce&lt;/code&gt; phases, saturating JVM heap and locking worker threads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Excessive Shard Fan-Out:&lt;/strong&gt; Executing wildcards across hundreds of daily indices (e.g., &lt;code&gt;logs-*&lt;/code&gt;) forces the coordinating node to dispatch requests to hundreds of shards simultaneously. The request latency is bounded by the slowest shard, while the coordinating node suffers CPU exhaustion merging priority queues.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Queue and Transit Blind Spot:&lt;/strong&gt; A query reporting 15 ms of total Lucene profile execution time can still take 1,200 ms to return to the client if it spent 1,150 ms waiting in a saturated &lt;code&gt;search&lt;/code&gt; thread pool queue or transferring a 50 MB response payload over the network.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;How do we systematically analyze an Elasticsearch search profile to determine whether slowness originates in Lucene scoring, aggregation collection, fetch serialization, shard fan-out, or upstream thread queue delays?&lt;/p&gt;
&lt;h2 id=&quot;build-a-profile-aware-search-evidence-pack&quot;&gt;Build a Profile-Aware Search Evidence Pack&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[client p99 search latency and slowlog entries] --&gt; E[time-bounded Elasticsearch search evidence pack]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B[sanitized Search Profile API output query fetch and aggs] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[search slow log thresholds and node stats cache hit rates] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[index mappings shard routing and thread pool search queue] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[isolate query phase vs fetch phase vs aggregation phase duration]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[reconcile profile execution time with client round-trip took time]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[LLM observations hypotheses contradictions and gaps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[Search Engineer and DBA verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[query refactoring validation and rollback]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To diagnose slow queries safely, assemble an evidence pack capturing the query payload, index mappings, and execution profiles from a staging environment or non-peak production replica:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Sanitized Search Profile (&lt;code&gt;POST &amp;#x3C;index&gt;/_search&lt;/code&gt; with &lt;code&gt;&quot;profile&quot;: true&lt;/code&gt;):&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Query Phase:&lt;/strong&gt; Per-shard breakdown showing &lt;code&gt;type&lt;/code&gt; (&lt;code&gt;TermQuery&lt;/code&gt;, &lt;code&gt;BooleanQuery&lt;/code&gt;, &lt;code&gt;PointRangeQuery&lt;/code&gt;), &lt;code&gt;time_in_nanoseconds&lt;/code&gt;, and phase breakdown (&lt;code&gt;score&lt;/code&gt;, &lt;code&gt;build_scorer&lt;/code&gt;, &lt;code&gt;create_weight&lt;/code&gt;, &lt;code&gt;next_doc&lt;/code&gt;, &lt;code&gt;advance&lt;/code&gt;, &lt;code&gt;match&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rewrite Time:&lt;/strong&gt; Nanoseconds spent rewriting complex queries into primitive Lucene queries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Aggregation Phase:&lt;/strong&gt; Breakdown of &lt;code&gt;build_aggregation&lt;/code&gt;, &lt;code&gt;collect&lt;/code&gt;, and &lt;code&gt;reduce&lt;/code&gt; times across the aggregation tree.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fetch Phase:&lt;/strong&gt; Shard-level fetch timing, including &lt;code&gt;fetch_source&lt;/code&gt;, &lt;code&gt;fetch_stored_fields&lt;/code&gt;, and &lt;code&gt;fetch_highlight&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Search Slow Log Samples:&lt;/strong&gt; Extract &lt;code&gt;took_millis&lt;/code&gt;, &lt;code&gt;total_shards&lt;/code&gt;, &lt;code&gt;total_hits&lt;/code&gt;, query string, and aggregation structure from &lt;code&gt;index.search.slowlog&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cache and Shard Telemetry:&lt;/strong&gt; Node Query Cache hit/miss rates (&lt;code&gt;indices.queries.cache.hit_count&lt;/code&gt;), Shard Request Cache metrics (&lt;code&gt;indices.requests.cache.hit_count&lt;/code&gt;), and total shard count hit by the query.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Thread Pool Search Queues:&lt;/strong&gt; Search queue depth and rejection rates on target data nodes at the time of query execution.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The LLM processes this sanitized bundle to produce four distinct diagnostic artifacts:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Observations:&lt;/strong&gt; Quantitative facts tied to specific query nodes (e.g., “The &lt;code&gt;terms&lt;/code&gt; aggregation on &lt;code&gt;user_id&lt;/code&gt; accounted for 88.4% of total shard execution time (420 ms of 475 ms total), with &lt;code&gt;collect&lt;/code&gt; consuming 390 ms across 1.2 million matching documents”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hypotheses:&lt;/strong&gt; Ranked explanations identifying the exact algorithmic bottleneck.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Missing Evidence:&lt;/strong&gt; Unobserved dimensions (e.g., “Need client round-trip &lt;code&gt;took&lt;/code&gt; vs total profile time to determine if the 800 ms discrepancy represents &lt;code&gt;search&lt;/code&gt; thread pool queue latency”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Actions:&lt;/strong&gt; Specific query rewrites, mapping adjustments, and cache optimization steps.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Plausible failure classes&lt;/th&gt;&lt;th&gt;Evidence that separates them&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Query phase takes &gt;90% of time&lt;/td&gt;&lt;td&gt;Unindexed field scan, complex regex/wildcard, un-cached score computation&lt;/td&gt;&lt;td&gt;Profile &lt;code&gt;query&lt;/code&gt; breakdown, &lt;code&gt;build_scorer&lt;/code&gt; vs &lt;code&gt;match&lt;/code&gt;, absence of &lt;code&gt;filter&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Fetch phase takes &gt;90% of time&lt;/td&gt;&lt;td&gt;Large &lt;code&gt;size&lt;/code&gt;, deep &lt;code&gt;from&lt;/code&gt; pagination, heavy highlighting, large &lt;code&gt;_source&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Profile &lt;code&gt;fetch&lt;/code&gt; breakdown, &lt;code&gt;fetch_source&lt;/code&gt; / &lt;code&gt;fetch_highlight&lt;/code&gt; duration&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aggregations take &gt;90% of time&lt;/td&gt;&lt;td&gt;High-cardinality &lt;code&gt;terms&lt;/code&gt;, deep nesting, unoptimized bucket scripts&lt;/td&gt;&lt;td&gt;Profile &lt;code&gt;aggregations&lt;/code&gt; breakdown, &lt;code&gt;collect&lt;/code&gt; vs &lt;code&gt;build_aggregation&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Client &lt;code&gt;took&lt;/code&gt; &gt;&gt; Profile total time&lt;/td&gt;&lt;td&gt;&lt;code&gt;search&lt;/code&gt; thread pool queue wait, network transit, coordinating merge&lt;/td&gt;&lt;td&gt;&lt;code&gt;thread_pool.search.queue&lt;/code&gt;, response payload size, shard count&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Shard execution time highly skewed&lt;/td&gt;&lt;td&gt;Data skew on one node, hot shard, un-cached cold shard on spinning disk&lt;/td&gt;&lt;td&gt;Per-shard profile breakdown comparison, &lt;code&gt;_cat/shards&lt;/code&gt; distribution&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Latency increases on repeated queries&lt;/td&gt;&lt;td&gt;Scoring context preventing Node Query Cache utilization&lt;/td&gt;&lt;td&gt;&lt;code&gt;indices.queries.cache.miss_count&lt;/code&gt; delta, query structure in &lt;code&gt;must&lt;/code&gt; vs &lt;code&gt;filter&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Slow Elasticsearch search alert] --&gt; B[1. Reconcile client took time against total shard profile duration]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[2. Deconstruct query phase scoring vs filter context caching]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[3. Inspect fetch phase overhead document size and highlighting]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[4. Evaluate aggregation tree collection depth and cardinality]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[5. Check shard fan-out and coordinating node reduction cost]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Synthesize observations hypotheses and query refactoring steps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-reconcile-client-took-time-against-total-shard-profile-duration&quot;&gt;1. Reconcile client took time against total shard profile duration&lt;/h3&gt;
&lt;p&gt;Compare the client-perceived elapsed time (&lt;code&gt;took_millis&lt;/code&gt; in slowlog or HTTP response) with the sum of max shard times reported by the Profile API:
$$\Delta \text{Overhead} = \text{Client_Took} - \max(\text{Shard_Profile_Times})$$&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If $\Delta \text{Overhead}$ exceeds 50% of total elapsed time, the query is suffering from coordinating node queueing, thread pool starvation, or network serialization. Optimizing Lucene query clauses will not solve the issue.&lt;/li&gt;
&lt;li&gt;If $\Delta \text{Overhead}$ is small, the bottleneck resides entirely inside the shard execution engine.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-deconstruct-query-phase-scoring-vs-filter-context-caching&quot;&gt;2. Deconstruct query phase scoring vs. filter context caching&lt;/h3&gt;
&lt;p&gt;Analyze the &lt;code&gt;query&lt;/code&gt; block in the search profile:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Scoring Overhead:&lt;/strong&gt; Look for high &lt;code&gt;build_scorer&lt;/code&gt; and &lt;code&gt;score&lt;/code&gt; times in &lt;code&gt;TermQuery&lt;/code&gt; or &lt;code&gt;BooleanQuery&lt;/code&gt;. If relevance ranking is not required (e.g., exact matches on status codes, tenant IDs, or date ranges), move those clauses from &lt;code&gt;must&lt;/code&gt;/&lt;code&gt;should&lt;/code&gt; into a &lt;code&gt;filter&lt;/code&gt; or &lt;code&gt;must_not&lt;/code&gt; block.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cache Bitset Verification:&lt;/strong&gt; Clauses inside &lt;code&gt;filter&lt;/code&gt; contexts generate compact Lucene Roaring Doc Id Sets cached in the Node Query Cache, reducing repeated execution time from milliseconds to microseconds.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-inspect-fetch-phase-overhead-document-size-and-highlighting&quot;&gt;3. Inspect fetch phase overhead, document size, and highlighting&lt;/h3&gt;
&lt;p&gt;Review the &lt;code&gt;fetch&lt;/code&gt; block in the profile output:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;fetch_source&lt;/code&gt; Latency:&lt;/strong&gt; If &lt;code&gt;fetch_source&lt;/code&gt; consumes hundreds of milliseconds, the query is retrieving full JSON documents for hundreds of hits. Use &lt;code&gt;_source_includes&lt;/code&gt; to retrieve only required fields, or set &lt;code&gt;_source: false&lt;/code&gt; for pure aggregation queries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Highlighting Costs:&lt;/strong&gt; Inspect &lt;code&gt;fetch_highlight&lt;/code&gt;. Plain highlighters re-analyze and re-score the entire text field at fetch time. Replace with the &lt;code&gt;unified&lt;/code&gt; highlighter using stored term vectors (&lt;code&gt;with_positions_offsets&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;4-evaluate-aggregation-tree-collection-depth-and-cardinality&quot;&gt;4. Evaluate aggregation tree collection depth and cardinality&lt;/h3&gt;
&lt;p&gt;Inspect the &lt;code&gt;aggregations&lt;/code&gt; section of the profile:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;collect&lt;/code&gt; vs. &lt;code&gt;build_aggregation&lt;/code&gt;:&lt;/strong&gt; High &lt;code&gt;collect&lt;/code&gt; time indicates that millions of matching documents are being evaluated per bucket.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Execution Hints:&lt;/strong&gt; For &lt;code&gt;terms&lt;/code&gt; aggregations on keyword fields, evaluate &lt;code&gt;execution_hint: &quot;map&quot;&lt;/code&gt; vs &lt;code&gt;&quot;global_ordinals&quot;&lt;/code&gt;. Ensure fields used in aggregations have doc values enabled (default for &lt;code&gt;keyword&lt;/code&gt; and numeric fields).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Composite Aggregations:&lt;/strong&gt; If paginating through bucket results, replace deep nested aggregations with &lt;code&gt;composite&lt;/code&gt; aggregations to stream buckets efficiently without memory explosion.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;5-check-shard-fan-out-and-coordinating-node-reduction-cost&quot;&gt;5. Check shard fan-out and coordinating node reduction cost&lt;/h3&gt;
&lt;p&gt;Evaluate the total number of shards targeted by the query:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Inspect &lt;code&gt;total_shards&lt;/code&gt; in the slowlog or search response. If a query hits 200+ shards, coordinating node reduction time climbs quadratically.&lt;/li&gt;
&lt;li&gt;Use date-math index filtering (e.g., &lt;code&gt;&amp;#x3C;logs-{now/d}&gt;&lt;/code&gt;) or custom routing keys (&lt;code&gt;routing: &quot;tenant_id&quot;&lt;/code&gt;) to prune shard fan-out before query execution.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Slow Elasticsearch search request] --&gt; B{Client took time much greater than profile sum}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes| C[Inspect search thread pool queue and coordinating node merge]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No| D{Query phase accounts for majority of profile time}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Yes| E{Clauses in scoring must context instead of filter}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|Yes| F[Move non-scored clauses to filter block to leverage query cache]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|No| G[Inspect regex wildcard or unindexed text query complexity]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|No| H{Fetch phase accounts for majority of profile time}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|Yes| I[Reduce size parameter prune source fields and optimize highlighters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|No| J[Profile terms aggregation and switch to composite or doc values]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/search-profile.html&quot;&gt;official Elasticsearch Profile API documentation&lt;/a&gt; defines the nanosecond timing breakdown of Lucene queries and aggregations. Elastic explicitly warns that profiling adds significant overhead and should be executed in controlled environments. Furthermore, documentation confirms that profiling measures only in-shard execution and excludes coordinating merge and transport layers.&lt;/p&gt;
&lt;p&gt;According to &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html&quot;&gt;Elasticsearch Query and Filter Context documentation&lt;/a&gt;, clauses in &lt;code&gt;filter&lt;/code&gt; context do not calculate relevance scores and are automatically cached in the Node Query Cache. Scoring queries in &lt;code&gt;must&lt;/code&gt; context must execute scoring algorithms for every matching document on every request.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html&quot;&gt;Elasticsearch Aggregations Reference&lt;/a&gt; specifies that global ordinals are used to optimize &lt;code&gt;terms&lt;/code&gt; aggregations on keyword fields by mapping term strings to integer ordinals in memory. Building global ordinals on high-cardinality fields can introduce multi-second latency on the first search request after a segment refresh.&lt;/p&gt;
&lt;p&gt;Documented &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html&quot;&gt;Search Sizing and Pagination best practices&lt;/a&gt; establish that paginating past 10,000 hits using &lt;code&gt;from + size&lt;/code&gt; is blocked by default (&lt;code&gt;index.max_result_window&lt;/code&gt;) because it forces every shard to build a priority queue of &lt;code&gt;from + size&lt;/code&gt; documents. The documented architecture pattern mandates using &lt;code&gt;search_after&lt;/code&gt; with point-in-time (&lt;code&gt;PIT&lt;/code&gt;) readers for deep pagination.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven root cause&lt;/th&gt;&lt;th&gt;Candidate remediation&lt;/th&gt;&lt;th&gt;Validation criteria&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Scoring computation on non-scored fields&lt;/td&gt;&lt;td&gt;Move boolean clauses from &lt;code&gt;must&lt;/code&gt; to &lt;code&gt;filter&lt;/code&gt; in query DSL&lt;/td&gt;&lt;td&gt;Profile &lt;code&gt;build_scorer&lt;/code&gt; drops to 0; Query Cache hit rate climbs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Fetch phase slowed by large &lt;code&gt;_source&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Restrict returned fields using &lt;code&gt;_source: [&quot;id&quot;, &quot;title&quot;]&lt;/code&gt; or disable source&lt;/td&gt;&lt;td&gt;Profile &lt;code&gt;fetch_source&lt;/code&gt; time drops by &gt;80%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Highlighting latency on large text fields&lt;/td&gt;&lt;td&gt;Add &lt;code&gt;index_options: &quot;offsets&quot;&lt;/code&gt; to mapping; use &lt;code&gt;unified&lt;/code&gt; highlighter&lt;/td&gt;&lt;td&gt;Profile &lt;code&gt;fetch_highlight&lt;/code&gt; time drops below 5 ms&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High-cardinality aggregation memory surge&lt;/td&gt;&lt;td&gt;Switch from standard &lt;code&gt;terms&lt;/code&gt; to &lt;code&gt;composite&lt;/code&gt; aggregation pagination&lt;/td&gt;&lt;td&gt;Shard aggregation &lt;code&gt;collect&lt;/code&gt; time drops; heap usage normalizes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Deep pagination freezing coordinating node&lt;/td&gt;&lt;td&gt;Replace &lt;code&gt;from: 5000&lt;/code&gt; with &lt;code&gt;search_after&lt;/code&gt; and Point-in-Time (&lt;code&gt;PIT&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;Coordinating node CPU drops; p99 latency stabilizes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Excessive shard fan-out across daily indices&lt;/td&gt;&lt;td&gt;Add shard routing keys or narrow date ranges in index aliases&lt;/td&gt;&lt;td&gt;&lt;code&gt;total_shards&lt;/code&gt; per query drops from 100+ to &amp;#x3C;10&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;When deploying query refactorings and index mapping optimizations, maintain an explicit reversal strategy:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Client Application Feature Flags:&lt;/strong&gt; Gate search DSL changes (such as switching to &lt;code&gt;filter&lt;/code&gt; blocks, &lt;code&gt;search_after&lt;/code&gt;, or &lt;code&gt;_source&lt;/code&gt; filtering) behind application-side feature flags. Revert to baseline query DSL instantly if client response parsing breaks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Index Mapping Updates:&lt;/strong&gt; Field mapping updates (e.g., adding &lt;code&gt;index_options: &quot;offsets&quot;&lt;/code&gt; or keyword multi-fields) require creating a new index mapping and reindexing or creating an updated index template. Keep prior index templates active until verification completes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Slow Log Threshold Rollback:&lt;/strong&gt; If lowering &lt;code&gt;index.search.slowlog.threshold&lt;/code&gt; generates excessive disk I/O, restore thresholds using &lt;code&gt;PUT &amp;#x3C;index&gt;/_settings&lt;/code&gt;:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;index.search.slowlog.threshold.query.warn&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;10s&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;index.search.slowlog.threshold.query.info&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;5s&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Point-in-Time (PIT) Resource Cleanup:&lt;/strong&gt; If using &lt;code&gt;search_after&lt;/code&gt; with PIT readers, ensure client applications explicitly close PIT sessions via &lt;code&gt;DELETE _pit&lt;/code&gt; to release Lucene segment memory immediately.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why naive reasoning fails&lt;/th&gt;&lt;th&gt;Better diagnostic boundary&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;”Profile shows 10ms, but query took 1s”&lt;/td&gt;&lt;td&gt;Profile omits thread queue wait time, transport lag, and coordinating merge&lt;/td&gt;&lt;td&gt;Check &lt;code&gt;search&lt;/code&gt; thread pool queue and response payload size&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Must and filter do the same thing”&lt;/td&gt;&lt;td&gt;&lt;code&gt;must&lt;/code&gt; scores every doc; &lt;code&gt;filter&lt;/code&gt; caches bitsets and skips scoring&lt;/td&gt;&lt;td&gt;Check &lt;code&gt;build_scorer&lt;/code&gt; in profile and query cache hit stats&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Adding more shards speeds up slow query”&lt;/td&gt;&lt;td&gt;Single query latency is bounded by the slowest shard; more shards increase merge cost&lt;/td&gt;&lt;td&gt;Prune shard count and use custom routing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Aggregations are slow, need more heap”&lt;/td&gt;&lt;td&gt;High-cardinality terms aggregations can overwhelm any heap size&lt;/td&gt;&lt;td&gt;Use &lt;code&gt;composite&lt;/code&gt; aggregation or pre-aggregate data&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Highlighting is just string formatting”&lt;/td&gt;&lt;td&gt;Plain highlighters re-score and re-analyze entire field from disk&lt;/td&gt;&lt;td&gt;Use stored offsets with unified highlighter&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Search Profile can run in production”&lt;/td&gt;&lt;td&gt;Profiling instruments internal loops, distorting timing and adding CPU load&lt;/td&gt;&lt;td&gt;Profile in staging or on dedicated non-production replica&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;To prevent production outages, data leakage, and inaccurate diagnoses, strict boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Search Term Ingestion:&lt;/strong&gt; The LLM must not receive unredacted search query strings containing user names, personal identifiers, or authentication tokens. All search terms must be masked before analysis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Speculation on Missing Queue Telemetry:&lt;/strong&gt; If search profile execution times are low but client latency is high, the LLM must explicitly list search thread pool queue depth and network transport as missing evidence rather than guessing that Lucene is slow.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Direct Execution of Reindexing:&lt;/strong&gt; The LLM cannot execute &lt;code&gt;POST _reindex&lt;/code&gt;, delete index templates, or modify cluster settings autonomously.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Search Traffic Rerouting:&lt;/strong&gt; The LLM cannot dynamically redirect client search traffic between cluster aliases or node tiers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Slow Elasticsearch searches present multi-layered symptoms where Lucene scoring, fetch serialization, aggregation collection, and coordinating thread queues overlap.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Construct a profile-aware evidence pack decomposing search execution into query scoring, fetch latency, aggregation trees, and queue wait times.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Reconcile client &lt;code&gt;took&lt;/code&gt; against shard profile totals, verify filter caching in the Node Query Cache, and validate fetch phase duration against document payload sizes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Deploy search slowlog instrumentation across slow indices. Having completed the Elasticsearch track, proceed to the Vector Search and RAG track to evaluate recall versus latency tradeoffs in approximate nearest neighbor retrieval.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/search-profile.html&quot;&gt;Elasticsearch Search Profile API Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html&quot;&gt;Elasticsearch Query and Filter Context Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html&quot;&gt;Elasticsearch Search Aggregations Reference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-slowlog.html&quot;&gt;Elasticsearch Search Slow Log Configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html&quot;&gt;Elasticsearch Pagination and Search After Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/highlighting.html&quot;&gt;Elasticsearch Highlighting Architecture and Performance&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>ai-engineering</category><category>failures</category></item><item><title>AWS RDS Oracle and SQL Server: The License Cost Nobody Talks About</title><link>https://rajivonai.com/blog/2026-03-04-aws-rds-oracle-sql-server-license-cost/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-03-04-aws-rds-oracle-sql-server-license-cost/</guid><description>Why the default License-Included model on AWS RDS is a financial trap for enterprise database workloads.</description><pubDate>Wed, 04 Mar 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The ease of provisioning a commercial database on AWS RDS masks a massive premium that compounds hourly.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Teams migrating quickly to the cloud often use AWS RDS for their existing Oracle or SQL Server workloads. During the provisioning wizard, they accept the default “License Included” pricing model to avoid the bureaucratic hassle of license procurement.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;“License Included” pricing bundles the compute cost with the software license cost. However, AWS applies a significant markup. For Oracle Enterprise Edition or SQL Server Enterprise, the license component of the RDS hourly rate can exceed the cost of the underlying EC2 compute by 3x to 5x.&lt;/p&gt;
&lt;h2 id=&quot;the-bring-your-own-license-byol-alternative&quot;&gt;The Bring Your Own License (BYOL) Alternative&lt;/h2&gt;
&lt;p&gt;AWS offers a BYOL model, but it comes with stringent requirements. For Oracle, you must ensure you are adhering to the Oracle Cloud Policy, which changes how core factors are calculated. For SQL Server, Microsoft’s licensing terms often require moving to EC2 Dedicated Hosts to fully realize the value of your Software Assurance.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;A documented pattern among enterprise migrations is that running commercial engines on RDS License Included is financially unsustainable at scale. Organizations that perform a licensing audit before migration often discover they can leverage existing Enterprise Agreements via BYOL, cutting their RDS spend drastically.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Strategy&lt;/th&gt;&lt;th&gt;Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;EC2 Dedicated Hosts&lt;/td&gt;&lt;td&gt;Reduces SQL Server licensing costs but shifts the burden of high availability, patching, and backups back to your DBA team, eliminating the benefits of RDS.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Oracle Core Factor&lt;/td&gt;&lt;td&gt;Oracle does not recognize AWS hyper-threading as equivalent to physical cores, meaning you often need to purchase twice as many licenses to cover the same vCPU footprint.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: RDS License Included pricing is punitively expensive for enterprise databases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Audit existing licenses and evaluate BYOL on RDS or EC2 Dedicated Hosts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: BYOL architectures routinely save 40-50% on AWS commercial database bills.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Compare your potential savings using our &lt;a href=&quot;https://rajivonai.com/tools/sql-server-license-calculator/&quot;&gt;SQL Server Cloud Licensing Calculator&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>failures</category></item><item><title>LLM-Assisted Elasticsearch Performance Troubleshooting: Heap, Shards, CPU and Search</title><link>https://rajivonai.com/blog/2026-03-03-llm-assisted-elasticsearch-performance-troubleshooting-heap-shards-cpu-search/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-03-03-llm-assisted-elasticsearch-performance-troubleshooting-heap-shards-cpu-search/</guid><description>A cluster-level diagnostic guide for Elasticsearch: isolating JVM garbage collection stalls, thread pool rejections, over-sharding bloat, disk watermarks, and circuit breakers.</description><pubDate>Tue, 03 Mar 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A cluster in yellow status is an allocation symptom, a peaking JVM heap is normal Java runtime behavior while a heap that never recovers is not, and thread pool rejections reflect upstream queue starvation rather than slow hardware. Triage begins by isolating garbage collection pauses, Lucene segment merge pressure, and shard-count bloat before tuning queries.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Elasticsearch 8.x clusters power distributed search, log analytics, security observability, and enterprise retrieval. In high-throughput architectures, a cluster coordinates master-eligible nodes, dedicated ingest nodes, coordinating routers, and tier-specific data nodes (Hot, Warm, Cold, Frozen). When search latency spikes or indexing pipelines back up, the cluster surfaces hundreds of interdependent signals across JVM heaps, Lucene segment stores, thread pools, and operating system caches.&lt;/p&gt;
&lt;p&gt;Elasticsearch exposes rich internal diagnostics through REST APIs (&lt;code&gt;_cluster/health&lt;/code&gt;, &lt;code&gt;_cluster/state&lt;/code&gt;, &lt;code&gt;_nodes/stats&lt;/code&gt;, &lt;code&gt;_cat/shards&lt;/code&gt;, &lt;code&gt;_cat/thread_pool&lt;/code&gt;, &lt;code&gt;_nodes/hot_threads&lt;/code&gt;, and &lt;code&gt;_cluster/allocation/explain&lt;/code&gt;). Linux hosts provide I/O wait, memory mapping (&lt;code&gt;vm.max_map_count&lt;/code&gt;), and network socket metrics.&lt;/p&gt;
&lt;p&gt;The operational challenge is multi-node correlation. When an indexing microservice receives HTTP 429 (&lt;code&gt;es_rejected_execution_exception&lt;/code&gt;), the root cause could be Lucene segment merge throttling, excessive shard count exhausting heap memory, G1GC garbage collection pauses, or disk flood-stage watermarks. An LLM acts as an evidence correlator: ingesting structured, synchronized cluster snapshots, eliminating contradictory hypotheses, identifying unobserved failure dimensions, and producing auditable remediation plans for human authorization.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Elasticsearch’s distributed, Lucene-backed architecture exhibits failure modes that deceive generic database troubleshooting:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Heap Occupancy vs. Garbage Collection Pressure:&lt;/strong&gt; Brief heap excursions into the 75–85% range are expected in Elasticsearch as the GC sawtooth peaks before a collection, driven by Lucene term dictionaries, request caches, and node buffers. Sustained old-generation pressure near or above 85% is a troubleshooting condition, not a steady state to normalize. True memory pressure manifests as rising GC pause frequency (&lt;code&gt;jvm.gc.collectors.young/old&lt;/code&gt;), stop-the-world stalls, or tripping circuit breakers (&lt;code&gt;CircuitBreakerException: [parent] Data too large&lt;/code&gt;). Distinguish the two by whether occupancy &lt;em&gt;recovers&lt;/em&gt; after each old-generation collection: a sawtooth that returns to a low baseline is healthy; a sawtooth whose floor keeps rising is not.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Over-Sharding Multiplier:&lt;/strong&gt; Allocating thousands of small (e.g., 200 MB) shards across a cluster wastes gigabytes of heap on Lucene index readers, Term Index FSTs, and open segment descriptors. During queries, shard fan-out consumes dozens of coordinating threads, creating catastrophic queueing delays even under light traffic.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Thread Pool Rejection vs. CPU Saturation:&lt;/strong&gt; HTTP 429 errors on the &lt;code&gt;write&lt;/code&gt; or &lt;code&gt;search&lt;/code&gt; thread pools indicate queue exhaustion (&lt;code&gt;rejected &gt; 0&lt;/code&gt;), not necessarily 100% CPU utilization. Rejections often occur when Lucene background segment merges saturate disk I/O or when unoptimized aggregations hold worker threads hostage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compressed OOPs and Sizing Traps:&lt;/strong&gt; Above a JVM- and host-dependent threshold, Java disables Compressed Object Pointers (Compressed OOPs), expanding pointer overhead from 32-bit to 64-bit and effectively reducing usable heap space while starving the OS filesystem cache of RAM needed for Lucene off-heap search. The cutoff is not a single universal number — Elastic describes roughly 26 GB as safe on most systems, with the limit reaching approximately 30 GB depending on JVM and memory layout. Verify the actual state rather than assuming a boundary: &lt;code&gt;GET _nodes/jvm&lt;/code&gt; reports &lt;code&gt;using_compressed_ordinary_object_pointers&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Disk Watermark Lockouts:&lt;/strong&gt; When disk utilization on a data node crosses the low (85%), high (90%), or flood-stage (95%) watermark, Elasticsearch automatically halts shard allocation, initiates emergency shard relocation, or sets indices to &lt;code&gt;read_only_allow_delete: true&lt;/code&gt;, abruptly rejecting all writes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;How do we systematically classify an Elasticsearch cluster incident into JVM heap exhaustion, shard allocation skew, thread pool starvation, storage watermark lockouts, or coordinating node bottlenecks?&lt;/p&gt;
&lt;h2 id=&quot;build-a-cluster-aware-elasticsearch-evidence-pack&quot;&gt;Build a Cluster-Aware Elasticsearch Evidence Pack&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[client p99 search latency HTTP 429 and indexing queue alerts] --&gt; E[time-bounded Elasticsearch incident evidence pack]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B[cluster health state and shard allocation explain] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[node stats JVM GC thread pools segments and circuit breakers] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[disk watermarks host I-O wait and hot threads samples] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[validate timestamps node roles uptime and counter deltas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[calculate thread rejection rates GC pause duration and heap ratios]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[LLM observations hypotheses contradictions and gaps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[DBA and SRE verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[remediation validation and rollback]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To diagnose Elasticsearch accurately, assemble a time-bounded evidence pack capturing baseline, onset, and degradation windows in synchronized UTC:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cluster Health and State (&lt;code&gt;_cluster/health&lt;/code&gt;, &lt;code&gt;_cluster/state&lt;/code&gt;):&lt;/strong&gt; Status (&lt;code&gt;green&lt;/code&gt;, &lt;code&gt;yellow&lt;/code&gt;, &lt;code&gt;red&lt;/code&gt;), unassigned shards, initializing shards, relocating shards, and cluster state version.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Node Resources and JVM (&lt;code&gt;_nodes/stats&lt;/code&gt;):&lt;/strong&gt; &lt;code&gt;jvm.mem.heap_used_percent&lt;/code&gt;, &lt;code&gt;jvm.gc.collectors.young.collection_time_in_millis&lt;/code&gt;, &lt;code&gt;jvm.gc.collectors.old.collection_time_in_millis&lt;/code&gt;, &lt;code&gt;breakers.parent.tripped&lt;/code&gt;, &lt;code&gt;breakers.fielddata.tripped&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Thread Pool Queue Dynamics (&lt;code&gt;_cat/thread_pool/write,search?v&amp;#x26;h=node_name,name,active,queue,rejected,completed&lt;/code&gt;):&lt;/strong&gt; Active thread counts, current queue depth, and interval deltas of rejected executions for &lt;code&gt;write&lt;/code&gt;, &lt;code&gt;search&lt;/code&gt;, and &lt;code&gt;get&lt;/code&gt; pools.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Indices, Shards, and Segments (&lt;code&gt;_cat/shards&lt;/code&gt;, &lt;code&gt;_cat/indices&lt;/code&gt;):&lt;/strong&gt; Shard count per node, average shard size, total Lucene segment count (&lt;code&gt;indices.segments.count&lt;/code&gt;), memory used by segments (&lt;code&gt;indices.segments.memory_in_bytes&lt;/code&gt;), and background merge activity (&lt;code&gt;indices.merges.current&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Disk Allocation and Storage (&lt;code&gt;_cat/allocation&lt;/code&gt;):&lt;/strong&gt; Disk used percentage, free disk space, and active index block settings (&lt;code&gt;read_only_allow_delete&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Execution Profiling (&lt;code&gt;_nodes/hot_threads&lt;/code&gt;):&lt;/strong&gt; CPU stack traces identifying hot execution loops across nodes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The LLM processes this sanitized bundle to generate four distinct, auditable artifacts:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Observations:&lt;/strong&gt; Quantitative facts tied to specific node names and metric deltas (e.g., “Node &lt;code&gt;data-hot-04&lt;/code&gt; rejected 14,200 search tasks over a 60-second window while G1GC old-generation pause time totaled 18.4 seconds”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hypotheses:&lt;/strong&gt; Ranked architectural explanations with supporting and contradicting metrics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Missing Evidence:&lt;/strong&gt; Unobserved metrics required to confirm the primary hypothesis (e.g., “Need &lt;code&gt;_nodes/hot_threads&lt;/code&gt; for &lt;code&gt;data-hot-04&lt;/code&gt; to verify if thread rejection is driven by Lucene segment sorting or regex query execution”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Actions:&lt;/strong&gt; Safe, read-only verification commands and reversible configuration adjustments.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Plausible failure classes&lt;/th&gt;&lt;th&gt;Evidence that separates them&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;HTTP 429 rejections on writes&lt;/td&gt;&lt;td&gt;Ingestion thread pool saturated, segment merge backpressure, disk flood stage&lt;/td&gt;&lt;td&gt;&lt;code&gt;thread_pool.write.rejected&lt;/code&gt; delta, &lt;code&gt;indices.merges.current&lt;/code&gt;, disk usage %&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Search p99 latency spikes&lt;/td&gt;&lt;td&gt;High shard fan-out, garbage collection pauses, large aggregation memory&lt;/td&gt;&lt;td&gt;&lt;code&gt;thread_pool.search.queue&lt;/code&gt;, &lt;code&gt;jvm.gc.collectors&lt;/code&gt;, shard count per query&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cluster status drops to Red&lt;/td&gt;&lt;td&gt;Primary shard unassigned, disk full, corrupted translog, hardware loss&lt;/td&gt;&lt;td&gt;&lt;code&gt;_cluster/health&lt;/code&gt;, &lt;code&gt;_cluster/allocation/explain&lt;/code&gt;, node departure logs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cluster status drops to Yellow&lt;/td&gt;&lt;td&gt;Replica shard unassigned due to disk watermark or allocation rules&lt;/td&gt;&lt;td&gt;&lt;code&gt;_cat/shards?h=index,shard,prirep,state,unassigned.reason&lt;/code&gt;, disk %&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Node disconnects from cluster&lt;/td&gt;&lt;td&gt;Long GC stop-the-world pause, network partition, CPU starvation&lt;/td&gt;&lt;td&gt;&lt;code&gt;jvm.gc.collectors.old&lt;/code&gt;, master coordination timeout logs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sudden write rejection across all nodes&lt;/td&gt;&lt;td&gt;Disk flood-stage watermark exceeded (95%), read-only index block&lt;/td&gt;&lt;td&gt;&lt;code&gt;index.blocks.read_only_allow_delete: true&lt;/code&gt;, &lt;code&gt;_cat/allocation&lt;/code&gt; disk %&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Elasticsearch latency or rejection alert] --&gt; B[1. Check cluster health unassigned shards and allocation explain]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[2. Inspect JVM heap usage GC pause durations and circuit breakers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[3. Audit thread pool queues and rejection deltas across nodes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[4. Analyze shard distribution count per node and segment merge rates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[5. Evaluate disk allocation watermarks and read-only index blocks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Synthesize observations hypotheses and remediation steps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-check-cluster-health-unassigned-shards-and-allocation-explain&quot;&gt;1. Check cluster health, unassigned shards, and allocation explain&lt;/h3&gt;
&lt;p&gt;Inspect cluster health immediately:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;GET&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; _cluster/health&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the status is &lt;code&gt;yellow&lt;/code&gt; or &lt;code&gt;red&lt;/code&gt;, retrieve the exact root cause for the unassigned shard using the Allocation Explain API:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;POST&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; _cluster/allocation/explain&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  &quot;include_disk_info&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  &quot;include_yes_decisions&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The response reveals whether shard allocation is blocked by disk watermarks (&lt;code&gt;disk_threshold&lt;/code&gt;), shard awareness attributes (&lt;code&gt;awareness&lt;/code&gt;), or in-flight snapshot restores.&lt;/p&gt;
&lt;h3 id=&quot;2-inspect-jvm-heap-usage-gc-pause-durations-and-circuit-breakers&quot;&gt;2. Inspect JVM heap usage, GC pause durations, and circuit breakers&lt;/h3&gt;
&lt;p&gt;Evaluate memory health across all data nodes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;GC Stop-the-World Pauses:&lt;/strong&gt; Check &lt;code&gt;_nodes/stats/jvm&lt;/code&gt;. If young- or old-generation GC collection time increments by more than 2,000 ms within a 1-minute window, the node is experiencing severe memory pressure that pauses the transport layer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Circuit Breakers:&lt;/strong&gt; Check &lt;code&gt;_nodes/stats/breaker&lt;/code&gt;. If &lt;code&gt;breakers.parent.tripped&lt;/code&gt; or &lt;code&gt;breakers.fielddata.tripped&lt;/code&gt; is incrementing, queries or aggregations are attempting to allocate memory beyond safety limits, triggering automatic query cancellation to prevent OutOfMemoryError crashes.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-audit-thread-pool-queues-and-rejection-deltas-across-nodes&quot;&gt;3. Audit thread pool queues and rejection deltas across nodes&lt;/h3&gt;
&lt;p&gt;Execute &lt;code&gt;_cat/thread_pool/write,search?v&amp;#x26;s=rejected:desc&lt;/code&gt; to identify overloaded nodes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Write Thread Pool (&lt;code&gt;write&lt;/code&gt;):&lt;/strong&gt; Rejections indicate that incoming bulk indexing requests exceed the capacity of Lucene indexing threads and hardware I/O queues.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Search Thread Pool (&lt;code&gt;search&lt;/code&gt;):&lt;/strong&gt; Rejections occur when concurrent search requests overwhelm coordinating and data node worker pools. If &lt;code&gt;queue&lt;/code&gt; is full (default 1,000) and &lt;code&gt;rejected&lt;/code&gt; increments, upstream search requests will fail with HTTP 429.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;4-analyze-shard-distribution-count-per-node-and-segment-merge-rates&quot;&gt;4. Analyze shard distribution, count per node, and segment merge rates&lt;/h3&gt;
&lt;p&gt;Inspect keyspace partitioning across data nodes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Shard Count per Node:&lt;/strong&gt; Check &lt;code&gt;_cat/allocation?v&lt;/code&gt;. A healthy production ratio is typically 20–30 shards per gigabyte of configured heap (e.g., max 600–900 shards on a 30 GB heap node). Higher ratios cause heap exhaustion due to Lucene metadata overhead.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Segment Count and Merges:&lt;/strong&gt; Check &lt;code&gt;_nodes/stats/indices/segments,merges&lt;/code&gt;. If &lt;code&gt;indices.merges.current&lt;/code&gt; is elevated and segment counts exceed 50 per shard, disk write bandwidth is throttling Lucene segment merging.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;5-evaluate-disk-allocation-watermarks-and-read-only-index-blocks&quot;&gt;5. Evaluate disk allocation watermarks and read-only index blocks&lt;/h3&gt;
&lt;p&gt;Check node storage headroom against cluster watermark thresholds:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Low Watermark (85%):&lt;/strong&gt; Elasticsearch stops allocating new shards to the node.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;High Watermark (90%):&lt;/strong&gt; Elasticsearch initiates background shard relocation away from the node.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Flood Stage Watermark (95%):&lt;/strong&gt; Elasticsearch enforces &lt;code&gt;index.blocks.read_only_allow_delete: true&lt;/code&gt; on all indices with shards on that node, blocking all write operations.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Elasticsearch latency rejection or health alert] --&gt; B{Cluster status is Red or Yellow}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes| C[Run allocation explain and check disk watermarks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No| D{Thread pool rejections detected on write or search}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Yes| E{Rejections on write thread pool}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|Yes| F[Check bulk request batch sizes and Lucene merge throttling]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|No| G[Inspect search queue depth and high-cardinality aggregations]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|No| H{JVM GC pauses &gt; 2000ms or circuit breakers tripped}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|Yes| I[Audit shard-to-heap ratio and fielddata memory usage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|No| J{Disk usage approaching 85-90% watermark}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|Yes| K[Apply Index Lifecycle Management ILM or add data nodes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|No| L[Sample hot threads to identify CPU-bound execution loops]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Elastic’s &lt;a href=&quot;https://www.elastic.co/docs/reference/elasticsearch/jvm-settings&quot;&gt;JVM settings reference&lt;/a&gt; frames heap sizing around preserving Java Compressed Object Pointers (Compressed OOPs) rather than around one fixed ceiling. Approximately 26 GB is safe on most systems; the threshold can reach roughly 30 GB depending on JVM version and host memory layout. Crossing it expands 32-bit object references to 64-bit, consuming significant extra heap while starving the operating system filesystem cache required for Lucene off-heap operations.&lt;/p&gt;
&lt;p&gt;Two practical consequences follow. First, treat the limit as version- and host-specific and confirm it empirically — check &lt;code&gt;using_compressed_ordinary_object_pointers&lt;/code&gt; in &lt;code&gt;GET _nodes/jvm&lt;/code&gt; after any heap change, rather than trusting a remembered number. Second, current Elasticsearch performs automatic heap sizing by default, so an explicit &lt;code&gt;Xms&lt;/code&gt;/&lt;code&gt;Xmx&lt;/code&gt; override is a deliberate decision that should be justified and recorded, not a default step in provisioning.&lt;/p&gt;
&lt;p&gt;According to &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/size-your-shards.html&quot;&gt;Elastic documentation on Shard Sizing and Capacity Planning&lt;/a&gt;, shard sizes should target 10 GB to 50 GB for log analytics and search workloads. Small shards (under 1 GB) create excessive overhead in Lucene segment memory, cluster state synchronization, and thread queue scheduling.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/circuit-breaker.html&quot;&gt;Elasticsearch Circuit Breakers Reference&lt;/a&gt; details how the parent circuit breaker (&lt;code&gt;indices.breaker.total.use_real_memory&lt;/code&gt;, default 95% of heap) dynamically tracks memory usage across all subsystems. When memory exceeds this limit, Elasticsearch refuses incoming requests rather than risking a JVM OOM crash.&lt;/p&gt;
&lt;p&gt;Documented &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-cluster.html#disk-based-shard-allocation&quot;&gt;Disk-Based Shard Allocation guidance&lt;/a&gt; confirms that exceeding the 95% flood-stage disk watermark automatically applies a read-only index block (&lt;code&gt;read_only_allow_delete: true&lt;/code&gt;). Resolving this state requires freeing disk space and explicitly removing the index block via the cluster settings API.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven root cause&lt;/th&gt;&lt;th&gt;Candidate remediation&lt;/th&gt;&lt;th&gt;Validation criteria&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Thread pool write rejections from small bulk requests&lt;/td&gt;&lt;td&gt;Increase bulk batch size (5–15 MB per bulk) and stagger client ingest workers&lt;/td&gt;&lt;td&gt;&lt;code&gt;thread_pool.write.rejected&lt;/code&gt; delta drops to 0; indexing throughput climbs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Over-sharding causing heap exhaustion&lt;/td&gt;&lt;td&gt;Implement Index Lifecycle Management (ILM) rollover; shrink old indices&lt;/td&gt;&lt;td&gt;Shard count per node drops below 20/GB heap; GC pause times drop&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Search thread starvation from unindexed fields&lt;/td&gt;&lt;td&gt;Add index mappings or keyword multi-fields; avoid wildcards on large text&lt;/td&gt;&lt;td&gt;&lt;code&gt;thread_pool.search.queue&lt;/code&gt; stabilizes; p99 latency drops below SLA&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Node locked in read-only due to 95% disk watermark&lt;/td&gt;&lt;td&gt;Delete expired indices, expand storage volume, and clear read-only block&lt;/td&gt;&lt;td&gt;&lt;code&gt;index.blocks.read_only_allow_delete&lt;/code&gt; removed; write operations resume&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lucene segment merge I/O throttling&lt;/td&gt;&lt;td&gt;Migrate data storage tier to higher IOPS SSD/gp3; adjust &lt;code&gt;max_thread_count&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;indices.merges.current&lt;/code&gt; decreases; indexing wait times normalize&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Circuit breaker tripped by memory-heavy aggregations&lt;/td&gt;&lt;td&gt;Rewrite query to use &lt;code&gt;composite&lt;/code&gt; aggregation or filter keyspace before aggregation&lt;/td&gt;&lt;td&gt;&lt;code&gt;breakers.parent.tripped&lt;/code&gt; stops incrementing; 0 CircuitBreakerExceptions&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;When executing cluster adjustments or index settings changes, maintain a clear, low-risk reversal path:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Cluster Settings Rollback:&lt;/strong&gt; Dynamic cluster settings modified via &lt;code&gt;PUT _cluster/settings&lt;/code&gt; (such as &lt;code&gt;cluster.routing.allocation.enable&lt;/code&gt; or watermark thresholds) can be restored immediately by applying &lt;code&gt;null&lt;/code&gt; to reset them to default values.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Index Setting Rollback:&lt;/strong&gt; If changing &lt;code&gt;index.refresh_interval&lt;/code&gt; or &lt;code&gt;index.translog.durability&lt;/code&gt; introduces unwanted indexing lag or memory pressure, revert to baseline settings via &lt;code&gt;PUT &amp;#x3C;index&gt;/_settings&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Read-Only Index Block Removal:&lt;/strong&gt; If clearing the flood-stage watermark requires lifting index blocks, execute:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;PUT */_settings&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;index.blocks.read_only_allow_delete&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;null&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ILM Policy Reversion:&lt;/strong&gt; If an updated Index Lifecycle Management policy causes unexpected index rollover or migration, reattach the prior ILM policy to the index template without interrupting live ingestion.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why naive reasoning fails&lt;/th&gt;&lt;th&gt;Better diagnostic boundary&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;”Heap at 80% means we need more RAM”&lt;/td&gt;&lt;td&gt;Java heaps peak near high watermarks between collections; a brief 80% excursion is expected, but sustained old-generation pressure at or above 85% is not&lt;/td&gt;&lt;td&gt;Measure GC pause duration, old-generation collection rate, and whether occupancy recovers to a stable floor after each collection&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”More shards increase parallel performance”&lt;/td&gt;&lt;td&gt;Excessive shards create thread contention and waste gigabytes of heap&lt;/td&gt;&lt;td&gt;Target 10–50 GB shard sizes and maintain &amp;#x3C;25 shards/GB heap&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Yellow status means data is lost”&lt;/td&gt;&lt;td&gt;Yellow status means all primary shards are active, but some replicas are unassigned&lt;/td&gt;&lt;td&gt;Run &lt;code&gt;_cluster/allocation/explain&lt;/code&gt; to identify unassigned replica cause&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Allocating 64GB heap doubles capacity”&lt;/td&gt;&lt;td&gt;Heaps &gt;31GB lose Compressed OOPs, degrading performance and starving OS cache&lt;/td&gt;&lt;td&gt;Keep heap &amp;#x3C;=31GB and reserve &gt;=50% host RAM for Lucene OS cache&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”CPU at 50% means no indexing bottleneck”&lt;/td&gt;&lt;td&gt;Disk I/O or segment merge throttling stalls indexing threads regardless of CPU&lt;/td&gt;&lt;td&gt;Check &lt;code&gt;indices.merges&lt;/code&gt; and disk queue depths&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Increasing thread pool queue fixes HTTP 429”&lt;/td&gt;&lt;td&gt;Expanding queues merely delays failures and consumes more heap&lt;/td&gt;&lt;td&gt;Optimize bulk sizes, tune merge threads, or scale data nodes&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;To protect production clusters and data availability, explicit operational guardrails govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Direct API Execution:&lt;/strong&gt; The LLM cannot execute &lt;code&gt;PUT _cluster/settings&lt;/code&gt;, issue index deletions (&lt;code&gt;DELETE *&lt;/code&gt;), force-merge indices, or allocate shards autonomously.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Unsanitized Log Ingestion:&lt;/strong&gt; The LLM must not ingest raw document content, user queries containing PII, or unredacted security credentials.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Speculative Shard Reallocation:&lt;/strong&gt; If telemetry lacks output from &lt;code&gt;_cluster/allocation/explain&lt;/code&gt;, the LLM must explicitly list missing allocation evidence rather than guessing why a shard is unassigned.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Cluster State Mutation:&lt;/strong&gt; All operational commands proposed by the LLM require verification and execution by human DBAs and SREs.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Elasticsearch performance incidents present intertwined symptoms across JVM heap, Lucene segment merging, shard distribution, and thread pool queues that mislead superficial triage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Construct a synchronized cluster evidence pack correlating &lt;code&gt;_cluster/health&lt;/code&gt;, &lt;code&gt;_nodes/stats&lt;/code&gt;, thread pool rejection deltas, and allocation explain responses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Formulate hypotheses that explicitly reconcile GC pause times against thread pool rejections, shard counts against heap occupancy, and disk watermarks against write blocks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Deploy automated telemetry collectors for your Elasticsearch cluster. Then proceed to Part 2 to diagnose slow queries using the Search Profile API, query versus fetch phases, and aggregation execution paths.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html&quot;&gt;Elasticsearch Guide (Version 8.x) — Cluster and Node Diagnostics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/size-your-shards.html&quot;&gt;Elasticsearch Documentation — Size Your Shards and Capacity Planning&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/circuit-breaker.html&quot;&gt;Elasticsearch Circuit Breakers Reference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-cluster.html#disk-based-shard-allocation&quot;&gt;Elasticsearch Documentation — Disk-Based Shard Allocation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-allocation-explain.html&quot;&gt;Elasticsearch Documentation — Allocation Explain API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-threadpool.html&quot;&gt;Elasticsearch Documentation — Thread Pools and Queue Management&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>ai-engineering</category><category>failures</category></item><item><title>Azure Hybrid Benefit for SQL Server: The Exact Math</title><link>https://rajivonai.com/blog/2026-02-25-azure-hybrid-benefit-database-guide/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-02-25-azure-hybrid-benefit-database-guide/</guid><description>A deep dive into the cost savings and mechanics of applying Azure Hybrid Benefit to SQL Server deployments.</description><pubDate>Wed, 25 Feb 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Defaulting to License-Included pricing on Azure means you might be paying twice for SQL Server licenses you already own.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Companies migrating from on-premises datacenters to Azure often carry large Enterprise Agreements with active Software Assurance (SA) for SQL Server.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cloud migration teams frequently provision Azure SQL Database or Managed Instances using the default “License-Included” tier. This ignores existing on-premises licenses, resulting in massive and unnecessary OPEX. How do you accurately model the break-even math for Azure Hybrid Benefit (AHB)?&lt;/p&gt;
&lt;h2 id=&quot;the-mechanics-of-ahb&quot;&gt;The Mechanics of AHB&lt;/h2&gt;
&lt;p&gt;Azure Hybrid Benefit allows you to use your existing SQL Server licenses with active SA to pay a reduced “base rate” (compute-only) for SQL Server on Azure VMs, Azure SQL Database, and Azure SQL Managed Instance.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for AHB adoption involves auditing your SA inventory, converting older DTU-based databases to the vCore model (which supports AHB), and applying the licenses. One Enterprise Edition core license typically covers four General Purpose vCores or one Business Critical vCore.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;New SA Purchase&lt;/td&gt;&lt;td&gt;Buying new SA solely to use AHB requires factoring the upfront cost against the annualized savings. Break-even is usually 7-10 months.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DTU Model&lt;/td&gt;&lt;td&gt;Legacy DTU-based Azure SQL databases do not support AHB. You must migrate to the vCore model first.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Paying retail license rates on Azure despite owning SQL Server SA.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Convert to vCore models and apply Azure Hybrid Benefit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: AHB can meaningfully reduce SQL Server costs; Microsoft cites up to roughly 55% for qualifying configurations, but realized savings vary — model your own EA and workload rather than assuming a fixed percentage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Try our &lt;a href=&quot;https://rajivonai.com/tools/sql-server-license-calculator/&quot;&gt;SQL Server Cloud Licensing Calculator&lt;/a&gt; to compare your License-Included costs against AHB modeled costs. Request a Cloud Database Cost Review if you need help navigating your EA.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category></item><item><title>Citus vs Aurora Limitless vs Aurora DSQL vs Sharded MySQL: How Do You Scale Writes?</title><link>https://rajivonai.com/blog/2026-02-24-citus-vs-aurora-limitless-vs-dsql-vs-sharded-mysql/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-02-24-citus-vs-aurora-limitless-vs-dsql-vs-sharded-mysql/</guid><description>A workload-first decision guide for choosing between managed distributed PostgreSQL, Citus, distributed SQL, application sharding, or keeping one writer.</description><pubDate>Tue, 24 Feb 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The wrong write-scaling decision starts with a product name. The right decision starts by locating the business fact that must have one owner, the transactions that must remain local to it, and the team that will operate that ownership during failure and recovery.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;RetailCo has reached the end of a familiar growth path. Its relational writer has been vertically scaled. Expensive SQL and unnecessary indexes have been removed. Connection management and batch jobs have been repaired. Read replicas serve read-only traffic. The remaining peak pressure comes from committed order, inventory, payment, and shipment mutations.&lt;/p&gt;
&lt;p&gt;RetailCo is a reference architecture, not a claimed customer deployment. The 40-TB transaction history, four brands, tenant model, workload shape, and recommendations below are illustrative until replaced by production traces and lab evidence. This article contains no measured throughput, latency, availability, recovery, migration, or cost result.&lt;/p&gt;
&lt;p&gt;The platform now has five plausible directions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;keep one conventional Aurora writer and buy more headroom;&lt;/li&gt;
&lt;li&gt;move the PostgreSQL workload to Aurora PostgreSQL Limitless Database;&lt;/li&gt;
&lt;li&gt;operate Citus across PostgreSQL workers;&lt;/li&gt;
&lt;li&gt;redesign a bounded workload for Aurora DSQL;&lt;/li&gt;
&lt;li&gt;preserve conventional MySQL semantics inside application-owned shards.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;All five can be valid. They do not solve the same problem or assign responsibility to the same control plane.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Pressure[writer pressure is sustained] --&gt; Question{what must scale independently}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Question --&gt; Capacity[one database needs more headroom]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Question --&gt; Tenants[different tenants need different write owners]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Question --&gt; Domains[payments inventory and orders need separate owners]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Question --&gt; Regions[two Regions need concurrent writes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Capacity --&gt; Standard[standard Aurora — one writer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Tenants --&gt; Distributed[distributed relational design]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Domains --&gt; Decompose[domain decomposition]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Regions --&gt; DSQL[Aurora DSQL evaluation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Distributed --&gt; Limitless[Aurora Limitless]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Distributed --&gt; Citus[Citus]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Distributed --&gt; MySQL[sharded MySQL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first article in this series established the governing distinction: &lt;a href=&quot;https://rajivonai.com/blog/2024-01-23-why-reads-scale-more-easily-than-writes/&quot;&gt;replication improves availability and read capacity, while sustained horizontal write scale normally requires distributed ownership&lt;/a&gt;. The rest of the series examined different ways to create and operate that ownership.&lt;/p&gt;
&lt;p&gt;This capstone is not a leaderboard. It is a rejection framework. A database should leave the shortlist as soon as its transaction, compatibility, failure, or migration model conflicts with the workload.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A normal feature matrix makes every distributed product look similar:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Product&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;SQL&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;ACID&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;High availability&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Horizontal scale&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Aurora Limitless&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Citus&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aurora DSQL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sharded MySQL&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes within a shard&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes within a shard&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Every cell is technically defensible and architecturally incomplete.&lt;/p&gt;
&lt;p&gt;“SQL” does not say which PostgreSQL or MySQL features, extensions, catalogs, isolation levels, constraints, functions, and operational tools behave the same. “ACID” does not identify whether the critical transaction stays on one shard, coordinates across shards, uses snapshot isolation, or becomes a workflow. “High availability” does not say which tenants fail together. “Horizontal scale” does not explain whether the database, an operator, or the application owns placement and movement.&lt;/p&gt;
&lt;p&gt;Five hidden decisions matter more than the headline capabilities.&lt;/p&gt;
&lt;h3 id=&quot;1-what-is-the-unit-of-write-ownership&quot;&gt;1. What is the unit of write ownership?&lt;/h3&gt;
&lt;p&gt;For RetailCo it might be &lt;code&gt;tenant_id&lt;/code&gt;, &lt;code&gt;store_id&lt;/code&gt;, an order aggregate, a payment account, or a business domain. A useful key must have enough cardinality to spread work and must appear in the transactions that need locality.&lt;/p&gt;
&lt;p&gt;A four-value &lt;code&gt;brand_id&lt;/code&gt; is unlikely to be a durable distribution boundary. One brand can dominate traffic, and four values cap useful placement choices. &lt;code&gt;tenant_id&lt;/code&gt; is stronger when checkout, order items, inventory reservations, and payment state can all be scoped to one tenant. It is still insufficient if one tenant alone exceeds one shard or if routine transactions span many tenants.&lt;/p&gt;
&lt;h3 id=&quot;2-who-owns-the-routing-truth&quot;&gt;2. Who owns the routing truth?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Limitless hides routers and shards inside a managed DB shard group.&lt;/li&gt;
&lt;li&gt;Citus exposes coordinator, worker, shard, and placement operations to the database team.&lt;/li&gt;
&lt;li&gt;Aurora DSQL uses its primary-key structure to underpin automatic partitioning and does not ask the application to select an explicit shard.&lt;/li&gt;
&lt;li&gt;Custom MySQL sharding requires an application placement catalog and route fencing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These are not implementation details. The routing owner also owns stale routes, hot placement, topology change, observability, and recovery.&lt;/p&gt;
&lt;h3 id=&quot;3-what-happens-when-a-transaction-is-not-local&quot;&gt;3. What happens when a transaction is not local?&lt;/h3&gt;
&lt;p&gt;Distributed databases can coordinate distributed transactions. That does not make coordination free. More participants add network work, larger failure surfaces, and harder tail-latency behavior. A design whose main checkout path repeatedly crosses ownership boundaries has probably chosen the wrong key or the wrong boundary.&lt;/p&gt;
&lt;h3 id=&quot;4-which-compatibility-is-non-negotiable&quot;&gt;4. Which compatibility is non-negotiable?&lt;/h3&gt;
&lt;p&gt;A workload that depends on PostgreSQL extensions, &lt;code&gt;SERIALIZABLE&lt;/code&gt;, triggers, procedural functions, temporary tables, foreign-key enforcement, or server-level tooling should not treat “PostgreSQL compatible” as a migration conclusion. A MySQL application with stored programs, optimizer-sensitive SQL, or operational dependencies should not assume a MySQL protocol makes TiDB or Vitess invisible.&lt;/p&gt;
&lt;h3 id=&quot;5-which-team-will-recover-the-system&quot;&gt;5. Which team will recover the system?&lt;/h3&gt;
&lt;p&gt;Managed infrastructure reduces server work. It does not eliminate application retries, hot-key diagnosis, schema migration, backup validation, or business reconciliation. Self-managed distribution increases control and also makes coordinator recovery, worker recovery, rebalancing, version compatibility, TLS, storage, and operating-system failure part of the database service.&lt;/p&gt;
&lt;p&gt;The core question is therefore: &lt;strong&gt;which architecture keeps the dominant transaction local, preserves the compatibility the application actually uses, and assigns the remaining distributed-system work to a team that can prove recovery?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;choose-the-ownership-model-before-the-product&quot;&gt;Choose the Ownership Model Before the Product&lt;/h2&gt;
&lt;p&gt;Start with a workload dossier, not a request for proposals. The dossier should contain:&lt;/p&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Evidence&lt;/th&gt;&lt;th&gt;Required answer&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Write-path trace&lt;/td&gt;&lt;td&gt;Which statements commit together, and which identifier appears in each?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Locality report&lt;/td&gt;&lt;td&gt;What portion of critical transactions is single-tenant or single-domain?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hot-key report&lt;/td&gt;&lt;td&gt;Can one tenant, account, product, or counter dominate a partition?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Constraint inventory&lt;/td&gt;&lt;td&gt;Which unique, foreign-key, check, trigger, and isolation guarantees matter?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;SQL and tool inventory&lt;/td&gt;&lt;td&gt;Which extensions, functions, catalogs, drivers, pools, migrations, and backup tools are required?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failure budget&lt;/td&gt;&lt;td&gt;What can fail together, and what RPO and RTO must be demonstrated?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Migration map&lt;/td&gt;&lt;td&gt;Can ownership move by tenant or domain while the source remains live?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Operating model&lt;/td&gt;&lt;td&gt;Who owns routing, resharding, schema rollout, backup, restore, and incidents?&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;If these answers are unavailable, the architecture is not ready for product selection.&lt;/p&gt;
&lt;h3 id=&quot;keep-standard-aurora-in-the-comparison&quot;&gt;Keep standard Aurora in the comparison&lt;/h3&gt;
&lt;p&gt;Standard Aurora is the control case. An Aurora cluster has one primary writer and can have up to 15 read-only Aurora Replicas. The replicas scale reads and can be promoted during failover (&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Replication.html&quot;&gt;Aurora replication&lt;/a&gt;). Local write forwarding lets supported applications submit writes through a reader, but the writer still commits those writes (&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-postgresql-write-forwarding.html&quot;&gt;Aurora PostgreSQL write forwarding&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;That is not a criticism. One writer is the simplest strong-consistency boundary in this comparison. It is the correct choice when optimization and vertical capacity provide forecast headroom, the workload lacks a stable ownership key, or the organization is not prepared to operate distributed correctness.&lt;/p&gt;
&lt;p&gt;Choose standard Aurora when the business problem is temporary capacity pressure, not a demonstrated ownership ceiling. Do not buy distribution merely to avoid finishing query, schema, connection, and history-retention work.&lt;/p&gt;
&lt;h3 id=&quot;aurora-postgresql-limitless--managed-explicit-locality&quot;&gt;Aurora PostgreSQL Limitless — managed explicit locality&lt;/h3&gt;
&lt;p&gt;Limitless replaces the conventional writer-and-reader shape with a DB shard group containing routers and shards. Routers accept client SQL, locate data, coordinate distributed work, and aggregate results. Shards store subsets of sharded tables. Clients use one cluster endpoint and do not connect directly to individual shards (&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless-architecture.html&quot;&gt;Limitless architecture&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;It supports four important table roles:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Sharded tables&lt;/strong&gt; use a hash-based shard key and distribute rows across shards.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Collocated tables&lt;/strong&gt; share a shard key so equal key values are stored together.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reference tables&lt;/strong&gt; are copied to shards for local access.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standard tables&lt;/strong&gt; stay together on one system-selected shard and therefore do not gain distributed write ownership.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For RetailCo, &lt;code&gt;orders&lt;/code&gt;, &lt;code&gt;order_items&lt;/code&gt;, and tenant-owned payment state might be sharded and collocated by &lt;code&gt;tenant_id&lt;/code&gt;. Small stable lookup data might be reference data. A large standard table would recreate a single-shard ceiling.&lt;/p&gt;
&lt;p&gt;The managed control plane is the main advantage. AWS owns the hidden router and shard infrastructure and exposes capacity and topology operations through the service. The data model still owns locality. Current documentation says primary and unique keys on sharded tables must include the shard key, shard keys cannot be updated, shards cannot be merged, and individual routers and shards cannot be deleted. Current Limitless requirements also exclude &lt;code&gt;SERIALIZABLE&lt;/code&gt; and several standard Aurora integrations (&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless-reqs-limits.html&quot;&gt;requirements and considerations&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Limitless is therefore the strongest first managed proof of concept when:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;PostgreSQL is required;&lt;/li&gt;
&lt;li&gt;a stable high-cardinality key exists;&lt;/li&gt;
&lt;li&gt;critical writes are predominantly local to that key;&lt;/li&gt;
&lt;li&gt;the schema can include the key in distributed constraints;&lt;/li&gt;
&lt;li&gt;the required extensions, isolation, connection, backup, and Aurora integrations fit the documented surface.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It is not the default answer for a mutable ownership key, one indivisible hot tenant, routine global transactions, or a 40-TB in-place conversion. The &lt;a href=&quot;https://rajivonai.com/blog/2025-01-21-scaling-writes-amazon-aurora-what-works/&quot;&gt;Aurora article&lt;/a&gt; explains why that migration needs staged copy, change capture, validation, canaries, and cutover rather than a table-setting change.&lt;/p&gt;
&lt;h3 id=&quot;citus--explicit-postgresql-sharding-with-operator-control&quot;&gt;Citus — explicit PostgreSQL sharding with operator control&lt;/h3&gt;
&lt;p&gt;Citus is a PostgreSQL extension that distributes tables into shards across PostgreSQL workers. Applications normally connect through a coordinator, which uses metadata to route local work or parallelize distributed work. The database architect selects a distribution column and explicitly classifies distributed, reference, and local tables (&lt;a href=&quot;https://docs.citusdata.com/en/stable/get_started/concepts.html&quot;&gt;Citus concepts&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Citus and Limitless share a central architectural truth: colocating related rows by a stable key makes the common transaction cheaper and easier to reason about. They assign operations differently.&lt;/p&gt;
&lt;p&gt;With Citus on EC2, RetailCo owns:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;PostgreSQL and Citus version compatibility;&lt;/li&gt;
&lt;li&gt;coordinator and worker high availability;&lt;/li&gt;
&lt;li&gt;stable addressing and routing;&lt;/li&gt;
&lt;li&gt;storage, networking, operating systems, and TLS;&lt;/li&gt;
&lt;li&gt;backup consistency and distributed restore;&lt;/li&gt;
&lt;li&gt;shard count, worker addition, rebalancing, and tenant isolation;&lt;/li&gt;
&lt;li&gt;monitoring and capacity for every coordinator and worker;&lt;/li&gt;
&lt;li&gt;upgrade and failure automation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In return, RetailCo gets direct PostgreSQL and extension control, visible worker topology, explicit shard operations, and deployment portability beyond one managed distributed service. Citus 13 documentation requires distributed unique constraints to include the distribution column and documents foreign-key rules for colocated distributed tables (&lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/reference_ddl.html&quot;&gt;distributed DDL&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Citus is the strongest first self-managed proof of concept when:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;PostgreSQL behavior and extension freedom matter;&lt;/li&gt;
&lt;li&gt;the workload has a durable locality key;&lt;/li&gt;
&lt;li&gt;the organization already operates PostgreSQL, Linux, storage, networking, backup, and failover as one service;&lt;/li&gt;
&lt;li&gt;direct control over placement, rebalancing, and topology is worth the on-call cost.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Reject it when the database team cannot own the full distributed lifecycle. The &lt;a href=&quot;https://rajivonai.com/blog/2024-05-21-scaling-postgresql-writes-citus-ec2-retailco/&quot;&gt;Citus architecture&lt;/a&gt;, &lt;a href=&quot;https://rajivonai.com/blog/2024-07-16-citus-shard-rebalancing-production/&quot;&gt;rebalancing&lt;/a&gt;, and &lt;a href=&quot;https://rajivonai.com/blog/2024-09-17-backing-up-recovering-citus-postgresql/&quot;&gt;recovery&lt;/a&gt; articles make that operational contract explicit.&lt;/p&gt;
&lt;h3 id=&quot;aurora-dsql--managed-distributed-sql-with-a-different-contract&quot;&gt;Aurora DSQL — managed distributed SQL with a different contract&lt;/h3&gt;
&lt;p&gt;Aurora DSQL is not sharded PostgreSQL hidden behind a router. It is a serverless distributed relational database that uses PostgreSQL core components and the PostgreSQL wire protocol while implementing optimistic concurrency control and distributed schema management. AWS currently describes it as based on PostgreSQL 16 (&lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with.html&quot;&gt;Aurora DSQL and PostgreSQL&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;In a supported multi-Region design, two Regional endpoints can concurrently read and write one strongly consistent logical database, with a third Region acting as a witness (&lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/multi-region-aws-cli.html&quot;&gt;multi-Region endpoints&lt;/a&gt;). That is a materially different goal from scaling tenant writes inside one Region.&lt;/p&gt;
&lt;p&gt;The application contract is also different:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;conflicts are evaluated at commit through optimistic concurrency control;&lt;/li&gt;
&lt;li&gt;data and schema conflicts return SQLSTATE &lt;code&gt;40001&lt;/code&gt;, so complete transactions need bounded idempotent retries;&lt;/li&gt;
&lt;li&gt;hot keys increase conflict pressure;&lt;/li&gt;
&lt;li&gt;the fixed isolation semantics are equivalent to PostgreSQL Repeatable Read, not &lt;code&gt;SERIALIZABLE&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;current quotas include five-minute transactions and 3,000 mutated rows per transaction block;&lt;/li&gt;
&lt;li&gt;foreign keys, triggers, procedural logic, temporary tables, catalogs, DDL, connection lifetime, and other PostgreSQL behaviors require explicit compatibility review.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;AWS documents that the primary key contributes to the cluster-wide key used for partitioning and recommends randomly distributed keys for high-write tables (&lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with-primary-keys.html&quot;&gt;DSQL primary keys&lt;/a&gt;). Its migration guide fixes isolation at Repeatable Read and describes application alternatives for referential integrity, triggers, temporary data, and procedural logic (&lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with-postgresql-compatibility-migration-guide.html&quot;&gt;PostgreSQL migration guide&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;The current limits are documented and can change, so revalidate them before publication or migration (&lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/CHAP_quotas.html&quot;&gt;DSQL quotas&lt;/a&gt;). The deeper point is stable: DSQL asks the application to own retry safety and more integrity behavior in exchange for a highly managed distributed write and multi-Region model.&lt;/p&gt;
&lt;p&gt;DSQL is the strongest pilot candidate when:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the workload is new or cleanly bounded;&lt;/li&gt;
&lt;li&gt;transactions are small and keys are well distributed;&lt;/li&gt;
&lt;li&gt;retry and idempotency behavior is already part of the application design;&lt;/li&gt;
&lt;li&gt;application-owned referential integrity is acceptable;&lt;/li&gt;
&lt;li&gt;two supported Regions need concurrent strongly consistent writes;&lt;/li&gt;
&lt;li&gt;the PostgreSQL compatibility inventory fits.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It is the weakest candidate here for a lift-and-shift PostgreSQL monolith with deep database programmability or large atomic batches. The &lt;a href=&quot;https://rajivonai.com/blog/2025-06-17-aurora-dsql-postgresql-compatible-not-postgresql/&quot;&gt;DSQL reality check&lt;/a&gt; provides the full compatibility and failure lab.&lt;/p&gt;
&lt;h3 id=&quot;sharded-mysql--conventional-semantics-inside-application-owned-domains&quot;&gt;Sharded MySQL — conventional semantics inside application-owned domains&lt;/h3&gt;
&lt;p&gt;Custom MySQL sharding preserves a familiar MySQL database inside each shard and scales writes by giving different tenants to different clusters. Each shard can be a three-member InnoDB Cluster in default single-primary mode. Group Replication supplies shard-local replication and failover; MySQL Router discovers the current shard primary (&lt;a href=&quot;https://dev.mysql.com/doc/mysql-shell/8.4/en/mysql-innodb-cluster.html&quot;&gt;MySQL InnoDB Cluster&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Neither InnoDB Cluster nor InnoDB ClusterSet is a tenant sharding control plane. ClusterSet links one primary cluster to read-only replica clusters through asynchronous replication; it is a disaster-tolerance topology for one write domain, not multiple independent tenant writers (&lt;a href=&quot;https://dev.mysql.com/doc/mysql-shell/8.4/en/innodb-clusterset.html&quot;&gt;InnoDB ClusterSet&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;RetailCo must build or adopt the missing layer:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a durable tenant-to-shard placement catalog;&lt;/li&gt;
&lt;li&gt;routing caches and behavior when the catalog is unavailable;&lt;/li&gt;
&lt;li&gt;a placement epoch or equivalent fence that rejects stale writers;&lt;/li&gt;
&lt;li&gt;tenant copy, CDC catch-up, validation, write fence, and cutover;&lt;/li&gt;
&lt;li&gt;fleet-wide schema rollout with expand-contract compatibility;&lt;/li&gt;
&lt;li&gt;backup and restore that reconstructs tenant ownership at a point in time;&lt;/li&gt;
&lt;li&gt;automation for hot tenants, capacity, patching, failure, and shard retirement.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is the strongest candidate when the estate is already MySQL, transactions are tenant-local, conventional MySQL inside each shard is valuable, and the platform team is prepared to own placement and fleet correctness. It is a poor fit when no stable tenant key exists, cross-shard ACID is common, or one tenant exceeds one cluster without another internal boundary.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://rajivonai.com/blog/2025-03-18-sharded-innodb-clusters-mysql-write-scaling/&quot;&gt;sharded MySQL article&lt;/a&gt; describes the routing, movement, schema, backup, and restore control plane in detail.&lt;/p&gt;
&lt;h3 id=&quot;the-ownership-spectrum&quot;&gt;The ownership spectrum&lt;/h3&gt;
&lt;p&gt;The products become easier to compare when placed by who owns distribution:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart LR&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Aurora[standard Aurora — one write owner] --&gt; Limitless[Limitless — AWS manages explicit shards]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Limitless --&gt; DSQL[DSQL — AWS manages distributed keyspace]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  DSQL --&gt; Citus[Citus — database team manages shards]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Citus --&gt; MySQL[sharded MySQL — application platform manages placement]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is not a quality ranking. Moving right generally increases explicit control and the amount of routing and topology work the organization owns. DSQL sits separately in semantic terms: it hides partition placement more completely than Limitless, but moves more concurrency and integrity responsibility into application design.&lt;/p&gt;
&lt;h3 id=&quot;decision-matrix&quot;&gt;Decision matrix&lt;/h3&gt;





























































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Decision dimension&lt;/th&gt;&lt;th&gt;Standard Aurora&lt;/th&gt;&lt;th&gt;Aurora Limitless&lt;/th&gt;&lt;th&gt;Citus on EC2&lt;/th&gt;&lt;th&gt;Aurora DSQL&lt;/th&gt;&lt;th&gt;Sharded MySQL&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Write ownership&lt;/td&gt;&lt;td&gt;One writer&lt;/td&gt;&lt;td&gt;Hash-sharded tables across managed shards&lt;/td&gt;&lt;td&gt;Distributed tables across explicit workers&lt;/td&gt;&lt;td&gt;Service-managed distributed keyspace&lt;/td&gt;&lt;td&gt;Tenant catalog assigns each tenant to one cluster&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Routing owner&lt;/td&gt;&lt;td&gt;Aurora endpoint&lt;/td&gt;&lt;td&gt;Managed routers&lt;/td&gt;&lt;td&gt;Citus coordinator and metadata&lt;/td&gt;&lt;td&gt;Managed service endpoint&lt;/td&gt;&lt;td&gt;Application shard router and catalog&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Best locality&lt;/td&gt;&lt;td&gt;One database transaction&lt;/td&gt;&lt;td&gt;Stable shard key and collocated PostgreSQL tables&lt;/td&gt;&lt;td&gt;Stable distribution column and colocated tables&lt;/td&gt;&lt;td&gt;Small transactions on well-distributed keys&lt;/td&gt;&lt;td&gt;Tenant-local transactions within one cluster&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;PostgreSQL or MySQL compatibility&lt;/td&gt;&lt;td&gt;Highest conventional Aurora compatibility&lt;/td&gt;&lt;td&gt;PostgreSQL subset plus Limitless constraints&lt;/td&gt;&lt;td&gt;PostgreSQL plus Citus constraints and extension compatibility&lt;/td&gt;&lt;td&gt;Focused PostgreSQL-compatible surface and different concurrency&lt;/td&gt;&lt;td&gt;Conventional MySQL per shard plus cross-shard application rules&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cross-boundary work&lt;/td&gt;&lt;td&gt;Local because there is one writer&lt;/td&gt;&lt;td&gt;Router-coordinated distributed work&lt;/td&gt;&lt;td&gt;Coordinator-coordinated multi-shard work&lt;/td&gt;&lt;td&gt;Distributed transaction with OCC conflict behavior&lt;/td&gt;&lt;td&gt;Application workflow, XA, or explicit rejection&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hot ownership key&lt;/td&gt;&lt;td&gt;One writer absorbs it&lt;/td&gt;&lt;td&gt;One key remains mapped to one shard&lt;/td&gt;&lt;td&gt;One tenant normally remains on one worker placement&lt;/td&gt;&lt;td&gt;Hot key raises conflict pressure&lt;/td&gt;&lt;td&gt;One tenant remains bounded by one cluster&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Scale operation&lt;/td&gt;&lt;td&gt;Increase writer capacity&lt;/td&gt;&lt;td&gt;Add routers or split shards under documented constraints&lt;/td&gt;&lt;td&gt;Add workers and rebalance shards&lt;/td&gt;&lt;td&gt;Service-managed elasticity&lt;/td&gt;&lt;td&gt;Add clusters and move tenant cohorts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Infrastructure owner&lt;/td&gt;&lt;td&gt;AWS&lt;/td&gt;&lt;td&gt;AWS&lt;/td&gt;&lt;td&gt;RetailCo&lt;/td&gt;&lt;td&gt;AWS&lt;/td&gt;&lt;td&gt;RetailCo or cloud provider per shard&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Correctness burden&lt;/td&gt;&lt;td&gt;Mostly local database constraints&lt;/td&gt;&lt;td&gt;Schema locality plus distributed compatibility&lt;/td&gt;&lt;td&gt;Locality plus distributed PostgreSQL operations&lt;/td&gt;&lt;td&gt;Retry, integrity, compatibility, and workflow design&lt;/td&gt;&lt;td&gt;Placement, fencing, cross-shard workflow, and fleet consistency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Migration difficulty for a large monolith&lt;/td&gt;&lt;td&gt;Lowest&lt;/td&gt;&lt;td&gt;High&lt;/td&gt;&lt;td&gt;Very high&lt;/td&gt;&lt;td&gt;High unless workload is new and bounded&lt;/td&gt;&lt;td&gt;High and cohort based&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;No matrix can select the database. It can expose which proof is still missing.&lt;/p&gt;
&lt;h3 id=&quot;fast-rejection-tests&quot;&gt;Fast rejection tests&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Start[writer ceiling verified] --&gt; Key{stable locality key exists}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Key --&gt;|no| Boundary{clear domain boundary exists}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Boundary --&gt;|yes| Decompose[decompose write ownership first]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Boundary --&gt;|no| Stay[stay conventional and redesign workload]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Key --&gt;|yes| Engine{engine constraint}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Engine --&gt;|PostgreSQL managed| Compat{Limitless compatibility fits}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Compat --&gt;|yes| Limitless[proof of concept — Limitless]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Compat --&gt;|no| Control{team can own distributed PostgreSQL}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Control --&gt;|yes| Citus[proof of concept — Citus]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Control --&gt;|no| ShardAurora[application-sharded managed clusters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Engine --&gt;|MySQL required| MySQL[proof of concept — sharded MySQL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Engine --&gt;|greenfield distributed| Retry{bounded transactions and retry safety}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Retry --&gt;|yes| DSQL[proof of concept — DSQL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Retry --&gt;|no| Redesign[redesign transaction boundary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The decision should stop at &lt;strong&gt;proof of concept&lt;/strong&gt;, not jump to production approval. Each branch still needs workload replay, failure injection, recovery, and migration evidence.&lt;/p&gt;
&lt;h3 id=&quot;scenario-recommendations&quot;&gt;Scenario recommendations&lt;/h3&gt;
&lt;h4 id=&quot;scenario-a--postgresql-managed-operations-tenant-local-checkout&quot;&gt;Scenario A — PostgreSQL, managed operations, tenant-local checkout&lt;/h4&gt;
&lt;p&gt;Choose Aurora PostgreSQL Limitless as the first proof of concept. Keep application-sharded standard Aurora clusters as the compatibility fallback. Reject both if &lt;code&gt;tenant_id&lt;/code&gt; is absent from critical constraints or cross-tenant transactions dominate.&lt;/p&gt;
&lt;h4 id=&quot;scenario-b--postgresql-extensions-and-direct-topology-control&quot;&gt;Scenario B — PostgreSQL extensions and direct topology control&lt;/h4&gt;
&lt;p&gt;Choose Citus when the workload is tenant-local and the database organization can operate coordinator and worker HA, rebalancing, distributed backup, restore, and upgrades. If that operating model does not exist, control is not an advantage; it is an unstaffed failure surface.&lt;/p&gt;
&lt;h4 id=&quot;scenario-c--new-distributed-oltp-service-with-two-writable-regions&quot;&gt;Scenario C — New distributed OLTP service with two writable Regions&lt;/h4&gt;
&lt;p&gt;Pilot Aurora DSQL when transactions are bounded, keys are well distributed, retries are idempotent, and the supported PostgreSQL surface fits. Do not use the pilot result to approve an unrelated monolith migration.&lt;/p&gt;
&lt;h4 id=&quot;scenario-d--existing-mysql-estate-with-a-stable-tenant-boundary&quot;&gt;Scenario D — Existing MySQL estate with a stable tenant boundary&lt;/h4&gt;
&lt;p&gt;Choose sharded InnoDB Clusters when preserving conventional MySQL inside each shard matters and the platform can own tenant placement and movement. Prove the ownership epoch during normal routing, failover, tenant move, backup, and restore.&lt;/p&gt;
&lt;h4 id=&quot;scenario-e--no-stable-tenant-key-but-clear-business-domains&quot;&gt;Scenario E — No stable tenant key, but clear business domains&lt;/h4&gt;
&lt;p&gt;Do not force tenant sharding. &lt;a href=&quot;https://rajivonai.com/blog/2025-12-16-decompose-database-write-path-before-microservices/&quot;&gt;Decompose the database write path&lt;/a&gt; so payment, inventory, order history, or another domain becomes an independent write owner. A modular monolith with selected domain databases may solve the ceiling without a microservice rewrite.&lt;/p&gt;
&lt;h4 id=&quot;scenario-f--one-writer-still-meets-the-forecast&quot;&gt;Scenario F — One writer still meets the forecast&lt;/h4&gt;
&lt;p&gt;Stay on standard Aurora or another conventional relational database. Preserve simplicity, keep optimizing, and define the metric that will reopen the distributed-database decision.&lt;/p&gt;
&lt;h3 id=&quot;adjacent-mysql-options--vitess-and-tidb&quot;&gt;Adjacent MySQL options — Vitess and TiDB&lt;/h3&gt;
&lt;p&gt;Vitess and TiDB deserve separate labs; neither is a footnote-equivalent replacement for the sharded MySQL design.&lt;/p&gt;
&lt;p&gt;Vitess supplies VTGate routing, VSchema and Vindex metadata, and VReplication workflows for movement and resharding. It can replace much of a custom MySQL sharding control plane, but its transaction modes and compatibility still require workload testing (&lt;a href=&quot;https://vitess.io/docs/23.0/reference/features/vschema/&quot;&gt;Vitess 23 VSchema&lt;/a&gt;, &lt;a href=&quot;https://vitess.io/docs/23.0/reference/features/distributed-transaction/&quot;&gt;Vitess 23 distributed transactions&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;TiDB is a distributed relational database with MySQL protocol and syntax compatibility, not an InnoDB sharding layer. Its documented incompatibilities and transaction semantics require a separate migration assessment (&lt;a href=&quot;https://docs.pingcap.com/tidb/v8.5/mysql-compatibility/&quot;&gt;TiDB 8.5 compatibility&lt;/a&gt;, &lt;a href=&quot;https://docs.pingcap.com/tidb/v8.5/transaction-overview/&quot;&gt;TiDB 8.5 transactions&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;This series has not run either platform. They remain theoretical alternatives until a dedicated architecture, compatibility, operations, and recovery lab produces evidence.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The comparison above combines documented system behavior with a proposed RetailCo decision. It does not claim that any candidate wins a benchmark or meets a service objective.&lt;/p&gt;
&lt;h3 id=&quot;what-the-documentation-actually-proves&quot;&gt;What the documentation actually proves&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; AWS documents one primary writer for standard Aurora, managed routers and shards for Limitless, and an OCC-based distributed architecture for DSQL. Citus documents explicit distribution columns, coordinator routing, workers, and colocated distributed tables. MySQL documents InnoDB Cluster as an HA group whose default topology has one read-write primary.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Classify each product by ownership and transaction behavior before comparing performance. Standard Aurora is one write domain. Limitless and Citus are explicit locality designs. DSQL is a distributed keyspace with commit-time conflict handling. Sharded MySQL is an application ownership design with HA inside each shard.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The derived result is a shortlist that matches workload responsibility, not a generic ranking. Two products can both scale writes while requiring completely different schema, retry, and recovery designs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; “Horizontal write scale” is an outcome category, not an architecture description.&lt;/p&gt;
&lt;h3 id=&quot;one-comparison-lab-five-honest-baselines&quot;&gt;One comparison lab, five honest baselines&lt;/h3&gt;
&lt;p&gt;Use the same RetailCo business workload, but do not force identical implementation where the platform semantics differ.&lt;/p&gt;
&lt;p&gt;Status: &lt;strong&gt;PROPOSED LAB — NO EMPIRICAL RESULTS&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Workload families&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;tenant-local order creation and item inserts;&lt;/li&gt;
&lt;li&gt;inventory reservation and release;&lt;/li&gt;
&lt;li&gt;idempotent payment-state transitions;&lt;/li&gt;
&lt;li&gt;one hot tenant and one hot key;&lt;/li&gt;
&lt;li&gt;cross-tenant operational query;&lt;/li&gt;
&lt;li&gt;large reconciliation batch;&lt;/li&gt;
&lt;li&gt;schema change under foreground traffic;&lt;/li&gt;
&lt;li&gt;backup restore and business reconciliation.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Candidate-specific implementation&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Standard Aurora uses one conventional transaction boundary.&lt;/li&gt;
&lt;li&gt;Limitless and Citus colocate tenant-owned tables by &lt;code&gt;tenant_id&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;DSQL uses bounded transactions, random primary keys, and complete-transaction retry.&lt;/li&gt;
&lt;li&gt;Sharded MySQL routes the tenant through a placement catalog into one InnoDB Cluster.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Capture attempted and successful TPS, P50, P95, P99, commit latency, errors, aborts, retries, conflicts, CPU, storage latency, I/O, WAL or binlog, network, connection utilization, coordinator or router load, shard skew, event lag where used, and cost inputs. Publish exact versions, Regions, node or capacity shapes, durability, dataset, client count, warm-up, duration, and raw results.&lt;/p&gt;
&lt;p&gt;An average TPS chart is insufficient. Report tenant-local, hot-key, cross-boundary, and failure results separately.&lt;/p&gt;
&lt;h3 id=&quot;failure-and-recovery-matrix&quot;&gt;Failure and recovery matrix&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Test[controlled failure test] --&gt; Detect[detection]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Detect --&gt; Ownership[identify surviving write owner]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Ownership --&gt; Route[converge client routing]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Route --&gt; Retry[resolve retries and ambiguous commits]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Retry --&gt; Repair[repair topology or capacity]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Repair --&gt; Restore[restore backup in isolation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Restore --&gt; Reconcile[reconcile business invariants]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For every candidate, test:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;loss of the current write-serving compute path;&lt;/li&gt;
&lt;li&gt;network delay and partial reachability;&lt;/li&gt;
&lt;li&gt;one hot shard, worker, key range, or tenant;&lt;/li&gt;
&lt;li&gt;a topology or capacity change interrupted midway;&lt;/li&gt;
&lt;li&gt;client timeout after an unknown commit outcome;&lt;/li&gt;
&lt;li&gt;schema deployment failure;&lt;/li&gt;
&lt;li&gt;restore into an isolated environment;&lt;/li&gt;
&lt;li&gt;correctness of tenant ownership or distributed state after restore.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The exact injection differs. Standard Aurora tests writer failover. Limitless tests router and shard behavior plus documented snapshot and PITR recovery (&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless-bak.html&quot;&gt;Limitless backup&lt;/a&gt;). Citus tests coordinator, worker, rebalance, and distributed restore. DSQL tests retry exhaustion, multi-Region behavior, outbox-relay interruption, and restoration into a new cluster through AWS Backup (&lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/backup-aurora-dsql.html&quot;&gt;DSQL backup and restore&lt;/a&gt;). Sharded MySQL tests one shard failure, placement-catalog outage, tenant movement, and point-in-time ownership reconstruction.&lt;/p&gt;
&lt;h3 id=&quot;cost-without-a-misleading-price-table&quot;&gt;Cost without a misleading price table&lt;/h3&gt;
&lt;p&gt;Public prices and capacity units change. More importantly, a unit-price comparison hides different labor and risk.&lt;/p&gt;
&lt;p&gt;Record four cost classes:&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Cost class&lt;/th&gt;&lt;th&gt;Examples&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Steady infrastructure&lt;/td&gt;&lt;td&gt;Minimum routers, shards, workers, replicas, clusters, storage, backup, network&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Elastic workload&lt;/td&gt;&lt;td&gt;Capacity growth, distributed query fanout, cross-zone or cross-Region traffic&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Platform engineering&lt;/td&gt;&lt;td&gt;Routing catalog, migration tooling, schema fleet, observability, automation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failure and change&lt;/td&gt;&lt;td&gt;Restore tests, rebalancing, tenant moves, upgrades, incident staffing&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Limitless and DSQL may reduce visible server operations while increasing the importance of schema compatibility and application design. Citus and sharded MySQL may avoid some managed-service boundaries while increasing platform labor. The cheapest architecture is the one that meets the workload and recovery contract with the lowest total operated complexity—not the smallest hourly number in isolation.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Decision mistake&lt;/th&gt;&lt;th&gt;Why it fails&lt;/th&gt;&lt;th&gt;Better gate&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Choose from a horizontal-scale checkbox&lt;/td&gt;&lt;td&gt;The ownership and transaction model remains unknown&lt;/td&gt;&lt;td&gt;Require a write-ownership diagram and locality report&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Treat write forwarding as another writer&lt;/td&gt;&lt;td&gt;Forwarded writes still commit on the Aurora writer&lt;/td&gt;&lt;td&gt;Use it for routing convenience, not independent capacity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Select a low-cardinality key&lt;/td&gt;&lt;td&gt;Work and storage concentrate into too few ownership domains&lt;/td&gt;&lt;td&gt;Measure cardinality, skew, and transaction coverage&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Assume one hot tenant will spread automatically&lt;/td&gt;&lt;td&gt;One ownership key normally maps to one placement&lt;/td&gt;&lt;td&gt;Sub-shard, isolate another domain, or give the tenant a dedicated design&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Compare only tenant-local TPS&lt;/td&gt;&lt;td&gt;Cross-boundary work and hot keys determine the real limit&lt;/td&gt;&lt;td&gt;Publish locality classes separately&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Equate PostgreSQL protocol with PostgreSQL migration&lt;/td&gt;&lt;td&gt;SQL, constraints, isolation, functions, catalogs, and tools differ&lt;/td&gt;&lt;td&gt;Replay the full compatibility inventory&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Equate managed with operation-free&lt;/td&gt;&lt;td&gt;Retries, migrations, backups, restores, and reconciliation remain&lt;/td&gt;&lt;td&gt;Assign runbook and on-call ownership before approval&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Equate self-managed with flexibility only&lt;/td&gt;&lt;td&gt;HA, upgrades, security, rebalance, and restore become internal products&lt;/td&gt;&lt;td&gt;Fund the distributed database platform explicitly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Ignore migration until after selection&lt;/td&gt;&lt;td&gt;A correct target can still be unreachable safely&lt;/td&gt;&lt;td&gt;Prove backfill, CDC, validation, cutover, and rollback boundaries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Restore data without routing history&lt;/td&gt;&lt;td&gt;Two shards or clusters can appear authoritative&lt;/td&gt;&lt;td&gt;Recover placement and ownership at the same logical point&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Choose microservices before data ownership&lt;/td&gt;&lt;td&gt;Deployment units multiply while one database remains coupled&lt;/td&gt;&lt;td&gt;Establish domain write ownership first&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;when-i-would-not-distribute-the-database&quot;&gt;When I would not distribute the database&lt;/h3&gt;
&lt;p&gt;I would not choose any distributed option when the conventional writer has measured headroom after local repairs, the workload has no stable ownership key, the dominant invariant is genuinely global, or the organization cannot operate partial failure and reconciliation.&lt;/p&gt;
&lt;p&gt;I would also defer distribution when the migration plan is “copy the tables and switch the connection string.” A live 40-TB platform needs a source-of-truth state machine, continuous change capture or another controlled delta mechanism, business reconciliation, canary ownership, a write fence, and a defined point after which rollback becomes another migration.&lt;/p&gt;
&lt;p&gt;Distribution should buy one of four concrete outcomes: independent write capacity, smaller failure domains, independent domain evolution, or a multi-Region write model the business actually needs. If none is measurable, retain the simpler database.&lt;/p&gt;
&lt;h3 id=&quot;retailco-conclusion&quot;&gt;RetailCo conclusion&lt;/h3&gt;
&lt;p&gt;RetailCo’s primary PostgreSQL scenario has a stable &lt;code&gt;tenant_id&lt;/code&gt;, predominantly tenant-local order writes, and a preference for managed operations. &lt;strong&gt;Aurora PostgreSQL Limitless is the first proof of concept. Citus is the control-rich alternative. Application-sharded Aurora clusters are the compatibility fallback.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Aurora DSQL should be evaluated separately for a new, bounded, retry-safe service that needs its distributed or multi-Region model. It should not be treated as a drop-in destination for the existing PostgreSQL monolith.&lt;/p&gt;
&lt;p&gt;For a MySQL business line, &lt;strong&gt;tenant-sharded InnoDB Clusters are the conventional-semantics option&lt;/strong&gt;, with Vitess evaluated if building the entire routing and resharding layer internally is not desirable. TiDB requires a separate distributed-database migration decision.&lt;/p&gt;
&lt;p&gt;If transaction traces show that tenant locality is weak but payment, inventory, and order history have clean business boundaries, RetailCo should decompose domains before selecting a distributed database.&lt;/p&gt;
&lt;p&gt;The recommendation can be summarized without pretending the tradeoffs disappear:&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;First proof of concept&lt;/th&gt;&lt;th&gt;Main reason&lt;/th&gt;&lt;th&gt;Publication blocker&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Managed PostgreSQL with stable tenant locality&lt;/td&gt;&lt;td&gt;Aurora Limitless&lt;/td&gt;&lt;td&gt;Managed routers and shards with explicit collocation&lt;/td&gt;&lt;td&gt;Compatibility and hot-tenant lab&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;PostgreSQL requiring control and extensions&lt;/td&gt;&lt;td&gt;Citus&lt;/td&gt;&lt;td&gt;Explicit workers, placement, and PostgreSQL operations&lt;/td&gt;&lt;td&gt;Full HA, rebalance, backup, and restore proof&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;New bounded distributed service&lt;/td&gt;&lt;td&gt;Aurora DSQL&lt;/td&gt;&lt;td&gt;Managed distribution and concurrent multi-Region endpoints&lt;/td&gt;&lt;td&gt;Retry, integrity, compatibility, and latency proof&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Existing tenant-local MySQL estate&lt;/td&gt;&lt;td&gt;Sharded InnoDB Clusters&lt;/td&gt;&lt;td&gt;Conventional MySQL inside explicit write domains&lt;/td&gt;&lt;td&gt;Routing, movement, schema, backup, and ownership proof&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No durable tenant key but clear domains&lt;/td&gt;&lt;td&gt;Database decomposition&lt;/td&gt;&lt;td&gt;Scale payment, inventory, or another domain independently&lt;/td&gt;&lt;td&gt;Workflow, outbox, reconciliation, and recovery proof&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One writer retains forecast headroom&lt;/td&gt;&lt;td&gt;Standard Aurora&lt;/td&gt;&lt;td&gt;Simplest correct transaction and recovery boundary&lt;/td&gt;&lt;td&gt;Capacity evidence and reopen threshold&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Product feature matrices hide the write-ownership, transaction-locality, compatibility, and recovery decisions that determine whether scale-out works.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build the workload dossier, reject incompatible ownership models, and run one candidate per scenario rather than declaring a universal winner.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Replay production-shaped tenant-local, hot-key, cross-boundary, schema, failure, and restore workloads with exact versions and archived evidence. Require business reconciliation, not only infrastructure health.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; For RetailCo’s managed PostgreSQL path, begin with a Limitless compatibility and locality proof. Run Citus as the control-rich alternative, DSQL only for a bounded redesign, and sharded MySQL only for the MySQL estate. Keep standard Aurora as the baseline every distributed option must justify replacing.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>Azure Synapse Cost Optimization: DWU Right-Sizing, Serverless, and Hybrid Benefit</title><link>https://rajivonai.com/blog/2026-02-18-azure-synapse-cost-optimization/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-02-18-azure-synapse-cost-optimization/</guid><description>How to reduce your Azure Synapse compute bill by right-sizing dedicated pools and offloading to serverless.</description><pubDate>Wed, 18 Feb 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Many data warehouse deployments are oversized for their 95th percentile workload, silently burning budget on idle compute capacity.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Data engineering teams often provision Azure Synapse dedicated SQL pools to handle peak quarter-end load, but leave them running at that size 24/7.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Synapse dedicated pools charge by the Data Warehouse Unit (DWU) hour. When ad-hoc analyst queries compete with SLA-bound ETL jobs on the same oversized pool, costs spiral. How do you optimize Synapse performance without paying for idle DWUs?&lt;/p&gt;
&lt;h2 id=&quot;synapse-optimization-strategy&quot;&gt;Synapse Optimization Strategy&lt;/h2&gt;
&lt;p&gt;Cost reduction in Synapse relies on three primary levers:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;DWU Right-Sizing&lt;/strong&gt;: Audit peak vs provisioned DWU. Most pools are 4-10x oversized.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Serverless Offload&lt;/strong&gt;: Move ad-hoc and exploratory queries to Synapse Serverless SQL pools, where you pay per TB scanned, not per hour.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Auto-Pause Schedules&lt;/strong&gt;: Pause non-prod pools during nights and weekends.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern is to isolate ETL workloads on dedicated pools (right-sized for the specific data integration window) while pointing BI tools and analysts to serverless endpoints. Additionally, applying Azure Hybrid Benefit to the underlying SQL Server licenses (if available) can significantly reduce the baseline compute cost.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Optimization&lt;/th&gt;&lt;th&gt;Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Serverless SQL&lt;/td&gt;&lt;td&gt;Unoptimized queries without partition pruning can scan massive amounts of data, leading to unexpected per-TB charges.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Auto-Pause&lt;/td&gt;&lt;td&gt;Resuming a paused pool takes time and clears the cache, potentially causing the first queries to run slower.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Synapse dedicated pools are expensive when left running at peak capacity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Right-size DWUs, offload ad-hoc queries to serverless, and pause non-prod environments.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Organizations routinely cut their Synapse compute bill in half using these exact levers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Use our &lt;a href=&quot;https://rajivonai.com/tools/azure-synapse-cost-calculator/&quot;&gt;Azure Synapse Cost Optimizer&lt;/a&gt; to estimate your monthly savings. Request a Cloud Database Cost Review for a deeper analysis.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category></item><item><title>ElastiCache for Valkey Performance Engineering: Nodes, Shards, Replicas and Failover</title><link>https://rajivonai.com/blog/2026-02-17-elasticache-for-valkey-performance-engineering-nodes-shards-replicas-failover/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-02-17-elasticache-for-valkey-performance-engineering-nodes-shards-replicas-failover/</guid><description>A production triage and performance engineering guide for Amazon ElastiCache for Valkey: diagnosing shard skew, replication lag, cluster-mode failover, and managed-service boundaries.</description><pubDate>Tue, 17 Feb 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;In Amazon ElastiCache for Valkey, high host CPU does not prove engine saturation, sub-second failover does not guarantee zero data loss, and cluster rebalancing can introduce cross-slot command errors under live traffic. Triage requires reconciling CloudWatch host metrics with internal cluster topology and managed service events.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Amazon ElastiCache for Valkey provides a fully managed, in-memory caching and data structure service with automated Multi-AZ failover, online resharding, and automated parameter management. Running Valkey (versions 7.2 and 8.0) in ElastiCache removes the operational burden of OS provisioning, kernel tuning, and manual replication setup.&lt;/p&gt;
&lt;p&gt;However, operating behind a managed cloud boundary alters the diagnostic workflow fundamentally:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Host Shell Access:&lt;/strong&gt; SREs and DBAs cannot inspect &lt;code&gt;/proc/vmstat&lt;/code&gt;, run &lt;code&gt;ethtool&lt;/code&gt; for network driver drops, or attach debuggers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Restricted Command Interface:&lt;/strong&gt; Administrative commands such as &lt;code&gt;CONFIG SET&lt;/code&gt;, &lt;code&gt;SHUTDOWN&lt;/code&gt;, &lt;code&gt;SLAVEOF&lt;/code&gt;, and &lt;code&gt;BGREWRITEAOF&lt;/code&gt; are blocked by AWS IAM and engine ACLs. Configuration changes must flow through ElastiCache Parameter Groups.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dual Telemetry Streams:&lt;/strong&gt; Performance data is split between CloudWatch metrics (&lt;code&gt;EngineCPUUtilization&lt;/code&gt;, &lt;code&gt;DatabaseMemoryUsagePercentage&lt;/code&gt;, &lt;code&gt;NetworkBandwidthInAllowanceExceeded&lt;/code&gt;, &lt;code&gt;ReplicationLag&lt;/code&gt;), ElastiCache Service Events, and client-accessible engine commands (&lt;code&gt;INFO&lt;/code&gt;, &lt;code&gt;CLUSTER SLOTS&lt;/code&gt;, &lt;code&gt;SLOWLOG&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When an upstream microservice experiences intermittent connection timeouts or cache miss storms during an ElastiCache incident, the diagnostic challenge is correlating CloudWatch infrastructure metrics, Valkey engine counters, and AWS control-plane maintenance events without making unsupported assumptions about inaccessible host layers.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Managed ElastiCache clusters introduce complex multi-node dynamics across shards, replicas, and routing topologies:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Host CPU vs. Engine CPU Misinterpretation:&lt;/strong&gt; &lt;code&gt;CPUUtilization&lt;/code&gt; averages compute across all instance vCPUs (including background threads and network I/O). If the single-threaded Valkey main loop is 100% saturated by an $O(N)$ query, &lt;code&gt;CPUUtilization&lt;/code&gt; on a 4-vCPU node may show only 25%, while &lt;code&gt;EngineCPUUtilization&lt;/code&gt; is pegged at 100%.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cluster Mode Topology and Slot Skew:&lt;/strong&gt; In Cluster Mode Enabled (CME) architectures, 16,384 hash slots are partitioned across shards. Workload imbalance, hot hash tags (e.g., &lt;code&gt;{tenant_42}:data&lt;/code&gt;), or uneven slot allocation concentrates write traffic onto a single shard, causing hot-node degradation while other shards remain idle.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replication Lag and Multi-AZ Failover Data Loss:&lt;/strong&gt; Valkey replication to read replicas is asynchronous. When a primary fails, ElastiCache promotes a replica automatically. If &lt;code&gt;ReplicationLag&lt;/code&gt; was elevated due to heavy write throughput or large key transfers prior to failover, writes acknowledged by the old primary but unapplied on the replica are permanently lost.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Online Resharding and Slot Migration Errors:&lt;/strong&gt; Adding or removing shards in a live cluster migrates hash slots online. Multi-key operations spanning slots in migration return &lt;code&gt;TRYAGAIN&lt;/code&gt; or &lt;code&gt;CROSSSLOT&lt;/code&gt; errors unless client drivers handle slot redirection (&lt;code&gt;MOVED&lt;/code&gt; and &lt;code&gt;ASK&lt;/code&gt;) correctly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Connection Storms on Primary Replacement:&lt;/strong&gt; During planned maintenance or unplanned failover, all connected application clients disconnect and reconnect simultaneously to the promoted primary, causing connection queue drops and thread pool starvation in upstream microservices.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;How do we systematically isolate whether an ElastiCache performance degradation stems from engine CPU saturation, shard key skew, asynchronous replication delay, managed maintenance failover, or client driver misconfiguration?&lt;/p&gt;
&lt;h2 id=&quot;build-a-managed-elasticache-evidence-pack&quot;&gt;Build a Managed ElastiCache Evidence Pack&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[application p99 latency timeouts and MOVED errors] --&gt; E[time-bounded ElastiCache incident evidence pack]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B[CloudWatch EngineCPU FreeableMemory and ENA allowances] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[Valkey INFO SLOWLOG and CLUSTER SLOTS per node] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[ElastiCache service events and parameter group state] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[validate node roles cluster topology and counter deltas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[reconcile CloudWatch metrics with engine commandstats]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[LLM observations hypotheses contradictions and gaps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[SRE and Cloud DBA verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[remediation validation and rollback]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To diagnose ElastiCache incidents, assemble an evidence pack synchronizing CloudWatch metrics, ElastiCache Service Events, and Valkey &lt;code&gt;INFO&lt;/code&gt; outputs across every primary and replica node in the cluster:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CloudWatch Infrastructure Metrics (per node):&lt;/strong&gt; &lt;code&gt;EngineCPUUtilization&lt;/code&gt;, &lt;code&gt;CPUUtilization&lt;/code&gt;, &lt;code&gt;FreeableMemory&lt;/code&gt;, &lt;code&gt;BytesUsedForCache&lt;/code&gt;, &lt;code&gt;DatabaseMemoryUsagePercentage&lt;/code&gt;, &lt;code&gt;NetworkBytesIn&lt;/code&gt;, &lt;code&gt;NetworkBytesOut&lt;/code&gt;, &lt;code&gt;NetworkBandwidthInAllowanceExceeded&lt;/code&gt;, &lt;code&gt;NetworkPacketsPerSecondAllowanceExceeded&lt;/code&gt;, &lt;code&gt;ReplicationLag&lt;/code&gt;, &lt;code&gt;CurrConnections&lt;/code&gt;, &lt;code&gt;Evictions&lt;/code&gt;, &lt;code&gt;CacheHits&lt;/code&gt;, &lt;code&gt;CacheMisses&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ElastiCache Service Events:&lt;/strong&gt; Capture event logs filtered by replication group and cache cluster ID (e.g., “Failover to replica completed”, “Added shard”, “Applying parameter group”, “Automated backup started”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Valkey Engine State (&lt;code&gt;INFO&lt;/code&gt;, &lt;code&gt;CLUSTER NODES&lt;/code&gt;, &lt;code&gt;CLUSTER SLOTS&lt;/code&gt;):&lt;/strong&gt; Extract node IDs, roles (&lt;code&gt;master&lt;/code&gt;/&lt;code&gt;slave&lt;/code&gt;), assigned slot ranges, connected client counts, &lt;code&gt;instantaneous_ops_per_sec&lt;/code&gt;, and differential &lt;code&gt;INFO commandstats&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parameter Group Configuration:&lt;/strong&gt; Inspect active parameter values, specifically &lt;code&gt;reserved-memory-percent&lt;/code&gt; (default 25%), &lt;code&gt;maxmemory-policy&lt;/code&gt;, &lt;code&gt;timeout&lt;/code&gt;, &lt;code&gt;tcp-keepalive&lt;/code&gt;, and &lt;code&gt;cluster-enabled&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The LLM correlates these synchronized dimensions to produce four distinct diagnostic artifacts:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Observations:&lt;/strong&gt; Quantitative facts tied to specific node IDs and timestamps (e.g., “Shard 0002 primary &lt;code&gt;cache.r7g.large&lt;/code&gt; reached 99.4% &lt;code&gt;EngineCPUUtilization&lt;/code&gt; while Shards 0001 and 0003 remained below 15%”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hypotheses:&lt;/strong&gt; Ranked explanations identifying the failure layer (e.g., “Hypothesis 1: Hash tag slot skew concentrating multi-key operations on Shard 0002; Hypothesis 2: Asynchronous replication buffer exhaustion during snapshotting”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Missing Evidence:&lt;/strong&gt; Inaccessible or unobserved telemetry (e.g., “Need client-side cluster routing logs to verify if application driver is retrying on &lt;code&gt;MOVED&lt;/code&gt; redirections”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Actions:&lt;/strong&gt; AWS CLI or management console operations (e.g., scaling cluster shards, modifying parameter groups, or refactoring client hash tags).&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Plausible failure classes&lt;/th&gt;&lt;th&gt;Evidence that separates them&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Latency spike on one shard only&lt;/td&gt;&lt;td&gt;Key distribution skew, hash tag hotspot, heavy single-key writes&lt;/td&gt;&lt;td&gt;Shard-specific &lt;code&gt;EngineCPUUtilization&lt;/code&gt;, &lt;code&gt;BytesUsedForCache&lt;/code&gt;, &lt;code&gt;CLUSTER SLOTS&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;CROSSSLOT&lt;/code&gt; or &lt;code&gt;TRYAGAIN&lt;/code&gt; errors&lt;/td&gt;&lt;td&gt;Multi-key command during resharding, missing hash tag &lt;code&gt;{...}&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Application error logs, ElastiCache resharding status, &lt;code&gt;INFO commandstats&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Read replica lag climbing&lt;/td&gt;&lt;td&gt;Heavy write throughput, replica CPU saturation, network allowance limits&lt;/td&gt;&lt;td&gt;&lt;code&gt;ReplicationLag&lt;/code&gt;, replica &lt;code&gt;EngineCPUUtilization&lt;/code&gt;, &lt;code&gt;NetworkBandwidthInAllowanceExceeded&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sudden failover and connection drops&lt;/td&gt;&lt;td&gt;Unhealthy primary health check, managed maintenance event, OOM crash&lt;/td&gt;&lt;td&gt;ElastiCache Service Events, &lt;code&gt;EngineCPUUtilization&lt;/code&gt; spike prior to event, CloudWatch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High client timeouts with low CPU&lt;/td&gt;&lt;td&gt;Network PPS allowance exceeded, connection pool exhaustion&lt;/td&gt;&lt;td&gt;&lt;code&gt;NetworkPacketsPerSecondAllowanceExceeded&lt;/code&gt;, &lt;code&gt;CurrConnections&lt;/code&gt; delta, client socket metrics&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Memory evictions while memory looks free&lt;/td&gt;&lt;td&gt;&lt;code&gt;reserved-memory-percent&lt;/code&gt; limit reached, unevictable keys under volatile policy&lt;/td&gt;&lt;td&gt;&lt;code&gt;DatabaseMemoryUsagePercentage&lt;/code&gt;, &lt;code&gt;FreeableMemory&lt;/code&gt;, &lt;code&gt;Evictions&lt;/code&gt;, &lt;code&gt;maxmemory-policy&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[ElastiCache latency or failover alert] --&gt; B[1. Check ElastiCache Service Events and node role stability]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[2. Reconcile EngineCPUUtilization against host CPUUtilization across shards]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[3. Inspect slot distribution and memory skew across cluster nodes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[4. Evaluate ReplicationLag and network allowance throttling]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[5. Verify reserved-memory-percent and parameter group configuration]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Synthesize observations hypotheses and Cloud DBA actions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-check-elasticache-service-events-and-node-role-stability&quot;&gt;1. Check ElastiCache Service Events and node role stability&lt;/h3&gt;
&lt;p&gt;Before analyzing engine metrics, inspect ElastiCache Service Events for the cluster over the preceding 2 hours:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; elasticache&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; describe-events&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --source-type&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; cache-cluster&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --start-time&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -u&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -v-2H&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +&quot;%Y-%m-%dT%H:%M:%SZ&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Verify whether AWS initiated an automated node replacement, Multi-AZ failover, parameter group update, or automated snapshot. If a failover occurred, map which node was promoted and check whether client timeouts align precisely with the promotion window.&lt;/p&gt;
&lt;h3 id=&quot;2-reconcile-enginecpuutilization-against-host-cpuutilization-across-shards&quot;&gt;2. Reconcile EngineCPUUtilization against host CPUUtilization across shards&lt;/h3&gt;
&lt;p&gt;Compare &lt;code&gt;EngineCPUUtilization&lt;/code&gt; and &lt;code&gt;CPUUtilization&lt;/code&gt; across all shards:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;EngineCPUUtilization&lt;/code&gt; is near 100% while &lt;code&gt;CPUUtilization&lt;/code&gt; is below 30%, a single-threaded Valkey workload bottleneck exists (e.g., $O(N)$ command or blocking Lua script).&lt;/li&gt;
&lt;li&gt;If both &lt;code&gt;EngineCPUUtilization&lt;/code&gt; and &lt;code&gt;CPUUtilization&lt;/code&gt; are elevated, the node is experiencing heavy multi-threaded network I/O parsing, TLS encryption overhead, or high connection churn.&lt;/li&gt;
&lt;li&gt;Compare metrics across shards: if only one shard is elevated, the issue is workload or slot skew, not cluster-wide traffic growth.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-inspect-slot-distribution-and-memory-skew-across-cluster-nodes&quot;&gt;3. Inspect slot distribution and memory skew across cluster nodes&lt;/h3&gt;
&lt;p&gt;Run &lt;code&gt;CLUSTER SLOTS&lt;/code&gt; or &lt;code&gt;CLUSTER NODES&lt;/code&gt; to map slot assignments:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Verify that each shard owns an approximately equal slice of the 16,384 total slots (~5,461 slots per shard in a 3-shard cluster).&lt;/li&gt;
&lt;li&gt;Compare &lt;code&gt;BytesUsedForCache&lt;/code&gt; and &lt;code&gt;DatabaseMemoryUsagePercentage&lt;/code&gt; across shards in CloudWatch. If one shard stores 80% of total cluster memory, inspect application key patterns for broad hash tags (e.g., &lt;code&gt;{app}:user:1&lt;/code&gt; forces all user keys into the same hash slot).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;4-evaluate-replicationlag-and-network-allowance-throttling&quot;&gt;4. Evaluate ReplicationLag and network allowance throttling&lt;/h3&gt;
&lt;p&gt;Inspect CloudWatch &lt;code&gt;ReplicationLag&lt;/code&gt; for all read replicas:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A rising &lt;code&gt;ReplicationLag&lt;/code&gt; (measured in seconds) indicates that write volume on the primary exceeds the replica’s ingestion capacity, or network bandwidth between AZs is constrained.&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;NetworkBandwidthInAllowanceExceeded&lt;/code&gt; and &lt;code&gt;NetworkPacketsPerSecondAllowanceExceeded&lt;/code&gt;. Nitro-based ElastiCache nodes enforce per-instance network bandwidth and PPS caps. If write traffic or snapshot synchronization exceeds allowances, packets are throttled at the hypervisor.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;5-verify-reserved-memory-percent-and-parameter-group-configuration&quot;&gt;5. Verify reserved-memory-percent and parameter group configuration&lt;/h3&gt;
&lt;p&gt;ElastiCache manages memory allocation via the &lt;code&gt;reserved-memory-percent&lt;/code&gt; parameter in custom parameter groups (default 25% for Valkey):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;reserved-memory-percent&lt;/code&gt; is set to 0, background snapshots (&lt;code&gt;BGSAVE&lt;/code&gt;) and replica synchronization can consume all free RAM during write bursts, triggering Linux OOM termination and unexpected node failover.&lt;/li&gt;
&lt;li&gt;As &lt;code&gt;DatabaseMemoryUsagePercentage&lt;/code&gt; approaches 100%, Valkey begins evicting keys according to &lt;code&gt;maxmemory-policy&lt;/code&gt; even when CloudWatch &lt;code&gt;FreeableMemory&lt;/code&gt; appears adequate.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That threshold is easy to get wrong, and getting it wrong delays the alert until after eviction has already started. &lt;code&gt;DatabaseMemoryUsagePercentage&lt;/code&gt; measures &lt;code&gt;used_memory&lt;/code&gt; against the engine’s &lt;strong&gt;configured &lt;code&gt;maxmemory&lt;/code&gt;&lt;/strong&gt;, and &lt;code&gt;reserved-memory-percent&lt;/code&gt; has already been subtracted when ElastiCache derives that &lt;code&gt;maxmemory&lt;/code&gt;. The reserved fraction is therefore baked into the denominator — subtracting it a second time to build a &lt;code&gt;100 - reserved-memory-percent&lt;/code&gt; alarm double-counts it and sets the threshold far below the real eviction boundary.&lt;/p&gt;
&lt;p&gt;Compare against 100% and reserve alerting headroom explicitly:&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Metric&lt;/th&gt;&lt;th&gt;Denominator&lt;/th&gt;&lt;th&gt;Use it for&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;DatabaseMemoryUsagePercentage&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Configured &lt;code&gt;maxmemory&lt;/code&gt; (reserved memory already excluded)&lt;/td&gt;&lt;td&gt;Eviction proximity — alert in the high 80s/90s, not at &lt;code&gt;100 - reserved&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;DatabaseMemoryUsageCountedForEvictPercentage&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Memory actually counted toward eviction&lt;/td&gt;&lt;td&gt;The closer read on eviction pressure where the two diverge&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;FreeableMemory&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Host RAM&lt;/td&gt;&lt;td&gt;OS and fork headroom, not engine eviction&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Where AWS documentation describes this behavior for “Valkey and Redis OSS” together, that reflects a shared engine lineage in the documentation — it is not a statement that this track covers both products. This series targets Valkey.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[ElastiCache latency or failure incident] --&gt; B{Service Events show recent failover or maintenance}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes| C[Correlate client reconnect storm and DNS TTL propagation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No| D{EngineCPUUtilization near 100% on one shard}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Yes| E{Is memory or write traffic skewed to this shard}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|Yes| F[Refactor hash tags and rebalance slot distribution]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|No| G[Profile expensive commands via SLOWLOG on that shard]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|No| H{ReplicationLag climbing on replicas}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|Yes| I[Inspect replica EngineCPU and NetworkBandwidth allowances]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|No| J{Network allowances exceeded on primary}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|Yes| K[Scale up node instance family for higher network baseline]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|No| L[Verify client connection pooling and timeout settings]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://docs.aws.amazon.com/AmazonElastiCache/latest/valkey-ug/WhatIs.html&quot;&gt;AWS ElastiCache for Valkey User Guide&lt;/a&gt; establishes that ElastiCache provides native support for open-source Valkey with automated cluster management. AWS documentation highlights that &lt;code&gt;EngineCPUUtilization&lt;/code&gt; is the primary health metric for the Valkey core process; relying on standard &lt;code&gt;CPUUtilization&lt;/code&gt; will obscure single-threaded event loop saturation on multi-core instances.&lt;/p&gt;
&lt;p&gt;AWS &lt;a href=&quot;https://docs.aws.amazon.com/AmazonElastiCache/latest/valkey-ug/BestPractices.html&quot;&gt;ElastiCache Best Practices documentation&lt;/a&gt; dictates maintaining &lt;code&gt;reserved-memory-percent&lt;/code&gt; at a minimum of 25% (or allocating dedicated &lt;code&gt;reserved-memory&lt;/code&gt;). Without reserved memory, failover synchronization and automated backups can trigger memory exhaustion during write-heavy workloads.&lt;/p&gt;
&lt;p&gt;According to the &lt;a href=&quot;https://docs.aws.amazon.com/AmazonElastiCache/latest/valkey-ug/Scaling.html&quot;&gt;AWS ElastiCache Resharding and Scaling documentation&lt;/a&gt;, online horizontal scaling adds or removes shards without cluster downtime. However, multi-key operations (such as &lt;code&gt;MGET&lt;/code&gt;, &lt;code&gt;MSET&lt;/code&gt;, or transactions) executed against keys residing in migrating slots will fail with &lt;code&gt;CROSSSLOT&lt;/code&gt; or &lt;code&gt;TRYAGAIN&lt;/code&gt; errors unless the keys share an identical hash tag &lt;code&gt;{...}&lt;/code&gt; that pins them to a single slot.&lt;/p&gt;
&lt;p&gt;Documented Multi-AZ architecture patterns confirm that Valkey replication across Availability Zones is asynchronous. During an unexpected primary outage, the promoted replica may experience an RPO (Recovery Point Objective) greater than zero if &lt;code&gt;ReplicationLag&lt;/code&gt; was non-zero at the time of failure. SREs must monitor &lt;code&gt;ReplicationLag&lt;/code&gt; continuously as an availability and durability metric.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven root cause&lt;/th&gt;&lt;th&gt;Candidate remediation&lt;/th&gt;&lt;th&gt;Validation criteria&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Shard slot / key skew from broad hash tags&lt;/td&gt;&lt;td&gt;Remove broad hash tags &lt;code&gt;{...}&lt;/code&gt;; distribute keys across independent slots&lt;/td&gt;&lt;td&gt;&lt;code&gt;BytesUsedForCache&lt;/code&gt; and &lt;code&gt;EngineCPUUtilization&lt;/code&gt; equalize across shards&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Engine CPU saturation on primary node&lt;/td&gt;&lt;td&gt;Scale horizontally (add shards via online resharding) or vertically (upgrade node size)&lt;/td&gt;&lt;td&gt;&lt;code&gt;EngineCPUUtilization&lt;/code&gt; drops below 70% across all primary nodes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica falling behind on write throughput&lt;/td&gt;&lt;td&gt;Scale read replica node size vertically or add read replicas to distribute read load&lt;/td&gt;&lt;td&gt;&lt;code&gt;ReplicationLag&lt;/code&gt; returns to 0 seconds&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ENA network allowance throttling&lt;/td&gt;&lt;td&gt;Upgrade instance type (e.g., from &lt;code&gt;cache.m7g.large&lt;/code&gt; to &lt;code&gt;cache.m7g.xlarge&lt;/code&gt; / &lt;code&gt;2xlarge&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;&lt;code&gt;NetworkBandwidthOutAllowanceExceeded&lt;/code&gt; delta drops to 0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Premature evictions under write bursts&lt;/td&gt;&lt;td&gt;Increase &lt;code&gt;reserved-memory-percent&lt;/code&gt; to 25–30% in ElastiCache Parameter Group&lt;/td&gt;&lt;td&gt;Node failovers during snapshots stop; eviction rates normalize&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Client disconnection storm during failover&lt;/td&gt;&lt;td&gt;Configure exponential backoff with jitter in client connection pool&lt;/td&gt;&lt;td&gt;Application p99 latency stabilizes during failover simulations&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;When modifying managed ElastiCache clusters, follow defined rollback procedures:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Parameter Group Reversion:&lt;/strong&gt; If modifying parameters (such as &lt;code&gt;maxmemory-policy&lt;/code&gt;, &lt;code&gt;timeout&lt;/code&gt;, or &lt;code&gt;reserved-memory-percent&lt;/code&gt;) causes unexpected eviction or connection behavior, modify the parameter group to restore previous values and apply changes immediately (&lt;code&gt;ApplyMethod: immediate&lt;/code&gt;).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Resharding Rollback:&lt;/strong&gt; If adding shards introduces application routing errors due to client driver limitations, reduce the shard count with &lt;code&gt;aws elasticache modify-replication-group-shard-configuration&lt;/code&gt;, specifying the target &lt;code&gt;--node-group-count&lt;/code&gt; and, where required, &lt;code&gt;--node-groups-to-remove&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Do not reach for &lt;code&gt;decrease-replica-count&lt;/code&gt; here: it removes &lt;strong&gt;replica nodes within&lt;/strong&gt; existing shards, not the shards themselves, so it will leave the new shards in place while silently stripping their redundancy.&lt;/p&gt;
&lt;p&gt;Treat this as a rollback in name only. Removing shards starts &lt;em&gt;another&lt;/em&gt; online resharding operation that migrates slots off the departing nodes — it is not an instantaneous revert to the prior topology. Before declaring rollback complete, verify that all 16,384 slots are covered, that clients have refreshed their slot maps, that the surviving nodes have memory headroom for the returning keyspace, and that migration has actually finished rather than stalled mid-flight.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Vertical Scaling Rollback:&lt;/strong&gt; If vertical node resizing causes performance regressions, initiate a reverse modification to the prior node type. ElastiCache executes rolling replacements without dropping data.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Client-Side Routing Flags:&lt;/strong&gt; Ensure application client driver updates (e.g., switching from single-node to cluster-mode routing) are controlled by application feature flags to revert to previous connection endpoints if connection storms occur.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why naive reasoning fails&lt;/th&gt;&lt;th&gt;Better diagnostic boundary&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;”Cluster CPU is 20%, we have headroom”&lt;/td&gt;&lt;td&gt;One primary shard is at 100% &lt;code&gt;EngineCPUUtilization&lt;/code&gt; while others idle&lt;/td&gt;&lt;td&gt;Monitor per-node &lt;code&gt;EngineCPUUtilization&lt;/code&gt; and slot traffic&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Failover was seamless with zero loss”&lt;/td&gt;&lt;td&gt;Asynchronous replication means non-zero &lt;code&gt;ReplicationLag&lt;/code&gt; loses data on failover&lt;/td&gt;&lt;td&gt;Verify &lt;code&gt;ReplicationLag&lt;/code&gt; prior to failover and audit write offsets&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”More replicas will speed up writes”&lt;/td&gt;&lt;td&gt;In Valkey, replicas only serve reads and add replication network overhead&lt;/td&gt;&lt;td&gt;Scale shards horizontally to scale write capacity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Adding shards fixes all hot keys”&lt;/td&gt;&lt;td&gt;A single hot key resides on exactly one shard; more shards will not split the key&lt;/td&gt;&lt;td&gt;Implement client-side caching or key salting&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”CloudWatch shows high FreeableMemory”&lt;/td&gt;&lt;td&gt;OS cache memory is counted as freeable, but Valkey may be evicting at &lt;code&gt;maxmemory&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Track &lt;code&gt;DatabaseMemoryUsagePercentage&lt;/code&gt; and &lt;code&gt;Evictions&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Multi-AZ prevents all downtime”&lt;/td&gt;&lt;td&gt;DNS CNAME propagation and client reconnection take 5–15 seconds to stabilize&lt;/td&gt;&lt;td&gt;Measure client-perceived error rates during failover tests&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;To prevent outages and protect cloud infrastructure, strict boundaries govern LLM diagnostic assistance:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Direct AWS API Execution:&lt;/strong&gt; The LLM cannot execute &lt;code&gt;aws elasticache modify-cache-cluster&lt;/code&gt;, initiate failovers, or alter parameter groups autonomously.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Unsanitized Metric Ingestion:&lt;/strong&gt; The LLM must not receive unredacted key names, connection string credentials, or internal VPC IP addresses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Inferences on Missing Shard Telemetry:&lt;/strong&gt; If telemetry only provides cluster-wide aggregates without per-node breakdown, the LLM must explicitly flag missing per-shard metrics rather than inferring uniform load distribution.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Real-Time Traffic Routing:&lt;/strong&gt; The LLM cannot dynamically redirect client traffic between primary and reader endpoints.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Managed ElastiCache for Valkey abstracts host infrastructure while introducing distributed cluster failure modes—including shard skew, replication lag, and failover data loss.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build a synchronized evidence pack correlating CloudWatch &lt;code&gt;EngineCPUUtilization&lt;/code&gt;, network allowance counters, ElastiCache Service Events, and Valkey &lt;code&gt;CLUSTER SLOTS&lt;/code&gt; topology.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Verify load balance across all shards, confirm &lt;code&gt;reserved-memory-percent&lt;/code&gt; headroom, and reconcile &lt;code&gt;ReplicationLag&lt;/code&gt; against client write throughput.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Instrument per-node CloudWatch alarms for your ElastiCache clusters. Then proceed to Part 4 to secure LLM diagnostics with key privacy redaction, audit evidence, and safe operational guardrails.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonElastiCache/latest/valkey-ug/WhatIs.html&quot;&gt;Amazon ElastiCache for Valkey User Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonElastiCache/latest/valkey-ug/BestPractices.html&quot;&gt;Amazon ElastiCache Best Practices Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonElastiCache/latest/valkey-ug/CacheMetrics.html&quot;&gt;Monitoring Amazon ElastiCache with CloudWatch Metrics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonElastiCache/latest/valkey-ug/Scaling.html&quot;&gt;Amazon ElastiCache Scaling and Online Resharding&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://valkey.io/topics/cluster-spec/&quot;&gt;Valkey Clustering Protocol and Hash Slot Specification&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/monitoring-network-performance-ena.html&quot;&gt;AWS EC2 Nitro System Network Performance and Allowances&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>ai-engineering</category><category>failures</category></item><item><title>Finding Expensive Valkey Commands and Data Structures</title><link>https://rajivonai.com/blog/2026-02-12-finding-expensive-valkey-commands-and-data-structures/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-02-12-finding-expensive-valkey-commands-and-data-structures/</guid><description>A production engineering guide to identifying slow commands, hot keys, big collections, unbounded pipelines, and blocking Lua scripts in Valkey without impacting live traffic.</description><pubDate>Thu, 12 Feb 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A single $O(N)$ command in Valkey can stall thousands of concurrent client requests, yet identifying the offending key or query pattern must never rely on invasive commands like &lt;code&gt;KEYS *&lt;/code&gt; or unmetered memory scans. Triage requires extracting differential command statistics, analyzing sampled slowlogs, and inspecting data structure encodings safely.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;When an application experiences tail latency spikes on an in-memory datastore like Valkey (versions 7.2 and 8.0), host-level metrics frequently show a single CPU core pinned at 100% while overall system memory remains stable. Because Valkey executes data-manipulation commands on a single main event loop, any command that takes 5 milliseconds to execute introduces an immediate 5-millisecond queueing delay for every subsequent operation in the pipeline.&lt;/p&gt;
&lt;p&gt;Valkey provides built-in telemetry to diagnose execution bottlenecks: &lt;code&gt;INFO commandstats&lt;/code&gt;, &lt;code&gt;SLOWLOG&lt;/code&gt;, &lt;code&gt;LATENCY DOCTOR&lt;/code&gt;, &lt;code&gt;LATENCY HISTORY&lt;/code&gt;, &lt;code&gt;MEMORY USAGE&lt;/code&gt;, &lt;code&gt;CLIENT LIST&lt;/code&gt;, and internal encoding statistics. However, naive diagnostic queries—such as executing &lt;code&gt;KEYS *&lt;/code&gt;, unthrottled &lt;code&gt;--bigkeys&lt;/code&gt; scans, or running &lt;code&gt;MONITOR&lt;/code&gt; on high-throughput nodes—amplify the incident and can cause complete service outages.&lt;/p&gt;
&lt;p&gt;The diagnostic objective is to collect deterministic execution metrics, calculate rate-of-change deltas across command families, and use an LLM to formulate and rank testable hypotheses regarding algorithmic complexity, hot-key contention, and collection anti-patterns without exposing sensitive payload data or running unsafe operations in production.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Valkey’s speed relies on sub-microsecond in-memory operations. When performance degrades due to workload patterns, the root cause usually falls into one of five distinct categories:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Algorithmic Complexity Traps:&lt;/strong&gt; Running $O(N)$ or $O(M \log N)$ commands where $N$ is the total keyspace or collection size (such as &lt;code&gt;KEYS&lt;/code&gt;, unbounded &lt;code&gt;HGETALL&lt;/code&gt;, &lt;code&gt;SMEMBERS&lt;/code&gt;, &lt;code&gt;SUNION&lt;/code&gt;, or &lt;code&gt;ZRANGEBYSCORE&lt;/code&gt; on large sorted sets).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Big Keys and Memory Serialization:&lt;/strong&gt; Keys holding hundreds of megabytes of nested data structures require proportional CPU time to serialize across the network socket and allocate memory, blocking the main thread during both retrieval and deletion.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hot-Key Access Skew:&lt;/strong&gt; A single popular cache key accessed by thousands of concurrent application threads saturates single-core throughput and fills network socket buffers, regardless of cluster size or shard count.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Blocking Lua Scripts and Functions:&lt;/strong&gt; Atomic execution of Lua scripts (&lt;code&gt;EVAL&lt;/code&gt;/&lt;code&gt;EVALSHA&lt;/code&gt;) or Valkey Functions (&lt;code&gt;FCALL&lt;/code&gt;) ensures consistency, but unoptimized loops or heavy string manipulations inside a script stall the entire server until the script completes or reaches &lt;code&gt;lua-time-limit&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pipelining and Buffer Bloat:&lt;/strong&gt; While pipelining amortizes network round-trip times, unconstrained client pipelines (issuing tens of thousands of commands in a single batch) monopolize the event loop and inflate client output buffers, exhausting server memory.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;How do we systematically isolate the exact command, key pattern, and data structure responsible for event-loop saturation while protecting production stability and data privacy?&lt;/p&gt;
&lt;h2 id=&quot;build-a-workload-aware-command-evidence-pack&quot;&gt;Build a Workload-Aware Command Evidence Pack&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[application p99 latency spikes and queueing alerts] --&gt; E[time-bounded Valkey command evidence pack]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B[INFO commandstats deltas and latency percentiles] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[SLOWLOG samples and latency history events] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[sanitized MEMORY USAGE samples and client tracking metrics] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[calculate command CPU share and per-call microsecond rates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[correlate slowlog timestamps with client buffers and blocked states]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[LLM observations hypotheses contradictions and gaps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[SRE and DBA verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[remediation validation and rollback]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To isolate expensive commands safely, construct an evidence pack covering baseline, onset, and degradation windows. The collection process must extract differential metrics without executing blocking keyspace scans:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Command Statistics (&lt;code&gt;INFO commandstats&lt;/code&gt;):&lt;/strong&gt; For each command family, record cumulative &lt;code&gt;calls&lt;/code&gt;, &lt;code&gt;usec&lt;/code&gt;, &lt;code&gt;usec_per_call&lt;/code&gt;, &lt;code&gt;rejected_calls&lt;/code&gt;, and &lt;code&gt;failed_calls&lt;/code&gt;. Convert these cumulative totals into interval deltas ($\Delta \text{usec} / \Delta t$) to determine the exact percentage of main-thread CPU consumed by each command.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Slow Query Log (&lt;code&gt;SLOWLOG GET 128&lt;/code&gt;):&lt;/strong&gt; Extract timestamp, execution duration in microseconds, command name, client IP/port, and key name (with payload arguments redacted to protect privacy).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Latency Diagnostics (&lt;code&gt;LATENCY LATEST&lt;/code&gt;, &lt;code&gt;LATENCY HISTORY command&lt;/code&gt;):&lt;/strong&gt; Track historical execution latency spikes across engine events.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Client and Buffer State (&lt;code&gt;INFO clients&lt;/code&gt;, &lt;code&gt;CLIENT LIST&lt;/code&gt;):&lt;/strong&gt; Capture &lt;code&gt;connected_clients&lt;/code&gt;, &lt;code&gt;blocked_clients&lt;/code&gt;, &lt;code&gt;client_recent_max_output_buffer&lt;/code&gt;, &lt;code&gt;client_recent_max_input_buffer&lt;/code&gt;, and identify clients with large output buffers. Pin the collector to these field names for the Valkey versions you support; the older &lt;code&gt;client_longest_output_list&lt;/code&gt; and &lt;code&gt;client_biggest_input_buf&lt;/code&gt; names belong to earlier Redis-era &lt;code&gt;INFO&lt;/code&gt; output and will not be present.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data Structure Telemetry:&lt;/strong&gt; Sampled memory usage via &lt;code&gt;MEMORY USAGE &amp;#x3C;key&gt; SAMPLES 5&lt;/code&gt; for suspect keys, and &lt;code&gt;OBJECT ENCODING &amp;#x3C;key&gt;&lt;/code&gt; to determine if collections have expanded beyond optimized compact encodings (e.g., listpack to hashtable/skiplist).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The LLM analyzes this structured payload to generate four distinct outputs:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Observations:&lt;/strong&gt; Quantitative facts tied to specific metric deltas (e.g., “&lt;code&gt;HGETALL&lt;/code&gt; accounted for 74.2% of total main-thread CPU time over a 60-second window, averaging 4,200 µs per invocation across 18,000 calls”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hypotheses:&lt;/strong&gt; Ranked architectural explanations with supporting and contradicting metrics (e.g., “Hypothesis 1: Large hash traversal on user session objects; Hypothesis 2: Client output buffer starvation caused by unbounded batch sizes”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Missing Evidence:&lt;/strong&gt; Additional safe metrics required to confirm the hypothesis (e.g., “Need &lt;code&gt;OBJECT ENCODING&lt;/code&gt; and &lt;code&gt;HLEN&lt;/code&gt; for top slowlog hash keys to confirm if hash exceeds listpack boundaries”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Actions:&lt;/strong&gt; Non-blocking validation commands and application-level query refactoring steps.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Plausible failure classes&lt;/th&gt;&lt;th&gt;Evidence that separates them&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Main-thread CPU pinned at 100%&lt;/td&gt;&lt;td&gt;$O(N)$ keyspace command, complex Lua script, large collection traversal&lt;/td&gt;&lt;td&gt;&lt;code&gt;INFO commandstats&lt;/code&gt; $\Delta \text{usec}$, &lt;code&gt;SLOWLOG&lt;/code&gt; duration, &lt;code&gt;used_cpu_user_main_thread&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sudden p99 latency spike to &gt;10ms&lt;/td&gt;&lt;td&gt;Big key serialization, blocking function, active defragmentation surge&lt;/td&gt;&lt;td&gt;&lt;code&gt;SLOWLOG&lt;/code&gt; entries, &lt;code&gt;LATENCY LATEST&lt;/code&gt;, &lt;code&gt;latest_fork_usec&lt;/code&gt;, &lt;code&gt;MEMORY USAGE&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Client timeout during write burst&lt;/td&gt;&lt;td&gt;Blocking Lua script, synchronous eviction of big keys, unbounded pipeline&lt;/td&gt;&lt;td&gt;&lt;code&gt;blocked_clients&lt;/code&gt;, &lt;code&gt;lazyfree-lazy-eviction&lt;/code&gt;, &lt;code&gt;client_recent_max_output_buffer&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High network egress with low ops/sec&lt;/td&gt;&lt;td&gt;Unbounded read commands (&lt;code&gt;HGETALL&lt;/code&gt;, &lt;code&gt;SMEMBERS&lt;/code&gt;, &lt;code&gt;LRANGE 0 -1&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;&lt;code&gt;total_net_output_bytes&lt;/code&gt; delta, &lt;code&gt;INFO commandstats&lt;/code&gt; &lt;code&gt;cmdstat_hgetall&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;blocked_clients&lt;/code&gt; metric climbing&lt;/td&gt;&lt;td&gt;Blocking list/stream reads (&lt;code&gt;BLPOP&lt;/code&gt;, &lt;code&gt;BRPOP&lt;/code&gt;, &lt;code&gt;XREAD BLOCK&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;&lt;code&gt;INFO clients&lt;/code&gt; &lt;code&gt;blocked_clients&lt;/code&gt;, application consumer connection counts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Memory ballooning without key count increase&lt;/td&gt;&lt;td&gt;Nested data structures expanding from listpack to hashtable/skiplist&lt;/td&gt;&lt;td&gt;&lt;code&gt;tracking_total_keys&lt;/code&gt; stable while &lt;code&gt;used_memory_dataset&lt;/code&gt; climbs, &lt;code&gt;OBJECT ENCODING&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Valkey command latency or CPU alert] --&gt; B[1. Compute commandstats deltas and CPU time distribution]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[2. Inspect SLOWLOG for long-running command signatures]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[3. Audit big keys and structural encodings via non-blocking sampling]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[4. Identify hot keys and client access skew via LFU metrics]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[5. Evaluate Lua script execution times and client output buffers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Synthesize observations hypotheses and remediation steps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-compute-commandstats-deltas-and-cpu-time-distribution&quot;&gt;1. Compute commandstats deltas and CPU time distribution&lt;/h3&gt;
&lt;p&gt;Never evaluate raw cumulative numbers from &lt;code&gt;INFO commandstats&lt;/code&gt;. Calculate the rate of change over a discrete sample interval ($T_1$ to $T_2$):&lt;/p&gt;
&lt;p&gt;$$\Delta \text{CPU_Share}(cmd) = \frac{\Delta \text{usec}(cmd)}{\Delta t_{\text{elapsed_microseconds}}} \times 100%$$&lt;/p&gt;
&lt;p&gt;If &lt;code&gt;cmdstat_keys&lt;/code&gt;, &lt;code&gt;cmdstat_hgetall&lt;/code&gt;, &lt;code&gt;cmdstat_smembers&lt;/code&gt;, or &lt;code&gt;cmdstat_eval&lt;/code&gt; accounts for more than 20% of total elapsed time, the workload is suffering from algorithmic inefficiency. Compare &lt;code&gt;usec_per_call&lt;/code&gt;: sub-microsecond commands (&lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;SET&lt;/code&gt;) typically average 0.5–2.0 µs, whereas problematic collection queries average 500–10,000 µs per call.&lt;/p&gt;
&lt;h3 id=&quot;2-inspect-slowlog-for-long-running-command-signatures&quot;&gt;2. Inspect SLOWLOG for long-running command signatures&lt;/h3&gt;
&lt;p&gt;Execute &lt;code&gt;SLOWLOG GET 32&lt;/code&gt; to capture the most recent commands that exceeded &lt;code&gt;slowlog-log-slower-than&lt;/code&gt; (default 10,000 µs; recommended 1,000 µs for low-latency triage).&lt;/p&gt;
&lt;p&gt;Analyze the slowlog payload:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Command Name and Duration:&lt;/strong&gt; Identify whether latency is driven by retrieval (&lt;code&gt;HGETALL&lt;/code&gt;), modification (&lt;code&gt;SADD&lt;/code&gt;), or deletion (&lt;code&gt;DEL&lt;/code&gt; on large collections).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Execution Timestamps:&lt;/strong&gt; Correlate slowlog timestamps with application latency alarms and CPU spikes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Client IP and Port:&lt;/strong&gt; Determine if slow commands originate from a specific batch service, background worker, or misconfigured microservice.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-audit-big-keys-and-structural-encodings-via-non-blocking-sampling&quot;&gt;3. Audit big keys and structural encodings via non-blocking sampling&lt;/h3&gt;
&lt;p&gt;Do not execute &lt;code&gt;valkey-cli --bigkeys&lt;/code&gt; on a heavily loaded production node without rate limiting, as it runs continuous &lt;code&gt;SCAN&lt;/code&gt; iterations that increase CPU overhead. Instead, perform targeted sampling on suspected keys identified in the slowlog:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Memory Footprint:&lt;/strong&gt; Execute &lt;code&gt;MEMORY USAGE &amp;#x3C;key&gt; SAMPLES 5&lt;/code&gt; to estimate total memory consumption including allocator overhead.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Internal Encoding:&lt;/strong&gt; Run &lt;code&gt;OBJECT ENCODING &amp;#x3C;key&gt;&lt;/code&gt;. Valkey stores small hashes, sets, and zsets using memory-efficient, compact &lt;code&gt;listpack&lt;/code&gt; structures. When element counts or value lengths exceed configured thresholds (&lt;code&gt;hash-max-listpack-entries&lt;/code&gt;, &lt;code&gt;hash-max-listpack-value&lt;/code&gt;), the engine converts the structure to a standard &lt;code&gt;hashtable&lt;/code&gt; or &lt;code&gt;skiplist&lt;/code&gt;, increasing memory overhead by 3x–5x and changing traversal CPU characteristics.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;4-identify-hot-keys-and-client-access-skew-via-lfu-metrics&quot;&gt;4. Identify hot keys and client access skew via LFU metrics&lt;/h3&gt;
&lt;p&gt;When a single key experiences massive concurrency, it saturates the execution thread. If Valkey is configured with an LFU eviction policy (such as &lt;code&gt;allkeys-lfu&lt;/code&gt; or &lt;code&gt;volatile-lfu&lt;/code&gt;), retrieve the access frequency counter using &lt;code&gt;OBJECT FREQ &amp;#x3C;key&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To scan for hot keys safely:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;valkey-cli --hotkeys -i 0.1&lt;/code&gt; during non-peak windows with an explicit sleep interval (&lt;code&gt;-i 0.1&lt;/code&gt; introduces a 100 ms pause every 100 &lt;code&gt;SCAN&lt;/code&gt; iterations) to avoid CPU contention.&lt;/li&gt;
&lt;li&gt;Monitor &lt;code&gt;CLIENT LIST&lt;/code&gt; to detect whether hundreds of connections are querying the exact same database index or executing identical query strings.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;5-evaluate-lua-script-execution-times-and-client-output-buffers&quot;&gt;5. Evaluate Lua script execution times and client output buffers&lt;/h3&gt;
&lt;p&gt;Lua scripts and Valkey Functions execute with strict ACID atomicity, locking the main thread for their entire duration:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Lua Execution Stalls:&lt;/strong&gt; Check &lt;code&gt;cmdstat_eval&lt;/code&gt; and &lt;code&gt;cmdstat_fcall&lt;/code&gt; in &lt;code&gt;INFO commandstats&lt;/code&gt;. If script duration exceeds &lt;code&gt;lua-time-limit&lt;/code&gt; (default 5,000 ms), the engine logs warnings and allows read-only &lt;code&gt;SCRIPT KILL&lt;/code&gt; operations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Client Output Buffers:&lt;/strong&gt; Inspect &lt;code&gt;INFO clients&lt;/code&gt; for &lt;code&gt;client_recent_max_output_buffer&lt;/code&gt;. Large pipelines (e.g., 50,000 &lt;code&gt;GET&lt;/code&gt; commands in a single pipeline) or wide queries (&lt;code&gt;LRANGE 0 -1&lt;/code&gt;) force Valkey to buffer megabytes of reply data in memory per client. If client consumption stalls, the engine spends excessive time managing buffer memory allocations. Because this field is a recent maximum across clients rather than a per-connection gauge, follow it with &lt;code&gt;CLIENT LIST&lt;/code&gt; and sort by &lt;code&gt;omem&lt;/code&gt; to find the offending connection.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Valkey tail latency spike or event loop stall] --&gt; B{Commandstats shows high usec per call}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes| C{Command type identified in SLOWLOG}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|KEYS or Full Keyspace Scan| D[Replace with SCAN and cursor pagination]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|HGETALL SMEMBERS LRANGE| E[Check collection size and switch to HSCAN or HMGET]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|EVAL FCALL Lua Script| F[Decompose script and remove blocking loops]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|DEL on Large Collection| G[Enable lazyfree or use UNLINK instead of DEL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No| H{High ops per sec on single key}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|Yes| I[Implement client-side caching or key sharding]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|No| J{Blocked clients climbing}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|Yes| K[Inspect BLPOP BRPOP XREAD queue consumer saturation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|No| L[Check client pipeline sizes and network output buffer limits]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://valkey.io/commands/&quot;&gt;official Valkey command reference&lt;/a&gt; documents the time complexity of every engine operation. Commands like &lt;code&gt;KEYS&lt;/code&gt; are $O(N)$ where $N$ is the total number of keys in the database, making them prohibited in production. Collection operations such as &lt;code&gt;HGETALL&lt;/code&gt; and &lt;code&gt;SMEMBERS&lt;/code&gt; are $O(N)$ where $N$ is the number of elements in the specific collection; for hashes containing 100,000 fields, executing &lt;code&gt;HGETALL&lt;/code&gt; forces the engine to serialize 100,000 key-value pairs synchronously.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://valkey.io/topics/memory-optimization/&quot;&gt;Valkey memory optimization guide&lt;/a&gt; specifies that hashes, lists, and sets below configured thresholds (such as &lt;code&gt;hash-max-listpack-entries 512&lt;/code&gt; and &lt;code&gt;hash-max-listpack-value 64&lt;/code&gt;) are encoded as a single contiguous memory block (&lt;code&gt;listpack&lt;/code&gt;). This representation provides extreme memory compactness and high CPU cache locality. Once a collection exceeds these limits, it converts to a hashtable or skiplist, increasing memory pointer overhead and altering allocation behavior.&lt;/p&gt;
&lt;p&gt;According to the &lt;a href=&quot;https://valkey.io/topics/latency/&quot;&gt;Valkey latency troubleshooting guide&lt;/a&gt;, deleting a large collection containing millions of elements using standard &lt;code&gt;DEL&lt;/code&gt; is an $O(N)$ operation that deallocates memory synchronously on the main thread. The documented best practice is using &lt;code&gt;UNLINK&lt;/code&gt; (non-blocking delete) or setting &lt;code&gt;lazyfree-lazy-user-del yes&lt;/code&gt; to offload memory reclamation to background &lt;code&gt;bio&lt;/code&gt; threads.&lt;/p&gt;
&lt;p&gt;Documented client architecture patterns establish that while pipelining eliminates network round-trip delays, issuing unbounded pipelines (e.g., &gt;10,000 operations per pipeline) forces the server to buffer all response objects simultaneously. This amplifies memory consumption in &lt;code&gt;client-output-buffer-limit&lt;/code&gt; and creates head-of-line blocking for other connected clients.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven root cause&lt;/th&gt;&lt;th&gt;Candidate remediation&lt;/th&gt;&lt;th&gt;Validation criteria&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Unbounded hash read (&lt;code&gt;HGETALL&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;Refactor application to use &lt;code&gt;HMGET&lt;/code&gt; for required fields or &lt;code&gt;HSCAN&lt;/code&gt; for pagination&lt;/td&gt;&lt;td&gt;&lt;code&gt;cmdstat_hgetall&lt;/code&gt; rate drops to 0; p99 latency normalizes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Synchronous collection deletion (&lt;code&gt;DEL&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;Replace application &lt;code&gt;DEL&lt;/code&gt; calls with &lt;code&gt;UNLINK&lt;/code&gt;; set &lt;code&gt;lazyfree-lazy-user-del yes&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;SLOWLOG&lt;/code&gt; entries for deletions disappear; latency spikes eliminate&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Keyspace scanning (&lt;code&gt;KEYS *&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;Replace with cursor-based &lt;code&gt;SCAN&lt;/code&gt; with bounded &lt;code&gt;COUNT&lt;/code&gt; parameters (e.g., &lt;code&gt;COUNT 100&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;&lt;code&gt;cmdstat_keys&lt;/code&gt; drops to 0; event-loop stalls resolve&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hot-key access saturation&lt;/td&gt;&lt;td&gt;Enable Client-Side Caching (&lt;code&gt;CLIENT TRACKING&lt;/code&gt;) or shard keys (&lt;code&gt;key_{1..N}&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;CPU distribution balances across cores/shards; &lt;code&gt;OBJECT FREQ&lt;/code&gt; skews flatten&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Long-running Lua script&lt;/td&gt;&lt;td&gt;Refactor complex business logic to client application or split script into smaller batches&lt;/td&gt;&lt;td&gt;&lt;code&gt;cmdstat_eval&lt;/code&gt; average &lt;code&gt;usec_per_call&lt;/code&gt; drops below 500 µs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unbounded client pipelining&lt;/td&gt;&lt;td&gt;Enforce maximum client pipeline batch size (e.g., 500 commands per batch)&lt;/td&gt;&lt;td&gt;&lt;code&gt;client_recent_max_output_buffer&lt;/code&gt; stabilizes; client buffer memory drops&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Blocking list queue thundering herd&lt;/td&gt;&lt;td&gt;Stagger worker retry intervals and add exponential backoff on empty &lt;code&gt;BLPOP&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;blocked_clients&lt;/code&gt; stabilizes; worker connection churn drops&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;All production workload adjustments and configuration changes must maintain a clear, low-risk rollback strategy:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Configuration Parameter Rollback:&lt;/strong&gt; Any runtime setting modified via &lt;code&gt;CONFIG SET&lt;/code&gt; (such as &lt;code&gt;lazyfree-lazy-user-del&lt;/code&gt;, &lt;code&gt;hash-max-listpack-entries&lt;/code&gt;, or &lt;code&gt;slowlog-log-slower-than&lt;/code&gt;) can be immediately restored to its baseline value using a reciprocal &lt;code&gt;CONFIG SET&lt;/code&gt;. Always record initial parameters using &lt;code&gt;CONFIG GET *&lt;/code&gt; before modifications.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Client Application Feature Flags:&lt;/strong&gt; When rolling out query refactorings (e.g., migrating from &lt;code&gt;HGETALL&lt;/code&gt; to &lt;code&gt;HMGET&lt;/code&gt;/&lt;code&gt;HSCAN&lt;/code&gt;, or implementing client-side caching), gate changes behind dynamic configuration flags. If application-side batching introduces unforeseen race conditions, toggle the feature flag to revert to baseline behavior instantly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Key Sharding / Salting Reversal:&lt;/strong&gt; If partitioning hot keys across multiple sub-keys (&lt;code&gt;key_0&lt;/code&gt;, &lt;code&gt;key_1&lt;/code&gt;), ensure the client read layer supports dual-reading from both the unpartitioned and partitioned key paths until data migration completes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Client-Side Caching Evacuation:&lt;/strong&gt; If enabling &lt;code&gt;CLIENT TRACKING&lt;/code&gt; causes invalidation message storming over application connections, disable tracking globally at the client library level without modifying database server configurations.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why naive reasoning fails&lt;/th&gt;&lt;th&gt;Better diagnostic boundary&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;”Slow database means slow server”&lt;/td&gt;&lt;td&gt;A single client issuing &lt;code&gt;KEYS *&lt;/code&gt; stalls an otherwise idle, multi-gigabyte instance&lt;/td&gt;&lt;td&gt;Audit &lt;code&gt;INFO commandstats&lt;/code&gt; and &lt;code&gt;SLOWLOG&lt;/code&gt; to isolate specific commands&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Low CPU means all queries are fast”&lt;/td&gt;&lt;td&gt;Bursts of $O(N)$ commands lasting 5ms may barely move 1-minute CPU averages&lt;/td&gt;&lt;td&gt;Inspect p99/p999 latency percentiles and &lt;code&gt;LATENCY LATEST&lt;/code&gt; history&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Big keys must be deleted immediately”&lt;/td&gt;&lt;td&gt;Executing synchronous &lt;code&gt;DEL&lt;/code&gt; on a 500MB key will freeze the server for seconds&lt;/td&gt;&lt;td&gt;Use &lt;code&gt;UNLINK&lt;/code&gt; or asynchronous background deallocation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Pipelining is always better”&lt;/td&gt;&lt;td&gt;Massive pipelines create head-of-line blocking and trigger memory buffer limits&lt;/td&gt;&lt;td&gt;Benchmark pipeline batch sizes and monitor client output buffers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Lua scripts run faster than commands”&lt;/td&gt;&lt;td&gt;Lua executes synchronously; complex loops inside scripts block all clients&lt;/td&gt;&lt;td&gt;Profile Lua execution duration via &lt;code&gt;cmdstat_eval&lt;/code&gt; and slowlog&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;”Hot keys can be fixed by vertical scaling”&lt;/td&gt;&lt;td&gt;Valkey command execution is single-threaded per shard; more CPU cores will not help&lt;/td&gt;&lt;td&gt;Implement client-side caching or key partitioning&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;To guarantee production security, data privacy, and operational stability, strict guardrails define LLM operations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Payload Inspection:&lt;/strong&gt; The LLM must never receive unredacted key names, user IDs, authentication tokens, or payload data from &lt;code&gt;SLOWLOG&lt;/code&gt; or &lt;code&gt;DUMP&lt;/code&gt; outputs. All diagnostic evidence packs must mask payload parameters before analysis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Direct Query Execution:&lt;/strong&gt; The LLM does not connect directly to the database to execute diagnostic queries, run &lt;code&gt;SCAN&lt;/code&gt; loops, or modify configurations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Key Deletion:&lt;/strong&gt; The LLM cannot execute &lt;code&gt;DEL&lt;/code&gt;, &lt;code&gt;UNLINK&lt;/code&gt;, or &lt;code&gt;FLUSHDB&lt;/code&gt;. Remediation actions must be authorized and applied by human engineers through validated deployment pipelines.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Guesses on Incomplete Commandstats:&lt;/strong&gt; If &lt;code&gt;INFO commandstats&lt;/code&gt; lacks baseline rate-of-change deltas, the LLM must explicitly flag missing evidence rather than guessing workload distribution from cumulative lifetime counters.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Expensive commands, big collections, and hot keys stall Valkey’s single-threaded event loop, introducing severe tail latency across upstream services.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Construct a workload-aware command evidence pack capturing differential &lt;code&gt;INFO commandstats&lt;/code&gt;, sampled &lt;code&gt;SLOWLOG&lt;/code&gt; entries, and structural encoding diagnostics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Correlate specific command families with main-thread CPU time share, verify collection encodings via &lt;code&gt;OBJECT ENCODING&lt;/code&gt;, and validate execution durations against slowlog timestamps.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Instrument differential commandstat collection across your Valkey nodes. If host-level triage and command profiling confirm engine health, proceed to Part 3 to diagnose multi-node clustering, shard balancing, and ElastiCache managed failover behavior.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://valkey.io/commands/&quot;&gt;Valkey Command Documentation and Time Complexities&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://valkey.io/topics/memory-optimization/&quot;&gt;Valkey Memory Optimization and Data Structure Encodings&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://valkey.io/topics/latency/&quot;&gt;Valkey Latency Troubleshooting Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://valkey.io/topics/programmability/&quot;&gt;Valkey Programmability — Lua Scripts and Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://valkey.io/topics/pipelining/&quot;&gt;Valkey Pipelining and Network Transport Architecture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://valkey.io/topics/client-side-caching/&quot;&gt;Valkey Client-Side Caching and Key Invalidation Architecture&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>ai-engineering</category><category>failures</category></item><item><title>Database Licensing Cost Across AWS, Azure, GCP, and OCI</title><link>https://rajivonai.com/blog/2026-02-11-database-licensing-cost-across-clouds/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-02-11-database-licensing-cost-across-clouds/</guid><description>A framework for managing commercial database licensing costs across the four major cloud providers.</description><pubDate>Wed, 11 Feb 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The cloud was supposed to eliminate licensing complexity, but for commercial databases, it simply embedded the cost into an hourly rate you can’t negotiate.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most engineering teams have no systematic framework for managing database licensing costs across AWS, Azure, GCP, and Oracle Cloud. They over-provision compute and default to “License-Included” pricing, inadvertently paying retail rates for licenses they may already own.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Commercial database engines like Oracle and SQL Server drive the majority of cloud database costs for enterprise customers. Without a structured approach to right-sizing, license reuse, and migration, platform teams lock in massive OPEX waste. How do you untangle compute cost from licensing cost across multi-cloud environments?&lt;/p&gt;
&lt;h2 id=&quot;the-prism-framework&quot;&gt;The PRISM Framework&lt;/h2&gt;
&lt;p&gt;The PRISM framework provides five phases to control cloud database spend:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Profile&lt;/strong&gt;: Inventory every database service, engine, and tier.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Right-size&lt;/strong&gt;: Match instance size to actual P95 workload metrics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Incentivize&lt;/strong&gt;: Apply reserved instances, BYOL, and Azure Hybrid Benefit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Switch&lt;/strong&gt;: Migrate from commercial engines to OSS-compatible managed services.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Monitor&lt;/strong&gt;: Tag enforcement and cost anomaly alerts.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern across enterprise environments shows that right-sizing before reservations avoids locking in waste. For example, AWS RDS offers Reserved Instances, but migrating Oracle SE2 to Aurora PostgreSQL eliminates the licensing burden entirely. On Azure, applying &lt;a href=&quot;https://rajivonai.com/tools/sql-server-license-calculator/&quot;&gt;Azure Hybrid Benefit&lt;/a&gt; to existing SQL Server SA-covered licenses can materially reduce licensing cost — Microsoft cites savings of up to roughly 55% for some configurations, though the realized figure varies by edition, region, and existing SA coverage. Model your own case rather than assuming a fixed percentage.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Strategy&lt;/th&gt;&lt;th&gt;Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Bring Your Own License (BYOL)&lt;/td&gt;&lt;td&gt;Requires strict compliance tracking and often restricts you to specific infrastructure types (like EC2 Dedicated Hosts on AWS).&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Migration to OSS&lt;/td&gt;&lt;td&gt;Schema conversion is rarely 100% automated; rewriting stored procedures requires significant engineering effort.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reserved Instances&lt;/td&gt;&lt;td&gt;Commits you to a specific instance family for 1-3 years, reducing flexibility if the workload shrinks.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: License-Included pricing obscures true database costs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Apply the PRISM framework starting with a comprehensive profile of all database assets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Structured license reuse (BYOL, AHB) can deliver meaningful savings on commercial engines — figures in the 30–50% range are commonly cited, but actual results depend on your licensing position and workload, so model your own case before assuming a number.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Try our &lt;a href=&quot;https://rajivonai.com/tools/sql-server-license-calculator/&quot;&gt;SQL Server Cloud Licensing Calculator&lt;/a&gt; to model your potential BYOL/AHB savings. If you need a comprehensive review, request a Cloud Database Cost Review.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category></item><item><title>LLM-Assisted Valkey Performance Triage on EC2: Memory, CPU, Latency and Evictions</title><link>https://rajivonai.com/blog/2026-02-10-llm-assisted-valkey-performance-triage-ec2-memory-cpu-latency-evictions/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-02-10-llm-assisted-valkey-performance-triage-ec2-memory-cpu-latency-evictions/</guid><description>A production triage workflow for diagnosing Valkey on EC2: separating engine event loop delays from host memory fragmentation, CPU credit exhaustion, eviction storms, and network PPS limits.</description><pubDate>Tue, 10 Feb 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;High memory occupancy in Valkey is by design, elevated CPU can reflect healthy I/O threading rather than a runaway command, and tail latency spikes frequently originate outside the core engine loop. Triage begins by isolating kernel virtual memory, EC2 hypervisor constraints, and allocator fragmentation from single-threaded engine stalls.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Deploying self-managed Valkey (versions 7.2 and 8.0) on Amazon EC2 powers latency-critical caching, session stores, rate limiters, and real-time state machines. Operating at sub-millisecond p99 latency targets leaves zero margin for diagnostic ambiguity. When an upstream API alerts on cache timeouts, the environment surfaces dozens of concurrent signals: elevated instance CPU, climbing RSS, sudden key evictions, replication buffer growth, and microbursts on the Elastic Network Adapter (ENA).&lt;/p&gt;
&lt;p&gt;Valkey exposes granular internal telemetry through &lt;code&gt;INFO&lt;/code&gt; commands (such as &lt;code&gt;memory&lt;/code&gt;, &lt;code&gt;cpu&lt;/code&gt;, &lt;code&gt;stats&lt;/code&gt;, &lt;code&gt;clients&lt;/code&gt;, &lt;code&gt;replication&lt;/code&gt;, and &lt;code&gt;commandstats&lt;/code&gt;), &lt;code&gt;SLOWLOG&lt;/code&gt;, and &lt;code&gt;LATENCY DOCTOR&lt;/code&gt;. Linux provides &lt;code&gt;/proc/vmstat&lt;/code&gt;, &lt;code&gt;sysfs&lt;/code&gt;, and &lt;code&gt;perf&lt;/code&gt;, while EC2 surfaces CloudWatch metrics, ENA driver counters, and EBS volume telemetry.&lt;/p&gt;
&lt;p&gt;The operational bottleneck is deterministic correlation. An LLM cannot poll production or execute unchecked configuration commands. Its role is strictly to correlate synchronized, sanitized evidence packs, eliminate contradictory hypotheses, surface missing diagnostic dimensions, and produce ranked, auditable remediation options for human authorization.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Valkey’s in-memory, single-threaded execution core (augmented by multi-threaded network I/O) creates distinctive failure modes that mislead generic database triage:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Memory Occupancy vs. Memory Pressure:&lt;/strong&gt; &lt;code&gt;used_memory&lt;/code&gt; reaching &lt;code&gt;maxmemory&lt;/code&gt; is expected when using an eviction policy like &lt;code&gt;allkeys-lru&lt;/code&gt;. True pressure arises from allocator fragmentation (&lt;code&gt;mem_fragmentation_ratio&lt;/code&gt;), jemalloc metadata bloat, unevictable keys under &lt;code&gt;volatile-*&lt;/code&gt; policies, or Linux swap invocation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Main Thread vs. I/O Thread Saturation:&lt;/strong&gt; High instance vCPU utilization can reflect efficient network offloading across I/O threads or execution starvation on the single main event loop caused by $O(N)$ commands, synchronous large-key evictions, or blocking functions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Burstable and Hypervisor Throttling:&lt;/strong&gt; On burstable EC2 instances (such as T4g or T3), CPU credit depletion surfaces as host CPU steal and sudden event-loop freezes, mimicking an internal database lock.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Copy-on-Write Memory Bloat:&lt;/strong&gt; Background persistence operations (&lt;code&gt;BGSAVE&lt;/code&gt; or &lt;code&gt;BGREWRITEAOF&lt;/code&gt;) fork child processes. If Linux Transparent Huge Pages (THP) are enabled, kernel 2 MB page-fault allocations during write traffic rapidly duplicate memory, exhausting RAM and triggering out-of-memory (OOM) termination.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Network Packet and Buffer Saturation:&lt;/strong&gt; Client timeouts often stem from exhausting EC2 ENA packet-per-second (PPS) allowances, TCP backlog drops (&lt;code&gt;somaxconn&lt;/code&gt;), or replication client output buffer overflows rather than internal command latency.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;How do we classify a Valkey incident into host virtual memory, kernel configuration, EC2 instance constraints, network buffer exhaustion, or engine event-loop stalls before running invasive diagnostics?&lt;/p&gt;
&lt;h2 id=&quot;build-an-engine-aware-valkey-evidence-pack&quot;&gt;Build an Engine-Aware Valkey Evidence Pack&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[application p95 p99 latency and timeouts] --&gt; E[time-bounded Valkey incident evidence pack]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B[host CPU memory swap and ENA counters] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[Valkey INFO commandstats memory and clients] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[Linux kernel sysctl THP and EBS storage metrics] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[validate timestamps uptime role and counter resets]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[derive interval rates deltas and ratios]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[LLM observations hypotheses contradictions and gaps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[SRE and DBA verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[remediation validation and rollback]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To diagnose Valkey accurately, build a time-bounded incident evidence pack capturing baseline, onset, peak, and recovery windows in synchronized UTC. Cumulative counters from &lt;code&gt;INFO stats&lt;/code&gt;, &lt;code&gt;INFO commandstats&lt;/code&gt;, and network interfaces must be converted to interval rates bounded by process uptime.&lt;/p&gt;
&lt;p&gt;The telemetry payload captures:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Engine State (&lt;code&gt;INFO&lt;/code&gt;):&lt;/strong&gt; Process &lt;code&gt;uptime_in_seconds&lt;/code&gt;, &lt;code&gt;instantaneous_ops_per_sec&lt;/code&gt;, &lt;code&gt;connected_clients&lt;/code&gt;, &lt;code&gt;blocked_clients&lt;/code&gt;, &lt;code&gt;rejected_connections&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Memory Subsystem (&lt;code&gt;INFO memory&lt;/code&gt;):&lt;/strong&gt; &lt;code&gt;used_memory&lt;/code&gt;, &lt;code&gt;used_memory_rss&lt;/code&gt;, &lt;code&gt;maxmemory&lt;/code&gt;, &lt;code&gt;maxmemory_policy&lt;/code&gt;, &lt;code&gt;mem_fragmentation_ratio&lt;/code&gt;, &lt;code&gt;mem_allocator&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Eviction and Expiration Counters (&lt;code&gt;INFO stats&lt;/code&gt;):&lt;/strong&gt; &lt;code&gt;evicted_keys&lt;/code&gt;, &lt;code&gt;expired_keys&lt;/code&gt;. These live in the &lt;code&gt;stats&lt;/code&gt; section, not &lt;code&gt;memory&lt;/code&gt; — a collector that scrapes only &lt;code&gt;INFO memory&lt;/code&gt; will silently return nothing for them. They are cumulative since start, so record deltas over the collection interval rather than absolute values.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compute and Execution (&lt;code&gt;INFO cpu&lt;/code&gt;):&lt;/strong&gt; &lt;code&gt;used_cpu_sys&lt;/code&gt;, &lt;code&gt;used_cpu_user&lt;/code&gt;, &lt;code&gt;used_cpu_sys_main_thread&lt;/code&gt;, &lt;code&gt;used_cpu_user_main_thread&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Persistence and Replication (&lt;code&gt;INFO persistence&lt;/code&gt;, &lt;code&gt;INFO replication&lt;/code&gt;):&lt;/strong&gt; &lt;code&gt;rdb_bgsave_in_progress&lt;/code&gt;, &lt;code&gt;aof_rewrite_in_progress&lt;/code&gt;, &lt;code&gt;role&lt;/code&gt;, &lt;code&gt;master_repl_offset&lt;/code&gt;, &lt;code&gt;repl_backlog_size&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Host and Kernel State:&lt;/strong&gt; Instance type, total RAM, swap usage, &lt;code&gt;vm.overcommit_memory&lt;/code&gt;, &lt;code&gt;vm.swappiness&lt;/code&gt;, THP state (&lt;code&gt;/sys/kernel/mm/transparent_hugepage/enabled&lt;/code&gt;), CPU steal percentage, and ENA driver counters (&lt;code&gt;ethtool -S &amp;#x3C;interface&gt;&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The LLM processes this sanitized bundle to generate four distinct, auditable artifacts:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Observations:&lt;/strong&gt; Direct facts tied to specific metric deltas (e.g., “&lt;code&gt;evicted_keys&lt;/code&gt; rate surged to 35,000/s while &lt;code&gt;used_memory&lt;/code&gt; remained at 99.8% of &lt;code&gt;maxmemory&lt;/code&gt;”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hypotheses:&lt;/strong&gt; Ranked potential root causes with explicit supporting and contradicting metrics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Missing Evidence:&lt;/strong&gt; Unobserved metrics required to confirm the primary hypothesis (e.g., “Need &lt;code&gt;SLOWLOG GET&lt;/code&gt; to verify whether evictions are stalling the main thread synchronously”).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Actions:&lt;/strong&gt; Safe, read-only verification commands and reversible configuration adjustments.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Plausible failure classes&lt;/th&gt;&lt;th&gt;Evidence that separates them&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;p99 latency spikes&lt;/td&gt;&lt;td&gt;O(N) command, synchronous eviction, THP memory allocation, EBS fsync stall&lt;/td&gt;&lt;td&gt;&lt;code&gt;used_cpu_user_main_thread&lt;/code&gt;, &lt;code&gt;evicted_keys&lt;/code&gt; delta, &lt;code&gt;latest_fork_usec&lt;/code&gt;, &lt;code&gt;aof_delayed_fsync&lt;/code&gt;, THP state&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Rapid memory climb to OOM&lt;/td&gt;&lt;td&gt;Memory fragmentation, unevictable keys, replication buffer overflow, fork COW&lt;/td&gt;&lt;td&gt;&lt;code&gt;mem_fragmentation_ratio&lt;/code&gt;, &lt;code&gt;used_memory&lt;/code&gt; vs &lt;code&gt;used_memory_rss&lt;/code&gt;, &lt;code&gt;client-output-buffer-limit&lt;/code&gt;, Linux swap&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sudden eviction spike&lt;/td&gt;&lt;td&gt;Working set exceeds &lt;code&gt;maxmemory&lt;/code&gt;, TTL expiration lag, data structure expansion&lt;/td&gt;&lt;td&gt;&lt;code&gt;evicted_keys&lt;/code&gt; delta, &lt;code&gt;expired_keys&lt;/code&gt; delta, &lt;code&gt;maxmemory_policy&lt;/code&gt;, write ingestion rate&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Client connection timeouts&lt;/td&gt;&lt;td&gt;Exhausted &lt;code&gt;maxclients&lt;/code&gt;, TCP backlog drops, ENA PPS allowance exhaustion&lt;/td&gt;&lt;td&gt;&lt;code&gt;rejected_connections&lt;/code&gt;, &lt;code&gt;somaxconn&lt;/code&gt;, &lt;code&gt;netstat&lt;/code&gt; listen drops, &lt;code&gt;ethtool&lt;/code&gt; allowance counters&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High CPU with low ops/sec&lt;/td&gt;&lt;td&gt;Spin-wait contention, regular expression matching, heavy Lua script execution&lt;/td&gt;&lt;td&gt;&lt;code&gt;instantaneous_ops_per_sec&lt;/code&gt;, &lt;code&gt;used_cpu_sys_main_thread&lt;/code&gt;, &lt;code&gt;SLOWLOG&lt;/code&gt;, &lt;code&gt;INFO commandstats&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replication lag / disconnect&lt;/td&gt;&lt;td&gt;Backlog buffer wrap, network bandwidth throttling, large snapshot transmission&lt;/td&gt;&lt;td&gt;&lt;code&gt;master_repl_offset&lt;/code&gt; vs &lt;code&gt;slave_repl_offset&lt;/code&gt;, &lt;code&gt;repl_backlog_size&lt;/code&gt;, replication client buffer drops&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Valkey latency or saturation alert] --&gt; B[1. Check process uptime counter resets and host CPU steal]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[2. Inspect memory breakdown RSS fragmentation and eviction rates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[3. Compare main thread vs system CPU and verify THP status]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[4. Check connection backlog ENA allowances and client buffers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[5. Evaluate persistence fork duration and replication offsets]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Generate ranked hypotheses with missing evidence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-validate-process-uptime-counter-continuity-and-host-cpu-steal&quot;&gt;1. Validate process uptime, counter continuity, and host CPU steal&lt;/h3&gt;
&lt;p&gt;Check &lt;code&gt;uptime_in_seconds&lt;/code&gt; to confirm the process has not crashed or restarted under Linux OOM-killer supervision (&lt;code&gt;dmesg -T | grep -E -i &quot;valkey|oom-killer&quot;&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;On the host, inspect CPU steal (&lt;code&gt;%steal&lt;/code&gt; via &lt;code&gt;vmstat&lt;/code&gt; or &lt;code&gt;top&lt;/code&gt;) and, on burstable instances (T3/T4g), the CloudWatch credit metrics. These are two independent signals and conflating them produces a confident but wrong diagnosis:&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Signal&lt;/th&gt;&lt;th&gt;What it actually means&lt;/th&gt;&lt;th&gt;What it does not prove&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;%steal&lt;/code&gt;&lt;/td&gt;&lt;td&gt;The hypervisor scheduled another tenant while this vCPU had runnable work&lt;/td&gt;&lt;td&gt;Credit exhaustion — it is a virtualization scheduling signal, present on non-burstable types too&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;CPUCreditBalance&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Accrued earned credits remaining&lt;/td&gt;&lt;td&gt;Throttling by itself; a low balance matters only in Standard mode&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;CPUSurplusCreditBalance&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Surplus credits consumed &lt;strong&gt;in Unlimited mode&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Throttling — in Unlimited mode the instance keeps bursting and accrues charges instead of being capped&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Read the credit mode first, because it determines what a depleted balance does. In &lt;strong&gt;Standard&lt;/strong&gt; mode, exhausted credits restrict the instance toward its baseline performance — that is genuine throttling. In &lt;strong&gt;Unlimited&lt;/strong&gt; mode, the instance continues to burst and bills for surplus credits, so a rising &lt;code&gt;CPUSurplusCreditBalance&lt;/code&gt; is a &lt;em&gt;cost&lt;/em&gt; signal, not evidence of a capped CPU.&lt;/p&gt;
&lt;p&gt;Do not apply a universal 5% steal threshold as proof of credit throttling. Evaluate credit mode, &lt;code&gt;CPUCreditBalance&lt;/code&gt;, surplus accrual, &lt;code&gt;CPUUtilization&lt;/code&gt; against the instance’s documented baseline, and OS scheduling metrics separately, then state which of them actually moved. Where the host is genuinely constrained, internal Valkey latency metrics reflect external virtualization limits rather than engine behavior — but that conclusion needs the specific signal named, not “steal was high.”&lt;/p&gt;
&lt;h3 id=&quot;2-characterize-memory-breakdown-rss-fragmentation-and-eviction-dynamics&quot;&gt;2. Characterize memory breakdown: RSS, fragmentation, and eviction dynamics&lt;/h3&gt;
&lt;p&gt;Evaluate the relationship between &lt;code&gt;used_memory&lt;/code&gt;, &lt;code&gt;used_memory_rss&lt;/code&gt;, and &lt;code&gt;maxmemory&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Fragmentation Ratio ($used_memory_rss / used_memory$):&lt;/strong&gt; A ratio above 1.5 indicates significant allocator fragmentation, where jemalloc holds unpurged arenas. A ratio below 1.0 indicates that the operating system has paged Valkey memory to swap disk, which destroys in-memory latency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Eviction vs. Expiration Rates:&lt;/strong&gt; If &lt;code&gt;used_memory&lt;/code&gt; reaches &lt;code&gt;maxmemory&lt;/code&gt; and &lt;code&gt;evicted_keys&lt;/code&gt; climbs, Valkey is pruning keys to accommodate incoming writes. If &lt;code&gt;maxmemory_policy&lt;/code&gt; is &lt;code&gt;noeviction&lt;/code&gt;, subsequent write commands fail immediately with &lt;code&gt;OOM command not allowed&lt;/code&gt; errors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Unevictable Key Traps:&lt;/strong&gt; Under &lt;code&gt;volatile-lru&lt;/code&gt;, &lt;code&gt;volatile-lfu&lt;/code&gt;, or &lt;code&gt;volatile-ttl&lt;/code&gt;, keys without an explicit TTL cannot be evicted. If unexpired or TTL-less keys fill &lt;code&gt;maxmemory&lt;/code&gt;, writes fail despite memory exhaustion.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-deconstruct-cpu-saturation-and-event-loop-blocking&quot;&gt;3. Deconstruct CPU saturation and event-loop blocking&lt;/h3&gt;
&lt;p&gt;Valkey handles network parsing across multiple I/O threads when configured, but core command execution runs on a single main event loop.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Main Thread CPU (&lt;code&gt;used_cpu_user_main_thread&lt;/code&gt;):&lt;/strong&gt; If main thread user CPU approaches 100% of a single core while overall instance CPU remains low (e.g., 25% on a 4-vCPU instance), the single-threaded engine is CPU-bound. Inspect &lt;code&gt;INFO commandstats&lt;/code&gt; for $O(N)$ operations (&lt;code&gt;KEYS&lt;/code&gt;, &lt;code&gt;HGETALL&lt;/code&gt;, &lt;code&gt;SMEMBERS&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Synchronous Eviction Blocking:&lt;/strong&gt; Freeing large strings, sets, or hashes during eviction happens synchronously on the main thread by default. If &lt;code&gt;lazyfree-lazy-eviction&lt;/code&gt; is &lt;code&gt;no&lt;/code&gt;, evicting large keys blocks the event loop.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Transparent Huge Pages (THP):&lt;/strong&gt; Check &lt;code&gt;/sys/kernel/mm/transparent_hugepage/enabled&lt;/code&gt;. If set to &lt;code&gt;[always]&lt;/code&gt;, write traffic during background persistence (&lt;code&gt;BGSAVE&lt;/code&gt;) forces 2 MB page allocations, stalling the event loop and inflating memory via Copy-on-Write.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;4-trace-latency-amplification-through-network-buffers-and-client-queues&quot;&gt;4. Trace latency amplification through network, buffers, and client queues&lt;/h3&gt;
&lt;p&gt;Sub-millisecond engine performance does not prevent upstream timeouts if transport queues are saturated:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Connection Backlog Drops:&lt;/strong&gt; Compare &lt;code&gt;connected_clients&lt;/code&gt; with &lt;code&gt;maxclients&lt;/code&gt;. Check the Linux socket listen backlog (&lt;code&gt;/proc/sys/net/core/somaxconn&lt;/code&gt;) against Valkey’s &lt;code&gt;tcp-backlog&lt;/code&gt;. If client connect bursts exceed this queue, TCP SYN packets are dropped, adding connection timeouts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ENA Network Throttling:&lt;/strong&gt; On EC2, check &lt;code&gt;ethtool -S &amp;#x3C;interface&gt; | grep -E &quot;allowance_exceeded|drop&quot;&lt;/code&gt;. Nitro instances enforce strict Packet Per Second (PPS) allowances; exceeding them causes packet drops at the hypervisor level.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Client Output Buffer Overflow:&lt;/strong&gt; Check &lt;code&gt;INFO clients&lt;/code&gt; for &lt;code&gt;client_recent_max_output_buffer&lt;/code&gt; and &lt;code&gt;client_recent_max_input_buffer&lt;/code&gt;. When clients issue large queries faster than socket buffers drain, Valkey allocates expanding output buffers, consuming memory until reaching &lt;code&gt;client-output-buffer-limit&lt;/code&gt;. Both fields report a recent maximum across all clients, so a high value proves a large buffer occurred but does not identify the connection holding it — use &lt;code&gt;CLIENT LIST&lt;/code&gt; and read its &lt;code&gt;omem&lt;/code&gt; and &lt;code&gt;tot-mem&lt;/code&gt; columns to attribute it.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;5-audit-persistence-replication-lag-and-fork-induced-stalls&quot;&gt;5. Audit persistence, replication lag, and fork-induced stalls&lt;/h3&gt;
&lt;p&gt;Inspect background persistence activity and replica synchronization status:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Fork Latency (&lt;code&gt;latest_fork_usec&lt;/code&gt;):&lt;/strong&gt; During &lt;code&gt;BGSAVE&lt;/code&gt; or &lt;code&gt;BGREWRITEAOF&lt;/code&gt;, the engine calls &lt;code&gt;fork()&lt;/code&gt;. Duplicating a 32 GB instance page table can stall the main thread for 50–200 ms.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AOF Fsync Stalls (&lt;code&gt;aof_delayed_fsync&lt;/code&gt;):&lt;/strong&gt; Under &lt;code&gt;appendfsync everysec&lt;/code&gt;, if an EBS volume hits IOPS limits during background flushing, the main thread delays subsequent writes by up to 2 seconds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replication Backlog Wrap:&lt;/strong&gt; Compare &lt;code&gt;master_repl_offset&lt;/code&gt; with replica offsets. If replication lag exceeds &lt;code&gt;repl-backlog-size&lt;/code&gt;, the primary cannot perform a partial resync (&lt;code&gt;PSYNC&lt;/code&gt;) and forces a full snapshot resync.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Valkey latency or saturation incident] --&gt; B{Process restart or CPU steal detected}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes| C[Investigate OOM crash or EC2 burstable credit starvation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No| D{used_memory near maxmemory}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Yes| E{Evictions climbing rapidly}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|Yes| F[Check lazyfree settings and working set growth]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|No| G{mem_fragmentation_ratio &gt; 1.5}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|Yes| H[Check allocator arenas and active defragmentation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|No| I[Check volatile policy unevictable keys]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|No| J{Main thread CPU at 100%}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|Yes| K[Inspect SLOWLOG and O-N commandstats]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|No| L{ENA allowance exceeded or backlog drops}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|Yes| M[Resize EC2 network tier or tune somaxconn]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|No| N[Inspect AOF fsync stalls and fork latency]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://valkey.io/topics/memory-optimization/&quot;&gt;official Valkey documentation&lt;/a&gt; establishes that memory management relies on jemalloc chunk allocation. When keys are allocated and released in rapid, variable-sized bursts, jemalloc can retain virtual memory pages, resulting in a high &lt;code&gt;mem_fragmentation_ratio&lt;/code&gt;. The documented behavior mandates enabling &lt;code&gt;activedefrag yes&lt;/code&gt; or tuning &lt;code&gt;active-defrag-threshold-lower&lt;/code&gt; rather than restarting the node or adding physical RAM.&lt;/p&gt;
&lt;p&gt;Valkey’s &lt;a href=&quot;https://valkey.io/topics/latency/&quot;&gt;latency troubleshooting guide&lt;/a&gt; details the single-threaded execution model: any command with $O(N)$ complexity blocking the main thread prevents all other client commands from executing. Furthermore, freeing keys containing large collections synchronously during eviction blocks the event loop; enabling &lt;code&gt;lazyfree-lazy-eviction&lt;/code&gt;, &lt;code&gt;lazyfree-lazy-expire&lt;/code&gt;, and &lt;code&gt;lazyfree-lazy-server-del&lt;/code&gt; offloads memory reclamation to background &lt;code&gt;bio&lt;/code&gt; threads.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://docs.kernel.org/admin-guide/sysctl/vm.html&quot;&gt;Linux Kernel Virtual Memory documentation&lt;/a&gt; documents that &lt;code&gt;vm.overcommit_memory = 1&lt;/code&gt; is mandatory for in-memory datastores relying on &lt;code&gt;fork()&lt;/code&gt; for snapshotting. Without this setting, background save operations fail if the kernel estimates that allocated address space exceeds physical RAM plus swap.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/monitoring-network-performance-ena.html&quot;&gt;AWS EC2 Nitro documentation&lt;/a&gt; emphasizes that network performance on EC2 is bounded by per-instance bandwidth and packet-per-second (PPS) allowances. ENA metrics surfaced via &lt;code&gt;ethtool&lt;/code&gt; (&lt;code&gt;pps_allowance_exceeded&lt;/code&gt;, &lt;code&gt;bw_in_allowance_exceeded&lt;/code&gt;) indicate when network traffic is throttled at the hypervisor, demonstrating why high p99 latency can occur while instance CPU and internal database execution latency appear normal.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven root cause&lt;/th&gt;&lt;th&gt;Candidate remediation&lt;/th&gt;&lt;th&gt;Validation criteria&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Main thread blocked by synchronous evictions&lt;/td&gt;&lt;td&gt;Set &lt;code&gt;lazyfree-lazy-eviction yes&lt;/code&gt; and &lt;code&gt;lazyfree-lazy-expire yes&lt;/code&gt; via &lt;code&gt;CONFIG SET&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;latency_percentiles_usec&lt;/code&gt; drops; background freeing visible in &lt;code&gt;INFO memory&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Allocator fragmentation causing memory ballooning&lt;/td&gt;&lt;td&gt;Enable &lt;code&gt;activedefrag yes&lt;/code&gt;; configure &lt;code&gt;active-defrag-threshold-lower 10&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;mem_fragmentation_ratio&lt;/code&gt; drops toward 1.1–1.2 without spike in CPU latency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;O(N) command execution stalling event loop&lt;/td&gt;&lt;td&gt;Replace &lt;code&gt;KEYS&lt;/code&gt; with &lt;code&gt;SCAN&lt;/code&gt;; replace large &lt;code&gt;HGETALL&lt;/code&gt; with &lt;code&gt;HSCAN&lt;/code&gt; or &lt;code&gt;HMGET&lt;/code&gt; in app&lt;/td&gt;&lt;td&gt;&lt;code&gt;SLOWLOG&lt;/code&gt; clears; &lt;code&gt;used_cpu_user_main_thread&lt;/code&gt; normalizes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Transparent Huge Pages causing COW latency&lt;/td&gt;&lt;td&gt;Disable THP: &lt;code&gt;echo never &gt; /sys/kernel/mm/transparent_hugepage/enabled&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;latest_fork_usec&lt;/code&gt; and p99 latency during &lt;code&gt;BGSAVE&lt;/code&gt; drop by &gt;80%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Burstable CPU credit exhaustion (T3/T4g)&lt;/td&gt;&lt;td&gt;Upgrade instance to memory-optimized Nitro type (e.g., &lt;code&gt;r7g.large&lt;/code&gt; / &lt;code&gt;r7g.xlarge&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;&lt;code&gt;%steal&lt;/code&gt; drops to 0; CPU throttling alerts clear&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ENA PPS allowance exhaustion&lt;/td&gt;&lt;td&gt;Upgrade instance size for higher network baseline or implement client-side pipelining&lt;/td&gt;&lt;td&gt;&lt;code&gt;pps_allowance_exceeded&lt;/code&gt; counter delta returns to 0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AOF disk fsync blocking writes&lt;/td&gt;&lt;td&gt;Set &lt;code&gt;no-appendfsync-on-rewrite yes&lt;/code&gt; or migrate EBS to gp3 with dedicated IOPS&lt;/td&gt;&lt;td&gt;&lt;code&gt;aof_delayed_fsync&lt;/code&gt; counter stops incrementing during AOF rewrites&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;Operational adjustments on a live Valkey EC2 instance require an explicit reversal path:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Dynamic Configuration Changes:&lt;/strong&gt; Any parameter modified via &lt;code&gt;CONFIG SET&lt;/code&gt; (such as &lt;code&gt;lazyfree-lazy-eviction&lt;/code&gt;, &lt;code&gt;maxmemory&lt;/code&gt;, or &lt;code&gt;activedefrag&lt;/code&gt;) can be reverted instantaneously with a reciprocal &lt;code&gt;CONFIG SET&lt;/code&gt;. Always capture prior settings via &lt;code&gt;CONFIG GET &amp;#x3C;parameter&gt;&lt;/code&gt; before applying changes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Active Defragmentation Rollback:&lt;/strong&gt; If enabling &lt;code&gt;activedefrag&lt;/code&gt; introduces unwanted CPU overhead on the main thread during peak traffic, disable it immediately with &lt;code&gt;CONFIG SET activedefrag no&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Kernel Parameter Adjustments:&lt;/strong&gt; If modifying sysctl parameters (such as &lt;code&gt;net.core.somaxconn&lt;/code&gt; or &lt;code&gt;vm.overcommit_memory&lt;/code&gt;), record existing values from &lt;code&gt;sysctl -a&lt;/code&gt; before applying &lt;code&gt;sysctl -w&lt;/code&gt;. Revert using the saved baseline.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Instance Resizing Rollback:&lt;/strong&gt; If vertical instance scaling requires stopping the instance, ensure a standby replica has taken over the master role or maintain an EBS snapshot before initiating the resize. Revert using standard AWS CLI commands if needed.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why naive reasoning fails&lt;/th&gt;&lt;th&gt;Better diagnostic boundary&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;High CPU means “needs bigger instance”&lt;/td&gt;&lt;td&gt;Single main thread can be pegged at 100% of 1 core while 15 cores sit idle&lt;/td&gt;&lt;td&gt;Measure &lt;code&gt;used_cpu_user_main_thread&lt;/code&gt; and profile &lt;code&gt;INFO commandstats&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High memory means “memory leak”&lt;/td&gt;&lt;td&gt;jemalloc allocator fragmentation or LRU cache design mimics memory leaks&lt;/td&gt;&lt;td&gt;Correlate &lt;code&gt;used_memory&lt;/code&gt;, &lt;code&gt;used_memory_rss&lt;/code&gt;, and &lt;code&gt;mem_fragmentation_ratio&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Latency spikes mean “slow network”&lt;/td&gt;&lt;td&gt;Kernel THP 2 MB page allocations during snapshotting cause silent engine freezes&lt;/td&gt;&lt;td&gt;Check &lt;code&gt;/sys/kernel/mm/transparent_hugepage/enabled&lt;/code&gt; and &lt;code&gt;latest_fork_usec&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Zero evictions means “healthy memory”&lt;/td&gt;&lt;td&gt;&lt;code&gt;noeviction&lt;/code&gt; policy causes hard application write errors instead of evictions&lt;/td&gt;&lt;td&gt;Verify &lt;code&gt;maxmemory_policy&lt;/code&gt; and check for &lt;code&gt;OOM&lt;/code&gt; command errors in application logs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Low ops/sec means “idle database”&lt;/td&gt;&lt;td&gt;A single multi-key blocking command or Lua script can stall the engine completely&lt;/td&gt;&lt;td&gt;Check &lt;code&gt;blocked_clients&lt;/code&gt;, &lt;code&gt;SLOWLOG&lt;/code&gt;, and main thread CPU utilization&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CloudWatch CPU looks normal&lt;/td&gt;&lt;td&gt;CloudWatch averages CPU across all cores, hiding a 100% pinned main execution core&lt;/td&gt;&lt;td&gt;Inspect per-vCPU utilization and internal engine thread metrics&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Unsanitized Payload Ingestion:&lt;/strong&gt; The LLM must not receive raw &lt;code&gt;MONITOR&lt;/code&gt; output or unredacted &lt;code&gt;DUMP&lt;/code&gt; payloads, as these contain raw key names, user sessions, and sensitive payload values.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Real-Time Monitoring or Polling:&lt;/strong&gt; The LLM does not continuously poll database ports or manage live telemetry streams. It operates exclusively on static, structured evidence packs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Production Execution:&lt;/strong&gt; The LLM cannot execute &lt;code&gt;CONFIG SET&lt;/code&gt;, issue &lt;code&gt;FLUSHALL&lt;/code&gt;, trigger failovers, or terminate client connections. Production modifications require human verification and authorization.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Intuitive Guesswork on Missing Data:&lt;/strong&gt; If evidence lacks &lt;code&gt;SLOWLOG&lt;/code&gt; samples, &lt;code&gt;ethtool&lt;/code&gt; network counters, or jemalloc memory breakdowns, the LLM must explicitly list these as missing evidence rather than guessing a root cause.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Valkey performance incidents on EC2 present overlapping symptoms across single-threaded execution, memory fragmentation, kernel page allocation, and hypervisor limits that mislead generic database triage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Assemble a role-aware, time-bounded evidence pack capturing synchronized Valkey &lt;code&gt;INFO&lt;/code&gt; deltas, Linux virtual memory counters, and EC2 ENA driver metrics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Formulate hypotheses that explicitly reconcile main thread CPU against I/O threads, &lt;code&gt;mem_fragmentation_ratio&lt;/code&gt; against eviction dynamics, and network allowance limits against application p99 latency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Deploy deterministic telemetry collectors on your Valkey EC2 fleet. If command execution latency is confirmed as the primary bottleneck, proceed to Part 2 to identify expensive commands, data structure anti-patterns, and blocking functions.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://valkey.io/topics/memory-optimization/&quot;&gt;Valkey Documentation — Memory Optimization and Configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://valkey.io/topics/latency/&quot;&gt;Valkey Documentation — How to Troubleshoot Latency Issues&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://valkey.io/commands/&quot;&gt;Valkey Documentation — Command Reference and Time Complexity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.kernel.org/admin-guide/sysctl/vm.html&quot;&gt;Linux Kernel Virtual Memory Documentation — &lt;code&gt;sysctl vm&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.kernel.org/admin-guide/mm/transhuge.html&quot;&gt;Linux Kernel Documentation — Transparent Hugepage Support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://jemalloc.net/&quot;&gt;jemalloc Documentation — Scalable Memory Allocation Architecture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/monitoring-network-performance-ena.html&quot;&gt;AWS EC2 Documentation — Monitor Network Performance for ENA&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/burstable-credits-baseline-concepts.html&quot;&gt;AWS EC2 Documentation — Burstable Performance Instances and Credit System&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>ai-engineering</category><category>failures</category></item><item><title>Cloud Database Cost Engineering: How to Reduce Database, Data Warehouse, and Licensing Spend Across Azure, AWS, GCP, and OCI</title><link>https://rajivonai.com/blog/2026-02-04-cloud-database-cost-engineering-framework/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-02-04-cloud-database-cost-engineering-framework/</guid><description>A comprehensive framework for reigning in cloud database costs, focusing on licensing, right-sizing, and architectural tradeoffs.</description><pubDate>Wed, 04 Feb 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The biggest hidden cost in any cloud migration isn’t the compute—it’s the database licensing and the failure to right-size legacy architecture.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Organizations migrating to the cloud are routinely shocked by their database bills. Lift-and-shift migrations carry over oversized on-premises hardware assumptions, and default “License-Included” options mask massive premiums on commercial engines like Oracle and SQL Server.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cloud cost optimization (FinOps) usually focuses on generic EC2/VM compute and S3/Blob storage tiering. But databases and data warehouses operate under entirely different constraints. You cannot simply autoscale a monolithic SQL Server, and pausing a dedicated data warehouse pool has severe cache implications. How do you systematically reduce cloud database spend across Azure, AWS, GCP, and OCI without risking production stability?&lt;/p&gt;
&lt;h2 id=&quot;the-cloud-database-cost-engineering-framework&quot;&gt;The Cloud Database Cost Engineering Framework&lt;/h2&gt;
&lt;h3 id=&quot;1-the-licensing-trap&quot;&gt;1. The Licensing Trap&lt;/h3&gt;
&lt;p&gt;Never accept “License-Included” pricing for enterprise databases without doing the math first.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Audit your existing Enterprise Agreements.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tool&lt;/strong&gt;: Use our &lt;a href=&quot;https://rajivonai.com/tools/sql-server-license-calculator/&quot;&gt;SQL Server Cloud Licensing Calculator&lt;/a&gt; to compare the retail cloud rate against Bring Your Own License (BYOL) and Azure Hybrid Benefit models.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-data-warehouse-right-sizing&quot;&gt;2. Data Warehouse Right-Sizing&lt;/h3&gt;
&lt;p&gt;Data warehouses like Azure Synapse and Google BigQuery are often provisioned for peak load and left running 24/7.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Enforce strict pause/resume schedules for non-prod environments and offload exploratory analyst queries to serverless endpoints.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tool&lt;/strong&gt;: Estimate your potential savings with the &lt;a href=&quot;https://rajivonai.com/tools/azure-synapse-cost-calculator/&quot;&gt;Azure Synapse Cost Optimizer&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-open-source-migration-roi&quot;&gt;3. Open-Source Migration ROI&lt;/h3&gt;
&lt;p&gt;Escaping commercial licensing by migrating to PostgreSQL or MySQL is financially attractive, but technically perilous.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Do not calculate ROI without including the engineering cost to rewrite stored procedures (PL/SQL or T-SQL).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tool&lt;/strong&gt;: Model the true 5-year payback period using our &lt;a href=&quot;https://rajivonai.com/tools/oracle-migration-savings-calculator/&quot;&gt;Oracle to PostgreSQL Migration Savings Calculator&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;4-reserved-instance-timing&quot;&gt;4. Reserved Instance Timing&lt;/h3&gt;
&lt;p&gt;Committing to 1-year or 3-year database Reserved Instances (RIs) immediately after a migration locks in architectural waste.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Wait 90 days. Profile the P95 workload, scale down the instance class, and &lt;em&gt;then&lt;/em&gt; purchase the RI.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tool&lt;/strong&gt;: Check the break-even math with the &lt;a href=&quot;https://rajivonai.com/tools/reserved-instance-roi-calculator/&quot;&gt;Database Reserved Instance ROI Calculator&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for mature engineering organizations is to decouple database scaling from application scaling. They treat database cost as an architectural problem (schema design, query patterns, license negotiation) rather than a simple FinOps discounting exercise.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Optimization&lt;/th&gt;&lt;th&gt;Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;BYOL / Azure Hybrid Benefit&lt;/td&gt;&lt;td&gt;Requires strict compliance tracking. Over-provisioning cores in the cloud triggers massive audit penalties from Oracle and Microsoft.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Serverless Offload&lt;/td&gt;&lt;td&gt;Moving from provisioned capacity to pay-per-TB-scanned (like BigQuery on-demand or Synapse Serverless) can cause costs to explode if tables lack strict partition filters.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Unchecked cloud database costs are unsustainable and often rooted in poor licensing or oversized architecture.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Apply a rigorous, database-specific cost engineering framework.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Organizations routinely cut commercial database spend by 40-60% through BYOL adoption and aggressive right-sizing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Try the free calculators linked above to model your savings.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&quot;request-a-cloud-database-cost-review&quot;&gt;Request a Cloud Database Cost Review&lt;/h3&gt;
&lt;p&gt;If you need an expert architectural review of your Azure Synapse footprint, SQL Server licensing, or a complete multi-cloud database TCO analysis, &lt;strong&gt;Request a Cloud Database Cost Review&lt;/strong&gt;. We will map your current spend, identify immediate right-sizing opportunities, and build a defensible migration ROI model.&lt;/p&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category><category>checklist</category></item><item><title>Repo-Embedded Skills for Database Teams</title><link>https://rajivonai.com/blog/2026-01-23-repo-embedded-skills-for-database-teams/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-01-23-repo-embedded-skills-for-database-teams/</guid><description>Why database teams should store agent instructions, runbook contracts, and review policies in the repository instead of in memory.</description><pubDate>Fri, 23 Jan 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;If the rule matters during review, it belongs in the repository where the agent can read it.&lt;/strong&gt; Database teams carry a lot of implicit knowledge: which tables are too large for blocking DDL, which accounts are break-glass only, which dashboards prove a rollout is safe, and which rollback path is acceptable for each schema change.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Database teams carry a lot of implicit knowledge: which tables are too large for blocking DDL, which accounts are break-glass only, which dashboards prove a rollout is safe, and which rollback path is acceptable for each schema change.&lt;/p&gt;
&lt;p&gt;The pattern matters for database, cloud, and platform teams because agents do not operate in a vacuum. They inherit repository rules, tool permissions, deployment workflows, incident history, and the quality of the evidence available to them.&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Operating layer&lt;/th&gt;&lt;th&gt;Default approach&lt;/th&gt;&lt;th&gt;Better alternative&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Context&lt;/td&gt;&lt;td&gt;Rely on a long prompt or chat history&lt;/td&gt;&lt;td&gt;Give the agent task-specific evidence and rules&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tooling&lt;/td&gt;&lt;td&gt;Expose broad tools and inspect later&lt;/td&gt;&lt;td&gt;Expose narrow tools with clear approval boundaries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Verification&lt;/td&gt;&lt;td&gt;Read the final answer&lt;/td&gt;&lt;td&gt;Check the artifact, trace, and final state&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Implicit knowledge does not survive agent execution. If the agent cannot read the rule, it cannot reliably follow it. Prompting the rule by hand in every session creates drift and makes review impossible.&lt;/p&gt;
&lt;p&gt;The practical question is not whether an agent can produce a convincing response. The question is whether the engineering system around that response makes the work observable, reversible, and reviewable.&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure point&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Weak boundary&lt;/td&gt;&lt;td&gt;Agent authority is broader than the task&lt;/td&gt;&lt;td&gt;A diagnostic run can become an unsafe change&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Missing evidence&lt;/td&gt;&lt;td&gt;The agent cannot cite the state it used&lt;/td&gt;&lt;td&gt;Review becomes opinion instead of verification&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No lifecycle&lt;/td&gt;&lt;td&gt;The workflow ends at a message&lt;/td&gt;&lt;td&gt;Ownership, audit, cleanup, and rollback disappear&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;repository-skill-backbone&quot;&gt;Repository Skill Backbone&lt;/h2&gt;
&lt;p&gt;Store skills beside the code: migration review rules, incident triage steps, Terraform plan review guidance, test commands, and abort conditions.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[task request — bounded intent] --&gt; B[repository skill backbone — controls]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[tool execution — evidence collected]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[verification — final state checked]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[human handoff — audit retained]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Define the operating boundary.&lt;/strong&gt;&lt;br&gt;
Write down the task class, allowed tools, environment, data class, and approval mode before the agent runs.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Shape the evidence.&lt;/strong&gt;&lt;br&gt;
Return compact observations instead of raw dumps. The agent should see enough to reason, but not so much that context is wasted.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Require proof of completion.&lt;/strong&gt;&lt;br&gt;
Completion should be an artifact or state check: a passing test, a reviewed plan, a valid rollback, a trace, or a linked ticket.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Create a &lt;code&gt;skills&lt;/code&gt; or &lt;code&gt;AGENTS.md&lt;/code&gt; layer that tells the agent how this repository works, which scripts are authoritative, and what proof is required before it can claim completion.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Context: OpenAI’s harness engineering discussion emphasizes repository skills, local scripts, and environment-specific guidance as part of the system around Codex. That makes repo-local instructions part of engineering infrastructure. Source: &lt;a href=&quot;https://openai.com/index/harness-engineering/&quot;&gt;OpenAI, Harness engineering&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Action: Create a &lt;code&gt;skills&lt;/code&gt; or &lt;code&gt;AGENTS.md&lt;/code&gt; layer that tells the agent how this repository works, which scripts are authoritative, and what proof is required before it can claim completion.&lt;/p&gt;
&lt;p&gt;Result: When the rule is versioned, every change to the agent operating model can be reviewed like code.&lt;/p&gt;
&lt;p&gt;Learning: Store skills beside the code: migration review rules, incident triage steps, Terraform plan review guidance, test commands, and abort conditions. This is a documented pattern or a direct consequence of how the named systems behave, not a fabricated production story.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Tribal policy&lt;/td&gt;&lt;td&gt;Only senior engineers know the rule&lt;/td&gt;&lt;td&gt;Move rules into repo-local instructions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Stale prompts&lt;/td&gt;&lt;td&gt;Different users paste different guidance&lt;/td&gt;&lt;td&gt;Version shared skills with the code&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Script ignorance&lt;/td&gt;&lt;td&gt;Agent invents commands instead of using local scripts&lt;/td&gt;&lt;td&gt;Document canonical scripts and expected outputs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No stop conditions&lt;/td&gt;&lt;td&gt;Agent keeps trying unsafe alternatives&lt;/td&gt;&lt;td&gt;Write explicit abort conditions&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Implicit knowledge does not survive agent execution. If the agent cannot read the rule, it cannot reliably follow it. Prompting the rule by hand in every session creates drift and makes review impossible.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Store skills beside the code: migration review rules, incident triage steps, Terraform plan review guidance, test commands, and abort conditions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: When the rule is versioned, every change to the agent operating model can be reviewed like code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Add one repository-local agent guide for migrations: allowed commands, rollback requirements, lock-risk rules, and proof of completion.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The teams that get value from agents will not be the teams with the longest prompts. They will be the teams that turn agent work into a controlled engineering workflow.&lt;/p&gt;</content:encoded><category>ai-engineering</category><category>databases</category><category>architecture</category></item><item><title>MongoDB Shard Hotspots, Balancing and Replica Performance</title><link>https://rajivonai.com/blog/2026-01-22-mongodb-shard-hotspots-balancing-replica-performance/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-01-22-mongodb-shard-hotspots-balancing-replica-performance/</guid><description>A self-managed MongoDB workflow for distinguishing routing fan-out, uneven ownership, range-migration overhead, cleanup debt, and replica-set pressure.</description><pubDate>Thu, 22 Jan 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A balanced MongoDB collection can still have a hot shard. The balancer distributes ranges under placement constraints; it does not equalize traffic, scatter-gather work, moving write frontiers, or replica health.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;MongoDB Parts 1 and 2 classified cluster pressure and traced expensive query shapes. Now suppose one shard remains at 90% CPU while its peers sit near 30%. Latency rises, replication lag appears inside that shard, and the balancer reports the collection as compliant.&lt;/p&gt;
&lt;p&gt;That is not contradictory evidence. A sharded cluster has three independent distributions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Routing:&lt;/strong&gt; how many shards each operation targets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ownership:&lt;/strong&gt; where owned documents, bytes, and ranges reside.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Service capacity:&lt;/strong&gt; how much useful work each shard replica set can perform.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;MongoDB’s balancer runs on the config-server primary, respects zones, and moves ranges when placement policy requires it. Its compliance verdict addresses ownership under those rules. It does not prove that traffic or capacity is balanced.&lt;/p&gt;
&lt;p&gt;An LLM can correlate router, shard, migration, cleanup, and replica evidence. It cannot infer a shard-key defect from CPU alone or safely turn a suspected hotspot into an immediate resharding command.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The same symptom—one shard is slow—has very different causes. A high-frequency shard-key value can concentrate targeted traffic. A monotonic key can send current inserts to the range containing &lt;code&gt;MaxKey&lt;/code&gt;. Queries missing the shard key can scatter to every shard. Zone placement can intentionally concentrate data. Range migration can tax the donor and recipient, while delayed orphan cleanup creates later I/O pressure. Or the ownership may be sound and one shard’s primary, storage, or secondary replication path may simply be unhealthy.&lt;/p&gt;
&lt;p&gt;These mechanisms can overlap. Migration begins after imbalance, so migration counters correlated with latency do not prove migration caused the original event. Replica lag may be a consequence of hot writes, not the cause of application latency.&lt;/p&gt;
&lt;p&gt;How do we prove which distribution failed before changing placement or the shard key?&lt;/p&gt;
&lt;h2 id=&quot;build-a-three-axis-sharding-evidence-pack&quot;&gt;Build a Three-Axis Sharding Evidence Pack&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[incident interval and topology snapshot] --&gt; B[router targeting and query shapes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; C[owned data ranges zones and orphans]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; D[per-shard hosts replica roles and capacity]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; E[migration and range-deletion timeline]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; F[deterministic interval aggregates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[LLM observations hypotheses contradictions and gaps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[DBA verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[bounded remediation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[traffic ownership and replica validation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Capture a time-bounded topology manifest: versions, &lt;code&gt;mongos&lt;/code&gt; identities, config-server primary, shards, replica-set roles, namespaces, shard keys, zones, and balancer state. Every metric needs UTC timestamps, source, units, counter semantics, and process start time.&lt;/p&gt;
&lt;p&gt;For routers, aggregate operation counts by query shape and number of shards targeted. For ownership, capture per-shard owned documents and bytes, orphaned documents and bytes, zone constraints, and collection compliance. For each shard replica set, retain CPU, memory, disk latency and throughput, network, connections, operations, WiredTiger cache pressure, tickets or queues where applicable, replication lag, oplog window, elections, and member state.&lt;/p&gt;
&lt;p&gt;Migration and deletion counters are cumulative and reset with process restart. Convert them to restart-bounded interval deltas; never compare raw totals across differently aged processes.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Evidence pattern&lt;/th&gt;&lt;th&gt;Plausible mechanism&lt;/th&gt;&lt;th&gt;Evidence still required&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;One shard owns similar bytes but serves far more operations&lt;/td&gt;&lt;td&gt;hot key, query-frequency skew, or moving write frontier&lt;/td&gt;&lt;td&gt;shard-key value distribution and targeted shape counts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;All shards receive most reads&lt;/td&gt;&lt;td&gt;scatter-gather routing&lt;/td&gt;&lt;td&gt;&lt;code&gt;mongos&lt;/code&gt; targeting counters and representative &lt;code&gt;explain()&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One shard owns materially more data&lt;/td&gt;&lt;td&gt;placement imbalance or zone constraint&lt;/td&gt;&lt;td&gt;owned bytes, ranges, zones, and balancer status&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Donor and recipient latency rise during migration&lt;/td&gt;&lt;td&gt;clone, catch-up, critical-section, or network work&lt;/td&gt;&lt;td&gt;migration phase, interval deltas, host and network metrics&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Orphaned bytes or deletion queue grows&lt;/td&gt;&lt;td&gt;range cleanup delayed or resource constrained&lt;/td&gt;&lt;td&gt;cursor activity, deletion-task trend, disk and cache pressure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Only one shard primary is saturated&lt;/td&gt;&lt;td&gt;replica-set member or storage problem&lt;/td&gt;&lt;td&gt;member-level metrics, elections, lag, and host comparison&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Balancer says compliant but latency remains skewed&lt;/td&gt;&lt;td&gt;traffic or capacity imbalance&lt;/td&gt;&lt;td&gt;router fan-out and per-shard service evidence&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;h3 id=&quot;1-freeze-the-topology-before-interpreting-load&quot;&gt;1. Freeze the topology before interpreting load&lt;/h3&gt;
&lt;p&gt;Record which &lt;code&gt;mongos&lt;/code&gt; observed each request, which node was config primary, and which members were shard primaries during baseline, transition, incident, and recovery. Elections and role changes otherwise make host graphs look like abrupt capacity loss.&lt;/p&gt;
&lt;p&gt;Record balancer enablement and window, but do not disable it reflexively. MongoDB warns that leaving balancing disabled can degrade performance as ownership diverges. A scheduled window uses the config-server primary’s local time, so verify timezone before concluding that a migration ran “outside” the window.&lt;/p&gt;
&lt;h3 id=&quot;2-measure-routing-fan-out&quot;&gt;2. Measure routing fan-out&lt;/h3&gt;
&lt;p&gt;A query containing the shard key or a usable compound-key prefix can target one shard or a subset. Without it, &lt;code&gt;mongos&lt;/code&gt; commonly broadcasts and merges results. Cost depends on returned work, network, and the slowest target.&lt;/p&gt;
&lt;p&gt;Use &lt;code&gt;shardingStatistics.numHostsTargeted&lt;/code&gt; from &lt;code&gt;serverStatus&lt;/code&gt; on each &lt;code&gt;mongos&lt;/code&gt; to derive interval rates for &lt;code&gt;oneShard&lt;/code&gt;, &lt;code&gt;manyShards&lt;/code&gt;, and &lt;code&gt;allShards&lt;/code&gt; operations. Break the change down by command and query shape. Then use representative &lt;code&gt;explain()&lt;/code&gt; evidence to inspect the &lt;code&gt;shards&lt;/code&gt;, &lt;code&gt;mergeType&lt;/code&gt;, and &lt;code&gt;splitPipeline&lt;/code&gt; fields where applicable.&lt;/p&gt;
&lt;p&gt;A rising all-shard ratio aligned with latency supports scatter-gather amplification. It does not prove the shard key should change: a missing application predicate, changed aggregation pipeline, or routing-unfriendly index/query design may be the narrower defect.&lt;/p&gt;
&lt;h3 id=&quot;3-separate-owned-data-from-hot-traffic&quot;&gt;3. Separate owned data from hot traffic&lt;/h3&gt;
&lt;p&gt;Run &lt;code&gt;$shardedDataDistribution&lt;/code&gt; through &lt;code&gt;mongos&lt;/code&gt; with an approved diagnostic role. It reports &lt;code&gt;numOwnedDocuments&lt;/code&gt;, &lt;code&gt;ownedSizeBytes&lt;/code&gt;, &lt;code&gt;numOrphanedDocs&lt;/code&gt;, and &lt;code&gt;orphanedSizeBytes&lt;/code&gt; per shard and namespace. Pair that snapshot with &lt;code&gt;sh.balancerCollectionStatus()&lt;/code&gt;, which can identify draining-shard, zone, or imbalance violations.&lt;/p&gt;
&lt;p&gt;These are placement facts, not traffic facts. Equal bytes can hide a high-frequency tenant or current-time range. Low shard-key cardinality limits distribution; frequent values create indivisible hot ranges; monotonic keys focus new inserts at one edge. Hashed sharding may distribute those writes but makes range predicates less targetable.&lt;/p&gt;
&lt;p&gt;Use &lt;code&gt;analyzeShardKey&lt;/code&gt; only as a bounded investigation with representative sampling. Its key characteristics and read-write distribution help test candidate keys, but sampling has cost and historical migrations can distort its monotonicity inference. The LLM should report the sample interval and coverage, not convert the output into certainty.&lt;/p&gt;
&lt;h3 id=&quot;4-correlate-migration-with-cleanup&quot;&gt;4. Correlate migration with cleanup&lt;/h3&gt;
&lt;p&gt;During range migration, the donor continues serving writes while the recipient creates missing indexes, clones documents, and catches up. Metadata then commits, and donor cleanup happens asynchronously. MongoDB can begin another migration without waiting for deletion of the previous range.&lt;/p&gt;
&lt;p&gt;Derive interval deltas for donor migrations started, committed, and aborted; donor migration time; recipient documents and bytes cloned; and documents deleted by the range deleter. Track &lt;code&gt;shardingStatistics.rangeDeleterTasks&lt;/code&gt; for queued or active work. Use &lt;code&gt;$shardedDataDistribution&lt;/code&gt; to verify whether orphan counts fall.&lt;/p&gt;
&lt;p&gt;This sequence matters. A recipient CPU spike during cloning, a short metadata-transition pause, and donor I/O after commit have different remedies. Long-running reads or cursors can delay deletion. &lt;code&gt;cleanupOrphaned&lt;/code&gt; is deprecated in MongoDB 8.0 and only waits for cleanup; it is not a manual deletion remedy.&lt;/p&gt;
&lt;h3 id=&quot;5-inspect-the-replica-set-inside-every-shard&quot;&gt;5. Inspect the replica set inside every shard&lt;/h3&gt;
&lt;p&gt;Each shard is a replica set, not one capacity number. Compare its primary, secondaries, and equivalent peer-shard members. Check write rate, disk latency, WiredTiger cache, replication lag, oplog window, elections, network, and read-preference routing.&lt;/p&gt;
&lt;p&gt;If one primary is slow while ownership and targeted traffic are normal, investigate member or infrastructure health. If secondaries lag only on the hot shard, determine whether replication pressure follows concentrated writes or whether a slow secondary is constraining durability and failover readiness. Do not solve a primary hotspot by shifting reads to secondaries without proving staleness tolerance and query capacity.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[one shard is hot] --&gt; B{router fan-out changed}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| C[identify broadcast shapes and missing targeting predicates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| D{traffic per owned byte is skewed}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes| E[test hot values monotonicity and zone constraints]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no| F{ownership is noncompliant}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|yes| G[inspect balancer state windows and migration blockers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|no| H{migration or cleanup overlaps}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| I[separate donor clone commit and deletion phases]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| J[inspect shard replica members storage and elections]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; K[bounded remediation and validation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The model’s output must separate &lt;strong&gt;observations&lt;/strong&gt;, &lt;strong&gt;hypotheses&lt;/strong&gt;, &lt;strong&gt;missing evidence&lt;/strong&gt;, and &lt;strong&gt;actions&lt;/strong&gt;. “Shard 2 handled 71% of targeted writes” is an observation. “A high-frequency tenant key caused the hotspot” is a hypothesis until value-frequency and routing evidence support it.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented MongoDB behavior produces a useful diagnostic pattern. Suppose owned bytes are within policy, collection status is compliant, all-shard reads remain flat, and one shard receives most targeted inserts. Migration activity begins only after the traffic divergence. That evidence contradicts “the balancer caused the incident” and supports a traffic hotspot—possibly a monotonic frontier or high-frequency key value.&lt;/p&gt;
&lt;p&gt;Now change the evidence: targeted traffic is stable, donor and recipient clone counters rise with network and disk activity, and range-deletion tasks accumulate after commits. Migration is then a plausible latency amplifier, while the original reason for balancing still needs explanation.&lt;/p&gt;
&lt;p&gt;The LLM can rank these hypotheses and identify contradictions across hundreds of interval summaries. A DBA must verify the actual shard key, zone policy, representative query plans, migration phase, and replica health before authorizing a change.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;p&gt;Prefer the narrowest reversible action:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Fix a missing shard-key predicate, aggregation routing defect, or unintended multi-shard write, then canary the application change.&lt;/li&gt;
&lt;li&gt;Move discretionary maintenance away from the incident interval or adjust the balancing window after confirming the config-primary timezone. Preserve a maximum pause and automatic re-enable procedure.&lt;/li&gt;
&lt;li&gt;Relieve a degraded shard member or infrastructure bottleneck using the platform’s tested failover, replacement, or capacity procedure.&lt;/li&gt;
&lt;li&gt;Correct zone ranges or membership only after proving the current constraint causes concentration and verifying data-residency requirements.&lt;/li&gt;
&lt;li&gt;Split a divisible oversized range where appropriate. Do not clear a &lt;code&gt;jumbo&lt;/code&gt; flag blindly; an indivisible high-frequency value remains indivisible.&lt;/li&gt;
&lt;li&gt;Refine the shard key when adding suffix fields improves cardinality or range divisibility. Refinement keeps the existing key as a prefix and does not immediately redistribute existing ranges.&lt;/li&gt;
&lt;li&gt;Reshard only when the current key cannot support required routing and distribution. MongoDB 8.0 can force redistribution with the same key, but resharding is write- and storage-intensive, permits one collection operation at a time, and includes a write-blocking phase.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;Define rollback per change. Application routing changes need feature flags and old-query compatibility. Balancer-window changes need the prior schedule, a timer, and ownership-drift thresholds. Zone changes need an exported mapping. Member replacement needs election and replication headroom.&lt;/p&gt;
&lt;p&gt;Shard-key refinement is not a simple metadata toggle, and completed resharding has no one-command return to the former design. Rehearse resharding on representative scale, validate storage and oplog headroom, monitor its phase with &lt;code&gt;$currentOp&lt;/code&gt;, define pre-commit abort criteria, and preserve a tested recovery procedure. Do not describe an untested re-reshard as rollback.&lt;/p&gt;
&lt;p&gt;After any change, validate all three axes: per-shape shard targeting, owned and orphaned data, and per-member capacity and replication. Also verify P95 and P99 latency, throughput, errors, elections, and the absence of a shifted hotspot.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why the analysis fails&lt;/th&gt;&lt;th&gt;Guardrail&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Single &lt;code&gt;mongos&lt;/code&gt; sampled&lt;/td&gt;&lt;td&gt;routing behavior differs across routers or deployments&lt;/td&gt;&lt;td&gt;aggregate every relevant router with process identity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Raw cumulative counters compared&lt;/td&gt;&lt;td&gt;process age looks like incident work&lt;/td&gt;&lt;td&gt;use restart-bounded interval deltas&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Balancer compliance treated as health&lt;/td&gt;&lt;td&gt;policy compliance ignores traffic and capacity&lt;/td&gt;&lt;td&gt;evaluate all three axes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Correlation treated as causation&lt;/td&gt;&lt;td&gt;balancing often follows the original skew&lt;/td&gt;&lt;td&gt;preserve baseline, transition, incident, and recovery order&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Orphans treated as duplicate application data&lt;/td&gt;&lt;td&gt;migration leftovers have ownership semantics&lt;/td&gt;&lt;td&gt;use supported distribution and cleanup evidence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Resharding recommended from averages&lt;/td&gt;&lt;td&gt;hot values and query patterns disappear in means&lt;/td&gt;&lt;td&gt;test frequency, monotonicity, targeting, and adverse values&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LLM receives unrestricted command access&lt;/td&gt;&lt;td&gt;a hypothesis can become an irreversible topology change&lt;/td&gt;&lt;td&gt;read-only evidence role and human-approved runbook&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Customer Data:&lt;/strong&gt; The LLM must not receive documents, profiler entries containing literal filter values, or &lt;code&gt;currentOp&lt;/code&gt; output with embedded query predicates. Send normalized query shapes, &lt;code&gt;planSummary&lt;/code&gt;, &lt;code&gt;docsExamined&lt;/code&gt;/&lt;code&gt;nReturned&lt;/code&gt; ratios, and timings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Production Execution:&lt;/strong&gt; The LLM cannot create or drop indexes, run &lt;code&gt;moveChunk&lt;/code&gt;, alter the balancer, step down primaries, or change cluster configuration. It proposes; a human authorizes, applies, validates, and holds the rollback condition.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Causal Conclusion Without Contradicting Evidence:&lt;/strong&gt; Every hypothesis must cite both the telemetry that supports it and the telemetry that would falsify it. A ranked hypothesis with no disconfirming test attached is an assertion, not a diagnosis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Silent Gap-Filling:&lt;/strong&gt; Where the evidence pack lacks a required signal, the LLM must name it explicitly under missing evidence rather than inferring a plausible value. “Not collected” and “collected and normal” are different findings and must never be merged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Change Without a Human Gate:&lt;/strong&gt; Production changes require human authorization, a stated validation signal, and a defined rollback condition agreed before the change is applied.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; A hot shard can originate in routing, ownership, migration, cleanup, or replica capacity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build synchronized router, placement, migration, and member evidence before asking the LLM to correlate causes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Confirm the hypothesis against targeting ratios, owned and orphaned data, restart-aware migration deltas, and replica health; then demonstrate recovery without moving the hotspot.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Implement read-only collectors and reversible runbooks now. Part 4 will secure profiler data, diagnostic logs, access boundaries, audit evidence, and production guardrails.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/sharding-balancer-administration/&quot;&gt;MongoDB 8.0: Sharded Cluster Balancer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/sharded-cluster-query-router/&quot;&gt;MongoDB 8.0: &lt;code&gt;mongos&lt;/code&gt; Query Router&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/sharding-choose-a-shard-key/&quot;&gt;MongoDB 8.0: Choose a Shard Key&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/command/analyzeShardKey/&quot;&gt;MongoDB 8.0: &lt;code&gt;analyzeShardKey&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/operator/aggregation/shardedDataDistribution/&quot;&gt;MongoDB 8.0: &lt;code&gt;$shardedDataDistribution&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/command/serverStatus/&quot;&gt;MongoDB 8.0: &lt;code&gt;serverStatus&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/method/sh.balancerCollectionStatus/&quot;&gt;MongoDB 8.0: &lt;code&gt;sh.balancerCollectionStatus()&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/sharding-reshard-a-collection/&quot;&gt;MongoDB 8.0: Reshard a Collection&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>failures</category><category>architecture</category></item><item><title>Agentic Code Review for Database Repositories</title><link>https://rajivonai.com/blog/2026-01-20-agentic-code-review-for-database-repositories/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-01-20-agentic-code-review-for-database-repositories/</guid><description>Database repositories contain hidden rules human reviewers know: never add a blocking index at peak hours, never widen IAM without owner approval. Agent review surfaces these violations before merge — without displacing the human judgment that set the rules.</description><pubDate>Tue, 20 Jan 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Database code review is no longer just syntax and style; agents can inspect the operational path around the diff.&lt;/strong&gt; A database repository usually contains more than SQL. It has Flyway or Liquibase migrations, Terraform modules, shell scripts, backup jobs, dashboards, and runbooks. Human reviewers know the hidden rules: never add the blocking index in peak hours, never widen IAM without owner approval, never merge a migration without rollback.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A database repository usually contains more than SQL. It has Flyway or Liquibase migrations, Terraform modules, shell scripts, backup jobs, dashboards, and runbooks. Human reviewers know the hidden rules: never add the blocking index in peak hours, never widen IAM without owner approval, never merge a migration without rollback.&lt;/p&gt;
&lt;p&gt;The pattern matters for database, cloud, and platform teams because agents do not operate in a vacuum. They inherit repository rules, tool permissions, deployment workflows, incident history, and the quality of the evidence available to them.&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Operating layer&lt;/th&gt;&lt;th&gt;Default approach&lt;/th&gt;&lt;th&gt;Better alternative&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Context&lt;/td&gt;&lt;td&gt;Rely on a long prompt or chat history&lt;/td&gt;&lt;td&gt;Give the agent task-specific evidence and rules&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tooling&lt;/td&gt;&lt;td&gt;Expose broad tools and inspect later&lt;/td&gt;&lt;td&gt;Expose narrow tools with clear approval boundaries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Verification&lt;/td&gt;&lt;td&gt;Read the final answer&lt;/td&gt;&lt;td&gt;Check the artifact, trace, and final state&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Generic linters cannot reason across that repository. They can catch formatting, but not whether a migration conflicts with the rollback playbook or whether a Terraform change breaks the service catalog contract.&lt;/p&gt;
&lt;p&gt;The practical question is not whether an agent can produce a convincing response. The question is whether the engineering system around that response makes the work observable, reversible, and reviewable.&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure point&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Weak boundary&lt;/td&gt;&lt;td&gt;Agent authority is broader than the task&lt;/td&gt;&lt;td&gt;A diagnostic run can become an unsafe change&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Missing evidence&lt;/td&gt;&lt;td&gt;The agent cannot cite the state it used&lt;/td&gt;&lt;td&gt;Review becomes opinion instead of verification&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No lifecycle&lt;/td&gt;&lt;td&gt;The workflow ends at a message&lt;/td&gt;&lt;td&gt;Ownership, audit, cleanup, and rollback disappear&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;agentic-repository-review&quot;&gt;Agentic Repository Review&lt;/h2&gt;
&lt;p&gt;Give the review agent repository rules, migration policy, operational runbooks, and read-only access to test commands. Its job is to produce review findings with evidence, not to approve the change.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[task request — bounded intent] --&gt; B[agentic repository review — controls]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[tool execution — evidence collected]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[verification — final state checked]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[human handoff — audit retained]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Define the operating boundary.&lt;/strong&gt;&lt;br&gt;
Write down the task class, allowed tools, environment, data class, and approval mode before the agent runs.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Shape the evidence.&lt;/strong&gt;&lt;br&gt;
Return compact observations instead of raw dumps. The agent should see enough to reason, but not so much that context is wasted.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Require proof of completion.&lt;/strong&gt;&lt;br&gt;
Completion should be an artifact or state check: a passing test, a reviewed plan, a valid rollback, a trace, or a linked ticket.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Split review into specialized checks: SQL lock risk, rollback completeness, Terraform blast radius, observability coverage, and deployment sequencing.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Context: OpenAI’s public Datadog Codex example frames agent review as system-level review rather than only local code suggestions. That is the right lens for database repositories. Source: &lt;a href=&quot;https://openai.com/index/datadog/&quot;&gt;OpenAI, Datadog uses Codex for system-level code review&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Action: Split review into specialized checks: SQL lock risk, rollback completeness, Terraform blast radius, observability coverage, and deployment sequencing.&lt;/p&gt;
&lt;p&gt;Result: A useful agent review cites the exact file, command, or policy that supports the finding. If it cannot cite evidence, the finding should be downgraded to a question.&lt;/p&gt;
&lt;p&gt;Learning: Give the review agent repository rules, migration policy, operational runbooks, and read-only access to test commands. Its job is to produce review findings with evidence, not to approve the change. This is a documented pattern or a direct consequence of how the named systems behave, not a fabricated production story.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Style-only review&lt;/td&gt;&lt;td&gt;Agent comments on names but misses lock risk&lt;/td&gt;&lt;td&gt;Give it operational policies and migration examples&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unbounded suggestions&lt;/td&gt;&lt;td&gt;Agent rewrites unrelated code&lt;/td&gt;&lt;td&gt;Require findings first, patches only after approval&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No evidence&lt;/td&gt;&lt;td&gt;Comments are plausible but uncited&lt;/td&gt;&lt;td&gt;Require file path, command output, or policy citation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Human bypass&lt;/td&gt;&lt;td&gt;Agent approval becomes social proof&lt;/td&gt;&lt;td&gt;Keep human owner as final approver&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Generic linters cannot reason across that repository. They can catch formatting, but not whether a migration conflicts with the rollback playbook or whether a Terraform change breaks the service catalog contract.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Give the review agent repository rules, migration policy, operational runbooks, and read-only access to test commands. Its job is to produce review findings with evidence, not to approve the change.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: A useful agent review cites the exact file, command, or policy that supports the finding. If it cannot cite evidence, the finding should be downgraded to a question.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Create a review checklist for one DB repo with five agent checks: lock risk, rollback, deploy order, observability, and Terraform blast radius.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The teams that get value from agents will not be the teams with the longest prompts. They will be the teams that turn agent work into a controlled engineering workflow.&lt;/p&gt;</content:encoded><category>ai-engineering</category><category>databases</category><category>architecture</category></item><item><title>Finding Expensive MongoDB Query Shapes with LLM-Assisted Analysis</title><link>https://rajivonai.com/blog/2026-01-16-finding-expensive-mongodb-query-shapes-llm-assisted-analysis/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-01-16-finding-expensive-mongodb-query-shapes-llm-assisted-analysis/</guid><description>A self-managed MongoDB workflow for ranking expensive query shapes, interpreting explain evidence, diagnosing aggregation fan-out, and validating reversible index changes.</description><pubDate>Fri, 16 Jan 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;&lt;code&gt;IXSCAN&lt;/code&gt; is not a performance verdict. A query can use an index and still examine excessive keys, fetch most of a collection, sort or spill, amplify an aggregation, and consume more total capacity than the single slowest operation.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;MongoDB Part 1 classified an incident at the host, cache, connection, replication, topology, and workload layers. Suppose workload evidence remains: operation demand changed, query-executor work rose, and the affected member consumed more CPU or I/O. The next question is not “Which query was slow?” It is “Which normalized operation shape created the resource demand?”&lt;/p&gt;
&lt;p&gt;Self-managed MongoDB exposes slow-operation evidence in diagnostic logs and the database profiler. &lt;code&gt;explain()&lt;/code&gt; reveals candidate plans, index bounds, execution stages, and work performed by a representative operation. Historical evidence finds recurring shapes; &lt;code&gt;explain()&lt;/code&gt; tests one representative value now.&lt;/p&gt;
&lt;p&gt;An LLM can connect shapes, plan evidence, pipeline structure, schema facts, and incident timing. It cannot safely infer an index from query text alone or treat one laboratory execution as production proof.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Slow-query lists overemphasize latency outliers. A moderate operation executed continuously may dominate total resource consumption. Sampling misses some executions, while one pathological literal can make an otherwise acceptable shape look universally bad.&lt;/p&gt;
&lt;p&gt;MongoDB 8.0 also exposes multiple identifiers that must not be conflated. &lt;code&gt;queryShapeHash&lt;/code&gt; identifies the newer query shape used by query settings. &lt;code&gt;planCacheShapeHash&lt;/code&gt; identifies a plan-cache query shape. &lt;code&gt;planCacheKey&lt;/code&gt; additionally depends on indexes available to that shape; even similarly shaped operations can have different keys when a partial index is eligible for one predicate value but not another.&lt;/p&gt;
&lt;p&gt;Finally, &lt;code&gt;explain()&lt;/code&gt; ignores existing plan-cache entries and prevents its winner from being cached. Output differs between classic and slot-based execution, and execution verbosity performs real work.&lt;/p&gt;
&lt;p&gt;How do we move from sampled operations to a proven shape-level diagnosis without turning production into a benchmark environment?&lt;/p&gt;
&lt;h2 id=&quot;build-a-shape-to-proof-workflow&quot;&gt;Build a Shape-to-Proof Workflow&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[incident workload and resource interval] --&gt; B[slow diagnostic logs and bounded profiler evidence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[normalize and group by namespace command and shape]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[rank by frequency latency and examined work]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[select representative and adverse values]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[bounded explain and plan-cache evidence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[index pipeline schema and data-distribution analysis]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[LLM observations hypotheses contradictions and gaps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[DBA validation on representative data]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[reversible change and before-after proof]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Retain UTC interval, member and role, namespace, command, shape identifiers, duration, keys and documents examined, returned count, sort and disk-use indicators, plan summary, query framework, application identity, and sanitized representative structure. Preserve count, sum, percentiles, and maximum per shape; omit raw literals by default.&lt;/p&gt;
&lt;p&gt;Rank shapes on more than maximum latency. Use execution frequency, cumulative latency, keys examined, documents examined, disk use, errors, and overlap with the incident resource curve. The LLM receives these deterministic aggregates and the evidence lineage behind them.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Evidence pattern&lt;/th&gt;&lt;th&gt;Plausible mechanism&lt;/th&gt;&lt;th&gt;Required proof&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;COLLSCAN&lt;/code&gt; with high documents examined&lt;/td&gt;&lt;td&gt;no eligible index or scan is cheaper&lt;/td&gt;&lt;td&gt;indexes, predicate, cardinality, execution stages&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;IXSCAN&lt;/code&gt; with high keys examined&lt;/td&gt;&lt;td&gt;broad bounds, low selectivity, multikey expansion&lt;/td&gt;&lt;td&gt;bounds, key pattern, representative values&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High documents examined after &lt;code&gt;IXSCAN&lt;/code&gt;&lt;/td&gt;&lt;td&gt;residual filtering or non-covering fetch&lt;/td&gt;&lt;td&gt;&lt;code&gt;FETCH&lt;/code&gt; filter, projection, index fields&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Explicit &lt;code&gt;SORT&lt;/code&gt; or &lt;code&gt;hasSortStage&lt;/code&gt;&lt;/td&gt;&lt;td&gt;index does not provide required order&lt;/td&gt;&lt;td&gt;sort keys, prefix equalities, memory and disk use&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aggregation uses disk&lt;/td&gt;&lt;td&gt;blocking stage exceeded its memory allowance&lt;/td&gt;&lt;td&gt;pipeline stages, &lt;code&gt;usedDisk&lt;/code&gt;, input cardinality&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;$lookup&lt;/code&gt; latency rises&lt;/td&gt;&lt;td&gt;outer fan-out or inefficient foreign access&lt;/td&gt;&lt;td&gt;outer row count, foreign plan and index eligibility&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Same shape has mixed latency&lt;/td&gt;&lt;td&gt;literal selectivity, cache state, concurrency, or plan behavior&lt;/td&gt;&lt;td&gt;value buckets, plan keys, host and timeline evidence&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;h3 id=&quot;1-discover-shapes-without-profiling-everything&quot;&gt;1. Discover shapes without profiling everything&lt;/h3&gt;
&lt;p&gt;Start with existing diagnostic slow-operation logs. Preserve the configured slow threshold, sampling rate, log component, member, and time window, because absence from sampled slow evidence is not proof of absence.&lt;/p&gt;
&lt;p&gt;Use the database profiler only through an approved, time-bounded collection plan. It operates per database, writes to the capped &lt;code&gt;system.profile&lt;/code&gt; collection, can affect performance and disk usage, and can expose unencrypted query data. It is not available through &lt;code&gt;mongos&lt;/code&gt;; a sharded deployment requires member-aware collection. Restore the prior profiling configuration after capture.&lt;/p&gt;
&lt;p&gt;Normalize literals before model access and retain types, operators, value buckets, sort, projection, collation, hint, read concern, and pipeline order when they distinguish behavior. Security and retention controls belong in Part 4.&lt;/p&gt;
&lt;h3 id=&quot;2-preserve-shape-and-plan-identity&quot;&gt;2. Preserve shape and plan identity&lt;/h3&gt;
&lt;p&gt;Group historical evidence by namespace, command, and the identifier actually emitted by that source. In MongoDB 8.0, prefer &lt;code&gt;planCacheShapeHash&lt;/code&gt; over its deprecated duplicate &lt;code&gt;queryHash&lt;/code&gt; for plan-cache analysis. Keep &lt;code&gt;queryShapeHash&lt;/code&gt; separate. Store &lt;code&gt;planCacheKey&lt;/code&gt;, index catalog version, server version, and &lt;code&gt;queryFramework&lt;/code&gt; beside each plan.&lt;/p&gt;
&lt;p&gt;Do not merge on truncated query text or assume one literal represents the shape. Partial-index eligibility, collation, types, arrays, and data skew can change the plan or cost.&lt;/p&gt;
&lt;h3 id=&quot;3-run-bounded-explain-at-the-right-verbosity&quot;&gt;3. Run bounded explain at the right verbosity&lt;/h3&gt;
&lt;p&gt;Begin with &lt;code&gt;queryPlanner&lt;/code&gt; to inspect the winning and rejected plan structure. Use &lt;code&gt;executionStats&lt;/code&gt; only for representative operations under a time, load, and data-sensitivity budget. Reserve &lt;code&gt;allPlansExecution&lt;/code&gt; for a focused planner question; it adds candidate-plan detail but is not routine telemetry.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.orders.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;explain&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;executionStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { tenantId: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&amp;#x3C;token&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, status: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pending&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, createdAt: { $gte: &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ISODate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&amp;#x3C;bucket&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) } },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { _id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, status: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, createdAt: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, total: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ createdAt: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;limit&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Prefer representative data in a controlled environment. On production, bound execution and avoid broad values. Compare &lt;code&gt;explain()&lt;/code&gt; with historical plan summaries and &lt;code&gt;$planCacheStats&lt;/code&gt;; do not claim it reproduced the cached incident path.&lt;/p&gt;
&lt;h3 id=&quot;4-read-work-not-just-stage-names&quot;&gt;4. Read work, not just stage names&lt;/h3&gt;
&lt;p&gt;Walk the plan tree. Compare &lt;code&gt;totalKeysExamined&lt;/code&gt; and &lt;code&gt;totalDocsExamined&lt;/code&gt; with &lt;code&gt;nReturned&lt;/code&gt;, but retain absolute counts and zero-result cases. Inspect index bounds, residual filters under &lt;code&gt;FETCH&lt;/code&gt;, explicit &lt;code&gt;SORT&lt;/code&gt;, limit placement, seeks, spills or &lt;code&gt;usedDisk&lt;/code&gt;, and execution estimates available for the deployed version.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;COLLSCAN&lt;/code&gt; can be rational for a tiny collection or an unselective predicate. &lt;code&gt;IXSCAN&lt;/code&gt; can be poor when its bounds cover most keys. A covered query generally avoids document fetch and can report zero documents examined, but a wider covering index increases storage, cache demand, and write maintenance.&lt;/p&gt;
&lt;p&gt;Do not require one fixed nesting path. MongoDB documents different classic and slot-based structures and warns that explain fields can change. Store raw versioned evidence plus normalized fields and parser version.&lt;/p&gt;
&lt;h3 id=&quot;5-expand-aggregation-plans-into-cardinality-flow&quot;&gt;5. Expand aggregation plans into cardinality flow&lt;/h3&gt;
&lt;p&gt;For aggregations, inspect the optimized plan rather than judging source order alone. MongoDB can move eligible &lt;code&gt;$match&lt;/code&gt; predicates ahead of projections or sorts. Early &lt;code&gt;$match&lt;/code&gt;, compatible &lt;code&gt;$sort&lt;/code&gt;, and certain &lt;code&gt;$group&lt;/code&gt; patterns can use indexes; later stages that access another collection can use that collection’s indexes.&lt;/p&gt;
&lt;p&gt;Track documents entering and leaving blocking or fan-out stages. &lt;code&gt;$unwind&lt;/code&gt; can multiply rows; &lt;code&gt;$group&lt;/code&gt; and &lt;code&gt;$sort&lt;/code&gt; hold state; &lt;code&gt;$lookup&lt;/code&gt; cost combines outer cardinality and foreign work. Correlated field-to-constant comparisons can use an eligible foreign index; field-to-field comparisons cannot.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[expensive MongoDB shape] --&gt; B{historical evidence overlaps incident}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| C[lower priority and find correlated shape]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| D{bounded explain reproduces excessive work}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no| E[test representative values cache concurrency and prior plan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes| F{collection scan}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|yes| G[test eligibility selectivity and index cost]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|no| H{keys or fetched documents excessive}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| I[test bounds residual filter projection and multikey fan-out]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| J{sort spill or aggregation fan-out}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|yes| K[test pipeline order indexes and data model]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|no| L[test concurrency cache storage and network]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; M[validate one reversible change]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt; M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;MongoDB’s &lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/query-plans/&quot;&gt;query-plan documentation&lt;/a&gt; says candidate plans run during a trial, the winner is cached for later operations of the same plan-cache shape, and &lt;code&gt;explain()&lt;/code&gt; bypasses that cache. It also documents that &lt;code&gt;planCacheKey&lt;/code&gt; depends on both shape and available indexes. The derived control is to preserve historical and reproduced plan evidence separately.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/explain-results/&quot;&gt;&lt;code&gt;explain&lt;/code&gt; result reference&lt;/a&gt; documents plan trees, classic and slot-based output differences, &lt;code&gt;IXSCAN&lt;/code&gt;, &lt;code&gt;COLLSCAN&lt;/code&gt;, and explicit &lt;code&gt;SORT&lt;/code&gt;. It warns that listed output fields are subject to change. A durable collector therefore normalizes a small stable contract while retaining raw, versioned evidence.&lt;/p&gt;
&lt;p&gt;MongoDB’s &lt;a href=&quot;https://www.mongodb.com/docs/v8.0/tutorial/find-slow-queries-with-database-profiler/&quot;&gt;profiler guidance&lt;/a&gt; explicitly warns about performance, storage, and disclosure risks. The &lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/database-profiler/&quot;&gt;profiler output&lt;/a&gt; exposes examined work, sort presence, and disk use. This supports shape discovery, but sampling and threshold configuration remain part of the evidence.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/aggregation-pipeline-optimization/&quot;&gt;aggregation optimizer documentation&lt;/a&gt; describes predicate movement and index opportunities for &lt;code&gt;$match&lt;/code&gt;, &lt;code&gt;$sort&lt;/code&gt;, selected &lt;code&gt;$group&lt;/code&gt; patterns, and later collection-access stages. The documented pattern is to prove optimized cardinality flow, not merely rewrite pipeline text.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven cause&lt;/th&gt;&lt;th&gt;Candidate response&lt;/th&gt;&lt;th&gt;Validation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Missing or weak access path&lt;/td&gt;&lt;td&gt;build a targeted compound index using ESR or selective ERS tradeoff&lt;/td&gt;&lt;td&gt;examined work, sort, latency, write cost&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Fetch dominates&lt;/td&gt;&lt;td&gt;narrow projection or consider a justified covered index&lt;/td&gt;&lt;td&gt;documents examined, response contract, index size&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Broad range dominates&lt;/td&gt;&lt;td&gt;tighten application predicate, paginate by stable key, or redesign access&lt;/td&gt;&lt;td&gt;bounded keys and documents per request&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Blocking sort or group&lt;/td&gt;&lt;td&gt;align index and early filters, reduce input, or precompute justified results&lt;/td&gt;&lt;td&gt;no unwanted sort or spill, correct output&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;$lookup&lt;/code&gt; fan-out&lt;/td&gt;&lt;td&gt;index eligible foreign predicate, reduce outer input, or reconsider embedding&lt;/td&gt;&lt;td&gt;foreign work and end-to-end correctness&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Planner needs temporary restriction&lt;/td&gt;&lt;td&gt;test &lt;code&gt;hint&lt;/code&gt;; consider governed MongoDB 8.0 query settings&lt;/td&gt;&lt;td&gt;all representative values and rollback pass&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Shape is operationally unsafe&lt;/td&gt;&lt;td&gt;rate-limit at application or use approved rejection control&lt;/td&gt;&lt;td&gt;rejected scope exact and service behavior acceptable&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Index hints are diagnostic controls, not proof of a universal fix. MongoDB 8.0 query-setting index hints restrict the planner’s choices but do not guarantee index use; the planner may still select a collection scan. Index creation also consumes build resources and adds maintenance to every affected write.&lt;/p&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;Record original indexes, query settings, profiler configuration, application query, and baseline. Before dropping an index, hide it: MongoDB maintains a hidden index but excludes it from planning, allowing quick reversal. It still consumes write, memory, and disk resources.&lt;/p&gt;
&lt;p&gt;For a new index, define build-window load limits, replication and disk stop conditions, and a drop procedure. For query settings, record the exact &lt;code&gt;queryShapeHash&lt;/code&gt;, permitted indexes, owner, expiry, and &lt;code&gt;removeQuerySettings&lt;/code&gt; rollback. For application or pipeline changes, use versioned rollout, correctness comparison, latency and resource validation, and rapid traffic reversal.&lt;/p&gt;
&lt;p&gt;Rollback when the target shape improves but total write latency, cache pressure, disk use, replication lag, or another important shape regresses. Local query speed is not sufficient production proof.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why the conclusion fails&lt;/th&gt;&lt;th&gt;Better boundary&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Slowest sample equals biggest offender&lt;/td&gt;&lt;td&gt;Ignores frequency and cumulative work&lt;/td&gt;&lt;td&gt;Aggregate cost per shape and interval&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;IXSCAN&lt;/code&gt; equals efficient&lt;/td&gt;&lt;td&gt;Bounds, fetches, and fan-out may remain large&lt;/td&gt;&lt;td&gt;Keys, documents, returned rows, stages&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One literal represents the shape&lt;/td&gt;&lt;td&gt;Skew and partial-index eligibility differ&lt;/td&gt;&lt;td&gt;Representative and adverse value buckets&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Explain equals incident plan&lt;/td&gt;&lt;td&gt;It bypasses the existing plan cache&lt;/td&gt;&lt;td&gt;Historical summary plus controlled reproduction&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Fixed JSON path parses every plan&lt;/td&gt;&lt;td&gt;Engines and versions change structure&lt;/td&gt;&lt;td&gt;Versioned raw output and tolerant normalization&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Move every &lt;code&gt;$match&lt;/code&gt; manually&lt;/td&gt;&lt;td&gt;Optimizer already performs eligible rewrites&lt;/td&gt;&lt;td&gt;Inspect optimized plan and stage cardinality&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Add every recommended index&lt;/td&gt;&lt;td&gt;Writes, memory, storage, and build cost accumulate&lt;/td&gt;&lt;td&gt;Portfolio review and end-to-end validation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LLM generates production DDL&lt;/td&gt;&lt;td&gt;Text cannot authorize capacity and correctness risk&lt;/td&gt;&lt;td&gt;DBA-approved typed change with rollback&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Customer Data:&lt;/strong&gt; The LLM must not receive documents, profiler entries containing literal filter values, or &lt;code&gt;currentOp&lt;/code&gt; output with embedded query predicates. Send normalized query shapes, &lt;code&gt;planSummary&lt;/code&gt;, &lt;code&gt;docsExamined&lt;/code&gt;/&lt;code&gt;nReturned&lt;/code&gt; ratios, and timings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Production Execution:&lt;/strong&gt; The LLM cannot create or drop indexes, run &lt;code&gt;moveChunk&lt;/code&gt;, alter the balancer, step down primaries, or change cluster configuration. It proposes; a human authorizes, applies, validates, and holds the rollback condition.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Causal Conclusion Without Contradicting Evidence:&lt;/strong&gt; Every hypothesis must cite both the telemetry that supports it and the telemetry that would falsify it. A ranked hypothesis with no disconfirming test attached is an assertion, not a diagnosis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Silent Gap-Filling:&lt;/strong&gt; Where the evidence pack lacks a required signal, the LLM must name it explicitly under missing evidence rather than inferring a plausible value. “Not collected” and “collected and normal” are different findings and must never be merged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Change Without a Human Gate:&lt;/strong&gt; Production changes require human authorization, a stated validation signal, and a defined rollback condition agreed before the change is applied.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Individual slow operations and top-level stage names hide which MongoDB shape consumed total incident capacity and why.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Correlate normalized historical shapes with representative, bounded explain evidence; then trace keys, documents, fetches, sorts, spills, and aggregation fan-out.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Require the proposed change to reduce shape-level examined work and incident resources without unacceptable write, cache, disk, replication, or correctness regression.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Build the shape-ranking collector first. For the highest correlated shape, preserve both historical plan identity and controlled explain evidence before proposing an index or pipeline rewrite.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/query-plans/&quot;&gt;MongoDB 8.0 — Query plans&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/query-shapes/&quot;&gt;MongoDB 8.0 — Query shapes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/explain-results/&quot;&gt;MongoDB 8.0 — Explain results&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/method/db.collection.explain/&quot;&gt;MongoDB 8.0 — &lt;code&gt;db.collection.explain()&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/tutorial/find-slow-queries-with-database-profiler/&quot;&gt;MongoDB 8.0 — Find slow queries with the database profiler&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/database-profiler/&quot;&gt;MongoDB 8.0 — Database profiler output&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/tutorial/equality-sort-range-guideline/&quot;&gt;MongoDB 8.0 — ESR guideline&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/aggregation-pipeline-optimization/&quot;&gt;MongoDB 8.0 — Aggregation pipeline optimization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/operator/aggregation/lookup/&quot;&gt;MongoDB 8.0 — &lt;code&gt;$lookup&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/command/setquerysettings/&quot;&gt;MongoDB 8.0 — Query settings&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/index-creation/&quot;&gt;MongoDB 8.0 — Index builds on populated collections&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/index-hidden/&quot;&gt;MongoDB 8.0 — Hidden indexes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>failures</category><category>architecture</category></item><item><title>CloudWatch Database Insights: Alerting on What Actually Matters</title><link>https://rajivonai.com/blog/2026-01-15-cloudwatch-database-insights-telemetry-alerting/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-01-15-cloudwatch-database-insights-telemetry-alerting/</guid><description>How to move beyond useless CPU alerts and use CloudWatch Database Insights to track locks, burst balance, and connection queues.</description><pubDate>Thu, 15 Jan 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A 99% CPU alert on a database usually fires 10 minutes after the application has already crashed. Alternatively, it fires at 2:00 AM for a routine backup job, waking up an engineer for a non-issue.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Historically, Database Administrators relied on Amazon RDS Performance Insights to see exactly which queries were eating the database. While powerful, Performance Insights existed in a silo. Application developers looked at Datadog or CloudWatch APM, DBAs looked at Performance Insights, and nobody could easily correlate a 502 Bad Gateway with a specific database wait event.&lt;/p&gt;
&lt;p&gt;AWS consolidated this telemetry into &lt;strong&gt;CloudWatch Database Insights&lt;/strong&gt;, combining the granular wait-state instrumentation of Performance Insights with the fleet-wide observability, alerting, and application-tracing capabilities of CloudWatch. This blog covered the initial rollout back in &lt;a href=&quot;https://rajivonai.com/blog/2024-07-16-cloudwatch-database-insights-aurora-rds/&quot;&gt;CloudWatch Database Insights for Aurora and RDS&lt;/a&gt; — the gap now isn’t the tooling, it’s that most teams never revisited their alert thresholds after adopting it.&lt;/p&gt;
&lt;p&gt;Despite this consolidated tooling, most organizations simply ported over their legacy alerts: they ping PagerDuty if CPU &gt; 80% or if Freeable Memory drops.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Alerting on resource exhaustion (CPU, Memory) is alerting on a symptom, not a cause.&lt;/p&gt;
&lt;p&gt;If your database CPU hits 90% because a new feature is wildly popular and processing millions of perfectly optimized micro-transactions, you don’t have an incident; you have a successful business. You just need a larger instance.&lt;/p&gt;
&lt;p&gt;If your database CPU drops to 5%, but your application is completely unresponsive because 2,000 threads are queued waiting on a single row lock, you have a critical severity-1 incident. A standard CPU alert will completely miss this.&lt;/p&gt;
&lt;p&gt;The core question is: using CloudWatch Database Insights, what are the precise, actionable metrics a Staff Engineer should alert on to detect genuine database degradation before the application crashes?&lt;/p&gt;
&lt;h2 id=&quot;modern-alerting-architecture&quot;&gt;Modern Alerting Architecture&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[CloudWatch Database Insights] --&gt; B{Is there a real problem?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes — Queuing| C[High DB Load vs low vCPU]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[Alert: Lock Contention / IO Wait]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes — Throttling| E[BurstBalance dropping near 0]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[Alert: EBS IOPS Exhaustion]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes — Connection Storm| G[DatabaseConnections spiking]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[Alert: Application Pool Failure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No — Expected Load| I[High CPU but high throughput]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[Ignore / Capacity Planning]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for mature database observability dictates alerting on &lt;strong&gt;contention and throttling&lt;/strong&gt;, not just utilization.&lt;/p&gt;
&lt;p&gt;With CloudWatch Database Insights, the most critical metric is the ratio of &lt;strong&gt;DB Load to vCPU&lt;/strong&gt;. DB Load (Average Active Sessions) tells you how many queries are currently executing. If you have 8 vCPUs, and your DB Load is 6, your database is busy but healthy. If you have 8 vCPUs, and your DB Load is 145, your database is catastrophically queued. A reasonable starting point is to alert when &lt;code&gt;DB Load &gt; (vCPU * 2)&lt;/code&gt; for a sustained 3-minute period, then tune that multiplier against your own workload’s baseline — a read-heavy OLAP workload and a latency-sensitive OLTP workload will tolerate different ratios before queuing becomes a real problem.&lt;/p&gt;
&lt;p&gt;Second, for standard RDS instances using &lt;code&gt;gp2&lt;/code&gt; or &lt;code&gt;gp3&lt;/code&gt; storage, you must alert on &lt;strong&gt;BurstBalance&lt;/strong&gt;. If this value drops below 20%, it means your database is about to hit a hard hypervisor throttle on disk I/O. When it hits 0%, your disk latency will increase by a factor of 100, and your application will die. A rapidly dropping BurstBalance gives you a window — how long depends on your burst-credit consumption rate — to batch queries or scale storage before the cliff, so alert on the rate of decline, not just a fixed time estimate.&lt;/p&gt;
&lt;p&gt;Third, use the integration between &lt;strong&gt;CloudWatch Application Signals&lt;/strong&gt; and Database Insights. You can now trace a specific API endpoint (e.g., &lt;code&gt;POST /checkout&lt;/code&gt;) directly to the exact SQL digest causing the latency. This allows you to set SLO (Service Level Objective) alerts on the application endpoint, and instantly pivot to the Database Insights dashboard to see the exact row lock causing the breach.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Legacy Alert&lt;/th&gt;&lt;th&gt;Why it Fails&lt;/th&gt;&lt;th&gt;What to Alert on Instead&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;CPU &gt; 80%&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Fires during normal batch jobs; misses low-CPU lock contention.&lt;/td&gt;&lt;td&gt;&lt;code&gt;DB Load &gt; (vCPU * 2)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Freeable Memory &amp;#x3C; 1GB&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Linux aggressively caches files in memory; “low memory” is normal database behavior.&lt;/td&gt;&lt;td&gt;&lt;code&gt;SwapUsage&lt;/code&gt; &gt; 500MB (indicating actual RAM starvation)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Disk Space &amp;#x3C; 10%&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;10% of a 5TB drive is 500GB, which is plenty.&lt;/td&gt;&lt;td&gt;Rate of change (e.g., “Will run out of space in 4 hours based on current velocity”).&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Slow Query Count&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Varies wildly with traffic volume.&lt;/td&gt;&lt;td&gt;&lt;code&gt;CommitLatency&lt;/code&gt; or &lt;code&gt;SelectLatency&lt;/code&gt; crossing a strict ms threshold.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Alerting on basic resource utilization (CPU, Memory) generates alert fatigue and misses critical lock contention incidents.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Shift to CloudWatch Database Insights and alert on DB Load queuing, BurstBalance depletion, and transaction latency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; A trace that pivots directly from an Application Signals SLO breach to the exact Database Insights wait event replaces the manual correlation step of cross-referencing timestamps across separate APM and DBA dashboards — the mechanism is verifiable in the CloudWatch console itself; I have not measured a specific MTTR reduction and won’t cite one without a source.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your PagerDuty or Datadog alerts today. Delete the generic &lt;code&gt;CPU &gt; 80%&lt;/code&gt; alert. Replace it with an alert that fires when &lt;code&gt;DB Load&lt;/code&gt; exceeds double your instance’s vCPU count for 3 consecutive minutes.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category></item><item><title>LLM-Assisted MongoDB Performance Triage: Cache, Replicas and Workload</title><link>https://rajivonai.com/blog/2026-01-13-llm-assisted-mongodb-performance-triage-cache-replicas-workload/</link><guid isPermaLink="true">https://rajivonai.com/blog/2026-01-13-llm-assisted-mongodb-performance-triage-cache-replicas-workload/</guid><description>A self-managed MongoDB incident workflow for correlating WiredTiger cache, host pressure, connections, workload, replication progress, and topology evidence without mistaking symptoms for causes.</description><pubDate>Tue, 13 Jan 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A full WiredTiger cache is not a root cause, a lagging secondary is not an explanation, and a busy primary does not prove that MongoDB needs more hardware. Triage starts by preserving which member did what, when, and under which role.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A self-managed MongoDB incident crosses the application, driver, replica set, operating system, storage, and network. One API latency spike can coincide with a primary workload burst, cache eviction on a secondary, flow-control waits, and an election. Each observation is real; only some belong to the causal chain.&lt;/p&gt;
&lt;p&gt;MongoDB exposes strong evidence through &lt;code&gt;serverStatus&lt;/code&gt;, &lt;code&gt;replSetGetStatus&lt;/code&gt;, diagnostic logs, Full Time Diagnostic Data Capture (FTDC), and host telemetry. The challenge is not finding another graph. It is comparing synchronized evidence without losing node identity, process lifetime, or replica role.&lt;/p&gt;
&lt;p&gt;The LLM belongs after deterministic collection and anomaly reduction. It correlates bounded evidence, ranks explanations, identifies contradictions, and requests the next safe diagnostic. It does not poll production, invent thresholds, or change the replica set.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;MongoDB metrics are easy to misread in isolation. WiredTiger is designed to use its internal cache; occupancy alone does not prove pressure. &lt;code&gt;opcounters&lt;/code&gt; and many other fields are cumulative since process start, so raw totals cannot be compared across restarts. Replica-set averages hide a single constrained member. Replication lag can exist without flow control engaging, and an old optime may simply reflect an idle primary.&lt;/p&gt;
&lt;p&gt;Collection can distort the timeline too. A primary at 10:01 may be a secondary at 10:03. Host CPU collected at one cadence and database counters at another produce false correlations. A single &lt;code&gt;rs.status()&lt;/code&gt; snapshot misses transition and recovery.&lt;/p&gt;
&lt;p&gt;How do we classify a MongoDB slowdown as workload demand, cache and storage pressure, connection stress, replication trouble, topology change, or an external host failure before investigating individual query shapes?&lt;/p&gt;
&lt;h2 id=&quot;build-a-role-aware-mongodb-evidence-pack&quot;&gt;Build a Role-Aware MongoDB Evidence Pack&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[application latency throughput and errors] --&gt; E[time-bounded incident evidence pack]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B[host CPU memory disk and network per member] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[server status and replica status per member] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[diagnostic logs FTDC and change events] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[validate timestamps roles uptime and counter resets]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[derive rates deltas and anomalies]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[LLM observations hypotheses contradictions and gaps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[DBA verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[remediation validation and rollback]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Collect baseline, pre-incident, incident, and recovery windows in UTC. Every sample needs replica-set name, member, role, MongoDB version, process start or uptime, collection timestamp, interval, source, and collector version. Store gauges as observed values and cumulative counters as restart-bounded deltas.&lt;/p&gt;
&lt;p&gt;Keep per-member evidence intact. A cluster summary may be derived later, but it cannot replace primary and secondary timelines. Record election, restart, deployment, index-build, backup, maintenance, and infrastructure events beside performance data.&lt;/p&gt;
&lt;p&gt;The analysis contract has four outputs: observations tied to evidence IDs; ranked hypotheses with supporting and contradicting facts; missing evidence; and the next bounded action. Confidence is an investigation priority, not a probability of truth.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Plausible classes&lt;/th&gt;&lt;th&gt;Evidence that separates them&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Read latency rises&lt;/td&gt;&lt;td&gt;cache misses, disk, query mix, secondary staleness&lt;/td&gt;&lt;td&gt;page-read deltas, storage latency, operation mix, read preference, member optimes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Write latency rises&lt;/td&gt;&lt;td&gt;primary saturation, journal or checkpoint work, flow control, network&lt;/td&gt;&lt;td&gt;host saturation, WiredTiger I/O, checkpoint time, flow-control wait delta, commit progress&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One secondary lags&lt;/td&gt;&lt;td&gt;disk, CPU, network, slow oplog apply, competing reads&lt;/td&gt;&lt;td&gt;per-member resources, &lt;code&gt;pingMs&lt;/code&gt;, sync source, REPL log events, applied optime&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Connections climb&lt;/td&gt;&lt;td&gt;traffic growth, pool leak, churn, slow operations&lt;/td&gt;&lt;td&gt;&lt;code&gt;current&lt;/code&gt;, &lt;code&gt;active&lt;/code&gt;, &lt;code&gt;available&lt;/code&gt;, &lt;code&gt;totalCreated&lt;/code&gt; delta, request rate, latency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CPU climbs&lt;/td&gt;&lt;td&gt;query work, compression, eviction, replication, background work&lt;/td&gt;&lt;td&gt;operation rates, query-executor deltas, eviction evidence, role, concurrent events&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Latency jumps during election&lt;/td&gt;&lt;td&gt;topology transition, driver discovery, retry behavior&lt;/td&gt;&lt;td&gt;term, member states, election logs, application errors and retry timing&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;h3 id=&quot;1-validate-time-role-and-process-continuity&quot;&gt;1. Validate time, role, and process continuity&lt;/h3&gt;
&lt;p&gt;Confirm clock alignment, sample gaps, member identity, &lt;code&gt;uptime&lt;/code&gt;, replica state, term, and restart events before calculating rates. Reject a delta if the later counter is smaller, uptime moved backward, or the process identity changed. Annotate every primary transition.&lt;/p&gt;
&lt;p&gt;This prevents two common errors: interpreting a reset counter as reduced demand and attributing a primary symptom to a host that had already changed roles.&lt;/p&gt;
&lt;h3 id=&quot;2-classify-host-saturation-per-member&quot;&gt;2. Classify host saturation per member&lt;/h3&gt;
&lt;p&gt;Compare CPU utilization with run queue and throttling; memory availability with swap and cgroup limits; disk latency with throughput, IOPS, and queue depth; and network throughput with loss and retransmits. Use &lt;code&gt;hostInfo.system.memLimitMB&lt;/code&gt; to verify the memory limit MongoDB sees, especially in containers.&lt;/p&gt;
&lt;p&gt;Host evidence defines constraints, not causality. High disk latency on only the lagging secondary is stronger than a cluster-wide storage average. High CPU with no run queue may still have headroom. Page faults can be limited and sporadic without indicating a fault.&lt;/p&gt;
&lt;h3 id=&quot;3-test-cache-pressure-as-a-chain-not-a-percentage&quot;&gt;3. Test cache pressure as a chain, not a percentage&lt;/h3&gt;
&lt;p&gt;From &lt;code&gt;wiredTiger.cache&lt;/code&gt;, retain configured bytes, current bytes, dirty bytes, page-read and page-write counters, and eviction-related counters available in the deployed version. Derive interval rates, then align them with application latency, disk reads, disk latency, CPU, and operation mix.&lt;/p&gt;
&lt;p&gt;MongoDB uses both the WiredTiger internal cache and the operating system’s filesystem cache. Collection data in the internal cache has a different, uncompressed representation, while filesystem-cached data retains the on-disk format. Increasing the internal cache can therefore reduce memory available to the filesystem and other consumers.&lt;/p&gt;
&lt;p&gt;A persuasive cache-pressure hypothesis needs concurrent eviction or read activity, host impact, and workload evidence. Occupancy near its configured maximum is not enough.&lt;/p&gt;
&lt;h3 id=&quot;4-separate-connection-count-from-connection-stress&quot;&gt;4. Separate connection count from connection stress&lt;/h3&gt;
&lt;p&gt;Track &lt;code&gt;connections.current&lt;/code&gt;, &lt;code&gt;active&lt;/code&gt;, &lt;code&gt;available&lt;/code&gt;, &lt;code&gt;rejected&lt;/code&gt;, and the delta of &lt;code&gt;totalCreated&lt;/code&gt;. Compare them with request volume and operation latency. A stable pool can keep many connections open harmlessly; a steep creation rate suggests churn even when the current count stays flat. Rejections or exhausted availability require file-limit and process-limit evidence as well as MongoDB metrics.&lt;/p&gt;
&lt;p&gt;Do not immediately raise a connection limit. Slow database work can retain connections and make the pool a downstream victim; excessive client concurrency can also amplify queueing and cache churn.&lt;/p&gt;
&lt;h3 id=&quot;5-reconstruct-replication-progress-and-topology&quot;&gt;5. Reconstruct replication progress and topology&lt;/h3&gt;
&lt;p&gt;Capture &lt;code&gt;replSetGetStatus&lt;/code&gt; from the appropriate members, including state, health, term, sync source, heartbeat timing, &lt;code&gt;pingMs&lt;/code&gt;, applied and durable optimes, and majority commit progress. MongoDB 8.0 also reports written optimes. Compare progress over the same interval instead of treating one timestamp as a rate.&lt;/p&gt;
&lt;p&gt;On the primary, derive deltas for &lt;code&gt;flowControl.timeAcquiringMicros&lt;/code&gt;, engagement count, and engagement time. Flow control delays writes as majority-commit lag approaches its configured target, but a secondary may lag when there is insufficient load to engage it. Record the oplog window: a disconnected secondary that falls beyond it cannot catch up incrementally and needs initial sync.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[MongoDB latency or throughput incident] --&gt; B{role change restart or sample break}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| C[segment timeline and analyze topology]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| D{one member or all members constrained}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|one| E[compare its host storage network and role]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|all| F[compare workload and shared infrastructure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; G{replication progress diverging}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; H{operation mix or concurrency changed}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|yes| I[test apply disk network and sync-source hypotheses]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|no| J[test cache workload and host hypotheses]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| K[identify workload class then inspect query shapes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| L[test storage checkpoint connection and external causes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; M[collect missing evidence and verify]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt; M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;MongoDB’s &lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/command/serverstatus/&quot;&gt;&lt;code&gt;serverStatus&lt;/code&gt;&lt;/a&gt; documentation defines &lt;code&gt;opcounters&lt;/code&gt; as totals since the process started and notes that received operations increment them even when unsuccessful. It also distinguishes operation counts from document-level work. The documented behavior makes interval deltas, restart boundaries, and workload context mandatory.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/wiredtiger/&quot;&gt;WiredTiger documentation&lt;/a&gt; states that MongoDB uses an internal WiredTiger cache and the filesystem cache, and warns that container memory limits require appropriate sizing. The &lt;code&gt;serverStatus&lt;/code&gt; reference recommends avoiding an internal-cache increase above the default without a specific use case. The derived practice is to prove pressure across cache, OS, disk, and workload before changing size.&lt;/p&gt;
&lt;p&gt;MongoDB’s &lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/command/replsetgetstatus/&quot;&gt;&lt;code&gt;replSetGetStatus&lt;/code&gt;&lt;/a&gt; exposes state, term, sync source, heartbeat information, and written, applied, durable, and majority-commit progress. Its &lt;a href=&quot;https://www.mongodb.com/docs/v8.0/troubleshooting/replication-lag/&quot;&gt;replication-lag guidance&lt;/a&gt; identifies network, secondary resource exhaustion, long-running operations, and inadequate write capacity as possible causes. Lag is therefore a classification input, not a diagnosis.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/administration/full-time-diagnostic-data-capture/&quot;&gt;FTDC&lt;/a&gt; is enabled by default and periodically captures &lt;code&gt;serverStatus&lt;/code&gt;, &lt;code&gt;replSetGetStatus&lt;/code&gt;, oplog collection statistics, and host data. MongoDB documents that it excludes query predicates, query results, user collection samples, credentials, and certificates, but includes hostnames, operating-system details, and startup settings that may still be confidential. It is valuable historical evidence, not an automatically safe LLM payload.&lt;/p&gt;
&lt;p&gt;The documented pattern is a two-level investigation: use synchronized member and host evidence to locate the constrained layer, then use query-shape evidence only when workload behavior remains a supported hypothesis.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven cause&lt;/th&gt;&lt;th&gt;Candidate response&lt;/th&gt;&lt;th&gt;Validation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Working set causes sustained cache and disk pressure&lt;/td&gt;&lt;td&gt;reduce scanned data, improve access path, add memory after sizing analysis&lt;/td&gt;&lt;td&gt;latency, page-read rate, disk latency, CPU, query proof&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Connection churn or excess concurrency&lt;/td&gt;&lt;td&gt;correct pool lifecycle, cap client concurrency, stagger reconnects&lt;/td&gt;&lt;td&gt;creation rate, active work, rejects, throughput&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Secondary storage or CPU constraint&lt;/td&gt;&lt;td&gt;remove competing load, repair host path, resize the member&lt;/td&gt;&lt;td&gt;apply progress, resource headroom, recovery rate&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Network degradation between members&lt;/td&gt;&lt;td&gt;correct route, loss, bandwidth, or sync-source condition&lt;/td&gt;&lt;td&gt;packet evidence, heartbeat latency, optime convergence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Oplog window below recovery requirement&lt;/td&gt;&lt;td&gt;resize oplog with disk and retention review&lt;/td&gt;&lt;td&gt;window exceeds tested outage and maintenance need&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Flow control reflects majority-commit lag&lt;/td&gt;&lt;td&gt;repair the sustaining secondary or capacity bottleneck&lt;/td&gt;&lt;td&gt;flow-control wait rate and commit lag fall&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Election exposes driver behavior&lt;/td&gt;&lt;td&gt;correct topology discovery, timeouts, and retry policy&lt;/td&gt;&lt;td&gt;controlled failover meets error and recovery objectives&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;Define rollback before modifying cache size, oplog size, read routing, pool concurrency, sync behavior, or infrastructure. Record old configuration, target members, expected effect, stop condition, and restoration command. Change one causal lever at a time.&lt;/p&gt;
&lt;p&gt;For a cache change, watch process memory, filesystem-cache headroom, swap, eviction, disk latency, and application latency; restore the prior value if host pressure or latency worsens. For connection controls, roll back if throughput falls or rejection increases without relieving the database. For secondary or topology work, preserve voting and failure tolerance, verify the member reaches &lt;code&gt;SECONDARY&lt;/code&gt;, and confirm optime convergence before returning traffic.&lt;/p&gt;
&lt;p&gt;An oplog enlargement consumes disk and is not a cure for slow application. A sync-source override is temporary and can move load to another member. Neither should survive validation merely because replication lag briefly fell.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why reasoning fails&lt;/th&gt;&lt;th&gt;Better evidence boundary&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Cluster averages&lt;/td&gt;&lt;td&gt;Hide the constrained member and its role&lt;/td&gt;&lt;td&gt;Per-member aligned timelines&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cache occupancy threshold&lt;/td&gt;&lt;td&gt;Confuses intended use with pressure&lt;/td&gt;&lt;td&gt;Eviction, I/O, latency, and workload chain&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Raw cumulative totals&lt;/td&gt;&lt;td&gt;Mix process age with activity&lt;/td&gt;&lt;td&gt;Restart-bounded interval deltas&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One replica-status snapshot&lt;/td&gt;&lt;td&gt;Misses direction, election, and recovery&lt;/td&gt;&lt;td&gt;Repeated status plus event timeline&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lag means network&lt;/td&gt;&lt;td&gt;Disk, CPU, apply work, reads, and sync source can contribute&lt;/td&gt;&lt;td&gt;Member host, REPL logs, progress, and network&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Flow control is not engaged&lt;/td&gt;&lt;td&gt;Does not prove replication health&lt;/td&gt;&lt;td&gt;Direct optime and commit-progress evidence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;More cache is always better&lt;/td&gt;&lt;td&gt;Removes filesystem and process headroom&lt;/td&gt;&lt;td&gt;Memory-limit-aware sizing and rollback&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LLM confidence proves cause&lt;/td&gt;&lt;td&gt;Correlation cannot establish mechanism&lt;/td&gt;&lt;td&gt;Contradiction, targeted test, DBA validation&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Customer Data:&lt;/strong&gt; The LLM must not receive documents, profiler entries containing literal filter values, or &lt;code&gt;currentOp&lt;/code&gt; output with embedded query predicates. Send normalized query shapes, &lt;code&gt;planSummary&lt;/code&gt;, &lt;code&gt;docsExamined&lt;/code&gt;/&lt;code&gt;nReturned&lt;/code&gt; ratios, and timings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Production Execution:&lt;/strong&gt; The LLM cannot create or drop indexes, run &lt;code&gt;moveChunk&lt;/code&gt;, alter the balancer, step down primaries, or change cluster configuration. It proposes; a human authorizes, applies, validates, and holds the rollback condition.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Causal Conclusion Without Contradicting Evidence:&lt;/strong&gt; Every hypothesis must cite both the telemetry that supports it and the telemetry that would falsify it. A ranked hypothesis with no disconfirming test attached is an assertion, not a diagnosis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Silent Gap-Filling:&lt;/strong&gt; Where the evidence pack lacks a required signal, the LLM must name it explicitly under missing evidence rather than inferring a plausible value. “Not collected” and “collected and normal” are different findings and must never be merged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Change Without a Human Gate:&lt;/strong&gt; Production changes require human authorization, a stated validation signal, and a defined rollback condition agreed before the change is applied.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; MongoDB symptoms cross members and layers, while raw totals, role changes, and cache occupancy create convincing false narratives.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build a synchronized, role-aware incident evidence pack with restart-bounded deltas across application, host, WiredTiger, connections, replication, topology, and events.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Require every hypothesis to identify supporting and contradicting evidence, survive a targeted diagnostic, and predict a measurable recovery signal.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Instrument per-member collection and timeline segmentation first. If the workload remains implicated, continue to Part 2 and prove which query shape, access path, or aggregation stage consumed the resources.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/administration/monitoring/&quot;&gt;MongoDB 8.0 — Monitoring a self-managed deployment&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/command/serverstatus/&quot;&gt;MongoDB 8.0 — &lt;code&gt;serverStatus&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/command/replsetgetstatus/&quot;&gt;MongoDB 8.0 — &lt;code&gt;replSetGetStatus&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/wiredtiger/&quot;&gt;MongoDB 8.0 — WiredTiger storage engine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/administration/full-time-diagnostic-data-capture/&quot;&gt;MongoDB 8.0 — Full Time Diagnostic Data Capture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/replica-set-oplog/&quot;&gt;MongoDB 8.0 — Replica set oplog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/core/replica-set-sync/&quot;&gt;MongoDB 8.0 — Replica set data synchronization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/troubleshooting/replication-lag/&quot;&gt;MongoDB 8.0 — Troubleshoot replication lag&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.mongodb.com/docs/v8.0/reference/command/hostInfo/&quot;&gt;MongoDB 8.0 — &lt;code&gt;hostInfo&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>failures</category><category>architecture</category></item><item><title>Automated Reliability Across the Stack: Database Backups, Platform Observability, and SQL Quality (November 2025)</title><link>https://rajivonai.com/blog/2025-12-20-database-reliability-observability-sql-nov-2025/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-12-20-database-reliability-observability-sql-nov-2025/</guid><description>Three November 2025 open-source releases eliminate manual work from three engineering reliability tasks — multi-database backup verification, self-hosted log and trace collection, and SQL static analysis in CI pipelines.</description><pubDate>Sat, 20 Dec 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Database teams running production systems still spend significant time on three tasks that should not require human attention: manually verifying that backup restores work before an incident forces the test, triage of logs and traces from platform services, and SQL code review that catches — or misses — the specific patterns that cause production incidents. Three November 2025 open-source releases automate each of these, covering backup verification across seven database engines, self-hosted observability backed by your choice of storage, and SQL static analysis with 272 production-focused rules.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The operational layer around production databases and platform services has a persistent gap: teams implement the primary infrastructure correctly and leave the reliability infrastructure to manual processes. Backup jobs run but restores are tested once at setup and never again. Observability requires either paying Datadog rates or running an ELK stack that needs its own operational attention. SQL quality gates rely on human code review — which scales poorly as schema complexity grows. All three of these gaps have open-source answers now.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Domain&lt;/th&gt;&lt;th&gt;Manual bottleneck&lt;/th&gt;&lt;th&gt;What it costs&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Databases&lt;/td&gt;&lt;td&gt;Backup pipelines verify checksums but never test actual restores&lt;/td&gt;&lt;td&gt;Teams discover restore failures during incidents, not before&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Platform engineering&lt;/td&gt;&lt;td&gt;Unified logs, traces, and metrics require a managed service or months of ELK configuration&lt;/td&gt;&lt;td&gt;Observability budgets consume engineering time for setup and maintenance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;System design&lt;/td&gt;&lt;td&gt;SQL quality review relies on code reviewers knowing which patterns — implicit casts, unbounded scans, missing indexes — cause production incidents&lt;/td&gt;&lt;td&gt;Incidents caused by anti-patterns that a static rule would catch at commit time&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Databases&lt;/td&gt;&lt;td&gt;MySQL, PostgreSQL, MongoDB, Redis each require separate backup tools in mixed environments&lt;/td&gt;&lt;td&gt;Four tools, four retention policies, four notification configs, four failure modes to monitor&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Can these three operational gaps be closed with self-hosted open-source tooling that doesn’t require managed service accounts or custom platform engineering?&lt;/p&gt;
&lt;h2 id=&quot;automated-operational-reliability-across-the-engineering-stack&quot;&gt;Automated Operational Reliability Across the Engineering Stack&lt;/h2&gt;
&lt;p&gt;These three tools each eliminate a category of manual operational work:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OpsTeam[engineering team — operational reliability]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OpsTeam --&gt; BackupOps[databases — backup restore never verified after initial setup]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OpsTeam --&gt; ObsOps[platform — logs and traces requiring managed service or ELK overhead]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OpsTeam --&gt; SQLOps[system design — SQL quality depending on reviewer knowledge]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    BackupOps --&gt; databasement[databasement — multi-DB backup with automated restore verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ObsOps --&gt; logtide[logtide — self-hosted observability on TimescaleDB or ClickHouse]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SQLOps --&gt; slowql[slowql — 272-rule SQL static analyzer in CI pipelines]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    databasement --&gt; Out1[restore failures caught in scheduled runs, not during incidents]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    logtide --&gt; Out2[logs and traces on your infrastructure with sub-100ms query target]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    slowql --&gt; Out3[SQL anti-patterns blocked at merge time, not found in production]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;databasement--multi-database-backup-with-automated-restore-verification&quot;&gt;databasement — Multi-Database Backup with Automated Restore Verification&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The productivity problem it solves:&lt;/strong&gt; Database teams running mixed environments — PostgreSQL for OLTP, MongoDB for documents, Redis for cache — manage separate backup tools for each engine, and most of those pipelines verify checksums rather than actually testing the restore. databasement manages all seven engines from one interface and automates the restore verification step.&lt;/p&gt;
&lt;p&gt;According to the project README, databasement supports MySQL, PostgreSQL, MariaDB, Microsoft SQL Server, MongoDB, SQLite, and Redis from a single web UI. Storage destinations include S3-compatible storage (AWS S3, MinIO, and compatible endpoints), local filesystem, and remote servers via SFTP/FTP. SSH tunnel support allows connecting to databases in private networks through bastion hosts using password or key-based authentication.&lt;/p&gt;
&lt;p&gt;Retention policies support both simple time-based (days) and GFS (grandfather-father-son) rotation per the README. Compression includes gzip, zstd (documented as 20-40% better compression), and AES-256 encrypted archives. The project also exposes a REST API and an MCP server, enabling backup scheduling and status queries from AI agents and CI pipeline automation.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;docker&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; run&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -p&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; 8080:8080&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -v&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /data/databasement:/app/storage&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -e&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; APP_KEY=your-32-char-key&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  davidcrty/databasement:latest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Access at http://localhost:8080&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Add database servers, configure schedules, enable restore verification per backup job&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The cross-server restore feature documented in the README allows restoring from a production backup to a staging instance — enabling RTO testing without touching production.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Where it breaks:&lt;/strong&gt; For databases in the hundreds of gigabytes, full restore verification per backup cycle may not complete within maintenance windows. The README does not publish restore verification timing benchmarks by database engine and size. Teams should measure restore time for their largest databases before scheduling nightly verification — weekly full restore verification with daily backup-only runs is a reasonable starting point for large datasets.&lt;/p&gt;
&lt;h3 id=&quot;logtide--self-hosted-observability-without-the-elk-overhead&quot;&gt;logtide — Self-Hosted Observability Without the ELK Overhead&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The productivity problem it solves:&lt;/strong&gt; Unified collection of logs, traces, and metrics on your own infrastructure has historically meant either paying for Datadog or spending weeks configuring the Elasticsearch + Logstash + Kibana stack and then maintaining it. logtide is a self-hosted observability platform with pluggable storage that runs in Docker in under five minutes.&lt;/p&gt;
&lt;p&gt;According to the project README, logtide (v0.9.4, stable alpha) provides logs, traces, and metrics in a single interface with built-in security detection. The storage backend is configurable: TimescaleDB for standard deployments, ClickHouse for high-volume scenarios, or MongoDB for flexible document storage. The README documents a sub-100ms query performance target, PII masking for GDPR compliance, and a native Sigma Rules engine for real-time threat detection.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;services&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  logtide&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    image&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;logtide/backend:latest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    environment&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      DB_ENGINE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;timescaledb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      DB_HOST&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;timescaledb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    ports&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;4000:4000&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  timescaledb&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    image&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;timescale/timescaledb:latest-pg16&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For platform teams choosing the TimescaleDB backend: observability data becomes queryable with standard SQL tools — the same &lt;code&gt;psql&lt;/code&gt; and query tooling used for application databases applies directly to log and trace data. Teams on ClickHouse for analytics already have the right infrastructure for the high-scale storage option.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Where it breaks:&lt;/strong&gt; logtide is in “stable alpha” per the README. The Artifact Hub and Docker Hub listings are published, but the project signals active development with version cadence. Teams should not migrate primary production observability from an established system without evaluating the alpha stability against their requirements. The Sigma Rules threat detection requires familiarity with the Sigma format to write custom rules beyond the built-in set.&lt;/p&gt;
&lt;h3 id=&quot;slowql--sql-anti-patterns-caught-at-commit-time&quot;&gt;slowql — SQL Anti-Patterns Caught at Commit Time&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The productivity problem it solves:&lt;/strong&gt; SQL code review depends on reviewers knowing which patterns cause production incidents — missing indexes on join columns, implicit type casts that prevent index use, unbounded scans, N+1 query patterns, security vulnerabilities, compliance violations. slowql encodes 272 of these rules and runs them offline in any CI pipeline, catching problems before they reach production.&lt;/p&gt;
&lt;p&gt;According to the project README, slowql is a “production-focused offline SQL static analyzer” covering performance, security, reliability, compliance, cost, and code quality categories. It ships as a Python package, Docker image, and VS Code extension. The README describes it as “completely offline” — no SQL leaves the developer’s machine during analysis. It supports CI pipeline integration via standard exit codes and JSON output format.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pip&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; slowql&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Analyze migration files before merge&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;slowql&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; analyze&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --path&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; ./db/migrations/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --rules&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; all&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# CI integration — fails on critical violations&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;slowql&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; analyze&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --path&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; ./db/migrations/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --format&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; json&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --fail-on&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; critical&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For engineering teams using GitHub Actions or GitLab CI, adding slowql as a blocking check on pull requests catches structural SQL problems the same way a linter catches code style issues — at the point where the cost of fixing them is lowest.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Where it breaks:&lt;/strong&gt; slowql is a static analyzer — it evaluates SQL text without executing queries against actual data. Performance problems caused by data distribution (a query fast on development data but slow on production table sizes) are not detectable by static analysis. Slowql catches structural anti-patterns; it does not replace query plan analysis and runtime monitoring for load-dependent performance problems. Teams should use it to gate structural quality while pairing it with &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; review for performance-critical queries.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;All descriptions above are grounded in the project READMEs. Items to verify:&lt;/p&gt;
&lt;p&gt;databasement’s cross-server restore is documented in the README feature list. The restore verification implementation — specifically how data integrity is confirmed after restore, not just that the restore process completed without error — should be reviewed in the project documentation before treating it as the primary RTO validation method.&lt;/p&gt;
&lt;p&gt;logtide’s sub-100ms query performance target is stated as a design goal in the README, not a published benchmark across workload types. Teams should benchmark against their specific event volume and query patterns against the storage backend they intend to run before replacing an existing observability system.&lt;/p&gt;
&lt;p&gt;slowql’s 272-rule count is documented in the project README. Rule coverage breakdown by SQL dialect (PostgreSQL vs. MySQL vs. others) is not detailed in the README summary — teams should verify that rules relevant to their primary database engine are represented before using it as a blocking CI gate.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;databasement restore verification timeout&lt;/td&gt;&lt;td&gt;Databases over 100 GB with narrow maintenance windows&lt;/td&gt;&lt;td&gt;Run weekly full restore verification; use backup-only jobs daily for large databases&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;databasement engine version mismatch&lt;/td&gt;&lt;td&gt;Backup from one major version, restore on another&lt;/td&gt;&lt;td&gt;Pin database engine version in backup configuration; test cross-version restores in staging&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;logtide alpha stability&lt;/td&gt;&lt;td&gt;Breaking configuration changes between 0.9.x releases&lt;/td&gt;&lt;td&gt;Pin to a specific image tag; review the changelog before upgrading&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;slowql false positives&lt;/td&gt;&lt;td&gt;Rules triggering on patterns valid in the team’s SQL dialect&lt;/td&gt;&lt;td&gt;Start with &lt;code&gt;--rules performance,security&lt;/code&gt;; expand to additional categories incrementally&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;slowql runtime gap&lt;/td&gt;&lt;td&gt;Queries fast on dev data but slow on production row counts&lt;/td&gt;&lt;td&gt;Pair slowql with mandatory &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; review for queries touching large tables&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Backup restore is untested until an incident, platform observability requires managed service costs or ELK complexity, and SQL quality depends on reviewer knowledge that doesn’t scale with schema growth.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: databasement for multi-engine backup with automated restore verification, logtide for self-hosted observability backed by TimescaleDB or ClickHouse, slowql for SQL static analysis as a CI pipeline gate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Add &lt;code&gt;slowql analyze --path ./db/migrations --fail-on critical&lt;/code&gt; to your CI pipeline and run it against existing migration history. Count how many files trigger a rule. Any result is a pattern that code review missed and that now has an automated gate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, deploy databasement against your staging environment and run one scheduled backup with cross-server restore verification enabled. The first restore failure you catch before an incident is direct evidence of value for expanding it to production.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>architecture</category></item><item><title>When SQL Is Innocent: Concurrency, Redo, RAC, Storage and Cloud</title><link>https://rajivonai.com/blog/2025-12-17-oracle-concurrency-redo-rac-storage-cloud/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-12-17-oracle-concurrency-redo-rac-storage-cloud/</guid><description>An Oracle incident workflow for proving whether blocking, commit processing, RAC block transfer, storage, or cloud infrastructure—not SQL efficiency—caused the slowdown.</description><pubDate>Wed, 17 Dec 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A stable execution plan and stable buffer gets do not make Oracle healthy. Sessions can spend most of their time waiting for another transaction, redo durability, a remote RAC block, or an infrastructure path that no SQL rewrite can repair.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The previous article required child-cursor identity, normalized work, executed plans, actual rows, and a reproducible mechanism. Sometimes that investigation finds no meaningful change in work per execution.&lt;/p&gt;
&lt;p&gt;That result narrows the incident; it does not end it. Foreground time may have moved into Application, Commit, Concurrency, Cluster, or User I/O. Each class describes where sessions waited, but not one mechanism.&lt;/p&gt;
&lt;p&gt;Collectors preserve blocker relationships, redo counters, RAC identity, file I/O, host pressure, and cloud metrics. The LLM ranks causal paths and gaps; the DBA decides what the evidence proves.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Wait names tempt investigators into immediate prescriptions:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;enq: TX - row lock contention  → kill the blocker&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;log file sync                  → buy faster storage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;gc cr block busy               → tune the interconnect&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;db file sequential read        → add IOPS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Every arrow can be wrong. A TX wait can involve a duplicate key or bitmap fragment. &lt;code&gt;log file sync&lt;/code&gt; includes LGWR coordination and group commit. A global-cache block can await remote processing or redo flush. An access path can request more I/O from a healthy device.&lt;/p&gt;
&lt;p&gt;High cloud storage utilization or RAC traffic can also coexist without affecting DB time. Correlation needs scope, timing, rate, latency, and affected sessions.&lt;/p&gt;
&lt;p&gt;How do we prove that SQL efficiency stayed stable and identify the non-SQL mechanism without turning a wait-event label into root cause?&lt;/p&gt;
&lt;h2 id=&quot;build-a-layered-non-sql-evidence-pack&quot;&gt;Build a Layered Non-SQL Evidence Pack&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[application latency throughput errors and retries] --&gt; H[time-aligned incident evidence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B[SQL work per execution and returned rows] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[blocked sessions final blockers and transactions] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[redo rate commits log writes and wait histograms] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E[RAC block transfers objects services and interconnect] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F[file I/O latency throughput queue and errors] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G[cloud compute storage network events and throttling] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[deterministic scope and interval checks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[LLM correlation and contradiction analysis]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[DBA and platform-owner verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Require the model to separate four outputs:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;OBSERVATION  final blocker 184 held a transaction through the latency interval&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;HYPOTHESIS   application transaction scope serialized checkout updates&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;MISSING      request owner, lock resource, transaction age, and commit path&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ACTION       trace the blocker to module and application transaction boundary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Retain instance, container, service, source, counter semantics, interval, sampling rate, licensing class, and redaction status. Never present five-minute cloud averages as one-second database samples.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Evidence pattern&lt;/th&gt;&lt;th&gt;Leading path&lt;/th&gt;&lt;th&gt;What it does not prove&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Many waiters converge on one final blocker&lt;/td&gt;&lt;td&gt;Transaction serialization&lt;/td&gt;&lt;td&gt;The blocker should be killed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Commit AAS rises with commits per second&lt;/td&gt;&lt;td&gt;Commit-frequency pressure&lt;/td&gt;&lt;td&gt;Redo storage is slow&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;log file sync&lt;/code&gt; tail rises with &lt;code&gt;log file parallel write&lt;/code&gt; tail&lt;/td&gt;&lt;td&gt;Local redo-write path&lt;/td&gt;&lt;td&gt;The volume alone caused it&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cluster AAS and remote block service rise on one service&lt;/td&gt;&lt;td&gt;RAC placement or shared-block pressure&lt;/td&gt;&lt;td&gt;RAC is misconfigured&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;gc current block busy&lt;/code&gt; concentrates on one object&lt;/td&gt;&lt;td&gt;Cross-instance write contention&lt;/td&gt;&lt;td&gt;Interconnect latency is primary&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;User I/O time rises across many stable SQL children&lt;/td&gt;&lt;td&gt;Storage or cache path&lt;/td&gt;&lt;td&gt;More provisioned IOPS will help&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OCI throttled operations overlap file latency&lt;/td&gt;&lt;td&gt;Block Volume constraint&lt;/td&gt;&lt;td&gt;All database files share that volume&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Host pressure rises but DB CPU stays flat&lt;/td&gt;&lt;td&gt;Non-database or virtualization path&lt;/td&gt;&lt;td&gt;Oracle needs more CPU&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;h3 id=&quot;1-prove-that-sql-work-remained-comparable&quot;&gt;1. Prove that SQL work remained comparable&lt;/h3&gt;
&lt;p&gt;Compare child-cursor executions, rows, gets, reads, CPU, elapsed time, plans, and bind classes across baseline and incident. Stable work supports a non-SQL path, but the same plan can wait differently or encounter another cache state.&lt;/p&gt;
&lt;p&gt;Reconcile application throughput: fewer executions can mean pool waits, while retries amplify locks or commits. Scope by service, module, action, PDB, instance, and phase.&lt;/p&gt;
&lt;h3 id=&quot;2-reconstruct-the-blocking-graph&quot;&gt;2. Reconstruct the blocking graph&lt;/h3&gt;
&lt;p&gt;Capture &lt;code&gt;GV$SESSION&lt;/code&gt; blocker status, blocking and final-blocking identity, event, transaction, current and previous SQL, service, module, action, and container. &lt;code&gt;V$SESSION_BLOCKERS&lt;/code&gt; emits pairs and can report multiple blockers, up to 30 per blocked session.&lt;/p&gt;
&lt;p&gt;The final blocker’s current SQL may be unrelated or idle after acquiring the lock. Connect its transaction, previous SQL, request, age, and commit behavior. Distinguish row modification, unique-key coordination, and bitmap-fragment contention before changing code.&lt;/p&gt;
&lt;p&gt;Session termination is containment with consistency risk. It removes a holder, not the transaction design.&lt;/p&gt;
&lt;h3 id=&quot;3-decompose-foreground-commit-time-from-redo-work&quot;&gt;3. Decompose foreground commit time from redo work&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;log file sync&lt;/code&gt; covers commit redo flush and LGWR’s completion post. &lt;code&gt;log file parallel write&lt;/code&gt; measures LGWR I/O until the last parallel write finishes. Compare deltas and histograms with commits, redo size and writes, log switches, archive pressure, synchronous transport, and throughput.&lt;/p&gt;
&lt;p&gt;Group commit breaks one-to-one reasoning: one slow redo write can release many sessions. Preserve histogram deltas instead of relying on averages.&lt;/p&gt;
&lt;p&gt;Changed commit frequency with stable redo-write latency favors transaction granularity. Aligned redo-write tails, device latency, throttling, and commit latency favor the write path. Asynchronous commit changes durability semantics and requires explicit business approval.&lt;/p&gt;
&lt;h3 id=&quot;4-separate-rac-transfer-contention-and-network-paths&quot;&gt;4. Separate RAC transfer, contention, and network paths&lt;/h3&gt;
&lt;p&gt;Analyze RAC per instance and service. Compare Cluster AAS, blocks received, transfer outcomes, busy and congested events, interconnect traffic, node CPU, and objects behind file and block parameters.&lt;/p&gt;
&lt;p&gt;Cache Fusion uses the private interconnect. A completed &lt;code&gt;gc cr block 2-way&lt;/code&gt; is not automatically faulty. Busy events mean the serving instance could not ship immediately; remote pinning, redo flush, or concurrency can contribute. Lost-block events more directly implicate loss, congestion, hardware, overload, or network path.&lt;/p&gt;
&lt;p&gt;Ongoing requests are placeholders renamed after the outcome. Do not compare them directly with completed-event totals. Test hot objects, service placement, cross-instance writes, and node load before changing GCS processes or network configuration.&lt;/p&gt;
&lt;h3 id=&quot;5-join-oracle-io-with-host-and-cloud-evidence&quot;&gt;5. Join Oracle I/O with host and cloud evidence&lt;/h3&gt;
&lt;p&gt;Separate foreground User I/O from background System I/O. Correlate files, operations, and latency with OS queues, errors, throughput, multipathing, ASM or Exadata, backups, and the exact cloud attachment.&lt;/p&gt;
&lt;p&gt;OCI database metrics expose instance IOPS and throughput. Block Volume adds operations, throughput, guaranteed performance, and &lt;code&gt;VolumeThrottledIOs&lt;/code&gt; by attachment or resource. Different intervals and scopes matter: a five-minute mean hides short stalls, and a busy volume may host unrelated files.&lt;/p&gt;
&lt;p&gt;On Exadata, include cell and storage-server evidence. Across clouds, preserve service events, maintenance, scaling, and topology changes; provider metrics complement Oracle file and wait evidence.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[latency rises with stable SQL efficiency] --&gt; B{Application waits dominate}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| C[build blocker graph and transaction timeline]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| D{Commit waits dominate}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes| E[compare commit demand redo writes tails and transport]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no| F{Cluster waits dominate}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|yes| G[segment instances services blocks and outcomes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|no| H{I/O waits dominate}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| I[join file device and cloud evidence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| J[inspect concurrency configuration network and scheduler]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; K[test application transaction mechanism]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; L[smallest reversible response and validation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Oracle’s &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/classes-of-wait-events.html&quot;&gt;wait-class reference&lt;/a&gt; separates Application, Commit, Cluster, Concurrency, System I/O, and User I/O. Its &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/descriptions-of-wait-events.html&quot;&gt;&lt;code&gt;enq: TX - row lock contention&lt;/code&gt;&lt;/a&gt; documentation lists row modification, unique-key coordination, and shared bitmap-index fragments as possible causes. The documented behavior requires resource and transaction evidence, not a generic locking diagnosis.&lt;/p&gt;
&lt;p&gt;Oracle defines &lt;code&gt;log file sync&lt;/code&gt; as foreground commit coordination with LGWR and &lt;code&gt;log file parallel write&lt;/code&gt; as redo-log I/O completion. Its &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/haovw/redo-transport-troubleshooting-and-tuning.html&quot;&gt;redo transport troubleshooting guidance&lt;/a&gt; shows why group commit and outliers can make average foreground wait time misleading. The derived operational rule is to correlate histograms, redo writes, transaction rate, and application impact.&lt;/p&gt;
&lt;p&gt;Oracle’s &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/racad/monitoring-performance.html&quot;&gt;RAC performance guidance&lt;/a&gt; states that many high-total global-cache waits are normal and distinguishes block, message, contention, and load events. Its &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/adrac/rac_db_instance.html&quot;&gt;RAC instance architecture&lt;/a&gt; describes Cache Fusion as block transfer through the private interconnect. This supports object, service, instance, and outcome analysis before an interconnect conclusion.&lt;/p&gt;
&lt;p&gt;OCI’s &lt;a href=&quot;https://docs.oracle.com/en-us/iaas/database-management/doc/oracle-cloud-database-metrics.html&quot;&gt;database metrics&lt;/a&gt; include instance dimensions for IOPS, throughput, RAC block receipt, interconnect traffic, DB time, and wait time under their documented management options. &lt;a href=&quot;https://docs.oracle.com/en-us/iaas/Content/Block/References/volumemetrics-reference.htm&quot;&gt;Block Volume metrics&lt;/a&gt; separately expose operations, throughput, guaranteed performance, and throttling. Their different namespaces and intervals make metric lineage mandatory.&lt;/p&gt;
&lt;p&gt;AWR, ASH, ADDM, Performance Hub, and historical views require Diagnostics Pack; SQL Monitoring requires Tuning Pack and Diagnostics Pack. Confirm entitlement. Dynamic views, OS telemetry, and cloud metrics require least-privilege access and data handling controls.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven mechanism&lt;/th&gt;&lt;th&gt;Candidate response&lt;/th&gt;&lt;th&gt;Validation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Application transaction holds rows too long&lt;/td&gt;&lt;td&gt;Shorten transaction scope or correct access order&lt;/td&gt;&lt;td&gt;Blocking time and request latency fall&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Duplicate-key coordination creates TX waits&lt;/td&gt;&lt;td&gt;Redesign key allocation or idempotency path&lt;/td&gt;&lt;td&gt;Waiters fall without duplicate writes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Commit frequency drives healthy redo path hard&lt;/td&gt;&lt;td&gt;Batch commits only where semantics permit&lt;/td&gt;&lt;td&gt;Commits fall while durability and throughput hold&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Redo write path has verified tail latency&lt;/td&gt;&lt;td&gt;Repair storage, transport, or log placement&lt;/td&gt;&lt;td&gt;Both redo-write and commit tails recover&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;RAC service placement causes hot-block transfer&lt;/td&gt;&lt;td&gt;Rebalance service or partition ownership&lt;/td&gt;&lt;td&gt;Remote block demand falls without node overload&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Interconnect loss or congestion is proven&lt;/td&gt;&lt;td&gt;Repair private-network path or capacity&lt;/td&gt;&lt;td&gt;Lost events and transfer tails recover&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;File latency aligns with cloud throttling&lt;/td&gt;&lt;td&gt;Raise approved performance or move the path&lt;/td&gt;&lt;td&gt;File latency and throttled operations fall&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup or maintenance overlaps foreground I/O&lt;/td&gt;&lt;td&gt;Reschedule or rate-limit the work&lt;/td&gt;&lt;td&gt;Foreground latency improves in repeated window&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;Define a reversal for every intervention: restore service placement, transaction batch size, log or storage configuration, volume performance, job schedule, network policy, or application release. Record previous values and ownership before change. Capacity changes may be reversible technically but still alter cost commitments.&lt;/p&gt;
&lt;p&gt;Do not use session termination, instance restart, cache flush, or RAC relocation as an unlabelled diagnostic test. If emergency containment requires one, preserve evidence first, define application consistency checks, and distinguish recovery from root-cause remediation.&lt;/p&gt;
&lt;p&gt;Stop when errors, recovery risk, commit durability, cross-instance imbalance, queue depth, or application latency worsens. After rollback, verify the active configuration and repeat the database, host, and cloud measurements; a successful API response is not proof that the data path reverted.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why the conclusion fails&lt;/th&gt;&lt;th&gt;Better evidence&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Blocker is killed and called root cause&lt;/td&gt;&lt;td&gt;Holder removal does not explain transaction design&lt;/td&gt;&lt;td&gt;Wait chain, request ownership, and lock resource&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;log file sync&lt;/code&gt; average drives storage purchase&lt;/td&gt;&lt;td&gt;Group commit and outliers distort averages&lt;/td&gt;&lt;td&gt;Histogram deltas, LGWR I/O, commits, throughput&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High RAC transfer rate is called contention&lt;/td&gt;&lt;td&gt;Useful work can transfer many blocks normally&lt;/td&gt;&lt;td&gt;Response time, busy outcomes, objects, baseline&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cluster wait is blamed on network&lt;/td&gt;&lt;td&gt;Remote CPU, pinning, redo flush, and hot blocks contribute&lt;/td&gt;&lt;td&gt;Per-instance outcome and infrastructure evidence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;User I/O means cloud storage fault&lt;/td&gt;&lt;td&gt;SQL demand and cache state affect foreground reads&lt;/td&gt;&lt;td&gt;Stable work plus file and device latency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cloud mean is aligned to a short spike&lt;/td&gt;&lt;td&gt;Aggregation dilutes or shifts the event&lt;/td&gt;&lt;td&gt;Raw interval metadata and phase overlap&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LLM receives object and bind details&lt;/td&gt;&lt;td&gt;Diagnostic data can expose sensitive design or values&lt;/td&gt;&lt;td&gt;Restricted original and sanitized evidence pack&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Customer Data:&lt;/strong&gt; The LLM must not receive bind capture values from &lt;code&gt;V$SQL_BIND_CAPTURE&lt;/code&gt;, literal SQL text, or row data. Send &lt;code&gt;SQL_ID&lt;/code&gt;, force-matching signature, plan hash values, wait-event timings, and DB Time attribution only.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Production Execution:&lt;/strong&gt; The LLM cannot run &lt;code&gt;ALTER SYSTEM&lt;/code&gt;, &lt;code&gt;ALTER SESSION&lt;/code&gt; on other sessions, kill sessions, apply SQL profiles or baselines, or gather statistics. It proposes; a human authorizes, applies, validates, and holds the rollback condition. Note also that AWR and ASH access carries Diagnostics Pack licensing implications — confirm entitlement before any collector reads them.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Causal Conclusion Without Contradicting Evidence:&lt;/strong&gt; Every hypothesis must cite both the telemetry that supports it and the telemetry that would falsify it. A ranked hypothesis with no disconfirming test attached is an assertion, not a diagnosis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Silent Gap-Filling:&lt;/strong&gt; Where the evidence pack lacks a required signal, the LLM must name it explicitly under missing evidence rather than inferring a plausible value. “Not collected” and “collected and normal” are different findings and must never be merged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Change Without a Human Gate:&lt;/strong&gt; Production changes require human authorization, a stated validation signal, and a defined rollback condition agreed before the change is applied.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Stable SQL work can coexist with severe Oracle latency when sessions serialize on transactions, durability, remote blocks, storage, or infrastructure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Preserve blocker graphs, redo distributions, RAC outcomes, file-level I/O, and cloud lineage in one time-aligned evidence pack while keeping each causal path distinct.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Show that the selected mechanism explains affected sessions, incident timing, scope, and application impact, then demonstrate recovery after the smallest reversible intervention.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Add blocker-pair, commit-histogram, RAC-instance, file-attachment, cloud-namespace, interval, and contradiction fields to the Oracle collector before asking an LLM for remediation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The next Oracle article defines diagnostic access, redaction, licensing, audit, and production-action boundaries.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/classes-of-wait-events.html&quot;&gt;Oracle AI Database 26ai — Classes of wait events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/descriptions-of-wait-events.html&quot;&gt;Oracle AI Database 26ai — Descriptions of wait events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/V-SESSION.html&quot;&gt;Oracle AI Database 26ai — V$SESSION&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/V-SESSION_BLOCKERS.html&quot;&gt;Oracle AI Database 26ai — V$SESSION_BLOCKERS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/admin/managing-transactions.html&quot;&gt;Oracle AI Database 26ai — Managing transactions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/haovw/redo-transport-troubleshooting-and-tuning.html&quot;&gt;Oracle AI Database 26ai — Redo transport troubleshooting and tuning&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/racad/monitoring-performance.html&quot;&gt;Oracle AI Database 26ai — Monitoring RAC performance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/adrac/rac_db_instance.html&quot;&gt;Oracle AI Database 26ai — Oracle RAC database instance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en-us/iaas/database-management/doc/oracle-cloud-database-metrics.html&quot;&gt;Oracle Cloud Infrastructure — Oracle Cloud Database metrics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en-us/iaas/Content/Block/References/volumemetrics-reference.htm&quot;&gt;Oracle Cloud Infrastructure — Block Volume metrics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/dblic/Licensing-Information.html&quot;&gt;Oracle AI Database 26ai — Licensing information&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>ai-engineering</category><category>failures</category></item><item><title>Don&apos;t Start with Microservices: Decompose the Database Write Path First</title><link>https://rajivonai.com/blog/2025-12-16-decompose-database-write-path-before-microservices/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-12-16-decompose-database-write-path-before-microservices/</guid><description>A phased architecture for moving a growing retail platform from one shared database transaction boundary to domain-owned write paths without beginning with a service rewrite.</description><pubDate>Tue, 16 Dec 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A monolith does not reach its write ceiling because it has too few services. It reaches the ceiling because too many business domains still require one database to serialize their writes, enforce their constraints, and absorb every failure.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;RetailCo began with one application and one relational database. That was the correct architecture when the company was small. Checkout could create an order, decrement inventory, record a payment attempt, and schedule fulfillment inside one transaction. Foreign keys caught broken references. Unique constraints stopped obvious duplicates. One backup contained the operational state of the company.&lt;/p&gt;
&lt;p&gt;RetailCo is a reference architecture, not a claimed customer deployment. Its brands, tenant count, workload, domain boundaries, and migration below are illustrative until replaced by an observed system inventory and lab evidence.&lt;/p&gt;
&lt;p&gt;Growth changed the workload without changing the ownership model. Four brands now share the same platform. Thousands of stores and tenants create orders, reserve inventory, capture payments, update shipments, and maintain customer profiles. Read replicas absorbed reporting and customer-history traffic, but every mutation still converges on one writer.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Web[web and mobile clients] --&gt; App[RetailCo monolith]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Stores[store systems] --&gt; App&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Jobs[batch and support jobs] --&gt; App&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App --&gt; DB[(shared relational writer)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  DB --&gt; Customer[customer tables]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  DB --&gt; Orders[order tables]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  DB --&gt; Inventory[inventory tables]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  DB --&gt; Payment[payment tables]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  DB --&gt; Shipping[shipping tables]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  DB --&gt; Replicas[read replicas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The database is not merely large. It is the meeting point for unrelated write pressure:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a promotion produces a burst of order inserts;&lt;/li&gt;
&lt;li&gt;warehouse reconciliation updates inventory reservations;&lt;/li&gt;
&lt;li&gt;a payment provider sends delayed callbacks;&lt;/li&gt;
&lt;li&gt;shipment tracking produces frequent status changes;&lt;/li&gt;
&lt;li&gt;customer and support tools correct operational data;&lt;/li&gt;
&lt;li&gt;schema migrations and backfills compete with all of the above.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;a href=&quot;https://rajivonai.com/blog/2024-01-23-why-reads-scale-more-easily-than-writes/&quot;&gt;single-writer ceiling&lt;/a&gt; explains why replicas do not remove this bottleneck. Sharding can distribute tenants, as the &lt;a href=&quot;https://rajivonai.com/blog/2024-05-21-scaling-postgresql-writes-citus-ec2-retailco/&quot;&gt;Citus&lt;/a&gt; and &lt;a href=&quot;https://rajivonai.com/blog/2025-03-18-sharded-innodb-clusters-mysql-write-scaling/&quot;&gt;MySQL&lt;/a&gt; designs show. But sharding the whole schema preserves the same domain coupling inside every shard. RetailCo still cannot scale or recover payments independently from inventory, and every tenant move carries all domains together.&lt;/p&gt;
&lt;p&gt;The architectural question is therefore not “How many microservices should RetailCo create?” It is &lt;strong&gt;which business facts must remain in one ACID boundary, which can become separate write domains, and how can ownership move without corrupting live orders?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Splitting the application first creates an attractive diagram and a dangerous database.&lt;/p&gt;
&lt;p&gt;Suppose RetailCo deploys separate Order, Inventory, Payment, and Shipping services while all four retain credentials to the shared schema. The deployment units changed, but the write topology did not. A Payment service release can still alter an order column. Inventory still joins customer and catalog tables. A transaction opened by one service can block another. One database incident still stops the company.&lt;/p&gt;
&lt;p&gt;That is a distributed application around a centralized ownership model. It adds network failure, versioned APIs, more deployment pipelines, and harder tracing without adding another independent write domain.&lt;/p&gt;
&lt;p&gt;The opposite shortcut is equally dangerous: give every proposed service a database immediately. The old checkout transaction then becomes four local commits:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;create order&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;reserve inventory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;authorize payment&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;request fulfillment&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is no atomic commit across those databases. Inventory can be reserved after payment is declined. Payment can succeed while the order response times out. A retry can execute a step twice. A customer can cancel while fulfillment is already moving forward. The database constraints that once made these states impossible no longer cross the new boundary.&lt;/p&gt;
&lt;p&gt;Three distinctions decide whether decomposition improves the system.&lt;/p&gt;
&lt;h3 id=&quot;a-table-boundary-is-not-necessarily-a-business-boundary&quot;&gt;A table boundary is not necessarily a business boundary&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;orders&lt;/code&gt;, &lt;code&gt;order_items&lt;/code&gt;, and &lt;code&gt;order_status_history&lt;/code&gt; probably belong together. A payment attempt references an order, but its core invariants concern authorization, capture, refund, provider idempotency, and ledger state. Inventory concerns stock positions and reservations. A foreign key shows a relationship; it does not prove that both sides need one owner or one scaling policy.&lt;/p&gt;
&lt;h3 id=&quot;data-ownership-means-one-writer&quot;&gt;Data ownership means one writer&lt;/h3&gt;
&lt;p&gt;An ownership label in a diagram is insufficient if old code, support scripts, and batch jobs can still update the table. During the transition RetailCo needs a source-of-truth matrix and database privileges that answer two questions for every business fact:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Which component is authorized to change it?&lt;/li&gt;
&lt;li&gt;Which database is authoritative in this migration phase?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Multiple readers are normal. Multiple uncoordinated writers are a correctness defect.&lt;/p&gt;
&lt;h3 id=&quot;compensation-is-not-rollback&quot;&gt;Compensation is not rollback&lt;/h3&gt;
&lt;p&gt;A relational rollback removes uncommitted changes. A distributed compensation creates new committed business facts. Releasing an inventory reservation does not erase the reservation history. Refunding a captured payment does not make the capture disappear. It creates a refund that can itself fail, retry, or require review.&lt;/p&gt;
&lt;p&gt;AWS describes a saga as a sequence of local transactions with forward recovery and compensating transactions when later steps fail (&lt;a href=&quot;https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/saga-patterns.html&quot;&gt;saga guidance&lt;/a&gt;). That is a useful workflow model, but it is not a substitute for deciding where strong consistency is mandatory.&lt;/p&gt;
&lt;h2 id=&quot;decompose-ownership-before-deployments&quot;&gt;Decompose Ownership Before Deployments&lt;/h2&gt;
&lt;p&gt;The recommended target is a &lt;strong&gt;modular monolith with selected domain-owned databases&lt;/strong&gt;, not an immediate fleet of services. RetailCo keeps one deployable application while creating enforceable module boundaries, separate connection pools, and one writer for each extracted domain.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Clients[RetailCo clients] --&gt; App[modular monolith — one deployment]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App --&gt; OrderModule[order module — command owner]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App --&gt; InventoryModule[inventory module — command owner]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App --&gt; PaymentModule[payment module — command owner]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App --&gt; ShippingModule[shipping module — command owner]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  OrderModule --&gt; OrderDB[(orders database)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  InventoryModule --&gt; InventoryDB[(inventory database)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  PaymentModule --&gt; PaymentDB[(payments database)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ShippingModule --&gt; ShippingDB[(shipping database)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  OrderDB --&gt; Events[event transport]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  InventoryDB --&gt; Events&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  PaymentDB --&gt; Events&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ShippingDB --&gt; Events&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Events --&gt; Views[local projections and read models]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This creates four independent write domains without requiring four independent runtime deployments on day one. Inventory can scale on its own database. Payment recovery can have a smaller security and blast-radius boundary. Order schema changes no longer require every domain to share the same migration window. If a module later needs independent deployment, its persistence contract already exists.&lt;/p&gt;
&lt;p&gt;The separation should be selective. Customer preferences may not justify another database. Product catalog data may remain a replicated read model. Order and order-item state should stay together if nearly every command changes both. The goal is not database-per-table or database-per-class. The goal is a small number of cohesive write domains.&lt;/p&gt;
&lt;h3 id=&quot;start-with-an-actor-and-invariant-map&quot;&gt;Start with an actor and invariant map&lt;/h3&gt;
&lt;p&gt;Before moving data, build an inventory from database evidence rather than architecture memory:&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Artifact&lt;/th&gt;&lt;th&gt;Question it answers&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Actor-to-table CRUD matrix&lt;/td&gt;&lt;td&gt;Which applications, jobs, users, and integrations read or write each table?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Transaction trace&lt;/td&gt;&lt;td&gt;Which statements actually commit together under production-shaped workflows?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Constraint inventory&lt;/td&gt;&lt;td&gt;Which foreign keys, unique indexes, checks, triggers, and procedures enforce correctness?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Query and join map&lt;/td&gt;&lt;td&gt;Which data must be read together, at what latency and freshness?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lock and resource profile&lt;/td&gt;&lt;td&gt;Which domain creates CPU, WAL or binlog, I/O, lock, and connection pressure?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Recovery inventory&lt;/td&gt;&lt;td&gt;Which facts must be restored together to preserve a valid business state?&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;AWS’s current database-decomposition guidance similarly begins with actor analysis, CRUD activity analysis, dependency mapping, and consistency requirements (&lt;a href=&quot;https://docs.aws.amazon.com/prescriptive-guidance/latest/database-decomposition/scope.html&quot;&gt;scope guidance&lt;/a&gt;). Schema shape alone cannot reveal a nightly writer, a support tool, or a business invariant implemented in application code.&lt;/p&gt;
&lt;p&gt;For RetailCo, a first ownership matrix might look like this:&lt;/p&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Domain&lt;/th&gt;&lt;th&gt;Authoritative facts&lt;/th&gt;&lt;th&gt;Local atomic boundary&lt;/th&gt;&lt;th&gt;Cross-domain references&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Order&lt;/td&gt;&lt;td&gt;order identity, items, totals, order state&lt;/td&gt;&lt;td&gt;Create order and items; legal order transition&lt;/td&gt;&lt;td&gt;customer ID, reservation ID, payment ID&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Inventory&lt;/td&gt;&lt;td&gt;stock position, reservation, release&lt;/td&gt;&lt;td&gt;Reserve or release units for one stock policy&lt;/td&gt;&lt;td&gt;order ID, product ID, location ID&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Payment&lt;/td&gt;&lt;td&gt;intent, attempt, authorization, capture, refund&lt;/td&gt;&lt;td&gt;Persist one idempotent money transition&lt;/td&gt;&lt;td&gt;order ID, customer payment reference&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Shipping&lt;/td&gt;&lt;td&gt;fulfillment request, package, tracking state&lt;/td&gt;&lt;td&gt;Create and advance one fulfillment&lt;/td&gt;&lt;td&gt;order ID, address snapshot&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Customer&lt;/td&gt;&lt;td&gt;identity, preferences, consent&lt;/td&gt;&lt;td&gt;Change customer-owned attributes&lt;/td&gt;&lt;td&gt;stable customer ID&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The word &lt;em&gt;snapshot&lt;/em&gt; matters. Shipping should not call Customer during every label operation to rediscover the address used when the order was accepted. The order or fulfillment boundary should store the address version required for that business fact, subject to the company’s privacy and correction rules.&lt;/p&gt;
&lt;h3 id=&quot;enforce-ownership-inside-the-monolith&quot;&gt;Enforce ownership inside the monolith&lt;/h3&gt;
&lt;p&gt;Create one public command interface per domain. Other modules may call &lt;code&gt;reserve_inventory&lt;/code&gt;, but they cannot update inventory tables directly. Give the module a database role that can write its schema, and remove those grants from general application, batch, and support roles. Log rejected legacy access so hidden writers become visible.&lt;/p&gt;
&lt;p&gt;The code may still run in one process, but the boundary is real because:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;only the owner can issue mutations;&lt;/li&gt;
&lt;li&gt;callers use commands with idempotency keys;&lt;/li&gt;
&lt;li&gt;local constraints remain inside the owning database;&lt;/li&gt;
&lt;li&gt;changes leave through a versioned event contract;&lt;/li&gt;
&lt;li&gt;read copies have declared freshness and provenance.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Shopify publicly documented choosing a modular monolith to strengthen domain boundaries without multiplying deployment units (&lt;a href=&quot;https://shopify.engineering/deconstructing-monolith-designing-software-maximizes-developer-productivity?page=2&quot;&gt;Deconstructing the Monolith&lt;/a&gt;). Its later account describes the practical value of explicit stewardship while also acknowledging that strong boundaries in legacy code take sustained work (&lt;a href=&quot;https://shopify.engineering/shopify-monolith&quot;&gt;Under Deconstruction&lt;/a&gt;). The relevant lesson is not to copy Shopify’s code structure. It is that deployment topology and domain modularity can change on different schedules.&lt;/p&gt;
&lt;h3 id=&quot;use-a-local-transaction-and-an-outbox&quot;&gt;Use a local transaction and an outbox&lt;/h3&gt;
&lt;p&gt;When Order accepts a command, it must not commit the order and then make a best-effort publish. A process crash between those actions creates a valid order that Inventory never sees. Publishing first creates the opposite failure: downstream work for an order that rolled back.&lt;/p&gt;
&lt;p&gt;Write the domain change and an outbox record in the same local transaction:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id, tenant_id, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, state_version, total_amount&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    :order_id, :tenant_id, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;pending_inventory&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, :total_amount&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; order_outbox (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    event_id, aggregate_type, aggregate_id, event_type,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    aggregate_version, payload, occurred_at&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    :event_id, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;order&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, :order_id, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;OrderPlaced&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, :payload, CURRENT_TIMESTAMP&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;COMMIT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;An outbox relay or CDC connector publishes committed outbox rows. AWS documents the pattern as a way to avoid the inconsistent dual write between a database and a message broker, while warning that consumers must handle duplicates and ordering (&lt;a href=&quot;https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/transactional-outbox.html&quot;&gt;transactional outbox guidance&lt;/a&gt;). Debezium’s outbox router emits an event ID that consumers can use for deduplication and uses the aggregate ID as the message key, which helps preserve aggregate order within a partition (&lt;a href=&quot;https://debezium.io/documentation/reference/stable/transformations/outbox-event-router.html&quot;&gt;Debezium outbox router&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;The guarantee is intentionally narrow:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The order state and the intent to publish &lt;code&gt;OrderPlaced&lt;/code&gt; commit together.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It does not guarantee that the broker, every consumer, and every consumer database commit exactly once. Debezium documents that recovery can re-emit changes after a fault; consumers should expect duplicates and use source metadata or event identity to converge (&lt;a href=&quot;https://debezium.io/documentation/reference/stable/connectors/postgresql.html&quot;&gt;PostgreSQL connector failure behavior&lt;/a&gt;). Every handler therefore needs a processed-event record or a business idempotency constraint.&lt;/p&gt;
&lt;h3 id=&quot;turn-checkout-into-an-explicit-workflow&quot;&gt;Turn checkout into an explicit workflow&lt;/h3&gt;
&lt;p&gt;Once Order, Inventory, and Payment have separate databases, checkout becomes a persisted state machine. The order is not immediately &lt;code&gt;complete&lt;/code&gt;; it moves through states that expose uncertainty.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Start[checkout command — idempotency key] --&gt; OrderTx[order transaction — pending inventory and outbox]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  OrderTx --&gt; Reserve[inventory command — reserve once]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reserve --&gt;|reserved| Payment[payment command — authorize once]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reserve --&gt;|rejected| Reject[order transition — rejected]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Payment --&gt;|authorized| Confirm[order transition — confirmed]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Payment --&gt;|declined| Release[inventory command — release once]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Payment --&gt;|unknown| Review[order transition — payment uncertain]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Release --&gt; Reject&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Confirm --&gt; Fulfill[shipping command — create fulfillment]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Review --&gt; Reconcile[payment reconciliation — query provider facts]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each arrow is a durable command or event, not an in-memory callback. Each local database records its command identity and result. The workflow knows which steps completed, which can retry, and which require compensation or manual review.&lt;/p&gt;
&lt;p&gt;For the first extraction, central orchestration is usually easier to operate than a long chain of anonymous event reactions. An order workflow record can contain the current step, version, deadline, last command ID, retry policy, and failure class. Choreography may remain useful for non-critical reactions such as analytics or notification, but the money-and-inventory path benefits from one visible coordinator.&lt;/p&gt;
&lt;p&gt;Do not automatically compensate every timeout. A timeout after payment authorization is an ambiguous outcome, not proof of failure. The workflow should reconcile by idempotency key or provider transaction ID before retrying or releasing inventory. This is why &lt;code&gt;payment_uncertain&lt;/code&gt; is a real state, not an exception to hide.&lt;/p&gt;
&lt;h3 id=&quot;replace-cross-database-joins-deliberately&quot;&gt;Replace cross-database joins deliberately&lt;/h3&gt;
&lt;p&gt;Separating writers does not eliminate cross-domain reads. The order-history page still needs fulfillment and payment summaries. Store operations need product, price, and stock. Support needs a coherent customer timeline.&lt;/p&gt;
&lt;p&gt;Choose the read path by freshness and failure semantics:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Request[read request] --&gt; Choice{freshness and fanout requirement}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Choice --&gt;|fresh owner fact| API[owner query or API composition]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Choice --&gt;|fast operational view| Projection[local projection — event maintained]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Choice --&gt;|wide historical analysis| Warehouse[warehouse or lakehouse]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  API --&gt; Response[response with partial failure policy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Projection --&gt; Response&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Warehouse --&gt; Analytics[analytical result]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Use owner queries or API composition when current data matters and the number of dependencies is bounded.&lt;/li&gt;
&lt;li&gt;Use a local projection for high-volume screens that tolerate named lag.&lt;/li&gt;
&lt;li&gt;Use a warehouse or search index for broad analytical access.&lt;/li&gt;
&lt;li&gt;Store stable identifiers across domains, but do not pretend they are cross-database foreign keys.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;AWS’s decomposition guidance lists denormalization, reference-by-key, CQRS-style read models, and event-based synchronization as alternatives to direct cross-database joins (&lt;a href=&quot;https://docs.aws.amazon.com/prescriptive-guidance/latest/database-decomposition/joins.html&quot;&gt;decoupling table relationships&lt;/a&gt;). Every copied field needs an owner, update contract, freshness objective, rebuild path, and behavior when its feed is unavailable.&lt;/p&gt;
&lt;h3 id=&quot;move-one-domain-through-a-controlled-ownership-state-machine&quot;&gt;Move one domain through a controlled ownership state machine&lt;/h3&gt;
&lt;p&gt;RetailCo should extract one high-value domain, not all five at once. Inventory is a plausible first candidate when it causes independent contention and has a clear reservation API. Payment may deserve earlier isolation when security and blast radius dominate. The actual choice comes from the actor, invariant, lock, and recovery maps.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Map[map actors writes constraints and invariants] --&gt; Owner[route all legacy mutations through owner module]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Owner --&gt; Enforce[remove direct database write grants]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Enforce --&gt; Target[create target database and schema]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Target --&gt; Backfill[backfill in bounded restartable chunks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Backfill --&gt; CDC[apply committed changes through CDC]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  CDC --&gt; Compare[shadow reads and reconcile business invariants]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Compare --&gt; Gate{cutover gates pass}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Gate --&gt;|no| Repair[repair mapping lag or data]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Repair --&gt; Compare&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Gate --&gt;|yes| Fence[fence old-domain writes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Fence --&gt; Drain[drain CDC and record final source position]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Drain --&gt; Flip[make target the authoritative writer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Flip --&gt; Remove[remove legacy reads writes and schema]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The target is only a candidate copy until the ownership flip. Before target-only writes, RetailCo can route back to the source if the source remains authoritative. After target-only writes, “rollback” requires proven reverse propagation or another migration. Redeploying the old application is not data rollback.&lt;/p&gt;
&lt;p&gt;This workflow intentionally reuses the repository’s detailed &lt;a href=&quot;https://rajivonai.com/blog/2024-07-29-database-migration-cutover-workflow-dual-writes-cdc-backfill-freeze-and-rollback/&quot;&gt;database cutover protocol&lt;/a&gt;. The additional rule for decomposition is that cutover must move &lt;strong&gt;command ownership&lt;/strong&gt;, not only rows. The old module, scheduled jobs, support scripts, and direct credentials all lose mutation authority together.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;This article describes a reference architecture, not a claimed RetailCo production result. The RetailCo workload, table boundaries, and migration sequence are proposed. No throughput gain, migration duration, event-lag objective, defect rate, RPO, or RTO is presented as measured evidence.&lt;/p&gt;
&lt;h3 id=&quot;what-the-documented-patterns-establish&quot;&gt;What the documented patterns establish&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; Shopify chose a modular monolith because it wanted stronger domain boundaries without immediately taking on the network, deployment, and data-access costs of many services. Its follow-up described a long-running effort in which ownership became useful before every boundary became strong.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; RetailCo first assigns modules and data owners inside one deployment. Database roles and public command interfaces turn those labels into controls. Independent deployment becomes an option after persistence boundaries and operational ownership work.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The derived architectural result is that write ownership can be separated from service deployment. RetailCo can create an independently scalable inventory database without also requiring a separately deployed Inventory service in the same release.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; Microservices are one possible destination, not the first unit of decomposition.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; AWS’s transactional-outbox guidance documents the failure in updating a database and publishing a notification as two separate actions. Debezium documents how a connector can capture an outbox table and route messages by aggregate, and that fault recovery can produce duplicates.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Commit each domain mutation with its outbox row, publish from the commit log, key messages by aggregate, and make consumers idempotent by event ID and business command ID.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The derived guarantee is atomic local intent and replayable propagation. A connector outage delays downstream work instead of losing the committed intent; duplicate delivery converges instead of repeating the business action.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; Outbox and CDC close one dual-write gap. They do not create an end-to-end exactly-once transaction.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; PostgreSQL logical replication identifies changed rows through replica identity, normally a primary key. Current documentation states that published &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; operations require a suitable replica identity; DDL is a separate concern (&lt;a href=&quot;https://www.postgresql.org/docs/current/logical-replication-publication.html&quot;&gt;PostgreSQL publication documentation&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Before relying on PostgreSQL CDC for an extraction, inventory keys, publications, schema changes, WAL retention, replication slots, connector offsets, and target apply behavior. MySQL-based migrations need the equivalent binary-log and primary-key review.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The migration treats CDC as an operated data product with lag, retention, schema, replay, and recovery constraints—not as an invisible pipe.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; A decomposition plan that says only “use CDC” has not specified the safety mechanism.&lt;/p&gt;
&lt;h3 id=&quot;proposed-lab--no-empirical-results&quot;&gt;Proposed lab — no empirical results&lt;/h3&gt;
&lt;p&gt;Use the RetailCo order, inventory, and payment schema to compare the current shared transaction with the first extracted-domain design.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Baseline&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;one application deployment;&lt;/li&gt;
&lt;li&gt;one relational writer;&lt;/li&gt;
&lt;li&gt;local order, inventory, and payment tables;&lt;/li&gt;
&lt;li&gt;checkout transaction boundaries captured from the existing implementation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Candidate&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;one modular application deployment;&lt;/li&gt;
&lt;li&gt;Order and Inventory on separate databases;&lt;/li&gt;
&lt;li&gt;local outbox in each write domain;&lt;/li&gt;
&lt;li&gt;CDC-backed event transport;&lt;/li&gt;
&lt;li&gt;persisted order workflow;&lt;/li&gt;
&lt;li&gt;idempotent inventory reservation and release commands;&lt;/li&gt;
&lt;li&gt;local order-history projection.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Capture request throughput, commit latency, P50, P95, P99, database CPU, I/O, WAL or binlog rate, lock waits, connection utilization, outbox age, CDC lag, command retries, duplicate deliveries, reconciliation differences, and workflow age by state. Report shared-writer and candidate results separately. Do not attribute an improvement to decomposition unless the same workload, dataset, durability, and failure policy were used.&lt;/p&gt;
&lt;p&gt;The failure lab matters more than a clean throughput chart:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;stop the CDC connector while orders continue;&lt;/li&gt;
&lt;li&gt;redeliver an event and prove the consumer converges;&lt;/li&gt;
&lt;li&gt;deliver an older aggregate version after a newer one;&lt;/li&gt;
&lt;li&gt;time out payment after the provider accepted authorization;&lt;/li&gt;
&lt;li&gt;make the Inventory database unavailable before and after reservation;&lt;/li&gt;
&lt;li&gt;attempt a direct legacy write with revoked credentials;&lt;/li&gt;
&lt;li&gt;pause the target during backfill and resume from a checkpoint;&lt;/li&gt;
&lt;li&gt;change a source schema incompatibly while CDC is running;&lt;/li&gt;
&lt;li&gt;restore one domain database and reconcile event positions and projections;&lt;/li&gt;
&lt;li&gt;cut ownership over, then prove the documented rollback boundary.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Archive commands, configuration, source positions, event IDs, reconciliation output, and recovery timelines before replacing &lt;code&gt;PROPOSED LAB&lt;/code&gt; with observed conclusions.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;What actually broke&lt;/th&gt;&lt;th&gt;Required control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Boundary follows tables, not invariants&lt;/td&gt;&lt;td&gt;Commands still require synchronous cross-database updates&lt;/td&gt;&lt;td&gt;Keep the invariant together or redesign the business workflow&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Old writers survive extraction&lt;/td&gt;&lt;td&gt;Two components can mutate the same business fact&lt;/td&gt;&lt;td&gt;Command inventory, database grants, rejected-write monitoring, final fencing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application dual writes&lt;/td&gt;&lt;td&gt;One database succeeds while the other fails&lt;/td&gt;&lt;td&gt;Local transaction with outbox; single authority per phase&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Duplicate or reordered events&lt;/td&gt;&lt;td&gt;Retry or partitioning replays an older fact&lt;/td&gt;&lt;td&gt;Event IDs, aggregate versions, idempotent conditional handlers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CDC stops&lt;/td&gt;&lt;td&gt;Downstream state becomes stale while source commits continue&lt;/td&gt;&lt;td&gt;Lag and retained-log alerts, capacity controls, tested connector recovery&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Projection becomes a secret authority&lt;/td&gt;&lt;td&gt;A read copy accepts corrections or business decisions&lt;/td&gt;&lt;td&gt;Read-only access and visible provenance, version, and freshness&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Saga compensation fails&lt;/td&gt;&lt;td&gt;A refund or reservation release is itself unavailable&lt;/td&gt;&lt;td&gt;Persisted workflow, retry budget, manual queue, reconciliation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cross-domain API fanout grows&lt;/td&gt;&lt;td&gt;One page now depends on many live owners&lt;/td&gt;&lt;td&gt;Purpose-built local projection with an explicit freshness objective&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Database restore ignores events&lt;/td&gt;&lt;td&gt;Restored state and consumer offsets describe different histories&lt;/td&gt;&lt;td&gt;Recovery manifest with backups, outbox state, source offsets, and replay plan&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Every module gets a database&lt;/td&gt;&lt;td&gt;Operational count grows faster than value&lt;/td&gt;&lt;td&gt;Extract only boundaries with clear scaling, ownership, risk, or change benefit&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Teams split before ownership&lt;/td&gt;&lt;td&gt;Service and database incidents have different owners&lt;/td&gt;&lt;td&gt;One accountable owner for commands, schema, data quality, and recovery&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;when-i-would-not-use-this-architecture&quot;&gt;When I would not use this architecture&lt;/h3&gt;
&lt;p&gt;Do not decompose the write path merely because the monolith is unfashionable. Keep one database when the workload fits comfortably, the important invariants are genuinely cohesive, one team can operate the system, and cross-domain consistency would be more valuable than independent scaling.&lt;/p&gt;
&lt;p&gt;Do not choose this path when the organization cannot operate event delivery, reconciliation, multiple backups, schema contracts, and partial failure. Vertical scaling, query repair, native partitioning, or tenant sharding may buy safer headroom first.&lt;/p&gt;
&lt;p&gt;Do not split an invariant that has no acceptable compensation. If a regulation or financial control requires one serializable decision across a set of facts, those facts may belong in one transactional owner—or in a database technology that provides the required distributed transaction semantics after explicit validation.&lt;/p&gt;
&lt;h3 id=&quot;scenario-verdict&quot;&gt;Scenario verdict&lt;/h3&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Decision&lt;/th&gt;&lt;th&gt;RetailCo conclusion&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Good fit&lt;/td&gt;&lt;td&gt;Order, inventory, payment, or shipping has distinct write pressure, ownership, recovery, and scaling needs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Poor fit&lt;/td&gt;&lt;td&gt;A small cohesive system where most critical commands require the same immediate invariant&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Recommended first move&lt;/td&gt;&lt;td&gt;Modularize commands and enforce one writer before moving a database&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Recommended target&lt;/td&gt;&lt;td&gt;One deployment with a small number of domain-owned databases, outbox events, and explicit workflows&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Main advantage&lt;/td&gt;&lt;td&gt;Independent write capacity and blast-radius control without a company-wide service rewrite&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Main limitation&lt;/td&gt;&lt;td&gt;Cross-domain operations become eventually consistent workflows with reconciliation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Operational complexity&lt;/td&gt;&lt;td&gt;High—multiple backups, CDC, event contracts, projections, and recovery positions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Migration complexity&lt;/td&gt;&lt;td&gt;High—hidden writers and implicit constraints must be discovered before cutover&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;For RetailCo, I would extract one measured bottleneck—likely Inventory if reservation contention dominates, or Payment if security and recovery isolation dominate—while keeping the application deployment intact. I would not split Customer, Order, Inventory, Payment, and Shipping simultaneously. The first extraction is successful only when the old writer is gone, the new owner can recover independently, and business invariants converge under duplicate delivery, delay, and partial failure.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; One shared writer couples RetailCo’s order, inventory, payment, and shipping capacity, releases, and incidents.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Establish enforceable domain ownership inside the monolith, then move one domain to its own database with a local outbox and an explicit cross-domain workflow.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Build the actor and invariant maps, revoke legacy writes, reconcile source and target, and run failure tests for duplicate events, CDC outage, ambiguous payment, target failure, and restore before claiming scalability or resilience.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Select the first domain from measured lock, CPU, log, growth, security, and recovery pressure. Keep one deployment for the first cutover; earn the data boundary before deciding whether another service is useful.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>From SQL ID to Root Cause: Plans, Cardinality and SQL Regression</title><link>https://rajivonai.com/blog/2025-12-11-oracle-sql-id-root-cause-plans-cardinality-regression/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-12-11-oracle-sql-id-root-cause-plans-cardinality-regression/</guid><description>An Oracle investigation workflow for proving SQL regressions with child-cursor history, normalized runtime evidence, actual row counts, bind behavior, and reversible plan control.</description><pubDate>Thu, 11 Dec 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A SQL ID can dominate Oracle DB time without having a bad plan, and a new plan hash can appear without causing the incident. Root cause begins only when the investigator proves which child cursor ran, how much work it performed, and why its behavior changed.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The first Oracle article reduced an incident to database time, foreground waits, and SQL IDs. That attribution is a lead, not a verdict. A statement can rank first because it ran more often, processed more data, waited behind another session, or inherited storage pressure.&lt;/p&gt;
&lt;p&gt;Oracle separates a parent cursor from its executable children. One &lt;code&gt;SQL_ID&lt;/code&gt; can have multiple children because optimizer environments, object metadata, bind selectivity, or compatibility checks differ. Their plans and runtime profiles can differ.&lt;/p&gt;
&lt;p&gt;After collectors preserve those distinctions, the LLM can connect deployment, child-cursor, estimate, bind, and workload evidence. It cannot replace the measurements.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Three shortcuts routinely manufacture a plan-regression story:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;comparing a good historical plan with whatever &lt;code&gt;EXPLAIN PLAN&lt;/code&gt; produces now;&lt;/li&gt;
&lt;li&gt;treating &lt;code&gt;PLAN_HASH_VALUE&lt;/code&gt; as a complete description of execution behavior;&lt;/li&gt;
&lt;li&gt;comparing cumulative elapsed time without normalizing executions, rows, and interval coverage.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN PLAN&lt;/code&gt; describes what Oracle would choose in the explain session. Binds and optimizer environment can make the executed cursor differ. A plan hash aids comparison but omits runtime context; full plan hashes are not comparable across releases.&lt;/p&gt;
&lt;p&gt;An actual plan change still does not prove regression. It may suit a selective bind or larger result set. The same plan can slow when it performs more starts, reads more rows, spills, or waits for resources.&lt;/p&gt;
&lt;p&gt;Can we prove that a specific execution path became less efficient for comparable work, identify the mechanism, and change it without freezing the wrong behavior?&lt;/p&gt;
&lt;h2 id=&quot;build-the-sql-regression-proof-chain&quot;&gt;Build the SQL Regression Proof Chain&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[ranked SQL ID from incident] --&gt; B[scope child cursors plans and execution window]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[normalize runtime deltas per execution and row]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[retrieve the actual executed plan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[compare estimated and actual row flow]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[correlate binds statistics environment and changes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[rank regression mechanisms and contradictions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[DBA verification with representative inputs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[reversible plan code or statistics response]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[application and database validation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The evidence pack should keep four layers separate:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;OBSERVATION  child 3 increased buffer gets per execution in the incident window&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;HYPOTHESIS   a cardinality error changed the join order&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;MISSING      actual rows, starts, predicates, bind class, and statistics history&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ACTION       capture scoped runtime plan statistics and test representative binds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Confidence belongs to hypotheses, not observations. The LLM should cite evidence identifiers and expose contradictions.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Evidence pattern&lt;/th&gt;&lt;th&gt;Leading interpretation&lt;/th&gt;&lt;th&gt;What remains unproven&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Same SQL ID, new child and plan hash, higher gets per execution&lt;/td&gt;&lt;td&gt;Possible access-path regression&lt;/td&gt;&lt;td&gt;Comparable binds and rows&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Same plan hash, higher elapsed time, stable gets&lt;/td&gt;&lt;td&gt;Wait or resource change&lt;/td&gt;&lt;td&gt;SQL is innocent&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Same plan, more rows and starts&lt;/td&gt;&lt;td&gt;Workload-shape or data-volume change&lt;/td&gt;&lt;td&gt;Optimizer regression&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Estimate and actual rows diverge early, then join work expands&lt;/td&gt;&lt;td&gt;Cardinality mechanism&lt;/td&gt;&lt;td&gt;Which statistics assumption failed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Several plans serve different bind ranges efficiently&lt;/td&gt;&lt;td&gt;Adaptive cursor sharing behaving as designed&lt;/td&gt;&lt;td&gt;One plan should be forced&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Total SQL time rises while time per execution is stable&lt;/td&gt;&lt;td&gt;Frequency-driven load growth&lt;/td&gt;&lt;td&gt;Individual executions regressed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Parse load, child count, and invalidations rise&lt;/td&gt;&lt;td&gt;Cursor lifecycle or sharing problem&lt;/td&gt;&lt;td&gt;Execution plan is the primary cost&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;h3 id=&quot;1-identify-the-execution-not-only-the-sql-id&quot;&gt;1. Identify the execution, not only the SQL ID&lt;/h3&gt;
&lt;p&gt;Record &lt;code&gt;SQL_ID&lt;/code&gt;, &lt;code&gt;CHILD_NUMBER&lt;/code&gt;, &lt;code&gt;PLAN_HASH_VALUE&lt;/code&gt;, parsing schema, service, module, action, instance, PDB, optimizer environment, load and activity times, and incident boundaries. Preserve RAC instance and multitenant container identity.&lt;/p&gt;
&lt;p&gt;Use matched &lt;code&gt;V$SQL&lt;/code&gt; snapshots or approved equivalents. Its values are cumulative per child, so calculate timestamped deltas. Cursor aging, reload, invalidation, restart, and reset can break the interval. Do not merge children that share SQL text.&lt;/p&gt;
&lt;p&gt;AWR plan and SQL history require Diagnostics Pack. Real-time SQL Monitoring requires Tuning Pack and Diagnostics Pack. Dynamic views still need least-privilege access and may expose SQL or binds; entitlement and data authorization are separate gates.&lt;/p&gt;
&lt;h3 id=&quot;2-normalize-before-comparing-plans&quot;&gt;2. Normalize before comparing plans&lt;/h3&gt;
&lt;p&gt;For each child and interval, calculate at least:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;elapsed_per_execution = delta_elapsed_time / delta_executions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;cpu_per_execution     = delta_cpu_time / delta_executions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;gets_per_execution    = delta_buffer_gets / delta_executions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;reads_per_execution   = delta_disk_reads / delta_executions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;rows_per_execution    = delta_rows_processed / delta_executions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;gets_per_row          = delta_buffer_gets / delta_rows_processed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Guard every denominator. Zero executions, partial fetches, parallel accounting, recursive work, and mixed bind classes can invalidate averages. Compare business throughput and row distributions: longer runtime for many more rows may be efficient, while an average can hide a slow percentile.&lt;/p&gt;
&lt;h3 id=&quot;3-retrieve-the-executed-cursor-plan&quot;&gt;3. Retrieve the executed cursor plan&lt;/h3&gt;
&lt;p&gt;Use &lt;code&gt;DBMS_XPLAN.DISPLAY_CURSOR&lt;/code&gt; for the relevant child while cached. Include runtime statistics only when collected. An approved test can use &lt;code&gt;gather_plan_statistics&lt;/code&gt; and &lt;code&gt;ALLSTATS LAST&lt;/code&gt;; setting &lt;code&gt;STATISTICS_LEVEL=ALL&lt;/code&gt; system-wide adds broader overhead and should not be the incident default.&lt;/p&gt;
&lt;p&gt;Do not send an unredacted plan automatically. SQL, predicates, objects, projections, and peeked binds can reveal sensitive data or design. Preserve a restricted original, create a sanitized copy, and record transformations.&lt;/p&gt;
&lt;h3 id=&quot;4-find-the-first-consequential-estimate-error&quot;&gt;4. Find the first consequential estimate error&lt;/h3&gt;
&lt;p&gt;Compare optimizer-estimated rows with actual rows at each operation. Include &lt;code&gt;Starts&lt;/code&gt;: total actual rows from an operation that started many times means something different from rows per start. Work from the leaves upward and identify the earliest material divergence that changes downstream join order, join method, access path, memory demand, or repeated work.&lt;/p&gt;
&lt;p&gt;Then test a mechanism. Beyond stale statistics, skew can require a histogram; correlated predicates can require column-group statistics; expressions can require expression statistics. Bind peeking may make the first value influential. Adaptive cursor sharing can legitimately create children for different selectivity ranges.&lt;/p&gt;
&lt;p&gt;Captured or sampled binds may not represent the incident execution. Store typed ranges or hashes, then verify representative values through an approved test.&lt;/p&gt;
&lt;h3 id=&quot;5-correlate-the-plan-with-change-and-wait-evidence&quot;&gt;5. Correlate the plan with change and wait evidence&lt;/h3&gt;
&lt;p&gt;Align child creation, invalidation, plan appearance, statistics, schema, deployment, optimizer, routing, and incident transitions. Compare wait composition by child where licensed evidence permits.&lt;/p&gt;
&lt;p&gt;If gets per execution rose with an access-path change, the plan is a strong lead. Stable gets with rising User I/O across many SQL IDs favors storage or cache conditions. Dominant Application waits may mean blocking. Explain supporting and contradicting evidence.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[SQL ID ranks high in incident] --&gt; B{work per execution increased}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| C{execution frequency increased}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|yes| D[classify workload growth or retry amplification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|no| E[inspect waits blocking and evidence coverage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| F{executed child or plan changed}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|no| G[inspect row volume starts spills cache and resources]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|yes| H{inputs and returned rows are comparable}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| I[segment by bind selectivity and workload shape]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| J[compare estimates actual rows and predicates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K{mechanism is reproducible}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|no| L[collect missing runtime statistics and change history]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|yes| M[test the smallest reversible correction]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Oracle’s &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/V-SQL_PLAN.html&quot;&gt;&lt;code&gt;V$SQL_PLAN&lt;/code&gt;&lt;/a&gt; stores plans per child cursor and describes &lt;code&gt;PLAN_HASH_VALUE&lt;/code&gt; as a convenient numerical plan representation. Oracle’s &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/generating-and-displaying-execution-plans.html&quot;&gt;execution-plan guidance&lt;/a&gt; also states that optimizer inputs include statistics, binds, initialization parameters, and schema changes, and that an explained plan can differ from the actual plan. The documented behavior supports child-level attribution, not SQL-ID-level certainty.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/arpls/DBMS_XPLAN.html&quot;&gt;&lt;code&gt;DBMS_XPLAN.DISPLAY_CURSOR&lt;/code&gt;&lt;/a&gt; displays a loaded cursor and can add I/O and memory statistics when basic plan statistics were collected. &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/V-SQL_PLAN_STATISTICS_ALL.html&quot;&gt;&lt;code&gt;V$SQL_PLAN_STATISTICS_ALL&lt;/code&gt;&lt;/a&gt; exposes operation-level starts, output rows, reads, buffer gets, and elapsed time. These are the mechanics behind estimate-versus-actual analysis; the LLM should never invent missing actual rows.&lt;/p&gt;
&lt;p&gt;Oracle documents that &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/histograms.html&quot;&gt;histograms&lt;/a&gt; improve estimates for skewed columns and that &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/managing-extended-statistics.html&quot;&gt;extended statistics&lt;/a&gt; can model correlated columns and expressions. Its &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/improving-rwp-cursor-sharing.html&quot;&gt;adaptive cursor sharing&lt;/a&gt; guidance explains how bind-sensitive statements can become bind-aware and use different plans for different selectivity ranges. Multiple plans are therefore evidence to explain, not an anomaly by definition.&lt;/p&gt;
&lt;p&gt;SQL Plan Management separates accepted, unaccepted, enabled, and fixed baselines. Oracle’s &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/overview-of-sql-plan-management.html&quot;&gt;SPM overview&lt;/a&gt; describes plan evolution as verification before acceptance. By contrast, a &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/managing-sql-profiles.html&quot;&gt;SQL profile&lt;/a&gt; supplies auxiliary optimizer statistics and does not pin one plan. SQL profiles and SQL Tuning Advisor are Tuning Pack features. Verify edition, deployment, and licensing before selecting any control.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven condition&lt;/th&gt;&lt;th&gt;Candidate response&lt;/th&gt;&lt;th&gt;Main risk&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Workload frequency or retries grew&lt;/td&gt;&lt;td&gt;Throttle, deduplicate, or correct the caller&lt;/td&gt;&lt;td&gt;Hides necessary demand&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Statistics no longer represent the data&lt;/td&gt;&lt;td&gt;Test scoped statistics correction and publish safely&lt;/td&gt;&lt;td&gt;Replans unrelated statements&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Skew or correlation breaks estimates&lt;/td&gt;&lt;td&gt;Test histogram or extended statistics&lt;/td&gt;&lt;td&gt;More parse sensitivity and maintenance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Known accepted plan is reproducibly better&lt;/td&gt;&lt;td&gt;Use reviewed SQL Plan Management workflow&lt;/td&gt;&lt;td&gt;Freezes a plan past its useful life&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;SQL formulation causes unnecessary work&lt;/td&gt;&lt;td&gt;Change SQL and test representative binds&lt;/td&gt;&lt;td&gt;Semantic or compatibility regression&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Access path needs a durable structure&lt;/td&gt;&lt;td&gt;Add or redesign an index after write-cost review&lt;/td&gt;&lt;td&gt;DML, storage, and maintenance cost&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Resource waits dominate with stable work&lt;/td&gt;&lt;td&gt;Fix blocking or platform constraint&lt;/td&gt;&lt;td&gt;Plan intervention changes nothing&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Do not gather schema statistics, flush the shared pool, fix a baseline, or accept an advisor recommendation merely to make the plan change. Each action changes scope and risk. A fixed baseline is a control, not a diagnosis; it can also block future improvement.&lt;/p&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;Define rollback before the test. Preserve the previous application artifact, accepted baseline state, statistics metadata, DDL, parameter values, and a representative validation workload. For a newly introduced plan baseline, use the reviewed &lt;code&gt;DBMS_SPM&lt;/code&gt; workflow to disable or remove that intervention and restore the previously approved accepted set. For code, index, or statistics changes, use their tested change-specific reversal.&lt;/p&gt;
&lt;p&gt;Do not describe shared-pool purge as rollback. It discards cached state, affects other sessions, and does not restore the prior optimizer inputs. Stop the change if application latency, errors, DB time per business operation, buffer gets per execution, or a workload-specific guardrail worsens. After reversal, confirm the executing child and plan; configuration rollback without execution proof is incomplete.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why the conclusion fails&lt;/th&gt;&lt;th&gt;Better evidence&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;New plan hash equals regression&lt;/td&gt;&lt;td&gt;Different inputs may need a different plan&lt;/td&gt;&lt;td&gt;Comparable bind class, rows, and normalized work&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Same plan hash equals stable SQL&lt;/td&gt;&lt;td&gt;Runtime volume, starts, spills, cache, and waits can change&lt;/td&gt;&lt;td&gt;Actual row flow and resource evidence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lower cost means faster&lt;/td&gt;&lt;td&gt;Cost is an optimizer comparison unit, not elapsed time&lt;/td&gt;&lt;td&gt;Measured runtime under representative inputs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Fresh explain output represents the incident&lt;/td&gt;&lt;td&gt;Environment and binds may differ&lt;/td&gt;&lt;td&gt;Executed child plan from the incident&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High total elapsed time means slow executions&lt;/td&gt;&lt;td&gt;More executions can raise the total&lt;/td&gt;&lt;td&gt;Interval deltas and per-unit metrics&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Large estimate error alone proves root cause&lt;/td&gt;&lt;td&gt;An error matters only if it changes consequential work&lt;/td&gt;&lt;td&gt;First divergence and downstream effect&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One captured bind represents production&lt;/td&gt;&lt;td&gt;Capture can be sampled, incomplete, or sensitive&lt;/td&gt;&lt;td&gt;Approved representative classes and redaction&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LLM recommends a profile or baseline&lt;/td&gt;&lt;td&gt;It may conflate estimate correction with plan control&lt;/td&gt;&lt;td&gt;DBA choice with licensing and rollback checks&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Customer Data:&lt;/strong&gt; The LLM must not receive bind capture values from &lt;code&gt;V$SQL_BIND_CAPTURE&lt;/code&gt;, literal SQL text, or row data. Send &lt;code&gt;SQL_ID&lt;/code&gt;, force-matching signature, plan hash values, wait-event timings, and DB Time attribution only.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Production Execution:&lt;/strong&gt; The LLM cannot run &lt;code&gt;ALTER SYSTEM&lt;/code&gt;, &lt;code&gt;ALTER SESSION&lt;/code&gt; on other sessions, kill sessions, apply SQL profiles or baselines, or gather statistics. It proposes; a human authorizes, applies, validates, and holds the rollback condition. Note also that AWR and ASH access carries Diagnostics Pack licensing implications — confirm entitlement before any collector reads them.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Causal Conclusion Without Contradicting Evidence:&lt;/strong&gt; Every hypothesis must cite both the telemetry that supports it and the telemetry that would falsify it. A ranked hypothesis with no disconfirming test attached is an assertion, not a diagnosis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Silent Gap-Filling:&lt;/strong&gt; Where the evidence pack lacks a required signal, the LLM must name it explicitly under missing evidence rather than inferring a plausible value. “Not collected” and “collected and normal” are different findings and must never be merged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Change Without a Human Gate:&lt;/strong&gt; Production changes require human authorization, a stated validation signal, and a defined rollback condition agreed before the change is applied.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; A top SQL ID, changed plan hash, or optimizer estimate is suggestive but cannot establish which execution regressed or why.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Scope the child cursor and interval, normalize runtime work, inspect the executed plan, compare estimated with actual row flow, and test bind, statistics, environment, and change mechanisms.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Reproduce the mechanism with representative inputs, show that the intervention reduces application latency and database work without harming other workload classes, then verify the executing child after change.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Add child-cursor snapshots and a redacted &lt;code&gt;DISPLAY_CURSOR&lt;/code&gt; artifact to the evidence-pack schema, with explicit fields for collection scope, runtime-statistics availability, licensing, and contradictions.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The next article tests the opposite conclusion: when SQL evidence is stable, investigate concurrency, redo, RAC coordination, storage, and cloud infrastructure without forcing a query-tuning narrative.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/generating-and-displaying-execution-plans.html&quot;&gt;Oracle AI Database 26ai — Explaining and displaying execution plans&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/V-SQL_PLAN.html&quot;&gt;Oracle AI Database 26ai — V$SQL_PLAN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/V-SQL_PLAN_STATISTICS_ALL.html&quot;&gt;Oracle AI Database 26ai — V$SQL_PLAN_STATISTICS_ALL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/arpls/DBMS_XPLAN.html&quot;&gt;Oracle AI Database 26ai — DBMS_XPLAN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/query-optimizer-concepts.html&quot;&gt;Oracle AI Database 26ai — Query optimizer concepts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/histograms.html&quot;&gt;Oracle AI Database 26ai — Histograms&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/managing-extended-statistics.html&quot;&gt;Oracle AI Database 26ai — Managing extended statistics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/improving-rwp-cursor-sharing.html&quot;&gt;Oracle AI Database 26ai — Adaptive cursor sharing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/overview-of-sql-plan-management.html&quot;&gt;Oracle AI Database 26ai — Overview of SQL Plan Management&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tgsql/managing-sql-profiles.html&quot;&gt;Oracle AI Database 26ai — Managing SQL profiles&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/dblic/Licensing-Information.html&quot;&gt;Oracle AI Database 26ai — Licensing information&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>failures</category><category>architecture</category></item><item><title>LLM-Assisted Oracle Performance Triage: From DB Time to Wait Events</title><link>https://rajivonai.com/blog/2025-12-09-llm-assisted-oracle-performance-triage-db-time-wait-events/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-12-09-llm-assisted-oracle-performance-triage-db-time-wait-events/</guid><description>An Oracle incident workflow for reconciling application symptoms, DB time, average active sessions, CPU, non-idle waits, host pressure, and licensed diagnostic evidence.</description><pubDate>Tue, 09 Dec 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Oracle can report high host CPU, many connected sessions, and a long list of wait events while none of those facts identifies why users are slow. The first useful question is where database time went during the incident.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Oracle exposes deep evidence, but depth creates a sequencing problem. An investigator can jump from latency to AWR, a top wait, SQL ID, or parameter before proving that the database consumed the missing time.&lt;/p&gt;
&lt;p&gt;The stable starting point is the Oracle time model. DB time is cumulative CPU time and non-idle wait time spent processing user requests. Divide its increase by elapsed wall time and the result is average active sessions (AAS). Ten AAS means that, on average, ten foreground sessions were on CPU or waiting for non-idle events during the interval; it does not mean ten sessions were connected.&lt;/p&gt;
&lt;p&gt;That decomposition constrains the LLM. Collectors calculate interval deltas and preserve scope; the model correlates application, host, workload, and change evidence.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Most Oracle triage mistakes concern denominator or scope:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;reading cumulative &lt;code&gt;V$SYS_TIME_MODEL&lt;/code&gt; and &lt;code&gt;V$SYSTEM_EVENT&lt;/code&gt; values as incident totals;&lt;/li&gt;
&lt;li&gt;treating connected sessions as active database load;&lt;/li&gt;
&lt;li&gt;ranking waits by count instead of time impact;&lt;/li&gt;
&lt;li&gt;including idle waits such as clients waiting for work;&lt;/li&gt;
&lt;li&gt;calling the event in &lt;code&gt;V$SESSION&lt;/code&gt; a current wait without checking session state;&lt;/li&gt;
&lt;li&gt;averaging RAC instances or CDB containers into one misleading value;&lt;/li&gt;
&lt;li&gt;blaming Oracle CPU without reconciling database CPU with host capacity and other processes;&lt;/li&gt;
&lt;li&gt;using AWR, ASH, ADDM, or Performance Hub without first proving Diagnostics Pack entitlement.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Application latency can rise while DB time stays flat. Calls may not reach the database, connection acquisition may stall, or throughput may collapse. Low DB load does not prove a healthy service.&lt;/p&gt;
&lt;p&gt;Did the incident add work, make work more expensive, serialize sessions, starve Oracle at the host, or occur outside database calls?&lt;/p&gt;
&lt;h2 id=&quot;build-a-time-aligned-oracle-evidence-pack&quot;&gt;Build a Time-Aligned Oracle Evidence Pack&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[application latency throughput errors and pool time] --&gt; H[incident timeline and scope]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B[DB time DB CPU and user calls] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[foreground wait-class and event deltas] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[active sessions services modules actions and SQL IDs] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E[blocking sessions and transaction context] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F[host CPU run queue memory and storage latency] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G[deployments jobs parameter and topology changes] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[deterministic interval calculations]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[LLM evidence correlation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[ranked hypotheses contradictions and gaps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; L[DBA and application-owner verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Every counter needs start and end values, timestamps, startup identity, instance number, and container scope:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;delta_C = C_end - C_start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;AAS = delta_DB_time_microseconds / interval_microseconds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;CPU_AAS = delta_DB_CPU_microseconds / interval_microseconds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Wait_AAS = AAS - CPU_AAS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Reject negative deltas and intervals crossing restart or reset boundaries. Preserve each RAC instance separately before calculating a cluster total. Preserve &lt;code&gt;CON_ID&lt;/code&gt; where the source supplies it; root-level instance values and PDB-attributed values answer different questions.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Evidence pattern&lt;/th&gt;&lt;th&gt;Leading classification&lt;/th&gt;&lt;th&gt;What it does not prove&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Application latency rises; DB time and calls stay flat&lt;/td&gt;&lt;td&gt;Connection, network, queue, or application path&lt;/td&gt;&lt;td&gt;Oracle is healthy end to end&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DB time rises mostly as DB CPU&lt;/td&gt;&lt;td&gt;CPU-demand growth or less efficient work&lt;/td&gt;&lt;td&gt;Host CPU is exhausted&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CPU AAS approaches available CPUs and host run queue rises&lt;/td&gt;&lt;td&gt;CPU saturation or scheduling pressure&lt;/td&gt;&lt;td&gt;One SQL statement caused it&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;User I/O AAS rises with storage latency&lt;/td&gt;&lt;td&gt;Read path, access path, or storage pressure&lt;/td&gt;&lt;td&gt;Storage is the root cause&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application wait AAS and blockers rise&lt;/td&gt;&lt;td&gt;Lock serialization&lt;/td&gt;&lt;td&gt;Killing the blocker is the durable fix&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Commit wait AAS rises&lt;/td&gt;&lt;td&gt;Foreground commit path pressure&lt;/td&gt;&lt;td&gt;Redo device latency alone caused it&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cluster wait AAS rises on one RAC instance&lt;/td&gt;&lt;td&gt;GCS or GES work and workload placement&lt;/td&gt;&lt;td&gt;RAC itself is misconfigured&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AAS rises while business throughput falls&lt;/td&gt;&lt;td&gt;More time per unit of work or serialization&lt;/td&gt;&lt;td&gt;More capacity will restore efficiency&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;h3 id=&quot;1-freeze-the-incident-contract&quot;&gt;1. Freeze the incident contract&lt;/h3&gt;
&lt;p&gt;Record UTC start, transition, peak, recovery, database unique name, database and instance IDs, startup time, RAC role, service, CDB and PDB scope, version, platform, and known changes. Capture user-facing latency, completed work, errors, retries, connection-pool wait, and request volume.&lt;/p&gt;
&lt;p&gt;Define success in application terms. Oracle’s method starts with throughput or response time, not an internal ratio. Without completed work, lower DB time after an outage can look like improvement.&lt;/p&gt;
&lt;h3 id=&quot;2-measure-db-time-db-cpu-and-workload-as-deltas&quot;&gt;2. Measure DB time, DB CPU, and workload as deltas&lt;/h3&gt;
&lt;p&gt;Take bounded snapshots from dynamic performance views when permitted:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stat_name, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; v$sys_time_model&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stat_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;DB time&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;DB CPU&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;V$SYS_TIME_MODEL&lt;/code&gt; reports microsecond accumulations. Subtract matched snapshots, then compare AAS with calls, executions, commits, and business throughput. Increased AAS with proportional throughput may be legitimate load growth. Increased AAS with flat or falling throughput indicates lost efficiency or serialization.&lt;/p&gt;
&lt;p&gt;Do not force a perfect identity between DB time, DB CPU, and summed waits. Sampling boundaries, publication delay, background work, nested timing, and scope differences create gaps. Treat reconciliation variance as a quality signal to explain, not a number to conceal.&lt;/p&gt;
&lt;h3 id=&quot;3-decompose-non-idle-foreground-wait-time&quot;&gt;3. Decompose non-idle foreground wait time&lt;/h3&gt;
&lt;p&gt;Capture &lt;code&gt;TIME_WAITED_MICRO_FG&lt;/code&gt; and &lt;code&gt;TOTAL_WAITS_FG&lt;/code&gt; from &lt;code&gt;V$SYSTEM_EVENT&lt;/code&gt; at both boundaries, excluding &lt;code&gt;Idle&lt;/code&gt;, then rank deltas by time. Wait count without duration overweights frequent short events. Instance-wide lifetime totals overvalue old history.&lt;/p&gt;
&lt;p&gt;Begin with classes: Application, Commit, Concurrency, Configuration, Cluster, Network, Scheduler, System I/O, and User I/O. Then expand the dominant class into events and parameters. A wait event describes where time accumulated, not automatically why. For example, User I/O can reflect an inefficient access path, lost cache locality, changed workload, or storage latency.&lt;/p&gt;
&lt;h3 id=&quot;4-reconcile-oracle-cpu-with-the-host&quot;&gt;4. Reconcile Oracle CPU with the host&lt;/h3&gt;
&lt;p&gt;Compare CPU AAS, database CPU per second, CPU count available to the instance, host busy and idle CPU, run queue, cgroup or VM limits, and non-Oracle consumers. &lt;code&gt;V$OSSTAT&lt;/code&gt; exposes selected OS statistics, but Oracle notes that availability is platform-dependent; retain native OS or cloud telemetry.&lt;/p&gt;
&lt;p&gt;If DB CPU rises but host idle capacity remains, investigate SQL and workload efficiency. If CPU AAS presses against usable CPU while the run queue and latency rise, saturation is plausible. If host CPU is high but DB CPU is flat, look for other instances, agents, backup or compression work, storage polling, or a virtualization boundary.&lt;/p&gt;
&lt;h3 id=&quot;5-attribute-active-time-without-overclaiming&quot;&gt;5. Attribute active time without overclaiming&lt;/h3&gt;
&lt;p&gt;For a live incident, inspect active sessions by instance, container, service, module, action, SQL ID, state, wait class, event, and blocker. &lt;code&gt;V$SESSION_WAIT&lt;/code&gt; reports the current or last wait; when &lt;code&gt;STATE&lt;/code&gt; is not &lt;code&gt;WAITING&lt;/code&gt;, its event is historical context, not evidence that the session is blocked now.&lt;/p&gt;
&lt;p&gt;Where licensed, ASH samples active sessions once per second and preserves dimensions such as SQL ID, module, state, and event. It is a sampled workload distribution, not an exact statement ledger: long-lived activity is more likely to appear than very short activity. Use it to rank hypotheses, then verify with session, SQL, plan, and application evidence.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[user-visible slowdown] --&gt; B{DB time per second increased}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| C{database calls or throughput fell}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|yes| D[inspect pool network routing and application]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|no| E[verify window scope and telemetry gaps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| F{CPU share dominates}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|yes| G{host CPU capacity constrained}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|yes| H[classify database and non-database CPU demand]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|no| I[find service module and SQL driving CPU]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|no| J[rank non-idle foreground wait deltas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K{one class dominates}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|no| L[segment by instance service container and phase]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|yes| M[expand events sessions blockers and workload]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    M --&gt; N[collect missing proof and validate hypothesis]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Oracle defines &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tdppt/addm-analysis.html&quot;&gt;DB time&lt;/a&gt; as cumulative CPU and non-idle wait time processing user requests. Its &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tdppt/monitoring-user-activity.html&quot;&gt;user-activity guidance&lt;/a&gt; defines AAS as DB time divided by elapsed time and decomposes AAS by CPU, wait class, instance, service, module, action, session, and SQL ID. The documented model supports correlation; it does not license a shortcut from top wait to root cause.&lt;/p&gt;
&lt;p&gt;Oracle’s &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/V-SYSTEM_EVENT.html&quot;&gt;&lt;code&gt;V$SYSTEM_EVENT&lt;/code&gt;&lt;/a&gt; includes foreground wait counts and microsecond wait time, while &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/V-SESSION_WAIT.html&quot;&gt;&lt;code&gt;V$SESSION_WAIT&lt;/code&gt;&lt;/a&gt; distinguishes &lt;code&gt;WAITING&lt;/code&gt; from a completed previous wait. These behaviors make interval arithmetic and session state mandatory evidence-quality checks.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tgdba/performance-improvement-methods.html&quot;&gt;Oracle performance method&lt;/a&gt; recommends confirming a bottleneck, changing one thing where possible, and validating the user’s experience. That maps directly to this series: deterministic observation, ranked hypotheses, additional proof, controlled remediation, and post-change validation.&lt;/p&gt;
&lt;p&gt;Licensing is part of collector design. Oracle’s &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/dblic/Licensing-Information.html&quot;&gt;26ai licensing manual&lt;/a&gt; places AWR, ADDM, ASH, &lt;code&gt;V$ACTIVE_SESSION_HISTORY&lt;/code&gt;, &lt;code&gt;DBA_HIST_*&lt;/code&gt; data with listed exceptions, and Performance Hub within Diagnostics Pack. Direct SQL access still requires entitlement. Real-time SQL Monitoring belongs to Tuning Pack, which also requires Diagnostics Pack. &lt;code&gt;CONTROL_MANAGEMENT_PACK_ACCESS&lt;/code&gt; controls availability; it is not evidence that the organization purchased the required licenses.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven condition&lt;/th&gt;&lt;th&gt;Candidate response&lt;/th&gt;&lt;th&gt;Validation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Calls stall outside DB while DB time is flat&lt;/td&gt;&lt;td&gt;Repair pool, routing, listener, or application path&lt;/td&gt;&lt;td&gt;End-to-end latency and completed calls recover&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CPU demand grew with one workload dimension&lt;/td&gt;&lt;td&gt;Reduce, defer, or tune that workload&lt;/td&gt;&lt;td&gt;DB CPU per business operation falls&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Host scheduling constrains Oracle&lt;/td&gt;&lt;td&gt;Remove competing load or add approved capacity&lt;/td&gt;&lt;td&gt;Run queue falls and throughput improves&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;User I/O is access-path driven&lt;/td&gt;&lt;td&gt;Tune SQL or indexing after plan proof&lt;/td&gt;&lt;td&gt;Reads and DB time per execution fall&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lock serialization is proven&lt;/td&gt;&lt;td&gt;Correct transaction scope or access order&lt;/td&gt;&lt;td&gt;Blocked time and business latency fall&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Commit time is workload driven&lt;/td&gt;&lt;td&gt;Batch safe commits or reduce redo demand&lt;/td&gt;&lt;td&gt;Commit AAS falls without durability loss&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Evidence window is incomplete&lt;/td&gt;&lt;td&gt;Collect another bounded interval&lt;/td&gt;&lt;td&gt;Counters reconcile within documented scope&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;Triage collection should be read-only. If it adds pressure, stop the collector, close its sessions, and remove temporary sampling frequency changes. Restore logging, monitoring, and snapshot settings through the normal change path. Preserve the incident window and collector manifest.&lt;/p&gt;
&lt;p&gt;For remediation, define rollback before execution: workload-routing reversal, job re-enable, parameter restoration, capacity reversal, or application release rollback. Reversing an index or SQL-plan intervention may require a forward correction rather than an immediate inverse. Stop when application health, throughput, or a guardrail worsens, then remeasure DB time and its decomposition.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why the conclusion fails&lt;/th&gt;&lt;th&gt;Better evidence&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Highest lifetime wait is called the incident&lt;/td&gt;&lt;td&gt;Cumulative history crosses the window&lt;/td&gt;&lt;td&gt;Matched foreground deltas&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AAS is compared only with CPU count&lt;/td&gt;&lt;td&gt;Wait AAS can legitimately exceed cores&lt;/td&gt;&lt;td&gt;Separate CPU and wait components&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High CPU utilization is called saturation&lt;/td&gt;&lt;td&gt;Utilization lacks run-queue and capacity context&lt;/td&gt;&lt;td&gt;CPU AAS, host idle, queue, throughput&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Top wait is called root cause&lt;/td&gt;&lt;td&gt;Wait location has several causal paths&lt;/td&gt;&lt;td&gt;Event parameters, sessions, workload, host&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Current-session snapshot explains the past&lt;/td&gt;&lt;td&gt;Transient activity disappeared&lt;/td&gt;&lt;td&gt;Licensed ASH or external bounded sampling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AWR endpoints bracket the incident loosely&lt;/td&gt;&lt;td&gt;Averages dilute the transition and peak&lt;/td&gt;&lt;td&gt;Phase-aligned windows and finer evidence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;RAC totals hide instance skew&lt;/td&gt;&lt;td&gt;Cluster sum removes placement context&lt;/td&gt;&lt;td&gt;Per-instance evidence before aggregation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LLM receives a report without scope metadata&lt;/td&gt;&lt;td&gt;It cannot distinguish deltas, resets, or containers&lt;/td&gt;&lt;td&gt;Typed evidence manifest and contradictions&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Customer Data:&lt;/strong&gt; The LLM must not receive bind capture values from &lt;code&gt;V$SQL_BIND_CAPTURE&lt;/code&gt;, literal SQL text, or row data. Send &lt;code&gt;SQL_ID&lt;/code&gt;, force-matching signature, plan hash values, wait-event timings, and DB Time attribution only.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Production Execution:&lt;/strong&gt; The LLM cannot run &lt;code&gt;ALTER SYSTEM&lt;/code&gt;, &lt;code&gt;ALTER SESSION&lt;/code&gt; on other sessions, kill sessions, apply SQL profiles or baselines, or gather statistics. It proposes; a human authorizes, applies, validates, and holds the rollback condition. Note also that AWR and ASH access carries Diagnostics Pack licensing implications — confirm entitlement before any collector reads them.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Causal Conclusion Without Contradicting Evidence:&lt;/strong&gt; Every hypothesis must cite both the telemetry that supports it and the telemetry that would falsify it. A ranked hypothesis with no disconfirming test attached is an assertion, not a diagnosis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Silent Gap-Filling:&lt;/strong&gt; Where the evidence pack lacks a required signal, the LLM must name it explicitly under missing evidence rather than inferring a plausible value. “Not collected” and “collected and normal” are different findings and must never be merged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Change Without a Human Gate:&lt;/strong&gt; Production changes require human authorization, a stated validation signal, and a defined rollback condition agreed before the change is applied.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Oracle exposes many plausible symptoms, but cumulative counters, idle waits, current-session snapshots, and mixed scopes can produce a confident diagnosis of the wrong interval.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Begin with user impact, calculate DB time and CPU as interval deltas, decompose non-idle foreground waits, reconcile the host, and attribute activity by instance, container, service, module, and SQL ID.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Recalculate the same incident independently from source snapshots. The AAS decomposition, workload change, wait deltas, host evidence, and application outcome should support the same ranked cause while contradictory evidence remains visible.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Build two collectors now—time-model deltas and foreground wait deltas—with startup, instance, container, and licensing metadata before sending Oracle evidence to an LLM.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The next article moves from a ranked SQL ID to proof: execution history, plan hash changes, cardinality estimates, runtime rows, bind behavior, and controlled plan remediation.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tdppt/oracle-database-performance-method.html&quot;&gt;Oracle AI Database 26ai — Oracle Database performance method&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tdppt/monitoring-user-activity.html&quot;&gt;Oracle AI Database 26ai — Monitoring user activity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/V-SYS_TIME_MODEL.html&quot;&gt;Oracle AI Database 26ai — V$SYS_TIME_MODEL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/V-SYSTEM_EVENT.html&quot;&gt;Oracle AI Database 26ai — V$SYSTEM_EVENT&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/V-SESSION_WAIT.html&quot;&gt;Oracle AI Database 26ai — V$SESSION_WAIT&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/refrn/classes-of-wait-events.html&quot;&gt;Oracle AI Database 26ai — Classes of wait events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tdppt/active-session-history-statistics.html&quot;&gt;Oracle AI Database 26ai — Active Session History statistics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/tdppt/monitoring-host-activity.html&quot;&gt;Oracle AI Database 26ai — Monitoring host activity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/26/dblic/Licensing-Information.html&quot;&gt;Oracle AI Database 26ai — Licensing information&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>ai-engineering</category><category>failures</category></item><item><title>Cassandra 5.0 for Operators: What SAI, UCS, Trie Memtables, and JDK 17 Actually Change</title><link>https://rajivonai.com/blog/2025-12-03-cassandra-5-0-operator-deep-dive-ucs-sai-trie/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-12-03-cassandra-5-0-operator-deep-dive-ucs-sai-trie/</guid><description>A deep dive into the physical storage engine overhaul in Apache Cassandra 5.0, explaining how Unified Compaction Strategy (UCS) and Storage-Attached Indexing (SAI) redefine cluster operations.</description><pubDate>Wed, 03 Dec 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;For a decade, operating Apache Cassandra at scale meant managing a delicate equilibrium. You had to choose the least-bad compaction strategy. You avoided secondary indexes at all costs. You wrestled with JVM Garbage Collection. You provisioned massive disk headroom to survive compaction spikes.&lt;/p&gt;
&lt;p&gt;Cassandra 5.0 does not just add new features; it fundamentally replaces the physical storage engine to solve these historical constraints.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;By late 2025, the 90-node cluster was holding 120 TB of data and serving a mix of transactional writes and new vector search workloads.&lt;/p&gt;
&lt;p&gt;Under Cassandra 4.x, this architecture required heavy operational lifting. We had legacy tables stuck on Size Tiered Compaction Strategy (STCS) suffering from 50% space amplification. The attempts to run materialized views and legacy secondary indexes (2i) had resulted in memory fragmentation and unpredictable read latencies. The memtables churned massive amounts of JVM heap, forcing us to meticulously tune G1GC.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra’s internal subsystems had hit architectural limits.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Compaction:&lt;/strong&gt; Operators had to choose between STCS (optimized for writes but requires 50% free disk space) and Leveled Compaction Strategy (LCS) (optimized for reads and space, but causes massive write amplification).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Indexing:&lt;/strong&gt; Legacy secondary indexes were global and didn’t share the SSTable lifecycle well. They were slow, expensive, and didn’t support vector search.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Memory:&lt;/strong&gt; Memtables and SSTable indexes were built on data structures that consumed massive amounts of JVM heap, forcing long GC pauses under heavy load.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Cassandra 5.0 solves these by overhauling the write path, the read path, the compaction engine, and the runtime.&lt;/p&gt;
&lt;h2 id=&quot;the-50-storage-engine-architecture&quot;&gt;The 5.0 Storage Engine Architecture&lt;/h2&gt;
&lt;p&gt;Cassandra 5.0 introduces three massive CEPs (Cassandra Enhancement Proposals) that interact to redefine how data is stored and indexed.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Client[Client Driver] --&gt; Coord(Coordinator Node)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord --&gt; Guard[Dynamic Guardrails]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Guard --&gt; JDK[JDK 17 LTS / ZGC / Vector API]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    JDK --&gt; TrieM[Trie Memtable — Off-Heap Allocator]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    TrieM --&gt;|Flush| BTI[BTI Format Trie SSTable]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    TrieM --&gt;|Flush| SAI[Storage-Attached Index — k-NN / HNSW Vector Graph]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    BTI --&gt; UCS[Unified Compaction Strategy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SAI --&gt; UCS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;UCS Dynamic Sharding&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        UCS --&gt; Shard1[Shard 1]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        UCS --&gt; Shard2[Shard 2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-unified-compaction-strategy-ucs&quot;&gt;1. Unified Compaction Strategy (UCS)&lt;/h3&gt;
&lt;p&gt;UCS (CEP-26) deprecates the need to choose between STCS and LCS. It introduces dynamic sharding and a configurable scaling parameter (W) per level. Instead of compacting massive SSTables into even larger ones, UCS breaks data into shards (e.g., 10 GB targets) and organizes SSTables into levels based on density (SSTable size relative to its owned token range). The scaling parameter determines whether a given level behaves tiered (like STCS) or leveled (like LCS) — it’s a configuration choice, not a runtime auto-detection — which is what lets one strategy replace hand-picking between STCS and LCS per table. The net effect is materially lower space amplification than STCS alone, though the exact reduction is workload- and configuration-dependent.&lt;/p&gt;
&lt;h3 id=&quot;2-storage-attached-indexing-sai&quot;&gt;2. Storage-Attached Indexing (SAI)&lt;/h3&gt;
&lt;p&gt;SAI (CEP-7) replaces legacy 2i (and SASI, its storage-attached predecessor). SAI indexes are tied to the exact lifecycle of the SSTable they index, which meaningfully reduces disk usage compared to legacy global secondary indexes — the exact ratio depends on cardinality and data shape. Vector search (k-NN via HNSW/JVector) was added on top of SAI by a separate, later enhancement (CEP-30), and is exposed through the same &lt;code&gt;StorageAttachedIndex&lt;/code&gt; interface alongside high-performance multi-dimensional queries directly within CQL.&lt;/p&gt;
&lt;h3 id=&quot;3-trie-memtables-and-bti-sstables&quot;&gt;3. Trie Memtables and BTI SSTables&lt;/h3&gt;
&lt;p&gt;Cassandra 5.0 replaces the legacy skip-list memtable with a concurrent prefix tree (Trie). This fundamentally changes serialization, cutting per-object allocation overhead and moving massive amounts of tracking data off the JVM heap. The on-disk &lt;code&gt;Index.db&lt;/code&gt; and &lt;code&gt;Summary.db&lt;/code&gt; are replaced by the BTI format, dramatically speeding up the read path.&lt;/p&gt;
&lt;h3 id=&quot;4-jdk-17-runtime&quot;&gt;4. JDK 17 Runtime&lt;/h3&gt;
&lt;p&gt;Running Cassandra 5.0 on OpenJDK 17 unlocks native support for ZGC and Shenandoah (reducing GC pauses to sub-millisecond ranges), Vector API optimizations for vector search math, and improved G1GC ergonomics out of the box.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Migrating the 4.1 cluster to these features involved specific &lt;code&gt;cassandra.yaml&lt;/code&gt; and schema changes. We transitioned the memtables to the new allocator:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# cassandra.yaml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;memtable&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;trie&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;memtable_allocation_type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;offheap_objects&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For the tables, we altered the compaction strategy to UCS. Because UCS supports sharding, subsequent compactions began reshaping the massive monolithic SSTables into manageable chunks as new data was written and compacted — existing SSTables aren’t rewritten instantly, but the strategy change takes effect without a full cluster rebuild:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; production&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; compaction &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;class&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;UnifiedCompactionStrategy&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;target_sstable_size&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;10GB&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, a typical deployment involves vector search for the ML workloads using SAI directly alongside the transactional data:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CUSTOM &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INDEX&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; user_vector_idx &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; production&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;users&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(embedding_vector) &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;USING&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;StorageAttachedIndex&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Enabling SAI on High-Cardinality Strings&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;SAI is incredibly efficient, but indexing an unbounded, totally unique UUID column will still balloon memory and disk requirements.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Explicitly model memory requirements. Do not index every column blindly.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Misconfiguring UCS Shard Sizes&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Setting the &lt;code&gt;target_sstable_size&lt;/code&gt; too small on a multi-terabyte volume will generate thousands of tiny SSTables, exhausting OS file descriptors.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Stick to the default UCS parameters initially (usually dynamic) before forcing specific shard targets.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Combined JDK 17 Upgrades&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Blindly upgrading the database binary and the JVM simultaneously masks root causes if a node fails to boot.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Decouple the runtime upgrade. Get the cluster stable on C* 5.0 with JDK 11, then upgrade to JDK 17.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-18-months-of-cassandra-50&quot;&gt;Modern Cassandra Context: 18 Months of Cassandra 5.0&lt;/h3&gt;
&lt;p&gt;After 18 months in production on AWS EC2, the GC-pause reduction from Trie Memtables and the space-reclamation efficiency of UCS let us shrink the EC2 instance sizes for this cluster, recovering meaningful cloud spend while absorbing new vector search workloads without bolting on an external, specialized vector database.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Legacy compaction strategies, memory-heavy memtables, and brittle secondary indexes restrict Cassandra’s scalability and increase cloud costs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Upgrade to Cassandra 5.0, adopt Unified Compaction Strategy (UCS), switch to Trie Memtables, and leverage Storage-Attached Indexing (SAI) on JDK 17.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Adopting UCS and Trie Memtables drastically reduces space amplification and eliminates &lt;code&gt;To-space exhausted&lt;/code&gt; GC pauses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before upgrading, run &lt;code&gt;nodetool tablestats&lt;/code&gt; to identify tables suffering from high space amplification. These are your first candidates for an &lt;code&gt;ALTER TABLE&lt;/code&gt; to &lt;code&gt;UnifiedCompactionStrategy&lt;/code&gt; post-upgrade.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>vector-db</category></item><item><title>Securing LLM-Assisted PostgreSQL Diagnostics: Roles, Audit Evidence and Safe Recommendations</title><link>https://rajivonai.com/blog/2025-11-27-securing-llm-assisted-postgresql-diagnostics-roles-audit-evidence-safe-recommendations/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-11-27-securing-llm-assisted-postgresql-diagnostics-roles-audit-evidence-safe-recommendations/</guid><description>A PostgreSQL and Aurora control plane for collecting performance evidence without leaking SQL, overloading production, confusing audit sources, or granting an LLM change authority.</description><pubDate>Thu, 27 Nov 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A PostgreSQL diagnostic role can be unable to update one business row yet still reveal other users’ SQL, hold locks, consume I/O, and turn an incident investigation into a data-exposure event.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Previous PostgreSQL investigations used statistics, plans, logs, host evidence, and Aurora metrics. These can expose SQL text, bind values, identities, object names, and topology.&lt;/p&gt;
&lt;p&gt;The LLM does not need that privilege. Collectors need scoped access; the model needs a sanitized, time-bounded evidence pack. Requests for more evidence return through a policy broker.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s monitoring roles are intentionally powerful. &lt;code&gt;pg_monitor&lt;/code&gt; includes &lt;code&gt;pg_read_all_settings&lt;/code&gt;, &lt;code&gt;pg_read_all_stats&lt;/code&gt;, and &lt;code&gt;pg_stat_scan_tables&lt;/code&gt;. The last can invoke monitoring functions that take &lt;code&gt;ACCESS SHARE&lt;/code&gt; locks for a long time. &lt;code&gt;pg_read_all_stats&lt;/code&gt; can reveal other users’ query text and query identifiers in &lt;code&gt;pg_stat_statements&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Read-only transactions can spill, scan, wait, or displace cache pages. A replica protects the writer from some collector load, but a bad diagnostic can increase lag or exhaust local resources.&lt;/p&gt;
&lt;p&gt;Audit coverage is fragmented. PostgreSQL logs, EC2 evidence, Aurora Database Activity Streams, and CloudTrail describe different activity. None records what was redacted, what the model saw, or why a recommendation was accepted.&lt;/p&gt;
&lt;p&gt;How do we prove causality while keeping sensitive data, unbounded diagnostic cost, and production authority outside the LLM’s control?&lt;/p&gt;
&lt;h2 id=&quot;build-a-five-boundary-diagnostic-control-plane&quot;&gt;Build a Five-Boundary Diagnostic Control Plane&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[operator request and incident scope] --&gt; B[policy broker — catalog budget and expiry]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[PostgreSQL allowlisted collectors]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; D[EC2 or Aurora telemetry collectors]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; E[restricted raw evidence vault]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[deterministic minimization and redaction]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[versioned sanitized evidence pack]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[LLM reasoning — no production credentials]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[observations hypotheses gaps and proposed actions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[human approval and separate change workflow]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K[independent audit ledger] --&gt; L[DBA and security review]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The five boundaries are identity, query capability, resource budget, disclosure, and change authority. Each must fail closed.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Identity&lt;/th&gt;&lt;th&gt;Allowed&lt;/th&gt;&lt;th&gt;Excluded&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Baseline database collector&lt;/td&gt;&lt;td&gt;Fixed statistics, waits, locks, WAL, vacuum, and digest summaries&lt;/td&gt;&lt;td&gt;Application tables, arbitrary SQL, backend signaling, resets&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Elevated evidence role&lt;/td&gt;&lt;td&gt;Expiring cross-session text or approved plan evidence&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_signal_backend&lt;/code&gt;, &lt;code&gt;pg_read_all_data&lt;/code&gt;, &lt;code&gt;CREATEROLE&lt;/code&gt;, DDL&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Host or AWS collector&lt;/td&gt;&lt;td&gt;Named OS metrics, CloudWatch data, RDS events, topology&lt;/td&gt;&lt;td&gt;Shell execution, cluster modification, failover, deletion&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Evidence transformer&lt;/td&gt;&lt;td&gt;Read restricted artifacts; write sanitized packs&lt;/td&gt;&lt;td&gt;Production access&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LLM runtime&lt;/td&gt;&lt;td&gt;Read sanitized packs; produce analysis&lt;/td&gt;&lt;td&gt;Credentials, collectors, approvals, deletion&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Change executor&lt;/td&gt;&lt;td&gt;Execute one approved runbook&lt;/td&gt;&lt;td&gt;Model-selected scope or self-approval&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Do not grant &lt;code&gt;pg_monitor&lt;/code&gt; automatically. Prefer fixed views containing only required columns. Treat &lt;code&gt;pg_read_all_stats&lt;/code&gt; as elevated because it expands cross-user visibility. Never combine it with &lt;code&gt;pg_signal_backend&lt;/code&gt;, which can cancel or terminate sessions.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Security or operational smell&lt;/th&gt;&lt;th&gt;Boundary failure&lt;/th&gt;&lt;th&gt;Immediate risk&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Collector connects as the application, owner, or &lt;code&gt;rds_superuser&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Identity separation failed&lt;/td&gt;&lt;td&gt;Excessive, unattributable authority&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;pg_monitor&lt;/code&gt; is called harmless read access&lt;/td&gt;&lt;td&gt;Evidence sensitivity was ignored&lt;/td&gt;&lt;td&gt;Query text, settings, and long-locking functions become reachable&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Model emits executable SQL&lt;/td&gt;&lt;td&gt;Reasoning and collection are combined&lt;/td&gt;&lt;td&gt;Novel or expensive statements reach production&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Query text, errors, or plans enter prompts unchanged&lt;/td&gt;&lt;td&gt;Redaction was bypassed&lt;/td&gt;&lt;td&gt;Literals, comments, identifiers, and personal data escape&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;statement_timeout&lt;/code&gt; is zero&lt;/td&gt;&lt;td&gt;Resource boundary is absent&lt;/td&gt;&lt;td&gt;Read-only collection can run indefinitely&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CloudTrail is treated as the SQL audit trail&lt;/td&gt;&lt;td&gt;Audit planes are conflated&lt;/td&gt;&lt;td&gt;Database activity is missing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;IAM token expiry is treated as session expiry&lt;/td&gt;&lt;td&gt;Authentication and authorization are confused&lt;/td&gt;&lt;td&gt;Established access outlives token generation&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;h3 id=&quot;1-prove-the-effective-database-identity&quot;&gt;1. Prove the effective database identity&lt;/h3&gt;
&lt;p&gt;Record login role, memberships, grants, role settings, and targets. Reject superuser, &lt;code&gt;BYPASSRLS&lt;/code&gt;, &lt;code&gt;CREATEDB&lt;/code&gt;, &lt;code&gt;CREATEROLE&lt;/code&gt;, replication, &lt;code&gt;pg_read_all_data&lt;/code&gt;, file access, program execution, and backend signaling.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;pg_monitor&lt;/code&gt; is a bundle, not a synonym for read-only. Prefer a &lt;code&gt;NOINHERIT&lt;/code&gt; login with a separately controlled evidence role. Aurora IAM authentication replaces the password with a 15-minute token; PostgreSQL privileges still govern the session. AWS states that CloudWatch and CloudTrail do not log token-generation calls, so record broker authentication separately.&lt;/p&gt;
&lt;h3 id=&quot;2-replace-arbitrary-sql-with-a-collector-catalog&quot;&gt;2. Replace arbitrary SQL with a collector catalog&lt;/h3&gt;
&lt;p&gt;The model may request &lt;code&gt;blocked_sessions_v3&lt;/code&gt; with a window and target; it may not compose a query. Each collector fixes SQL, versions, columns, rows, bytes, topology role, timeout, concurrency, and owner.&lt;/p&gt;
&lt;p&gt;Run each database request in a read-only transaction with a short &lt;code&gt;statement_timeout&lt;/code&gt;, a shorter &lt;code&gt;lock_timeout&lt;/code&gt;, an idle-session limit, a bounded connection pool, and a read-only search path. PostgreSQL read-only mode disallows DML against non-temporary tables, DDL, grants, and unsafe &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt;; it does not make an unbounded &lt;code&gt;SELECT&lt;/code&gt; operationally safe.&lt;/p&gt;
&lt;p&gt;Avoid &lt;code&gt;SECURITY DEFINER&lt;/code&gt; where ordinary grants suffice. If unavoidable, use a trusted &lt;code&gt;search_path&lt;/code&gt;, put &lt;code&gt;pg_temp&lt;/code&gt; last, schema-qualify objects, revoke default &lt;code&gt;PUBLIC&lt;/code&gt; execution, and grant selectively.&lt;/p&gt;
&lt;h3 id=&quot;3-minimize-query-and-log-content-before-inference&quot;&gt;3. Minimize query and log content before inference&lt;/h3&gt;
&lt;p&gt;Start with query identifiers, timings, rows, blocks, waits, and pseudonyms. &lt;code&gt;pg_stat_statements&lt;/code&gt; groups structurally equivalent statements, but privileged viewers can see representative text across users. Normalization is not a disclosure guarantee.&lt;/p&gt;
&lt;p&gt;Parse known formats and remove unused fields. Strip comments and bind values; use stable keyed pseudonyms where correlation matters. Treat SQL, errors, identifiers, and plans as untrusted data that cannot select tools or override policy. Quarantine unparseable evidence.&lt;/p&gt;
&lt;h3 id=&quot;4-bound-logging-and-audit-volume&quot;&gt;4. Bound logging and audit volume&lt;/h3&gt;
&lt;p&gt;Do not enable &lt;code&gt;log_statement = &apos;all&apos;&lt;/code&gt; merely for an incident. PostgreSQL warns that logs can reveal sensitive data, including plaintext passwords. Prefer duration thresholds or sampling, then configure parameter-length controls. Correlate entries through &lt;code&gt;log_line_prefix&lt;/code&gt;, session identity, and incident window.&lt;/p&gt;
&lt;p&gt;pgAudit adds session or object auditing through PostgreSQL logging, but audit-all can create disproportionate volume. Keep parameters off unless required. It uses &lt;code&gt;shared_preload_libraries&lt;/code&gt;; full DDL object information requires &lt;code&gt;CREATE EXTENSION pgaudit&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;5-reconcile-the-audit-chain&quot;&gt;5. Reconcile the audit chain&lt;/h3&gt;
&lt;p&gt;For every pack, store incident ID, target pseudonym, collector and redaction versions, source window and resets, hashes, approver, model and prompt version, tool requests, policy decisions, and output. Preserve allowed and denied requests.&lt;/p&gt;
&lt;p&gt;On EC2, reconcile database logs with host identity and process evidence. On Aurora, exported PostgreSQL logs can flow to CloudWatch Logs; CloudTrail records Aurora API actions, not SQL. Database Activity Streams sends database activity to Kinesis and separates stream administration from DBAs, but the stream includes full SQL and, for Aurora PostgreSQL, bind variables and stored-procedure parameters. It belongs in a restricted audit path, not directly in an LLM prompt.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[diagnostic question] --&gt; B{approved aggregate collector sufficient}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| C[run baseline role with budgets]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| D{cross-session text required and approved}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no| E[request safer evidence or stop]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes| F[activate expiring evidence role]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; G[validate size scope and source time]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[redact and pseudonymize]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I{classification confidence acceptable}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|no| J[quarantine for security review]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|yes| K[seal evidence pack and manifest]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; L[LLM correlates evidence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt; M{new request matches catalog and budget}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    M --&gt;|no| N[deny and record]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    M --&gt;|yes| C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;what-the-llm-can-and-cannot-do&quot;&gt;What the LLM Can and Cannot Do&lt;/h2&gt;





































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;It may&lt;/th&gt;&lt;th&gt;It may not&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Separate observations, hypotheses, contradictions, and gaps&lt;/td&gt;&lt;td&gt;Treat evidence text as instructions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Rank causes against sanitized evidence IDs&lt;/td&gt;&lt;td&gt;Connect to PostgreSQL, EC2, CloudWatch, or RDS&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Request an approved collector and bounded parameters&lt;/td&gt;&lt;td&gt;Write or execute arbitrary SQL or shell commands&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Explain a plan captured through an approved path&lt;/td&gt;&lt;td&gt;Run &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; on production SQL automatically&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Draft remediation, validation, risk, and rollback&lt;/td&gt;&lt;td&gt;Cancel sessions, reset statistics, alter roles, or change parameters&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Recommend human escalation&lt;/td&gt;&lt;td&gt;Reboot, resize, fail over, promote, or delete infrastructure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Identify incomplete audit coverage&lt;/td&gt;&lt;td&gt;Approve itself, hide denied requests, or erase evidence&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s &lt;a href=&quot;https://www.postgresql.org/docs/18/predefined-roles.html&quot;&gt;predefined-role documentation&lt;/a&gt; says &lt;code&gt;pg_monitor&lt;/code&gt; includes three monitoring roles and warns that their privileges may change as capabilities are added. Role reviews must therefore recur after major upgrades rather than treating the initial grant as permanent proof.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.postgresql.org/docs/18/pgstatstatements.html&quot;&gt;&lt;code&gt;pg_stat_statements&lt;/code&gt; security model&lt;/a&gt; exposes other users’ SQL text and &lt;code&gt;queryid&lt;/code&gt; only to superusers and roles with &lt;code&gt;pg_read_all_stats&lt;/code&gt;. This is precisely why cross-workload correlation and evidence confidentiality must be designed together.&lt;/p&gt;
&lt;p&gt;PostgreSQL’s &lt;a href=&quot;https://www.postgresql.org/docs/18/runtime-config-logging.html&quot;&gt;logging controls&lt;/a&gt; separate statement logging, duration thresholds, sampling, and parameter limits, supporting incident-specific collection. &lt;a href=&quot;https://github.com/pgaudit/pgaudit&quot;&gt;pgAudit&lt;/a&gt; supports session classes and finer-grained object auditing with superuser-controlled settings.&lt;/p&gt;
&lt;p&gt;For Aurora, &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.CloudWatch.html&quot;&gt;CloudWatch export&lt;/a&gt; preserves logs by retention after export is disabled. &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/logging-using-cloudtrail.html&quot;&gt;CloudTrail&lt;/a&gt; captures Aurora API requests. &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/DBActivityStreams.Monitoring.html&quot;&gt;Database Activity Streams&lt;/a&gt; captures database activity through Kinesis, including sensitive SQL and parameters. The sources are complementary.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Existing risk&lt;/th&gt;&lt;th&gt;Safer remediation&lt;/th&gt;&lt;th&gt;Proof&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Shared owner credential&lt;/td&gt;&lt;td&gt;Dedicated &lt;code&gt;NOINHERIT&lt;/code&gt; collector identity&lt;/td&gt;&lt;td&gt;Effective grants and connection logs agree&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Permanent &lt;code&gt;pg_monitor&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Fixed views plus expiring elevated access&lt;/td&gt;&lt;td&gt;Post-expiry access is denied and recorded&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Free-form model SQL&lt;/td&gt;&lt;td&gt;Versioned collector catalog and broker&lt;/td&gt;&lt;td&gt;Unknown collector IDs and parameters fail closed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Raw SQL in prompts&lt;/td&gt;&lt;td&gt;Aggregate-first extraction and deterministic redaction&lt;/td&gt;&lt;td&gt;Seeded literals and comments never reach the pack&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unbounded diagnostic reads&lt;/td&gt;&lt;td&gt;Timeouts, row and byte caps, concurrency limits&lt;/td&gt;&lt;td&gt;Load test terminates at every configured budget&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One audit source&lt;/td&gt;&lt;td&gt;Reconciled database, host or AWS, pipeline, and approval records&lt;/td&gt;&lt;td&gt;One incident ID traces the complete decision&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Model-issued remediation&lt;/td&gt;&lt;td&gt;Separate human-approved change identity&lt;/td&gt;&lt;td&gt;LLM runtime has no reachable mutation path&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;If diagnostics create load, stop collection, cancel only identified sessions through a separate DBA identity, and revoke elevated membership. Confirm elevated queries fail. Revert incident logging normally; on Aurora, disabling export does not delete existing CloudWatch records.&lt;/p&gt;
&lt;p&gt;If evidence exposure occurs, quarantine raw and derived artifacts, remove them from retrieval, disable model access, revoke storage and KMS permissions, and rotate exposed credentials. Apply retention and deletion policy to every copy without deleting the security-event record or its hashes. Verify caches, indexes, backups, and downstream consumers separately.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why the superficial control fails&lt;/th&gt;&lt;th&gt;Required response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Role is read-only&lt;/td&gt;&lt;td&gt;Reads can leak data, lock, spill, and saturate resources&lt;/td&gt;&lt;td&gt;Enforce identity, catalog, budgets, and disclosure limits&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;pg_stat_statements&lt;/code&gt; is normalized&lt;/td&gt;&lt;td&gt;Representative text and object names remain sensitive&lt;/td&gt;&lt;td&gt;Minimize and redact before inference&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Collector runs on a replica&lt;/td&gt;&lt;td&gt;Replica lag and local resources remain exposed&lt;/td&gt;&lt;td&gt;Budget by instance and protect recovery objectives&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;pgAudit is enabled&lt;/td&gt;&lt;td&gt;Coverage, classes, storage, and retention may still be wrong&lt;/td&gt;&lt;td&gt;Test expected and denied events end to end&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Database Activity Streams is isolated from DBAs&lt;/td&gt;&lt;td&gt;Full SQL and parameters still reach the audit consumer&lt;/td&gt;&lt;td&gt;Restrict consumers and sanitize any analytic derivative&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;IAM token lasts 15 minutes&lt;/td&gt;&lt;td&gt;Established sessions and database grants are separate&lt;/td&gt;&lt;td&gt;Bound pool, session, role, and broker lifetime&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Prompt injection detection is enabled&lt;/td&gt;&lt;td&gt;Detection does not authorize tools&lt;/td&gt;&lt;td&gt;Keep policy and credentials outside the model&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Everything is redacted&lt;/td&gt;&lt;td&gt;Causal relationships disappear&lt;/td&gt;&lt;td&gt;Use approved pseudonyms and preserve restricted originals&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Customer Data:&lt;/strong&gt; The LLM must not receive bind values, &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; output containing literal filter constants, &lt;code&gt;pg_stat_activity.query&lt;/code&gt; text with embedded parameters, or table contents. Send normalized &lt;code&gt;pg_stat_statements&lt;/code&gt; &lt;code&gt;queryid&lt;/code&gt; and digest text, plan shapes, and timings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Production Execution:&lt;/strong&gt; The LLM cannot run &lt;code&gt;VACUUM FULL&lt;/code&gt;, &lt;code&gt;REINDEX&lt;/code&gt;, &lt;code&gt;ALTER SYSTEM&lt;/code&gt;, &lt;code&gt;pg_terminate_backend&lt;/code&gt;, index DDL, or parameter changes. It proposes; a human authorizes, applies, validates, and holds the rollback condition.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Causal Conclusion Without Contradicting Evidence:&lt;/strong&gt; Every hypothesis must cite both the telemetry that supports it and the telemetry that would falsify it. A ranked hypothesis with no disconfirming test attached is an assertion, not a diagnosis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Silent Gap-Filling:&lt;/strong&gt; Where the evidence pack lacks a required signal, the LLM must name it explicitly under missing evidence rather than inferring a plausible value. “Not collected” and “collected and normal” are different findings and must never be merged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Change Without a Human Gate:&lt;/strong&gt; Production changes require human authorization, a stated validation signal, and a defined rollback condition agreed before the change is applied.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; PostgreSQL performance evidence crosses privileged statistics, SQL text, engine logs, host or cloud telemetry, and model output; read-only access does not contain disclosure or resource risk.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Separate identities, expose only versioned collectors, enforce runtime budgets, sanitize before inference, reconcile audit planes, and reserve every production change for a distinct human-approved path.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Seed a literal, SQL comment, sensitive identifier, forbidden collector, oversized result, expired role, and prohibited cloud action. Sensitive content must be removed or pseudonymized; unsafe requests must fail; every decision must remain attributable.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Inventory current monitoring grants and evidence fields, then run denial, load, leakage, audit, revocation, and recovery tests before adding another PostgreSQL source to the LLM workflow.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The next article begins the Oracle track by using DB time, average active sessions, waits, host evidence, and workload changes to classify an incident before SQL tuning begins.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/predefined-roles.html&quot;&gt;PostgreSQL 18 — Predefined roles&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/pgstatstatements.html&quot;&gt;PostgreSQL 18 — pg_stat_statements&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/sql-set-transaction.html&quot;&gt;PostgreSQL 18 — SET TRANSACTION&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/runtime-config-client.html&quot;&gt;PostgreSQL 18 — Client connection defaults and timeouts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/runtime-config-logging.html&quot;&gt;PostgreSQL 18 — Error reporting and logging&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/sql-createfunction.html&quot;&gt;PostgreSQL 18 — Writing SECURITY DEFINER functions safely&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/pgaudit/pgaudit&quot;&gt;pgAudit — PostgreSQL Audit Extension&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/UsingWithRDS.IAMDBAuth.html&quot;&gt;Amazon Aurora — IAM database authentication&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.CloudWatch.html&quot;&gt;Amazon Aurora — Publishing PostgreSQL logs to CloudWatch Logs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/logging-using-cloudtrail.html&quot;&gt;Amazon Aurora — Monitoring API calls in CloudTrail&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/DBActivityStreams.Monitoring.html&quot;&gt;Amazon Aurora — Monitoring Database Activity Streams&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/DBActivityStreams.ManagingAccess.html&quot;&gt;Amazon Aurora — Database Activity Streams access policy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>ai-engineering</category><category>architecture</category></item><item><title>Aurora PostgreSQL Performance Engineering: Storage, WAL, Replicas and Failover</title><link>https://rajivonai.com/blog/2025-11-25-aurora-postgresql-performance-engineering-storage-wal-replicas-failover/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-11-25-aurora-postgresql-performance-engineering-storage-wal-replicas-failover/</guid><description>An Aurora PostgreSQL incident workflow for separating writer pressure, shared-storage activity, local temporary I/O, WAL retention, replica lag, and application recovery after failover.</description><pubDate>Tue, 25 Nov 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Aurora can promote a reader while the application remains unavailable: database failover, endpoint convergence, connection recovery, cache warming, and workload stabilization are different events.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Aurora PostgreSQL preserves PostgreSQL behavior but changes the infrastructure boundary. A writer and as many as 15 readers attach to a distributed cluster volume. Each instance still has its own compute, memory, buffer cache, network path, and local temporary storage.&lt;/p&gt;
&lt;p&gt;That creates three scopes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;instance:&lt;/strong&gt; writer or one reader has CPU, memory, connection, cache, or local-temp pressure;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;cluster:&lt;/strong&gt; workload, shared-volume activity, WAL retention, topology, or control-plane events affect several instances;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;application:&lt;/strong&gt; endpoint use, DNS caching, pool behavior, retries, and read-after-write assumptions determine whether recovery is visible to users.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The LLM must preserve these scopes. Combining every signal into “Aurora storage is slow” destroys diagnostic context.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Aurora’s managed design invites shortcuts that would be wrong on EC2 and are still wrong here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;treating &lt;code&gt;VolumeReadIOPs&lt;/code&gt; as a provisioned-IOPS saturation gauge;&lt;/li&gt;
&lt;li&gt;treating every &lt;code&gt;IO:XactSync&lt;/code&gt; wait as an Aurora storage incident;&lt;/li&gt;
&lt;li&gt;averaging replica lag across readers and hiding one unhealthy instance;&lt;/li&gt;
&lt;li&gt;assuming shared data storage means shared buffer caches or shared temporary storage;&lt;/li&gt;
&lt;li&gt;confusing Aurora reader lag with logical-replication slot retention;&lt;/li&gt;
&lt;li&gt;declaring recovery when RDS reports promotion complete.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Aurora removes EBS administration, not I/O attribution. Cluster counters, instance waits, local storage, WAL, readers, and service events answer different questions.&lt;/p&gt;
&lt;p&gt;Did the incident originate in PostgreSQL execution, writer commits, shared-volume reads, local temporary work, a reader, retained WAL, failover selection, or application reconnection?&lt;/p&gt;
&lt;h2 id=&quot;build-an-aurora-specific-evidence-pack&quot;&gt;Build an Aurora-Specific Evidence Pack&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[application latency errors retries and pool state] --&gt; H[time-aligned Aurora incident]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B[cluster endpoint reader endpoint and instance topology] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[writer load waits commits and WAL] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[per-reader load lag cache and pending reads] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E[cluster-volume read write and storage metrics] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F[instance-local temp storage and memory] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G[RDS events failover tiers and parameter changes] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[deterministic scope classification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[LLM evidence correlation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[ranked hypotheses contradictions and gaps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; L[DBA and application-owner verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Keep instance and cluster dimensions. A cluster counter cannot identify responsible SQL; a writer wait cannot prove a service-wide fault.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Evidence pattern&lt;/th&gt;&lt;th&gt;Leading classification&lt;/th&gt;&lt;th&gt;What it does not prove&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Writer DB load and &lt;code&gt;IO:XactSync&lt;/code&gt; rise with commit throughput&lt;/td&gt;&lt;td&gt;Commit path or workload shape&lt;/td&gt;&lt;td&gt;Aurora storage is degraded&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cluster &lt;code&gt;VolumeReadIOPs&lt;/code&gt; rises with one reader’s &lt;code&gt;IO:DataFileRead&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Reader cache misses or scan workload&lt;/td&gt;&lt;td&gt;A provisioned IOPS limit was reached&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;FreeLocalStorage&lt;/code&gt; falls with temp-file waits&lt;/td&gt;&lt;td&gt;Local sort, hash, or index-build pressure&lt;/td&gt;&lt;td&gt;Cluster volume is full&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One reader’s lag and pending reads rise&lt;/td&gt;&lt;td&gt;Reader-specific apply, resource, or workload pressure&lt;/td&gt;&lt;td&gt;Every reader is unhealthy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Logical slot lag and transaction-log usage rise&lt;/td&gt;&lt;td&gt;Consumer or slot retention problem&lt;/td&gt;&lt;td&gt;Aurora replica promotion is impaired&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;RDS failover event completes but errors continue&lt;/td&gt;&lt;td&gt;Endpoint, pool, DNS, retry, or cache recovery&lt;/td&gt;&lt;td&gt;Promotion failed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Post-failover reads and latency spike&lt;/td&gt;&lt;td&gt;Cold cache or workload relocation&lt;/td&gt;&lt;td&gt;Data was copied during promotion&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;h3 id=&quot;1-freeze-topology-identity-and-service-events&quot;&gt;1. Freeze topology, identity, and service events&lt;/h3&gt;
&lt;p&gt;Record cluster ARN, version, writer, readers, instance classes, Availability Zones, promotion tiers, parameter groups, storage, endpoints, and type. Preserve contemporaneous RDS events and changes.&lt;/p&gt;
&lt;p&gt;Confirm where each application connects. The cluster endpoint follows the writer; the reader endpoint distributes new read connections across eligible readers but does not rebalance sessions already open. Instance endpoints pin callers to one DB instance. During failover, the reader endpoint can briefly direct connections to the newly promoted writer, so endpoint name alone is not proof of role.&lt;/p&gt;
&lt;p&gt;This decision tree targets a standard single-Region cluster. Serverless v2, Optimized Reads, Limitless Database, and Global Database add different metrics; label them rather than mixing baselines.&lt;/p&gt;
&lt;h3 id=&quot;2-classify-db-load-and-waits-per-instance&quot;&gt;2. Classify DB load and waits per instance&lt;/h3&gt;
&lt;p&gt;Use Database Insights or an equivalent collector to decompose DB load by instance, wait, SQL, host, and user. Keep writer and readers separate.&lt;/p&gt;
&lt;p&gt;Interpret Aurora waits using Aurora documentation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;IO:XactSync&lt;/code&gt; means a backend is awaiting an Aurora storage acknowledgement for transaction completion; AWS also lists CPU pressure, network pressure, and excessive commit frequency as possible contributors.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;IO:DataFileRead&lt;/code&gt; means a required page was not in that instance’s shared memory and had to be read.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;IO:BufFileRead&lt;/code&gt; and &lt;code&gt;IO:BufFileWrite&lt;/code&gt; identify temporary-file work.&lt;/li&gt;
&lt;li&gt;locks, client waits, CPU, and IPC can still dominate without any Aurora storage problem.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At the technology freeze, AWS was moving the console workflow toward CloudWatch Database Insights. Record the mode, retention, and enabled features. DB load visibility does not imply plan history exists.&lt;/p&gt;
&lt;h3 id=&quot;3-separate-shared-volume-local-temporary-storage-and-retained-wal&quot;&gt;3. Separate shared volume, local temporary storage, and retained WAL&lt;/h3&gt;
&lt;p&gt;The cluster volume stores durable data across Availability Zones. &lt;code&gt;VolumeReadIOPs&lt;/code&gt; and &lt;code&gt;VolumeWriteIOPs&lt;/code&gt; are cluster activity counters reported in five-minute intervals; AWS describes the read metric as billed reads, not an EBS saturation percentage. Correlate rates with waits, SQL, cache state, and instance load.&lt;/p&gt;
&lt;p&gt;Temporary sort, hash, and index-build files use instance-local storage. Track &lt;code&gt;FreeLocalStorage&lt;/code&gt;, temp-file waits, and PostgreSQL temp-block deltas per instance. A reader can exhaust local space while the shared cluster volume remains healthy.&lt;/p&gt;
&lt;p&gt;Logical replication and DMS introduce a third storage question. Inspect &lt;code&gt;pg_replication_slots&lt;/code&gt;, consumer health, &lt;code&gt;OldestReplicationSlotLag&lt;/code&gt;, and &lt;code&gt;TransactionLogsDiskUsage&lt;/code&gt;. Aurora readers use shared storage; a stalled logical consumer retains WAL through a slot. These require different remediations.&lt;/p&gt;
&lt;p&gt;For ordinary writer pressure, collect reset-aware &lt;code&gt;pg_stat_wal&lt;/code&gt; deltas with commit throughput and &lt;code&gt;IO:XactSync&lt;/code&gt;. This separates more changed data from the same work fragmented into more commits.&lt;/p&gt;
&lt;h3 id=&quot;4-diagnose-every-reader-independently&quot;&gt;4. Diagnose every reader independently&lt;/h3&gt;
&lt;p&gt;Do not stop at cluster lag extrema. Pair each reader’s &lt;code&gt;AuroraReplicaLag&lt;/code&gt; with CPU, memory, connections, DB load, waits, local storage, instance class, and traffic.&lt;/p&gt;
&lt;p&gt;Aurora PostgreSQL also exposes cluster status through a read-only function:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; server_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       CASE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; WHEN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; session_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;MASTER_SESSION_ID&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; THEN&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;writer&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ELSE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;reader&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; END&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; role&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       replica_lag_in_msec,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       pending_read_ios,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       log_stream_speed_in_kib_per_second&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; aurora_replica_status();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Timestamp the sample. AWS documents &lt;code&gt;AuroraReplicaLag&lt;/code&gt; in terms of the reader page cache, not self-managed physical-standby byte lag. Correlate it with writer changes, reader size, traffic, memory, long queries, and events.&lt;/p&gt;
&lt;h3 id=&quot;5-reconstruct-failover-as-a-multi-stage-timeline&quot;&gt;5. Reconstruct failover as a multi-stage timeline&lt;/h3&gt;
&lt;p&gt;Build timestamps for:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;failure detected&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;promotion started&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;target selected&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;new writer available&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;cluster endpoint changed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;old connections failed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;new connections succeeded&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;write health restored&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;cache and latency returned to baseline&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Verify that promotion tiers select an adequately sized reader in another Availability Zone. Inspect DNS caching, TCP keepalive, pool validation, retry backoff, transaction safety, and endpoint use. Promotion does not move existing connections.&lt;/p&gt;
&lt;p&gt;Post-failover latency needs proof because each instance owns its cache. Cluster cache management can warm one designated tier-0 reader, but AWS requires matching writer and reader instance classes and topology. Verify it.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Aurora PostgreSQL latency or availability incident] --&gt; B{RDS event or topology change}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| C[reconstruct promotion endpoint pool and cache timeline]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| D{one instance or whole cluster}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|one instance| E{reader lag or local temp pressure}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|lag| F[correlate reader resources workload and pending reads]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|temp| G[identify spilling SQL or index work]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|cluster| H{commit waits and write rate elevated}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| I[separate commit frequency CPU network and service evidence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| J{logical slot retention rising}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|yes| K[verify consumer progress and slot ownership]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|no| L[return to SQL locks client waits and upstream services]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;llm-correlation-contract&quot;&gt;LLM Correlation Contract&lt;/h2&gt;
&lt;p&gt;Require claims at the correct scope:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;OBSERVATION&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Reader r2 lag and pending reads increased while r1 remained at baseline [replica-08].&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;HYPOTHESIS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;r2 has instance-specific apply or read-workload pressure; cluster-wide storage failure is contradicted.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;MISSING EVIDENCE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;r2 DB load by wait and its read-traffic distribution for the same interval.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ACTION&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Collect the approved per-instance Database Insights slice and reader-endpoint connection distribution.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The model may recommend a collector. It may not fail over, delete a slot, modify tiers, resize instances, or change durability.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;AWS documents &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Overview.StorageReliability.html&quot;&gt;Aurora storage&lt;/a&gt; as one cluster volume copied across three Availability Zones, independent of instance count, with separate local storage for temporary work. Shared durability therefore does not imply shared compute, cache, or temporary capacity.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.AuroraMonitoring.Metrics.html&quot;&gt;Aurora metric reference&lt;/a&gt; defines volume I/O at cluster scope and &lt;code&gt;AuroraReplicaLag&lt;/code&gt; at reader scope. The incident evidence pack must retain those dimensions.&lt;/p&gt;
&lt;p&gt;AWS’s &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Tuning.html&quot;&gt;Aurora PostgreSQL wait guide&lt;/a&gt; warns that some waits differ from upstream PostgreSQL. Its &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/apg-waits.xactsync.html&quot;&gt;&lt;code&gt;IO:XactSync&lt;/code&gt; guidance&lt;/a&gt; names workload, CPU, and network contributors. A wait locates time; it does not identify the failed component alone.&lt;/p&gt;
&lt;p&gt;AWS documents &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora_replica_status.html&quot;&gt;&lt;code&gt;aurora_replica_status()&lt;/code&gt;&lt;/a&gt; as exposing lag, LSN, log speed, and pending reads. Its &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.BestPractices.FastFailover.html&quot;&gt;failover guidance&lt;/a&gt; separately covers endpoints, DNS, TCP, and application testing. Database and application recovery therefore need different timestamps.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven boundary&lt;/th&gt;&lt;th&gt;Immediate containment&lt;/th&gt;&lt;th&gt;Durable direction&lt;/th&gt;&lt;th&gt;Validate with&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Excess commit frequency&lt;/td&gt;&lt;td&gt;Reduce retry or commit amplification&lt;/td&gt;&lt;td&gt;Batch transactions without weakening correctness&lt;/td&gt;&lt;td&gt;Commit rate, &lt;code&gt;IO:XactSync&lt;/code&gt;, latency, errors&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reader-specific pressure&lt;/td&gt;&lt;td&gt;Remove or redirect read traffic from that reader&lt;/td&gt;&lt;td&gt;Right-size and control reader workload placement&lt;/td&gt;&lt;td&gt;Per-reader lag, DB load, pending reads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Local temp exhaustion&lt;/td&gt;&lt;td&gt;Cancel approved runaway work; defer index builds&lt;/td&gt;&lt;td&gt;Query reduction, scoped memory, larger or optimized local storage&lt;/td&gt;&lt;td&gt;&lt;code&gt;FreeLocalStorage&lt;/code&gt;, temp blocks, memory, latency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Logical-slot retention&lt;/td&gt;&lt;td&gt;Restore the owned consumer when safe&lt;/td&gt;&lt;td&gt;Slot ownership, lag budgets, consumer monitoring&lt;/td&gt;&lt;td&gt;Slot lag, transaction-log usage, consumer offset&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bad failover target&lt;/td&gt;&lt;td&gt;Use only an approved recovery path&lt;/td&gt;&lt;td&gt;Correct promotion tiers and capacity symmetry&lt;/td&gt;&lt;td&gt;Promotion choice, recovery timeline, peak load&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cold-cache recovery&lt;/td&gt;&lt;td&gt;Protect the new writer from immediate overload&lt;/td&gt;&lt;td&gt;Validate cluster cache management where supported&lt;/td&gt;&lt;td&gt;Read waits, cache state, post-failover latency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application reconnection&lt;/td&gt;&lt;td&gt;Shed retries and recycle stale connections safely&lt;/td&gt;&lt;td&gt;Endpoint, DNS, keepalive, pool, and retry design&lt;/td&gt;&lt;td&gt;Connection success, write probe, error rate&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;Observability changes can add cost and overhead. If Advanced Database Insights, detailed plan capture, Enhanced Monitoring, or log export was enabled for the investigation, record the previous mode and retention before change; revert after the approved evidence window if that was the plan.&lt;/p&gt;
&lt;p&gt;For topology changes, preserve the former promotion tiers and instance configuration. Validate a resized or replacement reader before returning traffic. Deleting a logical slot can discard a consumer’s required WAL and is not a reversible cleanup; require consumer ownership and a rebuild plan. A failover is also not a parameter rollback—applications see broken sessions and in-flight transactions may need safe retry. Define abort criteria before testing it.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Misleading conclusion&lt;/th&gt;&lt;th&gt;Correction&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Cluster metrics assigned to one SQL&lt;/td&gt;&lt;td&gt;The top query caused every volume I/O&lt;/td&gt;&lt;td&gt;Correlate query deltas, waits, and cache misses&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Billed I/O treated as a saturation gauge&lt;/td&gt;&lt;td&gt;Aurora reached an EBS IOPS ceiling&lt;/td&gt;&lt;td&gt;Use the metric as activity and cost evidence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Shared volume treated as shared cache&lt;/td&gt;&lt;td&gt;Reader promotion must be warm&lt;/td&gt;&lt;td&gt;Measure cache recovery or validate CCM&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lag maximum used without instance evidence&lt;/td&gt;&lt;td&gt;All replicas need resizing&lt;/td&gt;&lt;td&gt;Diagnose each reader and its traffic&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aurora reader and logical slot conflated&lt;/td&gt;&lt;td&gt;Deleting a slot repairs reader lag&lt;/td&gt;&lt;td&gt;Keep physical visibility and logical retention separate&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Promotion completion equals recovery&lt;/td&gt;&lt;td&gt;The outage ended at the RDS event&lt;/td&gt;&lt;td&gt;Measure endpoint, connection, write, and latency recovery&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LLM sees an event after a spike&lt;/td&gt;&lt;td&gt;Failover caused the original incident&lt;/td&gt;&lt;td&gt;Reconstruct ordering and preserve contradictions&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Customer Data:&lt;/strong&gt; The LLM must not receive bind values, &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; output containing literal filter constants, &lt;code&gt;pg_stat_activity.query&lt;/code&gt; text with embedded parameters, or table contents. Send normalized &lt;code&gt;pg_stat_statements&lt;/code&gt; &lt;code&gt;queryid&lt;/code&gt; and digest text, plan shapes, and timings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Production Execution:&lt;/strong&gt; The LLM cannot run &lt;code&gt;VACUUM FULL&lt;/code&gt;, &lt;code&gt;REINDEX&lt;/code&gt;, &lt;code&gt;ALTER SYSTEM&lt;/code&gt;, &lt;code&gt;pg_terminate_backend&lt;/code&gt;, index DDL, or parameter changes. It proposes; a human authorizes, applies, validates, and holds the rollback condition.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Causal Conclusion Without Contradicting Evidence:&lt;/strong&gt; Every hypothesis must cite both the telemetry that supports it and the telemetry that would falsify it. A ranked hypothesis with no disconfirming test attached is an assertion, not a diagnosis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Silent Gap-Filling:&lt;/strong&gt; Where the evidence pack lacks a required signal, the LLM must name it explicitly under missing evidence rather than inferring a plausible value. “Not collected” and “collected and normal” are different findings and must never be merged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Change Without a Human Gate:&lt;/strong&gt; Production changes require human authorization, a stated validation signal, and a defined rollback condition agreed before the change is applied.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Aurora PostgreSQL separates compute from durable storage, while replica visibility, local temporary work, WAL retention, and application recovery remain distinct failure paths.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Preserve cluster and instance scope, correlate Aurora-specific waits with correctly interpreted metrics, and model failover as a staged timeline.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Rehearse a reader overload, logical-consumer stall, temp-space incident, and controlled failover. The workflow should route each to a different owner and reject destructive action without approval.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Add topology snapshots, per-reader lag, local-storage evidence, logical-slot retention, RDS events, and application reconnection milestones to the incident evidence pack.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The next article adds the PostgreSQL security boundary: roles, SQL-text exposure, audit evidence, log controls, collector permissions, model redaction, approval gates, and actions the LLM must never perform.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Overview.StorageReliability.html&quot;&gt;AWS — Amazon Aurora storage&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Concepts.AuroraHighAvailability.html&quot;&gt;AWS — High availability for Amazon Aurora&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.AuroraMonitoring.Metrics.html&quot;&gt;AWS — CloudWatch metrics for Amazon Aurora&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Tuning.html&quot;&gt;AWS — Tuning with wait events for Aurora PostgreSQL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/apg-waits.xactsync.html&quot;&gt;AWS — IO:XactSync&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora_replica_status.html&quot;&gt;AWS — aurora_replica_status&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Database-Insights.html&quot;&gt;AWS — CloudWatch Database Insights&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.BestPractices.FastFailover.html&quot;&gt;AWS — Fast failover with Aurora PostgreSQL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.cluster-cache-mgmt.html&quot;&gt;AWS — Cluster cache management for Aurora PostgreSQL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Replication.Logical.html&quot;&gt;AWS — Logical replication with Aurora PostgreSQL&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>ai-engineering</category><category>failures</category></item><item><title>Top GitHub Breakouts: October 2025 (Part 2)</title><link>https://rajivonai.com/blog/2025-11-22-github-stars-oct-2025/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-11-22-github-stars-oct-2025/</guid><description>October&apos;s memory and retrieval breakouts: a structured agent memory framework with benchmarks, a self-hosted cognitive memory engine, and sub-10ms semantic search without a vector database cluster.</description><pubDate>Sat, 22 Nov 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;AI agents that forget everything between sessions are not AI assistants — they are expensive autocomplete.&lt;/strong&gt; Engineers building production agents in October spent significant effort maintaining session state manually, writing custom retrieval logic, or paying the latency cost of round-tripping to hosted vector databases. Three breakout repos from the month target these hand-rolled approaches directly: a structured framework for building and benchmarking agent memory systems, a self-hosted cognitive memory engine that abstracts storage from the memory interface, and a sub-10ms semantic search runtime that eliminates the vector database round-trip entirely.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Production AI agents face a compounding state problem: every new session starts from zero, forcing users to re-provide context, or forcing engineers to build ad-hoc session stores. When teams do add memory, they assemble it from scratch — custom vector embeddings, TTL logic, retrieval scoring — and discover the result is untestable because there are no standard benchmarks for memory quality. The retrieval step that populates each agent turn adds 50–200ms of latency, slow enough for users to notice.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Domain&lt;/th&gt;&lt;th&gt;Manual bottleneck&lt;/th&gt;&lt;th&gt;What it costs&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;System design&lt;/td&gt;&lt;td&gt;Agent memory implemented ad hoc per project — custom embedding, custom TTL, custom retrieval ranking&lt;/td&gt;&lt;td&gt;Memory bugs are invisible until the agent surfaces stale context at a critical moment&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AI engineering&lt;/td&gt;&lt;td&gt;No standard benchmark for comparing memory system quality&lt;/td&gt;&lt;td&gt;Teams cannot detect whether retrieval is degrading over time without building custom eval harnesses&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Databases / storage&lt;/td&gt;&lt;td&gt;Persistent memory requires a hosted vector database plus embedding pipelines plus per-user namespacing&lt;/td&gt;&lt;td&gt;Infrastructure complexity scales with the number of users; ops burden grows before any memory logic ships&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;System design&lt;/td&gt;&lt;td&gt;Semantic retrieval round-trips to hosted vector databases add 50–200ms per agent turn&lt;/td&gt;&lt;td&gt;Agents pause noticeably on context assembly; RAG pipelines slow proportionally&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Can the memory and retrieval tooling available today eliminate these hand-rolled systems while remaining testable and operationally simple?&lt;/p&gt;
&lt;h2 id=&quot;eliminating-agent-amnesia-memory-architecture-persistent-storage-and-fast-retrieval&quot;&gt;Eliminating Agent Amnesia: Memory Architecture, Persistent Storage, and Fast Retrieval&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Agent amnesia — 3 layers of manual work] --&gt; B[No standard memory architecture or evaluation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; C[No persistent cross-session state without a vector DB]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; D[Retrieval adds 50-200ms to every agent turn]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[EverMind-AI/EverOS]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; F[CaviraOSS/OpenMemory]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; G[usemoss/moss]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; H[Interchangeable memory methods with open benchmarks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; I[Cognitive memory on SQLite or Postgres — no separate vector DB]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; J[Sub-10ms semantic search — no network hop]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;evermind-aieveros--agent-memory-architecture-without-custom-eval-infrastructure&quot;&gt;EverMind-AI/EverOS — Agent Memory Architecture Without Custom Eval Infrastructure&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: Building agent memory requires making architectural decisions — what to store, how long to keep it, how to rank retrieval — with no standard way to measure whether those decisions are correct or degrading over time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How AI replaces or accelerates that task&lt;/strong&gt;: EverOS provides three components together: use-case implementations showing what persistent memory enables in real workflows, interchangeable architecture methods (the memory algorithms themselves, swappable without rewriting the agent), and open benchmark suites for measuring memory quality and agent self-evolution. According to the project documentation, it is “organized around three essential parts — use cases, architecture methods, and benchmarks — that together eliminate the need to build custom evaluation infrastructure.” At the center is EverCore, described as a “long-term memory operating system for agents.”&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The workflow&lt;/strong&gt;:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; clone&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; https://github.com/EverMind-AI/EverOS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pip&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; evercore&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Start with a use case to see what memory enables in practice&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; use-cases/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Run benchmarks to establish a memory quality baseline&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; benchmarks/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Follow README quickstart — output is a quality score for the current memory method&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Swap architecture methods to compare retrieval approaches&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; methods/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Replace the method, re-run benchmarks, compare scores&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: EverOS provides the framework for comparing memory architectures but does not prescribe a single production-ready method — teams still decide which architecture to deploy. The benchmarks measure memory quality; they do not measure the throughput cost of running memory retrieval at production query rates.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;caviraossopenmemory--persistent-agent-memory-without-a-hosted-vector-database&quot;&gt;CaviraOSS/OpenMemory — Persistent Agent Memory Without a Hosted Vector Database&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: Adding persistent memory to an agent requires hosting a vector database, managing embedding pipelines, and building per-user retrieval namespacing — three separate infrastructure concerns before any memory logic ships.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How AI replaces or accelerates that task&lt;/strong&gt;: OpenMemory provides a cognitive memory engine that stores memories in SQLite or PostgreSQL locally, without requiring a separate vector database. According to the README, it offers “explainable traces (see &lt;em&gt;why&lt;/em&gt; something was recalled)” and integrates with LangChain, CrewAI, AutoGen, and MCP. The API surface is three calls: &lt;code&gt;add&lt;/code&gt;, &lt;code&gt;search&lt;/code&gt;, &lt;code&gt;delete&lt;/code&gt;. &lt;strong&gt;Note: the project README states it is currently undergoing a breaking-changes rewrite — “expect breaking changes and potential bugs.”&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The workflow&lt;/strong&gt;:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pip&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; openmemory-py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; openmemory.client &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Memory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Before: host a vector DB, manage embeddings, write per-user retrieval logic&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# After: three-call API, local SQLite or Postgres storage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;mem &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Memory()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; mem.add(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;user prefers batch processing over streaming&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;user_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;u1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;results &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; mem.search(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;processing preferences&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;user_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;u1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# results include explainable traces showing why each memory was recalled&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
Node SDK:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;npm&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; openmemory-js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;typescript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; { Memory } &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;openmemory-js&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; mem&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; Memory&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; mem.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;user prefers dark mode&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, { user_id: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;u1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; results&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; mem.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;search&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;UI preferences&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, { user_id: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;u1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: The project is currently in a breaking-changes rewrite — production adoption should wait for the rewrite branch to stabilize. The local-first storage model works for single-instance deployments; horizontally scaled agent services need a shared PostgreSQL backend with coordinated writes.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;usemossmoss--sub-10ms-semantic-search-without-a-vector-database-cluster&quot;&gt;usemoss/moss — Sub-10ms Semantic Search Without a Vector Database Cluster&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: RAG pipelines incur 50–200ms of latency on each retrieval call from the round-trip to a hosted vector database, making agent turns noticeably slow and increasing operational cost.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How AI replaces or accelerates that task&lt;/strong&gt;: Moss embeds semantic search directly into the application as an SDK, eliminating the network hop on the retrieval path. According to the README, it delivers “sub-10ms” semantic retrieval using hybrid search (semantic plus keyword) with built-in embeddings. The SDK loads a managed index from Moss Cloud and queries it locally in Python, TypeScript, Elixir, or WebAssembly (browser). The README states: “No network hop on the hot path. No clusters to tune.”&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The workflow&lt;/strong&gt;:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pip&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; moss&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Requires a free-tier project_id and project_key from moss.dev&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; moss &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MossClient, QueryOptions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;client &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MossClient(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;your_project_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;your_project_key&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Before: upload docs to vector DB, wait for indexing, query with network round-trip&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# typical latency: 50–200ms per retrieval call&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# After: create index, load locally, query in &amp;#x3C;10ms&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.create_index(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;support-docs&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;text&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Refunds processed within 3–5 business days.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;2&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;text&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Order tracking available on the dashboard.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.load_index(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;support-docs&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;results &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.query(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;support-docs&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;how long do refunds take?&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    QueryOptions(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;top_k&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# results.time_taken_ms → sub-10ms (documented in README)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: Moss Cloud hosts the backing index — this is not a fully self-hosted deployment. Teams with data sovereignty requirements or air-gapped environments cannot use Moss as currently documented. The WebAssembly in-browser build is noted in the README; the practical limit on in-browser index size is not specified.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;EverMind-AI/EverOS&lt;/strong&gt;: The three-part structure (use cases, methods, benchmarks) and EverCore component are sourced from the README. The benchmark framework’s purpose — enabling comparison without custom eval infrastructure — is documented. I have not run EverOS benchmarks personally; memory quality comparison claims reflect the documented framework design.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CaviraOSS/OpenMemory&lt;/strong&gt;: The Python and Node SDK APIs, storage backend options (SQLite/Postgres), and integration list (LangChain, CrewAI, AutoGen, MCP) are sourced from the README. The active rewrite warning is quoted directly from the README header. Functionality described reflects the documented interface, not a stability guarantee.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;usemoss/moss&lt;/strong&gt;: The sub-10ms latency claim and hybrid retrieval capability are stated in the README and project description. The Moss Cloud hosting model is documented. Retrieval latency at production index sizes (large document corpora) has not been independently benchmarked.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;EverOS benchmark scores don’t reflect production memory set size&lt;/td&gt;&lt;td&gt;Lab benchmarks use small synthetic memory sets; production agent accumulates millions of memories&lt;/td&gt;&lt;td&gt;Run benchmarks at target scale before committing to a memory architecture&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OpenMemory breaking changes break deployed agents&lt;/td&gt;&lt;td&gt;Rewrite branch merges and changes the API mid-deployment&lt;/td&gt;&lt;td&gt;Pin to a specific commit; delay production use until the rewrite stabilizes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OpenMemory multi-instance write conflict&lt;/td&gt;&lt;td&gt;Two agent processes share one user’s memory namespace on SQLite&lt;/td&gt;&lt;td&gt;Switch to the PostgreSQL backend with a shared connection pool; coordinate writes at the application level&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Moss Cloud outage takes down retrieval&lt;/td&gt;&lt;td&gt;Moss Cloud experiences downtime&lt;/td&gt;&lt;td&gt;Add a degraded-mode fallback (BM25 keyword search) for when Moss is unavailable&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Moss in-browser index size exceeds browser memory&lt;/td&gt;&lt;td&gt;Large document corpus loaded into a WebAssembly build&lt;/td&gt;&lt;td&gt;Partition the index; load only the subset relevant to the current session&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;EverOS memory method swap degrades recall without detection&lt;/td&gt;&lt;td&gt;Architecture method changed but benchmarks not re-run&lt;/td&gt;&lt;td&gt;Run the full benchmark suite after every method change; track recall quality as a regression signal&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Agent memory built ad hoc per project is unmeasurable, degrades silently as the memory store grows, and requires maintaining vector database infrastructure before any memory logic ships.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Use EverOS benchmarks to establish a baseline for memory quality before building custom infrastructure; adopt OpenMemory (once the rewrite stabilizes) for self-hosted cognitive memory without a vector database dependency; use Moss where retrieval latency is the binding constraint.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: The earliest signal that EverOS is delivering value is a benchmark run that produces a quality score — that score, tracked across memory method changes, is the first observable evidence that memory is not silently degrading.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Clone EverOS and run the benchmark suite against a small synthetic memory set (&lt;code&gt;cd benchmarks/&lt;/code&gt; → follow the README quickstart) — the output gives a baseline memory quality score before any custom infrastructure is built. That baseline becomes the regression guard for every subsequent change.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>ai-engineering</category><category>architecture</category><category>databases</category></item><item><title>From pg_stat_statements to the Execution Plan: LLM-Assisted PostgreSQL Diagnosis</title><link>https://rajivonai.com/blog/2025-11-20-pg-stat-statements-execution-plan-llm-assisted-postgresql-diagnosis/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-11-20-pg-stat-statements-execution-plan-llm-assisted-postgresql-diagnosis/</guid><description>A safe PostgreSQL workflow for ranking query regressions from interval deltas, capturing the right execution plan, and using an LLM without mistaking correlation for proof.</description><pubDate>Thu, 20 Nov 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The query with the largest lifetime execution time is not necessarily the query that caused today’s incident, and the plan you reproduce afterward is not necessarily the plan that ran during it.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The previous investigation classified the incident boundary: PostgreSQL workload rose while blocking, host pressure, and storage limits were either supported or contradicted by evidence. The next question is attribution. Which query shape changed, and what execution behavior explains the change?&lt;/p&gt;
&lt;p&gt;PostgreSQL provides complementary evidence. &lt;code&gt;pg_stat_statements&lt;/code&gt; aggregates planning and execution counters by database, user, query identifier, and top-level status. &lt;code&gt;EXPLAIN&lt;/code&gt; shows the plan the optimizer would choose in a stated context. &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; executes the statement and adds actual rows, loops, time, buffers, and optional WAL evidence.&lt;/p&gt;
&lt;p&gt;None is a plan-history system. The diagnostic chain must preserve interval, identity, execution context, and plan provenance before an LLM compares anything.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Four shortcuts repeatedly produce false diagnoses:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;sorting cumulative &lt;code&gt;total_exec_time&lt;/code&gt; and calling the first row the incident cause;&lt;/li&gt;
&lt;li&gt;subtracting two lifetime averages instead of calculating counter deltas;&lt;/li&gt;
&lt;li&gt;treating normalized query text as the exact statement and parameter set that ran;&lt;/li&gt;
&lt;li&gt;running &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; in production and assuming the reproduced plan proves historical behavior.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;pg_stat_statements&lt;/code&gt; may combine literal variants, evict low-frequency entries, or reset counters. A query ID has limited stability and can change across major versions or catalog differences. Prepared statements may use custom or generic plans. Statistics, schema, configuration, role, &lt;code&gt;search_path&lt;/code&gt;, and parameter values all influence the plan.&lt;/p&gt;
&lt;p&gt;The core question is: can we connect an interval-level workload change to the execution plan that produced it, while preserving enough contradictory evidence to reject a convenient but unsupported explanation?&lt;/p&gt;
&lt;h2 id=&quot;build-a-query-attribution-chain&quot;&gt;Build a Query Attribution Chain&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[classified PostgreSQL workload incident] --&gt; B[baseline and incident counter snapshots]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[reset-aware interval deltas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[ranked query identity candidates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[version role schema settings and parameter provenance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[safe plan capture method]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[estimated and actual structural evidence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[LLM observations hypotheses gaps and actions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[DBA verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[bounded remediation and validation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The collector does the arithmetic. The LLM receives a compact record with source artifact IDs, not an unrestricted statistics dump or raw application parameters.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Interval evidence&lt;/th&gt;&lt;th&gt;Leading question&lt;/th&gt;&lt;th&gt;What it does not prove&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Execution-time share rises while calls are stable&lt;/td&gt;&lt;td&gt;Did cost per execution rise?&lt;/td&gt;&lt;td&gt;The plan changed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Calls rise while cost per call is stable&lt;/td&gt;&lt;td&gt;Did workload amplification occur?&lt;/td&gt;&lt;td&gt;The SQL is inefficient&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Shared reads per call rise&lt;/td&gt;&lt;td&gt;Did access behavior or working set change?&lt;/td&gt;&lt;td&gt;Physical storage performed each read&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Temp blocks per call rise&lt;/td&gt;&lt;td&gt;Did a sort, hash, or materialization spill?&lt;/td&gt;&lt;td&gt;Global &lt;code&gt;work_mem&lt;/code&gt; should increase&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Rows per call changes&lt;/td&gt;&lt;td&gt;Did parameters or result cardinality change?&lt;/td&gt;&lt;td&gt;Planner estimates were wrong&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;WAL bytes per call rise&lt;/td&gt;&lt;td&gt;Did write amplification change?&lt;/td&gt;&lt;td&gt;WAL configuration is the cause&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Latency rises but statement counters do not&lt;/td&gt;&lt;td&gt;Is time outside executor accounting or collection coverage?&lt;/td&gt;&lt;td&gt;PostgreSQL is innocent&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;h3 id=&quot;1-verify-collection-coverage-before-ranking-queries&quot;&gt;1. Verify collection coverage before ranking queries&lt;/h3&gt;
&lt;p&gt;Confirm &lt;code&gt;pg_stat_statements&lt;/code&gt; is preloaded, query identifiers are computed, the extension exists in the database being queried, and the monitoring role can see the required fields. Record these settings:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; current_setting(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;server_version&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; server_version,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       current_setting(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;compute_query_id&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; compute_query_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       current_setting(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;pg_stat_statements.track&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; statement_track,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       current_setting(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;pg_stat_statements.track_planning&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; planning_track;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stats_reset, dealloc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_statements_info;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A rising &lt;code&gt;dealloc&lt;/code&gt; count means statement churn exceeded the configured entry capacity and low-frequency history was discarded. Planning counters remain zero when planning tracking is disabled; enabling it is a deliberate overhead decision, not an incident-time reflex.&lt;/p&gt;
&lt;h3 id=&quot;2-rank-interval-deltas-not-cumulative-totals&quot;&gt;2. Rank interval deltas, not cumulative totals&lt;/h3&gt;
&lt;p&gt;Snapshot counters at the start and end of baseline, transition, incident, and recovery windows. Retain &lt;code&gt;dbid&lt;/code&gt;, &lt;code&gt;userid&lt;/code&gt;, &lt;code&gt;toplevel&lt;/code&gt;, &lt;code&gt;queryid&lt;/code&gt;, &lt;code&gt;stats_since&lt;/code&gt;, and the module reset time alongside the measurements.&lt;/p&gt;
&lt;p&gt;For each identity, derive:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;calls in window          = calls end minus calls start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;execution time in window = total exec time end minus total exec time start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;milliseconds per call    = execution time in window divided by calls in window&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;rows per call            = rows delta divided by calls in window&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;blocks per call          = block delta divided by calls in window&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;WAL bytes per call       = WAL byte delta divided by calls in window&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Do not subtract &lt;code&gt;mean_exec_time&lt;/code&gt; values. Recompute the interval mean from additive counters. Reject a delta when its entry appeared after the first snapshot, &lt;code&gt;stats_since&lt;/code&gt; changed, the module reset crossed the window, or identity cannot be joined safely. Rank both total contribution and per-call regression: a cheap query called far more often can dominate an incident.&lt;/p&gt;
&lt;h3 id=&quot;3-reconstruct-execution-context&quot;&gt;3. Reconstruct execution context&lt;/h3&gt;
&lt;p&gt;Treat &lt;code&gt;(dbid, userid, queryid, toplevel)&lt;/code&gt; as the local identity, not &lt;code&gt;queryid&lt;/code&gt; alone. Store server major version, database, role, &lt;code&gt;search_path&lt;/code&gt;, relevant planner settings, schema migration and statistics timestamps, prepared-statement behavior, parameter types, and sanitized parameter classes.&lt;/p&gt;
&lt;p&gt;The representative SQL replaces many constants with &lt;code&gt;$1&lt;/code&gt;, &lt;code&gt;$2&lt;/code&gt;, and similar symbols. It is not a safe source of production literals. Identical text can map to different identities under different object resolution, while unrelated statements have a small hash-collision risk. Never correlate query IDs across a major upgrade without direct validation.&lt;/p&gt;
&lt;h3 id=&quot;4-choose-the-least-risky-plan-evidence&quot;&gt;4. Choose the least risky plan evidence&lt;/h3&gt;
&lt;p&gt;Start with plain &lt;code&gt;EXPLAIN (SETTINGS, FORMAT JSON)&lt;/code&gt;: it plans but does not execute the statement. Use &lt;code&gt;EXPLAIN (GENERIC_PLAN, FORMAT JSON)&lt;/code&gt; when the normalized statement contains placeholders and a generic plan is the question; it cannot be combined with &lt;code&gt;ANALYZE&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Use runtime plans only through an approved route:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;capture the plan that naturally ran through bounded &lt;code&gt;auto_explain&lt;/code&gt; sampling;&lt;/li&gt;
&lt;li&gt;reproduce on a current physical replica only when replay state and read-only constraints are understood;&lt;/li&gt;
&lt;li&gt;reproduce in a production-like environment with the same statistics, schema, settings, and parameter class;&lt;/li&gt;
&lt;li&gt;run a bounded read-only &lt;code&gt;EXPLAIN (ANALYZE, TIMING FALSE, FORMAT JSON)&lt;/code&gt; only after estimating execution risk, locks, result work, and resource consumption.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;ANALYZE&lt;/code&gt; executes the statement. Wrapping write-bearing analysis in &lt;code&gt;BEGIN&lt;/code&gt; and &lt;code&gt;ROLLBACK&lt;/code&gt; prevents committed table changes, but it does not make expensive work, locks, sequence effects, functions, or external side effects harmless. Do not let the LLM select or execute this route.&lt;/p&gt;
&lt;h3 id=&quot;5-read-structure-before-recommending-a-fix&quot;&gt;5. Read structure before recommending a fix&lt;/h3&gt;
&lt;p&gt;Compare estimates with actual rows at each node, and multiply actual rows and time by &lt;code&gt;loops&lt;/code&gt; where repeated execution matters. Then inspect filters, rows removed, join fan-out, sort method, hash batches, shared and temp blocks, WAL, workers planned versus launched, and settings captured with the plan.&lt;/p&gt;
&lt;p&gt;Costs are planner units, not milliseconds. A sequential scan is not inherently wrong. A nested loop is not inherently wrong. Shared reads are not proof of physical device I/O because the operating system may satisfy them from its cache. A plan captured after &lt;code&gt;ANALYZE&lt;/code&gt;, DDL, configuration changes, or cache transition is a new observation—not historical proof.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[incident query candidate] --&gt; B{valid interval deltas and stable identity}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| C[repair collection gap]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| D{calls or cost per call changed}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|calls| E[investigate application workload amplification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|cost| F{historical plan artifact exists}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|yes| G[compare plan structure and provenance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|no| H[capture safe current runtime evidence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; I{estimates diverge from actuals}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|yes| J[inspect statistics skew correlation and parameters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|no| K{temp blocks or repeated loops dominate}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|yes| L[investigate spill or join fan-out]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|no| M[correlate waits I/O locks and application time]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;llm-evidence-contract&quot;&gt;LLM Evidence Contract&lt;/h2&gt;
&lt;p&gt;Require the model to return four separate objects:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;OBSERVATION&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Incident execution-time delta increased for identity dbid userid queryid toplevel [pps-delta-04].&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;HYPOTHESIS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;A row-estimation error may have amplified the inner side of a nested loop.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;MISSING EVIDENCE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;The incident plan and parameter class are unavailable; the current plan is not historical proof.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ACTION&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Capture a sampled runtime plan for the same identity and compare estimated rows actual rows and loops.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Confidence must fall when provenance is missing. The output must identify evidence against each leading hypothesis and must not translate “Seq Scan observed” directly into “create an index.”&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.postgresql.org/docs/18/pgstatstatements.html&quot;&gt;PostgreSQL 18 &lt;code&gt;pg_stat_statements&lt;/code&gt; documentation&lt;/a&gt; defines each entry by database, user, query ID, and top-level status. It documents normalization, possible hash collisions, entry deallocation, reset timestamps, and limited query-ID stability. It also states that planning tracking is off by default and can impose noticeable contention overhead. The derived operational pattern is to capture additive counters and collector health before performing any LLM analysis.&lt;/p&gt;
&lt;p&gt;PostgreSQL’s &lt;a href=&quot;https://www.postgresql.org/docs/18/sql-explain.html&quot;&gt;EXPLAIN command reference&lt;/a&gt; states that &lt;code&gt;ANALYZE&lt;/code&gt; executes the statement and that planner cost is expressed in arbitrary units. Its &lt;a href=&quot;https://www.postgresql.org/docs/18/using-explain.html&quot;&gt;plan-reading guide&lt;/a&gt; explains that actual rows and time are averages per execution when a node loops. These documented semantics make provenance and multiplication part of correctness, not presentation detail.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.postgresql.org/docs/18/sql-prepare.html&quot;&gt;PREPARE documentation&lt;/a&gt; describes how PostgreSQL may choose between parameter-specific custom plans and reusable generic plans. It also documents replanning after relevant DDL, updated planner statistics, or &lt;code&gt;search_path&lt;/code&gt; changes. Therefore a plan generated with a convenient literal after the incident can legitimately differ from the application’s prepared execution.&lt;/p&gt;
&lt;p&gt;PostgreSQL’s &lt;a href=&quot;https://www.postgresql.org/docs/18/auto-explain.html&quot;&gt;&lt;code&gt;auto_explain&lt;/code&gt; documentation&lt;/a&gt; provides automatic slow-plan logging and sampling, but warns about overhead. With analysis enabled, per-node timing can affect every statement even when the threshold is not met; disabling timing reduces that cost when row counts are sufficient. This supports bounded, measured capture—not permanent maximal logging.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven finding&lt;/th&gt;&lt;th&gt;Bounded remediation&lt;/th&gt;&lt;th&gt;Avoid&lt;/th&gt;&lt;th&gt;Validate with&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Stale table statistics&lt;/td&gt;&lt;td&gt;Targeted &lt;code&gt;ANALYZE&lt;/code&gt; under an approved window&lt;/td&gt;&lt;td&gt;Assuming every estimate error is stale data&lt;/td&gt;&lt;td&gt;New estimate accuracy, plan, latency, load&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Correlated predicates or skew&lt;/td&gt;&lt;td&gt;Evaluate higher statistics targets or extended statistics&lt;/td&gt;&lt;td&gt;Blanket statistics increases&lt;/td&gt;&lt;td&gt;Estimate error on representative parameter classes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Missing access path&lt;/td&gt;&lt;td&gt;Build and test a selective index with deployment controls&lt;/td&gt;&lt;td&gt;Indexing every sequential scan&lt;/td&gt;&lt;td&gt;Plan choice, write cost, index size, latency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sort or hash spill&lt;/td&gt;&lt;td&gt;Reduce input first; test scoped memory change&lt;/td&gt;&lt;td&gt;Raising global &lt;code&gt;work_mem&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Temp blocks, memory concurrency, latency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Nested-loop amplification&lt;/td&gt;&lt;td&gt;Fix estimates, predicates, or access path&lt;/td&gt;&lt;td&gt;Disabling join methods globally&lt;/td&gt;&lt;td&gt;Actual rows times loops, plan stability&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Generic-plan sensitivity&lt;/td&gt;&lt;td&gt;Verify custom versus generic behavior and application preparation&lt;/td&gt;&lt;td&gt;Forcing a cluster-wide cache mode&lt;/td&gt;&lt;td&gt;Tail latency across parameter classes, planning cost&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Call amplification&lt;/td&gt;&lt;td&gt;Fix retry, fan-out, batching, or request behavior&lt;/td&gt;&lt;td&gt;Tuning the plan for an application storm&lt;/td&gt;&lt;td&gt;Calls per request, throughput, total execution share&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;Collection changes need rollback too. If planning tracking, &lt;code&gt;auto_explain&lt;/code&gt;, I/O timing, or a higher sampling rate adds measurable overhead or log volume, return the named setting to its recorded value and confirm that telemetry pressure falls. Preserve the resulting evidence gap.&lt;/p&gt;
&lt;p&gt;For SQL remediation, record the old plan artifact, configuration, schema definition, statistics state, application version, and validation window. Index deployment needs a removal decision and dependency check. Scoped settings expire with their session or transaction. Statistics changes cannot always be restored exactly, so compare against a prepared fallback plan and application rollback. Never reset &lt;code&gt;pg_stat_statements&lt;/code&gt; to make validation look clean; use fresh snapshots and deltas.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;False conclusion&lt;/th&gt;&lt;th&gt;Correction&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Lifetime ranking&lt;/td&gt;&lt;td&gt;The largest historical query caused the incident&lt;/td&gt;&lt;td&gt;Rank reset-aware interval deltas&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Average subtraction&lt;/td&gt;&lt;td&gt;Mean latency changed by the difference of two means&lt;/td&gt;&lt;td&gt;Recompute from total-time and call deltas&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Query ID used globally&lt;/td&gt;&lt;td&gt;One ID is stable across upgrades and replicas&lt;/td&gt;&lt;td&gt;Scope identity and validate compatibility&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Current plan called incident plan&lt;/td&gt;&lt;td&gt;A regression has been proven&lt;/td&gt;&lt;td&gt;Require a historical artifact or state the gap&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Convenient literal is reproduced&lt;/td&gt;&lt;td&gt;Prepared behavior is represented&lt;/td&gt;&lt;td&gt;Capture parameter class and generic or custom provenance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;ANALYZE&lt;/code&gt; treated as read-only inspection&lt;/td&gt;&lt;td&gt;Plan capture has no operational effect&lt;/td&gt;&lt;td&gt;Use explicit approval and bounded execution&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Node type becomes diagnosis&lt;/td&gt;&lt;td&gt;Seq scan or nested loop is automatically wrong&lt;/td&gt;&lt;td&gt;Evaluate cardinality, loops, buffers, and total contribution&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LLM receives raw SQL and binds&lt;/td&gt;&lt;td&gt;More context means better diagnosis&lt;/td&gt;&lt;td&gt;Redact, minimize, and retrieve sensitive text only when approved&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Customer Data:&lt;/strong&gt; The LLM must not receive bind values, &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; output containing literal filter constants, &lt;code&gt;pg_stat_activity.query&lt;/code&gt; text with embedded parameters, or table contents. Send normalized &lt;code&gt;pg_stat_statements&lt;/code&gt; &lt;code&gt;queryid&lt;/code&gt; and digest text, plan shapes, and timings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Production Execution:&lt;/strong&gt; The LLM cannot run &lt;code&gt;VACUUM FULL&lt;/code&gt;, &lt;code&gt;REINDEX&lt;/code&gt;, &lt;code&gt;ALTER SYSTEM&lt;/code&gt;, &lt;code&gt;pg_terminate_backend&lt;/code&gt;, index DDL, or parameter changes. It proposes; a human authorizes, applies, validates, and holds the rollback condition.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Causal Conclusion Without Contradicting Evidence:&lt;/strong&gt; Every hypothesis must cite both the telemetry that supports it and the telemetry that would falsify it. A ranked hypothesis with no disconfirming test attached is an assertion, not a diagnosis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Silent Gap-Filling:&lt;/strong&gt; Where the evidence pack lacks a required signal, the LLM must name it explicitly under missing evidence rather than inferring a plausible value. “Not collected” and “collected and normal” are different findings and must never be merged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Change Without a Human Gate:&lt;/strong&gt; Production changes require human authorization, a stated validation signal, and a defined rollback condition agreed before the change is applied.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; PostgreSQL statement statistics aggregate history, while execution plans depend on context and runtime capture can change production behavior.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Rank reset-aware interval deltas, preserve scoped query identity and execution provenance, then acquire the least risky plan evidence needed to test a specific hypothesis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Replay a known call surge, cardinality change, and spill in a controlled environment. The workflow should distinguish frequency from per-call regression and refuse to claim a historical plan change when the prior plan is absent.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Add two-snapshot delta calculation, collector-health checks, plan provenance, and an approval-gated plan-capture route before allowing an LLM to recommend SQL remediation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The next article separates Aurora PostgreSQL from EC2 PostgreSQL: distributed storage, WAL behavior, replica topology, waits, failover, and the managed-service evidence needed before blaming SQL.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/pgstatstatements.html&quot;&gt;PostgreSQL 18 — pg_stat_statements&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/sql-explain.html&quot;&gt;PostgreSQL 18 — EXPLAIN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/using-explain.html&quot;&gt;PostgreSQL 18 — Using EXPLAIN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/sql-prepare.html&quot;&gt;PostgreSQL 18 — PREPARE&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/auto-explain.html&quot;&gt;PostgreSQL 18 — auto_explain&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>failures</category><category>checklist</category></item><item><title>LLM-Assisted PostgreSQL Triage on EC2: Sessions, Wait Events and I/O</title><link>https://rajivonai.com/blog/2025-11-18-llm-assisted-postgresql-triage-ec2-sessions-wait-events-io/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-11-18-llm-assisted-postgresql-triage-ec2-sessions-wait-events-io/</guid><description>A PostgreSQL-on-EC2 incident workflow for correlating backend state, wait events, locks, cumulative I/O, Linux pressure, and EBS limits before investigating SQL plans.</description><pubDate>Tue, 18 Nov 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A PostgreSQL latency incident cannot be classified from CPU alone: an active backend may be executing, waiting for storage, blocked by a lock, stalled behind shared-memory contention, or waiting on a client.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL on EC2 exposes the full performance boundary: engine sessions, waits, locks, checkpoints, WAL, and I/O; Linux scheduler, memory, filesystem, process, and device pressure; and CloudWatch instance and volume limits.&lt;/p&gt;
&lt;p&gt;That control is useful only on one timeline. A five-minute CPU average, current activity snapshot, and counters accumulated since another reset cannot prove what happened together. Retain timestamps, periods, reset times, versions, resource IDs, and change events.&lt;/p&gt;
&lt;p&gt;After collection, the LLM reconciles layers, ranks testable hypotheses, and identifies missing evidence. It cannot turn one snapshot into causality.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Several common shortcuts misclassify PostgreSQL incidents:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;high EC2 CPU means one expensive query;&lt;/li&gt;
&lt;li&gt;low CPU means the database is healthy;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;state = &apos;active&apos;&lt;/code&gt; means a backend is consuming CPU;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;wait_event_type = &apos;IO&apos;&lt;/code&gt; proves EBS is saturated;&lt;/li&gt;
&lt;li&gt;many connections mean connection pressure;&lt;/li&gt;
&lt;li&gt;a high cache-hit ratio means storage is irrelevant;&lt;/li&gt;
&lt;li&gt;cumulative &lt;code&gt;pg_stat_io&lt;/code&gt; values describe the incident window.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;PostgreSQL makes &lt;code&gt;state&lt;/code&gt; and &lt;code&gt;wait_event&lt;/code&gt; independent. An active backend with a wait is executing but blocked. A null wait means no reported wait at that instant—not continuous CPU use.&lt;/p&gt;
&lt;p&gt;The engine cannot distinguish a kernel-cache read from a storage read. Default EC2 metrics also omit the complete in-guest memory, swap, filesystem, and device story.&lt;/p&gt;
&lt;p&gt;The core question is: is latency caused by runnable PostgreSQL work, heavyweight locks, internal contention, I/O demand, checkpoint or WAL pressure, client behavior, host memory or scheduler pressure, an EBS limit, or a system outside the database?&lt;/p&gt;
&lt;h2 id=&quot;build-a-time-aligned-postgresql-triage-pack&quot;&gt;Build a Time-Aligned PostgreSQL Triage Pack&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[application latency throughput and errors] --&gt; H[time-aligned incident window]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B[EC2 CPU network status and credits] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[Linux scheduler memory swap filesystem and devices] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D[PostgreSQL sessions states and waits] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E[locks and blocking relationships] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F[PostgreSQL I/O checkpoints and WAL deltas] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G[EBS latency queue throughput and limit checks] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[deterministic classification and anomaly features]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[LLM evidence correlation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[observations with artifact references]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; L[ranked hypotheses and contradictions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; M[missing evidence and next bounded collector]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; N[DBA verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt; N&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    M --&gt; N&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Preserve baseline, transition, incident, and recovery. Retain distributions for gauges; retain start, end, elapsed time, and &lt;code&gt;stats_reset&lt;/code&gt; for counters. Compare deltas, not unrelated totals.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Evidence pattern&lt;/th&gt;&lt;th&gt;Leading classification&lt;/th&gt;&lt;th&gt;What it does not prove&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;High CPU, run queue, active backends without reported waits&lt;/td&gt;&lt;td&gt;Runnable work or excessive concurrency&lt;/td&gt;&lt;td&gt;One SQL statement is responsible&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Many &lt;code&gt;Lock&lt;/code&gt; waits with named blockers&lt;/td&gt;&lt;td&gt;Transaction or DDL contention&lt;/td&gt;&lt;td&gt;Storage or CPU needs resizing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;IO&lt;/code&gt; waits plus rising PostgreSQL read time and EBS latency&lt;/td&gt;&lt;td&gt;Storage path is involved&lt;/td&gt;&lt;td&gt;EBS provisioned performance is exceeded&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Low available memory, swap and reclaim activity&lt;/td&gt;&lt;td&gt;Host memory pressure&lt;/td&gt;&lt;td&gt;&lt;code&gt;shared_buffers&lt;/code&gt; alone is too large&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;ClientRead&lt;/code&gt; or &lt;code&gt;ClientWrite&lt;/code&gt; dominates&lt;/td&gt;&lt;td&gt;Client, network, or application flow control&lt;/td&gt;&lt;td&gt;PostgreSQL is necessarily slow&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Requested checkpoints and write pressure rise&lt;/td&gt;&lt;td&gt;WAL or checkpoint path needs investigation&lt;/td&gt;&lt;td&gt;&lt;code&gt;max_wal_size&lt;/code&gt; is automatically the fix&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CPU is low and waits are absent&lt;/td&gt;&lt;td&gt;Sampling or another layer may be missing&lt;/td&gt;&lt;td&gt;No incident exists&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;h3 id=&quot;1-freeze-identity-time-configuration-and-counter-age&quot;&gt;1. Freeze identity, time, configuration, and counter age&lt;/h3&gt;
&lt;p&gt;Record version, primary or standby role, boot and postmaster times, instance type, volumes, mounts, timeline, configuration hash, and recent changes. Confirm the application window first.&lt;/p&gt;
&lt;p&gt;Capture &lt;code&gt;stats_reset&lt;/code&gt; with cumulative views. PostgreSQL preserves statistics after clean shutdown but resets them after crashes, starting from a base backup, and point-in-time recovery. A reset can resemble workload collapse.&lt;/p&gt;
&lt;h3 id=&quot;2-classify-linux-and-ec2-pressure-before-blaming-sql&quot;&gt;2. Classify Linux and EC2 pressure before blaming SQL&lt;/h3&gt;
&lt;p&gt;Use bounded samples rather than one command at one instant:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;uptime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;vmstat&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;iostat&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -xz&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pidstat&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -dur&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -C&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; postgres&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;free&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -m&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;df&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -h&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;df&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -i&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ss&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Interpret them together. Load average includes uninterruptible waits as well as runnable work. Low free memory can be normal; available memory, reclaim, major faults, swap activity, and OOM events are stronger evidence. Pair device utilization with latency, queue, request size, and throughput. Check bytes and inodes separately.&lt;/p&gt;
&lt;p&gt;Built-in EC2 metrics add CPU, network, status, and applicable credit evidence. In-guest memory, swap, filesystem, and device detail require another collector. Preserve metric period and statistic.&lt;/p&gt;
&lt;h3 id=&quot;3-count-backend-state-and-waits-without-starting-from-query-text&quot;&gt;3. Count backend state and waits without starting from query text&lt;/h3&gt;
&lt;p&gt;Take repeated &lt;code&gt;pg_stat_activity&lt;/code&gt; samples grouped by backend, database, application, state, and wait:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; clock_timestamp() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; observed_at,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       backend_type,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       datname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       application_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       wait_event_type,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       wait_event,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; backends&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_backend_pid()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GROUP BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; backend_type, datname, application_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;         state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, wait_event_type, wait_event;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Label null waits &lt;strong&gt;no reported PostgreSQL wait&lt;/strong&gt;, never “CPU.” Separate client backends from autovacuum, checkpointer, WAL, replication, parallel, and extension workers. Retain transaction and state-change age plus &lt;code&gt;query_id&lt;/code&gt;. An &lt;code&gt;idle in transaction&lt;/code&gt; session can hold locks or an old snapshot while using little CPU; ordinary &lt;code&gt;idle&lt;/code&gt; connections are different from queued active work.&lt;/p&gt;
&lt;h3 id=&quot;4-prove-blocking-before-terminating-anything&quot;&gt;4. Prove blocking before terminating anything&lt;/h3&gt;
&lt;p&gt;For &lt;code&gt;Lock&lt;/code&gt; waits, record &lt;code&gt;pg_blocking_pids()&lt;/code&gt; and sanitized blocker metadata: transaction age, state, application, user pseudonym, wait, and query ID. &lt;code&gt;Lock&lt;/code&gt;, &lt;code&gt;LWLock&lt;/code&gt;, and &lt;code&gt;BufferPin&lt;/code&gt; represent different mechanisms.&lt;/p&gt;
&lt;p&gt;Do not infer the root blocker from age or let the LLM terminate it. The DBA must verify ownership, business impact, rollback cost, and permitted action.&lt;/p&gt;
&lt;h3 id=&quot;5-correlate-postgresql-io-deltas-with-linux-and-ebs&quot;&gt;5. Correlate PostgreSQL I/O deltas with Linux and EBS&lt;/h3&gt;
&lt;p&gt;For PostgreSQL 18, snapshot &lt;code&gt;pg_stat_io&lt;/code&gt;, &lt;code&gt;pg_stat_checkpointer&lt;/code&gt;, &lt;code&gt;pg_stat_wal&lt;/code&gt;, and &lt;code&gt;pg_stat_database&lt;/code&gt; at both window boundaries. &lt;code&gt;pg_stat_io&lt;/code&gt; groups cluster-wide activity by backend type, object, and context; version 18 includes byte counters and WAL rows. Timing columns require &lt;code&gt;track_io_timing&lt;/code&gt; or &lt;code&gt;track_wal_io_timing&lt;/code&gt; and are misleading after partial-window enablement.&lt;/p&gt;
&lt;p&gt;Align engine deltas with Linux device evidence and EBS latency, queue, volume exceeded checks, and instance-level EBS checks. &lt;code&gt;DataFileRead&lt;/code&gt; proves a backend waited for a read; surrounding evidence distinguishes cache churn, competing I/O, and limits.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[application latency incident] --&gt; B{host status filesystem or OOM failure}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| C[infrastructure recovery path]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| D{Lock waits and blockers present}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes| E[verify blocking chain and transaction owner]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no| F{IO waits elevated}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|yes| G[correlate PostgreSQL Linux and EBS deltas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|no| H{Client waits dominate active work}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| I[inspect application network and result consumption]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| J{LWLock or BufferPin elevated}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|yes| K[collect event-specific contention evidence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|no| L{CPU run queue and active work elevated}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|yes| M[move to workload and SQL diagnosis]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|no| N[verify sampling gaps and upstream dependencies]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;llm-correlation-contract&quot;&gt;LLM Correlation Contract&lt;/h2&gt;
&lt;p&gt;Require four distinct outputs:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;OBSERVATIONS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- Active backends increased during the incident window [activity-07].&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- Lock waits remained at baseline [waits-03].&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- EBS read latency and PostgreSQL read-time deltas rose together [ebs-04, pgio-09].&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;HYPOTHESES&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;1. Read-path pressure is contributing to latency.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;2. CPU saturation is less consistent with the evidence.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;MISSING EVIDENCE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- Per-device Linux latency for the same minute.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- Workload source of the additional reads.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ACTIONS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- Run the approved device snapshot collector.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- Continue to SQL attribution only after the storage boundary is classified.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;References are mandatory. A named wait is an observation, not a complete cause.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.postgresql.org/docs/18/monitoring-stats.html&quot;&gt;PostgreSQL 18 monitoring documentation&lt;/a&gt; defines &lt;code&gt;pg_stat_activity&lt;/code&gt; as current activity and warns that &lt;code&gt;state&lt;/code&gt; and &lt;code&gt;wait_event&lt;/code&gt; are independent. It also documents that cumulative statistics can lag, may remain cached within a transaction, and can be refreshed with &lt;code&gt;pg_stat_clear_snapshot()&lt;/code&gt;. The documented pattern is to timestamp and version snapshots rather than ask an LLM to reconcile counters with unknown age.&lt;/p&gt;
&lt;p&gt;The same documentation states that PostgreSQL I/O statistics cannot distinguish storage reads from data already in the kernel page cache and recommends combining them with operating-system utilities. It also notes that &lt;code&gt;pg_stat_io&lt;/code&gt; timing columns require the corresponding timing configuration. This is why an &lt;code&gt;IO&lt;/code&gt; wait, a &lt;code&gt;pg_stat_io&lt;/code&gt; delta, and an EBS metric answer different parts of the same question.&lt;/p&gt;
&lt;p&gt;AWS documents that &lt;a href=&quot;https://docs.aws.amazon.com/ebs/latest/userguide/using_cloudwatch_ebs.html&quot;&gt;EBS volumes publish one-minute metrics&lt;/a&gt;, including latency and queue evidence on supported configurations, and that exceeded-check metrics indicate attempts to drive beyond provisioned volume performance. AWS also documents separate &lt;a href=&quot;https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/viewing_metrics_with_cloudwatch.html&quot;&gt;instance-level EBS exceeded checks&lt;/a&gt;. A healthy volume limit does not prove the EC2 instance’s aggregate EBS limit is healthy, and neither proves PostgreSQL generated the competing I/O.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/metrics-collected-by-CloudWatch-agent.html&quot;&gt;CloudWatch agent’s Linux metrics&lt;/a&gt; include memory availability, swap, filesystem, device, and CPU-mode evidence beyond default EC2 monitoring. The operational conclusion is derived from named system behavior: self-managed PostgreSQL triage requires engine, guest, instance, and volume evidence on one clock.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven boundary&lt;/th&gt;&lt;th&gt;Immediate containment&lt;/th&gt;&lt;th&gt;Durable direction&lt;/th&gt;&lt;th&gt;Validate with&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Heavyweight blocking&lt;/td&gt;&lt;td&gt;Resolve owner; cancel or terminate only with approval&lt;/td&gt;&lt;td&gt;Shorter transactions, safer DDL, lock timeout policy&lt;/td&gt;&lt;td&gt;Blocked sessions, transaction age, application errors&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Excess runnable work&lt;/td&gt;&lt;td&gt;Shed or queue load; protect connection headroom&lt;/td&gt;&lt;td&gt;Pool sizing, concurrency control, SQL and plan remediation&lt;/td&gt;&lt;td&gt;Run queue, active backends, throughput, latency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Host memory pressure&lt;/td&gt;&lt;td&gt;Stop unbounded work; preserve recovery headroom&lt;/td&gt;&lt;td&gt;Memory model across connections, parallelism, maintenance, OS&lt;/td&gt;&lt;td&gt;Available memory, swap, OOM, query concurrency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;EBS volume or instance limit&lt;/td&gt;&lt;td&gt;Reduce competing I/O; defer maintenance&lt;/td&gt;&lt;td&gt;Provisioned IOPS and throughput matched to measured demand&lt;/td&gt;&lt;td&gt;Exceeded checks, latency, queue, PostgreSQL I/O rates&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Checkpoint pressure&lt;/td&gt;&lt;td&gt;Avoid speculative parameter changes during peak&lt;/td&gt;&lt;td&gt;Tune only after WAL and checkpoint-rate analysis&lt;/td&gt;&lt;td&gt;Requested checkpoints, write and sync time, latency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Client or network backpressure&lt;/td&gt;&lt;td&gt;Bound results or isolate unhealthy clients&lt;/td&gt;&lt;td&gt;Application consumption, timeout, network, payload design&lt;/td&gt;&lt;td&gt;Client waits, network, result size, end-to-end latency&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;Triage should be read-only. If a collector adds pressure, stop its schedule, close its pool, and return sampling frequency and CloudWatch-agent configuration to the prior version. Preserve the incident window and record the resulting evidence gap.&lt;/p&gt;
&lt;p&gt;For containment, define rollback before action: restore traffic limits gradually, revert pool settings, resume paused maintenance, or return storage configuration only when the previous capacity remains valid. A terminated transaction cannot be “unterminated”; PostgreSQL rolls it back, so recovery means verifying rollback completion and application retry semantics. Do not reset statistics during an incident—doing so destroys the baseline needed to validate the change.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Misleading conclusion&lt;/th&gt;&lt;th&gt;Correction&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;One &lt;code&gt;pg_stat_activity&lt;/code&gt; snapshot&lt;/td&gt;&lt;td&gt;A transient sample represents the incident&lt;/td&gt;&lt;td&gt;Sample repeatedly and preserve distributions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Null wait is labeled CPU&lt;/td&gt;&lt;td&gt;Every active backend consumes CPU&lt;/td&gt;&lt;td&gt;Correlate with process and scheduler evidence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cumulative totals are compared directly&lt;/td&gt;&lt;td&gt;Largest lifetime counter caused the spike&lt;/td&gt;&lt;td&gt;Calculate deltas with elapsed time and reset age&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;IO&lt;/code&gt; wait is equated with EBS saturation&lt;/td&gt;&lt;td&gt;Storage must be resized&lt;/td&gt;&lt;td&gt;Check Linux, volume, instance, cache, and competing I/O&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High cache-hit ratio closes storage analysis&lt;/td&gt;&lt;td&gt;Physical reads are negligible&lt;/td&gt;&lt;td&gt;Inspect rates, working-set transition, OS cache, temp and WAL I/O&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Connection total becomes the diagnosis&lt;/td&gt;&lt;td&gt;More sessions caused the incident&lt;/td&gt;&lt;td&gt;Separate idle, transactional, active, waiting, and backend types&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LLM sees raw query text first&lt;/td&gt;&lt;td&gt;Sensitive detail overwhelms classification&lt;/td&gt;&lt;td&gt;Begin with aggregates and stable query IDs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Remediation precedes proof&lt;/td&gt;&lt;td&gt;A parameter change masks the incident&lt;/td&gt;&lt;td&gt;Require observation, hypothesis, missing evidence, validation, rollback&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Customer Data:&lt;/strong&gt; The LLM must not receive bind values, &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; output containing literal filter constants, &lt;code&gt;pg_stat_activity.query&lt;/code&gt; text with embedded parameters, or table contents. Send normalized &lt;code&gt;pg_stat_statements&lt;/code&gt; &lt;code&gt;queryid&lt;/code&gt; and digest text, plan shapes, and timings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Production Execution:&lt;/strong&gt; The LLM cannot run &lt;code&gt;VACUUM FULL&lt;/code&gt;, &lt;code&gt;REINDEX&lt;/code&gt;, &lt;code&gt;ALTER SYSTEM&lt;/code&gt;, &lt;code&gt;pg_terminate_backend&lt;/code&gt;, index DDL, or parameter changes. It proposes; a human authorizes, applies, validates, and holds the rollback condition.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Causal Conclusion Without Contradicting Evidence:&lt;/strong&gt; Every hypothesis must cite both the telemetry that supports it and the telemetry that would falsify it. A ranked hypothesis with no disconfirming test attached is an assertion, not a diagnosis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Silent Gap-Filling:&lt;/strong&gt; Where the evidence pack lacks a required signal, the LLM must name it explicitly under missing evidence rather than inferring a plausible value. “Not collected” and “collected and normal” are different findings and must never be merged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Change Without a Human Gate:&lt;/strong&gt; Production changes require human authorization, a stated validation signal, and a defined rollback condition agreed before the change is applied.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; PostgreSQL, Linux, EC2, and EBS each expose only part of a performance incident, while snapshots and cumulative counters operate on different time semantics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build a time-aligned pack of backend states, waits, blockers, engine I/O deltas, host pressure, and cloud limits; let the LLM correlate those artifacts without treating any single signal as root cause.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Replay a known lock, CPU, and storage test. The classifier should route each to a different next diagnostic, retain contradictory evidence, and avoid proposing a parameter change before the boundary is proven.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Implement the five bounded collectors and reset-aware delta logic, then test the decision tree under load before connecting the output to an LLM.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The next article moves from a classified workload incident to SQL attribution: using &lt;code&gt;pg_stat_statements&lt;/code&gt;, execution plans, buffer evidence, and actual-versus-estimated rows without turning &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; into a production hazard.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/monitoring.html&quot;&gt;PostgreSQL 18 — Monitoring Database Activity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/monitoring-stats.html&quot;&gt;PostgreSQL 18 — The Cumulative Statistics System&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/monitoring-locks.html&quot;&gt;PostgreSQL 18 — Viewing Locks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/view-pg-locks.html&quot;&gt;PostgreSQL 18 — pg_locks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/release-18.html&quot;&gt;PostgreSQL 18 — Release Notes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/viewing_metrics_with_cloudwatch.html&quot;&gt;Amazon EC2 — CloudWatch metrics for instances&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/ebs/latest/userguide/using_cloudwatch_ebs.html&quot;&gt;Amazon EBS — CloudWatch metrics for EBS volumes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/ebs/latest/userguide/ebs-io-characteristics.html&quot;&gt;Amazon EBS — I/O characteristics and monitoring&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/metrics-collected-by-CloudWatch-agent.html&quot;&gt;Amazon CloudWatch — Metrics collected by the CloudWatch agent&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>ai-engineering</category><category>failures</category></item><item><title>Why We Moved from DataStax Enterprise to Apache Cassandra</title><link>https://rajivonai.com/blog/2025-11-13-migrating-datastax-enterprise-to-apache-cassandra/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-11-13-migrating-datastax-enterprise-to-apache-cassandra/</guid><description>How the playbook executes a zero-downtime migration of 45 nodes from proprietary DataStax Enterprise (DSE) to 100% open-source Apache Cassandra.</description><pubDate>Thu, 13 Nov 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In the early days of Cassandra, running the open-source version in production required a massive engineering team. DataStax Enterprise (DSE) solved this by bundling proprietary search, monitoring, and security plugins into a stable commercial offering.&lt;/p&gt;
&lt;p&gt;However, as the Apache Cassandra project matured through the 4.x lifecycle, the open-source community built enterprise-grade tooling that rivaled and often surpassed proprietary alternatives. When the massive DSE licensing renewal arrived, we realized we were paying a premium for vendor lock-in.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;By 2024, the 45-node DSE 6.8 cluster (~40 TB dataset) was the backbone of the platform. The commercial licensing and support subscriptions had become one of the highest infrastructure line items.&lt;/p&gt;
&lt;p&gt;We were mandated to migrate to 100% open-source Apache Cassandra 4.1. This was not a simple binary swap. We were deeply coupled to DSE-specific features: DSE Search (Solr), DSE Graph, DSE Analytics (Spark), OpsCenter for monitoring, and the proprietary &lt;code&gt;com.datastax.dse.driver&lt;/code&gt; in the Java microservices.&lt;/p&gt;
&lt;p&gt;Attempting to boot Apache Cassandra directly on DSE SSTables would fail because DSE uses proprietary header identifiers and on-disk format modifications. Operators must execute a live, zero-downtime migration across 40 TB of data.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Migrating off a proprietary database fork requires auditing and replacing every vendor-locked dependency before you can touch the data.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Observability:&lt;/strong&gt; Operators must replace OpsCenter, the proprietary GUI previously used for monitoring and repair orchestration.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Search:&lt;/strong&gt; Operators must decouple the application from DSE Search (Solr), which allowed developers to run &lt;code&gt;LIKE&lt;/code&gt; queries and text searches directly against Cassandra tables.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Data Migration:&lt;/strong&gt; Because the SSTable formats were incompatible, we could not use &lt;code&gt;nodetool upgradesstables&lt;/code&gt;. Operators must physically stream 40 TB of data from the old cluster to the new cluster while the application was still serving live traffic.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;the-open-source-migration-blueprint&quot;&gt;The Open-Source Migration Blueprint&lt;/h2&gt;
&lt;p&gt;The pattern architects a 4-Phase Migration Blueprint that relied on dual-writing and asynchronous data backfilling.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[Application Microservices] --&gt; Proxy{Dual-Write Proxy}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;Legacy DSE 6.8 Cluster&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Proxy --&gt;|Primary Write — Read| DSE[DSE Nodes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;New OSS Apache Cassandra 4.1 Cluster&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Proxy -.-&gt;|Shadow Write| OSS[OSS Nodes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        DSBulk[DSBulk Pipeline] --&gt;|Historical Backfill| OSS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DSE --&gt;|Source Data| DSBulk&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    %% Decoupled Search&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Proxy --&gt;|Search Queries| OS[OpenSearch Cluster]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-decoupling-search-and-observability&quot;&gt;1. Decoupling Search and Observability&lt;/h3&gt;
&lt;p&gt;First, we eliminated the proprietary DSE Search dependency. We stood up an Amazon OpenSearch cluster and updated the application to index search-heavy data directly into OpenSearch, removing the reliance on DSE Solr cores.&lt;/p&gt;
&lt;p&gt;To replace OpsCenter, a typical deployment involves the open-source “Holy Trinity” of Cassandra operations: the &lt;strong&gt;Prometheus Cassandra Exporter&lt;/strong&gt; for metrics, &lt;strong&gt;Grafana&lt;/strong&gt; for dashboards, and &lt;strong&gt;Cassandra Reaper&lt;/strong&gt; to automate anti-entropy repair scheduling.&lt;/p&gt;
&lt;p&gt;DSE Search and OpsCenter were the tractable dependencies. DSE Graph and DSE Analytics (Spark) were not: neither has a drop-in OSS Cassandra equivalent. The graph workload was rebuilt as a separate service on a purpose-built graph database, reading from Cassandra via CDC rather than living inside it, and the Spark analytics jobs were repointed at the OSS cluster using the open-source Spark Cassandra Connector — both were multi-quarter efforts running in parallel with, not inside, the migration blueprint below. The proprietary &lt;code&gt;com.datastax.dse.driver&lt;/code&gt; authentication and LDAP integration also had no equivalent in OSS Cassandra’s &lt;code&gt;PasswordAuthenticator&lt;/code&gt;; that gap was closed by fronting the cluster with an external identity-aware proxy rather than relying on database-native LDAP binding.&lt;/p&gt;
&lt;h3 id=&quot;2-the-dual-write-abstraction&quot;&gt;2. The Dual-Write Abstraction&lt;/h3&gt;
&lt;p&gt;We provisioned a brand new, parallel Apache Cassandra 4.1 cluster. We updated the application to use the open-source driver (&lt;code&gt;com.datastax.oss.driver&lt;/code&gt;) and implemented a dual-write proxy. The application wrote data synchronously to the legacy DSE cluster (primary) and asynchronously to the new OSS cluster (shadow). Reads were served exclusively from the DSE cluster.&lt;/p&gt;
&lt;h3 id=&quot;3-historical-backfill-with-dsbulk&quot;&gt;3. Historical Backfill with DSBulk&lt;/h3&gt;
&lt;p&gt;While the application was dual-writing live traffic, the new OSS cluster was still missing historical data. The pipeline utilizes &lt;strong&gt;DataStax Bulk Loader (DSBulk)&lt;/strong&gt;, an open-source CQL-based data transfer tool, to extract the 40 TB of historical data from the DSE cluster and load it directly into the OSS cluster. Because DSBulk operates via CQL, it bypassed the incompatible binary SSTable formats completely.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Once the DSBulk backfill completed, both clusters contained identical data.&lt;/p&gt;
&lt;p&gt;To prove data integrity, we enabled a “Shadow Read Validator” in the application. When a read request came in, the application fetched the data from DSE and returned it to the client. Asynchronously, it fetched the exact same data from the OSS cluster and compared the payloads. We monitored the discrepancy metrics for two weeks.&lt;/p&gt;
&lt;p&gt;Once the discrepancy rate hit absolute zero, the playbook executes a zero-downtime DNS cutover, routing all primary reads and writes to the open-source Apache Cassandra cluster. We kept the legacy DSE cluster running as a dual-write shadow for 30 days as a rollback safety net before finally decommissioning it.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Direct SSTable Copying&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Moving DSE SSTables directly into Apache Cassandra will crash the node on startup due to unreadable file headers.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;You must use CQL-based migration tools (like DSBulk) or dual-write replication to move data between forks.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Missing Driver Breaking Changes&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The Java Driver 3.x to 4.x migration involves significant API changes for asynchronous queries and load balancing policies.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Allocate adequate sprint capacity to rewrite driver connection logic. It is not a drop-in JAR replacement.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Skipping Repair Before Cutover&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;If the new OSS cluster drops a mutation during the dual-write phase, the data will be silently inconsistent upon cutover.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run a full Cassandra Reaper repair cycle on the new cluster &lt;em&gt;before&lt;/em&gt; cutting over the primary reads.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-closes-the-feature-gap&quot;&gt;Modern Cassandra Context: Cassandra 5.0 Closes the Feature Gap&lt;/h3&gt;
&lt;p&gt;Operating pure open-source Apache Cassandra has drastically reduced the Total Cost of Ownership (TCO). Furthermore, the release of Apache Cassandra 5.0 closes the final feature gaps that historically drove teams to DSE. With the introduction of Storage-Attached Indexing (SAI), open-source Cassandra now supports vector search and high-performance secondary indexing natively, eliminating the need to bolt on external search engines for basic lookup patterns.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Vendor lock-in to proprietary Cassandra forks prevents infrastructure agility and incurs massive licensing costs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Deploy a parallel open-source cluster, implement application-level dual-writing, and use DSBulk to migrate historical data via CQL.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Shadow-read validation ensures absolute data parity between the legacy and open-source clusters before executing a zero-downtime DNS cutover.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; If you are migrating drivers, upgrade to &lt;code&gt;com.datastax.oss.driver&lt;/code&gt; immediately. It natively supports both DSE and OSS clusters, allowing you to modernize your application code before moving the data.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>Aurora MySQL Performance Engineering: Waits, Storage, Replicas and Failover</title><link>https://rajivonai.com/blog/2025-11-04-aurora-mysql-performance-engineering-waits-storage-replicas-failover/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-11-04-aurora-mysql-performance-engineering-waits-storage-replicas-failover/</guid><description>An Aurora-native investigation method for correlating per-instance database load, distributed storage, reader lag, endpoint behavior, and failover readiness with LLM assistance.</description><pubDate>Tue, 04 Nov 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Aurora MySQL is not MySQL running on a host you cannot access; its writer, readers, endpoints, local temporary storage, and shared cluster volume create different evidence boundaries and different failure modes.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The prior MySQL articles classified EC2 failures and traced workload regression from digest to execution behavior. Aurora retains those fundamentals but moves storage durability, instance replacement, and endpoint routing behind a cluster control plane.&lt;/p&gt;
&lt;p&gt;An Aurora cluster has one writer and up to 15 readers attached to a distributed cluster volume. Each instance still has its own CPU, memory, page cache, network, connections, database load, and local storage. The reader endpoint balances new connections—not queries.&lt;/p&gt;
&lt;p&gt;As of the technology freeze, AWS had announced that the Performance Insights console would redirect to CloudWatch Database Insights after July 31, 2026, while its API would continue. Collectors should use supported APIs and record mode and retention—not depend on screenshots.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Aurora’s managed architecture invites several false equivalences:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;shared cluster storage means readers have no independent performance state;&lt;/li&gt;
&lt;li&gt;a storage-named wait proves an Aurora storage-service incident;&lt;/li&gt;
&lt;li&gt;adding a reader distributes existing pooled connections;&lt;/li&gt;
&lt;li&gt;low cluster-wide replica lag proves every reader is current;&lt;/li&gt;
&lt;li&gt;the reader endpoint can never resolve to the writer;&lt;/li&gt;
&lt;li&gt;automatic failover makes client recovery automatic.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;None is safe. &lt;code&gt;io/redo_log_flush&lt;/code&gt; can reflect excessive commit frequency. A reader can exhaust local temporary storage while the cluster volume is healthy. Long-lived pools can overload one reader. Failover requires clients to reconnect and resolve transaction outcomes.&lt;/p&gt;
&lt;p&gt;The core question is: does the incident belong to one DB instance, the writer’s transaction path, the shared cluster volume, reader workload and visibility, endpoint behavior, or a topology transition?&lt;/p&gt;
&lt;h2 id=&quot;auroras-evidence-boundaries&quot;&gt;Aurora’s Evidence Boundaries&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[application workloads] --&gt; B[writer endpoint — current writer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; C[reader endpoint — connection balancing]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; D[custom or instance endpoint]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[writer — CPU memory cache local storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; F[reader one — CPU memory cache local storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; G[reader two — CPU memory cache local storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; H[shared Aurora cluster volume]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I[Database Insights — load waits SQL] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J[CloudWatch — instance and cluster metrics] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K[RDS events and topology] --&gt; B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Tag every artifact with cluster and instance IDs, role, Availability Zone, endpoint class, engine version, time, and scope. Otherwise, an LLM can correlate the right signals to the wrong node.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Leading boundary&lt;/th&gt;&lt;th&gt;What it does not prove&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Writer commit latency and redo-flush load rise&lt;/td&gt;&lt;td&gt;Writer transaction path&lt;/td&gt;&lt;td&gt;Aurora storage is impaired&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One reader’s CPU and DB load rise&lt;/td&gt;&lt;td&gt;Reader workload or connection skew&lt;/td&gt;&lt;td&gt;The cluster needs another reader&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;AuroraReplicaLag&lt;/code&gt; rises on one reader&lt;/td&gt;&lt;td&gt;That reader is behind&lt;/td&gt;&lt;td&gt;Every reader or global replication is behind&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;FreeLocalStorage&lt;/code&gt; falls&lt;/td&gt;&lt;td&gt;Instance-local temporary or operational files&lt;/td&gt;&lt;td&gt;Cluster-volume capacity is low&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Read errors appear during failover&lt;/td&gt;&lt;td&gt;Endpoint and connection recovery&lt;/td&gt;&lt;td&gt;Data in the cluster volume was lost&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Many unrelated SQL digests wait together&lt;/td&gt;&lt;td&gt;Shared resource or contention&lt;/td&gt;&lt;td&gt;Every query independently regressed&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;h3 id=&quot;1-freeze-topology-role-version-and-events&quot;&gt;1. Freeze topology, role, version, and events&lt;/h3&gt;
&lt;p&gt;Capture cluster members, instance classes, Availability Zones, promotion tiers, writer flag, parameter groups, endpoints, maintenance state, and RDS events. Resolve which instance sampled connections reached.&lt;/p&gt;
&lt;p&gt;On Aurora MySQL, &lt;code&gt;innodb_read_only&lt;/code&gt; is &lt;code&gt;OFF&lt;/code&gt; on the writer and &lt;code&gt;ON&lt;/code&gt; on a reader:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; AURORA_VERSION() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; aurora_version,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       @@hostname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; instance_host,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       @@innodb_read_only &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; is_reader;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Do not infer role from an instance name. Failover changes the writer while instance endpoints remain tied to instances. Reject deltas across restarts and preserve events before proposing changes.&lt;/p&gt;
&lt;h3 id=&quot;2-decompose-database-load-by-instance-wait-and-sql&quot;&gt;2. Decompose database load by instance, wait, and SQL&lt;/h3&gt;
&lt;p&gt;For the writer and affected readers, collect average active sessions, CPU capacity, load by wait, top SQL, and digest references. Cluster aggregation can combine a healthy writer with a saturated reader.&lt;/p&gt;
&lt;p&gt;Attach the engine version to waits. Version 3 reports &lt;code&gt;io/redo_log_flush&lt;/code&gt; for persistence to Aurora storage; version 2 uses &lt;code&gt;io/aurora_redo_log_flush&lt;/code&gt;. Many small commits can raise this wait. &lt;code&gt;io/aurora_respond_to_client&lt;/code&gt; points toward result delivery, network capacity, or a slow client. Lock waits still require blocker evidence.&lt;/p&gt;
&lt;p&gt;Database load is concurrency, not application latency. Compare it with throughput, commit rate, SQL deltas, instance metrics, and Enhanced Monitoring. Use the vCPU line as a capacity reference only when runnable CPU load dominates.&lt;/p&gt;
&lt;h3 id=&quot;3-separate-the-cluster-volume-from-local-storage&quot;&gt;3. Separate the cluster volume from local storage&lt;/h3&gt;
&lt;p&gt;The cluster volume contains persistent data and binary logs. &lt;code&gt;VolumeReadIOPs&lt;/code&gt; and &lt;code&gt;VolumeWriteIOPs&lt;/code&gt; are cluster-level counts over five-minute intervals: useful for trends, but unable to identify the responsible instance or SQL.&lt;/p&gt;
&lt;p&gt;Correlate writer &lt;code&gt;CommitLatency&lt;/code&gt;, throughput, redo waits, DB load, and SQL commit patterns. For reads, combine volume operations with per-instance cache, load, and digest deltas. Shared storage does not mean shared cache.&lt;/p&gt;
&lt;p&gt;Version 3 uses local storage for sorts, index builds, and temporary-table overflow. Readers place &lt;code&gt;TempTable&lt;/code&gt; overflow in memory-mapped local files and can return table-full errors. Track &lt;code&gt;FreeLocalStorage&lt;/code&gt;, temporary counters, plans, and memory per instance. &lt;code&gt;AuroraVolumeBytesLeftTotal&lt;/code&gt; measures cluster-volume capacity, not local space.&lt;/p&gt;
&lt;h3 id=&quot;4-diagnose-each-reader-and-the-connection-distribution&quot;&gt;4. Diagnose each reader and the connection distribution&lt;/h3&gt;
&lt;p&gt;Collect &lt;code&gt;AuroraReplicaLag&lt;/code&gt; for every reader and pair it with load, resources, connections, query mix, and transaction age. &lt;code&gt;AuroraBinlogReplicaLag&lt;/code&gt; describes a separate binary-log topology.&lt;/p&gt;
&lt;p&gt;Because the reader endpoint distributes connections, long-lived pools can remain uneven after adding a reader. Record pool lifetime, reconnects, DNS behavior, endpoint, and target. Custom endpoints work only when applications use them.&lt;/p&gt;
&lt;p&gt;Long reader queries under default &lt;code&gt;REPEATABLE READ&lt;/code&gt; can hold back purge. Correlate &lt;code&gt;RollbackSegmentHistoryListLength&lt;/code&gt; with transaction age and query attribution. Reader-specific &lt;code&gt;READ COMMITTED&lt;/code&gt; can reduce pressure but permits documented result anomalies; it is a semantic decision.&lt;/p&gt;
&lt;h3 id=&quot;5-prove-failover-readiness-before-blaming-failover&quot;&gt;5. Prove failover readiness before blaming failover&lt;/h3&gt;
&lt;p&gt;Check whether restart, failover, patch, scaling, or replacement overlaps the symptom. Preserve writer history, event times, dropped connections, ambiguous transactions, endpoint resolution, capacity, and promotion tiers.&lt;/p&gt;
&lt;p&gt;Promotion tier &lt;code&gt;0&lt;/code&gt; is highest and &lt;code&gt;15&lt;/code&gt; lowest. A cross-AZ reader helps only if it can carry writer load. Use the cluster endpoint for writes; instance endpoints do not follow the writer role.&lt;/p&gt;
&lt;p&gt;Validate bounded reconnects, transaction reconciliation, DNS refresh, pool draining, and post-promotion capacity. Forced failover is a controlled production test, not a read-only diagnostic.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Aurora MySQL performance incident] --&gt; B{Topology event overlaps symptom}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| C[failover path — correlate events endpoints and retries]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| D{One instance affected}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes| E{Local storage or memory pressure}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|yes| F[instance-local path — identify query and resource]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|no| G{Reader lag or connection skew}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|yes| H[reader path — inspect pools queries and purge]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|no| I[instance workload path — inspect waits and SQL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no| J{Redo-flush load and commit latency agree}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|yes| K[writer transaction path — inspect commit frequency]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|no| L{Cluster I/O and cache evidence agree}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|yes| M[shared storage path — identify page demand]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|no| N[revisit application network and shared dependencies]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;AWS documents that the &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Concepts.AuroraHighAvailability.html&quot;&gt;Aurora cluster volume&lt;/a&gt; writes synchronously across six storage nodes, while readers use asynchronous replication. Separate durable storage from instance visibility and compute.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraMySQL.Reference.Waitevents.html&quot;&gt;wait-event reference&lt;/a&gt; defines version-specific redo, client, row-lock, and metadata-lock waits. Waits become actionable through load and workload context, not labels alone.&lt;/p&gt;
&lt;p&gt;AWS states that the &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Endpoints.Reader.html&quot;&gt;reader endpoint balances connections rather than queries&lt;/a&gt;. The documented pattern explains why adding a replica can leave an existing pool hot on one reader until connections recycle.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Concepts.AuroraHighAvailability.html&quot;&gt;high-availability documentation&lt;/a&gt; describes replica promotion, endpoint continuity, promotion tiers, and the brief application-visible failure interval. Aurora automates choosing a new writer; applications still own reconnection and transaction outcome handling.&lt;/p&gt;
&lt;h2 id=&quot;llm-correlation-contract&quot;&gt;LLM Correlation Contract&lt;/h2&gt;
&lt;p&gt;Provide topology and time-series evidence grouped by entity. Every observation must name an instance or cluster scope. A useful hypothesis states:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Hypothesis: writer commit amplification&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Supporting: redo-flush load, commit rate, digest and release references&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Contradicting: no platform event; reader load remains normal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Missing: transaction batch size by caller&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Next diagnostic: attribute commit count to leading write digests&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Change authority: none&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The LLM may correlate signals and request an approved collector. It must not fail over, reboot, change isolation or tiers, resize, or reroute traffic.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven mechanism&lt;/th&gt;&lt;th&gt;Candidate action&lt;/th&gt;&lt;th&gt;Validation and risk&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Excessive commit frequency&lt;/td&gt;&lt;td&gt;Batch related writes within correctness limits&lt;/td&gt;&lt;td&gt;Commit rate, redo wait, lock duration, and retry cost&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hot reader from connection skew&lt;/td&gt;&lt;td&gt;Recycle pools gradually or route with a custom endpoint&lt;/td&gt;&lt;td&gt;Distribution by instance; reconnection surge risk&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reader local-temp pressure&lt;/td&gt;&lt;td&gt;Rewrite or index the query, bound temporary use, or resize&lt;/td&gt;&lt;td&gt;&lt;code&gt;FreeLocalStorage&lt;/code&gt;, query result, memory, and cost&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reader-driven purge lag&lt;/td&gt;&lt;td&gt;End or reshape long reports; consider reader &lt;code&gt;READ COMMITTED&lt;/code&gt; only when semantics permit&lt;/td&gt;&lt;td&gt;History-list trend and result-consistency tests&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Writer CPU or SQL regression&lt;/td&gt;&lt;td&gt;Apply the digest-and-plan workflow from Article 6 or resize for containment&lt;/td&gt;&lt;td&gt;DB load, SQL latency, cost, and unchanged correctness&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Weak failover path&lt;/td&gt;&lt;td&gt;Add a cross-AZ reader, correct promotion tiers, and fix endpoint or retry behavior&lt;/td&gt;&lt;td&gt;Controlled failover recovery objective and reconciliation&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Do not treat a replica, resize, reboot, or failover as a substitute for proving the constrained boundary.&lt;/p&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Restore previous pool lifetime, endpoint, and routing configuration if errors, imbalance, or connection storms increase.&lt;/li&gt;
&lt;li&gt;Reverse promotion-tier or parameter-group changes to the recorded values; account for pending-reboot status.&lt;/li&gt;
&lt;li&gt;Remove a new reader only after draining connections and proving availability capacity remains sufficient.&lt;/li&gt;
&lt;li&gt;Revert reader isolation changes immediately if consistency tests or application contracts fail.&lt;/li&gt;
&lt;li&gt;Restore the previous SQL or deployment artifact if digest, latency, error, or correctness gates regress.&lt;/li&gt;
&lt;li&gt;For a failover drill, stop the test workload, reconcile ambiguous transactions, verify the intended writer and readers, and restore topology only through an approved runbook. A completed failover cannot be undone like a configuration edit.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;




























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;False conclusion&lt;/th&gt;&lt;th&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Per-instance load aggregated at cluster scope&lt;/td&gt;&lt;td&gt;The writer is saturated&lt;/td&gt;&lt;td&gt;Preserve role and instance dimensions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Five-minute volume counts treated as instantaneous&lt;/td&gt;&lt;td&gt;A short storage event is precisely timed&lt;/td&gt;&lt;td&gt;Retain waits, SQL, and finer instance telemetry&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Redo-flush wait treated as a service fault&lt;/td&gt;&lt;td&gt;Aurora storage is unhealthy&lt;/td&gt;&lt;td&gt;Correlate commit frequency, latency, SQL, and events&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Shared volume treated as shared cache&lt;/td&gt;&lt;td&gt;All readers have the same read cost&lt;/td&gt;&lt;td&gt;Measure cache and DB load per instance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reader endpoint treated as query balancing&lt;/td&gt;&lt;td&gt;A new replica absorbs existing work&lt;/td&gt;&lt;td&gt;Observe connection targets and recycle policy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Maximum lag used without per-reader data&lt;/td&gt;&lt;td&gt;Every reader is stale&lt;/td&gt;&lt;td&gt;Inspect &lt;code&gt;AuroraReplicaLag&lt;/code&gt; on each reader&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Local temporary space confused with cluster storage&lt;/td&gt;&lt;td&gt;Storage autoscaling will prevent table-full errors&lt;/td&gt;&lt;td&gt;Monitor &lt;code&gt;FreeLocalStorage&lt;/code&gt; and temp behavior&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failover tier treated as capacity assurance&lt;/td&gt;&lt;td&gt;The preferred reader is ready&lt;/td&gt;&lt;td&gt;Test instance size, configuration, workload, and AZ placement&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Restart crosses the evidence window&lt;/td&gt;&lt;td&gt;Counter drops look like recovery&lt;/td&gt;&lt;td&gt;Preserve uptime, events, and reset provenance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Forced failover treated as harmless validation&lt;/td&gt;&lt;td&gt;The test creates user impact&lt;/td&gt;&lt;td&gt;Use approval, retry tests, reconciliation, and abort criteria&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Customer Data:&lt;/strong&gt; The LLM must not receive raw rows, bind values, or unnormalized statement text from the slow query log, &lt;code&gt;events_statements_history&lt;/code&gt;, or &lt;code&gt;SHOW PROCESSLIST&lt;/code&gt;. Send digest text, digest hashes, timings, and row-examined counts — never literals.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Production Execution:&lt;/strong&gt; The LLM cannot run &lt;code&gt;ALTER TABLE&lt;/code&gt;, &lt;code&gt;SET GLOBAL&lt;/code&gt;, &lt;code&gt;KILL&lt;/code&gt;, index DDL, replication commands, or instance modifications. It proposes; a human authorizes, applies, validates, and holds the rollback condition.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Causal Conclusion Without Contradicting Evidence:&lt;/strong&gt; Every hypothesis must cite both the telemetry that supports it and the telemetry that would falsify it. A ranked hypothesis with no disconfirming test attached is an assertion, not a diagnosis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Silent Gap-Filling:&lt;/strong&gt; Where the evidence pack lacks a required signal, the LLM must name it explicitly under missing evidence rather than inferring a plausible value. “Not collected” and “collected and normal” are different findings and must never be merged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Change Without a Human Gate:&lt;/strong&gt; Production changes require human authorization, a stated validation signal, and a defined rollback condition agreed before the change is applied.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Aurora MySQL incidents cross writer, reader, endpoint, local-storage, cluster-volume, and control-plane boundaries that a MySQL-only evidence pack cannot distinguish.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Freeze topology, analyze load per instance, interpret version-specific waits, separate cluster I/O from local temporary storage, inspect every reader, and treat failover as an application-visible transition.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; A second DBA should reproduce the affected entity and failure boundary from immutable evidence, explain contradictions, and predict the metric that changes after remediation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Extend the evidence pack with cluster and instance scope, role history, endpoint targets, promotion tiers, per-reader lag and load, local-storage metrics, RDS events, and failover recovery observations.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The next MySQL article defines the security boundary: diagnostic roles, exported logs, SQL redaction, audit evidence, prompt-injection resistance, and production guardrails.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraMySQL.Reference.Waitevents.html&quot;&gt;AWS — Aurora MySQL wait events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraMySQL.Managing.Tuning.html&quot;&gt;AWS — Tuning Aurora MySQL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.AuroraMonitoring.Metrics.html&quot;&gt;AWS — CloudWatch metrics for Aurora&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Overview.StorageReliability.html&quot;&gt;AWS — Aurora storage&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraMySQL.Managing.Performance.html&quot;&gt;AWS — Temporary storage limits for Aurora MySQL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/ams3-temptable-behavior.html&quot;&gt;AWS — Temporary-table behavior in Aurora MySQL version 3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Endpoints.Reader.html&quot;&gt;AWS — Aurora reader endpoints&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Overview.Endpoints.html&quot;&gt;AWS — Aurora endpoint connections&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraMySQL.Reference.IsolationLevels.html&quot;&gt;AWS — Aurora MySQL isolation levels&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Concepts.AuroraHighAvailability.html&quot;&gt;AWS — High availability for Aurora&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Overview.Reliability.html&quot;&gt;AWS — Aurora reliability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PerfInsights.Overview.html&quot;&gt;AWS — Performance Insights transition&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>ai-engineering</category><category>failures</category></item><item><title>Finding the SQL Behind a MySQL Performance Incident</title><link>https://rajivonai.com/blog/2025-10-30-finding-sql-behind-mysql-performance-incident/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-10-30-finding-sql-behind-mysql-performance-incident/</guid><description>A MySQL 8.4 investigation method for using statement-digest deltas, latency distributions, execution plans, and LLM correlation to prove which workload changed.</description><pubDate>Thu, 30 Oct 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The SQL with the most accumulated latency is not necessarily the SQL that caused the incident; the useful question is which statement changed during the incident window, and why.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The preceding EC2 triage article stopped after classifying a MySQL incident by layer. Rising &lt;code&gt;mysqld&lt;/code&gt; CPU, more running threads, and no lock chain or EBS limit signal make SQL a credible branch—not yet the root cause.&lt;/p&gt;
&lt;p&gt;MySQL 8.4 groups normalized statements by digest with counts, timing, row, sort, and temporary-table evidence. Histograms preserve latency distributions, the slow log retains eligible executions, and &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; adds actual iterator behavior.&lt;/p&gt;
&lt;p&gt;Each source answers a different question. A defensible investigation joins them in time instead of asking an LLM to interpret a lifetime ranking. Aurora-specific behavior belongs to the next article.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A high-ranking digest can mean three different things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;traffic amplification:&lt;/strong&gt; execution count rose while cost per call remained stable;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;runtime regression:&lt;/strong&gt; calls remained comparable while mean or tail latency rose;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;work amplification:&lt;/strong&gt; each execution examined, sorted, materialized, or returned more data.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Lifetime totals blur those cases. Digest normalization replaces literal values, which helps group a query shape but can hide selectivity skew. An average hides a bad tail. A slow-log threshold omits statements below that threshold, even when a frequently executed medium-cost statement consumes the interval. A sampled SQL text can contain secrets or personal data. A plan generated later may not reproduce the incident’s data distribution, session settings, schema, or cache state.&lt;/p&gt;
&lt;p&gt;The core question is: which interval delta, execution, and plan behavior prove that a statement caused the resource and latency change?&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;
&lt;p&gt;Enter this workflow only after layered triage makes SQL or workload behavior a credible candidate.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Incident evidence&lt;/th&gt;&lt;th&gt;Plausible interpretation&lt;/th&gt;&lt;th&gt;Evidence still required&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Digest executions rise; mean latency is stable&lt;/td&gt;&lt;td&gt;Demand amplification&lt;/td&gt;&lt;td&gt;Caller, release, tenant, or job attribution&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Executions are stable; mean and tail latency rise&lt;/td&gt;&lt;td&gt;Runtime regression&lt;/td&gt;&lt;td&gt;Wait context and an incident-equivalent plan&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Rows examined rise faster than rows returned&lt;/td&gt;&lt;td&gt;Access-path or selectivity change&lt;/td&gt;&lt;td&gt;Predicates, chosen index, estimates, and actual rows&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One digest adds most interval latency&lt;/td&gt;&lt;td&gt;Leading workload contributor&lt;/td&gt;&lt;td&gt;Proof that it precedes or explains the system symptom&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Many unrelated digests slow together&lt;/td&gt;&lt;td&gt;Shared resource or contention victimization&lt;/td&gt;&lt;td&gt;Host, wait, lock, and storage correlation&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;h3 id=&quot;1-preserve-digest-identity-and-collection-state&quot;&gt;1. Preserve digest identity and collection state&lt;/h3&gt;
&lt;p&gt;Snapshot &lt;code&gt;performance_schema.events_statements_summary_by_digest&lt;/code&gt; at the baseline and incident boundaries. Key rows by both &lt;code&gt;SCHEMA_NAME&lt;/code&gt; and &lt;code&gt;DIGEST&lt;/code&gt;; the same normalized text in different default schemas is not necessarily the same object access.&lt;/p&gt;
&lt;p&gt;Capture at least:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SCHEMA_NAME,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       DIGEST&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       DIGEST_TEXT,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       COUNT_STAR,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       SUM_TIMER_WAIT,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       AVG_TIMER_WAIT,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       SUM_LOCK_TIME,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       SUM_ROWS_EXAMINED,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       SUM_ROWS_SENT,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       SUM_CREATED_TMP_DISK_TABLES,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       SUM_SORT_MERGE_PASSES,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       SUM_NO_INDEX_USED,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       FIRST_SEEN,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       LAST_SEEN,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       QUANTILE_95,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       QUANTILE_99&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; performance_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;events_statements_summary_by_digest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DIGEST&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IS NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Record server identity, uptime, collection time, MySQL version, Performance Schema sizing, enabled consumers, and resets. If the table fills, unmatched statements accumulate under a &lt;code&gt;NULL&lt;/code&gt; schema and digest; that is coverage loss. Restarts and truncation invalidate subtraction. Keep &lt;code&gt;QUERY_SAMPLE_TEXT&lt;/code&gt; and raw literals in a restricted tier; export redacted text.&lt;/p&gt;
&lt;h3 id=&quot;2-rank-interval-change-not-lifetime-cost&quot;&gt;2. Rank interval change, not lifetime cost&lt;/h3&gt;
&lt;p&gt;Subtract the first snapshot’s counters from the second for each schema and digest. Calculate:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;interval executions       = delta COUNT_STAR&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;interval total latency    = delta SUM_TIMER_WAIT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;interval mean latency     = delta SUM_TIMER_WAIT divided by delta COUNT_STAR&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;rows examined per call    = delta SUM_ROWS_EXAMINED divided by delta COUNT_STAR&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;rows sent per call        = delta SUM_ROWS_SENT divided by delta COUNT_STAR&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Reject deltas crossing a restart, reset, or coverage loss. Rank both added total latency and change from baseline: the first finds contribution; the second finds regression.&lt;/p&gt;
&lt;p&gt;MySQL’s &lt;code&gt;sys.statement_performance_analyzer()&lt;/code&gt; can create, save, compare, and clean up snapshots with a &lt;code&gt;delta&lt;/code&gt; action. A collector can implement the same interval contract externally without truncating summaries.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;QUANTILE_95&lt;/code&gt; and &lt;code&gt;QUANTILE_99&lt;/code&gt; are high estimates from cumulative histograms; do not subtract them. Subtract bucket counts in &lt;code&gt;events_statements_histogram_by_digest&lt;/code&gt;, then recompute interval quantiles.&lt;/p&gt;
&lt;h3 id=&quot;3-recover-representative-executions-safely&quot;&gt;3. Recover representative executions safely&lt;/h3&gt;
&lt;p&gt;The digest identifies a shape, not the literal values that produced it. Correlate approved slow-log records, restricted statement history, application traces, or sanitized replay samples.&lt;/p&gt;
&lt;p&gt;MySQL writes to the slow log after a statement finishes and initial table locks are acquired. Eligibility depends on &lt;code&gt;long_query_time&lt;/code&gt; and &lt;code&gt;min_examined_row_limit&lt;/code&gt;; optional no-index logging has separate controls. This is threshold-selected evidence, not a complete trace. Lowering thresholds adds I/O, exposure, and volume.&lt;/p&gt;
&lt;p&gt;Preserve default schema, relevant optimizer variables, bound-value class, schema, index visibility, and statistics state. Redact literals before model access while retaining a controlled DBA mapping.&lt;/p&gt;
&lt;h3 id=&quot;4-compare-estimates-with-execution-behavior&quot;&gt;4. Compare estimates with execution behavior&lt;/h3&gt;
&lt;p&gt;Start with non-executing &lt;code&gt;EXPLAIN FORMAT=JSON&lt;/code&gt; or &lt;code&gt;EXPLAIN FORMAT=TREE&lt;/code&gt; in an incident-equivalent environment. Preserve access paths, join order, keys, estimated rows, filters, materialization, sorts, and hash joins.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; runs the statement and returns estimates plus actual iterator timing, rows, and loops in tree format. It can repeat expensive work. MySQL 8.4 permits it for &lt;code&gt;SELECT&lt;/code&gt;, &lt;code&gt;TABLE&lt;/code&gt;, and multi-table &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; statements.&lt;/p&gt;
&lt;p&gt;Never paste model-generated SQL after &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt;. Use reviewed read-only SQL on a representative clone or replica, an execution budget, and a kill path. Do not execute a modifying form for evidence.&lt;/p&gt;
&lt;h3 id=&quot;5-prove-the-regression-mechanism&quot;&gt;5. Prove the regression mechanism&lt;/h3&gt;
&lt;p&gt;Compare baseline and incident executions, mean and tail latency, rows examined and sent per call, disk temporary tables, sort passes, selected keys, and plans. Join this with bounded wait and host evidence to distinguish a cause from a victim.&lt;/p&gt;
&lt;p&gt;A changed path plus a large estimate-to-actual gap supports a cardinality mechanism, but does not prove stale statistics. Correlated columns, parameter skew, expressions, and missing histograms can also mislead estimates. An unchanged plan can become expensive because calls, input range, result size, or concurrency changed.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[SQL candidate from layered triage] --&gt; B{Reliable digest delta exists}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| C[repair collection and preserve individual executions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| D{Execution count increased}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes| E{Cost per call stayed stable}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|yes| F[traffic amplification — attribute the caller]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|no| G[traffic and runtime changed — investigate both]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no| H{Mean or tail latency increased}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| I[SQL regression not supported — revisit shared layers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| J{Rows examined or spill work increased}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|yes| K[work amplification — compare plan and selectivity]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|no| L{Lock or shared wait evidence increased}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|yes| M[statement may be a victim — prove the blocker or resource]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|no| N[compare iterator timing and execution context]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The tree separates attribution from remediation. Neither classification authorizes a change.&lt;/p&gt;
&lt;h2 id=&quot;correlate-evidence-with-the-llm&quot;&gt;Correlate Evidence with the LLM&lt;/h2&gt;
&lt;p&gt;Give the model calculated facts and immutable references: digest and window IDs, interval executions and latency, rows examined and sent per call, baseline and incident plan artifacts, host and wait references, and a flag showing that raw SQL is unavailable. Require observations, ranked hypotheses, evidence for and against, missing evidence, and one bounded diagnostic. Confidence is not a measured probability without calibration.&lt;/p&gt;
&lt;p&gt;A useful answer distinguishes call-volume growth from more work per call. A scan alone does not justify an index.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;MySQL’s &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/performance-schema-statement-summary-tables.html&quot;&gt;statement-summary documentation&lt;/a&gt; defines digest aggregation by schema and normalized statement, cumulative timing and work counters, first and last observation times, latency quantiles, and sampling fields. It also documents the finite digest table. The derived operational rule is to preserve coverage and compute window deltas before ranking SQL.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/sys-statement-performance-analyzer.html&quot;&gt;&lt;code&gt;statement_performance_analyzer()&lt;/code&gt; documentation&lt;/a&gt; provides an explicit snapshot, save, and delta workflow over digest summaries. This supports the article’s central pattern: interval comparison is a database-native diagnostic method, not an LLM inference.&lt;/p&gt;
&lt;p&gt;MySQL documents that &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/explain.html&quot;&gt;&lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt;&lt;/a&gt; runs the statement and reports actual iterator timing, rows, and loops beside optimizer estimates. The derived safety rule is that actual-plan collection needs an execution boundary, approval policy, timeout, and representative target.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/slow-query-log.html&quot;&gt;slow query log documentation&lt;/a&gt; defines threshold and row-examination filters. Its absence cannot disprove a high-frequency workload contribution.&lt;/p&gt;
&lt;p&gt;Together, digest deltas identify changed workload, representative executions restore hidden context, and plan evidence explains the mechanism. The LLM correlates those artifacts; it does not create them.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;p&gt;Choose a change only after proving the mechanism and expected metric.&lt;/p&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Proven mechanism&lt;/th&gt;&lt;th&gt;Candidate response&lt;/th&gt;&lt;th&gt;Required proof before production&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Call volume increased&lt;/td&gt;&lt;td&gt;Fix caller loop, cache, batch, or apply admission control&lt;/td&gt;&lt;td&gt;Caller attribution and capacity or correctness impact&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Poor access path&lt;/td&gt;&lt;td&gt;Rewrite predicate or join, or add a deliberately designed index&lt;/td&gt;&lt;td&gt;Representative plan, read benefit, write and storage cost, DDL impact&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Estimate error&lt;/td&gt;&lt;td&gt;Refresh key statistics or create a targeted histogram&lt;/td&gt;&lt;td&gt;Estimate gap, affected plans, sampling behavior, and regression test&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sort or temporary-table work&lt;/td&gt;&lt;td&gt;Reduce selected data, rewrite grouping, or add suitable access order&lt;/td&gt;&lt;td&gt;Operator-level evidence and memory or disk consequence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Oversized result&lt;/td&gt;&lt;td&gt;Paginate, project fewer columns, or change API contract&lt;/td&gt;&lt;td&gt;Consumer behavior and correctness constraints&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Parameter skew&lt;/td&gt;&lt;td&gt;Split statement shapes or use a reviewed optimizer control temporarily&lt;/td&gt;&lt;td&gt;Reproduction across value classes and version-specific behavior&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Statement is a victim&lt;/td&gt;&lt;td&gt;Fix the blocker or shared resource&lt;/td&gt;&lt;td&gt;Temporal lock, wait, host, or storage proof&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;An invisible index tests removing an existing secondary index from optimizer consideration with fast restoration. Building a new invisible index still consumes resources, storage, and write maintenance.&lt;/p&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;Define rollback per change type before execution:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Application SQL:&lt;/strong&gt; retain the previous release artifact and route; restore it if correctness, latency, or error criteria fail.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;New index:&lt;/strong&gt; capture build impact and dependent plans. If the index regresses other reads, make it invisible before deciding whether to drop it; dropping still requires dependency and write-path review.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Existing index removal:&lt;/strong&gt; test invisibility first. Restore visibility if workload or errors regress; do not begin with &lt;code&gt;DROP INDEX&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Statistics or histograms:&lt;/strong&gt; save the prior histogram JSON and affected plans. &lt;code&gt;ANALYZE TABLE&lt;/code&gt; can influence other statements, so validate a workload set—not only the target digest. Restore a user-defined prior histogram where applicable or drop the new histogram under an approved plan.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optimizer hint or control:&lt;/strong&gt; scope it narrowly, give it an expiration condition, and remove it after the durable fix passes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Traffic containment:&lt;/strong&gt; record the original limit and restore gradually while watching the same digest and user-impact metrics.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Reduce collection if slow-log volume, instrumentation overhead, replica lag, duration, or exposure exceeds budget.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;




























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;False conclusion&lt;/th&gt;&lt;th&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Lifetime digest totals ranked as incident data&lt;/td&gt;&lt;td&gt;A historically expensive statement caused the event&lt;/td&gt;&lt;td&gt;Snapshot and subtract comparable windows&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Digest table overflow or reset ignored&lt;/td&gt;&lt;td&gt;Missing shapes appear absent or counters regress&lt;/td&gt;&lt;td&gt;Record null digest share, uptime, sizing, and reset provenance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Normalization hides literal skew&lt;/td&gt;&lt;td&gt;One plan represents every execution&lt;/td&gt;&lt;td&gt;Preserve redacted value classes and representative samples&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Averages used without histograms&lt;/td&gt;&lt;td&gt;Tail regression disappears&lt;/td&gt;&lt;td&gt;Snapshot histogram buckets and calculate interval tails&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slow-log absence treated as proof&lt;/td&gt;&lt;td&gt;No SQL was slow&lt;/td&gt;&lt;td&gt;Record thresholds and correlate digest deltas&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Later &lt;code&gt;EXPLAIN&lt;/code&gt; treated as incident plan&lt;/td&gt;&lt;td&gt;Current statistics explain past behavior&lt;/td&gt;&lt;td&gt;Preserve plan, schema, variables, and statistics in time&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; run casually&lt;/td&gt;&lt;td&gt;Diagnosis repeats expensive work or modifies data&lt;/td&gt;&lt;td&gt;Use reviewed read-only SQL, a representative target, and an execution budget&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Scan automatically triggers an index&lt;/td&gt;&lt;td&gt;Write cost and selectivity are ignored&lt;/td&gt;&lt;td&gt;Prove access benefit and total workload impact&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Many statements slow together&lt;/td&gt;&lt;td&gt;Every query independently regressed&lt;/td&gt;&lt;td&gt;Revisit waits, locks, host, and storage evidence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Raw SQL sent to the model&lt;/td&gt;&lt;td&gt;Literals leak sensitive data&lt;/td&gt;&lt;td&gt;Separate restricted raw evidence from redacted model input&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Customer Data:&lt;/strong&gt; The LLM must not receive raw rows, bind values, or unnormalized statement text from the slow query log, &lt;code&gt;events_statements_history&lt;/code&gt;, or &lt;code&gt;SHOW PROCESSLIST&lt;/code&gt;. Send digest text, digest hashes, timings, and row-examined counts — never literals.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Production Execution:&lt;/strong&gt; The LLM cannot run &lt;code&gt;ALTER TABLE&lt;/code&gt;, &lt;code&gt;SET GLOBAL&lt;/code&gt;, &lt;code&gt;KILL&lt;/code&gt;, index DDL, replication commands, or instance modifications. It proposes; a human authorizes, applies, validates, and holds the rollback condition.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Causal Conclusion Without Contradicting Evidence:&lt;/strong&gt; Every hypothesis must cite both the telemetry that supports it and the telemetry that would falsify it. A ranked hypothesis with no disconfirming test attached is an assertion, not a diagnosis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Silent Gap-Filling:&lt;/strong&gt; Where the evidence pack lacks a required signal, the LLM must name it explicitly under missing evidence rather than inferring a plausible value. “Not collected” and “collected and normal” are different findings and must never be merged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Change Without a Human Gate:&lt;/strong&gt; Production changes require human authorization, a stated validation signal, and a defined rollback condition agreed before the change is applied.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; MySQL’s “top SQL” views combine lifetime cost, demand, per-call work, and tail behavior, while normalized digests and threshold logs omit context needed for causal proof.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Compare schema-and-digest snapshots by incident window, classify traffic versus runtime versus work amplification, recover representative executions safely, and join plan behavior to host, wait, and release evidence.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; A second DBA should reproduce the leading digest and mechanism, identify contradictions, and predict the validation metric.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Add digest snapshots, histogram buckets, collection-state metadata, restricted sample references, plan artifacts, and deterministic delta calculations to the MySQL evidence pack before enabling LLM correlation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The next article applies the same evidence discipline to Aurora MySQL, where database load, Aurora-specific waits, distributed storage, readers, replicas, and failover change the diagnostic boundary.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/performance-schema-statement-summary-tables.html&quot;&gt;MySQL 8.4 — Performance Schema statement summary tables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/performance-schema-statement-digests.html&quot;&gt;MySQL 8.4 — Performance Schema statement digests and sampling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/performance-schema-statement-histogram-summary-tables.html&quot;&gt;MySQL 8.4 — Statement histogram summary tables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/sys-statement-performance-analyzer.html&quot;&gt;MySQL 8.4 — &lt;code&gt;statement_performance_analyzer()&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/slow-query-log.html&quot;&gt;MySQL 8.4 — Slow query log&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/explain.html&quot;&gt;MySQL 8.4 — &lt;code&gt;EXPLAIN&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/analyze-table.html&quot;&gt;MySQL 8.4 — &lt;code&gt;ANALYZE TABLE&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/invisible-indexes.html&quot;&gt;MySQL 8.4 — Invisible indexes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>failures</category><category>checklist</category></item><item><title>LLM-Assisted MySQL Performance Triage on EC2: From Host Saturation to Database Waits</title><link>https://rajivonai.com/blog/2025-10-28-llm-assisted-mysql-performance-triage-ec2/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-10-28-llm-assisted-mysql-performance-triage-ec2/</guid><description>A layered MySQL 8.4 triage method for distinguishing EC2 compute, memory, EBS, connection, lock, and engine-wait failures before investigating individual SQL statements.</description><pubDate>Tue, 28 Oct 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;When MySQL latency rises on EC2, high CPU is a symptom location—not a root cause—and the first investigation must prove whether work is running, waiting, or stalled below the database.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The first engine-specific investigation begins with a deliberately incomplete incident statement: application latency increased, MySQL remains reachable, and EC2 metrics moved in the same window.&lt;/p&gt;
&lt;p&gt;That does not distinguish productive CPU work from row locks, EBS latency, memory reclaim, a competing host process, or an EC2 platform limit.&lt;/p&gt;
&lt;p&gt;On EC2, the DBA owns the guest and storage layout as well as MySQL. Managed-service assumptions do not apply.&lt;/p&gt;
&lt;p&gt;This article classifies the constrained layer and stops. The next article investigates statements and plans; the Aurora article uses Aurora-native evidence rather than treating Aurora as MySQL installed on EC2.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Single-signal diagnosis creates predictable mistakes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;scaling for high &lt;code&gt;CPUUtilization&lt;/code&gt; when sessions are blocked;&lt;/li&gt;
&lt;li&gt;tuning MySQL while Linux reports I/O or memory stalls;&lt;/li&gt;
&lt;li&gt;blaming EBS queue length without latency, I/O size, or limit evidence;&lt;/li&gt;
&lt;li&gt;treating &lt;code&gt;Threads_connected&lt;/code&gt; as active work when most connections are sleeping;&lt;/li&gt;
&lt;li&gt;treating lifetime Performance Schema totals as incident deltas;&lt;/li&gt;
&lt;li&gt;interpreting absent host memory metrics as healthy memory.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;An LLM repeats these mistakes if screenshots and process lists lack timestamps, counter semantics, reset markers, and a comparable baseline.&lt;/p&gt;
&lt;p&gt;The core question is: which evidence proves that the incident belongs primarily to compute, memory, storage, connection concurrency, locks, MySQL internal waits, or a layer outside this database host?&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;
&lt;p&gt;Start when user impact and a supporting signal change in the same bounded window.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;What it establishes&lt;/th&gt;&lt;th&gt;What it does not establish&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Application latency or errors rise&lt;/td&gt;&lt;td&gt;User impact exists&lt;/td&gt;&lt;td&gt;MySQL caused it&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Throughput falls while demand remains&lt;/td&gt;&lt;td&gt;Work is not completing&lt;/td&gt;&lt;td&gt;Whether it is blocked, stalled, or rejected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;EC2 CPU or EBS latency changes&lt;/td&gt;&lt;td&gt;Resource behavior changed&lt;/td&gt;&lt;td&gt;Which workload caused it&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Connections or wait deltas rise&lt;/td&gt;&lt;td&gt;Sessions or waiting increased&lt;/td&gt;&lt;td&gt;That either is causal&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lock relationships appear&lt;/td&gt;&lt;td&gt;Transactions block one another&lt;/td&gt;&lt;td&gt;Which operation created the blocker&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;p&gt;Use a fixed classification order so the investigation does not jump from an application symptom directly to a parameter recommendation.&lt;/p&gt;
&lt;h3 id=&quot;1-establish-time-restarts-and-workload&quot;&gt;1. Establish time, restarts, and workload&lt;/h3&gt;
&lt;p&gt;Align application, CloudWatch, OS, and MySQL timestamps to UTC. Record instance and volume IDs, device mapping, instance type, MySQL and configuration versions, and &lt;code&gt;Uptime&lt;/code&gt;. Collect demand, completed work, and connection attempts with latency. Reject counter deltas across a restart or instance replacement.&lt;/p&gt;
&lt;h3 id=&quot;2-correlate-ec2-service-metrics-with-linux-pressure&quot;&gt;2. Correlate EC2 service metrics with Linux pressure&lt;/h3&gt;
&lt;p&gt;AWS documents five-minute periods for basic monitoring and one-minute periods for detailed monitoring. Short events can disappear inside basic averages. &lt;a href=&quot;https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/viewing_metrics_with_cloudwatch.html&quot;&gt;&lt;code&gt;CPUUtilization&lt;/code&gt;&lt;/a&gt; includes physical CPU time used for guest and EC2 code; guest tools can differ.&lt;/p&gt;
&lt;p&gt;Collect at least:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;CPUUtilization&lt;/code&gt;, status checks, network bytes, and packets;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CPUCreditBalance&lt;/code&gt; and surplus-credit metrics when the instance family is burstable;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;InstanceEBSIOPSExceededCheck&lt;/code&gt; and &lt;code&gt;InstanceEBSThroughputExceededCheck&lt;/code&gt; on supported Nitro instances;&lt;/li&gt;
&lt;li&gt;per-volume operations, throughput, queue length, and latency;&lt;/li&gt;
&lt;li&gt;provisioned volume and instance IOPS and throughput.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;EC2 &lt;code&gt;DiskReadOps&lt;/code&gt; and &lt;code&gt;DiskWriteOps&lt;/code&gt; describe instance-store volumes, not EBS. Use EBS volume metrics and supported Nitro EBS checks.&lt;/p&gt;
&lt;p&gt;Use the CloudWatch agent or another collector for guest memory, swap, filesystem, process, and scheduler evidence.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://docs.kernel.org/accounting/psi.html&quot;&gt;Linux kernel documentation&lt;/a&gt; exposes CPU, memory, and I/O pressure through &lt;code&gt;/proc/pressure/&lt;/code&gt;. &lt;code&gt;some&lt;/code&gt; measures time with at least some tasks stalled; &lt;code&gt;full&lt;/code&gt; measures time with all non-idle tasks stalled for memory or I/O. Capture PSI with run queue, CPU breakdown, memory, swap, major faults, OOM events, filesystem space, device latency, and per-process CPU and I/O.&lt;/p&gt;
&lt;p&gt;Compare pressure with the same workload regime; there is no portable safe threshold.&lt;/p&gt;
&lt;h3 id=&quot;3-separate-connected-sessions-from-running-work&quot;&gt;3. Separate connected sessions from running work&lt;/h3&gt;
&lt;p&gt;MySQL 8.4 defines &lt;code&gt;Threads_connected&lt;/code&gt; as currently open connections and &lt;code&gt;Threads_running&lt;/code&gt; as threads that are not sleeping. Collect both as gauges, then calculate rates for cumulative variables such as &lt;code&gt;Connections&lt;/code&gt;, &lt;code&gt;Aborted_connects&lt;/code&gt;, &lt;code&gt;Questions&lt;/code&gt;, and statement counters.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; VARIABLE_NAME, VARIABLE_VALUE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; performance_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;global_status&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; VARIABLE_NAME &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Uptime&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Connections&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Aborted_connects&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Threads_connected&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Threads_running&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Questions&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Innodb_buffer_pool_reads&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Innodb_buffer_pool_read_requests&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Innodb_buffer_pool_wait_free&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Innodb_data_pending_reads&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Innodb_data_pending_writes&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Innodb_data_pending_fsyncs&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Label every value as a gauge or counter. A connection storm appears in the rate of &lt;code&gt;Connections&lt;/code&gt;. &lt;code&gt;Innodb_buffer_pool_wait_free&lt;/code&gt; is cumulative; pending reads and writes are current backlog.&lt;/p&gt;
&lt;p&gt;Use &lt;code&gt;performance_schema.processlist&lt;/code&gt; for bounded current-state samples. It reads active Performance Schema thread data and can expose other users’ threads with the appropriate privilege. Treat SQL text as restricted evidence; send only digests or redacted text to the LLM.&lt;/p&gt;
&lt;h3 id=&quot;4-calculate-wait-deltas-not-lifetime-rankings&quot;&gt;4. Calculate wait deltas, not lifetime rankings&lt;/h3&gt;
&lt;p&gt;Performance Schema provides wait summaries by event, thread, account, host, and instance. The global table exposes event count and total, minimum, average, and maximum timer values:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; EVENT_NAME, COUNT_STAR, SUM_TIMER_WAIT,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       AVG_TIMER_WAIT, MAX_TIMER_WAIT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; performance_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;events_waits_summary_global_by_event_name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; COUNT_STAR &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SUM_TIMER_WAIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 50&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Summary tables aggregate over time and can be reset. Snapshot baseline, incident, and recovery; subtract by &lt;code&gt;EVENT_NAME&lt;/code&gt;; reject deltas across resets; and record instrumentation coverage. Rank added interval wait time, not lifetime totals.&lt;/p&gt;
&lt;p&gt;Correlate file, socket, and thread summaries with Linux and EBS evidence. A &lt;code&gt;wait/io/file&lt;/code&gt; name alone does not prove storage saturation.&lt;/p&gt;
&lt;h3 id=&quot;5-prove-locks-and-classify-across-layers&quot;&gt;5. Prove locks and classify across layers&lt;/h3&gt;
&lt;p&gt;MySQL 8.4 exposes active InnoDB transactions through &lt;code&gt;INFORMATION_SCHEMA.INNODB_TRX&lt;/code&gt; and lock relationships through Performance Schema &lt;code&gt;data_locks&lt;/code&gt; and &lt;code&gt;data_lock_waits&lt;/code&gt;. The &lt;code&gt;sys.innodb_lock_waits&lt;/code&gt; view makes the dependency easier to inspect:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; wait_age_secs,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       locked_table_schema,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       locked_table_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       waiting_pid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       blocking_pid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       waiting_query,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       blocking_query&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; sys&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;innodb_lock_waits&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; wait_age_secs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;blocking_query&lt;/code&gt; can be &lt;code&gt;NULL&lt;/code&gt; after the blocker becomes idle. Preserve transaction and process IDs, transaction start time, and prior statement history.&lt;/p&gt;
&lt;p&gt;Classify the interval as compute, transaction concurrency, storage, memory, connection management, MySQL internal waiting, or outside the observed host. Give the LLM observations, contradictions, missing evidence, and one next read-only diagnostic—not permission to resize, kill sessions, or change parameters.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;p&gt;Route the evidence by agreement across layers. A branch names the leading classification, not a proven root cause:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[MySQL latency incident] --&gt; B{Linux pressure changed}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| C{CPU pressure and mysqld CPU agree}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|yes| D[compute candidate — inspect statement digests]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|no| E{IO pressure and EBS latency agree}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|yes| F[storage candidate — map file waits to volumes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|no| G[memory candidate — inspect reclaim swap and allocations]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| H{active lock chain present}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| I[concurrency candidate — reconstruct blocker chain]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| J{MySQL wait deltas increased}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|yes| K[engine candidate — inspect dominant interval waits]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|no| L{connections rose without running work}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|yes| M[connection candidate — inspect client pools]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|no| N[outside host candidate — inspect application and network]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented behavior of the three telemetry layers shows why correlation is necessary.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://docs.aws.amazon.com/ebs/latest/userguide/ebs-io-characteristics.html&quot;&gt;Amazon EBS documentation&lt;/a&gt; defines volume queue length as pending I/O requests and latency as the end-to-end time from sending an operation to receiving its acknowledgement. AWS explicitly says optimal queue length varies by workload. A fixed queue threshold without I/O size, latency, IOPS, and throughput context is therefore not a portable diagnosis.&lt;/p&gt;
&lt;p&gt;MySQL’s &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/performance-schema-summary-tables.html&quot;&gt;Performance Schema summary-table documentation&lt;/a&gt; describes aggregated wait, stage, statement, transaction, file, socket, memory, error, and status summaries. It also documents reset behavior when summary tables are truncated. The derived operational rule is to capture deltas plus reset provenance rather than present absolute totals to the LLM.&lt;/p&gt;
&lt;p&gt;MySQL’s &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/sys-metrics.html&quot;&gt;&lt;code&gt;sys.metrics&lt;/code&gt; view&lt;/a&gt; combines global status, InnoDB metrics, and instrumented memory data, while &lt;code&gt;sys.host_summary&lt;/code&gt; groups statement latency, file-I/O latency, connections, scans, and memory by client host. These views can reduce collector complexity, but they inherit Performance Schema coverage and cumulative semantics. A convenient view is not an evidence-completeness guarantee.&lt;/p&gt;
&lt;p&gt;The documented pattern is triangulation: the platform reports service and limit behavior, Linux reports resource stalls and competing processes, and MySQL reports sessions, transactions, and instrumented waits. A root-cause hypothesis becomes credible when those layers agree in time and scope—and weaker when one layer contradicts it.&lt;/p&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;p&gt;Triage should select the next bounded action, not authorize a change. Any production mutation follows the proposal, approval, validation, and rollback contract from the preceding article.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Classification&lt;/th&gt;&lt;th&gt;Safe immediate response&lt;/th&gt;&lt;th&gt;Change that still requires proof and approval&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Compute candidate&lt;/td&gt;&lt;td&gt;Capture interval statement digests and per-process CPU&lt;/td&gt;&lt;td&gt;SQL change, instance resize, or concurrency adjustment&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Transaction concurrency&lt;/td&gt;&lt;td&gt;Preserve the blocker chain and identify the owning operation&lt;/td&gt;&lt;td&gt;Cancel a statement or connection, change transaction scope, or add an index&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Storage path&lt;/td&gt;&lt;td&gt;Map MySQL file events to devices and volumes; identify competing I/O&lt;/td&gt;&lt;td&gt;Provision more IOPS or throughput, relocate files, or change flush behavior&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Host memory&lt;/td&gt;&lt;td&gt;Preserve PSI, reclaim, swap, OOM, allocation, and process evidence&lt;/td&gt;&lt;td&gt;Resize memory, alter buffer-pool size, or change per-session limits&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Connection management&lt;/td&gt;&lt;td&gt;Attribute creation rate and idle sessions to client hosts and pools&lt;/td&gt;&lt;td&gt;Change pool limits, timeouts, proxy configuration, or admission control&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Outside the database host&lt;/td&gt;&lt;td&gt;Inspect pool wait time, application queues, DNS, network, and dependencies&lt;/td&gt;&lt;td&gt;Make no database change until new evidence moves the boundary&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Emergency containment should use a pre-approved runbook. The LLM may explain why a runbook appears applicable, but it must not choose a target, kill a transaction, resize an instance, or change a parameter directly.&lt;/p&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;The diagnostic workflow is read-only, but it still needs a recovery boundary:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Stop or reduce a collector if its query time, connection use, result volume, or Performance Schema overhead crosses its declared budget.&lt;/li&gt;
&lt;li&gt;Retain the last complete evidence-pack version; do not overwrite it with partial recovery samples.&lt;/li&gt;
&lt;li&gt;If an approved containment action changes traffic, pool limits, instance capacity, or job scheduling, record the exact prior state and a deterministic restoration step before execution.&lt;/li&gt;
&lt;li&gt;Treat session or statement cancellation as non-reversible control input. MySQL may roll back transactional work, but the application still needs a documented retry or reconciliation path.&lt;/li&gt;
&lt;li&gt;Roll back on predeclared user-impact, replication, durability, or availability criteria—not because the LLM says the result “looks worse.”&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Misclassification&lt;/th&gt;&lt;th&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Five-minute EC2 averages hide a short event&lt;/td&gt;&lt;td&gt;“No host anomaly”&lt;/td&gt;&lt;td&gt;Enable suitable resolution and retain guest samples&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;EC2 instance-store metrics treated as EBS metrics&lt;/td&gt;&lt;td&gt;“Disk was quiet”&lt;/td&gt;&lt;td&gt;Use EBS volume metrics and supported Nitro EBS checks&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Wait totals ranked without deltas&lt;/td&gt;&lt;td&gt;Old activity becomes current cause&lt;/td&gt;&lt;td&gt;Snapshot by window and account for resets and restarts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Performance Schema coverage is incomplete&lt;/td&gt;&lt;td&gt;Missing waits look like absence of waiting&lt;/td&gt;&lt;td&gt;Record instruments, consumers, sizing, and lost-event counters&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Process list sampled once&lt;/td&gt;&lt;td&gt;Transient concurrency disappears&lt;/td&gt;&lt;td&gt;Collect bounded repeated samples and summaries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Blocking query is null&lt;/td&gt;&lt;td&gt;Idle transaction appears harmless&lt;/td&gt;&lt;td&gt;Join lock, transaction, thread, and statement-history evidence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High buffer-pool hit ratio treated as proof&lt;/td&gt;&lt;td&gt;Storage or flush pressure is dismissed&lt;/td&gt;&lt;td&gt;Correlate pending I/O, file waits, dirty pages, PSI, and EBS latency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Host CPU attributed to MySQL automatically&lt;/td&gt;&lt;td&gt;Backup or agent load becomes a SQL investigation&lt;/td&gt;&lt;td&gt;Include per-process CPU, I/O, and scheduler evidence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Read-only collector is unbounded&lt;/td&gt;&lt;td&gt;Diagnosis worsens the incident&lt;/td&gt;&lt;td&gt;Limit rows, frequency, query time, concurrency, and text collection&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Customer Data:&lt;/strong&gt; The LLM must not receive raw rows, bind values, or unnormalized statement text from the slow query log, &lt;code&gt;events_statements_history&lt;/code&gt;, or &lt;code&gt;SHOW PROCESSLIST&lt;/code&gt;. Send digest text, digest hashes, timings, and row-examined counts — never literals.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Autonomous Production Execution:&lt;/strong&gt; The LLM cannot run &lt;code&gt;ALTER TABLE&lt;/code&gt;, &lt;code&gt;SET GLOBAL&lt;/code&gt;, &lt;code&gt;KILL&lt;/code&gt;, index DDL, replication commands, or instance modifications. It proposes; a human authorizes, applies, validates, and holds the rollback condition.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Causal Conclusion Without Contradicting Evidence:&lt;/strong&gt; Every hypothesis must cite both the telemetry that supports it and the telemetry that would falsify it. A ranked hypothesis with no disconfirming test attached is an assertion, not a diagnosis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Silent Gap-Filling:&lt;/strong&gt; Where the evidence pack lacks a required signal, the LLM must name it explicitly under missing evidence rather than inferring a plausible value. “Not collected” and “collected and normal” are different findings and must never be merged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Change Without a Human Gate:&lt;/strong&gt; Production changes require human authorization, a stated validation signal, and a defined rollback condition agreed before the change is applied.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; MySQL latency on EC2 spans application, cloud, guest operating system, EBS, connection, transaction, and engine-wait boundaries that no single dashboard can classify.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Align one incident window, collect platform-limit and EBS evidence, measure Linux pressure and competing processes, convert MySQL counters to deltas, and correlate connections, waits, locks, and pending I/O before asking the LLM for hypotheses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Replay the same evidence pack through the classifier. It should select the same primary layer, expose contradicting and missing evidence, and recommend the same next read-only diagnostic without proposing a production change.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Build the EC2 MySQL triage collector with explicit gauge and counter metadata, restart detection, one-minute or finer guest evidence, EBS volume mapping, Performance Schema coverage checks, repeated process samples, and lock-chain capture.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The next article takes the investigation from a classified MySQL workload problem to the responsible statement digest, execution plan, and regression mechanism.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/performance-schema-summary-tables.html&quot;&gt;MySQL 8.4 — Performance Schema summary tables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/performance-schema-processlist-table.html&quot;&gt;MySQL 8.4 — Performance Schema processlist table&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/innodb-information-schema-transactions.html&quot;&gt;MySQL 8.4 — InnoDB transaction and locking information&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/sys-innodb-lock-waits.html&quot;&gt;MySQL 8.4 — &lt;code&gt;sys.innodb_lock_waits&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/sys-metrics.html&quot;&gt;MySQL 8.4 — &lt;code&gt;sys.metrics&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/sys-host-summary.html&quot;&gt;MySQL 8.4 — &lt;code&gt;sys.host_summary&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/viewing_metrics_with_cloudwatch.html&quot;&gt;Amazon EC2 — CloudWatch metrics available for instances&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/manage-detailed-monitoring.html&quot;&gt;Amazon EC2 — Detailed monitoring&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/ebs/latest/userguide/ebs-io-characteristics.html&quot;&gt;Amazon EBS — I/O characteristics and monitoring&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.kernel.org/accounting/psi.html&quot;&gt;Linux kernel — Pressure Stall Information&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>ai-engineering</category><category>failures</category></item><item><title>Torn Page Protection Belongs Off the Foreground Path</title><link>https://rajivonai.com/blog/2025-10-25-torn-page-protection-belongs-off-the-foreground-path/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-10-25-torn-page-protection-belongs-off-the-foreground-path/</guid><description>A PostgreSQL kernel experiment shows why moving torn-page protection from WAL to background flush can change write latency.</description><pubDate>Sat, 25 Oct 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The expensive part of torn-page protection is not the extra write; it is where the extra write lands: PostgreSQL’s Full Page Write puts the copy on the foreground Write-Ahead Log path, while InnoDB’s Doublewrite Buffer moves the copy into the background flush path.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Database durability still lives below the abstraction line most application engineers prefer to ignore. That works until a write-heavy system hits checkpoint pressure, latency doubles, and the answer is not a missing index but an 8 KB page being protected from a 4 KB failure.&lt;/p&gt;
&lt;p&gt;PostgreSQL protects against torn pages with &lt;strong&gt;Full Page Write (FPW)&lt;/strong&gt;: after each checkpoint, the first modification of a data page writes the entire page image into &lt;strong&gt;Write-Ahead Log (WAL)&lt;/strong&gt;. MySQL’s InnoDB protects against the same class of failure with a &lt;strong&gt;Doublewrite Buffer (DWB)&lt;/strong&gt;: dirty pages are first written to a dedicated area, synced, then written to their final data-file locations.&lt;/p&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Design&lt;/th&gt;&lt;th&gt;Protection copy lives in&lt;/th&gt;&lt;th&gt;Request path impact&lt;/th&gt;&lt;th&gt;Recovery behavior&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;PostgreSQL FPW&lt;/td&gt;&lt;td&gt;WAL stream&lt;/td&gt;&lt;td&gt;The first post-checkpoint dirtying of each page expands foreground WAL&lt;/td&gt;&lt;td&gt;Recovery restores the full page image from WAL, then replays later WAL records&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;InnoDB DWB&lt;/td&gt;&lt;td&gt;Doublewrite files&lt;/td&gt;&lt;td&gt;Dirty-page copy is paid by flush machinery, not directly by SQL execution&lt;/td&gt;&lt;td&gt;Recovery repairs torn data pages from the doublewrite copy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Atomic-write storage&lt;/td&gt;&lt;td&gt;Storage layer&lt;/td&gt;&lt;td&gt;Database may avoid software copy only if the whole stack actually guarantees page atomicity&lt;/td&gt;&lt;td&gt;Recovery depends on the storage contract being true&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;PostgreSQL’s own documentation says &lt;code&gt;full_page_writes&lt;/code&gt; writes the entire disk page to WAL on first modification after checkpoint and warns that turning it off can cause unrecoverable or silent corruption after failure. The MySQL 8.4 manual describes InnoDB’s doublewrite buffer as a storage area written before final data-file placement and notes that the large sequential write usually avoids doubling I/O operations one-for-one. See the PostgreSQL WAL settings documentation and MySQL InnoDB doublewrite documentation for the baseline behavior: &lt;a href=&quot;https://www.postgresql.org/docs/current/runtime-config-wal.html&quot;&gt;PostgreSQL &lt;code&gt;full_page_writes&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/innodb-doublewrite-buffer.html&quot;&gt;MySQL 8.4 Doublewrite Buffer&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A torn page is not a logical transaction problem. It is a physical write atomicity problem. PostgreSQL pages are normally 8 KB; MySQL InnoDB pages are commonly 16 KB; operating systems and devices often expose smaller practical atomic write units such as 4 KB sectors. If power loss or kernel failure interrupts a database page write, recovery may find a page that is half old and half new.&lt;/p&gt;
&lt;p&gt;That matters because PostgreSQL WAL records are usually physiological: they identify a physical page, then describe a logical change inside it. If the page cannot be parsed after a crash, the redo record may not have a sane object to apply to. The PostgreSQL wiki explains the problem directly: recovery needs a readable page with valid structure before logical page changes can be replayed. &lt;a href=&quot;https://wiki.postgresql.org/wiki/Full_page_writes&quot;&gt;PostgreSQL wiki: Full page writes&lt;/a&gt;&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure point&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;First dirty page after checkpoint in PostgreSQL 16, 17, or 18&lt;/td&gt;&lt;td&gt;The WAL record may include an 8 KB full page image instead of only the logical change&lt;/td&gt;&lt;td&gt;Write-heavy workloads see WAL volume jump immediately after checkpoint&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;checkpoint_timeout&lt;/code&gt; too low, such as the documented minimum of 30 seconds&lt;/td&gt;&lt;td&gt;Pages become “first dirty after checkpoint” more often&lt;/td&gt;&lt;td&gt;Lower recovery distance increases foreground WAL amplification&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;max_wal_size&lt;/code&gt; too low under write load&lt;/td&gt;&lt;td&gt;PostgreSQL triggers size-driven checkpoints earlier than the time schedule&lt;/td&gt;&lt;td&gt;A workload can enter a loop of checkpoint, FPW surge, WAL growth, checkpoint&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;wal_compression=off&lt;/code&gt; with highly compressible page images&lt;/td&gt;&lt;td&gt;Full page images are stored without compression&lt;/td&gt;&lt;td&gt;The storage bill moves from CPU to WAL bandwidth; compression can help but adds CPU on WAL insert and replay&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Data checksums enabled&lt;/td&gt;&lt;td&gt;Hint-bit behavior can create additional WAL pressure because checksum-protected pages need correctness around page writes&lt;/td&gt;&lt;td&gt;Checksums detect corruption; they do not remove the need for torn-page protection&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Benchmark with &lt;code&gt;full_page_writes=off&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Throughput improves while the system is no longer protected against the same crash class&lt;/td&gt;&lt;td&gt;This is a measurement mode, not a production durability design&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;PostgreSQL checkpoints are started by &lt;code&gt;checkpoint_timeout&lt;/code&gt; or when &lt;code&gt;max_wal_size&lt;/code&gt; is about to be exceeded. That means FPW makes checkpoint frequency a durability-performance coupling: shorter intervals reduce crash-recovery distance but increase the rate at which pages become eligible for full-page images again.&lt;/p&gt;
&lt;p&gt;The core question is not whether FPW or DWB performs “two writes.” The question is whether the durability copy blocks the foreground commit path, or whether the system can batch it behind dirty-page flushing without weakening crash recovery.&lt;/p&gt;
&lt;h2 id=&quot;move-torn-page-copies-off-the-foreground-path&quot;&gt;Move Torn-Page Copies Off the Foreground Path&lt;/h2&gt;
&lt;p&gt;The right architecture is not “turn off full-page writes and hope the storage behaves.” The right architecture is to separate two responsibilities that FPW intentionally combines: WAL should preserve transaction order, while the torn-page protection copy should be paid by the page-flush path.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SQL[SQL transaction] --&gt; Buffer[shared buffer page dirtied]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Buffer --&gt; WAL[WAL foreground path — logical record]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Buffer --&gt; Checkpoint[checkpoint boundary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Checkpoint --&gt; FPW[PostgreSQL FPW — first dirty page image in WAL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Buffer --&gt; Flusher[background dirty page flusher]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Flusher --&gt; DWB[Doublewrite area — sequential page copies]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DWB --&gt; Sync[fsync doublewrite area]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Sync --&gt; DataFiles[scatter write final data files]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    FPW --&gt; Recovery[crash recovery — restore page then replay WAL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DataFiles --&gt; Recovery&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DWB --&gt; Recovery&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The important distinction is scheduling. FPW pays the copy at WAL insertion time for the first page modification after checkpoint. DWB pays the copy when dirty pages leave the buffer pool. Both protect against torn pages; they do not put the pressure on the same queue.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Keep WAL responsible for transaction ordering, not page-copy transport.&lt;/p&gt;
&lt;p&gt;In PostgreSQL, WAL must be flushed before dirty data pages reach durable storage. That ordering is non-negotiable. A DWB prototype should not weaken WAL-before-data; it should remove full page images from the normal WAL record path only when the doublewrite mechanism can guarantee a complete repair copy before final page placement.&lt;/p&gt;
&lt;p&gt;Verification: crash after WAL flush but before final data-file write; recovery must replay WAL without reading an unrecoverable torn page.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Insert a doublewrite stage into the dirty-page flush path.&lt;/p&gt;
&lt;p&gt;The flush path should write dirty buffers into a sequential doublewrite area, force that area durable, then write the same pages to their final relation files. The doublewrite area needs enough metadata to map page identity back to relation fork and block number after restart.&lt;/p&gt;
&lt;p&gt;Verification: force a partial final data-file page write and confirm restart repairs it from the doublewrite copy before normal redo continues.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Preserve checkpoint semantics explicitly.&lt;/p&gt;
&lt;p&gt;A checkpoint cannot simply assume pages are safe because they were scheduled for writeback. It needs a durable boundary: either the final page reached storage intact, or the doublewrite copy did. Otherwise the checkpoint can advertise a recovery point that depends on a page image which exists only in kernel cache.&lt;/p&gt;
&lt;p&gt;Verification: kill the postmaster during checkpoint completion, restart, and verify that checkpoint redo location never advances past unprotected dirty pages.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Measure WAL bytes, data-file bytes, fsync latency, and tail latency separately.&lt;/p&gt;
&lt;p&gt;A DWB design can reduce foreground WAL pressure while increasing background writeback pressure. That is a good trade only if latency-critical SQL stops waiting and the background system does not fall behind. Use &lt;code&gt;pg_current_wal_lsn()&lt;/code&gt; deltas, &lt;code&gt;pg_stat_bgwriter&lt;/code&gt;, &lt;code&gt;pg_stat_io&lt;/code&gt; in PostgreSQL 16 and later, filesystem writeback metrics, and storage latency histograms.&lt;/p&gt;
&lt;p&gt;Verification: compare p50, p95, and p99 transaction latency across &lt;code&gt;checkpoint_timeout&lt;/code&gt;, &lt;code&gt;max_wal_size&lt;/code&gt;, and &lt;code&gt;shared_buffers&lt;/code&gt;, not only aggregate transactions per second.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Treat AI-assisted kernel work as scaffolding, not proof.&lt;/p&gt;
&lt;p&gt;Zongzhi Chen’s 2026 experiment reported a PostgreSQL prototype where Claude Code helped replace FPW with a DWB-style mechanism, with DWB outperforming FPW in an I/O-bound pgbench workload. That is interesting engineering signal, especially because the patch touches real storage-engine paths. It is not enough to declare the design production-safe. Storage bugs are excellent at passing normal tests and failing only when the machine dies at precisely the wrong time. See the source experiment here: &lt;a href=&quot;https://medium.com/@baotiao/in-2026-can-ai-modify-database-kernel-code-c7c88cb43389&quot;&gt;Zongzhi Chen, 2026&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Verification: run crash-restart loops with forced partial writes, checksum validation, logical consistency checks, and comparisons against a known-good source.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented PostgreSQL pattern is that FPW is checkpoint-coupled. The PostgreSQL documentation states that the first modification of a page after checkpoint writes the full page image to WAL, and that increasing checkpoint interval parameters can reduce that cost. That is not an implementation footnote; it is the operational reason write latency often worsens around checkpoint-heavy workloads.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Documented behavior&lt;/th&gt;&lt;th&gt;Production implication&lt;/th&gt;&lt;th&gt;Validation signal&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;full_page_writes=on&lt;/code&gt; is the default in PostgreSQL and protects against partially completed page writes&lt;/td&gt;&lt;td&gt;Disabling it for throughput changes the crash-safety contract&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW full_page_writes;&lt;/code&gt; must be treated as a durability check, not a tuning curiosity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Full page images occur on first page modification after checkpoint&lt;/td&gt;&lt;td&gt;Checkpoint cadence directly affects WAL amplification&lt;/td&gt;&lt;td&gt;WAL growth should be measured before and after &lt;code&gt;CHECKPOINT&lt;/code&gt; under the same write workload&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;wal_compression&lt;/code&gt; can compress full page images with &lt;code&gt;pglz&lt;/code&gt;, &lt;code&gt;lz4&lt;/code&gt;, or &lt;code&gt;zstd&lt;/code&gt; when compiled in&lt;/td&gt;&lt;td&gt;Compression shifts cost from WAL bandwidth to CPU and replay decompression&lt;/td&gt;&lt;td&gt;Compare WAL bytes and CPU saturation with each compression method&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;pg_checksums&lt;/code&gt; can verify checksums offline when checksums are enabled&lt;/td&gt;&lt;td&gt;Checksums detect page corruption; they do not repair missing torn-page protection by themselves&lt;/td&gt;&lt;td&gt;Restart, stop cleanly, run &lt;code&gt;pg_checksums --check&lt;/code&gt; against the cluster&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;InnoDB DWB writes pages to doublewrite files before final placement&lt;/td&gt;&lt;td&gt;InnoDB pays an extra page-copy step outside the user transaction’s immediate WAL insert path&lt;/td&gt;&lt;td&gt;Monitor page cleaner activity, doublewrite files, fsync latency, and data-file writeback&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The documented InnoDB pattern is different. MySQL 8.4 says InnoDB writes flushed buffer-pool pages to doublewrite storage before writing to final data files, and crash recovery can use the doublewrite copy if the final page write was interrupted. The same documentation also says data is written twice, but not necessarily at twice the I/O operation cost, because the doublewrite write is a large sequential chunk with a single &lt;code&gt;fsync()&lt;/code&gt; in normal configurations.&lt;/p&gt;
&lt;p&gt;That distinction is the architecture lesson. Equal total bytes do not imply equal user-visible latency. A foreground WAL write competes with commit progress. A background doublewrite stage competes with page flushing, eviction, checkpoint completion, and storage bandwidth. Both queues can saturate; they fail differently.&lt;/p&gt;
&lt;p&gt;The source experiment’s reported pgbench numbers are consistent with this mechanism. In the reported write-only 128-thread result, FPW-on delivered 14,857 transactions per second, while the DWB prototype delivered 33,814 transactions per second. The interesting result is not “DWB is 2.3x faster” as a universal claim. The interesting result is that moving the copy away from foreground WAL changed where the bottleneck surfaced.&lt;/p&gt;
&lt;p&gt;For production builders, the deeper lesson is about validation. A storage-engine change is not proven by a five-minute pgbench run. It needs a crash matrix.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Test class&lt;/th&gt;&lt;th&gt;What it proves&lt;/th&gt;&lt;th&gt;Minimum bar&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Forced partial final-page write&lt;/td&gt;&lt;td&gt;DWB can repair a torn data page&lt;/td&gt;&lt;td&gt;Inject half-page writes and confirm recovery restores the page&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Crash after doublewrite sync before final scatter write&lt;/td&gt;&lt;td&gt;Durable repair copy exists before final placement&lt;/td&gt;&lt;td&gt;Restart must complete without checksum failure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Crash during doublewrite write&lt;/td&gt;&lt;td&gt;Recovery ignores incomplete doublewrite entries&lt;/td&gt;&lt;td&gt;Restart must not restore from a corrupt doublewrite slot&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Checkpoint boundary crash&lt;/td&gt;&lt;td&gt;Recovery point is not advanced beyond protected pages&lt;/td&gt;&lt;td&gt;Repeated kill during checkpoint must preserve logical contents&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica and backup interaction&lt;/td&gt;&lt;td&gt;WAL stream remains sufficient for replicas and point-in-time recovery expectations&lt;/td&gt;&lt;td&gt;Physical replica, base backup, and restore tests must pass&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Device diversity&lt;/td&gt;&lt;td&gt;Sequential-write assumptions hold on real storage&lt;/td&gt;&lt;td&gt;Test local NVMe, network-attached block storage, and throttled cloud volumes&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;I have not run this PostgreSQL DWB prototype at scale personally. The documented failure mode is clear anyway: if a DWB design acknowledges a checkpoint or allows final data-file writes before the repair copy is durable, it can create a database that looks faster until the first badly timed crash. That is the least charming kind of benchmark.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Doublewrite area becomes the new bottleneck&lt;/td&gt;&lt;td&gt;High dirty-page churn with &lt;code&gt;shared_buffers&lt;/code&gt; large enough to delay eviction, then sudden checkpoint pressure&lt;/td&gt;&lt;td&gt;Size the doublewrite area for flush bursts; track fsync latency and dirty buffer age&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Recovery restores the wrong page version&lt;/td&gt;&lt;td&gt;Doublewrite metadata does not encode relation identity, fork, block number, and page LSN safely&lt;/td&gt;&lt;td&gt;Treat DWB metadata as recovery-critical; checksum the slot header and page body&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Checkpoint completes too early&lt;/td&gt;&lt;td&gt;Prototype marks pages safe after scheduling writeback instead of after durable doublewrite or durable final write&lt;/td&gt;&lt;td&gt;Checkpoint accounting must wait for a durable protection point&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cloud block storage reorders or stalls writes&lt;/td&gt;&lt;td&gt;Network-attached volumes with variable latency and opaque cache behavior&lt;/td&gt;&lt;td&gt;Test under the actual storage class; do not extrapolate from local NVMe&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;WAL compression already solves enough of the pain&lt;/td&gt;&lt;td&gt;PostgreSQL workload has compressible full page images and CPU headroom&lt;/td&gt;&lt;td&gt;Benchmark &lt;code&gt;wal_compression=zstd&lt;/code&gt; or &lt;code&gt;lz4&lt;/code&gt; before changing storage architecture&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Full-page images help replica recovery behavior&lt;/td&gt;&lt;td&gt;Large working sets where WAL page images reduce random data-page reads during replay&lt;/td&gt;&lt;td&gt;Measure replica replay lag and recovery prefetch behavior, not only primary throughput&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DWB increases write amplification under cold churn&lt;/td&gt;&lt;td&gt;Workload dirties pages once and evicts them without repeated updates&lt;/td&gt;&lt;td&gt;Compare physical bytes written per committed transaction across FPW and DWB&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AI-generated kernel patch misses crash edge cases&lt;/td&gt;&lt;td&gt;Normal regression tests pass because they rarely interrupt I/O at durability boundaries&lt;/td&gt;&lt;td&gt;Add fault injection, checksum validation, crash loops, and page-level corruption tests&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Treating all durability writes as equivalent hides the queue that users actually wait on.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Keep transaction ordering in WAL, but move torn-page repair copies to a durable background flush mechanism when the storage engine can prove the ordering.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: A credible result is not one pgbench chart; it is lower foreground WAL amplification plus successful crash recovery across forced partial writes and checkpoint-boundary failures.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, measure your PostgreSQL WAL growth around &lt;code&gt;CHECKPOINT&lt;/code&gt; with &lt;code&gt;full_page_writes=on&lt;/code&gt;, test &lt;code&gt;wal_compression&lt;/code&gt;, and record p95 commit latency alongside &lt;code&gt;pg_stat_bgwriter&lt;/code&gt; and &lt;code&gt;pg_stat_io&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A storage engine is allowed to be faster only after it has earned the right to crash badly and come back boring.&lt;/p&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>architecture</category></item><item><title>PostgreSQL 18 Replication Upgrade Opportunities</title><link>https://rajivonai.com/blog/2025-04-21-postgresql-18-replication-upgrade-opportunities/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-04-21-postgresql-18-replication-upgrade-opportunities/</guid><description>What changes in replication when upgrading from PostgreSQL 14–16 to PostgreSQL 18: parallel apply, pg_createsubscriber, and surfaced conflict visibility.</description><pubDate>Tue, 07 Oct 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;PostgreSQL 18 ships with replication changes that are improvements in normal operation and surprises in the first week after upgrade.&lt;/strong&gt; Parallel logical apply, the &lt;code&gt;pg_createsubscriber --all&lt;/code&gt; utility, and better conflict logging each change the operational model for replication in ways that require preparation — not because they are dangerous, but because they surface behavior that was previously invisible. Planning the upgrade without understanding these changes means discovering them at 2 AM.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This post was originally written during the PostgreSQL 18 beta 1 period. It has been updated to confirm behavior against the final release (September 25, 2025). The &lt;code&gt;conflict_resolution&lt;/code&gt; parameter and &lt;code&gt;pg_createsubscriber --all&lt;/code&gt; behavior described here reflect the GA release.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;p&gt;Upgrading to PostgreSQL 18 introduces critical changes to logical replication that alter default concurrency and conflict visibility. While these represent architectural improvements, they will break applications that assume sequential logical apply and will trigger alerts for previously silent replication conflicts. Engineering leaders must ensure teams audit their current logical replication topology, explicitly test parallel apply ordering assumptions, and tune monitoring to handle the new structured conflict logging before upgrading production environments.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Teams on PostgreSQL 14, 15, or 16 are increasingly evaluating an upgrade to PostgreSQL 18. The database engine improvements — parallel query enhancements, improved statistics, and JSON improvements — are the typical headline justifications. Replication is often assessed as “nothing major changed” until someone runs the upgrade in staging and discovers that the conflict logging they had silenced for years is now surfacing in a new format that breaks their monitoring.&lt;/p&gt;
&lt;p&gt;The three replication areas that actually change in PostgreSQL 18 and require deliberate assessment:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Parallel logical apply&lt;/strong&gt; (available since PostgreSQL 16, now enabled by default with &lt;code&gt;max_parallel_apply_workers_per_subscription = 2&lt;/code&gt;): logical replication can now apply transactions concurrently across multiple apply workers when the publisher commits parallel transactions. This improves throughput significantly for write-heavy publishers but means that the apply order across concurrent transactions is no longer sequential — which breaks applications that assume apply order matches commit order.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;pg_createsubscriber --all&lt;/code&gt;&lt;/strong&gt;: a new command-line utility that converts a physical streaming standby into a logical replication subscriber in a single operation. Teams with physical standbys used for read scaling can now convert them to logical subscribers without tearing down and rebuilding the standby. This is an opportunity for teams that want subscriber-level table filtering or cross-version replication.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Improved conflict logging&lt;/strong&gt;: PostgreSQL 18 surfaces logical replication conflicts with more detail in the server log, including the specific row values involved. Previously, conflicts were logged at a level that was easy to suppress; now they appear as &lt;code&gt;ERROR&lt;/code&gt; level with structured detail. If you had suppressed replication conflict alerts because the volume was too noisy, PostgreSQL 18 will make them reappear prominently.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The current approach to PostgreSQL major version upgrades often treats replication as a transparent layer that will simply resume functioning once the engine is upgraded. However, this approach breaks when upgrading to PostgreSQL 18 because the default concurrency model for logical replication fundamentally shifts.&lt;/p&gt;
&lt;p&gt;When a team upgrades a logical subscriber to PostgreSQL 18 without preparation, the new default of &lt;code&gt;max_parallel_apply_workers_per_subscription = 2&lt;/code&gt; immediately activates. If the downstream application relies on strict sequential ordering of independent transactions — for example, building derived state or feeding an event-driven architecture — the sudden parallel apply will cause subtle data anomalies. Concurrently, the new verbose conflict logging will trigger massive volumes of &lt;code&gt;ERROR&lt;/code&gt; level alerts for conflicts that were previously ignored, overwhelming observability pipelines.&lt;/p&gt;
&lt;p&gt;How can engineering teams proactively identify and manage these replication changes before they cause data anomalies and alert fatigue in production?&lt;/p&gt;
&lt;h2 id=&quot;upgrade-readiness-framework&quot;&gt;Upgrade Readiness Framework&lt;/h2&gt;
&lt;p&gt;To navigate these changes, teams should follow a structured diagnostic and remediation process.&lt;/p&gt;
&lt;h3 id=&quot;symptoms-and-signals&quot;&gt;Symptoms and Signals&lt;/h3&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Signal&lt;/th&gt;&lt;th&gt;Where to see it&lt;/th&gt;&lt;th&gt;What it means&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Current replication lag baseline&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_replication.replay_lag&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Establish before upgrade to detect regression&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Existing logical subscriptions&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_subscription&lt;/code&gt; on subscribers&lt;/td&gt;&lt;td&gt;Will be affected by parallel apply default&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replication conflict errors in current logs&lt;/td&gt;&lt;td&gt;&lt;code&gt;postgresql.log&lt;/code&gt; grep for &lt;code&gt;conflict in logical replication&lt;/code&gt;&lt;/td&gt;&lt;td&gt;These will become more visible in PG18&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Physical standbys that could become logical&lt;/td&gt;&lt;td&gt;Infrastructure inventory&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_createsubscriber --all&lt;/code&gt; conversion opportunity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Current &lt;code&gt;max_wal_senders&lt;/code&gt; and &lt;code&gt;max_replication_slots&lt;/code&gt; values&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW max_wal_senders; SHOW max_replication_slots;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Parallel apply adds additional worker connections&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Identify current replication type and topology&lt;/strong&gt; — establish what you have before planning what changes:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Check physical standbys (streaming replication)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client_addr, application_name, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, sent_lsn, replay_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_last_xact_replay_timestamp() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; lag_estimate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_replication;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Check logical subscriptions (run on subscriber)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; subname, subenabled, subconninfo, subpublications&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_subscription;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Check logical publishers (run on publisher)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pubname, puballtables, pubinsert, pubupdate, pubdelete&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_publication;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This establishes your current topology. Physical standbys and logical subscribers are upgraded differently — physical standbys follow the primary’s upgrade path, logical subscribers can remain on older versions while the publisher upgrades to PG18, which is one of the benefits of logical replication.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;Measure current replication lag baseline&lt;/strong&gt; — capture before upgrade to detect regressions:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- On publisher: physical replication lag&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  application_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  client_addr,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  write_lag,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  flush_lag,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  replay_lag&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_replication&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; replay_lag &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NULLS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LAST&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- On subscriber: time-based lag for logical replication&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  subname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  received_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_msg_send_time,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_msg_receipt_time,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  latest_end_time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_subscription;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Record these baseline values. After the upgrade, the same queries run against the upgraded instance should show stable or improved lag. If lag increases after upgrade, parallel apply worker count or worker connection limits may need tuning.&lt;/p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;Check for existing logical replication subscriptions&lt;/strong&gt; — these require the most careful upgrade planning:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- On subscriber: full subscription inventory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;subname&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;subenabled&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  r&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;srrelid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;::regclass &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tablename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  r&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;srsubstate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_subscription s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_subscription_rel r &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; r&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;srsubid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;oid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;subname&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;srsubstate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Check current parallel apply setting (PostgreSQL 16+)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW max_parallel_apply_workers_per_subscription;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If your subscribers are on PostgreSQL 16 or 17, &lt;code&gt;max_parallel_apply_workers_per_subscription&lt;/code&gt; may already be set. If subscribers are on PostgreSQL 14 or 15, this parameter does not exist yet — it becomes relevant when the subscriber is upgraded to 18.&lt;/p&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;&lt;strong&gt;Audit current conflict handling&lt;/strong&gt; — understand what conflicts are already happening silently:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Search the current PostgreSQL log for existing replication conflicts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;conflict in logical replication&apos;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/log/postgresql/postgresql.log&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Get the distinct conflict types&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;grep&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;conflict in logical replication&apos;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/log/postgresql/postgresql.log&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -oP&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;conflict on \w+&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; sort&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; uniq&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -c&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; sort&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -rn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you find zero conflicts in the log, either your replication is clean or conflicts are being logged at a level you are not capturing. After upgrading to PostgreSQL 18, conflict errors will be more prominently logged. Knowing the baseline before upgrade means you can distinguish “this is a new problem” from “this was always happening.”&lt;/p&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;&lt;strong&gt;Check &lt;code&gt;max_wal_senders&lt;/code&gt; and &lt;code&gt;max_replication_slots&lt;/code&gt; headroom&lt;/strong&gt; — parallel apply uses additional worker slots:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW max_wal_senders;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW max_replication_slots;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Current usage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; active_wal_senders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_replication;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; active_slots &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_replication_slots &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; active;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Parallel apply workers each require a &lt;code&gt;walsender&lt;/code&gt; connection from the publisher. If you have 5 logical subscribers with &lt;code&gt;max_parallel_apply_workers_per_subscription = 2&lt;/code&gt;, you need at minimum &lt;code&gt;5 * (1 + 2) = 15&lt;/code&gt; wal senders just for logical replication. Ensure &lt;code&gt;max_wal_senders&lt;/code&gt; is sized to accommodate this plus physical standbys.&lt;/p&gt;
&lt;h3 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Planning PG18 upgrade] --&gt; B{Using logical replication?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| C{Parallel apply already enabled?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|yes — PG16 or 17| D[Test apply ordering assumptions in staging]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|no — PG14 or 15| E[Set max_parallel_apply to 0 initially after upgrade]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[Enable incrementally after validation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no — physical only| G{Physical standbys present?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|yes| H{Convert any to logical?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| I[Test pg_createsubscriber in staging first]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| J[Physical replication — minimal changes in PG18]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; K{Conflict log volume change after upgrade?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|yes — more conflicts visible| L[Review and resolve — do not suppress]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|no| M[Validate lag baseline matches pre-upgrade]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Option 1 — Staged parallel apply enablement&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;After upgrading the subscriber to PostgreSQL 18, start with parallel apply disabled, validate behavior, then enable incrementally:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Disable parallel apply immediately after upgrade&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SUBSCRIPTION my_subscription&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (max_parallel_apply_workers_per_subscription &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Verify subscriber is applying correctly with zero parallel workers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; subname, received_lsn, latest_end_lsn, latest_end_time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_subscription;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- After 48 hours of stable operation, enable with 1 worker&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SUBSCRIPTION my_subscription&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (max_parallel_apply_workers_per_subscription &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- If stable for another 48 hours, increase to default&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SUBSCRIPTION my_subscription&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (max_parallel_apply_workers_per_subscription &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The risk of parallel apply is not data corruption — PostgreSQL ensures causally-related transactions are applied in order. The risk is application code that assumes a specific apply order between causally-independent transactions and uses that assumption to build derived state.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 2 — Convert physical standby with &lt;code&gt;pg_createsubscriber&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PostgreSQL 18 includes &lt;code&gt;pg_createsubscriber&lt;/code&gt; with an &lt;code&gt;--all&lt;/code&gt; flag that converts an existing physical standby to a logical subscriber in one operation:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Stop the standby (required — it cannot be running during conversion)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_ctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; stop&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -D&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/lib/postgresql/standby_data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Convert to logical subscriber&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# (run as postgres user, connecting to publisher)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_createsubscriber&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --pgdata=/var/lib/postgresql/standby_data&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --publisher-server=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;host=publisher port=5432 dbname=mydb&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --all&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --subscription-name=my_logical_sub&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Start the converted subscriber&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_ctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; start&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -D&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/lib/postgresql/standby_data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Verify subscription is running&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;psql&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;SELECT subname, subenabled FROM pg_subscription;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;--all&lt;/code&gt; flag replicates all tables from all databases, equivalent to &lt;code&gt;FOR ALL TABLES IN SCHEMA public&lt;/code&gt;. Per the PostgreSQL 18 beta documentation, the standby must be on the same major version as the publisher for the conversion to succeed.&lt;/p&gt;
&lt;p&gt;This is an opportunity if you have read replicas that are underutilized as physical standbys and would benefit from logical replication’s filtering and cross-version upgrade flexibility.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 3 — Conflict monitoring setup for PG18 log format&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PostgreSQL 18 logs replication conflicts with structured detail. Update any log parsing or alerting to match the new format:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# New PG18 conflict log format includes row values:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# ERROR:  conflict detected on relation &quot;public.orders&quot;: conflict=insert_exists&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#         Key (id)=(12345); existing local tuple (12345, &apos;pending&apos;, ...);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#         remote tuple (12345, &apos;shipped&apos;, ...); ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Update log monitoring to capture conflict type&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -E&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;conflict=(insert_exists|update_missing|delete_missing)&apos;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  /var/log/postgresql/postgresql.log&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  awk&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;{print $NF}&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; sort&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; uniq&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Set up a per-conflict-type count alert in your monitoring tool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Alert threshold: &gt; 10 conflicts per hour of any type&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The PostgreSQL 18 beta documentation describes the &lt;code&gt;conflict_resolution&lt;/code&gt; parameter for subscriptions (new in PG18), which can be set to &lt;code&gt;apply_remote&lt;/code&gt; (default), &lt;code&gt;keep_local&lt;/code&gt;, or &lt;code&gt;skip&lt;/code&gt; to control automatic conflict resolution behavior. Previously, all conflicts required manual &lt;code&gt;SKIP&lt;/code&gt; intervention.&lt;/p&gt;
&lt;h3 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Parallel apply&lt;/strong&gt;: disable immediately with &lt;code&gt;ALTER SUBSCRIPTION ... SET (max_parallel_apply_workers_per_subscription = 0)&lt;/code&gt;. No data loss — takes effect on the next transaction. Reversible at any time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;pg_createsubscriber&lt;/code&gt; conversion&lt;/strong&gt;: not directly reversible — once converted to a logical subscriber, restoring to a physical standby requires rebuilding the standby from the primary with &lt;code&gt;pg_basebackup&lt;/code&gt;. Keep a snapshot of the standby data directory before conversion.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PostgreSQL 18 upgrade&lt;/strong&gt;: major version downgrades require restoring from a pre-upgrade backup. The upgrade itself does not change replication topology; the changes are in behavior. Pre-upgrade backup is the only rollback path.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Conflict resolution parameter&lt;/strong&gt;: &lt;code&gt;ALTER SUBSCRIPTION ... SET (conflict_resolution = &apos;skip&apos;)&lt;/code&gt; can be set or unset at any time without a restart.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h3&gt;
&lt;p&gt;A pre-upgrade validation script that runs the five checks automatically and flags risks:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/bin/bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# PostgreSQL 18 replication upgrade readiness check&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;PSQL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;psql -tAc&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;=== Replication Upgrade Readiness Check ===&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Check 1: Replication topology&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;--- Logical subscriptions:&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$PSQL &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SELECT count(*) FROM pg_subscription WHERE subenabled;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Check 2: Current lag&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;--- Max replay lag (physical):&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$PSQL &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SELECT max(replay_lag) FROM pg_stat_replication;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Check 3: Parallel apply headroom&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;MAX_WS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$($PSQL &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SHOW max_wal_senders;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ACTIVE_WS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$($PSQL &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SELECT count(*) FROM pg_stat_replication;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SUB_COUNT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$($PSQL &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SELECT count(*) FROM pg_subscription;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;NEEDED_WS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$((&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ACTIVE_WS&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; SUB_COUNT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# conservative: 3 workers per sub&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;--- max_wal_senders: &lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$MAX_WS&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;, current active: &lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$ACTIVE_WS&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;, needed with parallel: &lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$NEEDED_WS&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Check 4: Existing conflict count&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;--- Conflict count in last 7 days of logs:&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;conflict in logical replication&apos;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/log/postgresql/postgresql.log&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; 2&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/dev/null&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;0&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;=== Done ===&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Run this against production before the upgrade window and again 24 hours after the upgrade to confirm stable behavior.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern is that PostgreSQL 18 fundamentally alters logical replication concurrency. The PostgreSQL Global Development Group’s beta release notes describe parallel logical apply as controlled by &lt;code&gt;max_parallel_apply_workers_per_subscription&lt;/code&gt;, with a default of 2 workers. The parallel apply documentation explicitly notes that causally-related transactions — transactions where one depends on the other’s committed state — are always applied in order, but independent concurrent transactions may be applied in a different order than they were committed on the publisher.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;pg_createsubscriber&lt;/code&gt; utility was introduced in PostgreSQL 17 and is extended in PostgreSQL 18 with the &lt;code&gt;--all&lt;/code&gt; flag. The documented behavior is that it stops WAL recovery on the standby, promotes it to standalone, creates the necessary publication on the publisher, and sets up the logical subscription — all in one operation. The beta documentation notes that the standby must have been a synchronous or asynchronous physical standby that was fully caught up at the time of conversion.&lt;/p&gt;
&lt;h2 id=&quot;tradeoff-matrix&quot;&gt;Tradeoff Matrix&lt;/h2&gt;
&lt;p&gt;Three distinct upgrade paths. Each is appropriate for a different team posture — the wrong choice for your application topology creates the failure modes in the table below.&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Upgrade path&lt;/th&gt;&lt;th&gt;Sequential apply guarantee&lt;/th&gt;&lt;th&gt;Ops complexity&lt;/th&gt;&lt;th&gt;Standby topology change&lt;/th&gt;&lt;th&gt;When to choose&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Disable parallel apply&lt;/strong&gt; — set &lt;code&gt;max_parallel_apply_workers = 0&lt;/code&gt; after upgrade&lt;/td&gt;&lt;td&gt;Preserved fully&lt;/td&gt;&lt;td&gt;Low&lt;/td&gt;&lt;td&gt;None&lt;/td&gt;&lt;td&gt;Any application with causal ordering assumptions; start here for every upgrade&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Enable parallel apply incrementally&lt;/strong&gt; — 0 → 1 → 2 workers over 96 hours&lt;/td&gt;&lt;td&gt;Relaxed for causally-independent txns only&lt;/td&gt;&lt;td&gt;Medium — requires apply-order audit&lt;/td&gt;&lt;td&gt;None&lt;/td&gt;&lt;td&gt;Event-driven consumers that tolerate out-of-order independent writes; high-write publishers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Convert standby to logical&lt;/strong&gt; — run &lt;code&gt;pg_createsubscriber --all&lt;/code&gt;&lt;/td&gt;&lt;td&gt;N/A — logical replication model&lt;/td&gt;&lt;td&gt;High — topology change, irreversible without rebuild&lt;/td&gt;&lt;td&gt;Physical standby becomes logical subscriber&lt;/td&gt;&lt;td&gt;Teams needing table-level filtering, cross-version replication, or subscriber-level write access&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Choosing parallel apply without an ordering audit is the highest-risk option — it silently changes the consistency model of your subscriber for any application that reads derived state across independent tables.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Application reads stale data from subscriber&lt;/td&gt;&lt;td&gt;Parallel apply changes apply order for independent transactions&lt;/td&gt;&lt;td&gt;Audit application for causal ordering assumptions; add explicit ordering via sequence or timestamp&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;max_wal_senders&lt;/code&gt; exceeded after enabling parallel apply&lt;/td&gt;&lt;td&gt;Multiple subscriptions × parallel workers exceeds the limit&lt;/td&gt;&lt;td&gt;Increase &lt;code&gt;max_wal_senders&lt;/code&gt; before enabling parallel apply&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Conflict log volume overwhelms monitoring&lt;/td&gt;&lt;td&gt;PG18 surfaces previously-silent conflicts at ERROR level&lt;/td&gt;&lt;td&gt;Triage and resolve conflicts; do not suppress — they represent real data divergence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;pg_createsubscriber&lt;/code&gt; fails mid-conversion&lt;/td&gt;&lt;td&gt;Standby still active or primary unreachable during conversion&lt;/td&gt;&lt;td&gt;Stop standby completely before running; verify publisher connectivity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Conflict resolution parameter set to &lt;code&gt;skip&lt;/code&gt; globally&lt;/td&gt;&lt;td&gt;All conflicts silently skipped — subscriber diverges permanently&lt;/td&gt;&lt;td&gt;Set &lt;code&gt;conflict_resolution = &apos;apply_remote&apos;&lt;/code&gt; for insert conflicts; investigate and fix root cause&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: PostgreSQL 18 enables parallel logical apply by default and surfaces replication conflicts at a higher log level — both are improvements that can cause operational surprises if not prepared for before the upgrade.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Set &lt;code&gt;max_parallel_apply_workers_per_subscription = 0&lt;/code&gt; immediately after upgrading logical replication subscribers, validate behavior, then enable incrementally after confirming application ordering assumptions hold.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: After upgrade, replication lag should match or improve versus the pre-upgrade baseline, and &lt;code&gt;pg_stat_subscription.received_lsn&lt;/code&gt; should advance continuously.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Run the five pre-upgrade checks against your production database this week. Record baseline lag values and conflict log counts so you have a comparison point for post-upgrade validation.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;checklist&quot;&gt;Checklist&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Identify replication topology — physical standbys, logical subscribers, or both&lt;/li&gt;
&lt;li&gt;Record baseline replication lag from &lt;code&gt;pg_stat_replication&lt;/code&gt; and &lt;code&gt;pg_stat_subscription&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Check current &lt;code&gt;max_wal_senders&lt;/code&gt; — calculate headroom with parallel apply workers added&lt;/li&gt;
&lt;li&gt;Count existing replication conflicts in current logs — establish baseline before upgrade&lt;/li&gt;
&lt;li&gt;Check for logical subscriptions on PostgreSQL 14 or 15 — plan subscriber upgrade path&lt;/li&gt;
&lt;li&gt;Test upgrade procedure in staging with production data volume — including parallel apply enabled&lt;/li&gt;
&lt;li&gt;After upgrade: immediately set &lt;code&gt;max_parallel_apply_workers_per_subscription = 0&lt;/code&gt; on all subscribers&lt;/li&gt;
&lt;li&gt;Run for 48 hours at zero parallel workers — confirm lag is stable and no new conflicts&lt;/li&gt;
&lt;li&gt;Enable parallel apply with 1 worker — monitor for 48 hours&lt;/li&gt;
&lt;li&gt;Increase to default 2 workers — monitor lag and conflict log for another 48 hours&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>architecture</category><category>checklist</category></item><item><title>From Metrics to Root Cause: Why Database Troubleshooting Needs Evidence Correlation</title><link>https://rajivonai.com/blog/2025-10-07-database-performance-evidence-correlation-not-another-dashboard/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-10-07-database-performance-evidence-correlation-not-another-dashboard/</guid><description>Dashboards expose database symptoms one layer at a time. This architecture turns time-bounded telemetry into ranked, testable root-cause hypotheses without giving an LLM production control.</description><pubDate>Tue, 07 Oct 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A database dashboard can show exactly when latency rose, CPU climbed, and lock waits appeared—and still leave the on-call engineer unable to explain which event caused the others.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Technology and product capabilities in this series are evaluated as of June 30, 2026.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A production request crosses an application service, a connection pool, a database engine, an operating system, storage, and a network before it completes. Each layer emits useful evidence, but most monitoring products present that evidence in separate views. The application dashboard shows P95 latency. The cloud console shows CPU and storage latency. The database exposes waits, sessions, locks, statements, and plans. Deployment and configuration systems record changes somewhere else.&lt;/p&gt;
&lt;p&gt;Database-native tools already demonstrate why no single metric is enough. Amazon RDS defines database load as active sessions that are either running on CPU or waiting, then lets operators decompose that load by dimensions such as waits and SQL. &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.0/en/performance-schema-examples.html&quot;&gt;MySQL Performance Schema&lt;/a&gt; recommends iterative measurement and filtering to improve the signal-to-noise ratio. &lt;a href=&quot;https://www.postgresql.org/docs/18/monitoring.html&quot;&gt;PostgreSQL’s monitoring guidance&lt;/a&gt; explicitly places its cumulative statistics views alongside operating-system tools such as &lt;code&gt;ps&lt;/code&gt;, &lt;code&gt;top&lt;/code&gt;, &lt;code&gt;iostat&lt;/code&gt;, and &lt;code&gt;vmstat&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The evidence exists. The operational gap is assembling it into one defensible explanation while the incident is still happening.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Dashboards optimize for visibility, not causal proof. A CPU chart cannot tell whether CPU rose because a deployment changed a query plan, a batch workload arrived, or lock contention finally cleared and released queued work. A slow-query list cannot tell whether a statement became intrinsically slower or merely waited behind storage, locks, or a saturated connection pool.&lt;/p&gt;
&lt;p&gt;Adding an LLM directly on top of every raw metric and log does not solve this. It creates a different failure mode: too much context, incompatible timestamps, repeated events, sensitive SQL text, and no explicit boundary between observed facts and generated interpretation. A fluent narrative can then look like a root cause even when the required evidence was never collected.&lt;/p&gt;
&lt;p&gt;The core question is therefore: how do we turn fragmented telemetry into a ranked, testable explanation without treating the LLM as the monitoring system or the production operator?&lt;/p&gt;
&lt;h2 id=&quot;the-evidence-correlation-control-plane&quot;&gt;The Evidence-Correlation Control Plane&lt;/h2&gt;
&lt;p&gt;The architecture separates four responsibilities that are often collapsed into one “AI operations” box:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[application symptoms] --&gt; D[deterministic collectors]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B[database waits and workload] --&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C[host cloud and change events] --&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[time-bounded incident evidence pack]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[rule and statistical anomaly detection]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[LLM evidence correlation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[observations hypotheses missing evidence and actions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[DBA verification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[approved remediation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[post-change validation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Collectors establish facts.&lt;/strong&gt; Python, SQL, database APIs, and cloud APIs gather named measurements at known timestamps. They also record provenance: source system, query or API version, collection interval, units, and whether sampling or aggregation occurred.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Anomaly logic establishes what changed.&lt;/strong&gt; Rules and statistical methods compare the incident with an appropriate baseline. They reduce millions of samples to a tractable set of deltas: which waits appeared, which query digests changed, which resources departed from normal range, and which deployments preceded the transition.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The LLM establishes testable hypotheses.&lt;/strong&gt; It correlates the reduced evidence across layers, identifies contradictions, and asks for missing evidence. It does not convert correlation into proof. Its output should always separate:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Observations&lt;/strong&gt; directly supported by supplied evidence.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hypotheses&lt;/strong&gt; that could explain those observations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Missing evidence&lt;/strong&gt; needed to confirm or reject each hypothesis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Actions&lt;/strong&gt; for the next diagnostic or remediation step.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;The DBA establishes authorization.&lt;/strong&gt; A human verifies the evidence chain, decides whether the root cause is proven, evaluates blast radius, and authorizes any production change. Deterministic automation may execute an approved change, but the language model should not hold production write credentials.&lt;/p&gt;
&lt;p&gt;The incident evidence pack is the contract between these stages. It should be sanitized, time-bounded, and organized around four windows:&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Window&lt;/th&gt;&lt;th&gt;Purpose&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Baseline&lt;/td&gt;&lt;td&gt;Establish the normal workload and resource envelope&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Pre-incident&lt;/td&gt;&lt;td&gt;Capture the transition and preceding changes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Incident&lt;/td&gt;&lt;td&gt;Identify concurrent symptoms, waits, workload, and events&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Recovery&lt;/td&gt;&lt;td&gt;Test whether suspected causes disappeared with the symptoms&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;This temporal shape matters. One snapshot can show a blocked session. The four-window comparison can show that the blocker began after a deployment, lock load rose before API latency, storage stayed inside its baseline, and recovery followed a transaction rollback. That sequence does not automatically prove causality, but it makes the next verification step precise.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented behavior of existing database systems supports this layered method.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PerfInsights.Overview.ActiveSessions.html&quot;&gt;AWS Performance Insights&lt;/a&gt; models DB load as average active sessions and decomposes it by wait events and top SQL. That gives an investigator two connected facts: where sessions spend time and which workload contributes to that load. It still does not contain the complete application, deployment, operating-system, or business timeline.&lt;/p&gt;
&lt;p&gt;MySQL’s Performance Schema methodology starts with instrumentation, analyzes collected events, filters areas that have been ruled out, and repeats until the relevant signal becomes clearer. The important pattern is iterative diagnosis: evidence eliminates hypotheses and determines what to collect next.&lt;/p&gt;
&lt;p&gt;PostgreSQL exposes current activity, wait events, I/O, WAL, replication, checkpointer, and database statistics through separate views while advising operators not to neglect host tools. The documented pattern is cross-layer by design. A wait event describes what a backend is waiting for; it does not, by itself, prove why the underlying resource or conflicting transaction became constrained.&lt;/p&gt;
&lt;p&gt;An LLM adds value after these deterministic systems have done their jobs. It can notice that an application deployment, a query-digest shift, a lock-wait increase, and stable storage latency form a stronger transaction-design hypothesis than an infrastructure hypothesis. It can also state that the conclusion remains unproven until the blocking transaction and deployment diff are inspected.&lt;/p&gt;
&lt;p&gt;That last sentence is the quality bar: the model must be as explicit about missing evidence as it is about its preferred explanation.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why the conclusion becomes unsafe&lt;/th&gt;&lt;th&gt;Required control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Misaligned clocks or time zones&lt;/td&gt;&lt;td&gt;Events appear to occur in the wrong order&lt;/td&gt;&lt;td&gt;Normalize timestamps and retain source clock metadata&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Missing baseline&lt;/td&gt;&lt;td&gt;Normal batch activity looks anomalous&lt;/td&gt;&lt;td&gt;Compare equivalent workload and business windows&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Top-N truncation&lt;/td&gt;&lt;td&gt;The actual cause falls outside the retained evidence&lt;/td&gt;&lt;td&gt;Record truncation and allow targeted recollection&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sensitive SQL or log payloads&lt;/td&gt;&lt;td&gt;Diagnostic context leaks customer or credential data&lt;/td&gt;&lt;td&gt;Digest, redact, minimize, encrypt, and restrict retention&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Version-blind recommendations&lt;/td&gt;&lt;td&gt;Valid advice for one engine release is unsafe for another&lt;/td&gt;&lt;td&gt;Include engine, version, deployment model, and feature state&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Confidence without contradiction testing&lt;/td&gt;&lt;td&gt;A plausible correlation is presented as proof&lt;/td&gt;&lt;td&gt;Require evidence for, evidence against, and a falsifying test&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Direct production access&lt;/td&gt;&lt;td&gt;A reasoning error becomes an operational incident&lt;/td&gt;&lt;td&gt;Keep model tools read-only and route changes through approval&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-the-llm-cannot-do&quot;&gt;What the LLM Cannot Do&lt;/h2&gt;
&lt;p&gt;Explicit operational boundaries govern the LLM:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Raw Customer Data:&lt;/strong&gt; The LLM must not receive raw result sets, bind or literal parameter values, unrestricted query text, credentials, connection strings, or any column content that can carry PII. It receives shapes, counts, timings, and normalized identifiers only.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Live Database Access:&lt;/strong&gt; The LLM has no network path to any database, host, or cloud control plane. It reasons exclusively over a detached, sanitized incident evidence pack captured by a deterministic collector.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Causal Conclusion Without Contradicting Evidence:&lt;/strong&gt; Every hypothesis must cite both the telemetry that supports it and the telemetry that would falsify it. A ranked hypothesis with no disconfirming test attached is an assertion, not a diagnosis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Silent Gap-Filling:&lt;/strong&gt; Where the evidence pack lacks a required signal, the LLM must name it explicitly under missing evidence rather than inferring a plausible value. “Not collected” and “collected and normal” are different findings and must never be merged.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No Change Without a Human Gate:&lt;/strong&gt; Production changes require human authorization, a stated validation signal, and a defined rollback condition agreed before the change is applied.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Database telemetry is fragmented by layer, so dashboards reveal simultaneous symptoms without establishing their order, relationship, or cause.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build a time-bounded incident evidence pack from deterministic collectors, reduce it with explicit anomaly logic, and ask the LLM for observations, hypotheses, missing evidence, and next actions as separate outputs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Test the design against a resolved incident. The system should reconstruct the transition from baseline through recovery, identify the evidence that actually proved the root cause, and mark unsupported alternatives as rejected or unresolved.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Choose one recurring database incident and define its evidence contract before writing an LLM prompt: timestamps, application symptoms, host and cloud metrics, database waits, workload deltas, logs, change events, redaction rules, and the human approval boundary.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The next article builds that contract: the sanitized database incident evidence pack an LLM can reason over without receiving an uncontrolled production dump.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PerfInsights.Overview.ActiveSessions.html&quot;&gt;Amazon RDS — Database load, active sessions, waits, and top SQL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.0/en/performance-schema-examples.html&quot;&gt;MySQL 8.0 — Using the Performance Schema to Diagnose Problems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.postgresql.org/docs/18/monitoring.html&quot;&gt;PostgreSQL 18 — Monitoring Database Activity&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>architecture</category><category>failures</category></item><item><title>Using Cassandra Full Query Logs and LLMs for Performance Troubleshooting</title><link>https://rajivonai.com/blog/2025-10-02-cassandra-full-query-logs-llm-troubleshooting/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-10-02-cassandra-full-query-logs-llm-troubleshooting/</guid><description>How to combine Apache Cassandra&apos;s Full Query Logging (FQL) with a safe, sanitized LLM diagnostic pipeline to triage complex latency regressions.</description><pubDate>Thu, 02 Oct 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Diagnosing a completely broken database is relatively straightforward. Diagnosing a subtle, transient performance regression buried within 100,000 queries per second is one of the hardest challenges in distributed systems engineering.&lt;/p&gt;
&lt;p&gt;When your metrics dashboards show p99 latency spikes, but &lt;code&gt;nodetool tpstats&lt;/code&gt; looks healthy and OS utilization is nominal, the problem is almost always the application’s query shapes.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Between 2023 and 2025, the 90-node Cassandra 4.1 cluster experienced intermittent p99 latency spikes that specifically impacted critical business workflows.&lt;/p&gt;
&lt;p&gt;The symptoms were classic but infuriating: transient spikes where queries seemed to bypass indexes or trigger unbounded partition scans. However, because these sub-optimal queries were hidden within massive microservice traffic volumes, they were impossible to catch using standard sampling. On-call engineers were overwhelmed by terabytes of unstructured &lt;code&gt;system.log&lt;/code&gt; files and vague metric correlations.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra doesn’t have a “slow query log” that is easy to parse under heavy load without degrading performance.&lt;/p&gt;
&lt;p&gt;To see exactly what the application is sending, you need to enable &lt;strong&gt;Full Query Logging (FQL)&lt;/strong&gt;. FQL uses Chronicle Queue to write a highly optimized binary log of every CQL request. However, generating this data creates two massive problems:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Volume:&lt;/strong&gt; At 100k QPS, FQL generates gigabytes of binary data per minute.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Analysis:&lt;/strong&gt; You have to dump the binary to text using &lt;code&gt;fqltool&lt;/code&gt;, then manually search through millions of queries to find the culprit (e.g., an &lt;code&gt;IN&lt;/code&gt; query scanning 50 partition keys).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We realized that Large Language Models (LLMs) are uniquely suited to synthesize this type of unstructured text anomaly. However, feeding raw database logs to a cloud LLM is a catastrophic security violation. How do you leverage AI reasoning without leaking customer PII or granting the AI dangerous operational access?&lt;/p&gt;
&lt;h2 id=&quot;the-safe-diagnostic-ai-pipeline&quot;&gt;The Safe Diagnostic AI Pipeline&lt;/h2&gt;
&lt;p&gt;The pattern builds an automated Python diagnostic agent that acts as a secure airgap between the Cassandra cluster and the LLM.&lt;/p&gt;
&lt;p&gt;The pipeline strictly enforces deterministic data collection, rigorous PII redaction, and a mandatory Database Administrator (DBA) verification gate.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;Production Boundary (Airgapped)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Cass(Cassandra Cluster) --&gt;|fqltool dump| FQL[Full Query Logs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Cass --&gt;|nodetool| Stats[JMX Metrics]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        FQL --&gt; Agent(Python Diagnostic Agent)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Stats --&gt; Agent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Agent --&gt; Redact[AST/Regex Redaction Engine — Strip PII &amp;#x26; Literals]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Redact --&gt; Bundle[Structured JSON/Markdown — Incident Bundle]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Bundle --&gt; LLM(LLM Reasoning Engine)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;Human Validation&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        LLM --&gt;|Hypotheses &amp;#x26; Validation Commands| DBA{DBA Verification Gate}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        DBA --&gt;|Reject| Drop[Discard]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        DBA --&gt;|Approve| Action[Execute Remediation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-fql-capture&quot;&gt;1. FQL Capture&lt;/h3&gt;
&lt;p&gt;When an alert fired, the Python agent ran &lt;code&gt;nodetool enablefullquerylog&lt;/code&gt; for exactly 60 seconds, capturing a massive representative sample, and immediately disabled it to reclaim disk I/O. FQL is a low-overhead workload capture mechanism, but its operational impact must always be measured under your specific production workload.&lt;/p&gt;
&lt;h3 id=&quot;2-the-redaction-engine&quot;&gt;2. The Redaction Engine&lt;/h3&gt;
&lt;p&gt;Before leaving the production boundary, the Python agent parsed the CQL statements and stripped all literals, replacing them with &lt;code&gt;?&lt;/code&gt;.
&lt;code&gt;SELECT * FROM users WHERE email = &apos;bob@example.com&apos;;&lt;/code&gt; became &lt;code&gt;SELECT * FROM users WHERE email = ?;&lt;/code&gt;. This guaranteed zero PII exposure.&lt;/p&gt;
&lt;h3 id=&quot;3-llm-hypothesis-generation&quot;&gt;3. LLM Hypothesis Generation&lt;/h3&gt;
&lt;p&gt;The sanitized FQL dump, along with a snapshot of &lt;code&gt;nodetool tpstats&lt;/code&gt; and &lt;code&gt;nodetool tablestats&lt;/code&gt;, was packaged into a structured prompt and sent to the LLM. The prompt explicitly instructed the LLM to act as a Senior Cassandra DBA, identify anomalous query shapes (like massive &lt;code&gt;IN&lt;/code&gt; clauses or unbounded scans), and provide a ranked list of root-cause hypotheses alongside the deterministic commands required to verify them.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;During an incident, the output from the LLM pipeline looked like this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Hypothesis 1 (80% Confidence):&lt;/strong&gt; Application is executing &lt;code&gt;SELECT&lt;/code&gt; statements with massive &lt;code&gt;IN&lt;/code&gt; clauses on the partition key.
&lt;strong&gt;Evidence:&lt;/strong&gt; FQL sample shows &lt;code&gt;SELECT * FROM orders WHERE user_id IN (?, ?, ..., ?)&lt;/code&gt; with up to 200 parameters. This forces the coordinator to fan out reads across up to 200 distinct partitions in a single request, causing the observed coordinator latency spikes.
&lt;strong&gt;Verification Command:&lt;/strong&gt; &lt;code&gt;grep -o &quot;IN (&quot; sanitized_fql.txt | wc -l&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;By structuring the incident bundle, the LLM could instantly synthesize what would take an engineer hours of grepping. The DBA in the loop simply reviewed the LLM’s logic, ran the validation command, and pinpointed the offending microservice.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Piping Raw Logs to LLM&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Massive security violation. You will leak PII, passwords, and sensitive data to a third-party model provider.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Build a strict, deterministic redaction engine using AST parsing or regex to strip all literals before the LLM step.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;LLM Auto-Execution&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Allowing an LLM to automatically execute remediation scripts (like dropping indexes or killing connections) based on its own hypotheses will cause an outage.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The DBA Verification Gate is absolute. The AI suggests; the human verifies and types the command.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Leaving FQL On&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Forgetting to run &lt;code&gt;nodetool disablefullquerylog&lt;/code&gt; will eventually fill the OS disk, crashing the node.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;The Python agent must wrap the FQL capture in a strict &lt;code&gt;try/finally&lt;/code&gt; block that guarantees disabling the log.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-and-local-slms&quot;&gt;Modern Cassandra Context: Cassandra 5.0 and Local SLMs&lt;/h3&gt;
&lt;p&gt;Cassandra 5.0 introduces expanded virtual table diagnostics, making it easier to query running tasks via CQL rather than JMX. Furthermore, the AI landscape has shifted. Instead of sending redacted logs to frontier models, engineering teams are increasingly deploying open-weight Small Language Models (SLMs) locally within their secure VPC boundaries. This entirely eliminates the PII exfiltration risk, allowing for even deeper automated log analysis without redacting query contexts.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Transient query regressions are impossible to debug with standard metrics, but raw query logs are too massive to parse manually and too sensitive for cloud LLMs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Capture 60-second FQL samples, deterministically redact all CQL literals, and use an LLM to synthesize the sanitized query shapes and metrics into actionable hypotheses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; This pipeline turns hours of manual grepping through FQL dumps into a short, reviewable list of ranked hypotheses, while strictly enforcing the DBA verification boundary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Test FQL locally. Run &lt;code&gt;nodetool enablefullquerylog --path /tmp/fql&lt;/code&gt;, run some queries, stop it, and dump the binary with &lt;code&gt;fqltool dump /tmp/fql&lt;/code&gt;. See the volume for yourself.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>ai-engineering</category><category>failures</category></item><item><title>Top GitHub Breakouts: August 2025 — Part II</title><link>https://rajivonai.com/blog/2025-09-27-github-stars-aug-2025/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-09-27-github-stars-aug-2025/</guid><description>The highest-starred new open-source projects in August 2025 where AI takes over cloud operations, infrastructure provisioning, and production Postgres coding.</description><pubDate>Sat, 27 Sep 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The last generation of AI tooling told engineers what was wrong. August 2025’s second wave goes further — cloud agents that provision infrastructure from a description, AI that translates natural language into AWS operations, and an MCP server that teaches coding agents what production Postgres actually looks like. The gap being closed is not information; it is execution.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;AI-assisted operations have followed a familiar arc: first came dashboards, then query-answering chatbots, then recommendation engines. Each layer added latency between the diagnosis and the fix. The bottleneck was always the same: a human in the loop who had to translate the AI’s output into a real action.&lt;/p&gt;
&lt;p&gt;The tools gaining traction in August 2025 skip the translation step. They connect AI models directly to execution paths — a cloud CLI that generates and applies infrastructure plans, an agent that owns the AWS state machine, and a Postgres MCP server that gives coding agents the context they need to generate correct production SQL without a DBA in the loop.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Domain&lt;/th&gt;&lt;th&gt;Manual bottleneck&lt;/th&gt;&lt;th&gt;What it costs&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;System design&lt;/td&gt;&lt;td&gt;Translating a verbal infrastructure description into provider-specific CLI commands&lt;/td&gt;&lt;td&gt;30–60 minutes of lookup, flag-checking, and dry-runs per change&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Platform engineering&lt;/td&gt;&lt;td&gt;Context-switching between AWS console, Terraform state, and incident context during an outage&lt;/td&gt;&lt;td&gt;Slow incident response; cognitive overhead on the most critical path&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Platform engineering&lt;/td&gt;&lt;td&gt;Writing Terraform or CloudFormation for each new AWS resource type added to a service&lt;/td&gt;&lt;td&gt;Weeks of IaC work before a new service reaches production&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Databases&lt;/td&gt;&lt;td&gt;Providing AI coding agents with enough Postgres context to generate production-safe SQL&lt;/td&gt;&lt;td&gt;Agents that generate syntactically valid but operationally wrong queries (missing indexes, wrong isolation levels, no error handling)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Can AI tooling take over the execution step without requiring engineers to review every generated action in a separate review cycle?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Human describes intent in plain language] --&gt; B[Cloud infrastructure request]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; C[AWS provisioning request]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; D[Production Postgres code request]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[bgdnvk — Clanker CLI]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; F[VersusControl — AI Infrastructure Agent]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; G[timescale — Tiger CLI and MCP]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; H[Inspect and generate infra plans]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; I[Natural language to AWS operations]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; J[Context-aware Postgres code generation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;bgdnvkclanker--cloud-infrastructure-questions-and-plan-generation-from-the-terminal&quot;&gt;bgdnvk/clanker — cloud infrastructure questions and plan generation from the terminal&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: Engineers asking “what is deployed in this environment?” have to query multiple AWS/GCP/Cloudflare APIs manually; generating a change plan means writing CLI commands or Terraform from scratch.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How AI replaces that task&lt;/strong&gt;: The README describes Clanker as the CLI powering “the first AI DevOps IDE for agents and humans.” It supports two flows: an inspect flow (“ask questions about your infra”) and a maker/deploy flow (“generate or apply infrastructure and deploy plans”). It connects to your existing AWS CLI profiles — not raw keys — and uses OpenAI, Gemini, or Cohere as the reasoning backend. The ask-questions flow queries live infrastructure state; the maker flow generates plans the engineer can review before applying.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The workflow&lt;/strong&gt;: Install via Homebrew (&lt;code&gt;brew tap clankercloud/tap &amp;#x26;&amp;#x26; brew install clanker&lt;/code&gt;) or from source. Run &lt;code&gt;clanker config init&lt;/code&gt; to wire in your cloud credentials and AI provider. Then: &lt;code&gt;clanker ask &quot;what EC2 instances are running in production?&quot;&lt;/code&gt; for inspection, or trigger the maker flow to generate a deployment plan from a description. The README notes AWS CLI v2 is required; v1 breaks the &lt;code&gt;--no-cli-pager&lt;/code&gt; flag.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: Clanker is in active early development — the README links to docs.clankercloud.ai for full feature coverage, which signals the CLI surface is still shifting. The maker/deploy flow generates plans for review, not autonomous applies; teams expecting zero-touch automation will still have an approval step.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;versuscontrolai-infrastructure-agent--natural-language-to-aws-operations-with-state-tracking&quot;&gt;VersusControl/ai-infrastructure-agent — natural language to AWS operations with state tracking&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: Provisioning an EC2 instance with a matching security group requires knowing the specific CLI flags, correct CIDR notation, and order-of-operations across multiple &lt;code&gt;aws&lt;/code&gt; subcommands.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How AI replaces that task&lt;/strong&gt;: The README describes an agent that translates a natural language request like “Create an EC2 instance for hosting an Apache Server with a dedicated security group that allows inbound HTTP and SSH traffic” into a sequenced set of AWS API calls, while maintaining a Terraform-like state file to track what it has provisioned. It supports OpenAI GPT, Google Gemini, Anthropic Claude, AWS Bedrock Nova, and Ollama as the reasoning layer, and includes a web dashboard with built-in conflict detection and dry-run mode.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The workflow&lt;/strong&gt;: The agent maintains state and performs conflict detection before executing, which means it can identify when a requested resource would overlap with existing infrastructure. Current resource support per the README: VPC, EC2, security groups, Autoscaling Groups, and ALB.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: The README explicitly labels this “a proof-of-concept implementation” that is “not intended for production use.” This is worth taking seriously — the state management approach is described as “Terraform-like” but the codebase is in active development. The honest use case right now is evaluation and learning, not replacing Terraform in a production pipeline.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;timescaletiger-cli--mcp-server-that-teaches-ai-coding-agents-production-postgres&quot;&gt;timescale/tiger-cli — MCP server that teaches AI coding agents production Postgres&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: AI coding agents generating SQL or application database code lack the context to know whether their output is operationally safe — correct index usage, right transaction isolation level, appropriate use of connection pooling, error handling patterns for production Postgres.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How AI replaces that task&lt;/strong&gt;: Tiger CLI is the interface for Timescale’s managed Postgres service (Tiger Cloud), and the README describes a built-in MCP server (&lt;code&gt;tiger mcp install&lt;/code&gt;) designed to give AI assistants the production Postgres context they need. The project description calls this “context engineering” — the MCP server surfaces live schema information, service configuration, and connection parameters so coding agents can generate SQL that matches the actual production environment rather than a generic Postgres assumption.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The workflow&lt;/strong&gt;: Install via &lt;code&gt;curl -fsSL https://cli.tigerdata.com | sh&lt;/code&gt;, authenticate with &lt;code&gt;tiger auth login&lt;/code&gt;, and run &lt;code&gt;tiger mcp install&lt;/code&gt; to register the MCP server with your AI assistant. From that point, the assistant has access to service metadata, connection strings, and schema context. The CLI also handles full service lifecycle: &lt;code&gt;tiger service create&lt;/code&gt;, &lt;code&gt;tiger db connect&lt;/code&gt;, &lt;code&gt;tiger service logs&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: Tiger CLI is tightly coupled to Tiger Cloud — the MCP server’s value comes from live access to a managed Timescale instance. Teams running self-hosted Postgres won’t get the same context richness without a separate MCP layer pointed at their own cluster.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern is to tightly couple AI execution with local identity and operational state. For example, Timescale built Tiger CLI’s MCP server to surface live database engine versions and connection pool configurations directly to agents, a public decision rooted in how PostgreSQL’s behavior dictates query generation constraints. Rather than generic code, agents need the live schema to avoid missing indexes or incorrect isolation levels. Similarly, tools like Clanker rely on the user’s existing AWS CLI profiles rather than new API keys, honoring existing IAM boundaries. The AI Infrastructure Agent acknowledges the risk of unsanctioned modifications by operating with a state file, much like Terraform, proving that even natural-language tooling must adopt established distributed systems reconciliation patterns to safely modify cloud infrastructure.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Clanker maker flow generates incorrect plan for multi-region resources&lt;/td&gt;&lt;td&gt;AI model lacks region-specific context in the prompt&lt;/td&gt;&lt;td&gt;Add region and account context explicitly in the request; review plans before applying&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AI Infrastructure Agent state drifts from actual AWS state&lt;/td&gt;&lt;td&gt;Manual changes outside the agent between runs&lt;/td&gt;&lt;td&gt;Treat the agent’s state file as the source of truth; avoid manual console changes on agent-managed resources&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tiger CLI MCP loses context after schema changes&lt;/td&gt;&lt;td&gt;DDL applied outside the CLI session&lt;/td&gt;&lt;td&gt;Re-authenticate and refresh service metadata; run &lt;code&gt;tiger db connect&lt;/code&gt; to verify current schema&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Clanker requires AWS CLI v2 but v1 is installed&lt;/td&gt;&lt;td&gt;Legacy tooling in CI/CD environments&lt;/td&gt;&lt;td&gt;Pin &lt;code&gt;awscli&gt;=2.0&lt;/code&gt; in environment setup; test with &lt;code&gt;aws --version&lt;/code&gt; before wiring Clanker into a pipeline&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Engineering teams are still hand-writing cloud provisioning commands and generating SQL code without production database context — execution steps that AI can handle directly if given the right connections.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Clanker CLI for cloud infrastructure inspection and plan generation; AI Infrastructure Agent for natural-language-to-AWS provisioning (as an evaluation tool); Tiger CLI’s MCP server for grounding coding agents in live production Postgres context.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: The clearest signal from Tiger CLI is asking your AI coding assistant to write a query against your actual production schema — after &lt;code&gt;tiger mcp install&lt;/code&gt; — and comparing the output to what the same assistant produces without that context. The difference in index awareness and schema accuracy is the productivity delta.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Run &lt;code&gt;tiger mcp install&lt;/code&gt; and connect it to a Tiger Cloud service (or evaluate against the free tier). Ask your coding assistant to generate a query you know is tricky — a multi-table join with a specific filter selectivity. Compare the output with and without MCP context.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>ai-engineering</category><category>cloud</category><category>databases</category></item><item><title>PostgreSQL 18: Features DB Engineers Should Watch</title><link>https://rajivonai.com/blog/2025-09-25-postgresql-18-features-db-engineers-should-watch/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-09-25-postgresql-18-features-db-engineers-should-watch/</guid><description>PostgreSQL 18 introduces fundamental changes to the storage engine — asynchronous I/O, parallel logical apply, and improved conflict visibility are the changes operators need to understand before upgrading.</description><pubDate>Thu, 25 Sep 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;PostgreSQL 18 shipped in September 2025 and delivers the most fundamental change to PostgreSQL’s storage engine in its history: asynchronous I/O.&lt;/strong&gt; This post was written in January 2025 based on accepted CommitFest patches and has been validated against the final PG18 release. All four features described below shipped as documented.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL has used synchronous I/O since its inception. Every read and write to storage blocks the backend process until the kernel returns. This is simple, predictable, and correct — but it means every disk-bound query is a sequence of blocking kernel calls with no opportunity for the backend to do useful work while waiting for I/O.&lt;/p&gt;
&lt;p&gt;Modern storage — NVMe SSDs, io_uring-capable kernels, cloud block storage with significant parallelism — is well-suited to concurrent I/O. PostgreSQL could not take advantage of this without a fundamental change to how it submits and waits for I/O requests.&lt;/p&gt;
&lt;p&gt;PG18 introduces asynchronous I/O as an optional mode. Alongside this, several replication and operational improvements address long-standing gaps. Operators who plan upgrades should understand these changes now, because some of them alter default behavior.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The synchronous I/O model has a measurable impact on workloads that require high disk throughput: parallel queries hitting large tables, checkpoint writers under heavy write load, and logical replication subscribers applying changes from high-write publishers. Each backend process can only have one I/O operation in flight at a time.&lt;/p&gt;
&lt;p&gt;The operational impact shows up as I/O utilization that looks low on aggregate metrics (storage is not at 100% IOPS) while query latency is high. The storage device has capacity, but PostgreSQL is not submitting enough concurrent requests to use it. This is the structural problem that asynchronous I/O in PG18 addresses.&lt;/p&gt;
&lt;p&gt;The risk for operators: asynchronous I/O changes how PostgreSQL interacts with the kernel, which changes how it behaves on specific OS and storage configurations. Teams that upgrade to PG18 on non-standard storage setups (network block storage, certain cloud filesystems, shared storage) may observe different I/O patterns than they expect. How should engineering teams prepare their infrastructure for PostgreSQL 18’s new I/O and replication models?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[&quot;Client Query&quot;] --&gt; B[&quot;PG18 Backend Process&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C{&quot;io_method GUC&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|&quot;sync&quot;| D[&quot;Blocking Kernel Calls&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|&quot;worker&quot;| E[&quot;Background Worker Threads&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|&quot;io_uring&quot;| F[&quot;Linux io_uring Non-blocking AIO&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; G[&quot;Storage Engine&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;1. Asynchronous I/O (AIO)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PG18 introduces a framework for non-blocking I/O. On Linux with kernel 5.1 or newer, PostgreSQL can use &lt;code&gt;io_uring&lt;/code&gt; as the AIO backend. On other platforms, it falls back to a worker-thread-based AIO implementation.&lt;/p&gt;
&lt;p&gt;The GUC &lt;code&gt;io_method&lt;/code&gt; controls the behavior:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;sync&lt;/code&gt; — traditional synchronous I/O (always available, backward-compatible)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;worker&lt;/code&gt; — AIO using background worker threads (available on all platforms)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;io_uring&lt;/code&gt; — AIO using Linux io_uring (Linux 5.1 and newer; requires PostgreSQL built with &lt;code&gt;--with-liburing&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The expected benefit is measurable on parallel sequential scans and checkpointing — workloads where multiple I/O operations can be queued concurrently.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Parallel streaming apply for logical replication&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PG17 improved sequence replication. PG18 extends parallel apply by changing the default &lt;code&gt;streaming&lt;/code&gt; option for &lt;code&gt;CREATE SUBSCRIPTION&lt;/code&gt; from &lt;code&gt;off&lt;/code&gt; to &lt;code&gt;parallel&lt;/code&gt;. In PG16 and PG17, parallel streaming required explicit configuration. In PG18, new subscriptions stream large transactions in parallel by default.&lt;/p&gt;
&lt;p&gt;The operational consequence: subscribers on PG18 will consume more CPU and hold more locks during apply than a comparable PG17 subscriber would. Conflict handling logic that assumes single-threaded apply ordering may behave differently with parallel apply enabled. The &lt;code&gt;pg_stat_subscription_stats&lt;/code&gt; view provides per-subscription apply metrics including conflict counts, which is the right place to observe this.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. &lt;code&gt;pg_createsubscriber --all&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PG18 adds &lt;code&gt;--all&lt;/code&gt; to &lt;code&gt;pg_createsubscriber&lt;/code&gt;, the tool for converting a physical standby into a logical replication subscriber. Before PG18, this required specifying individual databases or tables. With &lt;code&gt;--all&lt;/code&gt;, the tool sets up logical replication for all databases on the standby in one command.&lt;/p&gt;
&lt;p&gt;This simplifies the zero-downtime major version upgrade workflow significantly. The documented use case: take a physical streaming replica, convert it to a logical subscriber of the primary, let it catch up as a logical subscriber, then promote. The &lt;code&gt;--all&lt;/code&gt; flag reduces the setup steps for multi-database clusters.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. Improved conflict visibility in logical replication&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Logical replication conflict handling in PG17 and earlier emitted minimal log information when a conflict occurred (a duplicate key or update to a row that was deleted on the subscriber). PG18 adds structured conflict detail to the log messages and extends &lt;code&gt;pg_stat_subscription_stats&lt;/code&gt; with conflict type counts.&lt;/p&gt;
&lt;p&gt;The operational impact: conflict-based apply failures are now diagnosable from log output without attaching debuggers or running manual queries. The new log format changes what conflict monitoring tools expect to parse. Log aggregation pipelines that alert on replication conflict patterns need to update their regex or structured log parsers before upgrading to PG18.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;PostgreSQL 18’s AIO framework shipped with &lt;code&gt;io_uring&lt;/code&gt; requiring both Linux kernel 5.1 or newer and a PostgreSQL build with &lt;code&gt;--with-liburing&lt;/code&gt;. PostgreSQL’s behavior when falling back is well-defined: if the environment restricts &lt;code&gt;io_uring&lt;/code&gt; at the container or hypervisor level — which is common in some managed cloud offerings — the system gracefully falls back to traditional modes. Database operators must test the specific &lt;code&gt;io_method&lt;/code&gt; setting against their target storage environment.&lt;/p&gt;
&lt;p&gt;For logical replication, PostgreSQL’s behavior with &lt;code&gt;max_parallel_apply_workers_per_subscription&lt;/code&gt; is documented to change ordering guarantees. Within a single transaction, order is preserved, but across transactions, parallel workers may apply changes out of logical commit order. Applications that depend on subscribers seeing changes in strict commit order must account for this behavior change.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;AIO on unsupported storage or kernel&lt;/td&gt;&lt;td&gt;io_uring mode falls back to worker mode, and expected I/O gains do not materialize&lt;/td&gt;&lt;td&gt;io_uring requires kernel 5.1 or newer and is blocked in some cloud managed environments&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Parallel apply with existing conflict handling&lt;/td&gt;&lt;td&gt;Apply errors or stalled replication on rows processed out of expected order&lt;/td&gt;&lt;td&gt;Multi-worker apply does not guarantee cross-transaction ordering, so single-threaded conflict logic may not handle this correctly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Log parsing for replication conflict alerts&lt;/td&gt;&lt;td&gt;Alert rules that matched old conflict log format produce no alerts or false positives&lt;/td&gt;&lt;td&gt;PG18 structured conflict log messages use a different format than PG17 unstructured messages&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: PG18’s AIO and default parallel apply change I/O behavior and replication ordering assumptions — upgrading without testing on representative workloads risks performance regressions and silent replication issues.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Test PG18 with &lt;code&gt;io_method = worker&lt;/code&gt; first to establish broad platform compatibility, validate logical replication behavior with parallel apply enabled, and update conflict log parsing before production adoption.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: On a PG18 test instance, run a parallel sequential scan against a large table with &lt;code&gt;io_method = worker&lt;/code&gt; and compare elapsed time against the same query on PG17 — the expected result is measurably faster for scans larger than shared buffers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: If you run logical replication subscribers today, review &lt;code&gt;pg_stat_subscription_stats&lt;/code&gt; on PG17 and establish a conflict count baseline — this is the metric to validate stays within expected range on PG18 after enabling parallel apply.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>checklist</category></item><item><title>Multi-DC Cassandra: Consistency, Replication, and Surviving a Datacenter Failure</title><link>https://rajivonai.com/blog/2025-09-20-multi-dc-cassandra-consistency-datacenter-failure/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-09-20-multi-dc-cassandra-consistency-datacenter-failure/</guid><description>How to survive an AWS regional outage by mastering Apache Cassandra&apos;s multi-datacenter replication, LOCAL_QUORUM consistency, and Hinted Handoff.</description><pubDate>Sat, 20 Sep 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Deploying a database across multiple cloud regions is the ultimate test of distributed systems engineering. Apache Cassandra is one of the few databases mathematically proven to survive the total loss of a datacenter without taking down the application.&lt;/p&gt;
&lt;p&gt;However, if your client drivers and consistency levels are misconfigured, a regional outage in AWS &lt;code&gt;us-east-1&lt;/code&gt; will instantly cascade and take down your healthy &lt;code&gt;us-west-2&lt;/code&gt; environment.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In 2024, the 54-node Cassandra 4.1 cluster was deployed active-active across two AWS Regions: 27 nodes in &lt;code&gt;us-east-1&lt;/code&gt; and 27 nodes in &lt;code&gt;us-west-2&lt;/code&gt;. The dataset was approximately 50 TB, fully replicated to both coasts.&lt;/p&gt;
&lt;p&gt;When a major AWS regional network partition isolated &lt;code&gt;us-east-1&lt;/code&gt;, the applications in &lt;code&gt;us-west-2&lt;/code&gt; should have continued operating normally. Instead, HTTP 500 errors spiked across the West Coast. Application driver threads blocked indefinitely on cross-region coordinator requests, VPC peering bandwidth saturated, and the entire platform ground to a halt.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure was not in Cassandra’s architecture; it was in the application’s consistency configuration.&lt;/p&gt;
&lt;p&gt;When you create a keyspace for multi-region replication, you use &lt;code&gt;NetworkTopologyStrategy&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; KEYSPACE production &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; replication &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;class&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;NetworkTopologyStrategy&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;us-east-1&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;us-west-2&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This guarantees three copies of the data exist on the East coast, and three copies exist on the West coast.&lt;/p&gt;
&lt;p&gt;However, the application was performing writes using the &lt;code&gt;QUORUM&lt;/code&gt; consistency level. In a 6-replica cluster, &lt;code&gt;QUORUM&lt;/code&gt; requires acknowledgements from 4 nodes (6 / 2 + 1 = 4).
This meant every single write executed in &lt;code&gt;us-west-2&lt;/code&gt; required a synchronous network trip across the country to wait for an acknowledgement from at least one node in &lt;code&gt;us-east-1&lt;/code&gt;. When &lt;code&gt;us-east-1&lt;/code&gt; went offline, &lt;code&gt;us-west-2&lt;/code&gt; could only gather 3 local acknowledgements. The writes failed. The database protected consistency at the cost of total system availability.&lt;/p&gt;
&lt;p&gt;Furthermore, the DataStax Java driver was not configured with local datacenter affinity. The load balancer was sending West Coast application queries directly to East Coast Cassandra coordinators, causing massive latency overhead even when the network was healthy.&lt;/p&gt;
&lt;h2 id=&quot;the-active-active-multi-region-architecture&quot;&gt;The Active-Active Multi-Region Architecture&lt;/h2&gt;
&lt;p&gt;To survive a regional failure, you must decouple the geographic regions using asynchronous replication boundaries.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[West Coast Application] --&gt;|LOCAL_QUORUM| Coord(West Coast Coordinator)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;AWS us-west-2 (Local DC)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Coord --&gt;|Write| N1[Node 1]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Coord --&gt;|Write| N2[Node 2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Coord --&gt;|Write| N3[Node 3]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord -.-&gt;|Async Replication| EastCoord(East Coast Coordinator)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;AWS us-east-1 (Remote DC)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        EastCoord --&gt;|Write| N4[Node 4]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        EastCoord --&gt;|Write| N5[Node 5]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    %% Hinted Handoff&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    EastCoord -.-&gt;|Network Partition| Failed[Nodes Unreachable]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord -.-&gt;|Store Hint| Hint[(Hinted Handoff — Local Disk)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-enforcing-local_quorum&quot;&gt;1. Enforcing LOCAL_QUORUM&lt;/h3&gt;
&lt;p&gt;The pattern standardizes all application profiles to use &lt;code&gt;LOCAL_QUORUM&lt;/code&gt; for both reads and writes.
&lt;code&gt;LOCAL_QUORUM&lt;/code&gt; fundamentally changes the physics of the query. It keeps the acknowledgement requirement strictly within the coordinator’s local datacenter. The West Coast coordinator requires 2 acknowledgements out of the 3 West Coast replicas to return success to the application. It still asynchronously replicates the mutation to the East Coast, but it &lt;strong&gt;does not wait&lt;/strong&gt; for the East Coast to acknowledge. If the East Coast goes offline, the West Coast application never notices.&lt;/p&gt;
&lt;h3 id=&quot;2-driver-datacenter-affinity&quot;&gt;2. Driver Datacenter Affinity&lt;/h3&gt;
&lt;p&gt;We reconfigured the DataStax Java Driver’s local-datacenter affinity (&lt;code&gt;basic.load-balancing-policy.local-datacenter&lt;/code&gt; in the 4.x driver — the successor to the legacy &lt;code&gt;DCAwareRoundRobinPolicy&lt;/code&gt; from the 2.x/3.x driver series). This forced West Coast microservices to only open connections and coordinate queries with West Coast Cassandra nodes, eliminating cross-country VPC traffic from the hot path.&lt;/p&gt;
&lt;h3 id=&quot;3-tuning-hinted-handoff&quot;&gt;3. Tuning Hinted Handoff&lt;/h3&gt;
&lt;p&gt;When &lt;code&gt;us-east-1&lt;/code&gt; goes offline, the &lt;code&gt;us-west-2&lt;/code&gt; nodes store the missed writes locally on disk as “hints”. When the network recovers, they stream these hints to the East Coast to heal the data. The standard approach tunes &lt;code&gt;max_hint_window_in_ms&lt;/code&gt; to 3 hours, ensuring the cluster could safely buffer a regional disconnect without dropping mutations.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;This architecture is proven by adopting Chaos Engineering. The documented pattern uses AWS Fault Injection Simulator to intentionally drop all packets across the VPC Peering link, effectively simulating a full regional partition.&lt;/p&gt;
&lt;p&gt;Because the applications were using &lt;code&gt;LOCAL_QUORUM&lt;/code&gt; and local-datacenter driver affinity, p99 latency in &lt;code&gt;us-west-2&lt;/code&gt; remained perfectly flat at 5ms. The nodes buffered the hints locally. When we restored the peering link 30 minutes later, &lt;code&gt;nodetool statushandoff&lt;/code&gt; showed the hints streaming across the country, silently reconciling the data.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Using &lt;code&gt;EACH_QUORUM&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Forces synchronous writes to &lt;em&gt;both&lt;/em&gt; datacenters. Ensures perfect global consistency but makes the cluster vulnerable to a single region failure.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Never use &lt;code&gt;EACH_QUORUM&lt;/code&gt; unless regulatory compliance mandates synchronous multi-region writes.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Disabling Hints&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;If you disable hinted handoff to save disk space, a 10-minute network blip will require a multi-terabyte cross-region repair.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep hints enabled and monitor the &lt;code&gt;HintsDispatchExecutor&lt;/code&gt; in &lt;code&gt;nodetool tpstats&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Massive Regional Outages&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;If a region is down for 24 hours, hint buffers will overflow and drop writes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run a full cross-DC &lt;code&gt;nodetool repair&lt;/code&gt; (via Cassandra Reaper) immediately after the region recovers.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-multi-region-database-cost-economics&quot;&gt;Modern Cassandra Context: Multi-Region Database Cost Economics&lt;/h3&gt;
&lt;p&gt;Deploying Active-Active Cassandra across AWS regions incurs significant Inter-Region VPC Peering bandwidth costs due to constant asynchronous replication. However, compared to managed services like Aurora Global Database or DynamoDB Global Tables, running open-source Cassandra 5.0 across multiple regions provides vastly superior write availability. Cassandra’s masterless architecture ensures that both regions can accept writes simultaneously without the latency penalties of cross-region primary-node failovers.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Using &lt;code&gt;QUORUM&lt;/code&gt; in a multi-region cluster creates synchronous cross-country dependencies, turning a single region failure into a global outage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Standardize on &lt;code&gt;LOCAL_QUORUM&lt;/code&gt; for all reads and writes, and enforce local datacenter affinity in the client driver.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; &lt;code&gt;LOCAL_QUORUM&lt;/code&gt; provides local strong consistency while allowing asynchronous cross-region replication to survive VPC network partitions cleanly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Check your application driver configuration. If &lt;code&gt;basic.load-balancing-policy.local-datacenter&lt;/code&gt; (or your driver’s equivalent local-DC affinity setting) is not explicitly set, you are paying a massive, unnecessary latency tax.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>failures</category></item><item><title>MySQL HA at Scale: ProxySQL and Orchestrator for a 36-Replica Topology</title><link>https://rajivonai.com/blog/2025-09-16-proxysql-orchestrator-mysql-ha-large-topology/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-09-16-proxysql-orchestrator-mysql-ha-large-topology/</guid><description>A failure-first design for coordinating MySQL topology recovery, client routing, candidate selection, and fencing across a large replica fleet.</description><pubDate>Tue, 16 Sep 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A promoted MySQL replica is not a recovered database service. Recovery is complete only when the old writer is fenced, the right successor owns writes, every traffic path agrees, and uncertain client transactions have a reconciliation path.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;RetailCo runs one MySQL order database with 36 replicas. Checkout and order-state mutations use the source. Customer order history, store operations, reporting, backup extraction, and downstream change consumers use different replicas.&lt;/p&gt;
&lt;p&gt;Connecting all 36 replicas directly to one source is simple to draw and expensive to operate. The source must maintain every replication connection and send the binary-log stream repeatedly. RetailCo instead uses three direct intermediate replicas. Each intermediate feeds eleven leaves, producing 36 replicas in total:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Apps[applications] --&gt; Front[stable proxy front door]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Front --&gt; Proxy[ProxySQL fleet]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Proxy --&gt; Source[MySQL source — writes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Proxy --&gt; Readers[eligible read pools]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Source --&gt; IA[intermediate A — preferred candidate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Source --&gt; IB[intermediate B — preferred candidate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Source --&gt; IC[intermediate C — preferred candidate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  IA --&gt; LA[eleven leaf replicas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  IB --&gt; LB[eleven leaf replicas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  IC --&gt; LC[eleven leaf replicas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  LA --&gt; Readers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  LB --&gt; Readers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  LC --&gt; Readers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The intermediate layer reduces direct fan-out from the source and creates clear promotion branches. It also creates new failure modes. If Intermediate B fails, eleven replicas lose their source. If Intermediate A is promoted, the other branches must be reattached without losing track of transaction history. Every intermediate must retain binary logging and replica-update logging so it can serve its descendants. MySQL 8.4 documents that replica-update logging is enabled by default when binary logging is enabled, but production should verify the effective settings rather than trust a default: &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/replication-options-replica.html&quot;&gt;replica options&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;RetailCo uses two control-plane components:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Orchestrator&lt;/strong&gt; discovers the topology, analyzes failures, chooses a compatible successor, promotes it, and reparents replicas.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ProxySQL&lt;/strong&gt; pools connections, groups backends by role, observes health and &lt;code&gt;read_only&lt;/code&gt;, and routes application traffic.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Neither product turns replication into horizontal write scaling. This topology remains one write domain. The &lt;a href=&quot;https://rajivonai.com/blog/2024-11-19-mysql-write-scaling-async-semisync-innodb-cluster/&quot;&gt;replication article&lt;/a&gt; explains the durability boundary; the &lt;a href=&quot;https://rajivonai.com/blog/2025-03-18-sharded-innodb-clusters-mysql-write-scaling/&quot;&gt;sharding article&lt;/a&gt; explains how multiple MySQL clusters create independent write ownership.&lt;/p&gt;
&lt;p&gt;RetailCo is a reference architecture, not a claimed customer deployment. The 36-replica topology and controls below are proposed. This article presents no measured failover time, RPO, RTO, client-error rate, routing-convergence time, or data-loss result.&lt;/p&gt;
&lt;h3 id=&quot;treat-orchestrator-as-an-archived-upstream-plus-a-pinned-downstream-build&quot;&gt;Treat Orchestrator as an archived upstream plus a pinned downstream build&lt;/h3&gt;
&lt;p&gt;The original OpenArk repository was archived on February 18, 2025. As of this article’s March 31, 2026 cutoff, there was no newly announced upstream maintainer to treat as a drop-in continuation. Percona published and used its own fork, while explicitly stating that it was not the public maintainer of the upstream project and modified the fork primarily for its operators: &lt;a href=&quot;https://github.com/percona/orchestrator&quot;&gt;Percona Orchestrator fork&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;That chronology matters. An architecture review should not treat the archived OpenArk repository as an actively maintained release channel or assume that independently carried patches are interchangeable. Pin the exact repository, commit, build flags, configuration schema, and operational tests. For a critical failover control plane, ownership and patch provenance are architecture inputs.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The tempting failure workflow is too short:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;source fails&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    ↓&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Orchestrator promotes a replica&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    ↓&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ProxySQL routes to it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    ↓&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;done&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At least five independent decisions are hidden inside “done”:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Detection:&lt;/strong&gt; Is the source dead, or can Orchestrator merely not reach it?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fencing:&lt;/strong&gt; Can the old source still accept writes from existing sessions, another proxy, a batch host, or a different network partition?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Durability:&lt;/strong&gt; Which candidate has received and applied the acknowledged transaction set?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Topology:&lt;/strong&gt; Which candidate can safely feed the surviving replica branches?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Routing:&lt;/strong&gt; Have all ProxySQL nodes stopped using the old writer and converged on the new one?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;These questions belong to different components:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Responsibility&lt;/th&gt;&lt;th&gt;Owner&lt;/th&gt;&lt;th&gt;Dangerous assumption&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Observe MySQL replication topology&lt;/td&gt;&lt;td&gt;Orchestrator&lt;/td&gt;&lt;td&gt;One failed probe proves the source is dead&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Choose and promote a successor&lt;/td&gt;&lt;td&gt;Orchestrator&lt;/td&gt;&lt;td&gt;Most current always means operationally eligible&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Stop the old source from writing&lt;/td&gt;&lt;td&gt;Infrastructure fence&lt;/td&gt;&lt;td&gt;&lt;code&gt;read_only&lt;/code&gt; or proxy removal is a hard fence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Move client traffic&lt;/td&gt;&lt;td&gt;ProxySQL&lt;/td&gt;&lt;td&gt;One proxy update changes every proxy atomically&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Retry and reconcile uncertain commits&lt;/td&gt;&lt;td&gt;Application&lt;/td&gt;&lt;td&gt;A broken connection means the transaction rolled back&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Repair and reseed failed nodes&lt;/td&gt;&lt;td&gt;DBA automation&lt;/td&gt;&lt;td&gt;Repointing replication repairs divergent data&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The worst failure is not a slow promotion. It is two writable histories: Orchestrator promotes Intermediate A while the isolated old source continues serving sessions through a proxy that did not update. Both databases can return successful commits. GTID can identify transactions; it cannot decide which business history should win.&lt;/p&gt;
&lt;p&gt;The architecture question is therefore: how does RetailCo make detection, fencing, promotion, routing, and application recovery one observable workflow without pretending they are one atomic operation?&lt;/p&gt;
&lt;h2 id=&quot;build-a-failover-control-plane&quot;&gt;Build a Failover Control Plane&lt;/h2&gt;
&lt;h3 id=&quot;give-every-replica-an-explicit-role&quot;&gt;Give every replica an explicit role&lt;/h3&gt;
&lt;p&gt;Orchestrator’s recovery documentation explains why the most advanced replica is not automatically the right successor. Version, binary-log configuration, replication filters, hardware, and the ability to preserve serving capacity all matter: &lt;a href=&quot;https://github.com/openark/orchestrator/blob/master/docs/topology-recovery.md&quot;&gt;topology recovery&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;RetailCo classifies nodes before failure:&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Replica class&lt;/th&gt;&lt;th&gt;Topology role&lt;/th&gt;&lt;th&gt;Promotion rule&lt;/th&gt;&lt;th&gt;Application traffic&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Three direct intermediates&lt;/td&gt;&lt;td&gt;Feed leaf branches and receive the source stream&lt;/td&gt;&lt;td&gt;&lt;code&gt;prefer&lt;/code&gt;&lt;/td&gt;&lt;td&gt;None or tightly bounded reads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;General serving leaves&lt;/td&gt;&lt;td&gt;Customer and store reads&lt;/td&gt;&lt;td&gt;&lt;code&gt;neutral&lt;/code&gt; or &lt;code&gt;prefer_not&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reporting or ETL leaves&lt;/td&gt;&lt;td&gt;Long-running analytical reads&lt;/td&gt;&lt;td&gt;&lt;code&gt;must_not&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Isolated reporting traffic&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Delayed recovery replica&lt;/td&gt;&lt;td&gt;Human-error recovery window&lt;/td&gt;&lt;td&gt;&lt;code&gt;must_not&lt;/code&gt;&lt;/td&gt;&lt;td&gt;No normal application traffic&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup source&lt;/td&gt;&lt;td&gt;Snapshot and backup work&lt;/td&gt;&lt;td&gt;&lt;code&gt;must_not&lt;/code&gt;&lt;/td&gt;&lt;td&gt;No application traffic&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Filtered replica&lt;/td&gt;&lt;td&gt;Special-purpose subset&lt;/td&gt;&lt;td&gt;&lt;code&gt;must_not&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Only its intended workload&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The three intermediates should match the source’s MySQL version, character sets, replication filters, durability settings, storage capability, binary-log format, and schema. They need GTID, unique server identifiers, binary logging, and replica-update logging. Candidate registration is operational state, not a one-time installation task. Orchestrator documents &lt;code&gt;prefer&lt;/code&gt;, &lt;code&gt;neutral&lt;/code&gt;, &lt;code&gt;prefer_not&lt;/code&gt;, and &lt;code&gt;must_not&lt;/code&gt; promotion rules and notes that dynamic rules expire; configuration automation must refresh and monitor them.&lt;/p&gt;
&lt;p&gt;GTID makes transaction identity portable across topology changes. A replicated transaction retains the GTID assigned at origin, and auto-positioning lets a replica request transactions it has not executed: &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/replication-gtids-concepts.html&quot;&gt;GTID concepts&lt;/a&gt;. GTID does &lt;strong&gt;not&lt;/strong&gt; prove zero RPO. The successor still needs the relevant transactions in its executed or recoverable relay-log state.&lt;/p&gt;
&lt;p&gt;Semisynchronous replication also needs candidate alignment. An acknowledgment proves that an enabled semisynchronous replica received the transaction into its relay log; it does not prove that an arbitrary promotion candidate applied it. RetailCo should restrict acknowledgment duty to eligible, durable candidates, compare GTID received and executed state, and decide whether promotion waits for the applier or fails closed. Orchestrator exposes controls to fail or delay promotion when the candidate’s SQL thread is not current: &lt;a href=&quot;https://github.com/openark/orchestrator/blob/master/docs/configuration-recovery.md&quot;&gt;recovery configuration&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;keep-the-control-plane-highly-available-but-separate-from-the-data-plane&quot;&gt;Keep the control plane highly available but separate from the data plane&lt;/h3&gt;
&lt;p&gt;Run three Orchestrator nodes across failure domains in Raft mode. The Raft leader is the only node allowed to change topology; a partitioned node without quorum cannot lead. This protects Orchestrator’s decision plane. It does not create quorum for MySQL transactions: &lt;a href=&quot;https://github.com/openark/orchestrator/blob/master/docs/raft.md&quot;&gt;Orchestrator Raft&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Run multiple ProxySQL instances behind a stable network endpoint or close to application pools. ProxySQL Cluster synchronizes users, servers, variables, and query rules through peer checksum and version comparison. It does not accept the application’s connection on behalf of another failed ProxySQL node, so RetailCo still needs a load balancer, service discovery, or local proxy deployment.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App[application connection pools] --&gt; LB[stable network endpoint]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  LB --&gt; P1[ProxySQL core A]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  LB --&gt; P2[ProxySQL core B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  LB --&gt; P3[ProxySQL core C]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  P1 --&gt; Writer[writer hostgroup]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  P2 --&gt; Writer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  P3 --&gt; Writer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  P1 --&gt; Read[reader hostgroups]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  P2 --&gt; Read&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  P3 --&gt; Read&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  O1[Orchestrator node A] --&gt; Leader[Raft leader]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  O2[Orchestrator node B] --&gt; Leader&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  O3[Orchestrator node C] --&gt; Leader&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Leader --&gt; Topology[MySQL topology actions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Leader --&gt; Fence[infrastructure fencing service]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Leader --&gt; P1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;ProxySQL’s traditional replication monitoring uses &lt;code&gt;read_only&lt;/code&gt; to move servers between writer and reader hostgroups and can shun replicas that exceed configured lag: &lt;a href=&quot;https://proxysql.com/documentation/backend-monitoring/&quot;&gt;backend monitoring&lt;/a&gt;. This is routing observation, not successor election. Orchestrator should remain the authority that decides which server becomes writable.&lt;/p&gt;
&lt;h3 id=&quot;treat-proxysql-hostgroups-as-workload-contracts&quot;&gt;Treat ProxySQL hostgroups as workload contracts&lt;/h3&gt;
&lt;p&gt;Do not put 36 replicas into one anonymous reader pool. Separate them by acceptable staleness and workload:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Hostgroup 10  writer — mutations and strict read-after-write&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Hostgroup 20  customer reads — low-lag serving replicas&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Hostgroup 21  store operations — bounded-staleness replicas&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Hostgroup 30  reporting — isolated reporting replicas&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The application should use separate read-write and read-only credentials or connection endpoints. The default path goes to the writer. Only explicitly safe read operations use reader pools.&lt;/p&gt;
&lt;p&gt;A broad query rule that sends every statement beginning with &lt;code&gt;SELECT&lt;/code&gt; to a replica is not a transaction model. &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;, session state, stored programs, temporary tables, and a read immediately following a write can all violate the assumption. Prefer routing intent at the connection boundary; use SQL rules only for reviewed, testable exceptions.&lt;/p&gt;
&lt;p&gt;ProxySQL configuration also has memory, runtime, and disk layers. A database-row update does not affect traffic until it is loaded to runtime, and a runtime change is not restart-persistent until saved: &lt;a href=&quot;https://proxysql.com/documentation/backend-server-configuration/&quot;&gt;backend server configuration&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;fence-before-promoting-during-an-ambiguous-failure&quot;&gt;Fence before promoting during an ambiguous failure&lt;/h3&gt;
&lt;p&gt;Fencing means proving the old source cannot accept new business writes. The mechanism depends on the environment:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;power off or stop the compute instance through an infrastructure control plane;&lt;/li&gt;
&lt;li&gt;isolate its database network interface or security policy;&lt;/li&gt;
&lt;li&gt;revoke or rotate a writer lease checked below the application routing layer;&lt;/li&gt;
&lt;li&gt;detach exclusive storage where the storage architecture supports it;&lt;/li&gt;
&lt;li&gt;use a hardware or virtualization fencing controller on-premises.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Changing &lt;code&gt;read_only&lt;/code&gt;, removing a server from ProxySQL, or changing DNS is not equivalent. Privileged sessions can bypass read-only controls, existing connections may survive, direct clients may bypass the proxy, and configuration can diverge across proxy nodes.&lt;/p&gt;
&lt;p&gt;The archived Orchestrator documentation specifies that a failing &lt;code&gt;PreFailoverProcesses&lt;/code&gt; command aborts recovery. RetailCo uses that gate for an idempotent fence command. The hook must return success only after the infrastructure control plane confirms the old source is stopped or isolated. If fencing cannot be proven, automatic promotion stops and the incident moves to a human decision. That intentionally trades availability for protection against two writers.&lt;/p&gt;
&lt;h3 id=&quot;coordinate-topology-recovery-and-routing-without-confusing-them&quot;&gt;Coordinate topology recovery and routing without confusing them&lt;/h3&gt;
&lt;p&gt;Use explicit Orchestrator recovery processes or a separate recovery controller to coordinate ProxySQL. Before promotion, the automation can drain the old source using &lt;code&gt;OFFLINE_SOFT&lt;/code&gt; or weight zero, but that remains a traffic action rather than a hard fence. After promotion, it removes the old writer, adds the successor to the writer hostgroup, loads the change to runtime, saves it to disk, and verifies every proxy’s runtime state.&lt;/p&gt;
&lt;p&gt;The integration contract must define whether a ProxySQL update failure aborts promotion, blocks application readiness, or pages an operator. ProxySQL Cluster can propagate server-module changes, but propagation is not the same as atomic convergence, and &lt;code&gt;OFFLINE_SOFT&lt;/code&gt; permits existing connections to complete. Therefore “Orchestrator recovery succeeded” and “application routing recovered” remain separate service states.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Detect[Orchestrator detects actionable source failure] --&gt; Confirm{failure eligible for automatic recovery}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Confirm --&gt;|no| Page[page operator and preserve topology]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Confirm --&gt;|yes| Fence[pre-failover hook requests hard fence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Fence --&gt; Proven{old source isolated}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Proven --&gt;|no| Stop[abort promotion and escalate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Proven --&gt;|yes| Drain[drain old writer from ProxySQL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Drain --&gt; Select[select compatible GTID candidate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Select --&gt; Catchup[apply required relay-log transactions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Catchup --&gt; Promote[promote one writable successor]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Promote --&gt; Reparent[repair surviving replica branches]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reparent --&gt; Route[update ProxySQL writer hostgroup]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Route --&gt; Converge[verify every proxy checksum and runtime route]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Converge --&gt; Resume[applications reconnect, retry, and reconcile]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The application should not resume writes merely because the Orchestrator audit says promotion succeeded. A recovery controller or runbook verifies:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;the fence token identifies the failed source and incident;&lt;/li&gt;
&lt;li&gt;exactly one MySQL node reports writable and carries the expected source epoch or service identity;&lt;/li&gt;
&lt;li&gt;each ProxySQL node’s runtime writer hostgroup contains only the successor;&lt;/li&gt;
&lt;li&gt;ProxySQL Cluster checksums have converged;&lt;/li&gt;
&lt;li&gt;a canary transaction commits through the same endpoint applications use;&lt;/li&gt;
&lt;li&gt;uncertain commits from the failure window enter reconciliation.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;make-client-recovery-explicit&quot;&gt;Make client recovery explicit&lt;/h3&gt;
&lt;p&gt;Failover breaks in-flight connections. A client can lose its socket after the server commits but before the response arrives. Retrying an unprotected checkout can create a duplicate order or payment.&lt;/p&gt;
&lt;p&gt;Every mutation needs a stable idempotency key and a result lookup. After a connection error:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;discard the old pooled connection;&lt;/li&gt;
&lt;li&gt;reconnect through the stable ProxySQL endpoint;&lt;/li&gt;
&lt;li&gt;query the idempotency record on the authoritative writer;&lt;/li&gt;
&lt;li&gt;return the existing result if the transaction committed;&lt;/li&gt;
&lt;li&gt;retry only if no result exists and the operation is safe to repeat.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;ProxySQL moves future connections and queries. It cannot reconstruct a transaction that was active on a dead connection or determine the business result of an ambiguous commit.&lt;/p&gt;
&lt;h3 id=&quot;preserve-branch-capacity-during-topology-repair&quot;&gt;Preserve branch capacity during topology repair&lt;/h3&gt;
&lt;p&gt;If one intermediate fails, the source may still be healthy while eleven leaves are orphaned. Orchestrator can analyze an intermediate-source failure and relocate descendants using GTID-aware topology recovery. The recovery choice is not free:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Failure[intermediate B fails] --&gt; Assess[inspect eleven orphaned leaves]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Assess --&gt; PromoteLeaf{eligible leaf can feed branch}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  PromoteLeaf --&gt;|yes| NewIntermediate[promote eligible leaf as intermediate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  PromoteLeaf --&gt;|no| Sibling[attach leaves below surviving intermediate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  NewIntermediate --&gt; Reattach[reparent remaining branch leaves]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Sibling --&gt; Capacity[verify sender, network, and storage capacity]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reattach --&gt; Capacity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Capacity --&gt; Restore[rebuild failed intermediate and restore redundancy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A surviving intermediate that was sized for eleven leaves may not safely feed twenty-two. The recovery plan needs sender capacity, network bandwidth, binary-log retention, replication credentials, TLS, and disk headroom. Filtered or delayed leaves must not silently become normal ancestors.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;h3 id=&quot;recommended-retailco-design&quot;&gt;Recommended RetailCo design&lt;/h3&gt;
&lt;p&gt;For this 36-replica topology, I would use:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;three direct, identical, lightly loaded intermediate replicas as preferred candidates;&lt;/li&gt;
&lt;li&gt;eleven leaves below each intermediate, divided into explicit serving, reporting, delayed, and backup roles;&lt;/li&gt;
&lt;li&gt;GTID auto-positioning and verified replica-update logging on every possible ancestor;&lt;/li&gt;
&lt;li&gt;semisynchronous acknowledgment only from eligible candidate class nodes, with operational fallback alerts;&lt;/li&gt;
&lt;li&gt;three Orchestrator nodes in Raft mode, with automatic recovery enabled only for named production clusters;&lt;/li&gt;
&lt;li&gt;an aborting pre-failover hook that requires infrastructure fence confirmation;&lt;/li&gt;
&lt;li&gt;three ProxySQL core nodes plus a stable application-facing endpoint;&lt;/li&gt;
&lt;li&gt;distinct writer and reader connection identities, not generic &lt;code&gt;SELECT&lt;/code&gt; regex routing;&lt;/li&gt;
&lt;li&gt;post-promotion proof that all runtime hostgroups and cluster checksums agree.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example configuration fragments should be treated as review inputs, not copy-paste production configuration:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;RaftEnabled&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;RaftBind&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;orchestrator-a.internal&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;RaftDataDir&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/var/lib/orchestrator&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;RaftNodes&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;orchestrator-a.internal&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;orchestrator-b.internal&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;orchestrator-c.internal&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;RecoverMasterClusterFilters&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;retailco-orders&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;RecoverIntermediateMasterClusterFilters&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;retailco-orders&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;PreventCrossRegionMasterFailover&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;FailMasterPromotionIfSQLThreadNotUpToDate&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;PreFailoverProcesses&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;/opt/retailco/bin/proxysql-drain-writer --host {failedHost}&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;/opt/retailco/bin/fence-mysql-source --host {failedHost} --incident {failureCluster}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  &quot;PostMasterFailoverProcesses&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;/opt/retailco/bin/proxysql-promote-writer --successor {successorHost} --cluster {failureCluster}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is one important thing this configuration does &lt;strong&gt;not&lt;/strong&gt; contain: any ProxySQL setting. Orchestrator has no built-in ProxySQL integration and no &lt;code&gt;ProxySQL*&lt;/code&gt; configuration keys. Every ProxySQL action is an external command invoked through the topology-recovery hooks, so RetailCo owns the script, its idempotency, its exit codes, and its timeout behavior. Treat any design document that configures ProxySQL “inside Orchestrator” as unverified.&lt;/p&gt;
&lt;p&gt;That distinction has an operational consequence. Because the ProxySQL update is a hook and not a transaction, its exit status is the only signal Orchestrator has. A &lt;code&gt;proxysql-promote-writer&lt;/code&gt; script that exits zero after updating one proxy — but before confirming the other two converged — reports success for an incomplete routing change. The script must poll every proxy’s runtime state and cluster checksum before returning, or the recovery audit will record a convergence that never happened.&lt;/p&gt;
&lt;p&gt;Do not place the ProxySQL Admin password in a committed example file or pass it on a hook command line, where it is visible in the process table. Use the secret mechanism supported by the pinned deployment and protect the Admin interface (port 6032) with network controls and TLS.&lt;/p&gt;
&lt;p&gt;The ProxySQL writer and reader relationship is explicit:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; mysql_replication_hostgroups (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    writer_hostgroup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    reader_hostgroup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    comment&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;retailco-orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LOAD&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MYSQL SERVERS &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; RUNTIME;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SAVE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MYSQL SERVERS &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TO&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DISK&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The production configuration would add each backend’s role, weight, connection limit, TLS setting, and maximum replication lag. It would also separate reporting from customer-serving readers rather than force both through hostgroup 20.&lt;/p&gt;
&lt;h3 id=&quot;historical-note--from-mha-scripts-to-topology-control&quot;&gt;Historical note — from MHA scripts to topology control&lt;/h3&gt;
&lt;p&gt;MySQL MHA’s manager project describes itself as tooling for automated source failover and fast source switching: &lt;a href=&quot;https://github.com/yoshinorim/mha4mysql-manager&quot;&gt;MHA manager&lt;/a&gt;. That model remains useful historical context. The architectural change with Orchestrator is continuous discovery and refactoring of the whole topology, including intermediate-source failures and successor compatibility.&lt;/p&gt;
&lt;p&gt;This is not a claim that every organization migrated from MHA or that Orchestrator is universally safer. Existing MHA deployments can encode years of environment-specific fencing and service-discovery knowledge. Replacing them requires reproducing those controls, not merely installing a newer topology UI.&lt;/p&gt;
&lt;h3 id=&quot;test-the-timeline-as-separate-control-loops&quot;&gt;Test the timeline as separate control loops&lt;/h3&gt;
&lt;p&gt;Status: &lt;strong&gt;PROPOSED LAB — NO EMPIRICAL RESULTS&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Measure these intervals independently:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  T0[source stops serving correctly] --&gt; T1[Orchestrator declares actionable failure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  T1 --&gt; T2[infrastructure fence confirmed]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  T2 --&gt; T3[successor selected and caught up]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  T3 --&gt; T4[successor becomes writable]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  T4 --&gt; T5[replica branches begin repair]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  T4 --&gt; T6[ProxySQL writer route changes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  T6 --&gt; T7[all proxy configurations converge]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  T7 --&gt; T8[first successful application write]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  T8 --&gt; T9[ambiguous transactions reconciled]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Run at least these failure shapes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;planned graceful takeover;&lt;/li&gt;
&lt;li&gt;hard source process and host loss;&lt;/li&gt;
&lt;li&gt;network isolation where the old source remains alive;&lt;/li&gt;
&lt;li&gt;fence-service failure;&lt;/li&gt;
&lt;li&gt;candidate with received but unapplied transactions;&lt;/li&gt;
&lt;li&gt;most advanced replica marked &lt;code&gt;must_not&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;intermediate loss with eleven descendants;&lt;/li&gt;
&lt;li&gt;one ProxySQL node unavailable during the hook;&lt;/li&gt;
&lt;li&gt;one ProxySQL satellite refusing to converge;&lt;/li&gt;
&lt;li&gt;Orchestrator leader loss during discovery and during recovery;&lt;/li&gt;
&lt;li&gt;old source returning after promotion.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Capture client errors, retries, ambiguous commits, GTID received and executed sets, lost replicas, semisynchronous status, recovery audit records, hook exit states, runtime hostgroups, ProxySQL checksums, and the observed RPO and RTO.&lt;/p&gt;
&lt;p&gt;Do not publish one “failover took 12 seconds” number. Detection, fencing, promotion, routing convergence, first successful application write, and full topology repair answer different operational questions.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure or design error&lt;/th&gt;&lt;th&gt;Consequence&lt;/th&gt;&lt;th&gt;Required control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Orchestrator loses contact but old source remains writable&lt;/td&gt;&lt;td&gt;Two writers can form after promotion&lt;/td&gt;&lt;td&gt;Hard fence proof before unplanned promotion&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;OFFLINE_SOFT&lt;/code&gt; treated as fencing&lt;/td&gt;&lt;td&gt;Existing or bypass connections can still write&lt;/td&gt;&lt;td&gt;Use it for drain; fence below the proxy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ProxySQL hook fails but Orchestrator succeeds&lt;/td&gt;&lt;td&gt;New source exists while traffic still targets old route&lt;/td&gt;&lt;td&gt;Separate topology and routing recovery states; alert and reconcile&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ProxySQL nodes have different configurations&lt;/td&gt;&lt;td&gt;Clients see different writers or reader pools&lt;/td&gt;&lt;td&gt;Verify runtime hostgroups and cluster checksums on every node&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Most advanced replica is filtered or undersized&lt;/td&gt;&lt;td&gt;Promotion loses capacity or data scope&lt;/td&gt;&lt;td&gt;Explicit candidate roles and promotion rules&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Semisynchronous acknowledgment comes from an ineligible replica&lt;/td&gt;&lt;td&gt;Promoted candidate may lack acknowledged transactions&lt;/td&gt;&lt;td&gt;Limit acknowledgers and inspect received, relay, and executed state&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One intermediate fails&lt;/td&gt;&lt;td&gt;Eleven leaves become orphaned&lt;/td&gt;&lt;td&gt;Preplanned branch recovery and spare sender capacity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Query rules send unsafe reads to replicas&lt;/td&gt;&lt;td&gt;Read-after-write and locking semantics break&lt;/td&gt;&lt;td&gt;Separate endpoints or users; narrow allowlisted rules&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Old source rejoins automatically&lt;/td&gt;&lt;td&gt;Divergent history can contaminate topology&lt;/td&gt;&lt;td&gt;Keep fenced; compare GTID sets; rebuild or perform reviewed reconciliation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Orchestrator Raft loses quorum&lt;/td&gt;&lt;td&gt;Automated topology changes stop&lt;/td&gt;&lt;td&gt;Preserve MySQL state, restore control-plane quorum, use reviewed manual procedure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application blindly retries&lt;/td&gt;&lt;td&gt;Duplicate orders or payments&lt;/td&gt;&lt;td&gt;Idempotency keys and authoritative result lookup&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Thirty-six replicas are treated as backups&lt;/td&gt;&lt;td&gt;Replicated deletion reaches every node&lt;/td&gt;&lt;td&gt;Independent backups and tested point-in-time recovery&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;when-i-would-not-use-this-architecture&quot;&gt;When I would not use this architecture&lt;/h3&gt;
&lt;p&gt;I would not build a 36-replica tree when the read workload can be reduced with query repair, caching, a search index, a warehouse, or fewer larger replicas. Every replica adds patching, backup, monitoring, credential, schema, and recovery work.&lt;/p&gt;
&lt;p&gt;I would not combine ProxySQL and Orchestrator without a team that can own infrastructure fencing and application idempotency. The tools automate topology and routing; they do not remove the need to choose availability versus split-brain risk during an ambiguous partition.&lt;/p&gt;
&lt;p&gt;For new deployments that can adopt Group Replication or a managed database, compare their failure contract with this external control plane. For genuine write-capacity limits, do not add more replicas—use the sharding or decomposition options elsewhere in this series.&lt;/p&gt;
&lt;h3 id=&quot;retailco-verdict&quot;&gt;RetailCo verdict&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Good fit:               Large self-managed MySQL replica fleets with one write source&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Poor fit:               Small fleets, weak operational ownership, or write-scale problems&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Recommended option:     Orchestrator for topology, ProxySQL for traffic, hard external fencing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Main advantage:         Topology-aware recovery and workload-aware routing remain separate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Main limitation:        The combined workflow is not atomic across infrastructure and clients&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Operational complexity: High; automation must cover failure, convergence, rebuild, and audit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Migration complexity:   Medium to high; introduce observation, then routing, then recovery&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The conclusion is narrower than “ProxySQL plus Orchestrator gives automatic HA.” It gives RetailCo the components for an HA control plane. The production guarantee comes from the contract between them: one eligible successor, one verified fence, one converged writer route, and an application that can resolve uncertain transactions.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Inventory every path that can write to MySQL, every replica role, and every place where current failover assumes unreachable means dead.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Separate detection, hard fencing, candidate promotion, topology repair, ProxySQL convergence, and application reconciliation into explicit states.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run planned, hard-failure, network-partition, proxy-divergence, intermediate-loss, Raft-leader-loss, and old-source-return exercises with timestamped evidence.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Begin in observation-only mode, register and verify candidate rules, route read-only traffic through ProxySQL, rehearse planned takeover, and enable automatic unplanned recovery only after the fence gate passes repeatedly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The next article returns to application architecture: decomposing a monolithic database write path without beginning with a disruptive microservices rewrite.&lt;/p&gt;
&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/openark/orchestrator&quot;&gt;Archived OpenArk Orchestrator repository&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/percona/orchestrator&quot;&gt;Percona Orchestrator fork and maintenance scope&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/openark/orchestrator/blob/master/docs/topology-recovery.md&quot;&gt;Orchestrator topology recovery&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/openark/orchestrator/blob/master/docs/configuration-recovery.md&quot;&gt;Orchestrator recovery configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/openark/orchestrator/blob/master/docs/raft.md&quot;&gt;Orchestrator Raft&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://proxysql.com/documentation/backend-monitoring/&quot;&gt;ProxySQL backend monitoring&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://proxysql.com/documentation/backend-server-configuration/&quot;&gt;ProxySQL backend server configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://proxysql.com/documentation/proxysql-cluster&quot;&gt;ProxySQL Cluster&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://proxysql.com/documentation/the-admin-schemas/stats/stats-proxysql-cluster/&quot;&gt;ProxySQL Cluster statistics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/replication-gtids-concepts.html&quot;&gt;MySQL 8.4 GTID concepts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/replication-solutions-switch.html&quot;&gt;MySQL 8.4 source switching during failover&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/yoshinorim/mha4mysql-manager&quot;&gt;MySQL MHA manager&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>Autovacuum Is a Capacity Problem, Not a Maintenance Task</title><link>https://rajivonai.com/blog/2025-09-13-autovacuum-is-a-capacity-problem-not-a-maintenance-task/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-09-13-autovacuum-is-a-capacity-problem-not-a-maintenance-task/</guid><description>PostgreSQL vacuum failures often start with blocked cleanup, table bloat, and weak lock observability during peak load.</description><pubDate>Sat, 13 Sep 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Autovacuum is not a background chore; it is part of write capacity, and PostgreSQL will collect that debt during peak traffic if the system does not budget for cleanup before the workload arrives.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s multi-version concurrency control, or MVCC, makes reads and writes coexist by leaving old row versions behind after &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt;. &lt;code&gt;VACUUM&lt;/code&gt; later removes or marks that dead space reusable, updates planner statistics, maintains visibility maps for index-only scans, and protects the database from transaction ID wraparound, as PostgreSQL’s own routine vacuuming documentation describes: &lt;a href=&quot;https://www.postgresql.org/docs/17/routine-vacuuming.html&quot;&gt;PostgreSQL 17 routine vacuuming&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The operational mistake is treating autovacuum as maintenance instead of capacity. In a write-heavy commerce system, queue processor, billing ledger, workflow engine, or event ingestion service, dead tuples are not an after-hours concern. They are a steady byproduct of throughput.&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Default mental model&lt;/th&gt;&lt;th&gt;Production reality&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Autovacuum is background maintenance&lt;/td&gt;&lt;td&gt;Autovacuum competes for I/O, workers, locks, and transaction horizon progress&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Active connection count explains the incident&lt;/td&gt;&lt;td&gt;Table-level dead tuples, lock waits, and oldest &lt;code&gt;xmin&lt;/code&gt; explain the incident&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One cluster setting fits every table&lt;/td&gt;&lt;td&gt;High-churn tables need per-table settings&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Killing autovacuum ends the emergency&lt;/td&gt;&lt;td&gt;Killing autovacuum creates cleanup debt that must be paid back deliberately&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The common failure is backwards: autovacuum usually does not start as the villain. It becomes visible after the system has already created cleanup debt.&lt;/p&gt;
&lt;p&gt;PostgreSQL standard &lt;code&gt;VACUUM&lt;/code&gt; can run alongside ordinary &lt;code&gt;SELECT&lt;/code&gt;, &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, and &lt;code&gt;DELETE&lt;/code&gt;, while &lt;code&gt;VACUUM FULL&lt;/code&gt; requires an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock and rewrites the table. That distinction matters. A normal autovacuum is designed to be cooperative, but it still consumes I/O and takes a &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt; lock. If conflicting operations keep interrupting it, if long transactions hold the visibility horizon open, or if the write rate exceeds cleanup capacity, dead tuples accumulate until the application starts paying for them in heap scans, index scans, cache churn, and longer vacuum cycles.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure point&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Long-running transaction or &lt;code&gt;idle in transaction&lt;/code&gt; session&lt;/td&gt;&lt;td&gt;Dead tuples remain visible to the oldest snapshot and cannot be removed&lt;/td&gt;&lt;td&gt;Autovacuum can run and still fail to reclaim the space operators expect&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Default &lt;code&gt;autovacuum_vacuum_scale_factor = 0.2&lt;/code&gt; on a 200M-row table&lt;/td&gt;&lt;td&gt;Vacuum may wait for tens of millions of obsolete tuples before triggering&lt;/td&gt;&lt;td&gt;The threshold is mathematically sane for small tables and operationally late for hot large tables&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replication slot or stale replica feedback holds &lt;code&gt;xmin&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Cleanup is pinned behind downstream consumption&lt;/td&gt;&lt;td&gt;Primary database bloat becomes a replication and availability problem, not just local storage waste&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Large tables become eligible together&lt;/td&gt;&lt;td&gt;&lt;code&gt;autovacuum_max_workers&lt;/code&gt; can be occupied by a small number of relations&lt;/td&gt;&lt;td&gt;Smaller hot tables wait behind large scans and latency spreads across unrelated features&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Monitoring only &lt;code&gt;pg_stat_activity&lt;/code&gt; active count&lt;/td&gt;&lt;td&gt;Operators see queueing, not the relation causing cleanup debt&lt;/td&gt;&lt;td&gt;The dashboard points at symptoms while the table-level cause grows&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The core question is not “Why did autovacuum run during peak load?” The useful question is: &lt;strong&gt;why did the system enter peak load with no table-level cleanup budget, no lock visibility, and no oldest-transaction alarm?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;treat-vacuum-as-a-capacity-control-plane&quot;&gt;Treat Vacuum as a Capacity Control Plane&lt;/h2&gt;
&lt;p&gt;The right architecture is a small vacuum control plane: table-level observability, per-table policy, lock and horizon detection, and an operator runbook that distinguishes emergency relief from debt repayment.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[application writes] --&gt; MVCC[MVCC creates old row versions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MVCC --&gt; Stats[pg_stat_user_tables dead tuple counters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MVCC --&gt; Horizon[oldest xmin and replication horizon]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Stats --&gt; Dashboard[vacuum health dashboard]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Horizon --&gt; Dashboard&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Locks[pg_locks and pg_stat_activity] --&gt; Dashboard&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Progress[pg_stat_progress_vacuum] --&gt; Dashboard&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Dashboard --&gt; Policy[per-table autovacuum policy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Policy --&gt; Workers[autovacuum workers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Workers --&gt; Cleanup[dead tuple cleanup and freeze progress]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Cleanup --&gt; Capacity[steady write capacity]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Dashboard --&gt; Runbook[operator runbook]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Build the dashboard around relations, not sessions.&lt;/p&gt;
&lt;p&gt;Start with &lt;code&gt;pg_stat_user_tables&lt;/code&gt;, &lt;code&gt;pg_class&lt;/code&gt;, &lt;code&gt;pg_stat_activity&lt;/code&gt;, &lt;code&gt;pg_locks&lt;/code&gt;, and &lt;code&gt;pg_stat_progress_vacuum&lt;/code&gt;. Active connections are only the smoke. The heat is per relation: &lt;code&gt;n_dead_tup&lt;/code&gt;, relation size, &lt;code&gt;last_autovacuum&lt;/code&gt;, &lt;code&gt;last_autoanalyze&lt;/code&gt;, current vacuum phase, lock wait duration, and the oldest transaction age.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;schemaname&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;relname&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;n_live_tup&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;n_dead_tup&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    pg_size_pretty(pg_total_relation_size(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;oid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; total_size,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    ROUND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;n_dead_tup&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; NULLIF&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;n_live_tup&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dead_rows_pct,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;last_autovacuum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;last_autoanalyze&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    age(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;last_autovacuum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; last_autovacuum_age&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_tables s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_class c &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;relname&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;relname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_namespace n &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; n&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;oid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;relnamespace&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; n&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;nspname&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;schemaname&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;n_dead_tup&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Verification: the top 20 write-heavy tables should have visible dead tuple count, dead tuple ratio, total relation size, last autovacuum age, and last analyze age on one screen.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add horizon monitoring before tuning cost limits.&lt;/p&gt;
&lt;p&gt;Autovacuum cannot remove row versions still visible to an old snapshot. A single abandoned transaction can make vacuum appear “ineffective” even when workers are active. Check for large &lt;code&gt;backend_xmin&lt;/code&gt;, old &lt;code&gt;backend_xid&lt;/code&gt;, prepared transactions, and replication slots.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    pid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    usename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    application_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    wait_event_type,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    wait_event,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    age(backend_xmin) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; backend_xmin_age,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    age(backend_xid) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; backend_xid_age,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    age(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(), xact_start) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; transaction_age,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    LEFT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(query, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;160&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; query_sample&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; backend_xmin &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IS NOT NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;   OR&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; backend_xid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IS NOT NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; GREATEST&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    COALESCE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(age(backend_xmin), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    COALESCE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(age(backend_xid), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Verification: alert when a transaction age crosses a workload-specific threshold, such as 5 minutes for OLTP checkout paths or 30 minutes for internal reporting, before tying the alert to dead tuple growth.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Track vacuum progress by phase.&lt;/p&gt;
&lt;p&gt;PostgreSQL exposes &lt;code&gt;pg_stat_progress_vacuum&lt;/code&gt; for active vacuum operations, including autovacuum workers. The view reports heap blocks scanned, heap blocks vacuumed, index vacuum count, dead tuple counters, and the current phase; PostgreSQL documents this under progress reporting: &lt;a href=&quot;https://www.postgresql.org/docs/current/progress-reporting.html&quot;&gt;VACUUM progress reporting&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    a&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;datname&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;relid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;::regclass &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; relation,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    a&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;phase&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;heap_blks_total&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;heap_blks_scanned&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;heap_blks_vacuumed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    ROUND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;heap_blks_scanned&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; NULLIF&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;heap_blks_total&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pct_scanned,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;index_vacuum_count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;num_dead_tuples&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_progress_vacuum p&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity a &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;USING&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (pid)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; p&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Verification: operators should be able to classify an active vacuum as scanning, vacuuming indexes, vacuuming heap, cleaning indexes, truncating heap, or performing final cleanup without reading server logs.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Tune hot tables with absolute thresholds, not ratios alone.&lt;/p&gt;
&lt;p&gt;PostgreSQL triggers autovacuum when obsolete tuple count exceeds:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor * reltuples&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That formula is documented in the PostgreSQL autovacuum daemon section: &lt;a href=&quot;https://www.postgresql.org/docs/17/routine-vacuuming.html&quot;&gt;autovacuum threshold formula&lt;/a&gt;. On a 10M-row &lt;code&gt;orders&lt;/code&gt; table, the default &lt;code&gt;50 + 0.2 * 10000000&lt;/code&gt; means roughly 2,000,050 obsolete tuples before vacuum eligibility. On a hot table updated continuously, that is not a maintenance threshold. It is an incident waiting room with chairs.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    autovacuum_vacuum_scale_factor &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;01&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    autovacuum_vacuum_threshold &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 50000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    autovacuum_analyze_scale_factor &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;02&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    autovacuum_analyze_threshold &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 50000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    autovacuum_vacuum_cost_delay &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Verification: after a realistic write-load test, the table should show smaller, more frequent vacuum cycles, stable &lt;code&gt;n_dead_tup&lt;/code&gt;, and no sustained increase in p95 query latency during vacuum phases.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Separate emergency termination from recovery.&lt;/p&gt;
&lt;p&gt;Terminating an autovacuum worker may reduce immediate pressure if it is contending with production traffic, but it does not remove the dead tuples. It postpones cleanup. Worse, if the worker is running to prevent wraparound, PostgreSQL does not treat it like ordinary background work; autovacuum behavior around wraparound prevention is intentionally harder to interrupt.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    pid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    query,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    age(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(), query_start) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; runtime,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    wait_event_type,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    wait_event&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; query ILIKE &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;%autovacuum%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Verification: every termination action must create a follow-up ticket with target relation, observed dead tuples, oldest transaction state, and an explicit manual &lt;code&gt;VACUUM&lt;/code&gt; or retuning plan.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern is not theoretical. GitLab publicly analyzed PostgreSQL autovacuum behavior on GitLab.com and treated it as a production tuning problem backed by stats, logs, and Prometheus data. In their autovacuum considerations issue, they reported autovacuum consuming a high share of read I/O while doing a small amount of block cleanup, then evaluated table-specific behavior and candidate configuration changes: &lt;a href=&quot;https://gitlab.com/gitlab-com/gl-infra/production-engineering/-/work_items/4916&quot;&gt;GitLab autovacuum considerations&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The important engineering detail is scale. GitLab called out relations in the hundreds of millions to over a billion tuples, including &lt;code&gt;merge_request_diff_files&lt;/code&gt; and &lt;code&gt;merge_request_diff_commits&lt;/code&gt;. For those shapes, a global threshold is a blunt instrument. A scale factor that is reasonable for a 500K-row table can be absurd for a 1B-row table, and a threshold tuned for one high-churn table can make quieter tables vacuum too often.&lt;/p&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Public evidence&lt;/th&gt;&lt;th&gt;What it shows&lt;/th&gt;&lt;th&gt;Production lesson&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;GitLab tracked autovacuum and autoanalyze daily counts&lt;/td&gt;&lt;td&gt;Vacuum frequency was measured as an operational signal&lt;/td&gt;&lt;td&gt;Count vacuum cycles per table, not just cluster-wide activity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GitLab compared before and after migration behavior&lt;/td&gt;&lt;td&gt;Configuration changed based on observed workload&lt;/td&gt;&lt;td&gt;Treat autovacuum tuning as capacity testing, not folklore&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GitLab inspected &lt;code&gt;pg_stat_all_table.n_dead_tup&lt;/code&gt; in Prometheus&lt;/td&gt;&lt;td&gt;Dead tuples were tracked over time&lt;/td&gt;&lt;td&gt;Alert on trajectory, not only threshold breach&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GitLab selected candidate tables for custom settings&lt;/td&gt;&lt;td&gt;Large relations needed table-specific policy&lt;/td&gt;&lt;td&gt;Per-table storage parameters are normal for serious PostgreSQL operations&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;This also follows directly from PostgreSQL behavior. &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; leave old row versions behind under MVCC until vacuum can mark space reusable. Standard vacuum does not generally return space to the operating system; it makes space reusable inside the relation. &lt;code&gt;VACUUM FULL&lt;/code&gt; rewrites the table and requires an exclusive lock. That is why waiting until bloat is obvious is expensive: at that point, the fix may require either a long plain vacuum that only stabilizes reuse or a rewrite operation that needs a maintenance window.&lt;/p&gt;
&lt;p&gt;The source incident describes the recognizable operational smell: response time spikes, lock waits, autovacuum visible in &lt;code&gt;pg_stat_activity&lt;/code&gt;, and operators reaching for termination commands. The deeper diagnosis is that the system had no pre-peak signal for cleanup debt. Once users are checking out, workers are busy, indexes are colder, heap pages are dirty, and autovacuum is behind, every option is ugly. The best time to find a bloated &lt;code&gt;orders&lt;/code&gt; table is before the marketing email, not while the payment service is practicing interpretive latency.&lt;/p&gt;
&lt;p&gt;A production vacuum dashboard should make five questions answerable in less than a minute:&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Question&lt;/th&gt;&lt;th&gt;View or metric&lt;/th&gt;&lt;th&gt;Bad signal&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Which tables are accumulating cleanup debt?&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_user_tables.n_dead_tup&lt;/code&gt;, relation size&lt;/td&gt;&lt;td&gt;Dead tuples rising faster than vacuum completion&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Is vacuum running or stalled?&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_progress_vacuum.phase&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Phase unchanged while lock waits or I/O waits climb&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;What is pinning cleanup?&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_activity.backend_xmin&lt;/code&gt;, replication slots&lt;/td&gt;&lt;td&gt;Old snapshot age grows while dead tuples persist&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Are workers saturated?&lt;/td&gt;&lt;td&gt;Active autovacuum workers and table queue&lt;/td&gt;&lt;td&gt;Large relations occupy workers for long periods&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Is the threshold wrong?&lt;/td&gt;&lt;td&gt;Dead tuples at vacuum start and duration&lt;/td&gt;&lt;td&gt;Vacuum starts only after latency or bloat is visible&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Dead tuple percentage looks fine while absolute debt is huge&lt;/td&gt;&lt;td&gt;A 1B-row table with 1 percent dead rows still has 10M obsolete tuples&lt;/td&gt;&lt;td&gt;Alert on absolute &lt;code&gt;n_dead_tup&lt;/code&gt;, dead tuple ratio, and relation size together&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Autovacuum runs but bloat does not fall&lt;/td&gt;&lt;td&gt;Long transaction, prepared transaction, stale replica feedback, or replication slot pins the visibility horizon&lt;/td&gt;&lt;td&gt;Monitor &lt;code&gt;backend_xmin&lt;/code&gt;, &lt;code&gt;backend_xid&lt;/code&gt;, &lt;code&gt;pg_prepared_xacts&lt;/code&gt;, and replication slot lag before changing vacuum cost settings&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Vacuum becomes too aggressive after lowering scale factor&lt;/td&gt;&lt;td&gt;Hot tables vacuum frequently enough to compete with foreground I/O&lt;/td&gt;&lt;td&gt;Tune &lt;code&gt;autovacuum_vacuum_cost_delay&lt;/code&gt;, table thresholds, and worker count under load; verify p95 latency during vacuum&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;VACUUM FULL&lt;/code&gt; becomes the only visible cleanup option&lt;/td&gt;&lt;td&gt;Plain vacuum can reuse space but cannot compact most table files back to the operating system&lt;/td&gt;&lt;td&gt;Prefer steady plain vacuum; reserve &lt;code&gt;VACUUM FULL&lt;/code&gt;, &lt;code&gt;CLUSTER&lt;/code&gt;, or table rewrite for controlled maintenance windows with disk headroom&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Partitioned parent has stale planner statistics&lt;/td&gt;&lt;td&gt;Autovacuum processes partitions, but parent-level statistics may not update as expected&lt;/td&gt;&lt;td&gt;Run explicit &lt;code&gt;ANALYZE&lt;/code&gt; on partitioned parents after load or distribution shifts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Insert-heavy table misses cleanup expectations&lt;/td&gt;&lt;td&gt;PostgreSQL 13 and later include insert-trigger autovacuum settings, but older tuning habits focus only on update and delete churn&lt;/td&gt;&lt;td&gt;Include &lt;code&gt;autovacuum_vacuum_insert_threshold&lt;/code&gt; and &lt;code&gt;autovacuum_vacuum_insert_scale_factor&lt;/code&gt; in version-aware reviews&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Terminating autovacuum becomes the runbook&lt;/td&gt;&lt;td&gt;Operators kill workers during peak traffic and never repay cleanup debt&lt;/td&gt;&lt;td&gt;Require a follow-up manual vacuum, threshold change, or capacity review for every terminated worker&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Managed database hides host-level detail&lt;/td&gt;&lt;td&gt;Amazon RDS, Aurora PostgreSQL, Cloud SQL, or Azure Database for PostgreSQL restrict OS-level inspection&lt;/td&gt;&lt;td&gt;Use SQL-visible signals first: stats views, logs, parameter groups, Performance Insights, and query wait sampling&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Vacuum incidents happen when write throughput creates cleanup debt faster than PostgreSQL can safely remove it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Treat autovacuum as a capacity control plane with table-level metrics, horizon detection, progress visibility, and per-table policy.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: A healthy system shows bounded &lt;code&gt;n_dead_tup&lt;/code&gt;, recent &lt;code&gt;last_autovacuum&lt;/code&gt; on hot tables, short transaction ages, and vacuum progress that completes without sustained lock waits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, build a dashboard for the top 20 write-heavy tables showing dead tuples, relation size, last autovacuum age, oldest transaction age, lock waiters, and active vacuum phase.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Autovacuum does not need heroics; it needs budget, observability, and the dignity of being treated like production capacity before it collects payment at the worst possible hour.&lt;/p&gt;</content:encoded><category>databases</category><category>failures</category><category>checklist</category></item><item><title>Stop Guessing: Solving Invisible Latency with the MySQL Performance Schema</title><link>https://rajivonai.com/blog/2025-09-10-mysql-performance-schema-metadata-locks-invisible-latency/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-09-10-mysql-performance-schema-metadata-locks-invisible-latency/</guid><description>How to move from trial-and-error database tuning to mathematical proof using the underutilized MySQL Performance Schema.</description><pubDate>Wed, 10 Sep 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The hardest database incidents to resolve are the ones where all the standard dashboards show green. CPU is at 20%, memory is stable, and the slow query log is completely empty. Yet, the application is throwing 504 Gateway Timeouts and users cannot log in.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most engineering teams rely on a standard set of metrics to monitor MySQL: CPU utilization, active connections, and the slow query log.&lt;/p&gt;
&lt;p&gt;This works perfectly when the problem is a missing index causing a query to scan 10 million rows, driving CPU to 100% and logging a 15-second execution time.&lt;/p&gt;
&lt;p&gt;But modern distributed architectures rarely fail that cleanly. They fail due to contention. When 5,000 fast queries suddenly block each other trying to acquire a lock, the individual execution time of each query might still be under the &lt;code&gt;long_query_time&lt;/code&gt; threshold. The CPU drops to near-zero because the threads aren’t doing work; they are sleeping, waiting in a queue.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;When faced with invisible latency, a “guessing culture” takes over. Engineers will arbitrarily increase the &lt;code&gt;innodb_buffer_pool_size&lt;/code&gt;, reboot the database, or throw larger EC2 instance types at the problem. If the problem vanishes, they claim victory, entirely unaware of what actually happened.&lt;/p&gt;
&lt;p&gt;The MySQL Performance Schema was built specifically to solve this. It instruments the internal execution of the database engine, tracking exactly how long threads spend waiting on mutexes, file I/O, or metadata locks.&lt;/p&gt;
&lt;p&gt;Despite its power, it is rarely used. It has a reputation for being complicated, overwhelming, and carrying a heavy performance overhead.&lt;/p&gt;
&lt;p&gt;The core question is: how can a Staff Engineer cut through the noise of the Performance Schema to mathematically prove the root cause of invisible latency?&lt;/p&gt;
&lt;h2 id=&quot;the-invisible-latency-diagnostics-flow&quot;&gt;The Invisible Latency Diagnostics Flow&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Application Timeout Spike] --&gt; B{CPU is high?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes| C[Check Slow Query Log / APM]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No| D{Active Connections spiking?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Yes| E[Query Performance Schema]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[events_waits_history_long]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Identify Metadata Locks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; H[Identify Mutex Contention]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; I[Kill blocking DDL or Backup Job]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; J[Tune Internal Engine Parameters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for navigating the Performance Schema is to rely on the &lt;code&gt;sys&lt;/code&gt; schema views, which aggregate the raw internal tables into human-readable summaries.&lt;/p&gt;
&lt;p&gt;When diagnosing invisible latency, the definitive proof lies in &lt;code&gt;events_waits_history_long&lt;/code&gt;. For example, if a developer runs an &lt;code&gt;ALTER TABLE&lt;/code&gt; on a live production table, MySQL must acquire a Metadata Lock. If a long-running &lt;code&gt;SELECT&lt;/code&gt; query is already reading that table, the &lt;code&gt;ALTER TABLE&lt;/code&gt; blocks. Crucially, every subsequent fast &lt;code&gt;SELECT&lt;/code&gt; query hitting that table will also queue up behind the &lt;code&gt;ALTER TABLE&lt;/code&gt;, waiting for the Metadata Lock.&lt;/p&gt;
&lt;p&gt;The slow query log will show nothing until the lock is released. But querying the Performance Schema will immediately reveal hundreds of threads in the &lt;code&gt;Waiting for table metadata lock&lt;/code&gt; state, pinpointing the exact thread ID that holds the lock.&lt;/p&gt;
&lt;p&gt;Beyond locking, the Performance Schema is the ultimate tool for verifying index usage. By querying &lt;code&gt;table_io_waits_summary_by_index_usage&lt;/code&gt;, an engineer can see every index on a table and its exact read vs. write count. If an index has 5 million writes and zero reads over a 30-day period, you have mathematical proof that the index is dead weight. You can drop it safely, instantly reducing your Write IOPS and storage costs.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Diagnostic Approach&lt;/th&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Relying purely on Slow Query Log&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Misses fast queries that execute millions of times, creating aggregate CPU exhaustion.&lt;/td&gt;&lt;td&gt;Use &lt;code&gt;events_statements_summary_by_digest&lt;/code&gt; to find high-frequency, low-latency queries.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Enabling all instrumentation&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;The Performance Schema consumes excessive RAM and CPU overhead, slowing down production.&lt;/td&gt;&lt;td&gt;Use default instrumentation; only enable specific deep wait events iteratively when actively debugging.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Ignoring the &lt;code&gt;sys&lt;/code&gt; schema&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Engineers get lost joining raw &lt;code&gt;setup_instruments&lt;/code&gt; and &lt;code&gt;events_waits&lt;/code&gt; tables.&lt;/td&gt;&lt;td&gt;Always query the &lt;code&gt;sys&lt;/code&gt; schema views (e.g., &lt;code&gt;sys.innodb_lock_waits&lt;/code&gt;) for actionable data.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Blindly adding indexes&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Increases write latency and storage costs with no guarantee of usage.&lt;/td&gt;&lt;td&gt;Verify index utilization via &lt;code&gt;table_io_waits_summary_by_index_usage&lt;/code&gt; before and after adding.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Standard database monitoring fails to detect internal engine contention, metadata locking, and unused index overhead, leading to “guessing” during outages.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Utilize the MySQL Performance Schema and its accompanying &lt;code&gt;sys&lt;/code&gt; schema to measure wait events and index utilization mathematically.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; &lt;code&gt;events_waits_history_long&lt;/code&gt; and &lt;code&gt;table_io_waits_summary_by_index_usage&lt;/code&gt; are documented MySQL internals tables that expose exact wait times and per-index I/O counts, independent of application-level logging — you can verify the mechanism yourself against MySQL’s own reference documentation rather than taking it on authority.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Connect to your production database today and run &lt;code&gt;SELECT * FROM sys.schema_unused_indexes;&lt;/code&gt;. Identify one index with zero reads and high writes, open a PR to drop it, and monitor the drop in Write IOPS.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category><category>cloud</category></item><item><title>Cassandra Node Lifecycle Operations: Bootstrap, Replace, Decommission, and RemoveNode</title><link>https://rajivonai.com/blog/2025-08-25-cassandra-node-lifecycle-bootstrap-replace-decommission/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-08-25-cassandra-node-lifecycle-bootstrap-replace-decommission/</guid><description>Understanding the difference between Apache Cassandra node lifecycle commands to prevent token confusion and stranded data when EC2 instances fail.</description><pubDate>Mon, 25 Aug 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In a cloud environment, hardware failure is guaranteed. EC2 instances degrade, underlying EBS volumes stall, and hypervisors crash.&lt;/p&gt;
&lt;p&gt;When a Cassandra node dies, the cluster continues serving traffic using the remaining replicas. However, restoring the cluster to full health requires human or automated intervention. If an operator uses the wrong lifecycle command to remove or replace that node, they can permanently strand data, duplicate tokens, and corrupt the cluster topology.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Between 2022 and 2024, the 54-node cluster experienced multiple AWS EC2 hardware degradation events.&lt;/p&gt;
&lt;p&gt;During one incident, an instance became completely unresponsive and had to be physically terminated via the AWS Console. An operator attempting to clean up the cluster ran &lt;code&gt;nodetool decommission&lt;/code&gt; from a different node. It failed. The operator then tried to bootstrap a new node with the exact same IP. Gossip fractured, tokens became confused, and data was stranded.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra is extremely protective of its data. It relies on a deterministic token ring to map data to physical nodes. You cannot simply delete a node from the cluster; you must mathematically reassign its tokens.&lt;/p&gt;
&lt;p&gt;Operators frequently confuse the four distinct lifecycle operations:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Bootstrap:&lt;/strong&gt; Adding a &lt;em&gt;net-new&lt;/em&gt; node to the cluster. The new node calculates random tokens, joins the ring, and forces existing nodes to stream data to it. The ring size increases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Decommission:&lt;/strong&gt; Safely removing a &lt;em&gt;healthy&lt;/em&gt; node. The node gracefully streams all its data to its peers, relinquishes its tokens, and exits the ring cleanly. The ring size decreases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RemoveNode:&lt;/strong&gt; Forcibly removing a &lt;em&gt;dead&lt;/em&gt; node. Because the dead node itself cannot participate, the cluster reassigns its tokens to the remaining healthy nodes and streams the data for those ranges from the surviving replicas as part of the &lt;code&gt;removenode&lt;/code&gt; operation itself. (A follow-up repair is still recommended, since that streaming draws from whichever replicas are available rather than guaranteeing full consistency across all of them).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replace:&lt;/strong&gt; Replacing a &lt;em&gt;dead&lt;/em&gt; node with a &lt;em&gt;new&lt;/em&gt; node. The new instance boots up, explicitly claims the exact tokens of the dead node, and streams the necessary data from the surviving replicas.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If an instance is physically dead, you cannot &lt;code&gt;decommission&lt;/code&gt; it, because it cannot run the streaming process.&lt;/p&gt;
&lt;h2 id=&quot;the-node-lifecycle-architecture&quot;&gt;The Node Lifecycle Architecture&lt;/h2&gt;
&lt;p&gt;The pattern standardizes the operational runbooks around a strict decision tree to ensure the correct command was executed based on the physical state of the hardware.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Incident[Hardware Issue Detected] --&gt; Health{Is the Node Alive?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Health --&gt;|Yes - Graceful Downscale| Decom[Run nodetool decommission — Node streams data and exits]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Health --&gt;|No - Dead Hardware| Replace{Provisioning Replacement?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Replace --&gt;|Yes - Keep Ring Size| ReplaceCmd[Boot new EC2 with: — cassandra.replace_address_first_boot]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Replace --&gt;|No - Permanent Shrink| RemoveCmd[Run nodetool removenode — from a healthy node]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;replacing-a-dead-ec2-instance&quot;&gt;Replacing a Dead EC2 Instance&lt;/h3&gt;
&lt;p&gt;When AWS retired an instance, the automation provisioned a new EC2 instance in the exact same Availability Zone. Crucially, we passed a specific JVM flag via EC2 User-Data during the initial startup:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;-Dcassandra.replace_address_first_boot=&amp;#x3C;Dead-Node-IP&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This tells the new Cassandra process: “Do not generate new tokens. Find the dead node with this IP, take over its exact position on the ring, and stream its data from the surviving replicas.”&lt;/p&gt;
&lt;h3 id=&quot;scaling-down--removing-dead-nodes&quot;&gt;Scaling Down / Removing Dead Nodes&lt;/h3&gt;
&lt;p&gt;If we decided not to replace the dead node (permanently shrinking the cluster), we SSH’d into a &lt;em&gt;healthy&lt;/em&gt; node and executed:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;nodetool removenode &amp;#x3C;Host-ID-of-Dead-Node&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This evicts the dead node’s tokens from gossip, reassigns them to the remaining healthy nodes, and streams the affected ranges from the surviving replicas as the removal runs (&lt;code&gt;system.peers&lt;/code&gt; simply reflects the resulting gossip state to clients). After the removal completes, we still run &lt;code&gt;nodetool repair&lt;/code&gt; to guarantee full replica consistency on the new token owners.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;We automated the replacement workflow using an auto-remediation daemon. The daemon listened for AWS EC2 termination notices via EventBridge. When a node was marked for retirement, the daemon provisioned a replacement instance, injected the &lt;code&gt;-Dcassandra.replace_address_first_boot&lt;/code&gt; flag with the dead node’s IP into the user-data, and attached the configuration.&lt;/p&gt;
&lt;p&gt;Because we automated the specific JVM flag, we eliminated the human error of accidentally bootstrapping a new node and shifting the entire token ring when we simply wanted a 1-for-1 hardware replacement.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Bootstrapping without &lt;code&gt;replace_address&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;If you boot a new node to replace a dead one without the flag, the new node generates new tokens. The dead node’s tokens remain in gossip forever.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce strict replacement parameters in infrastructure-as-code automation.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Running &lt;code&gt;removenode&lt;/code&gt; on an active node&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;If the node is actually alive (just network partitioned) and you force remove it, the cluster reassigns its tokens to other nodes while the original node still believes it owns them — once the partition heals, both sides accept conflicting writes for the same ranges.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Never run &lt;code&gt;removenode&lt;/code&gt; unless the EC2 instance is verifiably terminated in the AWS console.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Failing Replacement&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;If a replacement node fails mid-stream, it leaves the cluster in a half-replaced state.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Terminate the failed instance, wipe the new EBS volume, and restart the replacement process from scratch.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-aws-auto-recovery&quot;&gt;Modern Cassandra Context: AWS Auto-Recovery&lt;/h3&gt;
&lt;p&gt;Modern AWS EC2 Auto-Recovery integration has slightly shifted this operational burden. By configuring EC2 Auto-Recovery, AWS will automatically reboot a degraded instance onto healthy underlying hardware while retaining the exact same IP address and EBS volumes. If the EBS volumes survive the degradation, Cassandra simply boots up normally, completely bypassing the need for a full token replacement stream.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Operators execute the wrong Cassandra lifecycle commands during hardware failures, stranding data and fracturing the token ring.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Strictly align commands to hardware state: use &lt;code&gt;decommission&lt;/code&gt; for healthy scale-down, &lt;code&gt;replace_address&lt;/code&gt; for 1-for-1 hardware swaps, and &lt;code&gt;removenode&lt;/code&gt; for permanent removal of dead hardware.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Injecting the &lt;code&gt;replace_address_first_boot&lt;/code&gt; flag automates hardware replacement without triggering massive, cluster-wide token rebalancing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run &lt;code&gt;nodetool status&lt;/code&gt;. If you see nodes marked &lt;code&gt;DN&lt;/code&gt; (Down/Normal) that have been physically terminated for days, you must execute &lt;code&gt;nodetool removenode&lt;/code&gt; to heal the cluster ring.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>checklist</category></item><item><title>Upgrading a 90-Node Cassandra Cluster from 4.1 to 5.0</title><link>https://rajivonai.com/blog/2025-08-15-upgrading-90-node-cassandra-4-1-to-5-0/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-08-15-upgrading-90-node-cassandra-4-1-to-5-0/</guid><description>How to orchestrate a zero-downtime major version upgrade from Cassandra 4.1 to 5.0 across 90 nodes, decoupling the database binary upgrade from the Java 17 transition.</description><pubDate>Fri, 15 Aug 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A single misordered step in a major-version Cassandra upgrade can leave half a 90-node cluster unable to parse the other half’s schema, or make an on-disk format change that no longer accepts a rollback. That is the actual risk profile of moving a 90-node multi-region cluster (managing ~100 TB of data) from Apache Cassandra 4.1 to 5.0 — not whether the new release is worth adopting.&lt;/p&gt;
&lt;p&gt;In 2024, this cluster needed to make that move. Cassandra 5.0 brings real architectural gains — Unified Compaction Strategy (UCS), Storage-Attached Indexing (SAI), native JDK 17 support — but unlocking any of them first required navigating a dangerous mixed-mode operational state without downtime.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;We were faced with a complex matrix of upgrade requirements. Operators must upgrade the database binary (4.1 to 5.0), upgrade the JVM runtime (OpenJDK 11 to OpenJDK 17), and evolve the physical SSTable format on disk—all with zero downtime and no client errors.&lt;/p&gt;
&lt;p&gt;A poorly orchestrated major upgrade triggers schema agreement failures, JVM crash loops, driver disconnection storms, and catastrophic read-path latency. We needed an orchestration sequence that decoupled risk.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra is designed for rolling upgrades: older and newer nodes can coexist, gossip together, and continue serving reads/writes without a coordinated cutover. This state is called &lt;strong&gt;mixed-mode&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;However, if an operator attempts to enable Cassandra 5.0-only features (like creating an SAI index or switching to UCS) while the cluster is still in mixed-mode, the 4.1 nodes will instantly fail to parse the schema or the new file structures.&lt;/p&gt;
&lt;p&gt;Furthermore, upgrading both the Cassandra binary and the JVM runtime simultaneously introduces severe risk. If a node fails to boot, the operator doesn’t know if the failure was caused by the database binary upgrade or missing JDK 17 module encapsulation flags.&lt;/p&gt;
&lt;p&gt;Finally, once Cassandra writes data in the 5.0 format (&lt;code&gt;bti&lt;/code&gt; or &lt;code&gt;5.0 big&lt;/code&gt;), online rollback becomes functionally impossible. &lt;code&gt;nodetool upgradesstables&lt;/code&gt; is an irreversible checkpoint.&lt;/p&gt;
&lt;h2 id=&quot;the-decoupled-upgrade-architecture&quot;&gt;The Decoupled Upgrade Architecture&lt;/h2&gt;
&lt;p&gt;To mitigate these risks, the pattern architects a decoupled major upgrade sequence. We separated the database upgrade from the runtime upgrade, and separated the binary deployment from the feature enablement.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Start[Current: C* 4.1 / JDK 11] --&gt; Stage1[Stage 1: Upgrade Drivers &amp;#x26; Test]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Stage1 --&gt; Stage2[Stage 2: Single 5.0 Canary Node — in Non-Critical DC]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Stage2 --&gt; Stage3[Stage 3: Rolling Upgrade across 90 Nodes — Keep JDK 11]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Stage3 --&gt; Wait{Wait for Cluster Stability — &amp;#x26; Schema Agreement}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Wait --&gt; Stage4[Stage 4: nodetool upgradesstables — Irreversible Checkpoint]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Stage4 --&gt; Stage5[Stage 5: Enable 5.0 Features — UCS, SAI]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Stage5 --&gt; Stage6[Stage 6: Rolling JVM Upgrade — JDK 11 to JDK 17]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-the-lower-risk-decoupled-path&quot;&gt;1. The Lower-Risk Decoupled Path&lt;/h3&gt;
&lt;p&gt;Cassandra 5.0 natively supports both Java 11 and Java 17. Rather than doing a combined “big bang” upgrade, we kept the cluster on Java 11 during the 5.0 binary rollout. This meant we could reuse the battle-tested &lt;code&gt;jvm11-server.options&lt;/code&gt; without risking Java 17 JVM flag compatibility issues (like strict JMX encapsulation). Once the cluster was stable on Cassandra 5.0 for a week, we orchestrated a separate rolling restart to upgrade the runtime to Java 17.&lt;/p&gt;
&lt;h3 id=&quot;2-the-canary-strategy&quot;&gt;2. The Canary Strategy&lt;/h3&gt;
&lt;p&gt;Before touching the primary nodes, we upgraded a single node in a non-critical rack. We let it serve reads and writes for 48 hours to ensure the monitoring agents, backup scripts, and metrics exporters didn’t break under the new 5.0 JMX metrics format.&lt;/p&gt;
&lt;h3 id=&quot;3-sstable-upgrades-nodetool-upgradesstables&quot;&gt;3. SSTable Upgrades (&lt;code&gt;nodetool upgradesstables&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;When a node is upgraded to 5.0, it can still read 4.x SSTables, but reading legacy tables invokes a translation penalty that increases CPU usage. Once all 90 nodes were on 5.0 and schema agreed (&lt;code&gt;cqlsh -e &quot;SELECT schema_version FROM system.local;&quot;&lt;/code&gt;), the playbook runs a background script to execute &lt;code&gt;nodetool upgradesstables&lt;/code&gt; sequentially across the cluster. This rewrote the 100 TB of data into the new 5.0 format.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;We wrote an orchestration pipeline to manage the rolling restarts rack-by-rack across the two AWS regions.&lt;/p&gt;
&lt;p&gt;We heavily monitored the driver reconnection rates and CPU utilization during the mixed-mode transition. Cassandra 5.0 provides storage compatibility modes specifically designed to make this smooth.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Crucially&lt;/strong&gt;, the runbooks explicitly mandated taking pre-upgrade snapshots (&lt;code&gt;nodetool snapshot&lt;/code&gt;) before initiating &lt;code&gt;nodetool upgradesstables&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If a node crashed while simply running the 5.0 binary (before SSTables were rewritten), we could safely downgrade the binary back to 4.1 and restart. However, if the crash occurred &lt;em&gt;after&lt;/em&gt; rewriting the SSTables, the 4.1 binary would fail to read the new 5.0 files on disk. The only safe recovery in that scenario is wiping the data directory and restoring the pre-upgrade snapshot.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Combined JDK 17 Upgrade&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Upgrading C* 5.0 and JDK 17 simultaneously is faster but risks JVM flag crashes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Decouple the runtime upgrade. Stick to JDK 11 until the 5.0 binary is stable.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Skipping &lt;code&gt;upgradesstables&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Not rewriting SSTables saves disk I/O, but leaves a permanent CPU penalty on the read path.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Automate &lt;code&gt;nodetool upgradesstables&lt;/code&gt; via a low-priority background script.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Enabling Features Too Early&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Changing a keyspace to UCS before all 90 nodes are on 5.0 will instantly crash 4.1 node gossip and schema agreement.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Gate feature enablement behind a strict script that verifies &lt;code&gt;nodetool version&lt;/code&gt; across 100% of the ring.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-long-term-50-performance&quot;&gt;Modern Cassandra Context: Long-Term 5.0 Performance&lt;/h3&gt;
&lt;p&gt;In the post-upgrade operational review, the combination of Cassandra 5.0 running on JDK 17 (utilizing ZGC/Shenandoah and Vector API optimizations) deployed on AWS Graviton3 instances produced a meaningful price-performance improvement — the exact magnitude depends heavily on workload shape and table-by-table UCS/SAI adoption, so treat any single number here as illustrative rather than a benchmark to plan around. By decoupling the upgrade, we safely reached this architecture with zero dropped client mutations.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Upgrading 90 nodes across major database and JVM versions carries massive risk of downtime, schema forks, and irreversible file corruption.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Decouple the binary upgrade from the JVM upgrade. Use a canary, validate mixed-mode stability, and run &lt;code&gt;nodetool upgradesstables&lt;/code&gt; only when the entire ring is ready.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The decoupled sequence allowed us to halt and investigate monitoring agent issues on JDK 11 without compounding the risk with JDK 17 incompatibilities.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Check your &lt;code&gt;system.local&lt;/code&gt; schema versions. Do not execute a single &lt;code&gt;ALTER KEYSPACE&lt;/code&gt; command for 5.0 features until every node reports the exact same schema hash.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>checklist</category></item><item><title>Automating Cassandra Patch Upgrades Across Large Clusters</title><link>https://rajivonai.com/blog/2025-08-05-automating-cassandra-patch-upgrades-ansible/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-08-05-automating-cassandra-patch-upgrades-ansible/</guid><description>How to engineer a zero-downtime rolling patch upgrade pipeline for Apache Cassandra using Ansible, strict pre-flight gates, and safe connection draining.</description><pubDate>Tue, 05 Aug 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A security team flags a critical CVE in the JVM, or the Apache Cassandra project releases a stability patch (e.g., 4.1.3 → 4.1.4). You have 90 nodes spanning 3 Availability Zones.&lt;/p&gt;
&lt;p&gt;If an operator manually SSHs into nodes to upgrade the packages, the process will take three weeks. Worse, a single missed command during the restart sequence will cause native transport drops, gossip flapping, and client-facing outages.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Between 2023 and 2025, the 90-node cluster struggled with patch management. Manual upgrades were slow and error-prone.&lt;/p&gt;
&lt;p&gt;During one manual upgrade, an engineer stopped the Cassandra daemon without running &lt;code&gt;nodetool drain&lt;/code&gt;. When the upgraded node restarted, it had to replay gigabytes of un-flushed commitlogs, delaying startup by 20 minutes. Meanwhile, the load balancer continued routing traffic to the node before it had rejoined the gossip ring, causing elevated client timeouts. Because the process was so painful, the cluster routinely ran mixed patch versions for weeks, increasing operational risk.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra is designed for zero-downtime rolling upgrades, but the orchestration logic is entirely the operator’s responsibility.&lt;/p&gt;
&lt;p&gt;To safely upgrade a node without dropping a single client mutation, you must sequentially execute a strict state machine:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify the cluster is 100% healthy (no pending compactions, full schema agreement).&lt;/li&gt;
&lt;li&gt;Stop the node from accepting new client connections.&lt;/li&gt;
&lt;li&gt;Stop the node from receiving peer traffic (gossip).&lt;/li&gt;
&lt;li&gt;Flush all memory to disk.&lt;/li&gt;
&lt;li&gt;Upgrade the binary.&lt;/li&gt;
&lt;li&gt;Restart and wait for the node to fully sync before touching the next node.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If you automate this with a naive bash script that just runs &lt;code&gt;yum update &amp;#x26;&amp;#x26; systemctl restart cassandra&lt;/code&gt; across all nodes simultaneously, you will destroy the cluster quorum and cause a massive data outage.&lt;/p&gt;
&lt;h2 id=&quot;the-rolling-upgrade-architecture&quot;&gt;The Rolling Upgrade Architecture&lt;/h2&gt;
&lt;p&gt;The pattern replaces manual runbooks with a deterministic Ansible state machine that enforced strict concurrency controls: exactly &lt;strong&gt;one node per rack/AZ at a time&lt;/strong&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Start[Select Node] --&gt; Gate{Pre-flight Gate — Is Cluster Healthy?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Gate --&gt;|No| Abort[Abort Playbook]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Gate --&gt;|Yes| DisableBin[nodetool disablebinary — Stop Client Traffic]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DisableBin --&gt; DisableGossip[nodetool disablegossip — Stop Peer Traffic]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DisableGossip --&gt; Drain[nodetool drain — Flush Memtables to Disk]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Drain --&gt; Stop[systemctl stop cassandra]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Stop --&gt; Pkg[Install Patched RPM/DEB]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Pkg --&gt; StartC[systemctl start cassandra]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    StartC --&gt; WaitGossip{Wait for Gossip — &amp;#x26; Schema Agreement}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WaitGossip --&gt;|Success| Next[Move to Next Node]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-connection-draining&quot;&gt;1. Connection Draining&lt;/h3&gt;
&lt;p&gt;Before touching the package manager, Ansible executes &lt;code&gt;nodetool disablebinary&lt;/code&gt; to cleanly sever client connections. Then it executes &lt;code&gt;nodetool disablegossip&lt;/code&gt; so peer nodes stop routing requests to it. Finally, &lt;code&gt;nodetool drain&lt;/code&gt; flushes the memtables to SSTables. When the daemon is stopped, there is zero data in memory, meaning startup will be instant without commitlog replay.&lt;/p&gt;
&lt;h3 id=&quot;2-automated-validation-gates&quot;&gt;2. Automated Validation Gates&lt;/h3&gt;
&lt;p&gt;Once the upgraded node restarts, Ansible does not simply move to the next host. It pauses and queries JMX. It verifies that &lt;code&gt;nodetool status&lt;/code&gt; shows the node as &lt;code&gt;UN&lt;/code&gt; (Up/Normal), and executes &lt;code&gt;cqlsh -e &quot;SELECT release_version FROM system.local;&quot;&lt;/code&gt; to confirm the new patch version is running. Most importantly, it verifies schema agreement across the entire ring.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Our &lt;code&gt;cassandra_rolling_upgrade.yml&lt;/code&gt; Ansible playbook became the backbone of the operational security.&lt;/p&gt;
&lt;p&gt;Because the playbook was entirely idempotent and validated cluster health at every step, we could trigger an upgrade across 90 nodes and walk away. If a node failed to restart (e.g., due to a corrupted package download), the playbook aborted immediately, leaving 89 nodes healthy and maintaining complete data availability.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Upgrading Concurrently&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Upgrading 10 nodes at once saves time but risks taking an entire replica set offline, causing data unavailability.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Strictly enforce Ansible &lt;code&gt;serial: 1&lt;/code&gt; per rack/fault domain.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Skipping &lt;code&gt;nodetool drain&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bypassing the drain forces commitlog replay on startup, extending downtime and risking mutation loss if the OS forcefully kills the process.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Make &lt;code&gt;nodetool drain&lt;/code&gt; a mandatory, non-skippable task in the playbook.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Pending Schema Migrations&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;If a developer runs an &lt;code&gt;ALTER TABLE&lt;/code&gt; while the rolling upgrade is in progress, nodes will fork their schema hashes and crash.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Lock CI/CD schema pipelines while the upgrade playbook is running.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-aws-systems-manager-ssm-automation&quot;&gt;Modern Cassandra Context: AWS Systems Manager (SSM) Automation&lt;/h3&gt;
&lt;p&gt;While Ansible remains the industry standard, modern AWS-native architectures increasingly rely on AWS Systems Manager (SSM) State Manager to execute these exact rolling patching strategies. SSM integrates directly with EC2 Auto Scaling Groups to cycle Cassandra instances onto patched AMIs, but the same rolling, one-fault-domain-at-a-time discipline still applies — the immutable infrastructure paradigm doesn’t remove the need to drain and validate each node before replacing it.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Manual Cassandra patching causes downtime, commitlog replays, and human errors on large clusters.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Automate a deterministic rolling upgrade pipeline that isolates nodes via &lt;code&gt;disablebinary&lt;/code&gt; and &lt;code&gt;drain&lt;/code&gt; before swapping packages.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Enforcing pre-flight health gates and post-restart schema validation allows zero-downtime CVE patching across 90 nodes without manual operator intervention.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Open your current patching script. If it does not contain &lt;code&gt;nodetool drain&lt;/code&gt;, you are restarting nodes unsafely.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>checklist</category></item><item><title>Cassandra Backup and Restore on EC2: Snapshots, Incrementals, and S3</title><link>https://rajivonai.com/blog/2025-07-21-cassandra-backup-restore-ec2-s3-snapshots/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-07-21-cassandra-backup-restore-ec2-s3-snapshots/</guid><description>How Apache Cassandra backup mechanics actually work on AWS, from atomic on-disk hardlinks to S3 archiving and zero-downtime disaster recovery.</description><pubDate>Mon, 21 Jul 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A backup strategy that has never been restored is not a backup strategy—it is a hope strategy.&lt;/p&gt;
&lt;p&gt;In Cassandra, because data is distributed across dozens of nodes, the concept of a “backup” is fundamentally different from taking an RDS snapshot. If you don’t understand how Cassandra manages file pointers, your backups will either silently fail or quietly fill up your disk until the cluster crashes.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In 2023, a developer running a migration script accidentally executed &lt;code&gt;DROP TABLE&lt;/code&gt; on a critical production table in the 36-node cluster (housing ~35 TB of data).&lt;/p&gt;
&lt;p&gt;When the infrastructure team attempted to restore the data, they discovered three fatal flaws:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;They had no automated schema backups, meaning they didn’t know the exact DDL to recreate the table.&lt;/li&gt;
&lt;li&gt;The snapshots were sitting locally on the EC2 instances. Half the instances were out of disk space because historical snapshots were never cleared.&lt;/li&gt;
&lt;li&gt;The restore runbook estimated 72 hours for a full recovery because it relied on running &lt;code&gt;sstableloader&lt;/code&gt; over a slow network link for the entire 35 TB dataset.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra backups do not copy data. When you run &lt;code&gt;nodetool snapshot&lt;/code&gt;, Cassandra simply flushes the memtables to disk and creates a &lt;strong&gt;hardlink&lt;/strong&gt; in the &lt;code&gt;snapshots/&lt;/code&gt; directory pointing to the existing SSTables.&lt;/p&gt;
&lt;p&gt;This operation is instantaneous and atomic. However, because it is a hardlink, the backup only exists on the local EC2 disk. If the EC2 instance is terminated, the backup is gone. Furthermore, if you never run &lt;code&gt;nodetool clearsnapshot&lt;/code&gt;, those hardlinks prevent the OS from reclaiming disk space when Cassandra compacts and deletes the original SSTable. The disk slowly fills to 100%.&lt;/p&gt;
&lt;p&gt;To survive an AWS region failure or a dropped table, you must physically move those SSTable files off the EC2 instance to highly durable object storage (AWS S3) and capture the schema agreement.&lt;/p&gt;
&lt;h2 id=&quot;the-s3-backup-and-restore-architecture&quot;&gt;The S3 Backup and Restore Architecture&lt;/h2&gt;
&lt;p&gt;The documented pattern is to engineer an automated Backup &amp;#x26; Restore pipeline to guarantee strict RPO (Recovery Point Objective) and RTO (Recovery Time Objective) SLAs.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Cass[Cassandra Node] --&gt;|nodetool snapshot| Link[Local Disk — Hardlinks created]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Cass --&gt;|DESCRIBE KEYSPACE| Schema[Schema DDL Capture]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Link --&gt; Agent(Backup Agent / Medusa)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Schema --&gt; Agent&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Agent --&gt;|Throttled Upload| S3[(AWS S3 — Archival Storage)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    S3 -.-&gt;|sstableloader| Remote[Cross-Topology Restore]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    S3 -.-&gt;|nodetool import| Local[In-Place Fast Restore]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Agent --&gt;|nodetool clearsnapshot| Purge[Purge Local Hardlinks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-atomic-snapshots-and-s3-offload&quot;&gt;1. Atomic Snapshots and S3 Offload&lt;/h3&gt;
&lt;p&gt;A standard pattern configures the nodes to run daily atomic snapshots. Immediately after the snapshot, a Python backup agent (we eventually adopted open-source Medusa) throttled the upload of those SSTables to an S3 bucket to prevent network saturation. Crucially, the agent automatically exported the schema via &lt;code&gt;cqlsh -e &quot;DESCRIBE KEYSPACE&quot;&lt;/code&gt; and uploaded it alongside the data.&lt;/p&gt;
&lt;h3 id=&quot;2-incremental-backups&quot;&gt;2. Incremental Backups&lt;/h3&gt;
&lt;p&gt;To reduce the RPO from 24 hours to 1 hour, we enabled &lt;code&gt;incremental_backups: true&lt;/code&gt; in &lt;code&gt;cassandra.yaml&lt;/code&gt;. This forces Cassandra to hardlink every newly flushed SSTable into a &lt;code&gt;backups/&lt;/code&gt; directory immediately. The agent continuously synced this directory to S3, giving us point-in-time recovery capabilities.&lt;/p&gt;
&lt;h3 id=&quot;3-the-restore-paths&quot;&gt;3. The Restore Paths&lt;/h3&gt;
&lt;p&gt;If we lost a single node, we didn’t use &lt;code&gt;sstableloader&lt;/code&gt;. The documented pattern uses the zero-downtime &lt;strong&gt;In-Place Restore&lt;/strong&gt;. We copied the files from S3 directly into the new node’s data directory and ran &lt;code&gt;nodetool import &amp;#x3C;keyspace&gt; &amp;#x3C;table&gt; /path/to/sstables&lt;/code&gt;. This loads the SSTables directly into the live storage engine via local disk, without the network streaming overhead &lt;code&gt;sstableloader&lt;/code&gt; incurs.&lt;/p&gt;
&lt;p&gt;We only used &lt;code&gt;sstableloader&lt;/code&gt; if we were restoring the data into a cluster with a completely different token ring topology (e.g., migrating from a 36-node cluster to an 18-node cluster).&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The most critical part of this architecture was the automated DR (Disaster Recovery) validation drill.&lt;/p&gt;
&lt;p&gt;Every month, an automated pipeline spun up a sandboxed, single-node Cassandra cluster in a separate AWS account. It pulled the latest production schema from S3, recreated the keyspaces, downloaded a subset of the SSTables, and ran &lt;code&gt;nodetool import&lt;/code&gt;. A script then ran a &lt;code&gt;SELECT&lt;/code&gt; query to verify data integrity. If the query failed, the backup pipeline was immediately flagged as broken.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Forgetting &lt;code&gt;clearsnapshot&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Hardlinks prevent the OS from freeing disk space. The disk will fill to 100% silently.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Ensure your backup agent always runs &lt;code&gt;nodetool clearsnapshot&lt;/code&gt; after a successful S3 upload.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Restoring without Schema&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;SSTables are useless binaries if you don’t know the partition key and clustering column definitions.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Always back up the DDL schema in the exact same S3 prefix as the snapshot data.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;sstableloader on Live Clusters&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;sstableloader&lt;/code&gt; forces the receiving nodes to stream data through the network, destroying cluster performance.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use &lt;code&gt;nodetool import&lt;/code&gt; (or &lt;code&gt;nodetool refresh&lt;/code&gt; in older versions) for fast, local disk loading.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-and-backup-tooling&quot;&gt;Modern Cassandra Context: Cassandra 5.0 and Backup Tooling&lt;/h3&gt;
&lt;p&gt;Cassandra’s virtual tables (&lt;code&gt;system_views&lt;/code&gt;, since 4.0) expose operational state — caches, disk usage, SSTable tasks — as queryable CQL rows, but there is no dedicated snapshots virtual table as of 5.0; &lt;code&gt;nodetool listsnapshots&lt;/code&gt; remains the way to inspect snapshot state. While AWS offers EBS-level snapshots, application-consistent Cassandra backups still require database-aware orchestration. Tools like Medusa remain the industry standard for bridging Cassandra’s distributed hardlinks with cloud-native S3 object storage.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Relying on raw &lt;code&gt;nodetool snapshot&lt;/code&gt; leaves data vulnerable to EC2 termination and silently exhausts disk capacity via orphaned hardlinks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Deploy an agent (like Medusa) to offload snapshots and incrementals to S3, capture schema DDL, and automate the &lt;code&gt;clearsnapshot&lt;/code&gt; purge.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Monthly automated DR restore drills guarantee that the S3 artifacts can actually be imported and queried, proving an RTO of minutes rather than days.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run &lt;code&gt;nodetool clearsnapshot&lt;/code&gt; on your cluster right now. Then check your disk monitoring to see how many gigabytes you just recovered.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>checklist</category></item><item><title>AlloyDB vs Amazon Aurora PostgreSQL: A Workload-First Architecture Comparison</title><link>https://rajivonai.com/blog/2025-07-15-alloydb-vs-amazon-aurora-postgresql/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-07-15-alloydb-vs-amazon-aurora-postgresql/</guid><description>A source-backed comparison of AlloyDB and Aurora PostgreSQL across storage, HA, reads, analytics, scaling, and operational fit.</description><pubDate>Tue, 15 Jul 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;AlloyDB and Aurora PostgreSQL both separate compute from distributed storage, but that shared label does not make their failover, caching, analytics, or scaling behavior interchangeable.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;An application needs managed PostgreSQL and is choosing between Google Cloud and AWS. It mixes transactions with reporting, expects traffic variability, and requires zonal resilience. A feature checklist makes both products look similar: one writer, read scaling, distributed storage, automated backups, and PostgreSQL compatibility.&lt;/p&gt;
&lt;p&gt;The decision becomes useful only when tied to a workload trace, cloud boundary, recovery objective, extension inventory, and operating model.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Architecture comparisons often turn implementation details into universal performance claims. Aurora’s six storage copies do not imply a fixed application commit latency. AlloyDB’s columnar engine does not guarantee every analytical query accelerates. Aurora Optimized Reads is not available on every class, version, or pricing configuration. AlloyDB compute changes and read-pool autoscaling have feature-specific behavior and release stages.&lt;/p&gt;
&lt;p&gt;The core question is: &lt;strong&gt;which system behavior removes the measured bottleneck without introducing an unacceptable migration, consistency, or operational constraint?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-architectures-compared&quot;&gt;The Architectures Compared&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph AWS[Aurora PostgreSQL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        AApp[Application] --&gt; AWriter[Writer instance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        AReader[Reader instances] --&gt; AStorage[Shared cluster storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        AWriter --&gt; AStorage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph GCP[AlloyDB]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        GApp[Application] --&gt; GPrimary[Primary instance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        GRead[Read pool instances] --&gt; GStorage[Distributed regional storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        GPrimary --&gt; GStorage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        GRead --&gt; GColumn[Optional columnar engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Aurora stores a cluster volume across three Availability Zones and uses quorum-based storage. Aurora Replicas share that cluster volume. On writer failure, the service promotes an eligible replica; recovery behavior depends on replica presence, priority, health, and connection handling. Applications should use cluster endpoints and test DNS and pool behavior rather than promising a fixed failover interval.&lt;/p&gt;
&lt;p&gt;AlloyDB separates compute from regional distributed storage. A highly available primary instance has a standby node in another zone; read pools expose separate load-balanced read-only endpoints. Its optional columnar engine stores selected data in memory in column-oriented form and can accelerate eligible scans, joins, and aggregates.&lt;/p&gt;
&lt;h3 id=&quot;read-acceleration-is-not-one-feature&quot;&gt;Read acceleration is not one feature&lt;/h3&gt;
&lt;p&gt;Aurora Optimized Reads uses local NVMe storage on supported Aurora PostgreSQL versions and instance classes. In Aurora I/O-Optimized clusters it can add tiered caching; in supported Standard configurations it can provide temporary-object acceleration. The exact mode and eligibility matter.&lt;/p&gt;
&lt;p&gt;AlloyDB’s columnar engine changes execution for eligible analytical query fragments. It is not simply a block cache, and it consumes finite instance memory. Compare query plans, working-set residency, and OLTP interference—not marketing maximums.&lt;/p&gt;
&lt;h3 id=&quot;compute-elasticity-is-configuration-specific&quot;&gt;Compute elasticity is configuration-specific&lt;/h3&gt;
&lt;p&gt;Aurora Serverless v2 adjusts capacity within configured minimum and maximum ACUs; scaling is not a promise that every workload changes capacity instantly or without connection and memory effects. Provisioned Aurora behaves differently.&lt;/p&gt;
&lt;p&gt;As of September 2026, AlloyDB read-pool autoscaling is documented as Preview. Primary sizing and other scaling operations have their own availability effects. A production decision must distinguish GA and Preview capabilities and retest status at publication time.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Build a comparison harness with the same schema, supported extensions, data distribution, connection count, and query mix. Measure:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;commit latency under normal and impaired conditions;&lt;/li&gt;
&lt;li&gt;failover recovery including client reconnection;&lt;/li&gt;
&lt;li&gt;reader visibility and lag-sensitive application behavior;&lt;/li&gt;
&lt;li&gt;analytical p95 and p99 with and without specialized read acceleration;&lt;/li&gt;
&lt;li&gt;scale-out cold-cache effects;&lt;/li&gt;
&lt;li&gt;backup restore and regional recovery time;&lt;/li&gt;
&lt;li&gt;three cost profiles: steady load, bursty load, and failure headroom.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Keep evidence honest. Google’s columnar benchmark and AWS’s performance claims are vendor tests. They establish that a capability exists, not the outcome for this schema. The decision record should link each test result to a workload version and configuration.&lt;/p&gt;
&lt;p&gt;Cloud placement is also architectural. Choosing a database across clouds can add application latency, egress cost, IAM complexity, and a second incident surface. If the application and dependent data services already live in one cloud, require a measured advantage before crossing that boundary.&lt;/p&gt;
&lt;p&gt;Operational maturity belongs in the comparison. Rehearse a writer failure, reader removal, credential rotation, point-in-time restore, and regional recovery using the exact client libraries and endpoints planned for production. Compare the number of manual control-plane actions and the point at which traffic is safe to resume. A shorter database failover is irrelevant if DNS caching, an unhealthy connection pool, or an untested promotion runbook keeps the application unavailable.&lt;/p&gt;
&lt;p&gt;Publish the configurations with the result: region, engine version, instance class, storage mode, reader count, minimum and maximum capacity, parameter changes, and client placement. Without that metadata, the benchmark cannot be reproduced or used for a later upgrade decision.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Assumption&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Failure&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Every Aurora node has Optimized Reads&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Selected version or class is ineligible&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Verify the exact engine version, class, and storage configuration.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;AlloyDB columnar accelerates all SQL&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Query fragments are ineligible or columns are absent&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Inspect plans and column-store residency with representative SQL.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Serverless removes capacity planning&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Min and max ACUs, scaling rate, and connections still constrain behavior&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Load-test both scale-up and scale-down boundaries.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Shared storage means no stale reads&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reader visibility still has service semantics&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Test read-after-write behavior for application-critical paths.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Automatic failover preserves sessions&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Existing connections break or resolve slowly&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Test endpoints, DNS, pool recycling, retry, and idempotency.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; A shared “cloud-native PostgreSQL” label is masking materially different workload behavior.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Compare exact configurations against the application’s bottleneck, recovery objectives, and cloud placement.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; AWS and Google document distinct storage, read-acceleration, failover, and elasticity mechanisms with explicit eligibility conditions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Require a reproducible bake-off and a feature-stage review before approving either platform.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/overview&quot;&gt;AlloyDB overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/columnar-engine/about&quot;&gt;AlloyDB columnar engine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/instance-read-pool-scale&quot;&gt;AlloyDB read-pool autoscaling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Overview.StorageReliability.html&quot;&gt;Aurora storage and reliability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Concepts.AuroraHighAvailability.html&quot;&gt;Aurora high availability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.optimized.reads.html&quot;&gt;Aurora Optimized Reads&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-serverless-v2.how-it-works.html&quot;&gt;Aurora Serverless v2 scaling&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>cloud</category><category>architecture</category><category>databases</category><category>performance</category><category>checklist</category></item><item><title>Covering Indexes Are Not Enough Without Visibility</title><link>https://rajivonai.com/blog/2025-07-12-covering-indexes-are-not-enough-without-visibility/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-07-12-covering-indexes-are-not-enough-without-visibility/</guid><description>PostgreSQL index-only scans only stay fast when covering indexes and visibility map maintenance work together.</description><pubDate>Sat, 12 Jul 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A PostgreSQL covering index is not a performance fix by itself; it is a bet that the query, the index payload, and the visibility map will stay aligned under real production churn.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The default move is still an ordinary B-tree index on the predicate column: &lt;code&gt;CREATE INDEX ON users(email)&lt;/code&gt;. The better move, when the read path is stable, is a covering index using PostgreSQL 11’s &lt;code&gt;INCLUDE&lt;/code&gt; clause, which stores projected columns in the index payload so an index-only scan can answer the query without visiting the heap when visibility permits it.&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Approach&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;What it optimizes&lt;/th&gt;&lt;th&gt;What it still pays for&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Ordinary B-tree index&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Finds matching tuple IDs quickly&lt;/td&gt;&lt;td&gt;Heap reads for projected columns and Multi-Version Concurrency Control (MVCC) visibility&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Covering index with &lt;code&gt;INCLUDE&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Keeps predicate and selected columns in one index&lt;/td&gt;&lt;td&gt;Larger index, write overhead, visibility map dependency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Covering index plus vacuum discipline&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Avoids heap access for stable pages&lt;/td&gt;&lt;td&gt;Operational ownership of autovacuum and long transactions&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;PostgreSQL indexes do not store complete row visibility. They can point to candidate rows, but MVCC visibility is determined from heap state unless PostgreSQL can trust the visibility map. The official PostgreSQL documentation is explicit: index-only scans only win when the needed columns are available from the index and a significant fraction of heap pages have their all-visible bits set in the visibility map.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure point&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Projection misses the index&lt;/td&gt;&lt;td&gt;&lt;code&gt;SELECT username, status&lt;/code&gt; uses &lt;code&gt;idx_users_email(email)&lt;/code&gt; and still reads the heap&lt;/td&gt;&lt;td&gt;The index finds rows, but the table still serves the selected columns&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Visibility map is stale&lt;/td&gt;&lt;td&gt;Plan says &lt;code&gt;Index Only Scan&lt;/code&gt;, but reports &lt;code&gt;Heap Fetches: 12000&lt;/code&gt;&lt;/td&gt;&lt;td&gt;The scan is only “index-only” for pages marked all-visible&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Autovacuum threshold is too loose&lt;/td&gt;&lt;td&gt;Default &lt;code&gt;autovacuum_vacuum_scale_factor = 0.2&lt;/code&gt; can mean roughly 40M changed tuples on a 200M-row table before vacuum triggers&lt;/td&gt;&lt;td&gt;Large tables can accumulate heap pages that are not all-visible for too long&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Included column churn&lt;/td&gt;&lt;td&gt;Updating &lt;code&gt;status&lt;/code&gt; or &lt;code&gt;username&lt;/code&gt; touches an indexed column&lt;/td&gt;&lt;td&gt;PostgreSQL must maintain the index entry, and HOT updates are less likely&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Staging lies politely&lt;/td&gt;&lt;td&gt;Freshly loaded and manually vacuumed test data shows zero heap fetches&lt;/td&gt;&lt;td&gt;Production write churn, old snapshots, and delayed vacuum change the execution profile&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The core question is not “did we add an index?” It is: can PostgreSQL answer this production query from the index while proving that the referenced heap pages are visible to the current snapshot?&lt;/p&gt;
&lt;h2 id=&quot;design-the-index-around-the-read-path-and-the-visibility-map&quot;&gt;Design the Index Around the Read Path and the Visibility Map&lt;/h2&gt;
&lt;p&gt;The right architecture is a measured covering-index loop: identify the hot read path, build the narrowest covering index, verify heap avoidance with &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt;, and tune vacuum behavior for that table instead of celebrating the DDL.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[hot read query — predicate and projection] --&gt; Cover[covering B-tree index — key and included columns]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Cover --&gt; VM[visibility map — all visible bit]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    VM --&gt;|bit set| Return[index tuple returned]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    VM --&gt;|bit clear| Heap[heap visit for MVCC check]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Heap --&gt; Return&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Vacuum[VACUUM and autovacuum] --&gt; VM&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Writes[INSERT UPDATE DELETE on page] --&gt; VM&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Start from &lt;code&gt;pg_stat_statements&lt;/code&gt;, not intuition. Pick one query by total time and call count, then write down its &lt;code&gt;WHERE&lt;/code&gt;, &lt;code&gt;ORDER BY&lt;/code&gt;, and &lt;code&gt;SELECT&lt;/code&gt; columns.&lt;br&gt;
Verification: the candidate query has a stable fingerprint and enough calls to matter.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Put search columns in the key and projected columns in &lt;code&gt;INCLUDE&lt;/code&gt;. For the lookup path below, &lt;code&gt;email&lt;/code&gt; is the key; &lt;code&gt;username&lt;/code&gt; and &lt;code&gt;status&lt;/code&gt; are payload.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; CONCURRENTLY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx_users_email_covering&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; users(email)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INCLUDE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (username, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Verification: &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; finishes without blocking ordinary reads and writes, and the index size is acceptable via &lt;code&gt;pg_relation_size&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the real query with execution metrics.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; username, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; users&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; email &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;dev@example.com&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Verification: look for &lt;code&gt;Index Only Scan&lt;/code&gt;, low shared buffer reads, and &lt;code&gt;Heap Fetches: 0&lt;/code&gt; or a number small enough to survive peak traffic.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Check visibility health, not just plan shape. PostgreSQL’s visibility map stores all-visible and all-frozen state per heap page, and its bits are set by vacuum and cleared by data-modifying operations.&lt;br&gt;
Verification: if heap fetches remain high after the index is used, inspect &lt;code&gt;last_autovacuum&lt;/code&gt;, &lt;code&gt;n_dead_tup&lt;/code&gt;, long-running transactions, and table-level autovacuum settings.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Bound the write cost. Included columns are not search keys, but they still live in the index. A wide &lt;code&gt;text&lt;/code&gt;, &lt;code&gt;jsonb&lt;/code&gt;, or frequently updated status column can turn a read optimization into write amplification.&lt;br&gt;
Verification: compare &lt;code&gt;pg_stat_user_indexes.idx_scan&lt;/code&gt;, write latency, WAL volume, HOT update ratio, and index size before and after rollout.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;I am not going to invent a 2:14 AM incident with a heroic graph. The documented production pattern is enough, and the public PostgreSQL material gives a concrete measurement boundary.&lt;/p&gt;
&lt;p&gt;PostgreSQL 11 added covering indexes with &lt;code&gt;INCLUDE&lt;/code&gt;, documented in the project release notes and in the current index-only scan documentation. The documentation says the scan is physically possible when the index type supports it and the query’s referenced columns are available from the index. B-tree indexes satisfy the access-method requirement. The same documentation adds the operational catch: because visibility data is not stored in index entries, PostgreSQL checks the visibility map before skipping the heap.&lt;/p&gt;
&lt;p&gt;That behavior explains why a plan can contain &lt;code&gt;Index Only Scan&lt;/code&gt; and still do heap work. The plan node describes the access strategy; &lt;code&gt;Heap Fetches&lt;/code&gt; tells you how often the executor had to visit heap pages anyway. If heap fetches are high, the covering index may still reduce work, but it has not removed the table from the read path.&lt;/p&gt;
&lt;p&gt;A useful public comparison comes from Dalibo’s PostgreSQL 11 workshop, which uses a 10M-row table with columns &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, and &lt;code&gt;c&lt;/code&gt;. With a unique index on &lt;code&gt;(a, b)&lt;/code&gt;, selecting only &lt;code&gt;a, b&lt;/code&gt; can use an index-only scan with &lt;code&gt;Heap Fetches: 0&lt;/code&gt;. Selecting &lt;code&gt;a, b, c&lt;/code&gt; from the same predicate cannot be answered by that index, so PostgreSQL uses an index scan and reads the table to get &lt;code&gt;c&lt;/code&gt;. After adding a covering index on &lt;code&gt;(a, b) INCLUDE (c)&lt;/code&gt;, the same &lt;code&gt;a, b, c&lt;/code&gt; query returns to an index-only scan with &lt;code&gt;Heap Fetches: 0&lt;/code&gt;.&lt;/p&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Public PostgreSQL 11 workshop measurement&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Plan shape&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Heap fetch signal&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Execution time&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Existing unique index on &lt;code&gt;(a, b)&lt;/code&gt;, query selects &lt;code&gt;a, b&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;&lt;code&gt;Index Only Scan&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;&lt;code&gt;12.628 ms&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Existing unique index on &lt;code&gt;(a, b)&lt;/code&gt;, query selects &lt;code&gt;a, b, c&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;&lt;code&gt;Index Scan&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Heap access is inherent&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;&lt;code&gt;16.034 ms&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Covering unique index on &lt;code&gt;(a, b) INCLUDE (c)&lt;/code&gt;, query selects &lt;code&gt;a, b, c&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;&lt;code&gt;Index Only Scan&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;&lt;code&gt;14.263 ms&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The more interesting part is not the small read-time delta in that example. It is the storage and write tradeoff. Dalibo reports &lt;code&gt;214 MB&lt;/code&gt; for the unique &lt;code&gt;(a, b)&lt;/code&gt; index and &lt;code&gt;387 MB&lt;/code&gt; for a separate &lt;code&gt;(a, b, c)&lt;/code&gt; index, or &lt;code&gt;602 MB&lt;/code&gt; if both are kept. Replacing that pair with one unique covering index on &lt;code&gt;(a, b) INCLUDE (c)&lt;/code&gt; is reported at &lt;code&gt;386 MB&lt;/code&gt;. The same workshop then inserts 100k rows: maintaining one covering index reports &lt;code&gt;502.594 ms&lt;/code&gt;; maintaining the two-index design reports &lt;code&gt;843.147 ms&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;That is the design tradeoff senior engineers should care about. The covering index did not make writes free. It reduced a two-index design into one index while preserving uniqueness semantics on &lt;code&gt;(a, b)&lt;/code&gt;. If your alternative is no extra index, writes still pay. If your alternative is two overlapping indexes, a covering index may be the cheaper structure.&lt;/p&gt;
&lt;p&gt;The deeper production gotcha is autovacuum math. PostgreSQL documents &lt;code&gt;autovacuum_vacuum_threshold = 50&lt;/code&gt; and &lt;code&gt;autovacuum_vacuum_scale_factor = 0.2&lt;/code&gt; defaults. On small tables, that is fine. On a 200M-row relation, scale-factor-driven vacuum can wait for a very large number of changed tuples unless table storage parameters override it. That delay matters because visibility map bits are conservative: if PostgreSQL cannot prove a page is all-visible, it visits the heap.&lt;/p&gt;
&lt;p&gt;There is also a schema-design trap. Adding &lt;code&gt;INCLUDE (username, status)&lt;/code&gt; is reasonable for a hot lookup endpoint. Adding ten payload columns because “index-only scans are fast” is not engineering; it is moving the table into another structure with worse write economics. PostgreSQL will reject oversized index tuples, and before that hard failure, you pay with memory pressure, cache churn, WAL, and slower updates.&lt;/p&gt;
&lt;p&gt;The useful mental model is simple: a covering index is a read-path contract. Autovacuum, transaction age, and update patterns are the parties that can break it.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Index Only Scan&lt;/code&gt; still shows large &lt;code&gt;Heap Fetches&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Pages are not marked all-visible after recent &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, or &lt;code&gt;DELETE&lt;/code&gt; activity&lt;/td&gt;&lt;td&gt;Tune table-level autovacuum and remove long-running transactions holding old snapshots&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Covering index bloats quickly&lt;/td&gt;&lt;td&gt;&lt;code&gt;INCLUDE&lt;/code&gt; contains wide &lt;code&gt;text&lt;/code&gt;, &lt;code&gt;jsonb&lt;/code&gt;, or low-value projected columns&lt;/td&gt;&lt;td&gt;Keep payload columns narrow and tied to one hot query family&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Write latency rises after rollout&lt;/td&gt;&lt;td&gt;Included columns are frequently updated, preventing cheap heap-only behavior&lt;/td&gt;&lt;td&gt;Drop volatile payload columns or split read model from write-heavy table&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Planner ignores the new index&lt;/td&gt;&lt;td&gt;Query selects extra columns, uses mismatched predicates, or statistics are stale&lt;/td&gt;&lt;td&gt;Re-run &lt;code&gt;ANALYZE&lt;/code&gt;, verify exact projection, and compare with &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Staging benchmark overstates gains&lt;/td&gt;&lt;td&gt;Test data was bulk-loaded, vacuumed, and mostly static&lt;/td&gt;&lt;td&gt;Replay production write mix or test after churn before trusting heap-fetch counts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;RDS maintenance lags during peak write load&lt;/td&gt;&lt;td&gt;Autovacuum workers and cost limits cannot keep up with dead tuples&lt;/td&gt;&lt;td&gt;Use per-table autovacuum settings and monitor &lt;code&gt;pg_stat_user_tables&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Ordinary indexes still force heap access when the query projects columns outside the index or when MVCC visibility cannot be proven from the visibility map.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Build narrow covering indexes only for high-call-count read paths, then treat autovacuum health as part of the index design.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: The validation signal is not the presence of &lt;code&gt;Index Only Scan&lt;/code&gt;; it is low &lt;code&gt;Heap Fetches&lt;/code&gt;, stable buffer reads, acceptable index size, preserved HOT update ratio, and no write regression.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, take the top query from &lt;code&gt;pg_stat_statements&lt;/code&gt;, add one candidate covering index in staging, and compare &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt;, &lt;code&gt;pg_relation_size&lt;/code&gt;, write latency, WAL volume, and HOT update ratio before and after real write churn.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A fast PostgreSQL query is rarely the result of one clever index; it is the result of making the storage engine’s promises line up with the workload it is actually running.&lt;/p&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>When Autovacuum Becomes a Backpressure Signal</title><link>https://rajivonai.com/blog/2025-07-05-when-autovacuum-becomes-a-backpressure-signal/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-07-05-when-autovacuum-becomes-a-backpressure-signal/</guid><description>PostgreSQL vacuum stalls are often symptoms of lock pressure, table bloat, and missing operational visibility.</description><pubDate>Sat, 05 Jul 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Autovacuum is not background housekeeping; in a write-heavy PostgreSQL system, delayed vacuum is a backpressure signal from Multi-Version Concurrency Control before the application admits it is overloaded.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s default approach is to let autovacuum clean dead row versions in the background while application traffic continues. The alternative is to treat vacuum health as part of the write path: measured, alerted, tuned per table, and included in incident triage.&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Approach&lt;/th&gt;&lt;th&gt;What it assumes&lt;/th&gt;&lt;th&gt;What production eventually proves&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Default autovacuum&lt;/td&gt;&lt;td&gt;Table churn is moderate and cleanup can trail safely&lt;/td&gt;&lt;td&gt;High-update tables create cleanup debt faster than defaults can retire it&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Manual emergency vacuum&lt;/td&gt;&lt;td&gt;Operators can intervene after latency spikes&lt;/td&gt;&lt;td&gt;The database is already paying interest on bloat by then&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Vacuum as backpressure telemetry&lt;/td&gt;&lt;td&gt;Dead tuples, transaction age, locks, and vacuum progress are monitored together&lt;/td&gt;&lt;td&gt;The incident is visible before p95 latency becomes the alert&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Autovacuum is often blamed because it is visible during the outage. That is usually too shallow. In PostgreSQL, &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; create dead row versions under Multi-Version Concurrency Control; &lt;code&gt;VACUUM&lt;/code&gt; can only remove versions no active snapshot can still see. A single old transaction can hold back the cleanup horizon through &lt;code&gt;backend_xmin&lt;/code&gt;, which PostgreSQL exposes in &lt;code&gt;pg_stat_activity&lt;/code&gt;.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure point&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Long transaction age&lt;/td&gt;&lt;td&gt;Vacuum cannot remove dead tuples still visible to an old snapshot&lt;/td&gt;&lt;td&gt;Bloat grows even while autovacuum appears active&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Idle transaction sessions&lt;/td&gt;&lt;td&gt;&lt;code&gt;state = &apos;idle in transaction&apos;&lt;/code&gt; keeps a snapshot open without doing useful work&lt;/td&gt;&lt;td&gt;One abandoned app connection can pin cleanup behind thousands of writes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High-churn tables on defaults&lt;/td&gt;&lt;td&gt;&lt;code&gt;autovacuum_vacuum_scale_factor = 0.2&lt;/code&gt; waits for 20 percent table churn plus threshold&lt;/td&gt;&lt;td&gt;On a 200M-row table, that can mean tens of millions of dead tuples before cleanup starts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lock conflicts&lt;/td&gt;&lt;td&gt;Plain &lt;code&gt;VACUUM&lt;/code&gt; uses &lt;code&gt;ShareUpdateExclusiveLock&lt;/code&gt;; &lt;code&gt;VACUUM FULL&lt;/code&gt; takes &lt;code&gt;AccessExclusiveLock&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Confusing the two during an incident can turn a slowdown into an outage&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Dead tuple percent alone&lt;/td&gt;&lt;td&gt;Small tables, append-heavy tables, and partitioned tables distort the signal&lt;/td&gt;&lt;td&gt;Alerts need relation size, last vacuum age, transaction age, and latency together&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;PostgreSQL’s own documentation is explicit about the mechanics: routine vacuuming removes dead row versions and prevents transaction ID wraparound, while old open transactions can block cleanup progress. The operational question is not “is autovacuum running?” The question is: which workload condition is forcing it to fall behind?&lt;/p&gt;
&lt;h2 id=&quot;treat-autovacuum-as-backpressure-telemetry&quot;&gt;Treat Autovacuum as Backpressure Telemetry&lt;/h2&gt;
&lt;p&gt;The right architecture is a vacuum control loop: observe the cleanup horizon, identify blockers, tune the few hot tables, and validate under write load. Do not start by changing global autovacuum settings across the cluster. That is how a maintenance problem becomes an I/O scheduling problem.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[application writes] --&gt; MVCC[MVCC row versions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MVCC --&gt; Dead[dead tuples accumulate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Txn[old transaction xmin] --&gt; Horizon[cleanup horizon held back]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Dead --&gt; Auto[autovacuum worker]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Horizon --&gt; Auto&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Auto --&gt; Locks[ShareUpdateExclusiveLock]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DDL[DDL or index maintenance] --&gt; Locks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Locks --&gt; Lag[vacuum lag]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Lag --&gt; Bloat[table and index bloat]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Bloat --&gt; Planner[slower plans and more IO]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Planner --&gt; App&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Lag --&gt; Alert[backpressure alert]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Build a vacuum incident view.&lt;/p&gt;
&lt;p&gt;Include active vacuum progress, oldest transaction age, idle-in-transaction sessions, dead tuple counts, table size, and blockers. &lt;code&gt;pg_stat_progress_vacuum&lt;/code&gt; has existed since PostgreSQL 9.6 and reports active vacuum workers, including autovacuum workers.&lt;/p&gt;
&lt;p&gt;Verification: during a load test, you can name the table being vacuumed, its phase, heap blocks scanned, and any blocking backend in under one minute.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Alert on cleanup debt, not just dead tuple percentage.&lt;/p&gt;
&lt;p&gt;A 40 percent dead tuple ratio on a 5 MB table is noise. Five percent on a 900 GB high-update table may be a serious future incident. Use a composite signal: &lt;code&gt;n_dead_tup&lt;/code&gt;, &lt;code&gt;pg_total_relation_size&lt;/code&gt;, &lt;code&gt;last_autovacuum&lt;/code&gt;, oldest &lt;code&gt;backend_xmin&lt;/code&gt;, and query latency for the table’s top statements.&lt;/p&gt;
&lt;p&gt;Verification: every alert points to one table, one suspected blocker class, and one next action.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Tune high-churn tables per table.&lt;/p&gt;
&lt;p&gt;Lower scale factors on tables such as &lt;code&gt;orders&lt;/code&gt;, &lt;code&gt;sessions&lt;/code&gt;, and job queues. A setting like &lt;code&gt;autovacuum_vacuum_scale_factor = 0.01&lt;/code&gt; with a fixed threshold can make cleanup continuous instead of bursty. Keep cost delay and cost limit workload-aware; aggressive cleanup still competes for disk and cache.&lt;/p&gt;
&lt;p&gt;Verification: after tuning, &lt;code&gt;n_dead_tup&lt;/code&gt; forms a sawtooth with a lower ceiling under production-like write load.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Fix transaction hygiene before killing vacuum.&lt;/p&gt;
&lt;p&gt;Terminating autovacuum can reduce immediate pressure when it is competing with foreground work, but repeated termination increases bloat debt. The durable fix is shorter transactions, timeouts for idle sessions, safer migration locks, and partition or index maintenance where needed.&lt;/p&gt;
&lt;p&gt;Verification: oldest transaction age remains bounded during peak traffic, not only during maintenance windows.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A useful runbook query starts here:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  usename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  application_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  wait_event_type,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  wait_event,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  age(clock_timestamp(), xact_start) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; xact_age,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  age(clock_timestamp(), query_start) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; query_age,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  backend_xmin,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  left&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(query, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;160&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; query&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;idle&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; xact_start &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NULLS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LAST&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The most useful public case study is not an anonymous war story; it is the AWS Database Blog write-up on tuning autovacuum for Amazon RDS for PostgreSQL 9.6.3 after an Oracle-to-PostgreSQL OLTP migration. The database was provisioned for 30,000 IOPS. During the first weeks after migration, several databases saw Read IOPS spike as high as 25,000 without a matching increase in application load. The visible symptom was not one slow query. It was cleanup work arriving late, in large chunks, on already-bloated tables.&lt;/p&gt;
&lt;p&gt;The concrete numbers are the part worth carrying into a runbook:&lt;/p&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Published observation&lt;/th&gt;&lt;th&gt;Value&lt;/th&gt;&lt;th&gt;Operational reading&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;table1&lt;/code&gt; live tuples&lt;/td&gt;&lt;td&gt;450,398,643&lt;/td&gt;&lt;td&gt;Large enough that percentage-based thresholds delay cleanup&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;table1&lt;/code&gt; dead tuples&lt;/td&gt;&lt;td&gt;459,406,616&lt;/td&gt;&lt;td&gt;More dead tuples than estimated live tuples&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;table2&lt;/code&gt; dead tuples&lt;/td&gt;&lt;td&gt;1,919,230,596&lt;/td&gt;&lt;td&gt;Vacuum debt was not isolated to one table&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;table3&lt;/code&gt; dead tuples&lt;/td&gt;&lt;td&gt;4,642,232,802&lt;/td&gt;&lt;td&gt;Cluster-level worker saturation becomes plausible&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Longest autovacuum session&lt;/td&gt;&lt;td&gt;2 days 16:03 on &lt;code&gt;sh.table1&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Vacuum was active but not converging fast enough&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Blocking session state&lt;/td&gt;&lt;td&gt;&lt;code&gt;idle in transaction&lt;/code&gt; for 2 days 22:25 on &lt;code&gt;table1&lt;/code&gt;&lt;/td&gt;&lt;td&gt;The cleanup horizon was pinned by transaction hygiene&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;RDS setting called out&lt;/td&gt;&lt;td&gt;&lt;code&gt;autovacuum_vacuum_scale_factor = 0.1&lt;/code&gt;, &lt;code&gt;autovacuum_max_workers = 3&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Millions of dead tuples accumulated before work started&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tuning result reported&lt;/td&gt;&lt;td&gt;&lt;code&gt;autovacuum_max_workers = 8&lt;/code&gt;, &lt;code&gt;autovacuum_vacuum_cost_limit = 4800&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Read IOPS during concurrent autovacuum was brought to about 10,000, one-third of provisioned capacity&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;That case is useful because it separates three failure modes operators often collapse into one. First, the trigger threshold was too high for tables with hundreds of millions of rows. Second, the default worker count meant a few large tables could occupy all autovacuum workers while other tables continued to accumulate dead tuples. Third, an &lt;code&gt;idle in transaction&lt;/code&gt; session kept old tuple versions visible, so autovacuum could run and still fail to reclaim enough space.&lt;/p&gt;
&lt;p&gt;The lock behavior is documented, not folklore. PostgreSQL’s explicit locking documentation states that plain &lt;code&gt;VACUUM&lt;/code&gt; acquires &lt;code&gt;ShareUpdateExclusiveLock&lt;/code&gt;, while &lt;code&gt;VACUUM FULL&lt;/code&gt; requires &lt;code&gt;AccessExclusiveLock&lt;/code&gt;. That distinction matters at 03:00. Plain vacuum is designed to coexist with normal reads and writes; &lt;code&gt;VACUUM FULL&lt;/code&gt; rewrites the table and blocks concurrent access. Reaching for it during a live checkout incident is usually the database equivalent of fixing a smoke alarm with a hammer.&lt;/p&gt;
&lt;p&gt;A separate public PGConf/OtterTune autovacuum case connects the same mechanics to request latency. The case describes an update-heavy workload where long-running queries blocked autovacuum, dead tuples accumulated by 600x, blocks read increased by 375x, non-HOT updates reached 100 percent, update latency increased from 12 ms to 710 ms, throughput dropped by 25 percent during the spike, and query latency spiked by 90x. The exact schema is less important than the shape of the failure: stale tuple versions made ordinary updates read and write far more than the application expected.&lt;/p&gt;
&lt;p&gt;The practical pattern is visible in named system behavior:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;System behavior&lt;/th&gt;&lt;th&gt;Operational implication&lt;/th&gt;&lt;th&gt;Source&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Dead row versions remain until no active transaction can see them&lt;/td&gt;&lt;td&gt;Watch &lt;code&gt;backend_xmin&lt;/code&gt;, not only table size&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;https://www.postgresql.org/docs/current/routine-vacuuming.html&quot;&gt;PostgreSQL routine vacuuming&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Autovacuum triggers from threshold plus scale factor&lt;/td&gt;&lt;td&gt;Large tables need per-table thresholds&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;https://www.postgresql.org/docs/current/runtime-config-autovacuum.html&quot;&gt;Autovacuum settings&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Plain vacuum and DDL can conflict through table locks&lt;/td&gt;&lt;td&gt;Incident views need &lt;code&gt;pg_locks&lt;/code&gt;, not only connection counts&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;https://www.postgresql.org/docs/current/explicit-locking.html&quot;&gt;PostgreSQL explicit locking&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Vacuum progress is visible while running&lt;/td&gt;&lt;td&gt;Treat active vacuum as observable work, not mystery load&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;https://www.postgresql.org/docs/9.6/progress-reporting.html&quot;&gt;PostgreSQL progress reporting&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Large-table defaults can produce delayed, bursty cleanup&lt;/td&gt;&lt;td&gt;Tune hot tables before making broad cluster changes&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;https://aws.amazon.com/blogs/database/a-case-study-of-tuning-autovacuum-in-amazon-rds-for-postgresql/&quot;&gt;AWS RDS autovacuum case study&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Long-running queries can turn vacuum lag into latency spikes&lt;/td&gt;&lt;td&gt;Track transaction age beside table bloat and top statement latency&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;https://postgresconf.org/system/events/document/000/002/155/Autovacuum_PGCon.pdf&quot;&gt;PGConf autovacuum case study&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The more interesting production lesson is that vacuum lag is a system signal, not a storage metric. It often points at application behavior: oversized transactions, forgotten cursors, migration scripts without lock timeouts, reporting queries running at &lt;code&gt;REPEATABLE READ&lt;/code&gt;, or connection pools that keep sessions open after the request has ended.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Autovacuum workers saturated&lt;/td&gt;&lt;td&gt;Several large tables cross vacuum thresholds at the same time&lt;/td&gt;&lt;td&gt;Tune hot tables individually and review &lt;code&gt;autovacuum_max_workers&lt;/code&gt; with disk capacity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cleanup horizon pinned&lt;/td&gt;&lt;td&gt;Old &lt;code&gt;backend_xmin&lt;/code&gt;, prepared transaction, or replication slot prevents tuple removal&lt;/td&gt;&lt;td&gt;Alert on transaction age, prepared transactions, and replication slot lag&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Foreground latency worsens after tuning&lt;/td&gt;&lt;td&gt;Lower scale factors create more frequent vacuum I/O under peak writes&lt;/td&gt;&lt;td&gt;Adjust cost limit, cost delay, and schedule manual maintenance for cold periods&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;VACUUM FULL&lt;/code&gt; blocks traffic&lt;/td&gt;&lt;td&gt;Operator uses it to reclaim disk on a live table&lt;/td&gt;&lt;td&gt;Prefer regular vacuum, &lt;code&gt;REINDEX CONCURRENTLY&lt;/code&gt;, partition rotation, or planned maintenance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bloat estimate misleads&lt;/td&gt;&lt;td&gt;Statistics are stale or relation layout makes estimates noisy&lt;/td&gt;&lt;td&gt;Pair estimates with &lt;code&gt;pg_stat_user_tables&lt;/code&gt;, relation size trends, and query plans&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Partitioned table hides hot child&lt;/td&gt;&lt;td&gt;Parent looks healthy while one partition churns heavily&lt;/td&gt;&lt;td&gt;Monitor child partitions and tune storage parameters per partition&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: PostgreSQL vacuum lag becomes dangerous when dead tuples, old snapshots, and lock waits are observed as separate symptoms.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Build a single incident view that joins transaction age, blocked vacuum, table churn, relation size, and active vacuum progress.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: A valid signal names the blocker class before p95 query latency crosses the page threshold, and it explains whether the issue is threshold delay, worker saturation, pinned cleanup horizon, or lock conflict.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, pick the top three write-heavy tables and set table-specific vacuum alerts before changing global autovacuum settings.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Autovacuum is the database telling you how much write-path debt your architecture is carrying; the mature response is to measure the debt before the bill arrives.&lt;/p&gt;</content:encoded><category>databases</category><category>failures</category><category>checklist</category></item><item><title>Top GitHub Breakouts: May 2025 — Operational Baseline in a Config File</title><link>https://rajivonai.com/blog/2025-06-22-github-stars-may-2025/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-06-22-github-stars-may-2025/</guid><description>Three May 2025 open-source projects replace multi-tool assembly in document ingestion, deployment governance, and PostgreSQL backup with single-binary or configuration-first alternatives.</description><pubDate>Sun, 22 Jun 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Before any AI agent can answer questions from a document corpus, before any deployment can reach production safely, before any PostgreSQL failure can be recovered within an RTO — someone has to do setup work that should not exist.&lt;/strong&gt; PDF parsing pipelines need hand-tuning for every document type. Deployment gating still lives in Slack threads and wiki pages. PostgreSQL continuous backup requires assembling pg_receivewal, a scheduler, a retention script, and monitoring separately. Three projects that emerged in May 2025 reduced each of those setups to a single configuration file.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Document preparation, release governance, and database disaster recovery share a common failure pattern: engineers know how to do each one, the components exist, but assembling them into a production-ready system takes long enough that teams either skip it or do it once and never revisit it. Each category also sits on the critical path of something that matters — RAG pipeline accuracy, deployment compliance, and recovery objectives. The cost of half-finishing any of them shows up in production.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Domain&lt;/th&gt;&lt;th&gt;Manual bottleneck&lt;/th&gt;&lt;th&gt;What it costs&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;System design&lt;/td&gt;&lt;td&gt;Tuning PDF parsers per document type for table and layout accuracy&lt;/td&gt;&lt;td&gt;RAG pipeline precision degrades on complex layouts without per-document tuning&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;System design&lt;/td&gt;&lt;td&gt;Building custom OCR pipelines for scanned documents&lt;/td&gt;&lt;td&gt;Every scanned PDF corpus requires custom preprocessing before LLM ingestion&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Platform&lt;/td&gt;&lt;td&gt;Manually coordinating deploy gates across CI, on-call, and approval flows&lt;/td&gt;&lt;td&gt;Policy-gated deploys live in Slack threads and break on team turnover&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Platform&lt;/td&gt;&lt;td&gt;No audit trail for which conditions triggered a release or who approved&lt;/td&gt;&lt;td&gt;Compliance review of deployment history requires manual log correlation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Databases&lt;/td&gt;&lt;td&gt;Operating pg_receivewal, a scheduler, compression, and retention scripts separately&lt;/td&gt;&lt;td&gt;Four moving parts to maintain — failure in any one breaks the backup chain&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Databases&lt;/td&gt;&lt;td&gt;No integrated monitoring for backup lag or WAL segment loss&lt;/td&gt;&lt;td&gt;Backup failures are silent until a restore attempt exposes them&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Can each of these be reduced to a single-binary or configuration-first deployment?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Operational Baseline Automation] --&gt; B[System Design — OpenDataLoader PDF]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; C[Platform — SuperPlane]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; D[Databases — pgrwl]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[Structured PDF extraction — no per-document parser tuning]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; F[Event-driven release gates — no Slack coordination required]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; G[Single-binary PostgreSQL backup — no multi-tool assembly]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;opendataloader-pdf--eliminates-per-document-type-parser-tuning-for-rag-ingestion&quot;&gt;OpenDataLoader PDF — eliminates per-document-type parser tuning for RAG ingestion&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: Every PDF corpus — multi-column research papers, financial reports, technical manuals — previously required a custom extraction pipeline tuned to its layout. Table extraction accuracy with off-the-shelf tools degraded to 60–70% on complex layouts, requiring manual post-processing before the content was useful for retrieval.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How it replaces that task&lt;/strong&gt;: According to the project README, OpenDataLoader PDF achieves “#1 in benchmarks: 0.907 overall, 0.928 table accuracy across 200 real-world PDFs.” It operates in deterministic local mode (0.015s/page per README) or AI hybrid mode for complex pages, with built-in OCR supporting 80+ languages and structured output in Markdown, JSON with bounding boxes, and HTML.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The workflow&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Before: tune extraction per document layout&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pdfminer.high_level &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; extract_text&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;text &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; extract_text(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;paper.pdf&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# No table structure, no layout, no OCR for scanned pages&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Requires: custom table detection, reading order correction, OCR pipeline&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# After: opendataloader-pdf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;pip install opendataloader&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;pdf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; opendataloader_pdf &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; extract&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;result &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; extract(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;paper.pdf&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Returns: structured Markdown + JSON with bounding boxes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Works on digital PDFs, scanned PDFs, multi-column layouts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: The AI hybrid mode requires an external AI service, adding latency and cost on complex pages. The deterministic local mode is fast but may underperform on layouts that hybrid mode handles. Java 11+ runtime is required — Python-only environments need JVM before the library is usable.&lt;/p&gt;
&lt;h3 id=&quot;superplane--eliminates-manual-release-coordination-across-ci-approvals-and-policy-gates&quot;&gt;SuperPlane — eliminates manual release coordination across CI, approvals, and policy gates&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: Policy-gated deployments — deploy only during business hours, require on-call approval, wait for rollout verification before proceeding — previously required coordinating across CI/CD systems, chat tools, and people, with no durable record of which conditions were met or who approved.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How it replaces that task&lt;/strong&gt;: According to the README, SuperPlane lets teams define multi-step operational workflows as directed graphs (“Canvases”), triggered by events from CI/CD, observability, and incident tools. It executes the graph, tracks state, and exposes run history and debugging in a UI and CLI. The README describes the system as “agent-friendly” — coding agents can trigger workflows and investigate executions via the CLI.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The workflow&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Before: deploy gate documented in wiki, enforced via Slack&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# &quot;check with on-call, wait for 10am window, post in #deploys, run deploy.sh&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# No enforcement, no audit trail, breaks on team turnover&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# After: SuperPlane Canvas definition&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;canvas&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  steps&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;wait_business_hours&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      component&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;time_gate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      config&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;09:00&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;17:00&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;timezone&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;UTC&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;require_approval&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      component&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;approval&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      config&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;approvers&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;on-call&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      depends_on&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;wait_business_hours&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;trigger_deploy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      component&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;ci_trigger&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      config&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;pipeline&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;production-deploy&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      depends_on&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;require_approval&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: SuperPlane is in alpha — the README explicitly states “rough edges and occasional breaking changes while we stabilize the core model.” The integration surface is wide; workflows that depend on tooling without a built-in connector require custom component development. Teams with heavily customized CI pipelines should budget engineering time for connector work.&lt;/p&gt;
&lt;h3 id=&quot;pgrwl--eliminates-the-multi-tool-postgresql-backup-assembly&quot;&gt;pgrwl — eliminates the multi-tool PostgreSQL backup assembly&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: Production-grade PostgreSQL continuous backup requires assembling and operating pg_receivewal, a scheduled base backup job, compression, remote storage upload, retention management, and restore tooling — each separately configured, each a distinct failure mode.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How it replaces that task&lt;/strong&gt;: According to the README, pgrwl “replaces that entire stack with a single process: WAL streaming, scheduled base backups, compression, encryption, S3/SFTP upload, retention management, and a restore helper — all driven by one binary.” It is described as a container-friendly alternative to pg_receivewal with automatic reconnects, partial WAL file handling, and integrated monitoring.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The workflow&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Before: configure and operate 4+ tools&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;systemctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; start&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pg_receivewal&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;          # WAL streaming daemon&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pg_basebackup&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -D&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /backup&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;     # base backups via cron&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# + write retention cleanup script&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# + configure S3 upload separately&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# + add monitoring for each component&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# After: pgrwl with a single config file&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# pgrwl.yaml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;wal:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  streaming:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  archive:&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; s3://my-bucket/wal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;backup:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  schedule:&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;0 2 * * *&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  compression:&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; zstd&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  retention:&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; 7d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;monitoring:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  prometheus:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pgrwl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; start&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;  # one process, all components active&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: pgrwl was released May 22, 2025. No published production deployment case studies exist at the time of writing. Teams should run pgrwl in parallel with their existing backup tooling for at least 60 days and perform at least one PITR restore drill before decommissioning prior infrastructure. The restore helper is described in the README; detailed PITR validation documentation was not present in the initial release.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for configuration-first setups relies on consolidating fragmented state. The underlying technologies behave as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;OpenDataLoader PDF&lt;/strong&gt;: The documented pattern for PDF ingestion replaces separate layout detection and OCR passes with a unified pipeline. It uses hybrid fallback, meaning it defaults to local deterministic extraction and calls an external API only for complex layouts, standardizing the workflow into a single function call.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SuperPlane&lt;/strong&gt;: Policy-gated deployments depend on tracking multiple asynchronous conditions. SuperPlane’s documented behavior involves modeling these conditions as a directed graph (“Canvas”), executing them based on external events, and maintaining a centralized state ledger to replace fragmented CI and chat logs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;pgrwl&lt;/strong&gt;: PostgreSQL’s &lt;code&gt;pg_receivewal&lt;/code&gt; behaves as a continuous streaming daemon, while base backups are distinct scheduled processes. pgrwl’s documented pattern consolidates these by maintaining a persistent WAL replication connection while executing base backups from the same binary, reducing the number of external dependencies required for point-in-time recovery.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;OpenDataLoader PDF local mode accuracy&lt;/td&gt;&lt;td&gt;Complex multi-column or heavily formatted layouts hit edge cases&lt;/td&gt;&lt;td&gt;Use hybrid mode for known-complex document types; budget for AI service cost&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OpenDataLoader PDF Java runtime requirement&lt;/td&gt;&lt;td&gt;Python-only CI environments lack JVM&lt;/td&gt;&lt;td&gt;Pin Java 11+ in the build image before adding the library&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;SuperPlane alpha API changes&lt;/td&gt;&lt;td&gt;Breaking changes in Canvas API affect running workflow definitions&lt;/td&gt;&lt;td&gt;Pin to a specific release tag; subscribe to changelog before upgrading&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;SuperPlane connector gaps&lt;/td&gt;&lt;td&gt;Workflow depends on a tool without a built-in integration&lt;/td&gt;&lt;td&gt;Implement custom component using the SDK; expect engineering time investment&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;pgrwl restore path untested&lt;/td&gt;&lt;td&gt;Running for months without verifying a restore works&lt;/td&gt;&lt;td&gt;Schedule a quarterly PITR drill into a test environment&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;pgrwl early-release risk&lt;/td&gt;&lt;td&gt;No published production validation for the May 2025 release&lt;/td&gt;&lt;td&gt;Run parallel to existing backup tooling for 60 days before decommissioning&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Document ingestion for RAG, deployment policy enforcement, and PostgreSQL backup each require multi-tool setup that breaks in predictable and expensive ways — parser tuning failures reduce retrieval accuracy, untested backup stacks fail at recovery time, and manual deploy gates create compliance gaps when engineers leave.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: OpenDataLoader PDF for accurate multi-layout PDF extraction with no per-document tuning, SuperPlane for event-driven deployment governance with a durable audit trail, pgrwl for single-binary PostgreSQL WAL streaming and base backup.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: A successful OpenDataLoader PDF extraction of a complex multi-column document returns structured Markdown with correct table boundaries; a pgrwl startup log shows WAL streaming active and base backup completed without manual scheduling configuration.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Run &lt;code&gt;pip install opendataloader-pdf&lt;/code&gt; and extract one representative PDF from your existing corpus — compare table accuracy against your current parser on a document that previously required manual post-processing.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>architecture</category></item><item><title>Surviving Aurora MySQL: I/O Billing Traps and Small-Instance Cliffs</title><link>https://rajivonai.com/blog/2025-06-20-aurora-mysql-io-billing-traps-metadata-locks/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-06-20-aurora-mysql-io-billing-traps-metadata-locks/</guid><description>The brutal realities of scaling Amazon Aurora MySQL, from IOPS billing surprises to network limits on smaller instances.</description><pubDate>Fri, 20 Jun 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Amazon Aurora MySQL is marketed as a drop-in, highly available, limitless scaling replacement for standard MySQL. For many workloads, it is. But when a system hits the physical constraints of AWS’s distributed architecture, the failure modes are entirely different from bare-metal servers.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A rapidly growing startup migrates from a standard Amazon RDS MySQL instance to Amazon Aurora, enticed by the promise of sub-10ms replica lag and rapid failover. They deploy an &lt;code&gt;r5.large&lt;/code&gt; instance to save costs on compute, knowing that Aurora’s storage scales automatically.&lt;/p&gt;
&lt;p&gt;In the first month, everything runs smoothly. By the third month, the database bill balloons month over month, driven almost entirely by line items for “Aurora I/O requests” — this is an illustrative pattern, not a specific observed case, but it is the recurring shape of the complaint in Aurora Standard billing threads. A week later, during a traffic surge, the database completely locks up. The CPU is only at 40%, but queries are timing out, and the application is failing.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Aurora separates compute from storage. This architectural marvel provides incredible resilience, but it introduces three distinct pain points that catch teams off guard:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The I/O Billing Trap:&lt;/strong&gt; In Aurora Standard, you pay for every single I/O operation. A badly written query that scans 10 million rows doesn’t just waste time; it costs hard dollars. Similarly, writing to a table with 15 secondary indexes means you generate massive amounts of redo log data, generating millions of Write IOPS charges.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Small-Instance Cliff:&lt;/strong&gt; Aurora storage relies heavily on the EC2 instance’s network bandwidth to communicate with the 6 distributed storage nodes. Smaller instances (like &lt;code&gt;t3&lt;/code&gt;, &lt;code&gt;r5.large&lt;/code&gt;, or &lt;code&gt;r6g.large&lt;/code&gt;) have strict network baselines. When you exceed them, AWS throttles the network silently at the hypervisor level. The database CPU looks fine, but the network queues are saturated.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Metadata Lock Chain Reactions:&lt;/strong&gt; Aurora’s shared cluster volume means that schema changes can have cluster-wide implications. A poorly timed &lt;code&gt;ALTER TABLE&lt;/code&gt; on the writer can stall replication and cause metadata locks that cascade back to the application.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The core question is: how do you architect a workload for Aurora MySQL to avoid financial traps and network starvation?&lt;/p&gt;
&lt;h2 id=&quot;aurora-constraint-architecture&quot;&gt;Aurora Constraint Architecture&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Application Workload] --&gt; B[Aurora Writer Instance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[Network Bandwidth Limit — e.g. r5.large]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[Distributed Storage Fleet]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[Redo Log Generation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[Write IOPS Billing]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; G[Page Cache Misses]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[Read IOPS Billing]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; I[Shared Cluster Volume]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[Aurora Reader Instance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for surviving the Aurora I/O Billing Trap is two-fold. First, aggressive buffer pool tuning. By ensuring &lt;code&gt;innodb_buffer_pool_size&lt;/code&gt; is utilizing 75% of the instance’s memory, you force reads to happen in RAM, avoiding Read IOPS charges entirely. Second, for write-heavy workloads, AWS explicitly recommends migrating to the &lt;strong&gt;Aurora I/O-Optimized&lt;/strong&gt; billing tier. This tier charges a flat rate for compute and storage, eliminating I/O request charges entirely. AWS’s general guidance is that once I/O charges make up a meaningful share of your total database bill, I/O-Optimized tends to be cheaper — run the numbers against your own I/O-vs-compute split using AWS’s pricing calculator rather than treating any single percentage as a fixed rule.&lt;/p&gt;
&lt;p&gt;To survive the Small-Instance Cliff, engineers must stop looking at CPU metrics alone. In CloudWatch, you must monitor &lt;code&gt;NetworkReceiveThroughput&lt;/code&gt; and &lt;code&gt;NetworkTransmitThroughput&lt;/code&gt;. If your workload requires heavy sorting, joining, or scanning, a small instance will hit its EBS/Network limits long before it runs out of CPU. The solution is counter-intuitive but necessary: scale up the instance size (e.g., to an &lt;code&gt;r6g.2xlarge&lt;/code&gt;) purely to buy a larger network pipe, even if the CPU remains 90% idle.&lt;/p&gt;
&lt;p&gt;For Metadata Locks, the rule is absolute: never run blocking DDL (&lt;code&gt;ALTER TABLE&lt;/code&gt;) directly in production during peak hours. You must use tools like &lt;code&gt;pt-online-schema-change&lt;/code&gt; or GitHub’s &lt;code&gt;gh-ost&lt;/code&gt; to perform schema migrations via shadow tables, regardless of Aurora’s “fast DDL” marketing claims.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Assumption&lt;/th&gt;&lt;th&gt;Reality&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Aurora storage is infinite&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Storage capacity is large, but network bandwidth to reach it is strictly tied to your EC2 instance size.&lt;/td&gt;&lt;td&gt;Monitor instance network limits; scale up for bandwidth, not just CPU.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;More indexes speed up the database&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Every index multiplies redo log generation, skyrocketing Write IOPS costs in Aurora Standard.&lt;/td&gt;&lt;td&gt;Ruthlessly drop unused secondary indexes; use the Performance Schema to prove usage.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;CPU is the primary bottleneck&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;I/O wait times (&lt;code&gt;io/redo_log_flush&lt;/code&gt;) and network saturation are the true limits of distributed databases.&lt;/td&gt;&lt;td&gt;Alert on &lt;code&gt;CommitLatency&lt;/code&gt; and &lt;code&gt;VolumeQueueLength&lt;/code&gt; instead of CPU.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Standard billing is always cheaper&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;High-throughput logging or IoT telemetry workloads will generate bankrupting I/O charges.&lt;/td&gt;&lt;td&gt;Switch to Aurora I/O-Optimized billing tier for predictable costs.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Treating Amazon Aurora exactly like bare-metal MySQL leads to network throttling and massive I/O billing surprises due to the distributed storage architecture.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Monitor network bandwidth limits, aggressively tune the buffer pool to eliminate Read IOPS, and evaluate the I/O-Optimized tier for heavy write workloads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; AWS cost calculators and CloudWatch network metrics explicitly demonstrate the correlation between small instance types, I/O frequency, and cost spikes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Open your AWS Billing Dashboard today. Group your RDS costs by usage type. If IOPS charges make up a meaningful share of the total cluster cost, run your I/O-vs-compute split through AWS’s pricing calculator to see whether the Aurora I/O-Optimized tier wins for your workload — it requires no downtime and takes effect immediately.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>failures</category><category>architecture</category></item><item><title>Aurora DSQL Is PostgreSQL-Compatible—but It Isn&apos;t PostgreSQL: Know the Limits</title><link>https://rajivonai.com/blog/2025-06-17-aurora-dsql-postgresql-compatible-not-postgresql/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-06-17-aurora-dsql-postgresql-compatible-not-postgresql/</guid><description>A workload-first evaluation of Aurora DSQL covering optimistic concurrency, transaction limits, schema compatibility, multi-Region writes, retries, recovery, and migration fit.</description><pubDate>Tue, 17 Jun 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A PostgreSQL driver connecting successfully to Aurora DSQL proves protocol compatibility. It does not prove that the application’s transaction semantics, database features, migration scripts, or operational assumptions are compatible.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;RetailCo wants its order-authorization service to accept writes in two AWS Regions. The service owns checkout idempotency, reservation state, and a small order-status ledger. A regional database failover would be simpler if both application regions could read and write one strongly consistent logical database.&lt;/p&gt;
&lt;p&gt;The candidate workload has several characteristics that make a distributed database plausible:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;most transactions insert or update fewer than twenty rows;&lt;/li&gt;
&lt;li&gt;rows have application-generated UUIDs;&lt;/li&gt;
&lt;li&gt;transactions normally concern one order or one reservation;&lt;/li&gt;
&lt;li&gt;cross-order reporting can run outside the synchronous write path;&lt;/li&gt;
&lt;li&gt;the application already understands idempotency and bounded retries.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The existing PostgreSQL estate also contains patterns that may not migrate:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;foreign keys and triggers enforce part of the order workflow;&lt;/li&gt;
&lt;li&gt;PL/pgSQL functions contain business logic;&lt;/li&gt;
&lt;li&gt;reconciliation jobs use temporary tables and large transactions;&lt;/li&gt;
&lt;li&gt;schema tools assume several DDL statements and data backfills can share a transaction;&lt;/li&gt;
&lt;li&gt;connection pools expect long-lived password-authenticated sessions.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;RetailCo is a reference architecture, not a claimed customer deployment. The transaction sizes, topology, and migration patterns are scenario inputs. No latency, throughput, availability, conflict-rate, recovery, or cost result in this article is presented as measured fact.&lt;/p&gt;
&lt;p&gt;Aurora DSQL changed materially between its May 2025 general availability and this article’s March 31, 2026 cutoff. AWS added identity columns and sequence objects in February 2026, removing one launch-era compatibility gap. The service still needs an exact cutoff because current documentation can describe capabilities that did not exist when an earlier design was made: &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/release-notes.html&quot;&gt;Aurora DSQL release notes&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The current architecture is not a PostgreSQL primary with replicas. A multi-Region Aurora DSQL database has two peered Regional clusters with writable endpoints and a third witness Region that participates in transaction-log quorum but exposes no query endpoint:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  AppA[application — Region A] --&gt; EndA[DSQL endpoint — Region A]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  AppB[application — Region B] --&gt; EndB[DSQL endpoint — Region B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  EndA --&gt; Logical[one strongly consistent logical database]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  EndB --&gt; Logical&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Logical --&gt; Witness[witness Region — encrypted transaction log]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Logical --&gt; StorageA[distributed storage — Region A]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Logical --&gt; StorageB[distributed storage — Region B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;AWS documents both endpoints as concurrently readable and writable with strong consistency. The witness stores a limited window of encrypted transaction logs and has no client endpoint. Multi-Region clusters must use Regions from one supported Region set; cross-continent multi-Region clusters are not currently supported: &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/what-is-aurora-dsql.html&quot;&gt;What is Aurora DSQL?&lt;/a&gt; and &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/getting-started.html#multi-region&quot;&gt;creating a multi-Region cluster&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;That architecture directly addresses the single-writer ownership problem described earlier in this series. It does not make an arbitrary PostgreSQL workload distributed without redesign.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The phrase &lt;em&gt;PostgreSQL-compatible&lt;/em&gt; compresses several different questions into one label:&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Compatibility layer&lt;/th&gt;&lt;th&gt;What to ask&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Wire protocol&lt;/td&gt;&lt;td&gt;Can the driver connect and exchange PostgreSQL messages?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;SQL syntax and types&lt;/td&gt;&lt;td&gt;Does the application use only the supported subset?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Transaction semantics&lt;/td&gt;&lt;td&gt;Do isolation, conflicts, retries, and limits preserve business invariants?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Database programmability&lt;/td&gt;&lt;td&gt;Are required triggers, procedural functions, extensions, and constraints available?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Operations&lt;/td&gt;&lt;td&gt;Do authentication, pooling, observability, backup, and schema deployment fit?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Performance&lt;/td&gt;&lt;td&gt;Does this transaction and key distribution work under representative concurrency in each Region?&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Aurora DSQL uses PostgreSQL’s version 3 wire protocol and core PostgreSQL components including its parser, planner, optimizer, and type system. It is currently based on PostgreSQL 16. AWS describes its transaction isolation as equivalent to PostgreSQL &lt;code&gt;REPEATABLE READ&lt;/code&gt;: &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with.html&quot;&gt;Aurora DSQL and PostgreSQL&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Underneath that interface, Aurora DSQL uses optimistic concurrency control and a distributed catalog. It does not reproduce the storage, locking, extension, process, configuration, or administration model of a PostgreSQL server.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Client[PostgreSQL client or ORM] --&gt; Protocol[PostgreSQL wire protocol]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Protocol --&gt; Parser[PostgreSQL-compatible SQL surface]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Parser --&gt; OCC[optimistic concurrency control]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  OCC --&gt; Catalog[distributed schema catalog]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  OCC --&gt; Log[distributed transaction log]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  OCC --&gt; Storage[automatically partitioned storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Storage --&gt; Result[compatible result for supported behavior]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The critical architecture question is therefore not:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Does Aurora DSQL speak PostgreSQL?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It is:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Can RetailCo express and verify its order invariants using Aurora DSQL’s supported transaction model without importing assumptions from conventional PostgreSQL?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;evaluate-the-transaction-shape-before-the-product-name&quot;&gt;Evaluate the Transaction Shape Before the Product Name&lt;/h2&gt;
&lt;p&gt;RetailCo should evaluate Aurora DSQL in five gates: transaction semantics, key distribution, feature compatibility, operational lifecycle, and measured workload behavior. A failure at an early gate should stop the migration before a benchmark creates false confidence.&lt;/p&gt;
&lt;h3 id=&quot;gate-1--design-for-abort-and-retry&quot;&gt;Gate 1 — design for abort and retry&lt;/h3&gt;
&lt;p&gt;Aurora DSQL uses optimistic concurrency control. Transactions proceed without traditional blocking locks, and conflicting work is evaluated at commit. A data conflict returns PostgreSQL SQLSTATE &lt;code&gt;40001&lt;/code&gt; with OCC code &lt;code&gt;OC000&lt;/code&gt;. A stale distributed catalog can return the same SQLSTATE with code &lt;code&gt;OC001&lt;/code&gt;: &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with-concurrency-control.html&quot;&gt;Aurora DSQL concurrency control&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Deadlock-free does not mean conflict-free. It means a conflict becomes an aborted transaction instead of a lock wait or deadlock cycle.&lt;/p&gt;
&lt;p&gt;The retry boundary must be the complete transaction:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Start[start transaction attempt] --&gt; Read[read required state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Read --&gt; Validate[validate business invariant]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Validate --&gt; Write[write all database changes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Write --&gt; Commit[attempt commit]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Commit --&gt; Success{commit succeeded}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Success --&gt;|yes| Publish[return durable success]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Success --&gt;|no| Classify{retryable 40001}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Classify --&gt;|yes| Backoff[bounded backoff with jitter]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Backoff --&gt; Start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Classify --&gt;|no| Fail[fail and preserve diagnostics]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The second attempt must open a new transaction and repeat every database read. Retrying only &lt;code&gt;COMMIT&lt;/code&gt; would reuse decisions made from the losing snapshot.&lt;/p&gt;
&lt;p&gt;The retry implementation also needs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a maximum attempt count and total time budget;&lt;/li&gt;
&lt;li&gt;exponential or decorrelated backoff with jitter;&lt;/li&gt;
&lt;li&gt;metrics split by &lt;code&gt;OC000&lt;/code&gt;, &lt;code&gt;OC001&lt;/code&gt;, exhausted retries, and operation;&lt;/li&gt;
&lt;li&gt;an idempotency key for the business command;&lt;/li&gt;
&lt;li&gt;no irreversible external side effect before the database commit;&lt;/li&gt;
&lt;li&gt;safe handling when the client loses the connection and cannot tell whether commit succeeded.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For checkout, RetailCo should store the idempotency key and the order transition in the same transaction. A retry then re-reads that record and returns the already committed result rather than charging, reserving, or publishing twice.&lt;/p&gt;
&lt;h3 id=&quot;gate-2--distinguish-snapshot-isolation-from-serializable-execution&quot;&gt;Gate 2 — distinguish snapshot isolation from serializable execution&lt;/h3&gt;
&lt;p&gt;Aurora DSQL provides strong consistency and ACID transactions at a fixed isolation level equivalent to PostgreSQL &lt;code&gt;REPEATABLE READ&lt;/code&gt;. Those are important guarantees. They do not justify silently labeling every business invariant serializable.&lt;/p&gt;
&lt;p&gt;Snapshot isolation gives a transaction a stable snapshot and detects conflicting writes. A multi-row invariant can still require a separate correctness design when concurrent transactions read different rows and write disjoint rows. The safe approach is to test the exact invariant rather than infer behavior from the words &lt;em&gt;ACID&lt;/em&gt; or &lt;em&gt;strong consistency&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;RetailCo should include a write-skew test:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Invariant: at least one fraud-reviewer assignment remains active&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Transaction A:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  reads reviewer A and reviewer B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  sees both active&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  disables reviewer A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Transaction B:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  reads reviewer A and reviewer B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  sees both active&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  disables reviewer B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The lab must determine whether both can commit for the installed service behavior. If they can, RetailCo must move the invariant onto a common write key, use a unique or checkable representation that forces a conflict, or place the decision behind one application owner. This article does not claim the lab result.&lt;/p&gt;
&lt;p&gt;Aurora DSQL now supports a narrow &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; form: equality predicates on every primary-key column, against a single table. Range, &lt;code&gt;IN&lt;/code&gt;, &lt;code&gt;OR&lt;/code&gt;, and joined forms are rejected. That is not a drop-in replacement for every PostgreSQL locking query: &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with-postgresql-compatibility-supported-sql-features.html&quot;&gt;supported SQL features&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;gate-3--distribute-keys-and-avoid-hot-ownership&quot;&gt;Gate 3 — distribute keys and avoid hot ownership&lt;/h3&gt;
&lt;p&gt;Aurora DSQL physically organizes data by primary key and uses that key in its cluster-wide row identity and automatic partitioning. AWS recommends random primary keys for high-write tables and warns that monotonically increasing integers can direct new inserts toward one partition: &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with-primary-keys.html&quot;&gt;Aurora DSQL primary keys&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Use an explicit, distributed key even though Aurora DSQL can synthesize a hidden ID for a table without a declared primary key:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SCHEMA&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.order_state (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id          uuid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DEFAULT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; gen_random_uuid(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id         uuid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    idempotency_key   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;200&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_status      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;40&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    total_amount      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;18&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    updated_at        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DEFAULT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; current_timestamp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; UNIQUE INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ASYNC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; order_state_tenant_idempotency_uq&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;order_state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, idempotency_key);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;An explicit primary key improves application reasoning, query access, idempotency, and the identity carried in outbox events and reconciliation records.&lt;/p&gt;
&lt;p&gt;Sequences and identity columns are supported as of February 2026. That removes an earlier compatibility gap, but it does not make an ascending key ideal for a high-volume insert path. AWS notes that sequence cache size 1 can increase coordination overhead in a distributed system: &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/create-sequence-syntax-support.html&quot;&gt;Aurora DSQL sequences&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The more important hot-key problem is update contention. A single row holding a global inventory count, account balance, or daily sequence remains one write-conflict domain even when the database has enormous distributed capacity.&lt;/p&gt;
&lt;p&gt;For inventory, prefer ownership units that can change independently:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;global product counter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        becomes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;inventory by product, location, and reservation bucket&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That redesign is valid only if the business can reconcile those units without violating its sellable-quantity invariant. Sharding a counter is an application correctness decision, not merely a performance technique.&lt;/p&gt;
&lt;h3 id=&quot;gate-4--fit-inside-the-transaction-contract&quot;&gt;Gate 4 — fit inside the transaction contract&lt;/h3&gt;
&lt;p&gt;Current Aurora DSQL database limits include:&lt;/p&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Limit&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Current documented value&lt;/th&gt;&lt;th&gt;Architectural effect&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Maximum transaction duration&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;5 minutes&lt;/td&gt;&lt;td&gt;Long workflows and migrations must be decomposed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Rows mutated in a transaction block&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;3,000&lt;/td&gt;&lt;td&gt;Bulk settlement and backfills need bounded batches&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Modified data in one write transaction&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;10 MiB&lt;/td&gt;&lt;td&gt;Row count alone is not a safe batch limit&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Query memory base amount&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;128 MiB per transaction&lt;/td&gt;&lt;td&gt;Large joins and sorts require workload tests&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Databases per cluster&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;1&lt;/td&gt;&lt;td&gt;Database-per-service requires multiple clusters or schemas&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schemas per database&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;10&lt;/td&gt;&lt;td&gt;Tenant-per-schema and database consolidation need review&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tables per database&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;1,000&lt;/td&gt;&lt;td&gt;High-object-count designs need an inventory&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Connection duration&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;60 minutes&lt;/td&gt;&lt;td&gt;Pools must recycle connections proactively&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;These are service limits, not tuning parameters. AWS marks the transaction duration, row mutation, modified-data, and database-count limits as non-adjustable: &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/CHAP_quotas.html&quot;&gt;Aurora DSQL quotas and limits&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;RetailCo’s twenty-row authorization transaction fits comfortably in shape. Its 20,000-row settlement does not. The settlement must become restartable batches with progress checkpoints and idempotent reconciliation—or remain on another database.&lt;/p&gt;
&lt;p&gt;Batching is not automatically equivalent to one atomic settlement. If the business rule requires all 20,000 changes to commit or none, splitting the transaction changes semantics. RetailCo must redesign the workflow around an explicit state machine, staging records, and a final visibility decision, or reject Aurora DSQL for that path.&lt;/p&gt;
&lt;h3 id=&quot;gate-5--inventory-postgresql-features-not-only-sql-statements&quot;&gt;Gate 5 — inventory PostgreSQL features, not only SQL statements&lt;/h3&gt;
&lt;p&gt;AWS’s migration guide identifies several current differences:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;one built-in database named &lt;code&gt;postgres&lt;/code&gt; per cluster;&lt;/li&gt;
&lt;li&gt;no temporary tables;&lt;/li&gt;
&lt;li&gt;referential integrity validation moves to the application layer;&lt;/li&gt;
&lt;li&gt;trigger-like logic moves to the application or event-driven processing;&lt;/li&gt;
&lt;li&gt;SQL-language functions are supported, but procedural languages such as PL/pgSQL are not;&lt;/li&gt;
&lt;li&gt;system configuration and tablespaces are service-managed;&lt;/li&gt;
&lt;li&gt;the transaction isolation level is fixed at &lt;code&gt;REPEATABLE READ&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;DDL and DML must use separate transactions;&lt;/li&gt;
&lt;li&gt;one transaction can contain only one DDL statement.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;See &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with-postgresql-compatibility-migration-guide.html&quot;&gt;Migrating from PostgreSQL to Aurora DSQL&lt;/a&gt; and &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with-ddl.html&quot;&gt;DDL and distributed transactions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The current supported-SQL list includes &lt;code&gt;CREATE FUNCTION&lt;/code&gt; with &lt;code&gt;LANGUAGE SQL&lt;/code&gt;; it does not include &lt;code&gt;CREATE TRIGGER&lt;/code&gt;, procedural functions, or &lt;code&gt;CREATE EXTENSION&lt;/code&gt;. The current system-catalog matrix marks &lt;code&gt;pg_trigger&lt;/code&gt;, &lt;code&gt;pg_event_trigger&lt;/code&gt;, &lt;code&gt;pg_extension&lt;/code&gt;, foreign-data-wrapper catalogs, publications, subscriptions, and partition metadata as unavailable: &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with-systems-tables.html&quot;&gt;Aurora DSQL system tables&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This creates a migration boundary:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Existing[existing PostgreSQL application] --&gt; Scan[scan SQL schema ORM and operations]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Scan --&gt; Direct[supported SQL and types]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Scan --&gt; Rewrite[triggers PLpgSQL temporary tables and FK logic]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Scan --&gt; Reject[required extension or transaction semantic cannot move]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Direct --&gt; Lab[compatibility and concurrency lab]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Rewrite --&gt; Lab&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Lab --&gt; Decide{all invariants proven}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Decide --&gt;|yes| Migrate[migrate bounded service]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Decide --&gt;|no| Alternative[retain PostgreSQL or choose another scale path]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Foreign keys deserve special attention. Replacing a constraint with “the application checks first” can introduce a time-of-check-to-time-of-use race unless the validation and dependent write share a transaction and concurrent deletion is controlled. It also means every writer, migration tool, and repair script must obey the same rule. For relationships where orphan prevention is non-negotiable, encode the ownership differently, create a repairable asynchronous invariant with monitoring, or use a database that enforces the constraint.&lt;/p&gt;
&lt;h2 id=&quot;design-the-retailco-service-for-dsql&quot;&gt;Design the RetailCo Service for DSQL&lt;/h2&gt;
&lt;p&gt;Aurora DSQL is a better fit for a deliberately bounded service than for RetailCo’s 40-TB PostgreSQL monolith.&lt;/p&gt;
&lt;h3 id=&quot;keep-the-synchronous-transaction-small-and-self-contained&quot;&gt;Keep the synchronous transaction small and self-contained&lt;/h3&gt;
&lt;p&gt;One order-authorization transaction should contain only database changes required to decide and record the authorization:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;idempotency&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    idempotency_key,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    result_code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ($&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;PENDING&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CONFLICT (tenant_id, idempotency_key) DO NOTHING;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Read the idempotency row and current order state.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Validate the transition inside this transaction.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;order_state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; order_status &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    updated_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; current_timestamp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; order_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; order_status &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;outbox&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    event_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    event_type,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    event_payload,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    created_at&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ($&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, current_timestamp);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;COMMIT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is illustrative SQL, not a complete application implementation. At the March 2026 cutoff, the outbox payload must use a supported scalar representation such as text or normalized columns rather than assuming unsupported JSON types. The schema must include matching unique keys, and the application must check affected-row counts. A payment call must not occur between &lt;code&gt;BEGIN&lt;/code&gt; and &lt;code&gt;COMMIT&lt;/code&gt;; external side effects need their own idempotent workflow.&lt;/p&gt;
&lt;h3 id=&quot;do-not-assume-a-native-cdc-stream-exists&quot;&gt;Do not assume a native CDC stream exists&lt;/h3&gt;
&lt;p&gt;At this article’s March 31, 2026 cutoff, Aurora DSQL did not provide a native Kinesis CDC capability. A design that needs committed row changes must therefore treat change propagation as an application responsibility rather than quietly assuming PostgreSQL logical decoding or a native database stream is available.&lt;/p&gt;
&lt;p&gt;RetailCo can write an explicit outbox row in the same bounded order transaction, then let an application worker claim and publish pending rows idempotently. That worker needs retries, deduplication keys, visible delivery state, and reconciliation. The outbox keeps the business change and publication intent atomic, but it does not make email, payment, inventory, or search side effects exactly once.&lt;/p&gt;
&lt;h3 id=&quot;treat-schema-deployment-as-asynchronous-distributed-work&quot;&gt;Treat schema deployment as asynchronous distributed work&lt;/h3&gt;
&lt;p&gt;Aurora DSQL distributes its schema catalog. A completed DDL change can cause another session with a stale catalog to receive &lt;code&gt;OC001&lt;/code&gt;; retry normally refreshes the catalog.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;CREATE INDEX ASYNC&lt;/code&gt; returns a job ID rather than waiting for an index to finish. The index is not ready until the job completes and &lt;code&gt;pg_index.indisvalid&lt;/code&gt; becomes true. A failed index can remain invalid and needs to be dropped and recreated. Catalog activation can itself cause concurrency errors for sessions using the namespace: &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/working-with-create-index-async.html&quot;&gt;Aurora DSQL asynchronous indexes&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The deployment pipeline should therefore:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;run one DDL statement in its own transaction;&lt;/li&gt;
&lt;li&gt;capture asynchronous job IDs;&lt;/li&gt;
&lt;li&gt;wait for completion and verify catalog state;&lt;/li&gt;
&lt;li&gt;tolerate &lt;code&gt;OC001&lt;/code&gt; in application sessions;&lt;/li&gt;
&lt;li&gt;deploy code using an expand-and-contract compatibility window;&lt;/li&gt;
&lt;li&gt;run any data backfill as bounded DML batches;&lt;/li&gt;
&lt;li&gt;remove old schema only after old application versions are gone.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Do not translate a PostgreSQL migration file containing ten statements inside &lt;code&gt;BEGIN&lt;/code&gt; and &lt;code&gt;COMMIT&lt;/code&gt; into ten unobserved Aurora DSQL autocommits. That changes failure and rollback behavior.&lt;/p&gt;
&lt;h3 id=&quot;make-iam-authentication-part-of-pool-design&quot;&gt;Make IAM authentication part of pool design&lt;/h3&gt;
&lt;p&gt;Aurora DSQL uses signed IAM tokens instead of persistent database passwords. A token authenticates a new connection; an established session can remain active after that token expires. The database connection itself has a maximum duration of one hour: &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/accessing.html&quot;&gt;Aurora DSQL client access&lt;/a&gt; and &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/SECTION_authentication-token.html&quot;&gt;authentication tokens&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Use a supported connector or pool hook that generates a fresh token when opening a connection. Configure pool lifetime below 3,600 seconds and add jitter so the fleet does not replace every connection simultaneously. AWS’s Python connector example uses a 3,300-second maximum lifetime for this reason: &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/SECTION_program-with-dsql-connector-for-python.html&quot;&gt;Aurora DSQL Connector for Python&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;plan-recovery-even-when-infrastructure-is-serverless&quot;&gt;Plan recovery even when infrastructure is serverless&lt;/h3&gt;
&lt;p&gt;Aurora DSQL integrates with AWS Backup for full-cluster backups, schedules, retention, cross-account and cross-Region copies, and Vault Lock. A restore creates a new cluster rather than overwriting the source. Restoring a multi-Region cluster requires an identical backup copy in every target cluster Region before the restore begins: &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/backup-aurora-dsql.html&quot;&gt;Aurora DSQL backup and restore&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;RetailCo still needs to test:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;restoration into a network-isolated environment;&lt;/li&gt;
&lt;li&gt;IAM and database-role reconstruction;&lt;/li&gt;
&lt;li&gt;application connection to the new endpoint;&lt;/li&gt;
&lt;li&gt;outbox relay restart, duplicate-delivery handling, and downstream reconciliation;&lt;/li&gt;
&lt;li&gt;DNS or configuration cutover;&lt;/li&gt;
&lt;li&gt;validation of orders, reservations, and idempotency state;&lt;/li&gt;
&lt;li&gt;the observed recovery time and recovery point.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Serverless removes server restoration work. It does not prove application recovery.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;RetailCo should run a rejection-oriented lab before a migration pilot. The goal is to find incompatible invariants and workload shapes early, not to produce an attractive peak-TPS chart.&lt;/p&gt;
&lt;h3 id=&quot;test-1--compatibility-inventory&quot;&gt;Test 1 — compatibility inventory&lt;/h3&gt;
&lt;p&gt;Extract the current schema, migration history, SQL statements, ORM-generated SQL, and operational scripts. Classify every object or command:&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Status&lt;/th&gt;&lt;th&gt;Meaning&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Supported directly&lt;/td&gt;&lt;td&gt;Current AWS documentation and the lab both accept it&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Rewrite required&lt;/td&gt;&lt;td&gt;A documented alternative exists, but semantics must be re-proven&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Service boundary change&lt;/td&gt;&lt;td&gt;Logic moves to another component or database&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Blocking incompatibility&lt;/td&gt;&lt;td&gt;No acceptable replacement preserves the requirement&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Include triggers, functions, extensions, foreign keys, temporary tables, sequences, &lt;code&gt;TRUNCATE&lt;/code&gt;, lock clauses, isolation settings, schema count, table count, index count, and driver initialization SQL.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result to verify:&lt;/strong&gt; every production path has an owner and disposition. This article claims no completed inventory.&lt;/p&gt;
&lt;h3 id=&quot;test-2--transaction-correctness-under-concurrency&quot;&gt;Test 2 — transaction correctness under concurrency&lt;/h3&gt;
&lt;p&gt;Run at least these cases from both writable Regions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;independent UUID-key inserts;&lt;/li&gt;
&lt;li&gt;concurrent updates to the same order row;&lt;/li&gt;
&lt;li&gt;concurrent inserts using the same idempotency key;&lt;/li&gt;
&lt;li&gt;the write-skew invariant described earlier;&lt;/li&gt;
&lt;li&gt;a transaction that approaches each row, byte, and duration limit;&lt;/li&gt;
&lt;li&gt;a connection loss immediately before or after commit acknowledgment;&lt;/li&gt;
&lt;li&gt;application retry exhaustion under sustained contention.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Record success, &lt;code&gt;OC000&lt;/code&gt;, &lt;code&gt;OC001&lt;/code&gt;, other SQLSTATEs, retry count, commit latency, and invariant violations. AWS exposes CloudWatch metrics including &lt;code&gt;OccConflicts&lt;/code&gt;, &lt;code&gt;QueryTimeouts&lt;/code&gt;, &lt;code&gt;CommitLatency&lt;/code&gt;, transaction counts, bytes read and written, compute time, storage size, and DPU use: &lt;a href=&quot;https://docs.aws.amazon.com/aurora-dsql/latest/userguide/cloudwatch-monitoring.html&quot;&gt;Monitoring Aurora DSQL&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result to verify:&lt;/strong&gt; retries preserve correctness and remain within the application’s latency and error budgets. No conflict rate or latency result is claimed here.&lt;/p&gt;
&lt;h3 id=&quot;test-3--schema-change-under-application-load&quot;&gt;Test 3 — schema change under application load&lt;/h3&gt;
&lt;p&gt;Run one supported &lt;code&gt;ALTER TABLE&lt;/code&gt;, an asynchronous non-unique index, and an asynchronous unique index while old and new application versions issue transactions.&lt;/p&gt;
&lt;p&gt;Verify:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;OC001&lt;/code&gt; is retried safely;&lt;/li&gt;
&lt;li&gt;the application does not use the index before it becomes valid;&lt;/li&gt;
&lt;li&gt;a duplicate-data failure leaves an invalid index that the runbook removes;&lt;/li&gt;
&lt;li&gt;the deployment can resume after interruption;&lt;/li&gt;
&lt;li&gt;no migration assumes transactional rollback across multiple DDL statements.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;test-4--operational-failure-and-recovery&quot;&gt;Test 4 — operational failure and recovery&lt;/h3&gt;
&lt;p&gt;Exercise connection recycling, expired token replacement, one Regional application path becoming unavailable, outbox relay interruption and redelivery, an impaired downstream destination, and AWS Backup restoration to a new cluster.&lt;/p&gt;
&lt;p&gt;For a multi-Region test, measure commit behavior from both Regions before and during each supported failure injection. Do not infer the application’s availability from the service’s published availability target.&lt;/p&gt;
&lt;h3 id=&quot;test-5--compare-the-correct-alternatives&quot;&gt;Test 5 — compare the correct alternatives&lt;/h3&gt;
&lt;p&gt;Use the same schema subset and command mix against:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;standard Aurora PostgreSQL;&lt;/li&gt;
&lt;li&gt;Aurora PostgreSQL Limitless Database if its feature set fits;&lt;/li&gt;
&lt;li&gt;Aurora DSQL;&lt;/li&gt;
&lt;li&gt;application-sharded Aurora clusters if tenant routing is acceptable.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The earlier &lt;a href=&quot;https://rajivonai.com/blog/2025-01-21-scaling-writes-amazon-aurora-what-works/&quot;&gt;Aurora write-scaling article&lt;/a&gt; explains how those architectures distribute ownership differently. The comparison should include compatibility work, application retries, multi-Region commit latency, conflict behavior, backup restoration, observability, and operational ownership—not only throughput.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;







































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Workload assumption&lt;/th&gt;&lt;th&gt;What DSQL actually requires&lt;/th&gt;&lt;th&gt;Failure if ignored&lt;/th&gt;&lt;th&gt;Recommendation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;PostgreSQL driver means drop-in migration&lt;/td&gt;&lt;td&gt;Supported protocol, SQL, and behavior only&lt;/td&gt;&lt;td&gt;Runtime SQL and semantic failures&lt;/td&gt;&lt;td&gt;Inventory and replay real application SQL&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ACID means serializable&lt;/td&gt;&lt;td&gt;Fixed snapshot isolation equivalent to Repeatable Read&lt;/td&gt;&lt;td&gt;Multi-row invariant can remain unproven&lt;/td&gt;&lt;td&gt;Run write-skew tests and redesign ownership if needed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lock-free means contention-free&lt;/td&gt;&lt;td&gt;OCC aborts conflicting transactions at commit&lt;/td&gt;&lt;td&gt;Retry storms and tail-latency growth&lt;/td&gt;&lt;td&gt;Spread keys, bound retries, monitor &lt;code&gt;OccConflicts&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;More Regions remove latency&lt;/td&gt;&lt;td&gt;Synchronous strong consistency coordinates writes&lt;/td&gt;&lt;td&gt;Commit latency may exceed request budget&lt;/td&gt;&lt;td&gt;Benchmark from every application Region&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sequence support makes ascending keys ideal&lt;/td&gt;&lt;td&gt;Ascending writes can concentrate one partition&lt;/td&gt;&lt;td&gt;Insert hotspot or coordination cost&lt;/td&gt;&lt;td&gt;Prefer random keys on high-write tables&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application validation replaces every foreign key&lt;/td&gt;&lt;td&gt;All writers must preserve the invariant under concurrency&lt;/td&gt;&lt;td&gt;Orphans and inconsistent repair paths&lt;/td&gt;&lt;td&gt;Redesign ownership or use enforced constraints elsewhere&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;An outbox makes downstream delivery exactly once&lt;/td&gt;&lt;td&gt;The database makes the row and publication intent atomic, not the external side effect&lt;/td&gt;&lt;td&gt;Duplicate or stale downstream state&lt;/td&gt;&lt;td&gt;Deduplicate, record delivery state, and reconcile&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Serverless removes database operations&lt;/td&gt;&lt;td&gt;Authentication, retries, schema jobs, backup, recovery, and cost remain&lt;/td&gt;&lt;td&gt;Operational gaps move into the application&lt;/td&gt;&lt;td&gt;Own runbooks and observability explicitly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;A large transaction can be split harmlessly&lt;/td&gt;&lt;td&gt;Batching changes the atomicity boundary&lt;/td&gt;&lt;td&gt;Partial business operation becomes visible&lt;/td&gt;&lt;td&gt;Use a state machine or retain another database&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Every PostgreSQL tool will work&lt;/td&gt;&lt;td&gt;Extensions, catalogs, parameters, and commands differ&lt;/td&gt;&lt;td&gt;Migration or operations fail late&lt;/td&gt;&lt;td&gt;Test each required tool and initialization query&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;when-i-would-not-use-aurora-dsql&quot;&gt;When I would not use Aurora DSQL&lt;/h3&gt;
&lt;p&gt;I would not choose Aurora DSQL as a lift-and-shift target for a PostgreSQL monolith that depends on triggers, PL/pgSQL, extensions, foreign-key enforcement, temporary tables, large atomic batches, several databases, or extensive server-level observability and tuning.&lt;/p&gt;
&lt;p&gt;I would also reject it for a critical invariant that requires serializable behavior until a concurrency lab proves the exact design, or for a workload dominated by a few hot rows whose retry rate violates the service objective.&lt;/p&gt;
&lt;p&gt;Aurora DSQL is also not the current answer for a cross-continent active-active database because AWS limits multi-Region clusters to supported within-set combinations.&lt;/p&gt;
&lt;p&gt;For those workloads, standard Aurora PostgreSQL may provide the required compatibility while retaining a single writer. Aurora PostgreSQL Limitless can distribute selected PostgreSQL tables under its own constraints. Citus can provide explicit shard-key control with higher operational responsibility. Application sharding can preserve conventional PostgreSQL inside each ownership boundary.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; PostgreSQL protocol compatibility can hide incompatible concurrency, transaction, schema, and operational assumptions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Evaluate the service by transaction shape: small bounded writes, random keys, tolerable contention, idempotent retries, supported SQL, application-owned integrity, and a tested multi-Region latency budget.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Replay production SQL, run same-key and write-skew concurrency tests, exercise schema changes, force retry exhaustion, verify outbox redelivery and reconciliation, and restore an AWS Backup recovery point into a new cluster.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Pilot one new or well-bounded service before attempting a monolith migration. Keep every empirical claim marked unverified until the lab preserves business invariants under representative concurrency and failure.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For RetailCo’s order-authorization service, Aurora DSQL is worth a pilot because the core write path is small, keyed by UUID, idempotent, and potentially benefits from two active Regional endpoints. The reconciliation batch, database triggers, procedural logic, and foreign-key-dependent workflows should not move unchanged. They need redesign or a different database boundary.&lt;/p&gt;
&lt;p&gt;The scenario verdict is:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Good fit:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  new distributed OLTP services with small transactions and application-owned retries&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Poor fit:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  PostgreSQL monoliths with deep database programmability or large atomic batches&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Main advantage:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  managed active-active writes with strong consistency and automatic distribution&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Main limitation:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  a focused PostgreSQL surface and a different concurrency contract&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Operational complexity:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  low for servers, medium to high for application correctness and migrations&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Migration complexity:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  low for a new bounded service, high for an existing PostgreSQL-centric system&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The conclusion is not that Aurora DSQL is deficient because it is not conventional PostgreSQL. Its distributed behavior is the reason it can offer a different scale and availability model. The engineering mistake is to adopt that model while pretending the differences stop at the connection string.&lt;/p&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category></item><item><title>Three Open-Source Tools Filling the Gaps in Database Operations (May 2025)</title><link>https://rajivonai.com/blog/2025-06-14-github-stars-jun-2025/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-06-14-github-stars-jun-2025/</guid><description>May 2025&apos;s most-starred new projects solve three specific database team problems: backup restores that are never verified, internal knowledge that can&apos;t be retrieved, and AI agents blind to your schema history.</description><pubDate>Sat, 14 Jun 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Database teams have gotten good at the hard parts — query plans, replication lag, index tuning — and quietly left the infrastructure around those databases in a state that would embarrass a 2018 DevOps team.&lt;/strong&gt; Three projects that broke into GitHub’s top monthly stars in May 2025 attack that gap directly: one proves your backups actually restore before an incident does, one brings your scattered runbooks and postmortems into a local AI retrieval system that runs on a laptop, and one gives AI coding agents real access to your full schema and migration history without the context-window cost.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The operational layer around a database — backup pipelines, internal knowledge retrieval, AI-assisted schema work — has been treated as solved infrastructure while teams focused on query performance. It is not solved. Backup tools routinely verify checksums without running a restore. Internal runbooks and postmortems live in Confluence pages that no retrieval system can query efficiently. And when an engineer asks an AI coding agent to help with a migration, the agent sees only the files explicitly loaded into context — which for any real codebase never includes the full schema history.&lt;/p&gt;
&lt;p&gt;May 2025 produced three open-source tools, each crossing 7,000 stars within weeks of release, that treat each of these as an engineering problem with a specific, testable solution.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure modes are not hypothetical:&lt;/p&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure point&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Checksum-only backup validation&lt;/td&gt;&lt;td&gt;A corrupt or incomplete dump passes checksum; fails on restore&lt;/td&gt;&lt;td&gt;Teams discover unusable backups during incidents, not before&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Vector storage at runbook scale&lt;/td&gt;&lt;td&gt;A 1M-document embedding index (1536 dimensions) needs ~6 GB just for float32 vectors&lt;/td&gt;&lt;td&gt;Prohibitive for a local DB knowledge base; forces a vector DB server&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AI agent schema blindness&lt;/td&gt;&lt;td&gt;Coding agents load only explicitly referenced files&lt;/td&gt;&lt;td&gt;ORM logic, migration history, and stored procedures are invisible to the agent&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unverified RTO assumptions&lt;/td&gt;&lt;td&gt;Recovery time objectives are calculated against restores that have never been run&lt;/td&gt;&lt;td&gt;RTO figures are fiction until a real restore has been timed&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The core question for a database team in mid-2025: can these three gaps be closed with off-the-shelf open-source tooling, or does each require building something custom?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;These projects each target one failure mode. The architecture of how they connect to a typical database team’s workflow:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DBTeam[database team — operational gaps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DBTeam --&gt; BackupGap[backups verified by checksum only]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DBTeam --&gt; KnowledgeGap[runbooks and postmortems not retrievable]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DBTeam --&gt; AgentGap[AI agents blind to schema and migration history]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    BackupGap --&gt; Databasus[databasus — automated restore verification pipeline]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    KnowledgeGap --&gt; LEANN[LEANN — local RAG with 97% less vector storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    AgentGap --&gt; ClaudeCtx[claude-context — semantic schema search via MCP]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Databasus --&gt; Outcome1[backup failure found before an incident]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    LEANN --&gt; Outcome2[institutional knowledge queryable in seconds]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ClaudeCtx --&gt; Outcome3[AI agent writes migrations with full schema context]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;databasus--verify-the-restore-not-the-checksum&quot;&gt;databasus — Verify the Restore, Not the Checksum&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The problem it solves:&lt;/strong&gt; Your backup schedule is meaningless if you have never verified a restore succeeds. Most teams test this once, on setup, and never again. databasus makes restore verification part of every backup cycle.&lt;/p&gt;
&lt;p&gt;databasus is a self-hosted, open-source backup tool (Go, Docker/Kubernetes) for PostgreSQL 12–17, MySQL 5.7–9, MariaDB, and MongoDB. It backs up to S3, Google Drive, or FTP with Slack/Discord/Telegram notifications. The differentiating feature, according to the project documentation, is that after each backup it spins up a throwaway database container, runs the full restore, confirms data integrity at the row level, and only then marks the backup valid. This is not a file hash check — it is the same procedure an on-call DBA would run manually, automated into the pipeline.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;docker&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; run&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -e&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; DATABASE_URL=&quot;postgresql://user:pass@host:5432/mydb&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -e&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; STORAGE_S3_BUCKET=&quot;db-backups-prod&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -e&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; BACKUP_SCHEDULE=&quot;0 4 * * *&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -e&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; RESTORE_VERIFICATION=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  databasus/databasus:latest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Use case for the database team:&lt;/strong&gt; Run this against your staging environment first. Two weeks of nightly backups with restore verification will tell you what your current backup tooling has been silently missing. Any backup that fails restore verification but passes the existing checksum-only check represents a recovery gap that was invisible until now.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Where it breaks:&lt;/strong&gt; Restore verification spins up a full database container, which for databases in the hundreds of gigabytes makes per-backup verification impractical within typical maintenance windows. The documentation recommends sampling: run full restore verification weekly and keep daily backups on checksum-only. That is still a material improvement over the current state at most teams.&lt;/p&gt;
&lt;h3 id=&quot;leann--your-runbooks-deserve-a-real-retrieval-system&quot;&gt;LEANN — Your Runbooks Deserve a Real Retrieval System&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The problem it solves:&lt;/strong&gt; Database teams accumulate enormous institutional knowledge — postmortems, runbooks, query plan archives, schema change decisions, incident timelines. This knowledge is almost never retrievable at the moment it is needed because building a proper semantic search system over it requires a vector database server, which is substantial infrastructure for a tool used internally by one team.&lt;/p&gt;
&lt;p&gt;LEANN (arXiv:2505.08276) is a vector index that stores the graph topology connecting vectors but computes the actual embedding values on demand at query time rather than persisting them. According to the paper and README, this “graph-based selective recomputation with high-degree preserving pruning” approach reduces storage by 97% compared to standard ANN indexes like FAISS, with no reported accuracy loss on standard benchmarks. At one million 1536-dimension vectors, FAISS needs roughly 6 GB of float32 storage; LEANN stores the graph structure (a fraction of that) and recomputes vectors during search.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; leann &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; LEANNIndex&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Index your team&apos;s runbooks, postmortems, schema docs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;idx &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; LEANNIndex(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;storage_path&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;./db-knowledge&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;idx.add_texts(runbook_chunks)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Query at incident time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;results &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx.query(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;how did we fix the Aurora replication lag in Q3?&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;results &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx.query(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;which migrations touched the payments schema in the last 6 months?&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;LEANN integrates directly with LangChain, LlamaIndex, and Ollama and includes native MCP support for agent pipelines. The entire system runs on a laptop without a vector database server.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Use case for the database team:&lt;/strong&gt; Index your team’s Confluence export, postmortem archive, and schema changelog. Query it during incidents instead of searching Slack history. The knowledge base grows as the team adds more documents; re-indexing is incremental.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Where it breaks:&lt;/strong&gt; On-demand recomputation adds query latency compared to a pre-materialized in-memory index. For interactive internal knowledge retrieval — where 200–500ms response is acceptable — this is a reasonable tradeoff. For high-throughput external RAG serving thousands of queries per second, benchmark before replacing a production vector store. GPU acceleration is not yet available; the project README tracks this as the highest-priority community request.&lt;/p&gt;
&lt;h3 id=&quot;claude-context--ai-agents-that-can-read-your-schema-history&quot;&gt;claude-context — AI Agents That Can Read Your Schema History&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;The problem it solves:&lt;/strong&gt; When a database team engineer asks Claude Code to write a migration, add a foreign key, or refactor an ORM model, the agent operates on whatever files happen to be in context. For a database layer with years of migrations, multiple ORM models, and scattered stored procedures, “whatever is in context” is never enough for a correct answer. The agent writes migrations that conflict with constraints it could not see.&lt;/p&gt;
&lt;p&gt;claude-context is an MCP server from Zilliz — the company that develops Milvus — that indexes a codebase into a vector database and exposes semantic search to AI coding agents via the Model Context Protocol. When Claude Code needs to understand a schema, it calls the MCP tool and retrieves only the semantically relevant code — not the entire codebase loaded wholesale into context. Per the README, the tool uses a Merkle tree for incremental re-indexing: after a schema migration, only the changed files are re-embedded, not the full repository.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;npx&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; @zilliz/claude-context-mcp&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; init&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Prompts for vector DB credentials and repo path&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Registers the MCP server in Claude Code settings automatically&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After indexing, when you ask Claude Code to add a column to a table referenced in a migration from 18 months ago, the agent retrieves the relevant migration history and schema definition without you having to specify the files. The agent’s schema knowledge scales with the codebase rather than being capped by the context window.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Where it breaks:&lt;/strong&gt; The current implementation requires a Zilliz Cloud account (free tier available) or a self-hosted Milvus deployment. Teams with strict data residency policies need to verify the self-hosted path before indexing proprietary schemas. First-time indexing of a large monorepo can take 10–30 minutes; the documentation recommends running indexing in CI after each merge and serving from a pre-built index.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;All three descriptions above are grounded in the project READMEs and the LEANN arXiv paper (2505.08276). On LEANN’s storage claims specifically: the 97% reduction is measured against FAISS on standard ANN benchmarks under the documented experimental conditions. I have not run this against a production database runbook corpus at the scale of a real team’s knowledge base — teams should benchmark recall against their own query distribution before replacing a production vector store.&lt;/p&gt;
&lt;p&gt;databasus’s restore verification approach is consistent with the recommendation in PostgreSQL’s official documentation on backup and restore verification (under “Checking the Backup”). The innovation is automation rather than technique.&lt;/p&gt;
&lt;p&gt;claude-context’s Merkle-tree incremental indexing is documented in the README; it is the same general approach used by tools like Turborepo and Bazel for change detection, applied to embedding re-indexing.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Restore verification timeout&lt;/td&gt;&lt;td&gt;Databases &gt;100 GB with narrow backup windows&lt;/td&gt;&lt;td&gt;Switch to weekly full restore verification plus daily backup-only&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LEANN recall degradation&lt;/td&gt;&lt;td&gt;Very sparse or domain-specific query distributions&lt;/td&gt;&lt;td&gt;Benchmark recall@10 on your actual queries before moving off FAISS&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;claude-context cold index latency&lt;/td&gt;&lt;td&gt;First indexing of a 500k+ line monorepo&lt;/td&gt;&lt;td&gt;Run indexing in CI on merge; serve from pre-built index&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;databasus version mismatch&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_dump&lt;/code&gt; version in container differs from the database major version&lt;/td&gt;&lt;td&gt;Pin container image to match database major version explicitly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LEANN query latency at scale&lt;/td&gt;&lt;td&gt;Large corpus + high recomputation cost&lt;/td&gt;&lt;td&gt;Tune &lt;code&gt;num_recompute&lt;/code&gt;; GPU support is on the project roadmap&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Database operations infrastructure lags behind query-layer tooling — backups are unverified, internal knowledge is dark, AI agents are schema-blind.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: databasus for verified backup pipelines, LEANN for local knowledge retrieval, claude-context for semantic schema access in AI coding agents.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Run databasus with &lt;code&gt;RESTORE_VERIFICATION=true&lt;/code&gt; against staging for two weeks. Any backup that fails real restore but would have passed a checksum check is a recovery gap that existed silently until now.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, install LEANN (&lt;code&gt;pip install leann&lt;/code&gt;), index your team’s postmortem directory, and run three queries against incidents from the past year. If the results would have reduced time-to-resolution in any of them, you have a case for making it part of your incident response tooling.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>architecture</category></item><item><title>DB Team Automation Roadmap: Backups, Patching, Refreshes, Provisioning, and Guardrails</title><link>https://rajivonai.com/blog/2025-06-10-db-team-automation-roadmap-backups-patching-refreshes-provisioning-and-guardrails/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-06-10-db-team-automation-roadmap-backups-patching-refreshes-provisioning-and-guardrails/</guid><description>A sequenced roadmap for database teams to automate backups, patching, refreshes, and provisioning — with guardrails that prevent automation from becoming a risk multiplier.</description><pubDate>Tue, 10 Jun 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The database team should not be the human API for every backup check, patch window, refresh request, schema gate, and provisioning ticket. If every operational change depends on a senior DBA remembering the right sequence, the architecture is already carrying hidden outage risk.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Database teams are being pulled in two directions at once.&lt;/p&gt;
&lt;p&gt;On one side, application teams expect self-service infrastructure. They are used to CI pipelines, preview environments, ephemeral test stacks, policy-as-code, and automated rollback. Waiting three days for a database refresh or two weeks for a new instance feels broken.&lt;/p&gt;
&lt;p&gt;On the other side, databases remain stateful systems with real blast radius. A bad application deploy can often be rolled forward. A bad restore process, patch sequence, privilege grant, or retention policy can destroy evidence, break recovery objectives, or expose regulated data.&lt;/p&gt;
&lt;p&gt;That tension is where platform engineering becomes useful. The goal is not to remove the database team from operations. The goal is to move the team from ticket execution to workflow ownership: define the paved road, encode the checks, expose safe interfaces, and reserve human attention for exceptions.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Most DB automation programs start with scripts. A backup validation script. A patching runbook. A clone script for lower environments. A Terraform module for a standard instance. A policy check in CI.&lt;/p&gt;
&lt;p&gt;Each script helps, but the operating model often stays manual. Engineers still ask in Slack whether a restore was tested. A DBA still approves every refresh by reading a ticket. Patching still depends on a calendar spreadsheet. Provisioning still creates one-off exceptions. Guardrails still live in wiki pages instead of the deployment path.&lt;/p&gt;
&lt;p&gt;The failure mode is not lack of automation. The failure mode is disconnected automation without a control plane.&lt;/p&gt;
&lt;p&gt;A mature DB automation roadmap has to answer one question: how do we let teams move faster while making the dangerous paths harder to reach?&lt;/p&gt;
&lt;h2 id=&quot;the-automation-control-plane&quot;&gt;The Automation Control Plane&lt;/h2&gt;
&lt;p&gt;The answer is to treat database operations as typed workflows with policy, evidence, and rollback built in.&lt;/p&gt;
&lt;p&gt;The DB team should own a small set of durable workflows: backup verification, patch orchestration, environment refresh, database provisioning, access changes, schema safety checks, and operational guardrails. Each workflow should expose a product surface to application teams and an audit surface to operators.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[request portal — typed workflow] --&gt; B[policy engine — eligibility checks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[execution runner — idempotent tasks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D[evidence store — logs and artifacts]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; E[observability — status and alerts]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; F[human review — exception handling]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; G[guardrails — naming and data rules]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; H[database fleet — instances and clusters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt; I[backup system — restore validation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt; J[patch system — staged rollout]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt; K[refresh system — masked clones]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt; L[provisioning system — standard shapes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The important design choice is that every workflow has the same lifecycle.&lt;/p&gt;
&lt;p&gt;A request is structured. Policy decides whether it can proceed. Execution is idempotent and resumable. Evidence is captured automatically. Observability reports progress and failure. Humans review exceptions, not routine cases.&lt;/p&gt;
&lt;p&gt;Backups come first because recovery is the foundation for every other change. The roadmap should include automated backup inventory, restore drills, checksum validation, retention policy checks, and recovery time reporting. A backup that has not been restored is an assumption, not a control.&lt;/p&gt;
&lt;p&gt;Patching comes next because it is predictable risk. The workflow should group databases by criticality, dependency, engine version, and replication topology. It should support prechecks, staged rollout, health gates, automatic pause, and rollback instructions. The aim is not one-click patching everywhere. The aim is repeatable patching with fewer undocumented branches.&lt;/p&gt;
&lt;p&gt;Refreshes are usually the highest-volume workflow. They need strong policy boundaries: source eligibility, destination environment, masking requirements, retention period, approval rules, and post-refresh validation. A refresh system that copies production data faster but does not enforce masking has automated the wrong thing.&lt;/p&gt;
&lt;p&gt;Provisioning should become boring. Standard shapes, default encryption, default backup policy, default monitoring, default ownership tags, default network placement, and default access roles should be encoded once. Exceptions should be explicit because exceptions are where future incidents hide.&lt;/p&gt;
&lt;p&gt;Guardrails tie the roadmap together. They should run in CI, in infrastructure pipelines, and inside operational workflows. Good guardrails reject unsafe changes early: missing owner tags, weak retention, public exposure, unapproved engine versions, oversized privileges, disabled audit logs, and schema changes that require blocking locks on large tables.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; The documented pattern in Google’s Site Reliability Engineering books is that toil reduction matters, but automation must be engineered as production software. The lesson is not “automate everything.” The lesson is that repeated manual operations should be reduced while preserving reliability, observability, and human judgment for novel failures.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Apply that pattern by turning recurring DBA tickets into workflows with explicit inputs, preconditions, execution logs, and failure states. A refresh request should not be a paragraph in a ticket. It should be a form or API call with source, target, masking profile, retention window, requester, approver, and reason.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented pattern is that the team gains a clearer operational boundary. Application teams get faster service for standard work. DB engineers spend more time improving the system and less time translating ambiguous requests into risky commands.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Automation is safest when it narrows choices before it accelerates execution.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Amazon’s public Builders’ Library material describes deployment safety through practices such as small changes, staged rollout, automated checks, and rollback planning. The database equivalent is patch orchestration with health gates rather than calendar-driven bulk maintenance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Treat patching as a deployment pipeline. Run compatibility checks first. Patch low-risk environments before production. Advance by rings. Pause on health degradation. Record each decision and artifact.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The known architectural pattern is staged change management. It limits blast radius by making every step observable before the next step begins.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Database patching should look less like a weekend event and more like a controlled release train.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; PostgreSQL’s documented recovery model depends on base backups, WAL, restore configuration, and recovery targets. The behavior of the system makes backup success different from restore success.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Automate restore tests into isolated environments. Verify that the restored database starts, reaches an expected recovery point, passes integrity checks, and exposes measurable recovery time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The result is not a claim that recovery will always work. The result is current evidence about whether recovery worked under tested conditions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Recovery evidence expires. The automation must keep producing it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; The Kubernetes Operator pattern is a known reconciliation model: desired state is declared, controllers compare actual state to desired state, and corrective action happens continuously.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Use the same model for database provisioning standards. Desired state should include engine version, size class, backup policy, tags, monitoring, encryption, network placement, and access baseline.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Drift becomes visible because the platform has a declared target. Manual changes are no longer invisible just because the database still works.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Provisioning automation is incomplete unless it also detects drift after creation.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Area&lt;/th&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Backups&lt;/td&gt;&lt;td&gt;Backups exist but restores fail&lt;/td&gt;&lt;td&gt;Run scheduled restore validation and publish recovery evidence&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Patching&lt;/td&gt;&lt;td&gt;One failed dependency blocks the fleet&lt;/td&gt;&lt;td&gt;Use rings, dependency metadata, health gates, and pause controls&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Refreshes&lt;/td&gt;&lt;td&gt;Production data leaks into lower environments&lt;/td&gt;&lt;td&gt;Require masking profiles and expire refreshed environments&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Provisioning&lt;/td&gt;&lt;td&gt;Teams bypass standards for speed&lt;/td&gt;&lt;td&gt;Make the paved road faster than exceptions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Guardrails&lt;/td&gt;&lt;td&gt;Policy becomes too rigid&lt;/td&gt;&lt;td&gt;Support explicit exception workflows with owner, expiry, and review&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CI checks&lt;/td&gt;&lt;td&gt;Developers ignore noisy failures&lt;/td&gt;&lt;td&gt;Keep checks specific, actionable, and tied to real operational risk&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Ownership&lt;/td&gt;&lt;td&gt;Nobody maintains the workflows&lt;/td&gt;&lt;td&gt;Assign product ownership inside the DB platform team&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The DB team is overloaded because routine stateful operations still flow through humans as tickets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build a DB automation control plane around typed workflows for backups, patching, refreshes, provisioning, and guardrails.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Use documented patterns from SRE toil reduction, staged deployment safety, database recovery behavior, and reconciliation-based infrastructure management.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Start with backup restore validation, then automate refreshes with masking, then patching rings, then provisioning standards, then CI and runtime guardrails.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>automation</category><category>platform</category><category>ci-cd</category></item><item><title>Repairing a 90-Node Cassandra Cluster Without Crushing Production</title><link>https://rajivonai.com/blog/2025-06-03-multi-dc-cassandra-repair-orchestration-reaper/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-06-03-multi-dc-cassandra-repair-orchestration-reaper/</guid><description>How to orchestrate Apache Cassandra anti-entropy repair across multiple AWS regions using Cassandra Reaper, sub-range division, and inter-DC streaming limits.</description><pubDate>Tue, 03 Jun 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In a small Cassandra cluster, anti-entropy repair is a background maintenance task. In a 90-node, multi-region cluster holding 120 TB of data, repair is a distributed systems engineering problem that can easily destroy the database it was meant to protect.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;By 2024, the Cassandra 4.1 cluster had scaled to 90 nodes spanning two AWS regions (6 Availability Zones, 15 nodes per AZ). We understood that anti-entropy repair was mandatory within the &lt;code&gt;gc_grace_seconds&lt;/code&gt; window (10 days), but running the legacy repair scripts across 90 nodes created catastrophic operational incidents.&lt;/p&gt;
&lt;p&gt;When the scripts fired, cross-region AWS network egress bills skyrocketed. &lt;code&gt;CompactionStage&lt;/code&gt; queues overflowed on disk, and p99 read latencies on coordinator nodes spiked wildly as massive Merkle trees were computed across the network.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Global repair across a multi-region cluster exposes fundamental physics limits in Cassandra.&lt;/p&gt;
&lt;p&gt;If you execute a raw &lt;code&gt;nodetool repair -pr&lt;/code&gt; across a 90-node cluster, Cassandra attempts to synchronize the entire primary token range of each node. Generating a Merkle tree for a 1 TB token range requires massive sequential disk I/O. Worse, if a token mismatch spans across the country, Cassandra initiates un-throttled inter-DC streaming, saturating expensive VPC Peering links.&lt;/p&gt;
&lt;p&gt;Repairing an entire token range at once guarantees two things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Massive JVM heap usage to hold the Merkle tree.&lt;/li&gt;
&lt;li&gt;Complete failure if even a single node drops a connection midway through the 4-hour sync process.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;How do you repair 120 TB of data continuously, every 10 days, without degrading client read latency or saturating cross-region network links?&lt;/p&gt;
&lt;h2 id=&quot;multi-dc-repair-architecture-with-cassandra-reaper&quot;&gt;Multi-DC Repair Architecture with Cassandra Reaper&lt;/h2&gt;
&lt;p&gt;To solve this, the migration decouples repair orchestration from the database layer and deployed &lt;strong&gt;Cassandra Reaper&lt;/strong&gt;. Reaper is an external control plane designed specifically to handle the complexities of token sub-range chunking and multi-DC concurrency.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Reaper[Cassandra Reaper Control Plane] --&gt;|Schedule API| JMX_DC1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Reaper --&gt;|Schedule API| JMX_DC2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;AWS us-east-1 (DC1)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        JMX_DC1(Reaper Agent) --&gt; N1[Node 1]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        JMX_DC1 --&gt; N2[Node 2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;AWS us-west-2 (DC2)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        JMX_DC2(Reaper Agent) --&gt; N3[Node 3]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        JMX_DC2 --&gt; N4[Node 4]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    N1 &amp;#x3C;--&gt;|Throttled Sub-Range Sync — 50 Mbps| N3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    %% Reaper logic&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Token[Large Token Range] -.-&gt;|Reaper splits into| Sub1[Sub-Range 1]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Token -.-&gt;|Reaper splits into| Sub2[Sub-Range 2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Token -.-&gt;|Reaper splits into| Sub100[Sub-Range 100]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-sub-range-repair&quot;&gt;1. Sub-Range Repair&lt;/h3&gt;
&lt;p&gt;Instead of repairing the entire token range of a node at once, Reaper dynamically calculates the token boundaries and splits them into hundreds of tiny sub-ranges (e.g., 256 segments per node). If a segment fails to sync, Reaper simply retries that tiny segment without throwing away hours of work.&lt;/p&gt;
&lt;h3 id=&quot;2-dc-aware-parallelism&quot;&gt;2. DC-Aware Parallelism&lt;/h3&gt;
&lt;p&gt;A standard pattern configures Reaper to use &lt;code&gt;DATACENTER_AWARE&lt;/code&gt; repair parallelism. This ensures that repairs are isolated within a local datacenter first, validating local replicas against each other, before synchronizing across the expensive cross-region AWS links.&lt;/p&gt;
&lt;h3 id=&quot;3-strict-network-throttling&quot;&gt;3. Strict Network Throttling&lt;/h3&gt;
&lt;p&gt;In &lt;code&gt;cassandra.yaml&lt;/code&gt; and via &lt;code&gt;nodetool&lt;/code&gt;, we enforced strict network streaming limits. We throttled general streaming but applied a much harder clamp on inter-DC streaming to protect the AWS VPC Peering bandwidth and prevent client request timeouts:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Limit general streaming to 100 Mbps&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;nodetool&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; setstreamthroughput&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Limit expensive cross-region streaming to 50 Mbps&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;nodetool&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; setinterdcstreamthroughput&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 50&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;A typical deployment involves Reaper backed by a highly available PostgreSQL database to persist repair segment states. Using Reaper’s REST API, we automated the scheduling of continuous rolling repairs.&lt;/p&gt;
&lt;p&gt;Because Reaper continuously monitors the cluster via JMX, if &lt;code&gt;PendingCompactions&lt;/code&gt; on a node breached 20, Reaper automatically paused the repair segment directed at that node, allowing the database to catch its breath. This eliminated the massive I/O thrashing we saw with the legacy cron jobs.&lt;/p&gt;
&lt;p&gt;If a single replica became unresponsive, Reaper paused that specific segment schedule without blocking the remaining 89 nodes from continuing their repair workloads.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;High Segment Count&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Splitting a token range into 1,000 segments makes repair incredibly safe but very slow, potentially violating &lt;code&gt;gc_grace_seconds&lt;/code&gt;.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Monitor Reaper completion telemetry. Tune segment counts to balance safety with the 10-day deadline.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Anti-Compaction on Large Nodes&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Even with sub-ranges, anti-compaction splits SSTables. If disk headroom is tight (&amp;#x3C; 20%), anti-compaction can fill the disk.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Maintain adequate disk headroom. Ensure nodes are scaled out before they reach 75% capacity.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Un-throttled Inter-DC Streams&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Even with Reaper, if &lt;code&gt;inter_dc_stream_throughput_outbound_megabits_per_sec&lt;/code&gt; is unlimited, cross-region syncs will cause p99 read spikes.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Always explicitly set &lt;code&gt;setinterdcstreamthroughput&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-and-cost-optimizations&quot;&gt;Modern Cassandra Context: Cassandra 5.0 and Cost Optimizations&lt;/h3&gt;
&lt;p&gt;Cassandra 5.0 significantly optimized the Merkle tree generation code path, reducing the CPU cost of repair. Furthermore, its enhanced multi-DC repair coordination requires less raw bytes to be transferred. From a cloud FinOps perspective, managing inter-DC streaming directly correlates to reduced AWS Inter-Region VPC Peering costs—letting Reaper’s sub-range repairs and 5.0’s reduced Merkle-tree transfer volume minimize redundant cross-region data movement is a meaningful, if workload-dependent, line item to track rather than a fixed percentage.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Unorchestrated repairs on large, multi-region clusters cause cross-DC network saturation, disk thrashing, and database freezes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Deploy Cassandra Reaper to automate token sub-range chunking, enforce &lt;code&gt;DATACENTER_AWARE&lt;/code&gt; parallel repair, and explicitly throttle &lt;code&gt;inter_dc_stream_throughput&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Reaper’s backpressure awareness pauses repairs when &lt;code&gt;PendingCompactions&lt;/code&gt; rise, eliminating client read-latency degradation during the 10-day repair cycle.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Execute &lt;code&gt;nodetool netstats&lt;/code&gt; during a repair. If you see gigabytes transferring simultaneously across regions, halt the repair via &lt;code&gt;nodetool stop VALIDATION&lt;/code&gt; and rethink your orchestration.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>checklist</category></item><item><title>Merkle Trees and Cassandra Repair: Full vs. Incremental Repair in Real Production</title><link>https://rajivonai.com/blog/2025-05-26-cassandra-merkle-trees-anti-entropy-repair/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-05-26-cassandra-merkle-trees-anti-entropy-repair/</guid><description>How Apache Cassandra&apos;s anti-entropy repair actually works under the hood, and why incremental repair causes disk fragmentation without a managed full repair strategy.</description><pubDate>Mon, 26 May 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In a distributed database, entropy is guaranteed. Nodes crash, network packets drop, AWS EBS volumes stall, and AWS Availability Zones partition. When a Cassandra node misses a write, the database relies on two mechanisms to fix it: Read Repair (on the hot path) and Anti-Entropy Repair (in the background).&lt;/p&gt;
&lt;p&gt;When Anti-Entropy Repair is misunderstood, it becomes a weapon of mass cluster destruction.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In 2022–2024, the 36-node cluster (~40 TB dataset) began exhibiting silent replica drift. Applications running queries with a &lt;code&gt;LOCAL_ONE&lt;/code&gt; consistency level started returning inconsistent results depending on which replica answered.&lt;/p&gt;
&lt;p&gt;To fix it, an engineer SSH’d into a node and typed &lt;code&gt;nodetool repair&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Within minutes, disk I/O saturated, CPU spikes hit 100%, and p99 client latencies breached 5 seconds. &lt;code&gt;nodetool netstats&lt;/code&gt; showed gigabytes of data streaming uncontrollably across the network. A background process called “anti-compaction” began tearing apart existing SSTables, blocking normal compactions and filling the disk to the brink of capacity.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra’s anti-entropy repair is designed to synchronize data across replicas. To do this efficiently without sending the actual data across the network, Cassandra uses &lt;strong&gt;Merkle Trees&lt;/strong&gt;—cryptographic hash trees representing the data in a specific token range.&lt;/p&gt;
&lt;p&gt;When a repair starts:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Replicas generate a Merkle tree by scanning their SSTables and hashing the data.&lt;/li&gt;
&lt;li&gt;The coordinator compares the trees.&lt;/li&gt;
&lt;li&gt;If a leaf node (a hash of a specific sub-range) mismatches, the replicas stream only that specific slice of data to resolve the conflict.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The problem was twofold. First, running raw &lt;code&gt;nodetool repair&lt;/code&gt; without the &lt;code&gt;-pr&lt;/code&gt; (Primary Range) flag tells the node to repair &lt;em&gt;all&lt;/em&gt; token ranges it holds replicas for, causing massive redundant repair work across the cluster.&lt;/p&gt;
&lt;p&gt;Second, the cluster was using &lt;strong&gt;Incremental Repair&lt;/strong&gt;. Incremental repair attempts to optimize the process by splitting SSTables into two physical buckets on disk: &lt;code&gt;repaired&lt;/code&gt; and &lt;code&gt;unrepaired&lt;/code&gt;. Once data is repaired, it is never repaired again. However, the process of separating this data—&lt;strong&gt;anti-compaction&lt;/strong&gt;—physically rewrites SSTables. Because the repair process was ad-hoc, massive anti-compactions were colliding with normal traffic, fragmenting the disk, and locking up I/O threads.&lt;/p&gt;
&lt;h2 id=&quot;the-anti-entropy-architecture&quot;&gt;The Anti-Entropy Architecture&lt;/h2&gt;
&lt;p&gt;To control entropy without destroying performance, you must understand the Merkle tree exchange.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord(Coordinator Node) --&gt;|Trigger Repair| N1(Replica A)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord --&gt;|Trigger Repair| N2(Replica B)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;Merkle Tree Construction&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        N1 --&gt; M1[Compute Merkle Tree — Hash of Token Ranges]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        N2 --&gt; M2[Compute Merkle Tree — Hash of Token Ranges]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    M1 -.-&gt;|Exchange Hashes| Coord&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    M2 -.-&gt;|Exchange Hashes| Coord&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord --&gt; Compare{Hash Mismatch — in Leaf Node?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Compare --&gt;|No| Done[Data is Consistent]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Compare --&gt;|Yes| Stream[Stream Mismatched — Token Range]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Stream --&gt; Sync[(Replicas Synchronized)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-ditching-incremental-repair-for-managed-full-repair&quot;&gt;1. Ditching Incremental Repair for Managed Full Repair&lt;/h3&gt;
&lt;p&gt;While Incremental Repair sounds theoretically optimal, the resulting anti-compaction fragmentation in Cassandra 4.0 often causes more operational pain than it solves.
&lt;strong&gt;Fix:&lt;/strong&gt; The pattern standardizes on &lt;strong&gt;Full Primary Range Repairs&lt;/strong&gt; (&lt;code&gt;nodetool repair --full -pr&lt;/code&gt;). The &lt;code&gt;-pr&lt;/code&gt; flag ensures a node only repairs the token ranges it is the primary owner of, preventing duplicate work across the ring.&lt;/p&gt;
&lt;h3 id=&quot;2-time-bounding-the-repair-gc_grace_seconds&quot;&gt;2. Time-Bounding the Repair (gc_grace_seconds)&lt;/h3&gt;
&lt;p&gt;Every token range in the cluster must be fully repaired before &lt;code&gt;gc_grace_seconds&lt;/code&gt; expires (default 10 days). If a node misses a delete (tombstone) and is not repaired within 10 days, the tombstone is purged from healthy nodes. If the offline node comes back, the deleted data will be “resurrected” because the healthy nodes forgot it was deleted.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern across the industry is to strictly forbid manual &lt;code&gt;nodetool repair&lt;/code&gt; executions in production. Instead, teams adopt a dedicated repair control plane, with &lt;strong&gt;Cassandra Reaper&lt;/strong&gt; being the open-source standard.&lt;/p&gt;
&lt;p&gt;Cassandra’s architecture requires that every token range in the cluster is fully repaired before &lt;code&gt;gc_grace_seconds&lt;/code&gt; expires (default 10 days). If a node is offline and misses a delete, and a repair does not synchronize that tombstone before the grace period ends, the tombstone is purged from the healthy nodes. When the offline node is restored, the deleted data will be “resurrected” because the healthy nodes have forgotten the deletion ever occurred.&lt;/p&gt;
&lt;p&gt;To manage this safely, Reaper automatically computes the token ring and splits it into tiny segments (sub-range repair). It schedules these small segments sequentially across the cluster.&lt;/p&gt;
&lt;p&gt;Operators verify the state of repair on disk using offline tools, and maintain emergency commands to halt runaway processes:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Verify the repair status of an SSTable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sstablemetadata&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/lib/cassandra/data/ks/tbl/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;-Data.db&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -i&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Repaired at&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Abort a runaway repair immediately (kills the validation compactions driving it)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;nodetool&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; stop&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; VALIDATION&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By actively monitoring cluster backpressure, Reaper will automatically pause repair schedules if &lt;code&gt;PendingCompactions&lt;/code&gt; rise too high, ensuring that background anti-entropy never starves the client read and write paths.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Running Repair on Down Nodes&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;If a replica is offline, the remaining nodes generate gigantic Merkle trees but cannot complete the sync, wasting CPU and memory.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Stop all repairs if a node is marked &lt;code&gt;DOWN&lt;/code&gt; in &lt;code&gt;nodetool status&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Too Many Concurrent Repairs&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Merkle tree generation requires massive sequential disk reads.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Configure Cassandra Reaper to run only one segment repair at a time per datacenter.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Ignoring Sub-Range Repair&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Repairing an entire vnode token range at once generates massive Merkle trees that consume excessive JVM heap.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Always split repairs into smaller sub-ranges. (Reaper does this automatically).&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-enhancements&quot;&gt;Modern Cassandra Context: Cassandra 5.0 Enhancements&lt;/h3&gt;
&lt;p&gt;Cassandra 5.0 GA significantly stabilizes the repair pipeline. It introduces native sub-range repair optimizations and leverages zero-copy streaming during repair, drastically reducing the CPU and heap overhead previously required to synchronize mismatched data. However, the architectural mandate remains: full primary-range repairs orchestrated by a tool like Cassandra Reaper remain the gold standard for long-term data consistency on EC2.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Unmanaged repairs cause CPU saturation, network flooding, and anti-compaction fragmentation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Abandon manual incremental repairs. Adopt automated, throttled, full primary-range (&lt;code&gt;-pr&lt;/code&gt;) repairs using sub-ranges.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Deploying Cassandra Reaper stabilizes &lt;code&gt;PendingCompactions&lt;/code&gt; and guarantees 100% token range repair within the &lt;code&gt;gc_grace_seconds&lt;/code&gt; window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Check your SSTables with &lt;code&gt;sstablemetadata&lt;/code&gt;. If you see a mix of repaired and unrepaired data but no orchestrator is running, you are at risk of data resurrection. Install Cassandra Reaper.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category><category>checklist</category></item><item><title>Cloud SQL PostgreSQL Performance at Scale: Data Cache, Replicas, and Evidence-Driven Tuning</title><link>https://rajivonai.com/blog/2025-05-15-cloud-sql-postgresql-performance-at-scale/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-05-15-cloud-sql-postgresql-performance-at-scale/</guid><description>A performance-engineering guide to Cloud SQL Enterprise Plus, Data Cache, read replicas, and workload-specific validation.</description><pubDate>Thu, 15 May 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A larger cache can reduce physical reads; it cannot repair a missing index, remove a single-writer ceiling, or make an asynchronous replica strongly consistent. Cloud SQL performance work starts by identifying the constrained resource.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A PostgreSQL workload has rising read latency as its active data set grows. The team can increase memory, use Cloud SQL Enterprise Plus with Data Cache, add read replicas, or move analytical work elsewhere. All four choices can be reasonable, but they address different bottlenecks.&lt;/p&gt;
&lt;p&gt;The first task is measurement: query plans and latency distributions, buffer activity, storage latency and throughput, CPU, memory pressure, connections, locks, and replica lag. “The database is slow” is not a capacity model.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Rules such as “upgrade when cache hit ratio falls below 90%” are unsafe. PostgreSQL cache-hit ratios differ by workload, and sequential scans can be efficient. Separate PostgreSQL shared-buffer observations from Cloud SQL Data Cache evidence: SQL statistics such as &lt;code&gt;pg_statio_user_tables&lt;/code&gt; describe PostgreSQL buffer activity, while Cloud Monitoring exposes the GA &lt;code&gt;database/postgresql/data_cache/hit_ratio&lt;/code&gt; metric for the local-SSD cache.&lt;/p&gt;
&lt;p&gt;Adding replicas also does not fix a slow query. It creates more read capacity only when traffic can be routed to read-only endpoints and tolerate asynchronous visibility. The core question is: &lt;strong&gt;is the workload limited by execution, memory, storage reads, concurrency, or write serialization?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;a-performance-decision-model&quot;&gt;A Performance Decision Model&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Slow[Latency regression] --&gt; Plans[Compare query plans and waits]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Plans --&gt; CPU{CPU or execution bound}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Plans --&gt; IO{Storage read bound}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Plans --&gt; Conn{Connection or lock bound}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CPU --&gt; Tune[Tune SQL and indexes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    IO --&gt; Cache[Evaluate memory or Data Cache]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Conn --&gt; Pool[Bound pools and reduce contention]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Tune --&gt; Replica[Add replicas only for separable reads]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Cache --&gt; Replica&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Enterprise Plus is a Cloud SQL edition with different machine families and availability features. For eligible Enterprise Plus configurations, Data Cache uses local SSD as an additional read cache for PostgreSQL data pages. Google reports up to four times read-performance improvement for its tested workloads. “Up to” is neither a latency guarantee nor evidence that a particular application will benefit.&lt;/p&gt;
&lt;p&gt;Data Cache helps when repeat reads miss memory but have useful locality in the local SSD tier. It is less useful when queries scan data once, CPU dominates, indexes are absent, connections are saturated, or the write path is the constraint. Local cache contents are not durable state; cache warm-up and lifecycle events must be included in testing.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Use a before-and-after protocol:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Capture a representative workload, not only one favorable query.&lt;/li&gt;
&lt;li&gt;Record &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; for high-impact statements.&lt;/li&gt;
&lt;li&gt;Measure p50, p95, and p99 latency, CPU, storage reads, locks, and throughput.&lt;/li&gt;
&lt;li&gt;Record the Cloud Monitoring Data Cache hit-ratio, hit-count, and miss-count metrics after the cache has warmed; use PostgreSQL statistics separately for shared-buffer behavior.&lt;/li&gt;
&lt;li&gt;Repeat after documented cache-erasing events such as planned maintenance, an unexpected shutdown, a major-version upgrade, or a machine-type change.&lt;/li&gt;
&lt;li&gt;Compare total cost and application SLOs, not only database IOPS.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;An explicit Terraform configuration can record intent:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;google_sql_database_instance&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;postgres&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;             =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;production-postgres&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  database_version&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;POSTGRES_15&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  region&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;           =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;us-central1&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  settings&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    edition&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;ENTERPRISE_PLUS&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tier&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;db-perf-optimized-N-8&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    data_cache_config&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      data_cache_enabled&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Validate the provider schema, tier availability, edition-conversion path, downtime, and storage changes when the plan is executed. Data Cache availability and defaults depend on edition, machine type, and current service rules; keep the configuration explicit and verify the generated plan.&lt;/p&gt;
&lt;p&gt;Read replicas are a separate lever. Route only read-only requests that accept replica lag. Define a primary fallback policy, monitor lag, and test replay conflicts for long PostgreSQL queries. A new or restarted replica may be cold, so introduce traffic gradually and measure it rather than asserting a fixed warm-up time.&lt;/p&gt;
&lt;p&gt;Preserve a control case for every upgrade. Test the same workload on a right-sized Enterprise instance, an Enterprise Plus instance with Data Cache disabled, and the intended configuration with Data Cache enabled. Without those controls, a machine-family or memory change can be mistaken for a cache improvement. Hold connection count, data snapshot, client placement, and warm-up procedure constant, and report confidence intervals across repeated runs rather than selecting the fastest sample.&lt;/p&gt;
&lt;p&gt;Include write-side regression checks even when the goal is read performance. A configuration that improves dashboard latency but increases transaction commit latency, replica lag, or maintenance recovery time has shifted the cost rather than removed it.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Why&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Full scans evict useful pages&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Query or index design dominates cache capacity&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fix access paths before buying another cache tier.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Cache improves averages but not tail latency&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cold reads or lock contention dominate p99&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Measure waits and cold-cache events separately.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Replica returns stale state&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Replication is asynchronous&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep correctness-sensitive reads on the primary or design an explicit consistency token.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Replica query is canceled&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Replay conflicts with a long-running read&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bound query duration or isolate analytics outside the replica path.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;More connections erase the gain&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Per-session work and memory saturate compute&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use bounded pooling and admission control.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Scaling choices are being made from a single cache ratio or vendor benchmark.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Classify the bottleneck, then test Data Cache, larger memory, replicas, or query changes against the same workload.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Google describes Data Cache as a local-SSD read cache and publishes bounded, workload-dependent results rather than a universal multiplier.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Produce a performance report with baseline plans, latency percentiles, resource saturation, Data Cache measurements, replica lag, and total cost.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/editions-intro&quot;&gt;Cloud SQL editions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/data-cache&quot;&gt;Cloud SQL Data Cache&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/optimize-high-memory-usage&quot;&gt;Optimize high memory usage&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/replication&quot;&gt;Cloud SQL read replicas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/admin-api/metrics&quot;&gt;Cloud SQL metrics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database_instance&quot;&gt;Terraform instance resource&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>cloud</category><category>architecture</category><category>databases</category><category>performance</category><category>checklist</category></item><item><title>MongoDB Queryable Encryption Architecture Review: Envelope Key Hierarchies, Range Metadata Index Amplification, and KMS Resilience</title><link>https://rajivonai.com/blog/2025-05-12-mongodb-queryable-encryption-architecture-review/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-05-12-mongodb-queryable-encryption-architecture-review/</guid><description>A comprehensive pre-production architecture review for MongoDB Queryable Encryption (QE). This operational runbook establishes verification gates across envelope key hierarchies, external KMS failure modes, metadata index storage amplification, distributed client cache TTLs, and zero-downtime key rotation.</description><pubDate>Mon, 12 May 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Deploying MongoDB Queryable Encryption (QE) is an irreversible architectural commitment. Unlike standard database access controls or storage volume encryption, QE alters the physical wire protocol and on-disk cryptographic representation of document fields: every equality search is transformed into a deterministic cryptographic token, and every range query is evaluated through synthetic bloom filter structures stored in internal metadata collections. If an engineering team provisions QE with a local key provider, undersizes its range query precision bounds, or deploys un-audited aggregation pipelines, no &lt;code&gt;ALTER TABLE&lt;/code&gt; or index rebuild can remediate the cluster. Correcting a misconfigured query type post-launch requires provisioning a parallel cluster, deploying dual-writing application adapters, reading every document through an in-memory client decryption layer, and re-encrypting the entire dataset from scratch.&lt;/p&gt;
&lt;p&gt;A production-grade QE architecture review must enforce rigorous pre-flight validation gates across five engineering domains: hardware security module (HSM) and cloud KMS envelope hierarchies, driver-level &lt;code&gt;libmongocrypt&lt;/code&gt; interception hooks, distributed Data Encryption Key (DEK) cache coherence, range metadata storage amplification, and non-blocking key rotation workflows.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Enterprise platforms operating under strict regulatory compliance mandates (PCI DSS 4.0, HIPAA, GDPR, and FedRAMP High) require protecting sensitive data from privileged infrastructure administrators. In standard encryption-at-rest models, any user with &lt;code&gt;mongod&lt;/code&gt; host root access, database administrator privileges, or access to memory core dumps can view sensitive plaintext data.&lt;/p&gt;
&lt;p&gt;MongoDB Queryable Encryption enforces zero-trust data privacy at the database engine boundary. Sensitive fields are encrypted within application process memory via &lt;code&gt;libmongocrypt&lt;/code&gt; before BSON serialization. The database server receives and stores only ciphertext and auxiliary encrypted index structures, ensuring that database administrators, storage engineers, and cloud infrastructure providers cannot read plaintext values under any operational condition.&lt;/p&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Encryption Tier&lt;/th&gt;&lt;th&gt;Encryption Boundary&lt;/th&gt;&lt;th&gt;Server Decryption Capability&lt;/th&gt;&lt;th&gt;Query Support&lt;/th&gt;&lt;th&gt;Storage Overhead&lt;/th&gt;&lt;th&gt;Key Management Dependency&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Transparent Data Encryption (TDE)&lt;/td&gt;&lt;td&gt;Storage block level (WiredTiger)&lt;/td&gt;&lt;td&gt;Full plaintext visible in &lt;code&gt;mongod&lt;/code&gt; memory&lt;/td&gt;&lt;td&gt;All queries, full text, aggregations&lt;/td&gt;&lt;td&gt;Negligible (1–3% block overhead)&lt;/td&gt;&lt;td&gt;Single cluster-level key (KMS/Vault)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Client-Side Field Level Encryption (FLE v1)&lt;/td&gt;&lt;td&gt;Application client process&lt;/td&gt;&lt;td&gt;Zero server-side decryption capability&lt;/td&gt;&lt;td&gt;Exact match only (deterministic) or none (random)&lt;/td&gt;&lt;td&gt;Moderate (ciphertext padding per field)&lt;/td&gt;&lt;td&gt;Per-field Data Encryption Keys (DEK)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Queryable Encryption (QE / FLE2) — Equality&lt;/td&gt;&lt;td&gt;Application client process&lt;/td&gt;&lt;td&gt;Zero server-side decryption capability&lt;/td&gt;&lt;td&gt;Fast exact match via equality tokens&lt;/td&gt;&lt;td&gt;Low (compact cryptographic tokens in &lt;code&gt;.esc&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;Two-tier DEK/CMK envelope hierarchy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Queryable Encryption (QE / FLE2) — Range&lt;/td&gt;&lt;td&gt;Application client process&lt;/td&gt;&lt;td&gt;Zero server-side decryption capability&lt;/td&gt;&lt;td&gt;Bounded inequality, range scans, prefix ranges&lt;/td&gt;&lt;td&gt;&lt;strong&gt;High&lt;/strong&gt; (3–10× index amplification in &lt;code&gt;.ecoc&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;Two-tier DEK/CMK with precision tuning&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;While equality QE introduces minimal storage and latency friction, range-enabled QE fundamentally alters the physical layout of the database, requiring careful mathematical modeling of range bounds, tree sparsity, and trim factors before a single document is written.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Implementing Queryable Encryption without comprehensive architectural validation triggers three catastrophic failure modes:&lt;/p&gt;
&lt;p&gt;First, &lt;strong&gt;the external KMS latency and throttling collapse&lt;/strong&gt;. Every client application instance running &lt;code&gt;libmongocrypt&lt;/code&gt; must decrypt Data Encryption Keys (DEKs) using an external Customer Master Key (CMK) hosted in AWS KMS, Google Cloud KMS, or Azure Key Vault. When hundreds of application microservices autoscale during traffic spikes, cold-starting connection pools flood the cloud KMS with concurrent &lt;code&gt;kms:Decrypt&lt;/code&gt; API calls. If the KMS API rate limits are exceeded, client drivers fail to initialize connection pools, throwing fatal authentication exceptions and cascading into full application downtime.&lt;/p&gt;
&lt;p&gt;Second, &lt;strong&gt;metadata index storage explosion&lt;/strong&gt;. Range queries on QE fields rely on auxiliary internal collections (&lt;code&gt;&amp;#x3C;collection&gt;.esc&lt;/code&gt; for equality state and &lt;code&gt;&amp;#x3C;collection&gt;.ecoc&lt;/code&gt; for encrypted compaction output) that store encrypted bloom filter tokens. For a collection of 50 million documents, enabling range queries on two date or currency fields can cause the metadata indexes to consume hundreds of gigabytes of disk space, drastically exceeding the size of the underlying document collection itself. This storage bloat evicts active working-set pages from the WiredTiger cache, causing severe query degradation across unencrypted collections.&lt;/p&gt;
&lt;p&gt;Third, &lt;strong&gt;distributed DEK cache divergence during rotation&lt;/strong&gt;. Application servers cache decrypted DEKs in process memory to minimize KMS latency. When a security officer rotates or revokes a key, distributed application pods running across multiple Kubernetes clusters hold stale cached keys until their local TTLs expire. During this cache invalidation window, pods with expired keys fail to decrypt documents written by pods with newly rotated keys, producing intermittent, nondeterministic data corruption errors for end users.&lt;/p&gt;
&lt;p&gt;How do you architect an end-to-end verification gate that validates KMS resilience, caps metadata storage amplification, guarantees distributed key cache coherence, and proves query compatibility before releasing Queryable Encryption to production?&lt;/p&gt;
&lt;h2 id=&quot;the-zero-trust-queryable-encryption-architecture&quot;&gt;The Zero-Trust Queryable Encryption Architecture&lt;/h2&gt;
&lt;p&gt;The production-ready QE architecture decouples key management, query interception, and metadata compaction into isolated, verifiable operational planes.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Application Service Client] --&gt;|Intercept Query Predicate| B[libmongocrypt Local Proxy Engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C{DEK Cached in Process RAM?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|Yes &amp;#x26; TTL Valid| D[Encrypt Predicate into Structured Tokens]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|No or Expired| E[Request CMK Decrypt via AWS KMS / Azure KV]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|KMS Decrypt Success| F[Populate Local DEK Cache with 60m TTL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; G[Transmit Encrypted BSON Wire Frame to MongoDB 8.0]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[MongoDB Server Engine — Evaluates Ciphertext Tokens]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[Query Auxiliary Metadata Collections .esc &amp;#x26; .ecoc]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[Return Encrypted Payload to Application]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Decrypt via Local DEK| K[Deliver Plaintext Document to Business Logic]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;cryptographic-configuration-mechanics&quot;&gt;Cryptographic Configuration Mechanics&lt;/h3&gt;
&lt;p&gt;The foundation of Queryable Encryption is the &lt;code&gt;encryptedFieldsMap&lt;/code&gt;, which formally declares the cryptographic properties of each sensitive path:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Production-grade encryptedFields declaration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; encryptedFieldsMap&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;enterprise_hr.personnel_records&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    fields: [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        path: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ssn&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        bsonType: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;string&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        keyId: &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;UUID&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;e4caa7b2-91f1-4b13-9a4d-61c0d48a1201&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        queries: [{ queryType: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;equality&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        path: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;annual_compensation&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        bsonType: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;int&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        keyId: &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;UUID&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;8b14e304-3012-4d2c-81bc-40d6e8d19a22&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        queries: [{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          queryType: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;range&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          min: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          max: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2000000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          sparsity: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,     &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Controls tree indexing depth; lower = faster queries, larger index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;          trimFactor: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    // Density of bloom filter tokens; tuned for minimal false positives&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        }]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The mathematical parameters &lt;code&gt;sparsity&lt;/code&gt; and &lt;code&gt;trimFactor&lt;/code&gt; dictate the storage-vs-performance tradeoff. A &lt;code&gt;sparsity&lt;/code&gt; of 1 indexes every discrete level of the range tree, maximizing range query speed while maximizing storage bloat. A &lt;code&gt;sparsity&lt;/code&gt; of 2 or 3 skips intermediate tree levels, decreasing metadata index size by 40–60% at the cost of requiring the client to perform minor post-decryption range filtering on borderline matches.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;A production architecture review requires hands-on execution of validation runbooks before approving go-live.&lt;/p&gt;
&lt;h3 id=&quot;1-cloud-kms-failure-injection-and-fallback-verification&quot;&gt;1. Cloud KMS Failure Injection and Fallback Verification&lt;/h3&gt;
&lt;p&gt;To ensure that transient cloud KMS outages do not take down the application, initialize the driver with explicit connection timeout limits, retry backoffs, and verified IAM role delegation. This Python implementation establishes robust client-side auto-encryption with AWS KMS:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; os&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; bson.binary &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; Binary, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;STANDARD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; bson.codec_options &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CodecOptions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo.encryption &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ClientEncryption&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo.encryption_options &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; AutoEncryptionOpts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Strict environment-driven credential loading&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;KMS_PROVIDERS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;aws&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;accessKeyId&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: os.environ[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;AWS_ACCESS_KEY_ID&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;secretAccessKey&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: os.environ[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;AWS_SECRET_ACCESS_KEY&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;KEY_VAULT_NAMESPACE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;encryption.__keyVault&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;MONGODB_URI&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; os.environ[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;MONGODB_PROD_URI&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Initialize standalone client for key vault management&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;vault_client &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;MONGODB_URI&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;serverSelectionTimeoutMS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;client_encryption &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ClientEncryption(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;    kms_providers&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;KMS_PROVIDERS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;    key_vault_namespace&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;KEY_VAULT_NAMESPACE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;    key_vault_client&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;vault_client,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;    codec_options&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;CodecOptions(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;uuid_representation&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;STANDARD&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Automated health check: Verify KMS reachability and DEK decryption latency&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; verify_kms_health&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(cmk_arn: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) -&gt; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    start_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; time.time()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    try&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;        # Create an ephemeral test key to validate bidirectional KMS communication&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        test_key_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client_encryption.create_data_key(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;            &quot;aws&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            master_key&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: cmk_arn, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;region&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;us-east-1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;            key_alt_names&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;healthcheck_ephemeral&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        latency &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (time.time() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; start_time) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;        # Cleanup ephemeral key&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        vault_client[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;encryption&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;][&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;__keyVault&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;].delete_one({&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: test_key_id})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; latency&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    except&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; Exception&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; e:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        raise&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; RuntimeError&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;KMS verification failed: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{str&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(e)&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;latency_ms &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; verify_kms_health(os.environ[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;AWS_CMK_ARN&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;KMS Round-Trip Latency: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;latency_ms&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:.2f&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;ms&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;assert&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; latency_ms &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 150.0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;KMS latency exceeds 150ms safety threshold: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;latency_ms&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;ms&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2-measuring-range-metadata-index-amplification&quot;&gt;2. Measuring Range Metadata Index Amplification&lt;/h3&gt;
&lt;p&gt;Before deploying range QE to production collections, run an empirical benchmark in staging to measure the expansion factor across document data, equality state collections (&lt;code&gt;.esc&lt;/code&gt;), and compaction output collections (&lt;code&gt;.ecoc&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Execute in mongosh on staging cluster after populating 100,000 synthetic records&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; baseColl&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.personnel_records;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; baseStats&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; baseColl.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;stats&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ scale: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Locate internal QE collections&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; escName&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;personnel_records.esc&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ecocName&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;personnel_records.ecoc&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; escStats&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getCollection&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(escName).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;stats&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ scale: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ecocStats&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getCollection&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(ecocName).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;stats&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ scale: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; baseSizeMB&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; baseStats.storageSize;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; metaSizeMB&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (escStats.storageSize &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (ecocStats.storageSize &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; amplificationFactor&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (metaSizeMB &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; baseSizeMB).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`--- Queryable Encryption Storage Footprint ---`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Base Document Storage:     ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;baseSizeMB&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} MB`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Equality Metadata (.esc):   ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;escStats&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;storageSize&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} MB`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Range Metadata (.ecoc):     ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ecocStats&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;storageSize&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} MB`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Total Metadata Overhead:    ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;metaSizeMB&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} MB`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Index Amplification Factor: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;amplificationFactor&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}x`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (amplificationFactor &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3.0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`WARNING: Range metadata exceeds 3x document storage. Adjust sparsity or trimFactor.`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;3-automated-compaction-of-range-metadata-structures&quot;&gt;3. Automated Compaction of Range Metadata Structures&lt;/h3&gt;
&lt;p&gt;Frequent updates and deletes on range-encrypted fields cause the &lt;code&gt;.ecoc&lt;/code&gt; collection to accumulate tombstone tokens. Schedule a background maintenance task to run compaction and reclaim WiredTiger disk space:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Run periodic compaction on QE metadata&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Initiating compaction on Queryable Encryption metadata...&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; compactResult&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;runCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  compactStructuredEncryptionData: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;personnel_records&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;printjson&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(compactResult);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;4-zero-downtime-cmk-rotation-via-rewrapmanydatakey&quot;&gt;4. Zero-Downtime CMK Rotation via rewrapManyDataKey&lt;/h3&gt;
&lt;p&gt;When security compliance mandates annual Customer Master Key rotation in AWS KMS, do not re-encrypt document data. Re-wrap the Data Encryption Keys stored in &lt;code&gt;encryption.__keyVault&lt;/code&gt; using the new CMK ARN:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Execute via Node.js application management script&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; { &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;MongoClient&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;ClientEncryption&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; require&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;mongodb&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; rotateCustomerMasterKey&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;newCmkArn&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; client&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; MongoClient&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(process.env.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;MONGODB_PROD_URI&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;connect&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; clientEncryption&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ClientEncryption&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(client, {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    keyVaultNamespace: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;encryption.__keyVault&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    kmsProviders: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      aws: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        accessKeyId: process.env.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;AWS_ACCESS_KEY_ID&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        secretAccessKey: process.env.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;AWS_SECRET_ACCESS_KEY&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Re-wrapping active DEKs with new CMK...&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; rewrapResult&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; clientEncryption.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;rewrapManyDataKey&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    {}, &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Match all active DEKs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      provider: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;aws&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      masterKey: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        key: newCmkArn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        region: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;us-east-1&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Successfully re-wrapped ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;rewrapResult&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;bulkWriteResult&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;nModified&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} Data Encryption Keys.`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  await&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;close&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Root Cause&lt;/th&gt;&lt;th&gt;Production Blast Radius&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Cloud KMS Rate-Limit Throttling&lt;/td&gt;&lt;td&gt;Application autoscale triggers hundreds of simultaneous &lt;code&gt;kms:Decrypt&lt;/code&gt; requests on un-cached startup&lt;/td&gt;&lt;td&gt;Application connection pool initialization fails; HTTP 500 error storms across client APIs&lt;/td&gt;&lt;td&gt;Implement persistent local DEK caching via &lt;code&gt;keyVaultClient&lt;/code&gt; with a 60-minute TTL; configure AWS KMS exponential backoff retries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Metadata Storage Explosion&lt;/td&gt;&lt;td&gt;Range query enabled on high-cardinality fields with &lt;code&gt;sparsity: 1&lt;/code&gt; on a massive collection&lt;/td&gt;&lt;td&gt;Internal &lt;code&gt;.ecoc&lt;/code&gt; collections expand 5–10× larger than base data; evicts WiredTiger cache; disk space exhaustion&lt;/td&gt;&lt;td&gt;Tune range parameters: set &lt;code&gt;sparsity: 2&lt;/code&gt; or &lt;code&gt;3&lt;/code&gt;, set &lt;code&gt;trimFactor: 4&lt;/code&gt;; run weekly &lt;code&gt;compactStructuredEncryptionData&lt;/code&gt; maintenance jobs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unsupported Aggregation Pipeline Error&lt;/td&gt;&lt;td&gt;Application attempts &lt;code&gt;$regex&lt;/code&gt;, &lt;code&gt;$text&lt;/code&gt;, or mathematical &lt;code&gt;$group&lt;/code&gt; operations on encrypted fields&lt;/td&gt;&lt;td&gt;Query fails with fatal server error: &lt;code&gt;Queryable Encryption does not support the requested aggregation stage&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Audit all application query shapes prior to go-live; restrict QE fields strictly to &lt;code&gt;$eq&lt;/code&gt;, &lt;code&gt;$gt&lt;/code&gt;, &lt;code&gt;$gte&lt;/code&gt;, &lt;code&gt;$lt&lt;/code&gt;, and &lt;code&gt;$lte&lt;/code&gt; operators&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Range Boundary Overflow&lt;/td&gt;&lt;td&gt;Record inserted with a numerical or date value falling outside the declared &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; bounds&lt;/td&gt;&lt;td&gt;Document inserts successfully, but subsequent range queries silently omit the record from result sets&lt;/td&gt;&lt;td&gt;Set range bounds with wide safety margins (e.g., 2–3× maximum anticipated value); enforce client-side schema validation on write&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Distributed DEK Cache Desynchronization&lt;/td&gt;&lt;td&gt;Multiple application pods maintain independent DEK caches; key is revoked or rotated without pod restart&lt;/td&gt;&lt;td&gt;Split-brain decryption: newer pods succeed while older pods throw decryption assertion failures&lt;/td&gt;&lt;td&gt;Enforce bounded DEK cache TTLs (&lt;code&gt;keyExpirationMS: 3600000&lt;/code&gt;); trigger automated rolling restarts of application pods post-rotation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Insecure Local Key Provider in Production&lt;/td&gt;&lt;td&gt;Development configuration referencing local master key file inadvertently deployed to production Kubernetes&lt;/td&gt;&lt;td&gt;Total violation of threat model: master key stored on application filesystem accessible to attackers&lt;/td&gt;&lt;td&gt;Implement CI/CD pre-commit hooks that fail if &lt;code&gt;kmsProviders.local&lt;/code&gt; is detected in production environment manifests&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schema Evolution Lock-in&lt;/td&gt;&lt;td&gt;Product team requests converting an equality-only QE field to support range queries post-launch&lt;/td&gt;&lt;td&gt;In-place migration is mathematically impossible; requires multi-day collection rebuild and data re-encryption&lt;/td&gt;&lt;td&gt;Mandate formal product sign-off on field query types during the pre-go-live architecture review; plan for dual-write migrations&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OOM Killer Termination via libmongocrypt&lt;/td&gt;&lt;td&gt;Client application memory exhausted by caching excessive distinct DEKs in high-cardinality multitenant apps&lt;/td&gt;&lt;td&gt;Client microservice crashes with SIGKILL; connection reset errors propagated to upstream load balancers&lt;/td&gt;&lt;td&gt;Restrict key allocation to one DEK per collection rather than one DEK per tenant; monitor application container memory limits&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;2025-modern-compliance-and-hardening-update&quot;&gt;2025 Modern Compliance and Hardening Update&lt;/h2&gt;
&lt;p&gt;The regulatory landscape across financial and healthcare infrastructure has hardened considerably throughout 2024 and 2025. PCI DSS 4.0 explicitly requires cryptographic isolation of authentication credentials and primary account numbers (PAN), rendering legacy disk-level TDE insufficient when database administration is outsourced to managed cloud providers.&lt;/p&gt;
&lt;p&gt;MongoDB 8.0 introduced critical enhancements to the Queryable Encryption engine that directly address operational friction:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Native Dynamic Compaction&lt;/strong&gt;: The query engine now features automated background compaction of &lt;code&gt;.ecoc&lt;/code&gt; collections, significantly reducing the operational necessity of manually scheduling &lt;code&gt;compactStructuredEncryptionData&lt;/code&gt; commands.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tunable Contention Factors&lt;/strong&gt;: The addition of &lt;code&gt;contentionFactor&lt;/code&gt; configuration in MongoDB 8.0 allows platform engineers to parallelize write operations on frequently updated encrypted fields, eliminating internal metadata lock bottlenecks that previously throttled write-heavy workloads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hardware Security Module (HSM) KMIP 3.0 Support&lt;/strong&gt;: Modern enterprise deployments can now bridge directly to dedicated on-premises hardware security modules (such as Thales Luna or HashiCorp Vault Enterprise) using KMIP 3.0, providing cryptographic parity between public cloud and on-premises sovereign infrastructure.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Implementing MongoDB Queryable Encryption without comprehensive operational validation leads to irreversible query constraints, metadata storage bloat that exhausts database cache, and KMS rate-limiting outages.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Execute a formal pre-go-live architecture review that enforces external KMS integration, validates range indexing precision parameters, measures metadata amplification factors, and documents automated key rotation workflows.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The mathematical structures underlying structured encryption (bloom filter tokens and deterministic ciphertext) cannot be transformed in-place; running empirical benchmarks in staging proves compliance with latency and storage budgets before production cutover.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt;
&lt;ol&gt;
&lt;li&gt;Verify that &lt;code&gt;kmsProviders&lt;/code&gt; strictly uses AWS KMS, GCP KMS, Azure Key Vault, or KMIP—never &lt;code&gt;local&lt;/code&gt;—in all production deployment manifests.&lt;/li&gt;
&lt;li&gt;Execute the synthetic data benchmark script in staging to confirm that the metadata amplification factor does not exceed 3.0×.&lt;/li&gt;
&lt;li&gt;Validate that all application queries targeting QE fields strictly adhere to supported &lt;code&gt;$eq&lt;/code&gt;, &lt;code&gt;$gte&lt;/code&gt;, &lt;code&gt;$lte&lt;/code&gt; predicates, with zero usage of &lt;code&gt;$regex&lt;/code&gt; or &lt;code&gt;$text&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Implement an automated monitoring script that tracks the creation date and rotation status of all Data Encryption Keys in &lt;code&gt;encryption.__keyVault&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;MongoDB Manual — Queryable Encryption Core Architecture and Concepts: &lt;a href=&quot;https://www.mongodb.com/docs/manual/core/queryable-encryption/&quot;&gt;https://www.mongodb.com/docs/manual/core/queryable-encryption/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — Fundamentals of Encrypted Fields Configuration: &lt;a href=&quot;https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query/&quot;&gt;https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — Compacting Structured Encryption Collections: &lt;a href=&quot;https://www.mongodb.com/docs/manual/reference/command/compactStructuredEncryptionData/&quot;&gt;https://www.mongodb.com/docs/manual/reference/command/compactStructuredEncryptionData/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;PCI Security Standards Council — PCI DSS v4.0 Cryptographic Requirements: &lt;a href=&quot;https://www.pcisecuritystandards.org/&quot;&gt;https://www.pcisecuritystandards.org/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;NIST Special Publication 800-57 Part 1 — Recommendation for Key Management: &lt;a href=&quot;https://csrc.nist.gov/publications/detail/sp/800-57-part-1/rev-5/final&quot;&gt;https://csrc.nist.gov/publications/detail/sp/800-57-part-1/rev-5/final&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;AWS Key Management Service Developer Guide — Best Practices and Request Limits: &lt;a href=&quot;https://docs.aws.amazon.com/kms/latest/developerguide/&quot;&gt;https://docs.aws.amazon.com/kms/latest/developerguide/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;USENIX Security — Order-Revealing Encryption and Metadata Leakage Mitigations: &lt;a href=&quot;https://www.usenix.org/conference/usenixsecurity18/&quot;&gt;https://www.usenix.org/conference/usenixsecurity18/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>architecture</category><category>checklist</category></item><item><title>Debugging Cassandra Write Latency: CommitLog, Memtables, Flushes, and Backpressure</title><link>https://rajivonai.com/blog/2025-05-07-debugging-cassandra-write-latency-commitlog/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-05-07-debugging-cassandra-write-latency-commitlog/</guid><description>Tracing Apache Cassandra&apos;s write path to identify how saturated commitlogs and undersized memtable flush queues trigger massive write latency and client timeouts.</description><pubDate>Wed, 07 May 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Cassandra is famous for its raw write speed. Because it appends data sequentially, a properly tuned Cassandra cluster can ingest hundreds of thousands of writes per second with sub-millisecond latency.&lt;/p&gt;
&lt;p&gt;When write latency spikes, it is almost never a CPU problem. It is a queueing problem. When the queues fill up, Cassandra executes backpressure, dropping your data on the floor to prevent the JVM from crashing.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In 2023, the 36-node Cassandra 4.1 cluster was absorbing ~150k write QPS perfectly—until cluster-wide write latency suddenly spiked over 500ms.&lt;/p&gt;
&lt;p&gt;This latency cascaded upstream. Client drivers hit their &lt;code&gt;WriteTimeoutException&lt;/code&gt; limits, and the message queue consumers stalled. &lt;code&gt;nodetool tpstats&lt;/code&gt; showed thousands of pending and dropped tasks queuing up in &lt;code&gt;MutationStage&lt;/code&gt; (there’s no separate commitlog thread pool in &lt;code&gt;tpstats&lt;/code&gt; — commitlog I/O pressure shows up as &lt;code&gt;MutationStage&lt;/code&gt; backpressure, since mutations block there waiting on the commitlog write), and disk monitoring (&lt;code&gt;iostat&lt;/code&gt;) showed severe write latency on the EBS volume hosting the data directories.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;To debug a write timeout, you must understand Cassandra’s write path architecture.&lt;/p&gt;
&lt;p&gt;When a write hits a replica, the data bifurcates into two distinct locations simultaneously:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The CommitLog:&lt;/strong&gt; An append-only log. Under the default &lt;code&gt;periodic&lt;/code&gt; sync mode (covered below), this is written to the OS page cache and fsynced to disk on a timer rather than per-write, so it doesn’t guarantee durability against power loss for every individual write — only &lt;code&gt;commitlog_sync: batch&lt;/code&gt; does that.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Memtable:&lt;/strong&gt; An in-memory skip-list data structure.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The write is acknowledged as successful to the coordinator &lt;em&gt;only&lt;/em&gt; after the mutation is applied to both the commitlog and the memtable in memory — under &lt;code&gt;periodic&lt;/code&gt; sync this doesn’t mean the commitlog write has been fsynced to disk yet, just that it’s been handed to both structures.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord[Coordinator] --&gt;|Mutation Request| Rep(Replica Node)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Rep --&gt; Fork{Bifurcate Write}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Fork --&gt; Memtable[Memtable — In-Memory Skip-List]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Fork --&gt; CommitLog[CommitLog — Sequential Disk Append]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Memtable --&gt;|Threshold Reached| Flush[Memtable Flush Writers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Flush --&gt; SSTable[SSTable Data.db — Disk Flush]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CommitLog --&gt;|Acknowledged| Ack[Return Success]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Memtable --&gt;|Acknowledged| Ack&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    %% Bottlenecks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SSTable -.-&gt;|Disk IO Saturated| Flush&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Flush -.-&gt;|Queue Full| Memtable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Memtable -.-&gt;|Memory Full| Backpressure[MutationStage — Dropped Messages]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The configuration suffered from two catastrophic flaws:
First, we co-located the &lt;code&gt;commitlog_directory&lt;/code&gt; on the same EBS &lt;code&gt;gp3&lt;/code&gt; volume as the &lt;code&gt;data_file_directories&lt;/code&gt;. The commitlog demands pure, sequential I/O. When background compactions ran on the SSTables, they generated massive random read/write I/O, completely stalling the commitlog’s ability to append data.&lt;/p&gt;
&lt;p&gt;Second, our &lt;code&gt;memtable_flush_writers&lt;/code&gt; were undersized. When memtables fill up (based on &lt;code&gt;memtable_heap_space_in_mb&lt;/code&gt; and &lt;code&gt;memtable_offheap_space_in_mb&lt;/code&gt;), Cassandra queues them to be flushed to disk as SSTables. Because the disk was saturated by compaction, the flush writers backed up. When the flush queue is full, Cassandra cannot accept new writes into the memtable, triggering &lt;code&gt;MutationStage&lt;/code&gt; dropped messages.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The playbook executes a permanent physical separation of the I/O paths.&lt;/p&gt;
&lt;p&gt;The architecture moves the commitlog to a dedicated, physically isolated EBS volume with a provisioned 3,000 IOPS and 250 MB/s throughput. This guaranteed that compaction could run at 100% capacity on the data volume without ever stealing an IOP from the write path.&lt;/p&gt;
&lt;p&gt;We then tuned &lt;code&gt;cassandra.yaml&lt;/code&gt; to optimize the flush queues:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Dedicate specific threads to flushing memtables based on core count&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;memtable_flush_writers&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Move memtable allocations off the JVM heap to prevent G1GC pauses&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;memtable_allocation_type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;offheap_objects&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, the pattern builds a real-time Prometheus alert on &lt;code&gt;CommitLogPendingTasks&lt;/code&gt; and &lt;code&gt;MemtableFlushPendingTasks&lt;/code&gt;. If the queue builds up beyond 500 tasks, we receive an alert &lt;em&gt;before&lt;/em&gt; client write timeouts begin.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;&lt;code&gt;commitlog_sync: batch&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Batch mode forces a synchronous disk &lt;code&gt;fsync&lt;/code&gt; on every write before acknowledging the client. It provides maximum durability but absolutely destroys throughput.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use &lt;code&gt;periodic&lt;/code&gt; mode (sync every 10,000ms) for high-throughput workloads where OS page cache buffering is acceptable.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;High &lt;code&gt;offheap&lt;/code&gt; allocation&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Setting off-heap memtable space too high steals memory from the Linux OS page cache, degrading read performance.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Balance &lt;code&gt;memtable_offheap_space_in_mb&lt;/code&gt; against your system RAM, leaving at least 30% for the OS.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Combined Storage Volumes&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Putting commitlogs and data on one NVMe drive seems fast, but high concurrent load will inevitably cause write stalls.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Always isolate the commitlog directory to a dedicated volume.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-and-trie-memtables&quot;&gt;Modern Cassandra Context: Cassandra 5.0 and Trie Memtables&lt;/h3&gt;
&lt;p&gt;Cassandra 5.0 introduces &lt;strong&gt;Trie Memtables&lt;/strong&gt; (&lt;code&gt;memtable: trie&lt;/code&gt;). By replacing the legacy skip-list with a concurrent prefix tree, Cassandra significantly increases write concurrency. Because the Trie is vastly more efficient at managing memory overhead, it nearly eliminates the garbage collection stalls previously associated with high-velocity memtable churn, allowing operators to run massive write loads on smaller EC2 instances without dropping mutations.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Co-locating commitlogs with SSTables causes compactions to starve the sequential write path, backing up flush queues and dropping mutations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Move the commitlog to a dedicated EBS volume and shift memtable allocation off-heap.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Physical I/O separation isolates the write path, ensuring p99 write latencies remain stable regardless of background compaction storms.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run &lt;code&gt;nodetool tpstats&lt;/code&gt; and check &lt;code&gt;MutationStage&lt;/code&gt;. If &lt;code&gt;Dropped&lt;/code&gt; is greater than 0, your nodes are silently executing backpressure. Check your &lt;code&gt;iostat&lt;/code&gt; utilization immediately.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>failures</category></item><item><title>The Architecture of Natural Language Database Interfaces</title><link>https://rajivonai.com/blog/2025-05-03-nl-database-interface-apr-2025/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-05-03-nl-database-interface-apr-2025/</guid><description>Replacing the translation overhead between business questions and SQL queries requires an architecture that bridges LLM intent parsing with strict execution validation and schema retrieval.</description><pubDate>Sat, 03 May 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Database teams translate constantly — business questions into SQL queries, operational intent into CLI commands, and raw telemetry into actionable insights. Each translation step costs time and introduces error. While natural language interfaces offer a compelling solution, bolting a Large Language Model (LLM) directly to a production database creates unacceptable risks of hallucinated queries, inefficient resource usage, and unauthorized data access. Moving these interfaces from experimental prototypes to production requires solving deeply for schema complexity, semantic ambiguity, and execution safety.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The tooling for database query assistance has historically required specialists at every step. A stakeholder who wants to know which users had failed transactions last week needs an engineer to write the SQL. A product manager looking for churn metrics must wait in a business intelligence queue. Natural language-to-SQL (NL2SQL) interfaces have been technically feasible since large language models gained advanced reasoning capabilities, but deploying them safely in enterprise environments remains an architectural challenge.&lt;/p&gt;
&lt;p&gt;Early attempts focused merely on text generation, leaving engineers to manually verify the safety and correctness of the resulting queries before execution. These naive implementations often treated the LLM as an infallible translation layer, ignoring the reality of deeply nested schemas, undocumented legacy tables, and the sheer destructive potential of executing unvalidated code against live data.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The translation costs compound across a database team, but directly substituting engineers with naive LLM implementations fails predictably and dangerously. The failures manifest in three critical areas:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Schema Hallucination:&lt;/strong&gt; LLMs invent column names, imagine non-existent tables, or ignore critical foreign key relationships when the target schema is large. Without strict grounding, an LLM will confidently query a &lt;code&gt;user_transactions&lt;/code&gt; table that doesn’t actually exist.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ambiguous Intent:&lt;/strong&gt; “Total revenue” might mean gross sales, net collected, or booked ARR, requiring domain-specific logic that foundational models inherently lack. Business context is not encoded in the database dialect.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Execution Risk:&lt;/strong&gt; Generated queries might contain destructive operations (like an unintended &lt;code&gt;DROP&lt;/code&gt; or &lt;code&gt;UPDATE&lt;/code&gt; generated during a prompt injection) or execute inefficient cross joins that lock tables and degrade database performance for real users.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The question: how can engineering teams architect a natural language database interface that provides accurate, safe, and performant SQL generation without exposing the underlying infrastructure to unbounded risk?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;A robust Natural Language Database Interface separates intent parsing, context retrieval, execution validation, and the final query execution into strictly isolated architectural layers.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    User[user query — plain English]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    User --&gt; IntentLayer[intent parsing — LLM]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    IntentLayer --&gt; RAG[schema retrieval — vector store]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    RAG --&gt; DDL[context injection — DDL and definitions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DDL --&gt; GenerationLayer[SQL generation — LLM]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    GenerationLayer --&gt; Validation[query validation — EXPLAIN]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Validation --&gt; Execution[database execution — read-only role]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Execution --&gt; Output[results and visualization returned]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Schema Ingestion and RAG&lt;/strong&gt;
Instead of attempting to inject an entire massive database schema into the LLM’s context window—which quickly exceeds token limits, dilutes attention, and degrades reasoning capability—the architecture relies on Retrieval-Augmented Generation (RAG). The database schema, including DDL statements, table descriptions, metadata, and common query patterns, is continuously indexed into a vector store. When a user asks a question, a lightweight router first determines the intent, and only the relevant subset of the schema (e.g., the specific tables related to payments, users, and subscriptions) is retrieved. This provides highly concentrated, accurate context to the generation layer without overwhelming the model.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Generation and Domain Logic&lt;/strong&gt;
The generation layer requires domain-specific terminology libraries to bridge the gap between human idioms and raw column names. By mapping business terms to specific SQL snippets, canonical tables, or view definitions before the prompt is finalized, the system reduces the risk of the LLM misinterpreting business logic. If the user asks for “active users,” the system dynamically injects the agreed-upon corporate definition of an active user (e.g., users who have logged in within the last 30 days) into the LLM context. This semantic mapping prevents the model from guessing the logic and producing queries that are syntactically valid but business-incorrect.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Validation and Safe Execution&lt;/strong&gt;
Before execution, the generated SQL must be rigorously validated. This cannot rely on a simple application-layer regex check (like checking for the absence of &lt;code&gt;DROP TABLE&lt;/code&gt;). The query must be syntactically valid for the specific database dialect and semantically safe to execute against the target cluster without causing an outage.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for validating LLM-generated queries relies on native database parsing capabilities rather than application-layer regex, which is notoriously fragile against clever SQL injection or obfuscation. PostgreSQL’s behavior when processing the &lt;code&gt;EXPLAIN&lt;/code&gt; command (specifically without the &lt;code&gt;ANALYZE&lt;/code&gt; flag) evaluates the syntax and schema references of a query, returning the execution plan without actually executing the data retrieval or modification. This provides a deterministic validation step: if PostgreSQL’s query planner rejects the query due to a syntax error or a hallucinated column, the architecture can intercept the resulting database error, parse it, and automatically prompt the LLM to correct the syntax before any execution occurs.&lt;/p&gt;
&lt;p&gt;Furthermore, PostgreSQL’s role-based access control (RBAC) behaves as the ultimate safety net. By assigning the execution layer a strictly read-only role (&lt;code&gt;SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY&lt;/code&gt;), the database engine itself enforces safety at the lowest level. This prevents any hallucinated &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt;, or &lt;code&gt;DDL&lt;/code&gt; commands from succeeding, completely neutralizing the threat of destructive prompt injections, regardless of what the LLM generates. This approach guarantees that even if a malicious user manages to trick the LLM into generating a &lt;code&gt;DROP DATABASE&lt;/code&gt; command, the execution will deterministically fail.&lt;/p&gt;
&lt;p&gt;Additionally, the documented pattern for preventing runaway queries—such as accidental Cartesian products or unindexed table scans generated by the LLM—involves setting strict statement timeouts at the session level (&lt;code&gt;SET statement_timeout = &apos;10s&apos;&lt;/code&gt;). This ensures that an inefficient, AI-generated query does not monopolize database connection pools, exhaust memory, or degrade compute resources for production workloads. Combining RBAC, &lt;code&gt;EXPLAIN&lt;/code&gt; validation, and session timeouts creates a zero-trust execution environment explicitly designed for non-deterministic SQL generation.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Plausible-but-wrong SQL&lt;/td&gt;&lt;td&gt;Complex aggregations with multiple group-by dimensions where the LLM misunderstands the required granularity.&lt;/td&gt;&lt;td&gt;Maintain a library of validated SQL templates as few-shot examples for the most common complex business queries.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schema hallucination&lt;/td&gt;&lt;td&gt;Tables with ambiguous naming, undocumented legacy columns, or missing foreign key constraints.&lt;/td&gt;&lt;td&gt;Require strict metadata documentation in the schema index; enforce data constraints explicitly in the database.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Token limits exceeded&lt;/td&gt;&lt;td&gt;Attempting to inject a multi-thousand table schema directly into the prompt without filtering.&lt;/td&gt;&lt;td&gt;Implement a RAG pipeline to retrieve only the relevant table DDLs and schema fragments based on the user’s intent.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Dialect mismatch&lt;/td&gt;&lt;td&gt;An LLM trained heavily on MySQL generates valid syntax that fails in PostgreSQL (e.g., quoting rules).&lt;/td&gt;&lt;td&gt;Explicitly inject the target SQL dialect rules and database version constraints into the system prompt.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Business users wait on engineers for data, but naive LLM-to-SQL tools hallucinate queries and introduce significant operational and security risks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Implement a layered NL2SQL architecture that isolates generation from execution, using RAG for schema context, &lt;code&gt;EXPLAIN&lt;/code&gt; for native validation, and read-only roles for safe execution.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: PostgreSQL’s native &lt;code&gt;EXPLAIN&lt;/code&gt; behavior combined with read-only transaction characteristics provides a deterministic, zero-trust validation mechanism that cannot be bypassed by prompt injection.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Before building or buying the LLM layer, audit your database schema for missing foreign keys and undocumented columns—accurate, well-documented schema metadata is the unavoidable foundation of any reliable natural language interface.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>architecture</category></item><item><title>Per-Application Postgres on Kubernetes Is an Isolation Strategy</title><link>https://rajivonai.com/blog/2025-04-26-per-application-postgres-on-kubernetes-is-an-isolation-strat/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-04-26-per-application-postgres-on-kubernetes-is-an-isolation-strat/</guid><description>How CloudNativePG, GitOps, and External Secrets turn Postgres-on-Kubernetes into an operational isolation pattern.</description><pubDate>Sat, 26 Apr 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Postgres-on-Kubernetes is not a cheaper managed database; it is a decision to turn each application database into its own auditable, recoverable, failure-contained operating unit.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Teams are pushing more stateful infrastructure into Kubernetes because the rest of the delivery system already lives there: GitOps, policy admission, secrets, observability, and rollout control. CloudNativePG gives PostgreSQL a Kubernetes-native control plane, but the architectural question is not “can the operator run Postgres?” It can.&lt;/p&gt;
&lt;p&gt;The better question is whether per-application clusters are worth the operational multiplication.&lt;/p&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Default approach&lt;/th&gt;&lt;th&gt;Alternative&lt;/th&gt;&lt;th&gt;What changes&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Shared managed PostgreSQL instance&lt;/td&gt;&lt;td&gt;Per-application CloudNativePG cluster&lt;/td&gt;&lt;td&gt;Isolation moves from database names to failure domains&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Ticket-driven database provisioning&lt;/td&gt;&lt;td&gt;GitOps database manifests&lt;/td&gt;&lt;td&gt;Provisioning becomes reviewable infrastructure state&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Central backup policy&lt;/td&gt;&lt;td&gt;Declared backup per cluster&lt;/td&gt;&lt;td&gt;Recovery becomes an application contract&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One upgrade path&lt;/td&gt;&lt;td&gt;Independent cluster lifecycle&lt;/td&gt;&lt;td&gt;Coordination cost moves to platform standards&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Shared PostgreSQL looks efficient until one application’s database lifecycle starts behaving like everyone’s outage. A migration that takes an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock, a connection storm after a deploy, a bad &lt;code&gt;DELETE FROM&lt;/code&gt;, or a noisy autovacuum cycle does not respect team boundaries just because the schemas have different names.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure point&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Shared compute and I/O&lt;/td&gt;&lt;td&gt;One workload consumes CPU, memory, WAL bandwidth, or storage IOPS&lt;/td&gt;&lt;td&gt;PostgreSQL isolation inside one instance is weaker than Kubernetes isolation across pods, PVCs, and quotas&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Shared upgrade window&lt;/td&gt;&lt;td&gt;PostgreSQL 15 to 16, extension changes, or parameter restarts affect unrelated apps&lt;/td&gt;&lt;td&gt;Teams lose independent lifecycle control even when their schema is not changing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Shared blast radius&lt;/td&gt;&lt;td&gt;A rogue migration, bad application deploy, or dropped table lands inside a common operational boundary&lt;/td&gt;&lt;td&gt;Recovery decisions become political: restore one app and risk everyone else, or do surgery under pressure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GitOps drift&lt;/td&gt;&lt;td&gt;Argo CD can reconcile Deployments while the database remains a manually created external dependency&lt;/td&gt;&lt;td&gt;The application appears declarative, but its most important dependency is still tribal memory&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failover optimism&lt;/td&gt;&lt;td&gt;The database promotes a replica, but clients keep dead TCP sessions or stale DNS targets&lt;/td&gt;&lt;td&gt;The operator can move the primary; it cannot prove the application survived&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;CloudNativePG addresses part of this by giving each &lt;code&gt;Cluster&lt;/code&gt; resource its own primary, replicas, services, WAL archive, backups, and Kubernetes lifecycle. The trap is thinking that means the hard part is solved. The real design question is: how do you get the isolation benefit without creating fifty tiny database platforms?&lt;/p&gt;
&lt;h2 id=&quot;per-application-clusters-as-an-isolation-plane&quot;&gt;Per-Application Clusters as an Isolation Plane&lt;/h2&gt;
&lt;p&gt;The right architecture is a platform contract: every application gets its own PostgreSQL cluster, but every cluster is created through the same operator, GitOps layout, secret flow, backup policy, monitoring labels, and recovery drill.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Dev[developer change] --&gt; Git[git repository — apps and databases]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Git --&gt; Argo[Argo CD ApplicationSet]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Argo --&gt; App[application namespace]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Argo --&gt; DB[CloudNativePG Cluster]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Vault[cloud secret manager] --&gt; ESO[External Secrets operator]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ESO --&gt; AppSecret[Kubernetes Secret — app credentials]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ESO --&gt; DBSecret[Kubernetes Secret — backup credentials]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DB --&gt; RW[read write service]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DB --&gt; RO[read only service]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DB --&gt; WAL[WAL archive — object storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Prom[Prometheus] --&gt; Dash[Grafana dashboard]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DB --&gt; Prom&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App --&gt; RW&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Separate application and database manifests, but reconcile both from Git.&lt;/strong&gt;&lt;br&gt;
Use a layout such as &lt;code&gt;apps/linkding/overlays/dev&lt;/code&gt; and &lt;code&gt;databases/linkding/overlays/dev&lt;/code&gt;, with separate Argo CD &lt;code&gt;ApplicationSet&lt;/code&gt; definitions. The separation matters because application rollout and database lifecycle have different risk profiles. A Deployment rollback is not the same thing as rewinding a database.&lt;br&gt;
&lt;strong&gt;Verification:&lt;/strong&gt; a fresh namespace can be rebuilt from Git without a manual database creation step.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Use CloudNativePG services as the only in-cluster database entry point.&lt;/strong&gt;&lt;br&gt;
CloudNativePG manages &lt;code&gt;rw&lt;/code&gt;, &lt;code&gt;ro&lt;/code&gt;, and &lt;code&gt;r&lt;/code&gt; services; the &lt;code&gt;rw&lt;/code&gt; service points at the current primary, while &lt;code&gt;ro&lt;/code&gt; points at replicas where available, according to the &lt;a href=&quot;https://cloudnative-pg.io/docs/1.28/service_management/&quot;&gt;CloudNativePG service management documentation&lt;/a&gt;. Do not connect applications directly to pod DNS names. That is how failover tests pass in the database layer and fail in the application layer.&lt;br&gt;
&lt;strong&gt;Verification:&lt;/strong&gt; delete the current primary pod, then confirm the application writes through &lt;code&gt;&amp;#x3C;cluster&gt;-rw&lt;/code&gt; after promotion.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Externalize secrets before the first cluster exists.&lt;/strong&gt;&lt;br&gt;
Database owner credentials, application passwords, Azure Blob or S3 credentials, and backup access should come from a cloud secret manager through External Secrets. Kubernetes Secrets are the runtime projection, not the source of authority.&lt;br&gt;
&lt;strong&gt;Verification:&lt;/strong&gt; rotating the upstream secret updates the projected Kubernetes Secret and triggers the expected application or pooler reload path.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Treat WAL archiving as a production requirement, not a backup checkbox.&lt;/strong&gt;&lt;br&gt;
CloudNativePG 1.29 documents point-in-time recovery as dependent on a valid WAL archive, and recovery bootstraps a new cluster rather than restoring in place (&lt;a href=&quot;https://cloudnative-pg.io/docs/1.29/recovery&quot;&gt;recovery docs&lt;/a&gt;). That distinction is operationally important: your restore manifest is a runbook, not a patch to the broken cluster.&lt;br&gt;
&lt;strong&gt;Verification:&lt;/strong&gt; create a temporary namespace, restore from the latest base backup plus WAL, and run application-level read checks.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Standardize admission policy before the tenth database.&lt;/strong&gt;&lt;br&gt;
Per-app clusters multiply everything: PVCs, PodDisruptionBudgets, backup jobs, certificates, metrics, alerts, and upgrade queues. Use Kyverno or OPA Gatekeeper to require resource requests, backup retention, owner labels, network policies, and anti-affinity.&lt;br&gt;
&lt;strong&gt;Verification:&lt;/strong&gt; a malformed &lt;code&gt;Cluster&lt;/code&gt; manifest is rejected before Argo CD can apply it.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;One version-specific gotcha: CloudNativePG scheduled backups use a six-field cron expression with seconds, not the five-field Unix format; &lt;code&gt;0 0 0 * * *&lt;/code&gt; means midnight in CNPG, while Kubernetes CronJobs would use &lt;code&gt;0 0 * * *&lt;/code&gt; (&lt;a href=&quot;https://cloudnative-pg.io/docs/1.29/backup&quot;&gt;CNPG backup docs&lt;/a&gt;). That is exactly the kind of small mismatch that becomes a failed audit three months later.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern is not theoretical. Zalando wrote in 2017 that the gap between an engineer wanting PostgreSQL and the database team creating it was still a ticketing workflow; their stated direction was to trigger PostgreSQL cluster setup from engineers committing to Git through the Kubernetes API (&lt;a href=&quot;https://engineering.zalando.com/posts/2017/06/postgresql-in-a-time-of-kubernetes.html&quot;&gt;Zalando Engineering, 2017&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;By 2018, Zalando reported using its Postgres operator to manage more than 400 PostgreSQL clusters across Kubernetes installations, with the operator watching declarative manifests and carrying out create, update, and delete operations (&lt;a href=&quot;https://engineering.zalando.com/posts/2018/11/postgres-operator.html&quot;&gt;Zalando Engineering, 2018&lt;/a&gt;). That is the important lesson: the operator was not valuable because YAML is charming. It was valuable because manual operations had become impossible at fleet scale.&lt;/p&gt;
&lt;p&gt;CloudNativePG is a different operator, but the system behavior maps cleanly. A &lt;code&gt;Cluster&lt;/code&gt; custom resource describes desired database state. The operator reconciles pods, replication, services, backups, and status. Kubernetes becomes the control plane, and Git becomes the audit trail. The production pattern is per-application autonomy inside platform-enforced boundaries.&lt;/p&gt;
&lt;p&gt;The part the tutorial usually underplays is client behavior during failover. CloudNativePG can promote a replica and repoint the &lt;code&gt;rw&lt;/code&gt; service, but a Java service using HikariCP, a Django app with persistent connections, or PgBouncer in transaction pooling mode still has to discard broken sessions and reconnect. Kubernetes service updates do not magically heal a process holding a dead TCP socket. Your HA test is not complete until writes succeed through the normal application code path after primary loss.&lt;/p&gt;
&lt;p&gt;Schema changes also need their own protocol. GitOps is good at reconciling declarative infrastructure; it is not a migration ordering engine. PostgreSQL DDL can block, rewrite, or invalidate assumptions depending on the operation and version. Postgres 11 reduced pain for adding columns with constant defaults, but lock acquisition still matters. The practical rule is simple: deploy backward-compatible schema first, ship compatible application code second, remove old schema last. The database cluster being per-app makes this easier, not automatic.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Control-plane overload&lt;/td&gt;&lt;td&gt;Dozens of three-instance clusters create hundreds of pods, PVCs, Services, Secrets, PodMonitors, and backup objects&lt;/td&gt;&lt;td&gt;Set namespace quotas, require owner labels, cap default instance counts, and watch Kubernetes API latency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Fake failover success&lt;/td&gt;&lt;td&gt;&lt;code&gt;kubectl delete pod&lt;/code&gt; promotes a replica, but app clients hold stale TCP sessions&lt;/td&gt;&lt;td&gt;Test through the real app and pooler; enforce connection lifetime, retry policy, and startup probes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup theater&lt;/td&gt;&lt;td&gt;WAL ships to object storage, but no one has restored a cluster since launch&lt;/td&gt;&lt;td&gt;Schedule restore drills; measure recovery point objective and recovery time objective with restored application reads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GitOps fights the operator&lt;/td&gt;&lt;td&gt;Argo CD prunes generated objects or overwrites operator-managed fields&lt;/td&gt;&lt;td&gt;Scope Argo CD ownership to declared resources; ignore generated status and operator-owned children&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Migration lock incident&lt;/td&gt;&lt;td&gt;A large table migration blocks writes or waits behind long transactions&lt;/td&gt;&lt;td&gt;Add lock timeout budgets, split schema and code deploys, and run preflight checks for blocking sessions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Version skew&lt;/td&gt;&lt;td&gt;Tutorial pins CNPG chart &lt;code&gt;0.20.1&lt;/code&gt; and PostgreSQL &lt;code&gt;16.1&lt;/code&gt;, while the platform has moved to CNPG 1.29 and newer Postgres images&lt;/td&gt;&lt;td&gt;Pin operator, CRDs, image catalogs, and Postgres major versions explicitly; rehearse operator upgrades outside production&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Restore collision&lt;/td&gt;&lt;td&gt;A recovered cluster writes WAL into the same archive prefix as the source&lt;/td&gt;&lt;td&gt;Use unique server names and bucket paths; CNPG 1.29 includes archive safety checks for this class of mistake&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Read replica misuse&lt;/td&gt;&lt;td&gt;Application sends correctness-sensitive reads to &lt;code&gt;ro&lt;/code&gt; and observes replication lag&lt;/td&gt;&lt;td&gt;Use replicas for tolerant analytical reads; keep read-after-write paths on &lt;code&gt;rw&lt;/code&gt; unless the app handles lag explicitly&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Shared PostgreSQL hides unrelated applications inside the same failure and recovery boundary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Move one application at a time to its own CloudNativePG cluster, but require the same GitOps layout, external secret source, WAL archive, monitoring labels, resource limits, and admission policy for every cluster.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: The rollout is valid only when the application writes successfully through &lt;code&gt;&amp;#x3C;cluster&gt;-rw&lt;/code&gt; after primary deletion, restores into a temporary namespace from base backup plus WAL, and passes an application-level read check against the restored database.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, choose one non-critical service and run the checklist: create a three-instance CNPG cluster, wire credentials through External Secrets, archive WAL to object storage, add Prometheus alerts, enforce namespace quota and owner labels, delete the primary pod, restore into a temporary namespace, and document the recovery command sequence in the repository.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The mature version of Postgres-on-Kubernetes is not bravado about running stateful workloads; it is the discipline to make every small database boring in exactly the same way.&lt;/p&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category></item><item><title>Debugging Cassandra Read Latency: From CQL to SSTables</title><link>https://rajivonai.com/blog/2025-04-21-debugging-cassandra-read-latency-bloom-filters/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-04-21-debugging-cassandra-read-latency-bloom-filters/</guid><description>A deep dive into the Apache Cassandra read path, explaining how Bloom Filters, Key Caches, and Speculative Retries dictate millisecond query performance.</description><pubDate>Mon, 21 Apr 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In Apache Cassandra, writing data is incredibly fast because it is purely sequential. Reading data is where the distributed systems tax is paid.&lt;/p&gt;
&lt;p&gt;When a client application requests a single row, Cassandra must locate that data across a network of nodes, merge it with any recent updates sitting in memory, filter out deleted data (tombstones), and return the result. If any step in this pipeline degrades, sub-millisecond queries turn into 100ms bottlenecks.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In 2023, the p99 read latency on a critical 36-node cluster degraded from 5ms to 120ms during peak business hours. Strangely, the p50 (median) latency remained perfectly healthy at sub-5ms.&lt;/p&gt;
&lt;p&gt;Client drivers began triggering &lt;code&gt;ReadTimeoutException&lt;/code&gt;. On the cluster side, the coordinator thread pools showed elevated active tasks in the &lt;code&gt;ReadStage&lt;/code&gt;, and specific nodes were exhibiting high disk read IOPS. We needed to understand why a small percentage of queries were suddenly hitting a massive I/O wall.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;To debug read latency, you must understand the exact sequence of events that occurs when a &lt;code&gt;SELECT&lt;/code&gt; statement hits a Cassandra replica.&lt;/p&gt;
&lt;p&gt;Cassandra is designed to avoid physical disk reads whenever possible. The read path is a gauntlet of in-memory checks designed to definitively answer one question: &lt;em&gt;Does this SSTable contain the data I am looking for?&lt;/em&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Memtable Check:&lt;/strong&gt; Check in-memory recent writes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bloom Filter:&lt;/strong&gt; Check an in-memory bitset. If it says “No,” the data is definitively not in this SSTable. If it says “Yes,” the data &lt;em&gt;might&lt;/em&gt; be here.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Key Cache:&lt;/strong&gt; If the Bloom filter says yes, check the Key Cache to find the exact byte offset on disk.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Partition Index:&lt;/strong&gt; If there’s a cache miss, read the &lt;code&gt;Index.db&lt;/code&gt; file to find the offset.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Disk Fetch:&lt;/strong&gt; Finally, read the &lt;code&gt;Data.db&lt;/code&gt; SSTable.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The problem was occurring at the Bloom filter stage. A Bloom filter has a configurable False Positive chance (&lt;code&gt;bloom_filter_fp_chance&lt;/code&gt;). The read-heavy tables were using a misconfigured value (0.1 or 10%) instead of the 1% default. Because compaction was slightly lagging, a single query had to check 15 SSTables. The 10% false positive rate meant Cassandra was frequently guessing “Yes,” bypassing the filters, and performing physical disk seeks on SSTables that didn’t actually contain the data.&lt;/p&gt;
&lt;h2 id=&quot;the-read-path-architecture&quot;&gt;The Read Path Architecture&lt;/h2&gt;
&lt;p&gt;To fix the latency, operators must tune the layers that prevent physical disk access.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord[Coordinator Node] --&gt;|LOCAL_QUORUM| Rep(Replica Node)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Rep --&gt; Memtable{1. Memtable Check}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Memtable --&gt; Bloom{2. Bloom Filter Check — In-Memory Bitset}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Bloom --&gt;|Definitive NO| Skip[Skip SSTable — Zero Disk I/O]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Bloom --&gt;|Probable YES| KeyCache{3. Key Cache Hit?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    KeyCache --&gt;|Yes| Disk[5. SSTable Data.db Fetch — Direct to Byte Offset]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    KeyCache --&gt;|No| PartIndex[4. Read Partition Index.db]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PartIndex --&gt; Disk&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Disk --&gt; Merge[6. Merge Memtable &amp;#x26; SSTables — Filter Tombstones]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Merge --&gt; Return[Return to Coordinator]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    %% Speculative Retry&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord -.-&gt;|Latency &gt; p99| Spec[Speculative Retry — Query Another Replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-tuning-the-bloom-filter&quot;&gt;1. Tuning the Bloom Filter&lt;/h3&gt;
&lt;p&gt;For read-heavy tables, the misconfigured 10% false positive rate is too high. We altered the table schema to tighten the Bloom filter:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; lookups&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;user_profiles&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; bloom_filter_fp_chance &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;01&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This forced the database to use slightly more off-heap RAM to build a larger bitset, but it drastically reduced the number of false-positive disk seeks. The playbook runs &lt;code&gt;nodetool upgradesstables&lt;/code&gt; to rebuild the filters on disk.&lt;/p&gt;
&lt;h3 id=&quot;2-taming-speculative-retries&quot;&gt;2. Taming Speculative Retries&lt;/h3&gt;
&lt;p&gt;Because the initial latency spikes had pushed the p99 latency high, the coordinator’s &lt;strong&gt;Speculative Retry&lt;/strong&gt; mechanism triggered. If a coordinator doesn’t get a response within the p99 threshold, it fires the identical query to a different replica to race them. This was amplifying the read volume by 30%, adding load to an already struggling disk subsystem. We temporarily dialed &lt;code&gt;speculative_retry&lt;/code&gt; back from &lt;code&gt;99PERCENTILE&lt;/code&gt; to &lt;code&gt;NONE&lt;/code&gt; while the cluster stabilized to prevent amplifying the read volume.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;We diagnosed the exact bottleneck by using Cassandra’s internal tracing capabilities. Running a query with &lt;code&gt;TRACING ON&lt;/code&gt; in &lt;code&gt;cqlsh&lt;/code&gt; outputs the microsecond latency of every step in the pipeline.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Tracing session: 5123abcd...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Executing single-partition query on users [1000 µs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Bloom filter allows skipping sstable 42 [1200 µs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Key cache hit for sstable 41 [1500 µs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Merging data from 12 SSTables [95000 µs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Parsing the &lt;code&gt;system_traces.events&lt;/code&gt; table this way showed the 120ms latency was almost entirely consumed by the &lt;code&gt;Merging data from 12 SSTables&lt;/code&gt; step, driven by Bloom filter misses and compaction lag.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Bloom Filter FP = 0.0001&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Setting the false positive rate extremely low consumes massive amounts of native off-heap RAM, potentially starving the OS page cache.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Stick to &lt;code&gt;0.01&lt;/code&gt; (1%) for read-heavy workloads. Monitor &lt;code&gt;nodetool tablestats&lt;/code&gt; for Bloom filter size.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Over-allocating Key Cache&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Giving the Key Cache 50% of the JVM heap will cause massive G1GC pauses.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Cap Key Cache size in &lt;code&gt;cassandra.yaml&lt;/code&gt;. Rely on the Linux OS Page Cache for the heavy lifting.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Speculative Retry Storms&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;If the entire cluster is slow, speculative retries double the traffic, effectively DDoS-ing your own database.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Monitor &lt;code&gt;SpeculativeRetries&lt;/code&gt; in &lt;code&gt;nodetool tpstats&lt;/code&gt;. Disable it during cluster-wide disk I/O incidents.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-and-trie-sstables&quot;&gt;Modern Cassandra Context: Cassandra 5.0 and Trie SSTables&lt;/h3&gt;
&lt;p&gt;Cassandra 5.0 radically optimizes this exact read path. The introduction of Trie Memtables and the new BTI (Big-Trie-Indexed) SSTable format replaces the legacy &lt;code&gt;Index.db&lt;/code&gt; and &lt;code&gt;Summary.db&lt;/code&gt; lookups with a highly efficient, concurrent prefix tree. This dramatically reduces the read-path memory overhead, shrinks the on-disk index size, and allows zero-copy evaluation, ensuring that even when Bloom filters miss, the subsequent index lookup is orders of magnitude faster.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; False-positive Bloom filter hits force Cassandra to execute expensive physical disk reads on SSTables that don’t contain the requested data, spiking p99 latency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Tune &lt;code&gt;bloom_filter_fp_chance&lt;/code&gt; to &lt;code&gt;0.01&lt;/code&gt; for read-heavy tables and carefully manage Speculative Retry thresholds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Tightening the Bloom filter instantly reduces physical disk IOPS and drops p99 read latency back into the single digits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run &lt;code&gt;nodetool tablestats&lt;/code&gt; and check the &lt;code&gt;Bloom filter false positives&lt;/code&gt; metric. If the number is climbing rapidly during read operations, you are wasting disk I/O.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>failures</category></item><item><title>Top GitHub Breakouts: March 2025 (Part 2)</title><link>https://rajivonai.com/blog/2025-04-09-github-stars-mar-2025/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-04-09-github-stars-mar-2025/</guid><description>Three March 2025 open-source projects that eliminate the iteration pauses engineers manually bridge — research review loops, vector index calibration, and agent provisioning YAML.</description><pubDate>Wed, 09 Apr 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The bottleneck in AI engineering has shifted from what you can build to how fast you can iterate. Three March 2025 breakouts targeted the pauses that stop that iteration: the overnight research loop that waits for a human reviewer in the morning, the vector index that must be calibrated before it can serve queries, and the agent workload that cannot run until someone authors its Kubernetes manifest.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;AI teams building and evaluating models share a common operational pattern: each iteration cycle contains at least one manual handoff that blocks the next step. Researchers run an experiment, stop to evaluate results by hand, and start the next run the next day. RAG engineers set up a FAISS index, discover the quantization codebook needs retraining when the corpus changes, and block query serving while the rebuild runs. Platform teams deploying AI agents write per-workload Kubernetes YAML, configure API gateways separately, and repeat the process for each new agent runtime.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Domain&lt;/th&gt;&lt;th&gt;Manual bottleneck&lt;/th&gt;&lt;th&gt;What it costs&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;System design&lt;/td&gt;&lt;td&gt;Researcher must manually score, critique, and restart experiment loops&lt;/td&gt;&lt;td&gt;Each iteration cycle requires a human present; overnight compute goes unreviewed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Databases&lt;/td&gt;&lt;td&gt;FAISS and similar indexes require data-dependent codebook training before serving queries&lt;/td&gt;&lt;td&gt;Index becomes stale when corpus grows; rebuild blocks query serving for the duration&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Databases&lt;/td&gt;&lt;td&gt;Float32 vector storage grows linearly with corpus — 10M docs consume 31 GB RAM&lt;/td&gt;&lt;td&gt;Infrastructure cost forces engineers to cap corpus size or over-provision memory&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Platform engineering&lt;/td&gt;&lt;td&gt;Per-agent Kubernetes YAML must be authored before any new agent workload can be scheduled&lt;/td&gt;&lt;td&gt;4+ hours of manifest authoring, gateway configuration, and credential wiring per new agent type&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Can purpose-built tooling available today replace these four manual steps without adding new framework dependencies?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[AI iteration overhead] --&gt; B[System Design]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; C[Databases — Vector Storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; D[Platform Engineering]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[ARIS]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; F[turbovec]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; G[ClawManager]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; H[autonomous overnight research loops]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; I[zero-calibration quantized vector index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; J[K8s-native agent provisioning control plane]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;aris--eliminating-the-manual-research-review-loop&quot;&gt;ARIS — eliminating the manual research review loop&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: ML research iteration pauses each cycle to wait for a human to score results, identify weaknesses, and restart the next run — compute sits idle overnight while the researcher sleeps.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How AI replaces or accelerates that task&lt;/strong&gt;: According to the project README, ARIS implements a five-stage autonomous loop — plan, draft, adversarial review, iterate, persist — using cross-model collaboration. Claude Code (or Codex CLI) executes the research while an external LLM acts as a critical reviewer. The README explains the design choice: “using the same model reviewing its own patterns creates blind spots.” A second model actively probes weaknesses the executor did not anticipate, breaking the self-play local minimum. The system is implemented as plain Markdown skill files — zero dependencies, no database, no Docker. The entire workflow state is stored in files the agent can read and write.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The workflow&lt;/strong&gt;:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Install Claude Code, then clone ARIS skills&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; clone&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; https://github.com/wanshuiyin/Auto-claude-code-research-in-sleep&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# In your research project directory, run the W1 workflow&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# (score paper, identify weaknesses, propose experiments)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;claude&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /review-paper&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --workflow&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; W1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Runs overnight: scores the draft, adversarial review, iterates,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# writes findings to Research Wiki — no human required until morning&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
According to the README, the W2 workflow adds experiment automation and the W3 workflow adds multi-paper synthesis. The Research Wiki is a persistent knowledge base that accumulates scored papers, ideas, and experiment results across sessions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: The README notes that decomposing ambiguous research goals produces weaker review loops — concrete research questions (“does X outperform Y on benchmark Z?”) work better than open-ended ones (“improve this paper”). The cross-model setup requires API access to at least two model providers; teams with access to only one model must use single-model mode, which the README acknowledges loses the adversarial benefit.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;turbovec--eliminating-vector-index-calibration-and-rebuild-cycles&quot;&gt;turbovec — eliminating vector index calibration and rebuild cycles&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: FAISS and product quantization indexes require data-dependent codebook training before they can serve queries; when the corpus grows, the codebook must be retrained and the index rebuilt, blocking query serving for the rebuild duration.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How AI replaces or accelerates that task&lt;/strong&gt;: According to the project README, turbovec uses Google Research’s TurboQuant algorithm — a data-oblivious quantizer that “matches the Shannon lower bound on distortion with zero training and zero data passes.” The README states: “A 10 million document corpus takes 31 GB of RAM as float32. turbovec fits it in 4 GB — and searches it faster than FAISS.” Because the quantizer is data-oblivious, vectors can be added incrementally without rebuilding. The README documents that NEON (ARM) and AVX-512BW (x86) hand-written kernels beat FAISS IndexPQFastScan by 12–20% on ARM and match or beat it on x86. Filtered search (restricting results to a candidate set from SQL, BM25, or ACL) is built into the kernel directly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The workflow&lt;/strong&gt;:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Before: FAISS PQ index requires codebook training on a data sample&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; faiss&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;quantizer &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; faiss.IndexFlatL2(dim)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;index &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; faiss.IndexIVFPQ(quantizer, dim, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;index.train(training_vectors)   &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# blocks until training completes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;index.add(vectors)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# After: turbovec — no training, incremental adds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; turbovec &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; TurboQuantIndex&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;index &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; TurboQuantIndex(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;dim&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1536&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;bit_width&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;index.add(vectors)              &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# no training step; index is ready immediately&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;index.add(more_vectors)         &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# incremental adds work without rebuilding&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;scores, indices &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; index.search(query, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;k&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;index.write(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;my_index.tq&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
For filtered hybrid retrieval, the README shows passing an id allowlist directly to &lt;code&gt;search()&lt;/code&gt; — the filter is applied inside the SIMD kernel rather than as a post-filter, so recall is maintained on selective filters without over-fetching.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: According to the project documentation, turbovec is Python and Rust only; there are no JavaScript or Go bindings in the current release. The &lt;code&gt;bit_width=4&lt;/code&gt; default trades some recall for the memory reduction — the README documents this tradeoff but does not publish a benchmark table mapping bit widths to recall across common datasets. Teams requiring guaranteed recall thresholds should benchmark against their specific corpus before replacing FAISS in production.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;clawmanager--eliminating-per-agent-kubernetes-yaml-authoring&quot;&gt;ClawManager — eliminating per-agent Kubernetes YAML authoring&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The productivity problem it solves&lt;/strong&gt;: Platform teams deploying AI agents author Kubernetes manifests per workload, configure AI API gateways separately, and repeat the process for each new agent runtime — the README describes this as the “YAML sprawl” problem for agent infrastructure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How AI replaces or accelerates that task&lt;/strong&gt;: According to the project README, ClawManager is a Kubernetes-native control plane that provides a unified interface for agent instance management, AI Gateway governance, skill discovery, and multi-runtime orchestration. The README shows provisioning a new agent instance from a web UI in under 60 seconds in the product demo GIF. The AI Gateway layer centralizes API key management and access control across all agent runtimes, eliminating per-agent gateway configuration. Skill scanning discovers and registers agent capabilities automatically.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The workflow&lt;/strong&gt;:
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Install ClawManager into an existing K8s cluster&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;helm&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; repo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; clawmanager&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; https://yuan-lab-llm.github.io/ClawManager/charts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;helm&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; install&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; clawmanager&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; clawmanager/clawmanager&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Open the web UI — provision a new agent instance from the Agent Control Plane&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Skills are scanned and registered automatically; AI Gateway injects API access&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# No per-agent YAML authoring or gateway configuration required&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
According to the README changelog (2024-05-18), team workspace support was added with one-click team creation, shared storage, task dispatch, and Redis Team Bus injection. The changelog also documents Hermes runtime integration for Webtop-based agent provisioning.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Where it breaks&lt;/strong&gt;: ClawManager is designed for teams already running Kubernetes; bare-metal or Docker Compose deployments are not documented. The README’s changelog shows rapid weekly releases (v0.1 through multiple patches in the first 60 days), indicating the platform is early and the API surface may shift. Teams adopting it today should expect schema and config changes between minor releases.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;ARIS&lt;/strong&gt;: The documented pattern for ARIS involves a five-stage loop and Research Wiki behavior, as defined in the project’s &lt;code&gt;AGENT_GUIDE.md&lt;/code&gt;. The adversarial cross-model design rationale is explicitly explained in the README. The accompanying research paper (arXiv:2405.03042) should be consulted for methodology claims, as production research quality is still emerging.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;turbovec&lt;/strong&gt;: Derived from how the system actually behaves, the TurboQuant algorithm (arXiv:2404.19874) provides a “no training” guarantee specific to its quantizer. The memory reduction claim (“31 GB to 4 GB for 10M documents at float32”) and search speed comparison (12–20% faster than FAISS IndexPQFastScan on ARM) are stated in the project README. Benchmark figures at other corpus scales or on specific embedding model outputs have not been independently verified.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ClawManager&lt;/strong&gt;: Derived from its stated behavior, the project provides an AI Gateway, agent provisioning, skill scanning, and team workspaces. The 60-second provisioning claim is illustrated by a demo GIF in the README. No independent production-scale deployment report is available; the project is pre-1.0.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;ARIS review loop produces shallow critique&lt;/td&gt;&lt;td&gt;Open-ended research goal without concrete evaluation criteria&lt;/td&gt;&lt;td&gt;Define specific benchmark tasks and success thresholds before invoking the review loop&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ARIS second model not accessible&lt;/td&gt;&lt;td&gt;Single-provider API access or rate limit hit during overnight run&lt;/td&gt;&lt;td&gt;Configure a fallback single-model mode (documented in README); schedule runs when rate limits are low&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;turbovec recall drops on selective filters&lt;/td&gt;&lt;td&gt;Bit width too low for the embedding model’s effective dimensionality&lt;/td&gt;&lt;td&gt;Benchmark bit_width=4 vs bit_width=8 on your corpus before production; increase bit width if recall is below threshold&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;turbovec no Go or JavaScript bindings&lt;/td&gt;&lt;td&gt;Services written outside Python or Rust need vector search&lt;/td&gt;&lt;td&gt;Wrap turbovec search behind a thin Python REST service; use FAISS for non-Python runtimes in the interim&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ClawManager API surface changes between releases&lt;/td&gt;&lt;td&gt;Adopting ClawManager while it is pre-1.0&lt;/td&gt;&lt;td&gt;Pin to a specific release in Helm; track the changelog for breaking changes before upgrading&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ClawManager requires Kubernetes&lt;/td&gt;&lt;td&gt;Team running Docker Compose or bare-metal&lt;/td&gt;&lt;td&gt;Deploy a lightweight K3s cluster for agent infrastructure even if the rest of the stack uses Docker Compose&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: AI iteration speed is blocked at three manual handoffs — research review loops that pause overnight, vector indexes that cannot grow without a rebuild, and agent workloads that cannot be provisioned without per-workload YAML authoring.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Use ARIS to run cross-model research review overnight without human intervention, turbovec to replace FAISS with a zero-calibration index that grows incrementally, and ClawManager to provision and govern agent instances from a single Kubernetes-native control plane.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: After &lt;code&gt;pip install turbovec&lt;/code&gt;, replace one FAISS index with a TurboQuantIndex, add the same vectors, and run the same benchmark query — if the index built without a training call and returned results within the expected latency range, the integration is validated.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Run &lt;code&gt;pip install turbovec&lt;/code&gt; and convert one existing FAISS index this week; the before/after code is four lines and requires no corpus changes.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>ai-engineering</category><category>architecture</category><category>databases</category></item><item><title>Cassandra Data Modeling Reviews: How We Stopped Application Teams Creating Hot Partitions</title><link>https://rajivonai.com/blog/2025-04-02-cassandra-data-modeling-hot-partitions/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-04-02-cassandra-data-modeling-hot-partitions/</guid><description>Why treating Apache Cassandra like a relational database creates massive, node-crashing hot partitions, and how to use data bucketing to fix it.</description><pubDate>Wed, 02 Apr 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The most dangerous thing an engineering team can do with Apache Cassandra is design the schema before they know the queries.&lt;/p&gt;
&lt;p&gt;In a relational database, you model the entities (e.g., Users, Orders, Items), normalize the tables, and rely on &lt;code&gt;JOIN&lt;/code&gt;s and secondary indexes to make the queries fast. If you bring that mindset to Cassandra, you will create a distributed systems disaster known as a &lt;strong&gt;Hot Partition&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In 2022, the 27-node cluster began experiencing severe, localized degradation. One specific node (Node A) would hit 85% disk utilization and spike to 100% single-thread CPU usage, while Node B in the same rack sat idle at 30% disk and 5% CPU.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;system.log&lt;/code&gt; was spamming &lt;code&gt;WARN: Partition size &gt; 100MB&lt;/code&gt;. The background &lt;code&gt;CompactionExecutor&lt;/code&gt; thread was hanging for hours trying to merge massive SSTable chunks, causing write backpressure that eventually took the node offline.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra distributes data across nodes using a consistent hashing ring. When you create a table, you define a &lt;strong&gt;Primary Key&lt;/strong&gt;. The first part of that key is the &lt;strong&gt;Partition Key&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Cassandra takes the Partition Key, hashes it (using &lt;code&gt;Murmur3Partitioner&lt;/code&gt;), and uses that hash to determine exactly which nodes in the cluster own the data. &lt;em&gt;All data for a single partition key is stored together on the same physical nodes.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;An application team had created a &lt;code&gt;status_logs&lt;/code&gt; table:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- The Anti-Pattern&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; status_logs&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    event_timestamp &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    event_data &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, event_timestamp)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;They had one massive enterprise tenant that generated 95% of the system’s traffic. Because &lt;code&gt;tenant_id&lt;/code&gt; was the sole partition key, 95% of the writes and reads were hashed to the exact same partition. Cassandra dutifully sent all that traffic to the three nodes responsible for that hash.&lt;/p&gt;
&lt;p&gt;The partition grew to 5 GB. Cassandra is designed to handle billions of tiny partitions, not a few monolithic ones. A 5 GB partition breaks JVM memory limits during read scans and crushes the compaction engine. The long-standing operational guidance for a healthy cluster is a maximum of 100 MB per partition and no more than 100,000 cells per partition — these are soft, widely-cited thresholds rather than database-enforced hard limits (Guardrails, discussed below, can turn them into enforced limits).&lt;/p&gt;
&lt;h2 id=&quot;data-bucketing-architecture&quot;&gt;Data Bucketing Architecture&lt;/h2&gt;
&lt;p&gt;To fix a hot partition, you must fundamentally change the Primary Key to force the &lt;code&gt;Murmur3Partitioner&lt;/code&gt; to distribute the data across the ring.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[Client Writes Data] --&gt; Hash{Murmur3 Hash of — Partition Key}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;The Anti-Pattern (tenant_id)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Hash --&gt;|Hash: 104| Node1[Node 1: 5 GB Partition — CPU 100%]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Hash -.-&gt;|Empty| Node2[Node 2: Idle]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Hash -.-&gt;|Empty| Node3[Node 3: Idle]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;The Bucketed Pattern ((tenant_id, month), event_id)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Hash --&gt;|Hash: A| N1[Node 1: 50 MB]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Hash --&gt;|Hash: B| N2[Node 2: 50 MB]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Hash --&gt;|Hash: C| N3[Node 3: 50 MB]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;synthetic-bucketing-salting&quot;&gt;Synthetic Bucketing (Salting)&lt;/h3&gt;
&lt;p&gt;We enforced a new data modeling rule: any unbounded time-series or high-cardinality collection must use a compound partition key with a time or hash bucket.&lt;/p&gt;
&lt;p&gt;We redesigned the table to split the massive tenant’s data into monthly buckets:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- The Bucketed Pattern&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; status_logs_v2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    month&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; text&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    event_timestamp &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    event_data &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ((tenant_id, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;month&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), event_timestamp)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By wrapping &lt;code&gt;(tenant_id, month)&lt;/code&gt; in parentheses, we defined the compound partition key. Now, the tenant’s January data hashes to Node 2, February hashes to Node 15, and March hashes to Node 22. The 5 GB monolithic partition was shattered into 50 MB chunks, evenly distributed across the cluster.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Schema migrations in Cassandra are not as simple as an &lt;code&gt;ALTER TABLE&lt;/code&gt; statement, because changing a partition key changes where the data physically lives on disk.&lt;/p&gt;
&lt;p&gt;Operators must create &lt;code&gt;status_logs_v2&lt;/code&gt;, update the application to &lt;strong&gt;dual-write&lt;/strong&gt; to both tables, run an asynchronous backfill script to copy the historical data from V1 to V2, and finally flip the application reads to V2 before dropping V1.&lt;/p&gt;
&lt;p&gt;To prevent this from happening again, the runbook establishes a mandatory CI/CD schema linter. The pipeline parsed &lt;code&gt;.cql&lt;/code&gt; migration files and automatically rejected PRs that used unbounded collections (&lt;code&gt;SET&lt;/code&gt;, &lt;code&gt;LIST&lt;/code&gt;, &lt;code&gt;MAP&lt;/code&gt;), relied on &lt;code&gt;ALLOW FILTERING&lt;/code&gt;, or created time-series tables without explicit time-bucketing in the partition key.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Bucketing Too Finely&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bucketing by the &lt;code&gt;minute&lt;/code&gt; instead of &lt;code&gt;month&lt;/code&gt; fragments the data into millions of partitions. To read a day’s worth of data, the client must query 1,440 different nodes, causing read latency to skyrocket.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Size buckets so they hold ~50 MB of data. Model against your read queries.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;ALLOW FILTERING&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Using &lt;code&gt;ALLOW FILTERING&lt;/code&gt; forces Cassandra to scan entire nodes, load data into memory, and filter it. It will crash your cluster under load.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Reject any schema or query using &lt;code&gt;ALLOW FILTERING&lt;/code&gt;. Build a materialized table tailored to the query.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Unbounded Collections&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;A Cassandra &lt;code&gt;SET&lt;/code&gt; or &lt;code&gt;LIST&lt;/code&gt; is stored inside a single row. If it grows indefinitely, you recreate the hot partition problem.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Never use collections for data that grows over time. Use a clustered table.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-guardrails-and-sai&quot;&gt;Modern Cassandra Context: Cassandra 5.0 Guardrails and SAI&lt;/h3&gt;
&lt;p&gt;Cassandra 4.1 drastically improved an operator’s ability to defend against bad schemas by introducing native Guardrails (&lt;code&gt;partition_size_warn_threshold_in_mb&lt;/code&gt;, &lt;code&gt;partition_size_failure_threshold_in_mb&lt;/code&gt;) that can reject writes at the coordinator level &lt;em&gt;before&lt;/em&gt; they corrupt the storage engine. Furthermore, the introduction of Storage-Attached Indexing (SAI) safely enables multi-column queries without creating the secondary index fanout disasters of the 4.x era, slightly easing the strict denormalization requirements of legacy Cassandra modeling.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Designing Cassandra schemas without understanding the &lt;code&gt;Murmur3Partitioner&lt;/code&gt; creates massive, single-node hot partitions that cause cluster instability.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Enforce query-first data modeling. Use synthetic bucketing to break unbounded partitions into chunks smaller than 100 MB.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Migrating from a single-key partition to a time-bucketed partition evenly distributes disk I/O and CPU, eliminating hot-node crashes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run &lt;code&gt;nodetool tablestats&lt;/code&gt; and check the &lt;code&gt;Max partition size&lt;/code&gt; metric. If you see partitions exceeding 100 MB, you need to redesign your Primary Key.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>checklist</category></item><item><title>The Cassandra Tombstone Incident: Why Deletes Can Destroy Read Latency</title><link>https://rajivonai.com/blog/2025-03-25-cassandra-tombstones-read-latency/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-03-25-cassandra-tombstones-read-latency/</guid><description>How Apache Cassandra handles deletes, the mechanics of tombstones, and why deleting data can counter-intuitively crash your database read path.</description><pubDate>Tue, 25 Mar 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In a relational database, running &lt;code&gt;DELETE&lt;/code&gt; removes a row. The disk space is eventually reclaimed, and subsequent &lt;code&gt;SELECT&lt;/code&gt; statements are completely unaware the row ever existed.&lt;/p&gt;
&lt;p&gt;In a distributed, append-only database like Apache Cassandra, running &lt;code&gt;DELETE&lt;/code&gt; is actually an &lt;code&gt;INSERT&lt;/code&gt;. You insert a marker called a &lt;strong&gt;tombstone&lt;/strong&gt;. If you do not understand the physics of tombstones, deleting data will eventually destroy your read path.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In 2023, an application team deployed a new feature that introduced soft-deletes and TTL (Time-To-Live) updates on a heavy user notification table in the 36-node cluster. The table handled ~30k read QPS and ~50k write/delete QPS.&lt;/p&gt;
&lt;p&gt;Within four hours of the deployment, p99 read latency on that table skyrocketed from 4ms to 2,800ms. Clients started timing out. The Cassandra &lt;code&gt;system.log&lt;/code&gt; was flooded with &lt;code&gt;WARN: Read 5000 tombstones in &amp;#x3C;keyspace&gt;.&amp;#x3C;table&gt;&lt;/code&gt;, followed quickly by catastrophic &lt;code&gt;TombstoneOverwhelmingException&lt;/code&gt; errors, causing coordinator nodes to forcefully drop queries and sever client connections.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra uses a Log-Structured Merge-Tree (LSM) storage engine. SSTables on disk are immutable. You cannot go into a locked 10 GB file and erase a row.&lt;/p&gt;
&lt;p&gt;Instead, when you execute a &lt;code&gt;DELETE&lt;/code&gt; or a TTL expires, Cassandra writes a tombstone—a marker asserting that the data is dead. When a read query requests data for a partition, Cassandra must merge the live data from various SSTables with the tombstones. If the read query has to scan past 5,000 tombstones to find 10 live rows, the database burns massive amounts of CPU and JVM memory holding those tombstones during the merge process.&lt;/p&gt;
&lt;p&gt;To protect the JVM from running out of memory (OOM), Cassandra implements hard guardrails: &lt;code&gt;tombstone_warn_threshold&lt;/code&gt; (warns at 1,000 scanned tombstones) and &lt;code&gt;tombstone_failure_threshold&lt;/code&gt; (aborts the query at 100,000).&lt;/p&gt;
&lt;p&gt;The application team had introduced an anti-pattern. They were inserting rows with column-level &lt;code&gt;null&lt;/code&gt; values (which Cassandra interprets as point tombstones) and performing massive range scans across partitions littered with deleted records.&lt;/p&gt;
&lt;h2 id=&quot;the-tombstone-lifecycle-and-resurrection&quot;&gt;The Tombstone Lifecycle and Resurrection&lt;/h2&gt;
&lt;p&gt;You might ask: “Why doesn’t Cassandra just delete the tombstones during compaction?”&lt;/p&gt;
&lt;p&gt;Because Cassandra is a distributed system, nodes can be offline. If Node A misses a &lt;code&gt;DELETE&lt;/code&gt;, Node B and C record the tombstone. If Node B and C purge the tombstone immediately, and Node A comes back online, Node A will stream the old, live data back to Node B and C during repair. The deleted data comes back from the dead. We call this &lt;strong&gt;Zombie Resurrection&lt;/strong&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[Client Executes DELETE] --&gt; Insert[Cassandra Inserts Tombstone]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Insert --&gt; Wait[Wait for gc_grace_seconds — Default: 10 Days]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;The 10-Day Anti-Entropy Window&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Wait --&gt; Repair[Run nodetool repair — Synchronize Tombstone across Replicas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Repair --&gt; Compaction[Background Compaction]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Compaction --&gt; Eval{Is Tombstone Older — than gc_grace_seconds?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Eval --&gt;|No| Keep[Keep Tombstone in new SSTable]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Eval --&gt;|Yes| Purge[Safely Purge Tombstone — Reclaim Disk Space]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    %% Zombie Path&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Wait -.-&gt;|Repair Skipped — Missed| EarlyPurge[Premature Purge]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    EarlyPurge -.-&gt; OfflineNode[Offline Node returns]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OfflineNode -.-&gt; Zombie[Zombie Data Resurrected]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To prevent this, Cassandra forces tombstones to live on disk for &lt;code&gt;gc_grace_seconds&lt;/code&gt; (default 10 days) to give you time to run a full cluster repair (&lt;code&gt;nodetool repair&lt;/code&gt;). Purging isn’t automatic once that window passes, though: a tombstone is only eligible for removal once gc_grace has elapsed &lt;em&gt;and&lt;/em&gt; every SSTable fragment covering that partition — across all overlapping SSTables, not just one — gets compacted together in the same compaction. A tombstone sitting in an SSTable that a compaction doesn’t happen to touch stays on disk indefinitely past its grace period, which is exactly why STCS tables with wide, unevenly-sized SSTables can accumulate purge-eligible-but-unpurged tombstones for a long time.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The playbook executes a two-phase remediation plan.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Short-Term Emergency:&lt;/strong&gt; By the time the incident was fully diagnosed and the fix was ready to ship — several days after the initial deployment, not the same afternoon the alerts fired — the oldest tombstones from the offending feature were already past the point a shorter grace period would purge them. The read path was still choking on tombstones that couldn’t be purged because they were newer than 10 days. For this specific table (which we repaired daily), we safely lowered &lt;code&gt;gc_grace_seconds&lt;/code&gt; to 3 days (&lt;code&gt;259200&lt;/code&gt; seconds). We then triggered user-defined compactions (&lt;code&gt;nodetool compact -s&lt;/code&gt;) to force the merging of SSTables and the purging of expired tombstones, dropping p99 latency back to 10ms.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Long-Term Fix:&lt;/strong&gt; We redesigned the CQL data model with the application team. We banned inserting &lt;code&gt;null&lt;/code&gt; into columns. We shifted from point-deletes to whole-row deletes, and redesigned queries to avoid scanning across wide ranges of recently deleted data.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Dropping &lt;code&gt;gc_grace_seconds&lt;/code&gt; to 0&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Setting it to 0 allows immediate purging of tombstones during compaction. If a node is down for 1 minute during a delete, that data will resurrect as a zombie.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Only lower &lt;code&gt;gc_grace_seconds&lt;/code&gt; if you have absolute confidence in your automated anti-entropy repair frequency.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;High TTL Workloads on STCS&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Size-Tiered Compaction Strategy rarely compacts the largest, oldest SSTables, meaning tombstones get trapped forever, inflating disk usage.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use Time-Window Compaction Strategy (TWCS) for TTL data, allowing Cassandra to drop entire SSTables without scanning tombstones.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Column-Level Deletes&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Deleting 50 columns in a row creates 50 tombstones.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Delete the entire row, which creates exactly 1 range tombstone.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-guardrails&quot;&gt;Modern Cassandra Context: Cassandra 5.0 Guardrails&lt;/h3&gt;
&lt;p&gt;Cassandra 4.1 introduced dynamic Guardrails that can be configured per-keyspace. Rather than waiting for a &lt;code&gt;TombstoneOverwhelmingException&lt;/code&gt; to crash a replica, operators can now enforce strict &lt;code&gt;tombstone_warn_threshold&lt;/code&gt; and &lt;code&gt;tombstone_failure_threshold&lt;/code&gt; limits directly at the coordinator level, rejecting badly designed queries before they saturate the storage engine’s read path. Additionally, Cassandra 5.0’s Unified Compaction Strategy (UCS) reduces the number of overlapping SSTables a given partition’s fragments tend to accumulate across, which in practice means fewer partitions get stuck waiting on a wide compaction to become tombstone-purge eligible.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Deleting data in Cassandra inserts tombstones. Reading past thousands of tombstones causes JVM memory exhaustion and massive read latencies.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Avoid column-level deletes, stop inserting &lt;code&gt;null&lt;/code&gt; values, and ensure your data model does not rely on range-scanning across deleted records.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Lowering &lt;code&gt;gc_grace_seconds&lt;/code&gt; (with adequate repair) and forcing compaction purges tombstones and instantly restores read performance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run &lt;code&gt;nodetool tablestats&lt;/code&gt; and check the &lt;code&gt;Tombstones per read&lt;/code&gt; metric. If the max value is routinely over 1,000, you have a data modeling emergency.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>failures</category></item><item><title>Beyond Multi-Primary: Scaling MySQL Writes with Tenant-Sharded InnoDB Clusters</title><link>https://rajivonai.com/blog/2025-03-18-sharded-innodb-clusters-mysql-write-scaling/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-03-18-sharded-innodb-clusters-mysql-write-scaling/</guid><description>A production architecture for scaling MySQL writes across tenant-owned shards while preserving high availability inside each shard.</description><pubDate>Tue, 18 Mar 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The useful unit of MySQL write scale is not another writable copy of the same database. It is another database that owns different writes.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;RetailCo has outgrown the architecture from the earlier &lt;a href=&quot;https://rajivonai.com/blog/2024-11-19-mysql-write-scaling-async-semisync-innodb-cluster/&quot;&gt;MySQL replication article&lt;/a&gt;. A three-member InnoDB Cluster gives its order platform quorum, primary election, member recovery, and a stable MySQL Router endpoint. The primary, however, still executes every checkout transaction for every tenant.&lt;/p&gt;
&lt;p&gt;The workload has one property that changes the design: almost every transaction belongs to one tenant. A checkout reads and writes one tenant’s customer, order, order items, inventory reservation, payment state, shipment, and idempotency records. Cross-tenant transactions are exceptional.&lt;/p&gt;
&lt;p&gt;That gives RetailCo a natural ownership boundary. Instead of asking three members to agree on every tenant’s writes, it can place disjoint tenant sets on independent MySQL clusters:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Apps[RetailCo applications] --&gt; Resolve[resolve authenticated tenant]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Resolve --&gt; Catalog[tenant placement catalog and cache]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Catalog --&gt; RouterA[Router A]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Catalog --&gt; RouterB[Router B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Catalog --&gt; RouterC[Router C]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  RouterA --&gt; ShardA[InnoDB Cluster A — tenants 101 to 399]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  RouterB --&gt; ShardB[InnoDB Cluster B — tenants 400 to 799]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  RouterC --&gt; ShardC[InnoDB Cluster C — tenants 800 to 1199]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each InnoDB Cluster remains a single-primary high-availability unit. MySQL Shell’s AdminAPI manages the cluster, Group Replication maintains membership, and MySQL Router directs read-write connections to the current primary. MySQL documents that stack in &lt;a href=&quot;https://dev.mysql.com/doc/mysql-shell/8.4/en/mysql-innodb-cluster.html&quot;&gt;InnoDB Cluster&lt;/a&gt; and &lt;a href=&quot;https://dev.mysql.com/doc/mysql-router/8.4/en/mysql-router-deploying-bootstrapping.html&quot;&gt;Router bootstrap&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The write scale comes from the layer above MySQL: Cluster A never applies Cluster B’s tenant transactions. Adding a shard adds another write execution, buffer-pool, storage, binary-log, and failure domain.&lt;/p&gt;
&lt;p&gt;RetailCo is a reference architecture, not a claimed customer deployment. The placement catalog, route epoch, and move workflow below are proposed designs. This article presents no measured throughput, latency, failover-time, migration-duration, or data-loss result.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;“Shard by &lt;code&gt;tenant_id&lt;/code&gt;” is a data-distribution suggestion, not an operating model. A production design must answer questions that one MySQL instance previously answered implicitly:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Who is authoritative for tenant 487 right now?&lt;/li&gt;
&lt;li&gt;What prevents an old application process from writing to the previous shard after a move?&lt;/li&gt;
&lt;li&gt;How is a 2-TB tenant moved without making two writable copies authoritative?&lt;/li&gt;
&lt;li&gt;Where do global uniqueness and foreign keys live?&lt;/li&gt;
&lt;li&gt;How are 40 clusters upgraded, backed up, restored, and observed?&lt;/li&gt;
&lt;li&gt;What happens when the tenant catalog is unavailable?&lt;/li&gt;
&lt;li&gt;How is a business transaction completed when it crosses shards?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The wrong shortcut is to confuse any of these with sharding:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Mechanism&lt;/th&gt;&lt;th&gt;What it provides&lt;/th&gt;&lt;th&gt;Why it is not horizontal write ownership&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;More asynchronous replicas&lt;/td&gt;&lt;td&gt;Read copies and promotion candidates&lt;/td&gt;&lt;td&gt;One source still owns writes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;InnoDB Cluster single-primary&lt;/td&gt;&lt;td&gt;Quorum and automated primary replacement&lt;/td&gt;&lt;td&gt;One primary executes the shard’s writes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Group Replication multi-primary&lt;/td&gt;&lt;td&gt;Several write-entry points into one replicated dataset&lt;/td&gt;&lt;td&gt;Successful transactions still propagate through the group&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;InnoDB ClusterSet&lt;/td&gt;&lt;td&gt;Disaster-recovery clusters&lt;/td&gt;&lt;td&gt;Only the primary cluster is read-write&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Native table partitioning&lt;/td&gt;&lt;td&gt;Local data pruning and lifecycle control&lt;/td&gt;&lt;td&gt;Partitions still consume one MySQL server’s compute&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application sharding&lt;/td&gt;&lt;td&gt;Separate databases own separate tenant sets&lt;/td&gt;&lt;td&gt;The application must now operate placement and movement&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;InnoDB ClusterSet deserves special emphasis. MySQL documents one read-write primary cluster and read-only replica clusters, with asynchronous inter-cluster replication and no semisynchronous support. It is a disaster-recovery topology, not a tenant-sharding layer: &lt;a href=&quot;https://dev.mysql.com/doc/mysql-shell/8.4/en/innodb-clusterset-limitations.html&quot;&gt;ClusterSet limitations&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The core question is therefore not whether MySQL can run more clusters. It is whether RetailCo can make tenant ownership explicit and keep it correct through routing, failure, migration, recovery, and fleet change.&lt;/p&gt;
&lt;h2 id=&quot;build-independent-write-domains&quot;&gt;Build Independent Write Domains&lt;/h2&gt;
&lt;h3 id=&quot;separate-the-placement-control-plane-from-the-query-data-plane&quot;&gt;Separate the placement control plane from the query data plane&lt;/h3&gt;
&lt;p&gt;The tenant-placement catalog maps a stable tenant identity to a logical shard. It should be small, highly available, auditable, and boring. It should not sit in the critical path of every SQL statement.&lt;/p&gt;
&lt;p&gt;Applications authenticate the tenant before choosing a database, look up the placement through a bounded cache, and open a connection to that shard’s logical Router endpoint. MySQL Router then solves a narrower problem: it discovers which member is the current primary inside that InnoDB Cluster. Router does not know that tenant 487 belongs on Shard B.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Request[request with authenticated tenant] --&gt; Cache{placement cached}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Cache --&gt;|yes| Placement[tenant, shard, epoch]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Cache --&gt;|no| Catalog[read placement catalog]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Catalog --&gt; Placement&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Placement --&gt; Endpoint[resolve logical shard endpoint]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Endpoint --&gt; Router[shard-local MySQL Router]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Router --&gt; Primary[current shard primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Primary --&gt; Fence{epoch accepted}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Fence --&gt;|yes| Transaction[execute tenant-local transaction]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Fence --&gt;|no| Refresh[reject and refresh placement]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the catalog is unavailable, warm application processes can continue serving known stable placements for a deliberately bounded period. New tenant creation, tenant moves, and cold-cache routing should pause. This converts a catalog incident from an immediate global data-plane outage into a controlled degradation, while the cache limit prevents stale placement from living forever.&lt;/p&gt;
&lt;p&gt;A minimal catalog might look like this:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; tenant_placement&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BINARY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    shard_id        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;64&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    placement_epoch &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BIGINT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;           ENUM(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                        &apos;STABLE&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                        &apos;COPYING&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                        &apos;CATCHING_UP&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                        &apos;FENCED&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                        &apos;CUTOVER&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                        &apos;OBSERVING&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                    ) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    updated_at      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TIMESTAMP&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; shard_endpoint&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    shard_id               &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;64&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    logical_write_endpoint &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;255&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    region                 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    capacity_class         &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                 ENUM(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;ACTIVE&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;DRAINING&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;OFFLINE&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (shard_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The catalog stores logical endpoints, not database passwords. Credentials belong in a secret-management system. Every placement mutation also needs an append-only audit event recording old shard, new shard, old epoch, new epoch, actor, reason, and timestamp. That history becomes essential during restore.&lt;/p&gt;
&lt;h3 id=&quot;put-the-tenant-key-in-every-transactional-identity&quot;&gt;Put the tenant key in every transactional identity&lt;/h3&gt;
&lt;p&gt;The schema must make accidental cross-tenant access difficult. Tenant-owned tables should carry &lt;code&gt;tenant_id&lt;/code&gt;, and their primary and foreign keys should retain it:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BINARY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BINARY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    customer_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BINARY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    VARCHAR&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    total     &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;18&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TIMESTAMP&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, order_id),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders_customer (tenant_id, customer_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; order_items&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BINARY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BINARY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    line_id   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BINARY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    product_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BINARY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    quantity  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, order_id, line_id),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    CONSTRAINT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; order_items_order_fk&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      FOREIGN KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, order_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      REFERENCES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders (tenant_id, order_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Application-generated identifiers avoid coordinating one global auto-increment sequence. A composite tenant key also makes a missing tenant predicate more visible in query review, but it is not authorization. The service must bind the authenticated tenant, prevent callers from substituting it, and test every repository method for tenant isolation.&lt;/p&gt;
&lt;p&gt;Global data needs an explicit category:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;small, slowly changing reference data can be copied to every shard;&lt;/li&gt;
&lt;li&gt;globally unique usernames or idempotency keys need a separate registry or a tenant-scoped definition;&lt;/li&gt;
&lt;li&gt;product search and cross-tenant analytics should use a derived search, warehouse, or lake system;&lt;/li&gt;
&lt;li&gt;shared mutable rows such as one global inventory counter defeat tenant independence and may need a different domain boundary.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;persist-placement-do-not-recompute-ownership-forever&quot;&gt;Persist placement; do not recompute ownership forever&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;hash(tenant_id) % shard_count&lt;/code&gt; is operationally hostile because changing the shard count remaps most tenants. Consistent or rendezvous hashing reduces movement and can produce an initial candidate shard, but &lt;a href=&quot;https://rajivonai.com/blog/2022-03-27-consistent-hashing-what-it-solves-and-what-it-does-not/&quot;&gt;consistent hashing is not the whole architecture&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;RetailCo needs deliberate exceptions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a hot tenant may require a dedicated cluster;&lt;/li&gt;
&lt;li&gt;regulated data may be pinned to a region;&lt;/li&gt;
&lt;li&gt;a tenant may need a higher storage class;&lt;/li&gt;
&lt;li&gt;a draining shard must stop accepting new placements;&lt;/li&gt;
&lt;li&gt;a large move may be scheduled instead of triggered automatically.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The catalog is therefore authoritative. Hashing proposes; the placement record decides.&lt;/p&gt;
&lt;h3 id=&quot;fence-stale-routes-with-a-placement-epoch&quot;&gt;Fence stale routes with a placement epoch&lt;/h3&gt;
&lt;p&gt;A cache invalidation message is not a correctness boundary. A process can miss it, pause, and resume with an old route. RetailCo should increment &lt;code&gt;placement_epoch&lt;/code&gt; whenever ownership moves and maintain a small &lt;code&gt;tenant_home&lt;/code&gt; record on the authoritative shard.&lt;/p&gt;
&lt;p&gt;At the start of a write transaction, the data-access layer compares its expected tenant and epoch with the local authoritative record. A request carrying epoch 41 cannot write after the target becomes authoritative at epoch 42. This check should be part of the transaction contract, not a best-effort preflight call.&lt;/p&gt;
&lt;p&gt;The epoch does not solve every split-brain path. Administrative accounts, background jobs, repair scripts, and change-data-capture consumers must all use the same ownership rule or be explicitly fenced. The useful invariant is:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;At any placement epoch, exactly one shard is permitted to accept new business writes for a tenant.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;move-tenants-as-a-controlled-state-machine&quot;&gt;Move tenants as a controlled state machine&lt;/h3&gt;
&lt;p&gt;Copying rows is the easy part. Cutover is where ownership can split. A safe move separates bulk transfer, change catch-up, validation, fencing, route mutation, and cleanup:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Stable[STABLE — source owns tenant] --&gt; Copy[COPYING — chunked snapshot copy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Copy --&gt; Catchup[CATCHING UP — apply captured changes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Catchup --&gt; Validate[validate counts, checksums, and invariants]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Validate --&gt; Fence[FENCED — pause new tenant writes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Fence --&gt; Tail[apply final change tail]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Tail --&gt; Install[install target tenant home with next epoch]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Install --&gt; Cutover[CUTOVER — update catalog atomically]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Cutover --&gt; Observe[observe target and reject stale source routes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Observe --&gt; NewStable[STABLE — target owns tenant]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  NewStable --&gt; Retire[retire source copy after recovery window]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The source remains authoritative during copy and catch-up. Binary-log change capture can carry the tail, but the implementation must preserve per-tenant ordering, idempotency, delete events, schema compatibility, and restart position. Before cutover, RetailCo validates more than row counts: financial totals, order-to-item relationships, inventory invariants, and a sample of application reads.&lt;/p&gt;
&lt;p&gt;The write fence should be short, observable, and tested. Drain tenant-specific jobs, reject or queue new mutations, apply the final tail, install epoch 42 at the target, atomically change the catalog from source epoch 41 to target epoch 42, and refresh caches. The source must then reject epoch 41 traffic. Keep its data read-only through an observation and recovery window before deletion.&lt;/p&gt;
&lt;p&gt;Do not dual-write the tenant to both shards as a shortcut. Partial success produces two histories, and “last write wins” is not a reconciliation rule for payments or inventory. If the move fails before cutover, resume from the source. If it fails after the catalog commits, recover toward the target unless an explicit, tested reverse-cutover procedure proves otherwise.&lt;/p&gt;
&lt;p&gt;No zero-downtime or migration-duration claim is made here. This workflow is a proposed lab until its capture, fence, restart, rollback, and failure behavior are measured.&lt;/p&gt;
&lt;h3 id=&quot;keep-routine-transactions-inside-one-shard&quot;&gt;Keep routine transactions inside one shard&lt;/h3&gt;
&lt;p&gt;A local checkout can still use ordinary InnoDB ACID transactions because all participating tenant rows live together. Cross-shard foreign keys do not exist, and cross-shard joins become scatter-gather work or derived-data queries.&lt;/p&gt;
&lt;p&gt;MySQL supports external XA transactions with prepare, commit, rollback, and recovery states. That capability does not make distributed checkout the default design. XA adds a coordinator, prepared-state recovery, more failure combinations, and a wider latency path: &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/xa.html&quot;&gt;XA transactions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Prefer boundaries that avoid routine cross-shard ACID:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;keep a tenant’s checkout data colocated;&lt;/li&gt;
&lt;li&gt;write an outbox event in the same local transaction as the business change;&lt;/li&gt;
&lt;li&gt;use idempotent consumers and a saga for multi-domain workflows;&lt;/li&gt;
&lt;li&gt;expose explicit pending, compensated, and failed business states;&lt;/li&gt;
&lt;li&gt;send cross-tenant reporting to derived systems.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If the most important transaction constantly updates tenants on different shards, &lt;code&gt;tenant_id&lt;/code&gt; is probably the wrong ownership key—or the business domain needs decomposition before sharding.&lt;/p&gt;
&lt;h3 id=&quot;operate-schema-and-recovery-as-fleet-workflows&quot;&gt;Operate schema and recovery as fleet workflows&lt;/h3&gt;
&lt;p&gt;One schema migration is now many independently failing migrations. Use expand-contract changes, canary one low-risk shard, record a migration ledger per shard, throttle work, pause on replication or storage pressure, and make every step resumable. Application code must tolerate mixed schema versions during rollout. A single global DDL window is not a distributed transaction.&lt;/p&gt;
&lt;p&gt;Backup also changes shape. Each shard needs backups and point-in-time recovery appropriate to its RPO and RTO. The placement catalog needs its own backups. Placement audit events must survive long enough to answer: “Which shard owned tenant 487 at 10:37:22?”&lt;/p&gt;
&lt;p&gt;Restoring every shard to the same wall-clock timestamp is necessary for a fleet-wide incident but may not be sufficient. A tenant moved between shards can exist in both backups with different authority epochs. Recovery automation must replay placement history, identify the authoritative copy at the recovery point, and prevent both copies from becoming writable.&lt;/p&gt;
&lt;p&gt;Group Replication member recovery is not backup. It repairs or catches up a group member from the live cluster; it does not recover an operator-dropped tenant or provide an independent historical restore.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;h3 id=&quot;the-documented-pattern-is-isolation-plus-explicit-placement&quot;&gt;The documented pattern is isolation plus explicit placement&lt;/h3&gt;
&lt;p&gt;Shopify has publicly described moving from one large failure domain to pods—isolated groups containing a subset of shops—and later described &lt;a href=&quot;https://shopify.engineering/mysql-database-shard-balancing-terabyte-scale&quot;&gt;balancing MySQL shards at terabyte scale&lt;/a&gt;. Its published architecture is not proof that RetailCo should copy the implementation, but it demonstrates the operating consequence of tenant sharding: placement, isolation, and movement become first-class platform responsibilities. The shorter &lt;a href=&quot;https://rajivonai.com/blog/2024-04-15-shopify-style-multi-tenant-commerce-databases-isolation-sharding-and-operational-controls/&quot;&gt;multi-tenant database field note&lt;/a&gt; connects those public decisions to this repository’s broader isolation model.&lt;/p&gt;
&lt;p&gt;For RetailCo, the recommended architecture is:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use one single-primary InnoDB Cluster—or one cloud-managed HA MySQL database—as each shard’s availability unit.&lt;/li&gt;
&lt;li&gt;Route by authenticated &lt;code&gt;tenant_id&lt;/code&gt; through an explicit placement catalog.&lt;/li&gt;
&lt;li&gt;Keep the full tenant transaction graph on one shard.&lt;/li&gt;
&lt;li&gt;Fence moves with a monotonically increasing epoch and one authoritative writer.&lt;/li&gt;
&lt;li&gt;Treat schema rollout, backup, restore, hot-tenant isolation, and capacity balancing as fleet capabilities.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The deployment substrate changes who operates HA, not who owns sharding:&lt;/p&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Deployment&lt;/th&gt;&lt;th&gt;HA unit inside one application shard&lt;/th&gt;&lt;th&gt;RetailCo still owns&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;On-premises or virtual machines&lt;/td&gt;&lt;td&gt;InnoDB Cluster and redundant MySQL Routers&lt;/td&gt;&lt;td&gt;Placement, movement, fleet operations, backup, DR&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AWS&lt;/td&gt;&lt;td&gt;One RDS for MySQL Multi-AZ DB cluster per shard&lt;/td&gt;&lt;td&gt;Placement, movement, fleet operations, recovery contract&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Google Cloud&lt;/td&gt;&lt;td&gt;One Cloud SQL for MySQL regional HA instance per shard&lt;/td&gt;&lt;td&gt;Placement, movement, fleet operations, recovery contract&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Azure&lt;/td&gt;&lt;td&gt;One MySQL Flexible Server HA deployment per shard&lt;/td&gt;&lt;td&gt;Placement, movement, fleet operations, recovery contract&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;AWS currently documents one writer and two readable standbys for an &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/multi-az-db-clusters-concepts.html&quot;&gt;RDS Multi-AZ DB cluster&lt;/a&gt;. Google documents regional high availability for &lt;a href=&quot;https://cloud.google.com/sql/docs/mysql/high-availability&quot;&gt;Cloud SQL for MySQL&lt;/a&gt;, and Azure documents a primary plus managed standby for &lt;a href=&quot;https://learn.microsoft.com/en-us/azure/mysql/flexible-server/concepts-high-availability&quot;&gt;MySQL Flexible Server HA&lt;/a&gt;. Those services can remove member-level work. They do not supply RetailCo’s tenant map or move protocol.&lt;/p&gt;
&lt;h3 id=&quot;when-the-control-plane-becomes-a-product-evaluate-a-product&quot;&gt;When the control plane becomes a product, evaluate a product&lt;/h3&gt;
&lt;p&gt;Custom application sharding is a strong fit when the tenant boundary is stable, cross-tenant transactions are rare, and the organization wants conventional MySQL inside every shard. It is a poor fit when every team would have to rebuild routing, resharding, topology, and transaction coordination independently.&lt;/p&gt;
&lt;p&gt;Two alternatives deserve a documented evaluation, not a casual name-check:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Vitess&lt;/strong&gt; externalizes MySQL sharding into VTGate routing, VSchema and Vindex metadata, topology services, and VReplication workflows such as MoveTables and Reshard. Vitess 23 documents single-shard, multi-shard best-effort, and two-phase-commit transaction modes, while recommending that cross-shard updates be minimized. Start with &lt;a href=&quot;https://vitess.io/docs/23.0/reference/features/vschema/&quot;&gt;VSchema&lt;/a&gt;, &lt;a href=&quot;https://vitess.io/docs/23.0/reference/vreplication/vreplication/&quot;&gt;VReplication&lt;/a&gt;, and &lt;a href=&quot;https://vitess.io/docs/23.0/reference/features/distributed-transaction/&quot;&gt;distributed transactions&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TiDB&lt;/strong&gt; is not an InnoDB sharding layer. It uses stateless TiDB SQL servers, TiKV storage, and PD scheduling to provide a distributed database behind a MySQL-compatible protocol. PingCAP documents meaningful differences from MySQL, including unsupported features and transaction-semantics qualifications: &lt;a href=&quot;https://docs.pingcap.com/tidb/v8.5/tidb-faq/&quot;&gt;TiDB 8.5 architecture FAQ&lt;/a&gt; and &lt;a href=&quot;https://docs.pingcap.com/tidb/v8.5/mysql-compatibility/&quot;&gt;TiDB 8.5 MySQL compatibility&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The conclusion is deliberately narrow. RetailCo should choose tenant-sharded standard MySQL when preserving MySQL behavior and exploiting a clean tenant boundary matter most. It should evaluate Vitess when the sharding control plane itself has become the repeated problem. It should evaluate TiDB only as a database-platform migration, with SQL, isolation, operations, ecosystem, and failure semantics tested—not as a transparent switch that merely adds shards.&lt;/p&gt;
&lt;p&gt;Neither alternative is lab-verified in this article.&lt;/p&gt;
&lt;h3 id=&quot;benchmark-the-routing-and-operations-not-only-steady-state-tps&quot;&gt;Benchmark the routing and operations, not only steady-state TPS&lt;/h3&gt;
&lt;p&gt;Status: &lt;strong&gt;PROPOSED LAB — NO EMPIRICAL RESULTS&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Compare one, two, four, and eight shard clusters with the same aggregate hardware per phase and a tenant-local order workload. Run both uniform demand and a skewed distribution with one hot tenant. Capture:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;attempted and successful TPS;&lt;/li&gt;
&lt;li&gt;P50, P95, and P99 transaction and commit latency;&lt;/li&gt;
&lt;li&gt;CPU, storage latency, IOPS, network, and binary-log volume by shard;&lt;/li&gt;
&lt;li&gt;Group Replication certification, apply queues, and flow control;&lt;/li&gt;
&lt;li&gt;connection counts and Router behavior;&lt;/li&gt;
&lt;li&gt;placement-cache hit, miss, and stale-route rejection rates;&lt;/li&gt;
&lt;li&gt;cross-shard transaction attempts;&lt;/li&gt;
&lt;li&gt;tenant-copy throughput and source impact;&lt;/li&gt;
&lt;li&gt;write-fence duration and ambiguous client outcomes;&lt;/li&gt;
&lt;li&gt;schema-rollout duration and pause events.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Then run the tests that expose the actual architecture:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Lab[sharded MySQL validation] --&gt; Scale[scale test — one, two, four, eight shards]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Lab --&gt; Skew[hot-tenant and uneven-placement test]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Lab --&gt; Move[tenant move — copy, catch-up, fence, cutover]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Lab --&gt; Fail[shard primary and full-shard failure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Lab --&gt; Control[catalog outage — warm and cold cache]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Lab --&gt; Change[schema rollout — canary, pause, resume]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Lab --&gt; Restore[point-in-time restore across a tenant move]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Do not promise linear scale. The result depends on tenant locality, workload skew, Router and connection behavior, per-shard storage, Group Replication overhead, global-service dependencies, and how much work remains cross-shard. Publish results only with exact versions, topology, dataset, workload, failure injection, and raw evidence.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;




























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure or constraint&lt;/th&gt;&lt;th&gt;Consequence&lt;/th&gt;&lt;th&gt;Required response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;One tenant exceeds one shard&lt;/td&gt;&lt;td&gt;The tenant remains bounded by one cluster&lt;/td&gt;&lt;td&gt;Sub-shard that tenant by another stable key or decompose its hottest domain&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Placement catalog unavailable&lt;/td&gt;&lt;td&gt;Cold routes and control-plane changes stop&lt;/td&gt;&lt;td&gt;Serve bounded warm-cache routes; pause creation and moves&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Stale application route&lt;/td&gt;&lt;td&gt;Write can reach the old shard&lt;/td&gt;&lt;td&gt;Enforce tenant and placement epoch inside every write path&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cross-shard transaction becomes common&lt;/td&gt;&lt;td&gt;XA, sagas, or compensation dominate application logic&lt;/td&gt;&lt;td&gt;Revisit the shard key and domain boundary&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Uneven tenant growth&lt;/td&gt;&lt;td&gt;Some shards saturate while others idle&lt;/td&gt;&lt;td&gt;Measure tenant demand and run deliberate rebalancing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schema rollout fails halfway&lt;/td&gt;&lt;td&gt;Fleet contains mixed schema versions&lt;/td&gt;&lt;td&gt;Expand-contract, ledger, canary, pause, resume, and compatible code&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tenant move spans backup timestamps&lt;/td&gt;&lt;td&gt;Restore can produce two physical copies&lt;/td&gt;&lt;td&gt;Preserve placement history and recover one authoritative epoch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Entire shard fails&lt;/td&gt;&lt;td&gt;Only its tenants are unavailable, but impact is real&lt;/td&gt;&lt;td&gt;Test shard-level DR, routing health, RPO, and RTO&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Global registry fails&lt;/td&gt;&lt;td&gt;Many shards can remain healthy while checkout blocks&lt;/td&gt;&lt;td&gt;Minimize synchronous global dependencies and design degradation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Operations scale with shard count&lt;/td&gt;&lt;td&gt;Patching, alerts, backup, and capacity work multiply&lt;/td&gt;&lt;td&gt;Automate the fleet before multiplying clusters&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;when-i-would-not-use-this-architecture&quot;&gt;When I would not use this architecture&lt;/h3&gt;
&lt;p&gt;I would not recommend custom tenant sharding when no stable ownership key exists, most transactions span tenants, one tenant alone exceeds the largest practical cluster, or the organization cannot build and test routing, movement, schema, backup, and recovery automation.&lt;/p&gt;
&lt;p&gt;I would also avoid it when the current writer still has inexpensive local improvements available. Bad indexes, excessive secondary indexes, row contention, chatty transactions, poor connection management, and unbounded history remain bad after sharding—only multiplied.&lt;/p&gt;
&lt;h3 id=&quot;retailco-verdict&quot;&gt;RetailCo verdict&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Good fit:               Tenant-local OLTP with uneven but movable tenant demand&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Poor fit:               Frequent cross-tenant ACID or one indivisible hot tenant&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Recommended option:     Explicit tenant catalog plus HA MySQL cluster per shard&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Main advantage:         Independent write, compute, storage, and failure domains&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Main limitation:        The application now owns placement and fleet correctness&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Operational complexity: High until movement, schema, backup, and DR are automated&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Migration complexity:   High; move one tenant cohort at a time with fencing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The decisive test is not whether several clusters can accept writes. It is whether RetailCo can prove which cluster owns each tenant during normal routing, failure, move, and restore. If that invariant is solid, additional clusters create real write domains. If it is vague, sharding creates additional ways to corrupt ownership.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Confirm that one writer is still the bottleneck after query, schema, connection, and storage work—not merely the most visible metric.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Select a stable tenant boundary, build the placement catalog and epoch fence, and keep HA local to each shard.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run uniform, skew, move, catalog-outage, shard-failure, schema-rollout, and point-in-time-recovery tests with archived evidence.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Pilot two shards with a low-risk tenant cohort, automate movement and recovery before adding more, and keep the cross-shard rate on the architecture scorecard.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The MySQL track continues with failure operations: how &lt;a href=&quot;https://rajivonai.com/blog/2025-09-16-proxysql-orchestrator-mysql-ha-large-topology/&quot;&gt;ProxySQL and Orchestrator&lt;/a&gt; coordinate routing and promotion in a large MySQL replica topology—and where fencing still has to be designed explicitly.&lt;/p&gt;
&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/mysql-shell/8.4/en/mysql-innodb-cluster.html&quot;&gt;MySQL InnoDB Cluster&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/mysql-router/8.4/en/mysql-router-deploying-bootstrapping.html&quot;&gt;MySQL Router bootstrap and routing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/mysql-router/8.4/en/mysql-router-general-metadata.html&quot;&gt;MySQL Router and InnoDB Cluster metadata&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/mysql-shell/8.4/en/innodb-clusterset-limitations.html&quot;&gt;InnoDB ClusterSet limitations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/xa.html&quot;&gt;MySQL XA transactions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://vitess.io/docs/23.0/reference/features/vschema/&quot;&gt;Vitess 23 VSchema&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://vitess.io/docs/23.0/reference/vreplication/vreplication/&quot;&gt;Vitess 23 VReplication&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://vitess.io/docs/23.0/reference/features/distributed-transaction/&quot;&gt;Vitess 23 distributed transactions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.pingcap.com/tidb/v8.5/tidb-faq/&quot;&gt;TiDB 8.5 FAQ and architecture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.pingcap.com/tidb/v8.5/mysql-compatibility/&quot;&gt;TiDB 8.5 compatibility with MySQL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://shopify.engineering/a-pods-architecture-to-allow-shopify-to-scale&quot;&gt;Shopify’s pod architecture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://shopify.engineering/mysql-database-shard-balancing-terabyte-scale&quot;&gt;Shopify’s MySQL shard balancing&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category></item><item><title>Cassandra Compaction: When STCS, LCS, and TWCS Become Your Bottleneck</title><link>https://rajivonai.com/blog/2025-03-15-cassandra-compaction-stcs-lcs-twcs/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-03-15-cassandra-compaction-stcs-lcs-twcs/</guid><description>Understanding the physics of Apache Cassandra compaction strategies, write amplification vs. read amplification, and when to migrate tables to survive production load.</description><pubDate>Sat, 15 Mar 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In a Log-Structured Merge-Tree (LSM) database like Cassandra, compaction is the heartbeat of the storage engine. If compaction falls behind, reads become exponentially slower. If compaction runs too aggressively, disk I/O saturates, and writes are blocked.&lt;/p&gt;
&lt;p&gt;Choosing the right compaction strategy is an exercise in balancing write amplification, read amplification, and space amplification.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In 2023, the 36-node Cassandra 4.1 cluster was managing 15 distinct production tables across 3 keyspaces. We were experiencing two severe, isolated incidents simultaneously.&lt;/p&gt;
&lt;p&gt;First, a massive time-series table exhausted 85% of its disk space during a major compaction, triggering capacity alerts. Second, a read-heavy key-value table suffered 20ms p99 read latencies, timing out upstream microservices.&lt;/p&gt;
&lt;p&gt;Both tables were using Cassandra’s default: &lt;strong&gt;Size-Tiered Compaction Strategy (STCS)&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;When Cassandra flushes data from memory, it writes an immutable SSTable to disk. As these files accumulate, Cassandra must read them, merge the data, resolve deleted data (tombstones), and write a new, larger SSTable.&lt;/p&gt;
&lt;p&gt;STCS is optimized for write-heavy workloads. It waits for multiple SSTables of similar size and merges them. However, it carries two fatal flaws for specific workloads:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Space Amplification:&lt;/strong&gt; Because STCS requires writing the new, merged file to disk &lt;em&gt;before&lt;/em&gt; deleting the old files, it temporarily requires 50% free disk space. For the time-series table, massive historical files were constantly being rewritten, consuming terabytes of overhead.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Read Amplification:&lt;/strong&gt; For the read-heavy lookup table, STCS allowed data for a single partition key to spread across 18 different SSTables. To satisfy a read request, Cassandra had to seek across 18 separate files on disk, causing the 20ms latency spikes.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;the-compaction-strategy-matrix&quot;&gt;The Compaction Strategy Matrix&lt;/h2&gt;
&lt;p&gt;Operators must align the compaction strategy to the physical access pattern of the data.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Data[Table Workload Profile] --&gt; Eval{Access Pattern}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Eval --&gt;|Time-Series — Append Only| TWCS[Time-Window Compaction Strategy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Eval --&gt;|Read-Heavy Lookup| LCS[Leveled Compaction Strategy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Eval --&gt;|Write-Heavy — Updates| STCS[Size-Tiered Compaction Strategy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Eval --&gt;|Modern Cassandra 5.x and later| UCS[Unified Compaction Strategy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    TWCS --&gt;|Isolates data by time buckets — Zero-cost TTL drops| Benefit1[Bounds Space Amplification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    LCS --&gt;|Guarantees 1-2 SSTables per read — High write amplification| Benefit2[Bounds Read Amplification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    STCS --&gt;|Low write amplification — High space amplification| Benefit3[Fast Ingestion]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    UCS --&gt;|Dynamically adapts — Shards across disks| Benefit4[Unifies Tradeoffs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-migrating-to-time-window-compaction-strategy-twcs&quot;&gt;1. Migrating to Time-Window Compaction Strategy (TWCS)&lt;/h3&gt;
&lt;p&gt;For the time-series table, we migrated the schema to TWCS. TWCS groups SSTables by time buckets (e.g., &lt;code&gt;1 DAY&lt;/code&gt;). It compacts files &lt;em&gt;within&lt;/em&gt; the window, but never merges data across windows. This means old data is never rewritten, eliminating STCS space amplification. Furthermore, when the TTL expires, Cassandra simply drops the entire window from disk without requiring a compaction cycle.&lt;/p&gt;
&lt;h3 id=&quot;2-migrating-to-leveled-compaction-strategy-lcs&quot;&gt;2. Migrating to Leveled Compaction Strategy (LCS)&lt;/h3&gt;
&lt;p&gt;For the read-heavy table, we migrated to LCS. LCS organizes data into levels (L0, L1, L2), where each level is 10x larger than the previous. Crucially, LCS guarantees that 90% of reads will touch exactly one SSTable. This immediately solved the read amplification issue, bringing p99 latency back down to 2ms.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Migrating a live production table requires careful execution. Altering a table to TWCS is relatively safe, but altering a large STCS table to LCS online triggers a massive I/O storm as Cassandra frantically reorganizes all existing data into Level 0 and Level 1.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Safe migration for time-series data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; metrics&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;sensor_data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; compaction &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;class&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;TimeWindowCompactionStrategy&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;compaction_window_unit&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;DAYS&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;compaction_window_size&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- For LCS, we ensured NVMe instances and temporarily boosted throughput&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; lookups&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;user_profiles&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; compaction &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;class&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;LeveledCompactionStrategy&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;sstable_size_in_mb&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;160&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We temporarily increased &lt;code&gt;compaction_throughput_mb_per_sec&lt;/code&gt; to 128 MB/s via &lt;code&gt;nodetool&lt;/code&gt; to help the nodes chew through the LCS conversion backlog, carefully monitoring &lt;code&gt;nodetool compactionstats&lt;/code&gt; to ensure client latencies remained stable.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;STCS on Large Volumes&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;STCS merging a 1 TB file requires 1 TB of free disk space.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Never let STCS tables exceed 50% physical disk capacity.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;LCS Write Amplification&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;LCS rewrites data constantly as it promotes SSTables through levels, burning disk IOPS.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Only use LCS on read-heavy workloads. Ensure underlying storage is NVMe or provisioned IOPS EBS (&lt;code&gt;gp3&lt;/code&gt;).&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;TWCS Out-of-Order Writes&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;If you write data with a timestamp from 30 days ago, it breaks the TWCS time windows, forcing old windows to compact and destroying the TTL drop mechanism.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Strictly enforce append-only sequential writes at the application layer when using TWCS.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-and-ucs&quot;&gt;Modern Cassandra Context: Cassandra 5.0 and UCS&lt;/h3&gt;
&lt;p&gt;The operational pain of picking the “least bad” compaction strategy is exactly why Cassandra 5.0 introduces the &lt;strong&gt;Unified Compaction Strategy (UCS)&lt;/strong&gt;. UCS replaces STCS, LCS, and TWCS with a single algorithm that dynamically adapts to the workload. It introduces sharding, allowing it to cap space amplification like LCS while retaining the write speed of STCS. STCS remains the configured default in Cassandra 5.0 for backward compatibility, but UCS is the recommended strategy for most workloads going forward — adopting it still requires an explicit &lt;code&gt;ALTER TABLE&lt;/code&gt; per table, it isn’t automatic, but it does end the practice of hand-picking STCS vs. LCS vs. TWCS per table based on write/read/TTL tradeoffs.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Default STCS compaction causes disk capacity exhaustion on large tables and latency spikes on read-heavy workloads due to read amplification.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Align compaction strategies to workloads: use TWCS for time-series/TTL data, LCS for read-heavy lookups, and ensure adequate disk throughput.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Converting time-series tables to TWCS instantly halted 50% disk capacity swings, and LCS bounded read queries to a single SSTable lookup.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run &lt;code&gt;nodetool tablehistograms &amp;#x3C;keyspace&gt; &amp;#x3C;table&gt;&lt;/code&gt;. If the &lt;code&gt;SSTables&lt;/code&gt; metric frequently exceeds 3, your compaction strategy is failing your read path.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>failures</category></item><item><title>Linux and EC2 OS Tuning for Cassandra</title><link>https://rajivonai.com/blog/2025-02-26-linux-ec2-os-tuning-for-cassandra/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-02-26-linux-ec2-os-tuning-for-cassandra/</guid><description>Why default Linux AMIs destroy Apache Cassandra performance, and how to tune Transparent Huge Pages, swappiness, and disk readahead for stateful databases.</description><pubDate>Wed, 26 Feb 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A database is only as reliable as the operating system underneath it. You can spend weeks tuning Cassandra’s JVM and compaction strategies, but if the Linux kernel is fighting the database for memory pages, your cluster will randomly freeze.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In 2022, a typical deployment involves a 27-node Cassandra 4.0 cluster on AWS EC2 using a standard Ubuntu 20.04 LTS AMI. Within weeks, clusters often experience unexplained node freezes.&lt;/p&gt;
&lt;p&gt;During peak traffic, nodes would lock up for 10–15 seconds. &lt;code&gt;system.log&lt;/code&gt; was flooded with &lt;code&gt;java.lang.OutOfMemoryError: Map failed&lt;/code&gt; errors, even though the JVM heap had plenty of free space. Disk I/O stalled, and OS monitoring showed CPU spikes entirely consumed by the kernel’s &lt;code&gt;mm/vmscan&lt;/code&gt; process. Furthermore, swap memory usage was climbing even though the instances had 64 GB of physical RAM.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Out of the box, standard Linux distributions are tuned for generic, multi-tenant workloads. They attempt to optimize memory for desktop applications or stateless web servers. Cassandra is a stateful database that bypasses much of this logic to manage memory and disk directly.&lt;/p&gt;
&lt;p&gt;When default Linux settings collide with Cassandra’s architecture, three catastrophic things happen:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Transparent Huge Pages (THP):&lt;/strong&gt; Linux tries to merge 4KB memory pages into 2MB blocks to improve CPU cache hits. When memory fragments, the kernel locks the system to defragment it. Cassandra’s allocation patterns guarantee memory fragmentation, causing THP to freeze the OS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Memory Mapped Files (mmap):&lt;/strong&gt; Cassandra maps its SSTables directly into memory to allow the OS page cache to handle reads. A default Linux kernel limits a process to 65,530 memory maps (&lt;code&gt;vm.max_map_count&lt;/code&gt;). When Cassandra opens thousands of SSTables, it hits this limit and crashes with a map failure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;OS Readahead:&lt;/strong&gt; Linux assumes sequential disk reads and pre-fetches data. If &lt;code&gt;readahead&lt;/code&gt; is set to 128KB, a Cassandra random 4KB read fetches 128KB of data from EBS, wasting 96% of the I/O throughput and saturating the EBS network link.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;the-kernel-storage-and-memory-architecture&quot;&gt;The Kernel Storage and Memory Architecture&lt;/h2&gt;
&lt;p&gt;To fix this, operators must systematically disable the OS “optimizations” and hand control back to Cassandra.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Cass[Cassandra JVM] --&gt;|mmap SSTable| PageCache[Linux Page Cache]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Cass --&gt;|Direct IO| NVMe[EC2 NVMe — EBS Driver]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;Linux Kernel Memory Management&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        PageCache --&gt; THP{Transparent Huge Pages}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        THP --&gt;|Enabled: Defrag Lock| Freeze[OS Freeze — CPU Spike]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        THP --&gt;|Disabled: 4KB Pages| FastMem[Stable Allocations]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        PageCache --&gt; Swap{Swappiness &gt; 0?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Swap --&gt;|Yes| DiskSwap[Pages written to Disk]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Swap --&gt;|Swappiness 0 or 1| RAM[Pages kept in RAM]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-disable-transparent-huge-pages&quot;&gt;1. Disable Transparent Huge Pages&lt;/h3&gt;
&lt;p&gt;You must disable THP completely. A common mitigation is to implement a &lt;code&gt;systemd&lt;/code&gt; service that executes &lt;code&gt;echo never &gt; /sys/kernel/mm/transparent_hugepage/enabled&lt;/code&gt; before the Cassandra daemon starts. This instantly eliminated the &lt;code&gt;mm/vmscan&lt;/code&gt; CPU spikes.&lt;/p&gt;
&lt;h3 id=&quot;2-tuning-sysctl-for-databases&quot;&gt;2. Tuning Sysctl for Databases&lt;/h3&gt;
&lt;p&gt;We created a hardened &lt;code&gt;/etc/sysctl.d/99-cassandra.conf&lt;/code&gt; file to override default kernel behavior:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;vm.swappiness = 1&lt;/code&gt;: This tells the kernel to aggressively keep data in RAM and only use swap to prevent an OOM-killer panic. (Do not set it to &lt;code&gt;0&lt;/code&gt;, or the kernel will violently kill processes under memory pressure).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;vm.max_map_count = 1048575&lt;/code&gt;: This provides the headroom Cassandra needs to map thousands of SSTable components.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-disk-readahead&quot;&gt;3. Disk Readahead&lt;/h3&gt;
&lt;p&gt;Apache Cassandra’s production baseline for random-read workloads is a 4 KB readahead. We enforced this at the block device level using &lt;code&gt;blockdev --setra 8 /dev/nvme1n1&lt;/code&gt; (8 sectors × 512 bytes = 4 KB). This prevented the OS from saturating the EBS bandwidth by pre-fetching data Cassandra would never use.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for deploying Cassandra on Linux is to codify OS overrides into a configuration management tool like Ansible before the database daemon is ever installed.&lt;/p&gt;
&lt;p&gt;Because of how the Linux page cache and TCP stack behave under high concurrency, operators apply a strict set of kernel tunings to override default generic-server behaviors:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;ini&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# /etc/sysctl.d/99-cassandra.conf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;vm.swappiness&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;vm.max_map_count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 1048575&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;net.ipv4.tcp_rmem&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 4096 87380 16777216&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;net.ipv4.tcp_wmem&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 4096 65536 16777216&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To prevent the kernel from terminating the JVM under memory pressure, the documented pattern also configures PAM limits (&lt;code&gt;/etc/security/limits.d/cassandra.conf&lt;/code&gt;) to allow the &lt;code&gt;cassandra&lt;/code&gt; user to lock memory (&lt;code&gt;memlock&lt;/code&gt;) and open up to &lt;code&gt;1048576&lt;/code&gt; file descriptors (&lt;code&gt;nofile&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Cassandra’s behavior when these limits are misconfigured is catastrophic but predictable. When &lt;code&gt;max_map_count&lt;/code&gt; is too low, the database cannot map SSTable components into memory, resulting in &lt;code&gt;OutOfMemoryError: Map failed&lt;/code&gt; exceptions that force the JVM to crash. Furthermore, without sufficient file descriptors, the database cannot maintain its connection pools or open required SSTables during compaction, leading to failed reads and artificial I/O pressure as the system retries failed memory maps. Applying these limits strictly, alongside &lt;code&gt;blockdev --setra 8&lt;/code&gt; for 4 KB disk readahead, stabilizes the allocation and storage paths.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Disabling Swap Completely&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;If you disable swap entirely (&lt;code&gt;swapoff -a&lt;/code&gt;), a sudden memory spike will cause the kernel OOM-killer to instantly terminate the Cassandra process.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep swap enabled but set &lt;code&gt;vm.swappiness=1&lt;/code&gt; for graceful degradation under extreme pressure.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;High OS Readahead&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;A 128KB readahead causes massive read amplification on EBS volumes for random 4KB data fetches.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Strictly enforce &lt;code&gt;blockdev --setra 8&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Default CPU Scaling&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Standard AMIs use the &lt;code&gt;ondemand&lt;/code&gt; CPU governor, which introduces microsecond latencies when ramping up clock speeds.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set the scaling governor to &lt;code&gt;performance&lt;/code&gt; where the instance family actually exposes &lt;code&gt;scaling_governor&lt;/code&gt; control to the guest OS — check &lt;code&gt;/sys/devices/system/cpu/cpu0/cpufreq/&lt;/code&gt; first, since this varies by instance generation and hypervisor.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-linux-kernel-6x-and-nvme-schedulers&quot;&gt;Modern Cassandra Context: Linux Kernel 6.x and NVMe Schedulers&lt;/h3&gt;
&lt;p&gt;Modern EC2 instances running Linux Kernel 6.x and AWS Nitro have changed the storage I/O stack. With the introduction of &lt;code&gt;io_uring&lt;/code&gt; considerations and modern NVMe drivers, operators must ensure that the block layer I/O scheduler is set to &lt;code&gt;none&lt;/code&gt; (bypassing legacy scheduling) rather than &lt;code&gt;mq-deadline&lt;/code&gt;, allowing the Nitro hardware controller to manage queue depth natively. Furthermore, Cassandra 5.0’s startup scripts (&lt;code&gt;cassandra-env.sh&lt;/code&gt;) now actively validate these sysctl baselines and will warn operators before the daemon starts.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Default Linux AMIs enable Transparent Huge Pages and restrictive memory map limits, causing memory fragmentation stalls and process crashes on stateful databases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Disable THP, set &lt;code&gt;vm.swappiness=1&lt;/code&gt;, increase &lt;code&gt;vm.max_map_count&lt;/code&gt;, and clamp disk readahead to 4 KB.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Hardening these OS layers instantly stops &lt;code&gt;vmscan&lt;/code&gt; CPU spikes and eliminates EBS bandwidth saturation caused by useless pre-fetching.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; SSH into a node and run &lt;code&gt;cat /sys/kernel/mm/transparent_hugepage/enabled&lt;/code&gt;. If the output shows &lt;code&gt;[always]&lt;/code&gt; instead of &lt;code&gt;[never]&lt;/code&gt;, your cluster is a ticking time bomb.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>Double Write Buffers Fail at the I/O Boundary</title><link>https://rajivonai.com/blog/2025-02-22-double-write-buffers-fail-at-the-i-o-boundary/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-02-22-double-write-buffers-fail-at-the-i-o-boundary/</guid><description>Why porting InnoDB’s double write buffer to PostgreSQL breaks on buffered I/O, fsync semantics, and background writer design.</description><pubDate>Sat, 22 Feb 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A double write buffer only protects a database if the second write crosses the same durability boundary as the first; port InnoDB’s double write buffer into PostgreSQL without that boundary, and you have built a corruption machine with better comments.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;AI coding agents are now good enough to produce plausible systems code inside mature engines like PostgreSQL. That changes the review problem: the first failure is no longer “does it compile?” but “does the generated design preserve the subsystem’s recovery invariants?”&lt;/p&gt;
&lt;p&gt;The default PostgreSQL protection is write-ahead log (WAL) full page writes (FPW): after each checkpoint, the first modification of a page writes the whole page image into WAL. The tempting alternative is an InnoDB-style double write buffer (DWB): write a safe copy of the page elsewhere, flush it, then write the page to its final data-file location.&lt;/p&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Approach&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Recovery copy&lt;/th&gt;&lt;th&gt;Durability boundary&lt;/th&gt;&lt;th&gt;Primary cost&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;PostgreSQL FPW&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Full 8KB page image in WAL&lt;/td&gt;&lt;td&gt;WAL flush through &lt;code&gt;wal_sync_method&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Higher WAL volume after checkpoints&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;InnoDB DWB&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Page copy in doublewrite files&lt;/td&gt;&lt;td&gt;DWB flush before final data-file write&lt;/td&gt;&lt;td&gt;Extra data writes and recovery state&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Naive PostgreSQL DWB port&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Page copy in a new buffer area&lt;/td&gt;&lt;td&gt;Often mistaken as &lt;code&gt;smgrwrite()&lt;/code&gt; or &lt;code&gt;sync_file_range()&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Silent loss of the only safe copy&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The non-obvious failure is that InnoDB’s DWB and PostgreSQL’s FPW solve the same torn-page problem under different I/O contracts. MySQL documents InnoDB’s DWB as a storage area written before pages go to their proper locations, with a single &lt;code&gt;fsync()&lt;/code&gt; for the doublewrite chunk in the normal design (&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.0/en/innodb-doublewrite-buffer.html&quot;&gt;MySQL 8.0 manual&lt;/a&gt;). PostgreSQL documents FPW as necessary because an operating-system crash can leave a page containing a mix of old and new data, and row-level WAL alone cannot repair that page (&lt;a href=&quot;https://www.postgresql.org/docs/current/runtime-config-wal.html&quot;&gt;PostgreSQL WAL settings&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;The dangerous part is that the APIs look boring. &lt;code&gt;write()&lt;/code&gt;, &lt;code&gt;fsync()&lt;/code&gt;, &lt;code&gt;sync_file_range()&lt;/code&gt;, background writer, checkpointer. An AI agent can assemble those names into code that resembles a storage feature. The database will still start. Basic tests will still pass. Then the first crash at the wrong microsecond becomes your design review.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure point&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;smgrwrite()&lt;/code&gt; treated as durable&lt;/td&gt;&lt;td&gt;PostgreSQL has handed bytes to the kernel page cache, not necessarily persistent media&lt;/td&gt;&lt;td&gt;A DWB slot can be reused before the destination page is safe&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;sync_file_range()&lt;/code&gt; treated as &lt;code&gt;fsync()&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Linux documents &lt;code&gt;SYNC_FILE_RANGE_WRITE&lt;/code&gt; as asynchronous and warns it is not suitable for data integrity operations (&lt;a href=&quot;https://man7.org/linux/man-pages/man2/sync_file_range2.2.html&quot;&gt;man7&lt;/a&gt;)&lt;/td&gt;&lt;td&gt;The code can believe flushing started when recovery needs proof flushing finished&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;BgWriter given synchronous DWB work&lt;/td&gt;&lt;td&gt;&lt;code&gt;bgwriter_delay&lt;/code&gt; defaults to 200ms and &lt;code&gt;bgwriter_lru_maxpages&lt;/code&gt; bounds per-round writes in PostgreSQL’s background writer design (&lt;a href=&quot;https://www.postgresql.org/docs/16/runtime-config-resource.html&quot;&gt;PostgreSQL resource settings&lt;/a&gt;)&lt;/td&gt;&lt;td&gt;A process designed to smooth dirty-buffer pressure becomes an fsync bottleneck&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;FPW removed before DWB proves equivalence&lt;/td&gt;&lt;td&gt;PostgreSQL’s &lt;code&gt;full_page_writes&lt;/code&gt; default is &lt;code&gt;on&lt;/code&gt;, and docs warn disabling it can cause unrecoverable or silent corruption after failure&lt;/td&gt;&lt;td&gt;You save WAL bytes by deleting the recovery source of truth&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slot metadata reused early&lt;/td&gt;&lt;td&gt;The page copy may be durable, but the mapping from page identity to DWB slot is no longer valid&lt;/td&gt;&lt;td&gt;The hardest corruption is not a torn page; it is confidence in a backup you already overwrote&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The core question is not whether PostgreSQL can have a double write buffer. It is whether the design can prove, at every crash point, that either WAL or DWB contains a complete page image newer than the torn data-file page.&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;A correct PostgreSQL DWB design has to be staged around recovery truth, not modeled as an extra function call in &lt;code&gt;FlushBuffer()&lt;/code&gt;. The invariant is simple enough to write on a whiteboard: do not reuse the DWB slot until the final page location has been confirmed durable after the page write.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Dirty[dirty buffer selected] --&gt; Copy[copy page to DWB slot]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Copy --&gt; DwbFsync[fsync DWB file]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DwbFsync --&gt; WalCheck[confirm WAL ordering]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WalCheck --&gt; DataWrite[write page to tablespace]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DataWrite --&gt; DataSync[fsync tablespace file]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DataSync --&gt; Reclaim[reclaim DWB slot]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Crash[crash recovery] --&gt; Inspect[inspect page checksum and LSN]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Inspect --&gt;|page torn| Restore[restore from DWB or WAL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Inspect --&gt;|page valid| Replay[continue WAL replay]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Define the authoritative recovery copy per page version.&lt;br&gt;
If FPW remains enabled, WAL is authoritative for first-touch pages after checkpoint. If DWB is intended to replace FPW, the DWB slot plus metadata must become authoritative. Verification: write a crash-state matrix for DWB write, DWB fsync, tablespace write, tablespace fsync, checkpoint record, and slot reuse.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Separate page copy from durability confirmation.&lt;br&gt;
Copying an 8KB PostgreSQL page into a DWB slot is not the expensive part. The expensive part is proving that copy is on persistent storage, with its page identity, block number, relation fork, page LSN, and checksum intact. Verification: a crash after DWB copy but before DWB fsync must recover from WAL or ignore the incomplete DWB entry.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Delay slot reuse until the destination file crosses a real sync boundary.&lt;br&gt;
In PostgreSQL’s buffered I/O model, a successful data-file write is not enough. &lt;code&gt;sync_file_range()&lt;/code&gt; can start writeback, but Linux explicitly does not make it a portable crash-safety primitive. Verification: a crash after tablespace write but before tablespace fsync must still find the DWB slot valid.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Keep synchronous I/O out of the single BgWriter loop.&lt;br&gt;
PostgreSQL spreads checkpoint writes over time with &lt;code&gt;checkpoint_completion_target&lt;/code&gt;, defaulting to 0.9 in current releases, specifically to avoid bursty I/O (&lt;a href=&quot;https://www.postgresql.org/docs/current/runtime-config-wal.html&quot;&gt;PostgreSQL checkpoint settings&lt;/a&gt;). A DWB implementation needs a manager, batched slots, and completion accounting, not a per-buffer fsync in the background writer. Verification: track &lt;code&gt;buffers_backend&lt;/code&gt;, checkpoint duration, WAL generation, and p99 write latency under &lt;code&gt;pgbench&lt;/code&gt; before and after enabling the prototype.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Make recovery boring.&lt;br&gt;
Recovery must not infer intent from partially updated state. It should read DWB metadata, validate checksums and LSNs, restore only complete entries, and ignore anything whose durability boundary was not crossed. Verification: run crash injection at every transition, including slot metadata update and slot reuse.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented comparison is already enough to reject the naive port.&lt;/p&gt;
&lt;p&gt;PostgreSQL’s own documentation says &lt;code&gt;full_page_writes&lt;/code&gt; stores the whole disk page in WAL on the first modification after checkpoint because a torn data page cannot be repaired from row-level WAL alone. It also states the default is &lt;code&gt;on&lt;/code&gt; and that disabling it can lead to unrecoverable or silent corruption after a system failure. That is not a tuning hint. That is a contract.&lt;/p&gt;
&lt;p&gt;MySQL’s InnoDB documentation describes a different contract: pages flushed from the buffer pool are first written to the doublewrite area, and crash recovery can use that good copy if the final data-file write was interrupted. Since MySQL 8.0.20, those doublewrite pages live in doublewrite files rather than the old system tablespace location; since MySQL 8.0.30, &lt;code&gt;innodb_doublewrite&lt;/code&gt; also supports &lt;code&gt;DETECT_AND_RECOVER&lt;/code&gt; and &lt;code&gt;DETECT_ONLY&lt;/code&gt;. The design is not merely “write the page twice.” It is “write the page twice with ordered recovery metadata and a known flush point.”&lt;/p&gt;
&lt;p&gt;The documented pattern is clear: if generated code reclaims a DWB slot after &lt;code&gt;smgrwrite()&lt;/code&gt; or after an advisory range flush, it has confused a buffered write with a durable write. That is enough to violate the recovery invariant. The system can lose the durable DWB copy while the data-file page is still only dirty kernel state.&lt;/p&gt;
&lt;p&gt;This is exactly where AI-assisted systems work gets risky. Language models are strong at local similarity: InnoDB has a DWB, PostgreSQL has dirty pages, both have write paths, so assemble the bridge. But storage engines are not CRUD apps with worse naming. The important behavior lives between process architecture, kernel writeback, filesystem semantics, WAL ordering, and the crash replay path. The code shape is the least interesting part.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Premature DWB slot reuse&lt;/td&gt;&lt;td&gt;Slot is freed after &lt;code&gt;smgrwrite()&lt;/code&gt; returns on PostgreSQL with buffered I/O&lt;/td&gt;&lt;td&gt;Reclaim only after confirmed destination &lt;code&gt;fsync()&lt;/code&gt; or equivalent durable sync after the page write&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;False confidence from &lt;code&gt;sync_file_range()&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Linux &lt;code&gt;SYNC_FILE_RANGE_WRITE&lt;/code&gt; starts asynchronous writeback and does not flush volatile disk caches&lt;/td&gt;&lt;td&gt;Use it only as a writeback hint; keep &lt;code&gt;fsync()&lt;/code&gt; or &lt;code&gt;fdatasync()&lt;/code&gt; as the durability boundary&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;BgWriter latency collapse&lt;/td&gt;&lt;td&gt;Per-page DWB fsync added to a loop governed by &lt;code&gt;bgwriter_delay&lt;/code&gt; and &lt;code&gt;bgwriter_lru_maxpages&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Move DWB fsync into batched workers with completion queues and backpressure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Checkpoint storms&lt;/td&gt;&lt;td&gt;DWB fsync work prevents dirty buffers from being cleaned ahead of checkpoints&lt;/td&gt;&lt;td&gt;Budget DWB throughput against &lt;code&gt;checkpoint_completion_target&lt;/code&gt;, &lt;code&gt;max_wal_size&lt;/code&gt;, and observed checkpoint sync time&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;WAL invariant drift&lt;/td&gt;&lt;td&gt;DWB metadata claims protection for a page whose WAL record was not flushed in the expected order&lt;/td&gt;&lt;td&gt;Tie DWB entries to page LSNs and WAL flush state; reject entries recovery cannot order&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Recovery ambiguity&lt;/td&gt;&lt;td&gt;DWB slot has page bytes but stale relation, fork, block, checksum, or LSN metadata&lt;/td&gt;&lt;td&gt;Make metadata durable with the slot and validate all identifiers before restore&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Misleading benchmark win&lt;/td&gt;&lt;td&gt;FPW disabled on a clean shutdown benchmark with no crash injection&lt;/td&gt;&lt;td&gt;Require power-fail tests, torn-page injection, and recovery validation before comparing WAL volume&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Version-specific InnoDB copying&lt;/td&gt;&lt;td&gt;MySQL 8.0.20 moved DWB storage to doublewrite files; older mental models still cite &lt;code&gt;ibdata1&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Treat engine version as part of the design, not trivia&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: AI-generated storage code can compile while breaking the only invariant that matters: after a crash, one complete page image must exist.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Review DWB as a recovery protocol with explicit durable states, not as a write-path optimization.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: The validation signal is not a passing smoke test; it is crash injection across every DWB, WAL, tablespace write, fsync, checkpoint, and slot-reuse transition.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, take one generated systems patch and write its durability matrix: recovery source of truth, sync boundary, reclaim condition, and invalid crash states.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A database does not care that the code looked like the reference architecture; it only cares which bytes survived the crash.&lt;/p&gt;</content:encoded><category>databases</category><category>ai-engineering</category><category>failures</category></item><item><title>MySQL vs PostgreSQL vs Oracle: Why Uber Re-Architected Their Storage Layer</title><link>https://rajivonai.com/blog/2025-02-15-mysql-vs-postgres-vs-oracle-clustered-indexes-execution/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-02-15-mysql-vs-postgres-vs-oracle-clustered-indexes-execution/</guid><description>A deep dive into how MySQL, PostgreSQL, and Oracle fundamentally differ in physical storage organization, index architecture, and why Uber famously migrated from Postgres to MySQL.</description><pubDate>Sat, 15 Feb 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The SQL standard provides a unified language for querying data, but it dangerously obscures the violent physical differences in how database engines actually execute those queries. Engineers migrating from Oracle to PostgreSQL, or PostgreSQL to MySQL, often carry over architectural assumptions that result in catastrophic performance regressions in production.&lt;/p&gt;
&lt;p&gt;To understand how deep these differences go, we have to look past the syntax and examine the physical bytes on the disk. The decision between MySQL and PostgreSQL is not just about JSON support or vacuuming; it is a fundamental choice about how B-Trees map to physical storage, a choice that famously forced Uber to completely re-architect their persistence layer.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;As organizations modernize their tech stacks, platform teams frequently deploy a polyglot persistence layer. A single architecture might use Amazon Aurora MySQL for commerce, PostgreSQL for geo-spatial microservices, and legacy Oracle for financial ledgers.&lt;/p&gt;
&lt;p&gt;Developers interacting with these systems often treat them as interchangeable SQL endpoints. They run &lt;code&gt;EXPLAIN&lt;/code&gt; to verify an index is used, apply the same indexing strategies across all three, and assume the database optimizer will figure out the rest.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A query plan is a lie agreed upon by the optimizer and the statistics engine. More importantly, an index in MySQL is a completely different physical structure than an index in PostgreSQL or Oracle.&lt;/p&gt;
&lt;p&gt;If a developer adds three secondary indexes to a highly-updated table in PostgreSQL, the performance penalty manifests as massive write amplification and replication bloat. If they do the same thing in MySQL’s InnoDB, the penalty behaves entirely differently due to the clustered index.&lt;/p&gt;
&lt;p&gt;Understanding why this happens requires looking at the physical storage engine layer. The core question is: how does the physical storage architecture of a table dictate index efficiency, write amplification, and replication across MySQL, PostgreSQL, and Oracle?&lt;/p&gt;
&lt;h2 id=&quot;storage-and-execution-architecture&quot;&gt;Storage and Execution Architecture&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[SQL Query] --&gt; B[Database Optimizer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[Execution Engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[MySQL InnoDB — Clustered Index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[Data stored in Primary Key B-Tree leaves]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[Secondary Indexes store PK Strings]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; G[PostgreSQL — Heap Storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[Data stored in unordered Heap blocks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[Secondary Indexes store physical TIDs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; J[Oracle — Heap by Default]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[Data stored in Heap blocks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; L[Optional: Index-Organized Tables IOT]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice-mysqls-clustered-index&quot;&gt;In Practice: MySQL’s Clustered Index&lt;/h2&gt;
&lt;p&gt;The documented pattern for MySQL (InnoDB) is the &lt;strong&gt;Clustered Index&lt;/strong&gt;, or Index-Organized Table (IOT). In InnoDB, the table data does not exist independently of the Primary Key; the data &lt;em&gt;is&lt;/em&gt; the leaf node of the Primary Key B+Tree.&lt;/p&gt;
&lt;p&gt;Because of this, primary key lookups are incredibly fast. However, secondary indexes do not point directly to the physical row. Instead, a secondary index stores the string or integer value of the Primary Key. This means a secondary index lookup in MySQL is a two-step process: traverse the secondary index to find the Primary Key value, then traverse the Primary Key B-Tree to find the actual data row.&lt;/p&gt;
&lt;p&gt;This architecture provides a massive hidden advantage during &lt;code&gt;UPDATE&lt;/code&gt; operations. If you update a row in MySQL, and that update does not change the Primary Key or any columns used in a secondary index, the secondary indexes are completely untouched. The physical data is updated in place, and the secondary index pointers (the Primary Key values) remain perfectly valid.&lt;/p&gt;
&lt;h2 id=&quot;in-practice-ubers-battle-with-postgresql-write-amplification&quot;&gt;In Practice: Uber’s Battle with PostgreSQL Write Amplification&lt;/h2&gt;
&lt;p&gt;PostgreSQL operates on a &lt;strong&gt;Heap Storage&lt;/strong&gt; model. The table data is stored in an unordered structure (the heap). All indexes in PostgreSQL, including the primary key, are secondary indexes, and they contain direct physical pointers (Tuple IDs or TIDs) to the exact block and offset in the heap. A secondary index lookup in PostgreSQL is generally a faster, one-step process directly to the physical disk location.&lt;/p&gt;
&lt;p&gt;However, this architecture creates a devastating problem at extreme scale: &lt;strong&gt;Write Amplification&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In 2016, &lt;strong&gt;Uber’s engineering team&lt;/strong&gt; published a highly publicized breakdown of why they were migrating their core architecture off PostgreSQL and onto a custom sharding layer built on MySQL (“Schemaless”). Uber’s own writeup names several factors, but one of the primary documented reasons was Postgres’s implementation of Multiversion Concurrency Control (MVCC) combined with its Heap storage architecture.&lt;/p&gt;
&lt;p&gt;When you &lt;code&gt;UPDATE&lt;/code&gt; a row in PostgreSQL, the database does not update the physical row in place. Instead, it writes an entirely new copy of the row into the heap, and marks the old row as obsolete (to be cleaned up later by the Autovacuum process).&lt;/p&gt;
&lt;p&gt;Because the row’s physical location on disk just changed, &lt;em&gt;every single index on the table must be updated to point to the new physical TID&lt;/em&gt;. If Uber had a table with 10 secondary indexes, a single &lt;code&gt;UPDATE&lt;/code&gt; to a completely unindexed column resulted in 10 physical index writes. This caused massive write amplification, saturating their SSDs and crippling their replication bandwidth, because PostgreSQL’s Write-Ahead Log (WAL) had to broadcast all of these physical changes across data centers.&lt;/p&gt;
&lt;p&gt;By migrating to MySQL’s InnoDB, Uber leveraged the Clustered Index architecture. When a row was updated in MySQL, the data was updated in place (or the old version was pushed to the undo log). Because the Primary Key didn’t change, none of the secondary indexes had to be updated. The replication payload dropped significantly, and disk I/O was saved.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;(Note: PostgreSQL has since introduced HOT (Heap-Only Tuples) updates to mitigate this, but HOT updates only work if the new row version fits on the same physical page and no indexed columns are altered. At Uber’s scale, the physical architecture of InnoDB proved superior for their specific heavy-update workload.)&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;in-practice-oracles-enterprise-flexibility&quot;&gt;In Practice: Oracle’s Enterprise Flexibility&lt;/h2&gt;
&lt;p&gt;Oracle defaults to the same Heap Storage model as PostgreSQL, meaning it separates data blocks from index blocks. However, Oracle provides the enterprise capability to explicitly declare an &lt;strong&gt;Index-Organized Table (IOT)&lt;/strong&gt;, allowing engineers to intentionally opt-in to the InnoDB-style clustered architecture.&lt;/p&gt;
&lt;p&gt;Oracle requires deep DBA expertise to know when to override the default. If a financial application requires billions of fast, sequential primary-key lookups (like an IoT telemetry stream), an Oracle DBA will explicitly build an IOT. If the workload involves massive, wide tables with unpredictable secondary queries, they stick to the Heap.&lt;/p&gt;
&lt;p&gt;When it comes to execution plans, the tooling philosophies also differ. MySQL’s &lt;code&gt;EXPLAIN&lt;/code&gt; traditionally predicts what the optimizer will do based on heuristics. PostgreSQL’s &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; actually executes the query, discards the result, and reports the physical truth of memory usage, disk blocks read, and exact timing. Oracle’s &lt;code&gt;DBMS_XPLAN&lt;/code&gt; leans heavily on historical statistics, but its Adaptive Query Optimization can actually change the execution plan mid-flight if it realizes its initial statistical assumptions were wrong.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Database&lt;/th&gt;&lt;th&gt;Feature&lt;/th&gt;&lt;th&gt;Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;MySQL (InnoDB)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Clustered Index&lt;/td&gt;&lt;td&gt;Updates are highly efficient because secondary indexes aren’t physically tied to disk blocks. Primary keys must be sequential.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;PostgreSQL&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Heap Storage&lt;/td&gt;&lt;td&gt;Secondary indexes are fast. Heavy updates cause massive write amplification, requiring aggressive Autovacuuming.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Oracle&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Heap Default / Optional IOT&lt;/td&gt;&lt;td&gt;Extreme flexibility, allowing the DBA to choose the physical layout per table based on the workload.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;PostgreSQL&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Executes the query to provide exact physical block reads. Do not run it on a destructive &lt;code&gt;UPDATE&lt;/code&gt; or &lt;code&gt;DELETE&lt;/code&gt; in production without a transaction rollback.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Treating all relational databases identically leads to architectural failure. Updating a row in PostgreSQL triggers massive write amplification across all secondary indexes, while the same update in MySQL is localized and efficient.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Select PostgreSQL for read-heavy, analytical, or geo-spatial workloads where direct heap pointers are advantageous. Select MySQL for extreme high-throughput update workloads to avoid write amplification and replication bloat.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Uber Engineering’s public architectural migration from PostgreSQL to MySQL is a documented, named case where the I/O cost of Postgres’s Heap MVCC updates was one of the stated drivers at global scale — read their original writeup for the full set of reasons before treating this as a universal verdict on Postgres.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your PostgreSQL databases today. If you have highly updated tables with numerous secondary indexes, check your Autovacuum logs and replication lag. If write amplification is saturating your SSDs, consider refactoring the schema to utilize Heap-Only Tuples (HOT) updates, or begin a migration plan to a clustered storage engine like MySQL.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category></item><item><title>G1GC Tuning for Cassandra: What I Actually Look At</title><link>https://rajivonai.com/blog/2025-02-14-cassandra-g1gc-heap-tuning-ergonomics/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-02-14-cassandra-g1gc-heap-tuning-ergonomics/</guid><description>How to tune the Java 11 G1 Garbage Collector for Apache Cassandra 4.x, eliminating humongous allocations, evacuation failures, and stop-the-world pauses.</description><pubDate>Fri, 14 Feb 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Cassandra is notorious for punishing bad garbage collection tuning. Because Cassandra is a heavily concurrent, write-optimized database relying on immutable memtables, it generates massive amounts of short-lived objects. If the JVM fails to collect these efficiently, you get stop-the-world (STW) pauses.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In 2022–2023, the 36-node Cassandra 4.1 cluster running on OpenJDK 11 (r5.2xlarge, 64 GB RAM, 31 GB Heap) was suffering from periodic 800ms to 2,000ms latency spikes.&lt;/p&gt;
&lt;p&gt;These spikes resulted in Cassandra client read timeouts (&lt;code&gt;ReadTimeoutException&lt;/code&gt;), dropped coordinator mutations, and sporadic gossip flapping where nodes temporarily marked each other as &lt;code&gt;Dead&lt;/code&gt; because they failed to respond to heartbeats.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;We examined the &lt;code&gt;/var/log/cassandra/gc.log&lt;/code&gt; and found the classic signatures of G1GC struggling under load. We saw frequent &lt;code&gt;Concurrent Mark&lt;/code&gt; aborts, mixed collections failing with &lt;code&gt;To-space exhausted&lt;/code&gt;, and major spikes in &lt;code&gt;nodetool gcstats&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The Garbage-First (G1) collector works by dividing the heap into regions. Objects are allocated in Eden regions, survive into Survivor regions, and are eventually promoted to Old regions. However, if an object is larger than 50% of a G1 region size, it is classified as a “Humongous Allocation” and bypasses the young generation entirely, allocating directly into the Old generation.&lt;/p&gt;
&lt;p&gt;Because legacy tuning files from older Cassandra architectures had been carried over, the JVM was misconfigured. The JVM was configured with a fixed young generation size (&lt;code&gt;-Xmn&lt;/code&gt;), which forcibly disabled G1’s ability to automatically tune itself (G1 ergonomics). Furthermore, wide CQL partitions were causing humongous allocations, fragmenting the Old generation until it ran out of free space to copy objects during collection, triggering catastrophic &lt;code&gt;To-space exhausted&lt;/code&gt; STW evacuation failures.&lt;/p&gt;
&lt;h2 id=&quot;the-tuning-architecture&quot;&gt;The Tuning Architecture&lt;/h2&gt;
&lt;p&gt;G1GC is designed to be largely self-tuning, provided you don’t cripple its ergonomics and you explicitly define the boundaries of your workload.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[CQL Query Data Allocation] --&gt; Eval{Object Size &gt; 50% of Region?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Eval --&gt;|No: Standard Allocation| Eden[Eden Regions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Eden --&gt;|Minor GC| Surv[Survivor Regions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Surv --&gt;|Promotion| Old[Old Generation Regions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Eval --&gt;|Yes: Humongous Allocation| Humong[Humongous Regions — Directly in Old Gen]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Humong --&gt; Frag[Old Gen Fragmentation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Frag --&gt; Ex[Evacuation Failure — To-space Exhausted]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Ex --&gt; STW[Massive Stop-The-World Pause]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Old --&gt; Mixed[Mixed GC — Concurrent Mark]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-let-g1-control-the-young-generation&quot;&gt;1. Let G1 Control the Young Generation&lt;/h3&gt;
&lt;p&gt;The single biggest mistake engineers make with G1 is explicitly setting &lt;code&gt;-Xmn&lt;/code&gt; or &lt;code&gt;-XX:NewRatio&lt;/code&gt;. If you set a fixed young generation, G1 cannot resize Eden to meet your &lt;code&gt;-XX:MaxGCPauseMillis&lt;/code&gt; target.
&lt;strong&gt;Fix:&lt;/strong&gt; We removed &lt;code&gt;-Xmn&lt;/code&gt;, allowing G1’s ergonomics to dynamically size the young generation based on the pause target.&lt;/p&gt;
&lt;h3 id=&quot;2-defeat-humongous-allocations&quot;&gt;2. Defeat Humongous Allocations&lt;/h3&gt;
&lt;p&gt;On a 31 GB heap, the default G1 region size is usually 16MB (G1’s ergonomics target roughly 2048 regions, and 31744MB ÷ 2048 rounds up to the nearest power of two). Any object over 8MB is humongous. Cassandra reads, especially wide partitions, easily exceed 8MB in memory.
&lt;strong&gt;Fix:&lt;/strong&gt; Region size itself isn’t usually the lever — it’s already sized correctly by G1’s ergonomics for a given heap. The actual fix is upstream: find the CQL queries and partition shapes producing 8MB+ objects (&lt;code&gt;nodetool tablestats&lt;/code&gt; surfaces oversized partitions) and redesign the schema, since no IHOP or region-size tuning survives a data model that keeps generating humongous allocations. IHOP tuning (below) reduces the &lt;em&gt;blast radius&lt;/em&gt; of humongous allocations that do occur — it doesn’t prevent them.&lt;/p&gt;
&lt;h3 id=&quot;3-tuning-the-ihop&quot;&gt;3. Tuning the IHOP&lt;/h3&gt;
&lt;p&gt;G1 triggers a Concurrent Mark (to identify old garbage) when the Old generation reaches the Initiating Heap Occupancy Percent (IHOP). If it triggers too late, G1 runs out of space before it can finish cleaning, causing the dreaded &lt;code&gt;To-space exhausted&lt;/code&gt; pause.
&lt;strong&gt;Fix:&lt;/strong&gt; Java 11’s adaptive IHOP recalculates its trigger point from observed allocation and marking-cycle duration — it’s a reasonable default when object lifetimes are stable. Under the humongous-allocation churn this cluster was seeing, the adaptive estimate lagged reality, so the runbook pins a static baseline of &lt;code&gt;-XX:InitiatingHeapOccupancyPercent=40&lt;/code&gt; (deliberately overriding the adaptive behavior, not layering on top of it) along with &lt;code&gt;-XX:G1ReservePercent=15&lt;/code&gt; to give G1 a larger safety buffer for object copying.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Based on Oracle Java 11 G1GC tuning guidelines and Apache Cassandra production practices, a typical deployment involves the following &lt;code&gt;jvm11-server.options&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# jvm11-server.options&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;-Xms31G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;-Xmx31G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Max 31G keeps JVM Compressed OOPs active. Do NOT use 32G.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;-XX:+UseG1GC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;-XX:G1RSetUpdatingPauseTimePercent&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;=5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;-XX:MaxGCPauseMillis&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;=200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Pin IHOP instead of relying on adaptive estimation, and give G1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# extra reserve space to breathe during mixed collections&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;-XX:InitiatingHeapOccupancyPercent&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;=40&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;-XX:G1ReservePercent&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;=15&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After deploying these settings via a rolling restart, &lt;code&gt;To-space exhausted&lt;/code&gt; errors vanished entirely. The &lt;code&gt;MaxGCPauseMillis=200&lt;/code&gt; target was consistently respected, bringing p99 latencies down to stable bounds.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;MaxGCPauseMillis too low&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Setting &lt;code&gt;-XX:MaxGCPauseMillis=50&lt;/code&gt; forces G1 to aggressively shrink the young generation, causing it to run Minor GCs constantly. CPU burn spikes massively.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Set realistic targets (200ms–500ms). Cassandra is a database, not high-frequency trading software.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Heap &gt; 32GB&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Setting &lt;code&gt;-Xmx32G&lt;/code&gt; or &lt;code&gt;-Xmx64G&lt;/code&gt; disables JVM Compressed OOPs (Ordinary Object Pointers). Pointers double in size from 32-bit to 64-bit, instantly consuming 30-50% more RAM just to track objects.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Strictly limit heap to 31 GB (or 31744m). Rely on off-heap memory and the OS page cache for the remaining system RAM.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Humongous Allocations persist&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;If CQL queries still request 50MB partitions, tuning region size won’t save you.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Fix the data model. Run &lt;code&gt;nodetool tablestats&lt;/code&gt; to find oversized partitions and redesign the schema.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-on-jdk-17&quot;&gt;Modern Cassandra Context: Cassandra 5.0 on JDK 17&lt;/h3&gt;
&lt;p&gt;Cassandra 5.0 officially supports OpenJDK 17. JDK 17 introduces significant G1 enhancements, better adaptive IHOP calculations, and string deduplication improvements. Furthermore, the introduction of Trie Memtables significantly reduces on-heap pressure. Generational ZGC (JEP 439) only became production-ready in JDK 21, so it isn’t an option on JDK 17 — G1GC remains the most battle-tested and predictable collector for massive-scale EC2 deployments on JDK 17, and is worth re-evaluating against Generational ZGC only once the fleet moves to JDK 21+.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Fixed young generations and humongous allocations cause G1 to run out of copy space, resulting in multi-second database freezes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Remove &lt;code&gt;-Xmn&lt;/code&gt;, explicitly tune &lt;code&gt;InitiatingHeapOccupancyPercent&lt;/code&gt;, set a realistic pause target, and cap the heap at 31 GB to preserve Compressed OOPs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Eliminating &lt;code&gt;To-space exhausted&lt;/code&gt; messages from &lt;code&gt;gc.log&lt;/code&gt; stabilizes p99 latency bounds immediately.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Execute &lt;code&gt;jcmd $(pgrep -f CassandraDaemon) GC.heap_info&lt;/code&gt; and verify your current region size, then tail &lt;code&gt;/var/log/cassandra/gc.log&lt;/code&gt; looking for the word &lt;code&gt;humongous&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>My Cassandra Performance Troubleshooting Playbook</title><link>https://rajivonai.com/blog/2025-02-04-cassandra-performance-troubleshooting-playbook/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-02-04-cassandra-performance-troubleshooting-playbook/</guid><description>A deterministic, 7-step playbook for isolating Apache Cassandra latency spikes, distinguishing between coordinator drops, GC pauses, and disk I/O bottlenecks.</description><pubDate>Tue, 04 Feb 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;When a pager goes off at 3:00 AM for “High Cassandra Latency,” the average on-call engineer starts guessing. They restart nodes. They blame the network. They blame the cloud provider.&lt;/p&gt;
&lt;p&gt;Guessing during a database incident destroys page caches, compounds latency, and prolongs the outage. Troubleshooting a distributed system requires a deterministic hierarchy of elimination.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In 2023, the 36-node Cassandra 4.1 cluster began experiencing periodic p99 latency spikes of up to 1,500ms. Client applications were throwing &lt;code&gt;ReadTimeoutException&lt;/code&gt; and &lt;code&gt;WriteTimeoutException&lt;/code&gt;, causing cross-AZ traffic to drop.&lt;/p&gt;
&lt;p&gt;Engineers responding to the incident were overwhelmed by the number of metrics available. They saw CPU spikes on some nodes, disk I/O spikes on others, and thousands of dropped messages in JMX. Without a structured way to interpret the data, they considered restarting the cluster—a catastrophic mistake that would have wiped out the OS page cache and triggered a massive read-from-disk storm.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra is a distributed system where a single query touches multiple servers. When a client application executes a query, it connects to a &lt;strong&gt;Coordinator&lt;/strong&gt; node. That coordinator then forwards the request to the &lt;strong&gt;Replica&lt;/strong&gt; nodes that actually hold the data.&lt;/p&gt;
&lt;p&gt;If a client experiences a timeout, the root cause could be:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The network link between the client and the coordinator.&lt;/li&gt;
&lt;li&gt;The coordinator’s thread pools queueing the request.&lt;/li&gt;
&lt;li&gt;The network link between the coordinator and the replicas.&lt;/li&gt;
&lt;li&gt;A 2-second JVM Garbage Collection (GC) pause on a replica.&lt;/li&gt;
&lt;li&gt;A bad CQL query scanning 10,000 tombstones on disk.&lt;/li&gt;
&lt;li&gt;A saturated EBS volume blocking the read.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You cannot find the root cause by looking at a dashboard. You must isolate the bottleneck by following the query path.&lt;/p&gt;
&lt;h2 id=&quot;the-7-step-troubleshooting-hierarchy&quot;&gt;The 7-Step Troubleshooting Hierarchy&lt;/h2&gt;
&lt;p&gt;We developed a strict triage playbook that forces engineers to evaluate the system from the outside in: Client → Coordinator → ThreadPools → Replicas → JVM → Disk.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Incident[High Latency Alert] --&gt; Step1[1. proxyhistograms: Coordinator Latency]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Step1 --&gt; Step2[2. tablehistograms: Replica Latency]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Step2 --&gt; Step3{Are both high?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Step3 --&gt;|Yes| Step4[3. tpstats: Check Dropped Messages &amp;#x26; Thread Pools]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Step3 --&gt;|Replica Only| Step5[4. Check JVM &amp;#x26; Disk on Replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Step3 --&gt;|Coordinator Only| Step6[Check Network / Speculative Retry]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Step4 --&gt; GC[5. gcstats: Check Stop-the-World Pauses]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Step4 --&gt; CQL[6. tablestats: Tombstones &amp;#x26; SSTables per Read]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Step4 --&gt; IO[7. iostat: Storage EBS Latency]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-coordinator-vs-local-replica-latency&quot;&gt;1. Coordinator vs. Local Replica Latency&lt;/h3&gt;
&lt;p&gt;The first step is determining if the database is slow, or if the network &lt;em&gt;between&lt;/em&gt; the database nodes is slow.
Run &lt;code&gt;nodetool proxyhistograms&lt;/code&gt;. This shows the latency the &lt;strong&gt;coordinator&lt;/strong&gt; experiences while waiting for replicas.
Then run &lt;code&gt;nodetool tablehistograms &amp;#x3C;keyspace&gt; &amp;#x3C;table&gt;&lt;/code&gt;. This shows the latency of the &lt;strong&gt;local replica&lt;/strong&gt; fetching data from its own disk.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;proxyhistograms&lt;/code&gt; is high but &lt;code&gt;tablehistograms&lt;/code&gt; is low, the nodes are fast but network/gossip is degraded, or a single slow replica is poisoning the coordinator.&lt;/li&gt;
&lt;li&gt;If both are high, the nodes themselves are physically struggling to read data.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;2-thread-pools-and-dropped-messages&quot;&gt;2. Thread Pools and Dropped Messages&lt;/h3&gt;
&lt;p&gt;If the nodes are struggling, find out where the queue is backing up. Run &lt;code&gt;nodetool tpstats&lt;/code&gt;.
Look at &lt;code&gt;MutationStage&lt;/code&gt; and &lt;code&gt;ReadStage&lt;/code&gt;. If &lt;code&gt;PendingTasks&lt;/code&gt; is in the thousands, the node is saturated. If you see high &lt;code&gt;DroppedMessages&lt;/code&gt;, Cassandra is executing backpressure—it is purposefully dropping client requests because it cannot physically process them fast enough. This is usually not a network issue; it’s a compute or disk issue.&lt;/p&gt;
&lt;h3 id=&quot;3-tombstones-and-read-amplification&quot;&gt;3. Tombstones and Read Amplification&lt;/h3&gt;
&lt;p&gt;Why is the read stage saturated? The most common cause is a bad CQL query. Look at &lt;code&gt;nodetool tablestats &amp;#x3C;keyspace&gt;&lt;/code&gt;.
If &lt;code&gt;SSTables in each read&lt;/code&gt; is consistently above the low single digits (commonly cited as a &gt;3 warning sign, though the right threshold depends on your compaction strategy and read pattern), you are suffering from read amplification (likely due to compaction falling behind). If the query is hitting thousands of tombstones, the database is spending all its CPU iterating over deleted data.&lt;/p&gt;
&lt;h3 id=&quot;4-gc-pauses-and-disk-io&quot;&gt;4. GC Pauses and Disk I/O&lt;/h3&gt;
&lt;p&gt;If the queries are healthy but the node is locking up, run &lt;code&gt;nodetool gcstats&lt;/code&gt;. If you see GC pauses exceeding 500ms, the JVM is freezing. If GC is healthy, run &lt;code&gt;iostat -xz 1 5&lt;/code&gt; to check the underlying Linux disk. If &lt;code&gt;%util&lt;/code&gt; is 100% and &lt;code&gt;await&lt;/code&gt; is high, your EBS volume is saturated.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;During incidents, the architecture requires engineers to capture this data immediately using a CLI triage script before taking any destructive action.&lt;/p&gt;
&lt;p&gt;In the 1,500ms latency incident, &lt;code&gt;proxyhistograms&lt;/code&gt; were high, but &lt;code&gt;tablehistograms&lt;/code&gt; were normal. &lt;code&gt;nodetool tpstats&lt;/code&gt; showed zero dropped messages on the replicas. The issue wasn’t the database disks or queries—it was a network degradation in a single AWS Availability Zone causing the coordinator to wait for TCP timeouts. Note that &lt;code&gt;cassandra-rackdc.properties&lt;/code&gt; rack/DC assignment is only read at process startup — changing it requires a rolling restart, it isn’t a hot-applied fix. The actual mitigation was stopping the Cassandra process on the degraded AZ’s nodes: once the phi-accrual failure detector marked them &lt;code&gt;DOWN&lt;/code&gt; in gossip, other coordinators stopped routing requests to them within seconds, and latencies recovered well before a topology change or restart would have completed.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Restarting Nodes Prematurely&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Restarting a node destroys the Linux page cache. When the node comes back, all reads must hit physical disk, causing a massive latency spike that masks the original issue.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Never restart a node until you have captured &lt;code&gt;tpstats&lt;/code&gt; and &lt;code&gt;gcstats&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Misinterpreting Dropped Messages&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Engineers often assume &lt;code&gt;DroppedMessages&lt;/code&gt; means packet loss. It actually means the JVM intentionally discarded the request to prevent OOM.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Treat dropped messages as a symptom of disk or CPU exhaustion, not a networking switch failure.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Dynamic Rollbacks&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Making permanent &lt;code&gt;cassandra.yaml&lt;/code&gt; changes during an outage requires restarts.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use JMX to apply dynamic rollbacks: &lt;code&gt;nodetool settimeout&lt;/code&gt; or &lt;code&gt;nodetool setcompactionthroughput&lt;/code&gt; take effect instantly without restarting.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-observability&quot;&gt;Modern Cassandra Context: Cassandra 5.0 Observability&lt;/h3&gt;
&lt;p&gt;Cassandra 5.0 dramatically modernizes this playbook by exposing internal states via virtual tables. Instead of relying entirely on shell scripts and JMX polling, operators can now query &lt;code&gt;system_views.queries&lt;/code&gt; and &lt;code&gt;system_views.thread_pools&lt;/code&gt; directly via CQL. Combined with improved slow query logging, this allows centralized Prometheus exporters to build real-time views of coordinator versus replica latency without requiring SSH access to the instances.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Guessing during a database incident leads to destructive actions (like restarts) that compound latency and destroy caches.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Follow a deterministic triage hierarchy: isolate coordinator vs. replica latency, check thread pool queues, identify bad query shapes, and finally inspect JVM/Disk utilization.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Isolating the difference between &lt;code&gt;proxyhistograms&lt;/code&gt; and &lt;code&gt;tablehistograms&lt;/code&gt; prevents teams from debugging storage volumes when the actual problem is network topology.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Next time you have a latency spike, do not restart the node. SSH in and run &lt;code&gt;nodetool tpstats&lt;/code&gt;. Look at the &lt;code&gt;ReadStage&lt;/code&gt; and &lt;code&gt;MutationStage&lt;/code&gt; pending tasks.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>failures</category><category>checklist</category></item><item><title>Capacity Planning Cassandra: When Do You Add Nodes?</title><link>https://rajivonai.com/blog/2025-01-24-capacity-planning-cassandra-when-to-add-nodes/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-01-24-capacity-planning-cassandra-when-to-add-nodes/</guid><description>Why sizing an Apache Cassandra cluster based on CPU utilization is a trap, and how to calculate physical capacity based on disk headroom, IOPS, and compaction debt.</description><pubDate>Fri, 24 Jan 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In a stateless application tier, scaling is a one-dimensional problem: if CPU utilization crosses 70%, you add another container. If you apply this exact same logic to a stateful, distributed database like Apache Cassandra, you will experience a catastrophic production outage.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A 36-node Cassandra 4.1 cluster housed a 32 TB dataset and pushed 120k write QPS and 45k read QPS. The EC2 instances were hovering around 55% CPU utilization — a number that looks “healthy” if CPU is the only scaling signal being watched, which is exactly the trap this post is about.&lt;/p&gt;
&lt;p&gt;This is a documented Cassandra failure pattern, not a one-off: clusters sized on CPU alone get blindsided by disk. A daily traffic peak triggered a heavy background compaction cycle. Disk utilization on several nodes spiked from 65% to 95%. Cassandra’s own low-disk-space guardrails kicked in and the affected nodes stopped accepting writes to avoid running out of disk mid-flush. With disk headroom gone, the anti-compaction step that repair depends on had nowhere to write new SSTables, so repair jobs stalled and failed outright. The cluster suffered a multi-hour partial outage while the team freed disk space node by node to restore write availability.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra is a log-structured merge-tree (LSM) database. It does not update data in place. It writes immutable files (SSTables) to disk, and then a background process called &lt;strong&gt;compaction&lt;/strong&gt; merges those files together to reclaim space.&lt;/p&gt;
&lt;p&gt;If you are using the default Size-Tiered Compaction Strategy (STCS), when Cassandra merges four 100 GB SSTables, it must write a new 400 GB SSTable to disk &lt;em&gt;before&lt;/em&gt; deleting the original 400 GB of data. This means that at the moment of compaction, &lt;strong&gt;disk space requirements temporarily double&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Sizing a Cassandra cluster based on average steady-state read/write metrics completely ignores:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Anti-entropy repair space amplification.&lt;/li&gt;
&lt;li&gt;The massive disk headroom required for STCS compactions.&lt;/li&gt;
&lt;li&gt;The disk I/O required to stream data when a new node bootstraps.&lt;/li&gt;
&lt;li&gt;Snapshot storage accumulation.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If you wait until disks are 80% full to add nodes, you have already lost. The new nodes will require gigabytes of data to be streamed from the existing nodes, but the existing nodes won’t have the disk I/O or space headroom to safely execute the read streams.&lt;/p&gt;
&lt;h2 id=&quot;the-multi-dimensional-capacity-formula&quot;&gt;The Multi-Dimensional Capacity Formula&lt;/h2&gt;
&lt;p&gt;We abandoned CPU as the primary scaling trigger and established a physical capacity budgeting framework based on storage headroom and disk IOPS.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ScaleDecision{Scale Out?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ScaleDecision --&gt; CPU[CPU &amp;#x3C; 60%]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ScaleDecision --&gt; Thread[Mutation/Read — Queue Depths &amp;#x3C; 100]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ScaleDecision --&gt; Storage[Disk Headroom]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ScaleDecision --&gt; IOPS[EBS Burst Balance / IOPS]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Storage --&gt; STCS[STCS: Max 50% Full]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Storage --&gt; LCS[LCS/TWCS: Max 70% Full]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Storage --&gt; UCS[UCS: Variable Headroom]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    IOPS --&gt; WriteAMP[Write Amplification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    IOPS --&gt; ReadAMP[Read Amplification + Bloom Filter Miss]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;1-storage-headroom-rules&quot;&gt;1. Storage Headroom Rules&lt;/h3&gt;
&lt;p&gt;The runbook establishes strict alerts on disk utilization:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For tables using &lt;strong&gt;STCS&lt;/strong&gt;, an EC2 instance must never exceed &lt;strong&gt;50% disk utilization&lt;/strong&gt; during steady state.&lt;/li&gt;
&lt;li&gt;For tables using &lt;strong&gt;LCS&lt;/strong&gt; or &lt;strong&gt;TWCS&lt;/strong&gt;, it must never exceed &lt;strong&gt;70% disk utilization&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The capacity formula became:
&lt;code&gt;Total Required Storage = Dataset Size × Replication Factor × 1.4 (Headroom) + Snapshots&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The 1.4 multiplier comes directly from the 70% max-utilization rule above (1 ÷ 0.7 ≈ 1.4) — for STCS tables capped at 50% utilization, the equivalent multiplier is 2.0, so size per-table headroom according to whichever compaction strategy that table actually uses.&lt;/p&gt;
&lt;h3 id=&quot;2-iops-budget-per-mutation&quot;&gt;2. IOPS Budget per Mutation&lt;/h3&gt;
&lt;p&gt;Cassandra converts random writes into sequential disk I/O via the commitlog, but compactions convert those writes back into massive sequential read/write operations. If you are using EBS &lt;code&gt;gp3&lt;/code&gt;, you must provision IOPS not for your client write rate, but for your compaction and repair rates. We provisioned dedicated 12,000 IOPS &lt;code&gt;gp3&lt;/code&gt; volumes for data directories to ensure background compactions never stole IOPS from client read requests.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for Cassandra capacity management relies on Prometheus and Grafana forecasting based on &lt;code&gt;nodetool cfstats&lt;/code&gt; and &lt;code&gt;nodetool tablestats&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Instead of alerting on “Disk is 80% full” (which is an incident, not a warning), the pattern builds a derivative alert for &lt;strong&gt;“Days to 60% Disk Capacity.”&lt;/strong&gt; This calculates the daily growth rate of &lt;code&gt;TotalDiskSpaceUsed&lt;/code&gt; and &lt;code&gt;LiveDiskSpaceUsed&lt;/code&gt;, triggering a capacity planning ticket if the cluster is projected to cross the 60% threshold within 30 days. This provides the required lead time to procure hardware, update Terraform, and bootstrap nodes sequentially using zero-copy streaming.&lt;/p&gt;
&lt;p&gt;Because of how Cassandra’s architecture handles ring expansion, adding nodes does not instantly free up disk space on the existing cluster. The documented behavior is that existing nodes will stream their token ranges to the new nodes, but they do not automatically delete the data they no longer own. Operators must manually execute &lt;code&gt;nodetool cleanup&lt;/code&gt; on the original nodes that lost token ranges after the expansion completes to reclaim the un-owned disk space. If a team initiates a scale-out too late, they will lack the disk headroom required to even run this cleanup compaction safely.&lt;/p&gt;
&lt;p&gt;If a cluster unexpectedly breaches the capacity threshold, the documented emergency mitigation pattern is:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Clear Snapshots:&lt;/strong&gt; Run &lt;code&gt;nodetool clearsnapshot&lt;/code&gt; across the cluster to instantly reclaim disk space from old backup files.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Throttle Compaction:&lt;/strong&gt; Execute &lt;code&gt;nodetool setcompactionthroughput&lt;/code&gt; to reduce I/O pressure if CPU I/O wait is starving the read path, buying time for an emergency expansion.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Using CPU as a scaling metric&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;High I/O wait can make CPU look artificially low because JVM threads are blocked waiting on disk.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Monitor &lt;code&gt;iostat -xz 1&lt;/code&gt; to correlate CPU utilization with disk &lt;code&gt;await&lt;/code&gt; times.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Scaling too late&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Adding nodes when disks are &gt; 75% full causes donor nodes to crash during token streaming.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce strict 50-60% disk utilization scaling triggers.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Ignoring Tombstones&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Deletes take up space until &lt;code&gt;gc_grace_seconds&lt;/code&gt; passes. Massive delete workloads will fill your disk before freeing it.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run &lt;code&gt;nodetool tablestats&lt;/code&gt; to monitor tombstone payload sizes.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-and-ucs&quot;&gt;Modern Cassandra Context: Cassandra 5.0 and UCS&lt;/h3&gt;
&lt;p&gt;Cassandra 5.0’s Unified Compaction Strategy (UCS) fundamentally alters this math. By dynamically adapting fanout and sharding, UCS can materially reduce space amplification for appropriate configurations. However, operational disk headroom must still account for workload growth, compaction bursts, bootstrap streaming, repair operations, and pre-upgrade snapshots. Furthermore, if you are leveraging Storage-Attached Indexing (SAI) for vector search, you must calculate the off-heap memory requirements for JVector’s ANN graph structures alongside your physical disk capacity.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Sizing Cassandra clusters based on CPU or allowing disks to exceed 60% capacity results in cascading failures during compaction or repair.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Baseline capacity scaling triggers on strict disk headroom rules (50% for STCS) and IOPS budgets, not CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Enforcing a “Days to 60% Disk” alert gives teams the 30-day runway required to safely stream data to new nodes without starving client I/O.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run &lt;code&gt;nodetool tablestats&lt;/code&gt; on your heaviest keyspace. Look at &lt;code&gt;Space used (live)&lt;/code&gt; versus &lt;code&gt;Space used (total)&lt;/code&gt;. That difference is your current compaction overhead.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>cloud</category><category>databases</category><category>failures</category></item><item><title>Scaling Writes in Amazon Aurora: What Actually Works?</title><link>https://rajivonai.com/blog/2025-01-21-scaling-writes-amazon-aurora-what-works/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-01-21-scaling-writes-amazon-aurora-what-works/</guid><description>Standard Aurora, Serverless v2, and write forwarding still concentrate commits on one writer; this guide compares the architectures that genuinely distribute writes.</description><pubDate>Tue, 21 Jan 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;An Aurora cluster can add readers, scale its writer vertically, forward writes from replicas, and replicate across Regions—yet still send every data modification to one writer. The architecture does not scale writes until it distributes ownership of those writes.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;RetailCo runs its order platform on Aurora PostgreSQL. Product queries, order history, and operational reports have already moved to reader instances. Connection pooling is in place. The busiest SQL statements have been tuned. The writer can still become the limiting resource during peak checkout because orders, line items, inventory reservations, and idempotency records all commit there.&lt;/p&gt;
&lt;p&gt;Its &lt;code&gt;transactions&lt;/code&gt; history has grown to 40 TB. Most customer-facing transactions include a &lt;code&gt;tenant_id&lt;/code&gt; and remain inside one tenant. Finance, fraud, and executive reporting span tenants, but those workloads do not need to execute inside the checkout commit.&lt;/p&gt;
&lt;p&gt;This is a reference architecture, not a claimed customer benchmark. The important facts are the transaction boundaries:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;tenant-local OLTP is the dominant write path;&lt;/li&gt;
&lt;li&gt;cross-tenant work is primarily analytical;&lt;/li&gt;
&lt;li&gt;PostgreSQL compatibility matters;&lt;/li&gt;
&lt;li&gt;AWS-managed operations are preferred;&lt;/li&gt;
&lt;li&gt;recovery and schema compatibility matter as much as throughput.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The standard Aurora topology is excellent for availability and read scale:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App[RetailCo applications] --&gt; Writer[one primary writer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Writer --&gt; Volume[shared Aurora cluster volume]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ReaderA[reader one] --&gt; Volume&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ReaderB[reader two] --&gt; Volume&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ReaderC[reader three] --&gt; Volume&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ReadApi[order history and catalog reads] --&gt; ReaderA&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reports[operational reports] --&gt; ReaderB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Failover[writer failure] --&gt; ReaderC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;AWS documents one primary instance with up to 15 Aurora Replicas. The primary performs all modifications to the cluster volume; replicas support read operations and can be promoted during failover: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-replicas-adding.html&quot;&gt;Adding Aurora Replicas&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;That architecture answers two important questions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;How do we keep serving reads without sending every query to the writer?&lt;/li&gt;
&lt;li&gt;What instance can take over if the writer fails?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;It does not answer a third:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;How do independent compute resources process different writes at the same time?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Aurora offers several features with the word “scaling” or “forwarding” attached to them. They operate at different layers, and confusing those layers leads to expensive designs that leave the original bottleneck intact.&lt;/p&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Aurora capability&lt;/th&gt;&lt;th&gt;What it improves&lt;/th&gt;&lt;th&gt;Where writes commit&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Larger provisioned writer&lt;/td&gt;&lt;td&gt;CPU, memory, and network headroom&lt;/td&gt;&lt;td&gt;The same writer&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aurora Serverless v2&lt;/td&gt;&lt;td&gt;Elastic instance capacity&lt;/td&gt;&lt;td&gt;The writer instance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aurora Replicas&lt;/td&gt;&lt;td&gt;Read capacity and failover targets&lt;/td&gt;&lt;td&gt;The writer instance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Local write forwarding&lt;/td&gt;&lt;td&gt;Connection routing from in-Region replicas&lt;/td&gt;&lt;td&gt;The writer instance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Global write forwarding&lt;/td&gt;&lt;td&gt;Write submission from a secondary Region&lt;/td&gt;&lt;td&gt;The primary Region’s writer&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Multiple application-sharded clusters&lt;/td&gt;&lt;td&gt;Independent tenant or domain ownership&lt;/td&gt;&lt;td&gt;One writer per cluster&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aurora PostgreSQL Limitless Database&lt;/td&gt;&lt;td&gt;Database-managed routers and data shards&lt;/td&gt;&lt;td&gt;The owning shard or coordinated shards&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The first five choices can reduce load, simplify routing, or add elasticity. Only the last two create multiple write-ownership domains.&lt;/p&gt;
&lt;p&gt;This is the Aurora-specific form of the &lt;a href=&quot;https://rajivonai.com/blog/2024-01-23-why-reads-scale-more-easily-than-writes/&quot;&gt;single-writer ceiling&lt;/a&gt;: adding another place to send a request is not the same as adding another place that owns the data being changed.&lt;/p&gt;
&lt;h3 id=&quot;serverless-v2-changes-capacity-not-ownership&quot;&gt;Serverless v2 changes capacity, not ownership&lt;/h3&gt;
&lt;p&gt;Aurora Serverless v2 can scale a writer’s ACUs within a configured range. On supported engine versions and configurations, AWS also supports automatic pause at 0 ACUs: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-serverless-v2-auto-pause.html&quot;&gt;Aurora Serverless v2 auto-pause&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;That makes Serverless v2 useful for elastic capacity and idle-cost control. It does not turn a standard cluster into a multi-writer database. One writer can become larger or smaller; it remains one writer.&lt;/p&gt;
&lt;h3 id=&quot;write-forwarding-changes-the-route-not-the-commit-owner&quot;&gt;Write forwarding changes the route, not the commit owner&lt;/h3&gt;
&lt;p&gt;Local write forwarding allows an application connected to an Aurora Replica to issue a supported write. Aurora forwards that write to the cluster’s writer for commit.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App[application session] --&gt; Reader[Aurora Replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reader --&gt; Read[eligible reads execute on replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reader --&gt; Forward[write is forwarded]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Forward --&gt; Writer[cluster writer commits change]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Writer --&gt; Storage[shared cluster volume]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Storage --&gt; Reader&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;AWS currently documents local forwarding for Aurora PostgreSQL 16.4 and later 16 releases, 15.8 and later 15 releases, and 14.13 and later 14 releases: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-postgresql-write-forwarding.html&quot;&gt;Local write forwarding in Aurora PostgreSQL&lt;/a&gt;. Aurora MySQL supports local forwarding on version 3.04 and later, including its 8.4-compatible versions: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-mysql-write-forwarding.html&quot;&gt;Local write forwarding in Aurora MySQL&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This can simplify application connections and provide read-after-write consistency modes. It can also add forwarding latency, consistency waits, SQL restrictions, and writer sessions. Aurora PostgreSQL local forwarding does not support &lt;code&gt;SERIALIZABLE&lt;/code&gt; isolation and has restrictions involving statements, procedures, and RDS Proxy: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-postgresql-write-forwarding-configuring.html&quot;&gt;PostgreSQL forwarding configuration&lt;/a&gt; and &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-postgresql-write-forwarding-limitations.html&quot;&gt;forwarding limitations&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Global write forwarding follows the same architectural rule. A secondary Region forwards the statement to the primary cluster, the primary changes the data, and the result propagates back: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-global-database-write-forwarding.html&quot;&gt;Aurora Global Database write forwarding&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Write forwarding is therefore a routing feature. It is not horizontal write capacity.&lt;/p&gt;
&lt;h2 id=&quot;four-architectures-that-actually-change-the-outcome&quot;&gt;Four Architectures That Actually Change the Outcome&lt;/h2&gt;
&lt;p&gt;Once monitoring proves the writer is the sustained bottleneck, RetailCo has four credible paths. The right answer depends on whether the problem is temporary capacity, tenant ownership, product compatibility, or an overly broad transaction boundary.&lt;/p&gt;
&lt;h3 id=&quot;option-1--keep-standard-aurora-and-buy-headroom&quot;&gt;Option 1 — Keep standard Aurora and buy headroom&lt;/h3&gt;
&lt;p&gt;The first option is deliberately conservative:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;remove reads still hitting the writer;&lt;/li&gt;
&lt;li&gt;remove unused or duplicate indexes;&lt;/li&gt;
&lt;li&gt;batch small commits where correctness permits;&lt;/li&gt;
&lt;li&gt;reduce lock duration and hot-row contention;&lt;/li&gt;
&lt;li&gt;use an appropriately sized provisioned or Serverless v2 writer;&lt;/li&gt;
&lt;li&gt;move analytical reads to a separate path.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is the right answer when the projected workload still fits a larger writer with acceptable safety margin. It is also the safest first stage while a sharding migration is being designed.&lt;/p&gt;
&lt;p&gt;It is not a long-term scale-out architecture. The maximum practical writer size, storage latency, WAL generation, lock contention, and maintenance impact remain shared constraints.&lt;/p&gt;
&lt;h3 id=&quot;option-2--shard-across-multiple-aurora-clusters&quot;&gt;Option 2 — Shard across multiple Aurora clusters&lt;/h3&gt;
&lt;p&gt;Application sharding gives each tenant group an independent Aurora cluster:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Request[request with tenant identifier] --&gt; Catalog[tenant placement catalog]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Catalog --&gt; ClusterA[Aurora cluster A — tenant group one]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Catalog --&gt; ClusterB[Aurora cluster B — tenant group two]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Catalog --&gt; ClusterC[Aurora cluster C — tenant group three]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ClusterA --&gt; Stream[change stream]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ClusterB --&gt; Stream&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ClusterC --&gt; Stream&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Stream --&gt; Analytics[cross-tenant analytics]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each cluster retains familiar Aurora behavior: one writer, replicas, backups, failover, and its own blast radius. Across clusters, RetailCo now has several independent writers because each cluster owns different tenants.&lt;/p&gt;
&lt;p&gt;This is real horizontal write scaling. It also makes RetailCo responsible for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the tenant placement catalog;&lt;/li&gt;
&lt;li&gt;request routing and cache invalidation;&lt;/li&gt;
&lt;li&gt;tenant movement and shard rebalancing;&lt;/li&gt;
&lt;li&gt;schema deployment across clusters;&lt;/li&gt;
&lt;li&gt;cross-shard queries and transactions;&lt;/li&gt;
&lt;li&gt;fleet-wide backups, PITR, and recovery testing;&lt;/li&gt;
&lt;li&gt;hot-tenant isolation;&lt;/li&gt;
&lt;li&gt;connection-pool growth across destinations.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Application sharding is often the best compatibility option when the workload needs standard Aurora PostgreSQL features that Limitless does not support. It provides control at the price of running a database fleet.&lt;/p&gt;
&lt;h3 id=&quot;option-3--use-aurora-postgresql-limitless-database&quot;&gt;Option 3 — Use Aurora PostgreSQL Limitless Database&lt;/h3&gt;
&lt;p&gt;Aurora PostgreSQL Limitless Database replaces the standard writer-and-readers shape with a DB shard group containing routers and shards. Routers accept connections, locate data, coordinate distributed work, and return results. Shards own subsets of sharded tables and process those writes concurrently: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless-architecture.html&quot;&gt;Limitless architecture&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App[RetailCo PostgreSQL clients] --&gt; Endpoint[Limitless cluster endpoint]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Endpoint --&gt; RouterA[router one]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Endpoint --&gt; RouterB[router two]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  RouterA --&gt; ShardA[shard A — tenant key range]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  RouterA --&gt; ShardB[shard B — tenant key range]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  RouterB --&gt; ShardB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  RouterB --&gt; ShardC[shard C — tenant key range]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reference[reference tables — copied to shards] --&gt; ShardA&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reference --&gt; ShardB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reference --&gt; ShardC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Limitless provides three table types:&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Table type&lt;/th&gt;&lt;th&gt;Placement&lt;/th&gt;&lt;th&gt;RetailCo use&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Sharded&lt;/td&gt;&lt;td&gt;Hash-distributed by a chosen shard key&lt;/td&gt;&lt;td&gt;Orders, order items, tenant transactions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reference&lt;/td&gt;&lt;td&gt;Copied to each shard&lt;/td&gt;&lt;td&gt;Small, infrequently changed shared lookup data&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Standard&lt;/td&gt;&lt;td&gt;Stored on one system-selected shard&lt;/td&gt;&lt;td&gt;Small tables that do not need distributed capacity&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The standard-table detail matters. Moving a schema to Limitless without converting the growing tables to sharded tables does not distribute their write workload. Standard tables remain bounded by one shard.&lt;/p&gt;
&lt;p&gt;For RetailCo, the core schema could make &lt;code&gt;tenant_id&lt;/code&gt; part of every primary key and collocate the tables used by checkout:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LOCAL&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; rds_aurora&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;limitless_create_table_mode&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;sharded&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LOCAL&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; rds_aurora&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;limitless_create_table_shard_key&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;{&quot;tenant_id&quot;}&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    customer_id     &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_status    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;         NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ordered_at      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    total_amount    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;18&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, order_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LOCAL&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; rds_aurora&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;limitless_create_table_collocate_with&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; order_items&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    line_number     &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;integer&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    product_id      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    quantity        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;integer&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    unit_price      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;18&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, order_id, line_number),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    FOREIGN KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, order_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        REFERENCES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders (tenant_id, order_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;COMMIT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The session variables and collocation pattern follow AWS’s documented table-creation model: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless-creating-config.html&quot;&gt;Creating Limitless tables&lt;/a&gt;. AWS also documents that primary and unique keys on sharded tables must include the shard key. Foreign keys between sharded tables require collocation, and the foreign key must include the shard key: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless-reference.DDL-limitations.html&quot;&gt;Limitless DDL limitations&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This is not syntactic trivia. It forces the data model to declare the ownership boundary. An email address cannot be globally unique through &lt;code&gt;UNIQUE (email)&lt;/code&gt; on a table sharded by &lt;code&gt;tenant_id&lt;/code&gt;; the supported unique key must include the shard key. RetailCo must decide whether uniqueness means “inside one tenant” or whether a separate globally coordinated registry is required.&lt;/p&gt;
&lt;h3 id=&quot;option-4--decompose-the-database-write-path&quot;&gt;Option 4 — Decompose the database write path&lt;/h3&gt;
&lt;p&gt;RetailCo may not need one database to own checkout, payments, inventory, reporting, and historical audit data.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Checkout[checkout command] --&gt; Orders[orders write domain]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Checkout --&gt; Inventory[inventory reservation domain]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Checkout --&gt; Payments[payment command domain]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Orders --&gt; Outbox[durable outbox events]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Inventory --&gt; Outbox&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Payments --&gt; Outbox&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Outbox --&gt; History[transaction history and analytics]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This path scales writes by creating domain ownership rather than tenant ownership. It is a good fit when the domains have different availability, retention, contention, and recovery needs.&lt;/p&gt;
&lt;p&gt;The cost moves into the application: idempotency, durable events, compensation, reconciliation, and explicit state transitions. Decomposition is not a shortcut around distributed-systems work. It is a way to place that work at meaningful business boundaries instead of inside every row operation.&lt;/p&gt;
&lt;h3 id=&quot;limitless-is-managed-sharding-not-compatibility-magic&quot;&gt;Limitless is managed sharding, not compatibility magic&lt;/h3&gt;
&lt;p&gt;As of this article’s March 31, 2026 documentation cutoff, AWS release notes list Aurora PostgreSQL Limitless Database &lt;code&gt;16.11-limitless&lt;/code&gt;, including patch &lt;code&gt;16.11.100&lt;/code&gt; released February 25, 2026: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraPostgreSQLReleaseNotes/limitless-updates.html&quot;&gt;Limitless release notes&lt;/a&gt;. Limitless uses special &lt;code&gt;16.X-limitless&lt;/code&gt; engine versions, so compatibility must be validated against the exact Limitless release rather than inferred from standard Aurora PostgreSQL.&lt;/p&gt;
&lt;p&gt;That version discipline is not a formality, because the Limitless compatibility surface has moved quickly and in the direction that matters for this decision. Several restrictions that would have rejected a design at GA were lifted in later patches — advisory locks, &lt;code&gt;CHECK&lt;/code&gt; constraints with expressions, &lt;code&gt;ENUM&lt;/code&gt; shard keys, and &lt;code&gt;pg_dump&lt;/code&gt;/&lt;code&gt;pg_restore&lt;/code&gt; for migrations all arrived in &lt;code&gt;16.10.100&lt;/code&gt; (February 24, 2026), and extensions including &lt;code&gt;hstore&lt;/code&gt;, &lt;code&gt;ltree&lt;/code&gt;, &lt;code&gt;btree_gist&lt;/code&gt;, &lt;code&gt;btree_gin&lt;/code&gt;, &lt;code&gt;auto_explain&lt;/code&gt;, and &lt;code&gt;pg_prewarm&lt;/code&gt; were added across the 16.6 through 16.11 lines. A compatibility matrix built against the 16.4 GA release will reject workloads that the current engine supports.&lt;/p&gt;
&lt;p&gt;The correct operating rule is therefore to date the compatibility inventory and rebuild it against the exact patch the proof of concept will run, rather than to reuse a matrix from an earlier evaluation. Read the release notes forward from the version you last assessed.&lt;/p&gt;
&lt;p&gt;AWS currently documents several boundaries that should enter the decision before a proof of concept:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;I/O-Optimized storage is required;&lt;/li&gt;
&lt;li&gt;shard keys cannot be updated;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SERIALIZABLE&lt;/code&gt; isolation is not supported;&lt;/li&gt;
&lt;li&gt;shards cannot be merged;&lt;/li&gt;
&lt;li&gt;individual shards and routers cannot be deleted;&lt;/li&gt;
&lt;li&gt;RDS Proxy, Aurora Global Database, read replicas, AWS Backup, Blue/Green Deployments, and several other Aurora features are not supported;&lt;/li&gt;
&lt;li&gt;only supported PostgreSQL extensions are available;&lt;/li&gt;
&lt;li&gt;standard tables do not gain distributed capacity.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The full list changes as the service evolves and must be checked against &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless-reqs-limits.html&quot;&gt;Limitless requirements and considerations&lt;/a&gt; for the exact target release.&lt;/p&gt;
&lt;p&gt;The backup detail is equally specific. AWS documents cluster snapshots and point-in-time recovery for Limitless. Its backup guide says not to use &lt;code&gt;pg_dump&lt;/code&gt;, &lt;code&gt;pg_dumpall&lt;/code&gt;, or &lt;code&gt;pg_restore&lt;/code&gt; as the backup mechanism: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless-bak.html&quot;&gt;Backing up and restoring Limitless&lt;/a&gt;. Separately, the &lt;code&gt;16.10-limitless&lt;/code&gt; release notes added &lt;code&gt;pg_dump&lt;/code&gt; and &lt;code&gt;pg_restore&lt;/code&gt; support for &lt;strong&gt;database migrations&lt;/strong&gt;, including metadata preservation when moving between Limitless clusters: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraPostgreSQLReleaseNotes/limitless-updates.html&quot;&gt;Limitless release notes&lt;/a&gt;. That migration support should not be presented as a replacement for cluster snapshots and PITR. AWS Backup being unsupported also does not mean the service has no backup capability; it means the operating workflow differs from a standard Aurora estate integrated with AWS Backup.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS documents a standard Aurora cluster with one primary writer and up to 15 read-only Aurora Replicas. Replicas offload &lt;code&gt;SELECT&lt;/code&gt; traffic and can be promoted during failover: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Concepts.AuroraHighAvailability.html&quot;&gt;Aurora high availability&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Establish the post-offload baseline: measure writer CPU, commit latency, WAL generation, storage latency, lock waits, active sessions, and the reads that still use the writer endpoint.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The review can distinguish a genuine write ceiling from a routing, indexing, or connection-management problem.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Replica count is not evidence of write capacity.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS documents that both local and global write forwarding commit changes on the writer or primary cluster. It also publishes forwarding latency, session, consistency-wait, and error metrics for Aurora PostgreSQL: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-postgresql-write-forwarding-monitoring.html&quot;&gt;Monitoring local write forwarding&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Evaluate forwarding as a connection and consistency feature. Measure &lt;code&gt;AuroraForwardingReplicaDMLLatency&lt;/code&gt;, consistency wait latency, rejected sessions, and additional writer load.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The application can determine whether simpler endpoint use justifies the extra hop and compatibility boundaries.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Forwarding can simplify application routing while leaving—or increasing—writer work.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS documents that Limitless routers coordinate queries and distributed transactions, while shards store subsets of sharded tables. Collocation sends equal shard-key values from related tables to the same shard: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless-architecture.html&quot;&gt;Limitless architecture&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Test tenant-local and cross-shard workloads separately. Include a uniformly distributed tenant workload, a hot tenant, cross-tenant reporting, multi-shard transactions, shard failure, router failure, snapshot restore, and PITR.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The evaluation exposes whether the application’s real transaction shape benefits from distribution or routinely pays for distributed coordination.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; “Limitless” describes the service’s scaling architecture; it does not make shard-key choice or transaction locality irrelevant.&lt;/p&gt;
&lt;h3 id=&quot;proposed-lab-matrix--no-empirical-result-yet&quot;&gt;Proposed lab matrix — no empirical result yet&lt;/h3&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Test&lt;/th&gt;&lt;th&gt;Standard Aurora&lt;/th&gt;&lt;th&gt;Application-sharded Aurora&lt;/th&gt;&lt;th&gt;Aurora PostgreSQL Limitless&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Tenant-local checkout&lt;/td&gt;&lt;td&gt;Establish single-writer baseline&lt;/td&gt;&lt;td&gt;Route tenants across clusters&lt;/td&gt;&lt;td&gt;Route through Limitless endpoint with collocated tables&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hot tenant&lt;/td&gt;&lt;td&gt;Observe writer and lock concentration&lt;/td&gt;&lt;td&gt;Isolate or move tenant&lt;/td&gt;&lt;td&gt;Observe shard skew and split behavior&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cross-tenant transaction&lt;/td&gt;&lt;td&gt;Local but on one writer&lt;/td&gt;&lt;td&gt;Requires application coordination&lt;/td&gt;&lt;td&gt;Requires distributed database coordination&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cross-tenant report&lt;/td&gt;&lt;td&gt;Reader or analytical replica&lt;/td&gt;&lt;td&gt;Fan-out or analytical pipeline&lt;/td&gt;&lt;td&gt;Distributed query through routers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Writer or shard failure&lt;/td&gt;&lt;td&gt;Promote Aurora Replica&lt;/td&gt;&lt;td&gt;Fail over affected cluster&lt;/td&gt;&lt;td&gt;Exercise compute redundancy behavior&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Recovery&lt;/td&gt;&lt;td&gt;Snapshot and PITR&lt;/td&gt;&lt;td&gt;Restore and reconcile one cluster&lt;/td&gt;&lt;td&gt;Restore cluster and recreate DB shard group as documented&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Record TPS, P50, P95, P99, aborts, retries, lock waits, WAL, network, storage latency, ACU utilization, router utilization, shard utilization, and recovery time. Do not compare architectures with different durability settings, datasets, or transaction semantics.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Architecture&lt;/th&gt;&lt;th&gt;Good fit&lt;/th&gt;&lt;th&gt;Main failure mode&lt;/th&gt;&lt;th&gt;Complexity owner&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Standard Aurora with larger writer&lt;/td&gt;&lt;td&gt;Workload still fits one writer with forecast headroom&lt;/td&gt;&lt;td&gt;Reaches the same ceiling later&lt;/td&gt;&lt;td&gt;DBA and cloud platform&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Serverless v2 writer&lt;/td&gt;&lt;td&gt;Variable capacity within one write domain&lt;/td&gt;&lt;td&gt;Rapid or sustained load reaches configured or practical writer capacity&lt;/td&gt;&lt;td&gt;AWS plus capacity policy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Local or global write forwarding&lt;/td&gt;&lt;td&gt;Applications need simpler routing and supported consistency modes&lt;/td&gt;&lt;td&gt;Forwarding latency, unsupported SQL, writer session pressure&lt;/td&gt;&lt;td&gt;Application and DBA&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Multiple Aurora clusters&lt;/td&gt;&lt;td&gt;Stable tenant key and need for standard Aurora compatibility&lt;/td&gt;&lt;td&gt;Routing drift, hot shards, fleet operations, tenant moves&lt;/td&gt;&lt;td&gt;Application and platform&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aurora PostgreSQL Limitless&lt;/td&gt;&lt;td&gt;Tenant-local PostgreSQL workload compatible with its feature set&lt;/td&gt;&lt;td&gt;Poor shard key, distributed transactions, unsupported integrations, one-way topology growth&lt;/td&gt;&lt;td&gt;AWS plus database architecture&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Domain decomposition&lt;/td&gt;&lt;td&gt;Clear business consistency boundaries&lt;/td&gt;&lt;td&gt;Duplicate delivery, partial workflows, reconciliation gaps&lt;/td&gt;&lt;td&gt;Application and platform&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;For RetailCo, five questions can reject an architecture quickly:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Do most OLTP transactions include &lt;code&gt;tenant_id&lt;/code&gt;?&lt;/strong&gt; If not, tenant sharding may distribute tables while leaving transactions cross-shard.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Can primary, unique, and foreign-key rules include the shard key?&lt;/strong&gt; If not, Limitless requires a data-model redesign or a separate global registry.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Does the platform require unsupported Limitless features?&lt;/strong&gt; RDS Proxy, Global Database, AWS Backup integration, a particular extension, or &lt;code&gt;SERIALIZABLE&lt;/code&gt; can change the answer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Can a hot tenant be isolated or subdivided?&lt;/strong&gt; Hash distribution helps across many keys; it does not make one key use several ownership domains automatically.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Can cross-tenant reporting leave the OLTP path?&lt;/strong&gt; If every report fans through transactional shards, write scale may be purchased at the expense of read and network complexity.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Migration is also a first-class constraint. Converting an existing standard table to a Limitless sharded table is synchronous and takes an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock according to AWS’s documented conversion procedure: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless-converting-standard.html&quot;&gt;Converting standard tables&lt;/a&gt;. That is not a credible single-step migration plan for a busy 40-TB table. The design needs staged loading, change capture or a controlled write boundary, validation by tenant, cutover, and rollback.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;For the RetailCo scenario, &lt;strong&gt;Aurora PostgreSQL Limitless Database should be the primary managed proof of concept, with multiple application-sharded Aurora clusters as the compatibility fallback&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;That conclusion depends on the facts supplied by the scenario: PostgreSQL is required, most writes are tenant-local, &lt;code&gt;tenant_id&lt;/code&gt; is present in the critical transaction path, cross-tenant reporting can move out of checkout, and managed operation is preferred.&lt;/p&gt;
&lt;p&gt;The recommended sequence is:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Keep the existing standard Aurora cluster stable while removing remaining avoidable writer work.&lt;/li&gt;
&lt;li&gt;Do not count Serverless v2 or write forwarding as horizontal write scaling; use them only for elasticity or connection-routing needs.&lt;/li&gt;
&lt;li&gt;Build a Limitless compatibility inventory for extensions, isolation levels, keys, foreign keys, RDS Proxy, Global Database, backup tooling, and deployment workflows.&lt;/li&gt;
&lt;li&gt;Create a fresh Limitless cluster on an exact &lt;code&gt;16.X-limitless&lt;/code&gt; patch and model orders and order items as collocated sharded tables using &lt;code&gt;tenant_id&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Benchmark tenant-local, hot-tenant, and cross-shard workloads separately; publish no throughput claim until raw results exist.&lt;/li&gt;
&lt;li&gt;Run shard, router, snapshot, and PITR failure exercises before choosing the migration architecture.&lt;/li&gt;
&lt;li&gt;If a required compatibility boundary fails, shard standard Aurora clusters by &lt;code&gt;tenant_id&lt;/code&gt; and make the tenant placement catalog a production control plane.&lt;/li&gt;
&lt;li&gt;Keep domain decomposition available for payment, inventory, or history workloads whose transaction and recovery contracts differ from orders.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Good fit:&lt;/strong&gt; Aurora PostgreSQL Limitless with a high-cardinality tenant key and predominantly tenant-local transactions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Poor fit:&lt;/strong&gt; workloads requiring routine cross-shard transactions, mutable shard keys, &lt;code&gt;SERIALIZABLE&lt;/code&gt;, unsupported extensions, RDS Proxy, Aurora Global Database, or AWS Backup integration.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Main advantage:&lt;/strong&gt; AWS manages routers, shards, distributed query execution, and much of the capacity layer behind one PostgreSQL-compatible endpoint.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Main limitation:&lt;/strong&gt; the application and schema must still align with a stable shard key and the documented PostgreSQL and Aurora feature boundaries.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Operational complexity:&lt;/strong&gt; lower than building a large application-sharded Aurora fleet, but materially higher than operating one standard Aurora cluster.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Migration complexity:&lt;/strong&gt; high for a live 40-TB system; treat it as a tenant-by-tenant data migration with validation and rollback, not as a table-setting change.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; RetailCo’s reads scale across Aurora Replicas, but every checkout write still commits through one standard Aurora writer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Evaluate Limitless for database-managed tenant sharding, and retain application-sharded Aurora clusters as the fallback when compatibility requirements outweigh managed distribution.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; AWS documentation distinguishes the architectures directly: standard replicas and forwarded writes terminate at one writer, while Limitless shards store subsets of sharded tables and process writes across a DB shard group.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Produce a transaction-locality report and a compatibility matrix before provisioning the lab. If the team cannot show which writes are tenant-local and which required features Limitless lacks, it is not ready to choose the platform.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category></item><item><title>Scaling Cassandra from 27 to 36 Nodes Without Taking Production Down</title><link>https://rajivonai.com/blog/2025-01-17-scaling-cassandra-27-to-36-nodes-zero-copy-streaming/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-01-17-scaling-cassandra-27-to-36-nodes-zero-copy-streaming/</guid><description>How to safely expand an Apache Cassandra cluster horizontally using zero-copy streaming, vnodes, and staged bootstrap limits.</description><pubDate>Fri, 17 Jan 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Adding nodes to a stateless web server fleet is as simple as bumping an Auto-Scaling Group integer. Adding nodes to a stateful, globally distributed database while it continues serving thousands of queries per second requires precision orchestration.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In 2023, the 27-node Apache Cassandra 4.1 cluster (running on OpenJDK 11) reached 70% disk capacity across its 28 TB dataset. During traffic peaks, p99 read latency was doubling. We needed to expand the cluster horizontally to 36 nodes (moving from 9 to 12 nodes per Availability Zone across 3 AZs) without incurring downtime or dropping client mutations.&lt;/p&gt;
&lt;p&gt;Unlike a stateless application tier, we could not simply attach 9 new nodes simultaneously. When a new Cassandra node joins the ring, existing nodes must stream gigabytes of SSTable data to the new arrival to satisfy its token assignments. Un-throttled token streaming would saturate instance network interfaces, starve client read traffic, and exhaust disk IOPS on the donor nodes.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra distributes data using a consistent hashing ring. In modern Cassandra (4.x+), clusters use virtual nodes (vnodes) configured via &lt;code&gt;num_tokens: 16&lt;/code&gt;. When a new node bootstraps, it randomly selects 16 token ranges and calculates which existing nodes own the data for those ranges. It then requests a copy of that data.&lt;/p&gt;
&lt;p&gt;If we provisioned and bootstrapped all 9 new EC2 instances simultaneously, dozens of existing nodes would concurrently open heavy sequential read streams on their disks. This spikes &lt;code&gt;PendingCompactions&lt;/code&gt; on donor nodes, pushes EBS volume utilization past 75%, and triggers streaming timeouts.&lt;/p&gt;
&lt;p&gt;How do you safely extract 10 TB of data from an active 27-node cluster to 9 new nodes without degrading the primary read/write path?&lt;/p&gt;
&lt;h2 id=&quot;staged-zero-copy-expansion&quot;&gt;Staged Zero-Copy Expansion&lt;/h2&gt;
&lt;p&gt;To scale out safely, the pattern architects a staged batch pipeline. We expanded the cluster in waves of 3 nodes (one node per AZ per batch): 27 → 30 → 33 → 36.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Start[Current State: 27 Nodes] --&gt; Batch1[Batch 1: Add 3 Nodes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;Batch Execution (1 Node per AZ)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        AZA[Bootstrap Node 1 in us-east-1a]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        AZB[Bootstrap Node 2 in us-east-1b]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        AZC[Bootstrap Node 3 in us-east-1c]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        AZA --&gt; Stream[Zero-Copy Streaming]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        AZB --&gt; Stream&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        AZC --&gt; Stream&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        Stream --&gt; Join[Nodes Join Ring]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Batch1 --&gt; Gate1{Stabilization Gate: — Pending Tasks &amp;#x3C; 100 — No Dropped Mutations}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Gate1 --&gt;|Pass| Batch2[Batch 2: Add 3 Nodes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Batch2 --&gt; Gate2{Stabilization Gate}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Gate2 --&gt;|Pass| Batch3[Batch 3: Add 3 Nodes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Batch3 --&gt; Cleanup[Sequential `nodetool cleanup`]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We leveraged Cassandra 4.1’s Netty-based Zero-Copy Streaming. When an entire SSTable’s data falls within the requested token range, the donor node doesn’t need to deserialize it into Java objects — it uses Linux &lt;code&gt;sendfile()&lt;/code&gt; to stream the raw bytes directly from the page cache to the network socket, drastically reducing JVM heap pressure and CPU overhead. With 16 vnodes per node, many SSTables only partially overlap any single requested range and fall back to the slower per-partition streaming path, so the realized speedup depends on how cleanly SSTable boundaries align with the ranges being streamed.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for safe token range movement requires explicit network throttling. In &lt;code&gt;cassandra.yaml&lt;/code&gt;, we clamped the outbound streaming throughput to prevent donor nodes from saturating their ENIs (Elastic Network Interfaces) or competing with inter-node SSL encryption overhead for client traffic:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Limit outbound streaming to 200 Megabits/s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;stream_throughput_outbound_megabits_per_sec&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;During the bootstrap of a batch, we actively monitored progress using &lt;code&gt;nodetool netstats&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; nodetool&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; netstats&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Mode:&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; BOOTSTRAPPING&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Not&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; sending&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; any&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; streams.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;Receiving&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 6&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; files,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 15488102400&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; bytes&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; total.&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; Already&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; received&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; files,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3221225472&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; bytes&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; total&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Only after the 3 nodes in a batch had fully joined the ring, and &lt;code&gt;nodetool tpstats&lt;/code&gt; showed &lt;code&gt;PendingTasks&lt;/code&gt; on &lt;code&gt;MutationStage&lt;/code&gt; and &lt;code&gt;ReadStage&lt;/code&gt; had returned to baseline, did we proceed to the next batch.&lt;/p&gt;
&lt;p&gt;Once the ring reached 36 nodes, the original 27 nodes still held data they were no longer responsible for — roughly 25% of their prior load, corresponding to the 9 new nodes added to the 36-node ring. To reclaim that disk space, the playbook executes &lt;code&gt;nodetool cleanup&lt;/code&gt;. Crucially, the playbook runs this &lt;strong&gt;sequentially&lt;/strong&gt; (one node per AZ at a time) during off-peak hours, as cleanup forces a massive sequential disk read-and-rewrite operation that can easily cause disk I/O exhaustion if run concurrently across the cluster.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Batching Size&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Bootstrapping 1 node per AZ is safe but takes longer.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Automate the gating process via a Python JMX polling script (&lt;code&gt;scale_cluster_batch.py&lt;/code&gt;) to reduce human wait time.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Stream Throttling&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;200 Mbps&lt;/code&gt; protects client latency but prolongs the bootstrap window.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Adjust dynamically based on instance network baseline; 10Gbps instances can handle higher limits.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Failed Bootstrap&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;A bootstrapping node crashes midway, leaving dead tokens in gossip.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Do not restart the broken node. Run &lt;code&gt;nodetool removenode&lt;/code&gt; on its Host ID from a healthy node, wipe the broken node’s EBS volume, and reprovision fresh.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-enhancements&quot;&gt;Modern Cassandra Context: Cassandra 5.0 Enhancements&lt;/h3&gt;
&lt;p&gt;Cassandra 5.0 improves the scale-out experience in a few concrete ways. The Unified Compaction Strategy (UCS) adapts its sharding to reduce compaction amplification after massive token range shifts. The introduction of Trie SSTables (BTI format) reduces the memory overhead required during streaming, and TLS 1.3 hardware acceleration on modern AWS Graviton3/4 instances substantially lowers — though doesn’t eliminate — the cryptographic CPU penalty previously associated with encrypted zero-copy transfers.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Adding nodes to Cassandra forces massive data streaming that can overwhelm disks, CPUs, and networks, causing client timeouts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Throttle outbound streaming, use 4.x zero-copy streaming, and batch expansions logically by fault domain.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Staged 3-by-3 expansion prevents donor-node saturation, ensuring p99 read latencies remain stable while gigabytes of data migrate across the network.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before expanding, set &lt;code&gt;stream_throughput_outbound_megabits_per_sec&lt;/code&gt; and script your &lt;code&gt;nodetool netstats&lt;/code&gt; validation gates. Never run &lt;code&gt;nodetool cleanup&lt;/code&gt; on all nodes at once.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>Building a Safe Python Migration Runner for Operational Data Changes</title><link>https://rajivonai.com/blog/2025-01-14-building-a-safe-python-migration-runner-for-operational-data-changes/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-01-14-building-a-safe-python-migration-runner-for-operational-data-changes/</guid><description>A Python migration runner for live operational data needs idempotency guards, dry-run modes, and rollback hooks that schema migrations skip by default.</description><pubDate>Tue, 14 Jan 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The dangerous migration is rarely the one that changes a schema; it is the one that rewrites operational data while the system is still serving traffic.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most teams eventually outgrow ad hoc data fixes.&lt;/p&gt;
&lt;p&gt;At first, a one-off script is reasonable: backfill a nullable column, correct malformed rows, reassign ownership after a product change, repair denormalized state, or move records from an old workflow into a new one. The operator knows the table, runs the script from a laptop or CI job, watches a few logs, and calls it done.&lt;/p&gt;
&lt;p&gt;That works until the data change becomes operational infrastructure.&lt;/p&gt;
&lt;p&gt;The same script now has to run in staging and production. It must survive deploy retries. It must not run twice. It must pause when database latency rises. It must expose progress to the incident channel. It must prove what it plans to touch before it touches it. It must be auditable after the engineer who wrote it has moved on.&lt;/p&gt;
&lt;p&gt;Schema migration tools solve only part of this. Alembic, Django migrations, Rails migrations, and Flyway are good at ordering structural changes. They are less suited to long-running, chunked, resumable operational data changes where the core risk is not DDL correctness but production behavior under load.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure mode is not simply “the script has a bug.”&lt;/p&gt;
&lt;p&gt;The more common failure is that the script has no operating model. It scans too much. It holds locks too long. It retries without idempotency. It mixes deploy logic with data repair logic. It emits logs but no durable checkpoint. It has a &lt;code&gt;--dry-run&lt;/code&gt; flag that exercises a different path from the real run. It assumes rollback means reversing the script, even though the application may already have observed the new state.&lt;/p&gt;
&lt;p&gt;Operational data migrations need different guarantees from normal application jobs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;only one runner can own a migration at a time&lt;/li&gt;
&lt;li&gt;every unit of work can be retried safely&lt;/li&gt;
&lt;li&gt;progress is stored outside process memory&lt;/li&gt;
&lt;li&gt;batches are small enough to bound lock time&lt;/li&gt;
&lt;li&gt;validation runs before, during, and after execution&lt;/li&gt;
&lt;li&gt;operators can pause, resume, and abort without editing code&lt;/li&gt;
&lt;li&gt;CI can test the plan without touching production data&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The core question is: how do we make Python data migrations boring enough to run through the same platform controls as a deployment?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;A safe Python migration runner is a control plane around dangerous work. The migration code still contains domain-specific logic, but the runner owns orchestration, locking, checkpointing, validation, and observability.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[CI job — migration request] --&gt; B[plan builder — validate manifest]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[dry run — estimate rows and batches]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D[approval gate — human or policy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; E[runner — acquire advisory lock]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; F[checkpoint store — record state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; G[batch executor — bounded transaction]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; H[validators — preflight and postflight]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt; I[metrics and logs — progress stream]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; J{more batches}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  J --&gt;|yes| G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  J --&gt;|no| K[complete — release lock]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; L[pause switch — operator control]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  L --&gt;|paused| F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The unit of deployment is a migration package, not a loose script. Each package has a manifest:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;backfill_account_tiers_2026_05_24&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;owner&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;platform-data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;database&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;primary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;mode&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;online&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;batch_size&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;500&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;max_runtime_seconds&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1800&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;requires_approval&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The Python interface should be small:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; Migration&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; plan&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(self, db) -&gt; Plan:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; select_batch&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(self, db, checkpoint) -&gt; list[RowRef]:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; apply_batch&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(self, db, rows) -&gt; BatchResult:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; validate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(self, db) -&gt; ValidationResult:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;        ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The runner calls these methods; migration authors do not implement retries, locks, metrics, or state transitions. That division matters because platform safety depends on consistent behavior across migrations.&lt;/p&gt;
&lt;p&gt;The first guardrail is a durable state machine. A migration moves through &lt;code&gt;planned&lt;/code&gt;, &lt;code&gt;approved&lt;/code&gt;, &lt;code&gt;running&lt;/code&gt;, &lt;code&gt;paused&lt;/code&gt;, &lt;code&gt;failed&lt;/code&gt;, and &lt;code&gt;completed&lt;/code&gt;. Each batch records a checkpoint, row count, checksum if practical, start time, end time, and error. If the process dies, the next run resumes from the last committed checkpoint.&lt;/p&gt;
&lt;p&gt;The second guardrail is database-level ownership. In PostgreSQL, advisory locks are designed for application-defined coordination and are automatically cleaned up at session end or transaction end depending on the lock type. The runner can use a transaction-scoped advisory lock to prevent two workers from running the same migration concurrently without creating a coordination table hot spot. This follows PostgreSQL’s documented advisory lock behavior rather than inventing distributed locking semantics in Python.&lt;/p&gt;
&lt;p&gt;The third guardrail is batch isolation. Each batch runs in its own bounded transaction. That gives the system a chance to pause between batches, reduces lock duration, and makes retries tractable. Long transactions are operationally expensive: they hold locks, delay vacuum progress, and make failures harder to contain. A runner should default to many small commits rather than one heroic commit.&lt;/p&gt;
&lt;p&gt;The fourth guardrail is symmetry between dry run and execution. Dry run should call the same &lt;code&gt;plan&lt;/code&gt; and &lt;code&gt;select_batch&lt;/code&gt; logic, then stop before mutation. It should report estimated row counts, index usage assumptions, batch count, runtime budget, and the exact safety checks that will gate execution. A dry run that only prints “would update rows” is theater.&lt;/p&gt;
&lt;p&gt;The fifth guardrail is an operator contract. Pause means finish the current batch and stop. Abort means stop scheduling new work and mark the migration as failed or canceled. Retry means resume from the checkpoint. Rollback is not a button unless the migration defines a verified compensating action. In many operational data changes, the safer rollback is a forward fix.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; GitLab documents both post-deployment migrations and batched background migrations for database changes that should not be coupled directly to the main deploy path. Its documentation states that batched background migrations are used to update database tables in batches, and that queueing a batched background migration should happen in a post-deployment migration.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The architectural pattern is to separate application rollout, migration scheduling, and migration execution. A Python runner should copy that separation: CI packages and validates the migration, a deploy step registers it, and a worker executes batches under operational controls.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented pattern avoids treating a long-running data rewrite as a single deploy transaction. Operators can inspect migration state, reason about active background work, and keep application rollback concerns separate from data progress. That is the important lesson, not GitLab’s specific Rails implementation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Do not hide operational data changes inside app startup, release hooks, or arbitrary one-off jobs. Make them first-class platform objects with lifecycle, ownership, and status.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; PostgreSQL documents explicit locking and advisory locks as mechanisms with well-defined transaction and session behavior. It also documents that table-level locks conflict differently depending on the operation. This matters because a migration that is “just updating rows” can still create production pressure through lock waits, index churn, and transaction age.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The runner should encode database behavior into policy. It should require indexed batch selectors, set statement and lock timeouts, cap rows per transaction, and fail closed when the query plan is unsafe.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Safety moves from reviewer memory into automation. Reviewers still evaluate business logic, but the runner consistently enforces the mechanical rules that prevent common production incidents.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; A safe migration runner is not a clever script framework. It is a production workload scheduler for database mutations.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Full table scan during batch selection&lt;/td&gt;&lt;td&gt;migration selects by an unindexed predicate&lt;/td&gt;&lt;td&gt;require &lt;code&gt;EXPLAIN&lt;/code&gt; checks and indexed cursor columns&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Duplicate mutation after retry&lt;/td&gt;&lt;td&gt;batch writes are not idempotent&lt;/td&gt;&lt;td&gt;use deterministic row selection and write guards&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Long lock waits&lt;/td&gt;&lt;td&gt;transaction touches too many rows or waits behind traffic&lt;/td&gt;&lt;td&gt;set lock timeout and shrink batch size&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unbounded runtime&lt;/td&gt;&lt;td&gt;runner has no budget or pause point&lt;/td&gt;&lt;td&gt;enforce max runtime and pause between batches&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;False dry run confidence&lt;/td&gt;&lt;td&gt;dry run uses different logic&lt;/td&gt;&lt;td&gt;share plan and selection code with execution&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unsafe rollback expectation&lt;/td&gt;&lt;td&gt;data has already been consumed by live code&lt;/td&gt;&lt;td&gt;require compensating migration or forward fix plan&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Invisible progress&lt;/td&gt;&lt;td&gt;only process logs exist&lt;/td&gt;&lt;td&gt;persist checkpoint and emit metrics per batch&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Operational data changes fail when they are treated as scripts instead of production workflows.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build a Python runner that owns lifecycle, locking, checkpointing, batch execution, validation, and operator controls.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The pattern is consistent with documented systems behavior: GitLab separates post-deployment and batched background migrations, while PostgreSQL provides explicit primitives for lock-aware coordination.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Start with a minimal runner: manifest validation, dry run, advisory lock, checkpoint table, bounded batch transaction, pause flag, and postflight validator. Add policy only after every migration goes through that path.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>automation</category><category>platform</category><category>ci-cd</category></item><item><title>Running Apache Cassandra on EC2: The Production Architecture We Standardized On</title><link>https://rajivonai.com/blog/2025-01-07-running-apache-cassandra-ec2-production-architecture/</link><guid isPermaLink="true">https://rajivonai.com/blog/2025-01-07-running-apache-cassandra-ec2-production-architecture/</guid><description>How to design a multi-AZ Apache Cassandra topology on AWS EC2, mapping physical availability zones to logical racks, with dedicated storage for data and commitlogs.</description><pubDate>Tue, 07 Jan 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Cassandra is rarely the first database a team adopts. It usually enters the picture only when a relational database has been pushed past its write-scaling limits, and the organization demands zero-downtime survival across infrastructure failures.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;In 2022, we were migrating a critical, high-write-volume application onto Apache Cassandra 4.0 (running on OpenJDK 11). The goal was resilience and horizontal scalability across AWS. However, an ad-hoc EC2 provisioning approach by early teams had led to severe operational instability.&lt;/p&gt;
&lt;p&gt;We observed asymmetric write latencies across nodes, sporadic gossip drops during network bursts, and uneven disk usage across Availability Zones (AZs). Cassandra’s consistent hashing requires physical infrastructure to align perfectly with its logical awareness of failure domains. When it doesn’t, the database is forced to compensate.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cassandra assumes the operator has correctly mapped physical fault domains to its logical awareness. If an EC2 cluster is deployed blindly into a VPC without mapping AWS AZs to Cassandra racks, the &lt;code&gt;SimpleStrategy&lt;/code&gt; replication model (or misconfigured &lt;code&gt;NetworkTopologyStrategy&lt;/code&gt;) will distribute replicas arbitrarily.&lt;/p&gt;
&lt;p&gt;If AWS &lt;code&gt;us-east-1a&lt;/code&gt; suffers a power failure or network partition, and Cassandra unknowingly placed all three replicas of a partition in that single AZ, data becomes unavailable. Furthermore, co-locating the Cassandra &lt;code&gt;commitlog&lt;/code&gt; and &lt;code&gt;data&lt;/code&gt; directories on a shared OS disk—or worse, a burstable &lt;code&gt;gp2&lt;/code&gt; volume—forces sequential write-ahead logs to compete for IOPS with random-read SSTable fetches and heavy compaction workloads.&lt;/p&gt;
&lt;p&gt;How do you design a foundational Cassandra architecture on EC2 that guarantees data survival across a complete AZ failure while preventing storage I/O starvation?&lt;/p&gt;
&lt;h2 id=&quot;the-3-az-ec2-storage-topology&quot;&gt;The 3-AZ EC2 Storage Topology&lt;/h2&gt;
&lt;p&gt;The solution is a strict architectural standard: explicit mapping of 3 AWS Availability Zones to 3 Cassandra logical racks, utilizing the &lt;code&gt;GossipingPropertyFileSnitch&lt;/code&gt;, and hard physical separation of storage volumes.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Client[Application Client] --&gt;|LOCAL_QUORUM| Coord(Cassandra Coordinator Node)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph &quot;AWS Region (e.g., us-east-1)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        subgraph &quot;AZ A (Cassandra Rack: rack1)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            N1[Node 1 — Seed]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            N2[Node 2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            N3[Node 3]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        subgraph &quot;AZ B (Cassandra Rack: rack2)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            N4[Node 4 — Seed]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            N5[Node 5]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            N6[Node 6]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        subgraph &quot;AZ C (Cassandra Rack: rack3)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            N7[Node 7 — Seed]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            N8[Node 8]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            N9[Node 9]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord -.-&gt; N2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord -.-&gt; N4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Coord -.-&gt; N8&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    %% Storage Detail for Node 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    N1 --&gt; OS[(EBS gp3 — OS / Logs)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    N1 --&gt; CL[(EBS gp3 — Commitlog — 3000 IOPS)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    N1 --&gt; DATA[(EBS gp3 — SSTable Data — 12000 IOPS)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;storage-separation&quot;&gt;Storage Separation&lt;/h3&gt;
&lt;p&gt;Cassandra’s write path is heavily dependent on the sequential speed of the commitlog. We mandated three distinct EBS &lt;code&gt;gp3&lt;/code&gt; volumes per EC2 instance:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;OS / Binaries (&lt;code&gt;/dev/sda1&lt;/code&gt;)&lt;/strong&gt;: For the operating system and &lt;code&gt;system.log&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Commitlog (&lt;code&gt;/var/lib/cassandra/commitlog&lt;/code&gt;)&lt;/strong&gt;: Dedicated volume provisioned for high sequential throughput and 3,000 IOPS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data (&lt;code&gt;/var/lib/cassandra/data&lt;/code&gt;)&lt;/strong&gt;: Dedicated volume for SSTables, provisioned at 12,000 IOPS and 500 MB/s throughput to absorb compaction spikes and random reads without starving the write path.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;This architecture relies on strict adherence to the &lt;code&gt;GossipingPropertyFileSnitch&lt;/code&gt;. The documented pattern is to define the exact datacenter and rack in &lt;code&gt;cassandra-rackdc.properties&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;ini&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# cassandra-rackdc.properties on Node 1 (us-east-1a)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;dc&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=us-east-1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;rack&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=rack1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Keyspaces are then created using &lt;code&gt;NetworkTopologyStrategy&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; KEYSPACE production_data &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; replication &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;class&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;NetworkTopologyStrategy&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;us-east-1&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Because the replica count (&lt;code&gt;3&lt;/code&gt;) matches the number of configured racks (&lt;code&gt;rack1&lt;/code&gt;, &lt;code&gt;rack2&lt;/code&gt;, &lt;code&gt;rack3&lt;/code&gt;), Cassandra guarantees that one replica is placed in each AWS Availability Zone. If &lt;code&gt;us-east-1b&lt;/code&gt; (&lt;code&gt;rack2&lt;/code&gt;) goes entirely offline, queries executed at &lt;code&gt;LOCAL_QUORUM&lt;/code&gt; will still succeed using the replicas in &lt;code&gt;rack1&lt;/code&gt; and &lt;code&gt;rack3&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;seed-node-discipline&quot;&gt;Seed Node Discipline&lt;/h3&gt;
&lt;p&gt;A standard pattern configures exactly one seed node per rack (AZ). Placing all seeds in a single AZ is a common failure mode; if that AZ degrades, new nodes cannot bootstrap and join the ring, and existing nodes struggle to reconcile cluster state. Seed nodes must be distributed across the fault domains, but not placed behind an AWS Elastic Load Balancer (ELB), as Cassandra nodes must communicate via direct IP routing.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Design Choice&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Tradeoff&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;EBS gp3 vs. NVMe (i3en/i4i)&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;EBS introduces network dependency for storage. High compaction loads can exhaust EBS bandwidth limits on smaller EC2 instances.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use instances with adequate EBS-optimized bandwidth. Monitor EBS Burst Balance and IOPS utilization heavily. For latency-critical workloads, NVMe instances (&lt;code&gt;i4i&lt;/code&gt;) are required, but they trade durability for speed, requiring stricter backup strategies.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Misconfigured Snitch&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;If an operator uses &lt;code&gt;SimpleStrategy&lt;/code&gt; instead of &lt;code&gt;NetworkTopologyStrategy&lt;/code&gt;, Cassandra ignores racks and AZs.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Enforce keyspace schemas via CI/CD. Monitor &lt;code&gt;nodetool describecluster&lt;/code&gt; to ensure AZ mapping is active.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;&lt;strong&gt;Seed Node Overload&lt;/strong&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Making every node a seed node severely disrupts the gossip protocol and increases network chatter.&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Strictly limit seeds to 1–2 nodes per rack.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;modern-cassandra-context-cassandra-50-on-ec2&quot;&gt;Modern Cassandra Context: Cassandra 5.0 on EC2&lt;/h3&gt;
&lt;p&gt;Cassandra 5.0 (running on JDK 17) introduces the CIDR filtering authorizer (CEP-33), which restricts client access by IP address range using CIDR notation, allowing operators to enforce network-level access boundaries directly at the database layer. Additionally, the shift toward AWS Graviton (&lt;code&gt;c7g&lt;/code&gt;/&lt;code&gt;r7g&lt;/code&gt;) processors has proven highly cost-effective, though organizations must account for the off-heap memory footprints of the new Storage-Attached Indexing (SAI) when sizing EC2 instances for vector workloads.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Ad-hoc provisioning leads to unbalanced, fragile clusters that fail during single-AZ outages.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Standardize on &lt;code&gt;NetworkTopologyStrategy&lt;/code&gt;, 3-AZ distribution, and strict separation of EBS volumes for the OS, commitlogs, and data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Distributing seeds across racks and isolating I/O ensures steady &lt;code&gt;LOCAL_QUORUM&lt;/code&gt; performance and survival of AZ loss.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your cluster using &lt;code&gt;nodetool status&lt;/code&gt; and &lt;code&gt;nodetool ring&lt;/code&gt; to verify that replicas are evenly distributed across physical hardware fault domains. Validate storage separation with &lt;code&gt;lsblk -t&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>Google Spanner for PostgreSQL DBAs: The Production Mental Model</title><link>https://rajivonai.com/blog/2024-12-15-google-spanner-for-postgresql-dbas/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-12-15-google-spanner-for-postgresql-dbas/</guid><description>A production guide to Spanner&apos;s PostgreSQL interface, distributed transactions, key design, PGAdapter, and operational limits.</description><pubDate>Sun, 15 Dec 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Spanner’s PostgreSQL interface reduces language and driver friction; it does not turn Spanner into PostgreSQL. A syntactically successful migration can still fail on key distribution, transaction contention, unsupported features, and missing retry logic.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A workload needs horizontal write scaling or strong transactions across data that cannot remain on one PostgreSQL primary. Spanner is evaluated because it distributes data and compute while offering a PostgreSQL dialect and compatibility with supported PostgreSQL clients through PGAdapter.&lt;/p&gt;
&lt;p&gt;That interface is useful, but the storage engine, transaction manager, administration surface, and observability remain Spanner. The migration is a data-model and application-semantics project, not a binary upgrade.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;PostgreSQL instincts can point in the wrong direction. There is no host buffer cache to tune, no PostgreSQL WAL archive to manage, and no complete &lt;code&gt;pg_catalog&lt;/code&gt; or extension ecosystem. Yet it is equally wrong to say that TrueTime “replaces locks.” Spanner read-write transactions can use locks, and its documented concurrency behavior includes wound-wait deadlock prevention. TrueTime supplies bounded time uncertainty used in commit ordering and external consistency; it does not remove contention.&lt;/p&gt;
&lt;p&gt;The core question is: &lt;strong&gt;which PostgreSQL assumptions survive the interface, and which must be redesigned for a distributed database?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-spanner-mental-model&quot;&gt;The Spanner Mental Model&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Driver[Supported PostgreSQL driver] --&gt; PGAdapter[PGAdapter]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PGAdapter --&gt; API[Spanner API]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    API --&gt; SplitA[Key-range split A]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    API --&gt; SplitB[Key-range split B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SplitA --&gt; ReplicaA[Replicated storage group]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SplitB --&gt; ReplicaB[Replicated storage group]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Clock[TrueTime] --&gt; API&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;PGAdapter translates the PostgreSQL wire protocol to the Spanner API. It can run as a sidecar or, for Java, in-process. The PostgreSQL interface implements a documented subset of PostgreSQL syntax, types, functions, catalogs, drivers, and ORMs. Extensions, user-defined types, user-defined stored procedures, and some tools are not supported. Administration remains a Google Cloud and Spanner control-plane task; &lt;code&gt;pgAdmin&lt;/code&gt; is not the service console.&lt;/p&gt;
&lt;p&gt;Spanner partitions data into key ranges called splits and replicates them. A sequential leading key can concentrate new writes at one end of the keyspace. Avoiding hotspots may require UUIDs, bit-reversed sequences, or a schema-specific distributed key—not a blanket rule to hash every key, because locality can also be valuable.&lt;/p&gt;
&lt;p&gt;Read-write transactions can span rows, tables, and splits. That is the feature teams are paying for, but distributed coordination and contention still affect latency. Client libraries can retry aborted transactions when work is supplied through the transaction runner; custom transaction handling must preserve idempotency and respect retry semantics.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Start migration assessment with incompatibility, not throughput:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Inventory extensions, triggers, stored procedures, sequences, isolation assumptions, ORM features, and administrative tooling.&lt;/li&gt;
&lt;li&gt;Extract the highest-write key ranges and use Key Visualizer or workload tests to expose concentration.&lt;/li&gt;
&lt;li&gt;Identify transactions that touch many rows or splits and determine whether their atomic boundary is essential.&lt;/li&gt;
&lt;li&gt;Test supported drivers through PGAdapter, including prepared statements, pooling, authentication, and error handling.&lt;/li&gt;
&lt;li&gt;Replace PostgreSQL-specific monitoring with Spanner query statistics, transaction statistics, lock statistics, and Key Visualizer.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Transaction limits require nuance. Spanner documents a 100 MiB mutation-commit size limit. The 80,000 mutation limit applies to commits through the mutation API; DML has a limit of 80,000 mutations per statement. A transaction can still become operationally poor before reaching a documented maximum because it holds resources, increases contention, and expands retry work. Batch large maintenance jobs and make progress resumable.&lt;/p&gt;
&lt;p&gt;Schema changes are managed by Spanner and can run as long-running operations. Do not infer that every DDL statement is instant or transactional with application writes. Review the current PostgreSQL-dialect DDL support and test migration tooling against a representative schema.&lt;/p&gt;
&lt;p&gt;The go-live gate should include a contention experiment, not only a throughput test. Drive concurrent updates at one deliberately hot key and at the proposed production key distribution. Compare abort rate, retry count, commit latency, and total work completed. Then inject a retryable abort into the client path and confirm that the transaction body is safe to execute again. A driver that retries transport calls is not sufficient if the surrounding application also emits a message or invokes an external service.&lt;/p&gt;
&lt;p&gt;Finally, test locality assumptions with production-shaped keys. A random identifier can distribute writes while making range scans or parent-child access more expensive. Key design is an optimization problem across distribution and locality, so the architecture record should include both the hotspot evidence and the access patterns the chosen key preserves.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Evidence&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Hot leading key&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Key Visualizer shows concentrated access&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Redesign the leading key while preserving required locality.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Repeated &lt;code&gt;ABORTED&lt;/code&gt; transactions&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Transaction and lock statistics show contention&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Shorten transactions, order access consistently, and use supported retry runners.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Migration compiles but semantics differ&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Unsupported-language and known-issues review&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Build a feature inventory and semantic test suite before data movement.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Large write transaction fails or retries expensively&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Mutation count, commit size, duration&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Batch work with restartable checkpoints and bounded transactions.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Familiar DBA tooling is blind&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Missing PostgreSQL views or control functions&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Adopt Spanner-specific monitoring and incident runbooks.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; PostgreSQL compatibility is being mistaken for PostgreSQL architecture and administration.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Treat the interface as a migration aid over Spanner’s distributed transaction and storage system.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Google documents PGAdapter translation, partial language support, split-based storage, locks, wound-wait behavior, and explicit transaction limits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run a schema-and-transaction compatibility review, then load-test key distribution and retry behavior before selecting Spanner.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/spanner/docs/postgresql-interface&quot;&gt;PostgreSQL interface for Spanner&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/spanner/docs/pgadapter&quot;&gt;PGAdapter overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/spanner/docs/transactions&quot;&gt;Spanner transactions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/spanner/docs/whitepapers/life-of-reads-and-writes&quot;&gt;Life of Spanner reads and writes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/spanner/quotas&quot;&gt;Spanner quotas and limits&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/spanner/docs/schema-design&quot;&gt;Schema design best practices&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/spanner/docs/key-visualizer&quot;&gt;Key Visualizer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>cloud</category><category>architecture</category><category>databases</category><category>failures</category><category>checklist</category></item><item><title>The 2027 Cloud Database Architecture Roadmap</title><link>https://rajivonai.com/blog/2024-12-11-the-2027-cloud-database-architecture-roadmap/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-12-11-the-2027-cloud-database-architecture-roadmap/</guid><description>A 2027 cloud database architecture roadmap for teams that can no longer satisfy consistency, latency, residency, and recovery SLOs with a single engine.</description><pubDate>Wed, 11 Dec 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The next cloud database failure will not come from picking the wrong engine; it will come from pretending one engine can carry every consistency model, latency budget, residency rule, and recovery objective the business now depends on.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Cloud databases have moved from managed infrastructure to application architecture. The old decision was simple: choose Postgres, MySQL, DynamoDB, Spanner, Cassandra, Redis, or a warehouse, then make the application conform to the database. That worked when the product had one dominant workload and one dominant failure mode.&lt;/p&gt;
&lt;p&gt;By 2027, the database layer is no longer a single backing service. It is a fleet: regional OLTP, globally consistent ledgers, event logs, search indexes, vector retrieval, analytical replicas, tenant archives, and policy-aware data products. The operational boundary has shifted from “is the database up?” to “does the system still preserve the correct contract when part of the data plane is stale, relocated, throttled, replayed, or isolated?”&lt;/p&gt;
&lt;p&gt;The staff-level roadmap is therefore not a vendor matrix. It is a control-plane problem. Teams need to define which data must be strongly ordered, which data may be asynchronous, which data must stay in a geography, which data can be regenerated, and which data must remain queryable during a regional event.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Most database incidents are contract incidents disguised as capacity incidents.&lt;/p&gt;
&lt;p&gt;A write path is scaled horizontally, but the uniqueness guarantee still depends on a single regional primary. A read replica is added for latency, but a workflow quietly assumes read-your-writes behavior. A cache absorbs load, but the invalidation path becomes the real system of record during a failover. A vector index is introduced for retrieval, but nobody defines how embedding freshness relates to transactional truth. A data residency policy is implemented at the network layer, while asynchronous jobs still copy customer records into a global queue.&lt;/p&gt;
&lt;p&gt;These failures are rarely caused by ignorance. They are caused by architecture that does not name its database contracts explicitly. The application says “save order.” The database architecture silently decides ordering, durability, idempotency, placement, indexing, and recovery.&lt;/p&gt;
&lt;p&gt;The 2027 question is not “Which cloud database should we standardize on?” It is: &lt;strong&gt;which data contracts deserve first-class architecture, and which engines should be assigned only after those contracts are visible?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;The answer is a contract-first database platform: a small number of explicitly governed persistence patterns, each with a named consistency model, failure mode, and recovery procedure.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[product workflow — user intent] --&gt; B[contract classifier — data criticality]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[ledger store — strict ordering]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; D[regional OLTP — low latency writes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; E[event log — replayable facts]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; F[derived indexes — search and retrieval]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; G[analytical plane — historical queries]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; H[policy engine — residency and retention]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt; I[control plane — placement and recovery]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; J[verification suite — failover drills]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; K[observability — contract metrics]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This roadmap has five architectural moves.&lt;/p&gt;
&lt;p&gt;First, classify data before selecting engines. Ledgers, inventory reservations, financial balances, identity state, entitlement decisions, and audit trails are not generic rows. They require explicit ordering, idempotency keys, reconciliation flows, and restore tests. Product metadata, recommendations, notifications, activity feeds, and search documents can often tolerate asynchronous propagation if the user contract is clear.&lt;/p&gt;
&lt;p&gt;Second, split systems of record from systems of interaction. The system of record preserves facts. The system of interaction optimizes reads, search, ranking, and locality. Treating an index, cache, or embedding store as authoritative creates silent correctness debt.&lt;/p&gt;
&lt;p&gt;Third, make geography part of the schema. Region, tenant, retention class, and residency boundary should be visible in data modeling and routing. If placement is only a Terraform concern, the application will eventually leak data across an unintended path.&lt;/p&gt;
&lt;p&gt;Fourth, make recovery a queryable property. Every persistence pattern should declare restore point objective, restore time objective, replay source, backfill procedure, and validation query. A backup that cannot prove semantic recovery is storage, not resilience.&lt;/p&gt;
&lt;p&gt;Fifth, centralize database policy without centralizing every database. A platform team should own paved-road contracts, reference implementations, test harnesses, and operational scorecards. Application teams should still choose the simplest approved pattern that satisfies their workflow:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Strict global order&lt;/strong&gt;: Distributed SQL for externally consistent transactions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Regional low latency&lt;/strong&gt;: Regional relational primary with local replicas.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Massive key access&lt;/strong&gt;: Partitioned key-value store for predictable throughput.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replayable integration&lt;/strong&gt;: Event log for a durable append stream.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Semantic retrieval&lt;/strong&gt;: Index store for derived embeddings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Historical analysis&lt;/strong&gt;: Warehouse or lakehouse for batch and streaming ingest.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; The documented pattern in Amazon Aurora is that cloud-native relational systems can move substantial storage responsibility out of the database host and into a distributed storage layer. The Aurora paper describes a design where the database instance ships redo records to storage nodes instead of performing the full page-oriented storage work on the compute node: &lt;a href=&quot;https://www.amazon.science/publications/amazon-aurora-design-considerations-for-high-throughput-cloud-native-relational-databases&quot;&gt;Amazon Aurora design considerations&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The architectural action is to stop treating compute and storage as one scaling unit. For 2027 systems, the roadmap should separate write admission, transaction execution, log durability, page reconstruction, backup, and read scaling as distinct design surfaces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented result is not “Aurora fits every workload.” The result is narrower and more useful: separating database compute from distributed storage changes the bottleneck map. Network write amplification, recovery behavior, replica lag, and storage quorum health become first-order operational signals.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The pattern is that managed relational databases are no longer just hosted VMs. They are distributed systems with relational interfaces. Teams that operate them as single-node databases will miss the failure modes that matter.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Google Spanner documents a different contract: externally consistent transactions using TrueTime and replicated consensus. The public documentation describes external consistency as the strongest transaction ordering guarantee Spanner exposes when using serializable isolation: &lt;a href=&quot;https://cloud.google.com/spanner/docs/true-time-external-consistency&quot;&gt;Spanner TrueTime and external consistency&lt;/a&gt;. The original OSDI paper explains the globally distributed design: &lt;a href=&quot;https://research.google.com/archive/spanner-osdi2012.pdf&quot;&gt;Spanner paper&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The architectural action is to reserve globally ordered databases for workflows that truly need global ordering. Use them for ledgers, entitlement changes, cross-region inventory, and other facts where “which write happened first” is part of correctness.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented pattern is that global consistency has an explicit coordination cost. The roadmap should therefore avoid putting every user preference, page view, notification, and recommendation write into the same globally ordered path.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Strong consistency is a product contract, not a prestige feature. If the product does not need the contract, the architecture should not pay for it on every request.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Amazon DynamoDB documents a partitioned, fully managed key-value architecture built for predictable performance at scale: &lt;a href=&quot;https://www.amazon.science/publications/amazon-dynamodb-a-scalable-predictably-performant-and-fully-managed-nosql-database-service&quot;&gt;Amazon DynamoDB paper&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The architectural action is to design access patterns before table shape. High-scale key-value systems reward known query paths, bounded item sizes, explicit partition keys, and deliberate secondary indexes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented pattern is that predictable performance comes from constraining the data model around access. Teams that expect ad hoc relational query flexibility from a key-value store usually move complexity into application code, backfills, and secondary indexing pipelines.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The database roadmap should not ask one store to be both the high-throughput serving path and the exploratory query surface. Serve hot paths from constrained models; analyze history elsewhere.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; CockroachDB documents multi-region abstractions and transaction behavior for distributed SQL, including region-aware capabilities and serializable transaction semantics: &lt;a href=&quot;https://www.cockroachlabs.com/docs/stable/multiregion-overview&quot;&gt;CockroachDB multi-region overview&lt;/a&gt; and &lt;a href=&quot;https://www.cockroachlabs.com/docs/stable/architecture/transaction-layer&quot;&gt;transaction layer&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The architectural action is to model locality and contention together. A globally distributed table with hot transactional rows is not equivalent to a region-local table with replicated reference data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented pattern is that multi-region design is a schema and workload problem, not only a cluster topology problem.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Geography belongs in architecture reviews before launch, not in incident response after latency and residency collide.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Roadmap choice&lt;/th&gt;&lt;th&gt;What improves&lt;/th&gt;&lt;th&gt;Where it breaks&lt;/th&gt;&lt;th&gt;Verification step&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Contract-first persistence&lt;/td&gt;&lt;td&gt;Clear ownership of consistency and recovery&lt;/td&gt;&lt;td&gt;Slower upfront design&lt;/td&gt;&lt;td&gt;Review every critical workflow for ordering, idempotency, and replay&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Distributed SQL for global facts&lt;/td&gt;&lt;td&gt;Stronger cross-region correctness&lt;/td&gt;&lt;td&gt;Coordination latency and transaction retries&lt;/td&gt;&lt;td&gt;Run contention tests from every active region&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Regional OLTP by default&lt;/td&gt;&lt;td&gt;Lower write latency and simpler operations&lt;/td&gt;&lt;td&gt;Cross-region workflows need explicit reconciliation&lt;/td&gt;&lt;td&gt;Test regional isolation and delayed replication&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Event log for integration&lt;/td&gt;&lt;td&gt;Replayable downstream state&lt;/td&gt;&lt;td&gt;Consumers may treat events as current truth&lt;/td&gt;&lt;td&gt;Compare materialized views against source facts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Derived search and vector indexes&lt;/td&gt;&lt;td&gt;Fast retrieval and ranking&lt;/td&gt;&lt;td&gt;Staleness becomes user-visible&lt;/td&gt;&lt;td&gt;Track freshness lag as a product metric&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Central database platform&lt;/td&gt;&lt;td&gt;Fewer unsafe one-off patterns&lt;/td&gt;&lt;td&gt;Platform can become a bottleneck&lt;/td&gt;&lt;td&gt;Publish approved contracts with self-service templates&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Your database architecture probably names engines more clearly than it names contracts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build a persistence catalog with approved patterns for ledgers, regional OLTP, event streams, derived indexes, analytical stores, and archives.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; For each pattern, require a failover drill, restore drill, replay drill, and consistency test that a product engineer can understand.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before adding the next database, write the contract first: ordering, freshness, placement, recovery, ownership, and the query that proves the system is correct after failure.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>Python Database Maintenance Jobs: Safety Checks, Locks, Batches, and Rollback</title><link>https://rajivonai.com/blog/2024-12-10-python-database-maintenance-jobs-safety-checks-locks-batches-and-rollback/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-12-10-python-database-maintenance-jobs-safety-checks-locks-batches-and-rollback/</guid><description>Python database maintenance jobs that skip lock checks, batch limits, and replication lag awareness will corrupt data or starve live queries under load.</description><pubDate>Tue, 10 Dec 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The dangerous part of a database maintenance job is not the Python loop. It is the moment the loop starts believing the database is passive infrastructure instead of a living system with locks, replication lag, failed deploys, and users already depending on it.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Every mature platform eventually accumulates database maintenance work that does not fit cleanly into request paths or schema migrations.&lt;/p&gt;
&lt;p&gt;Old rows need archival. Large tables need backfills. Tenant metadata needs repair. Derived columns need recomputation. Invalid states need cleanup after a bug fix. Indexes, constraints, and materialized summaries need coordinated rollout. Python is often the natural tool: it has the application models, the operational libraries, the feature flag client, the observability stack, and the engineers who understand the business rules.&lt;/p&gt;
&lt;p&gt;That convenience is why Python maintenance jobs become dangerous.&lt;/p&gt;
&lt;p&gt;A script that works on staging can still take an exclusive lock in production. A batch that updates 1,000 rows at a time can still overwhelm replicas if each row fans out into triggers or index churn. A retry loop can turn a partial outage into a full write storm. A rollback plan that says “restore from backup” is not a rollback plan for a table receiving live writes.&lt;/p&gt;
&lt;p&gt;The job needs to be treated less like a script and more like a production control plane.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Most maintenance jobs start from a correct local intention: find rows, update rows, repeat until done. The failure appears when that local intention meets shared database behavior.&lt;/p&gt;
&lt;p&gt;A long transaction pins MVCC cleanup. A missing predicate turns a batch update into a table scan. A job running from two deploys races itself. A migration and a repair task touch the same table in opposite order and deadlock. A primary looks healthy while replicas fall minutes behind. The job succeeds technically but destroys the error budget around it.&lt;/p&gt;
&lt;p&gt;The hard question is not “how do we write the Python?” It is: how do we make a database maintenance job safe to start, safe to continue, and safe to stop?&lt;/p&gt;
&lt;h2 id=&quot;the-maintenance-job-control-plane&quot;&gt;The Maintenance Job Control Plane&lt;/h2&gt;
&lt;p&gt;A production-grade maintenance job has four explicit layers: preflight checks, lease ownership, bounded batches, and rollback checkpoints. The Python code is only the executor. The safety model lives around it.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[maintenance request — operational intent] --&gt; B[preflight checks — schema lag capacity]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C{risk gate — safe to run}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt;|blocked| D[exit cleanly — explain reason]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt;|allowed| E[lease acquisition — single owner]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; F[batch planner — bounded key range]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; G[transaction — small write set]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; H[verify batch — counts and invariants]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt; I{continue gate — health still good}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt;|pause| J[checkpoint — resumable state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt;|continue| F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  J --&gt; K[rollback path — inverse action or compensating job]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The preflight phase should fail closed. Before touching rows, the job verifies the expected schema version, required indexes, feature flag state, database role, replica lag, write capacity, and maximum allowed row count. These checks are not documentation. They are executable conditions.&lt;/p&gt;
&lt;p&gt;The lease phase prevents duplicate execution. In PostgreSQL, that may be a transaction-scoped or session-scoped advisory lock. In MySQL, it may be &lt;code&gt;GET_LOCK&lt;/code&gt;. In a platform scheduler, it may be a database-backed job table with a unique active lease. The key property is not elegance. It is that two workers cannot both believe they own the same maintenance scope.&lt;/p&gt;
&lt;p&gt;The batching phase bounds damage. Prefer stable keyset batches over offset pagination. Offset pagination gets slower and less predictable as rows move or disappear. A job should select a bounded set of primary keys, commit after a small write set, record progress, and then continue from the checkpoint. Each batch should have a maximum row count, maximum transaction duration, and maximum retry count.&lt;/p&gt;
&lt;p&gt;Rollback is not a single button. For destructive changes, rollback may mean writing an audit table before mutation. For derived data, it may mean recomputing from source of truth. For state transitions, it may mean a compensating transition that is valid under current application rules. The rollback path must be tested on the same representation the job writes, not described after the fact in a ticket.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; PostgreSQL documents that explicit locks, row locks, advisory locks, &lt;code&gt;lock_timeout&lt;/code&gt;, and &lt;code&gt;statement_timeout&lt;/code&gt; are part of the database’s concurrency control surface. The relevant pattern is that a maintenance job should assume it is competing with normal production traffic, not operating outside it. PostgreSQL’s MVCC model also means long-running transactions can delay cleanup and preserve old row versions longer than expected.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; A Python job against PostgreSQL should set &lt;code&gt;lock_timeout&lt;/code&gt; and &lt;code&gt;statement_timeout&lt;/code&gt; at the start of each transaction, acquire an advisory lock for the job scope, and process rows in keyset batches. A typical batch shape is: select candidate primary keys using an indexed predicate, update only those keys, verify the affected count, commit, then persist the last processed key or a batch watermark. When the job cannot acquire a lock quickly, it should exit or pause instead of waiting behind production traffic.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; This design changes the failure mode. Instead of a maintenance job silently waiting for a lock, holding a transaction open, or doubling work after a scheduler retry, it becomes interruptible. Each batch is either committed and checkpointed or abandoned by transaction rollback. Timeouts turn hidden contention into visible job failure. The advisory lock turns duplicate starts into a controlled no-op.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; The documented pattern is to use the database’s own concurrency controls as part of the application workflow. Safety does not come from trusting that a script is small. It comes from making every unit of work bounded, observable, and restartable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; GitHub has publicly described using online schema migration techniques for large MySQL tables, including throttling and operational safeguards around production database changes. The broader architectural pattern is that large data changes need pacing, measurement, and abort conditions because database load changes during the run.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Apply the same discipline to Python maintenance jobs. Add a health gate before every batch: replica lag under threshold, database error rate normal, queue depth acceptable, and application feature flag still enabled. Emit structured metrics for rows scanned, rows changed, batch latency, lock wait failures, retries, and remaining work estimate. Make pausing the job an ordinary operational action, not an emergency patch.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The job becomes compatible with production operations. It can slow down when replicas lag, stop when an incident begins, and resume without reprocessing the entire table. Operators can distinguish healthy progress from churn because the metrics describe both throughput and database pressure.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; The documented pattern is that online change systems are control loops. A Python job that mutates production data should also be a control loop: observe, decide, write, verify, and checkpoint.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Safer design&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Full-table scan&lt;/td&gt;&lt;td&gt;Predicate lacks a usable index&lt;/td&gt;&lt;td&gt;Preflight verifies the index and query plan shape&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Duplicate execution&lt;/td&gt;&lt;td&gt;Scheduler retries while old worker still runs&lt;/td&gt;&lt;td&gt;Database lease or advisory lock per job scope&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica lag spike&lt;/td&gt;&lt;td&gt;Batches write faster than replicas can replay&lt;/td&gt;&lt;td&gt;Health gate checks lag between batches&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Long lock wait&lt;/td&gt;&lt;td&gt;Job waits behind production transaction&lt;/td&gt;&lt;td&gt;Short &lt;code&gt;lock_timeout&lt;/code&gt; and retry with backoff&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unbounded transaction&lt;/td&gt;&lt;td&gt;Loop commits only at the end&lt;/td&gt;&lt;td&gt;Commit after bounded keyset batches&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bad rollback&lt;/td&gt;&lt;td&gt;Job overwrites source values&lt;/td&gt;&lt;td&gt;Audit table, inverse operation, or recompute from source&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Deadlocks&lt;/td&gt;&lt;td&gt;Job touches tables in inconsistent order&lt;/td&gt;&lt;td&gt;Fixed lock order and small write sets&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;False completion&lt;/td&gt;&lt;td&gt;Job counts attempted rows, not changed rows&lt;/td&gt;&lt;td&gt;Verify affected rows and invariant counts&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The uncomfortable tradeoff is that safe jobs are slower. They spend time checking, pausing, checkpointing, and emitting telemetry. That is the point. A maintenance job that cannot afford to stop is not a maintenance job. It is a migration pretending to be a script.&lt;/p&gt;
&lt;p&gt;Another tradeoff is operational complexity. Advisory locks, job tables, dry runs, audit records, and dashboards feel heavy for a one-time cleanup. But one-time cleanups are often copied into the next incident. The platform standard should make the safe path easier than the quick path.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Python database jobs often fail because they treat production databases as inert storage. They ignore locks, lag, retries, duplicate execution, and rollback.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Wrap the job in a control plane: executable preflight checks, single-owner locking, bounded keyset batches, health gates, checkpoints, and tested rollback behavior.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; PostgreSQL’s documented concurrency controls and public online migration patterns from large production systems both point to the same lesson: production data changes need pacing and abortability.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before the next maintenance job runs, require a dry-run mode, a database lease, per-batch timeouts, progress checkpoints, metrics, and a rollback mechanism that has been exercised outside production.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>automation</category><category>platform</category><category>ci-cd</category></item><item><title>MySQL Write Scaling: What Async, Semi-Sync, and InnoDB Cluster Actually Solve</title><link>https://rajivonai.com/blog/2024-11-20-mysql-write-scaling-async-semisync-innodb-cluster/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-11-20-mysql-write-scaling-async-semisync-innodb-cluster/</guid><description>A workload-first comparison of MySQL asynchronous replication, semisynchronous replication, and Group Replication—and why multi-primary is not the same as sharding.</description><pubDate>Wed, 20 Nov 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A second MySQL server can protect a transaction, serve a stale copy of it, or accept a competing version of it. Those are three different architectures. Calling all of them “write scaling” hides the failure mode that matters.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;RetailCo’s order platform runs on MySQL 8.4. One source owns every checkout write. Two replicas serve order history and provide promotion candidates. Query tuning, connection pooling, read routing, and larger hardware have created headroom, but the source remains the only place that can commit an order, reservation, payment state change, or idempotency record.&lt;/p&gt;
&lt;p&gt;The next proposal sounds simple:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Replace the topology with a three-node InnoDB Cluster. If all three nodes accept writes, write capacity should triple.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;That conclusion mixes four separate mechanisms:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;classic asynchronous replication;&lt;/li&gt;
&lt;li&gt;semisynchronous replication;&lt;/li&gt;
&lt;li&gt;MySQL Group Replication in single-primary mode;&lt;/li&gt;
&lt;li&gt;MySQL Group Replication in multi-primary mode.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;InnoDB Cluster adds another layer: MySQL Shell AdminAPI manages the cluster, Group Replication maintains the replicated group, and MySQL Router directs client connections. It is not merely another name for “three MySQL servers.” MySQL documents the complete stack in &lt;a href=&quot;https://dev.mysql.com/doc/mysql-shell/8.4/en/mysql-innodb-cluster.html&quot;&gt;MySQL InnoDB Cluster&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;RetailCo is a reference architecture, not a claimed customer deployment. Its topology and workload are design inputs. This article presents no measured transaction rate, latency, conflict rate, failover time, or data-loss result.&lt;/p&gt;
&lt;p&gt;The starting topology is conventional:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App[order applications] --&gt; Proxy[connection and query router]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Proxy --&gt; Source[MySQL source — all writes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Proxy --&gt; ReplicaA[replica A — eligible reads]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Proxy --&gt; ReplicaB[replica B — eligible reads]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Source --&gt; Binlog[binary log stream]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Binlog --&gt; ReplicaA&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Binlog --&gt; ReplicaB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The replicas add read capacity and promotion choices. They do not divide ownership of the order tables. The &lt;a href=&quot;https://rajivonai.com/blog/2024-01-23-why-reads-scale-more-easily-than-writes/&quot;&gt;foundation article in this series&lt;/a&gt; calls this the single-writer ceiling.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The architecture decision is often reduced to a misleading ladder:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;asynchronous&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    becomes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;semisynchronous&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    becomes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Group Replication&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    becomes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;multi-primary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    equals&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;more write capacity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These are not four settings on one scalability control. Each changes a different contract:&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Mechanism&lt;/th&gt;&lt;th&gt;Main question it answers&lt;/th&gt;&lt;th&gt;What it does not establish&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Asynchronous replication&lt;/td&gt;&lt;td&gt;Can copies receive and apply the source’s binlog later?&lt;/td&gt;&lt;td&gt;Another write owner or zero-RPO failover&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Semisynchronous replication&lt;/td&gt;&lt;td&gt;Did at least the configured number of replicas acknowledge receipt before the client proceeds?&lt;/td&gt;&lt;td&gt;Replica apply completion or more writer CPU&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Group Replication single-primary&lt;/td&gt;&lt;td&gt;Can a quorum maintain membership, order transactions, and elect one primary?&lt;/td&gt;&lt;td&gt;Concurrent independent write domains&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Group Replication multi-primary&lt;/td&gt;&lt;td&gt;Can several members accept writes and certify conflicts?&lt;/td&gt;&lt;td&gt;Linear scale for a shared dataset&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sharding&lt;/td&gt;&lt;td&gt;Can different databases own different tenants or key ranges?&lt;/td&gt;&lt;td&gt;Transparent cross-shard transactions&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The exact language matters. Traditional MySQL replication is asynchronous by default: source and replica commits are independent. Semisynchronous replication adds a receipt acknowledgment to the source commit path, but the replica still applies later. Group Replication uses a majority protocol to agree on transaction order, while each member can still have an applier queue.&lt;/p&gt;
&lt;p&gt;“Synchronous” is therefore too imprecise by itself. Ask three explicit questions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;What must happen before the client receives commit success?&lt;/li&gt;
&lt;li&gt;Which members have received the transaction at that point?&lt;/li&gt;
&lt;li&gt;Which members have actually applied it and can serve a current read?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The core problem is not choosing the strongest-sounding replication mode. It is deciding whether RetailCo needs lower RPO, automated failover, current reads, additional write-entry points, or genuinely independent write capacity.&lt;/p&gt;
&lt;h2 id=&quot;separate-durability-availability-and-write-ownership&quot;&gt;Separate Durability, Availability, and Write Ownership&lt;/h2&gt;
&lt;h3 id=&quot;asynchronous-replication-protects-copies-after-the-commit&quot;&gt;Asynchronous replication protects copies after the commit&lt;/h3&gt;
&lt;p&gt;In classic source-to-replica replication, the source executes and commits the transaction, records it in the binary log, and streams events to replicas. Each replica receives events into a relay log and applies them independently. MySQL’s diagrams explicitly show the source and replica commits as asynchronous: &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/group-replication-primary-secondary-replication.html&quot;&gt;source-to-replica replication&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Client[client transaction] --&gt; Execute[execute on source]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Execute --&gt; Commit[commit on source]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Commit --&gt; Ack[success to client]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Commit --&gt; Send[send binlog events]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Send --&gt; Relay[replica relay log]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Relay --&gt; Apply[replica applies later]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This topology is attractive because source commits do not normally wait for replica network or apply work. The trade-off is an exposure window. A promoted replica can be missing transactions that the old source acknowledged but never transmitted or that the candidate never received.&lt;/p&gt;
&lt;p&gt;The architecture still needs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;GTID-based candidate comparison;&lt;/li&gt;
&lt;li&gt;fencing that prevents the old source from accepting writes;&lt;/li&gt;
&lt;li&gt;a promotion rule that prioritizes correctness, not only low reported lag;&lt;/li&gt;
&lt;li&gt;client routing to the new source;&lt;/li&gt;
&lt;li&gt;reconciliation for uncertain transactions;&lt;/li&gt;
&lt;li&gt;read-after-write routing for requests that cannot tolerate replica lag.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Adding more asynchronous replicas can add read capacity. It does not reduce write work on the source; it can add binlog-sender, network, retention, and operational work.&lt;/p&gt;
&lt;h4 id=&quot;replica-lag-is-usually-an-applier-problem-and-it-has-a-specific-lever&quot;&gt;Replica lag is usually an applier problem, and it has a specific lever&lt;/h4&gt;
&lt;p&gt;This article’s recurring point is that a replica applies the transaction later. That gap is not fixed — it is a function of how much apply parallelism the replica is allowed. Before concluding that replicas cannot keep up with the source, verify the applier configuration, because two defaults commonly leave capacity unused:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; @@replica_parallel_workers,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       @@replica_parallel_type,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       @@replica_preserve_commit_order,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       @@binlog_transaction_dependency_tracking;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;binlog_transaction_dependency_tracking&lt;/code&gt; still defaults to &lt;code&gt;COMMIT_ORDER&lt;/code&gt; in MySQL 8.4. In that mode a replica can only apply transactions in parallel if they committed in the same binary-log group on the source, so a source with low write concurrency produces a binlog the replica must apply almost serially — a replica falling behind a source that is not itself busy. Setting it to &lt;code&gt;WRITESET&lt;/code&gt; lets the source record which transactions touched disjoint rows, and the replica applies those in parallel regardless of source commit grouping. It is frequently the single largest improvement available to a lagging replica, and it is a source-side setting, which surprises teams who look only at the replica.&lt;/p&gt;
&lt;p&gt;Two cautions. &lt;code&gt;WRITESET&lt;/code&gt; requires row-based binary logging and depends on tables having primary keys — the same requirement Group Replication imposes — so a legacy table without one silently limits parallelism. And &lt;code&gt;replica_preserve_commit_order&lt;/code&gt; (enabled by default since 8.0.27, alongside a &lt;code&gt;replica_parallel_workers&lt;/code&gt; default of 4) keeps replica commit order identical to the source, which is what makes the replica safe to promote and to read consistently; do not disable it to chase apply throughput.&lt;/p&gt;
&lt;p&gt;This matters for the architecture decision because a team that mistakes an applier-configuration problem for a write-capacity ceiling can spend a sharding migration solving the wrong constraint.&lt;/p&gt;
&lt;h3 id=&quot;semisynchronous-replication-narrows-one-loss-window&quot;&gt;Semisynchronous replication narrows one loss window&lt;/h3&gt;
&lt;p&gt;With semisynchronous replication enabled, the source waits for a configured number of replica acknowledgments. The default acknowledgment count is one. Under the default &lt;code&gt;AFTER_SYNC&lt;/code&gt; wait point, MySQL writes and synchronizes the binary log, waits for transaction-receipt acknowledgment, commits to the source storage engine, and returns to the client: &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/replication-semisync-interface.html&quot;&gt;configuring semisynchronous replication&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The replica acknowledgment means the events were received and placed in its relay log. It does &lt;strong&gt;not&lt;/strong&gt; mean the replica has executed and committed the transaction.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Client[client transaction] --&gt; Source[source executes transaction]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Source --&gt; Binlog[source syncs binary log]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Binlog --&gt; Relay[replica receives relay-log events]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Relay --&gt; Receipt[replica acknowledges receipt]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Receipt --&gt; SourceCommit[source commits under AFTER_SYNC]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  SourceCommit --&gt; ClientAck[success to client]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Relay --&gt; Later[replica applies independently]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This can improve the failover durability contract when the promotion process selects the acknowledged replica and fences the old source. It still does not scale writes. The same source executes every transaction, and commit latency now includes an acknowledgment path.&lt;/p&gt;
&lt;p&gt;Two operational details are easy to miss:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Semisynchronous replication is disabled by default.&lt;/li&gt;
&lt;li&gt;If acknowledgment exceeds &lt;code&gt;rpl_semi_sync_source_timeout&lt;/code&gt;, the source can revert to asynchronous operation.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;An enabled configuration variable is therefore not proof that the durability contract is active. MySQL documents &lt;code&gt;Rpl_semi_sync_source_status&lt;/code&gt; as the operational signal and exposes acknowledged and unacknowledged transaction counters: &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/replication-semisync-monitoring.html&quot;&gt;semisynchronous replication monitoring&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GLOBAL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; STATUS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LIKE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;Rpl_semi_sync%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GLOBAL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; VARIABLES &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIKE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;rpl_semi_sync%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The variable names above are deliberate. MySQL 8.4 removed the old &lt;code&gt;semisync_master&lt;/code&gt;/&lt;code&gt;semisync_slave&lt;/code&gt; plugins along with every &lt;code&gt;rpl_semi_sync_master_*&lt;/code&gt; and &lt;code&gt;rpl_semi_sync_slave_*&lt;/code&gt; variable; the supported plugins are &lt;code&gt;semisync_source&lt;/code&gt; and &lt;code&gt;semisync_replica&lt;/code&gt;, with &lt;code&gt;rpl_semi_sync_source_*&lt;/code&gt; and &lt;code&gt;rpl_semi_sync_replica_*&lt;/code&gt; variables. A configuration file carried forward from 8.0 will not start MySQL 8.4 if it still sets the old names, and automation that greps for &lt;code&gt;Rpl_semi_sync_master_status&lt;/code&gt; will report a healthy zero forever. RetailCo should treat the upgrade to 8.4 as a semisynchronous-replication reconfiguration, not only a version bump.&lt;/p&gt;
&lt;p&gt;RetailCo should alert when operational status falls to zero, when unacknowledged commits increase, or when wait time consumes the checkout latency budget. Quiet fallback is a change in RPO, not merely a performance event.&lt;/p&gt;
&lt;h3 id=&quot;group-replication-single-primary-is-an-ha-architecture&quot;&gt;Group Replication single-primary is an HA architecture&lt;/h3&gt;
&lt;p&gt;Group Replication builds on InnoDB, row-based binary logging, GTIDs, write-set conflict detection, group membership, and a Paxos-based communication engine. MySQL requires replicated tables to have a primary key or a non-null unique-key equivalent so transactions can identify conflicting rows: &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/group-replication-requirements.html&quot;&gt;Group Replication requirements&lt;/a&gt; and &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/group-replication-plugin-architecture.html&quot;&gt;plugin architecture&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Single-primary mode is the default. One member is read-write; the other members use &lt;code&gt;super_read_only&lt;/code&gt;. A transaction is committed only after a majority has received it and agreed on its relative order. Remote members then apply transactions through their applier path: &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/group-replication-deploying-in-multi-primary-or-single-primary-mode.html&quot;&gt;Group Replication modes&lt;/a&gt; and &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/group-replication-flow-control.html&quot;&gt;flow control&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Apps[applications] --&gt; Router[MySQL Router]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Router --&gt; Primary[member A — read-write primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Router --&gt; SecondaryB[member B — secondary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Router --&gt; SecondaryC[member C — secondary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Primary --&gt; Order[group agrees transaction order]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  SecondaryB --&gt; Order&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  SecondaryC --&gt; Order&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Order --&gt; ApplyA[apply on member A]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Order --&gt; ApplyB[apply on member B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Order --&gt; ApplyC[apply on member C]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A three-member group remains quorate after one member failure because two members still form a majority. Losing the majority blocks progress because the remaining partition cannot safely decide whether it is authoritative. MySQL warns that manually forcing membership after quorum loss is a last-resort operation that can create split brain if excluded members are still active: &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/group-replication-network-partitioning.html&quot;&gt;handling loss of quorum&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Two hard boundaries should enter the capacity plan before the design is approved, because neither is tunable away:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;A group cannot exceed nine members.&lt;/strong&gt; This is a protocol limit, not a configuration default. It settles the question of whether Group Replication can be scaled by adding members: it cannot be scaled far, and the ninth member is a hard stop rather than a performance knee. A fleet that needs thirty read copies needs asynchronous replicas attached to the group, not a larger group.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Large transactions can eject a member.&lt;/strong&gt; &lt;code&gt;group_replication_transaction_size_limit&lt;/code&gt; bounds the size of a transaction the group will accept, defaulting to 150 MB. Beyond it, the transaction fails; well below it, a large transaction can still cause message backlog, flow-control activation, and — if a member cannot keep up — expulsion from the group. A bulk &lt;code&gt;DELETE&lt;/code&gt; of expired reservations that was harmless on a standalone source becomes a group-stability event.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Both boundaries argue for the same conclusion as the rest of this article: Group Replication is sized for availability, and the operations that stress it are the batch and maintenance paths rather than the checkout path.&lt;/p&gt;
&lt;p&gt;Group Replication itself does not redirect client connections. InnoDB Cluster integrates MySQL Shell AdminAPI and MySQL Router so a failed single primary can be replaced and traffic can be routed to the new one. MySQL Shell 8.4 documents &lt;code&gt;BEFORE_ON_PRIMARY_FAILOVER&lt;/code&gt; as the InnoDB Cluster failover-consistency default, holding new work until the elected primary applies its backlog: &lt;a href=&quot;https://dev.mysql.com/doc/mysql-shell/8.4/en/mysql-innodb-cluster-failover-consistency.html&quot;&gt;failover consistency&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;That is valuable high availability. It is still one write execution point at a time.&lt;/p&gt;
&lt;h3 id=&quot;multi-primary-changes-admission-not-data-ownership&quot;&gt;Multi-primary changes admission, not data ownership&lt;/h3&gt;
&lt;p&gt;In multi-primary mode, every online member can accept transactions. Each transaction’s write set is ordered and certified against concurrent work. If two transactions conflict, one can be rolled back. Every successful transaction still becomes part of the same replicated dataset and must be applied by the group.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  TenantA[tenant A request] --&gt; MemberA[member A accepts write]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  TenantB[tenant B request] --&gt; MemberB[member B accepts write]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Hot[hot order request] --&gt; MemberC[member C accepts write]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  MemberA --&gt; Certify[group ordering and certification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  MemberB --&gt; Certify&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  MemberC --&gt; Certify&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Certify --&gt; Outcome{conflict detected}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Outcome --&gt;|no| Replicate[apply successful transaction on every member]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Outcome --&gt;|yes| Rollback[rollback losing transaction]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This architecture can be useful when writes enter through several locations and conflicts are rare. It does not create three independent storage owners:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;every member stores the full replicated dataset;&lt;/li&gt;
&lt;li&gt;successful writes are propagated to every member;&lt;/li&gt;
&lt;li&gt;certification remains group-wide;&lt;/li&gt;
&lt;li&gt;a slow member can build certification or applier queues;&lt;/li&gt;
&lt;li&gt;flow control can throttle writers when queue thresholds are exceeded;&lt;/li&gt;
&lt;li&gt;hot rows create conflicts rather than parallel ownership.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;MySQL’s own flow-control documentation states the boundary directly: the approach works when total group writes do not exceed the write capacity of any member. That is fundamentally different from sharding, where Cluster A never applies Cluster B’s tenant-local writes.&lt;/p&gt;
&lt;p&gt;Multi-primary also changes the SQL risk profile. MySQL 8.4 documents limitations involving &lt;code&gt;SERIALIZABLE&lt;/code&gt;, multi-level cascading foreign keys, concurrent DDL and DML on the same object from different members, and &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; expectations because locks are local rather than group-wide: &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/group-replication-limitations.html&quot;&gt;Group Replication limitations&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Auto-increment spacing prevents different members from choosing the same generated values. It does not distribute tenant ownership or remove certification: &lt;a href=&quot;https://dev.mysql.com/doc/mysql-shell/8.4/en/mysql-innodb-cluster-auto-increment.html&quot;&gt;InnoDB Cluster auto-increment behavior&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;RetailCo should make two decisions separately.&lt;/p&gt;
&lt;h3 id=&quot;decision-1--choose-the-availability-contract&quot;&gt;Decision 1 — choose the availability contract&lt;/h3&gt;
&lt;p&gt;For the existing unsharded order database, the default recommendation is a three-member InnoDB Cluster in single-primary mode when RetailCo is prepared to operate MySQL Shell, Router, quorum, distributed recovery, and failure testing.&lt;/p&gt;
&lt;p&gt;The recommendation is based on the documented system behavior, not a throughput claim:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;one explicit writer avoids multi-primary conflict semantics;&lt;/li&gt;
&lt;li&gt;majority ordering improves the failover contract;&lt;/li&gt;
&lt;li&gt;automatic election and Router integration reduce manual routing work;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;BEFORE_ON_PRIMARY_FAILOVER&lt;/code&gt; prevents the new primary from exposing an older applied state;&lt;/li&gt;
&lt;li&gt;each healthy member maintains the full replicated dataset and can become a candidate after applying its backlog.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Classic asynchronous replication remains reasonable when low commit latency, operational simplicity, delayed replicas, or independent read copies matter more than quorum-coordinated automated failover. Semisynchronous replication is useful when RetailCo wants a narrower acknowledged-loss window but is not ready to adopt Group Replication.&lt;/p&gt;
&lt;p&gt;For a managed cloud database, RetailCo should compare the provider’s documented contract rather than assume the service exposes upstream Group Replication. Ask the same questions: what is durable at acknowledgment, who elects and fences, how clients reroute, whether a reader can be stale, and what RPO/RTO the failure test actually observes.&lt;/p&gt;
&lt;h3 id=&quot;decision-2--choose-the-write-scaling-boundary&quot;&gt;Decision 2 — choose the write-scaling boundary&lt;/h3&gt;
&lt;p&gt;If the single-primary cluster still cannot sustain checkout writes, changing to multi-primary is not the first recommendation. RetailCo already carries &lt;code&gt;tenant_id&lt;/code&gt;, so the stronger scale-out design is independent tenant ownership:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;tenant group one   -&gt; InnoDB Cluster A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;tenant group two   -&gt; InnoDB Cluster B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;tenant group three -&gt; InnoDB Cluster C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Within each shard, Group Replication provides HA. Across shards, ownership provides write capacity. The &lt;a href=&quot;https://rajivonai.com/blog/2025-03-18-sharded-innodb-clusters-mysql-write-scaling/&quot;&gt;tenant-sharded InnoDB Cluster article&lt;/a&gt; develops that architecture, including routing, tenant moves, schema rollout, backup, and cross-shard transactions.&lt;/p&gt;
&lt;h3 id=&quot;proposed-benchmark--no-results-yet&quot;&gt;Proposed benchmark — no results yet&lt;/h3&gt;
&lt;p&gt;RetailCo should test four topologies with the same MySQL 8.4 patch, schema, storage, instance shape, and durability settings:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;one source plus two asynchronous replicas;&lt;/li&gt;
&lt;li&gt;one source plus two semisynchronous replicas, requiring one acknowledgment;&lt;/li&gt;
&lt;li&gt;three-member InnoDB Cluster in single-primary mode;&lt;/li&gt;
&lt;li&gt;three-member InnoDB Cluster in multi-primary mode.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Run at least four workload shapes:&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Workload&lt;/th&gt;&lt;th&gt;Purpose&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Independent tenant inserts&lt;/td&gt;&lt;td&gt;Best case for low-conflict multi-primary admission&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Different rows within one tenant&lt;/td&gt;&lt;td&gt;Exposes secondary-index and shared-record interactions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One hot inventory or sequence row&lt;/td&gt;&lt;td&gt;Exposes certification rollback and retry pressure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Large transactions&lt;/td&gt;&lt;td&gt;Exposes group message, memory, queue, and expulsion risks&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Capture attempted and successful TPS separately. Also capture P50/P95/P99 latency, commit latency, application retries, errors, ambiguous outcomes, semisync fallback, replica apply delay, certification conflicts, local rollbacks, certification queues, remote applier queues, and flow-control activation.&lt;/p&gt;
&lt;p&gt;The central Group Replication query is derived from MySQL’s documented Performance Schema table:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MEMBER_ID,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    COUNT_CONFLICTS_DETECTED,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    COUNT_TRANSACTIONS_LOCAL_ROLLBACK,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    COUNT_TRANSACTIONS_IN_QUEUE,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    COUNT_TRANSACTIONS_REMOTE_IN_APPLIER_QUEUE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; performance_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;replication_group_member_stats&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The lab must also test failure behavior:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;kill the async source before the replicas receive the last acknowledged transaction;&lt;/li&gt;
&lt;li&gt;interrupt the semisync acknowledgment path until it falls back;&lt;/li&gt;
&lt;li&gt;stop the Group Replication primary and verify client retry behavior;&lt;/li&gt;
&lt;li&gt;isolate one of three group members;&lt;/li&gt;
&lt;li&gt;isolate two of three and verify loss of quorum blocks writes;&lt;/li&gt;
&lt;li&gt;restore the excluded nodes only after proving the authoritative membership and fencing state.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Status: proposed lab—no empirical result is claimed.&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id=&quot;recovery-is-not-the-same-as-member-rejoin&quot;&gt;Recovery is not the same as member rejoin&lt;/h3&gt;
&lt;p&gt;Group Replication distributed recovery can use a remote clone and binary-log state transfer to catch up a joining member: &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/group-replication-distributed-recovery.html&quot;&gt;distributed recovery&lt;/a&gt;. That repairs a replica of current group state. It does not recover an order deleted by a valid transaction, because that deletion is also replicated.&lt;/p&gt;
&lt;p&gt;RetailCo still needs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;scheduled full backups;&lt;/li&gt;
&lt;li&gt;retained binary logs for point-in-time recovery;&lt;/li&gt;
&lt;li&gt;an isolated restore procedure;&lt;/li&gt;
&lt;li&gt;a method to rebuild a clean cluster from restored state;&lt;/li&gt;
&lt;li&gt;tenant-level recovery or repair tooling where required;&lt;/li&gt;
&lt;li&gt;measured RPO and RTO for operator error as well as server loss.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;High availability keeps the current state available. Backup recovers a previous state. Neither replaces the other.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;







































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Assumption&lt;/th&gt;&lt;th&gt;What the mechanism actually guarantees&lt;/th&gt;&lt;th&gt;Failure if ignored&lt;/th&gt;&lt;th&gt;Recommendation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;An async replica has every acknowledged commit&lt;/td&gt;&lt;td&gt;Receipt and apply happen after source commit&lt;/td&gt;&lt;td&gt;Promotion loses recent writes&lt;/td&gt;&lt;td&gt;Measure GTID position and test RPO&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Semisync means the replica applied the transaction&lt;/td&gt;&lt;td&gt;Configured replicas acknowledge receipt&lt;/td&gt;&lt;td&gt;A read from the replica is still stale&lt;/td&gt;&lt;td&gt;Route strict reads or wait for apply&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Semisync stays active once enabled&lt;/td&gt;&lt;td&gt;Timeout can revert the source to async&lt;/td&gt;&lt;td&gt;Durability silently changes during impairment&lt;/td&gt;&lt;td&gt;Alert on operational status and counters&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Group Replication reroutes clients&lt;/td&gt;&lt;td&gt;Group membership and transaction coordination are database functions&lt;/td&gt;&lt;td&gt;Applications remain connected to a failed member&lt;/td&gt;&lt;td&gt;Use and test Router, proxy, connector, or application routing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Majority receipt means every member can serve the write&lt;/td&gt;&lt;td&gt;Remote applier queues can lag&lt;/td&gt;&lt;td&gt;Secondary read returns older state&lt;/td&gt;&lt;td&gt;Select and test the needed consistency level&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Three primaries provide three times the write capacity&lt;/td&gt;&lt;td&gt;All successful writes are certified and applied group-wide&lt;/td&gt;&lt;td&gt;CPU, network, or applier ceiling remains&lt;/td&gt;&lt;td&gt;Benchmark successful TPS, queues, and retries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Different tenants never conflict&lt;/td&gt;&lt;td&gt;Shared indexes, counters, metadata, and DDL can create common write sets&lt;/td&gt;&lt;td&gt;Unexpected certification rollback&lt;/td&gt;&lt;td&gt;Inspect write sets and remove shared hot state&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;A distributed &lt;code&gt;FOR UPDATE&lt;/code&gt; lock protects all writers&lt;/td&gt;&lt;td&gt;Locks are local to a member&lt;/td&gt;&lt;td&gt;Cross-member transaction assumptions fail&lt;/td&gt;&lt;td&gt;Redesign ownership or use single-primary mode&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Automatic failover eliminates ambiguous commits&lt;/td&gt;&lt;td&gt;A client can lose its connection around commit&lt;/td&gt;&lt;td&gt;Duplicate business action after retry&lt;/td&gt;&lt;td&gt;Use idempotency keys and outcome lookup&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;A healthy cluster is a backup&lt;/td&gt;&lt;td&gt;Valid mistakes replicate to every member&lt;/td&gt;&lt;td&gt;No point before the error can be restored&lt;/td&gt;&lt;td&gt;Maintain and test PITR independently&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;when-i-would-not-use-multi-primary-group-replication&quot;&gt;When I would not use multi-primary Group Replication&lt;/h3&gt;
&lt;p&gt;I would not use multi-primary mode to rescue a writer that is already CPU-bound while every member stores the same tables. Adding certification, networking, and apply work does not partition that load.&lt;/p&gt;
&lt;p&gt;I would also avoid it when the workload contains hot rows, heavy &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; coordination, &lt;code&gt;SERIALIZABLE&lt;/code&gt; transactions, cascading foreign-key structures, uncoordinated DDL, large transactions, or applications that cannot safely retry certification failures.&lt;/p&gt;
&lt;p&gt;For RetailCo’s current database, single-primary InnoDB Cluster is the clearer HA choice. If write capacity remains the problem, shard tenants across several clusters. If the application cannot carry a stable shard key or regularly needs cross-tenant atomic transactions, retain a single ownership domain while optimizing and scaling vertically, or evaluate a distributed SQL design whose transaction contract fits the workload.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; RetailCo’s replicas improve reads and recovery options, but all order writes still consume one ownership domain.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Choose async, semisync, or single-primary Group Replication for the required durability and failover contract; use sharding—not multi-primary terminology—to create independent write capacity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run identical workloads across all four topologies, separate attempted from committed TPS, observe certification and applier queues, force semisync fallback, lose quorum, and restore from backup.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Pilot a three-member single-primary InnoDB Cluster for HA, keep multi-primary as an evidence-driven exception, and design tenant-sharded clusters if the measured writer ceiling remains.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The scenario verdict is:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Good fit:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  single-primary InnoDB Cluster for self-managed MySQL HA and automated election&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Poor fit:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  multi-primary as a shortcut around a saturated shared-data writer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Main advantage:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  quorum-based membership, transaction ordering, and integrated failover tooling&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Main limitation:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  every successful write still belongs to and is applied by the whole group&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Operational complexity:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  medium to high because Router, quorum, recovery, upgrades, and fencing must be tested&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Migration complexity:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  medium for a compliant InnoDB and GTID workload, high when SQL patterns violate multi-primary limits&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The practical conclusion is simple: &lt;strong&gt;async protects copies, semisync strengthens acknowledgment, Group Replication strengthens availability, and sharding distributes ownership.&lt;/strong&gt; Use each mechanism for the problem it actually solves.&lt;/p&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category></item><item><title>Cost Observability: Build Dashboards That Show Waste Before Finance Finds It</title><link>https://rajivonai.com/blog/2024-11-19-cost-observability-database-dashboards/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-11-19-cost-observability-database-dashboards/</guid><description>How to expand monitoring beyond uptime by building dashboards that expose underutilized RDS instances, EBS io2 waste, and backup retention drift.</description><pubDate>Tue, 19 Nov 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;If the first time engineering hears about a database cost spike is during a monthly finance review, your observability stack is fundamentally incomplete.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Database engineering traditionally focuses on two metrics: availability and latency. As long as the database is up and queries are fast, the system is considered healthy. However, in the cloud era, infrastructure is elastic, and cost is the hidden third metric. Managed database services like Amazon RDS, Aurora, and DynamoDB make it incredibly easy to spin up massive, highly available clusters. They also make it incredibly easy to bleed tens of thousands of dollars in hidden waste.&lt;/p&gt;
&lt;p&gt;Most monitoring dashboards ignore cost entirely. Engineers look at CPU utilization to ensure it isn’t too high, but they rarely look at CPU utilization to ensure it isn’t too low. When observability is decoupled from cost, teams routinely run development environments on &lt;code&gt;db.r6g.4xlarge&lt;/code&gt; instances, leave obsolete manual snapshots sitting in S3 for years, and over-provision EBS IOPS for workloads that no longer need them.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;
&lt;p&gt;Cost inefficiency in cloud databases rarely triggers an immediate outage. Instead, it manifests as silent financial degradation. The symptoms include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The Idle Giant:&lt;/strong&gt; A massive database instance sits at 2% CPU utilization and 5% memory usage 24/7.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The IOPS Over-Provision:&lt;/strong&gt; A database is running on an &lt;code&gt;io2&lt;/code&gt; Block Express volume provisioned for 20,000 IOPS, but CloudWatch shows it has never exceeded 1,000 IOPS in the past month.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Snapshot Hoard:&lt;/strong&gt; The AWS bill shows RDS backup storage costs exceeding the actual running instance costs due to years of manual, un-expired snapshots.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Multi-AZ Dev Environment:&lt;/strong&gt; Non-production environments are running with Multi-AZ redundancy enabled, doubling the compute cost for workloads that can tolerate an hour of downtime.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;p&gt;To integrate cost into your operational posture, build a dedicated “Cost Triage” dashboard with these five checks:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Check Peak CPU and Connection Counts (30-Day Window):&lt;/strong&gt;
If an instance has not exceeded 20% CPU utilization and 10% connection pool usage during its highest peak over a 30-day window, it is a prime candidate for downsizing.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Evaluate Provisioned IOPS vs. Consumed IOPS:&lt;/strong&gt;
Compare the &lt;code&gt;VolumeReadOps&lt;/code&gt; and &lt;code&gt;VolumeWriteOps&lt;/code&gt; against the provisioned IOPS limit. If consumption is a fraction of the limit, migrate from &lt;code&gt;io2&lt;/code&gt; to &lt;code&gt;gp3&lt;/code&gt; or lower the provisioned &lt;code&gt;io2&lt;/code&gt; ceiling.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Audit Multi-AZ Deployments by Environment Tag:&lt;/strong&gt;
Query your infrastructure state (via AWS Config or your IaC state file) to find any instance tagged &lt;code&gt;env:dev&lt;/code&gt; or &lt;code&gt;env:staging&lt;/code&gt; that has &lt;code&gt;MultiAZ&lt;/code&gt; set to &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Review Manual Snapshot Age:&lt;/strong&gt;
List all manual RDS snapshots without an expiration tag. Automated backups age out naturally; manual snapshots taken “just in case” before a migration live forever and incur continuous S3 storage costs.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Track CloudWatch Log Ingestion and Retention:&lt;/strong&gt;
Database audit logs, slow query logs, and error logs pushed to CloudWatch Logs can become extremely expensive. Check the retention policies—logs kept indefinitely instead of aging out to S3 Glacier drive up costs.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;p&gt;When evaluating a database for cost optimization, use this triage flow to determine the safest remediation path.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Database Identified as High Cost] --&gt; B{Is it Production?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No| C[Check High-Availability Config]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; C1{Is Multi-AZ Enabled?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C1 --&gt;|Yes| C2[Disable Multi-AZ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C1 --&gt;|No| C3[Check Uptime Needs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C3 --&gt;|Can be stopped| C4[Implement Nightly Stop/Start Schedule]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes| D[Check Utilization Metrics]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; D1{Is Peak CPU &amp;#x3C; 20%?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D1 --&gt;|Yes| D2[Downsize Instance Type]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D1 --&gt;|No| D3[Check Storage Configuration]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D3 --&gt; D4{Using Provisioned IOPS io1/io2?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D4 --&gt;|Yes| D5[Evaluate Migration to gp3]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Instance Downsizing (High Impact, Low Risk):&lt;/strong&gt;
Scaling an RDS instance down to a smaller instance class halves the compute cost.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Tradeoff:&lt;/em&gt; This requires a brief interruption of service (failover). Ensure the application is resilient to connection drops.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Migrating &lt;code&gt;io1/io2&lt;/code&gt; to &lt;code&gt;gp3&lt;/code&gt; (High Impact, Zero Downtime):&lt;/strong&gt;
Modern &lt;code&gt;gp3&lt;/code&gt; volumes offer baseline performance of 3,000 IOPS and can be scaled up to 16,000 IOPS, which covers 90% of database workloads at a fraction of the cost of &lt;code&gt;io2&lt;/code&gt;. Storage type modifications can be done online.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Tradeoff:&lt;/em&gt; Modifying a large volume can take days to complete in the background, during which performance may be slightly degraded.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Automated Start/Stop for Dev Environments (Medium Impact, Zero Cost Risk):&lt;/strong&gt;
Using AWS Instance Scheduler to shut down dev databases at 6 PM and start them at 8 AM reduces compute costs by over 60%.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Tradeoff:&lt;/em&gt; Engineers working off-hours will need self-service access to manually restart their environments.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;When downsizing a database, always monitor application latency immediately following the cutover. If the smaller instance lacks the CPU cache or memory to serve queries efficiently, the rollback plan is to immediately initiate another modify instance command to scale back up. Because scaling up requires a reboot/failover, expect an additional 30-60 seconds of disruption.&lt;/p&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;Deploy a Lambda function triggered by EventBridge that runs weekly. The function should scan all RDS snapshots, identify any manual snapshot older than 90 days that does not have a &lt;code&gt;Compliance&lt;/code&gt; or &lt;code&gt;LegalHold&lt;/code&gt; tag, and automatically delete it. This prevents the “snapshot hoard” from silently inflating the AWS bill over time.&lt;/p&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cost is an Engineering Metric:&lt;/strong&gt; Do not treat cost as an external business constraint. Expose cloud costs directly alongside CPU and memory on your engineering dashboards.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tagging is Operations:&lt;/strong&gt; You cannot optimize what you cannot identify. Strict enforcement of &lt;code&gt;Environment&lt;/code&gt;, &lt;code&gt;Team&lt;/code&gt;, and &lt;code&gt;Service&lt;/code&gt; tags is the prerequisite for all cost observability.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Cloud is Elastic, Use It:&lt;/strong&gt; A database that runs 24/7 at 5% utilization is a failure of cloud architecture. Build your environments to scale down or shut off entirely when not in use.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; When observability is decoupled from cost, teams routinely over-provision dev environments on &lt;code&gt;db.r6g.4xlarge&lt;/code&gt;, hoard manual snapshots for years, and leave &lt;code&gt;io2&lt;/code&gt; volumes provisioned at 20,000 IOPS for workloads that never exceed 1,000 — none of which triggers an availability alert until the finance review.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build a “Database Waste” dashboard ranking instances by lowest peak CPU and highest storage cost, then automate weekly scans for Multi-AZ dev environments and snapshots older than 90 days without a compliance tag.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Identify one non-production database with Multi-AZ enabled, disable it via Terraform, and show the projected yearly savings — this is the first concrete signal that cost observability is surfacing real waste before finance does.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run the five checks above against your current RDS fleet this week. Any dev instance at sub-20% peak CPU with Multi-AZ enabled is an immediate win: disable Multi-AZ and schedule a nightly stop/start via Instance Scheduler.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>cloud</category><category>architecture</category><category>checklist</category></item><item><title>AlloyDB Architecture Deep Dive: The Operating Model PostgreSQL DBAs Need</title><link>https://rajivonai.com/blog/2024-11-15-alloydb-architecture-deep-dive/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-11-15-alloydb-architecture-deep-dive/</guid><description>A source-backed explanation of AlloyDB compute, distributed storage, high availability, read pools, and the columnar engine.</description><pubDate>Fri, 15 Nov 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;AlloyDB speaks PostgreSQL, but it is not self-managed PostgreSQL placed on a larger disk. Its disaggregated compute and storage change what a DBA can tune, what can fail, and which measurements explain latency.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A transactional PostgreSQL system is simultaneously serving short OLTP requests and scan-heavy reporting. On a conventional deployment, both workloads compete for CPU, memory, buffer-cache residency, and storage bandwidth. Vertical scaling postpones contention but does not separate it.&lt;/p&gt;
&lt;p&gt;AlloyDB offers PostgreSQL compatibility, distributed storage, read pools, and an optional columnar engine. Those capabilities can reduce specific bottlenecks, but only if the workload and operating model match them.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Two misleading shortcuts appear in architecture reviews. The first is “AlloyDB is 100% PostgreSQL,” which ignores unsupported extensions and service-specific behavior. The second is “storage is infinite and checkpoints disappear,” which turns a documented disaggregated architecture into claims Google does not make.&lt;/p&gt;
&lt;p&gt;The useful question is narrower: &lt;strong&gt;where do query execution, durability, caching, read scaling, and analytical acceleration live, and how does each boundary change failure diagnosis?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-disaggregated-architecture&quot;&gt;The Disaggregated Architecture&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[Application] --&gt; Endpoint[Primary instance endpoint]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Endpoint --&gt; Primary[Active primary node]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Endpoint --&gt; Standby[Standby node on failover]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App --&gt; ReadPool[Optional read pool instance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt; Log[Regional log persistor]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Log --&gt; Processor[Regional log processing]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Processor --&gt; Storage[Distributed regional storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Standby --&gt; Log&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ReadPool --&gt; Storage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt; Columnar[Optional columnar engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ReadPool --&gt; ColumnarRead[Optional columnar engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Google documents three resource levels: a cluster contains instances, and instances are powered by nodes. The primary instance provides the writable endpoint. A highly available primary has two nodes in different zones; the standby is not an application read endpoint. Read pool instances provide load-balanced, read-only endpoints and may contain multiple nodes.&lt;/p&gt;
&lt;p&gt;Durability is more specific than “shared storage.” Google documents that the active node synchronously writes WAL to a regional log persistor before acknowledgement. Regional log-processing servers then process that WAL asynchronously into data blocks in regional storage. During HA failover, the standby becomes active and reconnects through the same instance IP; Google states that synchronous WAL persistence prevents data loss for that failover model. Storage grows automatically within documented limits, but none of this makes storage latency, capacity limits, network behavior, or a single primary’s CPU irrelevant.&lt;/p&gt;
&lt;p&gt;PostgreSQL compatibility is deliberately high at the SQL and ecosystem layer, not absolute. Applications must audit extensions, flags, authentication, logical replication, and operational tooling against the current supported-features documentation. Physical PostgreSQL backups are not a portability mechanism; migration out normally uses logical export, replication, or a migration service.&lt;/p&gt;
&lt;h3 id=&quot;columnar-engine&quot;&gt;Columnar engine&lt;/h3&gt;
&lt;p&gt;The columnar engine is an optional in-memory column store plus planner and execution support for scans, joins, and aggregates. It can run on a primary, a read pool, or both. Columns can be populated manually or by auto-columnarization. Queries still need to be tested: unsupported operators, insufficient column coverage, or memory pressure can lead to row-store execution.&lt;/p&gt;
&lt;p&gt;Do not treat Google’s “up to” benchmark as a workload forecast. Establish a representative query set, compare plans, verify column-store residency, and measure transaction latency while analytics run.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented system behavior supports four operational conclusions.&lt;/p&gt;
&lt;p&gt;First, preserve a connection budget. Shared storage does not change PostgreSQL’s per-session memory and process costs. Bound application pools and test reconnection during failover.&lt;/p&gt;
&lt;p&gt;Second, use read pools for workloads that tolerate a read-only endpoint and the service’s consistency behavior. Explicit reader endpoints keep routing visible. Transparent Query Forwarding is a separate Preview feature as of September 2026 and should not be assumed in an older architecture or enabled without its eligibility analysis.&lt;/p&gt;
&lt;p&gt;Third, diagnose the layer actually under pressure. Primary CPU saturation, connection exhaustion, column-store evictions, and storage reads are different problems. Changing a familiar PostgreSQL checkpoint parameter is not a substitute for AlloyDB-specific metrics and query plans.&lt;/p&gt;
&lt;p&gt;Fourth, treat the columnar engine as capacity with admission criteria:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; customer_id, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;sum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(amount)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; current_date &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; interval &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;30 days&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GROUP BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; customer_id;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Capture the plan and latency before and after configuration. Use the documented &lt;code&gt;google_columnar_engine&lt;/code&gt; functions for the cluster’s version rather than copying extension-creation commands from standard PostgreSQL; AlloyDB manages the feature and its configuration.&lt;/p&gt;
&lt;p&gt;The migration gate should also include a portability exercise. Export a representative schema and data set through the supported logical path, import it into standard PostgreSQL, and run the application’s semantic tests. This does not promise a fast emergency exit for a large database; it exposes dependencies on AlloyDB-only flags, functions, authentication, and operational tooling while the design can still change. Record the measured export rate, import rate, validation time, and catch-up strategy instead of calling logical export an adequate disaster-recovery plan.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Architectural cause&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Primary CPU saturation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Writes and primary-bound queries still execute on one writable instance&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Remove inefficient work, isolate reads, then resize within service limits.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Analytics evict useful cache&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Column store and PostgreSQL compete for finite memory&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Size from observed residency and isolate analytics on a read pool when appropriate.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Extension-dependent migration fails&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Compatibility was assumed rather than audited&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Inventory extensions, flags, types, and drivers before migration.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Read pool is treated as a synchronous copy&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Application assumes primary semantics at the reader endpoint&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Define which requests tolerate replica visibility and test them.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Failover becomes an application outage&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Clients do not reconnect or retry safely&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run manual failover under load and tune connection lifetimes and backoff.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Conventional PostgreSQL tuning is being applied without identifying AlloyDB’s compute, storage, and analytical boundaries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Model the primary, HA standby, read pools, distributed storage, and columnar engine as separate resources.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Google’s architecture and columnar-engine documentation explicitly describe separated storage, instance roles, and query eligibility.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Benchmark one OLTP trace and one analytical trace, then run failover and read-pool tests before approving migration.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/overview&quot;&gt;AlloyDB overview and resource hierarchy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/high-availability&quot;&gt;AlloyDB high-availability architecture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/reference/extensions&quot;&gt;AlloyDB supported extensions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/columnar-engine/about&quot;&gt;Columnar engine overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/instance-read-pool-create&quot;&gt;Create and configure a read pool instance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/alloydb/docs/high-availability&quot;&gt;AlloyDB high availability&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>cloud</category><category>architecture</category><category>databases</category><category>failures</category><category>checklist</category></item><item><title>Database Anti-Patterns that Break Cloud Scale: How Shopify and Instagram Survived Relational Database Physics</title><link>https://rajivonai.com/blog/2024-11-05-database-anti-patterns-triggers-uuids-index-abuse/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-11-05-database-anti-patterns-triggers-uuids-index-abuse/</guid><description>A deep dive into why common relational database practices—random UUIDs, triggers, and over-indexing—physically destroy clustered-index storage, featuring Shopify&apos;s MySQL move to ULIDs and Instagram&apos;s custom sharding IDs on PostgreSQL.</description><pubDate>Tue, 05 Nov 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The hardest part of scaling an engineering organization is explaining to a newly hired senior developer why the “best practices” they learned building a monolithic Django application will instantly crash a multi-terabyte production system. Relational databases abstract away the disk. Engineers write SQL, and the engine handles the bytes. At a small scale, this abstraction holds. At the scale of Shopify or Instagram, this abstraction is a lie that will bankrupt your company in cloud infrastructure costs and cause cascading, invisible outages.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A rapidly growing engineering team reaches a critical inflection point. They are processing thousands of transactions per second. During a routine architecture review, a developer notices that a legacy core table uses an auto-incrementing integer (&lt;code&gt;AUTO_INCREMENT&lt;/code&gt;) as a primary key. Citing modern distributed systems theory and the risk of enumeration attacks, they submit a pull request to change the primary key to a random UUID (UUIDv4).&lt;/p&gt;
&lt;p&gt;A week later, a product manager requests an un-bypassable audit log of every user action. Rather than writing complex event-bus logic in the application tier, the team creates a database Trigger to automatically insert a row into an &lt;code&gt;audit_log&lt;/code&gt; table whenever a user is updated. Finally, the data engineering team complains about slow analytics queries on the primary read replica, so the backend team adds 15 secondary indexes to the &lt;code&gt;users&lt;/code&gt; table to ensure that any potential future &lt;code&gt;SELECT&lt;/code&gt; query runs instantly.&lt;/p&gt;
&lt;p&gt;In a vacuum, or in a textbook, these are standard, native database features. In a high-throughput MySQL or Amazon Aurora environment, this exact combination of changes will destroy the physical storage layout, multiply the AWS bill by a factor of 10, and cause silent deadlocks that take days to diagnose.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;To understand why these features break cloud-scale systems, we have to look past the SQL dialect and examine the physics of the underlying storage engine. In MySQL, the default storage engine is InnoDB. InnoDB does not store table data and index data separately. It uses a &lt;strong&gt;Clustered Index&lt;/strong&gt;, meaning the table data &lt;em&gt;is&lt;/em&gt; the leaf node of the Primary Key B+Tree. The physical layout of the data on the SSD is strictly dictated by the sequential order of the Primary Key.&lt;/p&gt;
&lt;p&gt;When you use a random UUIDv4, you are essentially asking the database to insert new records randomly across the entire physical disk space. This destroys the physical contiguity of the B+Tree.&lt;/p&gt;
&lt;p&gt;Similarly, Triggers hide the execution semantics of transactions. A transaction that appears to the application to take 2 milliseconds might actually take 5 seconds because a hidden trigger is waiting on a lock for a completely different table.&lt;/p&gt;
&lt;p&gt;Furthermore, because of the Clustered Index architecture, secondary indexes in MySQL do not point to physical row locations; they point to the Primary Key string. If your primary key is a massive 36-character UUID string, every single one of your 15 secondary indexes must store that massive string. This causes index bloat. In a distributed cloud environment like Amazon Aurora, where every physical storage write costs money, this write amplification becomes a financial catastrophe.&lt;/p&gt;
&lt;p&gt;The core question is: how do the most successful engineering organizations in the world physically model their data to avoid these anti-patterns, and what architectural compromises did they have to make?&lt;/p&gt;
&lt;h2 id=&quot;the-physical-impact-of-anti-patterns&quot;&gt;The Physical Impact of Anti-Patterns&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Application Write Workload] --&gt; B{Anti-Pattern Detected?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes — Random UUIDv4 PK| C[InnoDB Clustered Index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[Massive Page Splits]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[Index Fragmentation &amp;#x26; Buffer Pool Thrashing]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[Write Latency Spikes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes — Database Trigger| G[Hidden Execution Path]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[Silent Metadata Locks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[Cascading Connection Pool Exhaustion]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes — Speculative Secondary Indexes| J[Redo Log Amplification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[Aurora Storage Fleet]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; L[Bankrupting Write IOPS Cloud Bill]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice-shopifys-battle-with-uuidv4&quot;&gt;In Practice: Shopify’s Battle with UUIDv4&lt;/h2&gt;
&lt;p&gt;The documented pattern for MySQL’s InnoDB storage engine requires sequential inserts. When you insert a sequential value (like an Auto-Increment ID), InnoDB simply appends the data neatly to the end of the data file, keeping the B+Tree perfectly balanced and minimizing disk I/O.&lt;/p&gt;
&lt;p&gt;When you insert a random UUIDv4, InnoDB must find the exact physical block in the middle of the file where that UUID logically belongs based on its random alphanumeric sort. It loads that 16KB page into memory. If the page is full, it must perform a “page split”—breaking the page into two, reorganizing the data, and writing both pages back to disk. At 5,000 inserts per second, this causes extreme write amplification, saturating the disk I/O and pushing valuable cached data out of the InnoDB Buffer Pool.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Shopify&lt;/strong&gt;, running one of the world’s largest Ruby on Rails and MySQL architectures, hit this exact ceiling. In their payments infrastructure, they initially adopted random UUIDs to ensure global uniqueness across disparate systems. As their transaction volume grew, they noticed severe degradation in database write performance entirely tied to InnoDB page splitting and B-Tree fragmentation.&lt;/p&gt;
&lt;p&gt;Rather than vertically scaling their database instances to absurd sizes, Shopify’s engineering team migrated to &lt;strong&gt;ULIDs (Universally Unique Lexicographically Sortable Identifiers)&lt;/strong&gt;. A ULID is still 128 bits, providing the same collision resistance as a UUID. However, a ULID contains a 48-bit timestamp component at the very beginning of the string. This guarantees that ULIDs generated in chronological order will sort lexicographically.&lt;/p&gt;
&lt;p&gt;To the application, the ULID looks like a unique string. To MySQL’s physical storage engine, it looks like a sequentially increasing integer. Shopify has documented moving away from random UUIDs toward time-sortable identifiers specifically to eliminate this page-splitting behavior; I have not verified a specific write-throughput percentage from their public writeups, so treat any particular figure as illustrative rather than a cited number. The mechanism—eliminating random page splits by keeping the primary key monotonically increasing—is the well-documented part, and it restores the health of the Clustered Index regardless of the exact magnitude of the win.&lt;/p&gt;
&lt;h2 id=&quot;in-practice-instagrams-custom-sharded-ids&quot;&gt;In Practice: Instagram’s Custom Sharded IDs&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Instagram&lt;/strong&gt; faced an even more complex version of this problem. Instagram is publicly known for running on sharded PostgreSQL, and its ID-generation scheme was built specifically for that architecture. In its hyper-growth phase, it needed to generate billions of IDs for photos, users, and comments across shards.&lt;/p&gt;
&lt;p&gt;Standard auto-incrementing integers fail in a multi-shard environment because two independent database shards will generate the exact same integer ID, causing catastrophic collisions when the data is aggregated or moved. Standard 128-bit UUIDs solved the collision problem, but Instagram’s engineering team identified that a 128-bit string was simply too large. In a system serving hundreds of thousands of requests per second, the physical size of the Primary Key dictates the size of every secondary index. A 128-bit string primary key would bloat their indexes so severely that the indexes would no longer fit in RAM (PostgreSQL’s shared buffer cache), forcing the database to read from disk on every query.&lt;/p&gt;
&lt;p&gt;Instagram engineered a public, highly documented solution: a &lt;strong&gt;Custom 64-bit ID generation schema&lt;/strong&gt; implemented directly in PL/pgSQL (and later adapted across their stack). Their 64-bit ID was composed of three parts:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;41 bits&lt;/strong&gt; for a custom epoch timestamp in milliseconds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;13 bits&lt;/strong&gt; representing the logical Shard ID.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;10 bits&lt;/strong&gt; for an auto-incrementing sequence number to handle multiple inserts within the exact same millisecond.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This architectural decision perfectly satisfied the physics of the database. The 41-bit timestamp ensured the IDs were strictly sequential, preventing page splits and fragmentation. The 13-bit Shard ID guaranteed no two database shards could ever generate a colliding ID. Finally, at only 64 bits, the ID was small enough to fit inside a standard 8-byte database integer type, keeping their secondary indexes impossibly lean and ensuring the entire index working set fit in memory.&lt;/p&gt;
&lt;h2 id=&quot;the-danger-of-triggers-and-index-abuse&quot;&gt;The Danger of Triggers and Index Abuse&lt;/h2&gt;
&lt;p&gt;Beyond Primary Keys, two other common application patterns reliably destroy cloud databases.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Database Triggers&lt;/strong&gt; are often implemented by teams looking to ensure data integrity without trusting the application layer. The physical reality of a Trigger is that it executes synchronously within the transaction of the statement that fired it. If you run a simple &lt;code&gt;UPDATE users SET status = &apos;active&apos;&lt;/code&gt; that normally takes 1 millisecond, but a Trigger fires to &lt;code&gt;INSERT INTO audit_log&lt;/code&gt; which happens to be locked by an overnight batch job, your 1-millisecond query now takes 30 seconds.&lt;/p&gt;
&lt;p&gt;Because Triggers are invisible to the application source code, backend developers will spend days debugging the ORM, convinced the network is dropping packets, completely unaware that the database is silently executing hidden, blocking logic. The established engineering consensus at cloud scale is absolute: &lt;strong&gt;all business, auditing, and cascading logic belongs in the application layer, an event bus, or a Change Data Capture (CDC) stream.&lt;/strong&gt; The database should remain a dumb, lightning-fast persistence layer.&lt;/p&gt;
&lt;p&gt;Finally, &lt;strong&gt;Speculative Secondary Indexing&lt;/strong&gt; is a financial hazard. In Amazon Aurora, every physical write to the storage layer costs money (unless you are paying the premium for the I/O-Optimized tier). When you execute a single &lt;code&gt;INSERT&lt;/code&gt; statement, Aurora doesn’t just write the row once. It writes the row data, and then it must update &lt;em&gt;every single secondary index&lt;/em&gt; on that table.&lt;/p&gt;
&lt;p&gt;If a data engineering team has requested 15 secondary indexes on the &lt;code&gt;orders&lt;/code&gt; table to support various ad-hoc dashboards, one logical &lt;code&gt;INSERT&lt;/code&gt; becomes 16 physical updates to the redo log. Because Aurora replicates storage across 6 nodes in 3 Availability Zones, the network write amplification is massive. Teams that over-index their tables routinely discover that their Aurora Write IOPS billing costs outstrip their EC2 compute costs by an order of magnitude. Every secondary index must be mathematically justified by query performance requirements; speculative indexing is financially ruinous.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Anti-Pattern&lt;/th&gt;&lt;th&gt;Physical Failure Mode&lt;/th&gt;&lt;th&gt;Cloud Scale Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Random UUIDv4 Primary Keys&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;InnoDB page splitting causes massive fragmentation, disk I/O saturation, and destroys write throughput.&lt;/td&gt;&lt;td&gt;Adopt time-sortable, sequential IDs like Shopify’s ULIDs or UUIDv7.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Monolithic 128-bit Strings&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;String primary keys bloat every secondary index, pushing the working set out of the Buffer Pool.&lt;/td&gt;&lt;td&gt;Implement 64-bit Snowflake-style integer IDs like Instagram, combining timestamp and shard identity.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Database Triggers&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Hidden synchronous execution causes impossible-to-debug deadlocks and connection pool exhaustion.&lt;/td&gt;&lt;td&gt;Migrate all trigger logic to application code, Kafka event buses, or Debezium CDC streams.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Speculative Secondary Indexing&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Redo log amplification destroys write throughput and spikes Aurora IOPS billing exponentially.&lt;/td&gt;&lt;td&gt;Use the MySQL Performance Schema (&lt;code&gt;table_io_waits_summary_by_index_usage&lt;/code&gt;) to find and ruthlessly drop unused indexes.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Standard SQL features (Triggers, random UUIDs, heavy indexing) physically conflict with InnoDB’s clustered index and Aurora’s distributed log architecture, causing latency and cost explosions at scale.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Enforce strict data modeling rules during code review: sequential primary keys only, zero triggers permitted in production, and mathematically justified secondary indexes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Public engineering documentation from Shopify (ULIDs) and Instagram (64-bit sharded integers) proves that altering primary key generation away from standard UUIDs directly results in massive, measurable throughput gains in clustered relational databases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your most active transactional table today. Run a query against the &lt;code&gt;sys&lt;/code&gt; schema to identify secondary indexes with zero reads in the last 30 days. Drop them to immediately reduce your cloud bill. If the table uses a random UUIDv4 as a primary key, begin drafting an architectural RFC to migrate to UUIDv7 or ULID for future inserts.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>PostgreSQL 16/17 Features That Matter to Operators</title><link>https://rajivonai.com/blog/2024-10-24-postgresql-16-17-features-that-matter-to-operators/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-10-24-postgresql-16-17-features-that-matter-to-operators/</guid><description>Which PostgreSQL 16 and 17 changes operators actually need to prepare for: logical replication improvements, vacuum visibility, connection limits, and monitoring additions that change on-call behavior.</description><pubDate>Thu, 24 Oct 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;PostgreSQL 16 and 17 each added dozens of features. Most of them are developer-facing: new SQL syntax, function improvements, improved type support. The ones that matter to operators are a shorter list — but they change how you observe I/O, configure replication, manage access control, and run backups.&lt;/strong&gt; Upgrading to PG16 or PG17 without reviewing these operational changes means your dashboards break silently, your replication topology adds unexpected complexity, and your backup process changes in ways your runbooks do not reflect.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL follows a yearly release cadence. PG16 shipped in September 2023 and PG17 in October 2024. Both releases continue the pattern of adding features that benefit application developers — but they also change or add several infrastructure-level capabilities that operators care about more than developers do.&lt;/p&gt;
&lt;p&gt;This post covers only operationally significant changes: new system views, replication topology changes, backup improvements, and access control changes. Developer-facing features (new SQL functions, JSON improvements, etc.) are out of scope.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Operators who upgrade without reviewing the release notes typically encounter problems in three categories: monitoring breaks (a metric they relied on moved or changed format), replication complexity increases (a new capability requires opting in or opting out), or a backup workflow changes (new flags or new manifest requirements).&lt;/p&gt;
&lt;p&gt;The specific risk with PG16’s &lt;code&gt;pg_stat_io&lt;/code&gt; view: if your monitoring stack queries the old I/O metrics from &lt;code&gt;pg_stat_bgwriter&lt;/code&gt; and &lt;code&gt;pg_stat_database&lt;/code&gt;, those views still exist in PG16, but the granularity and definitions changed. Dashboards built on those views produce misleading numbers without an explicit migration.&lt;/p&gt;
&lt;p&gt;The core question for each release: which changes require action before you upgrade, and which require action after?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;The operational surface area of PostgreSQL is evolving to provide more granular observability and more flexible replication, while pushing more complexity into backup management.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Upgrade[PostgreSQL Upgrade] --&gt; Observability[Observability]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Upgrade --&gt; Replication[Replication]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Upgrade --&gt; Backup[Backup and Restore]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Observability --&gt; IO[Migrate to pg_stat_io]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Replication --&gt; Lag[Monitor standby logical lag]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Backup --&gt; Manifest[Manage backup manifests]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;pg16-operational-changes&quot;&gt;PG16 Operational Changes&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;1. &lt;code&gt;pg_stat_io&lt;/code&gt; — new I/O observability view&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PG16 introduces &lt;code&gt;pg_stat_io&lt;/code&gt;, a new system view that breaks I/O statistics down by backend type (&lt;code&gt;client backend&lt;/code&gt;, &lt;code&gt;autovacuum worker&lt;/code&gt;, &lt;code&gt;WAL writer&lt;/code&gt;, &lt;code&gt;checkpointer&lt;/code&gt;, etc.), I/O object (&lt;code&gt;relation&lt;/code&gt;, &lt;code&gt;temp relation&lt;/code&gt;), and I/O context (&lt;code&gt;normal&lt;/code&gt;, &lt;code&gt;vacuum&lt;/code&gt;, &lt;code&gt;bulkread&lt;/code&gt;). This is the most significant monitoring change in years.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; backend_type, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, context, reads, writes, extends, evictions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_io&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; reads &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Before PG16, I/O was only observable in aggregate via &lt;code&gt;pg_stat_bgwriter&lt;/code&gt; and &lt;code&gt;pg_stat_database&lt;/code&gt;. After PG16, you can see that autovacuum workers are responsible for 80% of your block reads during a vacuum storm, or that WAL writes are saturating a specific I/O context. If your existing monitoring uses &lt;code&gt;pg_stat_bgwriter.buffers_clean&lt;/code&gt; or &lt;code&gt;pg_stat_database.blks_hit&lt;/code&gt;, those fields are still present but mean something different from &lt;code&gt;pg_stat_io&lt;/code&gt; — do not mix them.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Logical replication from standby servers&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PG16 allows a physical standby (streaming replica) to act as a logical replication publication source. Before PG16, you could only create a logical replication publication on a primary. With PG16, you can offload the logical decoding CPU and I/O cost to a standby.&lt;/p&gt;
&lt;p&gt;This is valuable when logical replication fans out to many subscribers and the decoding overhead affects primary throughput. The tradeoff: if the standby falls behind the primary, logical subscribers reading from the standby see higher replication lag. You now have two lag dimensions to monitor: physical lag (primary → standby) and logical lag (standby → subscriber).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. Role membership — &lt;code&gt;GRANT ... WITH INHERIT&lt;/code&gt; behavior change&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PG16 split the previously conflated &lt;code&gt;INHERIT&lt;/code&gt; and &lt;code&gt;SET ROLE&lt;/code&gt; privileges. Before PG16, &lt;code&gt;GRANT role TO user&lt;/code&gt; always implicitly granted both inheritance and the ability to &lt;code&gt;SET ROLE&lt;/code&gt;. In PG16, these are separate:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; role&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; user &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; INHERIT TRUE;   &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- inherits privileges automatically&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; role&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; user &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; TRUE;       &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- can SET ROLE to switch to the role&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The default behavior did not change for most cases, but explicit &lt;code&gt;GRANT ... WITH INHERIT FALSE&lt;/code&gt; statements from before PG16 may behave differently in PG16 if you also relied on &lt;code&gt;SET ROLE&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. &lt;code&gt;pg_hba.conf&lt;/code&gt; and &lt;code&gt;pg_ident.conf&lt;/code&gt; now have system views&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;pg_hba_file_rules&lt;/code&gt; and &lt;code&gt;pg_ident_file_mappings&lt;/code&gt; are now reliable system views that reflect the actual loaded configuration, including any syntax errors. This replaces the need to parse config files manually for audit purposes.&lt;/p&gt;
&lt;h3 id=&quot;pg17-operational-changes&quot;&gt;PG17 Operational Changes&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;1. Incremental backup with &lt;code&gt;pg_basebackup&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PG17 added &lt;code&gt;--incremental&lt;/code&gt; support to &lt;code&gt;pg_basebackup&lt;/code&gt;. An incremental backup records only the page changes since the last full or incremental backup, using a backup manifest to track which pages changed. The full and incremental backup set must be combined with &lt;code&gt;pg_combinebackup&lt;/code&gt; before restore.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Full backup (save the manifest)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_basebackup&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -D&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /backup/base&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --checkpoint=fast&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Incremental backup&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_basebackup&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -D&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /backup/incr1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --incremental=/backup/base/backup_manifest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Combine before restore&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_combinebackup&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /backup/base&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /backup/incr1&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -o&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /backup/restored&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This changes the backup workflow: you will need to store and manage backup manifests, and the restore process requires the combine step. Teams that automate restore testing need to update their scripts before moving to PG17 backups.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Vacuum improvements — skip frozen pages&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PG17 improved VACUUM’s ability to skip pages that are already fully frozen (all tuples have transaction IDs old enough to be safe). This reduces the I/O footprint of anti-wraparound vacuums on tables with stable old data. No configuration change is needed — this is automatic. The observable effect is shorter elapsed time for VACUUM operations on large tables with significant frozen page counts.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. Logical replication of sequences (partial)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PG17 added initial sequence replication support. Sequence values can be included in a publication and replicated to a subscriber. This addresses part of the long-standing gap where logical replication subscribers had diverged sequences after promotion. This is an opt-in addition to a publication (&lt;code&gt;FOR ALL SEQUENCES&lt;/code&gt; or named sequences) and does not replicate every increment — it sends periodic snapshots of sequence state.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. MERGE — full support for &lt;code&gt;NOT MATCHED BY SOURCE&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PG17 completed the MERGE statement implementation by adding &lt;code&gt;NOT MATCHED BY SOURCE&lt;/code&gt; — the ability to delete or update rows in the target that have no matching row in the source, completing the full SQL standard MERGE semantics. This is primarily a developer feature, but it affects ETL pipelines that previously required separate DELETE and MERGE logic.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The PostgreSQL 16 release notes (postgresql.org/docs/16/release-16.html) document &lt;code&gt;pg_stat_io&lt;/code&gt; as a new view with explicit field definitions. The release notes note that several counters previously in &lt;code&gt;pg_stat_bgwriter&lt;/code&gt; are now more granularly available in &lt;code&gt;pg_stat_io&lt;/code&gt;, and that &lt;code&gt;pg_stat_bgwriter&lt;/code&gt; fields related to buffer I/O are deprecated in favor of &lt;code&gt;pg_stat_io&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The PostgreSQL 17 release documentation (&lt;a href=&quot;https://www.postgresql.org/docs/17/app-pgbasebackup.html&quot;&gt;postgresql.org/docs/17/app-pgbasebackup.html&lt;/a&gt;) specifies that &lt;code&gt;pg_combinebackup&lt;/code&gt; is the required tool for restore — it is not optional. Backup manifests are required inputs for incremental backups and must be retained between backup cycles.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Upgrading to PG16 without updating monitoring&lt;/td&gt;&lt;td&gt;I/O dashboards show stale or misleading data&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_io&lt;/code&gt; changes the metric namespace; old views still exist but have different granularity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Logical replication from standby&lt;/td&gt;&lt;td&gt;Subscribers see elevated lag when standby falls behind primary&lt;/td&gt;&lt;td&gt;Two lag dimensions compound: physical replication lag plus logical decoding lag&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;PG17 incremental backup without manifest management&lt;/td&gt;&lt;td&gt;Restore fails at &lt;code&gt;pg_combinebackup&lt;/code&gt; step&lt;/td&gt;&lt;td&gt;Incremental backups are unusable without the backup manifest from the previous full backup&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Upgrading PostgreSQL without reviewing operational changes breaks monitoring, backup automation, and replication lag calculations without any visible error at upgrade time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: For PG16, migrate I/O monitoring to &lt;code&gt;pg_stat_io&lt;/code&gt; before decommissioning old dashboard queries; for PG17, update backup scripts to retain manifests and add a &lt;code&gt;pg_combinebackup&lt;/code&gt; step to restore runbooks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: After upgrading to PG16, query &lt;code&gt;pg_stat_io&lt;/code&gt; and confirm your monitoring system is capturing backend_type-level I/O breakdown; after upgrading to PG17, execute a test incremental restore and confirm &lt;code&gt;pg_combinebackup&lt;/code&gt; completes without error.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Before upgrading to either version, grep your monitoring configuration for references to &lt;code&gt;pg_stat_bgwriter.buffers_*&lt;/code&gt; and &lt;code&gt;pg_stat_database.blks_*&lt;/code&gt; — these are the most commonly broken queries after PG16 adoption.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>checklist</category></item><item><title>Prometheus + Grafana for Database Engineers: Open-Source Monitoring That Actually Works</title><link>https://rajivonai.com/blog/2024-10-17-prometheus-grafana-database-engineers/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-10-17-prometheus-grafana-database-engineers/</guid><description>How to position Prometheus and Grafana as the open-source baseline for teams that cannot send every byte of database telemetry to managed services.</description><pubDate>Thu, 17 Oct 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;If you blindly enable every database metric exporter without understanding high-cardinality data, your monitoring stack will collapse before your database does.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Managed observability platforms like Datadog and CloudWatch are exceptionally powerful, but their pricing models are fundamentally misaligned with high-volume database metrics. If you operate massive, self-managed database fleets on bare metal or Kubernetes, sending every connection state, wait event, and table-level metric to a SaaS provider quickly becomes a top-three line item on your cloud bill.&lt;/p&gt;
&lt;p&gt;For teams running their own infrastructure, the Prometheus and Grafana stack remains the definitive open-source baseline. OpenTelemetry’s unified model for logs, metrics, and traces provides the standard vocabulary, but Prometheus is the engine that pulls the metrics. However, database engineers often struggle with Prometheus because its pull-based architecture and label-based querying (PromQL) require a different mental model than traditional agent-based monitoring.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Out of the box, a tool like &lt;code&gt;postgres_exporter&lt;/code&gt; or &lt;code&gt;mysqld_exporter&lt;/code&gt; will scrape hundreds of metrics. The immediate trap that database teams fall into is “cardinality explosion.”&lt;/p&gt;
&lt;p&gt;If you configure an exporter to scrape the execution count of every unique normalized SQL query from &lt;code&gt;pg_stat_statements&lt;/code&gt;, and you have a high-churn ORM generating thousands of unique query shapes, Prometheus will attempt to store each of those as a unique time series. Memory consumption on the Prometheus server will skyrocket, OOM kills will follow, and you will lose visibility precisely when you need it most.&lt;/p&gt;
&lt;h2 id=&quot;the-open-source-database-observability-stack&quot;&gt;The Open-Source Database Observability Stack&lt;/h2&gt;
&lt;p&gt;A production-grade open-source monitoring stack for databases requires three strictly managed layers:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The Exporter Layer:&lt;/strong&gt; This is a lightweight process (e.g., &lt;code&gt;postgres_exporter&lt;/code&gt;) running alongside the database. It translates internal database states into the text-based exposition format Prometheus expects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Scrape Configuration:&lt;/strong&gt; The Prometheus server pulls data from the exporter at a defined interval (e.g., every 15 seconds). This is where you must aggressively filter out high-cardinality labels using &lt;code&gt;metric_relabel_configs&lt;/code&gt; to drop metrics you do not actively alert on.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Alerting Rules:&lt;/strong&gt; Raw metrics are useless during an incident. You must define Prometheus recording rules to pre-calculate expensive metrics (like the 5-minute rate of disk I/O) and alerting rules (e.g., alert if the connection pool is &gt;90% saturated for 3 minutes).&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for surviving Prometheus at scale involves ruthless metric dropping.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; The &lt;code&gt;mysqld_exporter&lt;/code&gt; default configuration exposes &lt;code&gt;mysql_perf_schema_events_statements_total&lt;/code&gt;, which creates one time series per unique normalized query digest tracked by the Performance Schema. On an ORM-driven application generating thousands of unique query shapes, this single metric produces hundreds of thousands of unique time series. Prometheus’s documentation on instrumentation best practices explicitly warns that unbounded label values — like &lt;code&gt;digest&lt;/code&gt; or &lt;code&gt;query_hash&lt;/code&gt; — cause memory growth proportional to the number of unique label combinations, and recommends against high-cardinality dimensions in metric labels (&lt;a href=&quot;https://prometheus.io/docs/practices/instrumentation/&quot;&gt;Prometheus: Instrumentation best practices&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The documented mitigation is a &lt;code&gt;metric_relabel_configs&lt;/code&gt; block with a &lt;code&gt;drop&lt;/code&gt; action targeting &lt;code&gt;mysql_perf_schema_events_statements_total&lt;/code&gt; in the Prometheus scrape configuration, combined with a replacement custom collector query that exports only the top-N slowest statements by total execution time from &lt;code&gt;performance_schema.events_statements_summary_by_digest&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The Prometheus TSDB status page (&lt;code&gt;/tsdb-status&lt;/code&gt;) exposes the top-10 highest-cardinality metrics by series count — this is the diagnostic that reveals which exporter metric is consuming the majority of Prometheus server memory before it OOM-kills.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Prometheus is an operational alerting database, not a data lake. The test for any scraped metric: does it drive an alert or a live dashboard panel? If not, drop it at the scrape layer rather than ingesting it and paying the memory cost.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Relying on Prometheus and Grafana involves significant operational tradeoffs compared to managed services:&lt;/p&gt;























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Approach&lt;/th&gt;&lt;th&gt;Advantage&lt;/th&gt;&lt;th&gt;Disadvantage&lt;/th&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Prometheus (Self-Hosted)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Zero variable cost for high data volume; complete control over scrape intervals.&lt;/td&gt;&lt;td&gt;You must manage the storage, backups, and high availability of the monitoring stack yourself.&lt;/td&gt;&lt;td&gt;The Prometheus server runs out of disk space and stops recording metrics during an outage.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Datadog / Managed SaaS&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Zero maintenance; built-in correlation between logs, traces, and metrics.&lt;/td&gt;&lt;td&gt;High-cardinality custom metrics incur massive monthly costs.&lt;/td&gt;&lt;td&gt;Finance forces engineering to drop critical metrics to meet budget constraints.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Database teams deploy &lt;code&gt;postgres_exporter&lt;/code&gt; or &lt;code&gt;mysqld_exporter&lt;/code&gt; with default settings, then watch the Prometheus server OOM-kill itself from cardinality explosion within days — the monitoring stack fails before the database does.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Apply &lt;code&gt;metric_relabel_configs&lt;/code&gt; to drop high-cardinality per-query metrics on every new exporter deployment, and replace them with a targeted custom collector that exports only top-N slowest queries by total execution time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Check your Prometheus TSDB status page (&lt;code&gt;/tsdb-status&lt;/code&gt;) — if any single metric family consumes more than 10% of total series, you have a cardinality problem that will eventually crash the server under incident load.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit current exporters via the TSDB status page this week and drop any metric not tied to an active alerting rule or dashboard panel — treat every unalerted metric as operational overhead with a memory cost.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category><category>checklist</category></item><item><title>MongoDB Queryable Encryption Internals: FLE2, Structured Encryption, and the Envelope Key Architecture</title><link>https://rajivonai.com/blog/2024-10-16-mongodb-80-queryable-encryption-matters/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-10-16-mongodb-80-queryable-encryption-matters/</guid><description>MongoDB Queryable Encryption lets the server evaluate equality and range queries against ciphertext without ever holding plaintext — using a structured encryption scheme built on AEAD, a two-tier DEK/CMK key hierarchy, and a libmongocrypt proxy that intercepts and transforms driver operations. This post explains the cryptographic architecture, key lifecycle, metadata index structure, and where the query constraint boundaries are drawn.</description><pubDate>Wed, 16 Oct 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;MongoDB Queryable Encryption solves a problem that standard encryption-at-rest and standard field-level encryption cannot: it allows the database server to evaluate a query predicate against an encrypted field without the server ever holding the field’s plaintext. Standard encryption-at-rest encrypts the storage layer — but decrypts every page into memory the moment a query accesses it. Standard field-level encryption (FLE v1) stores ciphertext at rest but requires the client to fetch documents and decrypt them before any field-level filtering can occur. Queryable Encryption (QE, also called FLE2 internally) is a third model: the client sends a cryptographically structured query token; the server evaluates the token against a specialized metadata index without decrypting the field; the server returns matching documents in ciphertext; the client decrypts the results. The server never sees plaintext for any QE-encrypted field, at any point in the query lifecycle.&lt;/p&gt;
&lt;p&gt;This distinction matters specifically when the threat model includes privileged database access: DBAs with &lt;code&gt;db.auth()&lt;/code&gt; credentials, MongoDB Atlas operational staff with infrastructure access, or an attacker who compromises the mongod process. Under standard encryption-at-rest, all these actors can query and read plaintext. Under QE, they see only ciphertext — bounded by the cryptographic scheme, not by access controls.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;MongoDB Queryable Encryption reached General Availability in MongoDB 7.0 (GA 2023-08-15) for equality queries. MongoDB 8.0 (GA 2024-10-02) expanded the supported query types to include range queries with configurable precision. Understanding the architecture requires distinguishing between the three encryption schemes MongoDB provides:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Encryption Scheme&lt;/th&gt;&lt;th&gt;Server Sees&lt;/th&gt;&lt;th&gt;Server Can Query&lt;/th&gt;&lt;th&gt;Threat Model Protected Against&lt;/th&gt;&lt;th&gt;Available Since&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Encryption-at-rest (WiredTiger)&lt;/td&gt;&lt;td&gt;Plaintext in memory during queries&lt;/td&gt;&lt;td&gt;Full query capability&lt;/td&gt;&lt;td&gt;Stolen storage media, offline backup theft&lt;/td&gt;&lt;td&gt;MongoDB 3.2+ (WiredTiger default)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;FLE v1 — Explicit Client-Side Encryption&lt;/td&gt;&lt;td&gt;Ciphertext stored, but client must fetch+decrypt before filtering&lt;/td&gt;&lt;td&gt;No server-side filtering on encrypted fields&lt;/td&gt;&lt;td&gt;Storage theft only — not privileged DB access&lt;/td&gt;&lt;td&gt;MongoDB 4.2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;QE (FLE v2) — Queryable Encryption&lt;/td&gt;&lt;td&gt;Ciphertext only — never decrypted on server&lt;/td&gt;&lt;td&gt;Equality and range queries via structured encryption&lt;/td&gt;&lt;td&gt;Storage theft AND privileged database access during normal operation&lt;/td&gt;&lt;td&gt;MongoDB 7.0 (equality), 8.0 (range)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;In-Use Encryption (future)&lt;/td&gt;&lt;td&gt;—&lt;/td&gt;&lt;td&gt;—&lt;/td&gt;&lt;td&gt;Full homomorphic — arbitrary queries&lt;/td&gt;&lt;td&gt;Not yet GA&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The threat model that QE is specifically designed for: a regulated enterprise where the database team, cloud provider, or infrastructure operators must be excluded from reading sensitive field values (SSN, salary, medical record IDs, PII) while the application must still be able to query those fields efficiently — without fetching entire collections to the application layer for in-memory filtering.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Understanding QE’s constraints requires understanding what the server is actually doing when it evaluates an encrypted query — and why the scheme cannot be extended to arbitrary query types without fundamentally changing the cryptographic foundation.&lt;/p&gt;
&lt;p&gt;Standard AEAD (Authenticated Encryption with Associated Data) encryption — the basis for FLE v1 — uses a random nonce per encryption operation, producing a different ciphertext for the same plaintext every time. This is a security property: identical plaintexts produce different ciphertexts, preventing frequency analysis. But it also means the server has no way to determine whether two encrypted values are equal — the ciphertexts are opaque to the server.&lt;/p&gt;
&lt;p&gt;QE uses a different cryptographic primitive: a &lt;em&gt;structured encryption scheme&lt;/em&gt; (specifically, a form of order-revealing encryption for range queries and deterministic encryption for equality). The server can evaluate equality by comparing encrypted tokens without learning the underlying plaintext. Range queries are supported through a specialized range tree structure that allows order comparisons on ciphertext — but only within the precision and bounds declared at field creation time.&lt;/p&gt;
&lt;p&gt;The operational consequence: &lt;strong&gt;QE field constraints are immutable at creation time&lt;/strong&gt;. A field created with &lt;code&gt;queryType: &quot;equality&quot;&lt;/code&gt; cannot later have range query support added without dropping and recreating the collection (including re-encrypting all documents). Teams that implement QE without thoroughly mapping all future query patterns against each field are building schema debt into an encrypted layer where migrations are expensive.&lt;/p&gt;
&lt;p&gt;How do you architect QE field declarations to satisfy both current and anticipated query requirements, and what are the exact cryptographic boundaries that determine which query types can never be server-side evaluated?&lt;/p&gt;
&lt;h2 id=&quot;the-fle2-cryptographic-architecture-dek-hierarchy-libmongocrypt-and-metadata-indexes&quot;&gt;The FLE2 Cryptographic Architecture: DEK Hierarchy, libmongocrypt, and Metadata Indexes&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;The envelope key hierarchy.&lt;/strong&gt; QE uses a two-tier key architecture:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CMK[Customer Master Key — CMK — stored in KMS — AWS KMS — Azure Key Vault — GCP KMS] --&gt; DEK[Data Encryption Key — DEK — encrypted by CMK — stored in key vault collection]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DEK --&gt; FC[Field-level encryption — each QE field encrypted with its own DEK]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    FC --&gt; EQ[Equality ciphertext — deterministic per plaintext — stored in document]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    FC --&gt; META[Metadata index entries — encrypted bloom filter tokens — stored in __safeContent__ array]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The Customer Master Key (CMK) never leaves the KMS. The Data Encryption Key (DEK) is a per-field or per-collection key that is: generated client-side, encrypted using the CMK via KMS API, and stored encrypted in a key vault collection in the same MongoDB database or a separate cluster. The DEK is fetched from the key vault and decrypted by the KMS at query time — meaning two round-trips to KMS are required for each unique DEK used in a query: one to fetch the encrypted DEK from MongoDB, one to decrypt the DEK via KMS.&lt;/p&gt;
&lt;p&gt;The cryptographic separation is the security property: an attacker with access to the MongoDB database sees the encrypted DEK in the key vault collection but cannot decrypt it without KMS access. An attacker with KMS access but no MongoDB access has the decryption capability but no ciphertext. Compromising either layer alone does not expose plaintext.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;libmongocrypt — the encryption proxy.&lt;/strong&gt; QE is implemented in &lt;code&gt;libmongocrypt&lt;/code&gt;, a C library embedded in MongoDB drivers (Node.js, Python, Java, Go, .NET, etc.). When the application sends a query against a QE-encrypted field, the driver intercepts the operation before it reaches the wire, passes it to &lt;code&gt;libmongocrypt&lt;/code&gt;, which:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Fetches the DEK for the field from the key vault collection (MongoDB read)&lt;/li&gt;
&lt;li&gt;Decrypts the DEK using the configured KMS provider (KMS API call)&lt;/li&gt;
&lt;li&gt;Encrypts the query predicate value using the DEK and the appropriate QE scheme (deterministic for equality, range tree for range)&lt;/li&gt;
&lt;li&gt;Substitutes the encrypted token into the query document&lt;/li&gt;
&lt;li&gt;Sends the encrypted query to the server&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The server receives and evaluates the encrypted query against the stored ciphertext. This interception is transparent to the application — the application uses standard MongoDB query syntax; &lt;code&gt;libmongocrypt&lt;/code&gt; handles the transformation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Metadata indexes — the &lt;code&gt;__safeContent__&lt;/code&gt; array.&lt;/strong&gt; For equality queries, QE stores deterministic ciphertext (the same plaintext produces the same ciphertext) directly in the document field. The server compares ciphertexts directly using its standard B-Tree index. For range queries, QE stores a &lt;code&gt;__safeContent__&lt;/code&gt; array on each document — an array of encrypted bloom filter tokens that encode the range position of the field value within the declared min/max bounds. The server queries this array using a specialized range metadata index to identify documents whose range tokens match the query range.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;__safeContent__&lt;/code&gt; array is automatically maintained by &lt;code&gt;libmongocrypt&lt;/code&gt; — it is not application-visible and should not be manually modified. Modifying it breaks the range query evaluation without producing an error — queries return incorrect results silently.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Setting up QE — field map declaration.&lt;/strong&gt; The encrypted fields map is the most consequential configuration decision in a QE deployment:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Python with pymongo — QE collection creation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;from pymongo &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; MongoClient&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo.encryption import ClientEncryption&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pymongo.encryption_options import AutoEncryptionOpts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;# Key vault collection (stores encrypted DEKs)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;KEY_VAULT_NAMESPACE = &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;encryption.__keyVault&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;# KMS provider configuration — AWS KMS example&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;kms_providers = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;aws&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;accessKeyId&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: os.environ[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;AWS_ACCESS_KEY_ID&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;secretAccessKey&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: os.environ[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;AWS_SECRET_ACCESS_KEY&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;# CMK ARN — the Customer Master Key in AWS KMS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;cmk_arn = &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;arn:aws:kms:us-east-1:123456789:key/your-cmk-key-id&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;# Create ClientEncryption object for key operations&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;client_encryption = ClientEncryption(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    kms_providers=kms_providers,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    key_vault_namespace=KEY_VAULT_NAMESPACE,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    key_vault_client=MongoClient(MONGODB_URI),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    codec_options=CodecOptions(uuid_representation=STANDARD),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;# Create a DEK for the SSN field (stored encrypted in key vault)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ssn_dek_id = client_encryption.create_data_key(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;aws&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    master_key={&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: cmk_arn, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;region&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;us-east-1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    key_alt_names=[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ssn-field-key&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]  # Named key for easier reference&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;# Create a DEK for the salary field&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;salary_dek_id = client_encryption.create_data_key(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;aws&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    master_key={&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: cmk_arn, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;region&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;us-east-1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    key_alt_names=[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;salary-field-key&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;# Encrypted fields map — define per-field encryption parameters&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;encrypted_fields_map = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;encrypted_hr.employees&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;fields&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;path&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ssn&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;bsonType&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;string&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;keyId&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: ssn_dek_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;queries&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [{&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;queryType&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;equality&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                # NOTE: equality uses deterministic encryption&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                # The same SSN always produces the same ciphertext&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                # This allows equality comparison without decryption&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;path&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;salary&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;bsonType&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;int&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;keyId&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: salary_dek_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;queries&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                    &quot;queryType&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;range&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                    &quot;min&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: 0,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                    &quot;max&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: 5000000,  # Must encompass all actual salary values&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                    &quot;sparsity&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: 1,   # Tree depth: lower = fewer false positives, larger index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                    &quot;trimFactor&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: 6  # Controls bloom filter density (MongoDB 8.0+)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                }]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                # Fields without a &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;queries&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; entry are encrypted but NOT queryable server-side&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;path&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;medical_record_id&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;bsonType&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;string&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                &quot;keyId&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: ssn_dek_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                # No &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;queries&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; key — this field is encrypted but never queried on server&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;# Auto-encryption options — libmongocrypt intercepts queries transparently&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;auto_encryption_opts = AutoEncryptionOpts(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    kms_providers=kms_providers,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    key_vault_namespace=KEY_VAULT_NAMESPACE,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    encrypted_fields_map=encrypted_fields_map,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;# Client with auto-encryption enabled&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;encrypted_client = MongoClient(MONGODB_URI, auto_encryption_opts=auto_encryption_opts)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Querying QE-encrypted fields.&lt;/strong&gt; The application uses standard MongoDB query syntax — the driver’s &lt;code&gt;libmongocrypt&lt;/code&gt; intercepts and transforms the query transparently:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Standard MongoDB query — libmongocrypt encrypts the predicate before sending&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Find employees with a specific SSN (equality query)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;employee &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; encrypted_client[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;encrypted_hr&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;][&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;employees&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;].find_one(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;ssn&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;123-45-6789&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}   &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# libmongocrypt encrypts &quot;123-45-6789&quot; using SSN DEK before send&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Server receives: {&quot;ssn&quot;: BinData(6, &quot;&amp;#x3C;encrypted_token&gt;&quot;)}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Server evaluates equality on encrypted tokens — no plaintext on server&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Range query on salary (range query — requires range queryType configured above)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;high_earners &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(encrypted_client[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;encrypted_hr&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;][&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;employees&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;].find(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;salary&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$gte&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;200000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$lte&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;500000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# libmongocrypt converts to range tokens using the salary DEK and range scheme&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Verifying encryption at rest — server perspective.&lt;/strong&gt; Connecting to MongoDB without encryption configuration shows only ciphertext:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Unencrypted connection — what the DBA / attacker sees&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;use encrypted_hr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.employees.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;findOne&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({}, {ssn: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, salary: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Returns:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;//   &quot;_id&quot;: ObjectId(&quot;...&quot;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;//   &quot;ssn&quot;: BinData(6, &quot;AQAAAAAAAAAAAAAAAAAAAA...&quot;),   // ciphertext — DBA cannot read&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;//   &quot;salary&quot;: BinData(6, &quot;AgAAAAAAAAAAAAAAAAAAAA...&quot;), // ciphertext — DBA cannot read&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;//   &quot;__safeContent__&quot;: [BinData(0, &quot;...&quot;), ...]        // range metadata — opaque&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;KMS latency impact.&lt;/strong&gt; Each distinct DEK used in a query session requires a KMS decrypt call. For a query that touches 3 QE-encrypted fields with 3 different DEKs, the driver makes 3 KMS API calls before sending the query. AWS KMS p99 latency is approximately 10–30ms per decrypt call. A query touching 3 DEKs adds 30–90ms of KMS overhead — acceptable for infrequent sensitive data lookups, problematic for high-frequency OLTP paths.&lt;/p&gt;
&lt;p&gt;Mitigation: &lt;code&gt;libmongocrypt&lt;/code&gt; caches decrypted DEKs in memory for the lifetime of the encrypted client instance. DEKs for frequently used fields are decrypted once and reused across queries in the same application process. Size the DEK cache (&lt;code&gt;MongoClient&lt;/code&gt; is long-lived in application servers) and limit the number of distinct DEKs per collection to minimize cold-start KMS overhead.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Identifying QE metadata indexes.&lt;/strong&gt; QE creates internal metadata indexes not visible via &lt;code&gt;db.collection.getIndexes()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// QE creates two internal collections per encrypted collection:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// - &amp;#x3C;collection&gt;.esc  — Equality State Collection (tracks encrypted equality entries)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// - &amp;#x3C;collection&gt;.ecoc — Encrypted Compaction Output Collection (for compaction)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// These appear as regular collections but should never be manually modified&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getCollectionNames&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;includes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;.esc&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;includes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;.ecoc&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Returns: [&quot;employees.esc&quot;, &quot;employees.ecoc&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// After frequent inserts/updates, run compaction to prevent __safeContent__ bloat:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;runCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({compactStructuredEncryptionData: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;employees&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Root Cause&lt;/th&gt;&lt;th&gt;Production Blast Radius&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Unsupported query type on QE field — regex, text, $exists&lt;/td&gt;&lt;td&gt;Application evolves to need a query type not declared at field creation (text search, regex, &lt;code&gt;$elemMatch&lt;/code&gt;); these cannot be evaluated server-side against QE ciphertext&lt;/td&gt;&lt;td&gt;Query falls back to collection scan with application-layer decryption of every document — O(N) latency for any collection size&lt;/td&gt;&lt;td&gt;Map all required query patterns per sensitive field before implementing QE; if open-ended query requirements are anticipated, use FLE v1 (decrypt before filter) or keep the field plaintext with application-layer access controls&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;KMS unavailability — application cannot query&lt;/td&gt;&lt;td&gt;The KMS provider (AWS KMS, Azure Key Vault) is unreachable or rate-limited; &lt;code&gt;libmongocrypt&lt;/code&gt; cannot decrypt DEKs; all QE-encrypted field operations fail&lt;/td&gt;&lt;td&gt;Application cannot read or write QE-encrypted fields; non-QE fields continue to work normally; the encrypted fields degrade to inaccessible&lt;/td&gt;&lt;td&gt;Implement KMS DEK caching with appropriate TTL (1–4 hours); use KMS key replication across regions; test KMS failure scenarios explicitly in disaster recovery runbooks&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;__safeContent__&lt;/code&gt; modification — silent query corruption&lt;/td&gt;&lt;td&gt;An application or DBA script modifies the &lt;code&gt;__safeContent__&lt;/code&gt; array directly (e.g., during a data repair operation)&lt;/td&gt;&lt;td&gt;Range queries return incorrect results without error; equality queries are unaffected (equality does not use &lt;code&gt;__safeContent__&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;Implement field-level access controls on the &lt;code&gt;__safeContent__&lt;/code&gt; field in the MongoDB role definitions; add automated tests that verify query results match expected counts after any data maintenance operation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Range query bounds mismatch — documents outside declared range&lt;/td&gt;&lt;td&gt;A salary range query field is declared with &lt;code&gt;max: 500000&lt;/code&gt;; a document is inserted with salary &lt;code&gt;750000&lt;/code&gt;; the salary value falls outside the declared range tree&lt;/td&gt;&lt;td&gt;The document is inserted successfully but is never returned by range queries for values above &lt;code&gt;500000&lt;/code&gt;; no error is produced&lt;/td&gt;&lt;td&gt;Validate all QE range field values against the declared min/max bounds at insert time in the application layer; set declared bounds 2–3× above the maximum expected value; treat bound changes as migrations requiring collection rebuild&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DEK rotation — re-encryption not atomic&lt;/td&gt;&lt;td&gt;The security team rotates the DEK (creates a new DEK, re-encrypts all documents); during rotation, some documents are encrypted with old DEK, some with new DEK; if rotation fails midway, a mixed-key collection exists&lt;/td&gt;&lt;td&gt;Reads succeed but write operations with the new DEK fail for documents still encrypted with old DEK until rotation completes; if rotation is abandoned, the collection state is inconsistent&lt;/td&gt;&lt;td&gt;Use MongoDB’s &lt;code&gt;rewrapManyDataKey&lt;/code&gt; API for DEK rotation — it handles the re-encryption atomically per document; run rotation during low-traffic windows; verify rotation completion before switching application to the new DEK&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Compaction neglect — &lt;code&gt;__safeContent__&lt;/code&gt; bloat&lt;/td&gt;&lt;td&gt;QE’s range encryption scheme appends to &lt;code&gt;__safeContent__&lt;/code&gt; on each update to a range-encrypted field; without periodic &lt;code&gt;compactStructuredEncryptionData&lt;/code&gt; commands, &lt;code&gt;__safeContent__&lt;/code&gt; arrays grow indefinitely&lt;/td&gt;&lt;td&gt;Per-document storage size grows with update count; collection storage expands beyond expected footprint; range query evaluation slows as server processes larger &lt;code&gt;__safeContent__&lt;/code&gt; arrays&lt;/td&gt;&lt;td&gt;Schedule &lt;code&gt;compactStructuredEncryptionData&lt;/code&gt; as a regular maintenance operation (weekly for high-update QE collections); monitor &lt;code&gt;db.collection.stats().avgObjSize&lt;/code&gt; for unexpected growth&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Audit log reveals query patterns&lt;/td&gt;&lt;td&gt;MongoDB’s audit log records the encrypted query tokens sent to the server; a privileged auditor who can correlate repeated identical tokens can infer that the same plaintext value is being queried repeatedly — even without breaking the encryption&lt;/td&gt;&lt;td&gt;Frequency analysis on audit logs can reveal which encrypted values are most frequently queried; for equality-encrypted low-cardinality fields (gender, status), this reveals distribution information&lt;/td&gt;&lt;td&gt;Use QE equality encryption only on high-cardinality fields (SSN, account ID); for low-cardinality sensitive fields, consider application-layer tokenization (store a non-sensitive token, maintain the mapping in a separate secure store)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Production schema migration — adding QE to existing field&lt;/td&gt;&lt;td&gt;A team adds QE encryption to an existing plaintext field in a running production collection; existing documents retain plaintext values while new documents receive ciphertext&lt;/td&gt;&lt;td&gt;Mixed collection: queries with QE configured only match documents with ciphertext (new documents); documents with plaintext values for the field are not returned by QE queries&lt;/td&gt;&lt;td&gt;There is no in-place migration path for adding QE to an existing field; the collection must be rebuilt — read all documents in plaintext, insert into a new QE-configured collection; this is a planned migration, not an in-place ALTER&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;2025-mongodb-80-queryable-encryption-update&quot;&gt;2025 MongoDB 8.0 Queryable Encryption Update&lt;/h2&gt;
&lt;p&gt;MongoDB 8.0 (GA October 2024) added &lt;code&gt;trimFactor&lt;/code&gt; as a configurable parameter for range-encrypted fields — controlling the density of the bloom filter tokens stored in &lt;code&gt;__safeContent__&lt;/code&gt;. A higher &lt;code&gt;trimFactor&lt;/code&gt; (default 6) reduces the number of metadata tokens per document (smaller storage footprint, faster compaction) at the cost of reduced query precision (wider range scan on the metadata index). Lower &lt;code&gt;trimFactor&lt;/code&gt; values increase metadata token density, narrowing range scans but increasing per-document storage overhead. For high-cardinality range fields (salary, date of birth, account balance), &lt;code&gt;trimFactor: 4&lt;/code&gt; reduces false positives in range evaluation; for lower-precision needs, &lt;code&gt;trimFactor: 8&lt;/code&gt; reduces storage overhead.&lt;/p&gt;
&lt;p&gt;MongoDB 8.0 also introduced &lt;code&gt;contentionFactor&lt;/code&gt; as a configurable parameter (previously fixed internally) — controlling how QE handles concurrent writes to the same field value. A higher &lt;code&gt;contentionFactor&lt;/code&gt; reduces write contention at the cost of increased metadata entries per distinct value. For fields with high write concurrency on the same values (e.g., a &lt;code&gt;status&lt;/code&gt; field with values shared across many documents), &lt;code&gt;contentionFactor: 8&lt;/code&gt; reduces lock contention at the QE metadata level.&lt;/p&gt;
&lt;p&gt;The MongoDB Query API now includes &lt;code&gt;$encryptedField&lt;/code&gt; as a hint in aggregation pipelines to explicitly route pipeline stages to client-side decryption when a pipeline expression cannot be evaluated server-side against QE ciphertext — replacing the previous behavior where unsupported pipeline stages silently fell back to full collection decryption without warning.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; MongoDB Queryable Encryption’s cryptographic scheme supports only equality and range query types for server-side evaluation; teams that implement QE without mapping all future query patterns per field discover the constraint as a schema migration requirement when a new feature requires text search or regex on an encrypted field — at which point the field must be re-encrypted in a new collection.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Map every anticipated query type for each sensitive field before implementing QE — equality-only fields use deterministic AEAD (same plaintext → same ciphertext); range fields require declaring min/max bounds and sparsity at creation time; any field requiring text search, regex, or complex aggregation cannot use QE and needs a different protection strategy.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; MongoDB’s libmongocrypt source code (MIT licensed on GitHub) documents the structured encryption scheme used for QE — the server-side metadata index stores encrypted bloom filter tokens, not plaintext, confirming that the server evaluates queries by comparing encrypted tokens against the stored tokens without any decryption path on the server side.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your current schema for fields that contain PII, financial data, or health information and that are queried with equality or range predicates — these are the exact QE candidates; run the &lt;code&gt;db.collection.stats()&lt;/code&gt; size check after enabling QE on a staging collection to measure the &lt;code&gt;__safeContent__&lt;/code&gt; storage overhead before committing to production.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;MongoDB Manual — Queryable Encryption Architecture and Supported Query Types: &lt;a href=&quot;https://www.mongodb.com/docs/manual/core/queryable-encryption/&quot;&gt;https://www.mongodb.com/docs/manual/core/queryable-encryption/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB 8.0 Release Notes — Range Query Encryption Enhancements and trimFactor: &lt;a href=&quot;https://www.mongodb.com/docs/manual/release-notes/8.0/&quot;&gt;https://www.mongodb.com/docs/manual/release-notes/8.0/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB libmongocrypt Source Code — Structured Encryption Implementation: &lt;a href=&quot;https://github.com/mongodb/libmongocrypt&quot;&gt;https://github.com/mongodb/libmongocrypt&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — ClientEncryption API Reference — createDataKey and encrypt: &lt;a href=&quot;https://www.mongodb.com/docs/manual/reference/method/ClientEncryption/&quot;&gt;https://www.mongodb.com/docs/manual/reference/method/ClientEncryption/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;AWS KMS Latency and Throughput Limits — Service Quotas: &lt;a href=&quot;https://docs.aws.amazon.com/kms/latest/developerguide/limits.html&quot;&gt;https://docs.aws.amazon.com/kms/latest/developerguide/limits.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;USENIX Security 2019 — Structured Encryption and its Applications: &lt;a href=&quot;https://www.usenix.org/conference/usenixsecurity19/presentation/kamara&quot;&gt;https://www.usenix.org/conference/usenixsecurity19/presentation/kamara&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — Compacting QE Collections — compactStructuredEncryptionData: &lt;a href=&quot;https://www.mongodb.com/docs/manual/reference/command/compactStructuredEncryptionData/&quot;&gt;https://www.mongodb.com/docs/manual/reference/command/compactStructuredEncryptionData/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>architecture</category></item><item><title>Datadog Database Monitoring: PostgreSQL, MySQL, and Aurora Setup</title><link>https://rajivonai.com/blog/2024-10-14-datadog-database-monitoring-setup-postgres-mysql-aurora/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-10-14-datadog-database-monitoring-setup-postgres-mysql-aurora/</guid><description>How to configure Datadog Database Monitoring for PostgreSQL, MySQL, and Aurora — query samples, explain plans, wait event analysis, and the specific Agent settings that make the difference between metric collection and real observability.</description><pubDate>Mon, 14 Oct 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Datadog Database Monitoring is not just metrics collection with a nicer UI — it ships query-level explain plans, wait event breakdown, and connection pool visibility without requiring &lt;code&gt;pg_stat_statements&lt;/code&gt; configuration or custom PromQL recording rules. The mistake is enabling it and leaving all sampling and explain plan collection at defaults, which produces query data that is too sparse to diagnose production slowdowns.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Teams running Datadog for application performance monitoring have a strong reason to use it for database monitoring too: one dashboard, one query language, and automatic correlation between slow application traces and the database queries those traces hit. The alternative — running a separate Prometheus stack with postgres_exporter, custom recording rules, and Grafana — is operationally heavier for teams that are not already Prometheus-native.&lt;/p&gt;
&lt;p&gt;Datadog Database Monitoring (DBM) covers PostgreSQL, MySQL, Aurora PostgreSQL, Aurora MySQL, SQL Server, and Oracle. This post focuses on PostgreSQL and MySQL/Aurora MySQL — the two most common open-source targets.&lt;/p&gt;
&lt;p&gt;The challenge is not installation. The challenge is that defaults produce incomplete data: explain plans are sampled at a low rate, wait event tracking requires explicit enabling, and the Agent needs database-side configuration (a dedicated monitoring user with the right grants) that Datadog’s quickstart guide underspecifies.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Symptom in Datadog DBM&lt;/th&gt;&lt;th&gt;Likely cause&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Query samples show “no explain plan available”&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_statements&lt;/code&gt; not in &lt;code&gt;shared_preload_libraries&lt;/code&gt;, or explain plan sampling rate is too low&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slow query visible in APM but not in DBM&lt;/td&gt;&lt;td&gt;Query duration is below DBM’s configured min duration threshold&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Wait events show only “ClientRead”&lt;/td&gt;&lt;td&gt;&lt;code&gt;track_activity_query_size&lt;/code&gt; too small; truncating queries before DBM can match them&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aurora read replicas not appearing in DBM&lt;/td&gt;&lt;td&gt;Agent not configured to connect to the reader endpoint separately&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High DBM Agent CPU on the database host&lt;/td&gt;&lt;td&gt;Explain plan collection running too frequently; throttle via &lt;code&gt;explain_statement_min_duration&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Connection count in DBM does not match &lt;code&gt;pg_stat_activity&lt;/code&gt;&lt;/td&gt;&lt;td&gt;DBM is reading from &lt;code&gt;pg_stat_activity&lt;/code&gt; but the monitoring user lacks &lt;code&gt;pg_monitor&lt;/code&gt; role&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;1. Is the monitoring user configured with the right grants?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For PostgreSQL:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; USER&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; datadog&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; WITH&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; password&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;use-secret-manager-here&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_monitor &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datadog;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Required for query samples and explain plans:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SCHEMA&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; datadog&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; USAGE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SCHEMA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datadog &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datadog;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; USAGE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SCHEMA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; public &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datadog;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_read_all_stats &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datadog;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Function required for DBM explain plan collection:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE OR REPLACE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FUNCTION&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; datadog&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.explain_statement(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;   l_query &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TEXT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;   OUT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; explain &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JSON&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;RETURNS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SETOF &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JSON&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $$&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DECLARE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;curs REFCURSOR;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;plan &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JSON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;   OPEN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; curs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FOR&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; EXECUTE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; pg_catalog&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;concat&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;EXPLAIN (FORMAT JSON) &apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, l_query);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;   FETCH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; curs &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INTO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; plan;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;   CLOSE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; curs;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;   RETURN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; QUERY &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; plan;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$$&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LANGUAGE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;plpgsql&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;RETURNS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NULL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; INPUT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SECURITY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; DEFINER;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;SECURITY DEFINER&lt;/code&gt; function is required because DBM collects explain plans for queries run by other users — the monitoring role does not have execution rights on arbitrary user queries.&lt;/p&gt;
&lt;p&gt;For MySQL/Aurora MySQL:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; USER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &apos;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;datadog&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;&apos;@&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; IDENTIFIED &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; mysql_native_password &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BY&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;use-secret-manager-here&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; REPLICATION CLIENT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TO&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;datadog&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROCESS &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TO&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;datadog&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; performance_schema.&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TO&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;datadog&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- For explain plan collection:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sys.&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TO&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;datadog&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;2. Is &lt;code&gt;pg_stat_statements&lt;/code&gt; enabled?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW shared_preload_libraries;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Must include &apos;pg_stat_statements&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- If missing, add to postgresql.conf and restart:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- shared_preload_libraries = &apos;pg_stat_statements&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- After restart, verify:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_extension &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; extname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;pg_stat_statements&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- If absent: CREATE EXTENSION pg_stat_statements;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Tune:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SYSTEM&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; pg_stat_statements&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;max&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SYSTEM&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; pg_stat_statements&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;track&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;all&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SYSTEM&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; track_activity_query_size &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 4096&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_reload_conf();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;track_activity_query_size&lt;/code&gt; defaults to 1024 bytes in PostgreSQL 13 and earlier. Queries longer than this are truncated in &lt;code&gt;pg_stat_activity&lt;/code&gt;, which prevents DBM from matching query samples to their explain plans.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. Is the Datadog Agent configured for DBM?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;/etc/datadog-agent/conf.d/postgres.d/conf.yaml&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;init_config&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;instances&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;host&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;your-db-host&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    port&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5432&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    username&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;datadog&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    password&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;ENC[your-secret]&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;   # use Datadog secret management&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    dbname&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;your_database&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    # Enable Database Monitoring:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    dbm&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    # Query metrics — increase statement cache:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    query_metrics&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      enabled&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    # Query samples — how often to collect explain plans:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    query_samples&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      enabled&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      explain_statement_min_duration&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;500&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;   # ms — only collect plans for queries over 500ms&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      samples_per_second&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;                  # Reduce if CPU pressure on the Agent host&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    # Wait events (PostgreSQL 9.6+):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    query_activity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      enabled&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      collection_interval&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;    # seconds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    tags&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;env:production&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;service:your-app&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;db_engine:postgres&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For MySQL:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;instances&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;host&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;your-mysql-host&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    user&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;datadog&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    pass&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;ENC[your-secret]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    port&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3306&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    dbm&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    query_metrics&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      enabled&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    query_samples&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      enabled&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      explain_statement_min_duration&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;500&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    query_activity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      enabled&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;4. Are explain plans being collected?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In Datadog UI: &lt;strong&gt;APM → Database Monitoring → Query Samples&lt;/strong&gt;. Filter to your database host. If queries show “no explain plan,” verify:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;datadog.explain_statement&lt;/code&gt; function exists in the target database&lt;/li&gt;
&lt;li&gt;&lt;code&gt;explain_statement_min_duration&lt;/code&gt; is not set too high (default 5000ms misses most slow OLTP queries — set to 500ms)&lt;/li&gt;
&lt;li&gt;The query is not a DDL or &lt;code&gt;COPY&lt;/code&gt; statement (explain plans are not collected for these)&lt;/li&gt;
&lt;li&gt;The Agent’s &lt;code&gt;datadog&lt;/code&gt; user has &lt;code&gt;USAGE&lt;/code&gt; on the schema where the queried tables live&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;5. Are wait events visible?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In Datadog UI: &lt;strong&gt;Database Monitoring → Query Metrics&lt;/strong&gt; → click a query → &lt;strong&gt;Wait Events&lt;/strong&gt; tab. If the tab is empty:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Verify &lt;code&gt;query_activity.enabled: true&lt;/code&gt; in &lt;code&gt;conf.yaml&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Verify the &lt;code&gt;datadog&lt;/code&gt; user has &lt;code&gt;pg_monitor&lt;/code&gt; role&lt;/li&gt;
&lt;li&gt;Check Agent logs: &lt;code&gt;datadog-agent check postgres&lt;/code&gt; — look for errors on the &lt;code&gt;pg_stat_activity&lt;/code&gt; collection&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Set up Datadog DBM] --&gt; B[Create monitoring user with correct grants]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C{PostgreSQL or MySQL?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|PostgreSQL| D[Enable pg_stat_statements — add to shared_preload_libraries]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|MySQL| E[Grant SELECT on performance_schema and sys]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; F[Create datadog.explain_statement SECURITY DEFINER function]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; G[Set dbm:true in Agent conf.yaml]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[Set explain_statement_min_duration to 500ms]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[Enable query_activity for wait events]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J{Verify data appears}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|Query samples empty| K[Check pg_stat_statements.track — set to all — check track_activity_query_size]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|No explain plans| L[Verify explain_statement function — check USAGE grant on all schemas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|No wait events| M[Verify pg_monitor grant — check query_activity.enabled in conf.yaml]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|All data visible| N[Set alert thresholds on p99 query latency and connection saturation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;If DBM is causing database load:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Reduce &lt;code&gt;query_samples.samples_per_second&lt;/code&gt; to &lt;code&gt;0.1&lt;/code&gt; or disable query sampling entirely: &lt;code&gt;query_samples.enabled: false&lt;/code&gt;. Query metrics (without explain plans) have minimal database impact.&lt;/li&gt;
&lt;li&gt;Increase &lt;code&gt;explain_statement_min_duration&lt;/code&gt; to 2000ms to reduce explain plan frequency.&lt;/li&gt;
&lt;li&gt;If the monitoring connection itself is causing connection count pressure, reduce Agent check frequency: &lt;code&gt;min_collection_interval: 30&lt;/code&gt; (seconds).&lt;/li&gt;
&lt;li&gt;Disable &lt;code&gt;query_activity&lt;/code&gt; collection if the &lt;code&gt;pg_stat_activity&lt;/code&gt; query is slow on instances with many databases or connections.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;datadog.explain_statement&lt;/code&gt; function runs &lt;code&gt;EXPLAIN&lt;/code&gt; on sampled queries. On very high-throughput databases, this adds measurable load. Disable plan collection and rely on query metrics only if the database is already under pressure.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Provision monitoring user via Terraform&lt;/strong&gt;: manage the &lt;code&gt;datadog&lt;/code&gt; PostgreSQL user and grants through the same Terraform module that provisions the database. Store the password in AWS Secrets Manager or Vault, not in the Agent config file directly.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Agent configuration as code&lt;/strong&gt;: manage &lt;code&gt;conf.yaml&lt;/code&gt; through Ansible or a Helm chart value. The &lt;code&gt;explain_statement_min_duration&lt;/code&gt; threshold and &lt;code&gt;collection_interval&lt;/code&gt; settings should be tunable per environment without touching the Agent host directly.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Alert from DBM metrics&lt;/strong&gt;: create Datadog monitors on:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;postgresql.connections&lt;/code&gt; &gt; 80% of &lt;code&gt;max_connections&lt;/code&gt; — warning; 90% critical&lt;/li&gt;
&lt;li&gt;&lt;code&gt;postgresql.replication.delay&lt;/code&gt; &gt; 60s warning; 300s critical&lt;/li&gt;
&lt;li&gt;&lt;code&gt;postgresql.queries.avg_time&lt;/code&gt; P99 spike &gt; 2× baseline — warning&lt;/li&gt;
&lt;li&gt;&lt;code&gt;mysql.replication.seconds_behind_master&lt;/code&gt; &gt; 30s warning; null = critical (broken replication)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;p&gt;Datadog Database Monitoring closes the gap between APM traces and database behavior. When an application trace is slow, DBM lets the team click through to the specific SQL, its explain plan at the time of the slowdown, and the wait events that show what the database was waiting on. Without DBM configured correctly — with the right grants, &lt;code&gt;pg_stat_statements&lt;/code&gt; enabled, &lt;code&gt;track_activity_query_size&lt;/code&gt; large enough, and explain plan sampling at a useful threshold — the team gets query metrics but not query diagnostics. The setup work is one-time; the operational benefit is continuous.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Explain plans absent for short queries&lt;/td&gt;&lt;td&gt;&lt;code&gt;explain_statement_min_duration&lt;/code&gt; set to 5000ms (default)&lt;/td&gt;&lt;td&gt;Lower to 500ms for OLTP databases&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Truncated queries in DBM&lt;/td&gt;&lt;td&gt;&lt;code&gt;track_activity_query_size&lt;/code&gt; too small&lt;/td&gt;&lt;td&gt;Set to 4096 in &lt;code&gt;postgresql.conf&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aurora read replicas not in DBM&lt;/td&gt;&lt;td&gt;Each endpoint is a separate instance&lt;/td&gt;&lt;td&gt;Add a separate &lt;code&gt;instances:&lt;/code&gt; entry for the reader endpoint in &lt;code&gt;conf.yaml&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;SECURITY DEFINER&lt;/code&gt; function security concern&lt;/td&gt;&lt;td&gt;Function runs EXPLAIN as superuser equivalent&lt;/td&gt;&lt;td&gt;Limit the function to read-only plans only — the function only calls &lt;code&gt;EXPLAIN&lt;/code&gt;, not &lt;code&gt;EXECUTE&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DBM adds one extra connection per Agent&lt;/td&gt;&lt;td&gt;On databases near &lt;code&gt;max_connections&lt;/code&gt;, Agent connection pushes over the limit&lt;/td&gt;&lt;td&gt;Reserve connections for monitoring: set &lt;code&gt;max_connections&lt;/code&gt; 10 higher than application pool max&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;pg_stat_statements&lt;/code&gt; reset on restart&lt;/td&gt;&lt;td&gt;Cumulative counters reset; DBM shows spike&lt;/td&gt;&lt;td&gt;Set &lt;code&gt;pg_stat_statements.save = on&lt;/code&gt;; use rate metrics in Datadog, not raw counters&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Your database is visible in Datadog as infrastructure metrics but slow queries are not linked to their explain plans or wait events.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Enable DBM with the monitoring user grants above, set &lt;code&gt;explain_statement_min_duration&lt;/code&gt; to 500ms, and verify &lt;code&gt;pg_stat_statements&lt;/code&gt; is loaded.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; After setup, trigger a known slow query and verify it appears in Query Samples with an explain plan attached within 60 seconds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, create the &lt;code&gt;datadog&lt;/code&gt; monitoring user, add the &lt;code&gt;SECURITY DEFINER&lt;/code&gt; explain function, and set &lt;code&gt;dbm: true&lt;/code&gt; in the Agent config. Restart the Agent and verify query samples appear in the Datadog UI within 5 minutes.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>checklist</category></item><item><title>Managed Database Selection: Operational Burden, Feature Fit, Cost, and Exit Risk</title><link>https://rajivonai.com/blog/2024-10-12-managed-database-selection-operational-burden-feature-fit-cost-and-exit-risk/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-10-12-managed-database-selection-operational-burden-feature-fit-cost-and-exit-risk/</guid><description>Managed database selection across operational burden, feature fit, cost trajectory, and exit risk — with failure modes the easy adoption story hides.</description><pubDate>Sat, 12 Oct 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The wrong managed database choice usually does not fail on day one. It fails later, when the team discovers that the easiest service to adopt is now the hardest system to operate, tune, govern, or leave.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Cloud teams rarely choose between “self-managed database” and “managed database” anymore. They choose between managed PostgreSQL, managed MySQL, Aurora, Cloud SQL, AlloyDB, Spanner, DynamoDB, Cosmos DB, Bigtable, Firestore, MongoDB Atlas, hosted Kafka-adjacent stores, and specialized vector or search systems.&lt;/p&gt;
&lt;p&gt;That abundance changes the architecture problem. The question is no longer whether the provider can provision storage, backups, monitoring, encryption, failover, and patching. Most credible managed services can. The harder question is whether the service’s operational model matches the workload’s failure modes.&lt;/p&gt;
&lt;p&gt;A transactional product database has different risks than an append-heavy analytics store. A global ledger has different risks than a regional SaaS control plane. A recommendation feature that tolerates stale reads has different risks than an entitlement check in the request path.&lt;/p&gt;
&lt;p&gt;Managed databases reduce toil, but they also move control boundaries. The provider owns parts of the stack you used to tune directly. That can be good. It can also turn routine engineering work into quota negotiations, support tickets, migration projects, or application rewrites.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Teams often evaluate managed databases as feature checklists: engine compatibility, availability SLA, storage limit, replication option, pricing page, Terraform support. Those checks matter, but they miss the real failure pattern.&lt;/p&gt;
&lt;p&gt;The expensive failures are usually cross-dimensional.&lt;/p&gt;
&lt;p&gt;A service has the right query model but the wrong operational controls. A database has excellent autoscaling but weak transactional semantics. A platform has attractive entry pricing but painful data egress. A proprietary API accelerates development but raises exit risk. A relational engine fits today’s product but becomes a bottleneck when multi-region writes become a business requirement.&lt;/p&gt;
&lt;p&gt;The mistake is treating selection as a procurement step instead of an architectural decision with reversibility, observability, and operating model consequences.&lt;/p&gt;
&lt;p&gt;The core question is: how should a senior engineering team choose a managed database when the tradeoff is not only performance, but operational burden, feature fit, cost shape, and exit risk?&lt;/p&gt;
&lt;h2 id=&quot;the-selection-matrix-that-actually-matters&quot;&gt;The Selection Matrix That Actually Matters&lt;/h2&gt;
&lt;p&gt;A useful decision model starts with four dimensions: operational burden, feature fit, cost behavior, and exit risk. Each dimension should be evaluated against the workload’s expected failure modes, not against generic platform claims.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[workload facts — traffic shape and consistency needs] --&gt; B[feature fit — data model and query behavior]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; C[operational burden — backups failover tuning observability]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; D[cost behavior — steady state spikes and growth]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; E[exit risk — data gravity and API coupling]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; F[database shortlist — viable candidates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[prototype under failure — latency load restore migration]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[decision record — chosen service and rejected options]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Operational burden is not “managed versus unmanaged.” It is the work left for your team after the provider takes its share. Managed PostgreSQL still leaves schema design, index discipline, connection pooling, vacuum behavior, query regression detection, and restore validation with the application team. Dynamo-style systems reduce many relational operations, but they move burden into access-pattern design, partition key selection, capacity modeling, and query denormalization.&lt;/p&gt;
&lt;p&gt;Feature fit should be judged by native workload alignment. If the application needs relational integrity, secondary indexes, ad hoc operational queries, and transactional migrations, PostgreSQL-compatible systems usually create less application complexity. If the application needs predictable key-value access at very high scale, a wide-column or document-key service may be a better fit. If it needs externally consistent global transactions, the shortlist changes again.&lt;/p&gt;
&lt;p&gt;Cost behavior is the shape of the bill under normal growth and abnormal events. Storage cost is usually not the surprise. Read amplification, write amplification, cross-region replication, backup retention, provisioned capacity, IOPS, network egress, and analytics side paths are more likely to create the painful bill.&lt;/p&gt;
&lt;p&gt;Exit risk is the cost of changing your mind. SQL dialect differences matter. Proprietary APIs matter more. Operational dependencies matter most: streams, backup formats, IAM integration, failover semantics, generated identifiers, TTL behavior, change data capture, and application assumptions about consistency.&lt;/p&gt;
&lt;p&gt;The right answer is rarely “avoid lock-in.” Lock-in is a tool when it buys enough operational leverage. The mature question is whether the lock-in is intentional, documented, and bounded.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;h3 id=&quot;context&quot;&gt;Context&lt;/h3&gt;
&lt;p&gt;Amazon DynamoDB’s public design material describes a system optimized around partitioned key-value access, predictable latency, and horizontal scale. The documented pattern is clear: applications must design around access patterns up front, because joins and broad relational queries are not the service’s center of gravity. That is a feature when the workload is known and high volume. It is a constraint when the product still needs exploratory query flexibility.&lt;/p&gt;
&lt;p&gt;Google Spanner’s public papers describe a distributed relational system with externally consistent transactions across regions, built on TrueTime. The documented pattern is different: Spanner trades architectural complexity and cost for a stronger global consistency model than most conventional managed relational deployments provide.&lt;/p&gt;
&lt;p&gt;PostgreSQL’s documented behavior shows another pattern. It offers rich relational features, transactions, indexing, extensions, and SQL flexibility, but performance depends heavily on schema design, query plans, vacuum behavior, locks, and connection management. A managed PostgreSQL service reduces infrastructure work; it does not remove database engineering.&lt;/p&gt;
&lt;h3 id=&quot;action&quot;&gt;Action&lt;/h3&gt;
&lt;p&gt;For a managed database decision, translate those documented behaviors into workload tests.&lt;/p&gt;
&lt;p&gt;First, write down the read and write paths that must remain correct during failure. Include consistency requirements in application language: “a user must see a successful payment before shipping,” “an entitlement check must not read stale revocation data,” or “recommendations can lag by ten minutes.”&lt;/p&gt;
&lt;p&gt;Second, build a thin prototype against the two or three realistic candidates. Do not benchmark only happy-path latency. Test restore time, failover behavior, connection storms, index creation, schema migration, hot partitions, regional outage assumptions, backup export, and change data capture.&lt;/p&gt;
&lt;p&gt;Third, model the bill using event-driven scenarios: launch traffic, batch backfill, analytics export, regional replication, restore rehearsal, and a bad query that scans far more data than expected.&lt;/p&gt;
&lt;p&gt;Fourth, create an exit note before committing. Identify which application abstractions are portable, which are provider-specific, how data can be exported, and what downtime or dual-write period a migration would require.&lt;/p&gt;
&lt;h3 id=&quot;result&quot;&gt;Result&lt;/h3&gt;
&lt;p&gt;This process tends to eliminate false winners. A globally distributed database may be technically impressive but unnecessary for a regional product with simple recovery requirements. A low-cost key-value service may become expensive when access patterns require duplicated writes and multiple global secondary indexes. A managed relational database may look operationally familiar but fail the availability target if the team cannot tolerate primary-region write unavailability.&lt;/p&gt;
&lt;p&gt;The result is not a perfect database. It is a decision with fewer hidden obligations.&lt;/p&gt;
&lt;h3 id=&quot;learning&quot;&gt;Learning&lt;/h3&gt;
&lt;p&gt;The documented pattern across managed databases is that every service moves complexity somewhere. Managed relational systems move less complexity into application code but retain query and schema discipline. Key-value and document systems can move operational scaling complexity away from the team, but they often require stricter access-pattern design. Globally distributed transactional systems can simplify correctness across regions, but they charge for that guarantee in cost, latency, and operational constraints.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Decision Pressure&lt;/th&gt;&lt;th&gt;Common Mistake&lt;/th&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Better Test&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Operational burden&lt;/td&gt;&lt;td&gt;Assuming managed means no database expertise&lt;/td&gt;&lt;td&gt;Slow queries, lock contention, failed migrations, untested restores&lt;/td&gt;&lt;td&gt;Run migration, failover, restore, and connection storm drills&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Feature fit&lt;/td&gt;&lt;td&gt;Choosing the most scalable service&lt;/td&gt;&lt;td&gt;Application code absorbs missing query or transaction features&lt;/td&gt;&lt;td&gt;Map every critical read and write path to native database operations&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cost&lt;/td&gt;&lt;td&gt;Comparing only storage and baseline compute&lt;/td&gt;&lt;td&gt;Replication, indexes, reads, backfills, and exports dominate spend&lt;/td&gt;&lt;td&gt;Model normal growth plus three abnormal traffic events&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Exit risk&lt;/td&gt;&lt;td&gt;Treating SQL compatibility or API similarity as portability&lt;/td&gt;&lt;td&gt;Provider semantics leak into code, data flows, and operations&lt;/td&gt;&lt;td&gt;Write an exit note with export, dual-write, and cutover assumptions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Availability&lt;/td&gt;&lt;td&gt;Buying a higher SLA than the architecture can use&lt;/td&gt;&lt;td&gt;Application still fails during dependency or region failure&lt;/td&gt;&lt;td&gt;Test dependency failure from the application boundary&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Scale&lt;/td&gt;&lt;td&gt;Benchmarking synthetic throughput&lt;/td&gt;&lt;td&gt;Hot keys, bad indexes, or query shape collapse under real traffic&lt;/td&gt;&lt;td&gt;Replay production-like access patterns and skew&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Managed database selection fails when teams optimize for launch convenience instead of long-term operating behavior.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Evaluate each candidate across operational burden, feature fit, cost behavior, and exit risk using workload-specific failure tests.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Publicly documented systems such as DynamoDB, Spanner, and PostgreSQL show that each database model moves complexity to a different layer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before committing, run a prototype that tests failover, restore, migration, hot-path latency, abnormal cost scenarios, and data exit mechanics.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>AWS vs Azure vs GCP vs OCI for Database-Backed Systems: Decision Framework</title><link>https://rajivonai.com/blog/2024-09-27-aws-vs-azure-vs-gcp-vs-oci-for-database-backed-systems-decision-framework/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-09-27-aws-vs-azure-vs-gcp-vs-oci-for-database-backed-systems-decision-framework/</guid><description>How to choose between AWS, Azure, GCP, and OCI for database-backed systems by matching managed database failure behavior to your system&apos;s dominant recovery requirement.</description><pubDate>Fri, 27 Sep 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The wrong cloud choice rarely fails on launch day; it fails during the first database incident where the recovery path depends on a managed service behavior the team never tested.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most cloud comparisons start with compute, pricing calculators, or the list of managed database products. That is backwards for database-backed systems. Compute is replaceable. Queues are movable. Stateless services can be redeployed. The database is where consistency, failover, replication lag, licensing, operational control, and institutional knowledge converge.&lt;/p&gt;
&lt;p&gt;AWS, Azure, GCP, and OCI can all run serious production databases. The decision is not whether one provider is “better.” The decision is which failure mode you want the provider to absorb, and which failure mode you are willing to own.&lt;/p&gt;
&lt;p&gt;AWS gives the broadest managed database catalog and strong primitives around Aurora, RDS, DynamoDB, ElastiCache, Redshift, and global infrastructure. Azure is strongest when the data platform is already anchored in Microsoft identity, SQL Server, Power BI, Synapse, or enterprise governance. GCP has a distinctive advantage when the system needs globally distributed consistency through Spanner, or when operational simplicity around Cloud SQL and data analytics integration matters. OCI is the most natural home for Oracle Database, especially when Exadata, RAC, Data Guard, licensing, and Oracle operational semantics dominate the workload.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cloud database decisions usually collapse several different questions into one:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Where should the application run?&lt;/li&gt;
&lt;li&gt;Where should the database run?&lt;/li&gt;
&lt;li&gt;Who owns failover?&lt;/li&gt;
&lt;li&gt;What is the consistency model?&lt;/li&gt;
&lt;li&gt;How much operational control does the database team need?&lt;/li&gt;
&lt;li&gt;What happens when a zone, region, managed control plane, or identity dependency fails?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A team can pick AWS because the application platform is mature, then discover that the database estate is mostly Oracle and the real bottleneck is licensing plus Exadata behavior. Another team can choose Azure because the enterprise contract is convenient, then find that global writes need application-level conflict handling. A third team can choose GCP because Spanner is the right consistency primitive, then realize that most existing operational tooling assumes PostgreSQL failover behavior.&lt;/p&gt;
&lt;p&gt;The core question is not “Which cloud is best?” It is: &lt;strong&gt;which provider reduces the most dangerous database failure for this system without creating a worse operational dependency elsewhere?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;Use the database failure mode as the primary axis, then evaluate cloud fit.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;A[database backed system — production requirement] --&gt; B{dominant failure mode}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;B --&gt;|relational scale in one region| C[AWS Aurora — managed relational resilience]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;B --&gt;|SQL Server estate| D[Azure SQL — Microsoft operational alignment]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;B --&gt;|global consistency needed| E[GCP Spanner — distributed transaction model]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;B --&gt;|Oracle workload gravity| F[OCI Exadata — Oracle optimized control plane]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;C --&gt; G[test failover — connection pooling — backup restore]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;D --&gt; G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;E --&gt; H[test latency — schema design — transaction limits]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;F --&gt; I[test RAC — Data Guard — license posture]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;G --&gt; J[choose cloud by recovery behavior]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;H --&gt; J&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;I --&gt; J&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What this diagram shows:&lt;/strong&gt; Cloud provider selection driven by the dominant database failure mode. AWS Aurora for regional relational resilience. Azure SQL for SQL Server estates where operational alignment matters. GCP Spanner for systems requiring global consistency across regions. OCI Exadata for Oracle workload gravity. Each path ends at provider-specific validation tests — failover behavior, latency, schema constraints, or license posture — before committing.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;aws&quot;&gt;AWS&lt;/h3&gt;
&lt;p&gt;Choose AWS when the system benefits from service breadth, mature automation, and a large ecosystem of managed data services. Aurora is often the center of the decision for relational systems because its storage layer replicates across multiple Availability Zones and separates compute failover from storage durability. AWS documents Aurora storage across three Availability Zones and synchronous replication to six storage nodes for writes (&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Concepts.AuroraHighAvailability.html&quot;&gt;AWS Aurora high availability&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;The operational advantage is not magic availability. It is that common failure modes such as instance replacement, backup, read scaling, and same-region durability are productized. The tradeoff is that cross-region recovery still needs explicit design. Aurora Global Database, RDS replicas, DNS behavior, client retry logic, and write promotion procedures must be tested as a system.&lt;/p&gt;
&lt;p&gt;Default to AWS when your workload is heterogeneous, PostgreSQL or MySQL compatible, event-driven, and likely to use several managed services around the database.&lt;/p&gt;
&lt;h3 id=&quot;azure&quot;&gt;Azure&lt;/h3&gt;
&lt;p&gt;Choose Azure when the database-backed system is already tied to Microsoft operational gravity: SQL Server, Active Directory or Entra ID, .NET estates, Power BI, Microsoft security controls, and enterprise procurement. Azure SQL Database handles patching, backups, upgrades, and failover mechanics as part of the managed service. Zone redundancy spans compute and storage components across availability zones in supported tiers, with Microsoft documenting zero committed-data loss for a single-zone failure in those configurations (&lt;a href=&quot;https://learn.microsoft.com/en-us/azure/azure-sql/database/high-availability-sla?view=azuresql-db&quot;&gt;Azure SQL availability&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;The advantage is organizational coherence. Identity, governance, data access, analytics, and operational runbooks often become simpler when the platform and database are Microsoft-native. The risk is assuming that Azure SQL, SQL Managed Instance, SQL Server on VMs, Cosmos DB, and PostgreSQL flexible server all share the same recovery model. They do not.&lt;/p&gt;
&lt;p&gt;Default to Azure when the highest-value reduction is integration risk across identity, SQL Server compatibility, compliance operations, and enterprise data workflows.&lt;/p&gt;
&lt;h3 id=&quot;gcp&quot;&gt;GCP&lt;/h3&gt;
&lt;p&gt;Choose GCP when the system’s hardest database problem is distributed consistency, analytics adjacency, or operational simplicity for managed PostgreSQL and MySQL. Cloud SQL high availability uses regional availability across zones and can bring an HA instance up in a secondary zone with the same IP and no data loss for zonal failures (&lt;a href=&quot;https://cloud.google.com/sql/docs/availability&quot;&gt;Cloud SQL availability&lt;/a&gt;). For region failure, Cloud SQL requires cross-region replicas or advanced disaster recovery design, and Google documents that asynchronous cross-region replication can create non-zero RPO (&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/intro-to-cloud-sql-disaster-recovery&quot;&gt;Cloud SQL disaster recovery&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;GCP is most differentiated by Spanner. Spanner is not simply “managed SQL at scale.” It is a distributed relational database with externally consistent transactions built around Google’s TrueTime model (&lt;a href=&quot;https://cloud.google.com/spanner/docs/true-time-external-consistency&quot;&gt;Spanner external consistency&lt;/a&gt;). That is valuable when the system needs global reads and writes without pushing conflict resolution into application code.&lt;/p&gt;
&lt;p&gt;Default to GCP when global consistency, BigQuery adjacency, data platform integration, or Spanner’s transaction model is worth designing around from the beginning.&lt;/p&gt;
&lt;h3 id=&quot;oci&quot;&gt;OCI&lt;/h3&gt;
&lt;p&gt;Choose OCI when Oracle Database is the system of record and the business depends on Oracle-specific performance, availability, or operational semantics. OCI’s advantage is not a generic cloud catalog comparison. It is the ability to run Oracle Database on infrastructure designed for Oracle Database, including Exadata, RAC, Autonomous Database, and Data Guard patterns. Oracle documents Exadata Database Service and Autonomous Database options across OCI and multicloud deployments, including Oracle Database@Azure for colocated Azure application estates (&lt;a href=&quot;https://docs.oracle.com/en-us/iaas/Content/database-at-azure/overview.htm&quot;&gt;Oracle Database@Azure overview&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;The operational win is minimizing translation. If the workload depends on PL/SQL, RAC behavior, Exadata storage offload, Oracle partitioning, Data Guard procedures, or existing Oracle operational expertise, moving it to a non-Oracle managed approximation can create more risk than it removes.&lt;/p&gt;
&lt;p&gt;Default to OCI when Oracle is not just a database engine, but the operational platform.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Aurora cross-region DNS caching during failover.&lt;/strong&gt; AWS documents Aurora failover as completing in under 30 seconds for same-region instance replacement (&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Concepts.AuroraHighAvailability.html&quot;&gt;Aurora HA docs&lt;/a&gt;). What the documentation does not prominently state is that applications using the cluster endpoint DNS name will continue routing to the old primary until their local DNS TTL expires, typically 5 seconds for Aurora but often cached longer by JVM connection pools, OS resolvers, or connection pool libraries. The operational consequence: application-level retry logic and connection pool eviction must be implemented separately from Aurora failover — the managed service covers the database, not the client. Teams that test “does Aurora failover work?” but do not test “does our application reconnect within 30 seconds?” have not tested their actual RTO.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Spanner TrueTime latency and transaction design.&lt;/strong&gt; Google Spanner’s documented external consistency guarantee relies on TrueTime, which introduces a commit-wait phase where Spanner holds a committed transaction until the global clock uncertainty window resolves (&lt;a href=&quot;https://cloud.google.com/spanner/docs/true-time-external-consistency&quot;&gt;Spanner external consistency&lt;/a&gt;). Google’s documentation states this adds single-digit milliseconds of commit latency in normal operation. The documented schema design constraint is hotspots: monotonically increasing primary keys (auto-increment IDs, timestamps) concentrate writes on a single Spanner split, eliminating the distributed write throughput that justifies Spanner’s cost. Applications migrated to Spanner from PostgreSQL without rethinking key design often re-create the single-writer bottleneck they were trying to eliminate.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cloud SQL and Azure SQL: documented RTO expectations for zonal failover.&lt;/strong&gt; Cloud SQL HA instances use a standby in a secondary zone with synchronous replication. Google documents typical failover to the secondary zone in 60 seconds or less, with the same IP address automatically routing to the new primary (&lt;a href=&quot;https://cloud.google.com/sql/docs/availability&quot;&gt;Cloud SQL availability&lt;/a&gt;). Azure SQL Business Critical tier documents 20–30 second failover to a read replica promoted to primary within the same availability zone group. Both services document non-zero RPO for cross-region scenarios — Cloud SQL cross-region replicas are asynchronous, and Azure SQL’s active geo-replication is documented to have seconds of lag under normal conditions, meaning a region failure can result in seconds to minutes of data loss depending on replication lag at the moment of failure (&lt;a href=&quot;https://learn.microsoft.com/en-us/azure/azure-sql/database/active-geo-replication-overview&quot;&gt;Azure SQL geo-replication&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Provider selection test sequence.&lt;/strong&gt; Run these four tests before any pricing analysis: (1) kill the primary database node and measure application recovery time end-to-end, not just service status; (2) simulate a zone outage and verify client behavior; (3) simulate regional loss and document RPO, RTO, promotion steps, and rollback procedure; (4) restore from backup into an isolated environment and run data correctness checks. The provider that produces acceptable results across all four tests for the dominant failure mode in your system is the correct choice.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Provider&lt;/th&gt;&lt;th&gt;Strong fit&lt;/th&gt;&lt;th&gt;Failure to watch&lt;/th&gt;&lt;th&gt;Concrete failure&lt;/th&gt;&lt;th&gt;Design response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;AWS&lt;/td&gt;&lt;td&gt;Mixed workloads, Aurora, managed service breadth&lt;/td&gt;&lt;td&gt;DNS caching extends actual client RTO past documented 30s Aurora failover&lt;/td&gt;&lt;td&gt;Application reconnect takes 60–120s due to JVM/pool DNS caching despite database failover completing in under 30s&lt;/td&gt;&lt;td&gt;Set &lt;code&gt;KeepAlive&lt;/code&gt; on connections, configure pool &lt;code&gt;testOnBorrow&lt;/code&gt;, use exponential backoff retry — test actual application reconnect time, not Aurora status page&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Azure&lt;/td&gt;&lt;td&gt;SQL Server, Microsoft identity, enterprise governance&lt;/td&gt;&lt;td&gt;Different HA behavior across SQL Database, SQL Managed Instance, and SQL Server on VMs&lt;/td&gt;&lt;td&gt;App built on SQL MI assumptions fails when migrated to SQL Database (different HA model, different failover window)&lt;/td&gt;&lt;td&gt;Validate HA tier and failover SLA per specific service and tier before committing architecture&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GCP&lt;/td&gt;&lt;td&gt;Spanner, analytics adjacency, managed PostgreSQL or MySQL&lt;/td&gt;&lt;td&gt;Monotonically increasing keys create Spanner hotspots&lt;/td&gt;&lt;td&gt;Write throughput degrades to single-node capacity for UUID v4 replaced by timestamp PKs&lt;/td&gt;&lt;td&gt;Use bit-reversal or hash-prefixed keys for Spanner; model expected TPS per split before launch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OCI&lt;/td&gt;&lt;td&gt;Oracle Database, Exadata, RAC, Data Guard&lt;/td&gt;&lt;td&gt;Using OCI as generic compute while running Oracle on-premises assumptions&lt;/td&gt;&lt;td&gt;Oracle RAC on OCI cloud VMs performs differently than on-premises Exadata — I/O semantics and latency profiles differ&lt;/td&gt;&lt;td&gt;Use Oracle Database@Azure or Exadata Cloud Service if Exadata storage offload is required for workload&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The database cloud decision is usually framed as a platform preference, which hides the actual recovery risks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Select AWS, Azure, GCP, or OCI by matching the provider’s managed database behavior to the system’s dominant failure mode.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Use provider-documented HA and DR mechanics, then verify with failover, replica promotion, backup restore, and application retry tests.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before committing, write the incident runbook first. If the runbook is vague, the cloud choice is not ready.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>cloud</category><category>databases</category></item><item><title>Cassandra Observability: Compaction, Tombstones, Repair, Latency, and Hot Partitions</title><link>https://rajivonai.com/blog/2024-09-19-cassandra-observability-compaction-tombstones/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-09-19-cassandra-observability-compaction-tombstones/</guid><description>Why generic server monitoring fails for Apache Cassandra, and how to track the true operational signals of a distributed masterless database.</description><pubDate>Thu, 19 Sep 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;If you try to monitor a distributed, masterless database like Cassandra using the same dashboard you use for a monolithic relational database, you will misdiagnose every single incident.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Apache Cassandra operates on fundamentally different assumptions than relational systems like PostgreSQL or MySQL. It is an AP system in the CAP theorem context: highly available, partition tolerant, and eventually consistent. Data is distributed across a ring of nodes, writes are appended to memory and disk sequentially, and deletes are executed by inserting a marker called a “tombstone.”&lt;/p&gt;
&lt;p&gt;When teams adopt Cassandra, they often plug it into their existing monitoring stack. They set alerts on CPU utilization, disk space, and memory consumption. But in Cassandra, a node running at 80% CPU might be perfectly healthy and churning through background compaction, while a node at 20% CPU might be silently dropping mutations because it is overwhelmed by tombstones during read repair. Generic infrastructure metrics are insufficient; you must observe Cassandra’s internal state machine.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;
&lt;p&gt;A Cassandra cluster experiencing distress exhibits unique failure modes that rarely trigger standard host-level alarms until it is too late:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The Tombstone Overwhelm:&lt;/strong&gt; Read latency spikes for a specific table. CPU is low, but the application is timing out. The node is scanning and discarding thousands of deleted records (tombstones) to return a single live row.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Compaction Debt:&lt;/strong&gt; Disk usage begins climbing relentlessly. The node is writing data faster than the background compaction threads can merge the SSTables, leading to read latency degradation as queries must scan dozens of fragmented files.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Partition Hotspot:&lt;/strong&gt; One node in a 10-node cluster is pegged at 100% CPU while the other nine sit at 15%. A single customer or entity is receiving a disproportionate share of traffic, overwhelming the node responsible for that token range.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Repair Drift:&lt;/strong&gt; Nodes return inconsistent data depending on the consistency level (&lt;code&gt;LOCAL_QUORUM&lt;/code&gt; vs &lt;code&gt;ONE&lt;/code&gt;). Anti-entropy repair processes have fallen behind or failed, leading to stale reads.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;p&gt;When a Cassandra pager alert fires—especially for p99 latency spikes—these are the five internal metrics you must check:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Check Pending Tasks (&lt;code&gt;nodetool tpstats&lt;/code&gt;):&lt;/strong&gt;
This shows the thread pool statistics. The critical metrics are &lt;code&gt;Pending&lt;/code&gt; and &lt;code&gt;Dropped&lt;/code&gt; messages. If &lt;code&gt;MutationStage&lt;/code&gt; or &lt;code&gt;ReadStage&lt;/code&gt; have high pending counts, the node is saturated. If there are dropped mutations, data is not being written.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Evaluate Compaction Backlog (&lt;code&gt;nodetool compactionstats&lt;/code&gt;):&lt;/strong&gt;
Look at &lt;code&gt;pending tasks&lt;/code&gt;. A small number is normal. A number in the hundreds or thousands indicates compaction has fallen permanently behind the write rate.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Analyze Tombstone Ratios (Log inspection or JMX metrics):&lt;/strong&gt;
Check the &lt;code&gt;system.log&lt;/code&gt; for warnings about &lt;code&gt;Scanned over X tombstones&lt;/code&gt;. If this number exceeds the &lt;code&gt;tombstone_warn_threshold&lt;/code&gt;, read queries are doing massive amounts of wasted work.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Verify Client Request Latency via JMX/Metrics:&lt;/strong&gt;
Look at &lt;code&gt;ClientRequest.Latency.Read&lt;/code&gt; and &lt;code&gt;ClientRequest.Latency.Write&lt;/code&gt; at the 99th percentile (p99). Cassandra is highly optimized for writes; if write latency spikes, disk I/O is usually the bottleneck.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Examine Partition Sizes (&lt;code&gt;nodetool tablestats&lt;/code&gt;):&lt;/strong&gt;
Look for the &lt;code&gt;Compacted partition maximum bytes&lt;/code&gt;. If a single partition exceeds 100MB, you have a data modeling problem causing a hotspot, not an infrastructure problem.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;p&gt;When diagnosing a Cassandra latency spike, use the following operational flow:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[p99 Latency Spike Detected] --&gt; B{Is it Read or Write Latency?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Write| C[Check Pending Tasks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; C1{Are Mutations Dropping?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C1 --&gt;|Yes| C2[Node is Overwhelmed: Add Capacity or Shed Load]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C1 --&gt;|No| C3[Check Disk I/O Wait]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C3 --&gt;|High| C4[Storage Bottleneck: Upgrade Disks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Read| D[Check Pending Tasks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; D1{Are ReadStages Pending?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D1 --&gt;|No| D2[Check Tombstone Warnings in Logs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D2 --&gt;|High| D3[Tombstone Overwhelm: Change Data Model or Lower GC Grace]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D2 --&gt;|Low| D4[Check Compaction Backlog]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D4 --&gt;|High| D5[Fragmented Reads: Tune Compaction Throughput]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Tune Compaction Throughput (Medium Speed, Low Risk):&lt;/strong&gt;
If compaction is falling behind, you can dynamically increase &lt;code&gt;compaction_throughput_mb_per_sec&lt;/code&gt; using &lt;code&gt;nodetool setcompactionthroughput&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Tradeoff:&lt;/em&gt; Compaction is highly I/O intensive. Increasing throughput might clear the backlog but can temporarily degrade read and write latencies.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Add Nodes to the Ring (Slow, Permanent Fix):&lt;/strong&gt;
If the entire cluster is legitimately saturated (high CPU, high pending tasks, dropping mutations across the ring), you must bootstrap new nodes.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Tradeoff:&lt;/em&gt; Bootstrapping involves streaming data across the network, which adds load to the existing struggling nodes. Do not wait until the cluster is at 95% capacity to scale.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lower &lt;code&gt;gc_grace_seconds&lt;/code&gt; (Fast, High Risk):&lt;/strong&gt;
If tombstones are crushing read performance on a specific table, and you do not require a long window for resurrecting dead data via repair, you can lower &lt;code&gt;gc_grace_seconds&lt;/code&gt; via &lt;code&gt;ALTER TABLE&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Tradeoff:&lt;/em&gt; If a node goes down for longer than the new &lt;code&gt;gc_grace_seconds&lt;/code&gt; and misses a delete, that deleted data will “resurrect” when the node comes back online.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;If you tune compaction throughput too aggressively and disk I/O saturates causing widespread query timeouts, revert &lt;code&gt;compaction_throughput_mb_per_sec&lt;/code&gt; to its previous conservative value (e.g., 16 MB/s) using &lt;code&gt;nodetool setcompactionthroughput 16&lt;/code&gt;. Note: setting the value to &lt;code&gt;0&lt;/code&gt; removes the limit entirely — it does not pause compaction. If background compaction is actively destroying cluster stability, use &lt;code&gt;nodetool stop COMPACTION&lt;/code&gt; to halt the specific running tasks until I/O pressure subsides.&lt;/p&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;Deploy an automated script that polls JMX metrics for &lt;code&gt;Dropped Mutations&lt;/code&gt; across all nodes. If a node begins dropping mutations for more than 5 minutes, automatically route application traffic away from that specific node’s local datacenter (if running multi-DC) or trigger a high-severity incident, because dropped mutations mean permanent data loss if not recovered via hinted handoff or repair.&lt;/p&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Acknowledge the Cassandra Tax:&lt;/strong&gt; Cassandra requires ongoing background maintenance (compaction and repair). You must provision your clusters so that they run at no more than 50-60% capacity during normal operations to leave headroom for this maintenance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data Modeling is Operations:&lt;/strong&gt; 90% of Cassandra performance issues are caused by bad data models (large partitions or heavy deletes), not bad hardware.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Monitor the 99th Percentile:&lt;/strong&gt; Cassandra is known for stable average latencies but terrifying tail latencies during JVM garbage collection or heavy compaction. Always alert on p99, never on the average.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Cassandra’s most destructive failure modes — tombstone read amplification, compaction debt, hot partitions — don’t register on CPU or memory dashboards until the cluster is already in distress, because a node scanning 50,000 tombstones to return one row can run at 20% CPU while its read latency is at 10 seconds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Ingest &lt;code&gt;nodetool tpstats&lt;/code&gt; (pending and dropped task counts), &lt;code&gt;nodetool compactionstats&lt;/code&gt; (pending compaction tasks), and tombstone scan warnings from &lt;code&gt;system.log&lt;/code&gt; as time-series metrics alongside host metrics — these are the only signals that surface Cassandra-specific distress before it becomes visible to users.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Artificially generate thousands of deletes on a test table in staging and verify that read latency alerts fire before the problem appears on CPU charts — if CPU is the first signal, the monitoring doesn’t give enough lead time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Configure JMX metrics ingestion (Datadog JMX integration or Prometheus JMX exporter) this week and add a panel tracking &lt;code&gt;ClientRequest.Latency.Read&lt;/code&gt; p99 and &lt;code&gt;Pending CompactionExecutor&lt;/code&gt; tasks — these two metrics together explain most Cassandra incidents.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>Backing Up and Recovering a Sharded PostgreSQL Database: The Citus Problem</title><link>https://rajivonai.com/blog/2024-09-18-backing-up-recovering-citus-postgresql/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-09-18-backing-up-recovering-citus-postgresql/</guid><description>A recovery-first design for Citus that coordinates snapshots, WAL, metadata, restore points, node recovery, regional recovery, and single-tenant repair.</description><pubDate>Wed, 18 Sep 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A green backup job on every Citus node does not prove that the distributed database can be recovered. It may only prove that several PostgreSQL servers were backed up at different moments.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;RetailCo’s checkout platform has outgrown one PostgreSQL writer. Orders, order items, payments, inventory reservations, and shipments are distributed by &lt;code&gt;tenant_id&lt;/code&gt; across four Citus workers. Each worker primary has a streaming standby. The coordinator also has a standby.&lt;/p&gt;
&lt;p&gt;The data now exceeds what RetailCo can restore casually during an incident. The backup dashboard still looks reassuring: every node has a recent base backup, every node archives WAL, and storage snapshots complete successfully.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App[RetailCo checkout platform] --&gt; Coord[Citus coordinator primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coord --&gt; W1[worker one primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coord --&gt; W2[worker two primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coord --&gt; W3[worker three primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coord --&gt; W4[worker four primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coord --&gt; CS[coordinator standby]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  W1 --&gt; S1[worker one standby]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  W2 --&gt; S2[worker two standby]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  W3 --&gt; S3[worker three standby]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  W4 --&gt; S4[worker four standby]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coord --&gt; B[backup repository]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  W1 --&gt; B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  W2 --&gt; B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  W3 --&gt; B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  W4 --&gt; B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That topology is highly available only if failover works. It is recoverable only if RetailCo can reconstruct one valid distributed state after corruption, operator error, or regional loss.&lt;/p&gt;
&lt;p&gt;Citus documents the separation clearly. Workers hold distributed shards, while the coordinator tracks nodes and shard locations in metadata tables. Citus uses PostgreSQL streaming replication for worker high availability, and the coordinator metadata can also be protected by streaming replication or PostgreSQL backup tools: &lt;a href=&quot;https://docs.citusdata.com/en/stable/admin_guide/cluster_management.html#dealing-with-node-failures&quot;&gt;Citus cluster management&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;RetailCo is a reference architecture, not a claimed customer deployment. The topology and data volume are scenario inputs. This article does not claim a measured recovery point objective, recovery time objective, backup duration, or restore duration.&lt;/p&gt;
&lt;p&gt;Version matters here, and this is one of the few database topics where the version gap changes the recommended design rather than only the syntax:&lt;/p&gt;





















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Citus line&lt;/th&gt;&lt;th&gt;Backup-relevant capability&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;13.0 / 13.2&lt;/td&gt;&lt;td&gt;&lt;code&gt;citus_create_restore_point()&lt;/code&gt; for a cluster-wide PITR marker; no cluster-change barrier&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;14.0 (February 2026)&lt;/td&gt;&lt;td&gt;PostgreSQL 18 support; &lt;code&gt;citus_create_restore_point()&lt;/code&gt; strengthened for multi-writer (MX) metadata mode by taking &lt;code&gt;ExclusiveLock&lt;/code&gt; on &lt;code&gt;pg_dist_transaction&lt;/code&gt; across all metadata nodes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;14.1 / 13.3 (May 2026)&lt;/td&gt;&lt;td&gt;Explicit cluster-wide change coordination for backups via &lt;code&gt;citus_cluster_changes_block()&lt;/code&gt; and related functions&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;This article targets the 13.x and 14.0 lines. The public stable API manual labels itself 13.0.1, so the runbook must be matched to the installed Citus minor, PostgreSQL major, and backup-tool version: &lt;a href=&quot;https://www.citusdata.com/updates/v14-0/&quot;&gt;Citus 14.0 release notes&lt;/a&gt;, &lt;a href=&quot;https://www.citusdata.com/updates/v14-1/&quot;&gt;Citus 14.1 and 13.3 release notes&lt;/a&gt;, and &lt;a href=&quot;https://www.citusdata.com/updates/v13-2/&quot;&gt;Citus 13.2 release notes&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A normal PostgreSQL physical backup protects one PostgreSQL cluster. A Citus database is a set of PostgreSQL clusters that participate in distributed transactions.&lt;/p&gt;
&lt;p&gt;Suppose one checkout transaction updates an order placement on one worker and an inventory placement on another. Citus can use two-phase commit so both changes become one distributed decision. Now suppose the infrastructure snapshots the coordinator at 10:37:20, worker one at 10:37:22, and worker two at 10:37:27.&lt;/p&gt;
&lt;p&gt;Each snapshot can be locally valid. The set can still be wrong. Citus describes the exact failure: a two-phase transaction can be committed in one node’s snapshot while still prepared but not committed in another. Restoring those independent moments can reconstruct an inconsistent distributed transaction: &lt;a href=&quot;https://www.citusdata.com/updates/v14-1/#cluster-wide-change-coordination-for-backups&quot;&gt;Citus cluster-wide backup coordination&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Tx[one distributed checkout transaction] --&gt; C1[commit decision across nodes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C1 --&gt; A[coordinator snapshot — earlier]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C1 --&gt; B[worker one snapshot — middle]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C1 --&gt; C[worker two snapshot — later]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A --&gt; R[locally valid files]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; R&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; R&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  R --&gt; Risk[restored cluster may disagree about one transaction]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The coordinator introduces a second consistency problem. Its metadata says which shards exist and where they live. If the restored coordinator remembers a shard move that the restored workers do not—or forgets a move that the workers contain—the data files and ownership map no longer describe the same cluster.&lt;/p&gt;
&lt;p&gt;The recovery question is therefore not:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Did every node finish a backup?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It is:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Can RetailCo restore the coordinator, every required worker, WAL history, global objects, configuration, and Citus metadata to one provably compatible distributed state?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;back-up-a-recovery-unit-not-a-fleet&quot;&gt;Back Up a Recovery Unit, Not a Fleet&lt;/h2&gt;
&lt;p&gt;RetailCo should treat the entire Citus deployment as one recovery unit. Each backup generation needs one cluster manifest that ties together:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the coordinator backup identifier;&lt;/li&gt;
&lt;li&gt;every worker backup identifier;&lt;/li&gt;
&lt;li&gt;PostgreSQL and Citus versions on every node;&lt;/li&gt;
&lt;li&gt;node names, ports, system identifiers, timelines, and tablespaces;&lt;/li&gt;
&lt;li&gt;the Citus node and shard-placement inventory;&lt;/li&gt;
&lt;li&gt;the WAL archive namespace and required WAL ranges for every node;&lt;/li&gt;
&lt;li&gt;roles, tablespaces, grants, extension packages, and configuration;&lt;/li&gt;
&lt;li&gt;the coordination method used for the backup;&lt;/li&gt;
&lt;li&gt;the named restore point, if the generation supports PITR;&lt;/li&gt;
&lt;li&gt;checksums, completion status, and a retention relationship between base backups and WAL.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  M[one Citus recovery manifest] --&gt; C[coordinator physical backup]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  M --&gt; W[all worker physical backups]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  M --&gt; WAL[per-node WAL archives and timelines]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  M --&gt; G[roles tablespaces grants and configuration]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  M --&gt; P[Citus topology and placement evidence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  M --&gt; T[restore target and validation plan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The manifest is not documentation added after the backup. It is part of the backup. A collection of objects in storage without a machine-readable relationship is an investigation, not a restore plan.&lt;/p&gt;
&lt;h3 id=&quot;use-three-layers-not-one-backup-mechanism&quot;&gt;Use three layers, not one backup mechanism&lt;/h3&gt;
&lt;p&gt;The recommended design combines physical recovery, logical recovery, and configuration recovery because they solve different incidents.&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Layer&lt;/th&gt;&lt;th&gt;Best use&lt;/th&gt;&lt;th&gt;What it does not solve alone&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Physical base backup or storage snapshot plus WAL&lt;/td&gt;&lt;td&gt;Full node recovery, cluster PITR, regional reconstruction&lt;/td&gt;&lt;td&gt;Selective tenant repair; cluster-wide coordination&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Logical export&lt;/td&gt;&lt;td&gt;Schema portability, object inspection, selected-data recovery, tenant extraction&lt;/td&gt;&lt;td&gt;WAL replay; practical full recovery of a very large cluster&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Infrastructure and configuration backup&lt;/td&gt;&lt;td&gt;Packages, parameters, access rules, certificates, service discovery, backup policy&lt;/td&gt;&lt;td&gt;Database pages, transactions, Citus shard data&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;PostgreSQL states that &lt;code&gt;pg_dump&lt;/code&gt; creates a consistent logical export of one database, but it does not capture cluster-wide global objects such as roles and tablespaces. Those require &lt;code&gt;pg_dumpall&lt;/code&gt;, commonly with &lt;code&gt;--globals-only&lt;/code&gt;. PostgreSQL also warns that logical dumps do not contain the information required for WAL replay: &lt;a href=&quot;https://www.postgresql.org/docs/current/app-pgdump.html&quot;&gt;PostgreSQL &lt;code&gt;pg_dump&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://www.postgresql.org/docs/current/app-pg-dumpall.html&quot;&gt;&lt;code&gt;pg_dumpall&lt;/code&gt;&lt;/a&gt;, and &lt;a href=&quot;https://www.postgresql.org/docs/current/continuous-archiving.html&quot;&gt;continuous archiving&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For Citus, run logical-export and restore tests through the canonical coordinator for the installed version. Inspect the archive contents and prove that the restored target contains logical distributed tables, correct distribution metadata, reference data, sequences, functions, privileges, and row counts. Do not assume that a generic PostgreSQL command has captured the intended distributed abstraction merely because it exited successfully.&lt;/p&gt;
&lt;p&gt;At RetailCo’s scale, a logical export is a complementary control and a selective-recovery tool. It should not be the only disaster-recovery path for the full distributed database.&lt;/p&gt;
&lt;h3 id=&quot;establish-a-validated-maintenance-boundary-for-storage-snapshots&quot;&gt;Establish a validated maintenance boundary for storage snapshots&lt;/h3&gt;
&lt;p&gt;Citus added purpose-built barrier functions for exactly this problem in 14.1 and 13.3 (May 2026), after this article’s March 31, 2026 cutoff. Where they are available, they are the correct control and should replace the hand-built boundary below:&lt;/p&gt;





















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Function&lt;/th&gt;&lt;th&gt;Behavior&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;citus_cluster_changes_block()&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Halts distributed 2PC commits and topology and schema changes cluster-wide while snapshots are taken&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;citus_cluster_changes_unblock()&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Resumes normal operation once every snapshot is recorded&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;citus_cluster_changes_block_status()&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Reports the current block state, its initiator, and its timestamp&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The important property is that the block is narrow. Reads and single-shard writes continue during the barrier; only multi-node writes pause. That is a materially smaller business impact than pausing every writer, which is what the conservative boundary below has to do.&lt;/p&gt;
&lt;p&gt;Two operational cautions apply. A block that is never unblocked is an outage, so the snapshot controller needs guaranteed cleanup on every exit path, an independent watchdog reading &lt;code&gt;citus_cluster_changes_block_status()&lt;/code&gt;, and an alert on block age. And the barrier coordinates the &lt;em&gt;cluster&lt;/em&gt;; it does not make independent cloud volume snapshots atomic with each other. It creates a window in which no new distributed commit decision is made, which is what makes the set of snapshots mutually restorable.&lt;/p&gt;
&lt;p&gt;On Citus lines without those functions — including the 13.0 and 13.2 clusters this article targets — a snapshot controller cannot claim cluster consistency merely because it requested every volume snapshot within a few seconds. Those deployments need the conservative maintenance boundary:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Reject the run if any required node, standby, archive stream, or snapshot API is unhealthy.&lt;/li&gt;
&lt;li&gt;Pause new application writes, scheduled mutations, schema changes, shard movement, and topology automation.&lt;/li&gt;
&lt;li&gt;Drain or terminate in-flight distributed transactions using an operator-defined timeout.&lt;/li&gt;
&lt;li&gt;Create and record one Citus named restore point across the cluster.&lt;/li&gt;
&lt;li&gt;Request snapshots of the coordinator and every worker, recording immutable identifiers in one manifest.&lt;/li&gt;
&lt;li&gt;Verify that every request was accepted and that the provider’s snapshot semantics satisfy the tested capture procedure.&lt;/li&gt;
&lt;li&gt;Resume writers only after the manifest is complete or the failed generation is abandoned safely.&lt;/li&gt;
&lt;li&gt;Mark the generation usable only after an isolated restore proves topology and transaction consistency.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This is a proposed operational boundary, not a claim that independent cloud snapshots become atomic. If the business cannot tolerate the write pause or the restore drill cannot prove consistency, use coordinated backup tooling with per-node WAL and a tested restore-point workflow instead of weakening the boundary on paper.&lt;/p&gt;
&lt;h3 id=&quot;use-a-citus-restore-point-for-the-pitr-cut&quot;&gt;Use a Citus restore point for the PITR cut&lt;/h3&gt;
&lt;p&gt;Storage snapshots provide a base. WAL provides recovery after that base. Each node must continuously archive its own WAL into a collision-safe namespace, and the recovery manifest must preserve each node’s PostgreSQL system identifier and timeline history.&lt;/p&gt;
&lt;p&gt;PostgreSQL physical recovery combines a base backup with an unbroken WAL sequence. It can stop at a timestamp, transaction, or named restore point. It restores an entire PostgreSQL cluster, not a selected table or tenant, and it does not restore manually edited files such as &lt;code&gt;postgresql.conf&lt;/code&gt;, &lt;code&gt;pg_hba.conf&lt;/code&gt;, and &lt;code&gt;pg_ident.conf&lt;/code&gt;: &lt;a href=&quot;https://www.postgresql.org/docs/current/continuous-archiving.html&quot;&gt;PostgreSQL PITR&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Citus adds the cluster-wide marker:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_create_restore_point(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;retailco_2026_03_31_103722&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;citus_create_restore_point()&lt;/code&gt; temporarily blocks cluster writes and creates a consistent named restore point on all nodes. The function returns the coordinator LSN, but the marker is created across the cluster. Citus documents it as suitable for PITR and cluster forking: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/api_udf.html#citus-create-restore-point&quot;&gt;Citus utility functions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;That distinction matters. A coordinator LSN cannot be reused as though it were the worker LSN. Each PostgreSQL node owns its own WAL stream. Recovery should target the same &lt;em&gt;restore-point name&lt;/em&gt; on every restored node, using the correct WAL archive for that node.&lt;/p&gt;
&lt;p&gt;Citus 14.0 strengthened this function for clusters that can initiate distributed work from metadata workers. It blocks distributed commit decisions across metadata nodes and protects topology and distribution metadata while the restore points are created. If RetailCo uses multi-writer metadata mode, the older coordinator-only behavior must not be assumed sufficient.&lt;/p&gt;
&lt;p&gt;A named marker is the cleanest documented cut for a cluster-wide drill. For an arbitrary request such as “restore to 10:37:22,” RetailCo should first recover the cluster in an isolated environment and prove that every node reaches a compatible state. A matching wall-clock timestamp on independent WAL streams is not by itself a distributed transaction protocol.&lt;/p&gt;
&lt;h3 id=&quot;separate-high-availability-from-recovery&quot;&gt;Separate high availability from recovery&lt;/h3&gt;
&lt;p&gt;When one worker primary fails, the fastest safe path is normally promotion of its streaming standby, not restoration of yesterday’s backup. Citus documents whole-worker streaming replication as its worker-failure mechanism. Patroni can automate Citus coordinator and worker group failover and maintain coordinator node metadata, but HA automation still does not replace an offline, independently retained backup: &lt;a href=&quot;https://www.citusdata.com/blog/2023/03/06/patroni-3-0-and-citus-scalable-ha-postgres/&quot;&gt;Patroni and Citus&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Keep the decisions separate:&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Event&lt;/th&gt;&lt;th&gt;First recovery path&lt;/th&gt;&lt;th&gt;Backup role&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;One primary host fails; standby is healthy&lt;/td&gt;&lt;td&gt;Fence old primary, promote standby, update or verify Citus routing&lt;/td&gt;&lt;td&gt;Last resort if replication copy is unusable&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Storage corruption replicated to standby&lt;/td&gt;&lt;td&gt;Restore affected PostgreSQL group, then prove cluster consistency&lt;/td&gt;&lt;td&gt;Primary recovery source&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Coordinator primary fails&lt;/td&gt;&lt;td&gt;Promote its standby and verify topology metadata&lt;/td&gt;&lt;td&gt;Restore metadata if no valid standby exists&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Region is lost&lt;/td&gt;&lt;td&gt;Rebuild the entire recovery unit in isolation&lt;/td&gt;&lt;td&gt;Primary recovery source&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tenant deletes data accidentally&lt;/td&gt;&lt;td&gt;Restore a separate cluster, extract tenant rows, reconcile forward&lt;/td&gt;&lt;td&gt;Source for selective repair&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;A replica protects service continuity. It can also reproduce operator mistakes, logical corruption, and destructive DDL. A backup protects a previous state. Neither is a substitute for the other.&lt;/p&gt;
&lt;h3 id=&quot;restore-in-quarantine-and-in-dependency-order&quot;&gt;Restore in quarantine and in dependency order&lt;/h3&gt;
&lt;p&gt;Do not point application traffic at the first node that starts. Restore into an isolated network with outbound controls so subscriptions, CDC connectors, email jobs, payment calls, and event publishers cannot act on historical data.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Select[select one complete recovery manifest] --&gt; Build[build isolated matching infrastructure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Build --&gt; Globals[restore packages roles config and certificates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Globals --&gt; Nodes[restore coordinator and every worker base]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Nodes --&gt; Replay[replay each node WAL to the shared named point]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Replay --&gt; Resolve[start nodes and allow Citus recovery logic]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Resolve --&gt; Topology[compare node shard and placement metadata]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Topology --&gt; Data[validate reference data tenant paths and invariants]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Data --&gt; Failover[test coordinator and worker behavior]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Failover --&gt; Cutover[choose controlled repair or production cutover]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The operational sequence is:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Choose one manifest; never mix a convenient worker backup from one generation with a coordinator from another.&lt;/li&gt;
&lt;li&gt;Provision the same PostgreSQL major, compatible Citus binary, extension dependencies, collation environment, and storage layout.&lt;/li&gt;
&lt;li&gt;Restore roles, tablespaces, certificates, access rules, and required configuration without enabling application access.&lt;/li&gt;
&lt;li&gt;Restore the coordinator and all required workers from their recorded bases.&lt;/li&gt;
&lt;li&gt;Configure each node to read only its own WAL namespace and recover to the shared named restore point.&lt;/li&gt;
&lt;li&gt;Start the restored nodes under controlled access and allow PostgreSQL and Citus transaction recovery to complete.&lt;/li&gt;
&lt;li&gt;Compare &lt;code&gt;pg_dist_node&lt;/code&gt;, distributed-table metadata, shard placements, reference tables, extension versions, and prepared transactions against the manifest.&lt;/li&gt;
&lt;li&gt;Execute tenant-local and cross-node invariant checks before any cutover.&lt;/li&gt;
&lt;li&gt;Test the application through the normal coordinator endpoint with every external side effect disabled.&lt;/li&gt;
&lt;li&gt;Decide whether the result is for full cutover, selective repair, evidence collection, or disposal.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Recovery ordering is a control-plane decision, not a claim that workers must finish WAL replay serially. Nodes can often be restored and replayed in parallel. The gate is that no application traffic is admitted until the complete distributed state passes validation.&lt;/p&gt;
&lt;h3 id=&quot;recover-one-tenant-without-rewinding-every-tenant&quot;&gt;Recover one tenant without rewinding every tenant&lt;/h3&gt;
&lt;p&gt;Tenant 418 accidentally deletes today’s orders at 10:42. A full in-place PITR to 10:41 would recover tenant 418 by deleting every legitimate transaction that all other tenants committed after 10:41. That is usually the wrong blast radius.&lt;/p&gt;
&lt;p&gt;Citus row-based sharding commonly puts multiple tenant values in the same shard. Citus documents that sharing explicitly; a tenant can be isolated to a dedicated shard for workload management, but isolation does not convert physical cluster backup into a safe row-level restore feature: &lt;a href=&quot;https://docs.citusdata.com/en/stable/admin_guide/cluster_management.html#tenant-isolation&quot;&gt;Citus tenant isolation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Use an isolated-recovery workflow:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Delete[tenant 418 data is deleted] --&gt; Freeze[stop further destructive workflow for that tenant]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Freeze --&gt; Restore[restore full Citus cluster in quarantine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Restore --&gt; Target[recover to point before deletion]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Target --&gt; Export[export tenant 418 rows by distribution key]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Export --&gt; Compare[compare historical rows with current production]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Compare --&gt; Repair[apply reviewed idempotent repair]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Repair --&gt; Verify[verify tenant and cross-table invariants]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Export the complete colocated tenant graph, not only &lt;code&gt;orders&lt;/code&gt;. For RetailCo that may include order items, payment state, inventory reservations, shipments, and audit references. Then classify rows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;deleted rows that should be reinserted;&lt;/li&gt;
&lt;li&gt;rows recreated after the incident that must not be duplicated;&lt;/li&gt;
&lt;li&gt;rows changed legitimately after the restore point that must win;&lt;/li&gt;
&lt;li&gt;external side effects that cannot be replayed blindly;&lt;/li&gt;
&lt;li&gt;sequences, idempotency keys, and uniqueness constraints affected by the repair.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The repair should use a reviewed, restartable process with explicit conflict handling. It should not attach old shard files to production or copy a restored Citus shard behind the coordinator’s metadata. Even a dedicated tenant shard participates in colocation, reference data, metadata, and transactions whose correctness must be reconciled logically.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented behavior leads to a practical recovery program, but it does not produce RetailCo’s RPO or RTO on paper.&lt;/p&gt;
&lt;h3 id=&quot;decision-1--make-backup-coordination-version-aware&quot;&gt;Decision 1 — make backup coordination version-aware&lt;/h3&gt;
&lt;p&gt;For Citus 14.0, the enhanced restore point is available for cluster-wide WAL markers, including multi-writer metadata mode. Other installed lines need a separately proven write-quiescence control, one manifest, per-node WAL continuity, and an upgrade plan when their recovery semantics cannot meet the requirement.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result to verify:&lt;/strong&gt; a restored snapshot generation contains no unresolved distributed decision caused by the capture window, and topology metadata matches the worker placements.&lt;/p&gt;
&lt;p&gt;No result is claimed here until a restore drill verifies it.&lt;/p&gt;
&lt;h3 id=&quot;decision-2--test-pitr-as-a-cluster-operation&quot;&gt;Decision 2 — test PITR as a cluster operation&lt;/h3&gt;
&lt;p&gt;Create a uniquely named Citus restore point, record it in the manifest, generate controlled writes before and after it, then recover every node to that name in quarantine.&lt;/p&gt;
&lt;p&gt;The test should include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a single-shard transaction before the marker;&lt;/li&gt;
&lt;li&gt;a multi-worker transaction before the marker;&lt;/li&gt;
&lt;li&gt;equivalent transactions after the marker;&lt;/li&gt;
&lt;li&gt;a topology or schema change near, but not during, the marker;&lt;/li&gt;
&lt;li&gt;worker and coordinator restarts during the drill;&lt;/li&gt;
&lt;li&gt;a check for unresolved prepared transactions;&lt;/li&gt;
&lt;li&gt;application invariants proving that pre-marker work exists and post-marker work does not.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Result to verify:&lt;/strong&gt; all participating nodes agree on the intended distributed state, not merely that PostgreSQL starts.&lt;/p&gt;
&lt;h3 id=&quot;decision-3--measure-the-real-recovery-path&quot;&gt;Decision 3 — measure the real recovery path&lt;/h3&gt;
&lt;p&gt;Run at least three different drills:&lt;/p&gt;





















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Drill&lt;/th&gt;&lt;th&gt;What it proves&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Worker rebuild&lt;/td&gt;&lt;td&gt;A failed worker group can be reconstructed and rejoined safely&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Full-cluster regional restore&lt;/td&gt;&lt;td&gt;Infrastructure, backups, WAL, packages, secrets, and topology can form a working cluster elsewhere&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tenant repair&lt;/td&gt;&lt;td&gt;The team can extract and reconcile one tenant without rewinding unrelated tenants&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Capture discovery time, infrastructure time, bytes restored, WAL replay time, validation time, decision time, and cutover time separately. The observed total—not a tool’s backup timestamp—determines the achievable recovery time. The newest fully restorable manifest and continuous WAL coverage—not a configured retention value—bound the achievable recovery point.&lt;/p&gt;
&lt;h3 id=&quot;decision-4--make-validation-executable&quot;&gt;Decision 4 — make validation executable&lt;/h3&gt;
&lt;p&gt;A successful startup is only the first gate. Automate checks for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the expected node groups and active primaries;&lt;/li&gt;
&lt;li&gt;shard counts and placements for every distributed relation;&lt;/li&gt;
&lt;li&gt;missing, extra, inactive, or unhealthy placements;&lt;/li&gt;
&lt;li&gt;matching Citus and PostgreSQL versions;&lt;/li&gt;
&lt;li&gt;reference-table availability on required nodes;&lt;/li&gt;
&lt;li&gt;no unexpected prepared transactions;&lt;/li&gt;
&lt;li&gt;row counts and financial totals by tenant and time window;&lt;/li&gt;
&lt;li&gt;order-to-item, order-to-payment, and reservation invariants;&lt;/li&gt;
&lt;li&gt;read and write paths for canary tenants;&lt;/li&gt;
&lt;li&gt;external integrations remaining disabled until explicitly released.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The documented pattern is simple: recovery proof must test the distributed database and the application boundary, not only the backup files.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure in the design&lt;/th&gt;&lt;th&gt;Why the backup can still look healthy&lt;/th&gt;&lt;th&gt;Consequence during recovery&lt;/th&gt;&lt;th&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Independent snapshots&lt;/td&gt;&lt;td&gt;Every node snapshot succeeds locally&lt;/td&gt;&lt;td&gt;Distributed transaction states can disagree&lt;/td&gt;&lt;td&gt;Use a validated maintenance boundary and one named restore point&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Stale coordinator generation&lt;/td&gt;&lt;td&gt;Metadata is small and restores quickly&lt;/td&gt;&lt;td&gt;It can describe the wrong shard topology&lt;/td&gt;&lt;td&gt;Restore from one manifest and compare placements&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Missing worker WAL&lt;/td&gt;&lt;td&gt;Base backup is present&lt;/td&gt;&lt;td&gt;Node cannot reach the common restore point&lt;/td&gt;&lt;td&gt;Isolate WAL namespaces and alert on archive gaps&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One shared WAL prefix&lt;/td&gt;&lt;td&gt;File names look valid&lt;/td&gt;&lt;td&gt;Different node histories can collide&lt;/td&gt;&lt;td&gt;Namespace by cluster, node system identifier, and timeline&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Logical dump as the only DR path&lt;/td&gt;&lt;td&gt;Dump command succeeds&lt;/td&gt;&lt;td&gt;Restore may be too slow and cannot replay WAL&lt;/td&gt;&lt;td&gt;Maintain physical backup and WAL; test logical restore separately&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica treated as backup&lt;/td&gt;&lt;td&gt;Standby is current&lt;/td&gt;&lt;td&gt;Corruption or destructive SQL is replicated&lt;/td&gt;&lt;td&gt;Keep isolated, immutable, independently retained backups&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Config omitted&lt;/td&gt;&lt;td&gt;Database pages are intact&lt;/td&gt;&lt;td&gt;Node starts with wrong access, extensions, or archive settings&lt;/td&gt;&lt;td&gt;Version and back up configuration and packages&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Writers not resumed&lt;/td&gt;&lt;td&gt;Snapshot request fails midway&lt;/td&gt;&lt;td&gt;The maintenance boundary becomes an outage&lt;/td&gt;&lt;td&gt;Use guaranteed cleanup, explicit ownership, and an abort runbook&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Untested arbitrary-time PITR&lt;/td&gt;&lt;td&gt;Every node reaches the same clock time&lt;/td&gt;&lt;td&gt;A distributed decision may not be proven consistent&lt;/td&gt;&lt;td&gt;Prefer a Citus named restore point and validate in quarantine&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Direct shard-file tenant restore&lt;/td&gt;&lt;td&gt;Old rows exist in recovered storage&lt;/td&gt;&lt;td&gt;Metadata, colocation, and current rows can diverge&lt;/td&gt;&lt;td&gt;Extract tenant data logically and reconcile&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Restore drill tests startup only&lt;/td&gt;&lt;td&gt;All PostgreSQL processes are green&lt;/td&gt;&lt;td&gt;Application or data invariants can still be wrong&lt;/td&gt;&lt;td&gt;Run topology, transaction, tenant, and side-effect tests&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;when-i-would-not-accept-self-managed-citus-recovery&quot;&gt;When I would not accept self-managed Citus recovery&lt;/h3&gt;
&lt;p&gt;I would not recommend this architecture when the organization cannot own all-node backup coordination, per-node WAL lifecycle, version-aligned rebuilds, distributed validation, and regular full-scale restore drills.&lt;/p&gt;
&lt;p&gt;I would also reject a design whose RTO assumes that dozens of terabytes can be restored and replayed faster than the environment has demonstrated. At that point the options are to reduce the recovery unit through tenant or domain decomposition, maintain tested warm capacity, accept a different recovery objective, or choose a managed platform whose documented recovery model fits the requirement. Managed does not remove the need for application-level validation or tenant-repair planning; it changes who operates the underlying backup machinery.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;For RetailCo, the recommendation is a layered design: streaming standbys for single-node availability, coordinated physical backups plus per-node WAL for disaster recovery, logical exports for portability and selective repair, and versioned infrastructure for reconstruction. A deleted tenant should be repaired from an isolated full-cluster restore; the live cluster should not be rewound for everyone.&lt;/p&gt;
&lt;p&gt;The main advantage is recoverability at distributed scale without pretending that each node is an independent database. The main limitation is operational: the recovery unit grows with the cluster, and the proof requires a full distributed restore. Operational complexity is high. Migration complexity is moderate for backup tooling but high for teams that currently have only node-by-node jobs.&lt;/p&gt;
&lt;p&gt;The conclusion is not that Citus cannot be backed up. It is that &lt;strong&gt;the backup boundary must be as distributed as the transaction boundary&lt;/strong&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Per-node backup success does not prove a recoverable Citus state.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Treat the coordinator, every worker, WAL, metadata, globals, configuration, and one consistency boundary as a single recovery unit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Restore a complete generation to quarantine, replay every node to one Citus restore-point name, validate distributed transactions and topology, and run a separate tenant-repair drill.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Inventory the installed versions first. Use &lt;code&gt;citus_create_restore_point()&lt;/code&gt; for the cluster-wide PITR marker — or &lt;code&gt;citus_cluster_changes_block()&lt;/code&gt; on Citus 14.1 and later — enforce a tested maintenance boundary for snapshot generations, and do not publish an RPO or RTO until the measured restore meets it.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>failures</category><category>architecture</category></item><item><title>Cloud Architecture Review Checklist for Database-Backed Applications</title><link>https://rajivonai.com/blog/2024-09-12-cloud-architecture-review-checklist-for-database-backed-applications/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-09-12-cloud-architecture-review-checklist-for-database-backed-applications/</guid><description>Review checklist for database-backed cloud applications: connection saturation, migration locking, retry amplification, and region dependency failures.</description><pubDate>Thu, 12 Sep 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Most cloud architecture reviews fail because they inspect topology before they inspect failure. The database is drawn as a box, the application tier as another box, and the review turns into a discussion about instance sizes, replicas, and network paths. The harder question is operational: when latency rises, connections saturate, retries multiply, migrations lock hot tables, or a region loses dependency access, what prevents the application from turning a database symptom into a customer-facing outage?&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Database-backed applications have changed shape. A typical service is no longer a single application talking to one database over a private network. It may run across containers, serverless jobs, queues, caches, search indexes, object storage, feature flag systems, identity providers, and third-party APIs. The database remains the system of record, but the user path increasingly depends on many control planes and data planes staying within their expected latency budgets.&lt;/p&gt;
&lt;p&gt;Cloud platforms make the first version easy to deploy. Managed databases remove backup scripts, failover automation, patch windows, and much of the storage plumbing. That convenience is real. It also changes the review burden. Engineers now need to verify the contracts around the managed service: connection limits, failover behavior, replication lag, backup restore time, parameter changes, maintenance windows, identity policies, encryption boundaries, and observability.&lt;/p&gt;
&lt;p&gt;The architecture review should therefore be less about whether a diagram looks cloud native and more about whether the system degrades deliberately.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The common review checklist is too static. It asks whether the database is replicated, whether backups exist, whether TLS is enabled, whether the application has autoscaling, and whether monitoring is configured. Those are necessary checks, but they do not expose the most expensive failures.&lt;/p&gt;
&lt;p&gt;The expensive failures happen in the interactions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Autoscaling adds application instances faster than the database can accept new connections.&lt;/li&gt;
&lt;li&gt;Retry policies amplify a short database stall into sustained overload.&lt;/li&gt;
&lt;li&gt;Read replicas hide primary pressure until replication lag invalidates user workflows.&lt;/li&gt;
&lt;li&gt;A migration that passed staging blocks production writes because production cardinality is different.&lt;/li&gt;
&lt;li&gt;A cache masks database latency until eviction, deployment, or regional failover makes all callers miss at once.&lt;/li&gt;
&lt;li&gt;A backup policy exists, but the restore path has never been timed against the recovery objective.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The review question is not, “Do we have the right components?” It is: &lt;strong&gt;can this application keep its database failure modes bounded, observable, and reversible under production load?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;A useful architecture review for a database-backed cloud application follows the request path, the write path, and the recovery path. Each path should expose limits, contracts, and rollback points.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[client request — external traffic] --&gt; B[edge controls — auth and rate limits]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[application tier — bounded concurrency]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[connection pool — fixed database pressure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[primary database — writes and transactions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; F[cache layer — explicit freshness contract]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; G[read replica — bounded stale reads]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; H[change stream — async propagation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[workers — idempotent side effects]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; J[backup system — restore tested]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; K[metrics and traces — saturation visible]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; L[runbook — rollback and failover]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The checklist should start with traffic admission. Every service needs a clear maximum for concurrent database work. Autoscaling policies should not be allowed to create unbounded database pressure. Connection pools should be sized from database capacity, not from the number of application instances. If the application uses serverless compute, the review must account for burst concurrency and cold starts creating connection storms.&lt;/p&gt;
&lt;p&gt;Next, inspect transaction design. Long transactions, interactive transactions, and transactions that call remote services are architecture smells. The database should protect invariants, but application code should avoid holding locks while waiting on external systems. For high-contention workflows, the review should ask how conflicts are detected, retried, surfaced, and measured.&lt;/p&gt;
&lt;p&gt;Then inspect read behavior. Read replicas are not a generic scaling button. They introduce a consistency contract. If a user writes data and immediately reads from a replica, the product may observe stale state unless the application routes read-after-write flows to the primary, uses session consistency, or makes staleness acceptable in the interface.&lt;/p&gt;
&lt;p&gt;Caching deserves a separate pass. The review should document what each cache entry means, how it expires, what invalidates it, and what happens when the cache is empty. A cache that protects a database in steady state can become an outage accelerator during mass eviction. Warmup, request coalescing, negative caching, and backpressure belong in the design, not in the incident retrospective.&lt;/p&gt;
&lt;p&gt;Finally, review recovery. Backups are not a recovery strategy until restores are exercised. The architecture needs defined recovery point objective, recovery time objective, restore ownership, data validation steps, and a tested path for reconnecting applications to the restored database.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;h3 id=&quot;context&quot;&gt;Context&lt;/h3&gt;
&lt;p&gt;The documented pattern across cloud reliability literature is that overload often propagates through retries and shared dependencies. The &lt;a href=&quot;https://sre.google/sre-book/handling-overload/&quot;&gt;Google SRE book chapter on handling overload&lt;/a&gt; describes overload as a system-level condition requiring load shedding, graceful degradation, and capacity-aware admission control. The database-backed application version of this pattern is direct: if every caller retries failed database work without a budget, the database receives more work precisely when it has the least capacity to serve it.&lt;/p&gt;
&lt;h3 id=&quot;action&quot;&gt;Action&lt;/h3&gt;
&lt;p&gt;The review action is to require retry budgets, deadlines, and idempotency. Amazon’s Builders’ Library article on &lt;a href=&quot;https://aws.amazon.com/builders-library/timeouts-retries-and-backoff-with-jitter/&quot;&gt;timeouts, retries, and backoff with jitter&lt;/a&gt; documents the operational pattern: timeouts must be chosen from downstream latency behavior, retries should be limited, and jitter helps avoid synchronized retry waves. In a database-backed system, that means every database call should sit inside a request deadline, every retry should have a bounded count, and every retried write should be safe through an idempotency key, natural constraint, or transactionally recorded operation identifier.&lt;/p&gt;
&lt;h3 id=&quot;result&quot;&gt;Result&lt;/h3&gt;
&lt;p&gt;The result is not “no failures.” The result is bounded failure. PostgreSQL, for example, documents transaction isolation levels and serialization failures as normal concurrency outcomes rather than exceptional mysteries. Under &lt;code&gt;SERIALIZABLE&lt;/code&gt;, applications must be prepared to retry transactions that fail due to serialization anomalies. Under weaker isolation, applications must understand which anomalies they have accepted. The architectural learning is that correctness is partly a database feature and partly an application contract.&lt;/p&gt;
&lt;h3 id=&quot;learning&quot;&gt;Learning&lt;/h3&gt;
&lt;p&gt;The documented pattern is that database reliability depends on explicit contracts at the edges: admission control before the database, transaction boundaries inside the database, consistency rules around replicas, and recovery tests outside the live path. A review that cannot name those contracts has not reviewed the architecture. It has reviewed the drawing.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Review Area&lt;/th&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Better Question&lt;/th&gt;&lt;th&gt;Common Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Autoscaling&lt;/td&gt;&lt;td&gt;Application fleet outgrows database connection capacity&lt;/td&gt;&lt;td&gt;What caps concurrent database work?&lt;/td&gt;&lt;td&gt;Pool limits, proxy, admission control&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Retries&lt;/td&gt;&lt;td&gt;Short stall becomes sustained overload&lt;/td&gt;&lt;td&gt;What is the retry budget per request?&lt;/td&gt;&lt;td&gt;Deadlines, backoff, jitter, idempotency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replicas&lt;/td&gt;&lt;td&gt;Stale reads break user workflows&lt;/td&gt;&lt;td&gt;Which reads require fresh data?&lt;/td&gt;&lt;td&gt;Primary routing, session reads, explicit staleness&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Migrations&lt;/td&gt;&lt;td&gt;Schema change blocks hot production paths&lt;/td&gt;&lt;td&gt;How is lock impact tested?&lt;/td&gt;&lt;td&gt;Online migrations, batching, rollback plan&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Caching&lt;/td&gt;&lt;td&gt;Cache miss storm overloads primary&lt;/td&gt;&lt;td&gt;What happens on cold cache?&lt;/td&gt;&lt;td&gt;Request coalescing, warmup, backpressure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backups&lt;/td&gt;&lt;td&gt;Backup exists but restore misses objective&lt;/td&gt;&lt;td&gt;When was restore last timed?&lt;/td&gt;&lt;td&gt;Restore drills, validation scripts, runbooks&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Observability&lt;/td&gt;&lt;td&gt;Metrics show symptoms but not saturation&lt;/td&gt;&lt;td&gt;Can we see queueing before errors?&lt;/td&gt;&lt;td&gt;Pool metrics, wait time, lock time, replica lag&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failover&lt;/td&gt;&lt;td&gt;Promotion succeeds but app does not recover&lt;/td&gt;&lt;td&gt;Who changes writers and verifies data?&lt;/td&gt;&lt;td&gt;Automated failover tests, DNS and connection review&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The tradeoff is that these checks add friction before launch. They force teams to define limits earlier than they would prefer. That friction is useful. A database-backed application without declared limits still has limits; it discovers them during incidents.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt; — Start the review from failure modes, not component inventory. Ask how the application behaves when the database is slow, unavailable, stale, locked, overloaded, or restored from backup.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt; — Require explicit contracts for concurrency, retries, transactions, replicas, caches, migrations, observability, and recovery. Put those contracts in the design review and the runbook.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Proof&lt;/strong&gt; — Verify the contracts with load tests, migration rehearsals, restore drills, replica lag tests, cache cold-start tests, and dashboards that show saturation before user-visible errors.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Action&lt;/strong&gt; — Before approving the architecture, make the team answer one operational question in writing: what exact mechanism prevents this application from making a struggling database worse?&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>cloud</category><category>databases</category><category>failures</category><category>checklist</category></item><item><title>Prometheus and Grafana for Database Monitoring: PostgreSQL and MySQL Setup</title><link>https://rajivonai.com/blog/2024-09-09-prometheus-grafana-database-monitoring-setup-postgres-mysql/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-09-09-prometheus-grafana-database-monitoring-setup-postgres-mysql/</guid><description>How to instrument PostgreSQL and MySQL with postgres_exporter and mysqld_exporter, configure Prometheus scrape jobs, and build Grafana panels that surface the metrics that matter — with working PromQL queries.</description><pubDate>Mon, 09 Sep 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Prometheus and Grafana are the right default for database monitoring when the team already runs them for infrastructure. The mistake is treating database exporters as install-and-forget: they require scope decisions, scrape tuning, recording rules for expensive queries, and panels aligned to operational questions rather than metric availability.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Prometheus with postgres_exporter or mysqld_exporter gives a team database metrics in the same system they use for Kubernetes, application, and infrastructure metrics. That consistency matters during incidents: one tool, one query language, one dashboard system.&lt;/p&gt;
&lt;p&gt;The challenge is setup quality. Both exporters expose hundreds of metrics by default. Without scope decisions and recording rules, the result is a Prometheus instance ingesting metrics that nobody queries, Grafana dashboards that show every metric but answer no operational question, and a scrape interval too infrequent to catch short-duration failures.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Likely cause&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Grafana database dashboard shows data but engineer can’t tell if system is healthy&lt;/td&gt;&lt;td&gt;Dashboard shows metrics, not answers — no thresholds, no anomaly detection&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Prometheus scrape latency is high&lt;/td&gt;&lt;td&gt;Exporter is running expensive queries during scrape; needs collector filtering&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Database monitoring is absent during Prometheus downtime&lt;/td&gt;&lt;td&gt;No remote write or long-term storage — single point of failure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Alert fires but metric data is missing&lt;/td&gt;&lt;td&gt;Scrape interval too long for the alert evaluation window&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Exporter crashes after database restart&lt;/td&gt;&lt;td&gt;Exporter not configured to retry connections&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;1. Is postgres_exporter running with appropriate collector scope?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;postgres_exporter&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --collector.stat_activity_autovacuum&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --collector.stat_statements&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --collector.stat_bgwriter&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --collector.stat_replication&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --collector.replication_slot&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --no-collector.wal&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --no-collector.database_wraparound&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --web.listen-address=:9187&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Disable expensive collectors you do not need. &lt;code&gt;database_wraparound&lt;/code&gt; queries &lt;code&gt;age(datfrozenxid)&lt;/code&gt; on every database and can be slow on instances with many databases. Enable only the collectors you have dashboard panels for.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Is the scrape interval appropriate?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For OLTP databases, scrape every 30 seconds. For analytics-heavy workloads with slow collector queries, 60 seconds is acceptable. Shorter than 30 seconds risks accumulating scrape delays during high-load periods.&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;prometheus.yml&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;scrape_configs&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;job_name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;postgres&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    scrape_interval&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;30s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    scrape_timeout&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;20s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    static_configs&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;targets&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;postgres-exporter:9187&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        labels&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          env&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;production&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          db_engine&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;postgres&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          cluster&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;primary&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;3. Are recording rules defined for expensive derived metrics?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PromQL queries that compute ratios from raw counters on every dashboard load are expensive at query time. Move them into recording rules evaluated once per scrape.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# prometheus/rules/database.yaml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;groups&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;database_derived&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    interval&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;60s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    rules&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;record&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;postgres:cache_hit_ratio&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        expr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          rate(pg_statio_user_tables_heap_blks_hit[5m]) /&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          (rate(pg_statio_user_tables_heap_blks_hit[5m]) +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;           rate(pg_statio_user_tables_heap_blks_read[5m]))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;record&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;postgres:connections_pct&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        expr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          pg_stat_activity_count{state!=&quot;idle&quot;} /&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          pg_settings_max_connections * 100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;record&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;postgres:replication_lag_seconds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        expr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;|&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          pg_replication_lag&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;4. Are alert rules configured with meaningful labels?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;groups&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;postgres_alerts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    rules&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;alert&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;PostgresReplicaLagHigh&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        expr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;pg_replication_lag &gt; 60&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;2m&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        labels&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          severity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;warning&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          team&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;database&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        annotations&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          summary&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;PostgreSQL replica lag above 60s on {{ $labels.instance }}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          runbook_url&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;https://wiki.example.com/runbooks/postgres-replica-lag&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      - &lt;/span&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;alert&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;PostgresConnectionsNearLimit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        expr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;postgres:connections_pct &gt; 85&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;5m&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        labels&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          severity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;critical&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          team&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;database&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;        annotations&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;          summary&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;PostgreSQL connections at {{ $value | humanize }}% on {{ $labels.instance }}&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;5. Is mysqld_exporter configured with the right user grants?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; USER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &apos;&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;prometheus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;&apos;@&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; IDENTIFIED &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BY&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;use-secret-manager-here&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; PROCESS, REPLICATION CLIENT, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TO&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;prometheus&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- For performance_schema access:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GRANT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; performance_schema.&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TO&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;prometheus&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;FLUSH PRIVILEGES;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The exporter connects as this user. Grant only what the collectors actually need — not &lt;code&gt;SUPER&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Set up database monitoring with Prometheus] --&gt; B[Install exporter]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C{Scope collectors}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|High-traffic OLTP| D[Enable: stat_activity, stat_statements, stat_bgwriter, stat_replication, locks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|Analytics replica| E[Enable: stat_statements, replication_slot, database_size]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; F[Set scrape interval 30s]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Define recording rules for ratios]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[Build Grafana panels by operational question]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I{Alert rules}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|Define warning + critical| J[Set runbook URL on every alert]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[Test alert with simulated failure in staging]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;core-grafana-panel-design&quot;&gt;Core Grafana Panel Design&lt;/h2&gt;
&lt;p&gt;Build panels that answer operational questions, not panels that display metrics.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Question&lt;/th&gt;&lt;th&gt;Panel type&lt;/th&gt;&lt;th&gt;PromQL&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Is replica lag within SLO?&lt;/td&gt;&lt;td&gt;Gauge + threshold&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_replication_lag{instance=&quot;$instance&quot;}&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;How close are we to connection limit?&lt;/td&gt;&lt;td&gt;Gauge + threshold&lt;/td&gt;&lt;td&gt;&lt;code&gt;postgres:connections_pct{instance=&quot;$instance&quot;}&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Which queries are slowest right now?&lt;/td&gt;&lt;td&gt;Table&lt;/td&gt;&lt;td&gt;&lt;code&gt;topk(10, rate(pg_stat_statements_total_time[5m]))&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Is cache hit ratio healthy?&lt;/td&gt;&lt;td&gt;Time series&lt;/td&gt;&lt;td&gt;&lt;code&gt;postgres:cache_hit_ratio{instance=&quot;$instance&quot;}&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Which tables have the most dead tuples?&lt;/td&gt;&lt;td&gt;Bar chart&lt;/td&gt;&lt;td&gt;&lt;code&gt;topk(10, pg_stat_user_tables_n_dead_tup)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Is checkpoint behavior normal?&lt;/td&gt;&lt;td&gt;Time series&lt;/td&gt;&lt;td&gt;&lt;code&gt;rate(pg_stat_bgwriter_checkpoints_req[5m])&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;For MySQL:&lt;/p&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Question&lt;/th&gt;&lt;th&gt;PromQL&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Replication lag&lt;/td&gt;&lt;td&gt;&lt;code&gt;mysql_slave_status_seconds_behind_master&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Threads running&lt;/td&gt;&lt;td&gt;&lt;code&gt;mysql_global_status_threads_running&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;InnoDB buffer pool wait&lt;/td&gt;&lt;td&gt;&lt;code&gt;rate(mysql_global_status_innodb_buffer_pool_wait_free[5m])&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slow queries per second&lt;/td&gt;&lt;td&gt;&lt;code&gt;rate(mysql_global_status_slow_queries[5m])&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Open tables vs cache&lt;/td&gt;&lt;td&gt;&lt;code&gt;mysql_global_status_open_tables / mysql_global_variables_table_open_cache&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;If the exporter is causing database load:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Disable the problematic collector immediately: restart the exporter with &lt;code&gt;--no-collector.&amp;#x3C;name&gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;pg_stat_activity&lt;/code&gt; for exporter sessions with long durations.&lt;/li&gt;
&lt;li&gt;Increase &lt;code&gt;scrape_timeout&lt;/code&gt; to avoid Prometheus treating slow scrapes as failed.&lt;/li&gt;
&lt;li&gt;If the database is degraded, disable the exporter entirely and fall back to CloudWatch or basic OS metrics until the database is stable.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Dashboards as code&lt;/strong&gt;: store Grafana dashboard JSON in Git and use &lt;code&gt;grafana-dashboard-exporter&lt;/code&gt; or Terraform to provision dashboards. This prevents dashboard drift between environments.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Exporter configuration templates&lt;/strong&gt;: manage &lt;code&gt;postgres_exporter&lt;/code&gt; configuration through a Helm chart or Ansible role with environment-specific variables. The monitoring role credentials and scrape endpoints should be provisioned through the same credential management pipeline as application secrets.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Alert rule testing&lt;/strong&gt;: use &lt;code&gt;promtool test rules&lt;/code&gt; to write unit tests for alert rules. Test that alerts fire correctly given synthetic metric data — before deploying the rules to production.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;promtool&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; test&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; rules&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; tests/database_alerts_test.yaml&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;p&gt;Prometheus and Grafana database monitoring is operationally complete only when it has four properties: appropriate collector scope (not every metric, only the ones with panels and alerts), recording rules for derived metrics (not computed on every dashboard load), alert rules with runbook links (not raw metric thresholds with no context), and tested alert coverage (simulated failures verified the alerts fire). An exporter that is installed but not tuned produces more cardinality than signal and slows down Prometheus at query time.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Exporter queries slow the database&lt;/td&gt;&lt;td&gt;Default collectors include expensive queries (e.g., bloat estimation)&lt;/td&gt;&lt;td&gt;Disable unused collectors; enable only what has dashboard panels&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Alert fires too often&lt;/td&gt;&lt;td&gt;Scrape every 15s, alert window is 1m — transient spikes trigger alert&lt;/td&gt;&lt;td&gt;Increase &lt;code&gt;for&lt;/code&gt; duration to 2–5 minutes for metric volatility&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Dashboard has 40 panels, no one knows what to look at&lt;/td&gt;&lt;td&gt;Metrics-first design instead of question-first&lt;/td&gt;&lt;td&gt;Redesign from operational questions, not metric availability&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Exporter loses database connection silently&lt;/td&gt;&lt;td&gt;PostgreSQL restart drops exporter connection; exporter does not reconnect&lt;/td&gt;&lt;td&gt;Set &lt;code&gt;--web.config.file&lt;/code&gt; reconnect policy; use Kubernetes liveness probe&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Alert runbook link is dead&lt;/td&gt;&lt;td&gt;Wiki reorganized, link not updated&lt;/td&gt;&lt;td&gt;Store runbook URL as a configmap value; validate links in CI&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Database monitoring uses Prometheus but panels show raw metrics, not operational health.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Add recording rules for derived metrics, build question-first panels, and add alert rules with runbook URLs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Walk through an incident simulation: kill one replica, verify the lag alert fires within 2 minutes, confirm the runbook link points to the correct procedure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, define three recording rules (connection utilization, replica lag, cache hit ratio), create an alert for each at the critical threshold, and add a Grafana time series panel for each.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>checklist</category></item><item><title>Service Decomposition Review: When a New Microservice Creates a Worse Database Problem</title><link>https://rajivonai.com/blog/2024-08-28-service-decomposition-review-when-a-new-microservice-creates-a-worse-database-problem/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-08-28-service-decomposition-review-when-a-new-microservice-creates-a-worse-database-problem/</guid><description>Splitting a service without relocating the database boundary creates distributed coordination overhead worse than the monolith the split was meant to fix.</description><pubDate>Wed, 28 Aug 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A service split that leaves the database boundary intact is not decomposition; it is a distributed lock manager with better branding.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most service decomposition proposals start with a reasonable pressure: one codebase has become too large for one team to change safely. Deployments queue behind unrelated work. Incidents require people who understand half the company. A single table has accumulated columns for every workflow that ever touched it. The proposed answer is familiar: extract a capability into its own microservice.&lt;/p&gt;
&lt;p&gt;That answer can be correct. But the first review question should not be “Can this logic run behind an API?” It should be “Can this service own the state required to make its decisions?”&lt;/p&gt;
&lt;p&gt;When the answer is no, the new service often makes the database problem worse. The code boundary moves. The data boundary does not. The organization now pays the coordination cost of distributed systems while still depending on the same shared schema, transactions, migrations, and operational blast radius.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A common extraction looks clean on a diagram. The order service owns order workflows. The billing service owns payment state. The fulfillment service owns shipping decisions. The API calls are explicit. The repositories are separate. Each team gets a deployable unit.&lt;/p&gt;
&lt;p&gt;Then production shows the real architecture.&lt;/p&gt;
&lt;p&gt;The billing service still reads &lt;code&gt;orders.status&lt;/code&gt; because pricing depends on fulfillment state. Fulfillment still joins against &lt;code&gt;customers.plan_tier&lt;/code&gt; because delivery promises depend on account status. The order service still updates billing columns during checkout because the old transaction was the only thing preventing double submission. Every “temporary” shared query becomes part of the contract.&lt;/p&gt;
&lt;p&gt;The result is a system with three operational failure modes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Schema coupling survives the split.&lt;/strong&gt; A column rename is now a multi-service release, not an internal refactor.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Transactions become implicit protocols.&lt;/strong&gt; What used to be one database transaction becomes retries, polling, reconciliation, and compensating writes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ownership becomes ambiguous.&lt;/strong&gt; When a row is wrong, the team that owns the service may not own the table, and the team that owns the table may not own the user-facing failure.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The core question is therefore simple: &lt;strong&gt;does the proposed microservice reduce coordination around state, or does it turn one database dependency into many distributed dependencies?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;review-the-data-boundary-first&quot;&gt;Review the Data Boundary First&lt;/h2&gt;
&lt;p&gt;A service decomposition review should begin with data ownership, not HTTP endpoints. The service boundary is only credible when the service can enforce its own invariants without reaching into another service’s tables.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[decomposition proposal — new billing service] --&gt; B[review state ownership]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C{can billing own payment state}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|yes| D[private billing schema — published events]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|no| E[shared order database — hidden coupling]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[cross service joins — schema release coordination]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; G[split transactions — retries and reconciliation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; H[explicit contract — API and event versioning]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[smaller blast radius — owned migrations]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The useful review is not anti-microservice. It is anti-pretend-boundary. A database table can be shared safely for a short migration window, but it should not be the steady-state integration mechanism between services.&lt;/p&gt;
&lt;p&gt;A practical decomposition review should ask five questions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Who owns each invariant?&lt;/strong&gt;&lt;br&gt;
If billing must guarantee “an order is charged at most once,” billing needs authoritative state for charge attempts, idempotency keys, and settlement status. If that invariant depends on reading and updating order rows owned elsewhere, the boundary is weak.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What data is copied, and why is it allowed to be stale?&lt;/strong&gt;&lt;br&gt;
Microservices often require duplication. That is not a flaw by itself. The flaw is duplicating data without naming the freshness requirement. A shipping service may keep a local projection of customer address data. It must know whether a five-minute delay is acceptable and what happens when the address changes after label creation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Which operations still need atomicity?&lt;/strong&gt;&lt;br&gt;
If the extraction depends on atomic updates across two databases, the design has not finished. Either keep the operation together, redesign the invariant, or introduce a workflow pattern such as saga orchestration with explicit compensation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is the migration path off shared reads?&lt;/strong&gt;&lt;br&gt;
A service that starts by reading legacy tables should have an exit plan: backfill local state, dual-write only through controlled migration code, compare results, switch reads, and remove the old query. Without removal criteria, the shared read becomes permanent.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How will failures be repaired?&lt;/strong&gt;&lt;br&gt;
Once state crosses service boundaries, correctness depends on replay, reconciliation, idempotency, and observability. The review should include repair commands and dashboards, not only happy-path API contracts.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; Martin Fowler’s published microservices guidance emphasizes decentralized data management: each service manages its own database, either different instances of the same technology or different storage technologies. The documented pattern is not “every service gets an endpoint.” It is that services own both behavior and persistence boundaries: &lt;a href=&quot;https://martinfowler.com/articles/microservices.html&quot;&gt;https://martinfowler.com/articles/microservices.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Apply that pattern as a review constraint. If a proposed service cannot own the data required for its core decisions, classify the work as modularization or strangler migration, not completed service decomposition. Keep the label honest because the operational obligations are different.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The team avoids the most expensive middle state: separately deployed services with one shared relational core. Shared databases preserve compile-time convenience but remove local reasoning. A query that looked harmless becomes a release dependency, an index dependency, and sometimes an incident dependency.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; The documented microservice pattern is about independent change. Independent deployment without independent data ownership is only partial independence.&lt;/p&gt;
&lt;p&gt;A second public pattern comes from Amazon’s guidance on the saga pattern for distributed transactions. AWS describes saga as a way to coordinate a sequence of local transactions, where each step publishes events or triggers the next action, and failures require compensating transactions: &lt;a href=&quot;https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/saga.html&quot;&gt;https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/saga.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; The database transaction that used to protect a checkout flow does not survive a naive split into order, payment, and fulfillment services.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Replace the old atomic assumption with an explicit workflow. Each service commits locally. The workflow records progress. Retry behavior is idempotent. Compensation is designed before launch.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The system gains a visible failure model. Instead of an invisible half-committed business process spread across tables, operators can see which step failed, retry it, or compensate it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; Distributed consistency is an architecture, not an implementation detail. If the decomposition review cannot explain compensation, the split is premature.&lt;/p&gt;
&lt;p&gt;PostgreSQL’s behavior gives a more concrete database lesson. A single relational database can enforce foreign keys, unique constraints, transactions, and isolation inside its boundary. Once those tables move behind separate services and separate databases, those guarantees no longer exist as database guarantees. They must be rebuilt at the application and workflow layer.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; A monolith may have a messy schema but still rely on real transactional semantics.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Identify which constraints are currently enforced by the database before extracting the service. Unique indexes, foreign keys, check constraints, and transaction scopes are part of the architecture.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The review surfaces hidden correctness requirements that were previously invisible because the database enforced them.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; Do not decompose code until you have inventoried the constraints the database is silently carrying.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Better response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Shared database after extraction&lt;/td&gt;&lt;td&gt;Service owns code but not state&lt;/td&gt;&lt;td&gt;Treat as migration phase with removal date&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cross-service joins&lt;/td&gt;&lt;td&gt;New service needs old read model&lt;/td&gt;&lt;td&gt;Build local projection with named staleness&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Distributed transaction pressure&lt;/td&gt;&lt;td&gt;Old invariant crossed the new boundary&lt;/td&gt;&lt;td&gt;Keep boundary together or use saga workflow&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Duplicate ownership&lt;/td&gt;&lt;td&gt;Multiple services update same row&lt;/td&gt;&lt;td&gt;Assign one writer and publish changes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slow migrations&lt;/td&gt;&lt;td&gt;Schema changes require all services&lt;/td&gt;&lt;td&gt;Version data contracts and remove direct reads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Incident ambiguity&lt;/td&gt;&lt;td&gt;State and behavior have different owners&lt;/td&gt;&lt;td&gt;Put ownership in runbooks and alerts&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The table is intentionally blunt because this is where many designs fail. The hard part is not extracting code. The hard part is deciding which invariants deserve to stay together.&lt;/p&gt;
&lt;p&gt;Sometimes the right answer is not a microservice. A modular monolith with clear internal boundaries may solve the deployment and ownership problem without introducing distributed state. Sometimes the right answer is a strangler pattern: place a new API in front of the legacy behavior, migrate one capability at a time, and retire shared database access gradually. Sometimes the right answer is a real service with private persistence, events, replay, and reconciliation.&lt;/p&gt;
&lt;p&gt;The review should force the proposal to name which one it is.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The proposed microservice still depends on another service’s tables for core decisions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Redraw the boundary around state ownership, not repository structure or API shape.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Inventory current database constraints, transaction scopes, shared reads, shared writes, and operational repair paths before approving the split.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Approve the service only when shared database access has a migration plan, an owner, observability, and a removal condition.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Why pgcrypto Is Not a Full Key Management Strategy</title><link>https://rajivonai.com/blog/2024-08-26-why-pgcrypto-is-not-a-full-key-management-strategy/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-08-26-why-pgcrypto-is-not-a-full-key-management-strategy/</guid><description>PostgreSQL&apos;s pgcrypto is a cryptographic function library, not a key management system. Treating it as one guarantees your encryption keys will eventually leak.</description><pubDate>Mon, 26 Aug 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;PostgreSQL’s &lt;code&gt;pgcrypto&lt;/code&gt; is a cryptographic function library, not a key management system. Treating it as one guarantees that your encryption keys will eventually leak into your observability pipelines, rendering your entire encryption strategy mathematically irrelevant.&lt;/strong&gt; If your architecture relies on passing plaintext keys across a database connection, you do not have a key management strategy; you have a compliance illusion.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;When platform teams are tasked with implementing column-level encryption for PII, the path of least resistance is often PostgreSQL’s native &lt;code&gt;pgcrypto&lt;/code&gt; extension. It is built-in, easy to use, and requires no external infrastructure.&lt;/p&gt;




















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;Default approach&lt;/th&gt;&lt;th&gt;Better alternative&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Operating model&lt;/td&gt;&lt;td&gt;Use &lt;code&gt;pgcrypto&lt;/code&gt; to encrypt data within the database engine using keys passed in SQL&lt;/td&gt;&lt;td&gt;Use an external Key Management Service (KMS) to encrypt data in the application memory space&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failure mode&lt;/td&gt;&lt;td&gt;Keys are exposed in plaintext to the database process and observability tools&lt;/td&gt;&lt;td&gt;Keys are isolated in a dedicated IAM-governed control plane&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The fundamental flaw in using &lt;code&gt;pgcrypto&lt;/code&gt; for symmetric encryption (&lt;code&gt;pgp_sym_encrypt&lt;/code&gt;) is that the database engine itself must process the plaintext encryption key to execute the function.&lt;/p&gt;
&lt;p&gt;This creates a massive, multi-vectored exposure risk. &lt;code&gt;pgcrypto&lt;/code&gt; has no native integration with enterprise key management concepts like IAM, automated key rotation, or cryptographic audit trails. Worse, by passing the key in the SQL string, the key is instantly exposed to the database’s internal state.&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure point&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Query Telemetry&lt;/td&gt;&lt;td&gt;Plaintext keys are logged in &lt;code&gt;pg_stat_activity&lt;/code&gt; and &lt;code&gt;pg_stat_statements&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Any engineer or tool with read access to system views can steal the keys&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slow Query Logs&lt;/td&gt;&lt;td&gt;Long-running queries containing the key are written to disk&lt;/td&gt;&lt;td&gt;Keys leak into external log aggregators like Datadog, Splunk, or CloudWatch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replication Streams&lt;/td&gt;&lt;td&gt;Logical replication streams may broadcast the raw SQL&lt;/td&gt;&lt;td&gt;Downstream consumer databases and data warehouses inadvertently receive the keys&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The core architectural question is this: How do we perform column-level encryption without ever exposing the plaintext encryption key to the database’s execution engine or its telemetry pipelines?&lt;/p&gt;
&lt;h2 id=&quot;the-implementation&quot;&gt;The Implementation&lt;/h2&gt;
&lt;p&gt;The solution is to deprecate the use of &lt;code&gt;pgcrypto&lt;/code&gt; for sensitive, high-value data entirely, replacing it with an external Key Management Service (KMS) architecture.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[&quot;Application Service&quot;] --&gt;|1. Fetch Key| B[&quot;Cloud KMS&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|2. Return Key| A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt;|3. Encrypt in Memory| A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt;|4. Execute INSERT| C[&quot;PostgreSQL Database&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|5. Telemetry| D[&quot;pg_stat_statements&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Move encryption to the application compute layer.&lt;/strong&gt;&lt;br&gt;
The application fetches the encryption key from a secure vault (e.g., AWS KMS, HashiCorp Vault).&lt;br&gt;
Confirm: The key exists only in the volatile memory of the application process.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Encrypt the payload before constructing the SQL statement.&lt;/strong&gt;&lt;br&gt;
The application performs the encryption locally.&lt;br&gt;
Confirm: The SQL statement constructed by the ORM or query builder contains only the ciphertext.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Execute the query against PostgreSQL.&lt;/strong&gt;&lt;br&gt;
The database receives an &lt;code&gt;INSERT&lt;/code&gt; or &lt;code&gt;UPDATE&lt;/code&gt; containing pure ciphertext.&lt;br&gt;
Confirm: When this query is logged in &lt;code&gt;pg_stat_activity&lt;/code&gt; or shipped to Datadog via a slow query log, no plaintext keys are present in the SQL string.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for maturing database security is to aggressively ban the use of inline key passing in SQL across the organization.&lt;/p&gt;
&lt;p&gt;Context: Consider a platform team troubleshooting performance issues. They enable &lt;code&gt;pg_stat_statements&lt;/code&gt; to track query execution times.&lt;/p&gt;
&lt;p&gt;Action: Because &lt;code&gt;pg_stat_statements&lt;/code&gt; normalizes queries but retains literal values depending on configuration (or because a specific slow query log captures the raw string), queries like &lt;code&gt;SELECT pgp_sym_encrypt(&apos;user_ssn&apos;, &apos;super_secret_key&apos;);&lt;/code&gt; are captured.&lt;/p&gt;
&lt;p&gt;Result: The encryption key (&lt;code&gt;super_secret_key&lt;/code&gt;) is now permanently stored in the telemetry database. If these logs are shipped to a centralized logging vendor, the key has now left your infrastructure perimeter. The encryption is entirely compromised.&lt;/p&gt;
&lt;p&gt;Learning: Cryptographic keys must never traverse the same network boundary or reside in the same system views as the data they are protecting. The database cannot be trusted to keep a secret that it must also use to parse a query.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Infrastructure Complexity&lt;/td&gt;&lt;td&gt;Developers need to encrypt data locally during testing&lt;/td&gt;&lt;td&gt;Provide local KMS emulators (e.g., AWS KMS Local) or deterministic dev-only keys in Docker Compose&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application CPU Load&lt;/td&gt;&lt;td&gt;Shifting encryption from the database to the application spikes app-tier CPU&lt;/td&gt;&lt;td&gt;Ensure application containers are provisioned with AES-NI hardware acceleration enabled&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Legacy Codebases&lt;/td&gt;&lt;td&gt;Millions of lines of code currently rely on &lt;code&gt;pgcrypto&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Implement a database-side proxy (like PgBouncer with custom interceptors) or a slow, phased migration at the ORM layer&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Treating &lt;code&gt;pgcrypto&lt;/code&gt; as a key management system inevitably leaks plaintext encryption keys into logs, metrics, and replication streams.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Shift the cryptographic workload out of the database and into the application layer using a dedicated KMS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: A query captured in a Datadog slow query log will only show the ciphertext payload, keeping the encryption key entirely out of the observability pipeline.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Audit your &lt;code&gt;pg_stat_statements&lt;/code&gt; and slow query logs today. Search for the string &lt;code&gt;pgp_sym_encrypt&lt;/code&gt; to determine if your keys are currently being actively leaked to your logging vendors.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If your encryption strategy relies on hoping that nobody looks too closely at your query logs, it is time to redesign your key management architecture.&lt;/p&gt;</content:encoded><category>databases</category><category>security</category><category>failures</category></item><item><title>PostgreSQL Observability: Vacuum, Bloat, Locks, Replication Lag, and Query Plans</title><link>https://rajivonai.com/blog/2024-08-20-postgresql-observability-vacuum-bloat-locks/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-08-20-postgresql-observability-vacuum-bloat-locks/</guid><description>Monitoring PostgreSQL requires looking past the operating system and into the internal bookkeeping of MVCC, autovacuum, and replication streams.</description><pubDate>Tue, 20 Aug 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;If you treat PostgreSQL like a black box that only consumes CPU and Memory, you will eventually be crushed by the invisible weight of its MVCC architecture.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s Multi-Version Concurrency Control (MVCC) is powerful, but it requires continuous internal maintenance. Every &lt;code&gt;UPDATE&lt;/code&gt; creates a new row version, and every &lt;code&gt;DELETE&lt;/code&gt; marks an old row as a “dead tuple.” The &lt;code&gt;autovacuum&lt;/code&gt; daemon must eventually clean up these dead tuples to prevent table bloat and transaction ID wraparound.&lt;/p&gt;
&lt;p&gt;When teams migrate to PostgreSQL from other database engines, they often bring their generic monitoring dashboards with them. They alert on CPU spikes or memory exhaustion. But in PostgreSQL, the most dangerous failures are silent. An aggressive transaction holds a lock for too long, replication falls silently behind, or autovacuum is misconfigured and gives up on heavily updated tables. By the time these issues manifest as CPU spikes, the database is already deeply unhealthy.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;
&lt;p&gt;A failing PostgreSQL instance leaves distinct operational footprints before it fully collapses:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The Bloat Spiral:&lt;/strong&gt; Queries that used to return in milliseconds now take seconds. The table size on disk has doubled, but the actual row count hasn’t changed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Stale Stats Fallacy:&lt;/strong&gt; The query planner suddenly switches from a fast Index Scan to a catastrophic Sequential Scan because the table statistics are out of date.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Lock Cascade:&lt;/strong&gt; Application monitoring shows massive latency spikes across unrelated endpoints because a long-running reporting query is holding an &lt;code&gt;AccessShareLock&lt;/code&gt; that blocks an &lt;code&gt;AccessExclusiveLock&lt;/code&gt; requested by a schema migration, which in turn blocks all subsequent &lt;code&gt;SELECT&lt;/code&gt; queries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replication Desync:&lt;/strong&gt; The primary database is healthy, but read-heavy applications serving from replicas are displaying data that is five minutes old.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;p&gt;When a PostgreSQL incident begins, these are the queries and metrics you must check first:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Check for Blocking Sessions (&lt;code&gt;pg_locks&lt;/code&gt;):&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocked_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocked_pid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       blocking_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_pid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       blocked_activity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocked_query,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       blocking_activity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_query&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; pg_catalog&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pg_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocked_locks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; pg_catalog&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pg_stat_activity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocked_activity &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocked_activity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocked_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; pg_catalog&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pg_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_locks &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocking_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;locktype&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocked_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;locktype&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; pg_catalog&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pg_stat_activity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_activity &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocking_activity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocking_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocked_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;granted&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocking_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;granted&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Check Dead Tuples and Autovacuum Status (&lt;code&gt;pg_stat_user_tables&lt;/code&gt;):&lt;/strong&gt;
Look at &lt;code&gt;n_dead_tup&lt;/code&gt; vs &lt;code&gt;n_live_tup&lt;/code&gt;. Check &lt;code&gt;last_autovacuum&lt;/code&gt; to see if the daemon is actually completing its work.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Check Replication Lag (&lt;code&gt;pg_stat_replication&lt;/code&gt;):&lt;/strong&gt;
Compare &lt;code&gt;pg_current_wal_lsn()&lt;/code&gt; with the &lt;code&gt;replay_lsn&lt;/code&gt; of the standby to calculate the byte lag.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Identify Long-Running Transactions (&lt;code&gt;pg_stat_activity&lt;/code&gt;):&lt;/strong&gt;
Transactions sitting in &lt;code&gt;idle in transaction&lt;/code&gt; for hours are holding locks and preventing dead tuples from being vacuumed.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Examine Query Plan Regressions (&lt;code&gt;pg_stat_statements&lt;/code&gt;):&lt;/strong&gt;
If a specific query is suddenly slow, use &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; to see if it is executing a sequential scan due to stale statistics.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;p&gt;When diagnosing sudden latency in PostgreSQL, the triage path branches quickly based on locks vs. load.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Latency Spike Detected] --&gt; B{Are there blocking sessions?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes| C[Identify Blocking PID]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; C1{Is the blocker idle in transaction?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C1 --&gt;|Yes| C2[Terminate Blocker]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C1 --&gt;|No| C3[Evaluate Impact: Terminate or Wait]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No| D{Are queries using Sequential Scans?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Yes| D1[Check n_dead_tup]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D1 --&gt;|High| D2[Run VACUUM ANALYZE manually]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D1 --&gt;|Low| D3[Update pg_statistic via ANALYZE]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|No| E[Check Connection Pool]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; E1[If saturated, increase pool size or shed load]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Kill the Blocking Session (Fast, Disruptive):&lt;/strong&gt;
Using &lt;code&gt;pg_terminate_backend(pid)&lt;/code&gt; will immediately release locks.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Tradeoff:&lt;/em&gt; The terminated application transaction will fail and must be retried.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Manual &lt;code&gt;VACUUM ANALYZE&lt;/code&gt; (Medium Speed, High I/O):&lt;/strong&gt;
If a table has massive bloat and stale stats, forcing a manual vacuum updates the planner.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Tradeoff:&lt;/em&gt; This generates significant disk I/O and can degrade performance further while it runs.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Tuning &lt;code&gt;autovacuum_vacuum_scale_factor&lt;/code&gt; (Slow, Permanent Fix):&lt;/strong&gt;
If large tables are never being vacuumed, lower the scale factor for those specific tables using &lt;code&gt;ALTER TABLE ... SET (autovacuum_vacuum_scale_factor = 0.01)&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Tradeoff:&lt;/em&gt; Requires understanding the write velocity of the specific table to tune correctly.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;If you execute a manual &lt;code&gt;VACUUM FULL&lt;/code&gt; attempting to reclaim disk space, remember that it takes an &lt;code&gt;AccessExclusiveLock&lt;/code&gt; on the entire table. If this blocks production traffic unexpectedly, the rollback plan is to immediately cancel the &lt;code&gt;VACUUM FULL&lt;/code&gt; command. PostgreSQL will safely release the lock and revert to the previous state, though no space will have been reclaimed.&lt;/p&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;Deploy an agent or cron job that explicitly alerts on “Transactions older than 1 hour” and “Idle in transaction older than 15 minutes.” These are almost always application bugs (leaked connections) and they are the primary cause of autovacuum failing to clean up dead tuples.&lt;/p&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Vacuum is a Feature, Not a Chore:&lt;/strong&gt; Do not disable or restrict autovacuum. If it is consuming too much I/O, tune it to run more frequently but less aggressively.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Alert on the Right Metrics:&lt;/strong&gt; Stop alerting purely on CPU. Alert on replication lag, connection saturation, and long-running locks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Monitor Query Plans:&lt;/strong&gt; Use &lt;code&gt;pg_stat_statements&lt;/code&gt; to track the average execution time of your top queries to catch regressions before they cause outages.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; PostgreSQL’s most dangerous failures — bloat spirals, lock cascades, replication desync — are invisible on CPU and memory dashboards until the database is already deeply unhealthy. By the time CPU spikes from bloat, the table has been unvacuumed long enough to cause query plan regressions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Add lock chain detection, dead tuple ratio, replication byte lag, and long transaction age as continuously scraped metrics alongside host metrics — these are the leading indicators CPU can never provide.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Introduce a sleeping &lt;code&gt;idle in transaction&lt;/code&gt; connection in staging and verify it appears on the “Transactions older than 15 minutes” alert before it blocks a schema migration — if the alert doesn’t fire, the monitoring gap is real.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Add &lt;code&gt;lock_timeout = &apos;5s&apos;&lt;/code&gt; to all schema migration scripts this sprint, and create a Grafana panel tracking &lt;code&gt;n_dead_tup / (n_live_tup + n_dead_tup)&lt;/code&gt; per table to catch bloat before it affects query plans.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>Event-Driven Architecture Review: Schema Evolution, Ordering, Replay, and Dead Letters</title><link>https://rajivonai.com/blog/2024-08-13-event-driven-architecture-review-schema-evolution-ordering-replay-and-dead-letters/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-08-13-event-driven-architecture-review-schema-evolution-ordering-replay-and-dead-letters/</guid><description>The four failure boundaries in event-driven systems: schema evolution contracts, ordering guarantees, consumer replay safety, and dead-letter queue handling.</description><pubDate>Tue, 13 Aug 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Events do not make a system resilient by themselves; they move the failure boundary from synchronous calls into contracts, queues, consumers, and time.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most teams adopt event-driven architecture for good reasons. Services can publish state changes without knowing every downstream consumer. Slow integrations can run asynchronously. New products can subscribe to existing facts instead of requesting new point-to-point APIs. Cloud platforms make the starting point deceptively simple: create a topic, emit JSON, add consumers, and scale workers horizontally.&lt;/p&gt;
&lt;p&gt;The architecture works while event volume is small, schemas are stable, and consumers process messages near real time. The real test arrives later. A producer changes a field. A consumer needs to rebuild a projection from last month. A payment event arrives before the account event it references. One malformed message is retried thousands of times and blocks useful work behind it.&lt;/p&gt;
&lt;p&gt;At that point, the design question is no longer “Should we use events?” It is “What operational contract keeps event-driven systems recoverable when change, delay, and bad data are normal?”&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The common failure is treating an event bus as a transport layer instead of a durable integration boundary. Transport thinking asks whether a message can be delivered. Architecture thinking asks whether a message can be understood, ordered, replayed, ignored, repaired, or retired without corrupting downstream state.&lt;/p&gt;
&lt;p&gt;Four failure modes dominate production reviews.&lt;/p&gt;
&lt;p&gt;First, schema evolution breaks consumers silently. JSON makes it easy to add fields, rename fields, widen meanings, or change nullability without a compiler noticing. The producer deploys cleanly; the consumer fails later under traffic.&lt;/p&gt;
&lt;p&gt;Second, ordering is often assumed globally but provided locally. Kafka, for example, provides ordering within a partition, not across an entire topic. If two events for the same aggregate land in different partitions, consumers can observe impossible histories.&lt;/p&gt;
&lt;p&gt;Third, replay is confused with retry. Retry handles temporary failure. Replay rebuilds state from historical events. A consumer that is safe to retry once may not be safe to replay over six months of data.&lt;/p&gt;
&lt;p&gt;Fourth, dead letters become a junk drawer. Teams add a dead letter queue after the first incident, but without classification, ownership, retention, and redrive rules, it becomes an unbounded evidence pile.&lt;/p&gt;
&lt;p&gt;The core question: how should an event-driven system define contracts for schema evolution, ordering, replay, and dead letters before the first major recovery event?&lt;/p&gt;
&lt;h2 id=&quot;the-operating-contract&quot;&gt;The Operating Contract&lt;/h2&gt;
&lt;p&gt;A durable event architecture needs a control plane around the message flow. The broker moves events. The control plane governs whether those events are valid, how they are partitioned, how they are replayed, and what happens when they cannot be processed.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[producer — domain event] --&gt; B[schema gate — compatibility check]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[event log — durable topic]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[ordered partition — aggregate key]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[consumer — idempotent handler]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[projection — derived state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; G[dead letter queue — classified failure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; H[replay runner — bounded rebuild]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; I[repair workflow — owner and redrive]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first rule is that events are facts, not commands. “InvoiceIssued” is safer than “SendInvoiceEmail” because the latter encodes one consumer’s desired action. Facts age better because multiple consumers can interpret them independently.&lt;/p&gt;
&lt;p&gt;The second rule is that every event has an envelope. The envelope should include event name, schema version, event id, aggregate id, producer, occurred time, published time, trace id, and idempotency key. The payload carries domain data. Consumers should be able to make routing, ordering, deduplication, and observability decisions from the envelope before parsing business fields.&lt;/p&gt;
&lt;p&gt;The third rule is schema compatibility at publication time. A schema registry or equivalent validation step should prevent incompatible producer changes from reaching the log. Backward-compatible changes include adding optional fields and preserving existing meanings. Breaking changes include renaming required fields, changing semantic meaning, or removing fields still consumed downstream.&lt;/p&gt;
&lt;p&gt;The fourth rule is partition by the thing that needs ordered history. If account lifecycle events must be processed in order, the partition key is account id. If order matters per shopping cart, use cart id. Do not partition by convenience fields such as region or event type unless those are the real ordering boundary.&lt;/p&gt;
&lt;p&gt;The fifth rule is replay must be designed as a first-class operation. Replays need bounded windows, explicit target consumers, rate limits, idempotent writes, and visibility into side effects. A replay should rebuild projections or repair missed processing; it should not resend customer emails, re-charge cards, or call external systems unless explicitly operating in a side-effecting repair mode.&lt;/p&gt;
&lt;p&gt;The sixth rule is dead letters need taxonomy. A dead letter caused by invalid schema is different from one caused by missing reference data, timeout, permission failure, or a bug in consumer code. Each class needs an owner, alert threshold, retention period, and redrive policy.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;h3 id=&quot;context&quot;&gt;Context&lt;/h3&gt;
&lt;p&gt;The documented pattern across mature event systems is that guarantees are scoped. Apache Kafka documents ordering at the partition level, which means application designers must choose keys that align with the ordering domain. Confluent Schema Registry documents compatibility modes such as backward, forward, and full compatibility, making schema evolution a governance choice rather than an informal convention. AWS SQS documents dead letter queues as a way to isolate messages that cannot be processed successfully after repeated receives.&lt;/p&gt;
&lt;p&gt;These are not competing products so much as operating lessons: brokers provide primitives, not complete recovery semantics.&lt;/p&gt;
&lt;h3 id=&quot;action&quot;&gt;Action&lt;/h3&gt;
&lt;p&gt;A practical review should start with a contract matrix for each event family.&lt;/p&gt;
&lt;p&gt;For schema evolution, define the schema owner, compatibility mode, versioning policy, and consumer migration window. Require compatibility checks in CI and again at publish boundaries for high-risk producers.&lt;/p&gt;
&lt;p&gt;For ordering, document the aggregate that requires ordered processing and prove the partition key matches it. If workflows require cross-aggregate ordering, make that dependency explicit and consider a coordinator, saga, or database transaction instead of pretending the event bus gives global order.&lt;/p&gt;
&lt;p&gt;For replay, separate consumer code paths into pure projection updates and side-effecting actions. Projection handlers should be idempotent and replayable. Side-effecting handlers should persist a decision record before acting and should deduplicate by event id or business idempotency key.&lt;/p&gt;
&lt;p&gt;For dead letters, require structured failure metadata: exception class, consumer version, event id, schema version, retry count, first failure time, last failure time, and failure category. A dead letter queue without enough metadata is not recoverability; it is delayed debugging.&lt;/p&gt;
&lt;h3 id=&quot;result&quot;&gt;Result&lt;/h3&gt;
&lt;p&gt;The result is not that failures disappear. The result is that failure blast radius becomes bounded.&lt;/p&gt;
&lt;p&gt;A schema-breaking producer deployment is stopped before publication or isolated to a known version transition. A hot aggregate can still create pressure on one partition, but the ordering rule is visible and intentional. A replay can rebuild a search index without accidentally triggering external side effects. A dead letter spike can be routed to the owning team with enough context to decide whether to redrive, patch, suppress, or migrate.&lt;/p&gt;
&lt;h3 id=&quot;learning&quot;&gt;Learning&lt;/h3&gt;
&lt;p&gt;The learning is that event-driven architecture is less about decoupling services than decoupling failure handling. Producers and consumers are only truly decoupled when each side can evolve, pause, replay, and recover without asking the other side to guess what happened.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Architectural response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Schema drift&lt;/td&gt;&lt;td&gt;Producers change payloads faster than consumers migrate&lt;/td&gt;&lt;td&gt;Enforce compatibility checks and publish versioned event contracts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;False ordering assumptions&lt;/td&gt;&lt;td&gt;Teams assume topic order means business order&lt;/td&gt;&lt;td&gt;Partition by aggregate id and document the ordering boundary&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replay creates duplicate effects&lt;/td&gt;&lt;td&gt;Consumers mix projection writes with external actions&lt;/td&gt;&lt;td&gt;Make handlers idempotent and isolate side effects behind decision records&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Dead letters accumulate forever&lt;/td&gt;&lt;td&gt;Messages are isolated but not owned&lt;/td&gt;&lt;td&gt;Classify failures, assign owners, set retention, and define redrive rules&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backfills overwhelm live traffic&lt;/td&gt;&lt;td&gt;Replay competes with production processing&lt;/td&gt;&lt;td&gt;Use bounded replay windows, throttling, and separate consumer groups&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Event meanings decay&lt;/td&gt;&lt;td&gt;Old names no longer match business behavior&lt;/td&gt;&lt;td&gt;Treat event semantics as public APIs and deprecate intentionally&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Your event bus may deliver messages reliably while your system still cannot recover reliably.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Define an operating contract for schema evolution, ordering, replay, and dead letters around every critical event family.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Use broker-documented guarantees as constraints: Kafka ordering is partition-scoped, schema compatibility must be enforced deliberately, and dead letter queues only help when failures are classified and owned.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Pick one production event flow and review four artifacts this week: schema compatibility rules, partition key choice, replay procedure, and dead letter ownership.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Database Alert Design: Thresholds That Fire on Real Problems</title><link>https://rajivonai.com/blog/2024-08-12-database-alert-design-thresholds-that-fire-on-real-problems/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-08-12-database-alert-design-thresholds-that-fire-on-real-problems/</guid><description>How to set database alert thresholds that catch real failures without burning the team on autovacuum noise, checkpoint churn, and replication lag spikes — with specific values for PostgreSQL, MySQL, and Aurora.</description><pubDate>Mon, 12 Aug 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Most database alert fatigue comes from thresholds set to catch anything unusual rather than thresholds calibrated to actual user impact. An alert that fires on every autovacuum run, every checkpoint, and every 5-second replica lag spike will be silenced by engineers within a week — and then the real incidents will go unnoticed.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Database teams accumulate alerts in one of two ways: copy default thresholds from the monitoring tool’s out-of-box configuration, or set thresholds after an incident when the previous absence of an alert was painful. Both approaches produce the wrong result.&lt;/p&gt;
&lt;p&gt;Default thresholds are calibrated for visibility, not signal quality. They generate enough noise that teams learn to ignore them. Incident-driven thresholds overfit to a specific failure pattern and miss adjacent ones.&lt;/p&gt;
&lt;p&gt;The right design is a two-level alert architecture: a warning level that gives the team early signal and time to investigate, and a critical level that triggers paging because user impact is already occurring or imminent.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Symptom in the alert system&lt;/th&gt;&lt;th&gt;What it usually means&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Alert fired, no incident found&lt;/td&gt;&lt;td&gt;Threshold is at wrong level or condition is transient and self-resolving&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Alert fired after users already complained&lt;/td&gt;&lt;td&gt;Threshold is too high or measurement resolution is too low&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Same alert fires daily at the same time&lt;/td&gt;&lt;td&gt;Normal batch job or backup window — suppress or add time-based exclusion&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Alert never fires in production&lt;/td&gt;&lt;td&gt;Either system is very healthy, or threshold is too permissive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Multiple alerts fire at once for the same root cause&lt;/td&gt;&lt;td&gt;Missing alert correlation — downstream symptoms of a single root cause&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;p&gt;Before setting any threshold, measure the baseline over 7 days on the production workload.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. What is the normal replica lag distribution?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Collect &lt;code&gt;replay_lag&lt;/code&gt; from &lt;code&gt;pg_stat_replication&lt;/code&gt; (PostgreSQL) or &lt;code&gt;Seconds_Behind_Master&lt;/code&gt; (MySQL) every 60 seconds for 7 days. Identify:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Median lag during business hours&lt;/li&gt;
&lt;li&gt;95th percentile lag during peak write periods&lt;/li&gt;
&lt;li&gt;Maximum lag during known batch jobs or backups&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Set the warning threshold at 2× the 95th percentile peak. Set the critical threshold at the point where read replicas return data more than one commit cycle stale for your application’s consistency requirements — typically 60–120 seconds for OLTP, 5–15 minutes for analytics.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. What is the normal connection utilization pattern?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- PostgreSQL: connections used vs max&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; active,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; setting::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_settings &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;max_connections&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; max_conn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       ROUND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;             (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; setting::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_settings &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;max_connections&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pct_used&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Measure this every minute over 7 days. Alert at 70% (warning — time to investigate pool settings) and 85% (critical — application will soon see connection errors).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. What does checkpoint behavior look like during normal operations?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;From &lt;code&gt;pg_stat_bgwriter&lt;/code&gt;, collect &lt;code&gt;checkpoints_req&lt;/code&gt; over time. Zero is ideal — all checkpoints should be &lt;code&gt;checkpoints_timed&lt;/code&gt;. Any non-zero &lt;code&gt;checkpoints_req&lt;/code&gt; over a 5-minute period means write pressure is forcing early checkpoints. Alert when &lt;code&gt;checkpoints_req &gt; 0&lt;/code&gt; for more than 3 consecutive minutes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. What is the slow query baseline?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Enable &lt;code&gt;pg_stat_statements&lt;/code&gt; and measure the 95th percentile query duration for your top 20 query types over 7 days. Use this to set application-specific slow query thresholds — not a global “any query over 1 second” rule, which fires on legitimate analytical queries.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;5. What does disk growth look like?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Measure database disk usage daily for 30 days and compute the trend. Alert when the projected exhaustion date (at the current growth rate) falls within 14 days. This is a warning. A critical alert triggers when the projected exhaustion falls within 3 days or when a sudden disk spike exceeds the 30-day average growth by 5×.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Alert fires] --&gt; B{User impact?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Users already reporting issues| C[Critical — escalate to on-call]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No user reports| D{Trending toward impact?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Yes — within SLO window| E[Warning — investigate now]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|No — transient spike| F{Is this a known pattern?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|Yes — batch job, backup, maintenance| G[Suppress for this window — add schedule exclusion]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|No — unexpected| H[Investigate root cause — check pg_stat_activity and slow query log]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I{Root cause identified?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|Yes| J[Fix or tune threshold — document the baseline]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|No| K[Escalate with evidence package — query plans, metrics window, server log]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;alert-thresholds-reference&quot;&gt;Alert Thresholds Reference&lt;/h2&gt;
&lt;h3 id=&quot;postgresql&quot;&gt;PostgreSQL&lt;/h3&gt;

































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Metric&lt;/th&gt;&lt;th&gt;Warning&lt;/th&gt;&lt;th&gt;Critical&lt;/th&gt;&lt;th&gt;Notes&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Replica lag&lt;/td&gt;&lt;td&gt;60s&lt;/td&gt;&lt;td&gt;300s&lt;/td&gt;&lt;td&gt;Use &lt;code&gt;replay_lag&lt;/code&gt;; adjust for batch job windows&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Connection utilization&lt;/td&gt;&lt;td&gt;70% of &lt;code&gt;max_connections&lt;/code&gt;&lt;/td&gt;&lt;td&gt;85%&lt;/td&gt;&lt;td&gt;Count only non-idle sessions for more accurate signal&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;checkpoints_req&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&gt; 0 for 3 min&lt;/td&gt;&lt;td&gt;&gt; 0 for 10 min&lt;/td&gt;&lt;td&gt;Any forced checkpoint means write pressure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Dead tuple ratio&lt;/td&gt;&lt;td&gt;20% on tables &gt; 100k rows&lt;/td&gt;&lt;td&gt;40%&lt;/td&gt;&lt;td&gt;Per-table alert, not global&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cache hit ratio&lt;/td&gt;&lt;td&gt;&amp;#x3C; 97%&lt;/td&gt;&lt;td&gt;&amp;#x3C; 90%&lt;/td&gt;&lt;td&gt;Monitor &lt;code&gt;pg_statio_user_tables&lt;/code&gt; hits vs reads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Table bloat (relation size growth)&lt;/td&gt;&lt;td&gt;2× expected&lt;/td&gt;&lt;td&gt;3× expected&lt;/td&gt;&lt;td&gt;Compare against 30-day baseline&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Long-running query&lt;/td&gt;&lt;td&gt;&gt; 60s&lt;/td&gt;&lt;td&gt;&gt; 300s&lt;/td&gt;&lt;td&gt;OLTP threshold; analytical systems need separate policy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Idle-in-transaction session&lt;/td&gt;&lt;td&gt;&gt; 5 min&lt;/td&gt;&lt;td&gt;&gt; 15 min&lt;/td&gt;&lt;td&gt;Per-session duration, not aggregate count&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;pg_stat_replication&lt;/code&gt; slot lag&lt;/td&gt;&lt;td&gt;100 MB&lt;/td&gt;&lt;td&gt;1 GB&lt;/td&gt;&lt;td&gt;Unused replication slots block WAL cleanup&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;mysql--aurora-mysql&quot;&gt;MySQL / Aurora MySQL&lt;/h3&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Metric&lt;/th&gt;&lt;th&gt;Warning&lt;/th&gt;&lt;th&gt;Critical&lt;/th&gt;&lt;th&gt;Notes&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Seconds_Behind_Master&lt;/code&gt;&lt;/td&gt;&lt;td&gt;30s&lt;/td&gt;&lt;td&gt;120s&lt;/td&gt;&lt;td&gt;Use Aurora replica lag metric in CloudWatch for Aurora&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Threads_connected&lt;/code&gt;&lt;/td&gt;&lt;td&gt;70% of &lt;code&gt;max_connections&lt;/code&gt;&lt;/td&gt;&lt;td&gt;85%&lt;/td&gt;&lt;td&gt;&lt;code&gt;Threads_running&lt;/code&gt; spike is the lead indicator&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Innodb_buffer_pool_wait_free&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&gt; 0 per 5 min&lt;/td&gt;&lt;td&gt;&gt; 100 per 5 min&lt;/td&gt;&lt;td&gt;Buffer pool pages not available — memory pressure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Innodb_log_waits&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&gt; 0 per 5 min&lt;/td&gt;&lt;td&gt;&gt; 10 per 5 min&lt;/td&gt;&lt;td&gt;Redo log full — write throughput exceeded&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slow query rate&lt;/td&gt;&lt;td&gt;2× 7-day average&lt;/td&gt;&lt;td&gt;5× 7-day average&lt;/td&gt;&lt;td&gt;Rate, not absolute count&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Open_tables&lt;/code&gt;&lt;/td&gt;&lt;td&gt;80% of &lt;code&gt;table_open_cache&lt;/code&gt;&lt;/td&gt;&lt;td&gt;95%&lt;/td&gt;&lt;td&gt;Too-small cache causes repeated table opens&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lock wait timeout&lt;/td&gt;&lt;td&gt;&gt; 5 per minute&lt;/td&gt;&lt;td&gt;&gt; 20 per minute&lt;/td&gt;&lt;td&gt;High contention — check for hot rows or large transactions&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;aurora-postgresql--aurora-mysql-cloudwatch-specific&quot;&gt;Aurora PostgreSQL / Aurora MySQL (CloudWatch-specific)&lt;/h3&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;CloudWatch metric&lt;/th&gt;&lt;th&gt;Warning&lt;/th&gt;&lt;th&gt;Critical&lt;/th&gt;&lt;th&gt;Notes&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;ReplicaLag&lt;/code&gt;&lt;/td&gt;&lt;td&gt;30s&lt;/td&gt;&lt;td&gt;120s&lt;/td&gt;&lt;td&gt;Distinct from standard PostgreSQL; checked via CloudWatch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;DatabaseConnections&lt;/code&gt;&lt;/td&gt;&lt;td&gt;70% of instance max&lt;/td&gt;&lt;td&gt;85%&lt;/td&gt;&lt;td&gt;Per-instance limit, check RDS parameter group&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;FreeStorageSpace&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&amp;#x3C; 20 GB or &amp;#x3C; 20%&lt;/td&gt;&lt;td&gt;&amp;#x3C; 5 GB&lt;/td&gt;&lt;td&gt;Aurora storage auto-scales but billing changes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;AuroraVolumeBytesLeftTotal&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&amp;#x3C; 10 TB&lt;/td&gt;&lt;td&gt;&amp;#x3C; 1 TB&lt;/td&gt;&lt;td&gt;Aurora 128 TB storage ceiling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;WriteIOPS&lt;/code&gt;&lt;/td&gt;&lt;td&gt;2× 7-day P95&lt;/td&gt;&lt;td&gt;5× 7-day P95&lt;/td&gt;&lt;td&gt;Sudden IOPS spike — check for bulk loads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;EngineUptime&lt;/code&gt;&lt;/td&gt;&lt;td&gt;—&lt;/td&gt;&lt;td&gt;Unexpected reset&lt;/td&gt;&lt;td&gt;Unexpected restart — check for OOM or crash&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;If a threshold change causes alert fatigue or misses a real incident:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Revert to the previous threshold immediately and document the direction of failure (too sensitive vs. too permissive).&lt;/li&gt;
&lt;li&gt;Collect a 7-day baseline at the previous threshold before making another change.&lt;/li&gt;
&lt;li&gt;For critical alerts, always test in staging with a simulated failure scenario before applying to production.&lt;/li&gt;
&lt;li&gt;Keep a changelog of threshold changes with the justification and the measurement that motivated each change.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;Alert routing automation that reduces toil:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Batch job suppression&lt;/strong&gt;: automatically suppress replica lag alerts during known ETL windows (e.g., 01:00–04:00 UTC) and backup windows. Log the suppression, do not silently drop.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Alert correlation&lt;/strong&gt;: when connection exhaustion and slow query alerts fire within 5 minutes of each other, group them into a single incident with both signals attached. The root cause is almost always the same event.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Baseline drift detection&lt;/strong&gt;: weekly job that checks whether current metric values have permanently shifted from the thresholds set 30 days ago. If p95 is consistently higher than the warning threshold, the baseline has shifted — either the system is degrading or the workload grew.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;p&gt;Database alert reliability is a trust problem as much as a technical one. Teams stop responding to alerts that have false-positive rates above 20%. The two-level architecture (warning = investigate, critical = page) with calibrated per-metric thresholds keeps signal quality high enough that critical alerts are taken seriously. The measurement-first approach — setting thresholds from 7-day baselines rather than intuition — produces thresholds that reflect actual system behavior, not guesses.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Threshold set without baseline&lt;/td&gt;&lt;td&gt;Alert fires on normal workload variation&lt;/td&gt;&lt;td&gt;Measure 7-day baseline before setting any threshold&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Global slow query threshold&lt;/td&gt;&lt;td&gt;Legitimate analytics queries fire alert constantly&lt;/td&gt;&lt;td&gt;Per-query-class thresholds or separate analytics monitoring policy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Alert on every autovacuum&lt;/td&gt;&lt;td&gt;autovacuum is working correctly but noisy&lt;/td&gt;&lt;td&gt;Alert on dead tuple ratio, not autovacuum event frequency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Missing maintenance window suppression&lt;/td&gt;&lt;td&gt;Backup and ETL jobs generate false positives every night&lt;/td&gt;&lt;td&gt;Add time-of-day or scheduled suppressions with logging&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No test for false negatives&lt;/td&gt;&lt;td&gt;Team knows when alerts fire too much, but not when they miss&lt;/td&gt;&lt;td&gt;Simulate failure scenarios in staging quarterly to verify alert coverage&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Your database alerts either fire too often (ignored) or too late (users complain first).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Measure 7-day baselines for the five metric groups above, then set two-level thresholds (warning, critical) calibrated to those baselines.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Replay the last three database incidents against the proposed thresholds and verify they would have alerted at the warning level before user impact.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, pull 7 days of replica lag, connection utilization, and slow query data from your monitoring tool and set the two-level thresholds using the reference values above as a starting point.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>checklist</category></item><item><title>MySQL RDS 8.4 vs Amazon Aurora: The Write Scaling and Storage Tradeoff</title><link>https://rajivonai.com/blog/2024-08-10-mysql-rds-84-vs-aurora-architecture-write-scaling/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-08-10-mysql-rds-84-vs-aurora-architecture-write-scaling/</guid><description>An architectural comparison of Amazon RDS for MySQL 8.4 against Aurora MySQL, focusing on write path physics, EBS bottlenecks, and distributed storage IOPS.</description><pubDate>Sat, 10 Aug 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The release of MySQL 8.4 LTS shifts the baseline for managed database expectations, but upgrading a high-throughput system on AWS requires answering a fundamental architectural question: do you rely on RDS 8.4 with Provisioned IOPS EBS volumes, or move to Amazon Aurora’s distributed storage?&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Historically, the decision between Amazon RDS and Amazon Aurora came down to feature availability and replica lag. If you needed the absolute latest MySQL minor version or relied heavily on specific local storage engines, you stayed on RDS. If you needed sub-10 millisecond read replica lag and crash resilience, you moved to Aurora.&lt;/p&gt;
&lt;p&gt;With the stabilization of MySQL 8.4 LTS, AWS offers fully managed RDS 8.4 environments utilizing modern graviton instances and io2 Block Express EBS volumes capable of sub-millisecond latency. At the same time, Aurora has matured its I/O-Optimized tier and Serverless offerings. The boundary between “standard managed database” and “cloud-native distributed database” has blurred in marketing, but remains fiercely distinct in production execution.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;When a database workload scales from hundreds to tens of thousands of writes per second, the storage architecture dictates the failure mode.&lt;/p&gt;
&lt;p&gt;On Amazon RDS for MySQL 8.4, writes are fundamentally bound by the EBS volume queue, network limits of the EC2 instance, and the traditional InnoDB doublewrite buffer physics. Scaling writes means vertically scaling the instance and paying for massive io2 Provisioned IOPS.&lt;/p&gt;
&lt;p&gt;On Amazon Aurora, the InnoDB storage engine is torn out and replaced with a distributed, purpose-built storage fleet. The database instance only writes redo log records across the network to six storage nodes. However, because Aurora treats every replicated log record as an IOPS operation (unless using the I/O-Optimized tier), a heavy write workload can generate unexpected storage costs that eclipse the compute bill, and high commit frequencies can trigger network latency bottlenecks.&lt;/p&gt;
&lt;p&gt;The core question is: when designing for extreme write scaling, how does the physical storage architecture of RDS 8.4 compare to Aurora, and which bottlenecks are you willing to manage?&lt;/p&gt;
&lt;h2 id=&quot;rds-84-vs-aurora-write-architectures&quot;&gt;RDS 8.4 vs Aurora Write Architectures&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Application Write Workload] --&gt; B[Database Engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[RDS MySQL 8.4 — Monolithic Storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; D[Aurora MySQL — Distributed Storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; E[InnoDB Buffer Pool]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[Doublewrite Buffer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[EBS Volume — Network Attached]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[EBS IOPS and Throughput Limits]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; I[Redo Log Buffer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[Storage Fleet — 6 Nodes across 3 AZs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[Quorum 4 of 6 Acknowledgment]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; L[Asynchronous Page Materialization]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for Amazon RDS relies on the traditional MySQL write path. When a transaction commits in RDS 8.4, InnoDB must flush the redo log to the EBS volume. Background threads continually flush dirty pages from the buffer pool to the doublewrite buffer, and then to the tablespace. AWS’s io2 Block Express EBS volumes handle this exceptionally well, but the physical reality is that the EC2 instance must push entire 16KB pages across the network multiple times. If your application causes high page churn—such as updating large JSON columns or maintaining many secondary indexes—the EBS volume queue length will grow, and write latency will spike.&lt;/p&gt;
&lt;p&gt;Amazon Aurora’s architecture bypasses this entirely. As AWS documentation states, Aurora does not flush 16KB data pages from the database instance to storage over the network. It only transmits the redo log records. The distributed storage nodes handle the computational heavy lifting of applying those redo logs to data pages asynchronously. This drastically reduces the network bandwidth required per transaction and eliminates the doublewrite buffer penalty.&lt;/p&gt;
&lt;p&gt;However, Aurora’s quorum model means a commit must be acknowledged by 4 out of 6 storage nodes distributed across 3 Availability Zones. While AWS has optimized this path, if the workload involves thousands of tiny, single-row, unbatched commits per second, the sheer volume of network round trips can saturate the writer instance’s network thread — visible in Performance Schema as elevated redo-log-flush wait time, though I have not verified the exact current wait-event name against AWS’s documentation, so treat the symptom (network-thread saturation under micro-commit load) as the citable part, not any specific event identifier.&lt;/p&gt;
&lt;p&gt;Furthermore, Aurora’s standard billing model charges per I/O request. A workload that relies on frequent, small updates across many secondary indexes can cause Aurora storage costs to spiral, leading many engineering teams to blindly upgrade to the Aurora I/O-Optimized tier rather than fixing their application’s commit patterns. For the operational playbook on diagnosing and fixing that billing spiral once it’s already in production, see &lt;a href=&quot;https://rajivonai.com/blog/2025-06-20-aurora-mysql-io-billing-traps-metadata-locks/&quot;&gt;Surviving Aurora MySQL: I/O Billing Traps and Small-Instance Cliffs&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Architecture&lt;/th&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;RDS MySQL 8.4&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;EBS Volume Queue Length saturation during massive bulk inserts or heavy index updates.&lt;/td&gt;&lt;td&gt;Pre-provision higher io2 IOPS; batch inserts; scale instance size for higher EBS bandwidth limits.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Aurora MySQL Standard&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Astronomical I/O billing costs due to excessive redo log generation from poor indexing or frequent commits.&lt;/td&gt;&lt;td&gt;Switch to Aurora I/O-Optimized billing tier; remove unused secondary indexes; batch transactions.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;RDS MySQL 8.4&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Replica lag spikes during long-running write transactions due to single-threaded SQL application on replicas.&lt;/td&gt;&lt;td&gt;Upgrade to MySQL 8.4 multi-threaded replication; keep transactions short.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Aurora MySQL&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Writer network-thread saturation from redo-log-flush wait time during high-frequency micro-commits.&lt;/td&gt;&lt;td&gt;Increase transaction batch sizes at the application tier to group redo log flushes.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Scaling write-heavy workloads requires navigating the physical differences between EBS-backed traditional MySQL and Aurora’s distributed storage log-shipping architecture.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Select RDS 8.4 for predictable, storage-heavy workloads where I/O costs must be strictly controlled via provisioned EBS. Select Aurora for workloads demanding extreme crash recovery, sub-10ms replica lag, and compute offloading of I/O operations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; AWS documentation explicitly details the elimination of data page flushing in Aurora, and CloudWatch metrics (&lt;code&gt;VolumeQueueLength&lt;/code&gt; in RDS vs &lt;code&gt;VolumeWriteIOPs&lt;/code&gt; in Aurora) provide the empirical evidence of these architectural differences.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before upgrading to MySQL 8.4 LTS or migrating to Aurora, capture your peak &lt;code&gt;CommitLatency&lt;/code&gt; and write IOPS. If I/O charges make up a meaningful share of your Aurora bill, run the numbers against AWS’s pricing calculator to see whether the I/O-Optimized tier or application-level batching wins for your specific I/O-to-compute ratio — don’t apply a fixed percentage threshold across workloads.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category></item><item><title>Database Encryption: TDE, Column Encryption, pgcrypto, KMS</title><link>https://rajivonai.com/blog/2024-08-05-database-encryption-tde-column-pgcrypto-kms/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-08-05-database-encryption-tde-column-pgcrypto-kms/</guid><description>Why Transparent Data Encryption ticks compliance boxes but fails against compromised credentials, and how to push encryption boundaries up the stack.</description><pubDate>Mon, 05 Aug 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Transparent Data Encryption (TDE) is a compliance checkbox that protects against a stolen hard drive, but it offers zero protection against the actual threat: an attacker walking through the front door with a compromised database credential.&lt;/strong&gt; To genuinely secure sensitive data, engineering teams must shift cryptographic boundaries out of the storage engine and into the application layer, moving away from legacy patterns that trust the database process with the keys to the kingdom.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The regulatory definition of “encrypted at rest” is colliding with the reality of modern cloud security and zero-trust architectures. For decades, the industry standard was to turn on Transparent Data Encryption (TDE) at the database layer. TDE satisfies auditors—the data on the raw block storage device is mathematically inaccessible to someone who walks into an AWS data center and physically unplugs the hard drive.&lt;/p&gt;
&lt;p&gt;But physical theft is not the failure mode we are fighting in 2024. The threats we face are leaked application credentials in source code, Server-Side Request Forgery (SSRF) hitting internal database endpoints, and SQL injection vulnerabilities upstream. TDE operates seamlessly below the database engine’s shared memory buffers; it decrypts data automatically for any authenticated session. If an attacker has a valid credential, the database engine eagerly decrypts every row the attacker requests.&lt;/p&gt;




















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;Default approach&lt;/th&gt;&lt;th&gt;Better alternative&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Operating model&lt;/td&gt;&lt;td&gt;Turn on disk-level encryption (TDE) at the infrastructure layer, trusting the database process&lt;/td&gt;&lt;td&gt;Envelope encryption managed entirely by the application compute layer via a KMS&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failure mode&lt;/td&gt;&lt;td&gt;Data is completely accessible in plaintext if a valid database credential is leaked&lt;/td&gt;&lt;td&gt;Data remains ciphertext to the database; keys live in a disconnected control plane&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;When you rely on the database engine to handle encryption, you are explicitly deciding that the database process itself is the boundary of trust.&lt;/p&gt;
&lt;p&gt;This breaks down mechanically in two ways: disk-level (TDE) and column-level via database extensions (&lt;code&gt;pgcrypto&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The mechanics of TDE failure:&lt;/strong&gt; TDE encrypts database pages as they are flushed to disk and decrypts them as they are read into memory (like PostgreSQL’s &lt;code&gt;shared_buffers&lt;/code&gt; or MySQL’s &lt;code&gt;InnoDB Buffer Pool&lt;/code&gt;). The database process holds the encryption key in memory. From the perspective of the SQL execution engine, the data is always in plaintext. A leaked database credential bypasses TDE completely.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The mechanics of database extension failure:&lt;/strong&gt; To solve the TDE problem, teams often move to column-level encryption using database extensions like PostgreSQL’s &lt;code&gt;pgcrypto&lt;/code&gt;. They execute queries like:
&lt;code&gt;SELECT pgp_sym_encrypt(&apos;sensitive_value&apos;, &apos;my_secret_key&apos;);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This introduces a catastrophic operational vulnerability. The plaintext encryption key is passed directly across the wire in the SQL string. Unless you aggressively sanitize your telemetry, that plaintext key will instantly leak into:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;pg_stat_activity&lt;/code&gt; (visible to any monitoring agent)&lt;/li&gt;
&lt;li&gt;Slow query logs shipped to Datadog or CloudWatch&lt;/li&gt;
&lt;li&gt;Logical replication streams&lt;/li&gt;
&lt;li&gt;PostgreSQL’s internal statement history&lt;/li&gt;
&lt;/ol&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure point&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;TDE (Disk-level)&lt;/td&gt;&lt;td&gt;Database decrypts data automatically on disk reads&lt;/td&gt;&lt;td&gt;Offers zero defense against SQL injection, SSRF, or credential theft&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Database Extensions&lt;/td&gt;&lt;td&gt;Keys are passed as string literals in SQL queries&lt;/td&gt;&lt;td&gt;Keys leak across all database observability and replication pipelines&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application Encryption&lt;/td&gt;&lt;td&gt;The database engine loses visibility into the payload&lt;/td&gt;&lt;td&gt;Query patterns must be fundamentally redesigned to support exact-match searches&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The core architectural question is this: How do we completely decouple data access from data storage without destroying the database’s ability to efficiently serve queries?&lt;/p&gt;
&lt;h2 id=&quot;the-implementation&quot;&gt;The Implementation&lt;/h2&gt;
&lt;p&gt;The most resilient architecture shifts the cryptographic boundary out of the database entirely. The database is treated as a hostile, untrusted storage plane. The application layer handles all encryption using envelope encryption backed by a cloud Key Management Service (KMS), such as AWS KMS or Google Cloud KMS.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[&quot;Application Memory Pool&quot;] --&gt;|1. Request DEK| B[&quot;Cloud KMS API&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|2. Return Plaintext — Ciphertext| A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt;|3. Encrypt Payload locally| A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt;|4. Write Ciphertext| C[&quot;Database Storage Engine&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Request the Data Encryption Key (DEK).&lt;/strong&gt;&lt;br&gt;
The application compute layer calls the KMS API, requesting a new DEK for a specific record.&lt;br&gt;
Confirm: The KMS returns two versions of the DEK to the application: the raw plaintext DEK and a KMS-wrapped ciphertext version of the DEK.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Encrypt locally in the application pool.&lt;/strong&gt;&lt;br&gt;
The application uses a local cryptographic library (like AES-GCM-256) to encrypt the sensitive payload using the plaintext DEK.&lt;br&gt;
Confirm: The plaintext DEK is immediately discarded and zeroed out from the application’s memory pool. Only the ciphertext payload and the ciphertext DEK remain.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Write ciphertext to the hostile storage.&lt;/strong&gt;&lt;br&gt;
The application issues an &lt;code&gt;INSERT&lt;/code&gt; or &lt;code&gt;UPDATE&lt;/code&gt; to the database, writing both the encrypted payload and the ciphertext DEK into the row.&lt;br&gt;
Confirm: The database receives pure ciphertext. It cannot read the payload, and it cannot decrypt the DEK. The database is mathematically blind.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When reading the data back, the application fetches the row, sends the ciphertext DEK to the KMS to be unwrapped into plaintext, and then locally decrypts the payload.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern across mature platform architectures—especially those handling payments, healthcare records, or critical PII—is to enforce application-side envelope encryption over database-native cryptography.&lt;/p&gt;
&lt;p&gt;Context: When storing highly sensitive data points, standard operational posture assumes the database storage tier will eventually be compromised. A snapshot might be copied into a staging environment by a rogue script, or a read-replica credential might be exposed in a Slack channel.&lt;/p&gt;
&lt;p&gt;Action: Teams implement interceptors at the Object-Relational Mapping (ORM) layer or within a dedicated data access service. These interceptors automatically intercept writes to designated fields, execute the KMS envelope encryption flow, and replace the plaintext with the ciphertext bundle before the SQL statement is ever constructed.&lt;/p&gt;
&lt;p&gt;Result: When a read-replica is inadvertently exposed, the exfiltrated data is entirely useless. An attacker holding the database dump only holds ciphertext. To actually read the data, the attacker would need simultaneous, active access to the specific IAM roles allowed to call the KMS &lt;code&gt;Decrypt&lt;/code&gt; API—a completely isolated security plane with its own rate limits and audit trails.&lt;/p&gt;
&lt;p&gt;Learning: The database must be decoupled from the cryptographic control plane. Relying on the database to police access to its own underlying data is a topological anti-pattern.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Shifting the cryptographic boundary to the application layer introduces severe mechanical constraints on the database engine.&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Searchability&lt;/td&gt;&lt;td&gt;Executing &lt;code&gt;SELECT ... WHERE encrypted_column = &apos;value&apos;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Implement deterministic encryption for exact-match lookups, or build cryptographic blind indexes (e.g., HMAC-SHA256 of the plaintext)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Key Rotation&lt;/td&gt;&lt;td&gt;A KMS key needs to be rotated due to personnel exit&lt;/td&gt;&lt;td&gt;Build asynchronous background workers to iterate over tables, pull ciphertext, unwrap, rewrap with the new key, and write back&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Compute Overhead&lt;/td&gt;&lt;td&gt;The application calls KMS over the network for every row read&lt;/td&gt;&lt;td&gt;Cache the un-wrapped DEKs locally within the application memory space for a strict, short TTL (e.g., 5 minutes) to avoid KMS rate limits&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Database-level encryption features like TDE and &lt;code&gt;pgcrypto&lt;/code&gt; provide a false sense of security against the most common vectors of data exfiltration, leaving data vulnerable to compromised credentials and SQL injection.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Move the cryptographic boundary out of the database and up to the application compute layer using KMS envelope encryption.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: A leaked database credential or snapshot yields only ciphertext; an attacker must breach both the data plane and the IAM control plane simultaneously to extract value.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Audit your schema for sensitive columns currently relying on TDE or &lt;code&gt;pgcrypto&lt;/code&gt;. Identify one critical field and scope the engineering effort to migrate it behind an application-side KMS flow with a blind index.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The ultimate measure of a zero-trust data architecture is not whether the disk is encrypted, but how many entirely disparate systems an attacker must compromise at the exact same time to read a single row of plaintext.&lt;/p&gt;</content:encoded><category>databases</category><category>architecture</category><category>security</category></item><item><title>Database Migration Cutover Workflow: Dual Writes, CDC, Backfill, Freeze, and Rollback</title><link>https://rajivonai.com/blog/2024-07-29-database-migration-cutover-workflow-dual-writes-cdc-backfill-freeze-and-rollback/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-07-29-database-migration-cutover-workflow-dual-writes-cdc-backfill-freeze-and-rollback/</guid><description>Database migration cutover using dual writes, CDC, backfill, and freeze phases — with rollback boundaries for when &apos;almost synchronized&apos; is not an operational state.</description><pubDate>Mon, 29 Jul 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A database migration does not fail at the data copy step; it fails when the organization discovers that “almost synchronized” is not an operational state.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Teams migrate databases for good reasons: splitting a monolith, moving from self-managed infrastructure to managed cloud, changing storage engines, isolating high-growth domains, or replacing a schema that can no longer carry product behavior. The hard part is rarely the first export. The hard part is keeping the old and new systems correct while real traffic continues to mutate the source of truth.&lt;/p&gt;
&lt;p&gt;That creates a familiar migration timeline: capture the source log position to start CDC, backfill historical rows up to that position, stream changes through CDC to catch up, run dual writes for application-owned mutations, validate both sides, freeze writes, cut over traffic, and preserve a rollback path. Each step sounds independently reasonable. Together, they form a distributed system with ordering, idempotency, schema drift, replay, and ownership problems.&lt;/p&gt;
&lt;p&gt;The mistake is treating cutover as a deployment event. It is not. Cutover is the final state transition in a long-running data protocol.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Most migration failures come from ambiguous ownership. During the migration, which system owns a row? Which write path is authoritative? Which timestamp wins? What happens when the new database accepts a write but the old database times out? Can the team roll back after target-only writes begin?&lt;/p&gt;
&lt;p&gt;Dual writes are especially dangerous when they are framed as “write to both databases.” A correct dual-write path needs idempotency keys, retry semantics, deterministic mapping, observability, and a defined failure policy. Without those controls, the system can silently create divergence while all application requests return success.&lt;/p&gt;
&lt;p&gt;CDC has a different failure mode. It is good at preserving ordered change streams from a database log, but it does not magically repair bad transformations, missing DDL, incompatible constraints, or application writes that bypass the captured source. A backfill can load yesterday’s truth while CDC races to deliver today’s mutations. If validation only checks row counts, the migration may pass while balances, permissions, inventory, or workflow states are wrong.&lt;/p&gt;
&lt;p&gt;The core question is: how do you design a migration cutover so that every phase has one owner, one verification gate, and one rollback boundary?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;The safest pattern is to run the migration as a controlled state machine, not as a collection of scripts. Each phase should have explicit entry criteria, exit criteria, metrics, and rollback behavior.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[source database — current owner] --&gt; B[backfill worker — bounded chunks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A --&gt; C[CDC stream — ordered changes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; D[target database — candidate owner]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E[application — feature flags] --&gt; F[dual write adapter — idempotent operations]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; G[validation — counts checksums invariants]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; H{cutover gate — lag zero errors zero}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt;|not ready| I[rollback plan — source remains owner]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt;|ready| J[write freeze — drain queues]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  J --&gt; K[flip reads and writes — target owner]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  K --&gt; L[post cutover watch — repair or revert]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Start with ownership. Before cutover, the source database remains authoritative. The target is a candidate copy. The correct operational timeline begins by establishing the CDC stream and capturing the source log position before data moves. Once the log sequence number is secured, backfill moves historical state in bounded chunks up to that point so it can be paused, resumed, and re-run. Each chunk should record high-water marks, row counts, checksums where practical, and transformation versions.&lt;/p&gt;
&lt;p&gt;CDC then continuously carries the delta from the established start point. The stream should be monitored as a first-class dependency: replication lag, apply latency, failed records, retry queue depth, schema errors, and last committed source position. AWS Database Migration Service documents this as a full-load plus CDC pattern for minimizing downtime during migration, where ongoing changes are cached during the initial load and then replicated continuously (&lt;a href=&quot;https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Task.CDC.html&quot;&gt;AWS DMS CDC documentation&lt;/a&gt;, &lt;a href=&quot;https://docs.aws.amazon.com/prescriptive-guidance/latest/strategy-database-migration/cut-over.html&quot;&gt;AWS cutover guidance&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Dual writes should be introduced only after the transformation path is deterministic. The adapter should not be scattered through business logic. It should be a narrow write boundary with idempotency, structured error handling, and a kill switch. The old database remains the commit authority until the cutover gate. If the target write fails before cutover, the system can retry or enqueue repair because the source still owns truth. If the source write fails, the request fails.&lt;/p&gt;
&lt;p&gt;Validation must go beyond “the table loaded.” Use layered checks: row counts, sampled checksums, domain invariants, referential integrity, read comparison on production-shaped queries, and reconciliation of recent writes by source position. The most useful checks are business invariants: every paid invoice has ledger entries, every active entitlement maps to a customer, every order state has a valid transition history.&lt;/p&gt;
&lt;p&gt;The write freeze is the shortest phase, but it is the most important. Freeze application writes, drain queues, stop scheduled jobs that mutate data, wait for CDC lag to reach zero, record the final source log position, run final validation, then flip reads and writes. If the system cannot tolerate a global freeze, freeze the migrating domain behind routing, feature flags, or partition ownership.&lt;/p&gt;
&lt;p&gt;Rollback must be defined before the flip. Before target-only writes, rollback is simple: route traffic back to the source because the source remains authoritative. After target-only writes, rollback is no longer a switch; it is another migration. You either need reverse replication already proven, or you need to roll forward by repairing the target. Teams often say “we can roll back” when they only mean “we can redeploy the old application.” That is not database rollback.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS’s published migration guidance describes cutover strategies including offline migration, flash cutover, active-active configuration, and incremental migration. Its DMS model commonly combines full load with CDC so that ongoing changes are tracked from a specific log sequence number during the initial copy, followed by continuous replication until the cutover window (&lt;a href=&quot;https://docs.aws.amazon.com/prescriptive-guidance/latest/strategy-database-migration/cut-over.html&quot;&gt;AWS Prescriptive Guidance&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The documented pattern is to capture the log position first, separate the initial load from ongoing change capture, monitor replication progress, and choose a cutover strategy based on acceptable downtime and write behavior. For application teams, that means the migration plan should expose replication lag and failed apply operations as release gates, not background metrics.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The operational result is reduced downtime, but not zero responsibility. CDC narrows the freeze window; it does not remove the need for validation, schema compatibility, application quiescence, and a final ownership flip.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Treat CDC as a transport, not as correctness. Correctness comes from deterministic transformations, replayable writes, invariant checks, and a cutover gate that can say no.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; GitHub’s &lt;code&gt;gh-ost&lt;/code&gt; is a public example of a migration tool designed around online MySQL schema change. Its repository describes it as a triggerless online schema migration tool that uses the binary log and supports controlled cutover behavior (&lt;a href=&quot;https://github.com/github/gh-ost&quot;&gt;GitHub gh-ost&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The documented pattern is to create a shadow structure, stream changes from the database log, copy data incrementally while applying those changes concurrently, throttle work, and postpone the final cutover until the system is ready.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; That architecture makes the dangerous part explicit. The copy and catch-up phases can run while production continues, but the final rename or ownership switch is still a deliberate cutover step.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Online migration tools succeed because they isolate phases. They do not pretend the final switch is ordinary background work.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Shopify has publicly described moving toward log-based CDC for capturing changes from its sharded MySQL monolith, emphasizing immutable append-only change capture rather than query-based extraction (&lt;a href=&quot;https://shopify.engineering/capturing-every-change-shopify-sharded-monolith&quot;&gt;Shopify Engineering&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The documented pattern is to capture database changes from the log so downstream consumers can process a durable sequence of mutations.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; This supports more reliable propagation than periodically querying mutable tables, especially when many consumers need to react to changes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; A migration target should consume changes like a durable event stream where possible. Polling and ad hoc extracts are weaker foundations for cutover because they obscure ordering and missed updates.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Silent divergence&lt;/td&gt;&lt;td&gt;Dual writes succeed on one side and fail on the other&lt;/td&gt;&lt;td&gt;Idempotency keys, retry queues, reconciliation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;False validation confidence&lt;/td&gt;&lt;td&gt;Counts match but business state differs&lt;/td&gt;&lt;td&gt;Domain invariants and query comparison&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CDC lag hides cutover risk&lt;/td&gt;&lt;td&gt;Backfill load or schema errors slow apply&lt;/td&gt;&lt;td&gt;Lag SLOs and failed-record gates&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Rollback is fictional&lt;/td&gt;&lt;td&gt;Target accepts writes with no reverse path&lt;/td&gt;&lt;td&gt;Define rollback boundary before cutover&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Freeze misses writers&lt;/td&gt;&lt;td&gt;Jobs, queues, admin tools, or batch systems keep mutating source&lt;/td&gt;&lt;td&gt;Write inventory and freeze enforcement&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schema drift breaks apply&lt;/td&gt;&lt;td&gt;DDL changes during migration are not mirrored&lt;/td&gt;&lt;td&gt;Migration change freeze and schema contract&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replayed events corrupt state&lt;/td&gt;&lt;td&gt;Updates are not idempotent or ordering-aware&lt;/td&gt;&lt;td&gt;Source positions and deterministic merge rules&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The migration is not safe while ownership is ambiguous. Name the authoritative database for every phase and document when that changes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build the workflow around a correct timeline: capture log position, backfill, CDC catch-up, validation, freeze, cutover, and post-cutover monitoring. Keep dual writes behind one idempotent adapter.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Require gates for CDC lag, failed applies, invariant checks, sampled read comparison, queue drain, and final source log position. A cutover without these gates is a bet.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Write the rollback plan before writing the migration script. If rollback after target-only writes requires reverse replication, prove it before cutover. Otherwise call the plan what it is: roll forward with repair.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>MySQL and Aurora Monitoring: The Dashboard That Catches Problems Before Users Do</title><link>https://rajivonai.com/blog/2024-07-22-mysql-aurora-monitoring-dashboard-queries-replication-innodb/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-07-22-mysql-aurora-monitoring-dashboard-queries-replication-innodb/</guid><description>The seven MySQL and Aurora metric groups that matter for production operations — threads, replication lag, InnoDB buffer pool, slow queries, connections, locks, and disk — with exact SQL, CloudWatch metrics, and alert thresholds.</description><pubDate>Mon, 22 Jul 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A MySQL dashboard that shows only CPU and disk IOPS will miss the failures that actually page you at 3 AM: replication stopped because of a single bad row, InnoDB buffer pool thrashing on a cold restart, connection exhaustion from a leaked pool, and a lock chain building behind an ALTER TABLE that forgot &lt;code&gt;LOCK=NONE&lt;/code&gt;.&lt;/strong&gt; The metrics that matter come from &lt;code&gt;INFORMATION_SCHEMA&lt;/code&gt;, &lt;code&gt;performance_schema&lt;/code&gt;, and the MySQL status variables — not the OS.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most MySQL monitoring starts with the infrastructure layer: CPU, memory, disk I/O, network. These are necessary for capacity planning but insufficient for operational health. A MySQL instance with 30% CPU and plenty of free memory can still be moments from an outage: replica lag at 45 minutes, InnoDB buffer pool hit rate at 80% (normal is 99%), connection count at 95% of &lt;code&gt;max_connections&lt;/code&gt;, and five sessions blocked behind a lock on a hot row.&lt;/p&gt;
&lt;p&gt;Aurora adds its own layer: storage auto-scaling, volume bytes ceiling, cluster-level failover, and replica lag measured differently than MySQL’s &lt;code&gt;Seconds_Behind_Master&lt;/code&gt;. Monitoring Aurora with only MySQL queries misses the Aurora-specific failure modes.&lt;/p&gt;
&lt;p&gt;The seven metric groups below apply to both self-managed MySQL and Aurora MySQL. Where Aurora differs, the Aurora-specific metric or query is noted.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Likely source&lt;/th&gt;&lt;th&gt;First place to check&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Application queries suddenly slower&lt;/td&gt;&lt;td&gt;Lock contention or plan regression&lt;/td&gt;&lt;td&gt;&lt;code&gt;INFORMATION_SCHEMA.INNODB_TRX&lt;/code&gt;, &lt;code&gt;SHOW PROCESSLIST&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Connection pool exhausted&lt;/td&gt;&lt;td&gt;&lt;code&gt;max_connections&lt;/code&gt; hit or leaked connections&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW STATUS LIKE &apos;Threads_connected&apos;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica reads returning stale data&lt;/td&gt;&lt;td&gt;Replication lag&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW SLAVE STATUS&lt;/code&gt; / Aurora CloudWatch &lt;code&gt;ReplicaLag&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Table scan on a previously fast query&lt;/td&gt;&lt;td&gt;Missing index or stale stats&lt;/td&gt;&lt;td&gt;&lt;code&gt;EXPLAIN&lt;/code&gt;, &lt;code&gt;information_schema.STATISTICS&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Got error 1040: Too many connections&lt;/code&gt; in app logs&lt;/td&gt;&lt;td&gt;Connections near or at limit&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW VARIABLES LIKE &apos;max_connections&apos;&lt;/code&gt; vs current threads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Disk filling faster than expected&lt;/td&gt;&lt;td&gt;Binary logs not purging or large temp tables&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW VARIABLES LIKE &apos;expire_logs_days&apos;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OOM kill on MySQL process&lt;/td&gt;&lt;td&gt;Buffer pool too large for available RAM&lt;/td&gt;&lt;td&gt;&lt;code&gt;innodb_buffer_pool_size&lt;/code&gt; vs system RAM&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Lock wait timeout exceeded&lt;/code&gt; in app&lt;/td&gt;&lt;td&gt;Long-running transaction holding row locks&lt;/td&gt;&lt;td&gt;&lt;code&gt;INFORMATION_SCHEMA.INNODB_TRX&lt;/code&gt; + &lt;code&gt;INNODB_LOCKS&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;p&gt;Run these in order when something is wrong. Each requires only &lt;code&gt;PROCESS&lt;/code&gt; privilege or &lt;code&gt;SELECT&lt;/code&gt; on &lt;code&gt;performance_schema&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. What are active threads doing right now?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; id, user, host, db, command, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;LEFT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(info, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;120&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; query&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;PROCESSLIST&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; command &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;Sleep&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; time&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; time&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Look for threads in &lt;code&gt;Waiting for lock&lt;/code&gt;, &lt;code&gt;Sending data&lt;/code&gt;, or &lt;code&gt;Copying to tmp table&lt;/code&gt; with long durations. Any active query running more than 30 seconds in OLTP deserves investigation. &lt;code&gt;Waiting for lock&lt;/code&gt; with a chain of blocked sessions is a reliability event.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Is anyone waiting on InnoDB row locks?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  r&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; waiting_trx_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  r&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_mysql_thread_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; waiting_thread,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  r&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_query&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; waiting_query,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  b&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_trx_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  b&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_mysql_thread_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_thread,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  b&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_query&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_query,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  TIMESTAMPDIFF(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SECOND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_wait_started&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOW&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; wait_seconds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;INNODB_LOCK_WAITS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; w&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;INNODB_TRX&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; r&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; w&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;requesting_trx_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;INNODB_TRX&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; b &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; w&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;blocking_trx_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; wait_seconds &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For MySQL 8.0+, use &lt;code&gt;performance_schema.data_lock_waits&lt;/code&gt; instead of &lt;code&gt;INNODB_LOCK_WAITS&lt;/code&gt; (deprecated). A lock wait exceeding 10 seconds on an OLTP system is a reliability event, not a transient blip.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. How far behind is the replica?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- MySQL self-managed:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW SLAVE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;STATUS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;\G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Key fields: Seconds_Behind_Master, Slave_IO_Running, Slave_SQL_Running, Last_Error&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;Seconds_Behind_Master&lt;/code&gt; reports the difference between the timestamp of the last event the replica’s SQL thread applied and the current timestamp. It goes to &lt;code&gt;NULL&lt;/code&gt; when replication is stopped — this is not zero lag, it is broken replication.&lt;/p&gt;
&lt;p&gt;For Aurora MySQL: use CloudWatch metric &lt;code&gt;ReplicaLag&lt;/code&gt;. Aurora’s lag metric is more accurate because replicas share the same storage volume and lag is measured as I/O apply delay, not binary log position difference.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. What is the InnoDB buffer pool hit rate?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  variable_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  variable_value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; performance_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;global_status&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; variable_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Innodb_buffer_pool_read_requests&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Innodb_buffer_pool_reads&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Innodb_buffer_pool_wait_free&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Innodb_buffer_pool_pages_dirty&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;Innodb_buffer_pool_pages_total&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Compute hit rate: &lt;code&gt;(Innodb_buffer_pool_read_requests - Innodb_buffer_pool_reads) / Innodb_buffer_pool_read_requests * 100&lt;/code&gt;. Below 99% means the buffer pool is too small or the working set exceeds available memory. &lt;code&gt;Innodb_buffer_pool_wait_free &gt; 0&lt;/code&gt; means MySQL had to wait for a clean page — a sign of memory pressure.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;5. What does the slow query rate look like?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;STATUS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LIKE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;Slow_queries&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW VARIABLES &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIKE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;long_query_time&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW VARIABLES &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIKE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;slow_query_log%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If &lt;code&gt;slow_query_log&lt;/code&gt; is &lt;code&gt;OFF&lt;/code&gt;, turn it on: &lt;code&gt;SET GLOBAL slow_query_log = &apos;ON&apos;; SET GLOBAL long_query_time = 1;&lt;/code&gt; (1 second threshold for OLTP). &lt;code&gt;Slow_queries&lt;/code&gt; is a cumulative counter since last restart — track the rate of change, not the absolute value.&lt;/p&gt;
&lt;p&gt;For &lt;code&gt;performance_schema&lt;/code&gt;, query the top queries by total latency:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; schema_name, digest_text,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       count_star &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; executions,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       ROUND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(avg_timer_wait &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; 1e12, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; avg_latency_sec,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       ROUND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(sum_timer_wait &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; 1e12, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; total_latency_sec&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; performance_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;events_statements_summary_by_digest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; schema_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IS NOT NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sum_timer_wait &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Symptom observed] --&gt; B{Active threads check}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Long-running active queries| C[Run EXPLAIN — plan regression or missing index?]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Threads in lock wait| D[Find blocking transaction — INNODB_TRX]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Many Sleep threads| E[Check connection pool — leaked connections or idle timeout not set?]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|All looks normal| F{Check replication}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|Seconds_Behind_Master high or NULL| G[Check Slave_IO_Running and Slave_SQL_Running — IO stopped means network or binlog issue — SQL stopped means error on replica apply]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|Lag acceptable| H{Check InnoDB buffer pool}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|Hit rate below 99%| I[Working set exceeds buffer pool — increase innodb_buffer_pool_size or identify hot tables]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|wait_free above zero| J[Memory pressure — check OS swap and buffer pool size vs available RAM]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|Buffer pool healthy| K{Check slow queries}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|Slow query rate spiking| L[Run EXPLAIN on top queries from performance_schema digest — find index gaps]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|No slow query signal| M{Check connections}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    M --&gt;|Threads_connected near max_connections| N[Check for leaked connections — application not closing pool]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    M --&gt;|Connections healthy| O[Check InnoDB redo log waits and binary log position]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Problem&lt;/th&gt;&lt;th&gt;Immediate action&lt;/th&gt;&lt;th&gt;Durable fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Lock chain blocking transactions&lt;/td&gt;&lt;td&gt;&lt;code&gt;KILL &amp;#x3C;blocking_thread_id&gt;&lt;/code&gt; — use with caution, rolls back the transaction&lt;/td&gt;&lt;td&gt;Fix the application transaction that holds locks across slow external calls; add &lt;code&gt;innodb_lock_wait_timeout&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replication stopped — SQL thread error&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW SLAVE STATUS&lt;/code&gt; for &lt;code&gt;Last_SQL_Error&lt;/code&gt;; &lt;code&gt;STOP SLAVE; SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE;&lt;/code&gt; only if the row is truly safe to skip&lt;/td&gt;&lt;td&gt;Fix the root cause (schema drift, unsupported statement in ROW format); never skip without understanding the error&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;InnoDB buffer pool hit rate below 99%&lt;/td&gt;&lt;td&gt;Identify and cache the hot tables; check if a large dump or batch job is evicting the working set&lt;/td&gt;&lt;td&gt;Increase &lt;code&gt;innodb_buffer_pool_size&lt;/code&gt; (safe upper bound: 70–80% of total RAM); use buffer pool warmup after restart&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Connection exhaustion&lt;/td&gt;&lt;td&gt;Kill idle connections: &lt;code&gt;SELECT CONCAT(&apos;KILL &apos;, id, &apos;;&apos;) FROM information_schema.PROCESSLIST WHERE command=&apos;Sleep&apos; AND time &gt; 300;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Set &lt;code&gt;wait_timeout&lt;/code&gt; and &lt;code&gt;interactive_timeout&lt;/code&gt;; fix application connection pool to return connections after use&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slow query regression&lt;/td&gt;&lt;td&gt;Temporarily add an index with &lt;code&gt;CREATE INDEX ... ALGORITHM=INPLACE, LOCK=NONE&lt;/code&gt;; or force a plan with &lt;code&gt;FORCE INDEX&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Tune the query; rebuild statistics with &lt;code&gt;ANALYZE TABLE&lt;/code&gt;; add index permanently after testing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Disk filling from binary logs&lt;/td&gt;&lt;td&gt;&lt;code&gt;PURGE BINARY LOGS BEFORE DATE_SUB(NOW(), INTERVAL 3 DAY)&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Set &lt;code&gt;expire_logs_days = 7&lt;/code&gt;; verify replica is not lagging — purging logs a replica needs will break replication&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;Three MySQL checks can be automated into a runbook trigger:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Replication watchdog&lt;/strong&gt;: poll &lt;code&gt;Seconds_Behind_Master&lt;/code&gt; every 60 seconds; alert when it exceeds 60 seconds; alert as critical when it is &lt;code&gt;NULL&lt;/code&gt; (replication stopped). For Aurora, subscribe to CloudWatch &lt;code&gt;ReplicaLag&lt;/code&gt; metric and create the same two-level alarm.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Connection saturation check&lt;/strong&gt;: query &lt;code&gt;Threads_connected / max_connections&lt;/code&gt; every 60 seconds. Alert at 70%, page at 85%. This gives the team time to identify the source (pool leak, burst traffic, slow query cascade) before connection errors reach the application.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Long-running transaction watchdog&lt;/strong&gt;: query &lt;code&gt;INFORMATION_SCHEMA.INNODB_TRX&lt;/code&gt; every 60 seconds. Alert if any transaction has been running more than 5 minutes. Auto-terminate transactions running more than 30 minutes with a logged record. Long-running transactions block autovacuum analogs (purge thread), hold row locks, and inflate undo log.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;p&gt;MySQL health is not visible in CPU and disk IOPS alone. Replication lag, InnoDB buffer pool utilization, lock chains, and connection exhaustion are the failure modes that cause user-visible errors — and all of them are visible in MySQL status variables and &lt;code&gt;INFORMATION_SCHEMA&lt;/code&gt; before CPU shows any anomaly. The most common monitoring gap in MySQL deployments is treating &lt;code&gt;Seconds_Behind_Master = NULL&lt;/code&gt; as zero lag instead of broken replication, and setting a single global slow query threshold that fires on legitimate batch queries while missing OLTP regressions. The seven metric groups above require only a &lt;code&gt;PROCESS&lt;/code&gt; privilege and a 60-second poll interval.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Seconds_Behind_Master = NULL&lt;/code&gt; treated as healthy&lt;/td&gt;&lt;td&gt;NULL means replication stopped, not zero lag&lt;/td&gt;&lt;td&gt;Alert on &lt;code&gt;NULL&lt;/code&gt; as critical, not informational&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slow query alert fires on batch jobs&lt;/td&gt;&lt;td&gt;Global &lt;code&gt;long_query_time&lt;/code&gt; threshold applies to all queries&lt;/td&gt;&lt;td&gt;Set per-session &lt;code&gt;long_query_time&lt;/code&gt; for batch roles; alert on rate from &lt;code&gt;performance_schema&lt;/code&gt; digest by schema&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Buffer pool hit rate appears fine but queries are slow&lt;/td&gt;&lt;td&gt;A large report query is evicting the working set during the report window&lt;/td&gt;&lt;td&gt;Alert on hit rate averaged over 5 minutes; monitor &lt;code&gt;Innodb_buffer_pool_reads&lt;/code&gt; rate alongside hit rate&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lock wait queries not visible&lt;/td&gt;&lt;td&gt;&lt;code&gt;INNODB_LOCK_WAITS&lt;/code&gt; requires MySQL 5.6–5.7 syntax; MySQL 8.0 uses &lt;code&gt;performance_schema.data_lock_waits&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Upgrade monitoring queries for MySQL 8.0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aurora &lt;code&gt;Seconds_Behind_Master&lt;/code&gt; not available&lt;/td&gt;&lt;td&gt;Aurora replicas don’t expose this variable via &lt;code&gt;SHOW SLAVE STATUS&lt;/code&gt; in the same way&lt;/td&gt;&lt;td&gt;Use CloudWatch &lt;code&gt;ReplicaLag&lt;/code&gt; metric; do not rely on &lt;code&gt;SHOW SLAVE STATUS&lt;/code&gt; for Aurora replica lag&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;performance_schema&lt;/code&gt; disabled&lt;/td&gt;&lt;td&gt;Default enabled since MySQL 5.7 but can be disabled; digest table empty&lt;/td&gt;&lt;td&gt;Verify &lt;code&gt;performance_schema = ON&lt;/code&gt; in &lt;code&gt;my.cnf&lt;/code&gt;; enable &lt;code&gt;events_statements_history&lt;/code&gt; consumer&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; MySQL and Aurora monitoring shows infrastructure metrics but misses the database-level signals that precede outages.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Add the seven metric groups above using a &lt;code&gt;PROCESS&lt;/code&gt;-privileged monitoring user and a 60-second poll interval. For Aurora, add CloudWatch alarms for &lt;code&gt;ReplicaLag&lt;/code&gt;, &lt;code&gt;DatabaseConnections&lt;/code&gt;, and &lt;code&gt;FreeStorageSpace&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run the five checks above against your production instance right now and confirm replication is not &lt;code&gt;NULL&lt;/code&gt;, buffer pool hit rate is above 99%, and no thread has been blocked on a lock for more than 10 seconds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, create a monitoring role (&lt;code&gt;GRANT PROCESS, SELECT ON performance_schema.* TO &apos;monitoring&apos;@&apos;%&apos;&lt;/code&gt;), enable &lt;code&gt;slow_query_log&lt;/code&gt;, and set a replication lag alert with a 60-second warning threshold.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>checklist</category></item><item><title>Database Changes in CI/CD: Migrations, Backfills, Expand-Contract, and Verification</title><link>https://rajivonai.com/blog/2024-07-18-database-changes-in-ci-cd-migrations-backfills-expand-contract-and-verification/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-07-18-database-changes-in-ci-cd-migrations-backfills-expand-contract-and-verification/</guid><description>Database changes in CI/CD require separate gates for schema migrations, backfills, and expand-contract patterns — not just a shell command before deployment.</description><pubDate>Thu, 18 Jul 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A deployment pipeline that treats database change as a shell command is not automated; it is just moving the outage closer to production.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Application delivery has become routine. Every merge can build, test, package, scan, deploy, and roll back. The uncomfortable exception is the database. Schema changes are durable, shared, stateful, and often expensive. A bad application deploy can be rolled back by moving traffic to a previous artifact. A bad column drop, blocking index build, or half-completed backfill is a different class of failure.&lt;/p&gt;
&lt;p&gt;That is why database delivery needs its own release protocol inside CI/CD. Migrations are not just files in a repository. They are operations against a live, contended system with locks, replication lag, query plans, old application versions, new application versions, background workers, and human rollback expectations.&lt;/p&gt;
&lt;p&gt;Rails describes migrations as a way to evolve schema over time, but its own documentation also notes that not every database supports transactional DDL for every schema operation; when a migration fails, some completed parts may not be rolled back automatically.&lt;sup&gt;&lt;a href=&quot;#user-content-fn-rails-migrations&quot; id=&quot;user-content-fnref-rails-migrations&quot; data-footnote-ref=&quot;&quot; aria-describedby=&quot;footnote-label&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; That small detail is the heart of the problem. Database change is deployment, data repair, capacity management, and verification all at once.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Most teams begin with a simple rule: run migrations before deploy. That works until the migration is slow, incompatible, or logically coupled to code that is not fully rolled out.&lt;/p&gt;
&lt;p&gt;The common failure modes are predictable:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A deploy adds code that reads a column before the migration is complete.&lt;/li&gt;
&lt;li&gt;A migration drops a column still used by an older application instance.&lt;/li&gt;
&lt;li&gt;A backfill competes with production traffic and creates lock waits or replica lag.&lt;/li&gt;
&lt;li&gt;A new constraint validates existing dirty data and blocks the deploy.&lt;/li&gt;
&lt;li&gt;A rollback reverts application code but leaves the database in the new shape.&lt;/li&gt;
&lt;li&gt;CI proves the migration works on an empty test database but not on production-sized data.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The question is not whether database changes should be automated. They should. The question is what the pipeline must know before it is allowed to change shared state.&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;The safe pattern is expand, deploy, backfill, verify, contract. It turns a dangerous one-step migration into a sequence of compatible states.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[proposal — schema change request] --&gt; B[static checks — unsafe operation detection]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[expand migration — additive schema]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D[deploy code — dual read or dual write]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; E[backfill job — bounded batches]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; F[verification — counts constraints and query plans]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; G[contract migration — remove obsolete shape]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; H[post deploy audit — drift and health checks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt;|reject| X[manual review — lock risk or data risk]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt;|pause| Y[traffic protection — throttle or stop]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt;|fail| Z[remediation — repair data before contract]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first design rule is compatibility. Every production state must tolerate old code and new code running together. That means additive migrations first: add nullable columns, create tables, add indexes concurrently where the database supports it, and avoid immediate destructive changes.&lt;/p&gt;
&lt;p&gt;The second rule is separation. Schema migration and data migration are different operations. A schema migration changes shape. A backfill changes volume. Backfills belong in resumable, observable jobs, not inside a deploy transaction. They need batch size, sleep interval, retry policy, progress state, error quarantine, and an emergency stop.&lt;/p&gt;
&lt;p&gt;The third rule is verification as a gate, not a dashboard. The pipeline should not merely run &lt;code&gt;db:migrate&lt;/code&gt; and report success. It should ask whether the resulting database state is compatible with the next release step. That means verifying migration order, expected columns, indexes, constraints, row counts, null rates, duplicate keys, backfill completion, and query plan changes for critical paths.&lt;/p&gt;
&lt;p&gt;The fourth rule is delayed destruction. Contract migrations happen only after the system has proven that the old shape is unused. Dropping a column is not the rollback plan. It is the last step after telemetry, code search, deploy completion, and data verification say the old contract is gone.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; The documented pattern across mature systems is that schema change must be decoupled from ordinary deploy speed. GitLab documents post-deployment migrations for changes that should run after application code is deployed, and it separately documents batched background migrations for long-running data changes.&lt;sup&gt;&lt;a href=&quot;#user-content-fn-gitlab-post-deploy&quot; id=&quot;user-content-fnref-gitlab-post-deploy&quot; data-footnote-ref=&quot;&quot; aria-describedby=&quot;footnote-label&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;sup&gt;&lt;a href=&quot;#user-content-fn-gitlab-batched&quot; id=&quot;user-content-fnref-gitlab-batched&quot; data-footnote-ref=&quot;&quot; aria-describedby=&quot;footnote-label&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; That is not an exotic optimization. It is an acknowledgement that different database operations belong at different points in the release lifecycle.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The platform should encode those phases directly. A pull request that adds a column should pass static migration checks. A deploy should apply only migrations that are safe before code rollout. A post-deploy phase should run operations that depend on new code being present. A backfill worker should own data movement in controlled batches. A final contract migration should be blocked until verification proves the old path is no longer required.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The result is not zero risk. It is localized risk. A failed additive migration can block a deploy before incompatible code ships. A slow backfill can be paused without rolling back the application. A failed verification can stop the contract phase while production continues using the expanded schema. GitHub’s &lt;code&gt;gh-ost&lt;/code&gt; is an example of the same operational instinct for MySQL schema changes: online migration machinery exists because directly altering large production tables can couple migration workload to user-facing database load.&lt;sup&gt;&lt;a href=&quot;#user-content-fn-github-ghost-blog&quot; id=&quot;user-content-fnref-github-ghost-blog&quot; data-footnote-ref=&quot;&quot; aria-describedby=&quot;footnote-label&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;&lt;sup&gt;&lt;a href=&quot;#user-content-fn-github-ghost-repo&quot; id=&quot;user-content-fnref-github-ghost-repo&quot; data-footnote-ref=&quot;&quot; aria-describedby=&quot;footnote-label&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The important lesson is that database CI/CD should optimize for reversible application states, not reversible SQL files. Rollback is often a code movement back to a compatible version while the database remains expanded. The database should move forward through safe states, with destructive changes delayed until they are boring.&lt;/p&gt;
&lt;h3 id=&quot;the-pipeline-contract&quot;&gt;The Pipeline Contract&lt;/h3&gt;
&lt;p&gt;A serious database pipeline needs more than a migration runner.&lt;/p&gt;
&lt;p&gt;It needs a classifier. Additive operations can proceed automatically. Potentially blocking operations require review. Destructive operations require proof that they are in the contract phase. Data rewrites require a backfill plan.&lt;/p&gt;
&lt;p&gt;It needs production realism. CI should run migrations from both an empty database and a recent schema snapshot. The empty case catches ordering problems. The snapshot case catches drift, long-forgotten assumptions, and migrations that only work when no data exists.&lt;/p&gt;
&lt;p&gt;It needs policy checks. Examples include rejecting column drops outside a contract migration, requiring concurrent index creation where supported, blocking non-null constraints without a prior validation plan, and requiring idempotent backfill jobs with checkpoints.&lt;/p&gt;
&lt;p&gt;It needs observability. A backfill without progress is just a long-running incident with a friendlier name. Track rows scanned, rows changed, error rate, lock waits, deadlocks, replica lag, batch latency, and estimated completion. The deploy system should be able to pause the job automatically when the database is under stress.&lt;/p&gt;
&lt;p&gt;It needs explicit ownership. The author of a migration owns the full lifecycle: expand, application compatibility, backfill, verification, and contract. Platform automation can enforce the gates, but it cannot infer the business invariant. Only the owning team can say what “fully backfilled” or “safe to remove” means.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Migration passes CI but blocks production&lt;/td&gt;&lt;td&gt;Test data is too small and lock behavior is invisible&lt;/td&gt;&lt;td&gt;Run static checks, use realistic schema snapshots, require online patterns for large tables&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backfill overloads the primary&lt;/td&gt;&lt;td&gt;Data movement is deployed like code instead of operated like workload&lt;/td&gt;&lt;td&gt;Use bounded batches, throttling, checkpoints, and automatic pause conditions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Rollback expectation is false&lt;/td&gt;&lt;td&gt;Application rollback cannot undo destructive schema changes&lt;/td&gt;&lt;td&gt;Use expand-contract and keep old schema available through rollback windows&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Constraint validation fails late&lt;/td&gt;&lt;td&gt;Existing data violates the new invariant&lt;/td&gt;&lt;td&gt;Add constraints in stages, preflight violations, repair data before enforcement&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Contract happens too early&lt;/td&gt;&lt;td&gt;Old code path still exists in workers, scripts, or delayed jobs&lt;/td&gt;&lt;td&gt;Verify usage with telemetry, code search, deploy completion, and job drain checks&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Pipeline becomes too slow&lt;/td&gt;&lt;td&gt;Every change is treated as maximum risk&lt;/td&gt;&lt;td&gt;Classify operations and automate the safe path while escalating only risky changes&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Database changes fail differently than application changes because they mutate shared durable state.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Treat schema migration, code rollout, backfill, verification, and contract as separate CI/CD phases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Use documented patterns such as post-deployment migrations, batched background migrations, and online schema migration tools as evidence that mature systems separate risk by operation type.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Add pipeline gates for unsafe DDL, require resumable backfills, block destructive changes until verification passes, and make every database change declare its expand-contract plan.&lt;/li&gt;
&lt;/ul&gt;
&lt;section data-footnotes=&quot;&quot; class=&quot;footnotes&quot;&gt;&lt;h2 class=&quot;sr-only&quot; id=&quot;footnote-label&quot;&gt;Footnotes&lt;/h2&gt;
&lt;ol&gt;
&lt;li id=&quot;user-content-fn-rails-migrations&quot;&gt;
&lt;p&gt;&lt;a href=&quot;https://guides.rubyonrails.org/active_record_migrations.html&quot;&gt;Rails Guides — Active Record Migrations&lt;/a&gt; &lt;a href=&quot;#user-content-fnref-rails-migrations&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 1&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;user-content-fn-gitlab-post-deploy&quot;&gt;
&lt;p&gt;&lt;a href=&quot;https://docs.gitlab.com/development/database/post_deployment_migrations/&quot;&gt;GitLab Docs — Post-deployment migrations&lt;/a&gt; &lt;a href=&quot;#user-content-fnref-gitlab-post-deploy&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 2&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;user-content-fn-gitlab-batched&quot;&gt;
&lt;p&gt;&lt;a href=&quot;https://docs.gitlab.com/development/database/batched_background_migrations/&quot;&gt;GitLab Docs — Batched background migrations&lt;/a&gt; &lt;a href=&quot;#user-content-fnref-gitlab-batched&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 3&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;user-content-fn-github-ghost-blog&quot;&gt;
&lt;p&gt;&lt;a href=&quot;https://github.blog/2016-08-01-gh-ost-github-s-online-migration-tool-for-mysql/&quot;&gt;GitHub Blog — gh-ost: GitHub’s online schema migration tool for MySQL&lt;/a&gt; &lt;a href=&quot;#user-content-fnref-github-ghost-blog&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 4&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;user-content-fn-github-ghost-repo&quot;&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/github/gh-ost&quot;&gt;GitHub — gh-ost repository&lt;/a&gt; &lt;a href=&quot;#user-content-fnref-github-ghost-repo&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 5&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;</content:encoded><category>databases</category><category>architecture</category></item><item><title>CloudWatch Database Insights for Aurora and RDS: The New AWS Monitoring Center</title><link>https://rajivonai.com/blog/2024-07-17-cloudwatch-database-insights-aurora-rds/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-07-17-cloudwatch-database-insights-aurora-rds/</guid><description>How to use CloudWatch and Performance Insights to root-cause Aurora and RDS incidents without deploying third-party agents.</description><pubDate>Wed, 17 Jul 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;If you are still SSH-ing into a bastion host to run &lt;code&gt;top&lt;/code&gt; and &lt;code&gt;SHOW PROCESSLIST&lt;/code&gt; during an Aurora outage, you are ignoring the richest telemetry plane AWS provides.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Historically, monitoring a managed database like Amazon RDS or Aurora meant making a choice: rely on the sparse, high-level metrics provided by default CloudWatch, or install a third-party agent that required network access, credential management, and additional compute overhead.&lt;/p&gt;
&lt;p&gt;The industry standard has shifted. AWS has unified Performance Insights (PI), Enhanced Monitoring (EM), and CloudWatch into a central observability plane. For teams operating Aurora and RDS at scale, the native AWS monitoring stack now provides enough granularity to diagnose deadlocks, pinpoint bad query plans, and trace I/O saturation without ever leaving the AWS console or writing a custom exporter.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;
&lt;p&gt;Database failures in Aurora rarely look like hard crashes. They look like creeping degradation. The operational symptoms typically manifest as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The Phantom CPU Spike:&lt;/strong&gt; &lt;code&gt;CPUUtilization&lt;/code&gt; hits 99%, but &lt;code&gt;DatabaseConnections&lt;/code&gt; remains flat. The application feels sluggish.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The I/O Ceiling:&lt;/strong&gt; Queries that normally take 5ms suddenly take 500ms. The &lt;code&gt;ReadIOPS&lt;/code&gt; or &lt;code&gt;WriteIOPS&lt;/code&gt; metrics flatline at the exact provisioned limit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Connection Storm:&lt;/strong&gt; &lt;code&gt;DatabaseConnections&lt;/code&gt; spikes vertically, followed immediately by application-side 502 Bad Gateway errors as the connection pool queue fills up.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Silent Blocker:&lt;/strong&gt; Application latency increases, but &lt;code&gt;CPUUtilization&lt;/code&gt; is suspiciously low. Threads are waiting, not working.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;p&gt;When a paging alert fires for an Aurora or RDS instance, these are the first five checks an engineer should perform using native AWS tools:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Check &lt;code&gt;DBLoad&lt;/code&gt; in Performance Insights:&lt;/strong&gt;
This is the single most important metric. DBLoad measures the number of active sessions in the database engine. If DBLoad exceeds the number of vCPUs, the database is bottlenecked.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Review the &lt;code&gt;Wait Events&lt;/code&gt; Breakdown:&lt;/strong&gt;
Slice the DBLoad metric by &lt;code&gt;waits&lt;/code&gt;. Are sessions waiting on &lt;code&gt;CPU&lt;/code&gt; (working)? &lt;code&gt;io/table/sql/read&lt;/code&gt; (I/O bound)? Or &lt;code&gt;Lock&lt;/code&gt; (contention)?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Check &lt;code&gt;FreeableMemory&lt;/code&gt; and &lt;code&gt;SwapUsage&lt;/code&gt; (CloudWatch):&lt;/strong&gt;
If &lt;code&gt;FreeableMemory&lt;/code&gt; plunges near zero and &lt;code&gt;SwapUsage&lt;/code&gt; begins climbing, the instance is thrashing. This often precedes an Out Of Memory (OOM) crash.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Identify the Top SQL by Load (Performance Insights):&lt;/strong&gt;
Look at the “Top SQL” panel. Is the load caused by a single terrible query plan (one bar dominates), or an aggregate increase in all traffic?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Examine &lt;code&gt;CommitLatency&lt;/code&gt; and &lt;code&gt;Deadlocks&lt;/code&gt; (Aurora Specific):&lt;/strong&gt;
For Aurora PostgreSQL, check the &lt;code&gt;CommitLatency&lt;/code&gt; metric. If commit latency spikes while read IOPS are low, the storage volume might be experiencing multi-AZ replication delays.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;p&gt;When diagnosing an Aurora performance incident, diagnosing the wait event is the critical pivot point.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[DBLoad Exceeds vCPUs] --&gt; B{What is the Dominant Wait State?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|CPU| C[Check Top SQL by Load]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; C1{Is it a single query?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C1 --&gt;|Yes| C2[Missing Index or Bad Plan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C1 --&gt;|No| C3[Traffic Spike: Scale Up Instance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|I/O| D[Check IOPS Metrics]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; D1{Hitting Provisioned Limits?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D1 --&gt;|Yes| D2[Increase Provisioned IOPS or EBS Volume Size]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D1 --&gt;|No| D3[Check Buffer Cache Hit Ratio]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Locks| E[Check Blocking Sessions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; E1[Identify the Blocking PID]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E1 --&gt; E2[Kill Blocker or Refactor Transaction Scope]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;p&gt;Once the root cause is identified, you have a limited set of remediation paths.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Kill the Offending Query (Fastest, High Risk):&lt;/strong&gt;
If a single analytic query is holding an &lt;code&gt;AccessExclusiveLock&lt;/code&gt;, terminating the PID (&lt;code&gt;pg_terminate_backend&lt;/code&gt;) immediately restores service.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Tradeoff:&lt;/em&gt; The application must handle the failure gracefully. If it immediately retries the exact same bad query, the database will lock up again.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Vertical Scaling (Medium Speed, High Cost):&lt;/strong&gt;
Modifying the instance to a larger SKU provides more CPU and memory. For Aurora, this takes minutes.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Tradeoff:&lt;/em&gt; It requires a brief interruption of service (failover) and treats the symptom (lack of resources) rather than the disease (bad queries).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Deploy an Emergency Index (Slowest, Permanent Fix):&lt;/strong&gt;
If the Top SQL reveals a missing index causing a sequential scan, building the index &lt;code&gt;CONCURRENTLY&lt;/code&gt; resolves the CPU load.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Tradeoff:&lt;/em&gt; Building an index takes time and adds I/O pressure to an already struggling database.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;If a remediation action worsens the situation (e.g., terminating a session causes a massive rollback that spikes I/O), the immediate rollback plan must be well-defined:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Stop the application traffic at the load balancer to shed load.&lt;/li&gt;
&lt;li&gt;Wait for the database engine to finish its internal rollback procedures.&lt;/li&gt;
&lt;li&gt;Do not reboot the instance during an active transaction rollback, as it will simply restart the rollback process upon recovery.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;CloudWatch allows for automated remediation through Alarms and Systems Manager (SSM) Runbooks. For example, you can create a CloudWatch Alarm that triggers when &lt;code&gt;FreeableMemory&lt;/code&gt; drops below 10%. Instead of just paging an engineer, the alarm can trigger an AWS Lambda function that queries Performance Insights, identifies the session consuming the most memory, and automatically terminates it.&lt;/p&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Standardize on Performance Insights:&lt;/strong&gt; Do not rely purely on basic CloudWatch metrics. PI’s &lt;code&gt;DBLoad&lt;/code&gt; is the only metric that accurately reflects database saturation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tag Your Queries:&lt;/strong&gt; Mandate that application teams use SQL comments (e.g., &lt;code&gt;/* route=checkout, user=123 */&lt;/code&gt;) so that PI can group database load by application feature.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Alert on Saturation, Not Averages:&lt;/strong&gt; Set alarms on wait events and connection limits, not just 80% CPU utilization.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Engineers SSH into bastion hosts and run &lt;code&gt;SHOW PROCESSLIST&lt;/code&gt; during Aurora incidents because the default CloudWatch dashboard surfaces host saturation, not database saturation — &lt;code&gt;CPUUtilization&lt;/code&gt; at 40% tells you nothing about 500 sessions waiting on a lock.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Make &lt;code&gt;DBLoad&lt;/code&gt; sliced by wait event type the primary diagnostic signal in every Aurora incident — it’s the only metric that shows whether the database is blocked, I/O-bound, or genuinely CPU-saturated.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Simulate an I/O spike in staging and verify the corresponding CloudWatch alarm fires within 2 minutes with the wait event correctly identified — if the alarm fires on CPU and not &lt;code&gt;DBLoad&lt;/code&gt;, the triage workflow hasn’t improved.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Enable Performance Insights at 1-second granularity on all production Aurora clusters, add a &lt;code&gt;DBLoad &gt; vCPUs&lt;/code&gt; alarm with wait-event context, and require “Top SQL by Load” in the next database post-mortem.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category></item><item><title>Adding Workers Isn&apos;t Enough: How Citus Shard Rebalancing Actually Works</title><link>https://rajivonai.com/blog/2024-07-16-citus-shard-rebalancing-production/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-07-16-citus-shard-rebalancing-production/</guid><description>A production runbook for planning, executing, monitoring, stopping, and validating Citus shard movement without mistaking an online rebalance for a free operation.</description><pubDate>Tue, 16 Jul 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A new Citus worker is only empty capacity. It does not relieve a saturated worker until the cluster transfers real shard ownership—and that transfer competes with the workload it is supposed to protect.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;RetailCo’s order platform has already made the hard architectural decision described in the &lt;a href=&quot;https://rajivonai.com/blog/2024-05-21-scaling-postgresql-writes-citus-ec2-retailco/&quot;&gt;Citus on EC2 design&lt;/a&gt;: tenant-owned tables are distributed by &lt;code&gt;tenant_id&lt;/code&gt;, the checkout graph is colocated, and three worker primaries own the existing shards.&lt;/p&gt;
&lt;p&gt;The cluster is now approaching its operating limit. One worker has little storage headroom, checkout P99 rises during maintenance, and the next seasonal peak will exceed the current safety margin. RetailCo provisions &lt;code&gt;worker-d&lt;/code&gt; and registers it:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_add_node(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;worker-d.internal&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5432&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The command registers the node and copies reference tables. It does not relocate existing distributed shards. Citus documentation is explicit: old shards stay where they are unless they are redistributed, so adding a worker may not improve performance by itself: &lt;a href=&quot;https://docs.citusdata.com/en/v13.0/admin_guide/cluster_management.html#add-a-worker&quot;&gt;Citus cluster management&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App[RetailCo checkout traffic] --&gt; Coord[Citus coordinator]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coord --&gt; A[worker A — existing shards]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coord --&gt; B[worker B — existing shards]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coord --&gt; C[worker C — existing shards]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coord --&gt; D[worker D — reference data only]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A --&gt; Limit[storage and write pressure remains]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; Limit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; Limit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;RetailCo is a reference architecture, not a claimed customer deployment. Its 40-TB dataset, shard sizes, and traffic shape are scenario inputs. No movement duration, throughput improvement, or application-latency result in this article is presented as measured fact.&lt;/p&gt;
&lt;p&gt;Version matters. By this article’s March 31, 2026 cutoff, Citus 13.2 had added snapshot-based node addition and rebalancing performance improvements, while Citus 14.0 added PostgreSQL 18 compatibility. The public stable API manual labels itself 13.0.1, so every production runbook must validate function signatures and behavior against the installed Citus minor before execution: &lt;a href=&quot;https://www.citusdata.com/updates/v13-2/&quot;&gt;Citus 13.2 release notes&lt;/a&gt; and &lt;a href=&quot;https://www.citusdata.com/updates/v14-0/&quot;&gt;Citus 14.0 release notes&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;“Run the rebalancer” sounds like one administrative command. Operationally, it is an online data migration across independent PostgreSQL servers.&lt;/p&gt;
&lt;p&gt;For each selected shard group, the system must copy the existing state, keep up with concurrent changes, move the ownership boundary, update metadata, and remove the old placement. During that work, the source still serves application traffic while reading data for the copy. The target writes the new placement. Network paths carry the transfer. PostgreSQL logical replication carries concurrent changes. Standbys, WAL archives, monitoring, and backups all observe the additional work.&lt;/p&gt;
&lt;p&gt;The word &lt;em&gt;nonblocking&lt;/em&gt; therefore needs a precise interpretation. Citus Community Edition 11.0 and later supports nonblocking reads and writes during shard rebalancing. That means the application can continue using a shard during most of the movement. Citus still takes a brief write lock when it updates metadata and promotes the target placement: &lt;a href=&quot;https://docs.citusdata.com/en/v13.0/admin_guide/cluster_management.html#rebalance-shards-without-downtime&quot;&gt;Citus online rebalancing&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Nonblocking does not mean:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;zero additional I/O, CPU, WAL, or network traffic;&lt;/li&gt;
&lt;li&gt;zero latency change for the application;&lt;/li&gt;
&lt;li&gt;zero locks at cutover;&lt;/li&gt;
&lt;li&gt;automatic correction of a hot tenant;&lt;/li&gt;
&lt;li&gt;rollback of all completed moves when the job is stopped;&lt;/li&gt;
&lt;li&gt;permission to begin with almost no disk headroom.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The production question is not simply, “Can Citus move shards while writes continue?” It is:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Can RetailCo transfer enough ownership before capacity is exhausted, while keeping checkout latency, replication health, recoverability, and abort options inside explicit limits?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;treat-rebalancing-as-a-controlled-data-migration&quot;&gt;Treat Rebalancing as a Controlled Data Migration&lt;/h2&gt;
&lt;p&gt;The recommended approach is a plan, canary, observe, expand, and validate loop. Do not start with maximum parallelism and wait for graphs to turn red.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Baseline[capture workload and capacity baseline] --&gt; Add[add and validate target worker]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Add --&gt; Plan[generate and review movement plan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Plan --&gt; Canary[start with one task per node]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Canary --&gt; Observe[observe application and database limits]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Observe --&gt; Expand[increase parallelism only with headroom]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Observe --&gt; Stop[stop if an abort threshold is crossed]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Expand --&gt; Validate[validate placements and workload balance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Stop --&gt; Replan[inspect state and generate a new plan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;step-1--prove-the-new-worker-is-a-safe-destination&quot;&gt;Step 1 — prove the new worker is a safe destination&lt;/h3&gt;
&lt;p&gt;Before registering the worker, verify the same PostgreSQL major, Citus line, extensions, roles, TLS trust, collation assumptions, storage policy, kernel settings, monitoring, backup agent, and standby topology used by the existing fleet.&lt;/p&gt;
&lt;p&gt;After registration, check cluster metadata and all-node connectivity:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; nodeid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       nodename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       nodeport,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       isactive,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       noderole,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       shouldhaveshards&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_dist_node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; nodeid;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_check_cluster_node_health();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;citus_check_cluster_node_health()&lt;/code&gt; checks every node-to-node connection, not only coordinator-to-worker connectivity. That matters because Citus system operations can require workers to connect to one another: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/api_udf.html#citus-check-cluster-node-health&quot;&gt;Citus utility functions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Large reference tables can make &lt;code&gt;citus_add_node()&lt;/code&gt; slow because they are copied at activation. Citus permits deferring that copy with &lt;code&gt;citus.replicate_reference_tables_on_activate = off&lt;/code&gt;, but then reference data is copied when a distributed shard is first created or moved to the node. Deferral moves the cost; it does not remove it: &lt;a href=&quot;https://docs.citusdata.com/en/latest/develop/api_guc.html#citus-replicate-reference-tables-on-activate-boolean&quot;&gt;Citus configuration reference&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Do not call the target ready merely because it appears in &lt;code&gt;pg_dist_node&lt;/code&gt;. Prove that its standby is streaming, backups include it, alerts are enabled, storage throughput is provisioned, and the coordinator can reach the direct PostgreSQL endpoint. Citus documents that shard rebalancing bypasses pool information such as PgBouncer and uses direct connections: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/api_metadata.html#connection-pooling-credentials&quot;&gt;Citus metadata tables&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;step-2--inventory-the-movement-unit&quot;&gt;Step 2 — inventory the movement unit&lt;/h3&gt;
&lt;p&gt;Citus moves a selected shard together with the shards colocated with it. For RetailCo, moving an &lt;code&gt;orders&lt;/code&gt; shard can also move the matching &lt;code&gt;order_items&lt;/code&gt;, &lt;code&gt;payments&lt;/code&gt;, &lt;code&gt;customers&lt;/code&gt;, and &lt;code&gt;tenants&lt;/code&gt; shards. That is correct for transaction locality, but it means the operational unit can be much larger than one row in an &lt;code&gt;orders&lt;/code&gt;-only size report: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/api_udf.html#citus-move-shard-placement&quot;&gt;Citus shard movement&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Inspect placements and colocation IDs before planning:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; table_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       shardid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       colocation_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       nodename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       shard_size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_shards&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; nodename, colocation_id, shardid, table_name;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;citus_shards&lt;/code&gt; view reports table, shard, node, colocation group, and size. Aggregate the entire colocation group when estimating a move. Also identify tables that became implicitly colocated only because their distribution columns share a type and shard count. Current Citus documentation warns that unrelated implicit colocation can create unnecessary movement cascades: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/api_udf.html#create-distributed-table&quot;&gt;Citus &lt;code&gt;create_distributed_table&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Every table in the group must support the selected transfer mode. A forgotten audit table without a primary key or replica identity can block an otherwise well-designed online move.&lt;/p&gt;
&lt;h3 id=&quot;step-3--inspect-the-strategy-citus-will-actually-use&quot;&gt;Step 3 — inspect the strategy Citus will actually use&lt;/h3&gt;
&lt;p&gt;Current Citus installations ship with &lt;code&gt;by_shard_count&lt;/code&gt; and &lt;code&gt;by_disk_size&lt;/code&gt;. The current metadata documentation marks &lt;code&gt;by_disk_size&lt;/code&gt; as the default. It calculates cost from total relation size, including indexes and colocated shards, and uses a threshold to avoid movement for insignificant disk differences: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/api_metadata.html#rebalancer-strategy-table&quot;&gt;Citus rebalancer strategy table&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Never infer the active default from memory or a blog post. Query the cluster:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       default_strategy,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       default_threshold,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       minimum_threshold,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       improvement_threshold&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_dist_rebalance_strategy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The two built-in strategies answer different questions:&lt;/p&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Strategy&lt;/th&gt;&lt;th&gt;Cost model&lt;/th&gt;&lt;th&gt;Useful when&lt;/th&gt;&lt;th&gt;Blind spot&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;by_shard_count&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Each shard group has equal cost&lt;/td&gt;&lt;td&gt;Shards, traffic, and worker capacity are genuinely similar&lt;/td&gt;&lt;td&gt;Equal counts can hide large size skew&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;by_disk_size&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Total bytes including indexes and colocated shards&lt;/td&gt;&lt;td&gt;Storage balance is the primary constraint&lt;/td&gt;&lt;td&gt;Equal bytes do not imply equal CPU, locks, or traffic&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Custom strategy&lt;/td&gt;&lt;td&gt;Operator-defined shard cost, node capacity, and placement rules&lt;/td&gt;&lt;td&gt;Heterogeneous workers or measured traffic placement&lt;/td&gt;&lt;td&gt;More logic to test and govern&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;For RetailCo, &lt;code&gt;by_disk_size&lt;/code&gt; is the safer starting point because one worker is short on storage. It is not a traffic balancer. A small shard serving a dominant tenant can be hotter than a much larger historical shard. Citus exposes tenant and statement statistics that can inform a custom strategy, but a custom cost function should be tested against skew, reset windows, and missing statistics before it controls placement.&lt;/p&gt;
&lt;h3 id=&quot;step-4--generate-the-plan-before-moving-bytes&quot;&gt;Step 4 — generate the plan before moving bytes&lt;/h3&gt;
&lt;p&gt;Generate the exact source, destination, shard, and size list:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; table_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       shardid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       pg_size_pretty(shard_size) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; shard_size,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       sourcename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       sourceport,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       targetname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       targetport&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; get_rebalance_table_shards_plan(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    rebalance_strategy :&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;by_disk_size&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The documented planner can accept controls such as a relation, threshold, maximum move count, excluded shards, and drain-only mode. The plan is not a reservation. Citus warns that the plan produced now can differ from the later execution because cluster facts such as disk space can change between calls: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/api_udf.html#get-rebalance-table-shards-plan&quot;&gt;Citus rebalance plan&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Turn the output into an operator-reviewed movement ledger:&lt;/p&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Gate&lt;/th&gt;&lt;th&gt;Required evidence&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Destination capacity&lt;/td&gt;&lt;td&gt;Planned bytes plus index growth, WAL, temporary transfer state, autovacuum, and safety reserve fit&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Source capacity&lt;/td&gt;&lt;td&gt;Source can serve traffic and scan the selected shard group without crossing latency limits&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Network&lt;/td&gt;&lt;td&gt;Source-to-target transfer has measured headroom and does not starve replication or application paths&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica identity&lt;/td&gt;&lt;td&gt;Every updated or deleted table in the group has a supported replica identity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Standby health&lt;/td&gt;&lt;td&gt;Source and target standbys are caught up before the move&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Recovery&lt;/td&gt;&lt;td&gt;Backup and WAL retention cover the entire maintenance window&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application&lt;/td&gt;&lt;td&gt;Abort thresholds exist for error rate, P95, P99, timeout rate, and checkout throughput&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Operations&lt;/td&gt;&lt;td&gt;An owner can stop, inspect, replan, and communicate the change&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;For a rough lower bound—not a promise—estimate:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;copy-time floor = total bytes in the colocation group&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                  ÷ measured end-to-end copy throughput&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Actual elapsed time includes indexes, concurrent change catch-up, contention, cutover, retries, and sequential scheduling. A 1-TB shard has no credible duration estimate until the target environment has measured the full path under representative load.&lt;/p&gt;
&lt;h3 id=&quot;step-5--understand-the-transfer-modes&quot;&gt;Step 5 — understand the transfer modes&lt;/h3&gt;
&lt;p&gt;Citus exposes three shard transfer modes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;auto&lt;/code&gt; requires replica identity when logical replication is available and is the default;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;force_logical&lt;/code&gt; uses logical replication without replica identity, but concurrent &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; statements can fail;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;block_writes&lt;/code&gt; uses a cross-worker copy while blocking modifications for tables without a primary key or replica identity.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These behaviors are documented for &lt;code&gt;citus_move_shard_placement()&lt;/code&gt; and node draining, and all modes are available in Citus Community Edition from version 11.0: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/api_udf.html#citus-move-shard-placement&quot;&gt;Citus utility functions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For RetailCo’s checkout tables, &lt;code&gt;auto&lt;/code&gt; is the recommended production posture. The schema already includes tenant-scoped primary keys. &lt;code&gt;force_logical&lt;/code&gt; is not a harmless workaround: a payment or order update failing during movement is an application incident. &lt;code&gt;block_writes&lt;/code&gt; is appropriate only when the business can tolerate and bound the write pause for that shard group.&lt;/p&gt;
&lt;p&gt;The online path works like this:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Source[source shard group serves traffic] --&gt; Copy[initial copy to target]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Source --&gt; Changes[logical stream captures concurrent changes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Copy --&gt; Catchup[target applies accumulated changes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Changes --&gt; Catchup&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Catchup --&gt; Lock[brief write lock for cutover]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Lock --&gt; Metadata[Citus promotes target placement]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Metadata --&gt; Cleanup[source placement removed after success]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;PostgreSQL needs a replica identity to apply &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; changes to the subscriber. A primary key supplies it by default; a qualifying unique index can be selected explicitly. Citus advises against &lt;code&gt;REPLICA IDENTITY FULL&lt;/code&gt; because subscriber-side row lookup can become very expensive: &lt;a href=&quot;https://docs.citusdata.com/en/v13.0/admin_guide/cluster_management.html#how-it-works&quot;&gt;Citus replica identity guidance&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;step-6--start-conservatively-and-watch-the-right-signals&quot;&gt;Step 6 — start conservatively and watch the right signals&lt;/h3&gt;
&lt;p&gt;Start the background job only after saving the approved plan and baseline:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_rebalance_start(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    rebalance_strategy :&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;by_disk_size&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The function returns immediately. Monitor both the job summary and per-shard progress:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; job_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       description&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       started_at,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       finished_at,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       details&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_rebalance_status();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; table_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       shardid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       sourcename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       targetname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       progress,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       source_shard_size,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       target_shard_size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; get_rebalance_progress();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;citus_rebalance_status()&lt;/code&gt; is the clearer current job view; &lt;code&gt;get_rebalance_progress()&lt;/code&gt; exposes per-shard states and source and target sizes: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/api_udf.html#citus-rebalance-status&quot;&gt;Citus rebalance monitoring&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The default &lt;code&gt;citus.max_background_task_executors_per_node&lt;/code&gt; is 1. Raising it permits parallel moves but may also require changes to &lt;code&gt;citus.max_background_task_executors&lt;/code&gt; and PostgreSQL &lt;code&gt;max_worker_processes&lt;/code&gt;. Shards in the same colocation group still move sequentially, so more executors do not guarantee proportional speedup: &lt;a href=&quot;https://docs.citusdata.com/en/v13.0/admin_guide/cluster_management.html#parallel-rebalancing&quot;&gt;Citus parallel rebalancing&lt;/a&gt; and &lt;a href=&quot;https://docs.citusdata.com/en/latest/develop/api_guc.html#citus-max-background-task-executors-per-node-integer&quot;&gt;Citus configuration&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;RetailCo should begin at the default of one task per node. Increase to two only after a canary shows adequate application latency, storage latency, network, WAL, standby lag, and target headroom. Parallelism is a risk budget, not a performance setting to maximize.&lt;/p&gt;
&lt;p&gt;Monitor four layers together:&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Layer&lt;/th&gt;&lt;th&gt;Signals&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Application&lt;/td&gt;&lt;td&gt;checkout TPS, P50, P95, P99, timeouts, retries, failed orders and payments&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Coordinator&lt;/td&gt;&lt;td&gt;rebalance state, distributed activity, worker connections, locks, planning latency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Source and target&lt;/td&gt;&lt;td&gt;CPU, disk read and write latency, throughput, queue depth, free space, autovacuum, checkpoints&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replication and recovery&lt;/td&gt;&lt;td&gt;WAL generation, physical standby lag, logical catch-up, archive health, backup success&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The goal is not the fastest possible copy. The goal is the highest movement rate that preserves the application and recovery safety margins.&lt;/p&gt;
&lt;h3 id=&quot;step-7--stop-safely-then-inspect-rather-than-assume&quot;&gt;Step 7 — stop safely, then inspect rather than assume&lt;/h3&gt;
&lt;p&gt;If an abort threshold is crossed:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_rebalance_stop();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The API documents that this cancels a rebalance in progress. It does not document a cluster-wide undo of moves already completed. Citus also documents that the rebalancer processes placements one by one. The safe operational inference is therefore to inspect current placements and generate a fresh plan after a stop; do not assume the cluster returned to its initial layout.&lt;/p&gt;
&lt;p&gt;For an individual &lt;code&gt;citus_move_shard_placement()&lt;/code&gt; call, Citus states that failure leaves the source and target unchanged for that move. That guarantee should not be generalized into an all-job rollback: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/api_udf.html#citus-move-shard-placement&quot;&gt;Citus shard movement failure behavior&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;After a stop or failure:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;capture &lt;code&gt;citus_rebalance_status()&lt;/code&gt; and database logs before restarting anything;&lt;/li&gt;
&lt;li&gt;verify application errors and locks have recovered;&lt;/li&gt;
&lt;li&gt;inspect &lt;code&gt;citus_shards&lt;/code&gt; and &lt;code&gt;pg_dist_placement&lt;/code&gt; for the actual ownership state;&lt;/li&gt;
&lt;li&gt;check source and target disk space, standbys, WAL archives, and logical replication artifacts;&lt;/li&gt;
&lt;li&gt;generate a new plan from current state;&lt;/li&gt;
&lt;li&gt;resume only after the original trigger is understood.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Do not combine a worker failover, Citus version change, storage resize, and rebalance restart into one recovery action. Preserve enough evidence to identify which operation changed the system.&lt;/p&gt;
&lt;h3 id=&quot;step-8--validate-the-result-not-just-the-job-state&quot;&gt;Step 8 — validate the result, not just the job state&lt;/h3&gt;
&lt;p&gt;A completed background job is necessary evidence, not acceptance evidence.&lt;/p&gt;
&lt;p&gt;Validate placement and size by node:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; nodename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DISTINCT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; shardid) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; shard_count,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       pg_size_pretty(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;sum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(shard_size)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; reported_shard_bytes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_shards&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_table_type &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;distributed&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GROUP BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; nodename&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; nodename;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then validate what the rebalancer cannot prove:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;checkout latency and errors returned to baseline;&lt;/li&gt;
&lt;li&gt;the new worker receives tenant-local reads and writes;&lt;/li&gt;
&lt;li&gt;no worker or standby has unsafe storage, WAL, or lag growth;&lt;/li&gt;
&lt;li&gt;colocated tables remain colocated;&lt;/li&gt;
&lt;li&gt;the routing and constraint invariants still pass;&lt;/li&gt;
&lt;li&gt;backups and monitoring include the new topology;&lt;/li&gt;
&lt;li&gt;the next plan contains no unexpected moves.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Disk balance alone is not success. If one tenant still drives most writes on &lt;code&gt;worker-b&lt;/code&gt;, the cluster remains traffic-skewed even when every worker stores the same number of terabytes. Use &lt;code&gt;citus_stat_tenants&lt;/code&gt;, application traces, and per-worker database metrics to distinguish storage balance from workload balance.&lt;/p&gt;
&lt;h3 id=&quot;on-citus-132-and-later-consider-adding-a-clone-instead-of-copying-shards&quot;&gt;On Citus 13.2 and later, consider adding a clone instead of copying shards&lt;/h3&gt;
&lt;p&gt;Everything above assumes the only way to fill a new worker is to copy shard groups into it over the network while the source serves traffic. Citus 13.2 added a second path that changes the economics for exactly the situation in this article: a source worker that is short on storage and cannot comfortably absorb the read amplification of a large outbound copy.&lt;/p&gt;
&lt;p&gt;Snapshot-based node addition registers an existing PostgreSQL streaming replica of a busy worker as a &lt;em&gt;clone&lt;/em&gt;, promotes it to a full worker, and then splits the shard range between the original and the promotion — instead of copying rows between two live workers:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_add_clone_node(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;worker-b-replica.internal&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5432&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;                            &apos;worker-b.internal&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5432&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The clone is caught up by streaming replication, which RetailCo already operates for worker high availability, rather than by a foreground logical-replication copy competing with checkout. After promotion, Citus splits shards between the source worker and the newly promoted node, so the write-blocking window is bounded by the split rather than by the size of the data.&lt;/p&gt;
&lt;p&gt;This does not remove the discipline in this runbook. The clone still needs the storage, monitoring, backup, and standby validation of Step 1; the split is still a data-reorganization operation with abort thresholds; and promoting a clone consumes the standby that was providing that worker’s HA, so the redundancy plan has to say how and when a replacement standby is rebuilt. Verify function signatures and behavior against the installed minor before use — &lt;a href=&quot;https://www.citusdata.com/updates/v13-2/&quot;&gt;Citus 13.2 release notes&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Choose between the two paths on evidence:&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Situation&lt;/th&gt;&lt;th&gt;Preferred path&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Source has healthy storage and I/O headroom&lt;/td&gt;&lt;td&gt;&lt;code&gt;citus_rebalance_start()&lt;/code&gt; — simpler, no standby consumed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Source is storage-constrained or copy would breach latency gates&lt;/td&gt;&lt;td&gt;Clone promotion and split, if the Citus line supports it&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cluster is below the supported version&lt;/td&gt;&lt;td&gt;&lt;code&gt;citus_rebalance_start()&lt;/code&gt; with the canary and gate discipline above&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Redundancy cannot be reduced even briefly&lt;/td&gt;&lt;td&gt;Build a fresh standby first, or rebalance conventionally&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;draining-a-worker-is-the-same-problem-in-reverse&quot;&gt;Draining a worker is the same problem in reverse&lt;/h3&gt;
&lt;p&gt;Removing a worker requires moving every placement off it first. &lt;code&gt;citus_remove_node()&lt;/code&gt; rejects a node that still holds shard placements: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/api_udf.html#citus-remove-node&quot;&gt;Citus node removal&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For one worker, Citus provides &lt;code&gt;citus_drain_node()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_drain_node(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;worker-c.internal&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5432&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Verify no distributed shard placements remain first.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_remove_node(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;worker-c.internal&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5432&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For several workers, mark them as ineligible and run one drain-only plan so Citus can minimize unnecessary moves:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_set_node_property(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;worker-c.internal&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    5432&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;shouldhaveshards&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; get_rebalance_table_shards_plan(drain_only :&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; true);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_rebalance_start(drain_only :&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; true);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Mark[mark worker as not eligible for shards] --&gt; Plan[review drain-only plan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Plan --&gt; Move[move colocated shard groups]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Move --&gt; Verify[verify zero distributed placements]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Verify --&gt; Remove[remove node from Citus metadata]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Remove --&gt; Retire[retire standby storage and monitoring]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;shouldhaveshards = false&lt;/code&gt; prevents normal new placement and tells the rebalancer to move shards away. It does not itself move data: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/api_metadata.html#worker-node-table&quot;&gt;Citus worker node metadata&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Citus documents that &lt;code&gt;citus_add_node()&lt;/code&gt; registers a worker and copies reference tables, while existing distributed shards remain on their current workers.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Record a before-and-after placement report from &lt;code&gt;citus_shards&lt;/code&gt;, then add the target without starting a rebalance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The test demonstrates the exact separation between provisioning capacity and assigning existing ownership. If checkout throughput changes before shards move, investigate caching, routing, or test noise rather than crediting the empty worker.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Capacity exists only where the workload’s data lives.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Citus’s default current &lt;code&gt;by_disk_size&lt;/code&gt; strategy includes index and colocated-shard bytes but not application traffic.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Review the disk-size plan alongside tenant request count, CPU time, lock waits, and write rate per worker. Flag any planned move that improves bytes while leaving the dominant tenant on the same saturated worker.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The approval separates storage balance from workload balance and identifies hot-tenant isolation or a custom strategy as a different operation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; A balanced cost function is only as useful as the resource it models.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Citus’s online move uses logical replication and a brief write lock at cutover.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run a canary movement under a controlled checkout workload. Capture application P95 and P99, database locks, source and target I/O, network, WAL, physical standby lag, logical catch-up, and the cutover interval.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The lab produces an evidence-based maintenance envelope and abort thresholds for the next move. Until raw results exist, no duration or latency claim is approved.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; “Online” is a concurrency capability, not a service-level guarantee.&lt;/p&gt;
&lt;h3 id=&quot;proposed-rebalance-experiment&quot;&gt;Proposed rebalance experiment&lt;/h3&gt;
&lt;p&gt;The lab should compare four states with the same dataset, offered workload, durability settings, worker shapes, and client hosts:&lt;/p&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Run&lt;/th&gt;&lt;th&gt;Cluster action&lt;/th&gt;&lt;th&gt;Question&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;A&lt;/td&gt;&lt;td&gt;Three workers, no new node&lt;/td&gt;&lt;td&gt;What is the steady-state baseline?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;B&lt;/td&gt;&lt;td&gt;Add fourth worker, no rebalance&lt;/td&gt;&lt;td&gt;Does empty capacity change anything?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;C&lt;/td&gt;&lt;td&gt;Rebalance with one executor per node&lt;/td&gt;&lt;td&gt;What is the safe movement rate and application impact?&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;D&lt;/td&gt;&lt;td&gt;Rebalance with two, then four executors if gates pass&lt;/td&gt;&lt;td&gt;Where does parallelism move the bottleneck?&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Use a tenant-local checkout script and a deliberately hot-tenant mix. Capture:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;planned and actual shard-group bytes moved;&lt;/li&gt;
&lt;li&gt;elapsed time per move and for the whole job;&lt;/li&gt;
&lt;li&gt;application TPS, P50, P95, P99, timeouts, retries, and errors;&lt;/li&gt;
&lt;li&gt;coordinator and per-worker CPU, connections, locks, and memory;&lt;/li&gt;
&lt;li&gt;storage latency, IOPS, throughput, queue depth, and free space;&lt;/li&gt;
&lt;li&gt;network throughput and packet loss between every source and target;&lt;/li&gt;
&lt;li&gt;WAL rate, archive delay, physical standby lag, and logical catch-up;&lt;/li&gt;
&lt;li&gt;autovacuum and checkpoint activity;&lt;/li&gt;
&lt;li&gt;final bytes, shard groups, tenant traffic, and CPU per worker.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Inject failures one at a time:&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure&lt;/th&gt;&lt;th&gt;Required evidence&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Target PostgreSQL stops during copy&lt;/td&gt;&lt;td&gt;Job state, source integrity, target cleanup, replan procedure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Source PostgreSQL fails&lt;/td&gt;&lt;td&gt;Affected tenants, worker promotion, movement state, restart decision&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Coordinator restarts&lt;/td&gt;&lt;td&gt;Background-job state, application behavior, operator recovery steps&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Target disk approaches abort threshold&lt;/td&gt;&lt;td&gt;Stop latency, remaining temporary state, free-space recovery&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Standby lag crosses limit&lt;/td&gt;&lt;td&gt;Whether stopping the rebalance restores lag before WAL safety is threatened&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Checkout P99 crosses limit&lt;/td&gt;&lt;td&gt;Whether movement stops and latency returns to baseline&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Do not run every fault in one experiment. A combined failure may be useful later, but first isolate the behavior and recovery contract of each component.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;




























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Condition&lt;/th&gt;&lt;th&gt;Why rebalancing is unsafe or insufficient&lt;/th&gt;&lt;th&gt;Recommended action&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Source is already at emergency capacity&lt;/td&gt;&lt;td&gt;Copy work competes with the traffic that created the emergency&lt;/td&gt;&lt;td&gt;Add capacity earlier, shed noncritical work, or migrate a bounded canary&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Target has little temporary headroom&lt;/td&gt;&lt;td&gt;Copy, indexes, WAL, and normal growth can exhaust storage&lt;/td&gt;&lt;td&gt;Resize before activation and enforce a free-space abort threshold&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tables lack replica identity&lt;/td&gt;&lt;td&gt;Online &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; replication cannot identify rows safely&lt;/td&gt;&lt;td&gt;Add a supported key or schedule bounded blocking mode&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Shard groups are enormous&lt;/td&gt;&lt;td&gt;One movement unit takes too long and concentrates failure risk&lt;/td&gt;&lt;td&gt;Redesign shard count before the emergency; measure real transfer time&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reference tables are large&lt;/td&gt;&lt;td&gt;Node activation or first shard placement carries extra copy cost&lt;/td&gt;&lt;td&gt;Measure separately; defer only with an explicit later-copy plan&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Physical standbys already lag&lt;/td&gt;&lt;td&gt;Rebalance-related work reduces recovery margin&lt;/td&gt;&lt;td&gt;Restore replication health before moving data&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Equal disk hides hot traffic&lt;/td&gt;&lt;td&gt;&lt;code&gt;by_disk_size&lt;/code&gt; solves the wrong imbalance&lt;/td&gt;&lt;td&gt;Isolate a tenant, use measured custom cost, subdivide ownership, or decompose&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One tenant exceeds one worker&lt;/td&gt;&lt;td&gt;Moving the tenant changes location, not its write ceiling&lt;/td&gt;&lt;td&gt;Split the tenant by another stable key or give it a separate architecture&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Peak traffic is active&lt;/td&gt;&lt;td&gt;Application and movement contend when the least headroom exists&lt;/td&gt;&lt;td&gt;Rebalance before peak and retain an abort window&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Stop and resume are untested&lt;/td&gt;&lt;td&gt;Operators may mistake cancellation for rollback&lt;/td&gt;&lt;td&gt;Rehearse stop, inspect, replan, and resume in the lab&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;RetailCo should not start the production job if any publication or change ticket still says “nonblocking, so there is no impact.” That wording hides the very measurements the operation requires.&lt;/p&gt;
&lt;p&gt;It should also reject a plan that moves the largest shard first merely because it delivers the most bytes to the new worker. A better canary is a representative colocation group large enough to exercise the full logical-replication and cutover path but small enough to keep the first abort recoverable.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;For this RetailCo scenario, the recommendation is to add &lt;code&gt;worker-d&lt;/code&gt;, validate it completely, use the cluster’s verified &lt;code&gt;by_disk_size&lt;/code&gt; strategy, and begin with the default one background task executor per node. The first movement should be a representative canary colocation group outside peak traffic. Parallelism can increase only after application latency, source and target storage, network, WAL, standby lag, and recovery gates remain healthy.&lt;/p&gt;
&lt;p&gt;The conclusion is deliberately conditional:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Good fit:&lt;/strong&gt; many movable tenant-local shard groups, adequate source and target headroom, supported replica identity, healthy standbys, and operators who can stop and replan.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Poor fit:&lt;/strong&gt; emergency-low storage, giant movement units, missing replica identity, lagging recovery systems, or one hot tenant that already exceeds one worker.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Main advantage:&lt;/strong&gt; Citus can transfer colocated PostgreSQL ownership while most application reads and writes continue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Main limitation:&lt;/strong&gt; the rebalancer moves the units the schema created; it cannot make an oversized shard or indivisible hot tenant smaller.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Operational complexity:&lt;/strong&gt; high. Rebalancing is a distributed PostgreSQL migration involving application SLOs, metadata, direct node connections, logical replication, physical standbys, storage, WAL, network, and recovery.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Migration complexity:&lt;/strong&gt; proportional to real colocation-group size and change rate, not the number of SQL commands.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; a fourth worker adds empty capacity while RetailCo’s existing shards and write pressure remain on three workers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; inspect the full colocation movement unit, review the active strategy and plan, canary at one executor per node, monitor the application and recovery path, and replan from actual state after any stop.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Citus documentation shows that adding a node does not move old shards, online rebalance uses logical replication plus a brief cutover lock, &lt;code&gt;citus_rebalance_status()&lt;/code&gt; exposes the background job, and the current default disk strategy balances bytes rather than traffic. The proposed lab must measure the scenario-specific safety envelope.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; build the before-state placement report and abort-threshold sheet before calling &lt;code&gt;citus_rebalance_start()&lt;/code&gt;. If the cluster lacks enough headroom to survive one representative shard-group move, do not use production as the benchmark environment.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category></item><item><title>PostgreSQL Monitoring: The Dashboard That Surfaces Problems Before Users Do</title><link>https://rajivonai.com/blog/2024-07-08-postgresql-monitoring-dashboard-queries-connections-replication-vacuum/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-07-08-postgresql-monitoring-dashboard-queries-connections-replication-vacuum/</guid><description>The eight PostgreSQL metric groups that matter for production operations — queries, connections, replication lag, autovacuum, locks, cache pressure, checkpoint behavior, and bloat — with exact SQL and alert thresholds.</description><pubDate>Mon, 08 Jul 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A PostgreSQL dashboard that only shows CPU and memory is a late warning system. The database tells you about problems in its own catalog — in &lt;code&gt;pg_stat_activity&lt;/code&gt;, &lt;code&gt;pg_stat_statements&lt;/code&gt;, &lt;code&gt;pg_stat_replication&lt;/code&gt;, and &lt;code&gt;pg_stat_bgwriter&lt;/code&gt; — before they surface as user-visible errors. The question is whether you’re reading those catalogs before or after the incident page fires.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most PostgreSQL monitoring setups start with the OS metrics the infrastructure team already collects: CPU, memory, disk I/O, network. Those metrics are necessary but not sufficient. A database with 20% CPU and 60% memory can still be in deep trouble: connection pools exhausted, replica 45 minutes behind, autovacuum fighting bloat on the largest tables, and a lock chain building behind a slow migration.&lt;/p&gt;
&lt;p&gt;The eight PostgreSQL metric groups below come from the database itself. Most can be collected by any monitoring agent — Datadog, Prometheus + postgres_exporter, CloudWatch with Enhanced Monitoring, or direct queries from a read-only monitoring role.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Likely source&lt;/th&gt;&lt;th&gt;First catalog to check&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Application queries suddenly slower&lt;/td&gt;&lt;td&gt;Lock contention or bad plan&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_activity&lt;/code&gt;, &lt;code&gt;pg_locks&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Connection pool exhausted&lt;/td&gt;&lt;td&gt;Idle-in-transaction or max_connections hit&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_activity&lt;/code&gt; filtered by state&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica reads returning stale data&lt;/td&gt;&lt;td&gt;Replication lag&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_replication&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Table scan on a previously fast query&lt;/td&gt;&lt;td&gt;Bloat has made statistics stale&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_user_tables&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Checkpoint warnings in server log&lt;/td&gt;&lt;td&gt;bgwriter pressure&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_bgwriter&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application sees deadlock errors&lt;/td&gt;&lt;td&gt;Write contention on hot rows&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_locks&lt;/code&gt; + server log&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Disk filling faster than expected&lt;/td&gt;&lt;td&gt;Orphaned temp files or unarchived WAL&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_bgwriter&lt;/code&gt;, WAL directory&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OOM kill on the database server&lt;/td&gt;&lt;td&gt;Work_mem overrun from parallel queries&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_activity&lt;/code&gt; + &lt;code&gt;work_mem&lt;/code&gt; setting&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;p&gt;Run these in order when something is wrong. Each check requires only read access to system catalogs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. What are active sessions doing right now?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pid, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;query_start&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; duration,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       query, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, wait_event_type, wait_event, usename&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;idle&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; query_start &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; interval &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;5 seconds&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; duration &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Look for sessions in &lt;code&gt;idle in transaction&lt;/code&gt; (holding locks while waiting on an application) or &lt;code&gt;active&lt;/code&gt; with long durations. Any query running more than 30 seconds in OLTP deserves investigation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Is anyone waiting on locks?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocked&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocked_pid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       blocked&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocked_query,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       blocking&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_pid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       blocking&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_query,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       blocking&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;usename&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_user,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocked&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;query_start&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocked_duration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_locks blocked_locks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity blocked &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocked_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocked&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_locks blocking_locks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;     ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocking_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;transactionid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocked_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;transactionid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;     AND&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocking_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocked_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity blocking &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocking_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocking&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocked_locks&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;granted&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocked_duration &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A lock chain longer than 10 seconds is a reliability event, not a monitoring blip.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. How far behind is the replica?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- On the primary:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client_addr, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, sent_lsn, write_lsn, flush_lsn, replay_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       (sent_lsn &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; replay_lsn) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; replication_lag_bytes,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       write_lag, flush_lag, replay_lag&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_replication;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For seconds of lag: &lt;code&gt;pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) / 16384 * (wal_block_size / 16384)&lt;/code&gt; approximates byte lag. Many monitoring agents compute this directly. Alert at 60 seconds; page at 300 seconds for read-replica-dependent applications.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. Is autovacuum keeping up?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; relname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       n_dead_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       n_live_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       ROUND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_dead_tup::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; NULLIF&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_live_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_dead_tup, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dead_pct,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       last_autovacuum,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       last_autoanalyze&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_dead_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_dead_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Dead tuple ratio over 20% on a high-traffic table means autovacuum is behind. Tables not autovacuumed in 24 hours are candidates for bloat investigation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;5. What is checkpoint pressure?&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; checkpoints_timed, checkpoints_req,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       checkpoint_write_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; write_secs,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       checkpoint_sync_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sync_secs,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       buffers_checkpoint, buffers_clean, buffers_backend,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       buffers_alloc,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       stats_reset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_bgwriter;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;checkpoints_req&lt;/code&gt; above zero means PostgreSQL is forcing checkpoints faster than &lt;code&gt;checkpoint_completion_target&lt;/code&gt; can absorb. &lt;code&gt;buffers_backend&lt;/code&gt; above zero means application processes are doing work that &lt;code&gt;bgwriter&lt;/code&gt; should handle — a sign of write pressure.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Symptom observed] --&gt; B{Active sessions check}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Long-running active queries| C[Check pg_stat_statements — plan regression or new query?]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Idle in transaction sessions| D[Find the application holding transactions open]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Lock waits| E[Kill blocking session or escalate to application team]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|All looks normal| F{Check replication}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|Replica lag above threshold| G[Identify write pressure source — high-volume writes or bloated WAL archiving?]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|Lag acceptable| H{Check autovacuum}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|Dead tuples high| I[Manual VACUUM on table or increase autovacuum_vacuum_scale_factor]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|Autovacuum absent| J[Check autovacuum_max_workers and pg_stat_activity for autovacuum processes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|No autovacuum issues| K{Check checkpoint pressure}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|checkpoints_req high| L[Increase max_wal_size or spread write workload]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|buffers_backend high| M[Tune bgwriter_lru_maxpages or review write amplification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Problem&lt;/th&gt;&lt;th&gt;Immediate action&lt;/th&gt;&lt;th&gt;Durable fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Long-running idle-in-transaction&lt;/td&gt;&lt;td&gt;&lt;code&gt;SELECT pg_terminate_backend(pid)&lt;/code&gt; on sessions over threshold&lt;/td&gt;&lt;td&gt;Set &lt;code&gt;idle_in_transaction_session_timeout&lt;/code&gt; on the application role&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lock chain&lt;/td&gt;&lt;td&gt;Identify and terminate the root blocking session&lt;/td&gt;&lt;td&gt;Fix the application transaction that holds locks across slow external calls&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica lag&lt;/td&gt;&lt;td&gt;Check for write burst or long transaction on primary&lt;/td&gt;&lt;td&gt;Add streaming replication slot monitoring; tune &lt;code&gt;wal_level&lt;/code&gt; and replica apply workers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High dead tuples&lt;/td&gt;&lt;td&gt;&lt;code&gt;VACUUM (VERBOSE) tablename;&lt;/code&gt; directly&lt;/td&gt;&lt;td&gt;Lower &lt;code&gt;autovacuum_vacuum_scale_factor&lt;/code&gt; for high-traffic tables; increase &lt;code&gt;autovacuum_max_workers&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Checkpoint pressure&lt;/td&gt;&lt;td&gt;Increase &lt;code&gt;max_wal_size&lt;/code&gt; (default 1GB, common to set 4–16GB)&lt;/td&gt;&lt;td&gt;Review write amplification from bulk loads; separate OLAP workloads to replicas&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cache hit ratio below 95%&lt;/td&gt;&lt;td&gt;Review &lt;code&gt;shared_buffers&lt;/code&gt; sizing (target 25% of RAM, not more)&lt;/td&gt;&lt;td&gt;Identify tables with sequential scans using &lt;code&gt;pg_statio_user_tables&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;Three PostgreSQL checks can be automated into a runbook trigger:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Idle-in-transaction watchdog&lt;/strong&gt;: query &lt;code&gt;pg_stat_activity&lt;/code&gt; every 60 seconds; alert if any session has been &lt;code&gt;idle in transaction&lt;/code&gt; for more than 5 minutes. Auto-terminate sessions over 30 minutes with a logged record.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Replica lag SLO&lt;/strong&gt;: collect &lt;code&gt;pg_stat_replication.replay_lag&lt;/code&gt; as a gauge metric; alert at 60s, page at 5 minutes, trigger write traffic rerouting away from reader endpoint at 10 minutes.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Autovacuum health check&lt;/strong&gt;: daily scheduled query against &lt;code&gt;pg_stat_user_tables&lt;/code&gt;; flag tables where &lt;code&gt;last_autovacuum&lt;/code&gt; is null or more than 48 hours old AND &lt;code&gt;n_live_tup &gt; 100000&lt;/code&gt;. Output as a structured JSON payload to the operations channel.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;p&gt;PostgreSQL health is not visible in CPU and memory alone. The database catalogs tell you about lock chains, replica lag, bloat accumulation, and checkpoint pressure — all of which affect user-visible latency before CPU crosses 80%. The metrics above require a read-only monitoring role and a scrape interval of 60 seconds or less. The most common monitoring gap in PostgreSQL deployments is not the absence of metrics but the absence of thresholds: teams collect data without defining what “bad” looks like until they are in an incident trying to find historical baselines.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Alert on every autovacuum completion&lt;/td&gt;&lt;td&gt;autovacuum runs are logged as activity; thresholds not tuned to table size&lt;/td&gt;&lt;td&gt;Alert on dead tuple ratio, not autovacuum frequency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lock alert fires during schema migration&lt;/td&gt;&lt;td&gt;Intentional DDL lock causes alert storm&lt;/td&gt;&lt;td&gt;Suppress lock alerts during maintenance windows; use &lt;code&gt;lock_timeout&lt;/code&gt; on migrations&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica lag alert on writes&lt;/td&gt;&lt;td&gt;Single large write causes temporary lag; recovers in seconds&lt;/td&gt;&lt;td&gt;Use 60-second averages, not point-in-time values&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;pg_stat_statements&lt;/code&gt; not populated&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_statements&lt;/code&gt; not in &lt;code&gt;shared_preload_libraries&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Add to &lt;code&gt;shared_preload_libraries&lt;/code&gt;, restart, set &lt;code&gt;track_activity_query_size&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Monitoring role missing&lt;/td&gt;&lt;td&gt;Agent lacks read access to catalogs&lt;/td&gt;&lt;td&gt;Create a dedicated &lt;code&gt;monitoring&lt;/code&gt; role with &lt;code&gt;pg_monitor&lt;/code&gt; system role (PG 10+)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Timestamp drift on replicas&lt;/td&gt;&lt;td&gt;Lag reported in bytes, not seconds&lt;/td&gt;&lt;td&gt;Use &lt;code&gt;replay_lag&lt;/code&gt; column directly (PG 10+) or compute from LSN difference&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-this-post-does-not-cover&quot;&gt;What This Post Does Not Cover&lt;/h2&gt;
&lt;p&gt;This post covers catalog-level PostgreSQL monitoring from inside the database. It does not cover: Prometheus exporter configuration and recording rules (covered in the Prometheus and Grafana post in this series), CloudWatch Enhanced Monitoring for RDS/Aurora, PgBouncer pool metrics, or logical replication slot lag as a distinct monitoring dimension. Each of those has a dedicated post in this series.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; PostgreSQL is reporting problems through its catalogs, but your dashboard only shows OS-level metrics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Add the eight metric groups above to your monitoring stack using &lt;code&gt;pg_monitor&lt;/code&gt; role and a 60-second scrape interval.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run the five checks above against your production instance right now and note whether any sessions are idle-in-transaction, whether replicas are within SLO, and whether any table has a dead tuple ratio above 10%.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; This week, create a &lt;code&gt;monitoring&lt;/code&gt; role with &lt;code&gt;GRANT pg_monitor TO monitoring&lt;/code&gt;, add it to your Datadog, Prometheus, or CloudWatch configuration, and set a replica lag alert with a 60-second threshold.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>checklist</category></item><item><title>Search Index Drift Workflow: Rebuilds, Dual Writes, CDC, and User-Visible Staleness</title><link>https://rajivonai.com/blog/2024-06-14-search-index-drift-workflow-rebuilds-dual-writes-cdc-and-user-visible-staleness/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-06-14-search-index-drift-workflow-rebuilds-dual-writes-cdc-and-user-visible-staleness/</guid><description>Search index drift is a truth-management failure: when to rebuild vs. dual-write vs. CDC, and how to bound user-visible staleness.</description><pubDate>Fri, 14 Jun 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Search drift is not a search problem first. It is a truth-management problem that becomes visible through search.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most product systems keep their source of truth in a transactional database and serve discovery from a separate search index. The database is optimized for correctness, constraints, and writes. The index is optimized for ranking, tokenization, faceting, filtering, autocomplete, and latency.&lt;/p&gt;
&lt;p&gt;That split is normal. PostgreSQL, MySQL, DynamoDB, Spanner, or another system owns the canonical record. Elasticsearch, OpenSearch, Solr, Vespa, Algolia, or a custom retrieval layer owns the read path for search. Between them sits a workflow that turns database mutations into index mutations.&lt;/p&gt;
&lt;p&gt;The uncomfortable part is that the index is not merely a cache. Users treat search results as product truth. If a deleted document still appears, if a price update lags, if an access-control change is missing, or if a newly created object is absent, the failure is not described as “eventual consistency.” It is described as “the product is wrong.”&lt;/p&gt;
&lt;p&gt;Search index drift is the gap between canonical state and searchable state. Some drift is expected. Unbounded drift is an incident.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Teams usually discover drift after adopting one of three write patterns.&lt;/p&gt;
&lt;p&gt;The first is application dual write: the request handler writes the database and then writes the search index. This looks simple until partial failure appears. The database commit succeeds, the index write times out, the retry creates stale ordering, or the process crashes between operations. If the two systems cannot share a transaction boundary, the application has accepted a consistency gap.&lt;/p&gt;
&lt;p&gt;The second is asynchronous job indexing: writes enqueue work, and workers update the index later. This removes latency from the request path, but it creates a backlog system. Queue lag, poison messages, deploy bugs, and schema incompatibilities become search correctness risks.&lt;/p&gt;
&lt;p&gt;The third is periodic rebuild: the team periodically scans the database and recreates the index. Rebuilds are useful, but they are not a complete freshness strategy. A nightly rebuild can repair silent corruption, but it cannot provide minute-level correctness unless the product accepts a full day of visible staleness.&lt;/p&gt;
&lt;p&gt;The core question is not “which tool indexes fastest?” It is: how do we bound, observe, repair, and communicate the difference between source-of-truth state and search-visible state?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;The practical architecture combines four ideas: change capture, idempotent indexing, rebuildable indexes, and user-visible freshness controls.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[primary database — canonical records] --&gt; B[transaction log — ordered changes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[change capture workers — durable cursor]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D[index writer — idempotent updates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; E[active search index — user queries]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A --&gt; F[bulk rebuild job — full snapshot]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; G[shadow search index — validation target]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; H[index alias switch — controlled cutover]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; I[drift monitor — lag and mismatches]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; J[operator workflow — replay repair rebuild]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; K[user interface — freshness signals]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The database remains the only source of truth. Search documents carry source version metadata: record ID, updated timestamp, logical sequence number, schema version, and deletion marker. Index writes are idempotent, so replaying the same change is safe. Out-of-order writes are rejected when the incoming version is older than the indexed version.&lt;/p&gt;
&lt;p&gt;Change data capture is the preferred steady-state path because it follows committed database changes rather than application intent. The application writes the database once. A CDC pipeline reads the transaction log and updates the index. This does not eliminate drift, but it moves drift into a measurable workflow: cursor lag, event age, failure rate, dead-letter volume, and version mismatch count.&lt;/p&gt;
&lt;p&gt;Rebuilds remain mandatory. CDC preserves forward progress; rebuilds repair historical mistakes. A rebuild creates a shadow index from a consistent source snapshot, validates document counts and sampled records, warms query paths, then atomically moves an alias or routing pointer. The old index remains available for rollback until confidence is high.&lt;/p&gt;
&lt;p&gt;Dual writes are still useful in narrow places. For example, a product may write directly to search for low-risk preview experiences while CDC provides authoritative correction. But dual writes should not be the only correctness mechanism for objects where permissions, money, inventory, or deletion semantics matter.&lt;/p&gt;
&lt;p&gt;User-visible staleness must be designed deliberately. Some systems can show “results updated a few seconds ago.” Others need read-after-write behavior for the author of a change, even if global search is eventually consistent. That can be handled by merging canonical database reads for the user’s own recent writes, routing a specific object lookup to the database, or hiding search results whose indexed version is older than a known permission version.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Elasticsearch documents its &lt;code&gt;_reindex&lt;/code&gt; API and alias-based index management as operational mechanisms for copying documents into a new index and switching traffic through aliases. The documented pattern is that index structure changes and large repairs are handled by creating a new index, filling it, and moving the read alias rather than mutating every serving assumption in place.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Apply that pattern to search drift recovery. Treat every serving index as replaceable. Keep index mappings and analyzers versioned. Build a shadow index from the canonical store, compare counts and sampled documents, then switch the alias when validation passes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Rebuilds become a normal maintenance operation instead of a one-off incident script. The system can repair missed CDC events, analyzer mistakes, mapping errors, and accidental partial deletes without taking search offline.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Rebuildability is a correctness property. If the index cannot be recreated from truth, then the index has quietly become truth.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Debezium’s documented architecture captures database changes from transaction logs and emits ordered change events to downstream consumers. PostgreSQL logical decoding and MySQL binlog replication expose the same architectural principle: committed database changes can be read after the fact without placing a second write inside the application request path.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Use CDC as the default index mutation source. Persist consumer offsets. Make index writes idempotent. Store source versions in documents. Send failed records to a dead-letter workflow that can be replayed after the bug is fixed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The indexing path becomes observable as a pipeline rather than hidden inside application handlers. Operators can measure lag, pause consumers, replay records, and distinguish source write failures from projection failures.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; CDC does not make search strongly consistent. It makes inconsistency bounded, inspectable, and repairable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Amazon DynamoDB Streams documents an ordered stream of item-level modifications that can trigger downstream processing. The documented pattern is not specific to search: one durable primary write can fan out to derived views.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; For key-value or document stores, use the database’s change stream as the trigger for index projection. Preserve deletion events, because missing tombstones are one of the most common sources of user-visible drift.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The index can track creates, updates, and deletes from the same committed mutation source. Replays can reconstruct the projected state if the index writer is deterministic.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Deletes deserve first-class workflow design. A stale creation is annoying; a stale deletion can be a privacy, permission, or compliance failure.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Out-of-order updates&lt;/td&gt;&lt;td&gt;Retries and parallel workers race&lt;/td&gt;&lt;td&gt;Store source versions and reject older writes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Missing deletes&lt;/td&gt;&lt;td&gt;Tombstones expire before indexing catches up&lt;/td&gt;&lt;td&gt;Retain delete events long enough for replay and rebuild&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Rebuild cutover errors&lt;/td&gt;&lt;td&gt;Shadow index differs from serving assumptions&lt;/td&gt;&lt;td&gt;Use aliases, validation queries, and rollback windows&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CDC backlog&lt;/td&gt;&lt;td&gt;Consumer deploy, poison event, or downstream throttling&lt;/td&gt;&lt;td&gt;Alert on event age, not only queue depth&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Mapping drift&lt;/td&gt;&lt;td&gt;Application emits fields the index cannot parse&lt;/td&gt;&lt;td&gt;Version schemas and fail records into replayable quarantine&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Permission staleness&lt;/td&gt;&lt;td&gt;Search document carries old access metadata&lt;/td&gt;&lt;td&gt;Version authorization data or verify sensitive results against truth&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Silent corruption&lt;/td&gt;&lt;td&gt;Index accepts wrong but valid documents&lt;/td&gt;&lt;td&gt;Run sampled truth-versus-index audits continuously&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Search drift becomes dangerous when nobody can say how stale the index is. Define freshness SLOs by product surface, not by infrastructure component.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Use CDC for steady-state propagation, idempotent writers for replay, shadow rebuilds for repair, and alias cutovers for controlled replacement.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Instrument source version, indexed version, CDC cursor lag, oldest unprocessed event age, dead-letter count, rebuild validation count, and sampled mismatch rate.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Start with one high-value entity. Add version metadata to its search document, build a truth-versus-index audit, and write the runbook for replay, rebuild, and rollback before the next drift incident.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>pgcrypto vs KMS vs HSM: Decision Framework</title><link>https://rajivonai.com/blog/2024-06-10-pgcrypto-vs-kms-vs-hsm-decision-framework/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-06-10-pgcrypto-vs-kms-vs-hsm-decision-framework/</guid><description>Engineers often over-rotate to Hardware Security Modules (HSMs) for non-regulatory workloads or under-rotate to database extensions. How to map data classification to the right cryptographic tier.</description><pubDate>Mon, 10 Jun 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Engineers often over-rotate to Hardware Security Modules (HSMs) for non-regulatory workloads, destroying database performance, or they under-rotate to database-native extensions, critically compromising security.&lt;/strong&gt; Choosing the right cryptographic boundary is a foundational architectural decision, not a compliance checkbox to be rushed during an audit.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;When a system needs to encrypt data, engineering teams are faced with three vastly different cryptographic tiers: database-native extensions (like &lt;code&gt;pgcrypto&lt;/code&gt;), cloud-managed Key Management Services (like AWS KMS), and dedicated Hardware Security Modules (HSMs).&lt;/p&gt;




















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;Default approach&lt;/th&gt;&lt;th&gt;Better alternative&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Operating model&lt;/td&gt;&lt;td&gt;Pick one encryption tier and apply it to the entire database universally&lt;/td&gt;&lt;td&gt;Implement a tiered cryptographic framework based strictly on data classification levels&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failure mode&lt;/td&gt;&lt;td&gt;Crippled performance from over-encryption, or leaked keys from under-encryption&lt;/td&gt;&lt;td&gt;Optimal balance of sub-millisecond latencies and regulatory compliance&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A mismatch between the data classification level and the cryptographic tier results in catastrophic operational failures.&lt;/p&gt;
&lt;p&gt;If you use an HSM to encrypt every single row in a standard user table, the application will crumble under the weight of network and hardware latency. Conversely, if you use &lt;code&gt;pgcrypto&lt;/code&gt; to encrypt highly regulated financial PANs (Primary Account Numbers), you violate PCI-DSS compliance by exposing plaintext keys to the database engine.&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure point&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why it matters&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;pgcrypto&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Encryption keys are processed in the database engine&lt;/td&gt;&lt;td&gt;Keys leak into &lt;code&gt;pg_stat_activity&lt;/code&gt; and logs; inadequate for highly sensitive PII or PCI data&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cloud KMS&lt;/td&gt;&lt;td&gt;Network roundtrips to the cloud provider’s API for every operation&lt;/td&gt;&lt;td&gt;Can introduce unacceptable latency (5-20ms per call) if Data Encryption Keys (DEKs) are not cached&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;HSM&lt;/td&gt;&lt;td&gt;Dedicated hardware appliances have strict throughput limits&lt;/td&gt;&lt;td&gt;Exceeding throughput limits causes application-wide connection queuing and outages&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The core architectural question is this: How do we map data classification levels to the correct cryptographic boundary without crippling database throughput or violating compliance?&lt;/p&gt;
&lt;h2 id=&quot;comparison&quot;&gt;Comparison&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;pgcrypto (database extension)&lt;/th&gt;&lt;th&gt;Cloud KMS (envelope encryption)&lt;/th&gt;&lt;th&gt;HSM (hardware module)&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Key storage&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Database engine (accessible to SQL, logs, &lt;code&gt;pg_stat_activity&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;Cloud provider key store (outside database)&lt;/td&gt;&lt;td&gt;Tamper-proof hardware; key never exported&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Operation latency&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Sub-millisecond (in-process)&lt;/td&gt;&lt;td&gt;5–20ms per API call without DEK caching&lt;/td&gt;&lt;td&gt;1–50ms depending on HSM throughput tier&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Throughput ceiling&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Unlimited — in-process&lt;/td&gt;&lt;td&gt;High with DEK caching; rate-limited per account&lt;/td&gt;&lt;td&gt;Strict hardware limits; over-subscription causes queuing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Key rotation&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Manual — SQL function; application restart required&lt;/td&gt;&lt;td&gt;API-driven; transparent to database&lt;/td&gt;&lt;td&gt;HSM-managed; hardware-enforced rotation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Compliance&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Not sufficient for PCI-DSS, HIPAA for high-risk data&lt;/td&gt;&lt;td&gt;Acceptable for most regulatory PII requirements&lt;/td&gt;&lt;td&gt;Required for PCI-DSS PANs, FIPS 140-2 Level 3&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Operational cost&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Effectively free&lt;/td&gt;&lt;td&gt;Pay-per-API-call + key storage&lt;/td&gt;&lt;td&gt;Hardware rental or cloud CloudHSM ($1.50+/hr)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Use this for&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Development, low-risk operational data, at-rest encryption supplements&lt;/td&gt;&lt;td&gt;Critical PII: SSNs, emails, financial amounts&lt;/td&gt;&lt;td&gt;PCI PANs, cryptographic key generation, FIPS environments&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-implementation&quot;&gt;The Implementation&lt;/h2&gt;
&lt;p&gt;A resilient architecture maps the cryptographic tier directly to the risk profile of the data.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[&quot;Data Classification&quot;] --&gt; B{&quot;Is it PCI or highly regulated?&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes| C[&quot;HSM — Hardware Security Module&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No| D{&quot;Is it critical PII?&quot;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|Yes| E[&quot;Cloud KMS Envelope Encryption&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|No| F[&quot;TDE — Transparent Data Encryption&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Tier 1: TDE (Disk-Level Encryption)&lt;/strong&gt;&lt;br&gt;
Use TDE for low-risk, operational data.&lt;br&gt;
Confirm: The data is protected against physical drive theft, with zero application-layer latency overhead.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Tier 2: Cloud KMS (Envelope Encryption)&lt;/strong&gt;&lt;br&gt;
Use KMS for critical PII (emails, SSNs). The application fetches a Data Encryption Key (DEK), encrypts the payload locally, and caches the DEK.&lt;br&gt;
Confirm: The database never sees the plaintext key, and the application avoids constant KMS network calls via DEK caching.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Tier 3: HSM (Hardware Security Module)&lt;/strong&gt;&lt;br&gt;
Use HSMs strictly for top-tier regulatory requirements (e.g., cryptographic key generation, PCI PANs).&lt;br&gt;
Confirm: Cryptographic operations occur entirely within a tamper-proof hardware boundary.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern across high-throughput financial platforms is to aggressively isolate HSM usage to the narrowest possible scope.&lt;/p&gt;
&lt;p&gt;Context: A payment gateway needs to store customer profiles (names, addresses) alongside credit card PANs.&lt;/p&gt;
&lt;p&gt;Action: The engineering team maps the customer profile data to AWS KMS envelope encryption, allowing the application fleet to cache DEKs and process profile reads in under 2 milliseconds. However, the PANs are routed to a completely separate, heavily isolated microservice backed by an HSM (like AWS CloudHSM), which handles the strict PCI-DSS requirements.&lt;/p&gt;
&lt;p&gt;Result: The vast majority of the database reads operate with minimal latency overhead. The HSM is protected from throughput exhaustion because it is only invoked for the rare, specific operations that strictly require hardware-level cryptographic isolation.&lt;/p&gt;
&lt;p&gt;Learning: Treat HSMs as scarce, highly constrained resources. Never put an HSM on the critical path of a high-volume, standard database read query.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;HSM Exhaustion&lt;/td&gt;&lt;td&gt;Routing standard PII encryption through an HSM cluster&lt;/td&gt;&lt;td&gt;Aggressively down-tier standard PII to KMS envelope encryption&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;KMS Rate Limiting&lt;/td&gt;&lt;td&gt;The application calls the KMS API for every single row returned in a large &lt;code&gt;SELECT&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Implement DEK caching in the application layer with a strict 5-minute TTL&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Developer Velocity&lt;/td&gt;&lt;td&gt;Local development becomes impossible without access to the cloud HSM&lt;/td&gt;&lt;td&gt;Abstract the cryptographic tier behind an interface; use mock encryption providers for local development&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Applying a single cryptographic tier across an entire database leads to either crippling performance degradation or severe security vulnerabilities.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Implement a tiered decision framework mapping data classification (Low, High, Critical) to the appropriate cryptographic boundary (TDE, KMS, HSM).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: A high-throughput query fetching standard user data bypasses the HSM entirely, preserving hardware compute capacity for actual PCI-regulated operations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Classify your database schema into three tiers today. Identify any low-risk data that is needlessly consuming expensive KMS or HSM resources, and identify any critical PII that is dangerously relying on database-native &lt;code&gt;pgcrypto&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>cloud</category><category>security</category></item><item><title>The Database Observability Baseline: What Every DBA Dashboard Must Show</title><link>https://rajivonai.com/blog/2024-06-04-database-observability-baseline-dashboard/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-06-04-database-observability-baseline-dashboard/</guid><description>Before you can adopt AI-assisted triage, your database dashboard needs a foundation built on saturation, locking, and lag metrics.</description><pubDate>Tue, 04 Jun 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;If your primary database monitoring signal is a CPU spike, your telemetry is designed to tell you when the application is already broken, rather than telling you why the database is about to break.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most engineering teams rely on default cloud dashboards that prioritize host-level metrics: CPU utilization, memory consumption, and disk I/O. While these metrics matter for capacity planning, they are lag indicators for database health. A CPU spike is the &lt;em&gt;result&lt;/em&gt; of a problem—a bad query plan, a missing index, or a connection storm—not the problem itself.&lt;/p&gt;
&lt;p&gt;As teams move toward automated operations and AI-assisted triage, the agentic systems investigating incidents need granular telemetry. You cannot build a reliable AI SRE if the only context it receives is “CPU is at 99%.” The foundation of database observability must shift from host-level symptoms to engine-level state.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;When a database fails, it usually does so in one of three ways: it runs out of connections, it gets blocked by a lock, or it falls behind on maintenance tasks (like replication or vacuuming) until performance collapses.&lt;/p&gt;
&lt;p&gt;Default dashboards rarely surface these states clearly. Engineers spend critical incident minutes running ad-hoc SQL queries to figure out what is currently executing, who is blocking whom, and whether the connection pool is saturated. If your observability strategy relies on engineers SSH-ing into a bastion or running &lt;code&gt;pg_stat_activity&lt;/code&gt; manually during an outage, your time-to-mitigation will never improve.&lt;/p&gt;
&lt;h2 id=&quot;the-saturation-and-contention-baseline&quot;&gt;The Saturation and Contention Baseline&lt;/h2&gt;
&lt;p&gt;Every database dashboard must surface three categories of engine-level telemetry:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Saturation Metrics&lt;/strong&gt;: Active connections vs. maximum allowed, thread pool utilization, and cache hit ratios. You must know if the database is refusing work.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contention Metrics&lt;/strong&gt;: Row locks, table locks, and wait events. In PostgreSQL, this means tracking &lt;code&gt;wait_event_type&lt;/code&gt;. In MySQL, it means watching InnoDB row lock waits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lag Metrics&lt;/strong&gt;: Replication lag (in bytes and seconds) and maintenance lag (e.g., autovacuum backlog, compaction queue depth).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A baseline SQL query for PostgreSQL contention that should be converted into a constant metric looks like this:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    wait_event_type, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    wait_event, &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; waiting_sessions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; wait_event_type &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IS NOT NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GROUP BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; wait_event_type, wait_event&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; waiting_sessions &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If your dashboard shows a spike in &lt;code&gt;Lock&lt;/code&gt; wait events alongside a drop in cache hit ratio, you immediately know you have a query contention issue, saving 15 minutes of triage.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern for robust observability involves turning engine-state queries into time-series data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; PostgreSQL’s lock architecture means that sessions waiting for a lock consume zero CPU — a blocked process is simply parked, not working. This makes host-level monitoring blind to lock-induced latency. The PostgreSQL documentation describes &lt;code&gt;pg_stat_activity.wait_event_type&lt;/code&gt; as the authoritative source for what a session is waiting on, with &lt;code&gt;Lock&lt;/code&gt; as the wait event type for sessions blocked behind another session’s hold (&lt;a href=&quot;https://www.postgresql.org/docs/current/monitoring-stats.html#MONITORING-PG-STAT-ACTIVITY-VIEW&quot;&gt;PostgreSQL docs: pg_stat_activity&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The documented operational pattern is to export &lt;code&gt;pg_stat_activity&lt;/code&gt; wait event counts as a time-series metric polled every 10–15 seconds, so that lock contention spikes appear on dashboards alongside — and often well ahead of — latency metrics.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; This approach surfaces &lt;code&gt;AccessExclusiveLock&lt;/code&gt; spikes from DDL operations — &lt;code&gt;TRUNCATE&lt;/code&gt;, &lt;code&gt;VACUUM FULL&lt;/code&gt;, schema migrations — that block all concurrent readers without generating any CPU activity on the database host.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; PostgreSQL lock waits are invisible to infrastructure monitoring. The only signal is in the engine itself: &lt;code&gt;wait_event_type = &apos;Lock&apos;&lt;/code&gt; in &lt;code&gt;pg_stat_activity&lt;/code&gt; is the diagnostic that turns a “CPU looks fine, why is the app slow?” incident into a sub-minute diagnosis.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;Relying entirely on custom engine metrics introduces its own set of tradeoffs:&lt;/p&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Approach&lt;/th&gt;&lt;th&gt;Advantage&lt;/th&gt;&lt;th&gt;Disadvantage&lt;/th&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;High-Frequency Polling&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Catches micro-spikes in locks and connection exhaustion.&lt;/td&gt;&lt;td&gt;Puts continuous load on the database just to monitor it.&lt;/td&gt;&lt;td&gt;The monitoring query itself times out when the database is fully saturated.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Log-Based Telemetry&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Zero additional query load; captures exact slow queries.&lt;/td&gt;&lt;td&gt;High ingestion costs and delayed parsing times.&lt;/td&gt;&lt;td&gt;Log volumes spike during an incident, delaying the very telemetry needed to diagnose it.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Cloud Provider Insights (e.g., PI)&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Managed, low-overhead, deep integration with the hypervisor.&lt;/td&gt;&lt;td&gt;Locked into the vendor’s UI; harder to expose to internal AI agents.&lt;/td&gt;&lt;td&gt;The data cannot be easily correlated with external application traces.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Default cloud dashboards report CPU and memory — lag indicators that fire after the database is already broken, not before. Lock-induced latency produces zero CPU signal.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Add a “What is Waiting?” panel tracking &lt;code&gt;pg_stat_activity&lt;/code&gt; wait event counts, active lock counts, connection pool saturation, and replication byte lag as continuously scraped time-series metrics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; A staging game day that artificially locks a row should fire an alert within 60 seconds based on wait events — if it doesn’t, the telemetry foundation is incomplete and the next production incident will look exactly like the current one.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Deploy a PostgreSQL exporter polling &lt;code&gt;pg_stat_activity&lt;/code&gt; every 15 seconds and add a dashboard panel for &lt;code&gt;Lock&lt;/code&gt; wait event counts this week.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category><category>checklist</category></item><item><title>pgvector Basics: Embeddings Inside PostgreSQL</title><link>https://rajivonai.com/blog/2024-06-03-pgvector-basics-embeddings-inside-postgresql/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-06-03-pgvector-basics-embeddings-inside-postgresql/</guid><description>How pgvector adds vector storage and similarity search to PostgreSQL, what the three distance operators do, and the index you must create before you hit 100K rows.</description><pubDate>Mon, 03 Jun 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;pgvector lets you store and query embeddings directly in PostgreSQL — no separate vector database required. The extension is straightforward to install and the SQL surface is small. What catches engineers is that PostgreSQL will silently fall back to a full sequential scan if you never create a vector index, and at 10K rows that’s fine, but at 1M rows it’s unusable.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Embedding-based search has moved from ML research into standard backend work. Any feature that does semantic search, recommendations, or RAG retrieval needs to store embedding vectors and query them by similarity. The default answer for the past few years was to reach for a dedicated vector database — Pinecone, Weaviate, Qdrant. That’s still reasonable for pure vector workloads at scale. But for teams already running PostgreSQL, adding a second operational system for vectors means new infrastructure, new credentials, a second backup strategy, and cross-system consistency problems when the embedding and the source document live in different stores.&lt;/p&gt;
&lt;p&gt;pgvector, a PostgreSQL extension maintained on GitHub at &lt;code&gt;pgvector/pgvector&lt;/code&gt;, adds a native &lt;code&gt;vector&lt;/code&gt; column type and three index strategies to an existing Postgres instance. If your application already runs on PostgreSQL and your vector search latency requirements are in the tens-of-milliseconds range rather than single-digit milliseconds, pgvector lets you keep vectors and metadata in the same rows, under the same ACID guarantees, queried with the same SQL you already write.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Engineers discover pgvector, install it in an afternoon, add a &lt;code&gt;vector(1536)&lt;/code&gt; column to an existing table, and populate it with OpenAI embeddings using &lt;code&gt;text-embedding-ada-002&lt;/code&gt;. The first few similarity queries are fast. They ship the feature. Six months later, the table has grown to several hundred thousand rows and those queries are timing out.&lt;/p&gt;
&lt;p&gt;The root cause is almost always the same: no index was created on the vector column. PostgreSQL’s query planner has no way to prune a vector search geometrically without an index, so it scans every row and computes the distance to the query vector one row at a time. At 10K rows a sequential scan takes milliseconds. At 1M rows it takes seconds. The extension documentation on the pgvector GitHub README is explicit about this — approximate nearest-neighbor indexes are required for large datasets — but the requirement is easy to miss when the extension works so well at small scale.&lt;/p&gt;
&lt;p&gt;The core question this post answers: what do you need to set up correctly on day one so that pgvector stays fast as data grows?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App[Application] --&gt; Query[SQL Query with Embedding]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Query --&gt; PG[PostgreSQL — pgvector extension]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  PG --&gt; Planner[Query Planner]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Planner --&gt; CheckIndex{Vector Index Exists}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  CheckIndex --&gt;|No| SeqScan[Sequential Scan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  SeqScan --&gt; ComputeAll[Compute Distance for Every Row]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  CheckIndex --&gt;|Yes| IndexScan[HNSW or IVFFlat Index Scan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  IndexScan --&gt; ComputeApprox[Approximate Nearest Neighbor Search]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ComputeAll --&gt; Results[Return Top K Results]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ComputeApprox --&gt; Results&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Installation.&lt;/strong&gt; pgvector ships as a standard PostgreSQL extension. On most managed cloud databases (Amazon RDS, Google Cloud SQL, Supabase, Neon) it’s already available. On a self-managed Postgres instance, install from the pgvector GitHub repository or via your distro’s package manager, then run:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; EXTENSION &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; EXISTS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; vector;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That’s the full installation step. No daemon, no separate service.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Column type and table shape.&lt;/strong&gt; pgvector adds a &lt;code&gt;vector(n)&lt;/code&gt; column type where &lt;code&gt;n&lt;/code&gt; is the number of dimensions. OpenAI’s &lt;code&gt;text-embedding-ada-002&lt;/code&gt; model produces 1536-dimensional vectors; &lt;code&gt;text-embedding-3-small&lt;/code&gt; and &lt;code&gt;text-embedding-3-large&lt;/code&gt; use variable dimensions configurable at generation time with 1536 as a common default. A minimal embeddings table looks like:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; documents&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  id       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigserial&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  content  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;       NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  embedding vector(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1536&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Inserting a row with an embedding means passing the vector as a string literal or using a client library that serializes it for you:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; documents (content, embedding)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;The query planner chooses scan strategies based on statistics.&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;[0.021, -0.008, 0.034, ...]&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;The three distance operators.&lt;/strong&gt; pgvector exposes three similarity operators, each suited to different use cases:&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Operator&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;When to use&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;&amp;#x3C;-&gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;L2 (Euclidean) distance&lt;/td&gt;&lt;td&gt;General-purpose; works on raw or normalized vectors&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;&amp;#x3C;=&gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Cosine distance&lt;/td&gt;&lt;td&gt;Text embeddings; robust to vectors of different magnitudes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;&amp;#x3C;#&gt;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Negative inner product&lt;/td&gt;&lt;td&gt;Normalized vectors only; fastest to compute&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;A cosine similarity query — “return the 5 documents most semantically similar to this query embedding” — looks like:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; id, content, embedding &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;[0.021, -0.008, 0.034, ...]&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; distance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; documents&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; distance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For text embeddings, &lt;code&gt;&amp;#x3C;=&gt;&lt;/code&gt; (cosine) is the safe default. It is magnitude-insensitive, which matters because embedding models do not guarantee that all vectors will have the same norm.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Index types.&lt;/strong&gt; Without an index, every query above is a full sequential scan. pgvector supports two approximate nearest-neighbor index types:&lt;/p&gt;


























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Index&lt;/th&gt;&lt;th&gt;Build cost&lt;/th&gt;&lt;th&gt;Query recall&lt;/th&gt;&lt;th&gt;Memory use&lt;/th&gt;&lt;th&gt;Good for&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;IVFFlat&lt;/td&gt;&lt;td&gt;Lower&lt;/td&gt;&lt;td&gt;Tunable (lists parameter)&lt;/td&gt;&lt;td&gt;Lower&lt;/td&gt;&lt;td&gt;Datasets that change infrequently; faster to build&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;HNSW&lt;/td&gt;&lt;td&gt;Higher&lt;/td&gt;&lt;td&gt;Higher by default&lt;/td&gt;&lt;td&gt;Higher&lt;/td&gt;&lt;td&gt;Datasets that are queried heavily; better recall at same speed&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;For an initial deployment, IVFFlat is simpler. The &lt;code&gt;lists&lt;/code&gt; parameter divides the vector space into clusters; a good starting value is &lt;code&gt;sqrt(row_count)&lt;/code&gt;. A minimal IVFFlat index on cosine distance:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; documents &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;USING&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ivfflat (embedding vector_cosine_ops)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (lists &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For HNSW:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; documents &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;USING&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; hnsw (embedding vector_cosine_ops);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At datasets below roughly 10K rows, a sequential scan will often outperform an approximate index because the index lookup overhead isn’t amortized. At 100K rows and beyond, the index becomes necessary. There is no harm in creating the index early.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The pgvector GitHub README documents the full operator and index syntax. The project is maintained at &lt;code&gt;pgvector/pgvector&lt;/code&gt; on GitHub and the README is the authoritative source for supported Postgres versions, operator names, and index parameter ranges.&lt;/p&gt;
&lt;p&gt;OpenAI’s embeddings API documentation specifies that &lt;code&gt;text-embedding-ada-002&lt;/code&gt; produces 1536-dimensional vectors. That dimension count is a fixed constraint — the &lt;code&gt;vector(n)&lt;/code&gt; column type enforces an exact match, and a query embedding with a different dimension count will return a PostgreSQL type error at runtime. This is a documented behavior of the pgvector type system, not an edge case.&lt;/p&gt;
&lt;p&gt;The documented behavior of PostgreSQL’s query planner is that without a vector index, the planner will perform a sequential scan and compute all distances. &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; on a similarity query against an unindexed column will show &lt;code&gt;Seq Scan&lt;/code&gt; in the plan. Adding an IVFFlat or HNSW index causes the planner to switch to an index scan for large enough datasets — observable directly in the &lt;code&gt;EXPLAIN&lt;/code&gt; output.&lt;/p&gt;
&lt;p&gt;The documented pattern for vector deployments is to implement index assertions in CI to prevent regressions. Because &lt;code&gt;pgvector&lt;/code&gt; will silently fall back to a sequential scan if the vector index is invalid or dropped, automated tests running &lt;code&gt;EXPLAIN&lt;/code&gt; against a sample dataset ensure that the planner selects an &lt;code&gt;Index Scan&lt;/code&gt; rather than a &lt;code&gt;Seq Scan&lt;/code&gt; before code reaches production.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;No index at scale&lt;/td&gt;&lt;td&gt;Similarity queries time out above ~100K rows&lt;/td&gt;&lt;td&gt;PostgreSQL falls back to sequential scan, computing all pairwise distances in memory&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Dimension mismatch&lt;/td&gt;&lt;td&gt;Type error at query time&lt;/td&gt;&lt;td&gt;pgvector enforces exact dimension count; query embedding must match column definition&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cosine similarity on non-normalized vectors&lt;/td&gt;&lt;td&gt;Unexpected result rankings&lt;/td&gt;&lt;td&gt;Cosine distance accounts for angle only; two vectors with very different magnitudes can rank highly even when semantically distant if norms are unequal — use &lt;code&gt;&amp;#x3C;=&gt;&lt;/code&gt; not &lt;code&gt;&amp;#x3C;#&gt;&lt;/code&gt; unless you normalize at insertion time&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: pgvector silently uses a sequential scan on unindexed vector columns, so similarity queries that are fast at development scale become unusable in production without a code change.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Create an IVFFlat or HNSW index on the vector column at table creation time, using &lt;code&gt;vector_cosine_ops&lt;/code&gt; for text embeddings; verify with &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; that the planner uses the index.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Run &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; on your similarity query — the plan should show &lt;code&gt;Index Scan using ... on documents&lt;/code&gt; rather than &lt;code&gt;Seq Scan&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, add the &lt;code&gt;CREATE INDEX ... USING hnsw&lt;/code&gt; statement to your schema migration for any table with a vector column, and add a &lt;code&gt;EXPLAIN&lt;/code&gt; assertion to your staging smoke test so index regression is caught before it reaches production.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>ai-engineering</category></item><item><title>Scaling PostgreSQL Writes with Citus on EC2: A Multi-Tenant Retail Architecture</title><link>https://rajivonai.com/blog/2024-05-23-scaling-postgresql-writes-citus-ec2-retailco/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-05-23-scaling-postgresql-writes-citus-ec2-retailco/</guid><description>A production-oriented Citus design for tenant-local PostgreSQL writes, covering distribution keys, colocation, EC2 failure boundaries, hot tenants, migration, and a credible benchmark plan.</description><pubDate>Thu, 23 May 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Citus does not scale PostgreSQL writes because a diagram contains more worker nodes. It scales the part of the workload whose data ownership, constraints, queries, and transactions all agree on the same distribution key.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;RetailCo operates four retail brands and thousands of stores and merchant accounts. Its order platform grew from one PostgreSQL database into a 40-TB transaction system. Read replicas and table partitioning improved read capacity and lifecycle management, but checkout writes still pass through one writer.&lt;/p&gt;
&lt;p&gt;The preceding &lt;a href=&quot;https://rajivonai.com/blog/2024-03-19-can-you-run-citus-amazon-rds-postgresql/&quot;&gt;RDS PostgreSQL and Citus reality check&lt;/a&gt; established that standard Amazon RDS for PostgreSQL cannot supply the Citus runtime. This article starts after that decision.&lt;/p&gt;
&lt;p&gt;RetailCo is now evaluating Citus 13.0.1 on EC2 under five explicit design assumptions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;most checkout transactions belong to one &lt;code&gt;tenant_id&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;cross-tenant finance and risk reporting can leave the synchronous checkout path;&lt;/li&gt;
&lt;li&gt;PostgreSQL behavior and extension control matter more than a fully managed database control plane;&lt;/li&gt;
&lt;li&gt;a database platform team can operate PostgreSQL and Citus across multiple hosts;&lt;/li&gt;
&lt;li&gt;no throughput or recovery claim will be approved without a lab result.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;RetailCo is a reference architecture, not a claimed customer deployment. The 40-TB size, tenant counts, and transaction shapes are scenario inputs. Any measurements remain proposed until a reproducible lab produces raw output.&lt;/p&gt;
&lt;p&gt;The intended production shape has one application entry point, a highly available coordinator tier, and worker groups distributed across Availability Zones:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App[RetailCo order services] --&gt; Endpoint[database writer endpoint]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Endpoint --&gt; Pool[PgBouncer transaction pool]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Pool --&gt; CoordA[coordinator primary — zone A]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  CoordA --&gt; CoordB[coordinator standby — zone B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  CoordA --&gt; WorkerA[worker primary A]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  CoordA --&gt; WorkerB[worker primary B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  CoordA --&gt; WorkerC[worker primary C]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  WorkerA --&gt; StandbyA[worker standby A]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  WorkerB --&gt; StandbyB[worker standby B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  WorkerC --&gt; StandbyC[worker standby C]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The arrows from primary to standby represent PostgreSQL streaming replication. The application endpoint, promotion controller, and connection pool are separate responsibilities; PgBouncer does not elect a new database primary.&lt;/p&gt;
&lt;p&gt;This diagram describes the failure domains, not the final capacity. The correct worker count, EC2 instance type, EBS configuration, shard count, and connection limits must come from the benchmark and recovery exercises.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;RetailCo could distribute its largest tables with a few SQL commands:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; create_distributed_table(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;tenant_id&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; create_distributed_table(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;order_items&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;tenant_id&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; create_distributed_table(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;payments&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;tenant_id&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That is necessary but insufficient. Four design errors can leave the platform slower, less reliable, or impossible to operate.&lt;/p&gt;
&lt;h3 id=&quot;error-1--choosing-a-business-label-instead-of-an-ownership-key&quot;&gt;Error 1 — choosing a business label instead of an ownership key&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;brand_id&lt;/code&gt; has only four values. It creates four large ownership domains and makes one busy brand a permanent hot spot. Adding a fifth worker would not divide any of those four values.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;tenant_id&lt;/code&gt; has much higher cardinality and appears in the dominant checkout path. Hashing many tenants across many shards gives Citus room to spread independent tenants across workers. It does not guarantee balance: a single large tenant can still dominate the shard and worker that own it.&lt;/p&gt;
&lt;h3 id=&quot;error-2--distributing-tables-but-not-colocating-the-transaction&quot;&gt;Error 2 — distributing tables but not colocating the transaction&lt;/h3&gt;
&lt;p&gt;If &lt;code&gt;orders&lt;/code&gt; is distributed by &lt;code&gt;tenant_id&lt;/code&gt; while &lt;code&gt;order_items&lt;/code&gt; is distributed by &lt;code&gt;order_id&lt;/code&gt;, one checkout transaction can cross worker boundaries. Joins and foreign keys no longer align naturally, and each commit may require distributed coordination.&lt;/p&gt;
&lt;p&gt;Citus describes colocation as placing the same distribution-key values for related tables on the same workers. In a row-based multi-tenant model, every tenant-owned table should normally carry the same distribution column, and tenant-local queries should filter by it: &lt;a href=&quot;https://docs.citusdata.com/en/stable/get_started/concepts.html&quot;&gt;Citus concepts&lt;/a&gt; and &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/reference_ddl.html&quot;&gt;Citus DDL reference&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;error-3--assuming-distributed-sql-makes-global-rules-free&quot;&gt;Error 3 — assuming distributed SQL makes global rules free&lt;/h3&gt;
&lt;p&gt;Citus cannot enforce a primary key or unique constraint across independent shards unless the constraint includes the distribution column. Foreign keys between colocated distributed tables must include that column as well: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/reference_ddl.html#co-locating-tables&quot;&gt;constraints on distributed tables&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;RetailCo must decide whether an email, order number, or idempotency key is unique globally or only inside one tenant. A database cannot preserve an unstated business rule.&lt;/p&gt;
&lt;h3 id=&quot;error-4--treating-ec2-as-a-managed-database-service&quot;&gt;Error 4 — treating EC2 as a managed database service&lt;/h3&gt;
&lt;p&gt;Citus on EC2 gives RetailCo package, extension, operating-system, storage, network, and failover control. It also gives RetailCo responsibility for all of them.&lt;/p&gt;
&lt;p&gt;The coordinator holds cluster metadata and plans distributed work. Workers hold the distributed shards. Losing either tier without a tested promotion and recovery procedure is an outage, even when every EC2 instance and EBS volume still exists.&lt;/p&gt;
&lt;p&gt;The architectural question is therefore not “Can Citus create shards?” It is:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Can RetailCo make its critical writes tenant-local while operating the coordinator, workers, replication, rebalancing, upgrades, backups, and recovery as one database system?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;build-around-tenant-local-transactions&quot;&gt;Build Around Tenant-Local Transactions&lt;/h2&gt;
&lt;p&gt;The recommended model is Citus row-based sharding with &lt;code&gt;tenant_id&lt;/code&gt; as the common distribution column for the order write path. Citus also supports schema-based sharding, but row-based sharding is the better match here because RetailCo has many tenants sharing one schema and needs parallel cross-tenant analysis outside the checkout path: &lt;a href=&quot;https://docs.citusdata.com/en/stable/get_started/concepts.html#sharding-models&quot;&gt;Citus sharding models&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;classify-every-table-before-distributing-anything&quot;&gt;Classify every table before distributing anything&lt;/h3&gt;
&lt;p&gt;Every table belongs in one of three groups:&lt;/p&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Table class&lt;/th&gt;&lt;th&gt;Placement&lt;/th&gt;&lt;th&gt;RetailCo examples&lt;/th&gt;&lt;th&gt;Rule&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Distributed and colocated&lt;/td&gt;&lt;td&gt;Hash-sharded across workers by &lt;code&gt;tenant_id&lt;/code&gt;&lt;/td&gt;&lt;td&gt;tenants, customers, orders, order items, payments&lt;/td&gt;&lt;td&gt;Include &lt;code&gt;tenant_id&lt;/code&gt; in keys and tenant-local queries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reference&lt;/td&gt;&lt;td&gt;One logical shard copied to every worker&lt;/td&gt;&lt;td&gt;brands, order status codes, country codes&lt;/td&gt;&lt;td&gt;Keep small and relatively stable&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Coordinator-local&lt;/td&gt;&lt;td&gt;Stored only on the coordinator&lt;/td&gt;&lt;td&gt;narrowly scoped administration metadata&lt;/td&gt;&lt;td&gt;Do not put large or checkout-critical data here&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Reference tables are useful because every worker can join them locally. They are not free replicas for arbitrary tables. Citus uses two-phase commits when modifying reference tables, and every new worker needs their contents. A large or frequently updated product catalog may be a poor reference table even if every tenant reads it: &lt;a href=&quot;https://docs.citusdata.com/en/stable/get_started/concepts.html#table-types&quot;&gt;Citus table types&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For RetailCo, &lt;code&gt;brands&lt;/code&gt; and &lt;code&gt;order_status_codes&lt;/code&gt; are safe reference-table candidates. A multi-million-row catalog with frequent price and inventory updates needs a separate ownership decision.&lt;/p&gt;
&lt;h3 id=&quot;put-the-ownership-key-into-the-schema&quot;&gt;Put the ownership key into the schema&lt;/h3&gt;
&lt;p&gt;The following schema is deliberately explicit. Identifiers are supplied by the application or an approved ID service; the example does not assume a particular global sequence design.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; brands&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    brand_id        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    brand_name      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; UNIQUE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; order_status_codes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_status    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    is_terminal     &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;boolean&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; tenants&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    brand_id        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_name     &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    FOREIGN KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (brand_id) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;REFERENCES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; brands (brand_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; customers&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    customer_id     &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    email           &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    customer_name   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, customer_id),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    UNIQUE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, email),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    FOREIGN KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;REFERENCES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tenants (tenant_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    customer_id     &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_status    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ordered_at      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    total_amount    &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;18&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, order_id),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    FOREIGN KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, customer_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        REFERENCES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; customers (tenant_id, customer_id),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    FOREIGN KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (order_status)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        REFERENCES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; order_status_codes (order_status)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; order_items&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    line_number     &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;integer&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    product_id      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    quantity        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;integer&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; CHECK&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (quantity &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    unit_price      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;18&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, order_id, line_number),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    FOREIGN KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, order_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        REFERENCES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders (tenant_id, order_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; payments&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    payment_id      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    idempotency_key &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    payment_state   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    amount          &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;18&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    updated_at      &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, payment_id),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    UNIQUE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, idempotency_key),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    FOREIGN KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tenant_id, order_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        REFERENCES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders (tenant_id, order_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The schema changes the meaning of uniqueness. &lt;code&gt;UNIQUE (tenant_id, email)&lt;/code&gt; prevents duplicate emails inside one tenant, not across RetailCo. If the business requires global email uniqueness, it needs a globally coordinated registry or a different ownership rule.&lt;/p&gt;
&lt;p&gt;Next, create reference tables first, distribute the parent tables before their dependents, and explicitly colocate the order graph:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; create_reference_table(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;brands&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; create_reference_table(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;order_status_codes&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Illustrative lab setting, not a universal production recommendation.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; citus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;shard_count&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 128&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; create_distributed_table(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;tenants&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;tenant_id&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; create_distributed_table(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;customers&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;tenant_id&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    colocate_with &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;tenants&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; create_distributed_table(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;tenant_id&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    colocate_with &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;tenants&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; create_distributed_table(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;order_items&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;tenant_id&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    colocate_with &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;tenants&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; create_distributed_table(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;payments&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;tenant_id&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    colocate_with &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;tenants&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Citus defaults &lt;code&gt;citus.shard_count&lt;/code&gt; to 32 for new hash-distributed tables: &lt;a href=&quot;https://docs.citusdata.com/en/latest/develop/api_guc.html#citus-shard-count-integer&quot;&gt;Citus configuration reference&lt;/a&gt;. The example uses 128 only to create enough placement units for a 1, 2, 4, and 8-worker lab.&lt;/p&gt;
&lt;p&gt;Shard count is a tradeoff. Too few shards produce large movement units and cap placement flexibility. Too many add planning, metadata, connection, maintenance, and rebalance overhead. Citus can change an existing table’s shard count with &lt;code&gt;alter_distributed_table&lt;/code&gt;, but that is still a data-reorganization operation—not permission to ignore the initial design: &lt;a href=&quot;https://docs.citusdata.com/en/v13.0/admin_guide/cluster_management.html#shard-count&quot;&gt;Citus cluster sizing&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For the scenario’s 40-TB transaction history, 32 equal shards would average about 1.25 TB before replicas and skew; 128 would average about 320 GB. Neither average proves that a shard is operationally safe to move, restore, vacuum, or keep within its storage latency target. Measure real shard sizes and tenant skew.&lt;/p&gt;
&lt;h3 id=&quot;make-the-checkout-transaction-route-to-one-worker&quot;&gt;Make the checkout transaction route to one worker&lt;/h3&gt;
&lt;p&gt;A tenant-local checkout carries &lt;code&gt;tenant_id&lt;/code&gt; into every statement:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    customer_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_status,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ordered_at,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    total_amount&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ($&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;created&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, clock_timestamp(), $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; order_items (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    line_number,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    product_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    quantity,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    unit_price&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VALUES&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ($&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ($&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; payments (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tenant_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    payment_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    idempotency_key,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    payment_state,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    amount,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    updated_at&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ($&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;pending&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, clock_timestamp());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;COMMIT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Because the tables are colocated on &lt;code&gt;tenant_id&lt;/code&gt;, one tenant’s rows map to the same shard placements. The coordinator can route the tenant-local transaction to the worker that owns those placements.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Checkout[checkout with tenant identifier 1842] --&gt; Coordinator[Citus coordinator]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coordinator --&gt; Hash[hash tenant identifier]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Hash --&gt; Shard[owning colocated shard group]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Shard --&gt; WorkerB[worker B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  WorkerB --&gt; Orders[orders rows]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  WorkerB --&gt; Items[order item rows]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  WorkerB --&gt; Payments[payment state rows]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The application must preserve that locality. A query that omits &lt;code&gt;tenant_id&lt;/code&gt;, joins on unrelated keys, or modifies several tenants can fan out to multiple shards. Citus splits multi-shard queries into tasks, opens connections to relevant workers, collects results through the coordinator, and returns the final result: &lt;a href=&quot;https://docs.citusdata.com/en/v13.0/develop/reference_processing.html&quot;&gt;Citus query processing&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Multi-shard SQL is supported, but it has different cost and failure behavior. Citus’s current DML documentation notes that updates or deletes affecting one shard execute on one worker, while multi-shard modifications can use two-phase commit for greater safety: &lt;a href=&quot;https://docs.citusdata.com/en/latest/develop/reference_dml.html#updates-and-deletion&quot;&gt;Citus DML reference&lt;/a&gt;. Transaction locality should therefore be measured as a first-class production metric, not treated as a coding convention.&lt;/p&gt;
&lt;h3 id=&quot;use-time-partitioning-for-time-distribution-for-ownership&quot;&gt;Use time partitioning for time, distribution for ownership&lt;/h3&gt;
&lt;p&gt;The 40-TB history still needs lifecycle boundaries. The distribution column should remain &lt;code&gt;tenant_id&lt;/code&gt;; using a timestamp as the hash key would scatter time ranges and make tenant-local transactions cross shards.&lt;/p&gt;
&lt;p&gt;Native PostgreSQL range partitioning can be layered on the time dimension where the workload and Citus version support the chosen table design:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;tenant_id hash distribution&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        ↓&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;independent worker ownership&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        ↓&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ordered_at range partitions inside the distributed design&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        ↓&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;smaller retention and maintenance units&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This preserves two separate purposes: Citus distribution assigns write compute and ownership, while PostgreSQL time partitioning supports retention, pruning, and maintenance. Test the exact DDL, foreign keys, indexes, and lifecycle operations before applying this combination to the production schema.&lt;/p&gt;
&lt;h3 id=&quot;plan-for-hot-tenants-before-they-become-incidents&quot;&gt;Plan for hot tenants before they become incidents&lt;/h3&gt;
&lt;p&gt;Hash distribution balances many tenant identifiers statistically. It does not divide the workload generated by one identifier.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Many[thousands of normal tenants] --&gt; Shards[many shared shards]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Large[one dominant tenant] --&gt; Hot[one hot shard group]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Shards --&gt; Workers[balanced worker fleet]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Hot --&gt; OneWorker[one saturated worker]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Citus provides &lt;code&gt;isolate_tenant_to_new_shard()&lt;/code&gt; to move one tenant out of a shared shard. With &lt;code&gt;CASCADE&lt;/code&gt;, it isolates the tenant across colocated tables. The isolated shard can then be moved to a dedicated worker: &lt;a href=&quot;https://docs.citusdata.com/en/v13.0/admin_guide/cluster_management.html#tenant-isolation&quot;&gt;Citus tenant isolation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Isolation improves blast-radius and placement control. It does not split one tenant’s rows across several write owners. If one tenant alone exceeds one worker, RetailCo must subdivide that tenant by a stable lower-level ownership key, move the tenant to a dedicated architecture, or decompose the write domain.&lt;/p&gt;
&lt;h3 id=&quot;adding-a-worker-does-not-move-existing-data&quot;&gt;Adding a worker does not move existing data&lt;/h3&gt;
&lt;p&gt;Registering a new worker adds it to Citus metadata and copies reference tables as required:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_add_node(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;worker-d.internal&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5432&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Existing distributed shards do not become balanced merely because the worker exists. Current Citus provides a plan-first background rebalance workflow:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; get_rebalance_table_shards_plan();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_rebalance_start();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The older &lt;code&gt;rebalance_table_shards()&lt;/code&gt; function is deprecated as of Citus 11.2; current designs should use &lt;code&gt;citus_rebalance_start()&lt;/code&gt;: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/api_udf.html#rebalance-table-shards&quot;&gt;Citus utility functions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Citus Community Edition 11.0 and later supports nonblocking reads and writes during logical-replication-based shard movement, but movement still consumes source disk reads, destination writes, WAL, network bandwidth, CPU, and replication capacity: &lt;a href=&quot;https://docs.citusdata.com/en/v13.0/admin_guide/cluster_management.html#rebalance-shards-without-downtime&quot;&gt;Citus cluster management&lt;/a&gt;. The next article in this series treats rebalancing as its own production operation.&lt;/p&gt;
&lt;h2 id=&quot;operate-the-cluster-as-one-failure-domain&quot;&gt;Operate the Cluster as One Failure Domain&lt;/h2&gt;
&lt;h3 id=&quot;coordinator-availability&quot;&gt;Coordinator availability&lt;/h3&gt;
&lt;p&gt;Applications normally connect to the coordinator. It stores the metadata describing nodes, shards, placements, and colocation groups, then plans work against workers. Citus documents PostgreSQL streaming replication as one coordinator-availability option: &lt;a href=&quot;https://docs.citusdata.com/en/v13.0/admin_guide/cluster_management.html#coordinator-node-failures&quot;&gt;coordinator node failures&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;A workable EC2 design needs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;synchronous or asynchronous replication selected from an explicit RPO and latency requirement;&lt;/li&gt;
&lt;li&gt;automated health assessment and a controlled promotion mechanism;&lt;/li&gt;
&lt;li&gt;an application endpoint that follows the promoted coordinator;&lt;/li&gt;
&lt;li&gt;fencing so the previous primary cannot return as a writer;&lt;/li&gt;
&lt;li&gt;metadata validation after promotion;&lt;/li&gt;
&lt;li&gt;a tested failback procedure.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The coordinator should not become a hidden monolith. Keep large application-local tables off it, bound client sessions with PgBouncer, monitor coordinator CPU and memory, and measure the rows returned from workers for cross-tenant queries. Citus notes that multi-shard results still pass through the coordinator and that each session can create connection pools to workers: &lt;a href=&quot;https://docs.citusdata.com/en/stable/get_started/concepts.html#query-execution&quot;&gt;Citus query execution&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;worker-availability&quot;&gt;Worker availability&lt;/h3&gt;
&lt;p&gt;The first thing to establish is that &lt;strong&gt;Citus does not replicate hash-distributed shards by default&lt;/strong&gt;. &lt;code&gt;citus.shard_replication_factor&lt;/code&gt; defaults to 1, so each shard placement exists on exactly one worker. Losing a worker does not degrade the cluster; it removes the only copy of every tenant that worker owned. Verify this rather than assume it:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;citus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;shard_replication_factor&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Any distributed shard with a single placement has no in-Citus redundancy.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; shardid, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; placements&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_shards&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; citus_table_type &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;distributed&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GROUP BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; shardid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;HAVING&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Raising the replication factor is not the fix. Citus’s statement-based shard replication is intended for append-only workloads, not for a transactional checkout path, and it multiplies write work rather than providing failover. For heavy OLTP, current Citus documentation recommends PostgreSQL streaming replication of entire workers instead. Each worker primary therefore needs a standby and a promotion procedure: &lt;a href=&quot;https://docs.citusdata.com/en/v13.0/admin_guide/cluster_management.html#worker-node-failures&quot;&gt;worker node failures&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The consequence for RetailCo’s capacity plan is concrete: an eight-worker cluster is sixteen PostgreSQL servers plus the coordinator pair, and the redundancy lives entirely in PostgreSQL replication that RetailCo operates — not in Citus.&lt;/p&gt;
&lt;p&gt;The coordinator tracks workers by hostname and port. A failover design must either preserve the registered address through a stable endpoint or update Citus metadata with &lt;code&gt;citus_update_node()&lt;/code&gt; when the address changes: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/api_udf.html#citus-update-node&quot;&gt;Citus utility functions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Do not stop the game day after promoting PostgreSQL. Verify:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;the coordinator reaches the promoted worker;&lt;/li&gt;
&lt;li&gt;tenant-local reads and writes route correctly;&lt;/li&gt;
&lt;li&gt;standby replication is rebuilt;&lt;/li&gt;
&lt;li&gt;the failed node is fenced;&lt;/li&gt;
&lt;li&gt;monitoring and backups follow the new primary;&lt;/li&gt;
&lt;li&gt;the application sees the expected error and retry behavior during the transition.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;security-and-version-consistency&quot;&gt;Security and version consistency&lt;/h3&gt;
&lt;p&gt;Workers accept database traffic from the coordinator and, for some operations, from other workers. Place database nodes in private subnets, restrict security groups to required application and inter-node paths, use TLS with hostname verification for inter-node connections, and manage database credentials outside instance images.&lt;/p&gt;
&lt;p&gt;Citus supports &lt;code&gt;sslmode=verify-full&lt;/code&gt; in &lt;code&gt;citus.node_conninfo&lt;/code&gt;: &lt;a href=&quot;https://docs.citusdata.com/en/latest/develop/api_guc.html#citus-node-conninfo-text&quot;&gt;Citus configuration reference&lt;/a&gt;. Certificates, DNS names, failover endpoints, and &lt;code&gt;citus.local_hostname&lt;/code&gt; must agree.&lt;/p&gt;
&lt;p&gt;Keep PostgreSQL and Citus versions consistent across the cluster. Citus validates extension code and SQL-object versions because loading mismatched versions can cause errors or crashes: &lt;a href=&quot;https://docs.citusdata.com/en/latest/develop/api_guc.html#citus-enable-version-checks-boolean&quot;&gt;Citus version checks&lt;/a&gt;. An upgrade is a coordinated cluster operation, not an independent package update on whichever worker is convenient.&lt;/p&gt;
&lt;h3 id=&quot;backup-and-recovery&quot;&gt;Backup and recovery&lt;/h3&gt;
&lt;p&gt;A distributed backup is not proven because every EC2 instance has a snapshot. Recovery must recreate a mutually consistent coordinator and worker state, roles, extensions, configuration, reference tables, shard placements, and WAL history.&lt;/p&gt;
&lt;p&gt;This article does not prescribe an untested backup design. The minimum acceptance test is a restore into an isolated environment followed by metadata checks, tenant-local checksum or aggregate reconciliation, cross-table constraint checks, and application replay. The dedicated Citus backup and recovery article will compare logical, physical, snapshot, and PITR approaches in detail.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Citus 13.0.1 documents row-based sharding as a shared-schema model where each tenant-owned table and tenant-local query carries a common distribution column.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Generate a query inventory from &lt;code&gt;pg_stat_statements&lt;/code&gt;, application traces, and schema dependencies. Classify each critical statement as single-tenant, cross-tenant, or missing tenant context before changing the schema.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The architecture review can calculate transaction locality and identify queries that would fan out or lose supported constraint patterns.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; A promising shard key is not enough; the application must consistently supply it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Citus requires primary and unique keys on distributed tables to include the distribution column. Foreign keys between colocated distributed tables must include it as well.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Redesign RetailCo’s keys as tenant-scoped composites before the migration. Record each rule that remains global and give it an explicit coordination mechanism.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The schema expresses the ownership boundary Citus can enforce locally instead of relying on cross-worker uniqueness checks that do not exist.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The distribution key changes the business meaning of a constraint.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Citus routes single-shard work to one worker and decomposes multi-shard work into tasks whose results return through the coordinator.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Tag lab scripts and production telemetry with &lt;code&gt;tenant_id&lt;/code&gt;, shard count, task count, worker connections, and whether each transaction touched one or several workers. Use &lt;code&gt;citus_stat_statements&lt;/code&gt;, &lt;code&gt;citus_stat_tenants&lt;/code&gt;, PostgreSQL statistics, and infrastructure metrics with their documented collection overhead understood: &lt;a href=&quot;https://docs.citusdata.com/en/v13.0/admin_guide/cluster_management.html#query-statistics&quot;&gt;Citus query statistics&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The team can attribute latency to tenant skew, fan-out, coordinator pressure, worker saturation, storage, or locks instead of reporting only cluster-wide CPU.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Aggregate TPS can hide the tenant and worker that determine the real ceiling.&lt;/p&gt;
&lt;h3 id=&quot;proposed-1-2-4-and-8-worker-benchmark&quot;&gt;Proposed 1, 2, 4, and 8-worker benchmark&lt;/h3&gt;
&lt;p&gt;There are no empirical results yet. The lab must answer two different scaling questions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Strong scaling:&lt;/strong&gt; With the dataset and offered workload fixed, how much faster does the system become as workers are added?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Weak scaling:&lt;/strong&gt; If data and offered load grow with worker count, can latency and utilization remain within the same target envelope?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Use identical worker instance types, PostgreSQL and Citus versions, EBS layouts, kernel settings, durability settings, autovacuum policy, shard count, schema, and query mix. Run load generators on separate hosts so the client does not become the bottleneck.&lt;/p&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Workload&lt;/th&gt;&lt;th&gt;Purpose&lt;/th&gt;&lt;th&gt;Expected routing shape&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Tenant-local checkout&lt;/td&gt;&lt;td&gt;Test the intended architecture&lt;/td&gt;&lt;td&gt;One colocated shard group per transaction&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tenant-local order update&lt;/td&gt;&lt;td&gt;Measure update and lock behavior&lt;/td&gt;&lt;td&gt;One worker&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cross-tenant operational report&lt;/td&gt;&lt;td&gt;Measure fan-out and coordinator merge&lt;/td&gt;&lt;td&gt;Many workers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cross-tenant modification&lt;/td&gt;&lt;td&gt;Measure distributed commit and failure behavior&lt;/td&gt;&lt;td&gt;Many shards and workers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hot-tenant mix&lt;/td&gt;&lt;td&gt;Expose skew hidden by averages&lt;/td&gt;&lt;td&gt;One worker receives disproportionate load&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reference-table update&lt;/td&gt;&lt;td&gt;Measure replicated coordination&lt;/td&gt;&lt;td&gt;All relevant workers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Rebalance under checkout load&lt;/td&gt;&lt;td&gt;Measure capacity-addition interference&lt;/td&gt;&lt;td&gt;Source, target, WAL, and network paths&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Use custom &lt;code&gt;pgbench&lt;/code&gt; scripts that preserve RetailCo’s transaction boundaries rather than the built-in TPC-B-like schema. PostgreSQL documents &lt;code&gt;-f&lt;/code&gt; for custom scripts, &lt;code&gt;-c&lt;/code&gt; for clients, &lt;code&gt;-j&lt;/code&gt; for client threads, &lt;code&gt;-T&lt;/code&gt; for duration, &lt;code&gt;-l&lt;/code&gt; for per-transaction logs, and &lt;code&gt;-r&lt;/code&gt; for statement-level reporting: &lt;a href=&quot;https://www.postgresql.org/docs/current/pgbench.html&quot;&gt;PostgreSQL &lt;code&gt;pgbench&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;A reproducible run should include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a documented data-generation seed and tenant-size distribution;&lt;/li&gt;
&lt;li&gt;at least one warm-up period followed by a sustained measurement period;&lt;/li&gt;
&lt;li&gt;at least three repetitions per configuration;&lt;/li&gt;
&lt;li&gt;open-loop rate-controlled tests as well as maximum-throughput tests;&lt;/li&gt;
&lt;li&gt;P50, P95, P99, maximum latency, TPS, failures, retries, and schedule lag;&lt;/li&gt;
&lt;li&gt;coordinator and per-worker CPU, memory, connections, WAL, disk latency, throughput, network, locks, and autovacuum activity;&lt;/li&gt;
&lt;li&gt;raw logs and configuration snapshots stored with the result.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Run the matrix at 1, 2, 4, and 8 worker primaries. For strong scaling, rebalance the same dataset across the target worker count. For weak scaling, grow tenants and offered load proportionally while keeping the per-worker data and load target approximately constant.&lt;/p&gt;
&lt;p&gt;Do not call the result linear because one data point doubled. Report efficiency explicitly:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;speedup = baseline runtime ÷ measured runtime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;scaling efficiency = measured speedup ÷ worker-count multiplier&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Also separate the steady-state result from the capacity-addition result. A cluster can show excellent tenant-local TPS after rebalancing while taking too long or consuming too much headroom to move shards safely.&lt;/p&gt;
&lt;h3 id=&quot;failure-matrix&quot;&gt;Failure matrix&lt;/h3&gt;
&lt;p&gt;Performance without failure behavior is not a production evaluation.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure&lt;/th&gt;&lt;th&gt;Exercise&lt;/th&gt;&lt;th&gt;Required evidence&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Coordinator process stops&lt;/td&gt;&lt;td&gt;Promote standby and move endpoint&lt;/td&gt;&lt;td&gt;RTO, rejected transactions, fencing, metadata validation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Worker primary stops&lt;/td&gt;&lt;td&gt;Promote worker standby&lt;/td&gt;&lt;td&gt;Affected tenant errors, routing restoration, replica rebuild&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One Availability Zone is isolated&lt;/td&gt;&lt;td&gt;Observe coordinator and worker dependencies&lt;/td&gt;&lt;td&gt;Surviving quorum assumptions, endpoint behavior, blast radius&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Network latency rises between coordinator and worker&lt;/td&gt;&lt;td&gt;Hold offered load constant&lt;/td&gt;&lt;td&gt;P95 and P99, connection buildup, timeouts, retry amplification&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Rebalance is interrupted&lt;/td&gt;&lt;td&gt;Stop source or target during movement&lt;/td&gt;&lt;td&gt;Job state, source integrity, retry or abort procedure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Restore to a point in time&lt;/td&gt;&lt;td&gt;Recover into isolated network&lt;/td&gt;&lt;td&gt;Coordinator-worker consistency, tenant reconciliation, measured RPO and RTO&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Design choice&lt;/th&gt;&lt;th&gt;Good fit&lt;/th&gt;&lt;th&gt;Where it breaks&lt;/th&gt;&lt;th&gt;Mitigation or alternative&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Row-based sharding by &lt;code&gt;tenant_id&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Many tenants and mostly tenant-local transactions&lt;/td&gt;&lt;td&gt;Critical transactions omit or cross tenant boundaries&lt;/td&gt;&lt;td&gt;Redesign boundary, application sharding, or decomposition&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Four-value &lt;code&gt;brand_id&lt;/code&gt; distribution&lt;/td&gt;&lt;td&gt;Rarely appropriate for scale-out&lt;/td&gt;&lt;td&gt;Low cardinality and brand skew cap parallel ownership&lt;/td&gt;&lt;td&gt;Use higher-cardinality tenant ownership&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reference tables&lt;/td&gt;&lt;td&gt;Small, shared, relatively stable data&lt;/td&gt;&lt;td&gt;Large or frequently updated tables replicate work everywhere&lt;/td&gt;&lt;td&gt;Distribute, decompose, or serve through another domain&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Coordinator-local tables&lt;/td&gt;&lt;td&gt;Small administration data&lt;/td&gt;&lt;td&gt;Checkout or large data recreates a coordinator write ceiling&lt;/td&gt;&lt;td&gt;Distribute or remove from critical path&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;More workers&lt;/td&gt;&lt;td&gt;Enough movable shards and broad tenant load&lt;/td&gt;&lt;td&gt;Empty new node, rebalance cost, hot tenant remains hot&lt;/td&gt;&lt;td&gt;Plan and test &lt;code&gt;citus_rebalance_start()&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tenant isolation&lt;/td&gt;&lt;td&gt;One large tenant needs dedicated placement&lt;/td&gt;&lt;td&gt;One tenant exceeds one worker&lt;/td&gt;&lt;td&gt;Sub-shard tenant or use dedicated architecture&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Citus on EC2&lt;/td&gt;&lt;td&gt;Need extension control and have strong DB operations&lt;/td&gt;&lt;td&gt;Team cannot own HA, backup, upgrade, monitoring, and recovery&lt;/td&gt;&lt;td&gt;Aurora Limitless or application-sharded managed databases&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;RetailCo should reject Citus for this workload if any of these statements is true:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The majority of critical writes cannot be scoped to one stable tenant identifier.&lt;/li&gt;
&lt;li&gt;Global uniqueness and cross-tenant foreign keys are pervasive and cannot be redesigned.&lt;/li&gt;
&lt;li&gt;A single tenant regularly requires more write capacity than one worker can provide.&lt;/li&gt;
&lt;li&gt;Cross-tenant analytical queries must run synchronously against raw OLTP rows at high concurrency.&lt;/li&gt;
&lt;li&gt;The organization cannot provide on-call ownership for PostgreSQL, Citus, Linux, storage, networking, failover, backup, restore, and upgrades.&lt;/li&gt;
&lt;li&gt;The migration cannot backfill and validate &lt;code&gt;tenant_id&lt;/code&gt; across every tenant-owned row.&lt;/li&gt;
&lt;li&gt;The benchmark shows coordinator pressure, shard skew, or rebalance interference before reaching the required safety margin.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The existing 40-TB table makes migration a larger risk than cluster creation. Citus can distribute a nonempty coordinator-local table, but its documentation states that writes are blocked while rows move: &lt;a href=&quot;https://docs.citusdata.com/en/stable/develop/reference_ddl.html#distributing-coordinator-data&quot;&gt;distributing coordinator data&lt;/a&gt;. That is not a credible one-command plan for this scenario.&lt;/p&gt;
&lt;p&gt;RetailCo needs a staged migration:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Inventory[schema and query inventory] --&gt; Backfill[add and backfill tenant identifier]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Backfill --&gt; Validate[validate keys and tenant ownership]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Validate --&gt; Build[build empty distributed schema]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Build --&gt; Copy[bulk copy historical data by tenant ranges]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Copy --&gt; Capture[capture ongoing source changes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Capture --&gt; Reconcile[reconcile counts hashes and business totals]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reconcile --&gt; Canary[route canary tenants]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Canary --&gt; Cutover[move remaining tenants]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Cutover --&gt; Rollback[retain bounded rollback path]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Every step needs an abort condition. “The copy command completed” is not proof that orders, items, payments, and tenant ownership agree.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;For this RetailCo scenario, &lt;strong&gt;Citus row-based sharding on EC2 is the recommended self-managed proof of concept, not yet an approved production migration&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The design should use &lt;code&gt;tenant_id&lt;/code&gt; across customers, orders, order items, payments, and every checkout query; small stable lookups should become reference tables; large shared domains should remain outside that shortcut. The coordinator and every worker need PostgreSQL streaming-replication standbys, controlled promotion, stable addressing, fencing, and tested restore procedures.&lt;/p&gt;
&lt;p&gt;Production approval requires the following sequence:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Calculate transaction locality from real queries and traces. Establish what percentage of checkout commits are single-tenant.&lt;/li&gt;
&lt;li&gt;Produce a table-classification ledger: distributed, reference, coordinator-local, or outside the order domain.&lt;/li&gt;
&lt;li&gt;Redesign primary, unique, and foreign keys so tenant-owned constraints include &lt;code&gt;tenant_id&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Build a Citus 13.0.1 lab using the same PostgreSQL version, extensions, schema, durability, TLS, and connection path intended for production.&lt;/li&gt;
&lt;li&gt;Run the 1, 2, 4, and 8-worker strong- and weak-scaling matrix without inventing or extrapolating results.&lt;/li&gt;
&lt;li&gt;Introduce hot tenants, cross-tenant SQL, worker failure, coordinator failure, network delay, rebalance, and restore while the workload is active.&lt;/li&gt;
&lt;li&gt;Select worker and shard counts from measured headroom, movement time, recovery time, and skew—not from a reference diagram.&lt;/li&gt;
&lt;li&gt;Migrate by verifiable tenant slices with continuous change capture, reconciliation, canaries, and rollback.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Good fit:&lt;/strong&gt; thousands of tenants, shared schema, high-cardinality &lt;code&gt;tenant_id&lt;/code&gt;, mostly tenant-local writes, and a team prepared to operate distributed PostgreSQL.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Poor fit:&lt;/strong&gt; global relational constraints, frequent cross-tenant transactions, one tenant larger than one worker, or a strong requirement for provider-managed database operations.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Main advantage:&lt;/strong&gt; independent workers can own different tenant writes while preserving PostgreSQL joins and transactions inside a colocated tenant boundary.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Main limitation:&lt;/strong&gt; one distribution column becomes a hard architectural boundary, and Citus does not remove coordinator, hot-tenant, migration, or distributed-operations costs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Operational complexity:&lt;/strong&gt; high. RetailCo owns the full EC2, PostgreSQL, Citus, replication, failover, security, monitoring, backup, restore, upgrade, and rebalance lifecycle.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Migration complexity:&lt;/strong&gt; very high for 40 TB. Treat the move as an online data-platform migration, not an extension installation.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; RetailCo’s order writer has reached a sustained ceiling, but distributing tables without transaction locality would replace one bottleneck with cross-worker coordination.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; use Citus row-based sharding on &lt;code&gt;tenant_id&lt;/code&gt;, explicitly colocate the order graph, replicate small reference data, and operate coordinator and worker HA as one system.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Citus documentation shows that the distribution column controls shard placement, colocation keeps related tenant rows together, single-shard work runs on one worker, and multi-shard work returns through the coordinator. The proposed lab must now verify RetailCo’s workload against those mechanics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; complete the transaction-locality and table-classification ledgers before provisioning production-sized EC2 instances. If the data model cannot make checkout tenant-local, stop the Citus migration and choose another ownership boundary.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category></item><item><title>Vectorless RAG Patterns for Database Knowledge Systems</title><link>https://rajivonai.com/blog/2024-05-16-vectorless-rag-patterns-for-database-knowledge-systems/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-05-16-vectorless-rag-patterns-for-database-knowledge-systems/</guid><description>How tree-based retrieval can improve DB runbooks, schema docs, and incident knowledge over chunked vector search.</description><pubDate>Thu, 16 May 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;RAG (Retrieval-Augmented Generation) is the default pattern for giving AI assistants context, but chunking structured operational documentation into 300-token vectors destroys the sequence of runbooks precisely when you need them most.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Engineering teams are increasingly feeding their incident response channels and database documentation into vector databases to build automated on-call assistants. The goal is to surface the right mitigation command at 2:13 a.m. when replica lag climbs or autovacuum gets blocked, without manually paging through Git repositories or wiki pages.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The default chunked vector search implementation fails catastrophically for procedural database runbooks. It splits documents into arbitrary token pieces, embedding each piece into a vector, and retrieving chunks based on vocabulary similarity.&lt;/p&gt;
&lt;p&gt;A PostgreSQL schema migration runbook contains a precheck, the DDL command, a validation query, and a rollback step. Vector chunking breaks this structure apart. Similarity scoring finds the chunk with the best vocabulary match for “migration,” which might return the validation query without the prerequisite rollback instructions. How do we retrieve operational knowledge while preserving the exact order of execution?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;Vectorless RAG bypasses embedding models for structured documentation by using &lt;strong&gt;section tree retrieval&lt;/strong&gt;. Instead of slicing text into chunks and measuring cosine similarity, documents are stored as a structured JSON tree keyed by document path. Retrieval happens via path prefixes rather than semantic approximation, guaranteeing that the precheck, command, validation, and rollback remain attached and in sequence.&lt;/p&gt;
&lt;h2 id=&quot;section-tree-retrieval-architecture&quot;&gt;Section Tree Retrieval Architecture&lt;/h2&gt;
&lt;p&gt;To build this, store your operational docs as a structured JSON tree in PostgreSQL using JSONB, keeping a vector store only for messy operational memory like Slack exports.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 1: Convert one critical runbook into a section tree.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The tree builder parses your Markdown headings into a nested JSON structure where each node has a &lt;code&gt;path&lt;/code&gt; (array of heading titles from root to section), a &lt;code&gt;summary&lt;/code&gt;, and the section &lt;code&gt;body&lt;/code&gt;. No embeddings — just structure.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;python&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; scripts/build_doc_tree.py&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --input&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; docs/postgres/replication-lag.md&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --doc-id&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; postgres-replication-lag&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --output&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; build/postgres-replication-lag.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Confirm with:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;jq&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;.doc_id, .children[0].path, .children[0].summary&apos;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; build/postgres-replication-lag.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 2: Store the tree in Postgres JSONB with path-aware lookup.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Each row is one document section. The &lt;code&gt;path&lt;/code&gt; column is an array (&lt;code&gt;ARRAY[&apos;Postgres&apos;,&apos;Replication&apos;,&apos;Lag&apos;]&lt;/code&gt;) so you can query by prefix — “give me all Replication sections” — without scanning the full document body.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; doc_index&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  doc_id        &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  path&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;          text&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[]  &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  title         &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  summary       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  body          &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  owner&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;         text&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_verified &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  node          jsonb   &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (doc_id, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;path&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; doc_index_path_gin&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; doc_index &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;USING&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; gin (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;path&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; doc_index_node_gin&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; doc_index &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;USING&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; gin (node jsonb_path_ops);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 3: Load sections without flattening the procedure.&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;python&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; scripts/load_doc_tree_pg.py&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --file&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; build/postgres-replication-lag.json&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --dsn&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$DOC_INDEX_DSN&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 4: Route structured questions to tree retrieval first.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;At query time, match document class before calling an LLM. Runbooks and schema docs route to the &lt;code&gt;doc_index&lt;/code&gt; table. Incident postmortems route to the vector store.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; path&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, title, summary, body&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; doc_index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; doc_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;postgres-replication-lag&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    summary ILIKE &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;%schema migration%&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    OR&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; body   ILIKE &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;%replica lag%&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    OR&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; path&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; @&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ARRAY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&apos;Postgres&apos;,&apos;Replication&apos;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; array_length(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;path&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Step 5: Keep vector search for messy incident memory.&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;python&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; scripts/embed_incidents.py&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --source&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; s3://db-knowledge/incidents/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --collection&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; db_incidents&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --vector-store&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; qdrant&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DBA[DBA question] --&gt; Router[retrieval router]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Router --&gt;|structured runbook| PostgresJSONB[doc_index in Postgres JSONB]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Router --&gt;|unstructured tickets| Qdrant[Qdrant — incidents collection]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PostgresJSONB --&gt; TreePath[section path — parent summaries — body]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Qdrant --&gt; VectorHits[top-k incident snippets]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    TreePath --&gt; LLM[LLM answer composer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    VectorHits --&gt; LLM&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    LLM --&gt; Answer[answer with exact citation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Answer --&gt; DBA&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The router decision is intentionally boring: classify the document type first, then retrieve. Boring routing wakes you up less often.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern across operational knowledge systems is to strictly bound retrieval by how database engines execute commands. Derived from how PostgreSQL handles locking, schema changes hold an &lt;code&gt;AccessExclusiveLock&lt;/code&gt; that queues all subsequent reads, often manifesting as replication lag or connection exhaustion. When a standard chunked RAG system encounters a query about this lock state, it routinely hallucinates by stitching together a &lt;code&gt;pg_stat_activity&lt;/code&gt; query from a minor version upgrade document with a generic &lt;code&gt;pg_cancel_backend&lt;/code&gt; snippet. This disjointed context encourages operators to blindly kill processes without verifying the blocker. By migrating to a section tree, the system instead pulls the entire operational branch—returning the specific diagnostic query, the targeted termination command, and the required rollback sequence as an atomic unit.&lt;/p&gt;
&lt;p&gt;This structural alignment yields measurable shifts in how retrieval behaves during incidents:&lt;/p&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Metric&lt;/th&gt;&lt;th&gt;Chunked vector search&lt;/th&gt;&lt;th&gt;Section tree retrieval&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Runbook answer citation&lt;/td&gt;&lt;td&gt;Chunk ID + similarity score&lt;/td&gt;&lt;td&gt;Exact section path&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Migration rollback retrieval&lt;/td&gt;&lt;td&gt;Often split across 2–4 chunks&lt;/td&gt;&lt;td&gt;Full prerequisite, command, validation, rollback in one section&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Embedding model change&lt;/td&gt;&lt;td&gt;Re-embed runbooks, tickets, postmortems&lt;/td&gt;&lt;td&gt;Re-embed tickets only; tree index unchanged&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Incident query behavior&lt;/td&gt;&lt;td&gt;Finds similar language&lt;/td&gt;&lt;td&gt;Follows operational structure first&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The architectural split between structured and unstructured data typically looks like this:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Corpus&lt;/th&gt;&lt;th&gt;Best retrieval pattern&lt;/th&gt;&lt;th&gt;Reason&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;PostgreSQL failover runbook&lt;/td&gt;&lt;td&gt;Section tree&lt;/td&gt;&lt;td&gt;Procedure order and rollback must stay together&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Snowflake warehouse guide&lt;/td&gt;&lt;td&gt;Section tree&lt;/td&gt;&lt;td&gt;Sections map to operational decisions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Prior SEV2 postmortems&lt;/td&gt;&lt;td&gt;Vector search&lt;/td&gt;&lt;td&gt;Language and structure vary across incidents&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slack incident channel export&lt;/td&gt;&lt;td&gt;Vector search&lt;/td&gt;&lt;td&gt;Messy, duplicated, high volume&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schema ownership docs&lt;/td&gt;&lt;td&gt;Section tree&lt;/td&gt;&lt;td&gt;Paths and citations matter&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slow query examples&lt;/td&gt;&lt;td&gt;Hybrid&lt;/td&gt;&lt;td&gt;Similar query shape + exact remediation docs&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Bad tree structure&lt;/td&gt;&lt;td&gt;Markdown headings are inconsistent or PDF parsing invents sections&lt;/td&gt;&lt;td&gt;Normalize docs to Markdown before building the tree; reject trees with missing &lt;code&gt;path&lt;/code&gt;, &lt;code&gt;summary&lt;/code&gt;, or &lt;code&gt;last_verified&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Wrong retrieval route&lt;/td&gt;&lt;td&gt;Query says “incident” but asks for the official rollback procedure&lt;/td&gt;&lt;td&gt;Add explicit document-class rules before any semantic routing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Stale runbook answer&lt;/td&gt;&lt;td&gt;Section exists but has not been tested since PostgreSQL 14&lt;/td&gt;&lt;td&gt;Require &lt;code&gt;last_verified&lt;/code&gt;; suppress sections older than the last engine upgrade&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;JSONB table abuse&lt;/td&gt;&lt;td&gt;Teams start dumping every Slack export as a tree&lt;/td&gt;&lt;td&gt;Enforce: high-volume, messy text stays in the vector store&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LLM over-summarizes commands&lt;/td&gt;&lt;td&gt;Retrieved section has multiple guarded branches&lt;/td&gt;&lt;td&gt;Return command blocks verbatim; make the model cite the section path, not paraphrase it&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Chunked vector search destroys the procedural sequence of database runbooks, leading to dangerous out-of-order execution during incidents.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Implement section tree retrieval using PostgreSQL JSONB to store and query operational documentation by hierarchical paths instead of token embeddings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Extracting a full node path guarantees that prerequisites, commands, and rollbacks are returned as cohesive units, respecting the database’s locking behaviors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Convert one critical PostgreSQL failover runbook into a JSON tree in &lt;code&gt;doc_index&lt;/code&gt;, and test 20 questions from recent incidents against both the tree index and the legacy vector store to compare citation accuracy.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>vector-db</category><category>ai-engineering</category></item><item><title>Cache Incident Workflow: Hit Rate Collapse, Stampede, TTLs, and Database Protection</title><link>https://rajivonai.com/blog/2024-05-15-cache-incident-workflow-hit-rate-collapse-stampede-ttls-and-database-protection/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-05-15-cache-incident-workflow-hit-rate-collapse-stampede-ttls-and-database-protection/</guid><description>Cache hit-rate collapse leads to stampede, TTL misconfiguration, and unprotected database load — a workflow for diagnosing each failure in sequence.</description><pubDate>Wed, 15 May 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A cache incident is not a cache problem; it is a database protection failure that happens to start in the cache layer.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most production systems treat caching as a performance optimization until the first real incident proves otherwise. A healthy cache hides read amplification, expensive joins, remote API latency, and uneven traffic. When the cache is warm, the database looks calm. When hit rate collapses, the same database is suddenly asked to serve traffic it was never provisioned to absorb directly.&lt;/p&gt;
&lt;p&gt;The modern version is worse because cache layers now sit in front of many different backends: relational databases, object stores, search indexes, vector databases, model gateways, feature stores, and third-party APIs. The cache is not only shaving milliseconds. It is often the only thing standing between normal traffic and cascading saturation.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cache incidents rarely begin with a clean outage. They begin with drift: hit rate drops from 96% to 88%, latency widens, backend queue depth rises, retry volume increases, and application workers hold connections longer. Then a TTL boundary, deploy, hot key, regional failover, or eviction event turns the drift into a cliff.&lt;/p&gt;
&lt;p&gt;The failure modes compound:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Hit rate collapse&lt;/strong&gt; moves traffic from cache to database.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stampede&lt;/strong&gt; causes many workers to recompute the same missing value.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TTL synchronization&lt;/strong&gt; expires many keys at once.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Retries&lt;/strong&gt; multiply backend pressure during the worst window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Eviction churn&lt;/strong&gt; removes useful keys faster than they can be refilled.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Database saturation&lt;/strong&gt; turns slow misses into timeouts, which create more retries.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The core question is not “How do we restore the cache?” It is: &lt;strong&gt;how do we keep the database alive while the cache is wrong, cold, overloaded, or partially unavailable?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-answer-treat-cache-recovery-as-an-incident-workflow&quot;&gt;The Answer: Treat Cache Recovery as an Incident Workflow&lt;/h2&gt;
&lt;p&gt;A reliable cache architecture separates three control loops: request serving, cache regeneration, and database protection. The application should not let every miss become an immediate backend query. The cache layer needs guardrails that decide when to serve stale data, when to coalesce work, when to shed load, and when to slow callers before the database falls over.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[request arrives] --&gt; B{cache lookup}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt;|hit| C[return cached value]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt;|miss| D{single flight guard}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt;|leader exists| E[wait briefly or serve stale]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt;|leader elected| F{backend budget available}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt;|yes| G[query database]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt;|no| H[serve stale or bounded error]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; I[refresh cache with jittered TTL]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; J[return value]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; J&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt; K[protect database and emit incident signal]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The architecture has four practical requirements.&lt;/p&gt;
&lt;p&gt;First, every expensive key path needs &lt;strong&gt;request coalescing&lt;/strong&gt;. In Go this pattern is often called singleflight; in other stacks it appears as per-key locks, lease tokens, or refresh ownership. The point is simple: one worker regenerates a missing value while the rest wait briefly, serve stale, or fail fast. Without coalescing, one expired hot key can become thousands of identical database queries.&lt;/p&gt;
&lt;p&gt;Second, TTLs need &lt;strong&gt;jitter and refresh policy&lt;/strong&gt;. Fixed TTLs create synchronized expiration. Jitter spreads refreshes over time. Refresh-ahead can help for predictable hot keys, but it must be bounded; an aggressive refresh daemon can become its own incident. The cache should know the difference between a value that is absent, a value that is stale but usable, and a value that must not be served.&lt;/p&gt;
&lt;p&gt;Third, the database needs an explicit &lt;strong&gt;miss budget&lt;/strong&gt;. A miss path should pass through a limiter sized to what the backend can survive. That limiter can be per service, per shard, per tenant, or per key class. If the budget is exhausted, the application should serve stale data, return a controlled degraded response, or shed low-priority traffic. It should not keep adding concurrent database work until connection pools collapse.&lt;/p&gt;
&lt;p&gt;Fourth, incident response needs &lt;strong&gt;cache-specific telemetry&lt;/strong&gt;. Overall latency is too late. Useful signals include cache hit rate by route and key family, miss rate, fill latency, stale serve count, coalescing wait time, backend query rate from cache misses, eviction rate, hot key distribution, TTL age distribution, and database saturation. The incident dashboard should answer: which keys are missing, why they are missing, who is regenerating them, and what the backend is absorbing.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; The documented pattern from Meta’s memcache architecture is that caching at scale requires more than a key-value store. The NSDI paper “Scaling Memcache at Facebook” describes leases to address stale sets and thundering herd behavior, regional cache deployment, and operational mechanisms for avoiding backend overload. The public lesson is not “use memcache.” It is that large read-heavy systems need cache coordination semantics when many clients share a backend.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Apply the same pattern in service-level design. Add per-key regeneration ownership, stale serving for eligible data, TTL jitter, and a database miss budget. Treat cache fills as controlled backend work, not ordinary request work. For hot objects, separate freshness policy from availability policy: a profile page, product catalog entry, or feature flag snapshot may tolerate seconds or minutes of staleness; a payment authorization result may not.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The expected operational result is reduced peak backend amplification. During a hit rate collapse, only bounded fill work reaches the database. Callers may see stale responses or controlled degradation, but the primary datastore remains available. This is the difference between a cache incident and a full service outage.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; The documented pattern is that cache correctness and cache availability are separate concerns. A system can be correct but fragile if every miss synchronously regenerates through the database. A system can also be fast but unsafe if TTLs align and all clients refresh together. Production cache design has to encode contention control, not just expiration.&lt;/p&gt;
&lt;p&gt;Another known pattern appears in Amazon DynamoDB Accelerator documentation: DAX is positioned as a write-through and read-through caching layer for DynamoDB workloads that need microsecond read latency. The architecture is useful because it makes the cache part of the data access path rather than a scattered application convention. The broader learning is that centralizing cache behavior can reduce inconsistent miss handling across services, but it does not remove the need for capacity planning, TTL discipline, and fallback behavior.&lt;/p&gt;
&lt;p&gt;PostgreSQL and MySQL also demonstrate the backend side of the same pattern. When connection pools saturate, the database does not merely become slower; it starts changing the behavior of the whole system. Transactions hold locks longer, application threads wait longer, retries overlap, and health checks can become noisy. A cache incident workflow must therefore protect database concurrency first, then restore hit rate.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;th&gt;Residual risk&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Hot key expiration&lt;/td&gt;&lt;td&gt;One popular key expires and all workers miss together&lt;/td&gt;&lt;td&gt;Per-key singleflight, stale-while-revalidate, refresh-ahead&lt;/td&gt;&lt;td&gt;Leader refresh can still fail repeatedly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;TTL cliff&lt;/td&gt;&lt;td&gt;Many keys share the same expiration window&lt;/td&gt;&lt;td&gt;TTL jitter and staged warmup&lt;/td&gt;&lt;td&gt;Bulk deploys can still invalidate too much&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cold cache after deploy&lt;/td&gt;&lt;td&gt;New version changes key names or serialization&lt;/td&gt;&lt;td&gt;Versioned rollout and prewarming&lt;/td&gt;&lt;td&gt;Bad prewarm can overload backend&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Eviction churn&lt;/td&gt;&lt;td&gt;Cache is too small or key distribution changed&lt;/td&gt;&lt;td&gt;Track eviction rate and resize by working set&lt;/td&gt;&lt;td&gt;Large tenants can dominate shared caches&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Retry amplification&lt;/td&gt;&lt;td&gt;Misses become slow, then callers retry&lt;/td&gt;&lt;td&gt;Retry budgets and circuit breakers&lt;/td&gt;&lt;td&gt;Client libraries may ignore service policy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Stale data misuse&lt;/td&gt;&lt;td&gt;Degraded mode serves data that must be fresh&lt;/td&gt;&lt;td&gt;Classify keys by freshness contract&lt;/td&gt;&lt;td&gt;Product requirements may be ambiguous&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Database collapse&lt;/td&gt;&lt;td&gt;Cache fill traffic exceeds backend capacity&lt;/td&gt;&lt;td&gt;Miss budget and load shedding&lt;/td&gt;&lt;td&gt;User-visible errors may be unavoidable&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Your cache is probably measured as a latency tool, not as a database safety boundary. Start by charting hit rate, miss rate, fill latency, stale serves, evictions, and backend queries caused by misses on the same dashboard.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Put a controlled workflow on every expensive miss: coalesce by key, check backend budget, serve stale when allowed, apply TTL jitter, and emit a structured incident signal when protection logic activates.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Test the failure directly. Run a game day that expires the top 1,000 keys, disables one cache node, or deploys a changed key prefix in staging. The pass condition is not zero errors; it is that the database remains inside its concurrency and latency budget.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Classify cached data into three contracts: must be fresh, may be briefly stale, and may degrade. Then make the miss path enforce those contracts in code instead of relying on humans to remember them during an incident.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Proxy Extreme: How GitHub Mastered Connection Storms and HA Failovers</title><link>https://rajivonai.com/blog/2024-05-15-proxysql-extreme-connection-storms-ha/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-05-15-proxysql-extreme-connection-storms-ha/</guid><description>A deep dive into how GitHub&apos;s Orchestrator decouples application connection state from database availability, and how pairing it with a SQL-aware proxy like ProxySQL survives failovers without downtime.</description><pubDate>Wed, 15 May 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Applications should not care which database instance is the primary writer, nor should they crash when the database topology shifts. Yet, most systems rely on DNS propagation or fragile client-side driver logic to handle High Availability (HA) failovers, resulting in guaranteed downtime and catastrophic connection storms.&lt;/p&gt;
&lt;p&gt;When your database serves the world’s largest repository of source code, 30 seconds of DNS propagation delay is an unacceptable eternity. This is why the industry relies on intelligent proxy layers and topology orchestrators.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;As a platform scales, the database topology inherently becomes more complex. You move from a single primary to a primary with read replicas, then to a massive sharded fleet.&lt;/p&gt;
&lt;p&gt;During a hardware failure or a routine maintenance window, a database failover occurs. A replica is promoted to become the new primary. However, the application servers are still holding onto thousands of established TCP connections pointing to the dead primary. When those connections break, the application pods desperately attempt to reconnect, overwhelming the newly promoted primary in a “connection storm.”&lt;/p&gt;
&lt;p&gt;DNS-based failover (updating a Route53 CNAME) is the traditional industry standard, but it relies on the application language, the OS resolver, and the local network caching honoring Time-To-Live (TTL) values correctly. In reality, DNS caches are sticky. The application throws &lt;code&gt;Connection Refused&lt;/code&gt; or &lt;code&gt;Read-only transaction&lt;/code&gt; errors until humans intervene or automated backoff scripts finally succeed.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The database layer and the application layer have misaligned expectations. The database expects to be able to shift roles (writer to reader, active to passive) instantly for HA purposes. The application expects a persistent, unbreakable TCP connection for its connection pool.&lt;/p&gt;
&lt;p&gt;When these layers are tightly coupled, any database topological change cascades as a failure into the application. If 50 application pods simultaneously detect a disconnect and aggressively attempt to recreate 100 connections each, the resulting 5,000 connection requests will instantly exhaust the database’s &lt;code&gt;max_connections&lt;/code&gt; limit, or max out the CPU handling the TLS handshakes, killing the database before it can serve a single query.&lt;/p&gt;
&lt;p&gt;The core question is: how do you decouple the application’s connection lifecycle from the physical database topology so that failovers become completely invisible to the end user?&lt;/p&gt;
&lt;h2 id=&quot;the-proxysql-decoupling-architecture&quot;&gt;The ProxySQL Decoupling Architecture&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Application Fleet] --&gt; B[ProxySQL Tier]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[Database Primary — Writer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; D[Database Replica 1 — Reader]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[Database Replica 2 — Reader]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F[Orchestrator Topology Manager] --&gt; C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|Admin API Hooks| B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice-githubs-orchestrator-paired-with-a-proxy-layer&quot;&gt;In Practice: GitHub’s Orchestrator, Paired with a Proxy Layer&lt;/h2&gt;
&lt;p&gt;The documented pattern for extreme HA survival relies on deploying an intelligent database proxy between the application fleet and the database layer, governed by an autonomous topology manager.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;GitHub’s engineering team&lt;/strong&gt; authored and open-sourced the definitive solution to this problem: &lt;strong&gt;Orchestrator&lt;/strong&gt;. GitHub runs a massive fleet of MySQL servers. When a primary database hardware node died, they could not afford the downtime of manual intervention or DNS TTL propagation.&lt;/p&gt;
&lt;p&gt;They built Orchestrator to act as the “brain” of the MySQL topology. Orchestrator continuously discovers, maps, and monitors the replication state of every MySQL node. When a primary fails, Orchestrator detects the failure, calculates the best replica to promote (based on replication lag and configuration) faster than DNS-based approaches can react — I have not independently verified a specific sub-15-second figure against current GitHub documentation, so treat that number as illustrative rather than a cited benchmark.&lt;/p&gt;
&lt;p&gt;However, promoting a new database doesn’t magically update the applications. To solve the traffic routing, GitHub’s own documented architecture pairs Orchestrator with GLB (GitHub’s load balancer) and HAProxy, not ProxySQL specifically. &lt;strong&gt;ProxySQL is the wider industry’s equivalent pattern&lt;/strong&gt;: many MySQL operators pair Orchestrator’s topology decisions with a SQL-aware proxy tier so application connections survive a failover. The mechanics below describe that general ProxySQL pattern, not a GitHub-specific implementation detail.&lt;/p&gt;
&lt;p&gt;ProxySQL acts as a connection multiplexer and query router. In this architecture, the application never connects directly to the database. It connects to ProxySQL. ProxySQL maintains two separate pools of connections: one pool facing the application (frontend) and one pool facing the databases (backend).&lt;/p&gt;
&lt;p&gt;When Orchestrator detects a failure and promotes a new primary, it immediately fires a web hook to the ProxySQL Admin API. Orchestrator tells ProxySQL, “Node A is dead; Node B is the new writer.” ProxySQL updates its internal hostgroups in milliseconds.&lt;/p&gt;
&lt;h2 id=&quot;surviving-the-connection-storm&quot;&gt;Surviving the Connection Storm&lt;/h2&gt;
&lt;p&gt;Because the application is connected to ProxySQL, its frontend TCP connections are never dropped. During the 10-second window while Orchestrator promotes the new database, ProxySQL simply holds the incoming write queries in a queue.&lt;/p&gt;
&lt;p&gt;Once Orchestrator informs ProxySQL of the new writer, ProxySQL flushes the queued queries to the new primary. The application experiences a slight latency bump (e.g., a query taking 5 seconds instead of 5 milliseconds) rather than a hard &lt;code&gt;Connection Refused&lt;/code&gt; exception. The failover is entirely transparent to the application code.&lt;/p&gt;
&lt;p&gt;Furthermore, ProxySQL mathematically solves the connection storm problem through multiplexing. Even if 50 application pods open 5,000 frontend connections to ProxySQL, ProxySQL might only open 200 persistent backend connections to the MySQL database. It rapidly routes queries from the 5,000 idle frontend connections through the 200 active backend connections.&lt;/p&gt;
&lt;p&gt;When the new primary is promoted, it only has to absorb 200 connection handshakes from ProxySQL, protecting the database engine from connection thrashing and CPU saturation. To prevent cascading failures, Orchestrator implements “anti-flapping” policies, ensuring that if the new primary is unstable, it won’t repeatedly trigger automated failovers that destabilize the entire fleet.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Topology&lt;/th&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Direct DNS Failover&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Application caches old IP address; writes fail against demoted primary.&lt;/td&gt;&lt;td&gt;Implement ProxySQL or smart drivers; stop relying on DNS TTLs for database HA.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;ProxySQL Tier&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;ProxySQL becomes the single point of failure (SPOF) and crashes under load.&lt;/td&gt;&lt;td&gt;Deploy ProxySQL as a highly available cluster or run it as a sidecar alongside application pods.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Connection Storms&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Application reconnect logic lacks jitter, DDOSing the database during recovery.&lt;/td&gt;&lt;td&gt;Use connection multiplexing via proxy; enforce exponential backoff with jitter in application code.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Prepared Statements&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Proxy multiplexing breaks session state or server-side prepared statements.&lt;/td&gt;&lt;td&gt;Configure ProxySQL to appropriately map session variables or use client-side prepared statements.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Direct application-to-database connections guarantee application errors during database HA failovers and risk connection storms that can crash newly promoted primary databases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Introduce an intelligent proxy tier (ProxySQL) paired with a topology manager (Orchestrator) to decouple application connection pools from physical database instances.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; GitHub Engineering explicitly documented the creation and open-sourcing of Orchestrator to achieve fast, low-downtime failovers across one of the world’s largest MySQL deployments; pairing that topology manager with a SQL-aware proxy (GitHub uses GLB/HAProxy, many other operators use ProxySQL) is the documented industry pattern for making the failover transparent to applications.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit your application’s connection pool settings. Deploy ProxySQL in a staging environment and perform a forced failover (e.g., &lt;code&gt;kill -9&lt;/code&gt; the primary database process) to verify that your application only experiences latency spikes, not hard errors or dropped connections.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category><category>cloud</category></item><item><title>Redis Licensing and Valkey: What Engineers Should Know</title><link>https://rajivonai.com/blog/2024-05-13-redis-licensing-valkey-what-engineers-should-know/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-05-13-redis-licensing-valkey-what-engineers-should-know/</guid><description>In March 2024, Redis Ltd changed Redis 7.4+ to a non-OSS license. Here is what that actually means for your deployment — and what Valkey is.</description><pubDate>Mon, 13 May 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The Redis license change affects far fewer engineers than the headlines implied — but the engineers it does affect have real decisions to make.&lt;/strong&gt; In March 2024, Redis Ltd relicensed Redis 7.4 and later versions from BSD to a dual SSPL/RSALv2 license. The Linux Foundation forked Redis 7.2.4 — the last BSD-licensed version — into a project called Valkey. Understanding which of these events actually applies to your situation determines what, if anything, you need to do.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Redis is one of the most widely deployed in-memory data stores in the industry. It runs as a cache, a session store, a message queue, a rate limiter, and more. For most application developers, Redis is a network dependency: you point a client library at a host and port, and it works.&lt;/p&gt;
&lt;p&gt;That familiarity is also why the licensing announcement in March 2024 generated so much noise. Engineers who had never thought about Redis licensing suddenly had to decide whether to care. Most of them do not need to. But the engineers who do — platform teams managing self-hosted Redis, teams using managed services, and teams building products that bundle Redis — need a clear picture before their next infrastructure review.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The license change created a widely-shared misconception: that all Redis users are now on proprietary software and must act immediately. That is not accurate, and acting on it without understanding the scope leads to unnecessary migration work or, worse, ignored risk where it actually exists.&lt;/p&gt;
&lt;p&gt;The SSPL (Server Side Public License) is a copyleft license written by MongoDB. Its key clause is that if you offer Redis as a service to others — meaning you build a product or SaaS on top of Redis and expose it to external users — you must either open-source your entire stack or obtain a commercial license. The RSALv2 (Redis Source Available License v2) restricts using Redis in a competing database product. Neither license affects a team using Redis as an internal application dependency.&lt;/p&gt;
&lt;p&gt;The concrete failure mode is a platform team that does not audit its Redis version, does not track the managed service provider’s roadmap, and then discovers that their AWS ElastiCache clusters have been silently migrated to Valkey — or that a Redis module they depend on (RedisSearch, RedisJSON) has incomplete Valkey compatibility.&lt;/p&gt;
&lt;p&gt;The decision this forces: what is your organization’s relationship to Redis — user, operator, or distributor?&lt;/p&gt;
&lt;h2 id=&quot;what-the-license-change-actually-changes-by-role&quot;&gt;What the License Change Actually Changes by Role&lt;/h2&gt;
&lt;p&gt;The answer depends entirely on how your organization uses Redis.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Application developers using Redis as a cache or queue&lt;/strong&gt; are not affected. Your application connects to Redis over the network — you are not distributing it. Existing deployments continue to work. Redis 6.x and 7.2.x remain under BSD license.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Platform teams running self-managed Redis&lt;/strong&gt; need to make a decision, but not immediately. Redis 7.2.4 and earlier are BSD-licensed. Options: stay on 7.2.x (accepting it will eventually fall behind on security), migrate to Valkey 7.2 or 8.x, or move to a managed service. Valkey 7.2 was released by the Linux Foundation in May 2024 with backing from AWS, Google, Oracle, and Ericsson. It maintains protocol and API compatibility with Redis 7.2 — most Redis client libraries need no changes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Teams on AWS ElastiCache or GCP Memorystore&lt;/strong&gt; should check their provider’s roadmap. AWS made ElastiCache for Valkey generally available in September 2024; new clusters default to Valkey. GCP Memorystore offers both modes. Staying on the default may mean you are already running Valkey without having made an explicit decision.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Teams building a product that includes Redis&lt;/strong&gt; are in scope for the SSPL. If you expose Redis to external users as part of a service, get a legal opinion before your next release.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Role&lt;/th&gt;&lt;th&gt;License risk&lt;/th&gt;&lt;th&gt;Action&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;App developer using Redis as a dependency&lt;/td&gt;&lt;td&gt;None&lt;/td&gt;&lt;td&gt;None&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Platform team — self-managed Redis 7.2.4 or earlier&lt;/td&gt;&lt;td&gt;None immediately&lt;/td&gt;&lt;td&gt;Plan migration timeline&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Platform team — self-managed Redis 7.4+&lt;/td&gt;&lt;td&gt;SSPL applies if distributing&lt;/td&gt;&lt;td&gt;Evaluate Valkey or commercial license&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AWS ElastiCache or GCP Memorystore user&lt;/td&gt;&lt;td&gt;Provider-managed&lt;/td&gt;&lt;td&gt;Check current cluster engine version&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Product builder distributing Redis&lt;/td&gt;&lt;td&gt;SSPL applies&lt;/td&gt;&lt;td&gt;Legal review required&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Redis Ltd announced the license change on March 20, 2024. The Linux Foundation announced the Valkey fork the same day, based on Redis 7.2.4. The Valkey repository is at github.com/valkey-io/valkey.&lt;/p&gt;
&lt;p&gt;AWS made Amazon ElastiCache for Valkey generally available in September 2024, confirming that Valkey 7.2 is API- and protocol-compatible with Redis 7.2 and that existing applications required no code changes to switch. Valkey 8.0 followed in September 2024, adding features beyond the Redis 7.2 baseline.&lt;/p&gt;
&lt;p&gt;The documented pattern from this event: a fork with institutional backing can reach production stability quickly when it starts from a well-tested codebase. The Redis-to-Valkey path is cleaner than many license-driven forks because Valkey explicitly maintains the Redis Serialization Protocol (RESP) and the standard Redis command set.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;SSPL applicability confusion&lt;/td&gt;&lt;td&gt;Engineers treat SSPL as affecting all Redis users and trigger unnecessary migration projects&lt;/td&gt;&lt;td&gt;SSPL copyleft clause is narrow — it targets service providers, not application users&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Redis module dependency&lt;/td&gt;&lt;td&gt;Teams using RedisSearch, RedisJSON, or RedisTimeSeries migrate to Valkey and find incomplete or missing module support&lt;/td&gt;&lt;td&gt;Valkey compatibility with Redis modules varies; some modules are Redis Ltd proprietary and have no Valkey equivalent&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Valkey feature divergence over time&lt;/td&gt;&lt;td&gt;Applications assume long-term Redis and Valkey compatibility, but the projects diverge on new features&lt;/td&gt;&lt;td&gt;Current divergence is minimal; future compatibility depends on both projects’ roadmaps and is unknown&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Platform teams that have not audited their Redis deployments since March 2024 may be running unlicensed Redis 7.4+ in a distribution context, or may be unaware that their managed service has already migrated to Valkey.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Audit your Redis deployment: check the exact version in each environment, identify whether you are distributing Redis to external users, and confirm your managed service provider’s current engine version and roadmap.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Query &lt;code&gt;INFO server&lt;/code&gt; on a running instance — the output identifies the fork and exact version unambiguously:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;redis-cli&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; INFO&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; server&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -E&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;redis_version|redis_git|os:&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Redis:  redis_version:7.2.4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Valkey: redis_version:7.2.5  (Valkey still uses the redis_version key for compatibility)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#         valkey_version:7.2.5  (added by Valkey; absent on Redis)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, run &lt;code&gt;INFO server&lt;/code&gt; against each production Redis instance and record the version. If any are 7.4 or later, assess your distribution exposure. If you are on AWS ElastiCache, open the console and check the engine version — you may already be on Valkey and just not know it.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The license change matters for a specific set of roles, and it barely registers for everyone else. The engineers who get hurt are the ones who either ignore it completely when they shouldn’t, or treat it as a fire drill when it doesn’t apply to them. Know which situation you are in before deciding how much energy to spend.&lt;/p&gt;</content:encoded><category>databases</category><category>architecture</category></item><item><title>MySQL 8.4 LTS: What DBAs Should Check Before Upgrade</title><link>https://rajivonai.com/blog/2024-01-29-mysql-84-lts-what-dbas-should-check-before-upgrade/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-01-29-mysql-84-lts-what-dbas-should-check-before-upgrade/</guid><description>MySQL 8.4 is the first long-term support release in the 8.x line — five breaking changes that require verification before any production upgrade.</description><pubDate>Tue, 07 May 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;MySQL 8.4, released April 30, 2024, is the first long-term support release in the 8.x series and will receive extended security and bug-fix support — but the upgrade path has real breaking changes that will silently break application authentication, pagination queries, and GROUP BY logic if you do not check them first.&lt;/strong&gt; The most dangerous change is the authentication plugin enforcement. Old client libraries that do not support &lt;code&gt;caching_sha2_password&lt;/code&gt; will fail to connect after the upgrade, and the failure mode is a hard connection error, not a graceful fallback.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Oracle shipped MySQL 8.4 as the first LTS release in April 2024, consolidating changes introduced throughout the 8.x Innovation releases. MySQL 8.0 introduced &lt;code&gt;caching_sha2_password&lt;/code&gt; as the new default authentication plugin in 2018, but left &lt;code&gt;mysql_native_password&lt;/code&gt; available as a fallback. Many applications stayed on the native password plugin because connector support for &lt;code&gt;caching_sha2_password&lt;/code&gt; was uneven in the early years. In MySQL 8.4, that path is now narrower: &lt;code&gt;caching_sha2_password&lt;/code&gt; is fully enforced as the default, and &lt;code&gt;mysql_native_password&lt;/code&gt; is deprecated and disabled by default.&lt;/p&gt;
&lt;p&gt;The LTS designation matters operationally: 8.4 will receive bug fixes and security patches through a longer window than standard Innovation releases, making it the natural target for organizations that want a stable upgrade from 8.0. But “long-term support” does not mean “backward compatible with everything in 8.0.” Five specific changes require explicit verification before any production upgrade.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The authentication change is the most disruptive because it fails at connection time, before the application executes any SQL. A Django app using &lt;code&gt;mysqlclient&lt;/code&gt; 1.x, a PHP application using an outdated &lt;code&gt;mysqlnd&lt;/code&gt;, or any service using the legacy &lt;code&gt;mysql-connector-python&lt;/code&gt; without SHA-2 support will fail to connect to a MySQL 8.4 server where user accounts are configured with the new default plugin.&lt;/p&gt;
&lt;p&gt;Beyond authentication, MySQL 8.4 removes two features that appear in more production codebases than most DBAs realize: &lt;code&gt;SQL_CALC_FOUND_ROWS&lt;/code&gt; and the associated &lt;code&gt;FOUND_ROWS()&lt;/code&gt; function, which are commonly used for pagination. Applications that use &lt;code&gt;SELECT SQL_CALC_FOUND_ROWS * FROM table WHERE ... LIMIT 20&lt;/code&gt; to get both the page results and the total row count in one query will encounter a syntax error after the upgrade. How can engineering teams ensure their applications survive the transition to MySQL 8.4 LTS?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;The core concept for a safe MySQL 8.4 upgrade is a pre-flight verification checklist that audits client connector capabilities, application query patterns, and server configuration prior to the cutover.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Pre-flight Check] --&gt; B[Audit Authentication]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; C[Audit Query Patterns]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; D[Audit Server Config]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[Identify Legacy Accounts]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; F[Verify SHA-2 Support]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; G[Remove SQL_CALC_FOUND_ROWS]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; H[Add Explicit ORDER BY]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; I[Enforce GTID Consistency]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; J[Audit utf8mb3 Usage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;1. Authentication plugin: caching_sha2_password enforcement&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Check which accounts still use &lt;code&gt;mysql_native_password&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; User, Host, plugin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; mysql&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;user&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; plugin &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;mysql_native_password&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For each account returned, verify the connecting client library version supports &lt;code&gt;caching_sha2_password&lt;/code&gt;. Upgrade connectors before migrating accounts. To migrate an account:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; USER&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;appuser&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; IDENTIFIED &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; caching_sha2_password &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BY&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;password&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;2. SQL_CALC_FOUND_ROWS removal&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Search application code for &lt;code&gt;SQL_CALC_FOUND_ROWS&lt;/code&gt; and &lt;code&gt;FOUND_ROWS()&lt;/code&gt;. The replacement is a separate COUNT() subquery:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Old pattern (breaks in 8.4)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SQL_CALC_FOUND_ROWS &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;active&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; FOUND_ROWS();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Replacement pattern&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; COUNT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;active&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;active&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The MySQL 8.4 release notes document this removal explicitly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. GROUP BY implicit sort behavior&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;MySQL historically returned GROUP BY results in the grouped column order as a side effect of implementation. This was not documented behavior, but applications developed against it. MySQL 8.0 already weakened this guarantee; 8.4 continues that path. Any query relying on implicit GROUP BY ordering needs an explicit ORDER BY clause added before the upgrade.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4. GTID enforcement&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;MySQL 8.4 more strongly encourages &lt;code&gt;gtid_mode=ON&lt;/code&gt; and treats GTID-related settings as preferred defaults. Verify your replication setup:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; @@gtid_mode, @@enforce_gtid_consistency;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you are on &lt;code&gt;OFF&lt;/code&gt; or &lt;code&gt;OFF_PERMISSIVE&lt;/code&gt;, test the upgrade path in staging with GTID implications in scope.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;5. utf8mb3 deprecation acceleration&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;MySQL 8.4 accelerates warnings around &lt;code&gt;utf8mb3&lt;/code&gt; (the 3-byte UTF-8 variant that MySQL labeled as &lt;code&gt;utf8&lt;/code&gt;). Any schema still using the &lt;code&gt;utf8&lt;/code&gt; alias that intends 3-byte encoding should be explicitly audited. The MySQL documentation notes that &lt;code&gt;utf8mb3&lt;/code&gt; remains functional but its deprecation path is active.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern from Oracle’s MySQL engineering team confirms that &lt;code&gt;mysql_native_password&lt;/code&gt; is officially deprecated in MySQL 8.4 and disabled by default. Based on how MySQL’s authentication handshake behaves, the server will reject connections from clients lacking SHA-2 capabilities with a fatal error, rather than falling back to older mechanisms.&lt;/p&gt;
&lt;p&gt;Oracle’s public release notes for MySQL 8.4 explicitly document the removal of &lt;code&gt;SQL_CALC_FOUND_ROWS&lt;/code&gt; and &lt;code&gt;FOUND_ROWS()&lt;/code&gt;, noting that the features were deprecated in MySQL 8.0.20 and are now entirely removed from the parser. Any application submitting these tokens will receive a syntax error.&lt;/p&gt;
&lt;p&gt;Furthermore, the behavior of MySQL’s optimizer regarding &lt;code&gt;GROUP BY&lt;/code&gt; sorting has been formally documented as non-deterministic unless an &lt;code&gt;ORDER BY&lt;/code&gt; clause is provided. Systems relying on legacy implicit sorting will observe unpredictable result sets when upgrading to the 8.4 execution engine.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Old client library without SHA-2 support&lt;/td&gt;&lt;td&gt;Hard connection failure at connect time&lt;/td&gt;&lt;td&gt;Client cannot negotiate caching_sha2_password handshake&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;SQL_CALC_FOUND_ROWS in pagination layer&lt;/td&gt;&lt;td&gt;Syntax error on execution&lt;/td&gt;&lt;td&gt;Function removed from MySQL 8.4 parser&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Implicit GROUP BY ordering in report queries&lt;/td&gt;&lt;td&gt;Result order changes silently&lt;/td&gt;&lt;td&gt;Undocumented sort behavior not guaranteed in 8.4&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: The upcoming MySQL 8.4 LTS has breaking changes that fail silently or hard depending on the client library, query patterns, and schema encoding in use.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Run the authentication query to find &lt;code&gt;mysql_native_password&lt;/code&gt; accounts, search application code for &lt;code&gt;SQL_CALC_FOUND_ROWS&lt;/code&gt;, and verify connector versions before any upgrade.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Deploy to a staging environment running 8.4 with production schema and a representative set of application queries; connection failures and syntax errors surface immediately.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, run &lt;code&gt;SELECT User, Host, plugin FROM mysql.user WHERE plugin = &apos;mysql_native_password&apos;&lt;/code&gt; on any server targeted for 8.4 upgrade and cross-reference each account against the connecting application’s connector version.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The LTS designation makes 8.4 worth upgrading to — but LTS means the maintenance window is longer, not that the upgrade is risk-free. The five checks above are the difference between a smooth cutover and an unplanned rollback at 2 AM.&lt;/p&gt;</content:encoded><category>databases</category><category>checklist</category></item><item><title>Shopify-Style Multi-Tenant Commerce Databases: Isolation, Sharding, and Operational Controls</title><link>https://rajivonai.com/blog/2024-04-15-shopify-style-multi-tenant-commerce-databases-isolation-sharding-and-operational-controls/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-04-15-shopify-style-multi-tenant-commerce-databases-isolation-sharding-and-operational-controls/</guid><description>Shopify-style per-merchant sharding prevents one large tenant from turning shared commerce database infrastructure into a shared outage.</description><pubDate>Mon, 15 Apr 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The dangerous part of a multi-tenant commerce database is not that one merchant becomes large; it is that one merchant can turn shared infrastructure into a shared failure.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Commerce platforms start with an attractive database model: every shop shares one application, one schema, and one operational surface. A &lt;code&gt;shop_id&lt;/code&gt; column scopes orders, products, customers, inventory, discounts, and fulfillment state. The product team moves quickly because every feature lands once. The platform team can provision a new merchant without creating databases, queues, caches, dashboards, and backup policies for each account.&lt;/p&gt;
&lt;p&gt;That model is rational. Early in the life of a commerce platform, tenant-per-database looks cleaner on a whiteboard but expensive in practice. It multiplies migrations, connection pools, backups, schema drift, and incident response. Shared tables with strict tenant scoping are often the correct first architecture.&lt;/p&gt;
&lt;p&gt;The shift comes when the workload stops being statistically smooth. A flash sale, bot campaign, import job, app integration, or checkout burst can make one shop dominate write IOPS, row locks, cache churn, background jobs, and replication lag. The platform is still logically multi-tenant, but operationally it behaves like the largest tenant owns the database.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure mode is subtle because the schema still looks isolated. Queries include &lt;code&gt;shop_id&lt;/code&gt;. Authorization checks pass. Unit tests prove that one shop cannot read another shop’s rows. Yet the database has no idea that tenants deserve independent blast radii. A hot merchant can fill the buffer pool with its products, pin locks around its checkouts, delay replication for unrelated shops, and consume worker capacity through retries.&lt;/p&gt;
&lt;p&gt;The usual reaction is to add read replicas, indexes, queue workers, or cache layers. Those help until the shared writer, shared migration path, or shared operational runbook becomes the bottleneck. The deeper problem is that tenant isolation has been implemented as a query predicate, not as an operational control.&lt;/p&gt;
&lt;p&gt;The design question is therefore: how do you keep the developer ergonomics of a shared commerce platform while making failures, migrations, and capacity decisions tenant-aware?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;A Shopify-style answer is to treat the tenant key as both a data model primitive and an operations primitive. The platform still presents one product, one admin, and one API surface, but internally each shop maps to a pod: a bounded slice of databases, caches, queues, and runtime capacity.&lt;/p&gt;
&lt;p&gt;The pod is not just a shard. A shard answers where the rows live. A pod answers what fails together, what scales together, what is drained together, and what can be moved under operational control.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[commerce request — shop context required] --&gt; B[tenant resolver — authenticated shop id]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[routing catalog — shop id to pod]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D[pod boundary — app workers and caches]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; E[writer shard — shop owned tables]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; F[replica set — guarded reads]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; G[async jobs — tenant scoped queues]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; H[CDC stream — logical table topics]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; I[control plane — shard moves and kill switches]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The request path must resolve tenant identity before touching application state. That identity chooses the pod, the writer shard, the replica policy, cache namespace, job routing, and operational limits. Once the request enters the pod, every downstream system should still carry the tenant context. The architecture should assume that missing tenant context is a production bug, not a convenience.&lt;/p&gt;
&lt;p&gt;The control plane is the important part. It owns the routing catalog, tenant placement, shard movement, read routing policy, throttles, and emergency controls. Without that layer, sharding becomes a library call scattered through application code. With it, operators can move a hot shop, drain a pod, disable expensive background work, or pin reads to a writer during replica lag without shipping a feature change.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; Shopify publicly described reaching the point where buying a larger database server was no longer viable in 2015, then moving toward pods as an isolation model for its Rails monolith. In Shopify’s description, a pod is an isolated instance containing a MySQL shard and related datastores such as Redis and Memcached, while some infrastructure remains shared outside the pod boundary. See Shopify Engineering’s &lt;a href=&quot;https://shopify.engineering/a-pods-architecture-to-allow-shopify-to-scale&quot;&gt;“A Pods Architecture to Allow Shopify to Scale”&lt;/a&gt; and &lt;a href=&quot;https://shopify.engineering/blogs/engineering/mysql-database-shard-balancing-terabyte-scale&quot;&gt;“Shard Balancing: Moving Shops Confidently with Zero-Downtime at Terabyte-scale”&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Shopify attached &lt;code&gt;shop_id&lt;/code&gt; to shop-owned tables and used it as the sharding key, according to its shard balancing write-up. That action matters because it makes tenant placement explicit. The data model, routing layer, and operational tooling can all agree on the same unit of movement: the shop.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; Shopify’s public Rails patterns article describes Core as using a podded architecture where each pod contains a distinct subset of shops, and notes that if one pod shuts down temporarily, the other pods are not affected. That is the architectural result to target: not perfect uptime, but bounded failure. See &lt;a href=&quot;https://shopify.engineering/shopify-made-patterns-in-our-rails-apps&quot;&gt;“Shopify-Made Patterns in Our Rails Apps”&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; Sharding alone does not solve multi-tenancy. The documented pattern is that the shard key must become a control surface. Shopify’s CDC work shows the same lesson on the analytics side: their public write-up describes consuming changes from 100-plus MySQL shards and producing Kafka topics per logical table so downstream consumers did not need to understand source shard topology. See &lt;a href=&quot;https://shopify.engineering/capturing-every-change-shopify-sharded-monolith&quot;&gt;“Capturing Every Change From Shopify’s Sharded Monolith”&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The broader learning is portable: operational isolation should be designed before the first emergency shard split. If the only way to react to a noisy tenant is to add capacity to everyone, the architecture is still shared in the place that matters.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Cross-tenant reads&lt;/td&gt;&lt;td&gt;Tenant context is optional in application code&lt;/td&gt;&lt;td&gt;Require tenant resolution at request entry and enforce scoped data access helpers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hot merchant overload&lt;/td&gt;&lt;td&gt;One shop dominates writer, cache, queue, or replica capacity&lt;/td&gt;&lt;td&gt;Move the shop, throttle expensive paths, isolate queues, and set pod-level budgets&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica inconsistency&lt;/td&gt;&lt;td&gt;Reads go to lagging replicas after writes&lt;/td&gt;&lt;td&gt;Track replication lag and route sensitive reads to the writer when needed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Shard imbalance&lt;/td&gt;&lt;td&gt;Tenant growth changes after initial placement&lt;/td&gt;&lt;td&gt;Maintain shard balancing tooling and measure load by tenant, not only by database&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Global migrations stall&lt;/td&gt;&lt;td&gt;Schema changes execute across every shard at once&lt;/td&gt;&lt;td&gt;Roll out by pod, pause safely, and verify per-shard completion&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Analytics coupling&lt;/td&gt;&lt;td&gt;Downstream systems depend on physical shard layout&lt;/td&gt;&lt;td&gt;Publish logical streams that hide shard placement&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Control plane drift&lt;/td&gt;&lt;td&gt;Routing metadata differs from actual data placement&lt;/td&gt;&lt;td&gt;Treat routing changes as audited operations with validation and rollback&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The hardest breakage is cultural. Once a platform shards by tenant, product teams can no longer pretend the database is a single invisible resource. They need APIs for tenant-scoped jobs, shard-safe migrations, cross-shop reporting, and backfills. Querying across all shops becomes an explicit platform workflow, not an accidental SQL habit.&lt;/p&gt;
&lt;p&gt;That cost is worth paying only when the shared model is already creating operational risk. Premature sharding slows engineering. Late sharding turns every incident into archaeology. The right time is when the team can name the tenants, jobs, tables, and operational events that would benefit from a smaller blast radius.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Identify the top tenant-driven failure modes: write saturation, lock contention, replica lag, cache churn, job backlog, and migration duration.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Make tenant identity mandatory at the request boundary, then route data, cache, queues, and controls through a pod-aware control plane.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run failure drills by disabling a pod, forcing replica lag, moving a tenant, pausing a shard migration, and replaying CDC from one shard.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Build the smallest operational primitive first: a routing catalog that maps tenant to shard, is audited, is testable, and can be changed without redeploying application code.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>MongoDB Version Upgrade Risk Review: FCV Invariants, Catalog Schema Transitions, and Rolling Replica Set Migration</title><link>https://rajivonai.com/blog/2024-04-08-mongodb-version-upgrade-risk-review/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-04-08-mongodb-version-upgrade-risk-review/</guid><description>MongoDB version upgrades carry severe operational risk because the Feature Compatibility Version (FCV) mechanism decouples binary execution from internal on-disk data format transformations. Once FCV advances, downgrade paths permanently close. This post details the exact catalog schema changes between major versions, rolling upgrade sequencing, driver handshake wire protocols, and forensic verification gates required before committing point-of-no-return changes.</description><pubDate>Mon, 08 Apr 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A MongoDB binary upgrade does not upgrade your database. It merely replaces the executable that reads your data files. The true upgrade occurs when you execute &lt;code&gt;setFeatureCompatibilityVersion&lt;/code&gt; (FCV) — a command that irrevocably alters internal catalog schemas, modifies system collections, changes WiredTiger table metadata formats, and rewires the replication oplog entry encoding. Upgrading the &lt;code&gt;mongod&lt;/code&gt; binary from 5.0 to 6.0 while leaving FCV at 5.0 allows instantaneous rollback: shut down the 6.0 binary, start the 5.0 binary, and the replica set resumes without incident. The instant you execute &lt;code&gt;{setFeatureCompatibilityVersion: &quot;6.0&quot;}&lt;/code&gt;, internal data structures are modified to formats that 5.0 binaries cannot parse, and the rollback path closes permanently. From that millisecond onward, recovery from an upgrade-induced bug is not a rollback operation; it is a multi-hour disaster recovery restore from backup.&lt;/p&gt;
&lt;p&gt;Treating a major version upgrade as a standard package deployment is among the most catastrophic misconceptions in database operations. A production-grade MongoDB upgrade requires a phased, multi-gate transition that verifies driver wire protocols, audits query plan cache compatibility, tests deprecated aggregation stages, and validates replication consensus before crossing the FCV threshold.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;MongoDB’s storage engine and consensus architecture enforce strict major-version sequentiality: upgrades cannot skip major releases. Migrating an infrastructure fleet from MongoDB 4.4 to 7.0 requires traversing three sequential version boundaries: 4.4 → 5.0 → 6.0 → 7.0. Each hop involves a complete rolling binary upgrade across all replica set members or sharded cluster tiers, followed by a separate FCV advancement.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Upgrade Boundary&lt;/th&gt;&lt;th&gt;Binary Compatibility State&lt;/th&gt;&lt;th&gt;On-Disk Metadata Format&lt;/th&gt;&lt;th&gt;Rollback Method&lt;/th&gt;&lt;th&gt;Primary Failure Vector&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Binary Upgrade (Step 1)&lt;/td&gt;&lt;td&gt;Mixed-version binaries in replica set; older FCV active&lt;/td&gt;&lt;td&gt;Unchanged legacy catalog schema; no new feature metadata written&lt;/td&gt;&lt;td&gt;Stop new binary, start old binary on secondary; failover if needed&lt;/td&gt;&lt;td&gt;Driver wire protocol rejection, TLS handshake negotiation failure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Intermediate Soak (Step 2)&lt;/td&gt;&lt;td&gt;All nodes running target binary; FCV held at N-1 version&lt;/td&gt;&lt;td&gt;Legacy catalog format with new binary execution engine&lt;/td&gt;&lt;td&gt;Clean binary downgrade to previous minor version across all members&lt;/td&gt;&lt;td&gt;Latent memory leaks, query planner regression, connection pool leaks&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;FCV Advancement (Step 3)&lt;/td&gt;&lt;td&gt;Target binary active; FCV matches target binary&lt;/td&gt;&lt;td&gt;Internal collections rewritten (e.g., config, system, change stream tokens)&lt;/td&gt;&lt;td&gt;&lt;strong&gt;None.&lt;/strong&gt; Binary downgrade blocked by startup assertion. Full restore required&lt;/td&gt;&lt;td&gt;Uncaught deprecated query operators, index build assertion crashes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Post-FCV Feature Adoption (Step 4)&lt;/td&gt;&lt;td&gt;Target binary; target FCV; new engine features unlocked&lt;/td&gt;&lt;td&gt;Modern WiredTiger block formats, time-series bucketing, compressed indexes&lt;/td&gt;&lt;td&gt;Forward-only patching or disaster recovery restore from pre-FCV snapshot&lt;/td&gt;&lt;td&gt;Unbounded WiredTiger cache allocation, background chunk migration stalls&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The operational environment during an upgrade is uniquely fragile. In a replica set, members run heterogeneous binary versions during the rolling replacement. A three-node replica set transitioning from 5.0 to 6.0 temporarily operates with two 6.0 secondaries and one 5.0 primary before the planned stepdown. The consensus protocol, heartbeats, and replication oplog streaming must maintain strict backward-compatible wire serialization across different compiled codebases.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure modes of MongoDB version upgrades stem from the interaction between client driver wire protocols, aggregation engine parser changes, and internal WiredTiger catalog locks.&lt;/p&gt;
&lt;p&gt;First, &lt;strong&gt;driver wire protocol deprecation&lt;/strong&gt;. MongoDB servers negotiate communication via OP_MSG wire protocol packets. When a major server version drops legacy command structures or alters wire authentication handshakes (such as SCRAM-SHA-256 requirements or dropping legacy OP_QUERY mechanisms), older application drivers fail during connection pooling handshakes. An application pod deploying a connection to a freshly stepped-up 6.0 primary can experience connection starvation if the client driver lacks support for modern server hello commands.&lt;/p&gt;
&lt;p&gt;Second, &lt;strong&gt;deprecated and removed query operators&lt;/strong&gt;. MongoDB versions frequently deprecate or alter aggregation pipeline stages and query operators. For example, moving from 5.0 to 6.0 alters &lt;code&gt;$where&lt;/code&gt; JavaScript execution sandbox permissions, drops legacy map-reduce commands entirely, and enforces strict schema validation on previously permissive pipeline stages. If application services contain un-audited query shapes that invoke removed commands, those queries do not degrade gracefully — they return immediate fatal command errors to the client.&lt;/p&gt;
&lt;p&gt;Third, &lt;strong&gt;the FCV irreversible trap&lt;/strong&gt;. When &lt;code&gt;setFeatureCompatibilityVersion&lt;/code&gt; executes, the primary acquires exclusive locks across the administrative catalog, updates &lt;code&gt;admin.system.version&lt;/code&gt;, and begins writing new oplog entry structures. If an application bug or performance regression emerges &lt;em&gt;after&lt;/em&gt; FCV has been bumped, attempting to start an older binary against the data files will result in fatal startup assertions:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Fatal assertion 28578 at src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp: Cannot start mongod with older binary version against an active FeatureCompatibilityVersion of 6.0.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The database process immediately terminates with an unrecoverable exit code.&lt;/p&gt;
&lt;p&gt;How do you architect a zero-downtime, fully verified rolling upgrade pipeline across replica sets and sharded clusters that validates every layer of the stack — drivers, consensus, query plans, and storage formats — while keeping the rollback window open until stability is empirically proven?&lt;/p&gt;
&lt;h2 id=&quot;the-decoupled-binary-and-fcv-upgrade-pipeline&quot;&gt;The Decoupled Binary and FCV Upgrade Pipeline&lt;/h2&gt;
&lt;p&gt;The architecture that guarantees safe version progression separates physical binary deployment from logical format commitment. The system enforces an explicit 48-hour soak period between the binary rollout and the FCV advancement.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Pre-Flight Audit — Static Code Scan &amp;#x26; Driver Matrix] --&gt; B{Driver Compatible?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|No| C[Deploy Updated Client Drivers First]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|Yes| D[Trigger Rolling Binary Upgrade on Secondaries]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[Monitor Secondary Replication Lag &amp;#x26; WiredTiger Cache]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[Graceful Primary Stepdown — Stepdown Window: 60s]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Upgrade Final Node — Former Primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[Enter 48-Hour Soak Gate — FCV Held at N-1]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I{Regression Detected During Soak?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|Yes| J[Roll Back Binaries — Fast Path Available]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|No| K[Execute setFeatureCompatibilityVersion — Irreversible Gate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; L[Post-Upgrade Verification &amp;#x26; New Feature Adoption]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;replica-set-rolling-topology&quot;&gt;Replica Set Rolling Topology&lt;/h3&gt;
&lt;p&gt;In a production replica set (e.g., &lt;code&gt;rs0&lt;/code&gt; consisting of Primary &lt;code&gt;mongo-01&lt;/code&gt;, Secondary &lt;code&gt;mongo-02&lt;/code&gt;, and Secondary &lt;code&gt;mongo-03&lt;/code&gt;), the upgrade proceeds strictly from the lowest-priority secondary upward.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Secondary Drain and Binary Replacement&lt;/strong&gt;: &lt;code&gt;mongo-03&lt;/code&gt; is isolated. Traffic is shifted away using driver read preferences. The service is stopped, packages updated, and the instance restarted on the new binary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replication Catch-up and Heartbeat Verification&lt;/strong&gt;: &lt;code&gt;mongo-03&lt;/code&gt; reconnects to the legacy 5.0 primary. The 6.0 secondary processes the 5.0 oplog stream via backward-compatible deserialization. The node must reach &lt;code&gt;SECONDARY&lt;/code&gt; state with zero replication lag (&lt;code&gt;rs.status().members[i].optimeDate&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Secondary Replication Repeat&lt;/strong&gt;: The process repeats on &lt;code&gt;mongo-02&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Coordinated Primary Stepdown&lt;/strong&gt;: The primary (&lt;code&gt;mongo-01&lt;/code&gt;) is stepped down via &lt;code&gt;replSetStepDown&lt;/code&gt;. One of the upgraded secondaries is elected primary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Former Primary Upgrade&lt;/strong&gt;: &lt;code&gt;mongo-01&lt;/code&gt; is upgraded on the secondary path, rejoining the set as a secondary.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;At this point, all three nodes run the new 6.0 binary, but the entire cluster operates with FCV set to &lt;code&gt;5.0&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Executing this workflow requires precise operational telemetry, copy-pasteable administrative commands, and automated pre-flight assertion checks.&lt;/p&gt;
&lt;h3 id=&quot;pre-flight-driver-and-query-plan-audit&quot;&gt;Pre-Flight Driver and Query Plan Audit&lt;/h3&gt;
&lt;p&gt;Before initiating the rolling binary upgrade, run this forensic validation script via &lt;code&gt;mongosh&lt;/code&gt; on the primary to detect connected client driver versions and identify active operations that could block stepdown:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Audit connected drivers across all client connection pools&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;currentOp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).inprog.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;reduce&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;acc&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (op.clientMetadata &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; op.clientMetadata.driver) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; driverName&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; op.clientMetadata.driver.name;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; driverVer&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; op.clientMetadata.driver.version;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; key&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; `${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;driverName&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} @ ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;driverVer&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    acc[key] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (acc[key] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; acc;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}, {});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Verify current Feature Compatibility Version&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; fcvDoc&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;adminCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ getParameter: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, featureCompatibilityVersion: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Current Active FCV: ${&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;JSON&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;stringify&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;fcvDoc&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;featureCompatibilityVersion&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Confirm replica set state and sync source stability&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; rsStatus&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rs.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;rsStatus.members.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Node: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} | State: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;stateStr&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} | Uptime: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;uptime&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}s | Lag: ${&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; Date&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; Date&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;optimeDate&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  }s`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;forensic-deprecated-command-static-scanner&quot;&gt;Forensic Deprecated Command Static Scanner&lt;/h3&gt;
&lt;p&gt;Run this shell script across your application repositories and CI/CD pipelines to detect removed or breaking aggregation operators and database commands prior to deployment:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/usr/bin/env bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -euo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pipefail&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TARGET_DIR&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;${1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:-&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;src&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;=== Scanning codebase in ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TARGET_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} for deprecated MongoDB operators ===&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Check for dropped mapReduce usage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Auditing for mapReduce calls...&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -rEn&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;mapReduce\s*\(&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TARGET_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;  Clean: No mapReduce calls found.&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Check for legacy $where JavaScript operations&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Auditing for &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\$&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;where evaluation blocks...&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -rEn&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\$&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;where\s*:&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TARGET_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;  Clean: No &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\$&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;where operators found.&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Check for legacy collection dropIndex syntax&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Auditing for legacy index management signatures...&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -rEn&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;dropIndex\s*\(\s*\[&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TARGET_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;  Clean: No legacy dropIndex array patterns.&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Check for unparameterized raw string queries&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Auditing for raw text query execution...&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -rEn&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;eval\s*\(&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TARGET_DIR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;  Clean: No db.eval commands found.&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;=== Static Audit Complete ===&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;executing-the-safe-rolling-stepdown-sequence&quot;&gt;Executing the Safe Rolling Stepdown Sequence&lt;/h3&gt;
&lt;p&gt;When transitioning the primary, standard termination will trigger an ungraceful election and client write exceptions. Use a bounded stepdown sequence with secondary catch-up enforcement:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Run on the Primary (mongo-01)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Step 1: Ensure all secondaries are within 10 seconds of oplog sync&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rs.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; primaryOptime&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; status.members.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.self).optime.ts;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;status.members.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;m.self).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;sec&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; lagSeconds&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; primaryOptime.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getTime&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sec.optime.ts.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getTime&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (lagSeconds &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    throw&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; Error&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Secondary ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;sec&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} has excessive lag: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;lagSeconds&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}s. Aborting stepdown.`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Step 2: Step down primary with a 60-second election timeout and 30-second catchup window&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Initiating controlled stepdown...&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;adminCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  replSetStepDown: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;60&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  secondaryCatchUpPeriodSecs: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;30&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  force: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;the-48-hour-fcv-gate-and-confirmation&quot;&gt;The 48-Hour FCV Gate and Confirmation&lt;/h3&gt;
&lt;p&gt;During the 48-hour soak period, monitor the cluster closely using Linux diagnostic tooling and internal database metrics. Run &lt;code&gt;iostat -xz 1&lt;/code&gt; to observe disk queue saturation and track WiredTiger dirty cache ratios.&lt;/p&gt;
&lt;p&gt;Once stability is proven, execute the point-of-no-return FCV transition:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Step 1: Verify all nodes report healthy and identical optimes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; conf&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rs.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;conf&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; preCheck&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rs.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; uncommittedMembers&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; preCheck.members.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.stateStr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;PRIMARY&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; m.stateStr &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;SECONDARY&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (uncommittedMembers.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  throw&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; Error&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Cluster contains unhealthy members. FCV upgrade rejected.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Step 2: Advance Feature Compatibility Version to 6.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Advancing FCV to 6.0...&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; result&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;adminCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ setFeatureCompatibilityVersion: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;6.0&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;printjson&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(result);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Step 3: Validate persistent catalog confirmation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; postFCV&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;adminCommand&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ getParameter: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, featureCompatibilityVersion: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (postFCV.featureCompatibilityVersion.version &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;6.0&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  throw&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; Error&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;FCV transition failed to persist in admin.system.version!&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;FCV advancement successfully committed. Downgrade window is now CLOSED.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Root Cause&lt;/th&gt;&lt;th&gt;Production Blast Radius&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Startup Crash Loop via Premature FCV&lt;/td&gt;&lt;td&gt;Binary downgrade attempted after &lt;code&gt;setFeatureCompatibilityVersion&lt;/code&gt; was executed on modern binaries&lt;/td&gt;&lt;td&gt;Complete cluster failure: all downgraded nodes crash on startup with assertion error 28578; replication halts&lt;/td&gt;&lt;td&gt;Enforce mandatory 48-hour soak with FCV held at N-1. If binary bugs occur, downgrade binaries immediately before touching FCV. Post-FCV recovery requires full snapshot restore&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Client Driver Wire Protocol Rejection&lt;/td&gt;&lt;td&gt;Outdated client driver lacks support for &lt;code&gt;hello&lt;/code&gt; command or modern OP_MSG wire serialization&lt;/td&gt;&lt;td&gt;Application connection pools exhaust on startup; mass connection drops and authentication handshake failures&lt;/td&gt;&lt;td&gt;Audit all application dependencies against the official driver compatibility matrix before touching database hosts; verify via &lt;code&gt;db.currentOp()&lt;/code&gt; client metadata&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Uncaught Removed Aggregation Operators&lt;/td&gt;&lt;td&gt;Target version removes legacy query operators (e.g., &lt;code&gt;mapReduce&lt;/code&gt;, specific &lt;code&gt;$where&lt;/code&gt; functions)&lt;/td&gt;&lt;td&gt;Microservices executing specific reporting or data transformation paths fail with runtime syntax errors&lt;/td&gt;&lt;td&gt;Execute comprehensive integration test suites in staging; run static AST scanners against application codebases to identify and replace deprecated operators prior to binary rollout&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replication Stalls During Mixed-Version State&lt;/td&gt;&lt;td&gt;Secondary running modern binary encounters incompatible index build format or collations from older primary&lt;/td&gt;&lt;td&gt;Secondary drops out of sync; replication lag grows unbounded; eventual out-of-sync state requiring full resync&lt;/td&gt;&lt;td&gt;Run rolling upgrades during off-peak maintenance windows; monitor &lt;code&gt;oplogLag&lt;/code&gt; continuously; temporarily disable background indexing jobs during the rolling binary phase&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Primary Stepdown Election Storm&lt;/td&gt;&lt;td&gt;&lt;code&gt;replSetStepDown&lt;/code&gt; executed while secondaries have diverging election priorities or high replication lag&lt;/td&gt;&lt;td&gt;Multiple successive elections occur; write availability blocked for 30–90 seconds; application write timeout cascades&lt;/td&gt;&lt;td&gt;Enforce &lt;code&gt;secondaryCatchUpPeriodSecs: 30&lt;/code&gt; in the stepdown command; verify that priority 1 secondaries have identical specs and network latency; verify zero optime lag before stepdown&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sharded Cluster Component Upgrade Misordering&lt;/td&gt;&lt;td&gt;Upgrading shards before config server replica sets or upgrading &lt;code&gt;mongos&lt;/code&gt; routers after shard FCV advancement&lt;/td&gt;&lt;td&gt;Metadata sync failure: &lt;code&gt;mongos&lt;/code&gt; routers crash or route chunk queries incorrectly; catalog cache corruption&lt;/td&gt;&lt;td&gt;Strictly adhere to component sequence: 1. Deploy binaries to &lt;code&gt;mongos&lt;/code&gt; routers; 2. Upgrade Config Server replica set; 3. Upgrade Shard replica sets; 4. Advance FCV on Config Servers; 5. Advance FCV on Shards&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;WiredTiger Storage Cache Exhaustion Post-Upgrade&lt;/td&gt;&lt;td&gt;New version changes default internal cache allocation or increases memory consumption per connection&lt;/td&gt;&lt;td&gt;Node memory runs out; Linux OOM killer sends SIGKILL to &lt;code&gt;mongod&lt;/code&gt;; catastrophic crash during failover&lt;/td&gt;&lt;td&gt;Review &lt;code&gt;storage.wiredTiger.engineConfig.cacheSizeGB&lt;/code&gt; settings; ensure it is explicitly set to 50–60% of available host RAM, leaving sufficient overhead for the OS page cache and connection threads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unverified Snapshot Backups&lt;/td&gt;&lt;td&gt;Storage snapshot taken during active mixed-version binary state without taking a point-in-time database lock&lt;/td&gt;&lt;td&gt;Restored snapshot contains torn pages or inconsistent metadata catalogs; secondary fails initial sync&lt;/td&gt;&lt;td&gt;Take explicit filesystem or volume snapshots immediately &lt;em&gt;before&lt;/em&gt; initiating the binary upgrade, and again immediately &lt;em&gt;before&lt;/em&gt; executing the irreversible FCV advancement&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;2024-upgrade-pipeline-and-tooling-update&quot;&gt;2024 Upgrade Pipeline and Tooling Update&lt;/h2&gt;
&lt;p&gt;In modern enterprise architectures, manual command-line upgrades are increasingly superseded by declarative Kubernetes operators and automated control planes. The MongoDB Community Kubernetes Operator and enterprise Atlas architectures implement native blue-green and canary rolling mechanisms that automate the replica set stepdown sequence.&lt;/p&gt;
&lt;p&gt;Under modern 2024 deployments, the operator manages pod disruption budgets (PDBs) to ensure that at no point does a replica set lose quorum. If a new secondary pod fails health checks or exhibits crashing behavior within its readiness probe window, the operator halts the rolling deployment automatically before the primary is ever touched.&lt;/p&gt;
&lt;p&gt;Furthermore, MongoDB’s automated cluster analyzers and schema telemetry now integrate directly with Prometheus alerts. By exporting &lt;code&gt;mongodb_ss_asserts&lt;/code&gt; and tracking &lt;code&gt;mongodb_ss_opcounters&lt;/code&gt;, infrastructure teams can automatically gate FCV advancement in CI/CD pipelines: an automated workflow polls the cluster over a 48-hour soak window, automatically executing &lt;code&gt;setFeatureCompatibilityVersion&lt;/code&gt; only if zero fatal assertions and zero query parser exceptions are recorded during that timeframe.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; MongoDB version upgrades carry severe operational risk because advancing the Feature Compatibility Version permanently restructures on-disk metadata catalogs, eliminating binary rollback paths and turning application regressions into disaster recovery events.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Formally decouple the physical binary upgrade from the logical FCV advancement. Execute a rolling replica set upgrade across secondaries first, conduct controlled primary stepdowns with secondary catch-up enforcement, and mandate a 48-hour soak period prior to FCV advancement.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; MongoDB source code assertions verify that older server binaries cannot start against an on-disk catalog where FCV matches a higher release; once committed, the downgrade barrier is physically immutable at the storage engine level.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt;
&lt;ol&gt;
&lt;li&gt;Audit all application repositories for deprecated query operators and obsolete driver versions using the static scanner script provided.&lt;/li&gt;
&lt;li&gt;Take clean, verified storage snapshots of all cluster volumes prior to initiating binary deployment.&lt;/li&gt;
&lt;li&gt;Execute rolling secondary upgrades and verify zero replication lag before stepping down the primary.&lt;/li&gt;
&lt;li&gt;Hold FCV at N-1 for a minimum of 48 hours to validate production query patterns before running &lt;code&gt;setFeatureCompatibilityVersion&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;MongoDB Manual — Major Version Upgrade Procedures and FCV Constraints: &lt;a href=&quot;https://www.mongodb.com/docs/manual/release-notes/&quot;&gt;https://www.mongodb.com/docs/manual/release-notes/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — Feature Compatibility Version (FCV) Reference: &lt;a href=&quot;https://www.mongodb.com/docs/manual/reference/command/setFeatureCompatibilityVersion/&quot;&gt;https://www.mongodb.com/docs/manual/reference/command/setFeatureCompatibilityVersion/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — Replica Set Rolling Upgrade Mechanics and Stepdown Sequencing: &lt;a href=&quot;https://www.mongodb.com/docs/manual/tutorial/upgrade-revision/&quot;&gt;https://www.mongodb.com/docs/manual/tutorial/upgrade-revision/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Drivers Compatibility Matrix and Wire Protocol Specifications: &lt;a href=&quot;https://www.mongodb.com/docs/drivers/driver-compatibility-reference/&quot;&gt;https://www.mongodb.com/docs/drivers/driver-compatibility-reference/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;WiredTiger Storage Engine Architecture — Metadata and Catalog Invariants: &lt;a href=&quot;https://source.wiredtiger.com/develop/arch-schema.html&quot;&gt;https://source.wiredtiger.com/develop/arch-schema.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Kubernetes Operator — Automated Upgrade and Rolling Update Controllers: &lt;a href=&quot;https://github.com/mongodb/mongodb-kubernetes-operator&quot;&gt;https://github.com/mongodb/mongodb-kubernetes-operator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;USENIX ATC — Consistency and Consensus in Distributed Document Databases: &lt;a href=&quot;https://www.usenix.org/conference/atc20/&quot;&gt;https://www.usenix.org/conference/atc20/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>checklist</category><category>architecture</category><category>failures</category></item><item><title>Can You Run Citus on Amazon RDS for PostgreSQL? A Reality Check</title><link>https://rajivonai.com/blog/2024-03-19-can-you-run-citus-amazon-rds-postgresql/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-03-19-can-you-run-citus-amazon-rds-postgresql/</guid><description>Citus is not a supported RDS PostgreSQL extension; this guide explains why pg_partman, logical replication, and foreign data wrappers do not replace it, and which write-scaling architectures are valid.</description><pubDate>Tue, 19 Mar 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;If an Amazon RDS for PostgreSQL architecture depends on running &lt;code&gt;CREATE EXTENSION citus&lt;/code&gt;, stop before estimating nodes, shards, or throughput. Citus is not in the current RDS extension matrix, and an RDS administrator cannot install the missing native package.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;RetailCo’s order platform began on one PostgreSQL database. Years later, its transaction history is 40 TB, checkout writes compete with operational queries, and the largest RDS instance no longer provides comfortable peak headroom.&lt;/p&gt;
&lt;p&gt;Most orders are tenant-local. An order, its items, payment state, and shipment belong to one &lt;code&gt;tenant_id&lt;/code&gt;. That makes Citus attractive: distribute tenant rows across workers, colocate related tables, and let several PostgreSQL servers own different writes.&lt;/p&gt;
&lt;p&gt;The platform team also has a firm managed-service requirement. Its first proposed architecture tries to combine both preferences:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App[RetailCo applications] --&gt; Coordinator[RDS PostgreSQL coordinator]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coordinator --&gt; WorkerA[RDS PostgreSQL worker A]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coordinator --&gt; WorkerB[RDS PostgreSQL worker B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coordinator --&gt; WorkerC[RDS PostgreSQL worker C]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Extension[Citus extension] --&gt; Coordinator&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Extension --&gt; WorkerA&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Extension --&gt; WorkerB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Extension --&gt; WorkerC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The diagram looks plausible. Each node is PostgreSQL. RDS supports PostgreSQL extensions. The database owner receives an &lt;code&gt;rds_superuser&lt;/code&gt; role. AWS even supports extensions such as &lt;code&gt;pg_partman&lt;/code&gt;, &lt;code&gt;pglogical&lt;/code&gt;, and &lt;code&gt;postgres_fdw&lt;/code&gt; on applicable engine versions.&lt;/p&gt;
&lt;p&gt;But the proposed architecture fails at its first prerequisite.&lt;/p&gt;
&lt;p&gt;As of this article’s March 31, 2026 documentation cutoff, AWS’s &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/PostgreSQLReleaseNotes/postgresql-extensions.html&quot;&gt;RDS for PostgreSQL extension matrix&lt;/a&gt; does not list Citus. AWS instructs customers to use &lt;code&gt;SHOW rds.extensions&lt;/code&gt; to see the extensions supported by a specific engine version. If an extension is not supplied for that engine, granting another database role does not make its files appear on the server.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The misunderstanding begins with the word &lt;em&gt;extension&lt;/em&gt;. PostgreSQL extensions do not all have the same deployment requirements.&lt;/p&gt;
&lt;p&gt;Some are primarily SQL objects. Others include compiled code, server hooks, background processes, control files, and shared libraries. PostgreSQL’s own &lt;a href=&quot;https://www.postgresql.org/docs/current/extend-extensions.html&quot;&gt;extension packaging documentation&lt;/a&gt; explains that &lt;code&gt;CREATE EXTENSION&lt;/code&gt; loads objects from files already installed on the database server; extensions containing C code typically include a shared library.&lt;/p&gt;
&lt;p&gt;Citus is in the second category. Its installation procedure installs an operating-system package and configures &lt;code&gt;shared_preload_libraries = &apos;citus&apos;&lt;/code&gt; before creating the extension. Citus then hooks into PostgreSQL’s planner and executor to coordinate distributed work: &lt;a href=&quot;https://docs.citusdata.com/en/latest/installation/single_node_rhel.html&quot;&gt;Citus 13 installation&lt;/a&gt; and &lt;a href=&quot;https://docs.citusdata.com/en/stable/get_started/concepts.html&quot;&gt;Citus concepts&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The dependency chain is therefore:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Package[Citus operating-system package] --&gt; Files[control files and shared library]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Files --&gt; Preload[Citus loaded at server start]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Preload --&gt; Create[CREATE EXTENSION citus]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Create --&gt; Metadata[distributed-table metadata]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Metadata --&gt; Workers[coordinator routes work to workers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;RDS controls the host operating system and the PostgreSQL installation. The customer cannot add the Citus package or place its control and library files into PostgreSQL’s installation directories.&lt;/p&gt;
&lt;p&gt;AWS is explicit about the role boundary. The initial &lt;code&gt;postgres&lt;/code&gt; account on RDS has &lt;code&gt;NOSUPERUSER&lt;/code&gt; and &lt;code&gt;NOREPLICATION&lt;/code&gt;; members of &lt;code&gt;rds_superuser&lt;/code&gt; can install extensions &lt;strong&gt;supported by RDS&lt;/strong&gt;, but cannot access the host operating system or connect as PostgreSQL’s actual superuser: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.PostgreSQL.CommonDBATasks.Roles.rds_superuser.html&quot;&gt;Understanding the &lt;code&gt;rds_superuser&lt;/code&gt; role&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;That leads to the core question:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If standard RDS cannot supply the Citus runtime, which architecture solves RetailCo’s real problem without pretending a different extension is a distributed database?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;the-answer-is-an-architecture-choice-not-an-extension-workaround&quot;&gt;The Answer Is an Architecture Choice, Not an Extension Workaround&lt;/h2&gt;
&lt;p&gt;There is no supported SQL command, privilege grant, parameter-group setting, or trusted-extension shortcut that turns a standard RDS for PostgreSQL instance into a Citus coordinator or worker.&lt;/p&gt;
&lt;p&gt;The preflight check should happen before any design review:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;rds&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;extensions&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    default_version,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    installed_version&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_available_extensions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;citus&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;pg_partman&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;pglogical&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &apos;postgres_fdw&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Run this on the exact RDS PostgreSQL engine and minor version under evaluation. Do not copy expected output from another version, and do not infer availability from generic PostgreSQL documentation.&lt;/p&gt;
&lt;p&gt;The RDS parameter &lt;code&gt;rds.allowed_extensions = &apos;*&apos;&lt;/code&gt; is easy to misread. AWS documents it as allowing all extensions &lt;strong&gt;available for the RDS engine version&lt;/strong&gt;. It does not allow arbitrary packages from the PostgreSQL ecosystem: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/PostgreSQL.Concepts.General.FeatureSupport.Extensions.html&quot;&gt;Restricting installation of PostgreSQL extensions&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;why-pg_partman-does-not-replace-citus&quot;&gt;Why &lt;code&gt;pg_partman&lt;/code&gt; does not replace Citus&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;pg_partman&lt;/code&gt; is supported on RDS for PostgreSQL 12.5 and later. It automates the creation, retention, and maintenance of native PostgreSQL table partitions: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/PostgreSQL_Partitions.html&quot;&gt;Managing PostgreSQL partitions with &lt;code&gt;pg_partman&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;That can be extremely useful for a 40-TB transaction table. RetailCo could partition by month so retention, index maintenance, archival, and time-bounded queries operate on smaller physical tables.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App[application writes] --&gt; Writer[one RDS PostgreSQL writer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Writer --&gt; Parent[transactions parent table]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Parent --&gt; P1[January partition]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Parent --&gt; P2[February partition]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Parent --&gt; P3[March partition]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Parent --&gt; P4[future partitions managed by pg_partman]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Every partition still uses the same RDS DB instance’s compute, memory, WAL path, and writer. Partitioning can improve pruning and data lifecycle operations. It can reduce particular maintenance and query costs. It does not create independent PostgreSQL workers that concurrently own different tenants.&lt;/p&gt;
&lt;p&gt;The distinction is structural:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Capability&lt;/th&gt;&lt;th&gt;Native partitioning with &lt;code&gt;pg_partman&lt;/code&gt;&lt;/th&gt;&lt;th&gt;Citus distributed tables&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Splits one logical table&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Automates partition lifecycle&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Not its primary role&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Places data on independent PostgreSQL workers&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Routes by distribution column across workers&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Adds independent write compute&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Yes, when work is distributed and sufficiently local&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Requires Citus on coordinator and workers&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;code&gt;pg_partman&lt;/code&gt; is a partition-management answer. Citus is a distributed-data and distributed-compute answer. Using one name in place of the other hides the original write ceiling.&lt;/p&gt;
&lt;h3 id=&quot;why-postgres_fdw-does-not-recreate-citus&quot;&gt;Why &lt;code&gt;postgres_fdw&lt;/code&gt; does not recreate Citus&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;postgres_fdw&lt;/code&gt; lets PostgreSQL access tables stored on remote PostgreSQL servers. It can support federation, migrations, administrative access, and deliberately designed remote-table workflows: &lt;a href=&quot;https://www.postgresql.org/docs/current/postgres-fdw.html&quot;&gt;PostgreSQL &lt;code&gt;postgres_fdw&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It does not provide Citus’s distribution metadata, shard placement model, distributed-table DDL, colocation decisions, or rebalance operations. Building application-visible foreign tables across many RDS instances transfers routing, transaction design, topology changes, and failure handling to the architects and operators. That may be a valid custom federation design, but it should not be represented as “Citus on RDS.”&lt;/p&gt;
&lt;h3 id=&quot;why-pglogical-does-not-add-write-owners&quot;&gt;Why &lt;code&gt;pglogical&lt;/code&gt; does not add write owners&lt;/h3&gt;
&lt;p&gt;RDS supports &lt;code&gt;pglogical&lt;/code&gt; on applicable versions, as shown in the current extension matrix. Logical replication copies selected changes between PostgreSQL systems. Replication can support migrations, data integration, and some availability patterns; it does not automatically divide authoritative ownership of a table’s write keyspace.&lt;/p&gt;
&lt;p&gt;If two writable databases accept conflicting updates for the same logical row, the architecture now needs ownership rules, conflict behavior, sequence and identity strategy, DDL coordination, and recovery reconciliation. Installing a replication extension does not answer those questions.&lt;/p&gt;
&lt;h2 id=&quot;four-valid-paths-for-retailco&quot;&gt;Four Valid Paths for RetailCo&lt;/h2&gt;
&lt;p&gt;Once the invalid RDS-plus-Citus premise is removed, the decision becomes much clearer.&lt;/p&gt;
&lt;h3 id=&quot;path-1--stay-on-one-rds-database-and-use-native-partitioning&quot;&gt;Path 1 — Stay on one RDS database and use native partitioning&lt;/h3&gt;
&lt;p&gt;Choose this when RetailCo’s main problem is table manageability, retention, vacuum scope, index maintenance, or query pruning—and when one writer still has sufficient measured headroom.&lt;/p&gt;
&lt;p&gt;This is the lowest-migration-risk path. It preserves standard RDS operations and PostgreSQL semantics. It should not be approved as a horizontal-write solution unless the benchmark proves the original resource ceiling was caused by work that partitioning actually removes.&lt;/p&gt;
&lt;h3 id=&quot;path-2--application-shard-across-multiple-rds-databases&quot;&gt;Path 2 — Application-shard across multiple RDS databases&lt;/h3&gt;
&lt;p&gt;If the managed-service requirement is firm and &lt;code&gt;tenant_id&lt;/code&gt; is a stable ownership key, RetailCo can place tenant groups on independent RDS PostgreSQL databases:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Request[request with tenant identifier] --&gt; Catalog[tenant placement catalog]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Catalog --&gt; RdsA[RDS shard A — tenants one]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Catalog --&gt; RdsB[RDS shard B — tenants two]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Catalog --&gt; RdsC[RDS shard C — tenants three]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  RdsA --&gt; Pipeline[change-data pipeline]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  RdsB --&gt; Pipeline&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  RdsC --&gt; Pipeline&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Pipeline --&gt; Analytics[cross-tenant reporting]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This creates real independent write capacity because each database owns different tenants. When every shard is configured with the required backup and Multi-AZ policies, RDS manages the database hosts, storage, backups, and instance failover inside each shard.&lt;/p&gt;
&lt;p&gt;RetailCo must own the distributed control plane:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;authoritative tenant placement and request routing;&lt;/li&gt;
&lt;li&gt;tenant movement, validation, cutover, and rollback;&lt;/li&gt;
&lt;li&gt;schema rollout across every database;&lt;/li&gt;
&lt;li&gt;hot-tenant isolation and capacity forecasting;&lt;/li&gt;
&lt;li&gt;fleet-wide PITR and disaster-recovery procedures;&lt;/li&gt;
&lt;li&gt;cross-shard reporting and any exceptional cross-shard transaction.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is not Citus, but it is an honest managed PostgreSQL sharding architecture.&lt;/p&gt;
&lt;h3 id=&quot;path-3--run-citus-on-infrastructure-retailco-controls&quot;&gt;Path 3 — Run Citus on infrastructure RetailCo controls&lt;/h3&gt;
&lt;p&gt;If Citus behavior is the requirement, the PostgreSQL hosts must permit Citus packages and configuration. On AWS, that usually means EC2 instances or another supported Citus platform rather than standard RDS for PostgreSQL.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App[RetailCo applications] --&gt; Pool[connection pool]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Pool --&gt; Coordinator[Citus coordinator on EC2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coordinator --&gt; WorkerA[worker A on EC2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coordinator --&gt; WorkerB[worker B on EC2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Coordinator --&gt; WorkerC[worker C on EC2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Orders[orders distributed by tenant identifier] --&gt; WorkerA&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Orders --&gt; WorkerB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Orders --&gt; WorkerC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reference[small reference tables] --&gt; WorkerA&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reference --&gt; WorkerB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reference --&gt; WorkerC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Citus uses a coordinator to hold distributed metadata and plan work across workers. Distributed tables are divided into shards according to a distribution column; reference tables are copied across workers; related tables can be colocated so equal distribution-key values reach the same worker: &lt;a href=&quot;https://docs.citusdata.com/en/stable/get_started/concepts.html&quot;&gt;Citus concepts&lt;/a&gt; and &lt;a href=&quot;https://docs.citusdata.com/en/v13.0/develop/reference_ddl.html&quot;&gt;&lt;code&gt;create_distributed_table&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For RetailCo, &lt;code&gt;tenant_id&lt;/code&gt; is a stronger candidate than four-value &lt;code&gt;brand_id&lt;/code&gt;. A high-cardinality tenant key spreads ownership more evenly and makes checkout transactions local when every related table carries that key. The decision still requires measurements for tenant skew, cross-tenant queries, global constraints, and large tenants.&lt;/p&gt;
&lt;p&gt;The operational exchange is substantial. RetailCo now owns operating-system patching, PostgreSQL and Citus upgrades, coordinator availability, worker replication, backups, point-in-time recovery, node replacement, shard movement, monitoring, and failure testing. Citus documentation describes worker high availability using PostgreSQL streaming replication and notes that the coordinator contains essential metadata: &lt;a href=&quot;https://docs.citusdata.com/en/stable/admin_guide/cluster_management.html&quot;&gt;Citus cluster management&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;path-3b--if-managed-citus-is-the-actual-requirement-the-offering-is-not-on-aws&quot;&gt;Path 3b — If managed Citus is the actual requirement, the offering is not on AWS&lt;/h3&gt;
&lt;p&gt;The question in this article’s title has a narrow answer for RDS, but architects should know the complete one. Managed Citus does exist: Microsoft acquired Citus Data and operates the extension as a first-party service, currently branded &lt;strong&gt;Azure Cosmos DB for PostgreSQL&lt;/strong&gt;. It provides coordinator and worker nodes, &lt;code&gt;create_distributed_table()&lt;/code&gt;, colocation, reference tables, and the rebalancer as a managed control plane rather than as packages RetailCo installs on EC2.&lt;/p&gt;
&lt;p&gt;That reframes the decision honestly. “We want Citus without operating Citus” is not an impossible requirement — it is a requirement that selects a different cloud. For RetailCo, whose constraint is a firm AWS-managed-service preference, the relevant conclusion is that the managed-Citus option and the AWS option are mutually exclusive, and the team must decide which of the two preferences is actually binding: the extension, or the provider.&lt;/p&gt;
&lt;p&gt;Two clarifications prevent the common follow-up mistakes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Aurora PostgreSQL does not change the answer.&lt;/strong&gt; Aurora is a different storage architecture, not a more permissive extension host. Citus is absent from its supported extension list for the same reason it is absent from RDS: AWS does not ship the server-side package. “Try Aurora instead” is not a workaround.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Aurora Limitless is not managed Citus.&lt;/strong&gt; It solves an adjacent problem with a different implementation, table model, and compatibility surface, as the next path describes. Shared vocabulary — shards, distribution keys, colocation — does not imply a portable design.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;path-4--evaluate-an-aws-managed-distributed-postgresql-architecture&quot;&gt;Path 4 — Evaluate an AWS-managed distributed PostgreSQL architecture&lt;/h3&gt;
&lt;p&gt;Aurora PostgreSQL Limitless Database provides database-managed routers and shards, but it is not “Citus delivered through RDS.” It has its own table types, shard-key rules, PostgreSQL compatibility boundaries, version track, backup model, and unsupported integrations.&lt;/p&gt;
&lt;p&gt;For RetailCo’s tenant-local workload, it is the primary AWS-managed proof-of-concept candidate. The compatibility inventory and design tradeoffs are covered in &lt;a href=&quot;https://rajivonai.com/blog/2025-01-21-scaling-writes-amazon-aurora-what-works/&quot;&gt;Scaling Writes in Amazon Aurora: What Actually Works?&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If neither Citus nor Limitless fits, domain decomposition remains a valid fifth direction: separate orders, payments, inventory, and history when their transaction and recovery contracts do not need one database commit.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS publishes an extension matrix per RDS PostgreSQL engine version and tells administrators to inspect &lt;code&gt;rds.extensions&lt;/code&gt;. Citus is absent from the current matrix, while &lt;code&gt;pg_partman&lt;/code&gt;, &lt;code&gt;pglogical&lt;/code&gt;, and &lt;code&gt;postgres_fdw&lt;/code&gt; are listed for applicable versions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Make extension availability a deployment gate. Run &lt;code&gt;SHOW rds.extensions&lt;/code&gt; and query &lt;code&gt;pg_available_extensions&lt;/code&gt; on the exact target version before approving any extension-dependent architecture.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The design either proceeds with a supported prerequisite or fails before migration code, performance projections, and operating procedures are built on an impossible topology.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; An extension name on the public internet is not evidence that a managed PostgreSQL service has installed its server-side files.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS documents that &lt;code&gt;rds_superuser&lt;/code&gt; is not PostgreSQL superuser and has no host operating-system access. PostgreSQL documents that native extensions require server-installed control, script, and often shared-library files.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Classify every required extension by installation mechanism: SQL-only or native package, preload requirement, background workers, engine-version compatibility, and upgrade coupling.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The compatibility review exposes which capabilities depend on the managed provider’s build rather than on a database grant.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Privilege and software availability are different controls.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS describes &lt;code&gt;pg_partman&lt;/code&gt; as automation for PostgreSQL’s native child partitions. Citus describes workers that store distributed shards and a coordinator that routes or parallelizes queries.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; State the resource boundary for every proposed “partitioning” solution: one writer with many child tables, or multiple workers with separate compute and ownership.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The architecture review can determine whether the proposal improves lifecycle management or actually distributes write work.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Partition count is not a write-scalability metric.&lt;/p&gt;
&lt;h3 id=&quot;proposed-verification-lab--no-empirical-result-yet&quot;&gt;Proposed verification lab — no empirical result yet&lt;/h3&gt;
&lt;p&gt;This article makes no throughput claim. A useful lab should compare the architectures using the same RetailCo schema, data distribution, durability, and transaction semantics.&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Test&lt;/th&gt;&lt;th&gt;One partitioned RDS database&lt;/th&gt;&lt;th&gt;Application-sharded RDS&lt;/th&gt;&lt;th&gt;Citus on EC2&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Tenant-local checkout&lt;/td&gt;&lt;td&gt;One writer, route to native partition&lt;/td&gt;&lt;td&gt;Route tenant to owning database&lt;/td&gt;&lt;td&gt;Route tenant to owning shard placement&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hot tenant&lt;/td&gt;&lt;td&gt;Observe local lock and CPU concentration&lt;/td&gt;&lt;td&gt;Isolate or move tenant&lt;/td&gt;&lt;td&gt;Observe worker and shard skew&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cross-tenant report&lt;/td&gt;&lt;td&gt;Scan relevant local partitions&lt;/td&gt;&lt;td&gt;Fan out or use analytics pipeline&lt;/td&gt;&lt;td&gt;Distributed query through coordinator&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cross-tenant transaction&lt;/td&gt;&lt;td&gt;Local database transaction&lt;/td&gt;&lt;td&gt;Application coordination required&lt;/td&gt;&lt;td&gt;Distributed transaction path&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Add write capacity&lt;/td&gt;&lt;td&gt;Scale writer vertically&lt;/td&gt;&lt;td&gt;Add database and move tenants&lt;/td&gt;&lt;td&gt;Add worker and rebalance shards&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Recovery&lt;/td&gt;&lt;td&gt;RDS snapshot and PITR&lt;/td&gt;&lt;td&gt;Restore one shard and reconcile catalog&lt;/td&gt;&lt;td&gt;Restore coordinator metadata and worker state consistently&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Record TPS, P50, P95, P99, aborts, retries, lock waits, WAL generation, CPU, storage latency, network traffic, connection count, tenant skew, rebalance time, and recovery time. Mark every result with the exact PostgreSQL, extension, instance, storage, and configuration versions.&lt;/p&gt;
&lt;p&gt;For the 40-TB table, do not begin by copying all data. Start with a representative but explicitly labeled dataset, validate query and transaction locality, then test a staged migration with change capture, per-tenant reconciliation, cutover, and rollback. A proposed test is not a measured result.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Architecture&lt;/th&gt;&lt;th&gt;Good fit&lt;/th&gt;&lt;th&gt;Where it breaks&lt;/th&gt;&lt;th&gt;Complexity owner&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;RDS with native partitioning&lt;/td&gt;&lt;td&gt;One writer still fits; lifecycle and pruning are the main problems&lt;/td&gt;&lt;td&gt;Writer CPU, WAL, locks, and storage path remain shared&lt;/td&gt;&lt;td&gt;AWS plus database team&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application-sharded RDS&lt;/td&gt;&lt;td&gt;Stable tenant key and strong managed-service requirement&lt;/td&gt;&lt;td&gt;Routing drift, hot tenants, fleet schema changes, cross-shard work&lt;/td&gt;&lt;td&gt;Application and platform teams&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Citus on EC2&lt;/td&gt;&lt;td&gt;Need Citus semantics and operational control&lt;/td&gt;&lt;td&gt;Coordinator and worker HA, upgrades, backups, rebalance, operational staffing&lt;/td&gt;&lt;td&gt;Database and infrastructure teams&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aurora PostgreSQL Limitless&lt;/td&gt;&lt;td&gt;AWS-managed horizontal writes and compatible tenant-local schema&lt;/td&gt;&lt;td&gt;Unsupported PostgreSQL features or integrations, poor shard key, distributed transactions&lt;/td&gt;&lt;td&gt;AWS plus database architecture team&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Domain decomposition&lt;/td&gt;&lt;td&gt;Clear business ownership and different recovery contracts&lt;/td&gt;&lt;td&gt;Event delivery, reconciliation, partial workflows, duplicated data&lt;/td&gt;&lt;td&gt;Application and platform teams&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Several warning signs should stop the design review:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;“We have &lt;code&gt;rds_superuser&lt;/code&gt;, so we can install it.”&lt;/strong&gt; The role cannot install a native package that AWS has not supplied.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;“&lt;code&gt;rds.allowed_extensions&lt;/code&gt; is &lt;code&gt;*&lt;/code&gt;, so all extensions are allowed.”&lt;/strong&gt; The wildcard covers extensions available for that RDS engine version.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;“&lt;code&gt;pg_partman&lt;/code&gt; distributes our partitions.”&lt;/strong&gt; It manages native partitions inside the same PostgreSQL database and writer boundary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;“&lt;code&gt;postgres_fdw&lt;/code&gt; gives us transparent sharding.”&lt;/strong&gt; It provides remote-table access; the architecture still owns placement, routing, transactions, and failure semantics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;“Logical replication gives every node write capacity.”&lt;/strong&gt; Replication does not define conflict-free ownership of the same rows.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;“Citus will scale any workload linearly.”&lt;/strong&gt; Cross-shard joins, multi-shard transactions, hot tenants, coordinator work, network, storage, and rebalance behavior must be measured.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The migration boundary is equally important. A live 40-TB table is not moved safely through one maintenance-window command. Regardless of target, RetailCo needs an inventory of tables and dependencies, a shard-key and transaction-locality analysis, a staged copy, ongoing change capture, reconciliation, controlled routing changes, and a rollback path.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;For RetailCo, &lt;strong&gt;do not pursue Citus on standard Amazon RDS for PostgreSQL&lt;/strong&gt;. The prerequisite is unsupported, and &lt;code&gt;pg_partman&lt;/code&gt;, &lt;code&gt;pglogical&lt;/code&gt;, &lt;code&gt;postgres_fdw&lt;/code&gt;, or extra privileges do not recreate Citus.&lt;/p&gt;
&lt;p&gt;Because RetailCo prefers AWS-managed operations and its checkout path is predominantly tenant-local, the recommended order is:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify that the current ceiling is write compute, WAL, lock contention, or storage latency—not avoidable reads or poor SQL.&lt;/li&gt;
&lt;li&gt;Use native partitioning and &lt;code&gt;pg_partman&lt;/code&gt; for the 40-TB table’s lifecycle and pruning needs, but do not claim that work adds independent writers.&lt;/li&gt;
&lt;li&gt;Build an Aurora PostgreSQL Limitless compatibility proof of concept using the exact supported engine patch and &lt;code&gt;tenant_id&lt;/code&gt; as the candidate shard key.&lt;/li&gt;
&lt;li&gt;Keep application-sharded RDS databases as the managed-PostgreSQL fallback when Limitless compatibility fails.&lt;/li&gt;
&lt;li&gt;Choose Citus on EC2 only when Citus-specific behavior is worth owning the full coordinator, worker, HA, backup, upgrade, and rebalance lifecycle.&lt;/li&gt;
&lt;li&gt;Move cross-tenant reporting away from the checkout commit path regardless of the selected OLTP architecture.&lt;/li&gt;
&lt;li&gt;Publish no scaling claim until tenant-local, hot-tenant, cross-shard, capacity-addition, and recovery tests have measured results.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Good fit:&lt;/strong&gt; application-sharded RDS or Aurora Limitless when &lt;code&gt;tenant_id&lt;/code&gt; is stable, high-cardinality, and present in most transactions; Citus on EC2 when operational control and Citus semantics are explicit requirements.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Poor fit:&lt;/strong&gt; any design labeled “Citus on standard RDS PostgreSQL,” or any sharding proposal whose critical transactions do not carry a stable ownership key.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Main advantage of the recommendation:&lt;/strong&gt; it preserves the managed-service preference without building on an unavailable extension.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Main limitation:&lt;/strong&gt; managed horizontal write scale still requires schema, routing, transaction, migration, and recovery redesign; AWS does not remove those architectural decisions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Operational complexity:&lt;/strong&gt; medium for application-sharded RDS or Limitless; high for self-managed Citus.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Migration complexity:&lt;/strong&gt; high for a live 40-TB system. Execute by tenant or another verifiable ownership slice, not as an undifferentiated table copy.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; RetailCo needs more write ownership domains, but the proposed Citus runtime is not available on standard RDS for PostgreSQL.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; separate table-management needs from distributed-compute needs, then choose native RDS partitioning, application-sharded RDS, Aurora Limitless, or self-managed Citus according to the actual boundary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; AWS’s current extension matrix omits Citus and its role documentation denies host access; Citus’s own installation and architecture documentation requires server packages, preload configuration, a coordinator, and workers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; run the extension preflight and transaction-locality review before the next architecture meeting. If either the required runtime or a stable ownership key is missing, reject that option before benchmarking it.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category></item><item><title>Index Debt Review: How to Find Bad, Missing, and Duplicate Indexes</title><link>https://rajivonai.com/blog/2024-03-18-index-debt-review-bad-missing-duplicate-indexes/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-03-18-index-debt-review-bad-missing-duplicate-indexes/</guid><description>A SQL-driven audit workflow for identifying unused, duplicate, bloated, and missing indexes in PostgreSQL before they drain write performance and storage.</description><pubDate>Mon, 18 Mar 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Indexes accumulate silently.&lt;/strong&gt; Engineers add them to fix slow queries, migration scripts add them to enforce constraints, ORM scaffolding adds them speculatively, and nobody systematically removes them. Over several years, a database with 50 tables can accumulate 200 indexes — half of which are never used, a tenth of which duplicate each other, and several of which are invalid or bloated. The cost is paid on every write: each insert, update, and delete must maintain every index on the affected table, whether or not that index is ever scanned.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s &lt;code&gt;pg_stat_user_indexes&lt;/code&gt; tracks cumulative scan counts for every index since the last statistics reset. An index with &lt;code&gt;idx_scan = 0&lt;/code&gt; has never been used in a query plan. An index that duplicates another index means two identical maintenance operations happen on every write. An invalid index — one that failed partway through a &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; — takes up space and maintenance overhead without ever being selected by the planner.&lt;/p&gt;
&lt;p&gt;Index debt reviews should happen on a schedule, not just when disk is running low. Write amplification from carrying 40 unused indexes on a high-write table is not dramatic — it adds microseconds per write — but it compounds. At high write volume, the cumulative effect shows up as elevated lock contention during bulk operations and higher checkpoint I/O pressure.&lt;/p&gt;
&lt;p&gt;The review is a structured SQL audit. No tools required beyond &lt;code&gt;psql&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Signal&lt;/th&gt;&lt;th&gt;Where to see it&lt;/th&gt;&lt;th&gt;What it means&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Table size growing faster than row count&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_size_pretty(pg_total_relation_size(...))&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Index bloat accumulating alongside table bloat&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slow bulk inserts or updates on large tables&lt;/td&gt;&lt;td&gt;Application timing logs&lt;/td&gt;&lt;td&gt;Too many indexes being maintained per write&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;idx_scan = 0&lt;/code&gt; on multiple indexes&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_user_indexes&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Unused indexes consuming write bandwidth&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Duplicate entries in &lt;code&gt;pg_index&lt;/code&gt; by &lt;code&gt;indrelid&lt;/code&gt; and &lt;code&gt;indkey&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_index&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Redundant indexes doubling maintenance overhead&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;indisvalid = false&lt;/code&gt; in &lt;code&gt;pg_index&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_index&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Invalid indexes from failed concurrent builds&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High seq_scan count with low idx_scan&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_user_tables&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Missing index on a frequently filtered column&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Unused indexes (zero scan count)&lt;/strong&gt; — the first thing to remove:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;schemaname&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;tablename&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indexname&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;idx_scan&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pg_size_pretty(pg_relation_size(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indexrelid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; index_size,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indisprimary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_indexes s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_index i &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indexrelid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indexrelid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;idx_scan&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indisprimary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indisunique&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_relation_size(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indexrelid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sort by size to prioritize — a 10 GB unused index is a higher-priority removal than a 10 MB one. Exclude primary keys and unique constraints; those enforce data integrity regardless of query usage.&lt;/p&gt;
&lt;p&gt;Check when statistics were last reset before acting on zero-scan counts:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stats_reset &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_database &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; current_database();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If &lt;code&gt;stats_reset&lt;/code&gt; was yesterday, a zero scan count is not evidence. If it was 60+ days ago, it is reliable.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;Duplicate indexes&lt;/strong&gt; — same table, same column list:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  indrelid::regclass &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tablename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  array_agg(indexrelid::regclass &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_relation_size(indexrelid) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; indexes,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  array_agg(pg_size_pretty(pg_relation_size(indexrelid)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_relation_size(indexrelid) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sizes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GROUP BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; indrelid, indkey&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;HAVING&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Two indexes on &lt;code&gt;(customer_id)&lt;/code&gt; with identical definitions are pure overhead — keep the one with higher &lt;code&gt;idx_scan&lt;/code&gt; and drop the other. Duplicates often result from migration tools generating a new index when a unique constraint was added on a column that already had a regular index.&lt;/p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;Bloated or low-use large indexes&lt;/strong&gt; — high storage cost relative to usage:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indexrelid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;::regclass &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; indexname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;tablename&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;idx_scan&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pg_size_pretty(pg_relation_size(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indexrelid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; index_size,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pg_relation_size(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indexrelid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; raw_size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_indexes s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;idx_scan&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; raw_size &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;An index with fewer than 10 scans that takes 5 GB of storage is worth examining closely. Combine with the age of statistics reset to determine if ”&amp;#x3C; 10 scans” reflects weeks of production traffic or just a few hours.&lt;/p&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;&lt;strong&gt;Tables with high sequential scan counts and missing indexes&lt;/strong&gt; — potential missing indexes:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  relname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  seq_scan,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  idx_scan,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_live_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  seq_scan &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx_scan &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; seq_excess&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; seq_scan &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx_scan&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_live_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; seq_scan &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; seq_scan &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A table with 500,000 rows where &lt;code&gt;seq_scan = 10000&lt;/code&gt; and &lt;code&gt;idx_scan = 50&lt;/code&gt; is performing full table scans on almost every access. Pair this with &lt;code&gt;EXPLAIN (ANALYZE)&lt;/code&gt; on the most frequent queries against that table to identify which column would benefit from an index.&lt;/p&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;&lt;strong&gt;Invalid indexes&lt;/strong&gt; — indexes that must be rebuilt:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  indexrelid::regclass &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; indexname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  indrelid::regclass &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tablename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pg_size_pretty(pg_relation_size(indexrelid)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; index_size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; indisvalid;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;An invalid index results from a &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; that failed partway through, typically due to a deadlock or constraint violation. PostgreSQL keeps the partially-built index but marks it as invalid — it takes up space and triggers write maintenance but is never used by the planner. These must be rebuilt or dropped.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Index audit triggered] --&gt; B{stats_reset recent?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes — under 30 days| C[Wait for 30 days of data before removing]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no — over 30 days of data| D{idx_scan = 0 indexes found?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes| E{Primary key or unique constraint?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|yes| F[Keep — data integrity requirement]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|no| G[DROP INDEX CONCURRENTLY]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no| H{Duplicate indexes found?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| I[Keep higher-scan index — drop duplicate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| J{Invalid indexes found?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|yes| K[REINDEX CONCURRENTLY]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|no| L{High seq_scan on large table?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|yes| M[EXPLAIN slow query — add covering index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|no| N[Index health OK — schedule next audit]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Option 1 — Drop unused indexes&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Always use &lt;code&gt;CONCURRENTLY&lt;/code&gt; to avoid blocking writes:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Drop a specific unused index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DROP&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CONCURRENTLY &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;schema_name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;unused_index_name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Verify it is gone&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; indexname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_indexes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tablename &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; indexname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;unused_index_name&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;DROP INDEX CONCURRENTLY&lt;/code&gt; waits for all transactions that reference the index to complete, then removes it. It does not hold an ACCESS EXCLUSIVE lock for the duration — it uses multiple lower-level locks and can coexist with reads and writes. It cannot run inside a transaction block.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 2 — Rebuild invalid or bloated indexes&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For invalid indexes from failed concurrent builds:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Rebuild concurrently — creates new valid index, replaces old&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;REINDEX &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INDEX&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CONCURRENTLY &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;schema_name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;invalid_index_name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Or drop and recreate&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DROP&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CONCURRENTLY &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;schema_name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;invalid_index_name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; CONCURRENTLY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx_orders_status &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For bloated indexes where the size has grown disproportionately to the data (common on tables with many deletes and updates), &lt;code&gt;REINDEX CONCURRENTLY&lt;/code&gt; reclaims the space. The bloat is visible by comparing &lt;code&gt;pg_relation_size(indexrelid)&lt;/code&gt; against &lt;code&gt;pg_relation_size(indrelid) * 0.1&lt;/code&gt; — an index larger than 10% of its table’s size on a low-selectivity column is worth investigating.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 3 — Create missing indexes for high-seq-scan tables&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When &lt;code&gt;pg_stat_user_tables&lt;/code&gt; shows a table with &lt;code&gt;seq_scan &gt;&gt; idx_scan&lt;/code&gt; and large &lt;code&gt;n_live_tup&lt;/code&gt;, identify the query pattern and create a covering index:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Always create concurrently in production&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; CONCURRENTLY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx_orders_status_created&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;pending&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;processing&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- partial index if applicable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Verify the index is used after creation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;pending&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; interval &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;7 days&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 50&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A partial index (&lt;code&gt;WHERE status IN (...)&lt;/code&gt;) is smaller, faster to maintain, and more selective than a full index on the same column. Use it when the query always filters to a known subset.&lt;/p&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;DROP INDEX CONCURRENTLY&lt;/code&gt;: reversible by recreating the index with &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt;. Keep the original index DDL in a migration file before dropping so reconstruction is a single command. Note that recreation is not instant on large tables — budget time for it.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;REINDEX CONCURRENTLY&lt;/code&gt;: leaves the original index in place until the rebuild is complete, then swaps atomically. Safe to abort at any point — if aborted, the original index is still valid.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt;: if the new index turns out to worsen plan choices, drop it with &lt;code&gt;DROP INDEX CONCURRENTLY&lt;/code&gt;. The planner will revert to its prior plan immediately.&lt;/li&gt;
&lt;li&gt;No rollback is needed for the read-only audit queries — they have no side effects.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;Index audits are well-suited to a quarterly automated report. This query generates a prioritized removal candidate list:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Quarterly index debt report&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;DROP INDEX CONCURRENTLY &apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; schemaname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;.&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; indexname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;;&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; removal_sql,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pg_size_pretty(pg_relation_size(indexrelid)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; reclaimed,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  idx_scan,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_idx_scan&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_indexes s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_index i &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indexrelid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indexrelid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;idx_scan&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indisprimary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indisunique&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_relation_size(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indexrelid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;  -- &gt; 10 MB only&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_relation_size(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;indexrelid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;last_idx_scan&lt;/code&gt; (added in PostgreSQL 16) shows the timestamp of the last use, which is more precise than relying on &lt;code&gt;stats_reset&lt;/code&gt;. For earlier versions, &lt;code&gt;stats_reset&lt;/code&gt; from &lt;code&gt;pg_stat_database&lt;/code&gt; is the best proxy.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The PostgreSQL documentation for &lt;code&gt;pg_stat_user_indexes&lt;/code&gt; explicitly notes that &lt;code&gt;idx_scan&lt;/code&gt; is reset by &lt;code&gt;pg_stat_reset()&lt;/code&gt; and reflects cumulative counts since the last reset. This means that before acting on zero-scan counts, verifying the age of the statistics reset is not optional — it is required. The PostgreSQL wiki recommends a minimum of 2–4 weeks of production traffic before treating a zero scan count as evidence of permanent non-use.&lt;/p&gt;
&lt;p&gt;The documented behavior of &lt;code&gt;DROP INDEX CONCURRENTLY&lt;/code&gt; is that it requires two table scans — one to mark the index invalid, one to remove it — and uses a series of lower-level locks rather than a single ACCESS EXCLUSIVE lock. Per the PostgreSQL documentation, it is safe to run on production tables under normal load, with the caveat that it cannot be executed inside an explicit transaction block.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Dropped index turns out to be needed&lt;/td&gt;&lt;td&gt;Statistics reset was recent; index was used before reset&lt;/td&gt;&lt;td&gt;Recreate with &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt;; add to rollback script before next drop&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;DROP INDEX CONCURRENTLY&lt;/code&gt; hangs&lt;/td&gt;&lt;td&gt;Long-running transaction holds a lock on the table&lt;/td&gt;&lt;td&gt;Wait for transaction to complete; monitor &lt;code&gt;pg_stat_activity&lt;/code&gt; for blockers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;REINDEX CONCURRENTLY&lt;/code&gt; fails midway&lt;/td&gt;&lt;td&gt;Disk full during index rebuild&lt;/td&gt;&lt;td&gt;Free disk space; the original index is still valid after failure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Duplicate index removal breaks constraint&lt;/td&gt;&lt;td&gt;Duplicate was actually a unique constraint enforced via index&lt;/td&gt;&lt;td&gt;Check &lt;code&gt;indisunique&lt;/code&gt; in &lt;code&gt;pg_index&lt;/code&gt; before dropping — never drop unique indexes without confirming the constraint is covered elsewhere&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;New covering index triggers plan regression&lt;/td&gt;&lt;td&gt;Planner prefers new index for a query it should not&lt;/td&gt;&lt;td&gt;Drop the new index and use &lt;code&gt;pg_hint_plan&lt;/code&gt; or partial index to constrain scope&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Unused and duplicate indexes consume write bandwidth on every insert, update, and delete, with no benefit — and invalid indexes waste space and maintenance work while never being selected by the planner.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Run the five audit queries on a schedule, confirm statistics age, and use &lt;code&gt;DROP INDEX CONCURRENTLY&lt;/code&gt; and &lt;code&gt;REINDEX CONCURRENTLY&lt;/code&gt; to clean up — always with CONCURRENTLY to avoid locking.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: After removing a high-overhead unused index, &lt;code&gt;pg_stat_bgwriter.buffers_clean&lt;/code&gt; should stabilize or decrease on write-heavy tables, and bulk insert timing should improve.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Run Check 1 and Check 5 this week. Drop any invalid indexes immediately with &lt;code&gt;REINDEX CONCURRENTLY&lt;/code&gt;, and flag any zero-scan indexes over 1 GB for the next review cycle.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;checklist&quot;&gt;Checklist&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Check &lt;code&gt;pg_stat_database.stats_reset&lt;/code&gt; — confirm statistics are at least 30 days old before acting&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;pg_stat_user_indexes&lt;/code&gt; for &lt;code&gt;idx_scan = 0&lt;/code&gt; — exclude primary keys and unique constraints&lt;/li&gt;
&lt;li&gt;Sort zero-scan indexes by &lt;code&gt;pg_relation_size&lt;/code&gt; — prioritize largest for removal&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;pg_index&lt;/code&gt; for duplicate &lt;code&gt;indrelid + indkey&lt;/code&gt; combinations — identify redundant indexes&lt;/li&gt;
&lt;li&gt;For duplicates, keep the index with the higher &lt;code&gt;idx_scan&lt;/code&gt; count and drop the other&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;pg_index WHERE NOT indisvalid&lt;/code&gt; — list all invalid indexes&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;REINDEX CONCURRENTLY&lt;/code&gt; on all invalid indexes immediately&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;pg_stat_user_tables&lt;/code&gt; for tables with &lt;code&gt;seq_scan &gt;&gt; idx_scan&lt;/code&gt; and &lt;code&gt;n_live_tup &gt; 10000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;For high-seq-scan tables, run &lt;code&gt;EXPLAIN (ANALYZE)&lt;/code&gt; on frequent queries to identify missing indexes&lt;/li&gt;
&lt;li&gt;Create any missing indexes with &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Document all dropped indexes with their original DDL before removing&lt;/li&gt;
&lt;li&gt;Schedule the next index audit for 90 days out — add to the team runbook&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>checklist</category><category>failures</category></item><item><title>Aurora Serverless v2: Good Fit, Bad Fit</title><link>https://rajivonai.com/blog/2024-03-11-aurora-serverless-v2-good-fit-bad-fit/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-03-11-aurora-serverless-v2-good-fit-bad-fit/</guid><description>Aurora Serverless v2 scales ACUs rather than to zero — understanding the cost floor, scale-up lag, and workload fit before you commit to it for production OLTP.</description><pubDate>Mon, 11 Mar 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Aurora Serverless v2 is not a zero-cost idle database. It does not scale to zero. The minimum ACU setting is a cost floor, not a free tier — and the seconds-long lag while capacity adds is invisible in load tests until it hits you at 9am on a Monday when traffic ramps faster than the scaler reacts. Picking the right workload for this product matters more than the configuration.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Aurora Serverless v2 replaced the original Aurora Serverless (v1) as AWS’s elastic capacity layer for Aurora MySQL and PostgreSQL. The core pitch is straightforward: instead of choosing an instance class and living with it, you set a minimum and maximum in Aurora Capacity Units (ACUs), and Aurora scales between them as your workload changes. One ACU is approximately 2 GiB of memory with proportional CPU.&lt;/p&gt;
&lt;p&gt;Engineers encounter Aurora Serverless v2 in two scenarios: they are building a new application and want to avoid instance sizing decisions, or they are running development and staging databases that sit idle most of the day. Both are valid entry points. The confusion arrives when teams read “serverless” and assume it behaves like Lambda — scaling to zero and costing nothing when unused. That is not how v2 works.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Aurora Serverless v2 does not scale to zero. Per &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-serverless-v2.html&quot;&gt;AWS Aurora Serverless v2 documentation&lt;/a&gt;, the minimum ACU setting is 0.5 ACU. A cluster sitting at 0.5 ACU is still running, still consuming storage, and still billing you for compute capacity — just at the floor. At 0.5 ACU the cluster is not responsive enough for most production workloads; it is a warm-standby state, not an off state.&lt;/p&gt;
&lt;p&gt;The second operational problem is scale-up latency. AWS documentation describes Aurora Serverless v2 scaling as happening in increments as fine as 0.5 ACU, and the scaling response is measured in seconds rather than the minutes v1 required. But “seconds” still means your application sees elevated latency during a rapid ramp. A workload that goes from idle to peak in under 30 seconds — a flash sale, a morning cron job flushing a large batch, a viral event — will encounter query latency spikes while ACUs catch up. That behavior does not show up in steady-state load tests.&lt;/p&gt;
&lt;p&gt;The core question becomes: Which production workloads can actually tolerate Aurora Serverless v2’s scaling latency and cost floor, and which should stay on provisioned instances?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;Aurora Serverless v2 and a provisioned Aurora instance solve different cost problems. The architectural behavior dictating this is that scaling events monitor CPU and memory constraints continuously, stepping up capacity only when thresholds are breached.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[&quot;Application Workload&quot;] --&gt; Router[&quot;Aurora Query Router&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Router --&gt; Instance[&quot;Serverless v2 Instance&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Instance --&gt; Monitor[&quot;Capacity Monitor — CPU and Memory&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Monitor --&gt;|&quot;Demand Exceeds Threshold&quot;| ScaleUp[&quot;Step Up ACU Allocation&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Monitor --&gt;|&quot;Demand Drops&quot;| ScaleDown[&quot;Step Down ACU Allocation&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ScaleUp --&gt; Storage[&quot;Aurora Shared Cluster Volume&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ScaleDown --&gt; Storage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The table below reflects the documented scaling behavior and AWS’s own guidance on workload suitability based on these architectural constraints.&lt;/p&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Workload type&lt;/th&gt;&lt;th&gt;Serverless v2 fit&lt;/th&gt;&lt;th&gt;Provisioned fit&lt;/th&gt;&lt;th&gt;Reason&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Development and staging databases&lt;/td&gt;&lt;td&gt;Good&lt;/td&gt;&lt;td&gt;Acceptable&lt;/td&gt;&lt;td&gt;Usage is variable; v2 saves money vs always-on provisioned at dev scale&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unpredictable traffic spikes — e-commerce, events&lt;/td&gt;&lt;td&gt;Good&lt;/td&gt;&lt;td&gt;Acceptable&lt;/td&gt;&lt;td&gt;v2 scales up to handle bursts; burst lag is usually tolerable if gradual&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Multi-tenant SaaS — many low-utilization tenant DBs&lt;/td&gt;&lt;td&gt;Good&lt;/td&gt;&lt;td&gt;Poor&lt;/td&gt;&lt;td&gt;Per-tenant provisioned capacity wastes money; v2 consolidates cost&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Steady high-throughput OLTP — payment rails, order processing&lt;/td&gt;&lt;td&gt;Poor&lt;/td&gt;&lt;td&gt;Good&lt;/td&gt;&lt;td&gt;Provisioned is cheaper at consistent high utilization; no scale-lag risk&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Latency-sensitive workloads with P99 budget under 100ms&lt;/td&gt;&lt;td&gt;Poor&lt;/td&gt;&lt;td&gt;Good&lt;/td&gt;&lt;td&gt;Scale-up pause exceeds latency budget during capacity adds&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Workloads that regularly hit the ACU maximum&lt;/td&gt;&lt;td&gt;Poor&lt;/td&gt;&lt;td&gt;Good&lt;/td&gt;&lt;td&gt;You are paying provisioned-equivalent prices with serverless overhead&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The pattern in the “Poor” column is a single failure mode in different clothing: you are running a workload whose demand profile does not benefit from dynamic scaling, but you are paying the operational cost of it anyway.&lt;/p&gt;
&lt;p&gt;Unlike Aurora Serverless v1, v2 supports Multi-AZ deployments, Global Database, and read replicas. For teams that rejected v1 because of those feature gaps, v2 is worth re-evaluating — the operational parity with provisioned Aurora is close. Aurora Global Database architecture details, including how the storage-level replication layer works beneath both provisioned and serverless configurations, are covered in &lt;a href=&quot;https://rajivonai.com/blog/2024-02-19-aurora-global-database-what-it-solves-and-does-not/&quot;&gt;Aurora Global Database: What It Solves and What It Does Not&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented behavior from AWS makes the cost model explicit: Aurora Serverless v2 bills per ACU-hour for the capacity consumed, with a floor at whatever minimum ACU you configure. A cluster set to a minimum of 0.5 ACU and a maximum of 16 ACU will never bill less than 0.5 ACU-hours per hour — even at 3am with zero connections. Because 0.5 ACUs represents a strict running floor, the documented pattern is that overnight idle cost remains a factor for production databases compared to stopping a traditional RDS instance.&lt;/p&gt;
&lt;p&gt;The scaling increment behavior — as small as 0.5 ACU per step — is explicitly described in &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-serverless-v2-setting-acus.html&quot;&gt;AWS Aurora Serverless v2 capacity documentation&lt;/a&gt;. The architectural consequence is that a cluster at minimum ACU receiving a sudden large query load will step up through multiple increments before reaching steady-state capacity, and each step takes a moment. Writer and reader instances scale independently, which matters for read-heavy workloads using read replicas — adding read capacity does not help a CPU-bound writer.&lt;/p&gt;
&lt;p&gt;The documented pattern from AWS is that workloads matching development environments or low-traffic production use-cases see meaningful savings from v2 over always-on provisioned instances. Conversely, workloads with consistent high utilization do not see these savings and incur the scale-up latency penalty unnecessarily.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Sudden traffic burst from a low ACU floor&lt;/td&gt;&lt;td&gt;Query latency spikes for seconds to tens of seconds&lt;/td&gt;&lt;td&gt;ACU scaling is fast but not instant; gap between demand arrival and capacity availability causes queuing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Minimum ACU misread as zero-cost idle&lt;/td&gt;&lt;td&gt;Surprise monthly bill for compute on a database with no traffic&lt;/td&gt;&lt;td&gt;0.5 ACU minimum is always running; “idle” is not “off”&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Maximum ACU cap during sustained high load&lt;/td&gt;&lt;td&gt;Connections queue or queries fail when ACU ceiling is hit&lt;/td&gt;&lt;td&gt;v2 does not exceed the maximum you set; a too-low ceiling behaves like an undersized provisioned instance&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High-utilization steady OLTP workload&lt;/td&gt;&lt;td&gt;v2 cost exceeds provisioned equivalent&lt;/td&gt;&lt;td&gt;At constant high utilization, provisioned instance pricing is cheaper and eliminates scale-up lag risk&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: A team selects Aurora Serverless v2 for production OLTP expecting elastic cost savings, sets a low minimum ACU to reduce idle cost, and discovers latency spikes every morning when traffic ramps faster than ACUs add.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Match the ACU minimum to the lowest acceptable sustained capacity for your P99 latency target, not to the cheapest idle state; use provisioned Aurora for workloads with consistent high utilization.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Set minimum ACU at least to the capacity needed to handle your initial morning ramp without queuing — then observe scale-up events in CloudWatch Aurora metrics (the &lt;code&gt;ServerlessDatabaseCapacity&lt;/code&gt; metric shows ACU consumption in real time) and verify latency does not spike during ramp-up.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Pull one week of CloudWatch &lt;code&gt;ServerlessDatabaseCapacity&lt;/code&gt; metrics for any existing Aurora Serverless v2 cluster and compare average ACU consumption to your configured maximum; if average is consistently above 80% of maximum, the workload belongs on provisioned.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category></item><item><title>Vector Search on GPU Databases</title><link>https://rajivonai.com/blog/2024-03-06-vector-search-on-gpu-databases/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-03-06-vector-search-on-gpu-databases/</guid><description>A DBA-friendly explanation of how vector search works, why GPUs help, and where vector retrieval fits inside modern database and AI systems.</description><pubDate>Wed, 06 Mar 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Vector search sounds mysterious until you map it to familiar database concepts.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Retrieval systems are shifting from pure lexical matching to meaning-based retrieval. Developers are generating high-dimensional embeddings—numerical representations of meaning—for documents, chat logs, and product catalogs to enable semantic search. Traditional databases have bolted on vector data types to support this new access pattern. In DBA language, embeddings place content into coordinates in a high-dimensional space so semantically related items are close, even when the exact text differs.&lt;/p&gt;
&lt;p&gt;Traditional indexes optimize exact or ordered lookups. Embeddings optimize semantic proximity. Production systems now regularly combine metadata filters, keyword retrieval, and vector similarity retrieval into a single serving path.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Traditional indexing strategies break down when the core query requirement shifts from equality to similarity. Instead of exact match queries like:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; products&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; category &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;laptop&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;vector retrieval executes:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;query vector -&gt; nearest stored vectors&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This requires comparing a query vector against millions of stored vectors to find the nearest neighbors. At scale, that means repeated arithmetic over large arrays—such as dot products, cosine similarity, or Euclidean distance. Exact vector search compares against all candidates, which is accurate but computationally costly. When the vector corpus is large and queries per second (QPS) are meaningful, CPU-based execution bottlenecks on candidate scoring. How do you maintain strict latency targets when distance calculations dominate the runtime?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;Vector search is nearest-neighbor retrieval over high-dimensional coordinates, and GPU databases accelerate the specific mathematical bottlenecks of this workload.&lt;/p&gt;
&lt;p&gt;Approximate Nearest Neighbor (ANN) indexes reduce the search space to hit practical latency targets. ANN narrows candidate sets quickly, and then GPU acceleration scores and ranks these large candidate sets efficiently. This combination is why vector search and GPU databases are frequently paired.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Client Query] --&gt; B[Embedding Model]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[Query Vector]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[Database Engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[Metadata Filter]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[ANN Index Search]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Candidate Set Fetch]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[GPU Scoring Engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[Top K Reranked Results]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To build a DBA mental model, this is not a different universe; it is a new retrieval access pattern with familiar system tradeoffs:&lt;/p&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Traditional DB Concept&lt;/th&gt;&lt;th&gt;Vector Search Equivalent&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Row&lt;/td&gt;&lt;td&gt;Content item — chunk&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Indexed column&lt;/td&gt;&lt;td&gt;Embedding vector&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Equality predicate&lt;/td&gt;&lt;td&gt;Similarity function&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Top-N query&lt;/td&gt;&lt;td&gt;Top-K nearest neighbors&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Post-filtering&lt;/td&gt;&lt;td&gt;Metadata filtering and reranking&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Production retrieval usually combines metadata filters (tenant, region, ACL scope, content type, time window) with semantic search. This is why databases still matter deeply in AI retrieval systems: governance, filtering, structure, and access control do not disappear.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern is that CPU-based databases struggle under high QPS when computing exact distances on large vector dimensions. Systems like PostgreSQL using &lt;code&gt;pgvector&lt;/code&gt; behave efficiently with HNSW (Hierarchical Navigable Small World) indexes for moderate workloads, but finding the exact top candidates still requires significant distance calculations on the final candidate set.&lt;/p&gt;
&lt;p&gt;NVIDIA’s RAPIDS RAFT library demonstrates how GPUs handle these operations in production. The SIMT (Single Instruction, Multiple Threads) architecture of a GPU is a perfect fit for repeated vector arithmetic over large arrays. By offloading candidate scoring and reranking to GPUs, systems like Milvus (using GPU-accelerated indexes like IVF-PQ) can evaluate larger candidate sets without missing latency targets. The GPU accelerates the exact math repeated many times in parallel, allowing the system to scale throughput without degrading response times.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;GPU acceleration introduces setup complexity and is not a universal solution. It is a specific tool for candidate scoring bottlenecks.&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Dimension&lt;/th&gt;&lt;th&gt;CPU Vector Search&lt;/th&gt;&lt;th&gt;GPU Vector Search&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Setup complexity&lt;/td&gt;&lt;td&gt;Lower&lt;/td&gt;&lt;td&gt;Higher&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Small datasets&lt;/td&gt;&lt;td&gt;Usually fine&lt;/td&gt;&lt;td&gt;Often overkill&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Large candidate scoring&lt;/td&gt;&lt;td&gt;Can bottleneck&lt;/td&gt;&lt;td&gt;Strong fit&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Throughput&lt;/td&gt;&lt;td&gt;Moderate&lt;/td&gt;&lt;td&gt;High&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Latency under load&lt;/td&gt;&lt;td&gt;Degrades sooner&lt;/td&gt;&lt;td&gt;Stronger at scale&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Best fit&lt;/td&gt;&lt;td&gt;Smaller and simpler workloads&lt;/td&gt;&lt;td&gt;Large-scale retrieval and ranking&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;CPU-only architectures are often sufficient when the corpus is small, QPS is low, latency constraints are loose, or retrieval runs as an offline batch process. GPU acceleration is worth serious consideration when candidate scoring dominates runtime, retrieval is user-facing, or reranking and inference exist in the same serving path.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: CPU candidate scoring bottlenecks high-throughput semantic search when exact distance calculations scale linearly with candidate size.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Offload candidate scoring and vector similarity math to GPU execution to process large arrays in parallel.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Database implementations leveraging NVIDIA RAFT or GPU-accelerated Milvus indexes demonstrate high throughput scaling for dense vector workloads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Profile your vector search workloads to determine if distance arithmetic is the primary bottleneck before adopting GPU instances.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>gpu</category><category>vector-search</category><category>retrieval</category></item><item><title>How a 10 Billion Row SQL Query Runs in 200ms on a GPU Database</title><link>https://rajivonai.com/blog/2024-03-05-how-a-10-billion-row-sql-query-runs-in-200ms-on-a-gpu-database/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-03-05-how-a-10-billion-row-sql-query-runs-in-200ms-on-a-gpu-database/</guid><description>A DBA-friendly walkthrough of how modern GPU databases execute large analytical SQL queries using columnar storage, parallel scans, and GPU aggregation.</description><pubDate>Tue, 05 Mar 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The same SQL that takes 60 seconds on a CPU database runs in 200ms on a GPU database — and the reason is not that GPUs are faster processors, it is that the execution model changes what happens between query plan and result.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Every database engineer has seen a query that looks harmless in code review and painful in production:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; country, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(revenue)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; events&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GROUP BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; country;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At 10,000 rows, nobody cares. At 10 billion rows, this becomes a serious execution problem. CPU-based execution engines process this query through a bounded number of threads, each handling a sequential slice of the data. The query is I/O-intensive and compute-intensive, but the CPU serializes its work in ways that GPU execution does not.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The structural gap is parallelism. A CPU-based database runs this query with dozens to hundreds of parallel workers. A GPU-based engine runs it with thousands to tens of thousands of parallel threads, each processing a slice of columnar data simultaneously. The difference in wall time is not incremental — it is a category change for the right workload shape.&lt;/p&gt;
&lt;p&gt;The engineering question is not “why is this fast?” but rather “which queries change category, and which don’t?” Getting this wrong leads to GPU infrastructure that produces no benefit for the actual hot paths, because the bottleneck is I/O or coordination, not compute throughput.&lt;/p&gt;
&lt;h2 id=&quot;step-by-step-how-the-query-executes&quot;&gt;Step-by-Step: How the Query Executes&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;https://rajivonai.com/diagrams/gpu-database-execution/10b_row_query_gpu_timeline.svg&quot; alt=&quot;10B row GPU query timeline&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 1: CPU plans the query&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The request starts as a normal SQL path: parse SQL, resolve objects, build logical plan, choose physical plan. CPU remains the control plane for planning, scheduling, and orchestration.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 2: Engine isolates the heavy path&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The planner identifies operators suitable for acceleration. In most systems, this is hybrid execution — CPU keeps control-flow-heavy tasks, GPU takes scan/compute-heavy operators. The right model is not “GPU-only database” but “GPU-accelerated execution.”&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 3: Columnar data minimizes work&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For this query, the engine only needs &lt;code&gt;country&lt;/code&gt; and &lt;code&gt;revenue&lt;/code&gt;. Columnar layouts avoid moving irrelevant columns and align better with parallel arithmetic over dense vectors.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 4: GPU fan-out across threads&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The heavy scan/compute path is fanned out across many threads:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Thread 1     -&gt; rows 1-1M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Thread 2     -&gt; rows 1M-2M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Thread 3     -&gt; rows 2M-3M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Thread 10000 -&gt; rows 9.9B-10B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each thread performs repeated, regular work over a slice of data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 5: Partial aggregation and reduction&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Each worker builds partial aggregates, then the engine reduces them into final grouped totals. This is familiar database behavior, but at much higher degrees of parallelism.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 6: Finalize on CPU&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;After heavy compute, final result shaping and response serialization return through CPU-side control flow.&lt;/p&gt;
&lt;p&gt;The complete flow:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;SQL query&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;-&gt; CPU planner&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;-&gt; column selection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;-&gt; GPU scan + compute&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;-&gt; GPU partial aggregates&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;-&gt; GPU reduction&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;-&gt; CPU final return&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Stage ownership summary&lt;/strong&gt;&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Stage&lt;/th&gt;&lt;th&gt;CPU-centric path&lt;/th&gt;&lt;th&gt;GPU-accelerated path&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Parse + optimize&lt;/td&gt;&lt;td&gt;CPU&lt;/td&gt;&lt;td&gt;CPU&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Column selection&lt;/td&gt;&lt;td&gt;CPU&lt;/td&gt;&lt;td&gt;CPU&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Large scan&lt;/td&gt;&lt;td&gt;CPU workers&lt;/td&gt;&lt;td&gt;GPU threads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Partial aggregation&lt;/td&gt;&lt;td&gt;CPU workers&lt;/td&gt;&lt;td&gt;GPU threads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reduction&lt;/td&gt;&lt;td&gt;CPU merge&lt;/td&gt;&lt;td&gt;GPU reduction + CPU finalize&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Result shaping&lt;/td&gt;&lt;td&gt;CPU&lt;/td&gt;&lt;td&gt;CPU&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;https://rajivonai.com/diagrams/gpu-database-execution/inside_gpu_database_engine.svg&quot; alt=&quot;Inside a GPU database engine&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;NVIDIA RAPIDS cuDF documents the execution pattern for DataFrame aggregations: the GPU receives a columnar memory representation, applies the projection and filter kernels in parallel across all rows, builds partial hash aggregates per thread block, then reduces across blocks. The documented behavior is that this execution model is fastest when the working set fits in GPU VRAM — data spills to system RAM through NVLink or PCIe, and the bandwidth of that interconnect becomes the new bottleneck when the query exceeds VRAM capacity.&lt;/p&gt;
&lt;p&gt;BlazeIT and similar GPU-accelerated SQL engines (documented in academic literature, e.g., &lt;a href=&quot;https://dl.acm.org/doi/10.14778/1453856.1453915&quot;&gt;He et al., VLDB 2008&lt;/a&gt;) established the baseline behavior: scan-heavy queries with low selectivity (reading most of a table) see the largest speedups because the GPU’s memory bandwidth advantage over CPU memory bandwidth is largest for sequential reads. Selective point lookups see no benefit because GPU thread management overhead dominates the per-row compute time.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Query workload is OLTP&lt;/td&gt;&lt;td&gt;No speedup, higher latency&lt;/td&gt;&lt;td&gt;GPU kernel overhead is larger than the compute savings for small, indexed lookups&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Working set exceeds GPU VRAM&lt;/td&gt;&lt;td&gt;Speedup collapses to CPU-level or slower&lt;/td&gt;&lt;td&gt;PCIe/NVLink transfer becomes the bottleneck; GPU’s internal bandwidth advantage disappears&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Query is I/O-bound, not compute-bound&lt;/td&gt;&lt;td&gt;Adding GPU does not help&lt;/td&gt;&lt;td&gt;The storage read is the bottleneck; GPU sits idle waiting for data&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Write-heavy workload&lt;/td&gt;&lt;td&gt;Incorrect fit&lt;/td&gt;&lt;td&gt;Transactional writes require coordination machinery that GPUs do not accelerate&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Irregular or sparse data access&lt;/td&gt;&lt;td&gt;Lower GPU utilization&lt;/td&gt;&lt;td&gt;Branching access patterns lead to thread divergence, reducing GPU parallelism efficiency&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: At 10B row scale, CPU-based analytical engines hit a parallelism ceiling that cannot be solved by adding CPU cores — the bottleneck is the number of simultaneous arithmetic operations, not the sophistication of the logic.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Move scan-heavy, aggregate-heavy SQL workloads to a GPU-accelerated execution engine; verify the query is compute-bound (not I/O-bound) before attributing speedup to GPU offload.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Run &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; on the target query and confirm the majority of time is in scan, aggregate, or join operators (not in network or storage I/O), then benchmark on a GPU-enabled instance with the same query and data volume.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Identify your three slowest analytical queries this week and profile whether the bottleneck is CPU compute, memory bandwidth, or storage I/O — only CPU compute bottlenecks are GPU-offload candidates.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>ai-engineering</category></item><item><title>Why Databases Are Moving Toward GPU Execution Engines</title><link>https://rajivonai.com/blog/2024-03-04-why-databases-are-moving-toward-gpu-execution-engines/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-03-04-why-databases-are-moving-toward-gpu-execution-engines/</guid><description>A practical, DBA-friendly explanation of why modern analytical databases are increasingly using GPUs for scans, joins, aggregations, and AI-adjacent workloads.</description><pubDate>Mon, 04 Mar 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The CPU-centric query engine is not being replaced — it is being augmented, and the teams who are not planning for that shift are about to face a capacity ceiling on their analytical workloads.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Database engines were designed around one default assumption: the CPU is the center of query execution. That was the right design for an era dominated by OLTP, indexed lookups, branch-heavy logic, and transaction coordination. Workload shape has changed. Modern platforms increasingly need to support large analytical scans, interactive dashboards, join-heavy columnar queries, vector search and retrieval, and AI-adjacent ranking and reranking. CPU-only systems are being asked to handle execution patterns they were not optimized for.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The operational symptom is predictable: a query that looked fine at 10 million rows becomes a sustained 60-second runtime at 10 billion rows, and adding more CPU capacity produces diminishing returns. The underlying problem is structural. CPU execution is sequential within a core — even well-parallelized CPU queries are constrained by thread count, cache pressure, and branch prediction overhead. The expensive paths in modern analytical workloads — scan, filter, join, aggregate — are massively data-parallel operations, not coordination-heavy operations. CPUs are excellent at coordination. They are less efficient at executing the same arithmetic operation across a billion rows.&lt;/p&gt;
&lt;p&gt;The core question for operators: when does a GPU-accelerated execution engine produce a different result than throwing more CPU capacity at the problem?&lt;/p&gt;
&lt;h2 id=&quot;gpu-accelerated-database-architecture&quot;&gt;GPU-Accelerated Database Architecture&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Layer&lt;/th&gt;&lt;th&gt;CPU-only&lt;/th&gt;&lt;th&gt;GPU-augmented&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Planning and coordination&lt;/td&gt;&lt;td&gt;CPU&lt;/td&gt;&lt;td&gt;CPU&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Heavy analytical execution&lt;/td&gt;&lt;td&gt;CPU&lt;/td&gt;&lt;td&gt;CPU + GPU&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AI retrieval and vector serving&lt;/td&gt;&lt;td&gt;External stack&lt;/td&gt;&lt;td&gt;Integrated into the data platform&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The shift is not CPU replaced by GPU. The shift is: &lt;strong&gt;CPU for control, GPU for throughput.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://rajivonai.com/diagrams/gpu-database-execution/inside_gpu_database_engine.svg&quot; alt=&quot;Inside a GPU database engine&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What problem GPUs solve&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A lot of analytical SQL reduces to this execution shape:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;SCAN -&gt; FILTER -&gt; PROJECT -&gt; JOIN -&gt; AGGREGATE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Take:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; country, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(revenue)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; events&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GROUP BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; country;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At billion-row scale, this is a throughput problem. The engine repeatedly does similar work — read values, compare values, transform values, aggregate partial results — over large datasets. That repeated, data-parallel pattern maps well to GPU execution.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Why columnar storage enabled the shift&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;GPU execution fits far better with columnar data than row-heavy transactional layouts. If a query only needs &lt;code&gt;price&lt;/code&gt; and &lt;code&gt;quantity&lt;/code&gt;, a columnar engine can feed only those vectors into execution. That aligns with GPU-friendly flow:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;vector in -&gt; vector transform -&gt; vector reduce&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The industry trend followed a progression: vectorized execution → columnar storage and compression → GPU-aware operator offload.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Why AI is accelerating adoption&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;AI-oriented data systems increasingly require embeddings, nearest-neighbor retrieval, reranking, vector similarity, and inference near data. Those are not classic OLTP operations. They align with accelerator-friendly execution patterns, making GPU-capable systems easier to justify for combined analytical + AI workloads.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Architecture evaluation checklist&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;What dominates the hot path: transactions, scans, joins, vector math, or ranking?&lt;/li&gt;
&lt;li&gt;Is the data layout GPU-friendly: columnar, batched, predictable access?&lt;/li&gt;
&lt;li&gt;Is the workload large enough to amortize offload overhead?&lt;/li&gt;
&lt;li&gt;Is the bottleneck compute, or actually data movement, modeling, or partitioning?&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;NVIDIA’s RAPIDS cuDF library documents the design split explicitly: the GPU handles columnar data operations while the CPU handles query planning, result finalization, and control flow. The documented limitation is PCIe transfer overhead — data movement between CPU memory and GPU memory is the dominant latency cost for small-to-medium datasets. RAPIDS’ own documentation recommends GPU offload only when the working set is large enough that the transfer overhead is amortized across the computation.&lt;/p&gt;
&lt;p&gt;PostgreSQL extensions for GPU offload, such as PG-Strom (documented at heterodb.com), follow the same documented hybrid pattern: the PostgreSQL planner runs on CPU, while scan-heavy and join-heavy operators are offloaded to the GPU. PG-Strom’s documented design states that only operators with high arithmetic intensity are candidates for GPU offload — point lookups and index scans remain on CPU.&lt;/p&gt;
&lt;p&gt;DuckDB’s documented vectorized execution (CPU-based, not GPU) is a useful reference point for the floor: a CPU-based columnar engine can execute analytical queries at speeds that were GPU-exclusive five years ago, which means the decision to add GPU hardware requires a workload that exceeds what modern in-process columnar execution can handle.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;GPU for small indexed lookups&lt;/td&gt;&lt;td&gt;No throughput gain, higher latency&lt;/td&gt;&lt;td&gt;GPU kernel launch overhead exceeds the per-request compute time&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GPU for write-heavy OLTP&lt;/td&gt;&lt;td&gt;Incorrect fit — no benefit&lt;/td&gt;&lt;td&gt;Transactional writes are coordination-bound, not compute-bound&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GPU for branch-heavy procedural logic&lt;/td&gt;&lt;td&gt;Falls back to CPU or performs worse&lt;/td&gt;&lt;td&gt;Divergent execution paths across GPU threads reduce parallelism&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GPU without columnar storage&lt;/td&gt;&lt;td&gt;Poor data locality and excess data movement&lt;/td&gt;&lt;td&gt;Row-oriented layouts require reading irrelevant columns into GPU memory&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Adding GPU without profiling the hot path&lt;/td&gt;&lt;td&gt;Wasted infrastructure spend&lt;/td&gt;&lt;td&gt;GPU acceleration only moves the needle when compute, not I/O or coordination, is the bottleneck&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: CPU-only analytical engines hit a scalability ceiling on scan-heavy, aggregate-heavy workloads — and that ceiling arrives earlier as AI retrieval and vector search enter the data platform.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Classify hot paths by execution pattern first; move scan-heavy, arithmetic-heavy workloads to GPU-accelerated execution while keeping planning, coordination, and OLTP on CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Run your top five analytical queries on a GPU-enabled instance or a GPU-accelerated engine such as RAPIDS cuDF, compare elapsed time and I/O throughput, and confirm the query is actually compute-bound (not I/O-bound) before attributing speedup to GPU offload.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, profile your three slowest analytical queries and determine whether the bottleneck is CPU compute, memory bandwidth, storage I/O, or query plan shape — only the CPU compute bottleneck is a GPU-offload candidate.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>ai-engineering</category></item><item><title>PostgreSQL Statistics Drift Workflow</title><link>https://rajivonai.com/blog/2024-02-26-postgresql-statistics-drift-workflow/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-02-26-postgresql-statistics-drift-workflow/</guid><description>When the query planner gets row estimates wrong, queries regress silently. This runbook diagnoses statistics drift and restores accurate plans.</description><pubDate>Mon, 26 Feb 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A query that ran in 8 milliseconds last week and now takes 4 seconds has not changed — but the planner’s model of the data has.&lt;/strong&gt; PostgreSQL’s query optimizer builds execution plans from table statistics: column value distributions, row counts, and correlation coefficients stored in &lt;code&gt;pg_statistic&lt;/code&gt;. When those statistics drift from reality, the optimizer chooses wrong plans with confidence, and the resulting regressions are difficult to catch because no error is raised — just slower queries.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL uses a cost-based optimizer that estimates how many rows each plan step will process. Those estimates come from statistics gathered by &lt;code&gt;ANALYZE&lt;/code&gt;. If statistics are stale — from a bulk load, a large delete, or simply not running &lt;code&gt;ANALYZE&lt;/code&gt; for an extended period — the planner’s row estimates diverge from actual counts, and plan choices that were correct for the old data distribution become wrong for the current one.&lt;/p&gt;
&lt;p&gt;The most common presentation: a query that joins two tables starts doing a nested loop instead of a hash join because the planner underestimates the inner table’s row count. Or an index scan gets chosen when the data has changed enough that a sequential scan would be faster. Or a partial index gets selected for a query where the filtered row count no longer makes that index selective.&lt;/p&gt;
&lt;p&gt;Statistics drift is distinct from index bloat or table bloat. The physical storage might be fine. The problem is that the optimizer’s mental model of the data is wrong, and it is building plans optimized for a database that no longer exists.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Signal&lt;/th&gt;&lt;th&gt;Where to see it&lt;/th&gt;&lt;th&gt;What it means&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; estimated rows far from actual rows&lt;/td&gt;&lt;td&gt;Query plan output&lt;/td&gt;&lt;td&gt;Statistics are stale or the column distribution is unusual&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;last_analyze&lt;/code&gt; or &lt;code&gt;last_autoanalyze&lt;/code&gt; is days old&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_user_tables&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Automatic statistics updates not running on this table&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Query plan changed after a bulk load or large delete&lt;/td&gt;&lt;td&gt;Application performance logs&lt;/td&gt;&lt;td&gt;The new data volume or distribution triggered a different plan&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Planner chooses sequential scan on a selective query&lt;/td&gt;&lt;td&gt;&lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; output&lt;/td&gt;&lt;td&gt;Row count estimate too high; planner thinks index would cost more&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Planner chooses nested loop for a large result set&lt;/td&gt;&lt;td&gt;&lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; output&lt;/td&gt;&lt;td&gt;Row count estimate too low; planner underestimated join output&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;n_distinct&lt;/code&gt; in &lt;code&gt;pg_stats&lt;/code&gt; shows -1 for a column with few distinct values&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stats&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Statistics estimate is extrapolated, not exact&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Confirm the estimate-vs-actual divergence&lt;/strong&gt; — the EXPLAIN output is the primary diagnostic:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPLAIN (ANALYZE, BUFFERS, FORMAT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TEXT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders o&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; customers c &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; o&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;customer_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; o&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;pending&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; o&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;created_at&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; interval &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;7 days&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Look for rows where &lt;code&gt;rows=N (actual rows=M)&lt;/code&gt; and &lt;code&gt;N&lt;/code&gt; is off by more than a factor of 10. A nested loop chosen over a hash join when the actual row count exceeds 10,000 is a clear statistics failure. Note the exact node type (SeqScan, IndexScan, Hash, NestLoop) — this tells you which estimate was wrong.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;Inspect column statistics for the affected table&lt;/strong&gt; — &lt;code&gt;pg_stats&lt;/code&gt; stores what the planner knows:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  attname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_distinct,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  correlation,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  null_frac,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  avg_width,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  most_common_vals,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  most_common_freqs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stats&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tablename &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;orders&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; attname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;status&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;created_at&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;customer_id&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; attname;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;n_distinct &gt; 0&lt;/code&gt; means an absolute count; &lt;code&gt;n_distinct &amp;#x3C; 0&lt;/code&gt; means a fraction of the table. If &lt;code&gt;n_distinct = -1&lt;/code&gt;, PostgreSQL is guessing that every row is unique — problematic for low-cardinality columns. Low &lt;code&gt;correlation&lt;/code&gt; (near 0) on a column used in a range scan means physical row order does not match logical sort order, which raises index scan costs.&lt;/p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;Check when statistics were last collected&lt;/strong&gt; — stale analyze timestamps are the first explanation:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  relname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_analyze,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_autoanalyze,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_live_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_dead_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_mod_since_analyze&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; relname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;customers&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; last_analyze &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NULLS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LAST&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;n_mod_since_analyze&lt;/code&gt; is the counter that autovacuum uses to decide whether to run &lt;code&gt;ANALYZE&lt;/code&gt;. If it is large relative to &lt;code&gt;n_live_tup&lt;/code&gt;, statistics are definitely stale. A &lt;code&gt;last_analyze&lt;/code&gt; of NULL means &lt;code&gt;ANALYZE&lt;/code&gt; has never run on this table.&lt;/p&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;&lt;strong&gt;Check for bulk data changes that were not followed by ANALYZE&lt;/strong&gt; — look at table modification counts:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  relname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_mod_since_analyze,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_live_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_mod_since_analyze::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nullif&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_live_tup, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; mod_pct&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_mod_since_analyze &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; mod_pct &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A &lt;code&gt;mod_pct&lt;/code&gt; above 20% means more than 20% of the table has changed since the last statistics collection — the autovacuum &lt;code&gt;analyze_scale_factor&lt;/code&gt; default is 0.2, so autovacuum should have triggered, but may not have if the table is very large or autovacuum was busy.&lt;/p&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;&lt;strong&gt;Check raw statistics storage&lt;/strong&gt; — to understand what the planner is actually seeing:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  staattnum,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  stakind1,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  stavalues1,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  stanumbers1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_statistic&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; starelid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;::regclass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;stakind&lt;/code&gt; 1 = most-common-values, 2 = histogram, 3 = correlation. If &lt;code&gt;stavalues1&lt;/code&gt; is sparse or missing, the planner has no useful distribution data for that column. This is the raw form of what &lt;code&gt;pg_stats&lt;/code&gt; presents in human-readable form.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Slow query — plan regression suspected] --&gt; B{EXPLAIN estimated rows match actual?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes — estimates correct| C[Statistics not the problem — check indexes or locks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no — large divergence| D{last_analyze recent?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no — stale or never| E[ANALYZE tablename — re-check plan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes — but still wrong| F{Column has unusual distribution?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|yes — skewed or correlated| G[ALTER COLUMN SET STATISTICS 500]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H[ANALYZE tablename — re-check plan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|no| I{Multiple columns in WHERE clause?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|yes| J[CREATE STATISTICS for correlated columns]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[ANALYZE tablename — re-check plan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|no| L{n_distinct estimate wrong?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|yes| M[ALTER COLUMN SET n_distinct — explicit override]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|no| N[Check for partial index mismatch or planner bugs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Option 1 — Run ANALYZE to refresh statistics&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The simplest fix — and always the first step:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Analyze a specific table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ANALYZE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VERBOSE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Analyze multiple tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ANALYZE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VERBOSE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders, customers, order_items;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Analyze a specific column (faster on large tables)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ANALYZE orders (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, created_at, customer_id);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;ANALYZE VERBOSE&lt;/code&gt; prints a summary of rows sampled, which is useful for confirming the statistics update ran successfully. After &lt;code&gt;ANALYZE&lt;/code&gt;, re-run &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; on the slow query to see if the estimates improved.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ANALYZE&lt;/code&gt; takes a &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt; lock — it blocks DDL but not reads or writes. It is safe to run on production tables at any time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 2 — Increase statistics target for selective columns&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The default &lt;code&gt;default_statistics_target = 100&lt;/code&gt; samples 300 * 100 = 30,000 rows for statistics. For columns with many distinct values or highly skewed distributions, this sample may not capture the tail. Increase the per-column target:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Increase statistics detail for a specific column&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; COLUMN &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; STATISTICS&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 500&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; COLUMN created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; STATISTICS&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 500&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Then refresh statistics&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ANALYZE orders;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A &lt;code&gt;statistics target&lt;/code&gt; of 500 collects approximately 150,000 rows — 5x the default. The &lt;code&gt;pg_stats&lt;/code&gt; documentation notes that &lt;code&gt;n_distinct&lt;/code&gt; estimates and histogram bucket counts improve with higher targets, especially for columns where the value distribution has a long tail.&lt;/p&gt;
&lt;p&gt;After increasing the target, verify in &lt;code&gt;pg_stats&lt;/code&gt; that &lt;code&gt;most_common_vals&lt;/code&gt; is more populated and that histogram buckets look representative:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; attname, array_length(most_common_vals, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; mcv_count,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       array_length(histogram_bounds, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; histogram_buckets&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stats&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tablename &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Option 3 — Create extended statistics for correlated columns&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When a &lt;code&gt;WHERE&lt;/code&gt; clause filters on two columns that are correlated — e.g., &lt;code&gt;status = &apos;shipped&apos; AND region = &apos;EU&apos;&lt;/code&gt; where shipped orders are disproportionately from EU — the planner multiplies the selectivity of each column independently and underestimates the result set. PostgreSQL 10 introduced extended statistics to model this:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Create statistics tracking correlation between two columns&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; STATISTICS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders_status_region (dependencies)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, region&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Collect the extended statistics&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ANALYZE orders;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Verify&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stxname, stxkind, stxdefined&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_statistic_ext&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stxrelid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;::regclass;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Extended statistics with &lt;code&gt;dependencies&lt;/code&gt; teaches the planner that the two columns are correlated. The &lt;code&gt;ndistinct&lt;/code&gt; option captures combined distinct value counts; &lt;code&gt;mcv&lt;/code&gt; captures the most common value combinations. After collecting, re-run &lt;code&gt;EXPLAIN&lt;/code&gt; to see if the multi-column estimate improved.&lt;/p&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;ANALYZE&lt;/code&gt; is always safe to run and always safe to re-run. It does not modify data. The only rollback consideration is performance: on a very large table with a high statistics target, &lt;code&gt;ANALYZE&lt;/code&gt; can take minutes and create I/O pressure. Run during off-peak hours on tables over 100 GB.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ALTER COLUMN SET STATISTICS N&lt;/code&gt; is reversible: &lt;code&gt;ALTER TABLE orders ALTER COLUMN status SET STATISTICS -1&lt;/code&gt; returns to the default. No &lt;code&gt;ANALYZE&lt;/code&gt; re-run is needed to revert — the change takes effect on the next &lt;code&gt;ANALYZE&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CREATE STATISTICS&lt;/code&gt; is reversible: &lt;code&gt;DROP STATISTICS orders_status_region&lt;/code&gt;. The planner reverts to independent column estimates immediately.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ALTER TABLE ... SET (n_distinct = N)&lt;/code&gt; — an explicit override that bypasses sampling — is reversible: &lt;code&gt;ALTER TABLE orders ALTER COLUMN col SET (n_distinct = -1)&lt;/code&gt; restores to estimated mode.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;Stale statistics are predictable: they happen after bulk loads and large deletes. A pattern worth automating is a post-ETL &lt;code&gt;ANALYZE&lt;/code&gt; call baked into the data pipeline itself, rather than relying on autovacuum timing:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- After any bulk insert, run ANALYZE immediately&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders_archive &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;completed&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; interval &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;1 year&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DELETE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;completed&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; interval &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;1 year&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ANALYZE orders;  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- do not skip this&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For monitoring, a pg_cron query that alerts when &lt;code&gt;n_mod_since_analyze&lt;/code&gt; exceeds a threshold gives advance notice before the planner starts making wrong decisions:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; cron&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;schedule&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;stats-staleness-check&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;30 * * * *&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $$&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ops&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;stats_alerts&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tablename, mod_pct, captured_at)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    relname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_mod_since_analyze::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nullif&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_live_tup, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_live_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_mod_since_analyze::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nullif&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_live_tup, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$$);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The PostgreSQL statistics documentation describes the statistics target as controlling both the number of histogram buckets and the most-common-values list length. The documented relationship is: &lt;code&gt;statistics_target&lt;/code&gt; × 300 = rows sampled. For a column where 0.01% of rows have a specific value that is frequently queried, the default 30,000-row sample will often miss that value entirely, producing a histogram-based estimate that is substantially wrong.&lt;/p&gt;
&lt;p&gt;The documented behavior of &lt;code&gt;CREATE STATISTICS&lt;/code&gt; with &lt;code&gt;dependencies&lt;/code&gt; is that it computes functional dependency statistics between columns. Where the selectivity of &lt;code&gt;col_a = &apos;x&apos;&lt;/code&gt; is 0.01 and &lt;code&gt;col_b = &apos;y&apos;&lt;/code&gt; is 0.05, the planner without extended statistics estimates the joint selectivity as 0.01 × 0.05 = 0.0005. With a dependencies statistic showing that &lt;code&gt;col_a = &apos;x&apos;&lt;/code&gt; implies &lt;code&gt;col_b = &apos;y&apos;&lt;/code&gt; with 95% probability, the planner correctly estimates closer to 0.01.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;ANALYZE&lt;/code&gt; runs but estimates still wrong&lt;/td&gt;&lt;td&gt;Column has extreme skew — 99% of rows share one value&lt;/td&gt;&lt;td&gt;Increase &lt;code&gt;statistics_target&lt;/code&gt; to 1000; use &lt;code&gt;CREATE STATISTICS mcv&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Extended statistics do not help&lt;/td&gt;&lt;td&gt;Correlation is partial, not functional dependency&lt;/td&gt;&lt;td&gt;Try &lt;code&gt;ndistinct&lt;/code&gt; variant of &lt;code&gt;CREATE STATISTICS&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;ANALYZE&lt;/code&gt; is too slow on large table&lt;/td&gt;&lt;td&gt;Table has 1B+ rows and wide schema&lt;/td&gt;&lt;td&gt;Analyze specific columns only: &lt;code&gt;ANALYZE table (col1, col2)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Autovacuum is running ANALYZE but estimates still drift&lt;/td&gt;&lt;td&gt;&lt;code&gt;analyze_scale_factor&lt;/code&gt; threshold crossed only after large drift&lt;/td&gt;&lt;td&gt;Lower &lt;code&gt;autovacuum_analyze_scale_factor&lt;/code&gt; per-table to 0.01&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Plan regression returns after ANALYZE&lt;/td&gt;&lt;td&gt;Statistics are correct but planner constant factors are wrong&lt;/td&gt;&lt;td&gt;Consider &lt;code&gt;pg_hint_plan&lt;/code&gt; as a temporary override while investigating&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Stale or low-resolution statistics cause the planner to choose wrong join types and scan methods, producing query regressions that look like load spikes but are actually optimizer failures.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Run &lt;code&gt;ANALYZE&lt;/code&gt; after bulk loads, raise &lt;code&gt;statistics target&lt;/code&gt; to 500 for join and filter columns on large tables, and create extended statistics for correlated column pairs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: After &lt;code&gt;ANALYZE&lt;/code&gt;, &lt;code&gt;EXPLAIN (ANALYZE)&lt;/code&gt; estimated rows should be within a factor of 2 of actual rows for the primary scan nodes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Run the &lt;code&gt;n_mod_since_analyze&lt;/code&gt; query from Check 4 this week. Any table where &lt;code&gt;mod_pct &gt; 20%&lt;/code&gt; needs an &lt;code&gt;ANALYZE&lt;/code&gt; run today.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;checklist&quot;&gt;Checklist&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Run &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; on the slow query — compare estimated vs actual rows at each node&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;pg_stats&lt;/code&gt; for the filtered columns — check &lt;code&gt;n_distinct&lt;/code&gt;, &lt;code&gt;correlation&lt;/code&gt;, and &lt;code&gt;most_common_vals&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;pg_stat_user_tables&lt;/code&gt; for &lt;code&gt;last_analyze&lt;/code&gt;, &lt;code&gt;last_autoanalyze&lt;/code&gt;, and &lt;code&gt;n_mod_since_analyze&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;last_analyze&lt;/code&gt; is stale or NULL: run &lt;code&gt;ANALYZE tablename&lt;/code&gt; immediately&lt;/li&gt;
&lt;li&gt;Re-run &lt;code&gt;EXPLAIN (ANALYZE)&lt;/code&gt; after &lt;code&gt;ANALYZE&lt;/code&gt; to verify estimates improved&lt;/li&gt;
&lt;li&gt;If estimates still wrong: check for correlated columns in the &lt;code&gt;WHERE&lt;/code&gt; clause&lt;/li&gt;
&lt;li&gt;Raise &lt;code&gt;statistics_target&lt;/code&gt; to 500 for high-cardinality or skewed columns&lt;/li&gt;
&lt;li&gt;Create extended statistics with &lt;code&gt;CREATE STATISTICS (dependencies)&lt;/code&gt; for correlated column pairs&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;ANALYZE&lt;/code&gt; again after any statistics configuration change&lt;/li&gt;
&lt;li&gt;Lower &lt;code&gt;autovacuum_analyze_scale_factor&lt;/code&gt; to 0.01 per-table for high-write tables&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;ANALYZE&lt;/code&gt; calls to ETL pipelines immediately after bulk loads or large deletes&lt;/li&gt;
&lt;li&gt;Add a monitoring query on &lt;code&gt;n_mod_since_analyze&lt;/code&gt; — alert when &lt;code&gt;mod_pct &gt; 15%&lt;/code&gt; on production tables&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>checklist</category><category>failures</category></item><item><title>Aurora Global Database: What It Solves and What It Does Not</title><link>https://rajivonai.com/blog/2024-02-19-aurora-global-database-what-it-solves-and-does-not/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-02-19-aurora-global-database-what-it-solves-and-does-not/</guid><description>Aurora Global Database delivers sub-second cross-region replication and under-one-minute RTO for disaster recovery — but it is not active-active, and application failover is never automatic.</description><pubDate>Mon, 19 Feb 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Aurora Global Database is frequently evaluated as an active-active multi-region database. It is not. The secondary region is read-only until you explicitly promote it, promotion does not re-point your application endpoints, and the RPO on an unplanned failover is measured in seconds, not zero. Understanding what the product actually delivers — and what it leaves to you — is the only way to size it correctly for a DR or read-scale design.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Multi-region database architecture sits at the intersection of two pressures: latency-sensitive reads that cross region boundaries unnecessarily, and disaster recovery designs that require tighter RTO/RPO than a daily snapshot gives you. Aurora Global Database is the AWS answer to both, and the marketing framing — “single database spanning multiple regions” — sounds closer to active-active than the implementation actually is.&lt;/p&gt;
&lt;p&gt;Engineers evaluating Global Database typically encounter it while building a DR failover plan or routing global reads to a closer region. Both use cases are real. The confusion starts when teams assume they compound into active-active behavior.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Aurora Global Database does not detect primary region failure and promote the secondary automatically. Promotion is an API call — manually triggered or triggered by your application logic. The application’s connection string still points at the old primary endpoint after promotion. The database cluster comes up cleanly; your application is still talking to a dead region.&lt;/p&gt;
&lt;p&gt;The “sub-one-minute RTO” claim is precise: it covers the time to promote a new primary cluster. It does not include DNS propagation, application reconfiguration, or connection pool drain. The actual application recovery time is longer, and the gap is entirely under your control rather than Aurora’s.&lt;/p&gt;
&lt;p&gt;What does Aurora Global Database actually guarantee, where does that guarantee stop, and what does your application need to provide for the rest?&lt;/p&gt;
&lt;h2 id=&quot;how-aurora-global-database-replicates&quot;&gt;How Aurora Global Database Replicates&lt;/h2&gt;
&lt;p&gt;Aurora’s replication mechanism is not binlog-based or WAL-shipping-based in the traditional sense. The Aurora storage layer replicates storage-level redo log records directly between regions. According to &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-global-database.html&quot;&gt;AWS Aurora documentation&lt;/a&gt;, this typically achieves under one second of replication lag using dedicated infrastructure separate from database compute nodes. Because replication does not go through the compute layer, writes on the primary are not slowed by cross-region replication — the storage tier handles it asynchronously.&lt;/p&gt;
&lt;p&gt;The secondary cluster can serve reads from its local storage copy. Those reads are up to one second stale. For dashboards, reporting, and non-transactional API endpoints that is fine. For reads that must reflect a just-completed write, it is not.&lt;/p&gt;
&lt;h3 id=&quot;planned-vs-unplanned-failover&quot;&gt;Planned vs. Unplanned Failover&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-global-database-disaster-recovery.html&quot;&gt;AWS documents two distinct failover modes&lt;/a&gt; with different guarantees.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Managed planned failover&lt;/strong&gt; is for intentional region migrations: maintenance, a region move, or a DR drill. Aurora coordinates the promotion, waits for the secondary to fully catch up, and promotes with RPO of zero — no data loss. The original primary must be reachable, and the operation takes longer than a forced failover.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Unplanned failover&lt;/strong&gt; is what you invoke when the primary region has failed. There is no coordination; the secondary region’s data reflects whatever was replicated before the failure. Given sub-one-second typical lag, RPO in practice is low — but it is not zero. AWS documentation states the RPO depends on replication lag at the time of failure.&lt;/p&gt;
&lt;p&gt;The promotion is an API call you must issue explicitly. For an unplanned failover:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; rds&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; failover-global-cluster&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --global-cluster-identifier&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; my-global-cluster&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --target-db-cluster-identifier&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; arn:aws:rds:us-west-2:123456789:cluster:my-secondary-cluster&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --allow-data-loss&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After promotion, the secondary cluster becomes the new writer. Your application’s connection string still points at the old primary endpoint — updating that is separate from the promotion step and is your responsibility.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-global-database.html&quot;&gt;Aurora Global Database user guide&lt;/a&gt; documents three patterns worth internalizing before committing to the architecture.&lt;/p&gt;
&lt;p&gt;Storage-layer replication means the secondary cluster can be promoted without replaying a long log — a genuine DR advantage over traditional streaming replication, where a lagging replica must finish replay before accepting writes.&lt;/p&gt;
&lt;p&gt;Read routing is not automatic. The application must explicitly send reads to the secondary cluster endpoint. Reads on the secondary reflect data up to the current replication lag behind the primary.&lt;/p&gt;
&lt;p&gt;Cost includes storage in both regions (a full copy in each) plus cross-region data transfer for replication. For large databases, storage cost effectively doubles. This is rarely in the first-pass sizing estimate.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Application assumes automatic endpoint failover&lt;/td&gt;&lt;td&gt;Application continues targeting the old primary endpoint after promotion&lt;/td&gt;&lt;td&gt;Aurora promotes the cluster but does not update the application’s connection string&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Writes needed in both regions simultaneously&lt;/td&gt;&lt;td&gt;Active-active writes are not supported&lt;/td&gt;&lt;td&gt;The secondary is read-only until promoted; there is no multi-primary write path&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;RPO must be exactly zero on unplanned failure&lt;/td&gt;&lt;td&gt;RPO on unplanned failover is bounded by replication lag, not guaranteed zero&lt;/td&gt;&lt;td&gt;Only managed planned failover provides zero data loss&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Aurora Global Database does not automatically re-point application traffic after a regional failure, so an untested failover plan typically means manual intervention under pressure during an outage.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Build and test the full failover path — promotion API call, DNS update or connection-string reconfiguration, connection pool reset — as a runbook that runs end-to-end in a staging environment.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: A successful failover drill where the application resumes writes within your RTO target, with the promotion time and application re-point time measured separately.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, find your current RTO target in your DR documentation, then measure how long the non-Aurora steps (DNS propagation, app reconfiguration, connection validation) actually take in your environment. That is your gap.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>architecture</category></item><item><title>Catalog Sync Workflow: Database, Search Index, CDN, and Cache Invalidation</title><link>https://rajivonai.com/blog/2024-02-15-catalog-sync-workflow-database-search-index-cdn-and-cache-invalidation/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-02-15-catalog-sync-workflow-database-search-index-cdn-and-cache-invalidation/</guid><description>Propagating a catalog update from database commit through Elasticsearch, CDN edge cache, and application cache without stranding stale reads downstream.</description><pubDate>Thu, 15 Feb 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A catalog update is not complete when the database transaction commits; it is complete when every reader that can show the product has converged, or has been explicitly allowed to serve stale data.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Product catalogs have become multi-surface systems. A price change may be read from the primary database by checkout, from a search index by the browse page, from a CDN edge by a product detail page, and from an application cache by recommendation or inventory services.&lt;/p&gt;
&lt;p&gt;Each surface exists for a good reason. The database gives transactional truth. The search index gives relevance and filtering. The CDN absorbs global read traffic. The cache keeps hot paths fast and isolates dependencies. None of these systems share the same consistency model.&lt;/p&gt;
&lt;p&gt;That means catalog sync is not a background detail. It is part of the product correctness boundary. If the architecture treats it as a best-effort side effect, the user experience will eventually split: checkout rejects a price shown on the page, search returns deleted products, category pages show stale availability, or a CDN edge keeps serving a retired SKU after the origin has been fixed.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The common failure is coupling the catalog write path to too many downstream effects.&lt;/p&gt;
&lt;p&gt;A simple implementation writes the database row, updates the search document, purges CDN URLs, deletes cache keys, and returns success. It feels direct, but it creates a distributed transaction without transaction semantics. If the database commit succeeds and the search update times out, the system now needs to know whether to retry, reconcile, or roll back. If CDN invalidation is slow, the product page can remain stale even though every internal API is correct. If the cache delete happens before commit, readers can refill old data.&lt;/p&gt;
&lt;p&gt;The reverse design is also dangerous. If sync is fully asynchronous but invisible, operational teams lose the ability to answer basic questions: Which SKUs are behind? Which downstream system is blocking convergence? Is the stale page caused by search lag, cache refill, CDN propagation, or a missing event?&lt;/p&gt;
&lt;p&gt;The core question is this: how do you make catalog updates fast enough for product teams while preserving a clear correctness model across database, search, CDN, and cache?&lt;/p&gt;
&lt;h2 id=&quot;the-catalog-sync-control-plane&quot;&gt;The Catalog Sync Control Plane&lt;/h2&gt;
&lt;p&gt;The answer is to separate the catalog write from catalog propagation, while making propagation observable, replayable, and bounded by explicit freshness contracts.&lt;/p&gt;
&lt;p&gt;The database remains the source of truth. Every catalog mutation writes both the business row and an outbox event in the same transaction. A sync worker reads the outbox, writes derived projections, and records per-target delivery state. Search indexing, CDN invalidation, and cache invalidation are treated as independent subscribers with their own retry policies.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[admin change — price update] --&gt; B[database transaction — catalog row]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[outbox event — committed with row]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D[sync dispatcher — ordered work]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; E[search index writer — product document]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; F[cache invalidator — key set]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; G[CDN invalidator — URL set]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; H[delivery ledger — search status]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; I[delivery ledger — cache status]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; J[delivery ledger — CDN status]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt; K[read freshness view — catalog convergence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  J --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is not just an event-driven architecture. The important part is the control plane around the events.&lt;/p&gt;
&lt;p&gt;First, the outbox is the durable handoff. A catalog change is not considered emitted because an HTTP call was attempted. It is emitted because an outbox record exists in the same commit as the catalog mutation.&lt;/p&gt;
&lt;p&gt;Second, the dispatcher owns idempotency. Every downstream write carries a stable catalog version, such as &lt;code&gt;product_id&lt;/code&gt; plus &lt;code&gt;catalog_version&lt;/code&gt;. Search indexing can safely retry the same document version. Cache invalidation can safely delete the same key more than once. CDN invalidation can deduplicate by path set and version window.&lt;/p&gt;
&lt;p&gt;Third, the read paths are explicit about freshness. Checkout should read the database or a strongly controlled projection. Browse can tolerate search lag if the UI and ranking contracts allow it. CDN-backed pages need short TTLs, versioned URLs, or active invalidation for fields that cannot remain stale.&lt;/p&gt;
&lt;p&gt;Fourth, reconciliation is a first-class workflow. A periodic job compares database versions against search document versions, cache metadata, and CDN invalidation completion records. This catches missed events, poison messages, and downstream outages that retry queues alone may hide.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; The documented pattern is the transactional outbox: persist the state change and the message in the same database transaction, then relay the message asynchronously. This pattern is widely described by Chris Richardson at microservices.io as a way to avoid dual writes between a database and a message broker.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; For catalog sync, the action is to treat the outbox table as the only source of propagation work. The application does not call Elasticsearch, Redis, or CloudFront inside the request transaction. It commits the catalog row and the outbox event, then lets workers advance downstream projections.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The result is not instant consistency. The result is recoverable inconsistency. If the search cluster is unavailable, the database remains correct, the outbox backlog grows, and operators can see exactly which catalog versions have not reached search.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; The practical lesson is that asynchronous does not mean best effort. It means the system accepts temporary lag in exchange for durable retry, replay, and isolation from downstream failures.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; PostgreSQL behavior reinforces the same lesson. A committed row is durable according to the database configuration, but &lt;code&gt;LISTEN&lt;/code&gt; and &lt;code&gt;NOTIFY&lt;/code&gt; are not a durable queue. Notifications can wake workers, but they should not be the only record of catalog work.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Use database polling, logical decoding, or a durable queue fed by the outbox as the real work source. Notifications can reduce latency, but workers must be able to recover from the table itself.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; A worker restart no longer loses product updates. The backlog is still present in the database, ordered by commit metadata or monotonically assigned outbox IDs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; Do not confuse a signal with a ledger. Catalog propagation needs a ledger.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; Elasticsearch and OpenSearch are near-real-time search systems. Indexed documents are not necessarily visible to search immediately after the write; refresh behavior controls when changes become searchable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Store the catalog version in every indexed document and expose sync lag by comparing the latest database version with the searchable version. Use forced refresh only for narrow operational cases, not as the default path for every product edit.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; Search freshness becomes measurable instead of anecdotal. Product teams can decide whether a five-second lag is acceptable for title edits and whether price or availability requires a different path.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; Search is a projection, not the catalog authority.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; CDN invalidation is also not a transaction. Providers such as Amazon CloudFront document invalidation as an asynchronous operation. Edge caches may continue serving old content until expiration or invalidation propagation completes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Use versioned asset URLs where possible, short TTLs for volatile catalog HTML, and targeted invalidations for pages whose stale content creates business risk. Record invalidation request IDs and completion state.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; CDN behavior stops being mysterious. A stale product page can be traced to a known invalidation request, an expected TTL, or a missing path mapping.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; CDN freshness must be designed into URL and TTL strategy; it cannot be patched reliably with broad emergency purges.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Database updated, search stale&lt;/td&gt;&lt;td&gt;Search write failed or refresh has not exposed the document&lt;/td&gt;&lt;td&gt;Outbox retry, versioned documents, search lag dashboards&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cache refilled with old data&lt;/td&gt;&lt;td&gt;Cache delete happened before commit or readers raced the writer&lt;/td&gt;&lt;td&gt;Commit first, then invalidate; use versioned cache keys for critical reads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CDN serves retired page&lt;/td&gt;&lt;td&gt;Edge TTL or invalidation propagation delay&lt;/td&gt;&lt;td&gt;Versioned URLs, targeted invalidation, volatile content TTL limits&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Worker poison message blocks queue&lt;/td&gt;&lt;td&gt;One malformed SKU or payload fails repeatedly&lt;/td&gt;&lt;td&gt;Dead letter queue, per-target isolation, replay tooling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reindex overwrites newer data&lt;/td&gt;&lt;td&gt;Bulk job writes an older document version&lt;/td&gt;&lt;td&gt;Compare versions before write, reject stale projection updates&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Operators cannot explain staleness&lt;/td&gt;&lt;td&gt;No per-target delivery ledger&lt;/td&gt;&lt;td&gt;Track catalog version, target, status, attempt count, and last error&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The hardest tradeoff is deciding which surfaces are allowed to be stale. A product description can usually tolerate propagation delay. Price, legal restrictions, and availability often cannot. The architecture should encode that distinction rather than pretending all catalog fields have the same consistency requirements.&lt;/p&gt;
&lt;p&gt;For high-risk fields, route reads through stronger sources. Checkout should validate against the database or a strongly consistent pricing service. Search can display a product, but checkout must make the final decision. CDN pages can show cached marketing content, but price and availability may need client-side hydration from a fresher API.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Catalog updates fail operationally when the database, search index, CDN, and cache are treated as one implicit transaction.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Use a transactional outbox, independent downstream subscribers, idempotent versioned writes, and a delivery ledger for every propagation target.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The design follows documented behavior of durable database commits, near-real-time search visibility, asynchronous CDN invalidation, and repeatable cache invalidation patterns.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Start by adding &lt;code&gt;catalog_version&lt;/code&gt; to the database row, search document, and cache payload. Then add an outbox table and a dashboard that shows, for each changed SKU, the latest version committed and the latest version visible in search, cache, and CDN.&lt;/p&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Running Production PostgreSQL on Cloud SQL: HA, DR, Backups, Maintenance, and Upgrades</title><link>https://rajivonai.com/blog/2024-02-15-running-production-postgresql-on-cloud-sql-ha-dr-backups/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-02-15-running-production-postgresql-on-cloud-sql-ha-dr-backups/</guid><description>An operational design for Cloud SQL PostgreSQL that separates zonal HA, regional recovery, point-in-time restore, maintenance, and major-version upgrades.</description><pubDate>Thu, 15 Feb 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A regional Cloud SQL instance, a cross-region replica, and point-in-time recovery solve three different incidents. Calling all three “backup” produces a recovery plan that fails exactly when the distinction matters.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A production database must withstand at least four classes of event: a zonal infrastructure failure, a regional outage, accidental or malicious data change, and a planned engine or platform change. The desired RPO and RTO are business requirements; Cloud SQL features do not convert them into guarantees automatically.&lt;/p&gt;
&lt;p&gt;The operating design must state which feature covers each event, who initiates recovery, which endpoint changes, and what the application observes.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Regional HA is not regional disaster recovery. A read replica is not a backup because logical corruption can replicate. An automated backup is not an immediate rollback because restore creates another instance. Maintenance availability is not guaranteed merely because HA is enabled. Major-version upgrades have prechecks, downtime, and rollback constraints.&lt;/p&gt;
&lt;p&gt;The core question is: &lt;strong&gt;how should these mechanisms be composed and tested so the measured recovery behavior satisfies the workload’s objectives?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-recovery-architecture&quot;&gt;The Recovery Architecture&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[Application] --&gt; Primary[Regional HA primary — region A]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt; Standby[HA standby — another zone]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt; DR[Cross-region read replica — region B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt; Backup[Automated and on-demand backups]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt; Logs[Transaction logs for PITR]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Backup --&gt; Restore[New restored instance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Logs --&gt; Restore&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    DR --&gt; Promote[Promoted DR primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Event&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Primary mechanism&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Important boundary&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Zonal failure&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Regional HA failover&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Sessions close; clients reconnect to the same instance endpoint.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Regional outage&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Promote a cross-region replica or restore elsewhere&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Replication is asynchronous; endpoint and connection name change after promotion or restore.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Bad deployment or &lt;code&gt;DROP TABLE&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;PITR to a new instance&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Recovery is not an in-place rewind; reconcile good data and redirect clients deliberately.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Media or administrative recovery&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Automated or on-demand backup restore&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;A successful backup is not evidence that the application can be restored inside its RTO.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Google documents regional HA as synchronous disk replication between zones. It also documents cross-region replicas as asynchronous. That means “zero RPO” must not be promised for regional recovery: observed lag at the failure boundary defines potential loss. For zonal HA, test the service behavior and application retry path rather than converting a typical failover duration into a contractual number.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;h3 id=&quot;make-recovery-a-workflow-not-a-checkbox&quot;&gt;Make recovery a workflow, not a checkbox&lt;/h3&gt;
&lt;p&gt;PITR depends on automated backups and transaction-log retention. A restore creates a new Cloud SQL instance. The runbook must therefore include validation, credentials and IAM, networking, connection-string changes, and a decision about reconciling writes accepted after the chosen restore time.&lt;/p&gt;
&lt;p&gt;Cross-region promotion has a similar control-plane consequence. Promotion makes the replica independent and writable; applications do not automatically discover it as the original primary. Track replica lag, establish an authority for promotion, and prevent the old primary from accepting writes during failover or failback.&lt;/p&gt;
&lt;p&gt;Record recovery evidence in every drill: the last confirmed source transaction, the first confirmed target transaction, DNS or configuration changes, validation queries, and the incident commander’s promotion decision. That produces an auditable RPO and RTO instead of an estimate inferred from a dashboard.&lt;/p&gt;
&lt;h3 id=&quot;treat-maintenance-as-a-client-resilience-test&quot;&gt;Treat maintenance as a client-resilience test&lt;/h3&gt;
&lt;p&gt;Cloud SQL maintenance behavior differs by edition and configuration. Current documentation says Enterprise instances lose connectivity for less than 30 seconds on average during maintenance, while Enterprise Plus supports near-zero-downtime planned maintenance under documented prerequisites and exclusions. Those figures are service descriptions, not universal results. High activity, read replicas, connectors, DDL, and client timeout configuration can change observed impact.&lt;/p&gt;
&lt;p&gt;Use bounded exponential backoff with jitter, fail fast on connection establishment, and make transactions idempotent where they can be retried. A pool that holds dead sockets indefinitely can turn a brief database event into a long application outage.&lt;/p&gt;
&lt;h3 id=&quot;upgrade-with-a-reversible-decision-point&quot;&gt;Upgrade with a reversible decision point&lt;/h3&gt;
&lt;p&gt;Cloud SQL supports in-place major-version upgrades for supported source and target versions. Run the service precheck and review extensions, flags, application drivers, and removed PostgreSQL behavior. Test against a clone or restored copy with production-like data. Record the documented rollback procedure and its eligibility constraints before starting; never describe the operation as “a few minutes” without measuring the actual database.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Why the design fails&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Backup exists but restore misses RTO&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Restore, validation, networking, and cutover were never timed&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Run scheduled restore drills and measure the whole application recovery.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;DR replica loses acknowledged writes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Asynchronous lag existed at promotion&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Monitor lag, define acceptable RPO, and quiesce the primary when possible.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Split authority after regional recovery&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Old and promoted instances both receive writes&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Use explicit fencing, traffic control, and a single incident commander.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Replica disk pressure or lag&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Long queries and write volume delay replay&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Monitor lag and disk; isolate analytics or cancel queries according to a stated policy.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Upgrade rollback is assumed&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Rollback rules were not reviewed before the change&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Keep a tested logical or service-level fallback and explicit go or no-go checkpoints.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; “HA enabled” is being used as a substitute for a complete recovery design.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Map every incident class to HA, replica promotion, PITR, or backup restore and document endpoint and data-loss implications.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Google documents different replication, restore, and client behavior for each mechanism.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Execute four game days: manual HA failover, cross-region promotion, PITR recovery, and a major-version rehearsal on a nonproduction copy.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/high-availability&quot;&gt;Cloud SQL high availability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/replication/cross-region-replicas&quot;&gt;Cross-region read replicas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/backup-recovery/pitr&quot;&gt;Point-in-time recovery&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/backup-recovery/backups&quot;&gt;Cloud SQL backups&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/maintenance&quot;&gt;Maintenance impact&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/upgrade-major-db-version-inplace&quot;&gt;In-place major-version upgrades&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>cloud</category><category>architecture</category><category>databases</category><category>failures</category><category>checklist</category></item><item><title>Why Reads Scale More Easily Than Writes: The Single-Writer Ceiling</title><link>https://rajivonai.com/blog/2024-01-23-why-reads-scale-more-easily-than-writes/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-01-23-why-reads-scale-more-easily-than-writes/</guid><description>Read replicas add query capacity, but sustained write scaling requires a deliberate decision about data ownership, transaction boundaries, and failure recovery.</description><pubDate>Tue, 23 Jan 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A database can have fifteen read replicas and still have exactly one place where every order, payment, balance change, and inventory reservation must commit. Replication copies data; it does not automatically divide write ownership.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A relational database usually begins as the correct architecture. One PostgreSQL or MySQL instance provides transactions, constraints, indexes, backups, and a simple operating model. As the application grows, the database instance grows with it. Read-heavy endpoints move to replicas. Connection pooling protects the server. Slow queries are tuned. Large tables are partitioned. Storage and memory are increased.&lt;/p&gt;
&lt;p&gt;This progression can produce a database topology that looks distributed while its write path remains centralized:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App[applications — orders, payments, inventory] --&gt; Writer[one writer — canonical state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Writer --&gt; ReaderA[reader one — read-only copy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Writer --&gt; ReaderB[reader two — read-only copy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Writer --&gt; ReaderC[reader three — read-only copy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Reports[reports and search] --&gt; ReaderA&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Api[read APIs] --&gt; ReaderB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Batch[batch reads] --&gt; ReaderC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The replicas can absorb product lookups, order history, reporting, and other tolerant reads. They can also improve availability by becoming failover candidates. They do not normally give the application another independent owner for the same rows.&lt;/p&gt;
&lt;p&gt;The problem is not limited to globally famous platforms. A regional retailer can reach it because every checkout updates several indexes. A financial platform can reach it because balance changes contend on a small set of accounts. A software provider can reach it because a few tenants generate most of the writes. Write intensity, transaction shape, and contention matter as much as customer count.&lt;/p&gt;
&lt;p&gt;This article uses one explicit design exercise throughout: &lt;strong&gt;RetailCo&lt;/strong&gt;, a multi-tenant retail platform whose &lt;code&gt;transactions&lt;/code&gt; history has grown to 40 TB. The number is a scenario input, not a claimed production benchmark. Each transaction belongs to a &lt;code&gt;tenant_id&lt;/code&gt;; most customer-facing transactions are tenant-local, while finance and risk reporting span tenants.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;A read can often run against a copy because it observes state without changing the authority for that state. The application may accept a small amount of replica lag, route strict read-after-write requests back to the writer, or tolerate a slightly old report.&lt;/p&gt;
&lt;p&gt;A write has a different job. Before acknowledging a commit, the database may need to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;find and lock the affected rows;&lt;/li&gt;
&lt;li&gt;validate unique, foreign-key, and check constraints;&lt;/li&gt;
&lt;li&gt;order concurrent changes;&lt;/li&gt;
&lt;li&gt;update table and index pages;&lt;/li&gt;
&lt;li&gt;record the change in WAL or the binlog;&lt;/li&gt;
&lt;li&gt;satisfy the configured durability policy;&lt;/li&gt;
&lt;li&gt;expose one committed outcome after a crash or failover.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That coordination is the value of a relational transaction. It is also why adding another writable node is not equivalent to adding another read replica.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Request[transaction request] --&gt; Route[locate authoritative data]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Route --&gt; Lock[coordinate concurrent changes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Lock --&gt; Rules[validate constraints]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Rules --&gt; Log[record durable commit]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Log --&gt; Replicate[replicate committed state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Replicate --&gt; Ack[acknowledge one outcome]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The diagram is deliberately simplified. The exact implementation differs across PostgreSQL, MySQL, distributed SQL systems, and NoSQL databases. The invariant is the important part: every system must decide who owns a write, how conflicting writes are ordered, and what survives failure.&lt;/p&gt;
&lt;p&gt;This creates four distinctions that architecture reviews often blur:&lt;/p&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Capability&lt;/th&gt;&lt;th&gt;What it changes&lt;/th&gt;&lt;th&gt;What it does not prove&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Read replica&lt;/td&gt;&lt;td&gt;Adds capacity for eligible reads&lt;/td&gt;&lt;td&gt;More write throughput&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failover replica&lt;/td&gt;&lt;td&gt;Provides a promotion target&lt;/td&gt;&lt;td&gt;Concurrent independent writers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Table partitioning&lt;/td&gt;&lt;td&gt;Divides one logical table for pruning and lifecycle operations&lt;/td&gt;&lt;td&gt;Independent database compute&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Multi-primary mode&lt;/td&gt;&lt;td&gt;Allows more than one node to accept writes under a coordination protocol&lt;/td&gt;&lt;td&gt;Linear write scaling for one contested dataset&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Reads are not free or perfectly easy. Replica lag can violate read-after-write expectations. Long queries can conflict with replay. A stale reader can make an application believe an update disappeared. The narrower claim is that &lt;strong&gt;read-only work is usually easier to spread because it does not need to establish a new global truth&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The core question is therefore not, “How do we add another writer?” It is:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Can the workload be divided into ownership boundaries so most transactions coordinate locally instead of globally?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;write-scaling-is-a-data-ownership-decision&quot;&gt;Write Scaling Is a Data-Ownership Decision&lt;/h2&gt;
&lt;p&gt;The most useful unit of write scalability is not the server. It is the &lt;strong&gt;write domain&lt;/strong&gt;: the smallest set of data that must commit together under one consistency decision.&lt;/p&gt;
&lt;p&gt;For RetailCo, a tenant-local checkout might update an order, its line items, a tenant inventory reservation, and an idempotency record. If all four tables use the same ownership key, that transaction can remain local to one shard. A risk report that aggregates every tenant does not need to be part of that commit. It can use a separate analytical path.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Checkout[checkout request with tenant identifier] --&gt; Router[ownership router]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Router --&gt; ShardA[writer domain A — tenant group one]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Router --&gt; ShardB[writer domain B — tenant group two]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Router --&gt; ShardC[writer domain C — tenant group three]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ShardA --&gt; Stream[change stream]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ShardB --&gt; Stream&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ShardC --&gt; Stream&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Stream --&gt; Analytics[reporting and risk platform]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This design adds write capacity because the three writer domains own different data. A transaction in Shard A does not normally need Shard B to approve its commit. The architecture has not eliminated coordination; it has reduced the scope of coordination.&lt;/p&gt;
&lt;p&gt;That is the common mechanism behind several different-looking solutions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Application sharding&lt;/strong&gt; routes each tenant, account, or customer to a database cluster.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Distributed relational databases&lt;/strong&gt; use a distribution key and database-managed routing to place data across nodes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Domain decomposition&lt;/strong&gt; moves orders, payments, inventory, or other consistency domains into separately owned databases.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Partition-oriented NoSQL databases&lt;/strong&gt; place records by partition or shard key and provide consistency guarantees within defined boundaries.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The product choice matters, but the ownership decision comes first. A poor key can place the busiest tenants on one shard, split transactions across nodes, or force every request through distributed coordination. A good key keeps the dominant write path local, has enough cardinality to spread load, and can be carried through related tables and requests.&lt;/p&gt;
&lt;h3 id=&quot;five-exits-from-the-ceiling&quot;&gt;Five exits from the ceiling&lt;/h3&gt;
&lt;p&gt;The writer reaching a limit does not automatically justify sharding. There are five distinct responses, and they solve different problems.&lt;/p&gt;
&lt;h4 id=&quot;1-remove-avoidable-work-and-scale-up&quot;&gt;1. Remove avoidable work and scale up&lt;/h4&gt;
&lt;p&gt;If the writer is spending capacity on missing indexes, excessive indexes, repeated small commits, connection storms, accidental full scans, or synchronous reporting, fix those first. A larger writer can provide useful headroom while the data model is still fundamentally sound.&lt;/p&gt;
&lt;p&gt;This is the lowest-complexity option. It is also finite. Scaling up delays the ownership decision; it does not change a one-writer architecture.&lt;/p&gt;
&lt;h4 id=&quot;2-partition-tables-inside-the-existing-database&quot;&gt;2. Partition tables inside the existing database&lt;/h4&gt;
&lt;p&gt;Time partitioning can make retention, archival, vacuum scope, and partition-pruned queries easier to operate. It is valuable for RetailCo’s 40-TB history. But all partitions can still share the same database instance, WAL path, buffer pool, and commit capacity.&lt;/p&gt;
&lt;p&gt;Native partitioning is therefore an operational companion to write scaling, not proof of it. The key must match the access pattern, as discussed in &lt;a href=&quot;https://rajivonai.com/blog/2023-08-21-partitioning-is-not-a-performance-feature-by-default/&quot;&gt;Partitioning Is Not a Performance Feature by Default&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id=&quot;3-shard-by-an-ownership-key&quot;&gt;3. Shard by an ownership key&lt;/h4&gt;
&lt;p&gt;Sharding creates independent write domains. For RetailCo, &lt;code&gt;tenant_id&lt;/code&gt; is a credible candidate because the reference workload says most OLTP transactions are tenant-local. Each shard can have its own writer and replicas.&lt;/p&gt;
&lt;p&gt;The cost is explicit: routing, tenant placement, resharding, cross-shard queries, schema rollout, backups, and incident response now span multiple clusters. A very large tenant can still create a hot shard. Cross-tenant uniqueness and foreign keys no longer come for free.&lt;/p&gt;
&lt;h4 id=&quot;4-use-a-distributed-relational-database&quot;&gt;4. Use a distributed relational database&lt;/h4&gt;
&lt;p&gt;Systems such as &lt;a href=&quot;https://docs.citusdata.com/en/stable/sharding/data_modeling.html&quot;&gt;Citus&lt;/a&gt;, &lt;a href=&quot;https://vitess.io/docs/23.0/overview/whatisvitess/&quot;&gt;Vitess 23&lt;/a&gt;, &lt;a href=&quot;https://cloud.google.com/spanner/docs/schema-and-data-model&quot;&gt;Spanner&lt;/a&gt;, &lt;a href=&quot;https://docs.pingcap.com/tidb/v8.5/tidb-architecture/&quot;&gt;TiDB 8.5&lt;/a&gt;, and &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/limitless.html&quot;&gt;Aurora PostgreSQL Limitless Database&lt;/a&gt; move some routing, placement, and distributed transaction machinery into the database platform. They can reduce application-managed sharding work.&lt;/p&gt;
&lt;p&gt;They do not remove data-model physics. Citus still needs a distribution column. Spanner warns that poor primary-key design can hotspot a split. Vitess needs a sharded keyspace and VSchema. Compatibility, transaction behavior, operational control, and migration effort differ substantially. Those distinctions deserve individual articles and lab plans rather than one generic “distributed SQL” label.&lt;/p&gt;
&lt;h4 id=&quot;5-decompose-the-write-path-by-business-domain&quot;&gt;5. Decompose the write path by business domain&lt;/h4&gt;
&lt;p&gt;Orders, payments, inventory, customer identity, and audit data do not always require one transaction boundary. Separating them can give each domain an independent writer and failure boundary.&lt;/p&gt;
&lt;p&gt;The trade is local ACID for explicit coordination between domains. Idempotency, outbox records, event delivery, compensation, and reconciliation become part of the application contract. Decomposition should follow consistency boundaries, not an arbitrary microservice count.&lt;/p&gt;
&lt;h3 id=&quot;diagnose-the-ceiling-before-choosing&quot;&gt;Diagnose the ceiling before choosing&lt;/h3&gt;
&lt;p&gt;An architecture review needs evidence that the writer is the constraint and evidence about what consumes it. Capture at least:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;write transactions per second and their mix;&lt;/li&gt;
&lt;li&gt;commit latency at P50, P95, and P99;&lt;/li&gt;
&lt;li&gt;CPU, storage latency, throughput, and queue depth;&lt;/li&gt;
&lt;li&gt;WAL or binlog generation rate;&lt;/li&gt;
&lt;li&gt;lock wait time, deadlocks, and hot rows;&lt;/li&gt;
&lt;li&gt;index count and write amplification on the busiest tables;&lt;/li&gt;
&lt;li&gt;connection count and time waiting for a connection;&lt;/li&gt;
&lt;li&gt;percentage of transactions that are tenant-local, account-local, or domain-local;&lt;/li&gt;
&lt;li&gt;replica lag and the volume of reads still sent to the writer.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;PostgreSQL exposes cumulative database and WAL counters that can support an interval-based baseline:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    datname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    xact_commit,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    xact_rollback,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tup_inserted,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tup_updated,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tup_deleted,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    temp_bytes,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    deadlocks,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    stats_reset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_database&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; datname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; current_database();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    wal_records,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    wal_fpi,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    wal_bytes,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    stats_reset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_wal;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Take deltas over a known interval and correlate them with latency, locks, and storage metrics. These queries do not prove that sharding is necessary. They prevent an expensive architecture change from being justified by a vague statement such as “the database is slow.”&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; PostgreSQL documents hot standby as a strictly read-only mode. Normal data-changing statements are not accepted, and queries on the standby can be canceled when they conflict with WAL replay: &lt;a href=&quot;https://www.postgresql.org/docs/current/hot-standby.html&quot;&gt;PostgreSQL hot standby&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Use a standby for eligible read traffic, but identify endpoints that require read-after-write consistency and route them according to an explicit consistency policy.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Read capacity and failover coverage can increase without changing the database’s authoritative write location.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; A readable standby is a copy of committed state, not another independent owner of that state.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Amazon Aurora documents a cluster architecture with one primary writer and up to 15 read-only Aurora Replicas: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Concepts.AuroraHighAvailability.html&quot;&gt;High availability for Amazon Aurora&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Route tolerant &lt;code&gt;SELECT&lt;/code&gt; traffic to the reader endpoint and reserve the writer for transactions that must change state.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented architecture scales eligible reads and provides promotion candidates, while the traditional cluster’s write path still terminates at its writer.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; A large replica count can coexist with a single-writer ceiling.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; MySQL documents traditional source-to-replica replication as asynchronous by default. It separately documents Group Replication in single-primary and multi-primary modes, where concurrent transactions are checked and certified and conflicts can be rolled back: &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/group-replication-primary-secondary-replication.html&quot;&gt;MySQL replication architecture&lt;/a&gt;, &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/group-replication-deploying-in-multi-primary-or-single-primary-mode.html&quot;&gt;Group Replication modes&lt;/a&gt;, and &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/group-replication.html&quot;&gt;Group Replication&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Evaluate multi-primary operation with the actual contention pattern, conflict rate, commit latency, and network topology. Do not use node count as the expected write-scaling factor.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The test distinguishes additional write entry points from independent ownership domains.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Multi-primary is a replication and coordination topology. Horizontal write scaling still depends on how the workload is partitioned.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Citus documents that the distribution column determines how rows are assigned to shards and that colocating related tables makes joins and foreign keys local where possible: &lt;a href=&quot;https://docs.citusdata.com/en/stable/sharding/data_modeling.html&quot;&gt;Citus data modeling&lt;/a&gt;. Google Cloud similarly documents that Spanner’s primary-key design determines data distribution and that a poor key can hotspot a split: &lt;a href=&quot;https://cloud.google.com/spanner/docs/primary-keys-overview&quot;&gt;Spanner primary keys&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Model the ownership key across the full transaction before selecting the platform. Verify that related rows carry the key and that the key spreads the busiest tenants or accounts.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The database can route the common transaction to a bounded set of nodes instead of turning it into routine distributed coordination.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Distributed write capacity begins with data placement, not with the number of database nodes.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Option&lt;/th&gt;&lt;th&gt;Good fit&lt;/th&gt;&lt;th&gt;Where it breaks&lt;/th&gt;&lt;th&gt;Operational cost&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Tune and scale up&lt;/td&gt;&lt;td&gt;Avoidable writer work or near-term headroom&lt;/td&gt;&lt;td&gt;Instance ceiling remains; larger failures have larger impact&lt;/td&gt;&lt;td&gt;Low to medium&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Read replicas&lt;/td&gt;&lt;td&gt;Read-heavy endpoints that tolerate replica semantics&lt;/td&gt;&lt;td&gt;Writer CPU, commit path, and hot-row contention remain&lt;/td&gt;&lt;td&gt;Low to medium&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Native table partitioning&lt;/td&gt;&lt;td&gt;Retention, pruning, and large-table maintenance&lt;/td&gt;&lt;td&gt;One database still owns all commits&lt;/td&gt;&lt;td&gt;Medium&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application sharding&lt;/td&gt;&lt;td&gt;Stable tenant or account key with local transactions&lt;/td&gt;&lt;td&gt;Cross-shard joins, global constraints, tenant movement&lt;/td&gt;&lt;td&gt;High&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Distributed relational database&lt;/td&gt;&lt;td&gt;Relational workload needing managed data distribution&lt;/td&gt;&lt;td&gt;Compatibility gaps, distributed coordination, key hotspots&lt;/td&gt;&lt;td&gt;Medium to high&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Domain decomposition&lt;/td&gt;&lt;td&gt;Clear business boundaries with different consistency needs&lt;/td&gt;&lt;td&gt;Cross-domain workflows, retries, reconciliation&lt;/td&gt;&lt;td&gt;High application complexity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Multi-primary replication&lt;/td&gt;&lt;td&gt;Availability or geographically placed write entry under controlled conflict patterns&lt;/td&gt;&lt;td&gt;Certification conflicts and shared-data coordination&lt;/td&gt;&lt;td&gt;Medium to high&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Partition-oriented NoSQL&lt;/td&gt;&lt;td&gt;Access patterns fit partition-local operations and documented consistency guarantees&lt;/td&gt;&lt;td&gt;Relational joins and broad multi-record transactions may require redesign&lt;/td&gt;&lt;td&gt;Medium to high&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Three failure patterns deserve particular attention.&lt;/p&gt;
&lt;p&gt;First, &lt;strong&gt;the wrong shard key converts scale-out into a hotspot&lt;/strong&gt;. A monotonically increasing key, one dominant tenant, or a low-cardinality brand identifier can direct too much traffic to one place.&lt;/p&gt;
&lt;p&gt;Second, &lt;strong&gt;cross-shard transactions can rebuild the original bottleneck at the coordination layer&lt;/strong&gt;. If checkout routinely updates multiple ownership domains, more shards may add network and failure modes without adding useful concurrency.&lt;/p&gt;
&lt;p&gt;Third, &lt;strong&gt;moving existing data is part of the architecture&lt;/strong&gt;. A 40-TB table cannot be treated as if every row appears in the new topology at once. Backfill, dual-read or controlled cutover, change capture, validation, rollback, and tenant-by-tenant migration need explicit designs.&lt;/p&gt;
&lt;p&gt;No architecture should be approved without answers for backup consistency, shard loss, router failure, split-brain prevention, schema rollout, tenant relocation, and recovery of one mistakenly deleted tenant.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;For the RetailCo reference scenario, the recommended direction is &lt;strong&gt;tenant-based write ownership, with time partitioning inside each shard&lt;/strong&gt;—but only after workload evidence confirms that the dominant OLTP transactions are tenant-local.&lt;/p&gt;
&lt;p&gt;The staged recommendation is:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Remove avoidable writer work and move eligible reads and reporting away from the writer.&lt;/li&gt;
&lt;li&gt;Preserve native time partitioning for retention and manageable table operations; do not count it as independent write capacity.&lt;/li&gt;
&lt;li&gt;Route tenant-local orders, line items, inventory reservations, and idempotency records by &lt;code&gt;tenant_id&lt;/code&gt; to independent writer domains.&lt;/li&gt;
&lt;li&gt;Move cross-tenant risk and finance reporting to a change-fed analytical path instead of querying every shard in the checkout path.&lt;/li&gt;
&lt;li&gt;Decompose payments or inventory only where their consistency and recovery boundaries are genuinely different.&lt;/li&gt;
&lt;li&gt;Choose between application sharding and a distributed relational platform after testing compatibility, resharding, failure recovery, and the exact cross-shard transaction rate.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For this scenario, adding more read replicas is useful but insufficient. Multi-primary replication is not the first recommendation because RetailCo needs separated write ownership, not merely more nodes that can receive writes to shared data. Replacing the relational system with Cassandra or MongoDB is also not the first recommendation; that decision would require the access patterns and consistency contract to fit those databases, not merely a desire to escape the current writer.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Good fit:&lt;/strong&gt; tenant-local sharding with time partitioning inside each shard.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Poor fit:&lt;/strong&gt; a low-cardinality &lt;code&gt;brand_id&lt;/code&gt; shard key, routine cross-tenant OLTP transactions, or a design that routes every distributed commit through one coordinator.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Main advantage:&lt;/strong&gt; write capacity can grow by adding independently owned tenant groups.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Main limitation:&lt;/strong&gt; routing, movement, recovery, and cross-shard operations become explicit platform responsibilities.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Operational complexity:&lt;/strong&gt; high enough to require automation and failure testing before migration.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Migration complexity:&lt;/strong&gt; high for 40 TB, so the unit of migration should be a tenant or tenant group with measurable reconciliation.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Read replicas have reduced query pressure, but every state change still commits through one writer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Define a stable ownership key and keep the dominant transaction inside one write domain; use partitioning, sharding, or decomposition for the problem each one actually solves.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Vendor documentation across PostgreSQL, Aurora, MySQL, Citus, and Spanner shows the same boundary: replicas copy state, while scalable writes depend on routing and coordinating ownership.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Measure the top twenty write transactions, record every table and ownership key they touch, and calculate what percentage can commit within one tenant, account, or business domain. That number should drive the next architecture—not the product label.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>cloud</category></item><item><title>Black Friday Database Readiness: Hot Keys, Connection Pools, Cache Misses, and Queue Depth</title><link>https://rajivonai.com/blog/2024-01-01-black-friday-database-readiness-hot-keys-connection-pools-cache-misses-and-queue-depth/</link><guid isPermaLink="true">https://rajivonai.com/blog/2024-01-01-black-friday-database-readiness-hot-keys-connection-pools-cache-misses-and-queue-depth/</guid><description>Hot key contention, connection pool exhaustion, and cache miss bursts each hit local thresholds before aggregate dashboards show anything alarming.</description><pubDate>Mon, 01 Jan 2024 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Black Friday does not usually take databases down because the average load was underestimated. It takes them down because one partition, one pool, one cache path, or one queue crosses a local limit before the aggregate dashboard looks frightening.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Seasonal traffic used to be mostly a capacity planning exercise: add replicas, raise instance classes, warm caches, and staff the incident bridge. That model worked when the bottleneck was broad, predictable, and mostly proportional to request volume.&lt;/p&gt;
&lt;p&gt;Modern commerce systems fail differently. Traffic is shaped by product drops, influencer links, personalized promotions, mobile push campaigns, fraud checks, inventory reservations, payment retries, and recommendation widgets. A single discounted item can concentrate reads and writes on one database key. A small cache invalidation can create a thundering herd. A retry policy can multiply load after the first timeout. A queue that looked harmless at steady state can become a second outage when workers recover too slowly.&lt;/p&gt;
&lt;p&gt;The readiness question is no longer, “Can the database handle 5x traffic?” The better question is, “Which local limit fails first when demand is uneven?”&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Most readiness reviews over-index on database size and under-index on shape.&lt;/p&gt;
&lt;p&gt;A primary database may have enough CPU but still collapse because the application opens too many connections. A distributed key-value store may have enough total provisioned throughput but throttle a single hot partition. A cache may show a strong hit rate while the misses all land on the same expensive query. A queue may absorb a burst but hide the fact that downstream workers cannot drain it before customer state becomes stale.&lt;/p&gt;
&lt;p&gt;These are not independent failures. They compound.&lt;/p&gt;
&lt;p&gt;When cache misses rise, application latency rises. When latency rises, clients and workers retry. When retries rise, connection pools stay occupied longer. When pools saturate, requests wait in the application. When request waits exceed timeouts, more retries are emitted. The database sees not the original Black Friday traffic, but the original traffic plus duplicated work from every layer trying to recover.&lt;/p&gt;
&lt;p&gt;That is why aggregate metrics lie. A database at 55 percent CPU can still be unavailable to the checkout path. A cache at 92 percent hit rate can still be melting the product-detail query. A queue with “only” 200,000 messages can be unrecoverable if the oldest message age is growing faster than the business can tolerate.&lt;/p&gt;
&lt;p&gt;The core question is: how do you design Black Friday readiness around local saturation, not average capacity?&lt;/p&gt;
&lt;h2 id=&quot;the-answer-partition-aware-backpressure&quot;&gt;The Answer: Partition-Aware Backpressure&lt;/h2&gt;
&lt;p&gt;The architecture should treat the database as one constrained participant in a wider control system. The goal is not to make every request succeed. The goal is to preserve the critical path, shed nonessential work early, and keep recovery possible.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[traffic sources — web mobile campaigns] --&gt; B[edge controls — rate limits and bot filters]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[application tier — bounded worker pools]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D[connection pool — fixed database concurrency]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; E[cache tier — prewarmed keys and request coalescing]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; F[database reads — replicas and partition aware access]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; G[write path — idempotent commands]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; H[queue — bounded depth and age alerts]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt; I[workers — controlled drain rate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; J[database writes — hot key protection]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; K[observability — per key and per dependency signals]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  J --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  K --&gt; L[load shedding — preserve checkout and payment]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This model has four operating principles.&lt;/p&gt;
&lt;p&gt;First, isolate hot keys before the event. The dangerous keys are not always obvious from normal traffic. They are launch products, coupon records, inventory counters, cart rows, session records, and configuration flags. For distributed databases, partition-key design determines whether load spreads or concentrates. For relational databases, the same problem appears as row-level contention, index-page contention, or a small number of queries dominating lock waits.&lt;/p&gt;
&lt;p&gt;Second, bound database concurrency at the application edge. A connection pool is not a queueing system of last resort. It is a concurrency governor. If the database can safely process 300 active checkout queries, allowing 3,000 application threads to wait on connections only increases tail latency and failure amplification. Pool wait time should be a first-class signal, not an incidental metric.&lt;/p&gt;
&lt;p&gt;Third, make cache misses boring. Cache readiness is not just prewarming. It includes request coalescing, jittered expiration, stale-while-revalidate behavior where correctness allows it, and explicit protection for expensive miss paths. The failure to avoid is one popular key expiring globally and causing every application instance to recompute it at once.&lt;/p&gt;
&lt;p&gt;Fourth, manage queues by age and drain rate, not just count. Queue depth is useful, but age tells the operational truth. If orders, inventory reservations, emails, search indexing, or fraud reviews are delayed, the business impact depends on how old the oldest work is and whether workers are catching up. A bounded queue with clear admission control is safer than an infinite buffer that turns a transient overload into hours of inconsistent customer state.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; Amazon DynamoDB documents that effective partition-key design matters because uneven access patterns can concentrate traffic and cause throttling even when a table has broader capacity available. The documented pattern is not “buy more capacity”; it is to distribute workload across partition keys and monitor throttling at the access-pattern level.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; For Black Friday readiness, model every high-volume operation by key shape: product ID, customer ID, cart ID, coupon ID, inventory SKU, and campaign ID. Identify keys likely to receive fan-in from promotions. Add synthetic load tests that focus traffic on those keys instead of only replaying average production ratios.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The result is a failure model that exposes hot partitions and contested rows before launch. It also gives teams a concrete mitigation list: key sharding, read replicas, cached derived views, asynchronous counters, reservation tokens, or explicit per-key rate limits.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; A database that scales horizontally still needs workload shape discipline. Partition-aware systems reward even distribution and punish accidental celebrity keys.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; PostgreSQL uses a process-per-connection model, and each active connection consumes server resources. PgBouncer exists because many applications need connection pooling in front of PostgreSQL rather than unbounded direct client connections.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Set connection budgets from the database inward. Reserve capacity for administrative access, migrations, payment-critical paths, and background workers. Configure application pools so their combined maximum cannot exceed the safe database budget. Alert on pool wait time, not only open connection count.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; During overload, callers wait or fail before the database is forced into a larger collapse. This creates a cleaner degradation mode: noncritical endpoints can be shed while checkout and payment retain predictable access.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; Connection pools are not merely performance tuning. They are admission control.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; The Amazon Builders’ Library describes retries as powerful but dangerous when they amplify load against an already-failing dependency. The documented pattern is to use timeouts, capped retries, backoff, and jitter so recovery traffic does not synchronize.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Audit every database-facing and queue-facing client before peak traffic. Remove retry loops that can multiply writes without idempotency. Add jitter to cache refresh and retry behavior. Use circuit breakers or load shedding for nonessential reads such as recommendations, review widgets, and recently viewed items.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The system sends less duplicated work during partial failure. Recovery becomes possible because the database is not competing with synchronized retries from every caller.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; Black Friday resilience depends as much on client behavior as database capacity.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Early signal&lt;/th&gt;&lt;th&gt;Typical bad response&lt;/th&gt;&lt;th&gt;Better response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Hot product key&lt;/td&gt;&lt;td&gt;Per-key latency or throttling rises&lt;/td&gt;&lt;td&gt;Add broad capacity only&lt;/td&gt;&lt;td&gt;Shard key, cache reads, cap per-key concurrency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Pool saturation&lt;/td&gt;&lt;td&gt;Pool wait time rises before database CPU&lt;/td&gt;&lt;td&gt;Increase max connections&lt;/td&gt;&lt;td&gt;Reduce concurrency, shed lower-priority work&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cache stampede&lt;/td&gt;&lt;td&gt;Miss rate rises on a small key set&lt;/td&gt;&lt;td&gt;Scale database replicas late&lt;/td&gt;&lt;td&gt;Coalesce requests, jitter TTLs, serve stale data where safe&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Queue overload&lt;/td&gt;&lt;td&gt;Oldest message age keeps growing&lt;/td&gt;&lt;td&gt;Add producers or retry faster&lt;/td&gt;&lt;td&gt;Slow admission, scale workers carefully, protect downstream writes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Retry storm&lt;/td&gt;&lt;td&gt;Dependency calls exceed user requests&lt;/td&gt;&lt;td&gt;Raise timeouts globally&lt;/td&gt;&lt;td&gt;Cap retries, add jitter, enforce idempotency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica lag&lt;/td&gt;&lt;td&gt;Read-after-write paths become inconsistent&lt;/td&gt;&lt;td&gt;Send all reads to primary&lt;/td&gt;&lt;td&gt;Route critical reads carefully, degrade stale features&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;These controls have tradeoffs. Per-key limits can disappoint customers during a popular drop. Stale cache reads can show inventory that is no longer exact. Queue admission control can defer noncritical work. Smaller connection pools can make failures visible earlier.&lt;/p&gt;
&lt;p&gt;Those are acceptable costs when chosen deliberately. The alternative is uncontrolled collapse where every path competes with every other path and the database becomes the place where product, platform, and customer pain all meet.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Average-load planning misses the local limits that break during Black Friday: hot keys, saturated pools, synchronized cache misses, and unbounded queues.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build partition-aware backpressure across the edge, application pools, cache layer, write queues, and database access paths.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Known systems such as DynamoDB, PostgreSQL with PgBouncer, and retry guidance from the Amazon Builders’ Library all point to the same operating lesson: shape and admission control matter as much as raw capacity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run peak-readiness tests that concentrate traffic on the riskiest keys, enforce database connection budgets, test cache-expiration storms, alert on queue age, and rehearse load shedding before the sale begins.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Search Indexes in Commerce: Why Elasticsearch Is Not the Source of Truth</title><link>https://rajivonai.com/blog/2023-12-02-search-indexes-in-commerce-why-elasticsearch-is-not-the-source-of-truth/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-12-02-search-indexes-in-commerce-why-elasticsearch-is-not-the-source-of-truth/</guid><description>Elasticsearch is a read index, not a record system — routing writes through it creates catalog drift that surfaces only after orders are placed.</description><pubDate>Sat, 02 Dec 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The fastest way to corrupt a commerce platform is to let the system that finds products become the system that decides what products are true.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Commerce teams reach for Elasticsearch because the user experience demands it. Product listing pages need faceted filters. Search boxes need typo tolerance, ranking, synonyms, and language-aware tokenization. Merchandising teams need boosted products, curated collections, and category rules. Buyers expect search to feel instant even when the catalog has millions of SKUs.&lt;/p&gt;
&lt;p&gt;A relational database is rarely the right serving layer for that experience. The transactional catalog stores products, variants, prices, inventory policies, category assignments, eligibility rules, and publishing state. Search wants something else: a denormalized document shaped for retrieval. One product document might contain title tokens, normalized attributes, category breadcrumbs, brand fields, popularity scores, availability flags, and precomputed price ranges.&lt;/p&gt;
&lt;p&gt;That separation is healthy. The operational mistake is forgetting that the search document is a projection.&lt;/p&gt;
&lt;p&gt;Elasticsearch is excellent at serving a read model. It is not the canonical catalog. It is not the pricing ledger. It is not the inventory authority. It is not the publishing workflow. It is a derived index optimized for retrieval, and every derived index can be stale, incomplete, or wrong.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Search indexes fail in ways that look harmless until they touch money.&lt;/p&gt;
&lt;p&gt;A product rename misses the indexer and customers keep seeing the old title. A price update lands in the transactional database but not in search, so listing pages show one price and checkout shows another. Inventory moves to zero, but cached search results continue to present the item as available. A product is unpublished for legal, compliance, or supplier reasons, but remains discoverable because deletion from the index failed. A backfill overwrites newer documents with older snapshots. A retry duplicates a stale event. A partial outage silently creates a gap.&lt;/p&gt;
&lt;p&gt;These are not Elasticsearch bugs. They are boundary bugs.&lt;/p&gt;
&lt;p&gt;The root cause is usually architectural ambiguity. If services read from Elasticsearch as though it were authoritative, the index becomes part database, part cache, part workflow state, and part operational hazard. Teams then patch individual symptoms: manual reindex buttons, admin scripts, replay jobs, delete queues, and dashboard alerts. Those are useful tools, but they cannot fix the deeper question.&lt;/p&gt;
&lt;p&gt;If the search index is allowed to disagree with the commerce system, which one wins?&lt;/p&gt;
&lt;h2 id=&quot;source-of-truth-projection-of-search&quot;&gt;Source of Truth, Projection of Search&lt;/h2&gt;
&lt;p&gt;The answer is to make the ownership boundary explicit: transactional systems own facts; search owns retrieval.&lt;/p&gt;
&lt;p&gt;In a commerce platform, facts include product identity, publication state, variant structure, price rules, inventory policy, fulfillment eligibility, and compliance status. These belong in systems that provide transactional semantics, durable writes, validation, and auditability.&lt;/p&gt;
&lt;p&gt;Search documents are projections built from those facts. They should be disposable. If the index is deleted, corrupted, or rebuilt with a new schema, the business should lose search availability or freshness for a period, not the catalog itself.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[commerce admin — product edits] --&gt; B[catalog database — canonical product state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C[pricing service — canonical price state] --&gt; D[event log — durable change stream]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E[inventory service — canonical availability state] --&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; F[indexer workers — build search documents]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; G[elasticsearch — retrieval projection]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; H[storefront search — ranked discovery]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt; I[product detail page — confirm canonical state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This architecture has a simple rule: Elasticsearch can help customers discover candidates, but the transaction path must verify canonical state before showing final commitments or accepting an order.&lt;/p&gt;
&lt;p&gt;The product listing page may use Elasticsearch to show searchable results. The product detail page can still hydrate critical fields from canonical services or a separately validated read model. Checkout must never trust search for price, availability, eligibility, or purchasability.&lt;/p&gt;
&lt;p&gt;That does not mean every request has to fan out to every source system. Mature platforms often introduce additional read models, caches, and materialized views. The point is not that only one database may serve reads. The point is that each derived model must have a declared authority boundary, freshness expectation, rebuild path, and conflict policy.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; The documented pattern is Command Query Responsibility Segregation: separate the model used to accept writes from the model used to answer reads. In commerce search, the write model is the catalog, pricing, and inventory authority. The query model is the search document.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Treat the search document as a CQRS read model. Build it from committed changes, not from best-effort application side effects. Common implementations use a transactional outbox, change data capture, or a durable event log. The important property is that catalog changes and indexable changes are not split across two unrelated writes where one can commit and the other can disappear.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Search becomes operationally recoverable. If an index mapping changes, rebuild from canonical data. If an indexer falls behind, measure lag and drain the queue. If a worker processes the same event twice, idempotent document writes converge on the same result. If a stale event arrives after a newer one, version checks or monotonic sequence numbers prevent regression.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The indexer is part of the data plane, not a background convenience. It needs replay, dead-letter handling, schema versioning, observability, and backpressure. A search outage is visible; silent search drift is worse.&lt;/p&gt;
&lt;p&gt;Elasticsearch’s own behavior reinforces this design. Documents are searchable after refresh, not necessarily immediately after write. Bulk indexing can partially fail. Distributed systems can retry, reorder, or duplicate work around failures. None of that is surprising; it is exactly why a search index should not be the place where business truth is born.&lt;/p&gt;
&lt;p&gt;The known pattern is therefore not “sync database rows into Elasticsearch.” It is “publish durable facts, build disposable projections, and verify money-moving decisions against authority.”&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;What happens&lt;/th&gt;&lt;th&gt;Architecture response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Index lag&lt;/td&gt;&lt;td&gt;Search shows old product data&lt;/td&gt;&lt;td&gt;Expose lag metrics and define freshness budgets&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Partial indexing failure&lt;/td&gt;&lt;td&gt;Some products disappear or retain stale fields&lt;/td&gt;&lt;td&gt;Use durable retries, dead-letter queues, and replayable events&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Stale overwrite&lt;/td&gt;&lt;td&gt;Older events replace newer documents&lt;/td&gt;&lt;td&gt;Store source version or sequence number in each indexed document&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Mapping migration&lt;/td&gt;&lt;td&gt;New search schema cannot read old documents cleanly&lt;/td&gt;&lt;td&gt;Build a new index, backfill, validate counts, then switch alias&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Search as checkout input&lt;/td&gt;&lt;td&gt;Customer sees wrong price or availability&lt;/td&gt;&lt;td&gt;Revalidate canonical price and inventory before commitment&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Manual index edits&lt;/td&gt;&lt;td&gt;Operators repair symptoms that later get overwritten&lt;/td&gt;&lt;td&gt;Make canonical data the only durable correction path&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Product deletion drift&lt;/td&gt;&lt;td&gt;Unpublished items remain searchable&lt;/td&gt;&lt;td&gt;Model publication state explicitly and include deletion events in replay&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backfill overload&lt;/td&gt;&lt;td&gt;Reindexing harms live traffic&lt;/td&gt;&lt;td&gt;Throttle workers and isolate bulk pipelines from interactive search&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;This design has tradeoffs. It adds infrastructure. It introduces eventual consistency. It forces teams to define ownership rather than letting every service read whatever is convenient. But the alternative is worse: a commerce system where the retrieval layer quietly becomes a second catalog with weaker guarantees and unclear accountability.&lt;/p&gt;
&lt;p&gt;The hard part is not writing to Elasticsearch. The hard part is proving that what Elasticsearch serves is a faithful, bounded, and rebuildable projection of the commerce facts.&lt;/p&gt;
&lt;p&gt;Good platforms make that proof routine. They compare canonical product counts against indexed counts. They sample documents and validate key fields. They track indexing lag by partition and event type. They test reindexing before emergencies. They keep old indexes until new ones are verified. They design search ranking experiments so they cannot mutate canonical product state.&lt;/p&gt;
&lt;p&gt;Most importantly, they keep the user journey honest. Search can rank candidates. Browse can filter projections. Recommendations can suggest products. But product detail, cart, and checkout must converge on the same authoritative answer: is this item sellable, at this price, under these rules, right now?&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Your search index is probably carrying more authority than intended. Audit every consumer of Elasticsearch and mark which fields are discovery-only versus business-critical.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Move canonical ownership back to catalog, pricing, inventory, and policy systems. Feed search through durable events, transactional outbox, or change data capture.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Add drift detection: indexed count versus canonical count, sampled field comparison, index lag by event stream, failed bulk item rates, and stale version rejection.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Make the index disposable. Practice rebuilding it from source data, switching aliases, replaying missed changes, and validating that checkout never depends on Elasticsearch truth.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Google Cloud PostgreSQL for DBAs: The Cloud SQL Operating Model</title><link>https://rajivonai.com/blog/2023-11-15-google-cloud-postgresql-for-dbas/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-11-15-google-cloud-postgresql-for-dbas/</guid><description>A production-focused guide to Cloud SQL for PostgreSQL boundaries, regional HA, connectivity, Terraform, and failure testing.</description><pubDate>Wed, 15 Nov 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Cloud SQL removes host and storage administration; it does not remove database engineering. The dangerous migration is the one that transfers PostgreSQL unchanged while leaving failover, connection recovery, and service limits untested.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A team moving PostgreSQL from Compute Engine already knows how to tune SQL and operate backups. Cloud SQL takes responsibility for VM lifecycle, supported engine maintenance, storage management, and managed availability. The application still owns connection behavior, transaction retries, query design, capacity planning, and recovery validation.&lt;/p&gt;
&lt;p&gt;That boundary matters because Cloud SQL is a service contract, not a VM with root access. DBAs cannot install arbitrary host software, replace the storage subsystem, or assume that a setting accepted by self-managed PostgreSQL is configurable in the service.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The most common design error is to reduce the migration decision to “PostgreSQL is PostgreSQL.” SQL compatibility does not make the operating model identical. A zonal instance has a different failure boundary from a regional HA instance. A read replica is not an HA standby. The Cloud SQL Auth Proxy authenticates and encrypts connections, but it is not a connection pool. A maintenance event or failover terminates existing sessions even when the endpoint remains stable.&lt;/p&gt;
&lt;p&gt;The architectural question is therefore: &lt;strong&gt;which responsibilities moved to Google, which remained with the workload, and how will the team prove the boundary under failure?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-managed-service-boundary&quot;&gt;The Managed-Service Boundary&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[Application] --&gt; Pool[Application connection pool]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Pool --&gt; Connector[Cloud SQL connector or Auth Proxy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Connector --&gt; Endpoint[Cloud SQL instance endpoint]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Endpoint --&gt; Primary[Primary — zone A]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt; PrimaryDisk[(Persistent disk — zone A)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt;|Synchronous write| StandbyDisk[(Persistent disk — zone B)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    StandbyDisk --&gt; Standby[Standby — zone B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt; Replica[Optional read replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For a regional HA instance, Google runs a primary and standby in different zones and synchronously replicates writes to regional persistent disk. The standby is not an application-readable replica. If the primary becomes unresponsive, Cloud SQL can fail over and retain the instance connection name and IP address; existing connections still close and must reconnect. Google documents that reconnection is typically possible in about 60 seconds, but that is an observation to test, not an application SLA to hard-code.&lt;/p&gt;
&lt;p&gt;Read replicas solve a different problem. They serve read traffic and use asynchronous replication, so they can lag and return stale results. Cross-region replicas can form part of disaster recovery, but promotion is an explicit recovery action and produces a new primary endpoint.&lt;/p&gt;
&lt;p&gt;The connectivity layer also has a precise boundary. Cloud SQL connectors and the Auth Proxy provide IAM-aware authorization and TLS without requiring the application to manage certificates. They do not restrict the number of PostgreSQL backends. The application still needs a bounded pool, timeouts, retry policy, and admission control. PgBouncer is one possible pooler, but it is an additional component with its own failure and transaction-pooling semantics.&lt;/p&gt;
&lt;p&gt;Document the boundary in the service ownership map: Google owns the managed host and control plane; the platform team owns network and IAM policy; the database team owns schemas and capacity; and the application team owns transaction semantics, pooling, and retries. Ambiguous ownership is itself a failure mode during an outage.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Define the instance declaratively, but keep environment-specific sizing out of a reusable example:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;hcl&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;resource&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;google_sql_database_instance&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; &quot;postgres&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;production-postgres&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  database_version&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;POSTGRES_15&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  region&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;us-central1&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  deletion_protection&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  settings&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tier&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;              =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;db-custom-8-32768&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    availability_type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;REGIONAL&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    disk_type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;          =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;PD_SSD&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    disk_autoresize&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    backup_configuration&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      enabled&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                        =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      point_in_time_recovery_enabled&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      start_time&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                     =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;03:00&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    maintenance_window&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      day&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;          =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      hour&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;         =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      update_track&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;stable&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This configuration is a starting point, not proof of readiness. Pin the provider version, review the selected PostgreSQL version’s support policy, validate the tier in the target region, and plan how Terraform state and deletion protection are controlled.&lt;/p&gt;
&lt;p&gt;The CARL evidence here is system behavior documented by Google: regional HA closes sessions during failover; maintenance can interrupt connectivity; replicas can lag; and connectors are not poolers. Convert those behaviors into tests:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Trigger a manual failover under representative traffic and measure reconnect time, retry volume, and tail latency.&lt;/li&gt;
&lt;li&gt;Exhaust the configured application pool in staging and verify load is rejected rather than expanding unbounded connections.&lt;/li&gt;
&lt;li&gt;Measure replica lag and test a read-after-write request against the replica path.&lt;/li&gt;
&lt;li&gt;Restore a backup or PITR copy and verify application-level integrity, not merely instance creation.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;left&quot;&gt;Failure mode&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Evidence to inspect&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Engineering response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Connection storm after failover&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Backend count, pool queue depth, retry rate&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Add jittered retry, cap pools, and avoid every process reconnecting simultaneously.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Storage saturation&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Read and write latency, throughput, queue depth&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Tune queries first; then size storage and compute from measured demand. Do not prescribe an arbitrary disk size.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Stale replica reads&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Replica lag and application consistency errors&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Route read-after-write and correctness-sensitive queries to the primary.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Maintenance surprise&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Maintenance event logs and client disconnects&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Configure windows and deny periods where appropriate; make disconnect recovery routine.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align=&quot;left&quot;&gt;Terraform destroys or replaces an instance&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Plan output and state history&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;Require reviewed plans, deletion protection, backups, and a tested recovery path.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The team has transferred the database but not redesigned around the managed-service failure boundary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Treat HA, connection management, read scaling, and recovery as separate capabilities with separate tests.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Google’s documented HA behavior explicitly closes existing connections while retaining the endpoint, and replicas explicitly permit lag.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run a failover and restore exercise before production cutover, recording measured RTO, observed RPO, retry behavior, and pool saturation.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/high-availability&quot;&gt;Cloud SQL high availability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/manage-connections&quot;&gt;Cloud SQL connection management&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/sql-proxy&quot;&gt;Cloud SQL Auth Proxy overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/replication&quot;&gt;Cloud SQL read replicas&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database_instance&quot;&gt;Terraform &lt;code&gt;google_sql_database_instance&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>cloud</category><category>architecture</category><category>databases</category><category>failures</category><category>checklist</category></item><item><title>Order State Machines: The Database Model Behind Checkout Reliability</title><link>https://rajivonai.com/blog/2023-11-02-order-state-machines-the-database-model-behind-checkout-reliability/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-11-02-order-state-machines-the-database-model-behind-checkout-reliability/</guid><description>Order state machines prevent checkout duplication by constraining which database transitions are legal — so a paid order cannot be paid twice.</description><pubDate>Thu, 02 Nov 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Checkout does not fail because a button was clicked twice; it fails because the database allowed the same business fact to be represented twice.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Modern checkout paths are distributed long before the architecture diagram admits it. The browser retries after a timeout. The API gateway retries after a connection reset. The payment provider responds slowly, then eventually succeeds. Inventory reservation, tax calculation, fraud review, fulfillment, email, and analytics all want to react to the same order.&lt;/p&gt;
&lt;p&gt;The mistake is treating &lt;code&gt;orders.status&lt;/code&gt; as a display field instead of the control plane for money movement. A checkout system needs a database-backed state machine: a constrained model of valid transitions, idempotent commands, auditable attempts, and recoverable side effects.&lt;/p&gt;
&lt;p&gt;The core design is not exotic. It is usually a relational table, a few uniqueness constraints, transaction boundaries, and an outbox. The hard part is refusing to let application code improvise around those constraints.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The naive model starts clean:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;orders(id, user_id, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, total_amount, created_at)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then production arrives.&lt;/p&gt;
&lt;p&gt;A shopper submits checkout and sees a network timeout. The browser retries. The first request is still charging the card while the second request creates another order. A worker polls &lt;code&gt;pending&lt;/code&gt; orders and races with the API thread. A webhook says payment succeeded after the order has already been canceled. Inventory is reserved for an order that never reaches fulfillment. Customer support sees three rows that each look plausible.&lt;/p&gt;
&lt;p&gt;The operational failure is not merely duplicate orders. It is ambiguous authority. Which row owns the payment? Which transition is legal? Which retry is safe? Which side effect has already happened? Which subsystem is allowed to move the order forward?&lt;/p&gt;
&lt;p&gt;When the database only stores the latest status, every caller becomes a partial state machine with a different memory of the world.&lt;/p&gt;
&lt;p&gt;The question is: how do you model checkout so retries, workers, webhooks, and human recovery all converge on one order history instead of multiplying failure modes?&lt;/p&gt;
&lt;h2 id=&quot;answer-make-the-database-own-the-state-machine&quot;&gt;Answer: Make The Database Own The State Machine&lt;/h2&gt;
&lt;p&gt;A reliable checkout model separates identity, state, attempts, and side effects.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[checkout request — idempotency key] --&gt;|unique insert| B[order row — pending checkout]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt;|create attempt| C[payment attempt row — authorization pending]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt;|conditional transition| D[order row — payment authorized]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt;|reserve stock| E[inventory reservation — confirmed]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt;|append message| F[outbox event — order placed]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt;|retry delivery| G[worker delivery — acknowledged]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;orders&lt;/code&gt; table is the aggregate root. It stores the current state and a monotonic version.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;orders(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  customer_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  checkout_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  state_version,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  total_amount,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  created_at,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  updated_at,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  UNIQUE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(customer_id, checkout_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;checkout_id&lt;/code&gt; is supplied by the caller or generated before submission. It is not a tracing field. It is the idempotency boundary for creating the order. If the same customer retries the same checkout, the database must return the same order, not create a sibling.&lt;/p&gt;
&lt;p&gt;Valid transitions should be represented explicitly:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;order_state_transitions(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  from_state,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  to_state,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  command,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(from_state, to_state, command)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Application code can still contain transition logic, but the database model should make illegal transitions hard to persist. The important rule is that every command updates from an expected state:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;UPDATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;payment_authorized&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    state_version &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; state_version &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    updated_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;payment_pending&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; state_version &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If zero rows update, the command did not own the transition. It must reload and decide whether the desired result already happened, became impossible, or should be retried.&lt;/p&gt;
&lt;p&gt;Payment attempts should not be collapsed into the order row. They are separate facts:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;payment_attempts(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  order_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  provider&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  provider_request_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  provider_payment_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  amount,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  created_at,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  updated_at,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  UNIQUE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;provider&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, provider_request_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This gives the system a place to record uncertainty. &lt;code&gt;authorization_pending&lt;/code&gt;, &lt;code&gt;authorized&lt;/code&gt;, &lt;code&gt;declined&lt;/code&gt;, &lt;code&gt;timed_out&lt;/code&gt;, and &lt;code&gt;reversed&lt;/code&gt; are attempt states, not always order states. The order should advance only when the attempt produces a business fact the order can consume.&lt;/p&gt;
&lt;p&gt;Side effects need the same discipline. Sending an email, publishing &lt;code&gt;OrderPlaced&lt;/code&gt;, or notifying fulfillment should be driven through an outbox table written in the same transaction as the order transition:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;order_outbox(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  order_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  event_type,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  payload,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  published_at,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  created_at&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The transition and the event become atomic. Delivery can be retried without re-deciding whether the order was placed.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Stripe documents idempotent requests as a way for clients to safely retry create or update operations, with the first result saved and returned for later requests using the same key. Stripe also notes that keys should be unique and that parameter mismatches are rejected to prevent accidental key reuse. &lt;a href=&quot;https://docs.stripe.com/api/idempotent_requests&quot;&gt;Stripe API docs&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The checkout command should persist an idempotency key at the boundary where money movement begins. The database equivalent is a uniqueness constraint on the caller, checkout key, and operation, plus a stored response or stored aggregate reference. This matches the documented pattern: retry returns the original result instead of executing the mutation again. &lt;a href=&quot;https://docs.stripe.com/api/idempotent_requests&quot;&gt;Stripe API docs&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Duplicate HTTP requests stop being duplicate business commands. They become repeated reads of the same command result. The learning is that idempotency is not a middleware concern; it is a persisted contract.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Shopify’s engineering write-up on payment idempotency describes tracking incoming requests by client and idempotency key, and using a lock around the API call so simultaneous duplicate requests do not both proceed. &lt;a href=&quot;https://shopify.engineering/blogs/engineering/building-resilient-graphql-apis-using-idempotency&quot;&gt;Shopify Engineering&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; A checkout system should record the command before doing external work and mark whether it is in progress, completed, or failed in a retryable way. A concurrent duplicate can then return a conflict or pollable result instead of entering the payment path twice. &lt;a href=&quot;https://shopify.engineering/blogs/engineering/building-resilient-graphql-apis-using-idempotency&quot;&gt;Shopify Engineering&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The database becomes the rendezvous point for concurrent retries. The learning is that idempotency keys need an in-progress state, not only a completed-response cache.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; PostgreSQL documents row-level locking with &lt;code&gt;SELECT FOR UPDATE&lt;/code&gt;, and &lt;code&gt;SKIP LOCKED&lt;/code&gt; for cases where locked rows should be skipped rather than waited on. &lt;a href=&quot;https://www.postgresql.org/docs/17/explicit-locking.html&quot;&gt;PostgreSQL documentation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Workers that advance orders from &lt;code&gt;payment_authorized&lt;/code&gt; to &lt;code&gt;ready_for_fulfillment&lt;/code&gt; can claim rows with explicit locks, or use conditional updates that move exactly one expected state. For queue-like recovery jobs, &lt;code&gt;SKIP LOCKED&lt;/code&gt; lets multiple workers avoid processing the same locked row. &lt;a href=&quot;https://www.postgresql.org/docs/10/sql-select.html&quot;&gt;PostgreSQL documentation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Background processors stop competing through stale reads. The learning is that state machines need concurrency control at the row that owns the transition.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; DynamoDB condition expressions allow writes only when an expression evaluates true, such as inserting an item only when the key does not already exist. &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ConditionExpressions.html&quot;&gt;AWS DynamoDB documentation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The same state-machine model works outside SQL when transitions are conditional writes: create only if absent, advance only if the current state and version match, and treat failed conditions as a signal to reload. &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ConditionExpressions.html&quot;&gt;AWS DynamoDB documentation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The pattern is not tied to one database engine. The learning is that checkout reliability comes from conditional ownership of business facts.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;What happens&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;State explosion&lt;/td&gt;&lt;td&gt;Every provider callback becomes a new order state&lt;/td&gt;&lt;td&gt;Keep provider details in attempt tables and promote only business-level states to the order&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Long transactions&lt;/td&gt;&lt;td&gt;Payment calls hold database locks while waiting on the network&lt;/td&gt;&lt;td&gt;Persist intent first, call the provider outside the lock, then conditionally apply the result&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Weak idempotency scope&lt;/td&gt;&lt;td&gt;The same key is reused across different carts or amounts&lt;/td&gt;&lt;td&gt;Store a request fingerprint and reject mismatched retries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Outbox backlog&lt;/td&gt;&lt;td&gt;Order transitions succeed but downstream delivery lags&lt;/td&gt;&lt;td&gt;Monitor unpublished event age and retry count as production health signals&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Manual repair bypasses rules&lt;/td&gt;&lt;td&gt;Support edits &lt;code&gt;orders.state&lt;/code&gt; directly&lt;/td&gt;&lt;td&gt;Build repair commands that use the same transition table and append audit records&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Webhook races&lt;/td&gt;&lt;td&gt;Provider success arrives before the API request finishes&lt;/td&gt;&lt;td&gt;Record provider events independently, then reconcile through conditional transitions&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Checkout failures become expensive when retries and callbacks can create new business facts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Model orders as database-owned state machines with idempotent commands, conditional transitions, separate attempt records, and an outbox.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Stripe and Shopify document idempotency as a persisted retry contract, while PostgreSQL and DynamoDB expose the locking and conditional-write primitives needed to enforce transition ownership.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Start by adding &lt;code&gt;checkout_id&lt;/code&gt;, &lt;code&gt;state_version&lt;/code&gt;, payment attempt records, and an outbox. Then change every checkout mutation to update from an expected state instead of assigning a new status directly.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Shopping Cart Storage: Session Cache, Durable Cart, and Recovery Semantics</title><link>https://rajivonai.com/blog/2023-10-03-shopping-cart-storage-session-cache-durable-cart-and-recovery-semantics/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-10-03-shopping-cart-storage-session-cache-durable-cart-and-recovery-semantics/</guid><description>Session cache versus durable cart: the recovery semantics that determine data survival across session loss, browser closure, and checkout failure.</description><pubDate>Tue, 03 Oct 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A shopping cart is not a cache entry with a checkout button; it is a user-facing recovery protocol hiding behind a retail UI.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Modern commerce stacks split the customer journey across browsers, mobile apps, edge services, identity providers, recommendation systems, inventory services, pricing engines, payment providers, and fulfillment platforms. The cart sits in the middle of that system, but it is often treated as local session state because the interaction feels temporary.&lt;/p&gt;
&lt;p&gt;That assumption works until the user changes devices, signs in after browsing anonymously, opens two tabs, returns after a cache eviction, or checks out during a partial outage. At that point the cart becomes a distributed state problem with business consequences: lost intent, double discounts, stale inventory, inconsistent tax estimates, and support tickets that read like data corruption.&lt;/p&gt;
&lt;p&gt;The durable part of a cart is not the rendered list of items. It is the customer’s recoverable purchase intent, plus enough version history to reconcile concurrent changes.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The common failure starts with a fast session cache. The product team wants instant add-to-cart latency. The platform team puts cart state in Redis or an in-memory session store with a TTL. The checkout service reads from that cache, pricing enriches the items, and the experience feels fast.&lt;/p&gt;
&lt;p&gt;Then reality arrives.&lt;/p&gt;
&lt;p&gt;A cache eviction deletes carts that users expected to survive. A regional failover sends traffic to a warm environment without the same session keys. An anonymous user signs in and overwrites an account cart. A mobile client retries an add operation after a timeout and increments quantity twice. A discount code is accepted in the cart but rejected at payment because the durable order service recomputed different state.&lt;/p&gt;
&lt;p&gt;The hard question is not “where do we store the cart?” The hard question is: which cart mutations must survive failure, which views can be regenerated, and what semantics does the user see when multiple versions exist?&lt;/p&gt;
&lt;h2 id=&quot;durable-cart-with-session-acceleration&quot;&gt;Durable Cart with Session Acceleration&lt;/h2&gt;
&lt;p&gt;The clean architecture separates three responsibilities: session acceleration, durable cart authority, and recovery semantics.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[client — browser or mobile] --&gt; B[cart API — command intake]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[session cache — fast cart view]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; D[durable cart store — source of intent]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; E[cart event log — mutation history]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; F[pricing service — computed quote]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; G[inventory service — availability check]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; H[rendered cart — low latency read]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; I[checkout service — order creation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; I&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; J[recovery worker — replay and merge]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  J --&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The session cache should hold a render-optimized projection: item IDs, display names, thumbnails, estimated totals, and a short TTL. It is allowed to be stale. It is allowed to disappear. It must not be the only place where intent lives.&lt;/p&gt;
&lt;p&gt;The durable cart store owns cart identity, user identity binding, item quantities, selected options, applied promotion references, client mutation IDs, timestamps, and a version number. Every mutating command should be expressed as an operation: add item, remove item, set quantity, attach user, apply coupon, select shipping option. The operation is written to durable storage before the cache is treated as authoritative.&lt;/p&gt;
&lt;p&gt;That durable store can be relational, document-oriented, or key-value. The important requirement is not the product category. The requirement is conditional mutation. A cart write should say: apply this command if the cart version is still &lt;code&gt;17&lt;/code&gt;, or if this client mutation ID has not already been processed. That protects the system from lost updates and retry amplification.&lt;/p&gt;
&lt;p&gt;For anonymous carts, the browser can hold an opaque cart token. On login, the system should merge the anonymous cart and account cart as an explicit operation, not as an overwrite. If both carts contain the same SKU with compatible options, summing quantities is usually reasonable. If the options conflict, preserve both lines. If a promotion only applies once, keep the promotion as pending until pricing validates it again.&lt;/p&gt;
&lt;p&gt;Checkout should not blindly trust the cart projection. It should create an order from a validated cart snapshot: current prices, current inventory reservation result, current shipping constraints, and idempotent payment intent. The cart can contain desire. The order must contain commitments.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Amazon’s Dynamo paper uses the shopping cart as a motivating example for high availability under network partitions. The documented pattern is that cart writes should remain available, and divergent versions may need reconciliation later rather than rejecting user intent during a failure.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The architecture choice is to accept cart mutations as durable commands and reconcile conflicts with application semantics. For a cart, “merge both items” is often better than “last writer wins,” because dropping a line item loses user intent.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented learning from Dynamo-style systems is that availability pushes conflict resolution into the application. A storage layer can preserve versions, but it cannot know whether two cart lines represent duplicates, alternatives, or separate purchases.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; If the business wants highly available cart writes, the cart domain must define merge behavior. Storage replication alone does not define recovery semantics.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Redis-style session caches are fast and support expiration, but cached data can be evicted or lost depending on memory policy and persistence configuration. The documented system behavior is that TTL-backed cache state is not equivalent to durable business state.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Use the cache for read acceleration and cart rendering, while writing cart commands to a durable store first. Rebuild the cache from durable state after misses, failovers, or deploys.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Cache loss becomes a latency event instead of a cart loss event. The user may wait for a reload, but their recoverable cart intent remains intact.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; A cart cache should be disposable. If losing the cache loses the cart, the cache has become the database without database semantics.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Relational systems such as PostgreSQL provide transactions, unique constraints, and conditional updates. The documented behavior is useful for cart mutation idempotency: a unique client mutation ID can prevent duplicate command application.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Store each cart command with a stable idempotency key from the client or API gateway. Apply quantity changes inside a transaction with version checks.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; A mobile retry after a timeout can safely return the already-applied result instead of adding the same item twice.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Idempotency is not a checkout-only concern. Cart mutation APIs need it because clients retry precisely when the user cannot tell whether the operation succeeded.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Weak design&lt;/th&gt;&lt;th&gt;Stronger design&lt;/th&gt;&lt;th&gt;Remaining tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Cache eviction&lt;/td&gt;&lt;td&gt;Cart disappears&lt;/td&gt;&lt;td&gt;Rehydrate projection from durable cart&lt;/td&gt;&lt;td&gt;First read after miss is slower&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Anonymous login&lt;/td&gt;&lt;td&gt;Account cart overwritten&lt;/td&gt;&lt;td&gt;Explicit merge command&lt;/td&gt;&lt;td&gt;Merge rules must be product-aware&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Multi-tab edits&lt;/td&gt;&lt;td&gt;Last write wins&lt;/td&gt;&lt;td&gt;Versioned conditional writes&lt;/td&gt;&lt;td&gt;Client must handle conflict response&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Mobile retry&lt;/td&gt;&lt;td&gt;Quantity increments twice&lt;/td&gt;&lt;td&gt;Idempotency key per mutation&lt;/td&gt;&lt;td&gt;Requires key storage and retention&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Regional failover&lt;/td&gt;&lt;td&gt;Session state unavailable&lt;/td&gt;&lt;td&gt;Durable replicated cart state&lt;/td&gt;&lt;td&gt;Conflict resolution becomes visible&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Price drift&lt;/td&gt;&lt;td&gt;Cart total trusted at checkout&lt;/td&gt;&lt;td&gt;Reprice validated snapshot&lt;/td&gt;&lt;td&gt;User may see final total change&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Inventory race&lt;/td&gt;&lt;td&gt;Cart reserves stock forever&lt;/td&gt;&lt;td&gt;Availability checked near checkout&lt;/td&gt;&lt;td&gt;Cart can contain unavailable items&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Promotion conflict&lt;/td&gt;&lt;td&gt;Coupon cached as accepted&lt;/td&gt;&lt;td&gt;Coupon revalidated before order&lt;/td&gt;&lt;td&gt;UX must explain rejected discounts&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Treating the cart as session state makes ordinary infrastructure events look like data loss to the user.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Split the system into a disposable session cache, a durable cart authority, and explicit recovery rules for retries, merges, and conflicts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Known systems such as Dynamo-style replicated stores, Redis-style caches, and transactional databases expose different failure semantics; the cart architecture must assign each responsibility to the right layer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit every cart mutation path for durability, idempotency, version checks, cache rebuild behavior, anonymous-to-authenticated merge rules, and checkout revalidation.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Why SELECT * Still Hurts Production Systems</title><link>https://rajivonai.com/blog/2023-10-02-why-select-star-still-hurts-production-systems/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-10-02-why-select-star-still-hurts-production-systems/</guid><description>SELECT * causes four distinct problems that compound at scale: it prevents covering index usage, transfers unnecessary data, breaks application code silently, and defeats column pruning in analytical systems.</description><pubDate>Mon, 02 Oct 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;&lt;code&gt;SELECT *&lt;/code&gt; is not a minor style violation. It is a query that opts out of covering indexes, pulls every TOAST column unconditionally, and defeats columnar storage’s only performance advantage — column pruning.&lt;/strong&gt; Engineers know the advice, but most have never seen the actual mechanism that makes &lt;code&gt;SELECT *&lt;/code&gt; expensive in production. The problem almost always shows up the same way: the query ran fine in development, shipped, then became the top line in I/O bytes as the table grew.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Applications accumulate columns over time. A &lt;code&gt;users&lt;/code&gt; table starts with a dozen fields and grows incrementally — a &lt;code&gt;preferences&lt;/code&gt; JSONB column here, a &lt;code&gt;bio&lt;/code&gt; TEXT there, an audit field, a feature flag blob. Each migration is routine. The &lt;code&gt;SELECT *&lt;/code&gt; queries that read that table are unchanged.&lt;/p&gt;
&lt;p&gt;By the time a query shows up in slow query logs, the table has 50 columns and two of them are 40KB per row on average. Development databases rarely catch this because dev data is small and large TEXT or JSONB values are usually short.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;There are four distinct mechanisms through which &lt;code&gt;SELECT *&lt;/code&gt; degrades production workloads.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Covering indexes become useless.&lt;/strong&gt; PostgreSQL’s index-only scan resolves a query entirely from the index without touching the heap — but only when every output column is present in the index. &lt;code&gt;SELECT *&lt;/code&gt; forces a heap fetch for every matching row regardless, turning a fast index-only scan into a random I/O operation per result.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;TOAST columns are fetched unconditionally.&lt;/strong&gt; PostgreSQL stores values larger than roughly 2KB out-of-line in a secondary TOAST table. A &lt;code&gt;TEXT&lt;/code&gt;, &lt;code&gt;JSONB&lt;/code&gt;, or &lt;code&gt;BYTEA&lt;/code&gt; column that exceeds the threshold is fetched separately when accessed. &lt;code&gt;SELECT *&lt;/code&gt; includes every column, so every oversized value triggers a secondary read — even when the application uses only two fields from the row.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Schema changes break application code silently.&lt;/strong&gt; ORM code that maps &lt;code&gt;SELECT *&lt;/code&gt; results onto struct fields may corrupt state when a new &lt;code&gt;NOT NULL&lt;/code&gt; column is added or columns are reordered. The query succeeds; the struct carries unexpected data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Columnar systems lose column pruning.&lt;/strong&gt; Redshift, BigQuery, and DuckDB store data by column. Their foundational I/O optimization is reading only the columns the query names. &lt;code&gt;SELECT *&lt;/code&gt; forces reads across every column in the table, with I/O cost proportional to column count.&lt;/p&gt;
&lt;p&gt;What does a query that avoids all four problems look like, and what needs to change at the schema and index layer?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s index-only scan allows the executor to return results directly from index pages without visiting heap pages at all. For this to work, every column in the SELECT list and WHERE clause must be present in the index.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Query execution] --&gt; B{All selected columns in index?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B -- Yes --&gt; C[Index-only Scan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B -- No — SELECT star used --&gt; D[Fetch full row from heap]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E{Has out-of-line TOAST columns?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E -- Yes --&gt; F[Fetch secondary TOAST pages]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E -- No --&gt; G[Return heap data]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A query like this can use an index-only scan if an index exists on &lt;code&gt;(email, id, name)&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; id, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; users &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; email &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;user@example.com&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Change that to &lt;code&gt;SELECT *&lt;/code&gt; and the covering index is bypassed. The executor must fetch the full heap row for every match regardless of index efficiency. The practical guidance from PostgreSQL’s documentation is direct: include output columns in the index using &lt;code&gt;INCLUDE&lt;/code&gt;, and name only the columns the query needs. &lt;code&gt;SELECT *&lt;/code&gt; makes both impossible because the output column list is unbounded.&lt;/p&gt;
&lt;p&gt;For EXPLAIN-based verification, &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; before and after switching from &lt;code&gt;SELECT *&lt;/code&gt; to named columns makes the heap fetch cost visible as the difference in &lt;code&gt;Buffers: shared hit&lt;/code&gt; counts. The &lt;a href=&quot;https://rajivonai.com/blog/2022-06-06-mysql-explain-reading-the-plan/&quot;&gt;MySQL EXPLAIN post&lt;/a&gt; walks through reading query plans systematically — the same principle applies to PostgreSQL’s EXPLAIN ANALYZE output when comparing index-only scan eligibility.&lt;/p&gt;
&lt;p&gt;For vector queries, column selection matters in the same way. A query retrieving pgvector embeddings alongside large JSON metadata columns pays the TOAST cost on every result row when &lt;code&gt;SELECT *&lt;/code&gt; is used. Selecting only the embedding and the fields the application reads avoids that fetch entirely. Index setup is only half the battle; column selection determines what gets fetched once the index returns its matches.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented behavior of PostgreSQL’s index-only scan is that it is unavailable when the query output includes columns not present in the index. The PostgreSQL documentation states this explicitly: every column in the query’s target list and WHERE clause must be available from the index. &lt;code&gt;SELECT *&lt;/code&gt; prevents this by construction.&lt;/p&gt;
&lt;p&gt;The PostgreSQL TOAST documentation describes out-of-line threshold behavior: values are not fetched unless the column is accessed. This means &lt;code&gt;SELECT id, name FROM users&lt;/code&gt; genuinely avoids reading oversized &lt;code&gt;metadata&lt;/code&gt; values, while &lt;code&gt;SELECT *&lt;/code&gt; fetches them for every row regardless of whether the application uses them.&lt;/p&gt;
&lt;p&gt;Google’s BigQuery documentation is explicit under query optimization guidance: selecting only needed columns reduces bytes scanned and therefore cost. The documented design of Redshift and DuckDB follows the same principle — column pruning requires a bounded output list. &lt;code&gt;SELECT *&lt;/code&gt; removes that bound entirely.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Covering index bypassed&lt;/td&gt;&lt;td&gt;Index-only scan degrades to heap fetch per row&lt;/td&gt;&lt;td&gt;&lt;code&gt;SELECT *&lt;/code&gt; requires columns the index cannot contain&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;TOAST column on every row&lt;/td&gt;&lt;td&gt;Seconds of extra I/O per query execution&lt;/td&gt;&lt;td&gt;Large out-of-line values fetched even when the app discards them&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ORM struct mapping&lt;/td&gt;&lt;td&gt;Application reads wrong values after schema migration&lt;/td&gt;&lt;td&gt;Positional mapping breaks when columns are added or reordered&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Columnar storage full-scan&lt;/td&gt;&lt;td&gt;Query cost proportional to column count instead of query selectivity&lt;/td&gt;&lt;td&gt;Column pruning requires knowing the output columns at parse time&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: &lt;code&gt;SELECT *&lt;/code&gt; bypasses covering indexes, unconditionally fetches TOAST columns, and eliminates column pruning — costs invisible in development, expensive in production.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Name only the columns the application consumes, and build indexes with &lt;code&gt;INCLUDE&lt;/code&gt; to cover the output columns needed on frequent read paths.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Run &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; before and after switching from &lt;code&gt;SELECT *&lt;/code&gt; to named columns — a drop in &lt;code&gt;shared hit&lt;/code&gt; buffer counts confirms the heap fetch is no longer happening.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Audit the top 10 queries by I/O bytes in &lt;code&gt;pg_stat_statements&lt;/code&gt; this week and identify which use &lt;code&gt;SELECT *&lt;/code&gt; on tables containing TEXT, JSONB, or BYTEA columns.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The rule exists not because of style but because the optimizer needs a bounded column list to make cost decisions. Give the optimizer that list and three of these four problems disappear entirely.&lt;/p&gt;</content:encoded><category>databases</category><category>failures</category></item><item><title>Product Catalog Modeling: Relational, Document, Search Index, or All Three</title><link>https://rajivonai.com/blog/2023-09-18-product-catalog-modeling-relational-document-search-index-or-all-three/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-09-18-product-catalog-modeling-relational-document-search-index-or-all-three/</guid><description>Modeling a product catalog across relational, document, and search-index layers: where each fits and why a single schema fails all three workloads.</description><pubDate>Mon, 18 Sep 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Product catalogs fail when teams treat “the product” as one data shape instead of three competing workloads: correctness, merchandising flexibility, and discovery.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A catalog begins innocently. There is a &lt;code&gt;products&lt;/code&gt; table, a few categories, a price, a description, and an image URL. Then the business asks for variants, bundles, regional availability, marketplace sellers, promotions, localized copy, regulated attributes, and category-specific fields.&lt;/p&gt;
&lt;p&gt;Shoes need size and material. Laptops need CPU, RAM, warranty, and energy labels. Groceries need allergens, pack size, substitution rules, and fulfillment temperature. The product catalog stops being a table of products and becomes the contract between commerce, fulfillment, search, analytics, ads, and customer support.&lt;/p&gt;
&lt;p&gt;At that point the database question becomes architectural. A relational model gives integrity and joins. A document model gives shape flexibility. A search index gives retrieval behavior that neither of the first two should be forced to emulate.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The common failure is picking one model and making it serve all catalog workloads.&lt;/p&gt;
&lt;p&gt;A purely relational catalog often starts clean, then accumulates entity-attribute-value tables, nullable columns, category-specific side tables, and migration anxiety. The schema protects invariants, but product teams wait on DDL for every new attribute family.&lt;/p&gt;
&lt;p&gt;A purely document catalog moves faster, but correctness gets harder. If price, availability, tax classification, seller state, and compliance flags live as loosely governed blobs, downstream systems have to rediscover which fields are authoritative.&lt;/p&gt;
&lt;p&gt;A search-only catalog feels fast until the index becomes the source of truth. Search indexes are optimized for denormalized retrieval, ranking, tokenization, and filtering. They are not designed to be the system of record for transactional correctness.&lt;/p&gt;
&lt;p&gt;The core question is not “which database stores products best?” It is: which parts of the product catalog must be correct, which parts must be flexible, and which parts must be discoverable?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;The strongest pattern is usually not relational or document or search. It is relational and document and search, with ownership boundaries that prevent each store from pretending to be the others.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[merchant tools — catalog edits] --&gt; B[relational core — identity and invariants]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A --&gt; C[document attributes — category shape]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; D[change stream — catalog events]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; E[index builder — denormalized projection]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; F[search index — retrieval and ranking]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; G[customer experience — browse and search]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; H[commerce services — price and availability checks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; I[content services — product detail pages]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The relational core owns identity and invariants: product ID, SKU, variant relationships, seller ownership, lifecycle state, tax classification references, and other fields where duplication or ambiguity creates operational risk.&lt;/p&gt;
&lt;p&gt;The document layer owns attribute shape: category-specific specs, localized content blocks, merchandising metadata, and optional fields that change faster than the canonical model. This can be a document database, a JSON column, or a structured object store. The key is governance: the document is flexible, but not lawless.&lt;/p&gt;
&lt;p&gt;The search index owns retrieval: tokenized text, facets, ranking signals, autocomplete fields, synonyms, and denormalized category views. It is rebuilt from upstream truth. It can be tuned aggressively because losing or corrupting it should degrade discovery, not corrupt orders.&lt;/p&gt;
&lt;p&gt;This split also clarifies write paths. Merchant edits update the system of record. A change stream or outbox emits catalog events. Index builders create projections for search and browse. Customer-facing product pages can read from a precomputed projection, but checkout-critical decisions still revalidate against authoritative services.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; PostgreSQL documents two catalog-relevant capabilities that are often combined: relational constraints for integrity and &lt;code&gt;jsonb&lt;/code&gt; for semi-structured data, including GIN indexes for querying JSON content. The documented pattern is not “put everything in JSON.” It is that relational and semi-structured fields can coexist when the boundary is deliberate. See the PostgreSQL documentation on JSON types and indexing: &lt;a href=&quot;https://www.postgresql.org/docs/current/datatype-json.html&quot;&gt;https://www.postgresql.org/docs/current/datatype-json.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Keep product identity, variant hierarchy, lifecycle state, and ownership in relational columns and tables. Put category-specific attributes in governed JSON only when they do not define core transactional identity. Validate those JSON documents with application schema checks or database constraints where appropriate.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The catalog can evolve attribute families without turning every new merchandising idea into a schema migration, while preserving relational guarantees where duplicate or inconsistent state would break commerce.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; JSON inside a relational database is useful when it extends a relational model. It becomes a liability when it replaces the model’s authority.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Elasticsearch describes its core strength as search over indexed documents, including full-text search, filtering, aggregations, and relevance scoring. The documented behavior is projection-oriented: documents are indexed for retrieval, not normalized for source-of-truth integrity. See Elastic’s guide to mapping and search behavior: &lt;a href=&quot;https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html&quot;&gt;https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Build the search document as a derived catalog projection. Include names, descriptions, category paths, normalized facets, popularity signals, availability hints, and merchandising boosts. Do not make the search document the final authority for price, inventory, seller eligibility, or compliance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Search can be tuned for relevance and latency without coupling ranking experiments to transactional correctness. If an index build fails, the recovery path is to replay events or rebuild from source, not manually repair business truth inside the index.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Search indexes are excellent read models. They are poor systems of record.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; MongoDB’s public schema design guidance uses product catalogs as a natural fit for document modeling because products in different categories can carry different attribute sets. The documented pattern is flexible representation for heterogeneous entities, not abandoning data ownership. See MongoDB’s data modeling guidance: &lt;a href=&quot;https://www.mongodb.com/docs/manual/data-modeling/&quot;&gt;https://www.mongodb.com/docs/manual/data-modeling/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Use document modeling for product attributes when category diversity is the main source of change. Keep cross-product invariants explicit: identifiers, references, lifecycle state, and integration contracts should remain stable and validated.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Attribute-heavy catalogs avoid brittle table explosions, but downstream systems still receive predictable contracts.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Document flexibility pays off when the business changes shape faster than the core identity model changes.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Architecture choice&lt;/th&gt;&lt;th&gt;Works well when&lt;/th&gt;&lt;th&gt;Breaks when&lt;/th&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Relational only&lt;/td&gt;&lt;td&gt;Catalog shape is stable and invariants dominate&lt;/td&gt;&lt;td&gt;Category attributes change constantly&lt;/td&gt;&lt;td&gt;EAV tables, nullable sprawl, slow schema evolution&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Document only&lt;/td&gt;&lt;td&gt;Products are heterogeneous and mostly read as whole objects&lt;/td&gt;&lt;td&gt;Checkout correctness depends on embedded mutable fields&lt;/td&gt;&lt;td&gt;Conflicting truth across services&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Search index only&lt;/td&gt;&lt;td&gt;The problem is discovery and ranking&lt;/td&gt;&lt;td&gt;The index becomes authoritative&lt;/td&gt;&lt;td&gt;Orders use stale or denormalized data&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Relational plus document&lt;/td&gt;&lt;td&gt;Core identity is stable but attributes vary&lt;/td&gt;&lt;td&gt;JSON fields are unvalidated&lt;/td&gt;&lt;td&gt;Flexible fields become hidden contracts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Relational plus document plus search&lt;/td&gt;&lt;td&gt;Multiple workloads need different read shapes&lt;/td&gt;&lt;td&gt;Eventing and rebuild paths are weak&lt;/td&gt;&lt;td&gt;Index drift, stale projections, unclear ownership&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The combined model has real cost. You now own propagation, idempotency, rebuilds, schema versioning, and observability across stores. The win is not simplicity of implementation. The win is operational clarity.&lt;/p&gt;
&lt;p&gt;You should be able to answer these questions during an incident:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Which store is authoritative for this field?&lt;/li&gt;
&lt;li&gt;Can this projection be rebuilt from upstream state?&lt;/li&gt;
&lt;li&gt;What happens if the search index is ten minutes stale?&lt;/li&gt;
&lt;li&gt;Which fields must be revalidated before checkout?&lt;/li&gt;
&lt;li&gt;Which schema changes require backfills?&lt;/li&gt;
&lt;li&gt;Which consumers are pinned to old document versions?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If those answers are unclear, adding more databases will amplify the failure rather than contain it.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Your catalog probably contains multiple workloads hidden behind one noun: product.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Separate the relational core, flexible attribute model, and search projection by ownership and failure behavior.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Use relational constraints for invariants, governed documents for heterogeneous attributes, and rebuildable indexes for discovery.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit the top twenty catalog fields by authority, freshness requirement, write owner, read path, and rebuild strategy before changing the storage engine.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>E-Commerce Databases Are Not One Database: Catalog, Cart, Orders, Inventory, Payments</title><link>https://rajivonai.com/blog/2023-09-03-e-commerce-databases-are-not-one-database-catalog-cart-orders-inventory-payments/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-09-03-e-commerce-databases-are-not-one-database-catalog-cart-orders-inventory-payments/</guid><description>Catalog, cart, orders, inventory, and payments as five distinct consistency problems — why a shared transaction boundary causes e-commerce system failures.</description><pubDate>Sun, 03 Sep 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;E-commerce systems fail when teams treat checkout as one database transaction instead of five different consistency problems moving at different speeds.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A storefront looks simple from the outside: browse a product, add it to a cart, pay, receive an order. That shape encourages a dangerous internal model: one application, one relational schema, one transaction boundary.&lt;/p&gt;
&lt;p&gt;That model works while traffic is low, SKU count is small, inventory is forgiving, and payment retries are rare. It breaks when the business adds marketplace sellers, regional fulfillment, promotions, backorders, fraud review, partial shipments, returns, and mobile clients that retry aggressively on weak networks.&lt;/p&gt;
&lt;p&gt;The operational truth is that “purchase” is not one write. It is a chain of state transitions across catalog, cart, order, inventory, and payment systems. Each subsystem has a different read pattern, write pattern, failure mode, and recovery requirement.&lt;/p&gt;
&lt;p&gt;Catalog wants broad, cached, searchable reads. Cart wants cheap ephemeral writes. Orders want durable append-only state. Inventory wants contention control. Payments want idempotent external side effects.&lt;/p&gt;
&lt;p&gt;Trying to force all of that into one database does not simplify the system. It hides the boundaries until the first incident.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The single-database version usually fails in one of five ways.&lt;/p&gt;
&lt;p&gt;First, catalog reads overload transactional tables. Search pages, recommendation widgets, product detail pages, and merchandising tools all want denormalized product data. If they read from the same schema used by checkout, a catalog launch or search crawler can degrade order creation.&lt;/p&gt;
&lt;p&gt;Second, cart state becomes falsely important. Most carts are abandoned. Treating every cart mutation like an order mutation wastes durable write capacity and turns transient user behavior into transactional load.&lt;/p&gt;
&lt;p&gt;Third, orders become mutable documents instead of ledgers. If order rows are repeatedly overwritten as payment, fulfillment, cancellation, and refund events arrive, it becomes hard to reconstruct what happened during disputes or retries.&lt;/p&gt;
&lt;p&gt;Fourth, inventory becomes a race condition. The system must decide whether it is selling available stock, reserving stock, promising future stock, or reconciling stock later. These are different contracts. A generic &lt;code&gt;quantity&lt;/code&gt; column is not an inventory system.&lt;/p&gt;
&lt;p&gt;Fifth, payments introduce side effects outside the database. A database rollback cannot undo a card authorization already sent to a processor. A client timeout does not mean the charge failed. Retrying without an idempotency boundary can create duplicate financial operations.&lt;/p&gt;
&lt;p&gt;The core question is: how should an e-commerce platform split data ownership so checkout remains reliable without making every subsystem strongly consistent with every other subsystem?&lt;/p&gt;
&lt;h2 id=&quot;five-stores-one-checkout-contract&quot;&gt;Five Stores, One Checkout Contract&lt;/h2&gt;
&lt;p&gt;The answer is not “microservices” as a slogan. The answer is separating consistency domains and then making the handoffs explicit.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Browser[buyer session — browse and checkout] --&gt; Catalog[catalog store — searchable product facts]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Browser --&gt; Cart[cart store — ephemeral buyer intent]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Cart --&gt; Checkout[checkout coordinator — validation and command boundary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Checkout --&gt; Inventory[inventory store — reservations and stock movements]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Checkout --&gt; Orders[order ledger — durable commercial record]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Checkout --&gt; Payments[payment ledger — idempotent external effects]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Inventory --&gt; Orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Payments --&gt; Orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Orders --&gt; Events[event stream — fulfillment and notifications]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Catalog --&gt; Events&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Catalog should be optimized for product discovery, not purchase finality. It can be document-oriented, search-indexed, cached, and rebuilt from authoritative product sources. Catalog availability shown to the user is often a hint, not a promise. The promise happens later, at reservation.&lt;/p&gt;
&lt;p&gt;Cart should represent intent, not revenue. It can expire aggressively, tolerate last-write-wins semantics, and store product snapshots only when needed for user experience. Cart storage should be horizontally cheap because cart write volume can exceed order volume by orders of magnitude.&lt;/p&gt;
&lt;p&gt;Orders should be the commercial ledger. Once an order is placed, the system should prefer append-only events or tightly controlled state transitions over arbitrary mutation. &lt;code&gt;OrderCreated&lt;/code&gt;, &lt;code&gt;PaymentAuthorized&lt;/code&gt;, &lt;code&gt;InventoryReserved&lt;/code&gt;, &lt;code&gt;FulfillmentReleased&lt;/code&gt;, and &lt;code&gt;RefundIssued&lt;/code&gt; are operational facts. They are not merely fields on a row.&lt;/p&gt;
&lt;p&gt;Inventory should own stock truth. The important decision is whether checkout reserves inventory before payment, after authorization, or asynchronously. Each choice has a business cost. Reserve too early and carts lock scarce goods. Reserve too late and paid orders can oversell. Reserve asynchronously and the customer experience must handle apology, substitution, or backorder flows.&lt;/p&gt;
&lt;p&gt;Payments should own idempotency and reconciliation. The payment system should record every attempted external operation with an idempotency key, request hash, provider reference, response, and final reconciliation state. Order creation may request payment, but it should not pretend the local order transaction and the remote payment operation are one atomic commit.&lt;/p&gt;
&lt;p&gt;The checkout coordinator is therefore not a giant transaction. It is a command boundary. It validates the cart, requests inventory reservation, creates an order record, requests payment authorization, and emits durable events. When one step fails, the coordinator executes compensating transitions rather than pretending it can roll back the world.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Public cloud documentation describes shopping carts as a canonical high-scale key-value workload. AWS documents DynamoDB as suitable for a shopping cart use case with single-digit millisecond performance across very large user counts: &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html&quot;&gt;Amazon DynamoDB introduction&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The documented pattern is to keep cart access keyed by buyer or session, avoid cross-cart joins, and let cart entries expire. This makes cart storage independent from order durability.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Cart traffic can scale without forcing checkout, inventory, or payment tables to absorb every add, remove, and quantity-change event.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Cart data is intent. Treating intent like revenue creates unnecessary coupling.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; PostgreSQL documents row-level locking behavior for statements such as &lt;code&gt;SELECT FOR UPDATE&lt;/code&gt;, and also notes that deadlocks can occur with row-level locks: &lt;a href=&quot;https://www.postgresql.org/docs/17/explicit-locking.html&quot;&gt;PostgreSQL explicit locking&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The documented database behavior supports an inventory pattern where reservations update a constrained set of stock rows under transaction control. The reservation write is small, explicit, and separated from catalog browsing.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The contention surface is reduced to the SKU, location, or stock bucket being reserved. Search, cart editing, and order history do not participate in the lock path.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Inventory correctness is a concurrency problem. It should not be mixed with high-fanout read models.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Stripe publicly documents idempotency for mutating API requests and explains that retry safety matters because clients and APIs form a distributed system: &lt;a href=&quot;https://docs.stripe.com/api/idempotent_requests&quot;&gt;Stripe idempotent requests&lt;/a&gt; and &lt;a href=&quot;https://stripe.com/blog/idempotency&quot;&gt;Stripe engineering on idempotency&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The documented payment pattern is to attach an idempotency key to a logical operation and persist the first result for that key.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; A timeout between checkout and payment provider does not require guessing whether to retry. The retry can reuse the same operation identity.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Payments are not just writes. They are external side effects requiring replay-safe command design.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Shopify also documents idempotency as a way to retry failed API requests without duplication or conflict: &lt;a href=&quot;https://shopify.dev/docs/api/usage/idempotent-requests&quot;&gt;Shopify idempotent requests&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The acknowledged pattern is to make client and server retries safe by assigning stable operation identity.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Network failure becomes a recoverable condition instead of a duplicate-order or duplicate-charge incident.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Retry behavior is part of the data model.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Boundary&lt;/th&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;th&gt;Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Catalog to cart&lt;/td&gt;&lt;td&gt;Product price or availability changes after add-to-cart&lt;/td&gt;&lt;td&gt;Reprice and revalidate at checkout&lt;/td&gt;&lt;td&gt;Users may see cart changes late&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cart to order&lt;/td&gt;&lt;td&gt;Duplicate checkout submission&lt;/td&gt;&lt;td&gt;Checkout idempotency key&lt;/td&gt;&lt;td&gt;Requires persisted command records&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Order to inventory&lt;/td&gt;&lt;td&gt;Paid order cannot reserve stock&lt;/td&gt;&lt;td&gt;Reserve before capture or support backorder compensation&lt;/td&gt;&lt;td&gt;Either lower conversion or more exception handling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Inventory to fulfillment&lt;/td&gt;&lt;td&gt;Reservation never converts to shipment&lt;/td&gt;&lt;td&gt;Reservation expiry and reconciliation jobs&lt;/td&gt;&lt;td&gt;Requires operational cleanup paths&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Order to payment&lt;/td&gt;&lt;td&gt;Payment succeeds but order write fails&lt;/td&gt;&lt;td&gt;Payment ledger and reconciliation by provider reference&lt;/td&gt;&lt;td&gt;Adds recovery workflow&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Payment to order&lt;/td&gt;&lt;td&gt;Payment retry creates duplicate charge&lt;/td&gt;&lt;td&gt;Idempotency key and request hash&lt;/td&gt;&lt;td&gt;Requires stable operation identity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Events to downstream systems&lt;/td&gt;&lt;td&gt;Email or fulfillment receives duplicate events&lt;/td&gt;&lt;td&gt;Consumer idempotency and event identifiers&lt;/td&gt;&lt;td&gt;Every consumer owns dedupe logic&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The important architectural smell is not eventual consistency. Eventual consistency is often the right answer. The smell is hidden inconsistency: no ledger, no operation identity, no reconciliation path, and no clear owner for the disputed fact.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; One database makes checkout look atomic while catalog, cart, orders, inventory, and payments have different correctness requirements.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Split the model by consistency domain: searchable catalog, ephemeral cart, durable order ledger, transactional inventory reservation, and idempotent payment ledger.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Known systems and documented behaviors support the split: key-value carts scale independently, row locks constrain inventory contention, and idempotency keys make payment retries safe.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Draw the checkout state machine before drawing tables. For every transition, define the owner, idempotency key, retry behavior, timeout behavior, reconciliation query, and customer-visible fallback.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Partitioning Is Not a Performance Feature by Default</title><link>https://rajivonai.com/blog/2023-08-21-partitioning-is-not-a-performance-feature-by-default/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-08-21-partitioning-is-not-a-performance-feature-by-default/</guid><description>PostgreSQL declarative partitioning only speeds up queries when the partition key appears in the WHERE clause — without it, you get the overhead of many tables with none of the pruning benefit.</description><pubDate>Mon, 21 Aug 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Partitioning a PostgreSQL table does not make queries faster. Partition pruning makes queries faster — and pruning only happens when the query’s WHERE clause includes the partition key.&lt;/strong&gt; Teams partition large tables expecting a general performance improvement, then discover that analytics queries without a date filter now touch every partition instead of one unified table, and the planner overhead makes things worse than before. Partitioning is a data management feature first; it is a performance feature only under specific, verifiable conditions.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL declarative partitioning (introduced in PG10, significantly improved in PG11–PG13) routes rows to child tables based on a partition key — most commonly a date column for time-series data. The mental model engineers carry is usually: “the table is split into smaller pieces, so queries run faster.” That is true only when the planner can eliminate the pieces that are not relevant.&lt;/p&gt;
&lt;p&gt;Teams with large event, audit, order, or log tables encounter partitioning as the recommended solution to table size problems. The recommendation is often correct, but the mechanism is misunderstood. Partitioning helps with archival (you can drop a partition instantly rather than running a DELETE), parallel query (PG11+ can parallelize across partitions), and large-table DDL operations. It does not help — and can hurt — when queries touch all partitions.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;When PostgreSQL receives a query against a partitioned table, it checks whether the planner can eliminate partitions based on the WHERE clause. This is partition pruning. PostgreSQL documents two types: static pruning at planning time (for literal values in the WHERE clause) and runtime pruning during execution (for parameterized queries, available since PG11 with &lt;code&gt;enable_partition_pruning = on&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Pruning requires the WHERE clause to include the partition key with a condition that maps to a subset of partitions. A range-partitioned table on &lt;code&gt;created_at&lt;/code&gt; prunes when you write &lt;code&gt;WHERE created_at &gt;= &apos;2024-01-01&apos; AND created_at &amp;#x3C; &apos;2024-02-01&apos;&lt;/code&gt;. It does not prune when you write &lt;code&gt;WHERE user_id = 12345&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The failure mode: a team partitions an &lt;code&gt;orders&lt;/code&gt; table by &lt;code&gt;created_at&lt;/code&gt; month, creating 36 partitions for three years of data. Most OLTP queries are by &lt;code&gt;order_id&lt;/code&gt; or &lt;code&gt;user_id&lt;/code&gt; — neither of which is the partition key. The planner must now plan against 36 child tables instead of one, generate separate plan nodes for each, and execute the query across all of them. Parallel query on partitions helps only if the query is large enough to benefit from parallelism — for point lookups, it adds overhead without benefit.&lt;/p&gt;
&lt;p&gt;You can verify whether pruning is happening using &lt;code&gt;EXPLAIN&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;2024-03-01&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;2024-04-01&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The plan should show only the relevant partition(s) under &lt;code&gt;Append&lt;/code&gt; or &lt;code&gt;Merge Append&lt;/code&gt;. If you see all 36 listed, the prune did not occur.&lt;/p&gt;
&lt;p&gt;The core question: what conditions must be true for partitioning to improve — rather than degrade — performance?&lt;/p&gt;
&lt;h2 id=&quot;how-partition-pruning-actually-works&quot;&gt;How Partition Pruning Actually Works&lt;/h2&gt;
&lt;p&gt;The planner evaluates partition constraints during planning. For a range partition on &lt;code&gt;created_at&lt;/code&gt;, the constraint is effectively &lt;code&gt;created_at &gt;= lower_bound AND created_at &amp;#x3C; upper_bound&lt;/code&gt;. If the WHERE clause contains a compatible condition on &lt;code&gt;created_at&lt;/code&gt;, the planner eliminates non-matching partitions before execution.&lt;/p&gt;
&lt;p&gt;Two settings control this behavior:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;enable_partition_pruning&lt;/code&gt; (default: &lt;code&gt;on&lt;/code&gt;) — enables both static and runtime pruning. Disabling this will cause the planner to scan all partitions on every query.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;constraint_exclusion&lt;/code&gt; (default: &lt;code&gt;partition&lt;/code&gt;) — enables exclusion based on &lt;code&gt;CHECK&lt;/code&gt; constraints for inheritance-based partitioning (pre-PG10 style). For declarative partitioning, &lt;code&gt;partition&lt;/code&gt; is the correct setting; setting this to &lt;code&gt;on&lt;/code&gt; adds unnecessary overhead on non-partitioned tables.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When partitioning genuinely helps:&lt;/p&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Use case&lt;/th&gt;&lt;th&gt;Why partitioning helps&lt;/th&gt;&lt;th&gt;What to verify&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Time-series archival&lt;/td&gt;&lt;td&gt;Drop old partitions instantly without a table lock&lt;/td&gt;&lt;td&gt;&lt;code&gt;DROP TABLE orders_2021&lt;/code&gt; completes in milliseconds&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Range-filtered analytics&lt;/td&gt;&lt;td&gt;Prune scans to relevant time window&lt;/td&gt;&lt;td&gt;EXPLAIN shows only matching partitions in plan&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Parallel query on large scans&lt;/td&gt;&lt;td&gt;PG11+ can assign workers per partition&lt;/td&gt;&lt;td&gt;&lt;code&gt;EXPLAIN&lt;/code&gt; shows &lt;code&gt;Parallel Append&lt;/code&gt; with multiple workers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bulk data ingestion&lt;/td&gt;&lt;td&gt;New data lands in the current-period partition, reducing index maintenance scope&lt;/td&gt;&lt;td&gt;Insert throughput measured before and after&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;When partitioning hurts or provides no benefit:&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Pattern&lt;/th&gt;&lt;th&gt;Problem&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Queries filter only on non-partition-key columns&lt;/td&gt;&lt;td&gt;All partitions scanned; planner overhead added&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Default partition exists&lt;/td&gt;&lt;td&gt;Some planners cannot prune past a default partition, causing all partitions to be scanned&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Very high partition count (500+)&lt;/td&gt;&lt;td&gt;Planning time increases linearly with partition count even when pruning works&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Foreign keys referencing a partitioned table&lt;/td&gt;&lt;td&gt;Foreign key checks must scan all partitions&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s declarative partitioning documentation (postgresql.org/docs/current/ddl-partitioning.html) describes partition pruning explicitly: “The query planner will only apply partition pruning when the query’s WHERE clause contains a condition on the partition key.” The documentation also notes that runtime pruning requires &lt;code&gt;enable_partition_pruning = on&lt;/code&gt; and is available for parameterized queries when the partition key appears in the plan’s parameter bindings.&lt;/p&gt;
&lt;p&gt;The documented PostgreSQL behavior for &lt;code&gt;DROP TABLE&lt;/code&gt; on a partition is that it completes in milliseconds regardless of partition size, because it removes the child table’s storage files without scanning rows — this is the principal operational benefit of partitioning for time-series data with defined retention policies.&lt;/p&gt;
&lt;p&gt;PostgreSQL 11’s release notes document the introduction of partition-wise joins and partition-wise aggregation as explicit opt-in settings (&lt;code&gt;enable_partitionwise_join&lt;/code&gt;, &lt;code&gt;enable_partitionwise_aggregate&lt;/code&gt;). These are off by default because they can increase planning time significantly on highly partitioned schemas.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Query lacks partition key in WHERE&lt;/td&gt;&lt;td&gt;All partitions scanned; query may be slower than on a non-partitioned table of the same total size&lt;/td&gt;&lt;td&gt;Planner cannot eliminate any partition; must generate plan nodes for all child tables&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Default partition prevents pruning&lt;/td&gt;&lt;td&gt;Even queries with the partition key may scan the default partition&lt;/td&gt;&lt;td&gt;Planner cannot prove a value is not in the default partition without scanning it&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Partition key does not match primary query access pattern&lt;/td&gt;&lt;td&gt;Partitioning optimizes the wrong dimension; primary key and foreign key lookups cross all partitions&lt;/td&gt;&lt;td&gt;Design decision cannot be undone without a full table rewrite&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Partitioning a table on a date column and then running OLTP queries filtered by user ID or order ID produces a plan that scans all partitions — no pruning, more overhead.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Validate that the most frequent WHERE clause patterns include the partition key before committing to a partitioning scheme; use &lt;code&gt;EXPLAIN&lt;/code&gt; to confirm partition pruning in production-representative queries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: &lt;code&gt;EXPLAIN&lt;/code&gt; output for a date-filtered query shows only the relevant partition(s) listed under the &lt;code&gt;Append&lt;/code&gt; node — not all 36.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, run &lt;code&gt;EXPLAIN&lt;/code&gt; on the five highest-volume queries against any recently partitioned table and check whether the plan shows one partition or many — if the answer is many, the partitioning key is wrong for those queries.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>OCI for Oracle-Heavy Enterprises: Migration Pattern, Risk Boundary, and Cost Model</title><link>https://rajivonai.com/blog/2023-08-19-oci-for-oracle-heavy-enterprises-migration-pattern-risk-boundary-and-cost-model/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-08-19-oci-for-oracle-heavy-enterprises-migration-pattern-risk-boundary-and-cost-model/</guid><description>OCI migration risk model for Oracle-heavy enterprises — where the lift-and-shift boundary shifts from the database tier into dependent application contracts.</description><pubDate>Sat, 19 Aug 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The expensive OCI migration is not the one where Oracle databases move slowly; it is the one where the enterprise accidentally moves the risk boundary from the database tier into every dependent application at the same time.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Oracle-heavy enterprises rarely start cloud migration from a clean portfolio. They usually start with decades of Oracle Database, RAC, Exadata, Data Guard, RMAN, batch schedulers, ERP integrations, reporting replicas, vendor packages, and operational runbooks that assume stable network topology and known failure behavior.&lt;/p&gt;
&lt;p&gt;That estate creates a different cloud question from a generic replatforming program. The strategic issue is not whether workloads can run on Kubernetes, whether object storage is cheaper than SAN, or whether a new data platform would be more modern. The first-order issue is that the database is already the system of record, the operational contracts are already written around Oracle behavior, and the blast radius of a failed migration includes month-end close, payroll, order capture, tax, inventory, and customer commitments.&lt;/p&gt;
&lt;p&gt;OCI is attractive in this context because it gives Oracle-heavy enterprises a lower-friction target for Oracle Database services, Exadata-based capacity, managed database operations, and multicloud adjacency. But that does not make the migration simple. It changes the shape of the problem: the safest migration is usually not a full-stack rewrite, but a staged relocation of the Oracle control plane with hard gates around latency, licensing, failover, and cost attribution.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Most cloud migration plans fail Oracle estates in one of three ways.&lt;/p&gt;
&lt;p&gt;The first failure mode is treating database migration as an application migration dependency. Teams create a massive dependency graph, declare that app and database tiers must move together, and then discover that every cutover window requires coordinated changes across connection pools, DNS, batch jobs, firewall rules, reporting users, and operational dashboards. The program becomes a release train with database physics attached.&lt;/p&gt;
&lt;p&gt;The second failure mode is underestimating stateful rollback. Stateless services can often redeploy, reroute, or scale out. Oracle databases require point-in-time recovery strategy, redo transport design, replication lag monitoring, backup validation, and a decision about whether the old primary can safely resume writes after a cutover failure.&lt;/p&gt;
&lt;p&gt;The third failure mode is treating cloud cost as a rate-card exercise. For Oracle estates, cost is not just compute, storage, and network. It is license position, Exadata shape, database edition, support model, backup retention, disaster recovery capacity, migration overlap, reserved capacity, and the operational cost of keeping parallel environments alive.&lt;/p&gt;
&lt;p&gt;The question is therefore: how do you move an Oracle-heavy enterprise to OCI without turning the database migration into a full-enterprise outage domain?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;The practical architecture is a database-first migration boundary. Move the Oracle estate into an OCI landing zone designed for database operations, keep application movement optional, and use private connectivity to preserve controlled communication between tiers during transition.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[Oracle estate — RAC, Exadata, ERP databases] --&gt; B[Discovery — workload classes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[Risk boundary — database first]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D[OCI database landing zone — VCN, IAM, keys]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; E[Migration lane — ZDM, Data Guard, GoldenGate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; F[Cutover gate — lag, backups, rollback]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; G[Application remap — connection pools and batch]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; H[Cost loop — tags, budgets, unit metrics]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; I[Keep app tier where it runs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; J[Private connectivity — FastConnect or interconnect]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  J --&gt; G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The boundary has one rule: only dependencies required for database correctness cross it early. That usually includes identity, networking, key management, backup storage, observability, replication, and runbooks. It does not automatically include every application server, reporting tool, ETL job, or vendor appliance.&lt;/p&gt;
&lt;p&gt;This pattern gives the program three control points.&lt;/p&gt;
&lt;p&gt;First, classify workloads by recoverability, not by org chart. A Tier 0 database with synchronous business impact needs a different lane from a reporting replica. For each database, document RPO, RTO, peak write rate, backup size, maintenance windows, database version, option usage, character set, external directory dependencies, and application connection behavior.&lt;/p&gt;
&lt;p&gt;Second, build the OCI landing zone around operational contracts. The database subnet, route tables, security lists or network security groups, IAM policies, KMS keys, vaults, backup policy, monitoring, DNS, and logging must exist before migration tooling touches production. This is where many programs lose time: they build a cloud account and call it a landing zone, but the database team still cannot answer who can restore, who can rotate keys, who can approve failover, and who gets paged on replication lag.&lt;/p&gt;
&lt;p&gt;Third, treat cutover as a controlled state transition. A safe cutover gate includes validated backup, measured replication lag, application freeze rules, connection drain behavior, rollback authority, post-cutover smoke tests, and a written rule for when rollback is no longer safe because writes have committed on the target.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Oracle documents Zero Downtime Migration as a migration utility for moving Oracle databases into Oracle-owned infrastructure, including OCI and Exadata Cloud targets. The documented pattern supports online and offline migration paths, and the offline path can use Object Storage as the intermediate backup location. See Oracle’s &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/zero-downtime-migration/19.7/zdmug/introduction-to-zero-downtime-migration.html&quot;&gt;Zero Downtime Migration documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Use ZDM as the orchestrated migration lane when the source and target meet support requirements. Keep the migration lane separate from the application modernization lane. That means the database team owns replication, backup, restore, and cutover verification, while application teams own connection behavior and functional validation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The result is not literally zero risk; it is a smaller risk boundary. The operational result is that the enterprise can rehearse database movement before committing every application tier to OCI. Failed rehearsals produce database-specific fixes instead of enterprise-wide release delays.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The documented pattern is that stateful migration needs a migration control plane, not a collection of manual restore steps. ZDM is useful because it makes the migration sequence explicit, but the engineering value comes from the surrounding gates: prechecks, backup validation, lag measurement, and rollback decision points.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Oracle’s Maximum Availability Architecture patterns use technologies such as Data Guard, Active Data Guard, backups, and cross-region deployment to define database availability posture. Oracle’s MAA guidance for Exadata and cloud database services emphasizes role transition, protection mode, and recovery design rather than simple VM placement. See Oracle’s &lt;a href=&quot;https://docs.oracle.com/en/database/oracle/oracle-database/19/haovw/oracle-maximum-availability-architecture-oracle-databaseaws.html&quot;&gt;MAA documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Map each workload to an availability tier before choosing the OCI service shape. A dev database, a reporting standby, a regional ERP database, and a global financial close system should not share the same architecture just because they are all Oracle.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The result is a cost and resilience model with visible tradeoffs. Some systems justify Exadata Database Service, cross-region standby, and aggressive recovery objectives. Others are better served by simpler database services, backup-driven recovery, or scheduled migration windows.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The documented pattern is that high availability is an application contract expressed through database topology. OCI does not remove the need to choose protection levels; it makes the cost of each protection level more explicit.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Oracle and Microsoft document private interconnection between Azure and OCI through ExpressRoute and FastConnect for cross-cloud Oracle workloads. This matters because many Oracle-heavy enterprises also have application, identity, analytics, or integration tiers in Azure. See Microsoft’s &lt;a href=&quot;https://learn.microsoft.com/en-us/azure/virtual-machines/workloads/oracle/configure-azure-oci-networking&quot;&gt;Azure and OCI networking guidance&lt;/a&gt; and Oracle’s &lt;a href=&quot;https://blogs.oracle.com/cloud-infrastructure/post/overview-of-the-interconnect-between-oracle-and-microsoft&quot;&gt;interconnect overview&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Use private connectivity when the application tier stays outside OCI during the first migration phase. Measure latency and failure behavior under production-like load before declaring the architecture acceptable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The result is a migration path that does not require all application tiers to move on the database cutover date. It also exposes hidden assumptions: chatty SQL access, hardcoded database addresses, batch windows that depend on LAN latency, and reporting jobs that overload the primary.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The documented pattern is that multicloud adjacency is useful only when latency, routing, DNS, and failover behavior are engineered as first-class production dependencies.&lt;/p&gt;
&lt;h2 id=&quot;cost-model&quot;&gt;Cost Model&lt;/h2&gt;
&lt;p&gt;The useful OCI cost model is not a single monthly estimate. It is a set of cost buckets tied to architectural decisions.&lt;/p&gt;
&lt;p&gt;Start with database capacity: service type, Exadata shape, OCPU allocation, storage, database edition, options, and license model. Then add resilience: standby capacity, cross-region replication, backup retention, recovery service, test restores, and nonproduction environments. Then add network: FastConnect, VPN, interconnect, data transfer, DNS, and observability traffic. Then add migration overlap: source environment, target environment, replication tooling, temporary storage, parallel support, and extended freeze windows.&lt;/p&gt;
&lt;p&gt;The model should produce three numbers:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Steady-state run cost:&lt;/strong&gt; what the estate costs after migration and decommissioning.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Migration overlap cost:&lt;/strong&gt; what the enterprise pays while both old and new environments run.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Risk-reduction cost:&lt;/strong&gt; what is intentionally spent on standby, backup, rehearsal, monitoring, and rollback.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;OCI Cost Management supports cost analysis, reports, budgets, and scheduled reporting, which makes it suitable for a tagged cost loop rather than a one-time spreadsheet. See Oracle’s &lt;a href=&quot;https://docs.oracle.com/en-us/iaas/Content/Billing/Concepts/costmanagementoverview.htm&quot;&gt;Cost Management overview&lt;/a&gt; and &lt;a href=&quot;https://docs.oracle.com/iaas/Content/Billing/Concepts/FinOps.htm&quot;&gt;FinOps Hub documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Application latency surprise&lt;/td&gt;&lt;td&gt;The app tier remains outside OCI but was written for low-latency database access&lt;/td&gt;&lt;td&gt;Run production-like SQL traces and batch tests across the private link before cutover&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Rollback ambiguity&lt;/td&gt;&lt;td&gt;Teams do not define when writes make rollback unsafe&lt;/td&gt;&lt;td&gt;Create a written rollback gate with ownership, timing, and data divergence rules&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cost overrun&lt;/td&gt;&lt;td&gt;Source and target run in parallel longer than planned&lt;/td&gt;&lt;td&gt;Track migration overlap as its own cost category with an executive burn-down&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;License confusion&lt;/td&gt;&lt;td&gt;Database options and editions are not inventoried before sizing&lt;/td&gt;&lt;td&gt;Run option usage discovery and map license position before target architecture selection&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Standby underdesign&lt;/td&gt;&lt;td&gt;DR is copied from on-premises without validating cloud failure domains&lt;/td&gt;&lt;td&gt;Assign each workload an RPO and RTO tier, then design standby topology from that contract&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tooling optimism&lt;/td&gt;&lt;td&gt;ZDM or replication tooling is treated as the whole plan&lt;/td&gt;&lt;td&gt;Pair migration tooling with rehearsals, observability, backup validation, and cutover authority&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Oracle estates fail cloud migration when the database move becomes coupled to every application and operational dependency at once.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Put OCI behind a database-first risk boundary, migrate Oracle systems through explicit lanes, and keep application movement optional until latency and cutover behavior are proven.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Use documented Oracle migration, availability, interconnect, and cost-management patterns rather than invented transformation stories.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Inventory workload tiers, build the OCI database landing zone, rehearse one representative migration per tier, publish the rollback gate, and track steady-state, overlap, and risk-reduction cost separately.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>OCI Disaster Recovery Review: Regions, ADs, Backups, Data Guard, and GoldenGate</title><link>https://rajivonai.com/blog/2023-08-04-oci-disaster-recovery-review-regions-ads-backups-data-guard-and-goldengate/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-08-04-oci-disaster-recovery-review-regions-ads-backups-data-guard-and-goldengate/</guid><description>OCI disaster recovery gaps that emerge when teams rely on regional failover alone, and how Data Guard and GoldenGate address the database replication tier.</description><pubDate>Fri, 04 Aug 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Disaster recovery fails when teams treat the cloud region as the failure boundary and the database as a restore problem.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;OCI gives engineering teams several layers of isolation: regions, availability domains, fault domains, object storage durability, block volume backups, database backups, Data Guard, and GoldenGate. Each layer solves a different failure mode. None of them, alone, is a disaster recovery architecture.&lt;/p&gt;
&lt;p&gt;A region protects against local infrastructure loss only if the application has a tested path to another region. An availability domain protects against facility-level failure only if the application can tolerate losing a datacenter. A backup protects against corruption only if restore time and restore point are acceptable. Data Guard protects Oracle Database continuity by shipping redo to a standby database. GoldenGate supports logical replication and cross-platform movement, but it introduces ordering, conflict, and operational complexity.&lt;/p&gt;
&lt;p&gt;The mistake is to collapse these into one vague promise: “we have DR.” That phrase hides the only questions that matter: what breaks, what data is lost, who decides to fail over, and how the system returns to steady state.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Most DR plans are written for infrastructure loss, but most incidents start smaller and uglier.&lt;/p&gt;
&lt;p&gt;A bad deployment corrupts data. A batch job deletes rows. A network path between application and database becomes unstable. A regional control plane is impaired. A standby database is behind because redo transport is lagging. A GoldenGate extract stops while the application continues writing. Object storage contains backups, but the restore procedure has not been timed against the real database size.&lt;/p&gt;
&lt;p&gt;These are not the same incident. They need different recovery mechanics.&lt;/p&gt;
&lt;p&gt;Backups are excellent for recovery from logical corruption, but they are usually too slow for low-RTO service continuity. Data Guard is excellent for Oracle Database failover, but it replicates many logical mistakes quickly. GoldenGate can support active-active or selective replication patterns, but it is not a free consistency layer. Multi-AD placement improves availability inside a region, but it does not protect against regional loss. Cross-region standby improves survivability, but it adds replication lag, routing, identity, secrets, and runbook complexity.&lt;/p&gt;
&lt;p&gt;The core question is simple: which OCI capability should own each failure mode, and how do you prove the handoff works before the incident?&lt;/p&gt;
&lt;h2 id=&quot;a-layered-oci-dr-architecture&quot;&gt;A Layered OCI DR Architecture&lt;/h2&gt;
&lt;p&gt;The practical answer is to separate availability, recoverability, and continuity.&lt;/p&gt;
&lt;p&gt;Availability is handled inside the primary region with multiple availability domains where available, fault domains, load balancers, stateless application nodes, and automated replacement. Recoverability is handled with backups, retention policies, restore tests, and immutable or protected storage where the risk model requires it. Continuity is handled with a prebuilt standby path: Data Guard for Oracle Database role transition, GoldenGate where logical replication or heterogeneous targets are required, and DNS or traffic management for client cutover.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[primary region — production entrypoint] --&gt; B[availability domain one — application tier]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A --&gt; C[availability domain two — application tier]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; D[primary database — oracle workload]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt;|redo transport| E[standby database — data guard]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt;|logical trail| F[target datastore — goldengate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt;|scheduled backup| G[object storage — protected backups]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; H[configuration store — replicated secrets]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I[recovery runbook — tested cutover] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  J[traffic manager — regional failover] --&gt; A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  J --&gt; K[standby region — recovery entrypoint]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  K --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The key design decision is not “Data Guard or GoldenGate.” It is which state transition you need.&lt;/p&gt;
&lt;p&gt;Use backups when the business can tolerate restore time and when the failure is corruption, accidental deletion, ransomware exposure, or a need to recover to a point before the mistake. Backups should be treated as a recovery product, not a compliance artifact. A backup that has never been restored is an assumption.&lt;/p&gt;
&lt;p&gt;Use Data Guard when the primary requirement is Oracle Database continuity with a standby database that can be promoted. The operational center is redo transport, apply lag, protection mode, switchover discipline, and application reconnection. Data Guard is strongest when the application can tolerate a database role transition and when failover authority is explicit.&lt;/p&gt;
&lt;p&gt;Use GoldenGate when the requirement is logical replication: cross-version migration, heterogeneous replication, selective table movement, regional read locality, or active-active designs with conflict handling. GoldenGate gives flexibility, but that flexibility means the team must own replication topology, trail retention, checkpoint health, schema drift, and conflict semantics.&lt;/p&gt;
&lt;p&gt;Use multi-AD design for regional availability, not regional disaster recovery. It reduces blast radius for compute and service placement, but it does not remove the need for cross-region recovery if the region becomes unavailable.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Oracle documents Maximum Availability Architecture as a pattern that combines local high availability, Data Guard, backups, and operational practices rather than relying on one product. The documented pattern is that different failure scopes require different controls.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Apply that model directly in OCI. Place stateless services across fault domains and availability domains where available. Keep the database protected with Data Guard when RTO demands standby promotion. Maintain backups for point-in-time recovery. Add GoldenGate only where logical replication is required, not as a default replacement for Data Guard.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The architecture has separate recovery paths. A compute failure is handled by replacement capacity. A facility failure is handled inside the region when the region has multiple availability domains. A database host or storage failure is handled through database HA features. A regional disaster is handled through standby promotion and traffic movement. A logical corruption incident is handled by restore or point-in-time recovery.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The documented pattern is that DR architecture is a portfolio of controls. Data Guard reduces downtime for Oracle Database role transitions, but it is not a substitute for backups. Backups can recover older state, but they do not provide instant continuity. GoldenGate can move logical changes, but it makes consistency and conflict decisions visible operational responsibilities.&lt;/p&gt;
&lt;p&gt;A second documented behavior matters: Oracle Data Guard applies redo from the primary database to the standby database. That is its strength and its hazard. If the primary commits a bad logical change, the standby may faithfully receive it. This is why a DR plan that says “Data Guard protects the database” is incomplete. It protects continuity, not necessarily correctness.&lt;/p&gt;
&lt;p&gt;GoldenGate has the opposite shape. It works at the logical change level and uses extract, trail, pump, and replicat processes. That makes it powerful for selective replication and migration, but also sensitive to schema changes, process lag, trail storage, and conflict policy. The documented pattern is to operate GoldenGate as a replication system with observability and runbooks, not as background plumbing.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Weak default assumption&lt;/th&gt;&lt;th&gt;Better OCI pattern&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Regional outage&lt;/td&gt;&lt;td&gt;Multi-AD means DR is done&lt;/td&gt;&lt;td&gt;Use cross-region standby, replicated configuration, and traffic cutover&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Logical corruption&lt;/td&gt;&lt;td&gt;Standby database is safe&lt;/td&gt;&lt;td&gt;Use backups and point-in-time recovery with restore drills&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Database failover&lt;/td&gt;&lt;td&gt;Promotion is only a database task&lt;/td&gt;&lt;td&gt;Test application reconnect, DNS, credentials, connection pools, and jobs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;GoldenGate lag&lt;/td&gt;&lt;td&gt;Replication is always current&lt;/td&gt;&lt;td&gt;Monitor extract, trail, replicat, checkpoints, and apply delay&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup compliance&lt;/td&gt;&lt;td&gt;Successful backup equals recovery&lt;/td&gt;&lt;td&gt;Measure restore time with production-scale data&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Control plane issue&lt;/td&gt;&lt;td&gt;Runbooks can be improvised&lt;/td&gt;&lt;td&gt;Pre-stage access, scripts, break-glass roles, and manual decision paths&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Return to primary&lt;/td&gt;&lt;td&gt;Failover is the end&lt;/td&gt;&lt;td&gt;Plan reinstate, resync, validation, and traffic return&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The hardest failure is not the initial outage. It is the moment after failover when the team must decide whether the new primary is authoritative, whether old writers are fully fenced, and whether downstream systems agree on time, identity, and data ownership.&lt;/p&gt;
&lt;p&gt;That is why every DR test should include failure entry, failover, validation, degraded operation, and return. A switchover exercise that stops after database promotion is not a disaster recovery test. It is a database role-change test.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Treating OCI DR as a checklist creates hidden coupling between regions, databases, backups, replication, and application routing.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Assign each OCI capability to a failure mode: multi-AD for local availability, backups for recoverability, Data Guard for Oracle Database continuity, GoldenGate for logical replication, and traffic management for regional cutover.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run timed exercises. Prove backup restore time, Data Guard switchover and failover, GoldenGate lag recovery, application reconnect behavior, and cross-region configuration readiness.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Write the runbook around decisions, not tools: declare failure, fence writers, promote or restore, redirect traffic, validate data, operate degraded, resync, and return to steady state.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Deadlocks vs Blocking: The Difference Engineers Miss</title><link>https://rajivonai.com/blog/2023-07-31-deadlocks-vs-blocking-the-difference-engineers-miss/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-07-31-deadlocks-vs-blocking-the-difference-engineers-miss/</guid><description>Blocking and deadlocks are two distinct failure modes that require opposite responses — confusing them leads to retry logic that doesn&apos;t help and investigations that point at the wrong cause.</description><pubDate>Mon, 31 Jul 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Deadlocks and blocking look similar in a dashboard — queries stuck, latency climbing, transactions piling up — but the database resolves them differently, and so must you.&lt;/strong&gt; Adding retry logic when you have a blocking problem won’t help. Investigating lock contention when you have a long-running transaction holding locks will send you down the wrong path entirely. These are two distinct failure modes. Treating them as one is how engineers waste hours in incident response.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Row-level locking is how relational databases protect concurrent writes. Any transaction that modifies a row acquires a lock on it; others that need the same row wait. This is expected behavior — not a bug — and for most workloads it resolves quickly as transactions commit or roll back.&lt;/p&gt;
&lt;p&gt;Lock problems surface when that assumption breaks: a transaction holds a lock longer than expected, two transactions each wait for what the other holds, or a missing index forces the database to lock far more rows than necessary. The symptoms look similar from the outside — stalled queries, timeouts, connection pool pressure — but the causes and correct responses are completely different.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Engineers see “lock wait timeout exceeded” or a deadlock error, conclude there is a locking problem, and apply whatever fix they read about most recently — retry logic, a &lt;code&gt;lock_timeout&lt;/code&gt; change, an index. Any of those might be wrong for the actual problem present.&lt;/p&gt;
&lt;p&gt;Blocking and deadlocks have different root causes, different detection mechanisms, and different remediation paths. Applying deadlock fixes to a blocking problem — or vice versa — obscures the real signal and delays finding the actual cause.&lt;/p&gt;
&lt;p&gt;The core question: given a stalled transaction or a lock error, how do you determine which condition you have, and what do you do about each one?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;These are not the same condition expressed at different severity levels. They are structurally different.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Blocking&lt;/strong&gt; is one transaction waiting for a lock held by another. The waiter sits until the holder commits or rolls back — no automatic resolution occurs. The database waits indefinitely (or until a &lt;code&gt;lock_timeout&lt;/code&gt; fires). The fix is almost always about the holder: find it, understand why it’s holding the lock longer than expected, and address that.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A deadlock&lt;/strong&gt; is a cycle. Transaction A holds lock X and waits for lock Y. Transaction B holds lock Y and waits for lock X. Neither can proceed. PostgreSQL and MySQL InnoDB detect this automatically via a wait-for graph, pick one transaction as the victim, and terminate it — the other proceeds. Deadlocks resolve themselves; the application must handle the error and retry. The fix is about eliminating the cycle, typically by acquiring locks in a consistent order across transactions.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph Blocking [Blocking — Linear Wait]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        T1[Transaction A] --&gt;|Holds Lock| R1[Row 1]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        T2[Transaction B] --&gt;|Waits for Lock| R1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph Deadlock [Deadlock — Circular Wait]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        T3[Transaction C] --&gt;|Holds Lock| R2[Row 2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        T4[Transaction D] --&gt;|Holds Lock| R3[Row 3]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        T3 --&gt;|Waits for Lock| R3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        T4 --&gt;|Waits for Lock| R2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;Blocking&lt;/th&gt;&lt;th&gt;Deadlock&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Cause&lt;/td&gt;&lt;td&gt;One transaction holds a lock another needs&lt;/td&gt;&lt;td&gt;Two transactions each wait for what the other holds&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Resolution&lt;/td&gt;&lt;td&gt;Manual — requires the holder to commit or roll back&lt;/td&gt;&lt;td&gt;Automatic — database detects the cycle and kills one victim&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Error surfaced&lt;/td&gt;&lt;td&gt;&lt;code&gt;lock_timeout&lt;/code&gt; if configured; otherwise the query just waits&lt;/td&gt;&lt;td&gt;Explicit deadlock error (PostgreSQL: &lt;code&gt;ERROR: deadlock detected&lt;/code&gt;; MySQL: &lt;code&gt;ERROR 1213: Deadlock found&lt;/code&gt;)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Correct response&lt;/td&gt;&lt;td&gt;Find and address the long-running transaction&lt;/td&gt;&lt;td&gt;Handle the error in the application; fix lock ordering&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Where to look&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_activity&lt;/code&gt; (PostgreSQL); &lt;code&gt;SHOW ENGINE INNODB STATUS&lt;/code&gt; (MySQL)&lt;/td&gt;&lt;td&gt;PostgreSQL server log; MySQL &lt;code&gt;SHOW ENGINE INNODB STATUS&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;PostgreSQL detection:&lt;/strong&gt; &lt;code&gt;pg_stat_activity&lt;/code&gt; surfaces every session currently blocked on a lock via &lt;code&gt;SELECT pid, state, wait_event_type, wait_event, query FROM pg_stat_activity WHERE wait_event_type = &apos;Lock&apos;;&lt;/code&gt;. Deadlocks are logged at &lt;code&gt;ERROR&lt;/code&gt; level in the server log.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;MySQL InnoDB detection:&lt;/strong&gt; &lt;code&gt;SHOW ENGINE INNODB STATUS\G&lt;/code&gt; includes a &lt;code&gt;LATEST DETECTED DEADLOCK&lt;/code&gt; section showing the two transactions, the locks held and waited for, and which was rolled back as the victim. For blocking, &lt;code&gt;information_schema.INNODB_LOCK_WAITS&lt;/code&gt; shows live lock waits.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lock timeout vs deadlock detection&lt;/strong&gt; are separate mechanisms. &lt;code&gt;lock_timeout&lt;/code&gt; (PostgreSQL) and &lt;code&gt;innodb_lock_wait_timeout&lt;/code&gt; (MySQL) abort a waiting transaction after a configured interval — that is a timeout, not a deadlock. Deadlock detection runs independently on the server side regardless of timeout settings. A blocking event terminated by a timeout was never a deadlock; the application log error codes differ accordingly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Row-level vs table-level locking:&lt;/strong&gt; missing indexes force broader locks. A &lt;code&gt;DELETE WHERE status = &apos;pending&apos;&lt;/code&gt; without an index on &lt;code&gt;status&lt;/code&gt; may escalate to a table lock in InnoDB rather than acquiring row locks for only matching rows — turning a narrow delete into a blocking event for every other writer on that table.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s lock management documentation describes the wait-for graph approach: “PostgreSQL automatically detects deadlock situations and resolves them by aborting one of the transactions involved, allowing the other(s) to complete.” It explicitly recommends consistent lock ordering as the prevention strategy (&lt;a href=&quot;https://www.postgresql.org/docs/current/explicit-locking.html&quot;&gt;https://www.postgresql.org/docs/current/explicit-locking.html&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;MySQL’s InnoDB deadlock documentation draws a sharp distinction from lock wait timeouts: a lock wait timeout rolls back only the current SQL statement, whereas a deadlock detection event rolls back the entire transaction (&lt;a href=&quot;https://dev.mysql.com/doc/refman/8.0/en/innodb-deadlocks.html&quot;&gt;https://dev.mysql.com/doc/refman/8.0/en/innodb-deadlocks.html&lt;/a&gt;). That distinction matters for application retry logic — a partial statement rollback and a full transaction rollback require different recovery paths.&lt;/p&gt;
&lt;p&gt;The documented pattern from both systems: deadlock handling belongs in the application layer with a full-transaction retry. Blocking calls for operational investigation — find the long-running holder and address it at source.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;ORM batch inserts without consistent row ordering&lt;/td&gt;&lt;td&gt;Deadlocks under concurrent batch operations&lt;/td&gt;&lt;td&gt;Two batches inserting the same rows in different orders create lock cycle; ORM doesn’t guarantee insertion order&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Missing index on a filtered column used in writes&lt;/td&gt;&lt;td&gt;Blocking affects all writers to the table, not just contended rows&lt;/td&gt;&lt;td&gt;No row-level lock available, so InnoDB or PostgreSQL acquires a broader lock than necessary&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Connection pool holding open transactions&lt;/td&gt;&lt;td&gt;Long-running blocking events that appear intermittent&lt;/td&gt;&lt;td&gt;Idle connections holding uncommitted transactions keep locks live; the blocking appears random because it follows the pool’s transaction lifecycle, not the application’s&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Engineers apply the wrong fix because blocking and deadlocks produce similar symptoms but have structurally different causes and resolution paths.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Identify which condition you have first — use &lt;code&gt;pg_stat_activity&lt;/code&gt; or &lt;code&gt;SHOW ENGINE INNODB STATUS&lt;/code&gt; to determine whether a lock cycle or a long-running holder is the root cause — then respond accordingly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: If &lt;code&gt;pg_stat_activity&lt;/code&gt; shows one session in &lt;code&gt;Lock&lt;/code&gt; wait state with a single blocking pid, you have blocking. If the PostgreSQL log shows &lt;code&gt;ERROR: deadlock detected&lt;/code&gt; or MySQL reports a deadlock in &lt;code&gt;SHOW ENGINE INNODB STATUS&lt;/code&gt;, you have a deadlock.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, add &lt;code&gt;lock_timeout = &apos;5s&apos;&lt;/code&gt; (PostgreSQL) or lower &lt;code&gt;innodb_lock_wait_timeout&lt;/code&gt; (MySQL) to surface blocking events that would otherwise wait silently, and confirm your application explicitly handles the &lt;code&gt;40P01&lt;/code&gt; error code (PostgreSQL deadlock) with a retry path.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>failures</category></item><item><title>OCI E-Commerce Database Architecture: Autonomous Transaction Processing, GoldenGate, and Object Storage</title><link>https://rajivonai.com/blog/2023-07-20-oci-e-commerce-database-architecture-autonomous-transaction-processing-goldengate-and-object-storage/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-07-20-oci-e-commerce-database-architecture-autonomous-transaction-processing-goldengate-and-object-storage/</guid><description>Isolating the OCI Autonomous Transaction Processing write path from catalog and analytics load using GoldenGate replication and Object Storage offloading.</description><pubDate>Thu, 20 Jul 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Checkout does not fail because a database is slow; it fails because every downstream concern was allowed to compete with the order write path.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;E-commerce platforms have stopped being single applications wrapped around a single relational database. A real storefront now has inventory reservations, payment authorization, fraud checks, catalog search, marketing attribution, shipment events, customer service workflows, personalization, analytics, and regulatory retention requirements.&lt;/p&gt;
&lt;p&gt;The database architecture has to absorb that complexity without making the buyer wait for it.&lt;/p&gt;
&lt;p&gt;OCI gives teams a useful set of primitives for this shape of system: Autonomous Transaction Processing for the transactional core, Oracle GoldenGate for change data capture and replication, and Object Storage for durable event and analytical landing zones. The trap is treating those services as a reference diagram instead of an operational boundary.&lt;/p&gt;
&lt;p&gt;Autonomous Transaction Processing can reduce database administration burden through managed scaling, patching, backups, and Oracle Database compatibility. GoldenGate can capture committed changes from transaction logs and deliver them into other systems with low latency. Object Storage can hold large volumes of semi-structured and immutable data at a different cost and durability profile than the order database.&lt;/p&gt;
&lt;p&gt;None of those facts automatically produce a resilient architecture. They only give you sharper tools.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The common failure is coupling. The order service writes an order, updates inventory, emits an event, refreshes search, stores an audit record, writes an analytics row, and calls a marketing integration. At low traffic, the design looks straightforward. During a product drop or holiday campaign, it becomes a distributed lock disguised as a checkout flow.&lt;/p&gt;
&lt;p&gt;Three failure modes show up first.&lt;/p&gt;
&lt;p&gt;The first is write amplification on the transactional database. Tables that should protect order correctness become a shared integration surface. Reporting queries, exports, support dashboards, and partner feeds all read from the same database serving checkout.&lt;/p&gt;
&lt;p&gt;The second is dual-write inconsistency. If the application writes to ATP and then separately publishes to a stream or object store, failures between those operations create missing events, duplicate events, or conflicting recovery procedures.&lt;/p&gt;
&lt;p&gt;The third is recovery ambiguity. When a downstream index, warehouse table, or fraud feature store is wrong, the team cannot answer a simple question: what is the source of truth, and can we replay it?&lt;/p&gt;
&lt;p&gt;The core question is not “How do we connect OCI services?” It is: how do we preserve checkout correctness while still feeding every derived system fast enough to be useful?&lt;/p&gt;
&lt;h2 id=&quot;the-answer--transactional-core-change-stream-durable-landing-zone&quot;&gt;The Answer — Transactional Core, Change Stream, Durable Landing Zone&lt;/h2&gt;
&lt;p&gt;The architecture should make ATP the system of record for orders, payments, inventory reservations, and customer commitments. GoldenGate should read committed changes from that source of truth and deliver them to consumers. Object Storage should hold immutable, replayable change files, exports, receipts, and analytical snapshots.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[web and mobile storefront — buyer requests] --&gt; B[checkout service — order command]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[ATP transactional core — orders inventory payments]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D[commit log — durable database truth]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; E[GoldenGate capture — committed changes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; F[GoldenGate delivery — fanout control]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; G[search index — product and order lookup]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; H[fraud features — near real time signals]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; I[Object Storage landing zone — immutable change files]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; J[data lake queries — analytics and audit]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; K[replay jobs — rebuild derived state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; L[operational read models — support workflows]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The critical design decision is that checkout completion depends only on the transactional commit and the minimum synchronous checks required to safely accept the order. Everything else becomes derived state.&lt;/p&gt;
&lt;p&gt;ATP owns invariants: an order has one authoritative lifecycle, inventory reservations cannot go negative according to the business rule, payment authorization state is recorded transactionally, and idempotency keys prevent duplicate checkout attempts from creating duplicate commitments.&lt;/p&gt;
&lt;p&gt;GoldenGate owns movement: once the transaction commits, changes are captured from the database log rather than reconstructed by application code. That reduces dual-write pressure because the application does not need to write the order and separately remember to publish the exact same fact.&lt;/p&gt;
&lt;p&gt;Object Storage owns replay: every delivered change batch should be stored with partitioning by domain, table or event type, and commit time. The format matters less than the contract. The files must be immutable, discoverable, schema-versioned, and tied back to source transaction metadata.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;h3 id=&quot;context&quot;&gt;Context&lt;/h3&gt;
&lt;p&gt;Oracle documents GoldenGate as a log-based change data capture and replication system for transactional data movement. That pattern matters because the database commit remains the authoritative event boundary, not an application callback that may or may not run after the commit. Oracle also documents OCI Object Storage as a scalable and durable object service, which makes it a better home for long-lived exports and replay files than the OLTP database.&lt;/p&gt;
&lt;p&gt;The documented pattern is not “put everything in a lake.” It is separating operational truth from derived consumption.&lt;/p&gt;
&lt;h3 id=&quot;action&quot;&gt;Action&lt;/h3&gt;
&lt;p&gt;Design the checkout write model first. Use ATP tables for the smallest set of records required to answer: did the customer place an order, what inventory was reserved, what payment state was recorded, and what must happen next?&lt;/p&gt;
&lt;p&gt;Then design CDC contracts around committed facts. A GoldenGate trail or delivery pipeline should publish order-created, payment-state-changed, inventory-reservation-updated, and shipment-state-changed records as derived representations of committed rows. Consumers should treat those records as at-least-once inputs and use source transaction identifiers for idempotency.&lt;/p&gt;
&lt;p&gt;Finally, persist a copy of the change stream into Object Storage before or alongside delivery to analytical consumers. Partition by event date and domain. Store schemas beside the data. Keep enough metadata to replay a consumer from a known commit point.&lt;/p&gt;
&lt;h3 id=&quot;result&quot;&gt;Result&lt;/h3&gt;
&lt;p&gt;The order database stops being the place every consumer goes to ask every question. Search can lag without blocking checkout. Analytics can scan Object Storage without adding read pressure to ATP. Fraud systems can consume near real-time changes while still being rebuilt from historical files if their feature logic changes.&lt;/p&gt;
&lt;p&gt;This architecture also improves incident response. If a downstream consumer corrupts its own projection, recovery is no longer a manual SQL export from production. The team can truncate the projection, select a commit window, and replay from Object Storage or from the GoldenGate-managed delivery path.&lt;/p&gt;
&lt;h3 id=&quot;learning&quot;&gt;Learning&lt;/h3&gt;
&lt;p&gt;The learning is that managed services do not remove ownership boundaries. ATP reduces operational database toil, but it does not decide which writes are part of the buyer commitment. GoldenGate moves changes efficiently, but it does not make non-idempotent consumers safe. Object Storage gives durable capacity, but it does not create a replay contract unless the team stores ordered, versioned, traceable data.&lt;/p&gt;
&lt;p&gt;The architecture works when every component has a narrow job.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;CDC lag during traffic spikes&lt;/td&gt;&lt;td&gt;Downstream delivery cannot keep pace with committed transactions&lt;/td&gt;&lt;td&gt;Monitor commit-to-delivery latency, scale delivery workers, and define consumer freshness SLOs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schema drift breaks consumers&lt;/td&gt;&lt;td&gt;Source tables evolve faster than derived contracts&lt;/td&gt;&lt;td&gt;Version change records and require compatibility checks before deployment&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Object Storage becomes a dumping ground&lt;/td&gt;&lt;td&gt;Teams write files without ownership, partitioning, or retention rules&lt;/td&gt;&lt;td&gt;Define bucket layout, lifecycle policy, schema location, and replay ownership&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Checkout still depends on derived systems&lt;/td&gt;&lt;td&gt;Fraud, search, analytics, or notifications remain synchronous&lt;/td&gt;&lt;td&gt;Classify dependencies as required-before-commit or after-commit&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Duplicate downstream effects&lt;/td&gt;&lt;td&gt;CDC delivery is retried and consumers are not idempotent&lt;/td&gt;&lt;td&gt;Use source transaction IDs, operation timestamps, and consumer-side dedupe tables&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reporting queries hit ATP anyway&lt;/td&gt;&lt;td&gt;Teams bypass the pipeline for convenience&lt;/td&gt;&lt;td&gt;Provide curated read models and make production database access exceptional&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt; — Inventory, orders, payments, analytics, and search fail together when the transactional database is treated as both system of record and integration bus.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt; — Keep ATP as the authoritative OLTP core, use GoldenGate to move committed changes, and land replayable records in Object Storage for analytics, audit, and rebuilds.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Proof&lt;/strong&gt; — The documented OCI pattern aligns with known database architecture principles: commit once, capture from the log, isolate derived consumers, and preserve replayable history.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Action&lt;/strong&gt; — Start by drawing the checkout commit boundary. Then list every consumer that reads order data today, move each one behind CDC or a read model, and require every downstream system to prove idempotency and replay before it is allowed near peak traffic.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Logical Replication Failure Workflow</title><link>https://rajivonai.com/blog/2023-07-17-logical-replication-failure-workflow/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-07-17-logical-replication-failure-workflow/</guid><description>A diagnostic runbook for logical replication lag, apply worker failures, replication conflicts, and schema drift between publisher and subscriber.</description><pubDate>Mon, 17 Jul 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Logical replication lag does not announce itself with an error message — it accumulates silently in the WAL retention on the publisher, and the subscriber falls further and further behind until either the replication slot fills the disk or you notice the data is hours stale.&lt;/strong&gt; Unlike streaming replication, which breaks loudly, logical replication degrades quietly: the subscription stays connected, the apply worker reports running, and the divergence grows until something downstream catches it.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL logical replication works by decoding WAL changes on the publisher into a row-level change stream, which the subscriber applies table by table. This is fundamentally different from physical replication, which ships binary WAL blocks. Logical replication lets you replicate subsets of tables, replicate across major versions, and fan out to multiple subscribers — but it introduces failure modes that streaming replication does not have.&lt;/p&gt;
&lt;p&gt;The most common operational problems: a subscription falls behind because the apply worker hit a conflict (an update arriving for a row that does not exist on the subscriber); the subscription is technically active but the apply worker is stalled waiting for a lock; the publisher and subscriber diverge on schema, causing the apply worker to crash with a type mismatch; or the replication slot on the publisher accumulates enough unreleased WAL to fill the disk.&lt;/p&gt;
&lt;p&gt;The diagnostic workflow must cover all four of these. They share symptoms but have different root causes and different remediations.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Signal&lt;/th&gt;&lt;th&gt;Where to see it&lt;/th&gt;&lt;th&gt;What it means&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Increasing lag between publisher and subscriber&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_replication_slots.confirmed_flush_lsn&lt;/code&gt; vs &lt;code&gt;pg_current_wal_lsn()&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Apply worker not keeping up — lag in bytes growing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replication slot holding excessive WAL&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_replication_slots&lt;/code&gt; — slot not advancing&lt;/td&gt;&lt;td&gt;Subscriber disconnected or stalled; disk risk if slot persists&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Apply worker process absent from &lt;code&gt;pg_stat_subscription&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_activity&lt;/code&gt;, &lt;code&gt;pg_stat_subscription&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Apply worker crashed — check PostgreSQL error log&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Subscription state &lt;code&gt;e&lt;/code&gt; (error) in &lt;code&gt;pg_subscription_rel&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_subscription_rel.srsubstate&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Specific table failed to apply — conflict or schema mismatch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Error message in logs — “conflict in logical replication”&lt;/td&gt;&lt;td&gt;&lt;code&gt;postgresql.log&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Row-level conflict on insert, update, or delete&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schema-related error in logs — “column X of relation Y does not exist”&lt;/td&gt;&lt;td&gt;&lt;code&gt;postgresql.log&lt;/code&gt;&lt;/td&gt;&lt;td&gt;DDL executed on publisher without matching DDL on subscriber&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Replication lag in bytes&lt;/strong&gt; — the most immediate measure of how far behind the subscriber is:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Run on the publisher&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  slot_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  plugin,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  active,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  confirmed_flush_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pg_current_wal_lsn() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; confirmed_flush_lsn &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; lag_bytes,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pg_size_pretty(pg_current_wal_lsn() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; confirmed_flush_lsn) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; lag_human&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_replication_slots&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; slot_type &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;logical&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A growing &lt;code&gt;lag_bytes&lt;/code&gt; means the subscriber is not applying changes as fast as they are being generated. A slot that is not &lt;code&gt;active&lt;/code&gt; (no connected subscriber) is holding WAL indefinitely — disk risk. A slot that is active but &lt;code&gt;lag_bytes&lt;/code&gt; is growing means the apply worker is falling behind.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;Subscription status&lt;/strong&gt; — verify the subscription is enabled and the apply worker is running:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Run on the subscriber&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  subname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  subenabled,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  subpublications,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  subconninfo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_subscription;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;subenabled = false&lt;/code&gt; means the subscription was manually disabled. It will not apply changes until re-enabled. This is the most common cause of lag that looks like a network issue but is actually an administrative action that was forgotten.&lt;/p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;Per-table replication state&lt;/strong&gt; — identify which tables are in which state:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Run on the subscriber&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  srrelid::regclass &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tablename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  srsubstate,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  srsublsn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_subscription_rel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; srsubstate;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;State codes: &lt;code&gt;i&lt;/code&gt; = initialize, &lt;code&gt;d&lt;/code&gt; = data copy in progress, &lt;code&gt;s&lt;/code&gt; = synchronized, &lt;code&gt;r&lt;/code&gt; = ready, &lt;code&gt;e&lt;/code&gt; = error. A table in state &lt;code&gt;e&lt;/code&gt; has failed to apply changes — check the error log for the specific conflict or error. A table stuck in state &lt;code&gt;d&lt;/code&gt; for an extended period means the initial data copy is running slowly or stalled.&lt;/p&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;&lt;strong&gt;Apply worker activity&lt;/strong&gt; — check what the apply worker is currently doing:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Run on the subscriber&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  application_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  client_addr,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  sent_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  write_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  flush_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  replay_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; backend_start &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; worker_age&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_replication;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Also check the subscription worker directly&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  subname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  received_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_msg_send_time,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_msg_receipt_time,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  latest_end_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  latest_end_time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_subscription;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A &lt;code&gt;pid&lt;/code&gt; that is NULL in &lt;code&gt;pg_stat_subscription&lt;/code&gt; means no worker is running for that subscription. Check the PostgreSQL log for the crash reason.&lt;/p&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;&lt;strong&gt;Error log review&lt;/strong&gt; — the log contains the exact conflict type and LSN:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Find conflict-related errors in the PostgreSQL log&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -E&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;ERROR|conflict|replication&apos;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/log/postgresql/postgresql.log&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; tail&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -50&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# More targeted&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;grep&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;logical replication&apos;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/log/postgresql/postgresql.log&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; tail&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -20&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The log will contain lines like &lt;code&gt;ERROR: duplicate key value violates unique constraint&lt;/code&gt; or &lt;code&gt;ERROR: could not find row for updating&lt;/code&gt; — these identify the conflict type. The log also shows the LSN at which the conflict occurred, which is needed for the &lt;code&gt;SKIP&lt;/code&gt; remediation in Option 1 below.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Logical replication lag growing] --&gt; B{Subscription enabled?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| C[ALTER SUBSCRIPTION sub ENABLE]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| D{Apply worker running?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no — pid null| E[Check pg_subscription_rel for error state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F{Table in error state?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|yes| G{Conflict type?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|insert conflict| H[ALTER SUBSCRIPTION sub SKIP lsn]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|update or delete missing row| I[ALTER SUBSCRIPTION sub SKIP lsn]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|schema mismatch| J[Apply DDL to subscriber — re-enable]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes — worker running| K{Lag growing despite active worker?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|yes| L{Publisher write rate too high?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|yes| M[Tune max_logical_replication_workers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|no| N{Lock wait on subscriber?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    N --&gt;|yes| O[Identify blocking query on subscriber]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    N --&gt;|no| P[Check network throughput publisher to subscriber]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|no — stuck in data copy| Q[Check disk and I/O on subscriber]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Option 1 — Skip a conflicting transaction&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When the apply worker fails due to a row conflict — an update or delete targeting a row that does not exist on the subscriber, or an insert violating a unique constraint — the correct resolution is to identify the LSN of the conflicting transaction and skip it:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- On the subscriber, find the last received LSN from pg_stat_subscription&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; received_lsn &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_subscription &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; subname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;my_subscription&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Skip the conflicting transaction (PostgreSQL 15+)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SUBSCRIPTION my_subscription &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SKIP&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (lsn &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;LSN_VALUE&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- For PostgreSQL 14 and earlier, use:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_replication_origin_advance(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;pg_16399&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;LSN_VALUE&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- where 16399 is the subscription OID from pg_subscription&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After skipping, re-enable the subscription if it was auto-disabled:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SUBSCRIPTION my_subscription &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ENABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The skipped transaction is permanently lost on the subscriber. Before skipping, verify the row conflict is expected — for example, the subscriber already has the correct version of that row through another path. If data integrity is critical, investigate why the divergence occurred before skipping blindly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 2 — Resync after schema drift&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When a schema change (DDL) was applied to the publisher without also being applied to the subscriber, the apply worker will crash with a column or type mismatch error. The fix is to apply the matching DDL to the subscriber, then re-enable the subscription:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- On the subscriber: apply the matching DDL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ADD&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; COLUMN shipped_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Re-enable the subscription&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SUBSCRIPTION my_subscription &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ENABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Verify lag starts recovering&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_size_pretty(pg_current_wal_lsn() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; confirmed_flush_lsn)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_replication_slots&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; slot_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;my_subscription&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- check on publisher&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Logical replication does not replicate DDL. Every schema change on the publisher must be manually applied to the subscriber in the correct order before re-enabling the subscription.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 3 — Full resync of a specific table&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When the data divergence is too large to resolve by skipping individual transactions, resync the affected table:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- On the subscriber: refresh the subscription for a specific table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SUBSCRIPTION my_subscription REFRESH PUBLICATION &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FOR&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ALL TABLES;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Or drop and recreate with initial data copy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SUBSCRIPTION my_subscription &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DISABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DROP&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SUBSCRIPTION my_subscription;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SUBSCRIPTION my_subscription&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  CONNECTION&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;host=publisher port=5432 dbname=mydb&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  PUBLICATION my_publication&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (copy_data &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; true, create_slot &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; true);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A full resync will re-copy all data for subscribed tables. On large tables this can take hours. During resync, the subscriber is in an inconsistent state. If downstream applications read from the subscriber during resync, they should be aware the data is being rebuilt.&lt;/p&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;ALTER SUBSCRIPTION sub ENABLE&lt;/code&gt; and &lt;code&gt;DISABLE&lt;/code&gt; are immediately reversible — toggle between them as needed. No data is lost.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ALTER SUBSCRIPTION sub SKIP (lsn)&lt;/code&gt; is irreversible — the skipped transaction is permanently lost on the subscriber. There is no undo. The only recovery if the skipped data was needed is a full table resync.&lt;/li&gt;
&lt;li&gt;DDL applied to the subscriber for schema drift: cannot be automatically undone — but the DDL itself can be reversed (e.g., &lt;code&gt;ALTER TABLE DROP COLUMN&lt;/code&gt;) if the column is not yet populated. Coordinate DDL rollback with the publisher-side change.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;DROP SUBSCRIPTION&lt;/code&gt; followed by &lt;code&gt;CREATE SUBSCRIPTION&lt;/code&gt;: dropping a subscription removes the replication slot on the publisher. The slot must be recreated (it happens automatically with &lt;code&gt;create_slot = true&lt;/code&gt;). Once dropped, WAL that was retained for the old slot is released.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;Replication lag monitoring should be a first-class alert, not a periodic check. The key metric is the byte lag at the replication slot:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Scheduled query to capture slot lag for alerting&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; cron&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;schedule&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;replication-lag-monitor&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;*/5 * * * *&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $$&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ops&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;replication_lag&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (slot_name, lag_bytes, active, captured_at)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    slot_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    pg_current_wal_lsn() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; confirmed_flush_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    active,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_replication_slots&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; slot_type &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;logical&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$$);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Alert thresholds: lag exceeding 1 GB warrants a warning; lag exceeding 10 GB is an incident — the publisher is retaining that much WAL, and disk exhaustion is a real risk. A slot that becomes &lt;code&gt;active = false&lt;/code&gt; for more than 5 minutes outside a maintenance window should page immediately.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The PostgreSQL logical replication documentation describes conflict handling behavior: when an apply worker encounters a conflict (e.g., a unique constraint violation), it pauses the apply process and waits for manual intervention. The documented resolution is either to skip the conflicting transaction using &lt;code&gt;ALTER SUBSCRIPTION ... SKIP&lt;/code&gt; (PostgreSQL 15+) or to use &lt;code&gt;pg_replication_origin_advance&lt;/code&gt; on earlier versions. The documentation explicitly states that skipping is a destructive operation — the skipped changes are permanently absent from the subscriber.&lt;/p&gt;
&lt;p&gt;The documented constraint on logical replication and DDL is unambiguous: DDL changes are not replicated. The PostgreSQL replication documentation requires that schema changes be applied to all subscribers before or simultaneously with the publisher, depending on whether the change is backward-compatible. Adding a nullable column with a default is backward-compatible and can be applied to the subscriber after the publisher; removing a column is not backward-compatible and must be applied to both simultaneously.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Replication slot fills disk on publisher&lt;/td&gt;&lt;td&gt;Subscriber disconnected for hours while high-write workload runs&lt;/td&gt;&lt;td&gt;Monitor slot lag; set &lt;code&gt;max_slot_wal_keep_size&lt;/code&gt; to cap WAL retention&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Apply worker stuck waiting for lock&lt;/td&gt;&lt;td&gt;Long-running query on subscriber table being replicated&lt;/td&gt;&lt;td&gt;Identify and terminate the blocking query on subscriber&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;SKIP&lt;/code&gt; causes downstream data inconsistency&lt;/td&gt;&lt;td&gt;Skipped row was a critical update needed for referential integrity&lt;/td&gt;&lt;td&gt;Resync the table after skip; audit downstream data for orphaned rows&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schema divergence not caught until conflict&lt;/td&gt;&lt;td&gt;Publisher DDL run without notifying the subscriber&lt;/td&gt;&lt;td&gt;Add subscriber DDL to publisher migration scripts; use migration locking tools&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;max_wal_senders&lt;/code&gt; exceeded&lt;/td&gt;&lt;td&gt;Too many replication connections — logical and physical combined&lt;/td&gt;&lt;td&gt;Increase &lt;code&gt;max_wal_senders&lt;/code&gt; in &lt;code&gt;postgresql.conf&lt;/code&gt;; requires restart&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Logical replication lag accumulates silently, WAL retention grows on the publisher, and by the time the disk alert fires, the subscriber is hours behind with no fast path to catch up.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Add active monitoring on replication slot lag bytes with an alert threshold at 1 GB, set &lt;code&gt;max_slot_wal_keep_size&lt;/code&gt; as a disk safety cap, and treat any &lt;code&gt;pg_subscription_rel&lt;/code&gt; table in &lt;code&gt;e&lt;/code&gt; state as an incident requiring same-day resolution.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: After resolving a conflict and re-enabling the subscription, &lt;code&gt;pg_size_pretty(pg_current_wal_lsn() - confirmed_flush_lsn)&lt;/code&gt; from the publisher should decrease steadily — the subscriber is catching up.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Run Check 1 on the publisher this week. If any replication slot shows &lt;code&gt;lag_bytes &gt; 1 GB&lt;/code&gt; or &lt;code&gt;active = false&lt;/code&gt;, treat it as an open incident. If lag is normal, add a monitoring alert so you know before it becomes critical.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;checklist&quot;&gt;Checklist&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Query &lt;code&gt;pg_replication_slots&lt;/code&gt; on publisher — check &lt;code&gt;active&lt;/code&gt; status and &lt;code&gt;lag_bytes&lt;/code&gt; for each logical slot&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;pg_subscription&lt;/code&gt; on subscriber — verify &lt;code&gt;subenabled = true&lt;/code&gt; for each subscription&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;pg_subscription_rel&lt;/code&gt; on subscriber — check &lt;code&gt;srsubstate&lt;/code&gt; for any tables in &lt;code&gt;e&lt;/code&gt; (error) state&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;pg_stat_subscription&lt;/code&gt; on subscriber — confirm &lt;code&gt;pid&lt;/code&gt; is not NULL for each subscription&lt;/li&gt;
&lt;li&gt;Review PostgreSQL log on subscriber for conflict type and LSN&lt;/li&gt;
&lt;li&gt;If table in error state with row conflict: use &lt;code&gt;ALTER SUBSCRIPTION sub SKIP (lsn)&lt;/code&gt; to unblock&lt;/li&gt;
&lt;li&gt;If schema mismatch: apply matching DDL to subscriber, then re-enable subscription&lt;/li&gt;
&lt;li&gt;If apply worker stalled on lock: identify and resolve the blocking query on subscriber&lt;/li&gt;
&lt;li&gt;After resolution, monitor &lt;code&gt;lag_bytes&lt;/code&gt; decreasing — confirm subscriber is catching up&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;max_slot_wal_keep_size&lt;/code&gt; on publisher to cap disk usage from stalled slots&lt;/li&gt;
&lt;li&gt;Add monitoring alert at lag &gt; 1 GB per logical replication slot&lt;/li&gt;
&lt;li&gt;Document schema change protocol — every publisher DDL must have a matching subscriber DDL step&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>checklist</category><category>failures</category></item><item><title>Database Connection Pooling: Why Apps Kill Databases</title><link>https://rajivonai.com/blog/2023-07-10-database-connection-pooling-why-apps-kill-databases/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-07-10-database-connection-pooling-why-apps-kill-databases/</guid><description>Without a connection pool, traffic spikes exhaust OS-level resources before a single slow query runs — here is what actually happens and how to fix it.</description><pubDate>Mon, 10 Jul 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Most applications exhaust their database long before the database is under load.&lt;/strong&gt; The failure is not query pressure — it is connection pressure. Every new connection to PostgreSQL forks a backend process. Every new connection to MySQL spawns a thread. Without a pool capping that number, a traffic spike generates hundreds of OS-level resources in seconds, and the database runs out of capacity to accept connections before it runs out of capacity to execute queries.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Backend engineers know connection pools exist. Most frameworks configure one by default — SQLAlchemy, HikariCP, ActiveRecord, and similar libraries all ship with pool settings. The problem is that those library-level pools live inside a single application process. Scale to five app pods and you have five independent pools, each with their own ten connections: fifty total connections to the database. Scale to fifty pods and you have five hundred. Add a deployment rollout that starts new pods before draining old ones and the math gets worse fast.&lt;/p&gt;
&lt;p&gt;This matters because databases have hard limits. PostgreSQL’s &lt;code&gt;max_connections&lt;/code&gt; defaults to 100. MySQL’s defaults to 151. Those limits are not arbitrary — they map to real resource consumption per connection.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s connection model, documented in the &lt;a href=&quot;https://www.postgresql.org/docs/current/connect-estab.html&quot;&gt;PostgreSQL Server Programming documentation&lt;/a&gt;, forks a new backend process for each client connection. Each backend process carries its own memory space — typically 5–10 MB per connection depending on work_mem settings and query state. One hundred connections means one hundred processes. At five hundred connections you are consuming several gigabytes of RAM just in process overhead before a single row is read.&lt;/p&gt;
&lt;p&gt;MySQL uses a thread-per-connection model rather than processes, which reduces per-connection overhead, but the problem is structurally identical: threads consume stack space, file descriptors, and scheduler overhead. At high connection counts both systems degrade.&lt;/p&gt;
&lt;p&gt;The acute failure mode is a connection storm: an app deployment or autoscale event brings up many new pods simultaneously, each opening their full pool. The database hits &lt;code&gt;max_connections&lt;/code&gt;, new connection attempts queue or return errors, and the application starts logging “too many connections” at the moment it most needs to be available — during a traffic spike or recovery event. The database itself is not overloaded. It simply cannot accept new clients.&lt;/p&gt;
&lt;p&gt;What is the right way to decouple application instance count from database connection count?&lt;/p&gt;
&lt;h2 id=&quot;how-connection-poolers-work&quot;&gt;How Connection Poolers Work&lt;/h2&gt;
&lt;p&gt;A connection pooler sits between application processes and the database. Applications connect to the pooler, which maintains a fixed, smaller set of long-lived connections to the actual database. The application sees a normal database endpoint; the database sees a bounded number of backend processes regardless of how many application pods are running.&lt;/p&gt;
&lt;p&gt;The two dominant tools are PgBouncer for PostgreSQL and ProxySQL for MySQL.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;PgBouncer&lt;/strong&gt; operates in three modes, documented in the &lt;a href=&quot;https://www.pgbouncer.org/config.html&quot;&gt;PgBouncer documentation&lt;/a&gt;:&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Mode&lt;/th&gt;&lt;th&gt;How it works&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Session mode&lt;/td&gt;&lt;td&gt;One server connection per client session; held for the life of the client connection&lt;/td&gt;&lt;td&gt;Minimal breakage; connection count reduction only happens if clients disconnect promptly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Transaction mode&lt;/td&gt;&lt;td&gt;Server connection returned to pool after each transaction completes&lt;/td&gt;&lt;td&gt;LISTEN/NOTIFY, advisory locks, prepared statements, and SET LOCAL state do not survive across transactions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Statement mode&lt;/td&gt;&lt;td&gt;Server connection returned after each statement&lt;/td&gt;&lt;td&gt;Breaks transactions; use only for simple read-only workloads&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Transaction mode delivers the most aggressive multiplexing — a pooler with 20 server-side connections can service hundreds of application clients that are between transactions — but it breaks any feature that assumes state persists across transactions. PostgreSQL’s &lt;code&gt;LISTEN/NOTIFY&lt;/code&gt; mechanism relies on a persistent server connection; in transaction mode the pooler may reassign that connection to another client between events. Advisory locks held at session scope are lost the moment the transaction commits. Applications using &lt;code&gt;SET LOCAL&lt;/code&gt; to configure session parameters will find those settings gone after each transaction boundary.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ProxySQL&lt;/strong&gt; applies the same multiplexing principle to MySQL, with additional query routing capabilities (read-write splitting, rule-based routing) that make it common in MySQL environments with replicas. Its connection pool size is configured independently of the application-side connection settings.&lt;/p&gt;
&lt;p&gt;The practical deployment pattern is to configure application connection pools small (3–5 connections per pod) so the pooler remains the single point of configuration, and set the pooler’s server-side pool to a number the database can sustain — typically 20–50% of &lt;code&gt;max_connections&lt;/code&gt;, leaving headroom for administrative connections and monitoring.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The PostgreSQL project documents the process-per-connection model explicitly, and the &lt;a href=&quot;https://www.pgbouncer.org/faq.html&quot;&gt;PgBouncer FAQ&lt;/a&gt; describes the transaction mode tradeoffs in detail, noting that applications must be verified compatible before enabling it.&lt;/p&gt;
&lt;p&gt;The Heroku Postgres team published guidance on PgBouncer in transaction mode specifically because Heroku’s platform runs many small dynos each with their own application process — exactly the multi-pod scaling problem described above. Their tooling, &lt;a href=&quot;https://github.com/heroku/heroku-buildpack-pgbouncer&quot;&gt;pgbouncer-heroku&lt;/a&gt;, emerged from the documented operational reality that a modest Heroku app on ten dynos could exhaust a standard PostgreSQL &lt;code&gt;max_connections&lt;/code&gt; without any pooler in place.&lt;/p&gt;
&lt;p&gt;The documented pattern from the PgBouncer project itself is: use session mode as a starting point when application compatibility is uncertain, verify that no LISTEN/NOTIFY or advisory lock usage exists, then migrate to transaction mode for maximum multiplexing.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Transaction mode with LISTEN/NOTIFY&lt;/td&gt;&lt;td&gt;Notifications are never received or delivered to the wrong client&lt;/td&gt;&lt;td&gt;The pooler reassigns server connections between events; the persistent channel the listener expects does not exist&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Pool exhaustion under bursts&lt;/td&gt;&lt;td&gt;New client connections are queued or rejected by the pooler itself&lt;/td&gt;&lt;td&gt;The pooler’s server-side pool is also bounded; if all server connections are busy, clients wait or time out&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Health check connections consuming pool slots&lt;/td&gt;&lt;td&gt;Liveness probes open a connection and close it repeatedly, consuming pool capacity&lt;/td&gt;&lt;td&gt;Health checks should connect to the pooler’s stats port or use a single persistent probe connection rather than opening fresh database connections&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Without a standalone pooler, application pod count directly drives database connection count — a deployment event can exhaust &lt;code&gt;max_connections&lt;/code&gt; before the database processes a single query.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Deploy PgBouncer (PostgreSQL) or ProxySQL (MySQL) as a sidecar or dedicated service; configure application pools to 3–5 connections per pod; set the pooler’s server pool to a fraction of &lt;code&gt;max_connections&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: After deploying the pooler, run &lt;code&gt;SELECT count(*) FROM pg_stat_activity&lt;/code&gt; during a load test — the number should stay flat as application replicas scale, rather than increasing proportionally.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, check your current connection count and compare it to your &lt;code&gt;max_connections&lt;/code&gt; setting; if you are above 60% of the limit without a pooler, that is the gap to close first:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Connection count by state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GROUP BY&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Show the configured limit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW max_connections;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content:encoded><category>databases</category><category>failures</category></item><item><title>Exadata Cloud Service: When Hardware Architecture Still Matters</title><link>https://rajivonai.com/blog/2023-07-05-exadata-cloud-service-when-hardware-architecture-still-matters/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-07-05-exadata-cloud-service-when-hardware-architecture-still-matters/</guid><description>Exadata Cloud Service exposes RDMA interconnects and Smart Scan offload tiers that matter when Oracle workload latency cannot be fixed with software alone.</description><pubDate>Wed, 05 Jul 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The cloud did not make hardware irrelevant; it made most teams stop seeing the hardware until a workload fails in a way software abstractions cannot hide.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most cloud database architecture discussions start from an assumption: compute is elastic, storage is remote, and the network is a commodity substrate. That model works well for many transactional systems, event-driven services, and horizontally partitioned applications. It is also the model behind much of the modern managed database market.&lt;/p&gt;
&lt;p&gt;But some database workloads are not dominated by stateless request fan-out. They are dominated by data movement, cache locality, redo latency, scan efficiency, concurrency control, and the cost of moving blocks between storage, memory, and CPUs.&lt;/p&gt;
&lt;p&gt;Oracle Exadata Cloud Service exists for that class of workload. It puts Oracle Database on an engineered system with database servers, storage servers, high-bandwidth low-latency fabric, smart storage software, flash cache, and database-aware offload behavior. The cloud control plane provisions and manages the service, but the performance model still depends on hardware and storage architecture.&lt;/p&gt;
&lt;p&gt;That makes Exadata uncomfortable for engineers who prefer pure abstraction. It is cloud, but it is not hardware-agnostic cloud.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure usually appears during migration. A team moves an Oracle workload from a tuned on-prem estate or engineered appliance into a generic cloud database shape. The application still works. The SQL still parses. The schema still exists. Then batch windows stretch, reporting queries interfere with OLTP traffic, storage latency becomes visible, and scaling compute stops helping.&lt;/p&gt;
&lt;p&gt;The root cause is often not a single bad query. It is a broken assumption about where database work happens.&lt;/p&gt;
&lt;p&gt;In a conventional cloud database deployment, a query that needs a large scan may pull data from remote storage into database compute nodes before filtering, joining, or aggregating. That can be acceptable when the data set is small, the working set is cached, or the access pattern is selective. It becomes expensive when the database repeatedly moves large volumes of blocks across the storage boundary only to discard most of them after predicate evaluation.&lt;/p&gt;
&lt;p&gt;Exadata changes that boundary. Storage servers are not passive disks behind a network. They can participate in database work through mechanisms such as Smart Scan, storage indexes, flash cache, and hybrid columnar compression. The architecture tries to reduce the amount of data that crosses from storage into database compute.&lt;/p&gt;
&lt;p&gt;The question is not whether Exadata is “faster hardware.” The better question is: when does database architecture need hardware and storage to become part of the query execution system?&lt;/p&gt;
&lt;h2 id=&quot;the-answer-database-aware-infrastructure&quot;&gt;The Answer: Database-Aware Infrastructure&lt;/h2&gt;
&lt;p&gt;Exadata Cloud Service is best understood as database-aware infrastructure exposed through a cloud operating model. The important architectural move is not simply that Oracle runs on large machines. It is that the database, storage layer, flash tier, and internal network are designed as one system.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[application workload — OLTP and analytics] --&gt; B[Oracle Database servers — SQL execution]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[high speed fabric — low latency data path]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[Exadata storage servers — database aware storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[Smart Scan — predicate offload]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; F[Flash Cache — hot block acceleration]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; G[Storage Indexes — skip irrelevant regions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; H[reduced data movement — fewer blocks returned]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; I[cloud control plane — provisioning and lifecycle]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This matters because relational database performance is often constrained by coordination and movement rather than raw CPU. A large analytic query does not only need processors. It needs efficient filtering, predictable access to hot data, and a way to avoid shipping unnecessary blocks. A high-throughput OLTP system does not only need more cores. It needs stable latency on redo, buffer access, and interconnect traffic.&lt;/p&gt;
&lt;p&gt;Exadata’s design pushes work closer to the data when it can. Smart Scan can offload eligible query processing to storage cells, returning fewer rows or columns to database servers. Storage indexes can avoid reading regions that cannot match predicates. Flash cache can absorb hot reads without treating flash as merely a generic disk tier. These features do not remove the need for SQL tuning, indexing discipline, or application-level architecture, but they change the operating envelope.&lt;/p&gt;
&lt;p&gt;The cloud service layer then changes who operates the system. Teams consume Exadata through Oracle Cloud infrastructure primitives, automation, patching workflows, and service boundaries. They still need database engineering judgment, but they do not have to build the appliance management plane themselves.&lt;/p&gt;
&lt;p&gt;The architectural pattern is clear: hide operational toil where possible, but do not pretend the physical execution path is irrelevant.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Oracle publicly documents Exadata as an engineered system where database servers, storage servers, networking, and Exadata storage software are designed together. Oracle’s documentation describes Smart Scan as a mechanism that offloads eligible SQL processing to Exadata storage servers, reducing data returned to database servers.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The documented pattern is to place Oracle workloads with heavy scan, consolidation, mixed OLTP and analytics, or demanding latency profiles on infrastructure where storage is database-aware rather than generic. That means treating storage cells as participants in execution, not only as block providers.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The result is not magic performance for every workload. It is a different bottleneck profile. Queries that can benefit from offload, pruning, compression, or flash locality may move less data and consume database server resources differently. Workloads that are CPU-bound in procedural code, poorly modeled, or dominated by application round trips may see less benefit.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The engineering lesson is that managed cloud does not remove the need to understand execution paths. It changes which parts are automated. Exadata Cloud Service automates parts of infrastructure lifecycle, but the workload still succeeds or fails based on data shape, SQL behavior, contention, and whether the hardware-aware features are actually exercised.&lt;/p&gt;
&lt;p&gt;This is not unique to Oracle. Amazon Aurora’s public architecture separates compute from a distributed storage layer and pushes replication and durability behavior into that layer. Google Spanner’s public papers describe a database architecture built around replication, Paxos, and TrueTime. In both cases, the architecture is not “just software on machines.” The database service is shaped by assumptions about storage, networking, clocks, and failure domains.&lt;/p&gt;
&lt;p&gt;The documented pattern is that serious database systems eventually make infrastructure part of the database design. Exadata does it through engineered database hardware and storage offload. Aurora does it through a purpose-built cloud storage service. Spanner does it through globally coordinated replication and time semantics. Different answers, same lesson: the abstraction is only reliable when the underlying architecture matches the workload.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Treating Exadata as generic compute&lt;/td&gt;&lt;td&gt;Teams expect the service to fix poor SQL, bad indexing, or chatty application access&lt;/td&gt;&lt;td&gt;Profile SQL plans, wait events, and offload eligibility before migration&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Assuming all queries offload&lt;/td&gt;&lt;td&gt;Smart Scan applies only to eligible operations and access paths&lt;/td&gt;&lt;td&gt;Validate execution plans and cell offload statistics&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Ignoring operational coupling&lt;/td&gt;&lt;td&gt;Engineered systems improve the data path but introduce platform-specific lifecycle knowledge&lt;/td&gt;&lt;td&gt;Build runbooks for patching, scaling, backup, and incident response&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Over-consolidating workloads&lt;/td&gt;&lt;td&gt;Mixed workloads can still contend for CPU, memory, IO, locks, and maintenance windows&lt;/td&gt;&lt;td&gt;Use workload management, resource plans, and isolation boundaries&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Misreading cloud economics&lt;/td&gt;&lt;td&gt;Higher unit cost may be justified only when consolidation, performance, or licensing economics align&lt;/td&gt;&lt;td&gt;Compare total cost against workload outcomes, not instance pricing alone&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Portability expectations&lt;/td&gt;&lt;td&gt;Exadata-specific behavior can make future migration harder&lt;/td&gt;&lt;td&gt;Keep application contracts clean and document platform-dependent assumptions&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The largest risk is architectural laziness in either direction. One team dismisses Exadata because it is too specialized. Another buys it as a substitute for engineering discipline. Both positions miss the point.&lt;/p&gt;
&lt;p&gt;Specialized infrastructure is justified when it removes a real bottleneck that generic infrastructure cannot remove cleanly. It is not justified when the bottleneck is unknown.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Identify whether the workload is constrained by data movement, storage latency, scan volume, redo pressure, or concurrency hot spots. Do not start with a product decision.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Use Exadata Cloud Service when Oracle Database performance depends on database-aware storage, predictable low-latency infrastructure, consolidation, and operational integration with Oracle tooling.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Before committing, test representative SQL, batch windows, maintenance operations, backup behavior, failover procedures, and offload statistics. A benchmark that only measures a synthetic happy path is not evidence.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Build a migration scorecard with workload classes, top SQL statements, expected offload candidates, non-negotiable latency targets, operational runbooks, and exit assumptions. If the architecture depends on hardware, make that dependency explicit.&lt;/p&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Schema Deployment Risk Checklist</title><link>https://rajivonai.com/blog/2023-06-26-schema-deployment-risk-checklist/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-06-26-schema-deployment-risk-checklist/</guid><description>Assessing lock type, table size, reversibility, and rollback plan before every schema migration — a structured checklist for zero-downtime deployments.</description><pubDate>Mon, 26 Jun 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The most dangerous moment in a schema deployment is not the migration itself — it is the 30 seconds before you run it when you think you understand the lock behavior but haven’t confirmed it.&lt;/strong&gt; &lt;code&gt;ALTER TABLE ADD COLUMN&lt;/code&gt; on a 2 GB table is instantaneous on PostgreSQL 11 and later. The same statement on PostgreSQL 10 can hold an ACCESS EXCLUSIVE lock for minutes. &lt;code&gt;CREATE INDEX&lt;/code&gt; without &lt;code&gt;CONCURRENTLY&lt;/code&gt; will block all writes on the table for the duration of the build. Understanding which statement takes which lock, and what the options are to avoid it, is table stakes for schema work on production databases.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Schema migrations in a running production system have three risk dimensions: lock duration, reversibility, and execution time. These are independent axes. A migration can be fast but irreversible (dropping a column). It can be slow but non-blocking (&lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt;). It can be fast, reversible, and still dangerous because the lock type is wrong for the traffic pattern.&lt;/p&gt;
&lt;p&gt;Most teams have learned about &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt;. Fewer have mapped out the full lock table for &lt;code&gt;ALTER TABLE&lt;/code&gt; variants. The failure pattern is predictable: an engineer runs &lt;code&gt;ALTER TABLE orders ADD COLUMN tax_id VARCHAR(32) NOT NULL DEFAULT &apos;&apos;&lt;/code&gt; on a table with 500 million rows, assumes it is fast because they have done it before on small tables, and discovers it is holding an ACCESS EXCLUSIVE lock while taking 12 minutes to backfill the default.&lt;/p&gt;
&lt;p&gt;This checklist forces the assessment before the migration runs, not after it starts.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;When schema migrations fail, they usually do not corrupt data — they corrupt availability. A migration that holds an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock on a heavily trafficked table causes all incoming queries to queue. Once the connection pool saturates, the application begins dropping requests, triggering an escalating cascade of timeouts.&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Signal&lt;/th&gt;&lt;th&gt;Where to see it&lt;/th&gt;&lt;th&gt;What it means&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Application connection queuing after migration started&lt;/td&gt;&lt;td&gt;APM or &lt;code&gt;pg_stat_activity&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Migration holding ACCESS EXCLUSIVE lock — connections waiting&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Migration running longer than expected&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_activity&lt;/code&gt; with &lt;code&gt;state = &apos;active&apos;&lt;/code&gt; and old &lt;code&gt;xact_start&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Table size or data backfill underestimated on staging&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replication lag spiking during migration&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_replication&lt;/code&gt; — &lt;code&gt;replay_lag&lt;/code&gt; growing&lt;/td&gt;&lt;td&gt;Migration WAL volume causing replication to fall behind&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Migration script fails with lock timeout&lt;/td&gt;&lt;td&gt;Application or migration tool error log&lt;/td&gt;&lt;td&gt;Lock acquisition timed out — another transaction holding the table&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Rollback script unavailable&lt;/td&gt;&lt;td&gt;Migration tool history&lt;/td&gt;&lt;td&gt;Migration was run without a matching down migration&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The traditional approach of “test it on staging” provides a false sense of security. A deployment that runs in two seconds on a 100 MB staging table can stall for twenty minutes on a 500 GB production table. Furthermore, if a migration blocks mid-execution due to lock contention or disk space limits, the lack of an immediate, tested rollback plan forces engineers to invent recovery strategies during an active incident.&lt;/p&gt;
&lt;p&gt;How can a team systematically verify the lock behavior, execution duration, and reversibility of a schema migration before it ever touches production?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;The solution is a structured evaluation that categorizes migrations by lock type, table size, and rollback complexity before execution.&lt;/p&gt;
&lt;h3 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Schema migration planned] --&gt; B{Requires ACCESS EXCLUSIVE lock?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no — CONCURRENTLY or ANALYZE| C[Safe to run anytime — proceed]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| D{Table size greater than 1 GB?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes| E{Online alternative available?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|yes| F[Use online alternative — see options below]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|no| G[Schedule maintenance window]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no — small table| H{Traffic pattern allows short lock?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| I[Run during low-traffic window]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| J[Use online alternative or maintenance window]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; K{NOT NULL without default?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|yes| L[3-step split — nullable then backfill then constraint]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|no| M[ADD COLUMN with DEFAULT on PG11 or later — instant]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What this diagram shows:&lt;/strong&gt; A migration risk decision tree. The first branch identifies whether the operation requires ACCESS EXCLUSIVE lock. If so, table size determines whether an online alternative exists. The final branch handles NOT NULL without a default — which requires the three-step pattern: add as nullable, backfill, then add the constraint.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Does the migration require ACCESS EXCLUSIVE lock?&lt;/strong&gt; — the most important question to answer first:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Check the lock type for common DDL operations:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- ACCESS EXCLUSIVE (blocks reads AND writes):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;--   ALTER TABLE (most variants)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;--   DROP TABLE, TRUNCATE, DROP INDEX&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;--   VACUUM FULL, CLUSTER&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;--&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- SHARE UPDATE EXCLUSIVE (allows reads and writes):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;--   CREATE INDEX CONCURRENTLY&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;--   VACUUM, ANALYZE, CREATE STATISTICS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;--&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- SHARE (allows reads, blocks writes):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;--   CREATE INDEX (without CONCURRENTLY)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- To confirm lock behavior during a migration, check what is waiting:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pid, relation::regclass, mode, granted&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_locks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; granted&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pid;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If your migration uses &lt;code&gt;ALTER TABLE&lt;/code&gt; on a large table, it will take ACCESS EXCLUSIVE. Period. Understand this before starting.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;What is the table size?&lt;/strong&gt; — execution time scales with table size for any migration that rewrites rows:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pg_size_pretty(pg_total_relation_size(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;::regclass)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; total_size,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pg_size_pretty(pg_relation_size(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;::regclass)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; heap_size,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pg_size_pretty(pg_indexes_size(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;::regclass)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; index_size,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  reltuples::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;bigint&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; estimated_rows&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_class&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; relname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For any migration that rewrites the heap (ADD COLUMN with default on PG10, changing column types, ADD CONSTRAINT), the lock duration is proportional to table size. A migration that runs in 3 seconds on a 100 MB staging table will run for 18 minutes on a 36 GB production table.&lt;/p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;Is the migration reversible?&lt;/strong&gt; — classify before running:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Check existing column definitions before adding or dropping&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  column_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  data_type,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  is_nullable,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  column_default&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;columns&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; table_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;orders&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ordinal_position;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Reversibility classification:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ADD COLUMN nullable&lt;/code&gt; — reversible: &lt;code&gt;DROP COLUMN&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ADD COLUMN NOT NULL DEFAULT value&lt;/code&gt; — reversible on PG11 and later: &lt;code&gt;DROP COLUMN&lt;/code&gt; (PG11+ stores the default in catalog, no rewrite)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;DROP COLUMN&lt;/code&gt; — irreversible: data is gone after vacuum runs&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ALTER COLUMN TYPE&lt;/code&gt; — reversible in principle, but requires another full rewrite; plan carefully&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CREATE INDEX&lt;/code&gt; — fully reversible: &lt;code&gt;DROP INDEX&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ADD CONSTRAINT CHECK&lt;/code&gt; — reversible: &lt;code&gt;DROP CONSTRAINT&lt;/code&gt;, but adds a lock; use &lt;code&gt;NOT VALID&lt;/code&gt; + &lt;code&gt;VALIDATE CONSTRAINT&lt;/code&gt; split&lt;/li&gt;
&lt;/ul&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;&lt;strong&gt;Test the migration on a production-sized staging database&lt;/strong&gt; — estimate true execution time:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Time the migration on a copy of production data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;psql&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; staging_prod_copy&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;\timing&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;ALTER TABLE orders ADD COLUMN archived_at timestamptz;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# For longer migrations, use EXPLAIN to see what the operation will do before committing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;BEGIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; orders&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; ADD&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; COLUMN&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; archived_at&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; timestamptz&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;--&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; Check&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pg_locks&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; here&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; to&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; observe&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; lock&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; behavior&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ROLLBACK&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;  &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;--&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; abort&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; to&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; avoid&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; actual&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; change&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Timing on staging with a production-sized dataset is the only reliable estimate. Factor-of-10 size differences between staging and production are common and explain most migration surprises.&lt;/p&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;&lt;strong&gt;Is the migration idempotent?&lt;/strong&gt; — essential for safe retries:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Idempotent column addition&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ADD&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; COLUMN &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; EXISTS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; archived_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Idempotent index creation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; CONCURRENTLY&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IF&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; EXISTS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx_orders_archived_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders (archived_at);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Idempotent constraint addition&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DO $$&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  IF&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; EXISTS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_constraint&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; conname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;chk_orders_status&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; conrelid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;::regclass&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;THEN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ADD&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; CONSTRAINT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; chk_orders_status&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    CHECK&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;pending&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;processing&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;shipped&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;cancelled&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    NOT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; VALID;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  END&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IF&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $$;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A migration that fails midway and cannot be safely retried creates recovery debt. &lt;code&gt;IF NOT EXISTS&lt;/code&gt; guards on &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; and &lt;code&gt;ADD COLUMN&lt;/code&gt; are the standard pattern.&lt;/p&gt;
&lt;h3 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Option 1 — Lock-safe online alternatives&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For the most common migration types, online alternatives avoid ACCESS EXCLUSIVE:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- ADD INDEX: always use CONCURRENTLY on production&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; CONCURRENTLY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx_orders_customer_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders (customer_id);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- ADD COLUMN with default (PostgreSQL 11 and later): instant, no table rewrite&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- PostgreSQL 11 and later stores the default in pg_attrdef, not in the heap&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ADD&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; COLUMN archived_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DEFAULT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- ADD NOT NULL constraint without default: 3-step split&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Step 1: Add column as nullable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ADD&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; COLUMN &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; EXISTS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tax_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Step 2: Backfill in batches (do NOT do this in a single UPDATE)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;DO $$&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DECLARE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  batch_size &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  offset_val &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  rows_updated &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;BEGIN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  LOOP&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    UPDATE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tax_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tax_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      LIMIT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; batch_size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    GET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; DIAGNOSTICS rows_updated &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ROW_COUNT;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    EXIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHEN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; rows_updated &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PERFORM pg_sleep(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- brief pause between batches&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  END&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LOOP&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $$;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Step 3: Add NOT NULL constraint (fast — validates only in PG12 and later)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; COLUMN tax_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- PG12 and later: uses a not-null marker in pg_attribute, not a CHECK constraint scan&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Option 2 — Table rewrite with &lt;code&gt;pg_repack&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For bloated tables needing a full rewrite (e.g., removing a column after many deletes), &lt;code&gt;pg_repack&lt;/code&gt; performs online table rebuilding without extended ACCESS EXCLUSIVE:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Install pg_repack extension&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; EXTENSION&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pg_repack&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Run repack online — rebuilds table without long lock&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_repack&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -h&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; localhost&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -U&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; postgres&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; mydb&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -t&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# With specific columns (version 1.4.7 and later)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_repack&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -h&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; localhost&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -U&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; postgres&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; mydb&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --table&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;pg_repack&lt;/code&gt; works by building a new table copy online, capturing changes via a trigger, then performing a fast swap at the end. The final swap takes a brief ACCESS EXCLUSIVE lock (usually under a second). Per the &lt;code&gt;pg_repack&lt;/code&gt; documentation, it requires the table to have a primary key or a unique constraint.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 3 — Scheduled maintenance window with monitoring&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When no online alternative exists — changing a column type, adding a foreign key that requires a full scan, or truncating a large table — execute during a maintenance window with active monitoring:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Set a lock timeout to abort if the migration waits too long for a lock&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; lock_timeout&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;5s&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Set a statement timeout as a safety net&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; statement_timeout &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;10min&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Run migration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; COLUMN amount &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TYPE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NUMERIC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Monitor from a second session during execution&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pid, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, query, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; xact_start &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; duration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;idle&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; duration &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A &lt;code&gt;lock_timeout&lt;/code&gt; prevents the migration from queuing indefinitely behind a long-running transaction. If the migration cannot acquire its lock in 5 seconds, it aborts cleanly, allowing you to investigate what is holding the lock before retrying.&lt;/p&gt;
&lt;h3 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h3&gt;
&lt;p&gt;For every migration, have the rollback command written before running the forward migration:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Forward: add column&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ADD&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; COLUMN archived_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamptz&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Rollback: drop column&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DROP&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; COLUMN &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; EXISTS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; archived_at;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Forward: create index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; CONCURRENTLY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx_orders_status &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Rollback: drop index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DROP&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CONCURRENTLY &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; EXISTS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx_orders_status;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Forward: add constraint (using NOT VALID to avoid full scan)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ADD&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; CONSTRAINT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; chk_orders_positive_amount&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  CHECK&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (amount &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; VALID;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Validate separately (allows reads and writes during validation)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders VALIDATE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CONSTRAINT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; chk_orders_positive_amount;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Rollback: drop constraint&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DROP&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; CONSTRAINT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; IF&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; EXISTS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; chk_orders_positive_amount;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For migrations that are irreversible at the data level (DROP COLUMN, TRUNCATE), the rollback plan is: restore from backup. This should be documented explicitly in the migration, and the backup should be confirmed current before running.&lt;/p&gt;
&lt;h3 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h3&gt;
&lt;p&gt;A pre-migration risk assessment script that runs before any &lt;code&gt;ALTER TABLE&lt;/code&gt; in your CI pipeline catches most issues automatically:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/bin/bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Check if a migration will require a table rewrite on a large table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TABLE_SIZE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;psql&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -tAc&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;SELECT pg_relation_size(&apos;${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TABLE_NAME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&apos;::regclass)&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TABLE_SIZE_GB&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;scale=2; ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TABLE_SIZE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}/1073741824&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; bc&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (( $(echo &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$TABLE_SIZE_GB&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &gt; 1&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; bc &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;l) )); &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;then&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;WARNING: Table ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TABLE_NAME&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} is ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TABLE_SIZE_GB&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}GB&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Verify migration is CONCURRENTLY-safe or schedule maintenance window&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  exit&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;fi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For teams using schema migration tools (Flyway, Liquibase, golang-migrate), pre-migration hooks that run the size check and lock-type classification against the target SQL are the standard pattern.&lt;/p&gt;
&lt;h3 id=&quot;schema-deployment-checklist&quot;&gt;Schema Deployment Checklist&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Identify the SQL statement and its lock type — ACCESS EXCLUSIVE, SHARE, or SHARE UPDATE EXCLUSIVE&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;pg_total_relation_size&lt;/code&gt; for the target table — flag if greater than 1 GB&lt;/li&gt;
&lt;li&gt;Determine if the migration is reversible — write the rollback SQL before running the forward migration&lt;/li&gt;
&lt;li&gt;Test execution time on a production-sized staging database with &lt;code&gt;\timing&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Confirm the migration is idempotent — add &lt;code&gt;IF NOT EXISTS&lt;/code&gt; and &lt;code&gt;IF EXISTS&lt;/code&gt; guards where applicable&lt;/li&gt;
&lt;li&gt;Determine if an online alternative exists — &lt;code&gt;CONCURRENTLY&lt;/code&gt; index, PG11+ ADD COLUMN, 3-step NOT NULL&lt;/li&gt;
&lt;li&gt;For ACCESS EXCLUSIVE on large tables — schedule a maintenance window or use the online alternative&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;lock_timeout = &apos;5s&apos;&lt;/code&gt; and &lt;code&gt;statement_timeout&lt;/code&gt; before running any blocking migration&lt;/li&gt;
&lt;li&gt;Confirm a current backup exists before running any irreversible migration (DROP COLUMN, TRUNCATE)&lt;/li&gt;
&lt;li&gt;Monitor &lt;code&gt;pg_stat_activity&lt;/code&gt; for lock contention during the migration window from a second session&lt;/li&gt;
&lt;li&gt;Verify replication lag does not spike during migration — check &lt;code&gt;pg_stat_replication&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;After migration completes, run &lt;code&gt;EXPLAIN (ANALYZE)&lt;/code&gt; on the primary affected queries to confirm plan is correct&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The PostgreSQL documentation for &lt;code&gt;ADD COLUMN&lt;/code&gt; explicitly describes the behavioral change in PostgreSQL 11: prior to version 11, &lt;code&gt;ADD COLUMN&lt;/code&gt; with a &lt;code&gt;DEFAULT&lt;/code&gt; clause required a full table rewrite to store the default in every existing row. PostgreSQL 11 introduced storage of the default in &lt;code&gt;pg_attrdef&lt;/code&gt;, allowing &lt;code&gt;ADD COLUMN ... DEFAULT&lt;/code&gt; to complete in milliseconds regardless of table size — the default is applied on read for existing rows, not during the migration. This behavior is documented in the PostgreSQL 11 release notes.&lt;/p&gt;
&lt;p&gt;The documentation for &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; documents its two-pass scan approach: it makes two passes over the table — one to build the initial index, one to incorporate concurrent changes — before marking the index valid. This means it takes longer than non-concurrent index creation, but it never holds an ACCESS EXCLUSIVE lock. The tradeoff is explicit in the documentation: “the table is not locked against writes for an extended period of time, but the build takes longer.”&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; leaves invalid index&lt;/td&gt;&lt;td&gt;Transaction conflict or cancellation during build&lt;/td&gt;&lt;td&gt;Drop the invalid index; recreate with CONCURRENTLY&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;NOT VALID&lt;/code&gt; constraint skips existing data violations&lt;/td&gt;&lt;td&gt;Backfill was incomplete before constraint was added&lt;/td&gt;&lt;td&gt;Run &lt;code&gt;VALIDATE CONSTRAINT&lt;/code&gt; to enforce on all rows; fix violations first&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;3-step NOT NULL breaks if backfill is skipped&lt;/td&gt;&lt;td&gt;Developer runs step 1 and step 3 without step 2&lt;/td&gt;&lt;td&gt;Enforce step ordering in migration tooling; use explicit progress markers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;lock_timeout&lt;/code&gt; causes migration abort&lt;/td&gt;&lt;td&gt;Another long transaction holds an incompatible lock&lt;/td&gt;&lt;td&gt;Identify and wait for blocking transaction; retry migration with longer timeout&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;pg_repack&lt;/code&gt; fails on table with no primary key&lt;/td&gt;&lt;td&gt;Table uses composite key or has no unique identifier&lt;/td&gt;&lt;td&gt;Add a surrogate primary key first, or use a maintenance window rewrite&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-this-post-does-not-cover&quot;&gt;What This Post Does Not Cover&lt;/h2&gt;
&lt;p&gt;This checklist covers schema migration risk for PostgreSQL and MySQL. It does not cover: migration tooling comparisons (Flyway vs Liquibase vs sqitch), zero-downtime application deployment patterns when schema and code changes must roll out together, MongoDB schema validation evolution, or database-level encryption key rotation during schema changes. Each of those is a separate decision area.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Schema migrations that appear safe on small staging tables can hold ACCESS EXCLUSIVE locks for minutes on large production tables, queuing and dropping connections until they complete or are killed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Classify every migration by lock type and table size before running it; use &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; and the 3-step NOT NULL split for large tables; and always have the rollback command written before the forward migration runs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: After implementing &lt;code&gt;CONCURRENTLY&lt;/code&gt; and deferred NOT NULL patterns, migration deployments should complete with zero connection queuing — observable in &lt;code&gt;pg_stat_activity&lt;/code&gt; showing no waiting state during the migration window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Pick one upcoming schema migration and run through this checklist before executing it. If it requires ACCESS EXCLUSIVE on a table over 1 GB, find the online alternative or schedule the maintenance window before the deployment date.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>checklist</category><category>architecture</category></item><item><title>Oracle Autonomous Database: What It Automates and What It Cannot Know</title><link>https://rajivonai.com/blog/2023-06-20-oracle-autonomous-database-what-it-automates-and-what-it-cannot-know/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-06-20-oracle-autonomous-database-what-it-automates-and-what-it-cannot-know/</guid><description>Oracle Autonomous Database automates patching and scaling, but cannot substitute for query intent, schema decisions, and access patterns the team must own.</description><pubDate>Tue, 20 Jun 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The dangerous version of “autonomous database” is not the vendor promise. It is the team assumption that automation understands intent.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Database operations have always carried a high coordination cost. Someone has to size compute, watch storage, patch engines, validate backups, rotate certificates, tune indexes, review execution plans, harden defaults, and respond when the workload changes faster than the runbook.&lt;/p&gt;
&lt;p&gt;Oracle Autonomous Database attacks that operational surface directly. Oracle describes the service as automating routine database lifecycle work such as provisioning, patching, upgrades, backups, tuning, and scaling. Its documentation also separates provider-owned responsibilities from customer-owned ones, including application security and application design in the customer boundary.&lt;/p&gt;
&lt;p&gt;That distinction matters. Autonomous Database is not just a managed Oracle instance with fewer knobs. It is a database control plane that continuously observes telemetry, applies policy, and changes parts of the system without waiting for a human DBA to schedule every step.&lt;/p&gt;
&lt;p&gt;For teams running mostly standard transactional or analytical workloads, that is a real architectural shift. A large class of toil moves from human procedure to provider automation. The question is no longer whether a DBA remembered to apply a quarterly patch. The question is whether the system being patched, tuned, and scaled actually represents the product’s correctness model.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The operational failure mode changes shape.&lt;/p&gt;
&lt;p&gt;In a self-managed database, many incidents come from missed maintenance: an expired certificate, an untested backup, an index that should have been created, a patch window that never happened, a storage threshold ignored until the filesystem filled.&lt;/p&gt;
&lt;p&gt;In an autonomous database, many of those failures are reduced, but a different class remains. The database can observe SQL latency, wait events, resource consumption, storage growth, backup state, and configuration drift. It cannot infer whether an order may be charged twice, whether a customer record belongs to a regulated residency boundary, whether a new column changes contractual reporting, or whether a migration is reversible under live traffic.&lt;/p&gt;
&lt;p&gt;This creates a subtle trap. Teams outsource database administration and accidentally outsource database thinking. They treat fewer operational knobs as fewer architectural responsibilities.&lt;/p&gt;
&lt;p&gt;The core question is: &lt;strong&gt;what should be delegated to Autonomous Database, and what must stay explicitly owned by the application and platform team?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;autonomous-databases-are-control-loops-not-architects&quot;&gt;Autonomous Databases Are Control Loops, Not Architects&lt;/h2&gt;
&lt;p&gt;The clean boundary is to treat Oracle Autonomous Database as a set of managed control loops around the database engine, not as a replacement for system design.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;A[Workload intent — service objectives] --&gt; B[Database automation boundary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;B --&gt; C[Provisioning — placement and capacity]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;B --&gt; D[Operations — backups patching repair]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;B --&gt; E[Performance control — indexing tuning plans]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;B --&gt; F[Security baseline — encryption hardened defaults]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;A --&gt; G[Application boundary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;G --&gt; H[Data model — ownership and invariants]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;G --&gt; I[Query shape — access paths and latency budgets]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;G --&gt; J[Release process — migrations and rollback]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;G --&gt; K[Business semantics — correctness and risk]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Inside the automation boundary, Autonomous Database can remove large amounts of undifferentiated work. It can provision database resources, apply patches, manage backups, tune SQL plans, create or manage indexes, encrypt data, and scale capacity. Oracle’s own technical overview says the service automates administrative functions while application code, SQL shape, and schema semantics remain outside the automation contract.&lt;/p&gt;
&lt;p&gt;That makes the architecture useful when the team is clear about the handoff:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Let the service own repeatable operational mechanics.&lt;/li&gt;
&lt;li&gt;Let the application own intent, invariants, access patterns, and failure semantics.&lt;/li&gt;
&lt;li&gt;Let platform engineering own evidence: tests, metrics, alerts, recovery drills, and migration discipline.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The mistake is expecting telemetry to substitute for intent. The database can notice that a query became expensive. It cannot know that the query should no longer exist because the product flow changed. It can tune access paths. It cannot decide whether denormalization violates a reporting invariant. It can keep backups. It cannot decide the business recovery point objective after a mistaken bulk update.&lt;/p&gt;
&lt;p&gt;Autonomy is strongest when the objective function is measurable: lower latency, less wasted capacity, current patches, successful backups, reduced plan regressions. It is weakest when the objective function is semantic: correctness, contractual risk, regulatory meaning, customer trust, and release reversibility.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; Oracle’s documented pattern is explicit shared responsibility. Autonomous Database automates database infrastructure and many administrative tasks, but Oracle’s responsibility model leaves application security and application-level behavior with the customer. That is not a loophole; it is the architecture boundary.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Design the database layer as if the engine will keep improving operations, while the application must keep declaring intent. Use constraints for invariants the database can enforce. Use idempotency keys where retries can duplicate effects. Use schema migration tooling that supports expand-and-contract changes. Define service-level objectives around query families, not only aggregate database health. Keep recovery drills that test restore, replay, and operator decision paths.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The team gets the benefit of autonomous operations without losing engineering control. Patching, backup management, baseline hardening, and capacity changes become less dependent on individual memory. At the same time, product correctness remains testable because it is encoded in schema constraints, transaction boundaries, migration checks, and release gates.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; The documented pattern is that managed databases reduce the administrative failure surface, not the design failure surface. PostgreSQL’s behavior around transaction isolation is a useful comparison: the database can provide isolation levels and enforce constraints, but the application still chooses transaction scope and must handle serialization failures when using strict isolation. The same principle applies here. A database can provide stronger machinery than the team could reasonably operate alone, but it cannot choose the application’s correctness contract.&lt;/p&gt;
&lt;p&gt;A practical example is indexing. Automatic indexing can help when recurring SQL statements have stable patterns and measurable improvement. But index creation is not a substitute for understanding access paths. If a new feature starts issuing unbounded exploratory queries against a hot transactional table, the problem is not merely missing indexes. The problem is an access pattern that may need pagination, precomputation, query isolation, or a separate analytical path.&lt;/p&gt;
&lt;p&gt;Security has the same split. Autonomous Database can enforce hardened defaults, encryption, patching, and database-level controls. It cannot know whether an application endpoint exposes a report to the wrong tenant, whether a developer put secrets in a deployment variable with excessive reach, or whether a service account has become a confused deputy. Those failures live above the database boundary.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Area&lt;/th&gt;&lt;th&gt;What Autonomous Database can automate&lt;/th&gt;&lt;th&gt;What it cannot know&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Patching&lt;/td&gt;&lt;td&gt;Apply database and infrastructure updates with provider control&lt;/td&gt;&lt;td&gt;Whether a release window conflicts with business operations&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backups&lt;/td&gt;&lt;td&gt;Create and manage database backups&lt;/td&gt;&lt;td&gt;Which mistaken writes are legally or commercially reversible&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Tuning&lt;/td&gt;&lt;td&gt;Adjust plans, indexes, and resources from workload telemetry&lt;/td&gt;&lt;td&gt;Whether the query should exist in the product path&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Scaling&lt;/td&gt;&lt;td&gt;Add or reduce capacity based on demand signals&lt;/td&gt;&lt;td&gt;Whether demand is legitimate traffic, abuse, or a broken client loop&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Security&lt;/td&gt;&lt;td&gt;Provide encryption, hardened configuration, and database controls&lt;/td&gt;&lt;td&gt;Whether application authorization matches tenant and data policy&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Availability&lt;/td&gt;&lt;td&gt;Reduce operational toil and infrastructure failure modes&lt;/td&gt;&lt;td&gt;Whether the end-to-end workflow survives dependency failure&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schema&lt;/td&gt;&lt;td&gt;Store and enforce declared structures and constraints&lt;/td&gt;&lt;td&gt;Whether the model expresses the business domain correctly&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The hardest failures are cross-layer failures. A migration that changes a nullable column to required is not just a database operation. It is a deployment choreography problem. A reporting query that times out is not just a tuning problem. It may be a workload isolation problem. A restored backup is not recovery unless the application, queues, caches, and downstream systems can be brought back to a coherent point.&lt;/p&gt;
&lt;p&gt;Autonomous Database can make the database tier more reliable while making weak architecture easier to ignore. That is the tradeoff. Less toil creates more room for design work, but only if the team spends the freed capacity on design.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Treating database autonomy as full system autonomy hides failures in application semantics, migrations, and recovery behavior.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Draw a hard boundary between provider-owned database operations and team-owned intent. Use Autonomous Database for repeatable operational control loops, not for architectural judgment.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Validate the boundary with evidence: constraint tests, migration rehearsals, query budgets, restore drills, tenant authorization tests, and dashboards by workload class rather than only database-wide averages.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before moving a workload onto Oracle Autonomous Database, write down the decisions it will automate, the decisions your team still owns, and the incident scenarios that must be tested outside the database engine.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Cloud Database Cost Triage: Storage, IOPS, CPU, Replicas</title><link>https://rajivonai.com/blog/2023-06-05-cloud-database-cost-triage-storage-iops-cpu-replicas/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-06-05-cloud-database-cost-triage-storage-iops-cpu-replicas/</guid><description>A structured runbook for identifying which cost dimension is driving your AWS RDS or Aurora bill before making any changes.</description><pubDate>Mon, 05 Jun 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The RDS bill is higher than expected and the instinct to scale up the instance or add a replica is almost always the wrong first move.&lt;/strong&gt; Cost spikes in cloud databases have four distinct drivers — storage, IOPS, instance class, and replicas — and each requires a different remediation. Acting on the wrong one wastes money and may make the problem worse. The right move is triage first.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;AWS RDS and Aurora bill on four independent cost dimensions: storage consumed, I/O operations performed, the instance class running the engine, and the number of instances attached to the cluster. When a monthly bill grows faster than traffic, it is usually one of these dimensions accelerating — not all four simultaneously.&lt;/p&gt;
&lt;p&gt;The problem is that Cost Explorer shows total database spend, not cost per dimension. An engineer looking at a $4,000 line item for “Amazon RDS” cannot tell whether the driver is 2 TB of unclaimed storage, a gp2 volume depleting its burst I/O credits, an over-provisioned db.r6g.2xlarge sitting at 8% CPU, or three read replicas that no longer carry meaningful traffic.&lt;/p&gt;
&lt;p&gt;Each of those four scenarios has a different first command to run and a different remediation. Conflating them means you might rightsize the instance when the actual driver is 800 GB of dead tuples waiting on autovacuum.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Signal&lt;/th&gt;&lt;th&gt;Where to see it&lt;/th&gt;&lt;th&gt;What it means&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Storage cost growing without traffic growth&lt;/td&gt;&lt;td&gt;AWS Cost Explorer, grouped by usage type&lt;/td&gt;&lt;td&gt;Table bloat, dead tuples, or log accumulation not being reclaimed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;IOPS charges on a gp2 volume&lt;/td&gt;&lt;td&gt;CloudWatch &lt;code&gt;VolumeReadIOPS&lt;/code&gt; and &lt;code&gt;VolumeWriteIOPS&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Burst credit balance depleted; every I/O now billed at the gp2 overage rate&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High instance cost relative to CPU utilization&lt;/td&gt;&lt;td&gt;CloudWatch &lt;code&gt;CPUUtilization&lt;/code&gt; p95 over 30 days&lt;/td&gt;&lt;td&gt;Instance class is over-provisioned for the actual workload&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica count grew over time&lt;/td&gt;&lt;td&gt;RDS console — DB instances view&lt;/td&gt;&lt;td&gt;Replicas added reactively without a retirement policy; each one bills at primary instance rates&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Snapshot retention set to maximum&lt;/td&gt;&lt;td&gt;RDS console — Maintenance and backups&lt;/td&gt;&lt;td&gt;Snapshots older than policy requires accumulate silently at $0.095 per GB-month&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Database and table sizes&lt;/strong&gt; — connect to the PostgreSQL instance and run both queries. The first gives total database size; the second surfaces the top bloat candidates by table.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Total database size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_size_pretty(pg_database_size(current_database()));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Top 10 tables by total size (including indexes and toast)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  schemaname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tablename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pg_size_pretty(pg_total_relation_size(schemaname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;.&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tablename)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; total_size,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pg_size_pretty(pg_relation_size(schemaname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;.&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tablename))       &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; table_size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_total_relation_size(schemaname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;.&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tablename) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If a table’s total size is significantly larger than its live row count implies, dead tuples are accumulating. Cross-reference with &lt;code&gt;pg_stat_user_tables.n_dead_tup&lt;/code&gt;.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;Write amplification signal from the background writer&lt;/strong&gt; — PostgreSQL’s &lt;code&gt;pg_stat_bgwriter&lt;/code&gt; tracks how much I/O the background writer and checkpointer are generating. High &lt;code&gt;buffers_checkpoint&lt;/code&gt; relative to &lt;code&gt;buffers_clean&lt;/code&gt; or &lt;code&gt;buffers_backend&lt;/code&gt; indicates that checkpointing is driving write I/O, not the application directly.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  checkpoints_timed,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  checkpoints_req,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  buffers_checkpoint,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  buffers_clean,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  buffers_backend,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  maxwritten_clean&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_bgwriter;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;AWS documents that RDS gp2 volumes use a credit-based burst model. As documented in the AWS RDS storage documentation, a gp2 volume earns 3 IOPS per GB per second and can burst to 3,000 IOPS until the credit bucket empties. Once depleted, throughput drops to the baseline rate and every operation above baseline is billed at the provisioned IOPS rate. &lt;code&gt;buffers_checkpoint&lt;/code&gt; growing while CloudWatch &lt;code&gt;BurstBalance&lt;/code&gt; drops toward zero is the signature of this problem.&lt;/p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;IOPS consumption in CloudWatch&lt;/strong&gt; — pull &lt;code&gt;VolumeReadIOPS&lt;/code&gt; and &lt;code&gt;VolumeWriteIOPS&lt;/code&gt; for the last 30 days with a 1-hour resolution. If the volume is gp2 and you see sustained IOPS above 3,000, the burst balance is gone and you are in the expensive steady state.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; cloudwatch&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; get-metric-statistics&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --namespace&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; AWS/RDS&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --metric-name&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; WriteIOPS&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --dimensions&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; Name=DBInstanceIdentifier,Value=YOUR_DB_ID&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --start-time&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -u&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -v-30d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +%Y-%m-%dT%H:%M:%SZ&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --end-time&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -u&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +%Y-%m-%dT%H:%M:%SZ&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --period&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3600&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --statistics&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; Average&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --output&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;&lt;strong&gt;CPU utilization p95 over 30 days&lt;/strong&gt; — pull &lt;code&gt;CPUUtilization&lt;/code&gt; statistics. AWS Compute Optimizer evaluates RDS instances and flags over-provisioned instances when p99 CPU stays below 40% over a 14-day observation window. If p95 CPU is consistently below 40%, the instance is a rightsizing candidate.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; cloudwatch&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; get-metric-statistics&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --namespace&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; AWS/RDS&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --metric-name&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; CPUUtilization&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --dimensions&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; Name=DBInstanceIdentifier,Value=YOUR_DB_ID&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --start-time&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -u&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -v-30d&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +%Y-%m-%dT%H:%M:%SZ&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --end-time&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -u&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +%Y-%m-%dT%H:%M:%SZ&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --period&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3600&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --statistics&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; p95&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --output&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Rightsizing down one instance class (e.g., db.r6g.2xlarge to db.r6g.xlarge) typically halves the instance-hour cost while maintaining the same network and storage performance characteristics.&lt;/p&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;&lt;strong&gt;Replica replication activity&lt;/strong&gt; — query &lt;code&gt;pg_stat_replication&lt;/code&gt; on the primary to see what each replica is actually doing. &lt;code&gt;sent_lsn&lt;/code&gt; minus &lt;code&gt;replay_lsn&lt;/code&gt; is the replication lag in bytes. If a replica’s &lt;code&gt;state&lt;/code&gt; is &lt;code&gt;streaming&lt;/code&gt; but it is rarely queried (verify via the replica’s own &lt;code&gt;pg_stat_activity&lt;/code&gt; or CloudWatch &lt;code&gt;DatabaseConnections&lt;/code&gt;), it is a cost-only presence.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  client_addr,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  application_name,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  sent_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  write_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  flush_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  replay_lsn,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  sync_state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_replication;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For the broader question of whether read replicas are delivering value relative to their cost, see &lt;a href=&quot;https://rajivonai.com/blog/2023-04-17-read-replicas-are-not-free-scale/&quot;&gt;Read Replicas Are Not Free Scale&lt;/a&gt; — which covers the replication lag model and the routing decisions that make replicas worth keeping.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Bill spike detected] --&gt; B{Storage cost growing?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| C{Table bloat above 20%?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|yes| D[Run VACUUM or pg_repack]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|no| E[Audit snapshot retention policy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| F{IOPS charges high?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|yes| G{gp2 burst balance depleted?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|yes| H[Migrate volume to gp3]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|no| I[Check pg_stat_bgwriter for write amplification]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|no| J{CPU p95 below 40%?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|yes| K[Rightsize instance class down]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|no| L{CPU p95 above 70%?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|yes| M[Optimize queries or scale up]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|no| N{Replica traffic justified?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    N --&gt;|no| O[Remove idle replicas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    N --&gt;|yes| P[No cost action needed — monitor]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Option 1 — Reclaim storage from table bloat&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PostgreSQL’s MVCC model retains dead tuples until autovacuum or manual vacuum cleans them. On RDS, autovacuum runs automatically but can fall behind on high-write tables. Bloat inflates &lt;code&gt;pg_database_size&lt;/code&gt;, which directly inflates Aurora storage billing (Aurora charges per GB-month for all allocated storage, including dead tuple space).&lt;/p&gt;
&lt;p&gt;For tables where you can tolerate a brief lock, &lt;code&gt;VACUUM FULL&lt;/code&gt; rewrites the table and releases space to the OS. For live tables, &lt;code&gt;pg_repack&lt;/code&gt; performs the same operation online without a full table lock.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Identify bloat candidates&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  schemaname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tablename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_dead_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_live_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_dead_tup::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; NULLIF&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_live_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_dead_tup, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dead_pct,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_autovacuum,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_vacuum&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_dead_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dead_pct &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Reclaim space (causes brief AccessExclusiveLock)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;VACUUM FULL &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VERBOSE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; your_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;your_table&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Option 2 — Migrate gp2 to gp3 for explicit IOPS control&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;AWS documents the gp2 volume type as a burst model: baseline throughput is 3 IOPS/GB, maximum burst is 3,000 IOPS, and burst credits replenish at 3 credits per GB per second. Once the credit bucket empties, the volume returns to baseline and sustained writes above baseline are billed at the gp2 I/O pricing tier.&lt;/p&gt;
&lt;p&gt;gp3 eliminates the burst model. Storage and IOPS are provisioned independently: 3,000 IOPS and 125 MiB/s baseline are included at no additional cost, with additional IOPS purchasable at $0.02 per provisioned IOPS-month. For workloads that have depleted their gp2 burst balance, gp3 is typically lower cost at equivalent IOPS.&lt;/p&gt;
&lt;p&gt;The migration is online and reversible — RDS performs it as a storage modification with no downtime required.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; rds&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; modify-db-instance&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --db-instance-identifier&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; YOUR_DB_ID&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --storage-type&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; gp3&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --iops&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3000&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --apply-immediately&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Option 3 — Rightsize the instance class&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When CloudWatch &lt;code&gt;CPUUtilization&lt;/code&gt; p95 stays below 40% over a 30-day window, the instance class is over-provisioned. AWS Compute Optimizer surfaces RDS rightsizing recommendations automatically; the recommendations include projected savings and a confidence rating based on observed utilization.&lt;/p&gt;
&lt;p&gt;Rightsizing down one class within the same instance family (e.g., db.r6g.2xlarge to db.r6g.xlarge) retains the same memory-to-CPU ratio and network performance tier while halving instance-hour cost. Verify that the target instance class can accommodate peak connection count and memory requirements before applying.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Apply instance class change with minimal downtime (uses MultiAZ failover if enabled)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; rds&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; modify-db-instance&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --db-instance-identifier&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; YOUR_DB_ID&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --db-instance-class&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; db.r6g.xlarge&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --apply-immediately&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Option 4 — Remove idle read replicas&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Each RDS or Aurora read replica is a full instance billed at the same rate as the primary. Replicas that carry negligible query traffic (verify via CloudWatch &lt;code&gt;DatabaseConnections&lt;/code&gt; on the replica endpoint) are pure cost with no throughput benefit.&lt;/p&gt;
&lt;p&gt;Removing a replica is a permanent action — there is no undo. If a replica might be needed for failover, promote it to a standalone instance first, then terminate the original replica relationship. If it is genuinely unused, delete it directly.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Delete a replica with no promotion needed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; rds&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; delete-db-instance&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --db-instance-identifier&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; YOUR_REPLICA_ID&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --skip-final-snapshot&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Storage VACUUM FULL&lt;/strong&gt; — not reversible in the traditional sense; the operation releases space. If the lock causes application errors, monitor &lt;code&gt;pg_stat_activity&lt;/code&gt; for blocking queries. Prefer &lt;code&gt;pg_repack&lt;/code&gt; on production tables to avoid the lock.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;gp2 to gp3 migration&lt;/strong&gt; — reversible. AWS allows reverting a gp3 volume back to gp2 via another storage modification. Monitor CloudWatch &lt;code&gt;WriteLatency&lt;/code&gt; and &lt;code&gt;ReadLatency&lt;/code&gt; after the change; if latency increases, revert.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Instance class rightsize&lt;/strong&gt; — reversible. Scale back up via &lt;code&gt;modify-db-instance&lt;/code&gt;. If using Multi-AZ, the downtime is a failover window (typically under 60 seconds). Monitor &lt;code&gt;DatabaseConnections&lt;/code&gt;, &lt;code&gt;FreeableMemory&lt;/code&gt;, and &lt;code&gt;CPUUtilization&lt;/code&gt; for 48 hours after the change.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Replica removal&lt;/strong&gt; — not reversible. A deleted replica cannot be re-attached. Create a new replica from scratch if needed. Before deleting, capture the replica’s CloudWatch &lt;code&gt;DatabaseConnections&lt;/code&gt; over the last 30 days to confirm it was idle.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;Cost anomaly detection in AWS Cost Explorer can alert when RDS spend deviates from a predicted baseline. Set a threshold of 10–15% above the trailing 30-day average for the database service line; this catches storage growth and IOPS spikes before the end-of-month invoice.&lt;/p&gt;
&lt;p&gt;AWS Compute Optimizer generates RDS rightsizing recommendations on a rolling basis. Export the recommendations weekly via the Compute Optimizer API and route flagged instances to a Slack channel or ticket queue for review. The documented API call is straightforward:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; compute-optimizer&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; get-rds-database-recommendations&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --filters&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; name=Finding,values=Overprovisioned&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --output&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For replica auditing, a scheduled PostgreSQL query on the primary that writes &lt;code&gt;pg_stat_replication&lt;/code&gt; state and replica endpoint &lt;code&gt;DatabaseConnections&lt;/code&gt; to a monitoring table gives a weekly audit trail. Flag replicas where the rolling 7-day average connection count on the replica endpoint is below five; those are candidates for removal review.&lt;/p&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;What broke: The RDS billing line grew faster than traffic because one or more of four cost dimensions — storage bloat, IOPS burst depletion, over-provisioned instance class, or idle replicas — was not monitored against a policy.&lt;/li&gt;
&lt;li&gt;What was done: Each dimension was triaged in order using documented CloudWatch metrics and PostgreSQL system catalog queries; the offending dimension was identified and remediated with a reversible change.&lt;/li&gt;
&lt;li&gt;What prevents recurrence: Compute Optimizer rightsizing alerts, Cost Explorer anomaly detection, and a monthly replica audit ensure each dimension is reviewed before it compounds.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;checklist&quot;&gt;Checklist&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Pull AWS Cost Explorer grouped by RDS usage type to identify which billing dimension is growing.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;SELECT pg_size_pretty(pg_database_size(current_database()))&lt;/code&gt; on each RDS instance to establish a storage baseline.&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;pg_stat_user_tables&lt;/code&gt; for tables with dead tuple percentages above 20%; schedule &lt;code&gt;VACUUM FULL&lt;/code&gt; or &lt;code&gt;pg_repack&lt;/code&gt; for the top offenders.&lt;/li&gt;
&lt;li&gt;Check CloudWatch &lt;code&gt;BurstBalance&lt;/code&gt; on any gp2 volume; if it is below 50% and trending down, plan a gp3 migration.&lt;/li&gt;
&lt;li&gt;Pull 30-day &lt;code&gt;VolumeWriteIOPS&lt;/code&gt; with 1-hour resolution; compare to gp2 baseline rate for the volume size.&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;pg_stat_bgwriter&lt;/code&gt; to detect write amplification from checkpoint pressure; tune &lt;code&gt;checkpoint_completion_target&lt;/code&gt; and &lt;code&gt;max_wal_size&lt;/code&gt; if &lt;code&gt;checkpoints_req&lt;/code&gt; is high.&lt;/li&gt;
&lt;li&gt;Pull 30-day &lt;code&gt;CPUUtilization&lt;/code&gt; p95; flag any instance where p95 is below 40% as an over-provisioning candidate.&lt;/li&gt;
&lt;li&gt;Review AWS Compute Optimizer recommendations for the RDS cluster; document each flagged instance and projected savings.&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;pg_stat_replication&lt;/code&gt; on the primary and cross-reference replica endpoint &lt;code&gt;DatabaseConnections&lt;/code&gt; to identify replicas with no meaningful traffic.&lt;/li&gt;
&lt;li&gt;Remove or repurpose idle replicas after confirming they are not required for failover topology.&lt;/li&gt;
&lt;li&gt;Set snapshot retention to match the recovery point objective in the database’s SLA; remove retention beyond policy.&lt;/li&gt;
&lt;li&gt;Enable Cost Explorer anomaly detection for the RDS service line at a 10–15% deviation threshold.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: An RDS bill spike triggers the instinct to scale the instance or add replicas — changes that are expensive, slow to take effect, and often targeting the wrong cost dimension entirely.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Triage the four cost dimensions in order — storage bloat, IOPS burst depletion, over-provisioned instance class, idle replicas — using CloudWatch metrics and PostgreSQL system catalog queries before making any change.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: A specific dimension is identified as the driver, a targeted remediation is applied, and the next month’s Cost Explorer line for that dimension is lower — without touching the dimensions that were not the cause.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, enable AWS Compute Optimizer for your RDS instances and set a Cost Explorer anomaly detection alert at 15% above your 30-day RDS baseline — both are free to configure and will surface the next cost spike before it compounds.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>checklist</category></item><item><title>OCI Reference Architecture: Load Balancing, OKE, Autonomous Database, Cache, and Queue</title><link>https://rajivonai.com/blog/2023-06-05-oci-reference-architecture-load-balancing-oke-autonomous-database-cache-and-queue/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-06-05-oci-reference-architecture-load-balancing-oke-autonomous-database-cache-and-queue/</guid><description>How OCI load balancing, OKE, Autonomous Database, cache, and queue layers interact — and why cross-service ambiguity assumptions cause the first failure.</description><pubDate>Mon, 05 Jun 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The first failure in a cloud architecture is rarely the database, the cluster, or the load balancer alone; it is the assumption that one managed service can absorb ambiguity from every other layer.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Teams moving transactional systems onto Oracle Cloud Infrastructure usually start with a clean target picture: traffic enters through OCI Load Balancer, application containers run on Oracle Container Engine for Kubernetes, durable state lives in Autonomous Database, hot reads use OCI Cache, and slow work moves through OCI Queue.&lt;/p&gt;
&lt;p&gt;That shape is directionally right. It separates ingress, compute, persistence, cache, and asynchronous processing. It lets each layer scale on a different axis. It also maps well to managed OCI services: Load Balancer provides backend sets and health checks, OKE provides Kubernetes clusters and node pools, Autonomous Database removes much of the database administration surface, OCI Cache provides Redis-compatible memory storage, and Queue gives a managed asynchronous buffer.&lt;/p&gt;
&lt;p&gt;But the reference diagram is not the architecture. The architecture is the set of failure contracts between those services.&lt;/p&gt;
&lt;p&gt;The load balancer must know when a pod is not ready. OKE must keep stateless workers replaceable. The database must remain the source of truth when cache data is stale. The queue must tolerate duplicate work. The application must degrade intentionally when one dependency is slow.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The common failure is treating managed services as if they remove distributed systems behavior. They do not. They move parts of the operational burden, but they leave the coupling decisions with the application team.&lt;/p&gt;
&lt;p&gt;A load balancer health check only proves the configured endpoint answered. It does not prove the pod can reach the database, has warmed its connection pool, can write to the queue, or can tolerate the current cache latency. A Kubernetes readiness probe can protect traffic, but only if it reflects dependencies carefully enough without turning every downstream blip into a full outage.&lt;/p&gt;
&lt;p&gt;A cache improves latency until it becomes a hidden consistency layer. If the application reads stale entitlements, inventory, pricing, or authorization data, the cache has stopped being an optimization and has become an undocumented database. A queue smooths spikes until producers outpace consumers, visibility timeouts expire, and duplicate messages reappear. Autonomous Database reduces administrative work, but it still needs bounded transactions, indexed access paths, connection pool limits, and backpressure from the application.&lt;/p&gt;
&lt;p&gt;The core question is: how should an OCI reference architecture be wired so each layer can fail without converting a local fault into a system-wide incident?&lt;/p&gt;
&lt;h2 id=&quot;failure-oriented-reference-architecture&quot;&gt;Failure-Oriented Reference Architecture&lt;/h2&gt;
&lt;p&gt;The answer is to make every boundary explicit: external traffic, service readiness, persistent writes, cache semantics, queue ownership, and operational control loops.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    U[users — browsers and clients] --&gt; LB[OCI Load Balancer — public ingress]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    LB --&gt;|health checked traffic| SVC[OKE service — stable virtual endpoint]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    SVC --&gt; PODS[application pods — stateless business logic]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PODS --&gt;|bounded query| ADB[Autonomous Database — durable system of record]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PODS --&gt;|read through cache| CACHE[OCI Cache — Redis compatible hot data]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PODS --&gt;|enqueue command| QUEUE[OCI Queue — asynchronous work buffer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    QUEUE --&gt; WORKERS[worker pods — idempotent processors]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WORKERS --&gt;|transactional update| ADB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WORKERS --&gt;|refresh derived data| CACHE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    PODS --&gt; OBS[metrics and logs — service level signals]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WORKERS --&gt; OBS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ADB --&gt; OBS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    CACHE --&gt; OBS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    QUEUE --&gt; OBS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OPS[operators — deployment and response] --&gt; OKE[OKE node pools — replaceable capacity]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OKE --&gt; PODS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OKE --&gt; WORKERS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The load balancer should terminate public ingress and forward only to Kubernetes services that represent deployable application boundaries. Its health checks should align with Kubernetes readiness, not with a superficial process check. A pod that has started but cannot serve production traffic should not be in rotation.&lt;/p&gt;
&lt;p&gt;OKE should run application pods and worker pods as separate deployments. The web path and asynchronous processing path have different scaling signals. Web pods scale on request concurrency and latency. Worker pods scale on queue depth, processing age, and downstream database saturation. Merging them into one deployment makes the critical path compete with background work during precisely the periods when isolation matters most.&lt;/p&gt;
&lt;p&gt;Autonomous Database should be treated as the authority for committed state. Cache entries should be derived, bounded by TTL, and safe to drop. The service should continue correctly when cache misses rise or the cache is flushed. A cache outage may hurt latency; it should not change correctness.&lt;/p&gt;
&lt;p&gt;Queue consumers should be idempotent. OCI Queue documents the core behavior that in-flight messages are hidden until their visibility timeout expires, and messages that exceed configured delivery attempts can move to a dead letter queue. That is the contract the application must honor: a message can be delivered more than once, and failure handling must be explicit.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; The documented OCI pattern is not a single magic service; it is a composition of managed primitives. OCI Load Balancer uses backend sets and health checks to decide where to send traffic. OKE exposes Kubernetes clusters and node pools for running containerized applications. OCI Cache is a managed in-memory cluster service compatible with Redis-style access patterns. OCI Queue is a managed service for decoupling producers and consumers. Autonomous Database automates many database operations, but it remains the transactional dependency that application code must use deliberately.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; Wire the request path for fast rejection and bounded work. Use load balancer and readiness checks to remove bad pods before users see errors. Keep API pods stateless and move slow side effects into OCI Queue. Use Autonomous Database for committed writes and transactional reads. Use OCI Cache for expensive, repeatable, disposable reads. Let workers consume queue messages, write idempotently, and update derived cache entries after the database commit succeeds.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The documented pattern is controlled degradation. If a pod fails, the load balancer and Kubernetes service stop routing to it. If a node fails, OKE can replace capacity through the node pool model. If cache latency rises, the application can bypass or miss through to the database while preserving correctness. If downstream processing slows, Queue absorbs work temporarily and exposes backlog as an operational signal. If a message cannot be processed repeatedly, the dead letter queue makes the failure inspectable instead of silently looping forever.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; The architecture works when every managed service has a narrow job. Load Balancer owns ingress distribution, not business health. OKE owns container orchestration, not transactional correctness. Autonomous Database owns durable state, not request admission. Cache owns latency reduction, not truth. Queue owns decoupling, not exactly-once execution. Once those boundaries are clear, the remaining engineering work is mostly about budgets: timeout budgets, retry budgets, connection budgets, queue age budgets, and recovery budgets.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;What goes wrong&lt;/th&gt;&lt;th&gt;Design response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Health check drift&lt;/td&gt;&lt;td&gt;Load balancer sends traffic to pods that Kubernetes would not consider ready&lt;/td&gt;&lt;td&gt;Use one readiness endpoint and make ingress health checks match it&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cache as truth&lt;/td&gt;&lt;td&gt;Stale cache entries create incorrect user-visible behavior&lt;/td&gt;&lt;td&gt;Treat cache as derived data with TTLs and safe miss behavior&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Queue retry storm&lt;/td&gt;&lt;td&gt;Failed work is retried until it overloads the database&lt;/td&gt;&lt;td&gt;Use visibility timeouts, max delivery attempts, dead letter queues, and idempotency keys&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Worker starvation&lt;/td&gt;&lt;td&gt;Background processing competes with user traffic&lt;/td&gt;&lt;td&gt;Separate API and worker deployments with independent autoscaling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Database saturation&lt;/td&gt;&lt;td&gt;More pods create more database connections than the database can absorb&lt;/td&gt;&lt;td&gt;Use connection pooling, request limits, and backpressure before scaling pods&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Deployment blast radius&lt;/td&gt;&lt;td&gt;One release changes web, worker, cache, and schema behavior together&lt;/td&gt;&lt;td&gt;Split rollouts and verify each contract independently&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The riskiest part of this architecture is not selecting OCI services; it is leaving the contracts between them implicit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Define the runtime contract for every boundary: readiness, timeout, retry, idempotency, cache freshness, queue age, and database connection limits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Verify the contracts with failure drills: kill pods, flush cache keys, slow database calls, poison queue messages, and force worker restarts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Build the first production version with separate API and worker deployments, Autonomous Database as the only durable authority, OCI Cache as disposable acceleration, and OCI Queue as an explicit asynchronous buffer.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>MySQL Binlog Format: Row vs Statement vs Mixed</title><link>https://rajivonai.com/blog/2023-05-29-mysql-binlog-format-row-statement-mixed/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-05-29-mysql-binlog-format-row-statement-mixed/</guid><description>Choosing the wrong MySQL binary log format silently breaks replication or bloats the binlog — this is the decision tree for picking the right one.</description><pubDate>Mon, 29 May 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;MySQL’s binary log records every change for replication and point-in-time recovery, but the format it uses to record those changes determines whether replicas stay consistent.&lt;/strong&gt; Three formats are available. One of them has a silent correctness problem that surfaces only when non-deterministic SQL runs on a replica, at which point the divergence is already committed to disk.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The binary log (binlog) is the backbone of MySQL replication and PITR. Every write that commits on the primary is written to the binlog. Replicas consume the binlog and replay those writes locally. The format controls how each write is recorded: as the original SQL statement, as the actual row values that changed, or as a combination of both selected automatically.&lt;/p&gt;
&lt;p&gt;Engineers provisioning a new MySQL server or migrating from an older version frequently encounter the format question without a clear default rationale. MySQL 5.7 defaulted to STATEMENT. MySQL 8.0 changed the default to ROW. The reason for that change is the correctness problem in STATEMENT format, and understanding it clarifies why ROW is the right default for most production workloads.&lt;/p&gt;
&lt;p&gt;You can check the current format on any running server:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; @@binlog_format;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;STATEMENT format logs the SQL text that ran on the primary. When the replica applies the statement, it re-executes that SQL. For most deterministic DML this is fine. The problem appears with non-deterministic functions: &lt;code&gt;UUID()&lt;/code&gt;, &lt;code&gt;RAND()&lt;/code&gt;, &lt;code&gt;NOW()&lt;/code&gt;, &lt;code&gt;SYSDATE()&lt;/code&gt;, user-defined functions, and some stored procedure patterns.&lt;/p&gt;
&lt;p&gt;Consider this insert:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders (id, session_token, created_at)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;42&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, UUID(), &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOW&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On the primary, &lt;code&gt;UUID()&lt;/code&gt; generates a specific UUID and &lt;code&gt;NOW()&lt;/code&gt; captures the current timestamp. That statement is written to the binlog verbatim. On the replica, the statement re-executes — but &lt;code&gt;UUID()&lt;/code&gt; generates a different UUID and &lt;code&gt;NOW()&lt;/code&gt; captures a different time. The primary and replica now hold different data for the same row. The replica has not errored. It has silently diverged.&lt;/p&gt;
&lt;p&gt;The same problem appears with &lt;code&gt;RAND()&lt;/code&gt;, triggers that call non-deterministic functions, and stored procedures whose output depends on server state. MySQL logs a warning in STATEMENT mode when it detects a non-deterministic statement, but the warning is easy to miss in a busy log.&lt;/p&gt;
&lt;h2 id=&quot;how-the-three-formats-work&quot;&gt;How the Three Formats Work&lt;/h2&gt;





























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Format&lt;/th&gt;&lt;th&gt;What is logged&lt;/th&gt;&lt;th&gt;Safe for non-deterministic SQL&lt;/th&gt;&lt;th&gt;Binlog size&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;STATEMENT&lt;/td&gt;&lt;td&gt;SQL text of the change&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Small&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ROW&lt;/td&gt;&lt;td&gt;Before and after values for each row&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Large for bulk operations&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MIXED&lt;/td&gt;&lt;td&gt;Automatically ROW when unsafe, STATEMENT otherwise&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Moderate&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;ROW format&lt;/strong&gt; logs the actual column values that changed for every row. For a statement that updates 10,000 rows, ROW format writes 10,000 row images to the binlog. This is verbose. A bulk DELETE or UPDATE that touches millions of rows produces a proportionally large binlog event. Binlog disk usage and replication bandwidth both increase relative to STATEMENT.&lt;/p&gt;
&lt;p&gt;The tradeoff is correctness: ROW format replicas always apply the exact values the primary committed. There is no re-execution, no non-determinism, no divergence risk.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;MIXED format&lt;/strong&gt; attempts to get the best of both: it uses STATEMENT by default and switches to ROW automatically when MySQL detects that the statement is unsafe for statement-based replication. The detection covers most known unsafe patterns, but coverage is not exhaustive — some stored procedure and trigger combinations can still produce unsafe MIXED-format behavior in edge cases.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;MySQL 8.0 default:&lt;/strong&gt; ROW. The MySQL 8.0 Reference Manual documents this change explicitly, noting that ROW is safer for replication consistency and required for some features including multi-source replication and certain crash-safe replica configurations.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Changing the format at runtime&lt;/strong&gt; (requires SUPER or BINLOG_ADMIN privilege):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Session level&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SESSION&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; binlog_format &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;ROW&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Global level (takes effect for new connections)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; GLOBAL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; binlog_format &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;ROW&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For a permanent change, set it in the MySQL configuration file:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;ini&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;[mysqld]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;binlog_format&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = ROW&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that changing the global binlog format does not affect the current session’s format. Each session that was open before the change continues using the old format until reconnected.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The MySQL 8.0 Reference Manual, in the chapter “Binary Logging Formats,” explicitly documents the non-deterministic function risk in STATEMENT mode and lists the categories of unsafe statements. The change from STATEMENT to ROW as the MySQL 8.0 default is documented in the MySQL 8.0 release notes and the replication chapter of the manual.&lt;/p&gt;
&lt;p&gt;The binlog size growth with ROW format is documented behavior: the MySQL documentation notes that ROW format generates more log data for statements that modify many rows, particularly for bulk DELETE, UPDATE, and INSERT…SELECT operations. The practical implication is that teams migrating from STATEMENT to ROW should audit their batch operations and ensure binlog retention and disk capacity accounts for the larger volume.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;STATEMENT with non-deterministic functions&lt;/td&gt;&lt;td&gt;Replica silently diverges from primary&lt;/td&gt;&lt;td&gt;Different values for UUID, RAND, NOW on re-execution&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ROW format with bulk multi-row operations&lt;/td&gt;&lt;td&gt;Binlog grows very large; replication bandwidth spikes&lt;/td&gt;&lt;td&gt;One row image written per changed row&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MIXED with complex stored procedures or triggers&lt;/td&gt;&lt;td&gt;Unsafe pattern not detected; falls back to STATEMENT&lt;/td&gt;&lt;td&gt;MySQL’s unsafe-detection does not cover all trigger and procedure edge cases&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: STATEMENT format silently breaks replica consistency when any non-deterministic function appears in DML, and the divergence is committed before the error is visible.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Set &lt;code&gt;binlog_format = ROW&lt;/code&gt; in the MySQL configuration for all production servers; MySQL 8.0 defaults to this already.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Check &lt;code&gt;SELECT @@binlog_format&lt;/code&gt; on all replicas and the primary; run SHOW REPLICA STATUS and verify &lt;code&gt;Seconds_Behind_Source&lt;/code&gt; stays near zero after the format change.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, run &lt;code&gt;SELECT @@binlog_format&lt;/code&gt; on every MySQL instance in production. For any instance running STATEMENT or MIXED, review whether non-deterministic functions appear in the application’s DML patterns before the next major version upgrade.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;ROW format is not a performance optimization — it is a correctness requirement for any workload that uses non-deterministic SQL. The binlog size cost is real but manageable. Replica divergence is not.&lt;/p&gt;</content:encoded><category>databases</category></item><item><title>GCP Multi-Region Architecture: Global Load Balancing, Spanner, Pub/Sub, and Failure Testing</title><link>https://rajivonai.com/blog/2023-05-21-gcp-multi-region-architecture-global-load-balancing-spanner-pub-sub-and-failure-testing/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-05-21-gcp-multi-region-architecture-global-load-balancing-spanner-pub-sub-and-failure-testing/</guid><description>Control plane coupling, Spanner split boundaries, and untested Pub/Sub failover are why GCP multi-region architectures break before the region goes dark.</description><pubDate>Sun, 21 May 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A multi-region architecture does not fail when a region goes dark; it fails earlier, when the control plane, data model, and test discipline quietly assume the region will never go dark.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Cloud teams move to multi-region GCP for predictable reasons: lower user latency, higher availability targets, regulatory placement, and protection from regional incidents. The default architecture often starts cleanly: Cloud Load Balancing in front, stateless services on GKE or Cloud Run, Cloud Spanner for globally replicated state, Pub/Sub for asynchronous work, and Cloud Monitoring for visibility.&lt;/p&gt;
&lt;p&gt;That design is directionally right. It uses managed primitives that were built for global systems. Google’s external HTTP load balancer is a global entry point. Spanner provides synchronous replication with strong consistency across configured replicas. Pub/Sub decouples request paths from background processing and supports replay-oriented recovery patterns.&lt;/p&gt;
&lt;p&gt;The operational question is not whether these services can run across regions. They can. The question is whether the application, deployment system, and failure tests agree on what “multi-region” actually means.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Most failed multi-region designs are not missing regions. They are missing decision boundaries.&lt;/p&gt;
&lt;p&gt;A global load balancer can route around an unhealthy backend, but only if the health check represents real service health. A backend that returns &lt;code&gt;200&lt;/code&gt; while its regional Spanner access path is saturated is not healthy. A service that accepts writes but cannot publish required events is not healthy. A cache that serves stale entitlement data may look fast while violating business correctness.&lt;/p&gt;
&lt;p&gt;Spanner can replicate data across regions, but it does not remove the cost of coordination. Strong consistency is useful because it gives the application a clear correctness contract. It also means write latency, leader placement, schema design, and transaction shape become architectural concerns. A careless transaction that spans user profile, billing state, and workflow history may work in one region and become expensive under global replication.&lt;/p&gt;
&lt;p&gt;Pub/Sub can absorb spikes and help recover work, but it changes the failure mode. Instead of a synchronous request failing visibly, work may queue, retry, duplicate, or arrive later than the caller expects. That is a better failure mode only when handlers are idempotent, ordering assumptions are explicit, and backlog age is treated as production health.&lt;/p&gt;
&lt;p&gt;The core question: how do you design a GCP multi-region system that survives regional failure without pretending every dependency is equally global?&lt;/p&gt;
&lt;h2 id=&quot;a-control-plane-for-regional-failure&quot;&gt;A Control Plane for Regional Failure&lt;/h2&gt;
&lt;p&gt;The answer is to separate global routing, regional execution, globally consistent state, asynchronous work, and failure testing into different responsibilities.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  U[users — global traffic] --&gt; LB[global load balancer — policy and health]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  LB --&gt; R1[region one — stateless services]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  LB --&gt; R2[region two — stateless services]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  R1 --&gt; S[spanner — multi-region database]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  R2 --&gt; S&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  R1 --&gt; P[pubsub — durable event intake]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  R2 --&gt; P&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  P --&gt; W1[workers region one — idempotent handlers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  P --&gt; W2[workers region two — idempotent handlers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  T[failure tests — regional drills] --&gt; LB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  T --&gt; R1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  T --&gt; R2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  T --&gt; P&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  T --&gt; S&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  O[observability — user visible health] --&gt; LB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  O --&gt; R1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  O --&gt; R2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  O --&gt; P&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  O --&gt; S&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The global load balancer should make traffic decisions based on meaningful health. A shallow process check is insufficient. Health should include whether the service can reach its critical dependencies, whether it can complete a representative read path, and whether regional queues are within acceptable lag. Not every dependency belongs in every health check, but the check should match the promise the endpoint makes to users.&lt;/p&gt;
&lt;p&gt;Regional services should stay stateless where possible. If a regional instance disappears, another region should be able to serve new requests without local disk recovery, manual cache promotion, or hidden singleton ownership. Session state, workflow state, and idempotency records belong in durable stores, not inside regional processes.&lt;/p&gt;
&lt;p&gt;Spanner should hold state that truly requires strong consistency: account balances, ownership, entitlements, inventory, global uniqueness, and workflow state machines. The schema should reflect access patterns. Keep write transactions narrow. Avoid cross-entity transactions unless the invariant demands them. Choose leader placement deliberately because it affects write latency. Multi-region Spanner is not a latency eraser; it is a consistency system with explicit topology.&lt;/p&gt;
&lt;p&gt;Pub/Sub should carry work that can be retried safely: email delivery, projection updates, audit fanout, search indexing, billing workflow steps, and integration calls. Consumers should use stable idempotency keys. Message handlers should tolerate duplicate delivery. Backlog age, dead-letter volume, and retry rate should be first-class service indicators.&lt;/p&gt;
&lt;p&gt;The architecture also needs a small but explicit operational control plane. That can be a runbook, an internal tool, or automated policy, but the decisions must be named: drain region, disable writes for a path, pause consumers, replay subscription, promote read-only mode, or fail closed for a sensitive operation.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Google published Spanner as a globally distributed database providing externally consistent transactions across replicated data. The documented pattern is not “put every query in a global transaction.” The pattern is to use strong consistency where the business invariant needs it and to understand that replication topology affects latency and availability behavior.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; In a GCP architecture, place Spanner behind service APIs that own transaction boundaries. Do not let every caller compose arbitrary cross-table writes. Keep the transactional surface narrow: one aggregate, one workflow transition, one ownership decision. Use asynchronous Pub/Sub fanout for derived state.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The system has a smaller correctness core. Regional services can fail over without also moving hidden state. Pub/Sub consumers can rebuild projections after interruption. Spanner remains responsible for authoritative state, not every operational side effect.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Multi-region reliability improves when strong consistency and eventual completion are separated. Spanner is the authority for invariants. Pub/Sub is the recovery channel for work. The load balancer is the traffic decision point. Each has a different contract.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Google’s SRE material emphasizes testing reliability assumptions through controlled failure exercises and disaster recovery planning. The documented pattern is that availability is not only a design property; it is an operational practice.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Test regional failure before it is needed. Run drills that remove one regional backend from service, block a dependency from a region, pause a subscription, and inject latency into a critical path. Measure user-visible success rate, write latency, queue backlog age, and recovery time.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The team learns which failures are automatic and which require human judgment. A load balancer failover that works for reads may still expose write hot spots. A Pub/Sub backlog may drain cleanly in normal load and fail under catch-up pressure. A region may be removable only after a deployment dependency is made global.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Failure tests turn architecture diagrams into contracts. If a diagram says traffic can move from one region to another, the drill must prove it under realistic dependency behavior.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Area&lt;/th&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Load balancing&lt;/td&gt;&lt;td&gt;Health check passes while the service cannot complete real work&lt;/td&gt;&lt;td&gt;Use endpoint-specific health and synthetic transactions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Spanner&lt;/td&gt;&lt;td&gt;Global writes become slow because transactions are too broad&lt;/td&gt;&lt;td&gt;Model aggregates carefully and keep write paths narrow&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Pub/Sub&lt;/td&gt;&lt;td&gt;Duplicate or delayed messages corrupt derived state&lt;/td&gt;&lt;td&gt;Require idempotency keys and replay-safe consumers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Regional services&lt;/td&gt;&lt;td&gt;Local state prevents clean failover&lt;/td&gt;&lt;td&gt;Move durable state to Spanner or another managed store&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Deployment&lt;/td&gt;&lt;td&gt;A bad rollout reaches every region at once&lt;/td&gt;&lt;td&gt;Use staged regional rollout and fast rollback&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Observability&lt;/td&gt;&lt;td&gt;Metrics show infrastructure health but not user impact&lt;/td&gt;&lt;td&gt;Track success rate, latency, backlog age, and correctness signals&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Runbooks&lt;/td&gt;&lt;td&gt;Engineers know the design but not the emergency decisions&lt;/td&gt;&lt;td&gt;Predefine drain, pause, replay, and read-only procedures&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The architecture claims multi-region availability, but health checks, transaction boundaries, and recovery paths may still be regional assumptions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Put global load balancing at the edge, keep services stateless, use Spanner for authoritative invariants, use Pub/Sub for retryable work, and define explicit regional control actions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Validate the design with failure drills: drain a region, pause consumers, inject dependency latency, replay messages, and measure user-visible outcomes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before calling the system multi-region, write down the top five failure scenarios and run them in staging or production under controlled conditions. The architecture is not complete until the tests can fail honestly and recover predictably.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Database Backup Validation Workflow</title><link>https://rajivonai.com/blog/2023-05-15-database-backup-validation-workflow/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-05-15-database-backup-validation-workflow/</guid><description>A repeatable runbook for proving that your database backups are actually restorable — with exact commands, decision tree, and automation patterns.</description><pubDate>Mon, 15 May 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A backup that has never been restored is a hypothesis, not a safety net.&lt;/strong&gt; The job of a backup validation workflow is not to confirm that backup files exist — it is to prove that a recoverable database can be produced from them within your documented RTO, on demand, and on a schedule that keeps that proof fresh.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most teams reach a point where backup jobs are running nightly, retention windows are configured, and monitoring shows no failures. The backup checkbox is green. What is rarely true is that anyone has measured how long a restore actually takes, or whether the restored database is consistent enough to serve traffic.&lt;/p&gt;
&lt;p&gt;The gap between “backups are running” and “we can recover from backups” is where most recovery failures live. That gap expands silently: schema migrations add tables that the restore script does not verify, sequences drift out of sync, foreign key constraints that were dropped for a bulk load never get re-added, and PITR windows shrink as WAL archiving falls behind. None of these register as a backup failure. They register as a recovery failure — at 3am, under incident pressure, with customers waiting.&lt;/p&gt;
&lt;p&gt;This runbook operationalizes the difference. The goal is a weekly validation cycle that produces a measured RTO, a verified consistent restore, and documented PITR coverage — before you need any of them.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Signal&lt;/th&gt;&lt;th&gt;Where to see it&lt;/th&gt;&lt;th&gt;What it means&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;No documented restore time&lt;/td&gt;&lt;td&gt;Runbook or incident playbook&lt;/td&gt;&lt;td&gt;RTO is aspirational, not measured&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup job shows “succeeded” but restore has never been tested&lt;/td&gt;&lt;td&gt;CI logs, backup tool dashboard&lt;/td&gt;&lt;td&gt;File integrity is confirmed; recoverability is not&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup files exist but manifest or catalog is unverified&lt;/td&gt;&lt;td&gt;pg_dump output, S3 bucket listing&lt;/td&gt;&lt;td&gt;Partial or corrupt dump may silently pass a file-size check&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Last restore test was more than 90 days ago&lt;/td&gt;&lt;td&gt;Backup validation log, calendar&lt;/td&gt;&lt;td&gt;Schema and data drift since last test may invalidate assumptions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;RTO and RPO are in the SLA doc but not measured&lt;/td&gt;&lt;td&gt;SLA document, incident retrospectives&lt;/td&gt;&lt;td&gt;Numbers were estimated at design time and never validated&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;pg_stat_archiver shows gaps or lag&lt;/td&gt;&lt;td&gt;PostgreSQL system view&lt;/td&gt;&lt;td&gt;WAL archive is falling behind; PITR window is narrowing&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Verify backup file integrity&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For a PostgreSQL logical dump, verify the catalog without performing a full restore:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_restore&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --list&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; backup.dump&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /dev/null&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &amp;#x26;&amp;#x26; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;catalog OK&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;--list&lt;/code&gt; flag reads the table of contents from a custom-format dump. If the dump is corrupt or truncated, this fails immediately. A clean exit with “catalog OK” confirms the file is structurally valid. It does not confirm data integrity — that requires a restore.&lt;/p&gt;
&lt;p&gt;For Aurora RDS snapshots, check snapshot status and progress via the CLI:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; rds&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; describe-db-snapshots&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --db-instance-identifier&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; mydb&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --query&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;DBSnapshots[*].[DBSnapshotIdentifier,Status,PercentProgress]&apos;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --output&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Any snapshot not in &lt;code&gt;available&lt;/code&gt; status cannot be used for restore. The &lt;code&gt;PercentProgress&lt;/code&gt; field indicates whether an automated snapshot is still in progress.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Check backup age and frequency&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For PostgreSQL with WAL archiving, query the archiver process state:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; archived_count,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       last_archived_wal,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       last_archived_time,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       failed_count,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       last_failed_wal,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       last_failed_time,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       stats_reset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_archiver;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The documented behavior of &lt;code&gt;pg_stat_archiver&lt;/code&gt; (PostgreSQL documentation, §28.2) is that &lt;code&gt;last_archived_time&lt;/code&gt; reflects when the most recent WAL segment was successfully archived. A &lt;code&gt;failed_count&lt;/code&gt; greater than zero with a recent &lt;code&gt;last_failed_time&lt;/code&gt; means the archive pipeline is broken and your PITR window has stopped advancing. &lt;code&gt;archived_count&lt;/code&gt; resetting unexpectedly can indicate a statistics reset, not necessarily a problem — check &lt;code&gt;stats_reset&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For RDS, list recent snapshots with a date filter:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; rds&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; describe-db-snapshots&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --db-instance-identifier&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; mydb&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --query&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;DBSnapshots[?SnapshotCreateTime&gt;=`2023-05-08`].[DBSnapshotIdentifier,SnapshotCreateTime,Status]&apos;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --output&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Time a restore to a test instance&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Record the start time, execute the restore, and record the end time. This is your measured RTO. Do not estimate — measure:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RESTORE_START&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -u&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +%Y-%m-%dT%H:%M:%SZ&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Restore started: &lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$RESTORE_START&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# PostgreSQL logical restore to a test instance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_restore&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --host=test-db.internal&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --port=5432&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --username=restore_user&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --dbname=restore_target&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --verbose&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  backup.dump&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;RESTORE_END&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -u&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +%Y-%m-%dT%H:%M:%SZ&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Restore completed: &lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$RESTORE_END&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For Aurora, restore from a snapshot using the AWS CLI:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; rds&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; restore-db-instance-from-db-snapshot&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --db-instance-identifier&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; mydb-validation-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +%Y%m%d&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --db-snapshot-identifier&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; mydb-snapshot-id&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --db-instance-class&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; db.t3.medium&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --no-multi-az&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --no-publicly-accessible&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Log start and end times. The elapsed wall-clock time is your real RTO for this backup type and database size.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Verify data consistency post-restore&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Row counts on critical tables catch gross data loss. Sequence values confirm identity columns are in sync. Foreign key constraints confirm referential integrity was preserved:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Row counts on high-value tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; schemaname, tablename, n_live_tup&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; schemaname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;public&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_live_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Check current sequence values&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sequence_name, last_value&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;sequences&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; sequence_schema &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;public&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Verify foreign key constraints are present&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; conname, contype, conrelid::regclass &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; table_name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_constraint&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; contype &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;f&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The expected output is that row counts roughly match production (accounting for any lag), sequences are ahead of the maximum id values in their respective tables, and all foreign key constraints are present. A missing constraint row indicates the constraint was dropped and not re-added before the backup was taken.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Test point-in-time recovery&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For PostgreSQL, a PITR test restores to a target LSN or timestamp rather than the latest checkpoint. This verifies that WAL segments are intact and readable:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# In recovery.conf (Postgres 11 and earlier) or postgresql.conf (12+):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# recovery_target_time = &apos;2023-05-14 22:00:00 UTC&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# restore_command = &apos;cp /mnt/wal_archive/%f %p&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# For Aurora, restore to a point in time one hour before present:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aws&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; rds&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; restore-db-instance-to-point-in-time&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --source-db-instance-identifier&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; mydb&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --target-db-instance-identifier&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; mydb-pitr-validation-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +%Y%m%d&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --restore-time&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; $(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -u&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -v-1H&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +%Y-%m-%dT%H:%M:%SZ&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --db-instance-class&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; db.t3.medium&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --no-publicly-accessible&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The AWS Aurora PITR documentation specifies that the &lt;code&gt;--restore-time&lt;/code&gt; parameter accepts an ISO 8601 timestamp. The restored instance should come up in a consistent state at the target time. Verify by checking a table that had known writes in the hour before the target timestamp.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Backup exists in storage] --&gt; B{Integrity verified?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| C[Re-run backup — check for errors]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| D{Restore timed in last 30 days?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no| E[Run restore drill — record start and end time]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F{Measured RTO within SLA?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|no| G[Escalate — switch to physical backup or optimize]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt;|yes| H{Data consistency verified?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes| H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| I[Investigate — row counts, constraints, sequences]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| J{PITR tested in last 30 days?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|no| K[Run PITR drill — restore to timestamp minus 1 hour]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; L{PITR restore succeeded?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|no| M[Check WAL archive — review pg_stat_archiver]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|yes| N[Mark validation complete — log date and RTO]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|yes| N&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Option 1 — Switch from logical to physical backup for faster RTO&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PostgreSQL &lt;code&gt;pg_dump&lt;/code&gt; produces a portable logical backup but restore time scales with database size and is limited by the single-threaded restore process for custom-format dumps (parallel restore with &lt;code&gt;-j&lt;/code&gt; helps but still requires full data transfer). For large databases where RTO is failing its SLA target, switching to a physical backup method — &lt;code&gt;pg_basebackup&lt;/code&gt; for self-managed PostgreSQL, or Aurora snapshots which use storage-level cloning — typically reduces restore time significantly because physical restores do not need to re-execute every INSERT.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Physical base backup for self-managed PostgreSQL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_basebackup&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --host=primary.internal&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --username=replication_user&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --pgdata=/var/lib/postgresql/base_backup&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --format=tar&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --gzip&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --progress&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --wal-method=stream&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use when: logical restore times consistently exceed RTO targets and the database is large enough that parallel restore does not close the gap.&lt;/p&gt;
&lt;p&gt;Risk: physical backups are not portable across major PostgreSQL versions and require the same OS page size as the source.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 2 — Automate weekly restore drill to an isolated test instance&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Manual restore drills get deferred. An automated weekly drill that spins up a test instance, runs consistency checks, logs the RTO, and terminates the instance provides continuous validation without engineer attention. The pattern works for both self-managed PostgreSQL (via cron + pg_restore + psql checks) and Aurora (via AWS Lambda + EventBridge + the RDS API).&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Shell skeleton for a self-managed weekly drill&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;#!/bin/bash&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -euo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pipefail&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;BACKUP_FILE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/backups/latest.dump&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;TEST_HOST&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;test-restore.internal&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;LOG_FILE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;/var/log/backup_validation/$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +%Y%m%d).log&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;START&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +%s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_restore&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --host=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$TEST_HOST&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --dbname=restore_target&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$BACKUP_FILE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$LOG_FILE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; 2&gt;&amp;#x26;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; +%s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ELAPSED&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$((&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; START&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;RTO measured: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ELAPSED&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}s&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$LOG_FILE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;psql&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --host=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$TEST_HOST&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --dbname=restore_target&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  -c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;SELECT count(*) FROM pg_stat_user_tables;&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$LOG_FILE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;Validation complete: $(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -u&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$LOG_FILE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use when: restore drills are happening less than monthly, or the team wants evidence of RTO measurements for compliance purposes.&lt;/p&gt;
&lt;p&gt;Risk: the test instance must be isolated from production network paths to avoid accidental writes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 3 — Add catalog verification to CI/CD for schema migrations&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Schema migrations are the most common way a logical backup becomes silently unrestorable — a migration drops and re-creates a constraint, a sequence, or a table in a way that the backup catalog does not reflect. Adding &lt;code&gt;pg_restore --list&lt;/code&gt; verification as a post-migration CI check confirms that the dump catalog matches expected objects after every migration run.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# In CI pipeline, after migration:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_dump&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --format=custom&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --schema-only&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  --file=schema_backup.dump&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$DATABASE_URL&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_restore&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --list&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; schema_backup.dump&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -E&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;TABLE|SEQUENCE|CONSTRAINT&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; sort&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /tmp/current_objects.txt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Diff against expected objects baseline&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;diff&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /tmp/expected_objects.txt&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /tmp/current_objects.txt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use when: the team runs frequent migrations and wants early warning before a corrupt backup reaches the weekly restore drill.&lt;/p&gt;
&lt;p&gt;Risk: schema-only catalog verification does not catch data integrity issues — it only confirms structural completeness.&lt;/p&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;p&gt;The backup validation workflow is entirely read-only on production. All restore operations target isolated test instances. There is nothing to roll back from the validation process itself.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;If Option 1 (physical backup) causes issues&lt;/strong&gt;: The original logical backup schedule is unchanged. Run both in parallel for one validation cycle before cutting over. Revert by disabling the &lt;code&gt;pg_basebackup&lt;/code&gt; cron job and monitoring the next scheduled logical backup.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;If Option 2 (automated restore drill) causes unexpected resource usage&lt;/strong&gt;: The EventBridge or cron schedule can be disabled immediately. If a test instance was not terminated by the script, terminate it manually via &lt;code&gt;aws rds delete-db-instance --db-instance-identifier mydb-validation-YYYYMMDD --skip-final-snapshot&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;If Option 3 (CI catalog check) produces false positives after a migration&lt;/strong&gt;: Regenerate the &lt;code&gt;expected_objects.txt&lt;/code&gt; baseline from the current schema and commit it. The diff will be clean on the next run.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;The most impactful automation for this runbook is a weekly restore drill that requires no engineer involvement. The AWS pattern for Aurora uses EventBridge to trigger a Lambda function once per week. The Lambda calls &lt;code&gt;restore-db-instance-from-db-snapshot&lt;/code&gt; using the most recent available snapshot, polls the instance status until it reaches &lt;code&gt;available&lt;/code&gt;, runs row count checks via the RDS Data API or a temporary Lambda-to-RDS connection, logs the elapsed time and results to CloudWatch Logs, then calls &lt;code&gt;delete-db-instance&lt;/code&gt; to terminate the test instance.&lt;/p&gt;
&lt;p&gt;For a 100 GB Aurora database, the AWS RDS pricing documentation indicates that snapshot restore charges apply at the storage rate for the duration the instance is running. A validation instance that runs for two hours per week at &lt;code&gt;db.t3.medium&lt;/code&gt; pricing (on-demand) costs approximately $0.34 per week at current us-east-1 rates — less than the cost of one engineer-hour spent on a manual drill. The actual cost depends on instance class, storage provisioned, and region.&lt;/p&gt;
&lt;p&gt;For self-managed PostgreSQL, a pg_cron job or a systemd timer can trigger the shell skeleton from Option 2. The key instrumentation addition is writing the elapsed RTO and row count results to a table in a monitoring database so that trend data is available — a restore time that grows month over month as the database grows is a signal to revisit backup type before it breaches SLA.&lt;/p&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What broke&lt;/strong&gt;: Backup jobs were succeeding but restorability had never been tested, meaning the team’s documented RTO had no measured basis and recovery from a real incident would be slower and less certain than assumed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;What was done&lt;/strong&gt;: A validation workflow was implemented that measures actual restore time, verifies data consistency post-restore, and tests point-in-time recovery on a documented schedule.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;What prevents recurrence&lt;/strong&gt;: Automated weekly restore drills log measured RTO to a persistent store, and a CI catalog check flags schema migrations that would make a backup unrestorable before they reach production.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;checklist&quot;&gt;Checklist&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Verify backup file integrity using &lt;code&gt;pg_restore --list&lt;/code&gt; (PostgreSQL) or &lt;code&gt;aws rds describe-db-snapshots&lt;/code&gt; (Aurora) — confirm no errors before proceeding&lt;/li&gt;
&lt;li&gt;Check backup age: confirm the most recent backup is within the expected retention window and frequency&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;pg_stat_archiver&lt;/code&gt; and confirm &lt;code&gt;failed_count&lt;/code&gt; is zero and &lt;code&gt;last_archived_time&lt;/code&gt; is recent&lt;/li&gt;
&lt;li&gt;Run a timed restore to an isolated test instance and record wall-clock start and end times as the measured RTO&lt;/li&gt;
&lt;li&gt;Compare measured RTO against documented SLA target — escalate if over threshold&lt;/li&gt;
&lt;li&gt;Run row counts on the top 20 tables by size on the restored instance and compare to production baseline&lt;/li&gt;
&lt;li&gt;Verify sequence values are ahead of their respective table maximum id values&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;pg_constraint&lt;/code&gt; on the restored instance and confirm all expected foreign key constraints are present&lt;/li&gt;
&lt;li&gt;Run a PITR drill to a timestamp 1 hour before the current time — confirm the instance comes up and data at the target time is present&lt;/li&gt;
&lt;li&gt;Document the validation date, measured RTO, PITR result, and any anomalies in the validation log&lt;/li&gt;
&lt;li&gt;Set a calendar reminder or automate a trigger to repeat this cycle within 30 days&lt;/li&gt;
&lt;li&gt;If measured RTO exceeds SLA: open a ticket to evaluate physical backup method or restore parallelism before the next scheduled drill&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Backup jobs report success but the team has never measured actual restore time or verified data consistency — meaning the documented RTO is a guess and a real recovery event will be slower and less certain than expected.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Run a timed restore to an isolated test instance, verify row counts and foreign key constraints post-restore, and test PITR to a target timestamp — on a schedule that keeps the measurement fresh.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: A logged RTO that fits inside the SLA target, verified by wall-clock start and end times from the last restore drill, plus a confirmed PITR result within the last 30 days.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, run &lt;code&gt;pg_restore --list backup.dump&lt;/code&gt; (or &lt;code&gt;aws rds describe-db-snapshots&lt;/code&gt;) to verify your most recent backup file is structurally intact, then schedule the first timed restore drill if one has not been run in the past 30 days.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>checklist</category><category>failures</category></item><item><title>Scorecards: Turning Platform Standards Into Visible Engineering Debt</title><link>https://rajivonai.com/blog/2023-05-09-scorecards-turning-platform-standards-into-visible-engineering-debt/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-05-09-scorecards-turning-platform-standards-into-visible-engineering-debt/</guid><description>Scorecards turn platform standards into per-service debt that owners can see, dispute, and retire — the mechanism that makes wiki-page rules enforceable.</description><pubDate>Tue, 09 May 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Platform standards fail quietly when they live as wiki pages, and scorecards work when they turn those standards into debt that every owner can see, dispute, and retire.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Platform teams are being asked to scale engineering quality without scaling review meetings, ticket queues, and architecture boards. The usual standards are familiar: every service should have an owner, runbook, SLO, dependency update policy, supported runtime, deployment rollback path, telemetry baseline, and documented data classification. None of those controls are exotic. The hard part is keeping them true after the service count grows past what humans can inspect by hand.&lt;/p&gt;
&lt;p&gt;The older operating model treats standards as guidance. A platform team publishes templates, recommends CI checks, asks teams to adopt golden paths, and occasionally audits critical services. That works while the organization is small enough that social memory still carries the system map. Once there are hundreds of repositories, multiple deployment platforms, and several generations of frameworks, the standards become invisible. Teams do not know which services are out of policy. Leaders do not know whether the estate is improving. Platform engineers cannot tell whether their paved road is actually reducing risk.&lt;/p&gt;
&lt;p&gt;A scorecard changes the control surface. Instead of asking whether a team has read the standard, it asks whether there is evidence that the service currently meets it.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Most platform debt is not missing work. It is unpriced work.&lt;/p&gt;
&lt;p&gt;A service can be missing an owner annotation, running an unsupported runtime, lacking a rollback job, and shipping without dependency review, while still appearing healthy on the dashboard that matters to its product team. The defects are latent. They become visible only during an incident, migration, compliance review, or security response. By then, the platform team is no longer discussing standards. It is negotiating under time pressure.&lt;/p&gt;
&lt;p&gt;The common failure mode is to respond with more governance: mandatory review gates, manual spreadsheets, quarterly attestations, and broad policy documents. These mechanisms create the appearance of control while moving the evidence farther from the systems that produce it. A spreadsheet says a service has a runbook. CI knows whether the runbook link exists. The catalog knows whether the owner exists. The deployment system knows whether rollback is wired. The observability stack knows whether the SLO has traffic behind it.&lt;/p&gt;
&lt;p&gt;The question is: how do you make platform standards visible as engineering debt without turning the platform team into a permanent audit function?&lt;/p&gt;
&lt;h2 id=&quot;scorecards-as-a-debt-ledger&quot;&gt;Scorecards as a Debt Ledger&lt;/h2&gt;
&lt;p&gt;A platform scorecard is not a grade for teams. It is a continuously refreshed ledger of evidence about services. Each check maps one platform standard to one observable signal, one owner, one remediation path, and one exception policy.&lt;/p&gt;
&lt;p&gt;The architecture should start with the catalog, not the dashboard. A score without ownership is trivia. A failing check without a path to fix it is nagging. A standard without versioning is an argument waiting to happen.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;A[platform standards — versioned controls] --&gt; B[collectors — ci signals]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;A --&gt; C[collectors — runtime signals]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;A --&gt; D[collectors — catalog metadata]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;B --&gt; E[score engine — evidence and weights]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;C --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;D --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;E --&gt; F[team view — owned debt]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;E --&gt; G[leader view — risk trend]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;F --&gt; H[workflow — pull request task]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;G --&gt; I[planning — budget and exceptions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;H --&gt; J[remediation — standard path]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;I --&gt; J&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;J --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The design has five parts.&lt;/p&gt;
&lt;p&gt;First, define controls as code. A control should state what is being measured, why it matters, where evidence comes from, how it is scored, and what counts as an accepted exception. “Has observability” is too vague. “Service has a production dashboard link, alert route, and SLO identifier in catalog metadata” is testable.&lt;/p&gt;
&lt;p&gt;Second, collect evidence from source systems. CI can report whether required jobs exist. The repository host can report branch protection and dependency policy. The catalog can report ownership, lifecycle, and system membership. Runtime platforms can report deployment frequency, rollback support, and supported base images. Observability systems can report SLO presence and alert routing.&lt;/p&gt;
&lt;p&gt;Third, separate facts from scoring. “This repository has no CODEOWNERS file” is a fact. “This service loses ten points” is policy. Keeping them separate lets teams dispute evidence without relitigating the standard.&lt;/p&gt;
&lt;p&gt;Fourth, expose scorecards where engineers work. A portal view is useful for browsing, but the real value comes from pull request annotations, backlog tickets, service pages, and migration dashboards. A scorecard should create the shortest possible path from red status to remediation.&lt;/p&gt;
&lt;p&gt;Fifth, treat exceptions as first-class records. Some services are frozen. Some are being decommissioned. Some cannot adopt a control until a shared platform capability lands. Exceptions should have owners, expiry dates, and reasons. Otherwise the scorecard becomes a permanent list of known false positives.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; The documented pattern behind modern scorecards already exists in three places. Backstage’s Software Catalog centers service metadata such as ownership and lifecycle, making it a practical base for connecting standards to components rather than repositories alone (&lt;a href=&quot;https://backstage.io/docs/features/software-catalog/&quot;&gt;Backstage Software Catalog&lt;/a&gt;). OpenSSF Scorecard applies automated checks to open source repositories and summarizes security posture from observable signals (&lt;a href=&quot;https://openssf.org/scorecard/&quot;&gt;OpenSSF Scorecard&lt;/a&gt;). Google’s SRE model uses SLOs and error budgets to make reliability risk explicit enough to guide release decisions (&lt;a href=&quot;https://sre.google/sre-book/service-level-objectives/&quot;&gt;Google SRE — Service Level Objectives&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The shared architectural move is to replace intent with evidence. Backstage-style catalogs establish what exists and who owns it. OpenSSF-style checks show how repository health can be assessed automatically. SRE-style budgets show how a technical signal becomes an operating mechanism when it has thresholds, consequences, and review loops.&lt;/p&gt;
&lt;p&gt;For an internal platform scorecard, that means a service should not receive credit because a team says it follows the deployment standard. It receives credit because the deployment pipeline exposes the rollback job, the catalog points to the owner and runbook, the runtime reports the supported image, and the observability system confirms the SLO identifier.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The output is not a single vanity score. It is a queryable map of debt. Platform teams can see which standards fail because teams have not adopted them, which fail because the paved road is incomplete, and which fail because the standard is poorly specified. Product teams can see what they own. Leadership can see whether risk is burning down or accumulating.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Scorecards are useful only when they preserve the link between signal, owner, and action. A scorecard that collapses everything into one number will be gamed. A scorecard that lists failures without remediation will be ignored. A scorecard that blocks delivery before trust is established will be routed around.&lt;/p&gt;
&lt;p&gt;The strongest implementation pattern is progressive enforcement. Start with visibility. Then add service-level objectives for remediation. Then apply gates only to narrow, high-confidence controls where false positives are rare and the remediation path is automated.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Engineering response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Vanity scoring&lt;/td&gt;&lt;td&gt;Teams optimize the number instead of reducing risk&lt;/td&gt;&lt;td&gt;Show check-level evidence and trend, not only totals&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;False positives&lt;/td&gt;&lt;td&gt;Signals are inferred from inconsistent repositories or metadata&lt;/td&gt;&lt;td&gt;Allow disputes, expose raw evidence, and fix collectors quickly&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unowned debt&lt;/td&gt;&lt;td&gt;Scores attach to repositories with no real accountable team&lt;/td&gt;&lt;td&gt;Make catalog ownership a prerequisite control&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Platform blame&lt;/td&gt;&lt;td&gt;Teams fail checks because the paved road is incomplete&lt;/td&gt;&lt;td&gt;Track platform-owned blockers separately from service-owned debt&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Frozen exceptions&lt;/td&gt;&lt;td&gt;Waivers never expire&lt;/td&gt;&lt;td&gt;Require owner, reason, and expiry for every exception&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Gate fatigue&lt;/td&gt;&lt;td&gt;CI blocks delivery for low-confidence controls&lt;/td&gt;&lt;td&gt;Use advisory mode before enforcement and gate only proven checks&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Control sprawl&lt;/td&gt;&lt;td&gt;Every stakeholder adds another check&lt;/td&gt;&lt;td&gt;Version standards and require a retirement path for obsolete checks&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The hardest tradeoff is weight. Weighted scores are attractive because they give leaders one number. They are dangerous because the weights imply a risk model the organization may not actually believe. A missing owner, missing SLO, and unsupported runtime are different kinds of risk. Summing them can hide the one failure that matters during an incident.&lt;/p&gt;
&lt;p&gt;A better default is tiered health: required, recommended, and contextual. Required controls represent minimum operational safety. Recommended controls represent platform maturity. Contextual controls apply only to certain service classes, such as internet-facing APIs, regulated data systems, or tier-zero dependencies.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Platform standards are usually written as policy, but engineering debt accumulates in systems. Start by listing the ten failures that hurt most during incidents, migrations, or security response.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Convert each standard into a versioned control with evidence source, owner mapping, remediation link, scoring rule, and exception policy. Build the first scorecard from signals the organization already trusts.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Validate the scorecard against known painful services. If it cannot explain existing platform risk, it is measuring convenience rather than debt.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Publish scorecards in advisory mode for one quarter, review false positives weekly, automate the top remediation paths, and enforce only the controls that have become boringly accurate.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>automation</category><category>platform</category><category>ci-cd</category></item><item><title>Logical Replication vs Physical Replication in PostgreSQL</title><link>https://rajivonai.com/blog/2023-05-08-logical-replication-vs-physical-replication/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-05-08-logical-replication-vs-physical-replication/</guid><description>Physical replication copies bytes; logical replication copies row changes — and confusing the two causes silent schema drift, sequence divergence, and failed zero-downtime upgrades.</description><pubDate>Mon, 08 May 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;PostgreSQL ships with two replication mechanisms that solve different problems, but they get confused often enough that teams use one where the other is required — and discover the difference during a failover.&lt;/strong&gt; Physical (streaming) replication is for high availability and read scaling. Logical replication is for selective data movement and zero-downtime major version upgrades. Using logical replication as a drop-in HA replacement leaves you with sequence values that have diverged, DDL changes that never arrived at the subscriber, and a schema state on the standby that does not match the primary.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most PostgreSQL deployments start with physical streaming replication. It works, it is simple to configure, and for HA purposes it does exactly what is needed: a replica that is continuously kept in sync and can be promoted in seconds if the primary fails.&lt;/p&gt;
&lt;p&gt;Logical replication was added in PostgreSQL 10 and extended significantly in each subsequent release. It has a specific purpose: moving a subset of data across PostgreSQL instances that may differ by major version, schema, or platform. The canonical use case is a zero-downtime major version upgrade — replicate from a PG14 primary to a PG15 target, validate, then promote.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Teams encounter confusion when they try to use logical replication for HA or try to use physical replication for version upgrades.&lt;/p&gt;
&lt;p&gt;The failure mode that hurts: an engineer sets up logical replication from a PG13 primary to a PG14 standby as the HA plan, does no DDL synchronization, runs several migrations over six months, and then fails over. The standby runs, but queries immediately fail because the schema is months out of date.&lt;/p&gt;
&lt;p&gt;How do we safely distinguish these mechanisms and use the right one for the right operational constraint?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph Physical Replication&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    P1[Primary — PG14] --&gt;|Raw WAL Bytes| S1[Standby — PG14]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    S1 -.-&gt;|Exact Clone| R1[Read Only Query]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    subgraph Logical Replication&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    P2[Publisher — PG14] --&gt;|Decoded Row Changes| S2[Subscriber — PG15]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    S2 -.-&gt;|Writeable Target| R2[Zero Downtime Upgrade]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What this diagram shows:&lt;/strong&gt; Physical replication sends raw WAL bytes to an exact binary copy of the primary that must run the same major PostgreSQL version and stays read-only. Logical replication decodes individual row changes and sends them to a subscriber that can run a different PostgreSQL version and accept writes — which is what enables zero-downtime major version upgrades.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Physical replication copies WAL byte-for-byte. The replica is a binary clone of the primary: same files, same transaction IDs, same system catalog. This means it requires the same PostgreSQL major version as the primary (minor version differences are allowed). It replicates everything — all databases, all tables, all sequences, system catalogs — because it is literally replaying the raw write-ahead log.&lt;/p&gt;
&lt;p&gt;Logical replication decodes WAL into row-level changes: INSERT, UPDATE, DELETE events per table. A publication on the primary defines which tables to send; a subscription on the target applies those changes. The target is a separate, writeable PostgreSQL instance — it can be a different major version, a different schema, or even a different Postgres fork.&lt;/p&gt;
&lt;p&gt;There are specific limitations of logical replication that dictate when it can be used:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;DDL is not replicated.&lt;/strong&gt; Schema changes executed on the publisher — &lt;code&gt;ALTER TABLE&lt;/code&gt;, &lt;code&gt;CREATE INDEX&lt;/code&gt;, &lt;code&gt;ADD COLUMN&lt;/code&gt; — are not sent to the subscriber. The subscriber’s schema must be managed separately. A column added on the primary will not exist on the subscriber, and the replication stream will fail when it encounters rows with that column.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sequences are not replicated.&lt;/strong&gt; Sequence state (the current counter) is not sent over logical replication. After promotion of a logical subscriber, all &lt;code&gt;SERIAL&lt;/code&gt; and &lt;code&gt;IDENTITY&lt;/code&gt; columns will restart from wherever the sequence was initialized on the subscriber — which may be far below the primary’s current value, causing primary key conflicts on first insert.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Large objects are excluded.&lt;/strong&gt; PostgreSQL logical replication does not support &lt;code&gt;pg_largeobject&lt;/code&gt; — any data stored via the large object interface is not sent.&lt;/p&gt;























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Property&lt;/th&gt;&lt;th&gt;Physical Replication&lt;/th&gt;&lt;th&gt;Logical Replication&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;WAL content&lt;/td&gt;&lt;td&gt;Raw bytes, page-level&lt;/td&gt;&lt;td&gt;Decoded row changes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Version requirement&lt;/td&gt;&lt;td&gt;Same PG major version&lt;/td&gt;&lt;td&gt;Cross-major-version capable&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Scope&lt;/td&gt;&lt;td&gt;Entire cluster&lt;/td&gt;&lt;td&gt;Per-table, per-publication&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DDL replicated&lt;/td&gt;&lt;td&gt;Yes (byte-for-byte)&lt;/td&gt;&lt;td&gt;No — must apply manually&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sequences replicated&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Large objects&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Subscriber writeable&lt;/td&gt;&lt;td&gt;No (hot standby read-only)&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Primary use case&lt;/td&gt;&lt;td&gt;HA, read replicas&lt;/td&gt;&lt;td&gt;Version upgrades, selective sync&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failover time&lt;/td&gt;&lt;td&gt;Seconds (promote standby)&lt;/td&gt;&lt;td&gt;Minutes (manual schema validation needed)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s streaming replication documentation (postgresql.org/docs/current/warm-standby.html) describes physical replication’s behavior: the standby continuously applies WAL records and can be promoted instantly because it shares the same timeline and transaction state as the primary.&lt;/p&gt;
&lt;p&gt;PostgreSQL’s logical replication documentation (postgresql.org/docs/current/logical-replication.html) documents the known limitations explicitly: “Only DML operations are replicated. Schema changes (DDL) are not replicated.” The documentation also notes that “sequences are not replicated” and recommends that operators who use logical replication for version upgrades must handle sequence advancement manually during the cutover.&lt;/p&gt;
&lt;p&gt;The documented pattern from the PostgreSQL logical replication documentation is that the initial table sync for a new subscription copies the current table contents as a snapshot — on large tables this can take hours, and replication lag accumulates during that window. Physical replication has no equivalent initial sync cost because it starts from a base backup and streams from there.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;
&lt;p&gt;The limitations of logical replication create operational risk if used incorrectly:&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;DDL on publisher not applied to subscriber&lt;/td&gt;&lt;td&gt;Replication stream errors when row data includes columns not present in subscriber schema; apply worker stops&lt;/td&gt;&lt;td&gt;Logical replication does not decode or forward DDL; subscriber schema must be kept in sync manually&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sequence values diverge after failover&lt;/td&gt;&lt;td&gt;First INSERT after promotion generates IDs that conflict with rows that existed on the former primary&lt;/td&gt;&lt;td&gt;Subscriber sequences were never updated; they restart from initialization value, not primary’s current value&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Initial snapshot for large tables&lt;/td&gt;&lt;td&gt;Replication lag grows during the hours-long initial sync; the subscriber cannot be used as an HA target during this window&lt;/td&gt;&lt;td&gt;Logical replication’s initial sync is a table-level snapshot copy, not a streaming catchup&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;For a zero-downtime major version upgrade, the sequence problem is solved by advancing the subscriber’s sequences past the primary’s current values before promotion. PostgreSQL’s &lt;code&gt;pg_upgrade&lt;/code&gt; documentation recommends scripting this using &lt;code&gt;setval()&lt;/code&gt; against each affected sequence immediately before the promotion cutover.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Teams treating logical replication as a drop-in HA mechanism get schema drift and sequence conflicts at promotion time — failover appears to succeed, then applications fail immediately.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Use physical streaming replication for HA; reserve logical replication for cross-version migration or selective data movement, and build explicit DDL sync and sequence advancement steps into the cutover runbook.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: After a logical replication setup, query &lt;code&gt;SELECT schemaname, tablename FROM information_schema.tables WHERE table_schema = &apos;public&apos;&lt;/code&gt; on both primary and subscriber and diff the results — schema parity must be verified before any promotion.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: If you have an existing logical replication setup intended for HA, audit it this week: list all DDL changes since the subscription was created and confirm each was applied on the subscriber.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category></item><item><title>GCP Database Cost Review: Cloud SQL, Spanner, Bigtable, Memorystore, and BigQuery</title><link>https://rajivonai.com/blog/2023-05-06-gcp-database-cost-review-cloud-sql-spanner-bigtable-memorystore-and-bigquery/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-05-06-gcp-database-cost-review-cloud-sql-spanner-bigtable-memorystore-and-bigquery/</guid><description>Cloud SQL, Spanner, Bigtable, Memorystore, and BigQuery each bill differently — cost overruns trace to applying the wrong model to the wrong workload.</description><pubDate>Sat, 06 May 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Database cost failures rarely start with a bad price sheet; they start when every workload gets treated like the same workload with a different product name.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most GCP database estates grow through local decisions. A team needs PostgreSQL semantics, so it provisions Cloud SQL. Another needs global consistency, so it evaluates Spanner. An ingestion path needs low-latency keyed writes, so Bigtable appears. Session state, locks, queues, and leaderboards find their way into Memorystore. Analytics lands in BigQuery because SQL over large data is operationally easier than running another warehouse.&lt;/p&gt;
&lt;p&gt;Each choice is defensible in isolation. The failure appears later, when finance reviews spend by SKU while engineering reasons by service. Those views do not line up. A Cloud SQL bill might be driven by provisioned HA capacity, storage growth, backups, and read replicas. A BigQuery bill might be driven by accidental full-table scans. A Bigtable bill might be mostly idle nodes kept online for peak traffic. A Memorystore bill might be memory reserved for data that should have expired. A Spanner bill might be the cost of buying global correctness for a workload that only needed regional isolation.&lt;/p&gt;
&lt;p&gt;The review has to start one layer above pricing. It has to ask what shape of state each workload actually owns.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The common anti-pattern is service-first cost review: list every database, sort by monthly spend, and ask owners to reduce it. That usually produces local optimizations: smaller instances, fewer replicas, cheaper storage, shorter retention, lower query frequency. Some of those help. Many transfer risk into latency, recovery, correctness, or operator toil.&lt;/p&gt;
&lt;p&gt;The more dangerous version is product substitution without workload analysis. Moving Cloud SQL to Spanner may replace vertical scaling pressure with distributed transaction cost. Moving BigQuery workloads into Bigtable may avoid scan charges but create operational read-path complexity. Moving hot reads into Memorystore may reduce database load while introducing cache stampede risk and silent memory bloat.&lt;/p&gt;
&lt;p&gt;The core question is not “which GCP database is cheapest?” The core question is: &lt;strong&gt;what workload contract are we paying for, and is the system using that contract enough to justify its cost?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;cost-control-is-a-workload-placement-architecture&quot;&gt;Cost Control Is a Workload Placement Architecture&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[Billing export — daily cost facts] --&gt; B[Workload taxonomy — latency and shape]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[Cloud SQL — relational steady state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; D[Spanner — global transactional state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; E[Bigtable — wide row access]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; F[Memorystore — hot ephemeral state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; G[BigQuery — analytical scans]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; H[Guardrails — sizing and retention]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt; I[Review loop — schema and access patterns]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Cloud SQL should be reviewed as managed relational capacity. The right questions are boring and important: is HA required for this environment, are read replicas serving production reads, are backups and point-in-time recovery aligned with the recovery objective, and is vertical scaling masking missing indexes or connection misuse? Cloud SQL cost is usually easiest to control when ownership is tight: one application boundary, explicit lifecycle, clear retention, measured connection pools, and query plans reviewed before scaling.&lt;/p&gt;
&lt;p&gt;Spanner should be reviewed as a correctness and distribution purchase. Its value is strongest when the workload needs horizontal scale, relational access, strong consistency, and multi-region behavior together. If the application does not need those properties, Spanner can become an expensive substitute for schema discipline. If it does need them, the review should focus on schema design, key distribution, transaction shape, and placement configuration rather than treating node cost as the only lever.&lt;/p&gt;
&lt;p&gt;Bigtable should be reviewed as a high-throughput keyed access system. It rewards predictable row-key design and punishes accidental hot spotting. Cost review is therefore inseparable from access review: row-key distribution, cluster sizing, storage class, replication, retention, and whether large analytical scans have leaked into an operational store.&lt;/p&gt;
&lt;p&gt;Memorystore should be reviewed as reserved memory for volatile performance. The key question is whether the data is truly hot, bounded, and disposable. If the answer is no, Redis becomes a memory-priced database with weaker durability assumptions than the application may realize. Expiration policy, max key cardinality, value size, and cache-miss behavior matter more than a generic “cache hit rate” dashboard.&lt;/p&gt;
&lt;p&gt;BigQuery should be reviewed as analytical execution over stored data. It is not just a database line item; it is a query behavior line item. Partitioning, clustering, materialized views, table expiration, reservations, query limits, and user-level attribution are cost controls. Google’s own BigQuery guidance emphasizes estimating and controlling query costs, including limiting bytes processed and analyzing billing data in BigQuery itself (&lt;a href=&quot;https://docs.cloud.google.com/bigquery/docs/best-practices-costs&quot;&gt;Google Cloud BigQuery cost practices&lt;/a&gt;).&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; The documented pattern across Google’s data systems is specialization, not a universal database. The Spanner paper describes a globally distributed database built for externally consistent transactions across datacenters (&lt;a href=&quot;https://research.google.com/archive/spanner-osdi2012.pdf&quot;&gt;Spanner OSDI 2012&lt;/a&gt;). The Bigtable paper describes a sparse, distributed, persistent sorted map for large-scale structured data (&lt;a href=&quot;https://research.google/pubs/pub27898&quot;&gt;Bigtable OSDI 2006&lt;/a&gt;). Dremel, the system behind BigQuery’s analytical model, was designed for interactive analysis over web-scale datasets (&lt;a href=&quot;https://research.google/pubs/pub36632&quot;&gt;Dremel paper&lt;/a&gt;). These are different contracts.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Treat every database review as a contract test. For each workload, write down the required latency, consistency, access pattern, retention period, recovery target, regionality, and failure behavior. Then map it to the cheapest service configuration that still satisfies those constraints. Cloud SQL gets query-plan and instance-rightsizing review. Spanner gets transaction and key-design review. Bigtable gets row-key and hot-spot review. Memorystore gets TTL and memory-bound review. BigQuery gets scan, partition, and attribution review.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The result is not a guaranteed lower bill from one setting change. The result is cost explainability. A Spanner line item can be defended because the system needs global transactions. A BigQuery spike can be traced to a query class or user group. A Bigtable increase can be tied to replication, node count, or access skew. A Memorystore increase can be tied to retained keys, larger values, or missing expiration. This turns cost review from negotiation into engineering evidence.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The durable pattern is that cost follows shape. Transactional cost follows isolation, availability, and write coordination. Wide-column cost follows node count, replication, and key distribution. Cache cost follows memory residency. Analytical cost follows scanned data and slot consumption. A mature architecture does not ask one database to be cheaper at doing the wrong job; it routes state to the service whose failure model matches the business contract.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Service&lt;/th&gt;&lt;th&gt;Cost failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Review lever&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Cloud SQL&lt;/td&gt;&lt;td&gt;Oversized always-on instances&lt;/td&gt;&lt;td&gt;Scaling used to compensate for missing indexes, excess connections, or unclear environment lifecycle&lt;/td&gt;&lt;td&gt;Query plans, connection pooling, rightsizing, retention, HA scope&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Spanner&lt;/td&gt;&lt;td&gt;Paying for global correctness without needing it&lt;/td&gt;&lt;td&gt;Workload needs relational scale but not multi-region consistency or distributed transactions&lt;/td&gt;&lt;td&gt;Regionality review, transaction boundaries, schema and key design&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bigtable&lt;/td&gt;&lt;td&gt;Idle or skewed cluster capacity&lt;/td&gt;&lt;td&gt;Nodes are sized for peak, hot keys reduce effective throughput, replication multiplies storage&lt;/td&gt;&lt;td&gt;Row-key distribution, autoscaling policy, replication review, TTL&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Memorystore&lt;/td&gt;&lt;td&gt;Memory becomes permanent storage&lt;/td&gt;&lt;td&gt;Keys lack TTLs, values grow, cache miss paths are unsafe, eviction policy is unclear&lt;/td&gt;&lt;td&gt;TTL contracts, key cardinality budgets, miss testing, value-size limits&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;BigQuery&lt;/td&gt;&lt;td&gt;Unbounded analytical scans&lt;/td&gt;&lt;td&gt;Users query raw wide tables, partitions are ignored, exploratory workloads lack limits&lt;/td&gt;&lt;td&gt;Partition filters, clustering, materialized views, reservations, query quotas&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Database spend is being reviewed after the architecture has already encoded access patterns, retention, and correctness requirements.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build a workload placement matrix before changing SKUs: latency, consistency, read shape, write shape, retention, recovery, regionality, and failure tolerance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Use billing export, query logs, database metrics, schema review, and documented system behavior from Cloud SQL, Spanner, Bigtable, Memorystore, and BigQuery to tie cost to workload shape.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; For the next review cycle, pick the top five database cost centers and write one contract per workload. If the contract does not justify the service configuration, change the architecture before shaving capacity.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>BigQuery as an Operational Analytics Boundary, Not an OLTP Escape Hatch</title><link>https://rajivonai.com/blog/2023-04-21-bigquery-as-an-operational-analytics-boundary-not-an-oltp-escape-hatch/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-04-21-bigquery-as-an-operational-analytics-boundary-not-an-oltp-escape-hatch/</guid><description>Slot contention and multi-second scan latency are the failure modes when BigQuery gets used as the transactional backend of a user-facing service.</description><pubDate>Fri, 21 Apr 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;BigQuery fails most often when teams ask it to be the thing it is explicitly not: the transactional system of record behind a user-facing workflow.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Cloud data warehouses have moved closer to production systems. BigQuery is serverless, scales storage and compute independently, supports streaming ingestion, materialized views, federated queries, scheduled queries, and BI workloads. That makes it tempting to collapse the boundary between operational storage and analytical storage.&lt;/p&gt;
&lt;p&gt;The pressure is understandable. Product teams want fresh operational dashboards. Finance wants usage and billing facts without waiting for nightly ETL. Support wants searchable customer history. Machine learning teams want feature extraction from the same events product engineers already emit. The latency expectation has shifted from “tomorrow morning” to “within minutes.”&lt;/p&gt;
&lt;p&gt;BigQuery can support that shift. It is very good at operational analytics: answering large analytical questions over recent and historical business events. But operational analytics is not the same thing as OLTP. The distinction is architectural, not semantic. If a user action depends on single-row mutation latency, transaction isolation, hot-key protection, or synchronous correctness, the workload belongs in an operational database first.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure starts with a shortcut: a team already lands product events in BigQuery, so a service starts querying BigQuery directly for user-visible state. At first the query is small. Then it joins more tables. Then a workflow writes corrections back. Then a support tool treats the warehouse as the source of truth. Eventually a request path that should have been bounded by a transactional store is now coupled to warehouse query planning, ingestion freshness, table partitioning, and analytical concurrency.&lt;/p&gt;
&lt;p&gt;This creates several operational failures.&lt;/p&gt;
&lt;p&gt;First, latency becomes probabilistic. Analytical engines optimize throughput and scan efficiency, not per-request tail latency. A query that is acceptable for an analyst can be unacceptable in an API path.&lt;/p&gt;
&lt;p&gt;Second, correctness becomes ambiguous. Streaming ingestion, batch loads, deduplication, late events, and backfills all have different freshness semantics. If an application reads BigQuery as if it were a current-state database, every delayed event becomes a product bug.&lt;/p&gt;
&lt;p&gt;Third, cost control moves into the serving path. A badly shaped query is no longer an expensive dashboard mistake; it is now an expensive production incident.&lt;/p&gt;
&lt;p&gt;Fourth, ownership blurs. Data teams optimize schemas for analytical access. Product teams need stable transactional invariants. When both groups share one physical system for different consistency models, neither group can change it safely.&lt;/p&gt;
&lt;p&gt;The core question is not “can BigQuery answer this query?” It is: where should the boundary sit between transactional truth and analytical reach?&lt;/p&gt;
&lt;h2 id=&quot;the-boundary-architecture&quot;&gt;The Boundary Architecture&lt;/h2&gt;
&lt;p&gt;The answer is to treat BigQuery as an operational analytics boundary: close enough to production to observe, explain, and aggregate operational behavior, but separated from the OLTP path that decides user-visible truth.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[application service — user request] --&gt; B[OLTP database — current state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A --&gt; C[event publisher — durable facts]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; D[change stream — committed mutations]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; E[stream buffer — ordered ingestion]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; F[transform layer — schema normalization]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; G[BigQuery — operational analytics]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; H[BI and investigations — aggregate answers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; I[derived tables — reporting products]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; J[cache or serving index — bounded reads]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; K[synchronous API response — transactional truth]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this architecture, the OLTP database owns current state. It may be PostgreSQL, MySQL, Spanner, SQL Server, DynamoDB, FoundationDB, or another transactional system, but its role is explicit: enforce invariants and serve the synchronous request path.&lt;/p&gt;
&lt;p&gt;Events and change streams cross the boundary. They represent facts that have already happened, not commands that must still decide correctness. BigQuery receives those facts through batch loads, streaming ingestion, Dataflow, Pub/Sub, Kafka, Datastream, or another ingestion mechanism. Transformation code turns operational records into analytical tables with stable partitioning, clustering, retention, and lineage.&lt;/p&gt;
&lt;p&gt;BigQuery then answers questions that are operationally important but not transactionally decisive: usage by customer, fraud review queues, billing reconciliation, product funnel regressions, support investigations, SLO burn analysis, and capacity planning.&lt;/p&gt;
&lt;p&gt;When BigQuery-derived results must influence production behavior, they should cross back through an explicit serving boundary. That usually means precomputing derived state into a cache, search index, feature store, or operational table with a clear freshness contract. The application reads the serving layer, not arbitrary warehouse queries.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Google’s own BigQuery documentation describes BigQuery as a serverless, highly scalable data warehouse for analytics, not as an OLTP database. Its documented strengths are large-scale SQL analytics, managed storage, and separation of compute from storage.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The architectural pattern is to keep request-time mutation and invariant enforcement in a transactional system, then replicate facts into BigQuery for analytical consumption. Google Cloud reference architectures commonly pair operational stores, Pub/Sub, Dataflow, Datastream, and BigQuery to separate serving state from analytical state.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The serving system can optimize for bounded reads, writes, indexes, transactions, and retries. BigQuery can optimize for partition pruning, columnar scans, aggregation, and historical analysis. Each side can fail differently without turning every dashboard delay into a checkout incident.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The boundary is useful because it forces teams to name freshness and correctness contracts. “The dashboard may lag by five minutes” is an analytics contract. “The user must not be charged twice” is an OLTP invariant. Those should not live in the same query path.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; BigQuery’s documented behavior includes quotas, limits, partitioning guidance, clustering guidance, streaming semantics, and query cost controls. Those are normal for an analytical warehouse. They are dangerous only when hidden inside synchronous product behavior.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Teams should model BigQuery tables as read-optimized analytical products. Partition by event time or ingestion time where appropriate. Cluster on high-selectivity analytical dimensions. Use scheduled queries, materialized views, or transformed tables for repeated access patterns. Keep ad hoc exploration away from user-facing paths.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Incidents become easier to localize. If ingestion is delayed, analytics freshness is degraded. If the OLTP database is unhealthy, product correctness is at risk. If a BigQuery query is too expensive, the blast radius is a reporting or investigation workflow, not the primary write path.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; BigQuery can be operationally critical without being operationally authoritative. That distinction lets teams take analytics seriously without turning the warehouse into a fragile replacement for a database.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;What happens&lt;/th&gt;&lt;th&gt;Better boundary&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;API reads BigQuery directly&lt;/td&gt;&lt;td&gt;Tail latency and query planning affect users&lt;/td&gt;&lt;td&gt;Precompute into a serving table or cache&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;BigQuery stores mutable current state&lt;/td&gt;&lt;td&gt;Corrections, deletes, and late events become application logic&lt;/td&gt;&lt;td&gt;Keep current state in OLTP and publish changes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Dashboards define business truth&lt;/td&gt;&lt;td&gt;Backfills change historical answers without ownership&lt;/td&gt;&lt;td&gt;Version metrics and document freshness&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Analysts query raw production-shaped tables&lt;/td&gt;&lt;td&gt;Schema changes break reports and investigations&lt;/td&gt;&lt;td&gt;Publish curated analytical tables&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Streaming is treated as synchronous&lt;/td&gt;&lt;td&gt;Missing recent rows look like product defects&lt;/td&gt;&lt;td&gt;Define freshness windows and late-arrival handling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cost is unmanaged&lt;/td&gt;&lt;td&gt;Repeated scans become production cost incidents&lt;/td&gt;&lt;td&gt;Partition, cluster, materialize, and cap workloads&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The main tradeoff is duplication. You now have operational data in one place and analytical data in another. That is not accidental complexity; it is the cost of preserving different correctness models. The alternative is pretending one system can simultaneously optimize for transactions, ad hoc analytics, historical reconstruction, and low-latency serving.&lt;/p&gt;
&lt;p&gt;Another tradeoff is governance. Once BigQuery becomes the analytical boundary, schemas become contracts. Teams need owners for event definitions, retention, partition strategy, backfill rules, and metric semantics. Without that discipline, the warehouse becomes a lake of plausible but contradictory answers.&lt;/p&gt;
&lt;p&gt;The final tradeoff is latency. Some decisions require immediate state. Others tolerate minutes. Architecture improves when teams stop calling both of them “real time” and write down the actual tolerance.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Identify every production path that reads BigQuery synchronously. Classify each read as user-visible, operator-visible, or analytical.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Move user-visible reads behind an OLTP database, cache, search index, or serving table with explicit freshness and retry behavior.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Verify that BigQuery delays, failed scheduled queries, expensive scans, and backfills cannot corrupt transactional state or block primary user workflows.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Publish a boundary contract: OLTP owns current truth; BigQuery owns operational analytics; derived serving stores must declare freshness, lineage, and fallback behavior.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Read Replicas Are Not Free Scale</title><link>https://rajivonai.com/blog/2023-04-17-read-replicas-are-not-free-scale/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-04-17-read-replicas-are-not-free-scale/</guid><description>Read replicas add read throughput but they do not reduce write load, do not eliminate replication lag, and silently serve stale data under write bursts — understanding those constraints before you add replicas is the decision engineers skip.</description><pubDate>Mon, 17 Apr 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Adding a read replica is often the first instinct when a database is under load — and it often makes things worse in ways that take weeks to surface.&lt;/strong&gt; Replicas do increase read throughput, but they do not reduce write pressure on the primary, do not guarantee consistent data, and the operational burden of managing lag, failover, and session consistency accumulates quietly until something breaks.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Read replicas are standard infrastructure in most relational deployments. AWS RDS, Aurora, Cloud SQL, and self-managed PostgreSQL and MySQL all support them. The pitch is straightforward: offload read traffic to replica nodes, keep the primary free for writes, scale horizontally without sharding.&lt;/p&gt;
&lt;p&gt;That pitch is accurate as far as it goes. The problem is what it leaves out.&lt;/p&gt;
&lt;p&gt;Engineers reach for replicas when they see high CPU or query latency on the primary. What this misses: replication is not free. Replicas consume resources on the primary for log shipping, introduce lag between writes and reads, and create an eventual-consistency model that most application code is not written to handle.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The silent failure mode: your application writes a record, then immediately reads it back, but the read lands on a replica that has not yet applied the write. No error is returned. The user sees stale data. This is the documented behavior of asynchronous replication — the bug is routing the read to a replica without accounting for the replication window.&lt;/p&gt;
&lt;p&gt;Under normal conditions, lag is milliseconds and rarely surfaces. Under a write burst — a batch import, a traffic spike, a schema migration — lag climbs to seconds or minutes. During that window, every read routed to a replica is potentially wrong.&lt;/p&gt;
&lt;p&gt;The core question: which reads are safe to serve from a replica, and how do you verify that the replica is current enough to answer them?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App[Application Client] --&gt;|1. Write Record| Primary[Primary Database Node]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Primary --&gt;|2. Ship WAL Asynchronously| Replica[Read Replica Node]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    App --&gt;|3. Immediate Read| Replica&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Replica --&gt;|4. Returns Stale Data| App&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Replication lag is the delay between a commit on the primary and that commit being visible on a replica. How large the window gets — and what you can do about it — depends on the model.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;PostgreSQL streaming replication&lt;/strong&gt; is asynchronous by default. The primary commits before the replica confirms receipt or apply. &lt;code&gt;pg_stat_replication&lt;/code&gt; exposes &lt;code&gt;write_lag&lt;/code&gt;, &lt;code&gt;flush_lag&lt;/code&gt;, and &lt;code&gt;replay_lag&lt;/code&gt;. Under write load, replay lag dominates; the WAL apply process is fundamentally single-threaded for physical streaming replication.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;MySQL Group Replication&lt;/strong&gt; offers synchronous and semi-synchronous modes. Semi-synchronous (the default) confirms receipt but not apply — lag persists at the relay log. Fully synchronous mode blocks the primary commit until a replica confirms receipt, which reduces read lag at the cost of write latency (MySQL 8.0 Reference Manual, Group Replication).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Aurora&lt;/strong&gt; uses shared distributed storage rather than WAL shipping, so replicas observe page mutations directly. AWS documentation cites typical lag below 10 ms. Faster than streaming replication, but the session consistency problem remains: reads routed to the Aurora reader endpoint immediately after a write can still miss it.&lt;/p&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Replication model&lt;/th&gt;&lt;th&gt;Lag driver&lt;/th&gt;&lt;th&gt;Session consistency risk&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;PostgreSQL streaming (async)&lt;/td&gt;&lt;td&gt;WAL ship and replay&lt;/td&gt;&lt;td&gt;Yes — read can land before write applies&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MySQL semi-synchronous&lt;/td&gt;&lt;td&gt;Binlog receipt confirmed; apply async&lt;/td&gt;&lt;td&gt;Yes — same apply lag pattern&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MySQL Group Replication (sync)&lt;/td&gt;&lt;td&gt;Commit blocked until majority confirms receipt&lt;/td&gt;&lt;td&gt;Reduced but not eliminated&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aurora read replicas&lt;/td&gt;&lt;td&gt;Storage page propagation — sub-10 ms&lt;/td&gt;&lt;td&gt;Yes — writer endpoint required for read-after-write&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s &lt;code&gt;pg_stat_replication.replay_lag&lt;/code&gt; can grow unbounded under write load — including during heavy &lt;code&gt;COPY&lt;/code&gt; operations — because the WAL apply process cannot keep pace with the primary (PostgreSQL documentation, “Monitoring Replication”). The application has no visibility into this metric unless explicitly instrumented.&lt;/p&gt;
&lt;p&gt;AWS documentation on Aurora Replicas explicitly recommends the writer endpoint for read-after-write consistency. Even sub-10 ms storage propagation creates a window where the reader endpoint can miss the most recent write. The shared storage architecture changes the lag mechanism but not the session consistency constraint.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Write burst&lt;/td&gt;&lt;td&gt;Reads return stale data silently&lt;/td&gt;&lt;td&gt;Replica apply process falls behind; no error surfaces to the client&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica promotion during failover&lt;/td&gt;&lt;td&gt;Writes fail for 30–120 seconds in streaming replication setups&lt;/td&gt;&lt;td&gt;Primary must be confirmed, DNS or proxy updated, and applications reconnected&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Session consistency violation&lt;/td&gt;&lt;td&gt;User writes then immediately reads stale data&lt;/td&gt;&lt;td&gt;Connection pooler routes the read to a replica before replication applies the write&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Routing reads to replicas without accounting for lag means applications silently return wrong answers during write bursts — no error, just stale data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Classify reads by consistency requirement before routing. Reads that must see the latest write go to the primary; reads that tolerate bounded staleness go to replicas, with lag monitored against that bound.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Query &lt;code&gt;pg_stat_replication.replay_lag&lt;/code&gt; on the primary (or &lt;code&gt;Seconds_Behind_Source&lt;/code&gt; in MySQL) during a write spike. If it exceeds your application’s staleness tolerance, replica routing is already producing silent correctness errors.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Audit your connection pooler or load balancer this week to confirm which queries reach replicas, then add a lag threshold alert — reject or redirect replica reads when lag exceeds your application’s tolerance.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The cost of replicas shows up in consistency, failover latency, and operational complexity — not on a throughput graph. That mismatch is why replica failures are hard to catch until they surface as user-visible data errors.&lt;/p&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>GCP E-Commerce Inventory Architecture: Spanner, Pub/Sub, Dataflow, and BigQuery</title><link>https://rajivonai.com/blog/2023-04-06-gcp-e-commerce-inventory-architecture-spanner-pub-sub-dataflow-and-bigquery/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-04-06-gcp-e-commerce-inventory-architecture-spanner-pub-sub-dataflow-and-bigquery/</guid><description>Spanner prevents inventory oversells under concurrent checkouts; Pub/Sub and Dataflow push stock events to BigQuery without blocking reservation writes.</description><pubDate>Thu, 06 Apr 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Overselling inventory is not a traffic problem; it is a truth problem disguised as a scaling problem.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;E-commerce inventory systems used to be dominated by synchronous request flows: product page reads stock, cart reserves stock, checkout decrements stock, warehouse systems reconcile later. That model works while the business is small enough for one database, one warehouse, and one operational clock.&lt;/p&gt;
&lt;p&gt;The failure arrives when inventory becomes multi-channel. A single SKU can be sold through the website, mobile app, marketplace integrations, customer support tooling, backorder workflows, promotions, and warehouse adjustments. Each channel wants low latency. Each channel also wants the right to say, with confidence, that an item can be sold.&lt;/p&gt;
&lt;p&gt;On Google Cloud, the natural architecture often reaches for Spanner, Pub/Sub, Dataflow, and BigQuery. Spanner becomes the transactional inventory system. Pub/Sub carries committed inventory events. Dataflow derives stream projections. BigQuery serves analytics, reconciliation, and planning.&lt;/p&gt;
&lt;p&gt;That stack can work well, but only if the ownership boundary is explicit. Spanner should not be “one more database in the pipeline.” It should be the system that decides whether inventory exists. Everything else should derive, distribute, or analyze that decision.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The common failure mode is treating inventory as a cacheable attribute instead of a ledgered constraint.&lt;/p&gt;
&lt;p&gt;A product detail page can tolerate stale stock counts. A merchandising dashboard can tolerate delayed aggregates. A warehouse forecast can tolerate batch correction. Checkout cannot tolerate ambiguity. If two customers attempt to buy the last unit of a SKU, only one transaction can win.&lt;/p&gt;
&lt;p&gt;Event-driven systems make this more subtle. Pub/Sub can move updates quickly, but messaging speed does not create transactional correctness. Dataflow can compute reliable stream results, but stream correctness is not the same as reservation correctness. BigQuery can expose powerful analytical views, but analytical truth is not operational authority.&lt;/p&gt;
&lt;p&gt;The architecture breaks when downstream projections are allowed to answer upstream questions. A search index says five units remain, a cached product page says three, BigQuery says seven, and the order service tries to reconcile the conflict after payment authorization. At that point the business is no longer choosing between consistency models. It is choosing between customer apologies, manual fulfillment work, and hidden financial leakage.&lt;/p&gt;
&lt;p&gt;The question is: how do you keep checkout strongly correct while still letting the rest of the commerce platform move asynchronously?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;The answer is to make inventory a ledger in Spanner and make every other system downstream of committed ledger mutations.&lt;/p&gt;
&lt;p&gt;The operational model has three tables: current inventory, reservations, and inventory movements. The checkout service writes through a Spanner transaction that verifies available quantity, creates a reservation, appends a movement record, and updates the current balance. If the transaction cannot prove availability, it fails before payment capture or order confirmation.&lt;/p&gt;
&lt;p&gt;Pub/Sub is not the authority. It is the distribution layer. After Spanner commits, an outbox table or Spanner change stream emits inventory mutations to Pub/Sub. Dataflow consumes those events to maintain read-optimized projections: product availability feeds, search index updates, alerting streams, warehouse deltas, and BigQuery fact tables.&lt;/p&gt;
&lt;p&gt;BigQuery is not asked whether an item can be sold. It is asked what happened, where drift is emerging, and which SKUs require operational attention.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Checkout[Checkout service — reserve inventory] --&gt; Spanner[Spanner inventory ledger — transactional authority]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Spanner --&gt; Current[Current inventory — committed balance]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Spanner --&gt; Reservations[Reservations — expiring holds]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Spanner --&gt; Movements[Inventory movements — immutable facts]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Spanner --&gt; ChangeStream[Spanner change stream — committed mutations]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  ChangeStream --&gt; PubSub[PubSub topic — inventory events]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  PubSub --&gt; Dataflow[Dataflow pipeline — derived projections]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Dataflow --&gt; Search[Search index — availability hints]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Dataflow --&gt; Cache[Product cache — read path acceleration]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Dataflow --&gt; BigQuery[BigQuery warehouse — analytics and reconciliation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  BigQuery --&gt; Ops[Operations dashboards — drift and planning]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This design separates decisions from distribution. The decision path is short, transactional, and owned by Spanner. The distribution path is elastic, asynchronous, and owned by event processing.&lt;/p&gt;
&lt;p&gt;A reservation should have an expiration timestamp and a state machine: pending, confirmed, released, expired. The expiration path must be idempotent because retries are normal in distributed systems. A release event for an already released reservation should not add stock twice. A confirmation event for an expired reservation should fail unless the checkout flow creates a new valid reservation.&lt;/p&gt;
&lt;p&gt;SKU partitioning also matters. A hot SKU during a flash sale can turn one logical product into a write hotspot. The usual mitigation is to model inventory at the right granularity: SKU, location, fulfillment pool, and sometimes allocation bucket. The goal is not to avoid contention entirely. The goal is to put contention exactly where the business requires serial decisions.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Google’s Spanner documentation describes external consistency as its strongest transaction guarantee, and the original Spanner paper explains how TrueTime supports globally ordered transactions. The documented pattern is that Spanner is appropriate when the system needs SQL transactions with strong consistency across distributed data, not merely high availability storage. See Google’s Spanner documentation on &lt;a href=&quot;https://cloud.google.com/spanner/docs/true-time-external-consistency&quot;&gt;TrueTime and external consistency&lt;/a&gt; and the Spanner OSDI paper, &lt;a href=&quot;https://research.google.com/archive/spanner-osdi2012.pdf&quot;&gt;“Spanner: Google’s Globally-Distributed Database”&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Put the inventory invariant inside Spanner transactions. The invariant is simple: available quantity cannot go below zero for the sellable unit being reserved. Write the reservation and movement record in the same transaction that changes the balance. Do not rely on a Pub/Sub consumer to repair oversell after checkout.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The system narrows its correctness boundary. If Spanner commits, the reservation exists and the ledger records why stock changed. If Spanner rejects the write, the order path has no ambiguous intermediate state to explain later.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Strong consistency should be spent where the business invariant lives. Most of the platform can be eventually consistent, but the moment that decides whether money can be accepted for scarce inventory should not be.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Pub/Sub documentation states that default delivery is at least once and that ordering requires explicit ordering keys. It also documents exactly-once delivery options with scope and subscriber requirements. See Google Cloud Pub/Sub docs on &lt;a href=&quot;https://docs.cloud.google.com/pubsub/docs/subscription-overview&quot;&gt;subscription behavior&lt;/a&gt;, &lt;a href=&quot;https://docs.cloud.google.com/pubsub/docs/ordering&quot;&gt;message ordering&lt;/a&gt;, and &lt;a href=&quot;https://cloud.google.com/pubsub/docs/exactly-once-delivery&quot;&gt;exactly-once delivery&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Treat Pub/Sub messages as repeatable notifications, not single-use commands. Give every inventory event a stable event ID, reservation ID, SKU, location, sequence, and committed timestamp. Consumers should deduplicate by event ID and update projections idempotently.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Redelivery becomes a normal case. Replaying the same event may refresh a projection, but it does not double-count inventory, duplicate a warehouse task, or corrupt an analytical aggregate.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Messaging guarantees do not remove the need for idempotent application semantics. The event contract must make duplicate handling boring.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Dataflow documentation describes exactly-once processing behavior and the constraints around timely records and streaming sources. See Google Cloud Dataflow’s documentation on &lt;a href=&quot;https://cloud.google.com/dataflow/docs/concepts/exactly-once&quot;&gt;exactly-once processing&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Use Dataflow for projections whose correctness is defined by event processing: availability feeds, low-stock alerts, BigQuery loads, and reconciliation streams. Keep checkout outside this path.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Stream processing can scale independently from the checkout transaction rate. If a Dataflow job lags, product pages may show conservative availability or temporarily hide stock, but confirmed orders remain correct.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Stream processors are excellent at deriving state from facts. They should not be the first place where scarce inventory is promised.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; BigQuery descends from Google’s Dremel architecture for interactive analysis of large read-only datasets, and Google’s Dremel papers describe the analytical model behind BigQuery’s scale. See &lt;a href=&quot;https://research.google/pubs/dremel-interactive-analysis-of-web-scale-datasets-2/&quot;&gt;“Dremel: Interactive Analysis of Web-Scale Datasets”&lt;/a&gt; and &lt;a href=&quot;https://research.google/pubs/dremel-a-decade-of-interactive-sql-analysis-at-web-scale/&quot;&gt;“Dremel: A Decade of Interactive SQL Analysis at Web Scale”&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Load inventory movements into BigQuery as facts, not mutable truth. Build reconciliation queries that compare Spanner balances, movement sums, warehouse adjustments, and order states.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; BigQuery becomes the place to find drift, not the place to authorize sales. Analysts can ask why inventory moved without adding latency or coupling to checkout.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Analytical systems should explain operational truth after the fact. They should not own the write path that creates it.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Hot SKU contention&lt;/td&gt;&lt;td&gt;Many buyers reserve the same scarce item at once&lt;/td&gt;&lt;td&gt;Partition by fulfillment pool, use explicit reservation limits, and accept serialization where correctness requires it&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Duplicate events&lt;/td&gt;&lt;td&gt;Pub/Sub redelivers or consumers retry after partial work&lt;/td&gt;&lt;td&gt;Use event IDs, idempotent writes, and projection checkpoints&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Stale product availability&lt;/td&gt;&lt;td&gt;Cache and search projections lag committed inventory&lt;/td&gt;&lt;td&gt;Show conservative states, expire cache aggressively, and re-check availability at checkout&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reservation leaks&lt;/td&gt;&lt;td&gt;Holds are created but never confirmed or released&lt;/td&gt;&lt;td&gt;Use expiration timestamps, scheduled cleanup, and state transition guards&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Analytics disagreement&lt;/td&gt;&lt;td&gt;BigQuery loads lag or late events arrive&lt;/td&gt;&lt;td&gt;Model event time and processing time separately, then reconcile with Spanner snapshots&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Warehouse drift&lt;/td&gt;&lt;td&gt;Physical counts diverge from system counts&lt;/td&gt;&lt;td&gt;Append adjustment movements rather than rewriting balances silently&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Checkout correctness fails when inventory is treated as a distributed cache value.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Put the sellable inventory invariant inside Spanner transactions and publish committed changes downstream.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Spanner provides the transactional consistency boundary, Pub/Sub distributes committed facts, Dataflow builds repeatable projections, and BigQuery explains history.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Start by defining the inventory ledger schema, reservation state machine, event ID contract, and reconciliation queries before optimizing the read path.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>PostgreSQL Connection Storm Runbook</title><link>https://rajivonai.com/blog/2023-04-03-postgresql-connection-storm-runbook/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-04-03-postgresql-connection-storm-runbook/</guid><description>Diagnosing and resolving connection exhaustion in PostgreSQL: too many clients, idle-in-transaction accumulation, and the case for connection pooling.</description><pubDate>Mon, 03 Apr 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;“Sorry, too many clients already” means PostgreSQL has rejected a connection before your application could run a single query.&lt;/strong&gt; Every connection to PostgreSQL is a forked OS process consuming memory — typically 5–10 MB of RAM per connection — so &lt;code&gt;max_connections&lt;/code&gt; is a hard ceiling that cannot be stretched without consequences. Once you hit it, the failure mode is not graceful degradation; it is hard rejection of new connections until existing ones close.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s process-per-connection architecture dates to a period when connection counts were measured in dozens, not thousands. Each connection forks a backend process, inherits a memory allocation, and holds that allocation for the duration of the connection regardless of whether a query is running. At 200 connections, this overhead is manageable. At 1,000 connections, PostgreSQL is spending more memory serving idle backends than it is serving active queries.&lt;/p&gt;
&lt;p&gt;The default &lt;code&gt;max_connections = 100&lt;/code&gt; reflects this constraint — it is not a conservative setting that exists to be raised. The PostgreSQL documentation explicitly notes that increasing &lt;code&gt;max_connections&lt;/code&gt; requires increasing &lt;code&gt;shared_buffers&lt;/code&gt; proportionally, and that the memory overhead of idle connections is real and measurable.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Connection storms occur in three patterns: application connection leaks (connections opened and never closed), pool exhaustion from too many services competing for the same pool, and deployments that spin up new application instances without shutting down old ones cleanly. The &lt;code&gt;idle in transaction&lt;/code&gt; state is particularly damaging because those connections are holding transactions open, which blocks vacuum and prevents transaction ID advancement.&lt;/p&gt;
&lt;p&gt;Without a centralized connection multiplexer, every new microservice or horizontal pod autoscaling event directly multiplies the active TCP connections to the database host. Eventually, the database runs out of available connection slots or OS memory, triggering catastrophic connection rejection. How do you scale application instances without proportionally scaling database connection overhead?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;The structural solution is to decouple application connection counts from PostgreSQL process counts using connection pooling, specifically PgBouncer in transaction mode, while implementing aggressive server-side transaction timeouts to prevent zombie state accumulation.&lt;/p&gt;
&lt;h3 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h3&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Signal&lt;/th&gt;&lt;th&gt;Where to see it&lt;/th&gt;&lt;th&gt;What it means&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Application errors: “sorry, too many clients already”&lt;/td&gt;&lt;td&gt;Application logs&lt;/td&gt;&lt;td&gt;&lt;code&gt;max_connections&lt;/code&gt; ceiling hit — no new connections possible&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;count(*)&lt;/code&gt; near &lt;code&gt;max_connections&lt;/code&gt; value&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_activity&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Connection headroom nearly exhausted&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High count of &lt;code&gt;idle in transaction&lt;/code&gt; state&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_activity&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Connections holding open transactions, blocking vacuum&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One client IP with &gt; 50 connections&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_activity&lt;/code&gt; grouped by &lt;code&gt;client_addr&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Connection leak on a specific application server&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;No PgBouncer or pgpool in the stack&lt;/td&gt;&lt;td&gt;Infrastructure review&lt;/td&gt;&lt;td&gt;Direct connection architecture that cannot scale safely&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Memory pressure on the PostgreSQL host&lt;/td&gt;&lt;td&gt;OS metrics&lt;/td&gt;&lt;td&gt;Each idle connection consuming 5–10 MB RAM&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Count connections by state&lt;/strong&gt; — get the distribution of active, idle, and idle-in-transaction connections:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; connection_count,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  max&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; state_change) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oldest_in_state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_backend_pid()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GROUP BY&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; connection_count &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;High &lt;code&gt;idle&lt;/code&gt; counts mean connections are staying open without doing work — a pooling problem. High &lt;code&gt;idle in transaction&lt;/code&gt; counts mean applications are opening transactions and not committing or rolling back — a connection leak or long-running operation pattern.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;Check the connection ceiling&lt;/strong&gt; — confirm max_connections and how close you are:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW max_connections;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; total_connections,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; setting::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_settings &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;max_connections&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; max_connections,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; setting::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_settings &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;max_connections&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pct_used&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Anything above 80% of &lt;code&gt;max_connections&lt;/code&gt; is operational risk. At 90%, connection failures are likely during traffic spikes. PostgreSQL reserves a small number of connections for superusers via &lt;code&gt;superuser_reserved_connections&lt;/code&gt; (default 3), so regular users lose access before the absolute ceiling.&lt;/p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;Count idle-in-transaction connections&lt;/strong&gt; — these are the most damaging:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idle_in_txn_count,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  max&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; xact_start) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; oldest_open_txn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;idle in transaction&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Any &lt;code&gt;oldest_open_txn&lt;/code&gt; value above 5 minutes should be treated as an incident. These connections are holding their transaction’s snapshot, preventing vacuum from advancing the horizon, and consuming a process slot doing nothing.&lt;/p&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;&lt;strong&gt;Connection distribution by client address&lt;/strong&gt; — identify connection hogs:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  client_addr,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  usename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; connections,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  sum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CASE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; WHEN&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;idle in transaction&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; THEN&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ELSE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; END&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idle_in_txn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_backend_pid()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;GROUP BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; client_addr, usename&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; connections &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A single application server holding 80 connections to PostgreSQL while a second server holds 2 is a strong signal of either a connection leak or misconfigured pool sizing on the first server.&lt;/p&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;&lt;strong&gt;Check for a connection pooler&lt;/strong&gt; — if there is no PgBouncer or pgpool in front of PostgreSQL, that is the fix:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Check whether PgBouncer is running on the standard port&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;nc&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -z&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; localhost&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 6432&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &amp;#x26;&amp;#x26; &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;PgBouncer present&quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; echo&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;No pooler on 6432&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Or check from the PostgreSQL side — poolers identify themselves&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; client_addr,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; application_name,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; application_name&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; ILIKE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;%pgbouncer%&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;   OR&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; application_name&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; ILIKE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;%pgpool%&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;GROUP&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; BY&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; client_addr,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; application_name&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If no pooler is present and connection counts are near the ceiling, adding PgBouncer in transaction mode is the fastest structural fix available. Nothing else will prevent recurrence under load.&lt;/p&gt;
&lt;h3 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h3&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Connections near max_connections] --&gt; B{idle in transaction count high?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| C[Set idle_in_transaction_session_timeout]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| D{idle connection count high?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes| E{Pooler in front of Postgres?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|no| F[Add PgBouncer in transaction mode]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|yes| G{Pool sized correctly?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|no| H[Reduce pool_size per service]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|yes| I{One client addr dominant?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|yes| J[Investigate connection leak on that host]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|no| K[Too many services — reduce direct connections]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no| L{Connection rate spiking?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|yes| M[Check deploy — new instances not closing old]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|no| N[Increase max_connections as last resort]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Option 1 — Add PgBouncer in transaction mode (fastest structural fix)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PgBouncer in transaction mode multiplexes many application connections onto a small number of PostgreSQL backend processes. A typical configuration allows 1,000 application connections to share 20 PostgreSQL connections if the average transaction is short.&lt;/p&gt;
&lt;p&gt;Install and configure PgBouncer with a minimal &lt;code&gt;pgbouncer.ini&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;ini&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;[databases]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;mydb&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;host&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=127.0.0.1 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;port&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=5432 &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;dbname&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;=mydb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;[pgbouncer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;listen_addr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 0.0.0.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;listen_port&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 6432&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;pool_mode&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = transaction&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;max_client_conn&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 1000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;default_pool_size&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 20&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;server_idle_timeout&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 600&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;log_connections&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;log_disconnections&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Application changes: point connection strings to PgBouncer’s port (6432) instead of PostgreSQL’s port (5432). This is the only change required at the application layer.&lt;/p&gt;
&lt;p&gt;Transaction mode has one constraint documented in the PgBouncer documentation: prepared statements tied to a specific backend do not survive across transactions in transaction mode. Applications using &lt;code&gt;PREPARE&lt;/code&gt; statements must either use the statement cache inside PgBouncer or be moved to session mode.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 2 — Set idle_in_transaction_session_timeout&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For immediate relief from accumulated &lt;code&gt;idle in transaction&lt;/code&gt; connections, set a server-side timeout:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Immediate change, no restart required&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SYSTEM&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idle_in_transaction_session_timeout &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;5min&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_reload_conf();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Verify it took effect&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW idle_in_transaction_session_timeout;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After reload, any session that stays in &lt;code&gt;idle in transaction&lt;/code&gt; state for more than 5 minutes will be automatically terminated by PostgreSQL. The application will see a connection error and must handle reconnection.&lt;/p&gt;
&lt;p&gt;This parameter was added in PostgreSQL 9.6. It does not affect sessions with actively running queries — only sessions that have an open transaction but are not executing SQL.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 3 — Increase max_connections (last resort)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Increasing &lt;code&gt;max_connections&lt;/code&gt; requires a PostgreSQL restart and must be paired with a proportional increase in memory:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Edit postgresql.conf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;max_connections&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 200&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# shared_buffers should be at least 128MB per 100 connections as a starting point&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;shared_buffers&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; 2GB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Restart required&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pg_ctl&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; restart&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -D&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/lib/postgresql/data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is the last resort because it treats the symptom — not enough connection slots — without addressing the underlying cause, which is direct connections rather than pooled connections. Each additional connection slot adds OS process overhead. The PostgreSQL wiki notes that raising &lt;code&gt;max_connections&lt;/code&gt; above 200 without a pooler in front rarely solves connection exhaustion; it only defers it.&lt;/p&gt;
&lt;h3 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;idle_in_transaction_session_timeout&lt;/code&gt;: Revert immediately with &lt;code&gt;ALTER SYSTEM SET idle_in_transaction_session_timeout = 0; SELECT pg_reload_conf();&lt;/code&gt; — zero disables the timeout. No restart required.&lt;/li&gt;
&lt;li&gt;PgBouncer addition: PgBouncer is a proxy; removing it means pointing application connection strings back to the direct PostgreSQL port. No PostgreSQL changes are needed. PgBouncer itself can be stopped or removed at any time.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;max_connections&lt;/code&gt; increase: Decreasing &lt;code&gt;max_connections&lt;/code&gt; requires a restart. Before decreasing, verify that active connections at the new lower limit will not be rejected. Query &lt;code&gt;SELECT count(*) FROM pg_stat_activity&lt;/code&gt; first to confirm actual utilization.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h3&gt;
&lt;p&gt;A Prometheus alert on &lt;code&gt;pg_stat_activity_count&lt;/code&gt; by state is the standard monitoring approach. If you do not have Prometheus, this pg_cron query captures connection utilization hourly for capacity planning:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; cron&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;schedule&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;connection-capacity-log&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;0 * * * *&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $$&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ops&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;connection_log&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (ts, total, idle, idle_in_txn, active, max_conn)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FILTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;idle&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FILTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;idle in transaction&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    count&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FILTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;active&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; setting::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_settings &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;max_connections&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_backend_pid();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$$);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Alert thresholds worth setting: &lt;code&gt;total &gt; 0.8 * max_connections&lt;/code&gt; for capacity warning, &lt;code&gt;idle_in_txn &gt; 10&lt;/code&gt; for transaction hygiene alert, &lt;code&gt;idle_in_txn&lt;/code&gt; with &lt;code&gt;age &gt; 5 minutes&lt;/code&gt; for immediate escalation.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The PgBouncer documentation describes transaction mode as suitable for any application that does not use session-level PostgreSQL features across transactions: advisory locks, &lt;code&gt;SET LOCAL&lt;/code&gt;, &lt;code&gt;LISTEN/NOTIFY&lt;/code&gt;, prepared statements in session scope, and temporary tables. For applications that do use these features, session mode provides pooling with fewer constraints but with lower connection multiplexing ratios.&lt;/p&gt;
&lt;p&gt;The documented pattern from the PostgreSQL documentation on &lt;code&gt;max_connections&lt;/code&gt; is that each additional connection adds approximately &lt;code&gt;400 bytes&lt;/code&gt; of shared memory overhead, plus the per-process allocation (typically 5–10 MB). The PostgreSQL wiki explicitly recommends that databases serving more than a few hundred concurrent application connections place a pooler in front rather than raising &lt;code&gt;max_connections&lt;/code&gt; beyond 200.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;PgBouncer transaction mode breaks application&lt;/td&gt;&lt;td&gt;Application uses prepared statements or &lt;code&gt;SET LOCAL&lt;/code&gt; across transactions&lt;/td&gt;&lt;td&gt;Switch specific pools to session mode; or migrate to &lt;code&gt;pg_prepared_statements&lt;/code&gt; cache&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;idle_in_transaction_session_timeout&lt;/code&gt; causes unexpected rollbacks&lt;/td&gt;&lt;td&gt;Application holds open transactions intentionally for long operations&lt;/td&gt;&lt;td&gt;Increase the timeout for those connections, or refactor to commit-per-batch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Increasing &lt;code&gt;max_connections&lt;/code&gt; causes OOM&lt;/td&gt;&lt;td&gt;New connection ceiling consumes available RAM&lt;/td&gt;&lt;td&gt;Reduce &lt;code&gt;max_connections&lt;/code&gt; and add PgBouncer instead&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;PgBouncer pool exhausted under burst load&lt;/td&gt;&lt;td&gt;&lt;code&gt;default_pool_size&lt;/code&gt; too small for concurrent query volume&lt;/td&gt;&lt;td&gt;Increase &lt;code&gt;default_pool_size&lt;/code&gt;; add read replicas for read traffic&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Application does not retry on connection error&lt;/td&gt;&lt;td&gt;&lt;code&gt;idle_in_transaction_session_timeout&lt;/code&gt; terminates and app crashes&lt;/td&gt;&lt;td&gt;Add connection retry logic with exponential backoff&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: PostgreSQL rejects connections hard when &lt;code&gt;max_connections&lt;/code&gt; is exhausted — no graceful degradation, just immediate errors for every new connection attempt.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Add PgBouncer in transaction mode between applications and PostgreSQL to multiplex application connections onto a small pool of PostgreSQL backends, and set &lt;code&gt;idle_in_transaction_session_timeout = &apos;5min&apos;&lt;/code&gt; to prevent zombie transactions from consuming connection slots.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: After adding PgBouncer, &lt;code&gt;SELECT count(*) FROM pg_stat_activity&lt;/code&gt; on the PostgreSQL side should show a small stable number (equal to &lt;code&gt;default_pool_size&lt;/code&gt;) regardless of how many application-side connections exist.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Run the connection-by-state query from Check 1 against your production database today. If &lt;code&gt;idle in transaction&lt;/code&gt; count exceeds 5, set &lt;code&gt;idle_in_transaction_session_timeout&lt;/code&gt; immediately — it requires only a config reload, not a restart.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;checklist&quot;&gt;Checklist&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Query &lt;code&gt;pg_stat_activity&lt;/code&gt; grouped by state to see total, idle, idle-in-transaction, and active counts&lt;/li&gt;
&lt;li&gt;Compare total connections to &lt;code&gt;max_connections&lt;/code&gt; — flag if &gt; 80% used&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;idle in transaction&lt;/code&gt; count and age of oldest open transaction&lt;/li&gt;
&lt;li&gt;Group connections by &lt;code&gt;client_addr&lt;/code&gt; to identify any single-host leak&lt;/li&gt;
&lt;li&gt;Confirm whether PgBouncer or pgpool is present and accepting connections&lt;/li&gt;
&lt;li&gt;If no pooler: install PgBouncer in transaction mode before the next traffic event&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;idle_in_transaction_session_timeout = &apos;5min&apos;&lt;/code&gt; and reload config&lt;/li&gt;
&lt;li&gt;Verify &lt;code&gt;pool_mode&lt;/code&gt; in PgBouncer config is &lt;code&gt;transaction&lt;/code&gt; for OLTP workloads&lt;/li&gt;
&lt;li&gt;Confirm application handles connection errors with retry logic&lt;/li&gt;
&lt;li&gt;Review &lt;code&gt;max_connections&lt;/code&gt; setting — resist raising it without adding a pooler&lt;/li&gt;
&lt;li&gt;Add a monitoring alert at 80% of &lt;code&gt;max_connections&lt;/code&gt; utilization&lt;/li&gt;
&lt;li&gt;Log connection counts hourly to build a capacity baseline for the next 30 days&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>checklist</category><category>failures</category></item><item><title>MongoDB WiredTiger Cache Internals: Eviction Pressure, OS Page Cache Interaction, and Working Set Sizing</title><link>https://rajivonai.com/blog/2023-03-13-mongodb-wiredtiger-cache-practical-basics/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-03-13-mongodb-wiredtiger-cache-practical-basics/</guid><description>WiredTiger&apos;s internal cache uses direct I/O and holds uncompressed B-Tree pages in a private memory pool separate from the OS page cache. Understanding its eviction thread architecture, reconciliation pipeline, and MVCC update chains is the difference between a MongoDB instance that absorbs working-set growth and one that silently enters foreground eviction — stalling application threads mid-operation.</description><pubDate>Mon, 13 Mar 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;WiredTiger does not use the Linux OS page cache. This is not a configuration option — it is a deliberate architectural decision. WiredTiger opens its data files with &lt;code&gt;O_DIRECT&lt;/code&gt;, bypassing the kernel buffer cache entirely, and manages its own memory pool: the WiredTiger internal cache. When engineers size MongoDB instances by disk footprint alone, they are measuring the compressed on-disk representation of data that WiredTiger will decompress to 2–4× its disk size when loaded into its internal cache. The mismatch between compressed disk size and uncompressed cache size is the root cause of the most common MongoDB memory misconfiguration: a server with 64 GB of RAM, a 20 GB dataset on disk, and WiredTiger eviction pressure severe enough to stall application threads — because the 20 GB dataset occupies 55 GB of uncompressed cache space.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;WiredTiger has been MongoDB’s default storage engine since version 3.2, replacing MMAPv1. The architectural shift from MMAPv1 to WiredTiger changed the memory model fundamentally: MMAPv1 used memory-mapped files and relied entirely on the OS page cache for buffering. WiredTiger inverts this — it uses &lt;code&gt;O_DIRECT&lt;/code&gt; I/O (bypassing OS page cache) and maintains its own internal cache, independent of and separate from the OS buffer pool.&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Storage Engine&lt;/th&gt;&lt;th&gt;Cache Layer&lt;/th&gt;&lt;th&gt;Compression&lt;/th&gt;&lt;th&gt;I/O Mode&lt;/th&gt;&lt;th&gt;Working Set Sizing Model&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;MMAPv1 (MongoDB ≤3.0)&lt;/td&gt;&lt;td&gt;OS page cache (kernel manages)&lt;/td&gt;&lt;td&gt;None — data stored uncompressed on disk&lt;/td&gt;&lt;td&gt;mmap — kernel handles read/write buffering&lt;/td&gt;&lt;td&gt;Size RAM to ≥ total uncompressed dataset&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;WiredTiger (MongoDB ≥3.2)&lt;/td&gt;&lt;td&gt;WT internal cache (WT manages directly)&lt;/td&gt;&lt;td&gt;Snappy/zlib/zstd on disk — uncompressed in cache&lt;/td&gt;&lt;td&gt;O_DIRECT — bypasses OS page cache for data files&lt;/td&gt;&lt;td&gt;Size WT cache to ≥ uncompressed working set&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;WiredTiger + OS page cache (both layers)&lt;/td&gt;&lt;td&gt;Both: WT cache for working set + OS cache for journal/oplog&lt;/td&gt;&lt;td&gt;WT: on-disk compression. OS: sees compressed blocks&lt;/td&gt;&lt;td&gt;WT data files: O_DIRECT. Journal: buffered I/O&lt;/td&gt;&lt;td&gt;WT cache for primary working set; OS cache for journal replay&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The default WiredTiger cache size is computed as: &lt;code&gt;max(256 MB, (totalRAM − 1 GB) × 0.5)&lt;/code&gt;. On a 64 GB server, that is 31.5 GB. MongoDB documentation explicitly states that this leaves approximately half of system RAM for the OS page cache — but because WiredTiger uses &lt;code&gt;O_DIRECT&lt;/code&gt; for its data files, the OS page cache primarily serves the journal (WiredTiger’s write-ahead log), oplog, and any memory-mapped files used by other processes. The OS page cache does not buffer WiredTiger data reads.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;WiredTiger’s eviction system attempts to keep the internal cache below a configurable high-water mark. When read and write pressure drives cache occupancy above that mark faster than background eviction threads can drain it, application threads are forced into foreground eviction: before completing their read or write operation, the application thread must evict one or more pages from the cache to make room. This foreground eviction is serialized with the application operation — the thread stalls until eviction completes.&lt;/p&gt;
&lt;p&gt;The diagnostic signature is characteristic and often misattributed: IOPS climbs, CPU remains flat, operation latency increases to 50–200ms on operations that normally complete in under 1ms, and adding indexes provides no relief because the index B-Tree pages themselves are not fitting in cache and are being evicted between accesses.&lt;/p&gt;
&lt;p&gt;The uncompressed-vs-compressed ratio makes the failure mode non-obvious until it is too late. Snappy compression, WiredTiger’s default, achieves approximately 2–4× compression on document data. A collection that occupies 15 GB on disk after Snappy compression holds 30–60 GB of uncompressed data in the WiredTiger cache when fully loaded. On a server with 31.5 GB of WiredTiger cache, a 15 GB dataset can still cause severe eviction pressure.&lt;/p&gt;
&lt;p&gt;How do you determine the uncompressed working set size, identify whether WiredTiger eviction is active, distinguish foreground from background eviction, and size the cache correctly before the failure mode appears in production latency?&lt;/p&gt;
&lt;h2 id=&quot;wiredtiger-cache-architecture-b-tree-pages-eviction-threads-and-mvcc-update-chains&quot;&gt;WiredTiger Cache Architecture: B-Tree Pages, Eviction Threads, and MVCC Update Chains&lt;/h2&gt;
&lt;p&gt;WiredTiger stores all data in B-Tree structures. Each collection and each index is stored as a separate B-Tree file. Understanding the in-memory representation of a B-Tree page explains both why the cache is larger than the disk representation and why eviction is expensive.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;B-Tree page layout in memory.&lt;/strong&gt; A WiredTiger B-Tree page has three states: clean (read from disk, not modified), dirty (modified, must be reconciled before eviction), and empty (evicted from cache). When a page is read from disk, WiredTiger decompresses it (Snappy/zlib) and stores the uncompressed &lt;code&gt;WT_PAGE&lt;/code&gt; structure in the cache. The &lt;code&gt;WT_PAGE&lt;/code&gt; structure includes the page header, the array of &lt;code&gt;WT_ROW&lt;/code&gt; entries (for row-store collections), and a pointer to the in-memory update list.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;MVCC update chains.&lt;/strong&gt; WiredTiger implements MVCC (Multi-Version Concurrency Control) at the storage engine level. When a document is updated, WiredTiger does not modify the existing page record in-place. Instead, it prepends a &lt;code&gt;WT_UPDATE&lt;/code&gt; structure to the update list for that row. The update list forms a singly-linked chain: each &lt;code&gt;WT_UPDATE&lt;/code&gt; contains the new value, the transaction ID that wrote it, and a pointer to the previous version. Reads traverse this chain to find the appropriate version for their read timestamp.&lt;/p&gt;
&lt;p&gt;This MVCC structure has a direct impact on cache size: a document updated 50 times between checkpoints may have a chain of 50 &lt;code&gt;WT_UPDATE&lt;/code&gt; structures held in cache simultaneously — all versions must be retained until they are no longer needed by any active read. High-update-rate workloads on frequently written documents accumulate longer update chains, increasing the effective memory footprint per document beyond the document size itself.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Eviction architecture: server thread, worker threads, and application threads.&lt;/strong&gt; WiredTiger eviction is managed by a hierarchy of threads:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Eviction Server Thread (1)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  └── Scans B-Trees for eviction candidates&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  └── Places candidates on eviction queue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  └── Monitors cache thresholds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Eviction Worker Threads (configurable, default 4)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  └── Pull pages from eviction queue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  └── Reconcile dirty pages (write dirty data to disk via checkpoint)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  └── Evict clean pages directly (no I/O required)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Application Threads (foreground eviction)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  └── Triggered when cache &gt; eviction_trigger (default 95%)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  └── Each application thread evicts pages before completing its operation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  └── Direct cause of latency spikes during severe cache pressure&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The eviction thresholds that govern this hierarchy:&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Threshold&lt;/th&gt;&lt;th&gt;Default&lt;/th&gt;&lt;th&gt;Meaning&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;eviction_target&lt;/code&gt;&lt;/td&gt;&lt;td&gt;80% of cache&lt;/td&gt;&lt;td&gt;Background eviction keeps cache below this level during normal operation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;eviction_trigger&lt;/code&gt;&lt;/td&gt;&lt;td&gt;95% of cache&lt;/td&gt;&lt;td&gt;Application thread eviction begins above this level&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;eviction_dirty_target&lt;/code&gt;&lt;/td&gt;&lt;td&gt;5% of cache&lt;/td&gt;&lt;td&gt;Background eviction keeps dirty pages below this fraction&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;eviction_dirty_trigger&lt;/code&gt;&lt;/td&gt;&lt;td&gt;20% of cache&lt;/td&gt;&lt;td&gt;Aggressive dirty page eviction begins above this fraction&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;eviction_updates_target&lt;/code&gt;&lt;/td&gt;&lt;td&gt;0%&lt;/td&gt;&lt;td&gt;Target for update list memory (0 = unbounded)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;When &lt;code&gt;eviction_trigger&lt;/code&gt; is breached, every application thread that needs to allocate cache space first participates in evicting one or more pages. At this point, operation latency is directly proportional to eviction cost (page size × reconciliation cost for dirty pages).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reconciliation — the dirty page write path.&lt;/strong&gt; Reconciling a dirty page is significantly more expensive than evicting a clean page. Reconciliation involves: consolidating all &lt;code&gt;WT_UPDATE&lt;/code&gt; entries on the page into the on-disk format, compressing the reconciled page (Snappy by default), and writing the compressed page to the data file via direct I/O. Only after reconciliation can the dirty page be evicted from cache. A workload with many dirty pages (high write rate, long checkpoint intervals) will see eviction worker threads spending the majority of their time on reconciliation — creating a backlog on the eviction queue and increasing the probability of foreground eviction.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Identifying cache pressure and eviction state.&lt;/strong&gt; The primary diagnostic is &lt;code&gt;db.serverStatus().wiredTiger.cache&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Paste this directly into mongosh — returns the cache state you need&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;serverStatus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().wiredTiger.cache;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; fill&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (c[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;bytes currently in the cache&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; c[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;maximum bytes configured&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; dirty&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (c[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;tracked dirty bytes in the cache&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; c[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;maximum bytes configured&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Cache fill:           ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;fill&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}%`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Dirty pages:          ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;dirty&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}% of max cache`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Foreground evictions: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pages evicted by application threads&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Background evictions: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pages evicted by the internal page eviction worker&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Read into cache:      ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pages read into cache&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Written from cache:   ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;pages written from cache&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Unmodified removed:   ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;unmodified pages evicted&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Modified removed:     ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;modified pages evicted&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The field &lt;code&gt;pages evicted by application threads&lt;/code&gt; is the single most actionable signal. A nonzero and growing value means foreground eviction is active. Any value above zero means application threads are being stalled for eviction. Cross-reference it against the &lt;code&gt;bytes currently in the cache&lt;/code&gt; fill ratio: if fill exceeds 90% and foreground eviction is occurring, the cache is undersized for the active working set.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Distinguishing WiredTiger cache misses from OS page cache misses.&lt;/strong&gt; Because WiredTiger uses &lt;code&gt;O_DIRECT&lt;/code&gt;, a WiredTiger cache miss results in a direct I/O read to the block device — bypassing the OS page cache entirely. This means &lt;code&gt;iostat&lt;/code&gt; shows real physical reads:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Run on the MongoDB server to correlate WT cache pressure with disk I/O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# -x = extended stats, -d = disk only, 1 = 1-second interval&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;iostat&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -xd&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; grep&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -E&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;^Device|$(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;lsblk&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -o&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; NAME,MOUNTPOINT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; awk&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;/\/var\/lib\/mongodb/{print $1}&apos;)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Key &lt;code&gt;iostat&lt;/code&gt; fields during WiredTiger cache miss pressure:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;r/s&lt;/code&gt; (reads per second) climbs from near-zero (working set fits in cache) to hundreds or thousands (cache misses driving disk reads)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rMB/s&lt;/code&gt; reveals the read bandwidth; compare against the device’s maximum sequential read bandwidth&lt;/li&gt;
&lt;li&gt;&lt;code&gt;r_await&lt;/code&gt; (average read latency in milliseconds) above 5ms for SSDs indicates queueing or device saturation&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For NVMe SSDs, WiredTiger’s &lt;code&gt;O_DIRECT&lt;/code&gt; reads achieve 200–800 µs per 4K page at low queue depth. At full eviction pressure with hundreds of concurrent application threads triggering foreground eviction, the I/O queue depth can saturate the NVMe device’s internal queue (typically 1024 commands), and &lt;code&gt;r_await&lt;/code&gt; climbs to 5–20ms per read — accounting for the observed 50–200ms operation latency when each operation triggers multiple page reads.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;eBPF tracing for WiredTiger I/O patterns (Linux 5.8+).&lt;/strong&gt; For deeper I/O analysis without application instrumentation:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Trace WiredTiger pread64 calls — shows actual page read sizes and latency&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Requires bpftrace on Linux 5.8+ with kernel headers installed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;bpftrace&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -e&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;tracepoint:syscalls:sys_enter_pread64 /comm == &quot;mongod&quot;/ {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    @start[tid] = nsecs;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    @size[tid] = args-&gt;count;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;tracepoint:syscalls:sys_exit_pread64 /comm == &quot;mongod&quot; &amp;#x26;&amp;#x26; @start[tid]/ {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    $lat_us = (nsecs - @start[tid]) / 1000;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    @read_latency_us = hist($lat_us);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    @read_size_bytes = hist(@size[tid]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    delete(@start[tid]); delete(@size[tid]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;interval:s:10 { print(@read_latency_us); print(@read_size_bytes); clear(@read_latency_us); clear(@read_size_bytes); }&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This traces every &lt;code&gt;pread64&lt;/code&gt; syscall from the &lt;code&gt;mongod&lt;/code&gt; process, capturing read latency (microseconds) and read size (bytes). WiredTiger reads pages in its configured page size (default 32KB for leaf pages, configurable via &lt;code&gt;leaf_page_max&lt;/code&gt;). A histogram dominated by 32KB reads with latency above 500µs indicates cache miss pressure reaching the disk layer.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Estimating uncompressed working set size.&lt;/strong&gt; Before sizing the WiredTiger cache, determine the uncompressed working set:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Get compression ratio for each collection in the working database&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getCollectionNames&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; stats&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getCollection&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(name).&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;stats&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({scale: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1024&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1024&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;stats.storageSize) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ratio&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stats.size &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; stats.storageSize;   &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// uncompressed / compressed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;stats&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;storageSize&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} MB compressed → `&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          `${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;stats&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} MB uncompressed `&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          `(${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ratio&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;toFixed&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}× expansion)`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;size&lt;/code&gt; field is the uncompressed data size (as if stored without compression); &lt;code&gt;storageSize&lt;/code&gt; is the on-disk compressed footprint. The ratio reveals the actual expansion factor for each collection. Sum the &lt;code&gt;size&lt;/code&gt; fields for all collections that are in the hot working set (accessed in the past 24–48 hours) — that sum is the minimum WiredTiger cache requirement.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;WiredTiger cache configuration.&lt;/strong&gt; Set the cache size to accommodate the uncompressed working set with 20% headroom:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# /etc/mongod.conf — WiredTiger cache configuration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;storage&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  dbPath&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;/var/lib/mongodb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;  wiredTiger&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    engineConfig&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      cacheSizeGB&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;48&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;          # Set to 60-70% of system RAM on dedicated MongoDB hosts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      journalCompressor&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;snappy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      directoryForIndexes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;   # Keep indexes in same WT files as collections&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    collectionConfig&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      blockCompressor&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;snappy&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;      # Default; zstd gives better compression at moderate CPU cost&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;    indexConfig&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#85E89D&quot;&gt;      prefixCompression&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;      # Reduces index disk footprint by 40-60% for many index types&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For dedicated MongoDB hosts, MongoDB’s official documentation recommends setting &lt;code&gt;cacheSizeGB&lt;/code&gt; to 60% of system RAM, leaving 40% for: the OS page cache (used by WiredTiger’s journal, oplog, and MongoDB’s diagnostic log), connection buffers (roughly 1 MB per connection × max connection count), sort operation memory, and system processes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;WiredTiger checkpoint and eviction interaction.&lt;/strong&gt; WiredTiger runs a checkpoint every 60 seconds by default (configurable via &lt;code&gt;checkpointDelaySecs&lt;/code&gt;). During checkpoint, WiredTiger writes all dirty pages to disk and advances the durable timestamp. Checkpoint is NOT the same as eviction — it writes dirty pages but does not remove them from cache. Eviction removes pages from cache (after reconciling if dirty). Understanding this distinction prevents a common misconfiguration: reducing checkpoint frequency to reduce I/O actually increases eviction cost, because it allows dirty pages to accumulate larger &lt;code&gt;WT_UPDATE&lt;/code&gt; chains before reconciliation, making each reconciliation more expensive.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Monitoring with Atlas metrics or Prometheus.&lt;/strong&gt; For Atlas deployments, the relevant metrics in the Atlas monitoring dashboard:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;# Atlas metric names for WiredTiger cache monitoring&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;CACHE_USED_BYTES                    # Current cache bytes in use&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;CACHE_DIRTY_BYTES                   # Dirty bytes in cache&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;TICKETS_AVAILABLE_READS             # Available read tickets (128 default) — saturation = queuing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;TICKETS_AVAILABLE_WRITES            # Available write tickets (128 default)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;# For self-managed with mongodb_exporter (Prometheus):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;mongodb_ss_wiredTiger_cache_bytes_currently_in_cache&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;mongodb_ss_wiredTiger_cache_maximum_bytes_configured&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;mongodb_ss_wiredTiger_cache_pages_evicted_by_application_threads_total&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;mongodb_ss_wiredTiger_cache_tracked_dirty_bytes_in_cache&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;PromQL alert for foreground eviction:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;# Alert if foreground eviction rate exceeds 10 pages/second over 5 minutes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;rate(mongodb_ss_wiredTiger_cache_pages_evicted_by_application_threads_total[5m]) &gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Root Cause&lt;/th&gt;&lt;th&gt;Production Blast Radius&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Foreground eviction stalls application threads&lt;/td&gt;&lt;td&gt;Cache fill exceeds &lt;code&gt;eviction_trigger&lt;/code&gt; (95% default); background eviction cannot drain cache fast enough; application threads participate in eviction before completing read/write&lt;/td&gt;&lt;td&gt;All operations experience added latency proportional to page eviction cost (10–100ms per eviction cycle at disk speeds); at high concurrency, the effect compounds — 100 threads each stalling 50ms = 5,000 ms-thread-seconds of latency added per second&lt;/td&gt;&lt;td&gt;Increase &lt;code&gt;cacheSizeGB&lt;/code&gt; to 60–70% of RAM; if cache is correctly sized, check &lt;code&gt;eviction_dirty_trigger&lt;/code&gt; — a workload with many small dirty pages may need &lt;code&gt;eviction_dirty_target&lt;/code&gt; reduced to 2% to trigger more aggressive background dirty page flush&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MVCC update chain accumulation — cache bloat from high-update documents&lt;/td&gt;&lt;td&gt;Frequently updated documents accumulate long &lt;code&gt;WT_UPDATE&lt;/code&gt; chains between checkpoints; each chain version is retained in cache until checkpoint advances the stable timestamp past the oldest reader&lt;/td&gt;&lt;td&gt;Cache appears undersized even though document count is small; &lt;code&gt;tracked dirty bytes in the cache&lt;/code&gt; grows faster than data volume&lt;/td&gt;&lt;td&gt;Ensure checkpoints complete within the 60-second interval; monitor &lt;code&gt;db.serverStatus().wiredTiger[&quot;transaction&quot;][&quot;transaction checkpoint max time (msecs)&quot;]&lt;/code&gt; — if checkpoint takes &gt;30 seconds, investigate dirty page accumulation; consider workloads that update many versions of the same document (time-series style) as candidates for WiredTiger’s native time-series collection type&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Analytics scan flooding cache — OLTP working set evicted&lt;/td&gt;&lt;td&gt;Full collection scan (aggregation, &lt;code&gt;$match&lt;/code&gt; without index) scans all pages of a large collection sequentially; these pages fill the cache and evict the OLTP hot pages that were previously resident&lt;/td&gt;&lt;td&gt;OLTP operation latency spikes during or after an analytics query; subsequent OLTP reads require cache re-warming from disk&lt;/td&gt;&lt;td&gt;Use &lt;code&gt;allowDiskUse: true&lt;/code&gt; for analytics aggregations and ensure the aggregation uses an index; consider routing analytics queries to a secondary with a dedicated read preference; configure &lt;code&gt;maxTimeMS&lt;/code&gt; to prevent unbounded analytics scans from running indefinitely&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Compression ratio underestimate — cache too small for working set&lt;/td&gt;&lt;td&gt;Team sizes cache based on compressed disk footprint (e.g., 20 GB on disk → assume 20 GB cache needed); actual uncompressed working set is 50 GB; cache is undersized by 2.5×&lt;/td&gt;&lt;td&gt;Constant eviction pressure from startup; no amount of tuning resolves it because the root cause is insufficient RAM&lt;/td&gt;&lt;td&gt;Run the collection-level uncompressed size query above before hardware provisioning; multiply &lt;code&gt;db.collection.stats().size&lt;/code&gt; (uncompressed) sum across the working set by 1.2 for safety headroom&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Index pages not fitting in cache&lt;/td&gt;&lt;td&gt;B-Tree index pages for large indexes are evicted from cache between accesses; adding more indexes increases the total index working set beyond cache capacity&lt;/td&gt;&lt;td&gt;Index scans trigger disk reads for index pages before reaching document pages; IXSCAN operations paradoxically become slower than expected because the index itself is not cache-resident&lt;/td&gt;&lt;td&gt;Run &lt;code&gt;db.collection.stats().indexSizes&lt;/code&gt; to get the uncompressed size of each index; sum active index sizes plus document working set to compute total cache requirement; drop unused indexes (&lt;code&gt;$indexStats&lt;/code&gt; reveals unused indexes older than 30 days)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;WiredTiger checkpoint I/O spike — eviction latency increases during checkpoint&lt;/td&gt;&lt;td&gt;During 60-second checkpoints, WiredTiger reconciles dirty pages to disk; if dirty page volume is large, checkpoint I/O saturates the storage device; eviction competes with checkpoint for I/O bandwidth&lt;/td&gt;&lt;td&gt;Periodic latency spikes at 60-second intervals; &lt;code&gt;r_await&lt;/code&gt; and &lt;code&gt;w_await&lt;/code&gt; in &lt;code&gt;iostat&lt;/code&gt; spike simultaneously&lt;/td&gt;&lt;td&gt;Provision storage with sufficient write bandwidth for both normal write operations and checkpoint I/O; on AWS, use io2 Block Express volumes (not gp3) for deployments with &gt;50% dirty page ratios; separate WiredTiger journal to a different volume (EBS volume) to reduce write I/O contention during checkpoint&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Read ticket exhaustion — queuing before cache interaction&lt;/td&gt;&lt;td&gt;WiredTiger limits concurrent reads to 128 tickets by default; at high concurrency, tickets exhaust before cache pressure is reached; subsequent reads queue waiting for a ticket&lt;/td&gt;&lt;td&gt;Operation latency climbs linearly with read ticket queue depth; &lt;code&gt;db.serverStatus().wiredTiger.concurrentTransactions.read.out&lt;/code&gt; shows tickets in use; &lt;code&gt;available&lt;/code&gt; drops to zero when queuing occurs&lt;/td&gt;&lt;td&gt;Increase read tickets to 256 for read-heavy workloads: &lt;code&gt;db.adminCommand({setParameter: 1, wiredTigerConcurrentReadTransactions: 256})&lt;/code&gt;; this increases parallelism but also increases cache pressure — only raise tickets if cache headroom exists&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Two-layer memory confusion — OS page cache not helping WT data reads&lt;/td&gt;&lt;td&gt;Engineers expect the OS page cache to absorb WiredTiger cache misses for data files; WiredTiger opens data files with &lt;code&gt;O_DIRECT&lt;/code&gt;, so the OS page cache never buffers data file reads&lt;/td&gt;&lt;td&gt;Teams allocate less RAM to WiredTiger cache assuming the OS layer will help; it does not for data; cache misses hit disk directly&lt;/td&gt;&lt;td&gt;Confirm WiredTiger data file I/O bypasses OS cache: `lsof -p $(pgrep mongod)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;2025-wiredtiger-and-mongodb-7x8x-update&quot;&gt;2025 WiredTiger and MongoDB 7.x/8.x Update&lt;/h2&gt;
&lt;p&gt;WiredTiger (available in MongoDB 7.x, GA in 8.0 released October 2024) introduced a revised eviction configuration model that adds &lt;code&gt;eviction_updates_target&lt;/code&gt; and &lt;code&gt;eviction_updates_trigger&lt;/code&gt; thresholds specifically for controlling &lt;code&gt;WT_UPDATE&lt;/code&gt; chain memory. In high-update workloads where update chains accumulate faster than checkpoint can advance the stable timestamp, these thresholds allow the eviction system to target update-heavy pages specifically, reducing the incidence of cache bloat from MVCC chains without requiring a reduction in checkpoint frequency.&lt;/p&gt;
&lt;p&gt;WiredTiger’s block manager in MongoDB 8.x (GA 2024-10-02) also added improved read-ahead logic: when an eviction-forced read requests a page, WiredTiger now pre-fetches adjacent pages in the B-Tree leaf level, reducing the number of sequential cache misses during range scans. This improvement specifically benefits workloads that experienced cache thrashing during range queries where adjacent pages were consistently evicted between query iterations.&lt;/p&gt;
&lt;p&gt;For deployments on Linux kernel 6.x, WiredTiger added support for &lt;code&gt;io_uring&lt;/code&gt; as an alternative to &lt;code&gt;pread64&lt;/code&gt;/&lt;code&gt;pwrite64&lt;/code&gt; for direct I/O — reducing per-I/O system call overhead by batching I/O submissions. The &lt;code&gt;io_uring&lt;/code&gt; backend is enabled by setting &lt;code&gt;wiredTigerEngineConfig: &quot;io_type=io_uring&quot;&lt;/code&gt; in the mongod configuration (experimental as of late 2024, pending stabilization). On NVMe SSDs, &lt;code&gt;io_uring&lt;/code&gt; reduces per-read latency from approximately 50–100µs (pread64 system call overhead) to 10–30µs at low queue depths.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; WiredTiger uses &lt;code&gt;O_DIRECT&lt;/code&gt; and manages its own memory pool separate from the OS page cache; teams that size MongoDB RAM based on compressed disk footprint routinely undersize the WiredTiger cache by 2–4×, causing foreground eviction that stalls application threads mid-operation and is misdiagnosed as a storage I/O problem.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Run &lt;code&gt;db.serverStatus().wiredTiger.cache&lt;/code&gt; to check cache fill ratio and &lt;code&gt;pages evicted by application threads&lt;/code&gt;; run the collection-level uncompressed working set query to determine the true cache requirement; set &lt;code&gt;cacheSizeGB&lt;/code&gt; to cover the uncompressed working set plus 20% headroom, targeting 60–70% of total system RAM on dedicated MongoDB hosts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; WiredTiger’s use of &lt;code&gt;O_DIRECT&lt;/code&gt; for data files is documented in the WiredTiger architecture documentation and confirmed by MongoDB’s Server Manual section on WiredTiger storage engine internals; the &lt;code&gt;pages evicted by application threads&lt;/code&gt; metric is explicitly documented in MongoDB’s &lt;code&gt;db.serverStatus()&lt;/code&gt; output reference as the indicator of foreground eviction pressure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run the mongosh cache fill check above against any MongoDB instance currently showing elevated read latency or IOPS; if &lt;code&gt;pages evicted by application threads&lt;/code&gt; is nonzero and &lt;code&gt;bytes currently in the cache&lt;/code&gt; exceeds 85% of &lt;code&gt;maximum bytes configured&lt;/code&gt;, you have foreground eviction — verify your &lt;code&gt;cacheSizeGB&lt;/code&gt; against the uncompressed working set size before concluding the storage layer is the bottleneck.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;WiredTiger Architecture Documentation — Cache, Eviction, and Reconciliation Internals: &lt;a href=&quot;https://source.wiredtiger.com/develop/arch-cache.html&quot;&gt;https://source.wiredtiger.com/develop/arch-cache.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Server Manual — WiredTiger Storage Engine, Cache and Compression: &lt;a href=&quot;https://www.mongodb.com/docs/manual/core/wiredtiger/&quot;&gt;https://www.mongodb.com/docs/manual/core/wiredtiger/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Server Manual — db.serverStatus() Output Reference — wiredTiger.cache Fields: &lt;a href=&quot;https://www.mongodb.com/docs/manual/reference/command/serverStatus/#mongodb-serverstatus-serverstatus.wiredTiger.cache&quot;&gt;https://www.mongodb.com/docs/manual/reference/command/serverStatus/#mongodb-serverstatus-serverstatus.wiredTiger.cache&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;WiredTiger Source — eviction_trigger and eviction_target Configuration Parameters: &lt;a href=&quot;https://source.wiredtiger.com/develop/tune_cache.html&quot;&gt;https://source.wiredtiger.com/develop/tune_cache.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Linux &lt;code&gt;io_uring&lt;/code&gt; and Direct I/O Interaction — Jens Axboe, USENIX ATC 2019: &lt;a href=&quot;https://www.usenix.org/conference/atc19/presentation/axboe&quot;&gt;https://www.usenix.org/conference/atc19/presentation/axboe&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Engineering Blog — WiredTiger Eviction Deep Dive (2022): &lt;a href=&quot;https://www.mongodb.com/blog/post/performance-best-practices-mongodb-data-modeling-and-memory-sizing&quot;&gt;https://www.mongodb.com/blog/post/performance-best-practices-mongodb-data-modeling-and-memory-sizing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Brendan Gregg — Systems Performance (2nd ed.) Chapter 8: Memory — OS Page Cache vs Direct I/O Interaction: &lt;a href=&quot;https://www.brendangregg.com/systems-performance.html&quot;&gt;https://www.brendangregg.com/systems-performance.html&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>failures</category></item><item><title>Cloud Spanner vs Cloud SQL: The Real Distributed Database Decision</title><link>https://rajivonai.com/blog/2023-03-07-cloud-spanner-vs-cloud-sql-the-real-distributed-database-decision/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-03-07-cloud-spanner-vs-cloud-sql-the-real-distributed-database-decision/</guid><description>Cloud Spanner vs Cloud SQL turns on failure domain tolerance — whether your SLA survives a regional primary outage, not on scale or throughput alone.</description><pubDate>Tue, 07 Mar 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Most teams do not outgrow Cloud SQL because they need a more interesting database. They outgrow it when the failure domain of a single primary stops matching the business contract.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Attribute&lt;/th&gt;&lt;th&gt;Cloud SQL&lt;/th&gt;&lt;th&gt;Cloud Spanner&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Architecture&lt;/td&gt;&lt;td&gt;Single primary, optional replicas&lt;/td&gt;&lt;td&gt;Distributed, multi-region native&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Write scaling&lt;/td&gt;&lt;td&gt;Primary is the ceiling&lt;/td&gt;&lt;td&gt;Horizontal by key design and split routing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Read scaling&lt;/td&gt;&lt;td&gt;Cross-region replicas (async)&lt;/td&gt;&lt;td&gt;Global reads from nearest replica&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Consistency&lt;/td&gt;&lt;td&gt;Strong within region&lt;/td&gt;&lt;td&gt;Externally consistent globally (TrueTime)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failover&lt;/td&gt;&lt;td&gt;Managed event, HA standby in secondary zone (~60s)&lt;/td&gt;&lt;td&gt;Built-in; no promotion event&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Engine compatibility&lt;/td&gt;&lt;td&gt;PostgreSQL, MySQL, SQL Server&lt;/td&gt;&lt;td&gt;Spanner SQL + PostgreSQL-compatible API&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schema changes&lt;/td&gt;&lt;td&gt;Standard DDL&lt;/td&gt;&lt;td&gt;Online schema changes, fully managed&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Starting cost&lt;/td&gt;&lt;td&gt;Low&lt;/td&gt;&lt;td&gt;Significant base cost (minimum 1 processing unit)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Choose when&lt;/td&gt;&lt;td&gt;Regional system, standard engine tooling needed&lt;/td&gt;&lt;td&gt;Global writes, distributed consistency, horizontal scale&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The usual database decision starts too low in the stack. Teams compare PostgreSQL compatibility, MySQL familiarity, query syntax, managed backups, pricing pages, and migration tooling. Those details matter, but they are rarely the real decision between Cloud SQL and Cloud Spanner.&lt;/p&gt;
&lt;p&gt;Cloud SQL is a managed relational database service for engines teams already know: PostgreSQL, MySQL, and SQL Server. Its operating model is familiar: one writable primary, optional replicas, managed backups, maintenance windows, and high availability inside the constraints of a traditional database architecture.&lt;/p&gt;
&lt;p&gt;Cloud Spanner is a distributed relational database. It is built for horizontal scale, synchronous replication, strong consistency, and multi-region availability. Its operating model is less familiar because the database is not a single machine with replicas attached. It is a distributed system that happens to expose SQL and transactions.&lt;/p&gt;
&lt;p&gt;That difference changes the architecture conversation. The question is not “which one is better?” The question is whether your system can survive the operational shape of a primary database.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Cloud SQL works extremely well when the write path fits on a primary, the application can tolerate regional recovery behavior, and scaling pressure is mostly read-heavy. In that world, replicas absorb analytics and reporting, indexes are tuned, connection pools are sized, and vertical scaling buys time.&lt;/p&gt;
&lt;p&gt;The trouble begins when the application contract quietly becomes distributed while the database contract stays centralized.&lt;/p&gt;
&lt;p&gt;A checkout system wants writes accepted during regional impairment. A financial ledger wants globally ordered transactions. A SaaS control plane wants tenant placement across regions without writing custom shard routing. A mobile backend wants low-latency reads from multiple continents but cannot allow stale business invariants. A marketplace wants inventory decrements, payment state, and fulfillment reservations to commit consistently even as traffic shifts between regions.&lt;/p&gt;
&lt;p&gt;Teams often respond by building the missing distribution layer above Cloud SQL. They introduce application-level sharding, dual writes, queue-based reconciliation, read-your-writes exceptions, regional failover procedures, and increasingly complicated runbooks. The database remains familiar, but the system becomes less honest. The hard part moved into application code.&lt;/p&gt;
&lt;p&gt;So the real question is: do you need a managed relational database, or do you need the database itself to own distributed consistency and failure recovery?&lt;/p&gt;
&lt;h2 id=&quot;the-real-decision-boundary&quot;&gt;The Real Decision Boundary&lt;/h2&gt;
&lt;p&gt;The clean decision boundary is the write contract.&lt;/p&gt;
&lt;p&gt;Use Cloud SQL when the system has a natural primary region, write throughput is within the practical limits of a single primary, and failover can be treated as an operational event. Use Cloud Spanner when the write contract is distributed, the data model must scale horizontally, and consistency across failure domains is part of the product requirement rather than an optimization.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[database decision — start with failure contract] --&gt; B[Cloud SQL — primary database architecture]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; C[Cloud Spanner — distributed database architecture]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; D[single writable primary — familiar operations]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[read replicas — scale read paths]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; F[regional HA — managed failover event]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; G[synchronous replication — database owned consistency]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; H[horizontal splits — scale write paths]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; I[multi-region topology — failure domain in design]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; J[best fit — monoliths and regional services]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; J&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; J&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; K[best fit — ledgers and global control planes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Cloud SQL’s advantage is operational simplicity. You get standard engines, deep ecosystem support, straightforward local development, and a migration path that most engineers understand. If your bottleneck is schema design, query performance, connection management, or basic high availability, Cloud SQL is usually the sharper tool.&lt;/p&gt;
&lt;p&gt;Cloud Spanner’s advantage is removing a category of application-owned distributed systems work. It gives up some engine-specific compatibility and some familiar tuning knobs, but it replaces them with a database architecture designed around replication, partitioning, and strong consistency. That trade is worth making only when the system’s correctness depends on it.&lt;/p&gt;
&lt;p&gt;The mistake is choosing Spanner as an expensive scaling talisman. Spanner does not fix unclear ownership boundaries, unbounded transactions, careless indexes, or chatty request paths. It rewards teams that model access patterns deliberately. Poor key design can create hot ranges. Cross-region writes still pay physics. Distributed transactions are powerful, not free.&lt;/p&gt;
&lt;p&gt;The opposite mistake is staying on Cloud SQL after the architecture has already become distributed. Once teams are coordinating shards, replaying outboxes, reconciling duplicate writes, and maintaining regional promotion playbooks, they are already paying the complexity cost. They are just paying it in application code, incident response, and human judgment.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Google’s Spanner paper, “Spanner: Google’s Globally-Distributed Database,” documents the core pattern: a database designed to distribute data across datacenters while still supporting externally consistent transactions. The important lesson is not that every company needs global SQL. The lesson is that once correctness spans datacenters, the transaction protocol and clock uncertainty become first-class architecture concerns.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Spanner exposes a model where replication and transaction ordering are part of the database contract. Google’s public documentation describes TrueTime and external consistency as mechanisms for making transaction order match real-time ordering. That is a database-level answer to a problem many teams otherwise approximate with queues, timestamps, locks, and compensating jobs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented pattern is simpler application reasoning at the cost of a more specialized database architecture. Application code can rely on strong consistency guarantees instead of encoding a large amount of regional coordination logic itself. The tradeoff is that schema design, key choice, and transaction shape become central performance decisions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Cloud SQL follows the traditional managed relational pattern. Google Cloud’s documentation for Cloud SQL high availability and read replicas describes a familiar architecture: a primary instance, standby or failover behavior, backups, and replicas used to offload reads. That pattern is excellent when the system can name a primary write location. It becomes strained when the product needs the database to behave like a multi-region coordination system.&lt;/p&gt;
&lt;p&gt;The practical conclusion is not “Spanner for scale, Cloud SQL for small.” Many large systems should stay on Cloud SQL because their data ownership is regional, their operational model is simple, and their engineering leverage comes from standard PostgreSQL or MySQL behavior. Some smaller systems may need Spanner because their correctness boundary is global from day one: payments, identity, inventory, entitlement, or control-plane state.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Decision area&lt;/th&gt;&lt;th&gt;Cloud SQL failure mode&lt;/th&gt;&lt;th&gt;Cloud Spanner failure mode&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Write scaling&lt;/td&gt;&lt;td&gt;Primary becomes the ceiling for write throughput&lt;/td&gt;&lt;td&gt;Hot keys or poor split behavior concentrate load&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Regional resilience&lt;/td&gt;&lt;td&gt;Failover is an event the system must tolerate&lt;/td&gt;&lt;td&gt;Multi-region writes pay latency and topology costs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Consistency&lt;/td&gt;&lt;td&gt;Cross-region correctness often moves into application code&lt;/td&gt;&lt;td&gt;Strong consistency can encourage oversized transactions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Ecosystem&lt;/td&gt;&lt;td&gt;Excellent compatibility with PostgreSQL, MySQL, or SQL Server tooling&lt;/td&gt;&lt;td&gt;SQL support is relational but not identical to a chosen engine&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Operations&lt;/td&gt;&lt;td&gt;Familiar tuning can hide growing sharding complexity&lt;/td&gt;&lt;td&gt;Distributed design requires deliberate schema and key choices&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cost model&lt;/td&gt;&lt;td&gt;Starts simple, then grows through replicas, larger instances, and operations&lt;/td&gt;&lt;td&gt;Starts higher, but may replace custom coordination machinery&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Write down the failure contract before choosing the database. Name the maximum acceptable write outage, recovery point, recovery time, and regions that must continue accepting writes.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Choose Cloud SQL when a primary-region relational database satisfies that contract. Choose Cloud Spanner when consistency, availability, and horizontal write scale must be owned by the database across failure domains.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Test the architecture under the failure it claims to survive. Promote replicas, block regions, replay writes, measure stale reads, and verify whether application invariants still hold without manual reconciliation.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Do not migrate because “distributed” sounds safer. Migrate when the current architecture has already forced you to build a distributed database outside the database.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Aurora MySQL Writer CPU Spike Workflow</title><link>https://rajivonai.com/blog/2023-03-06-aurora-mysql-writer-cpu-spike-workflow/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-03-06-aurora-mysql-writer-cpu-spike-workflow/</guid><description>A systematic runbook for diagnosing Aurora MySQL writer CPU spikes — from Performance Insights through lock contention, long transactions, and read offload.</description><pubDate>Mon, 06 Mar 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;An Aurora MySQL writer CPU spike is almost never just a CPU problem.&lt;/strong&gt; The writer processes writes exclusively for the cluster, and when CPU spikes, the culprit is usually a query that changed execution plan, a lock contention burst, a batch job running longer than expected, or a sudden increase in connection count. Treating it as a capacity problem and scaling the instance is the expensive, slow-feedback response. The fast response starts with Performance Insights.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;CloudWatch shows Aurora MySQL writer &lt;code&gt;CPUUtilization&lt;/code&gt; at 80–95%. Application latency is climbing. The P99 for write endpoints has doubled. The on-call engineer opens the console and sees the CPU metric, the latency metric, and a blinking cursor.&lt;/p&gt;
&lt;p&gt;Aurora MySQL separates the writer from the reader cluster endpoints. The writer handles all DML. Readers handle only SELECT queries that have been explicitly routed to the reader endpoint. When the writer is saturated, writes stall, and any reads routed to the writer stall with them. Scaling the writer instance buys time but does not address the root cause — and Aurora Serverless v2 auto-scaling adds latency while scaling happens, which worsens the incident in the short term.&lt;/p&gt;
&lt;p&gt;The diagnostic sequence determines whether this resolves in 10 minutes or 2 hours.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Signal&lt;/th&gt;&lt;th&gt;Where to see it&lt;/th&gt;&lt;th&gt;What it means&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;CPUUtilization 80–100%&lt;/td&gt;&lt;td&gt;CloudWatch — Aurora writer&lt;/td&gt;&lt;td&gt;Writer is bottlenecked; cause unknown&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High DBLoad&lt;/td&gt;&lt;td&gt;Performance Insights — DBLoad metric&lt;/td&gt;&lt;td&gt;Confirms sessions waiting; compare DBLoadCPU vs DBLoadNonCPU&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One query dominating AAS&lt;/td&gt;&lt;td&gt;Performance Insights — Top SQL&lt;/td&gt;&lt;td&gt;Single query is consuming most writer capacity&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Long lock wait in INNODB STATUS&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW ENGINE INNODB STATUS\G&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Lock contention between concurrent transactions&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Active connections spike&lt;/td&gt;&lt;td&gt;CloudWatch — DatabaseConnections&lt;/td&gt;&lt;td&gt;Connection pool exhausted or connection storm&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;PROCESSLIST shows many similar queries&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW FULL PROCESSLIST&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Hot query pattern, not a single rogue query&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Performance Insights — split CPU vs wait&lt;/strong&gt; — Determine whether the bottleneck is CPU execution or wait events:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Performance Insights DBLoad chart separates &lt;code&gt;db.load.avg&lt;/code&gt; into &lt;code&gt;DBLoadCPU&lt;/code&gt; (executing on CPU) and &lt;code&gt;DBLoadNonCPU&lt;/code&gt; (waiting — on locks, I/O, etc.). If &lt;code&gt;DBLoadNonCPU&lt;/code&gt; dominates, the CPU spike is a secondary effect of sessions piling up behind a lock or slow I/O, not pure execution load.&lt;/p&gt;
&lt;p&gt;Navigate to: RDS Console → your Aurora cluster → Performance Insights → select DB Load breakdown by wait event.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;Top SQL by average active sessions&lt;/strong&gt; — Identify the specific query driving load:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Performance Insights → Top SQL tab, sorted by &lt;code&gt;Load (AAS)&lt;/code&gt;. The top query by AAS is the first candidate. Note its digest, get the full SQL text, and examine its execution plan.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Run on the Aurora writer — substitute the digest from Performance Insights&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPLAIN &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; customer_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 12345&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;Currently running queries:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW FULL PROCESSLIST;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Look for queries in &lt;code&gt;State: executing&lt;/code&gt; or &lt;code&gt;State: Waiting for table metadata lock&lt;/code&gt; or &lt;code&gt;State: updating&lt;/code&gt;. A large number of identical or similar queries stacking up indicates the query is not returning promptly — the connection pool is filling with in-flight sessions.&lt;/p&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;&lt;strong&gt;InnoDB lock contention:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW ENGINE INNODB &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;STATUS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;\G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Scroll to the &lt;code&gt;TRANSACTIONS&lt;/code&gt; section and look for &lt;code&gt;LOCK WAIT&lt;/code&gt;. Lock waits indicate two or more transactions competing for the same row or range. The &lt;code&gt;LATEST DETECTED DEADLOCK&lt;/code&gt; section shows the most recent deadlock event — if it is recent and matches the CPU spike timing, lock contention is the primary cause.&lt;/p&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;&lt;strong&gt;Long transactions:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; trx_id, trx_started, trx_query,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       TIMESTAMPDIFF(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SECOND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, trx_started, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOW&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; age_sec&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;INNODB_TRX&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; trx_started&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Any transaction older than 60 seconds on the writer during a CPU spike is a strong suspect. Long transactions hold row locks longer, block concurrent writes, and generate undo log that increases internal InnoDB maintenance work.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Aurora writer CPU spike] --&gt; B{Performance Insights — single query dominant?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| C[EXPLAIN the query — check for full scan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D{Missing index?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes| E[Add index — test in staging first]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no| F[Check statistics staleness — run ANALYZE TABLE]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| G{DBLoadNonCPU dominant?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|yes| H{INNODB STATUS shows lock waits?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| I[Find blocking transaction — reduce scope or kill]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| J[Check I/O metrics — consider read offload]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|no| K{Many connections in PROCESSLIST?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|yes| L[Check connection pool config — reduce max connections]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|no| M{Aurora Serverless v2 scaling in progress?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    M --&gt;|yes| N[Wait for scale-up — increase minimum ACU to prevent recurrence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    M --&gt;|no| O[Check recent schema or code deployment]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Option 1 — Add index for the top query&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If Performance Insights identifies a query doing a full scan (&lt;code&gt;type=ALL&lt;/code&gt; in EXPLAIN) as the top AAS consumer, adding the right index is the highest-leverage fix:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Confirm execution plan before adding index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPLAIN &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; customer_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 12345&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;pending&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Add the index (run during low-traffic window or use pt-online-schema-change for large tables)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ADD&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx_customer_status (customer_id, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Verify the new plan&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPLAIN &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; customer_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 12345&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;pending&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Aurora MySQL supports online DDL for most index additions. For large tables, monitor &lt;code&gt;information_schema.INNODB_ONLINE_DDL&lt;/code&gt; for progress.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 2 — Route reads to Aurora reader endpoint&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If reads are being sent to the writer endpoint — intentionally or by misconfiguration — routing them to the reader reduces writer load immediately:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Verify no heavy reads are running on writer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; user, info, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;PROCESSLIST&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; command &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;Sleep&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; info &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIKE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;SELECT%&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; time&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Update application connection configuration to direct SELECT queries to the Aurora reader endpoint (&lt;code&gt;cluster.ro.amazonaws.com&lt;/code&gt;). For applications that cannot distinguish read vs write connections, a read-write splitting proxy (ProxySQL, RDS Proxy) is an intermediate step.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 3 — Kill long-running blocking transactions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If &lt;code&gt;INFORMATION_SCHEMA.INNODB_TRX&lt;/code&gt; shows a transaction blocking others and it has been running longer than its normal expected duration:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Identify the blocking thread&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; trx_mysql_thread_id, trx_started, trx_query&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;INNODB_TRX&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; trx_started&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Kill it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;KILL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;trx_mysql_thread_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Coordinate with the application team before killing production transactions. For recurring batch jobs that grow too large, the fix is chunking them: process rows in batches of 1,000–10,000 with explicit commits between chunks rather than one large transaction.&lt;/p&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Index additions:&lt;/strong&gt; Indexes can be dropped if they cause unexpected plan changes for other queries: &lt;code&gt;ALTER TABLE orders DROP INDEX idx_customer_status&lt;/code&gt;. Monitor query plan changes via Performance Insights for 24 hours after index additions.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Read routing changes:&lt;/strong&gt; Application-level changes to reader endpoint routing can be reverted by changing the connection string back. Stateful connections in the pool drain within one connection TTL cycle.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Killed transactions:&lt;/strong&gt; The killed transaction rolls back automatically. InnoDB rollback time is proportional to transaction size. Monitor &lt;code&gt;information_schema.INNODB_TRX&lt;/code&gt; to confirm completion. No binlog event is written for the rolled-back transaction.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;Aurora Performance Insights exposes API access to DB load metrics. A CloudWatch Alarm on &lt;code&gt;DBLoad&lt;/code&gt; exceeding the instance’s &lt;code&gt;max_connections&lt;/code&gt;-based threshold (typically 2x vCPU count as a conservative threshold) can trigger automated notification before CPU fully saturates.&lt;/p&gt;
&lt;p&gt;A more targeted detection: schedule a query every 2 minutes on the writer that checks for long-running transactions and high-AAS queries simultaneously:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Long transaction detection (run on writer, schedule via external monitor)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; COUNT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; long_txn_count&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;INNODB_TRX&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; TIMESTAMPDIFF(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SECOND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, trx_started, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOW&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 120&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Alert if &lt;code&gt;long_txn_count&lt;/code&gt; exceeds 2 during business hours. In most workloads, a transaction running more than 2 minutes on a write-heavy Aurora cluster is either a stuck batch job or a deadlock victim that failed to rollback.&lt;/p&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What broke:&lt;/strong&gt; Aurora MySQL writer CPU spiked to 90%+, causing write latency to climb and application error rates to increase. The root cause was a high-AAS query executing a full table scan on a growing table after a recent data volume increase changed the query’s cost model.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;What was done:&lt;/strong&gt; Performance Insights identified the specific query. An index was added targeting the full-scan column. Writer CPU returned to baseline within 5 minutes of the index becoming active.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;What prevents recurrence:&lt;/strong&gt; Performance Insights monitoring with a DBLoad alarm at 4 AAS (writer-size-appropriate threshold) provides early warning. The long-transaction check query is scheduled to run every 2 minutes as a canary for batch job runaway.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;checklist&quot;&gt;Checklist&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Open Performance Insights — confirm DBLoad is elevated on the writer, not the reader&lt;/li&gt;
&lt;li&gt;Compare &lt;code&gt;DBLoadCPU&lt;/code&gt; vs &lt;code&gt;DBLoadNonCPU&lt;/code&gt; — determine if wait events or CPU execution dominate&lt;/li&gt;
&lt;li&gt;Identify top query by AAS in Performance Insights Top SQL tab&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;EXPLAIN&lt;/code&gt; on the top query — look for &lt;code&gt;type=ALL&lt;/code&gt; or high &lt;code&gt;rows&lt;/code&gt; estimate&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;SHOW FULL PROCESSLIST&lt;/code&gt; — check for many stacked identical queries&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;SHOW ENGINE INNODB STATUS\G&lt;/code&gt; — look for lock waits and recent deadlocks&lt;/li&gt;
&lt;li&gt;Run long-transaction query on &lt;code&gt;INFORMATION_SCHEMA.INNODB_TRX&lt;/code&gt; — look for transactions older than 60 seconds&lt;/li&gt;
&lt;li&gt;If full scan confirmed — add index in staging, test plan change, deploy to production&lt;/li&gt;
&lt;li&gt;If lock contention confirmed — identify blocking transaction, coordinate kill or reduce transaction scope&lt;/li&gt;
&lt;li&gt;Verify no SELECT queries are routed to writer endpoint — check connection strings in application config&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: An Aurora MySQL writer CPU spike is treated as a capacity problem, which leads to scaling the instance or adding replicas — changes that are slow, expensive, and do not address a bad query plan, lock contention, or a batch job that outgrew its transaction scope.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Open Performance Insights first: split &lt;code&gt;DBLoadCPU&lt;/code&gt; from &lt;code&gt;DBLoadNonCPU&lt;/code&gt; to determine whether the bottleneck is execution or waiting, identify the top AAS query, then follow the decision tree to the targeted remediation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: CPU returns to baseline and DBLoad drops below the vCPU-count threshold within minutes of addressing the root cause — without any instance scaling.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, enable a CloudWatch alarm on &lt;code&gt;DBLoad&lt;/code&gt; at a threshold of 2× the instance’s vCPU count, and verify that Performance Insights is enabled on your Aurora writer so the top SQL tab is populated the next time a spike occurs.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>cloud</category><category>checklist</category><category>failures</category></item><item><title>GCP Reference Architecture: Cloud Run, Load Balancing, Cloud SQL, Memorystore, and Pub/Sub</title><link>https://rajivonai.com/blog/2023-02-20-gcp-reference-architecture-cloud-run-load-balancing-cloud-sql-memorystore-and-pub-sub/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-02-20-gcp-reference-architecture-cloud-run-load-balancing-cloud-sql-memorystore-and-pub-sub/</guid><description>Cloud Run autoscales compute, but Cloud SQL connection limits, Memorystore eviction, and Pub/Sub backpressure are where capacity planning actually lives.</description><pubDate>Mon, 20 Feb 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A serverless web tier does not remove capacity planning; it moves the hardest part to the boundaries where autoscaling compute meets stateful systems.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Cloud Run is attractive because it gives teams a small operational surface: ship a container, expose HTTP, configure concurrency, and let the platform create more instances when traffic rises. For many product systems, that is exactly the right default. The problem is not Cloud Run. The problem is treating Cloud Run as if every dependency scales the same way.&lt;/p&gt;
&lt;p&gt;A typical GCP production path has five moving parts. The external Application Load Balancer terminates public traffic and routes to a serverless network endpoint group. Cloud Run handles request execution. Cloud SQL stores the durable relational state. Memorystore absorbs repeated reads, coordination hints, and short-lived derived data. Pub/Sub carries work that does not need to block the user request.&lt;/p&gt;
&lt;p&gt;That architecture is common because each component has a clear job. It fails when those jobs blur. If request handlers open unbounded database connections, autoscaling becomes a database denial-of-service. If the cache becomes the source of truth, Redis maintenance becomes a data-loss event. If Pub/Sub consumers are not idempotent, retry behavior turns a transient failure into duplicated side effects.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The dangerous moment is a traffic spike, deploy rollback, regional incident, or upstream retry storm. The load balancer and Cloud Run can admit more work quickly. Cloud SQL cannot create infinite connections. Memorystore can reduce read pressure, but only for keys that are hot and safe to recompute. Pub/Sub can preserve work, but it also extends the lifetime of bad messages unless consumers classify failures correctly.&lt;/p&gt;
&lt;p&gt;The system therefore needs two separate control loops. The request path must protect latency and database capacity. The asynchronous path must protect correctness and recovery. They share code, identity, observability, and deployment pipelines, but they should not share the same scaling assumptions.&lt;/p&gt;
&lt;p&gt;The core question is: how do we use managed GCP services without letting serverless elasticity overload the stateful parts of the system?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    U[users] --&gt; LB[external Application Load Balancer — TLS and routing]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    LB --&gt; NEG[serverless NEG — Cloud Run backend]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    NEG --&gt; WEB[Cloud Run web service — bounded concurrency]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WEB --&gt; CACHE[Memorystore Redis — cache aside and leases]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WEB --&gt; DB[Cloud SQL — durable relational state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WEB --&gt; TOPIC[Pub Sub topic — deferred work]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    TOPIC --&gt; WORKER[Cloud Run worker — idempotent consumer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WORKER --&gt; CACHE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WORKER --&gt; DB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OPS[operations plane — logs metrics traces alerts] --&gt; LB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OPS --&gt; WEB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OPS --&gt; WORKER&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OPS --&gt; DB&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OPS --&gt; CACHE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    OPS --&gt; TOPIC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The load balancer owns the public edge: TLS certificates, global or regional ingress, URL routing, Cloud Armor policies, and a stable IP. A serverless NEG points that edge at Cloud Run, which keeps the application container independent from the ingress policy. Google documents serverless NEGs as the mechanism for connecting Cloud Run to Application Load Balancers, and the load balancer becomes the place to centralize edge controls rather than embedding them in every service.&lt;/p&gt;
&lt;p&gt;Cloud Run owns stateless execution. Set concurrency deliberately instead of accepting it as a neutral default. High concurrency is efficient for CPU-light handlers, but it multiplies the number of simultaneous database operations per instance. Maximum instances are also a safety control, not only a cost control. A useful starting formula is:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;maximum database clients = max Cloud Run instances * per instance pool size&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;That number must fit under Cloud SQL connection limits with room for migrations, consoles, maintenance, background workers, and emergency access.&lt;/p&gt;
&lt;p&gt;Cloud SQL owns durable relational state. Prefer private connectivity where possible, use connection pooling, and assume connections will be dropped during maintenance or failover. Google’s Cloud SQL guidance explicitly calls out connection pooling, exponential backoff, testing maintenance behavior, and testing failover behavior as best practices. That means the application contract is not “connections stay alive.” The contract is “the application reconnects, retries safe operations, and sheds load when the database is unavailable.”&lt;/p&gt;
&lt;p&gt;Memorystore owns speed, not truth. Use cache-aside for expensive reads: read Redis, fall back to Cloud SQL, populate Redis with a TTL, and tolerate cache misses. Use short leases only where duplicate work is acceptable or guarded by database constraints. Do not place unrecoverable state in Redis unless the business has accepted that failure mode.&lt;/p&gt;
&lt;p&gt;Pub/Sub owns decoupling. Publish after the durable transaction commits, or use an outbox table if the event and database write must move together. Workers should be idempotent by construction: natural keys, database uniqueness constraints, processed-event tables, or compare-and-set updates. Pub/Sub retries are useful only when repeated delivery is safe.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Google Cloud documents Application Load Balancers as Layer 7 proxies and serverless NEGs as backends that can point to Cloud Run. The documented pattern is to put Cloud Run behind the load balancer when the service needs centralized ingress features such as a stable external endpoint and edge policy controls. See Google Cloud’s documentation on &lt;a href=&quot;https://cloud.google.com/load-balancing/docs/https&quot;&gt;external Application Load Balancers&lt;/a&gt; and &lt;a href=&quot;https://docs.cloud.google.com/load-balancing/docs/negs/serverless-neg-concepts&quot;&gt;serverless NEGs&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Treat the load balancer as the public contract and Cloud Run as the revisioned compute target. Keep Cloud Run services private to intended callers where possible, grant invoker permissions intentionally, and route public traffic through the load balancer. This prevents every service from inventing its own edge behavior.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Deployments become safer because traffic management, TLS, and application revision rollout are separate concerns. A bad revision can be rolled back without changing public DNS or certificate handling.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The load balancer is not decorative infrastructure. It is the boundary where product traffic becomes controlled platform traffic.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Cloud Run documents concurrent request handling and maximum instances as service controls. Cloud SQL documents connection pooling and reconnect behavior because database connections can be dropped by the database or infrastructure. See Cloud Run’s &lt;a href=&quot;https://cloud.google.com/run/docs/about-concurrency&quot;&gt;concurrency&lt;/a&gt;, &lt;a href=&quot;https://cloud.google.com/run/docs/configuring/max-instances-limits&quot;&gt;maximum instances&lt;/a&gt;, and Cloud SQL’s &lt;a href=&quot;https://cloud.google.com/sql/docs/postgres/connect-run&quot;&gt;Cloud Run connection guidance&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Size Cloud Run concurrency and max instances against Cloud SQL, not only against HTTP throughput. Put a small pool inside each instance, use timeouts, use exponential backoff, and fail fast when the database is saturated.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The service degrades by rejecting excess work rather than turning a spike into connection exhaustion. Users see controlled errors and retries instead of a full database collapse.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Autoscaling needs a governor whenever the next hop is stateful.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Google Cloud documents Memorystore connectivity from Cloud Run through VPC access patterns, and Redis itself is commonly used as a cache with expiration semantics rather than a relational source of record. See &lt;a href=&quot;https://docs.cloud.google.com/memorystore/docs/redis/connect-redis-instance-cloud-run&quot;&gt;connecting Cloud Run to Memorystore for Redis&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Use Redis for cache-aside reads, short-lived coordination, and rate hints. Put TTLs on cached data. Make cache population safe under concurrent misses. Keep writes authoritative in Cloud SQL.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Hot reads stop hammering Cloud SQL, but the system still recovers when Redis is flushed, unavailable, or cold after maintenance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; A cache is an optimization that must be removable during an incident.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Pub/Sub is documented as an asynchronous messaging service with high reliability and scalability, and authenticated push to Cloud Run requires the caller identity to have Cloud Run invoker permission. See Pub/Sub’s &lt;a href=&quot;https://docs.cloud.google.com/pubsub/architecture&quot;&gt;architecture overview&lt;/a&gt; and &lt;a href=&quot;https://docs.cloud.google.com/pubsub/docs/authenticate-push-subscriptions&quot;&gt;push authentication guidance&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Move slow and retryable work out of the user request. Publish events after durable state changes. Make workers idempotent. Use dead-letter topics for poison messages and alert on backlog age, not just message count.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; User-facing latency is protected, and operational recovery becomes visible. A worker outage accumulates backlog instead of losing work, while dead-letter routing separates bad data from temporary dependency failures.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Queues do not remove failure. They make failure durable enough to inspect and replay.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Control&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Cloud Run scales faster than Cloud SQL&lt;/td&gt;&lt;td&gt;Connection exhaustion, rising latency, failed logins&lt;/td&gt;&lt;td&gt;Bound max instances, bound pool size, use backoff&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cache stampede&lt;/td&gt;&lt;td&gt;Redis miss causes many identical database reads&lt;/td&gt;&lt;td&gt;Singleflight, leases, jittered TTLs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Redis treated as durable state&lt;/td&gt;&lt;td&gt;Data disappears after maintenance or flush&lt;/td&gt;&lt;td&gt;Keep source of truth in Cloud SQL&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Pub/Sub consumer is not idempotent&lt;/td&gt;&lt;td&gt;Duplicate emails, double charges, repeated mutations&lt;/td&gt;&lt;td&gt;Idempotency keys and database constraints&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Load balancer health hides dependency failure&lt;/td&gt;&lt;td&gt;Edge stays healthy while app returns 500s&lt;/td&gt;&lt;td&gt;Application health checks and dependency alerts&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cloud SQL failover is untested&lt;/td&gt;&lt;td&gt;Long recovery, stuck connections&lt;/td&gt;&lt;td&gt;Run failover tests and reconnect drills&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Worker backlog is invisible&lt;/td&gt;&lt;td&gt;Async work misses business deadlines&lt;/td&gt;&lt;td&gt;Alert on oldest unacked message age&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Serverless compute can overload stateful dependencies faster than humans can react.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Put Cloud Run behind an Application Load Balancer, cap concurrency and instances, use Cloud SQL as the source of truth, use Memorystore only for recoverable acceleration, and move non-blocking work through Pub/Sub.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The documented GCP patterns all point to explicit boundaries: serverless NEGs for ingress, Cloud Run concurrency controls for admission, Cloud SQL pooling for connection survival, Redis access through private networking, and Pub/Sub authentication for asynchronous invocation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before production, run four drills: a traffic spike against max instances, a Cloud SQL failover, a Redis flush, and a Pub/Sub poison-message replay. If the system cannot survive those drills, the architecture is not ready; it is only deployed.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>cloud</category><category>databases</category></item><item><title>MySQL Replication Lag Decision Tree</title><link>https://rajivonai.com/blog/2023-02-06-mysql-replication-lag-decision-tree/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-02-06-mysql-replication-lag-decision-tree/</guid><description>A systematic runbook for diagnosing MySQL replication lag — from initial SHOW REPLICA STATUS to parallel apply, long transactions, and relay log space.</description><pubDate>Mon, 06 Feb 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Replication lag in MySQL is a symptom, not a cause — but the cause is almost always one of five things, and the diagnostic sequence matters.&lt;/strong&gt; Engineers who start tuning parallel replica workers before they check whether the replica’s SQL thread is even running waste an hour on the wrong problem. This runbook covers the decision tree from first alert to targeted remediation.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The alert fires: &lt;code&gt;Seconds_Behind_Source&lt;/code&gt; is 300 and climbing. Read queries routed to the replica are returning data that is several minutes stale. The application is surfacing incorrect balances, missing recent records, or serving out-of-date inventory counts depending on what is being replicated.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Seconds_Behind_Source&lt;/code&gt; measures the timestamp difference between the most recently executed event on the replica and the timestamp recorded in the primary’s binlog for the same event. It is an estimate of how far behind the replica is in applying committed transactions from the primary. When it grows without bound, the replica is applying events slower than the primary is producing them — or it has stopped applying events entirely.&lt;/p&gt;
&lt;p&gt;The distinction between “stopped” and “slow” is the first fork in the diagnostic tree.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Signal&lt;/th&gt;&lt;th&gt;Where to see it&lt;/th&gt;&lt;th&gt;What it means&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Seconds_Behind_Source&lt;/code&gt; growing&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW REPLICA STATUS\G&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Replica is falling behind; does not indicate why&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;SQL_Running: No&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW REPLICA STATUS\G&lt;/code&gt;&lt;/td&gt;&lt;td&gt;SQL thread stopped — replication halted, not just slow&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;IO_Running: No&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW REPLICA STATUS\G&lt;/code&gt;&lt;/td&gt;&lt;td&gt;I/O thread stopped — not receiving new binlog events&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Last_SQL_Error&lt;/code&gt; non-empty&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW REPLICA STATUS\G&lt;/code&gt;&lt;/td&gt;&lt;td&gt;SQL thread encountered an error on a specific event&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High relay log space&lt;/td&gt;&lt;td&gt;&lt;code&gt;Relay_Log_Space&lt;/code&gt; in SHOW REPLICA STATUS&lt;/td&gt;&lt;td&gt;Binlog arriving faster than SQL thread can apply it&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Long-running transactions on primary&lt;/td&gt;&lt;td&gt;&lt;code&gt;INFORMATION_SCHEMA.INNODB_TRX&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Large transactions create large binlog events that take time to apply&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Thread status&lt;/strong&gt; — Verify both replication threads are running before investigating lag causes:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;REPLICA&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; STATUS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;\G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Look for &lt;code&gt;Replica_IO_Running: Yes&lt;/code&gt; and &lt;code&gt;Replica_SQL_Running: Yes&lt;/code&gt;. If either is &lt;code&gt;No&lt;/code&gt;, read &lt;code&gt;Last_IO_Error&lt;/code&gt; or &lt;code&gt;Last_SQL_Error&lt;/code&gt; for the stop reason. A stopped thread is not a lag problem — it is a replication failure. Fix the root cause before any lag remediation.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;Long-running transactions on the primary&lt;/strong&gt; — A single long transaction creates one large binlog event that the replica must apply sequentially:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; trx_id, trx_started, trx_query,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;       TIMESTAMPDIFF(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SECOND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, trx_started, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOW&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; trx_age_sec&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;INNODB_TRX&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; trx_started&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Any transaction older than 30–60 seconds is a candidate for blocking replica apply. Check &lt;code&gt;trx_query&lt;/code&gt; for the SQL responsible.&lt;/p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;Top queries by wait time on primary&lt;/strong&gt; — Identify what the primary is spending time on:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; DIGEST_TEXT, COUNT_STAR, SUM_TIMER_WAIT,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;       ROUND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(SUM_TIMER_WAIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; COUNT_STAR &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; 1e12, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; avg_latency_sec&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; performance_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;events_statements_summary_by_digest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SUM_TIMER_WAIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;High-latency statements generating large binlog events are a common cause of chronic lag. A 10-second DELETE running every minute creates a 10-second replication backlog per cycle.&lt;/p&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;&lt;strong&gt;Parallel apply configuration&lt;/strong&gt; — Check whether multi-threaded replica apply is enabled:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; @@replica_parallel_workers, @@replica_parallel_type;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If &lt;code&gt;replica_parallel_workers&lt;/code&gt; is 0 or 1, the replica applies one transaction at a time. Modern MySQL supports &lt;code&gt;LOGICAL_CLOCK&lt;/code&gt; parallelism, which applies transactions from the same binlog group commit in parallel. On a high-throughput primary, single-threaded apply is the most common cause of chronic lag.&lt;/p&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;&lt;strong&gt;Relay log space&lt;/strong&gt; — Check if the relay log backlog is growing:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;REPLICA&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; STATUS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;\G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Look at &lt;code&gt;Relay_Log_Space&lt;/code&gt;. If this is large and growing, the I/O thread is receiving binlog events faster than the SQL thread processes them — confirming a slow-apply bottleneck rather than a network or connectivity issue.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Seconds_Behind_Source growing] --&gt; B{SQL_Running = YES?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| C[Read Last_SQL_Error]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D[Fix SQL error — skip or repair event]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| E{IO_Running = YES?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|no| F[Read Last_IO_Error]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Fix network or auth issue]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|yes| H{Long transaction on primary?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| I[Reduce transaction size on primary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| J{parallel_workers is 0 or 1?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|yes| K[Enable LOGICAL_CLOCK parallel apply]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|no| L{Relay log space growing?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|yes| M[Increase relay_log_space_limit or scale replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt;|no| N[Check primary write volume vs replica capacity]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Option 1 — Enable parallel replica apply&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Single-threaded apply is the most common cause of lag on busy primaries. Enable multi-threaded apply using the &lt;code&gt;LOGICAL_CLOCK&lt;/code&gt; algorithm, which replicates the parallelism from the primary’s binlog group commit:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; GLOBAL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; replica_parallel_workers &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; GLOBAL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; replica_parallel_type &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;LOGICAL_CLOCK&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Required for crash-safe parallel apply&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; GLOBAL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; replica_preserve_commit_order &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Restart the SQL thread to apply:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;STOP&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; REPLICA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SQL_THREAD;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;START&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; REPLICA&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SQL_THREAD;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Monitor &lt;code&gt;Seconds_Behind_Source&lt;/code&gt; to confirm the replica is catching up. The MySQL documentation recommends &lt;code&gt;replica_preserve_commit_order = 1&lt;/code&gt; when using parallel apply to maintain consistent external visibility.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 2 — Kill blocking long transactions on the primary&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If a single large transaction is generating a binlog event that takes minutes to apply, identify and interrupt it:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- On the primary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; trx_id, trx_started, trx_mysql_thread_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;INNODB_TRX&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; trx_started&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;KILL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;trx_mysql_thread_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After killing the transaction, verify it rolls back cleanly. This is disruptive — validate that the transaction is truly blocking before killing it. If the transaction is a scheduled batch job, coordinate with the application team to reduce its scope (process in smaller batches) or schedule it during low-replication-sensitivity windows.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 3 — Promote replica or add a new downstream replica&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If the primary’s write volume consistently exceeds what a single replica can apply even with parallel workers, the architecture has reached a scale limit. Options:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Promote the lagging replica to primary and demote the original (for planned maintenance or topology change)&lt;/li&gt;
&lt;li&gt;Add a second-tier replica that replicates from a relay replica closer to the primary&lt;/li&gt;
&lt;li&gt;Evaluate whether reads can be sharded or moved to a read-optimized layer&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is not a quick fix — it is an architectural response to sustained primary write volume exceeding replica apply capacity.&lt;/p&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;For parallel apply changes:&lt;/strong&gt; Disable by setting &lt;code&gt;replica_parallel_workers = 0&lt;/code&gt; and restarting the SQL thread. The change is non-destructive — disabling parallel apply reverts to sequential mode immediately.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;For killed transactions on primary:&lt;/strong&gt; The transaction will roll back automatically. Monitor &lt;code&gt;information_schema.INNODB_TRX&lt;/code&gt; to confirm the rollback completes. If the transaction was large, rollback can take as long as the original execution. No binlog event is emitted for the rolled-back transaction.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;For relay log space changes:&lt;/strong&gt; Increasing &lt;code&gt;relay_log_space_limit&lt;/code&gt; is non-destructive and can be done at runtime with &lt;code&gt;SET GLOBAL&lt;/code&gt;. Decreasing it requires waiting for relay log consumption to catch up first.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;Replication lag monitoring lends itself to a simple alerting script. The core signal — &lt;code&gt;Seconds_Behind_Source&lt;/code&gt; above a threshold — can be captured from &lt;code&gt;SHOW REPLICA STATUS&lt;/code&gt; via any MySQL-compatible monitoring tool (Percona Monitoring and Management, CloudWatch RDS Enhanced Monitoring, or a custom cron-driven script).&lt;/p&gt;
&lt;p&gt;A more targeted automation: schedule a query on the primary every 5 minutes to check for transactions older than 60 seconds and write the result to a monitoring table. Any row in that table with &lt;code&gt;trx_age_sec &gt; 300&lt;/code&gt; is a candidate for alerting before it generates a multi-minute binlog event that stalls the replica.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Scheduled check for long-running transactions (run on primary)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; COUNT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; long_txn_count&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;INNODB_TRX&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; TIMESTAMPDIFF(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SECOND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, trx_started, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOW&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 60&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If this returns nonzero during steady-state operation, the replication lag root cause is already present even when lag is not yet visible.&lt;/p&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What broke:&lt;/strong&gt; MySQL replication lag caused read replicas to serve stale data. The replica was applying committed transactions slower than the primary was producing them.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;What was done:&lt;/strong&gt; Identified the root cause (long transactions or single-threaded apply), enabled parallel replica apply or reduced transaction scope on the primary, and verified &lt;code&gt;Seconds_Behind_Source&lt;/code&gt; returned to near zero.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;What prevents recurrence:&lt;/strong&gt; Parallel apply configured with &lt;code&gt;LOGICAL_CLOCK&lt;/code&gt; handles normal write volume. Long-transaction alerting on the primary gives early warning before binlog events stall the replica apply thread.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;checklist&quot;&gt;Checklist&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Run &lt;code&gt;SHOW REPLICA STATUS\G&lt;/code&gt; and confirm both &lt;code&gt;Replica_IO_Running&lt;/code&gt; and &lt;code&gt;Replica_SQL_Running&lt;/code&gt; are &lt;code&gt;Yes&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Read &lt;code&gt;Last_SQL_Error&lt;/code&gt; and &lt;code&gt;Last_IO_Error&lt;/code&gt; — if either is non-empty, address the error before diagnosing lag&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;Seconds_Behind_Source&lt;/code&gt; trend — is it growing, stable, or recovering?&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;INFORMATION_SCHEMA.INNODB_TRX&lt;/code&gt; on primary for transactions older than 30 seconds&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;performance_schema.events_statements_summary_by_digest&lt;/code&gt; on primary for top wait-time queries&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;SELECT @@replica_parallel_workers, @@replica_parallel_type&lt;/code&gt; — if workers is 0 or 1, evaluate enabling parallel apply&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;Relay_Log_Space&lt;/code&gt; from &lt;code&gt;SHOW REPLICA STATUS&lt;/code&gt; — large growing relay log confirms slow-apply bottleneck&lt;/li&gt;
&lt;li&gt;If enabling parallel apply, set &lt;code&gt;replica_preserve_commit_order = 1&lt;/code&gt; before restarting the SQL thread&lt;/li&gt;
&lt;li&gt;After any change, monitor &lt;code&gt;Seconds_Behind_Source&lt;/code&gt; for 10–15 minutes to confirm the trend reverses&lt;/li&gt;
&lt;li&gt;Document the root cause and resolution in your incident log for pattern tracking&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: &lt;code&gt;Seconds_Behind_Source&lt;/code&gt; grows during an incident and the natural instinct is to tune parallel workers — but if the SQL thread has stopped or there is a long transaction blocking apply, that tuning changes nothing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Follow the decision tree: check thread status first, long transactions second, parallel apply configuration third, relay log space last. Each check either identifies the cause or rules it out before the next step.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: After the correct remediation, &lt;code&gt;Seconds_Behind_Source&lt;/code&gt; stops growing and trends back toward zero within a few minutes, confirming the apply bottleneck was addressed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, run &lt;code&gt;SELECT @@replica_parallel_workers, @@replica_parallel_type&lt;/code&gt; on every replica in your fleet — if any replica has &lt;code&gt;parallel_workers = 0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt;, evaluate enabling &lt;code&gt;LOGICAL_CLOCK&lt;/code&gt; parallel apply before the next high-write event.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>checklist</category><category>failures</category></item><item><title>Azure Multi-Region Design: Front Door, Cosmos DB, SQL Failover, and Operational Tradeoffs</title><link>https://rajivonai.com/blog/2023-02-05-azure-multi-region-design-front-door-cosmos-db-sql-failover-and-operational-tradeoffs/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-02-05-azure-multi-region-design-front-door-cosmos-db-sql-failover-and-operational-tradeoffs/</guid><description>Azure multi-region design tradeoffs: Front Door routing, Cosmos DB consistency, and SQL failover group lag — and which failures each bet absorbs.</description><pubDate>Sun, 05 Feb 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A multi-region Azure architecture is not a diagram with two identical boxes; it is a set of explicit bets about which failures you will absorb, which inconsistencies you will tolerate, and which operations team will be awake during failover.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Cloud teams are under pressure to make regional outages uneventful. The business asks for active-active. The platform team hears global ingress, replicated data, zero downtime, and automated failover. Azure provides credible building blocks: Azure Front Door for global HTTP entry, Azure Cosmos DB for globally distributed NoSQL data, Azure SQL Database failover groups for relational continuity, and zone-redundant regional services for local resilience.&lt;/p&gt;
&lt;p&gt;The trap is that these services do not compose into a single availability guarantee. Front Door can route traffic away from an unhealthy origin, but it cannot make a half-failed application safe. Cosmos DB can accept writes in multiple regions, but consistency and conflict behavior become application concerns. Azure SQL failover groups can redirect relational workloads, but forced failover can lose data because geo-replication is asynchronous. Each layer solves a different part of the failure.&lt;/p&gt;
&lt;p&gt;The architecture has to start with failure ownership, not product selection.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The naive design is symmetrical: deploy the same application into East US and West US, put Front Door in front, replicate Cosmos DB globally, configure SQL failover, and call the system active-active.&lt;/p&gt;
&lt;p&gt;That design usually fails in the gaps between layers.&lt;/p&gt;
&lt;p&gt;A user request can be routed to West US while its relational write path still depends on a primary SQL database in East US. A Cosmos DB document can be written locally under session consistency while a downstream relational transaction is serialized through a different region. Front Door health probes can mark an origin healthy because &lt;code&gt;/healthz&lt;/code&gt; returns 200, while checkout, billing, or identity is degraded because a dependency is timing out. A failover group can move SQL to the secondary, but application connection pools, caches, background workers, and idempotency tables might still assume the old primary.&lt;/p&gt;
&lt;p&gt;The hard question is not “how do we deploy two regions?” It is: &lt;strong&gt;which requests are allowed to continue when one region, one data system, or one replication path is impaired?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-answer--regional-stamps-with-explicit-data-ownership&quot;&gt;The Answer — Regional Stamps With Explicit Data Ownership&lt;/h2&gt;
&lt;p&gt;A safer Azure multi-region architecture uses regional stamps. Each stamp contains the compute, cache, queues, and regional dependencies needed to serve a bounded slice of traffic. Azure Front Door routes users to healthy stamps. Cosmos DB handles data that can tolerate distributed consistency semantics. Azure SQL Database remains the system of record only for data that needs relational constraints, with failover treated as a controlled operational event.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  U[users — global clients] --&gt; AFD[Azure Front Door — global ingress]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  AFD --&gt;|latency routing| R1[region one stamp — app and workers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  AFD --&gt;|latency routing| R2[region two stamp — app and workers]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  R1 --&gt; C1[Cosmos DB region one — local reads and writes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  R2 --&gt; C2[Cosmos DB region two — local reads and writes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C1 --&gt; CR[Cosmos DB replication — consistency policy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C2 --&gt; CR&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  R1 --&gt; S1[Azure SQL primary — relational system of record]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  R2 --&gt; S2[Azure SQL secondary — failover target]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  S1 --&gt; SG[SQL failover group — listener and replication]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  S2 --&gt; SG&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  R1 --&gt; Q1[regional queue — retry and isolation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  R2 --&gt; Q2[regional queue — retry and isolation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  SG --&gt; OPS[operations runbook — failover decision]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Azure Front Door should route at the edge, not decide business correctness. Its job is to evaluate origin health, priority, latency, and weight, then send HTTP traffic to an origin group. Microsoft documents Front Door routing methods including latency and priority routing, and health probes are the signal used to evaluate origin health. That means the probe endpoint must represent real dependency readiness, not just process liveness.&lt;/p&gt;
&lt;p&gt;Cosmos DB should be used deliberately. Multi-region writes can reduce regional write latency and improve availability, but conflict handling and consistency become part of the application contract. Microsoft documents five consistency levels: strong, bounded staleness, session, consistent prefix, and eventual. Strong consistency improves programmability but increases cross-region write latency and can reduce availability during failures. Session consistency is often the pragmatic default for user-facing workloads because it preserves read-your-writes within a client session, but it is not a global serial order.&lt;/p&gt;
&lt;p&gt;Azure SQL failover groups are a different tool. They are appropriate when the relational model is required and the application can tolerate a failover event. The operational distinction matters: Cosmos DB can be designed for continuous regional writes, while SQL failover is usually a promotion decision. A forced failover prioritizes recovery time over potential data loss because replication to the secondary is asynchronous.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Microsoft’s Azure Well-Architected mission-critical guidance recommends multi-region deployment and scale-unit thinking for workloads with high availability requirements. The documented pattern is to avoid one large shared platform and instead use repeatable deployment units that can fail independently.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Apply that pattern by making each Azure region a stamp with its own app instances, queue consumers, cache, observability, and dependency configuration. Put Front Door in front, but keep the routing policy simple enough to reason about during an incident. Use priority routing for active-passive systems and latency or weighted routing only when both regions can safely process the same class of request.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The operational result is clearer blast radius. If one stamp loses its cache, queue, or regional app tier, Front Door can drain traffic from that origin. If Cosmos DB replication is delayed, the application can apply its documented consistency contract. If SQL must fail over, the team knows which write paths pause, which read paths remain available, and which workers must be restarted or re-pointed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The documented pattern is not “make everything active-active.” It is to separate failure domains and match the data model to the recovery behavior. Cosmos DB is a good fit for globally distributed user state, catalogs, preferences, idempotency records, and event materialized views when the consistency model is explicit. Azure SQL is a better fit for relational invariants, financial ledgers, complex transactions, and reporting models that require schema constraints. Mixing both is normal; hiding their different failure modes is the mistake.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Decision&lt;/th&gt;&lt;th&gt;Benefit&lt;/th&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Front Door latency routing&lt;/td&gt;&lt;td&gt;Sends users to nearby healthy origins&lt;/td&gt;&lt;td&gt;Healthy probe does not mean healthy transaction path&lt;/td&gt;&lt;td&gt;Probe critical dependencies and expose degraded readiness&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Front Door priority routing&lt;/td&gt;&lt;td&gt;Simple active-passive failover&lt;/td&gt;&lt;td&gt;Passive region can rot if it receives no real traffic&lt;/td&gt;&lt;td&gt;Send synthetic and controlled production traffic&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cosmos DB multi-region writes&lt;/td&gt;&lt;td&gt;Low regional write latency and high availability&lt;/td&gt;&lt;td&gt;Conflicts and stale reads become product behavior&lt;/td&gt;&lt;td&gt;Define partitioning, conflict policy, and consistency per workload&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cosmos DB strong consistency&lt;/td&gt;&lt;td&gt;Easier correctness model&lt;/td&gt;&lt;td&gt;Higher cross-region latency and lower failure tolerance&lt;/td&gt;&lt;td&gt;Reserve for data that truly needs linearizable reads&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;SQL failover groups&lt;/td&gt;&lt;td&gt;Relational disaster recovery with listener abstraction&lt;/td&gt;&lt;td&gt;Forced failover can lose recent committed primary writes&lt;/td&gt;&lt;td&gt;Define RPO, rehearse failover, and pause unsafe writers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Shared global cache&lt;/td&gt;&lt;td&gt;Simpler application code&lt;/td&gt;&lt;td&gt;Cross-region dependency becomes hidden single point of failure&lt;/td&gt;&lt;td&gt;Prefer regional caches with explicit invalidation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Background workers in both regions&lt;/td&gt;&lt;td&gt;Faster recovery and local processing&lt;/td&gt;&lt;td&gt;Duplicate side effects during failover&lt;/td&gt;&lt;td&gt;Use idempotency keys and lease ownership&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;One global deployment pipeline&lt;/td&gt;&lt;td&gt;Consistent releases&lt;/td&gt;&lt;td&gt;Bad release reaches every region quickly&lt;/td&gt;&lt;td&gt;Use staged regional rollout and automatic rollback&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Start by listing failure modes, not Azure services. For each user journey, decide what happens when the local app, remote app, Cosmos DB region, SQL primary, queue, cache, or Front Door origin is impaired.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build regional stamps behind Azure Front Door. Use Cosmos DB for data that can live with an explicit distributed consistency contract. Use Azure SQL failover groups for relational state, but treat failover as an operational mode with runbooks, alerts, and rehearsals.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Test the architecture with regional game days. Disable one origin, block SQL primary connectivity, inject Cosmos DB latency, poison a queue consumer, and verify that routing, retries, idempotency, and dashboards show the expected behavior.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Write the failover contract before the next implementation sprint: routing policy, data ownership, consistency level, SQL RPO and RTO, manual approval points, rollback steps, and the exact request classes that must stop rather than run incorrectly.&lt;/p&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>MySQL Cardinality and Index Selectivity</title><link>https://rajivonai.com/blog/2023-01-30-mysql-cardinality-and-index-selectivity/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-01-30-mysql-cardinality-and-index-selectivity/</guid><description>MySQL ignores an index when the optimizer estimates a full scan is cheaper — which happens when cardinality is too low, statistics are stale, or the query shape doesn&apos;t match index selectivity. How to diagnose which problem it is and what to do about each.</description><pubDate>Mon, 30 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;MySQL can have a perfectly valid index on a column and still choose a full table scan — not because the optimizer is broken, but because the index is genuinely not worth using.&lt;/strong&gt; Understanding cardinality and selectivity is what separates engineers who add indexes thoughtfully from those who add them and then wonder why EXPLAIN still shows &lt;code&gt;type=ALL&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most engineers learn early that indexes speed up queries. What the introductory materials skip is the optimizer’s decision logic: an index is only used when the optimizer estimates it will be cheaper than not using it. That estimate is driven by selectivity — how many rows the index is expected to filter out. A high-selectivity index on an email column eliminates nearly every row it does not match. A low-selectivity index on a status column with three possible values eliminates almost nothing, and the optimizer correctly concludes that scanning the whole table in a single sequential pass is cheaper than bouncing through the index structure.&lt;/p&gt;
&lt;p&gt;This distinction matters most on large tables. On a 200-row test database, the optimizer often uses indexes it would ignore on a 50-million-row production table, because the cost model changes with scale. Engineers who tune queries against small datasets frequently miss the issue until the table grows.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure mode is specific: you create an index, run EXPLAIN, and see &lt;code&gt;type=ALL&lt;/code&gt;. The index exists. The query filters on the indexed column. But the optimizer ignores it. This confuses engineers who expect index presence to imply index use.&lt;/p&gt;
&lt;p&gt;The root cause is low selectivity. If a &lt;code&gt;status&lt;/code&gt; column has three values — &lt;code&gt;active&lt;/code&gt;, &lt;code&gt;inactive&lt;/code&gt;, &lt;code&gt;deleted&lt;/code&gt; — and 60% of rows are &lt;code&gt;active&lt;/code&gt;, an index on &lt;code&gt;status&lt;/code&gt; where the query filters &lt;code&gt;WHERE status = &apos;active&apos;&lt;/code&gt; returns 60% of the table. InnoDB’s cost model estimates that reading 60% of a large table via random index lookups is more expensive than a sequential full scan, and it is usually right.&lt;/p&gt;
&lt;p&gt;The second failure mode is stale cardinality estimates. InnoDB samples pages to estimate cardinality rather than counting exact distinct values. After a large bulk insert, a table truncate and reload, or months of accumulating rows, the stored cardinality estimate can be wildly wrong, causing the optimizer to make poor choices.&lt;/p&gt;
&lt;p&gt;Why does the optimizer choose a full table scan despite an index, and how can engineers design indexes that the database will actually use?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Cardinality&lt;/strong&gt; is the number of distinct values in an index, as estimated by InnoDB. &lt;strong&gt;Selectivity&lt;/strong&gt; is the ratio of cardinality to total rows, driving the optimizer’s cost model.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Query filters by status] --&gt; B{MySQL Optimizer}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[Evaluate index — High random IO cost]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; D[Evaluate table scan — Sequential IO cost]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; E{Cost Model}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F[Table scan chosen]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Index ignored]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A selectivity of 0.99 (nearly unique column) is excellent. A selectivity of 0.000003 (three values across a million rows) is almost worthless for filtering.&lt;/p&gt;
&lt;p&gt;You can query estimated selectivity directly:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;INDEX_NAME&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;COLUMN_NAME&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;CARDINALITY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  t&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;TABLE_ROWS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  ROUND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;CARDINALITY&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;TABLE_ROWS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; selectivity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;STATISTICS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;TABLES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; t&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;TABLE_SCHEMA&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;TABLE_SCHEMA&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;TABLE_NAME&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;TABLE_NAME&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;TABLE_SCHEMA&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;your_db&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;TABLE_NAME&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;your_table&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;How InnoDB estimates cardinality:&lt;/strong&gt; InnoDB uses random page sampling rather than a full scan. The number of pages sampled is controlled by &lt;code&gt;innodb_stats_sample_pages&lt;/code&gt; and &lt;code&gt;innodb_stats_persistent_sample_pages&lt;/code&gt;. Small samples on large tables with skewed data distributions produce inaccurate estimates.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Refreshing stale estimates:&lt;/strong&gt; Running &lt;code&gt;ANALYZE TABLE orders;&lt;/code&gt; re-runs the sampling process and updates the stored cardinality in &lt;code&gt;mysql.innodb_table_stats&lt;/code&gt;. After bulk loads, table rebuilds, or significant data changes, running this is the fastest way to restore accurate optimizer decisions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Composite indexes and leading column selectivity:&lt;/strong&gt; A composite index on &lt;code&gt;(status, created_at)&lt;/code&gt; is only useful when the query can filter on &lt;code&gt;status&lt;/code&gt; first. If &lt;code&gt;status&lt;/code&gt; has low selectivity, the optimizer may still prefer a full scan, unless the &lt;code&gt;created_at&lt;/code&gt; range is exceptionally narrow.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented pattern across high-scale engineering teams is to enforce strict index selectivity thresholds during schema reviews. Shopify’s engineering blog explicitly outlines their MySQL indexing strategy, noting that adding an index on a boolean or low-cardinality column is an anti-pattern. They observe that MySQL’s optimizer will frequently ignore these indexes because the random I/O required to fetch rows exceeds the sequential I/O cost of a full table scan.&lt;/p&gt;
&lt;p&gt;Similarly, MySQL’s own InnoDB engine relies heavily on &lt;code&gt;innodb_stats_persistent_sample_pages&lt;/code&gt;. If the sample pages do not accurately reflect the distribution of data — such as immediately following a massive backfill — the optimizer behaves unpredictably. The established behavior to combat this is hooking &lt;code&gt;ANALYZE TABLE&lt;/code&gt; into post-migration automation to ensure the optimizer has fresh cardinality estimates before taking production traffic.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Stale cardinality after bulk load&lt;/td&gt;&lt;td&gt;Optimizer uses wrong index or skips a valid one&lt;/td&gt;&lt;td&gt;Estimate reflects pre-load row distribution&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Composite index with low-selectivity leading column&lt;/td&gt;&lt;td&gt;Index not entered even when tail columns are selective&lt;/td&gt;&lt;td&gt;Optimizer evaluates leading column selectivity first&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;FORCE INDEX overriding a correct low-selectivity decision&lt;/td&gt;&lt;td&gt;Query runs slower than a full scan would&lt;/td&gt;&lt;td&gt;Forces random I/O on a column that benefits from sequential scan&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: An index exists but EXPLAIN shows &lt;code&gt;type=ALL&lt;/code&gt; because selectivity is too low for the optimizer to prefer it over a full scan.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Check selectivity using the formula above; run ANALYZE TABLE after bulk data changes; design composite indexes with the most selective column first.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Compare &lt;code&gt;EXPLAIN&lt;/code&gt; output before and after ANALYZE TABLE on a table with stale stats; watch &lt;code&gt;type&lt;/code&gt; change from &lt;code&gt;ALL&lt;/code&gt; to &lt;code&gt;ref&lt;/code&gt; or &lt;code&gt;range&lt;/code&gt; when the estimate is accurate.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, run the selectivity query on your largest tables and verify that indexes on low-cardinality columns are intentional.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category><category>failures</category></item><item><title>Azure Database Reliability Review: Failover Groups, Backups, and Geo-Replication</title><link>https://rajivonai.com/blog/2023-01-21-azure-database-reliability-review-failover-groups-backups-and-geo-replication/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-01-21-azure-database-reliability-review-failover-groups-backups-and-geo-replication/</guid><description>Azure database recovery beyond &apos;we have backups&apos;: failover group cutover, geo-replication lag, and backup restore testing as the real reliability floor.</description><pubDate>Sat, 21 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A database disaster recovery plan that only says “we have backups” is not a recovery plan; it is a delayed outage with better paperwork.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Azure SQL Database gives teams several reliability primitives that sound similar but solve different failure modes: automated backups, point-in-time restore, active geo-replication, and failover groups. They all help recover data, but they do not provide the same recovery time, recovery point, endpoint behavior, or operational contract.&lt;/p&gt;
&lt;p&gt;That distinction matters because database failures rarely arrive as clean “region down” events. More often, they begin as ambiguous symptoms: connection spikes, high log generation, degraded replicas, bad deployments, accidental deletes, expired credentials, firewall drift, or an application still writing to a primary while operators are trying to promote a secondary.&lt;/p&gt;
&lt;p&gt;In Azure SQL Database, active geo-replication creates readable secondary databases and asynchronously replicates transaction log records from the primary. Microsoft documents it as a business continuity capability for individual databases, with manual or application-initiated geo-failover. Failover groups build on that model, adding group-level failover and stable listener endpoints for applications that need to move several databases together. Automated backups serve a different role: they support point-in-time restore, geo-restore, and long-term retention, but they restore into another database rather than instantly moving live traffic.&lt;/p&gt;
&lt;p&gt;The architecture question is not whether Azure provides enough features. It does. The question is whether the system design assigns each feature to the failure mode it can actually handle.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The common failure is treating geo-replication, failover groups, and backups as interchangeable layers of redundancy. They are not.&lt;/p&gt;
&lt;p&gt;Backups are excellent for corruption, accidental deletion, bad migrations, and compliance retention. They are poor as the primary mechanism for a low-RTO regional outage because restore time depends on database size, log volume, backup storage, and operational execution. A restored database also needs application connection strings, identity, firewall, private networking, jobs, secrets, and dependent services aligned before it is useful.&lt;/p&gt;
&lt;p&gt;Active geo-replication is better for regional survivability because a secondary already exists. But it is asynchronous. Microsoft’s documentation is explicit that forced failover can lose transactions committed on the primary but not yet replicated to the secondary. That is not a defect; it is the cost of using wide-area asynchronous replication without blocking every commit on cross-region durability.&lt;/p&gt;
&lt;p&gt;Failover groups improve the operational surface by failing over a group of databases and providing read-write and read-only listener endpoints. But the failover decision still has to be designed carefully. A Microsoft-managed automatic failover policy uses a grace period before forced failover. Too short, and transient platform or network issues can become a data-loss event. Too long, and the application remains unavailable while operators wait for certainty.&lt;/p&gt;
&lt;p&gt;The hard question is: which failures should be recovered by restore, which by controlled failover, and which by forced failover with acknowledged data loss risk?&lt;/p&gt;
&lt;h2 id=&quot;reliability-architecture&quot;&gt;Reliability Architecture&lt;/h2&gt;
&lt;p&gt;The reliable design separates recovery paths instead of collapsing them into one “DR” checkbox.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[application — write workload] --&gt; B[primary database — Azure SQL Database]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[automated backups — point in time restore]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; D[geo secondary — active replication]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[failover group listener — stable endpoint]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; F[restore database — corruption recovery]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; G[application reconnect — regional recovery]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H[runbooks — tested decisions] --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I[monitoring — lag and restore drills] --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use failover groups when the application needs a stable endpoint and the failure domain is regional availability. The application should connect through the failover group listener rather than hard-coding the primary logical server. The secondary server must be production-grade before the incident: same service tier, comparable compute, matching backup retention policy, configured authentication, network access, private endpoints where required, and tested application connectivity.&lt;/p&gt;
&lt;p&gt;Use active geo-replication directly when the unit of recovery is one database and the application can tolerate explicit endpoint movement or has its own routing layer. It is useful for read scale-out and targeted database mobility, but it asks more of the application and the operator during failover.&lt;/p&gt;
&lt;p&gt;Use backups for logical recovery. If a deployment drops a table, a user deletes tenant data, or a migration corrupts rows, failing over may only replicate the damage. Point-in-time restore is the safer path because it creates a separate database at a known timestamp. Long-term retention is for audit, compliance, and historical recovery, not for minute-by-minute availability.&lt;/p&gt;
&lt;p&gt;A practical design has three runbooks:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Controlled failover&lt;/strong&gt; — used during planned region evacuation or when the primary is reachable enough to synchronize.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Forced failover&lt;/strong&gt; — used during primary region loss, with an explicit data-loss acceptance step.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Point-in-time restore&lt;/strong&gt; — used for logical corruption, bad releases, or accidental data changes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The most important engineering control is not the Azure checkbox. It is the decision table that tells operators which runbook to use when symptoms are incomplete.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Microsoft documents active geo-replication as asynchronous replication for Azure SQL Database, where transactions commit on the primary before replication to the secondary completes. The documented pattern is that this improves availability across regions but means forced failover can lose transactions that had not reached the secondary.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Design the application’s critical-write path around that fact. For ordinary writes, accept the configured recovery point objective. For transactions that cannot be lost, Microsoft documents &lt;code&gt;sp_wait_for_database_copy_sync&lt;/code&gt;, which blocks until the last committed transaction has been hardened in the secondary transaction log. That should be used selectively because it adds latency and couples user-facing commits to cross-region replication.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The architecture has an explicit distinction between “normal durable enough” writes and “must survive regional loss” writes. That is a better operational contract than pretending all commits have the same cross-region guarantee.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Geo-replication is not a substitute for consistency design. It is a recovery mechanism with a known replication boundary.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Microsoft documents failover groups as a way to manage replication and failover of databases to another Azure region, with listener endpoints and either customer-managed or Microsoft-managed failover policy.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Put application connection strings on the failover group listener, not the regional database server. Test both read-write and read-only routing. Validate that the secondary region has the same identity, firewall, private networking, secrets, alerts, and capacity assumptions as the primary.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Failover becomes an application routing event instead of a broad configuration rewrite during an outage.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; A secondary database without a working endpoint path is only a replica, not a recovery environment.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Microsoft documents automated backups for Azure SQL Database with short-term retention for point-in-time restore, default retention of seven days for new, restored, and copied databases, configurable backup storage redundancy, and long-term retention for up to ten years.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Treat backups as the recovery path for logical mistakes. Run restore drills into an isolated environment. Measure time to restore, time to validate, and time to reconnect a quarantined application stack.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Operators know whether the backup strategy can recover from corruption before the first real corruption event.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Backup existence is not evidence of recoverability. Restore rehearsal is the evidence.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Best recovery path&lt;/th&gt;&lt;th&gt;Where teams get hurt&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Primary region unavailable&lt;/td&gt;&lt;td&gt;Failover group or geo-replication failover&lt;/td&gt;&lt;td&gt;Forced failover may lose unreplicated commits&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bad deployment corrupts data&lt;/td&gt;&lt;td&gt;Point-in-time restore&lt;/td&gt;&lt;td&gt;Failover can replicate the corruption&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Accidental table or tenant deletion&lt;/td&gt;&lt;td&gt;Point-in-time restore&lt;/td&gt;&lt;td&gt;Restore target may be slow to validate&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Secondary undersized&lt;/td&gt;&lt;td&gt;Scale secondary before incident&lt;/td&gt;&lt;td&gt;Lag increases and post-failover performance collapses&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Authentication or firewall drift&lt;/td&gt;&lt;td&gt;Pre-flight secondary configuration&lt;/td&gt;&lt;td&gt;Database is online but application cannot connect&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Unclear incident ownership&lt;/td&gt;&lt;td&gt;Runbook with decision table&lt;/td&gt;&lt;td&gt;Operators debate RPO during active outage&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Your database reliability posture is probably described by features, not by failure modes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Map each failure mode to one recovery path: failover group, active geo-replication, or point-in-time restore.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run quarterly drills that measure failover time, restore time, replication lag, application reconnect behavior, and data validation steps.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Build the runbook now: define when controlled failover is allowed, when forced failover requires approval, and when restore is mandatory because replication would preserve the damage.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;References: &lt;a href=&quot;https://learn.microsoft.com/en-us/azure/azure-sql/database/active-geo-replication-overview&quot;&gt;Azure SQL Database active geo-replication&lt;/a&gt;, &lt;a href=&quot;https://learn.microsoft.com/en-us/azure/azure-sql/database/auto-failover-group-sql-db&quot;&gt;Azure SQL Database failover groups&lt;/a&gt;, &lt;a href=&quot;https://learn.microsoft.com/en-us/azure/azure-sql/database/automated-backups-overview&quot;&gt;Azure SQL Database automated backups&lt;/a&gt;.&lt;/p&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>PostgreSQL Autovacuum Failure Workflow</title><link>https://rajivonai.com/blog/2023-01-16-postgresql-autovacuum-failure-workflow/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-01-16-postgresql-autovacuum-failure-workflow/</guid><description>A step-by-step runbook for diagnosing and resolving autovacuum failures: dead tuple accumulation, bloat, and transaction ID wraparound risk.</description><pubDate>Mon, 16 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;When &lt;code&gt;n_dead_tup&lt;/code&gt; climbs and autovacuum isn’t keeping up, you have roughly two problems running in parallel: the bloat you can see today, and the transaction ID wraparound risk you might not notice until PostgreSQL forces an emergency shutdown.&lt;/strong&gt; The failure modes compound — bloat slows queries, which slows transactions, which delays vacuum, which grows bloat further. Getting out requires understanding which part of the cycle broke first.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s MVCC model keeps old row versions in the heap rather than updating in place. Autovacuum’s job is to reclaim those dead tuples and keep the transaction ID horizon from advancing too far. Under moderate write load, autovacuum usually runs unnoticed. Under high write volume — bulk loads, frequent deletes, update-heavy workloads — it falls behind.&lt;/p&gt;
&lt;p&gt;When autovacuum falls behind, the visible effects are: growing table size on disk, sequential scans replacing index scans as indexes become less selective relative to bloat, and queries that were running in single-digit milliseconds start showing variance. The less visible effect is &lt;code&gt;age(relfrozenxid)&lt;/code&gt; creeping toward the 2-billion wraparound limit, at which point PostgreSQL will refuse to serve any read or write until a full-table vacuum completes.&lt;/p&gt;
&lt;p&gt;The root cause is almost never “autovacuum is broken.” It is almost always one of three things: a long-running transaction blocking vacuum from removing dead tuples, the &lt;code&gt;autovacuum_vacuum_scale_factor&lt;/code&gt; threshold being too coarse for a large table, or &lt;code&gt;autovacuum_vacuum_cost_delay&lt;/code&gt; throttling throughput below what the write rate demands.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Signal&lt;/th&gt;&lt;th&gt;Where to see it&lt;/th&gt;&lt;th&gt;What it means&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;n_dead_tup&lt;/code&gt; rising continuously&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_user_tables&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Vacuum not keeping up with write rate&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Table size growing without row count growth&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_size_pretty(pg_total_relation_size(...))&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Physical bloat accumulating in heap&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sequential scans replacing index scans&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_user_tables.seq_scan&lt;/code&gt; increasing&lt;/td&gt;&lt;td&gt;Planner estimates degrading due to bloat&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;age(datfrozenxid)&lt;/code&gt; &gt; 1.5 billion&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_database&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Transaction ID wraparound risk is real&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Last autovacuum timestamp hours or days stale&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_user_tables.last_autovacuum&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Vacuum is being blocked or never triggered&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Long-lived idle-in-transaction sessions&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_activity&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Blocking vacuum horizon advancement&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Dead tuple accumulation by table&lt;/strong&gt; — find which tables are most behind:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  schemaname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tablename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_dead_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_live_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_dead_tup::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nullif&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_live_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_dead_tup, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dead_pct,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_autovacuum,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_autoanalyze&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_dead_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;High &lt;code&gt;dead_pct&lt;/code&gt; on a large table tells you where to focus. A &lt;code&gt;last_autovacuum&lt;/code&gt; that is hours old on a high-write table means the trigger threshold was never crossed or vacuum was blocked.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;Active blocking transactions&lt;/strong&gt; — long-running transactions prevent vacuum from advancing the horizon:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  usename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  wait_event_type,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  wait_event,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; xact_start &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; xact_duration,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  left&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(query, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;80&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; query_preview&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;idle&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; xact_start &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IS NOT NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; xact_duration &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Any session with &lt;code&gt;xact_duration&lt;/code&gt; over 10 minutes that is &lt;code&gt;idle in transaction&lt;/code&gt; is a primary vacuum-blocker candidate. PostgreSQL cannot remove dead tuples older than the oldest open transaction’s snapshot.&lt;/p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;Transaction ID wraparound risk&lt;/strong&gt; — check how close each database is to the 2-billion limit:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  datname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  age(datfrozenxid) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; xid_age,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  2000000000&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; age(datfrozenxid) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; xid_remaining&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_database&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; age(datfrozenxid) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;PostgreSQL issues a WARNING at &lt;code&gt;age &gt; 1.5 billion&lt;/code&gt; and becomes read-only at &lt;code&gt;age &gt; 1.95 billion&lt;/code&gt;. Any value above 1 billion warrants attention. Above 1.5 billion, treat it as an incident in progress.&lt;/p&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;&lt;strong&gt;Current autovacuum scale factor&lt;/strong&gt; — determine whether the threshold is too coarse:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW autovacuum_vacuum_scale_factor;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Also check per-table overrides:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; relname, reloptions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_class&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; reloptions &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;IS NOT NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; relkind &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;r&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The default &lt;code&gt;autovacuum_vacuum_scale_factor = 0.2&lt;/code&gt; means autovacuum triggers after 20% of the table’s live rows have become dead. On a 100-million-row table, that is 20 million dead tuples before vacuum runs — enough bloat to double the table’s physical size.&lt;/p&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;&lt;strong&gt;Background writer and checkpoint pressure&lt;/strong&gt; — determine if I/O is the bottleneck:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  checkpoints_timed,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  checkpoints_req,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  checkpoint_write_time,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  checkpoint_sync_time,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  buffers_clean,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  maxwritten_clean,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  buffers_backend&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_bgwriter;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;High &lt;code&gt;maxwritten_clean&lt;/code&gt; means the background writer hit its &lt;code&gt;bgwriter_lru_maxpages&lt;/code&gt; limit repeatedly. High &lt;code&gt;buffers_backend&lt;/code&gt; means backends are doing their own dirty buffer flushing — a sign that I/O throughput is limiting vacuum’s ability to write.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[n_dead_tup growing] --&gt; B{last_autovacuum recent?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no — never triggered| C{autovacuum=on globally?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|no| D[Enable autovacuum in postgresql.conf]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|yes| E{scale_factor too high?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|yes| F[Lower per-table scale_factor]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes — vacuum ran but did not help| G{oldest xact blocking vacuum?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|yes| H{safe to terminate?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|yes| I[pg_terminate_backend — then VACUUM]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt;|no| J[Wait for transaction — then VACUUM]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|no| K{cost_delay throttling?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|yes| L[Reduce cost_delay per-table]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|no| M{xid_age above 1.5B?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    M --&gt;|yes| N[VACUUM FREEZE — emergency]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    M --&gt;|no| O[Manual VACUUM VERBOSE — diagnose output]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Option 1 — Manual VACUUM to clear immediate bloat&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Run a manual &lt;code&gt;VACUUM VERBOSE&lt;/code&gt; to force reclamation and get diagnostic output:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;VACUUM &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VERBOSE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tablename;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The verbose output shows how many dead tuples were removed, how many pages were scanned, and whether any tuples could not be removed due to transaction horizon constraints. If the output shows tuples “not removable due to oldest xmin,” a blocking transaction is the problem, not the configuration.&lt;/p&gt;
&lt;p&gt;For wraparound risk specifically, add &lt;code&gt;FREEZE&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;VACUUM FREEZE tablename;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;FREEZE&lt;/code&gt; advances &lt;code&gt;relfrozenxid&lt;/code&gt; and is the only action that reduces &lt;code&gt;age(datfrozenxid)&lt;/code&gt;. It is I/O-intensive on large tables, so run it during off-peak hours when possible.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 2 — Tune per-table autovacuum thresholds&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For high-write tables where the global &lt;code&gt;scale_factor&lt;/code&gt; is too coarse, override at the table level:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; high_write_table &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  autovacuum_vacuum_scale_factor &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;01&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  autovacuum_vacuum_threshold &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  autovacuum_vacuum_cost_delay &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  autovacuum_vacuum_cost_limit &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 400&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;scale_factor = 0.01&lt;/code&gt; triggers autovacuum after 1% dead tuples instead of 20%. &lt;code&gt;cost_delay = 2ms&lt;/code&gt; with &lt;code&gt;cost_limit = 400&lt;/code&gt; doubles autovacuum’s I/O budget relative to the default (&lt;code&gt;cost_delay = 20ms&lt;/code&gt;, &lt;code&gt;cost_limit = 200&lt;/code&gt;). These are per-table and do not affect global behavior.&lt;/p&gt;
&lt;p&gt;To verify the override is active:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; relname, reloptions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_class&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; relname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;high_write_table&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Option 3 — Terminate blocking long-running transactions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If &lt;code&gt;pg_stat_activity&lt;/code&gt; shows a session that has been &lt;code&gt;idle in transaction&lt;/code&gt; for an extended period and it cannot be resolved through application-layer means, terminate it:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_terminate_backend(pid)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;idle in transaction&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; xact_start &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; interval &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;10 minutes&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After terminating, run &lt;code&gt;VACUUM VERBOSE&lt;/code&gt; on the affected table immediately to reclaim the dead tuples that were being held.&lt;/p&gt;
&lt;p&gt;To prevent recurrence, set the session-level timeout in &lt;code&gt;postgresql.conf&lt;/code&gt; or per-role:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SYSTEM&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idle_in_transaction_session_timeout &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;5min&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_reload_conf();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;VACUUM&lt;/code&gt; and &lt;code&gt;VACUUM FREEZE&lt;/code&gt; are read-safe operations. They do not lock tables for reads or writes (except at the very start of each heap page scan, which is a brief shared lock). They can be run and stopped at any time without data risk.&lt;/li&gt;
&lt;li&gt;Per-table &lt;code&gt;autovacuum_*&lt;/code&gt; overrides via &lt;code&gt;ALTER TABLE ... SET (...)&lt;/code&gt; are immediately active and immediately reversible: &lt;code&gt;ALTER TABLE tablename RESET (autovacuum_vacuum_scale_factor)&lt;/code&gt; returns to the global default.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pg_terminate_backend&lt;/code&gt; terminates the target session’s transaction — the application will see a connection error and must retry. This is the most disruptive remediation and should only be used when the blocking duration justifies it.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;idle_in_transaction_session_timeout&lt;/code&gt; changes take effect for new transactions immediately after &lt;code&gt;pg_reload_conf()&lt;/code&gt;. Existing connections are not affected until they start a new transaction.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;The most impactful automation is a scheduled query that surfaces tables where &lt;code&gt;n_dead_tup&lt;/code&gt; exceeds a threshold before vacuum falls far enough behind to cause bloat. Using &lt;code&gt;pg_cron&lt;/code&gt; (if installed):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Run every hour; log tables where dead_pct &gt; 10%&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; cron&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;schedule&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;vacuum-watch&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;0 * * * *&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, $$&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; ops&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;vacuum_alerts&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (tablename, n_dead_tup, dead_pct, captured_at)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    tablename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    n_dead_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;    round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_dead_tup::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nullif&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_live_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_dead_tup, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_dead_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    AND&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; round&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_dead_tup::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; nullif&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_live_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_dead_tup, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_dead_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;$$);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Separately, a daily alert on &lt;code&gt;age(datfrozenxid)&lt;/code&gt; crossing 500 million gives operational lead time well before the 1.5-billion warning threshold.&lt;/p&gt;
&lt;p&gt;For the deeper argument on why autovacuum should be treated as a capacity planning problem rather than a maintenance task, see &lt;a href=&quot;https://rajivonai.com/blog/2025-09-13-autovacuum-is-a-capacity-problem-not-a-maintenance-task/&quot;&gt;Autovacuum Is a Capacity Problem, Not a Maintenance Task&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The foundation of what autovacuum is doing and why its defaults are sized the way they are is covered in &lt;a href=&quot;https://rajivonai.com/blog/2022-04-11-postgresql-autovacuum-what-every-engineer-should-know/&quot;&gt;PostgreSQL Autovacuum: What Every Engineer Should Know&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s autovacuum documentation describes the trigger formula directly: a table is eligible for autovacuum when &lt;code&gt;n_dead_tup &gt; autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor * pg_class.reltuples&lt;/code&gt;. The default &lt;code&gt;scale_factor&lt;/code&gt; of 0.2 was sized for databases where tables have at most a few million rows. For tables with tens or hundreds of millions of rows, the documented recommendation from PostgreSQL wiki is to lower &lt;code&gt;scale_factor&lt;/code&gt; to 0.01 or even 0.001 and raise &lt;code&gt;autovacuum_vacuum_threshold&lt;/code&gt; to a fixed low count.&lt;/p&gt;
&lt;p&gt;The documented pattern from the PostgreSQL MVCC documentation is that vacuum cannot remove a dead tuple that is still visible to any open transaction. This is not a bug — it is a consequence of snapshot isolation. The oldest running transaction’s &lt;code&gt;xmin&lt;/code&gt; forms the vacuum horizon; dead tuples older than that horizon cannot be reclaimed regardless of how aggressively autovacuum is configured.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Vacuum makes no progress despite running&lt;/td&gt;&lt;td&gt;Long-running transaction holds vacuum horizon&lt;/td&gt;&lt;td&gt;Terminate the blocking session; set &lt;code&gt;idle_in_transaction_session_timeout&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Autovacuum never triggers on large table&lt;/td&gt;&lt;td&gt;&lt;code&gt;scale_factor&lt;/code&gt; too high; threshold never crossed&lt;/td&gt;&lt;td&gt;Lower &lt;code&gt;scale_factor&lt;/code&gt; to 0.01 per-table&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;VACUUM FREEZE&lt;/code&gt; takes hours, blocks operations&lt;/td&gt;&lt;td&gt;Emergency freeze on a table with billions of rows&lt;/td&gt;&lt;td&gt;Run during maintenance window; break into table partition chunks if possible&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;cost_delay&lt;/code&gt; throttles vacuum below write rate&lt;/td&gt;&lt;td&gt;Default 20ms delay limits vacuum I/O to burst&lt;/td&gt;&lt;td&gt;Lower &lt;code&gt;cost_delay&lt;/code&gt; to 2ms and raise &lt;code&gt;cost_limit&lt;/code&gt; to 400 per-table&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Manual vacuum returns immediately with no work&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_activity&lt;/code&gt; shows active &lt;code&gt;xmin&lt;/code&gt; holding horizon&lt;/td&gt;&lt;td&gt;Wait for long transaction to close, then re-run vacuum&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Autovacuum falling behind grows bloat silently until queries slow, and eventually creates transaction ID wraparound risk that can force an emergency database shutdown.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Tune per-table &lt;code&gt;autovacuum_vacuum_scale_factor&lt;/code&gt; and &lt;code&gt;cost_delay&lt;/code&gt; for high-write tables, and set &lt;code&gt;idle_in_transaction_session_timeout&lt;/code&gt; to prevent long transactions from blocking the vacuum horizon.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: After applying per-table overrides, &lt;code&gt;last_autovacuum&lt;/code&gt; timestamps on affected tables should refresh within minutes, and &lt;code&gt;n_dead_tup&lt;/code&gt; should stabilize rather than grow between checks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Run the dead tuple query from Check 1 this week against your production database. If any table has &lt;code&gt;dead_pct &gt; 10%&lt;/code&gt; and a &lt;code&gt;last_autovacuum&lt;/code&gt; older than an hour, that table needs a per-table threshold override today.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;checklist&quot;&gt;Checklist&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Query &lt;code&gt;pg_stat_user_tables&lt;/code&gt; to identify tables with high &lt;code&gt;n_dead_tup&lt;/code&gt; and stale &lt;code&gt;last_autovacuum&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;pg_stat_activity&lt;/code&gt; for sessions in &lt;code&gt;idle in transaction&lt;/code&gt; state longer than 5 minutes&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;age(datfrozenxid)&lt;/code&gt; in &lt;code&gt;pg_database&lt;/code&gt; — alert if any value exceeds 500 million&lt;/li&gt;
&lt;li&gt;Verify &lt;code&gt;autovacuum = on&lt;/code&gt; is set globally in &lt;code&gt;postgresql.conf&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Check per-table &lt;code&gt;reloptions&lt;/code&gt; for existing autovacuum overrides on affected tables&lt;/li&gt;
&lt;li&gt;If no blocking transaction: run &lt;code&gt;VACUUM VERBOSE tablename&lt;/code&gt; and inspect output for horizon messages&lt;/li&gt;
&lt;li&gt;Apply per-table &lt;code&gt;autovacuum_vacuum_scale_factor = 0.01&lt;/code&gt; to any table with &gt; 10 million rows&lt;/li&gt;
&lt;li&gt;Apply per-table &lt;code&gt;autovacuum_vacuum_cost_delay = 2&lt;/code&gt; for high-write tables&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;xid_age &gt; 1.5 billion&lt;/code&gt;: schedule emergency &lt;code&gt;VACUUM FREEZE&lt;/code&gt; immediately&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;idle_in_transaction_session_timeout = &apos;5min&apos;&lt;/code&gt; in &lt;code&gt;postgresql.conf&lt;/code&gt; to prevent recurrence&lt;/li&gt;
&lt;li&gt;Verify changes with &lt;code&gt;pg_reload_conf()&lt;/code&gt; and re-check &lt;code&gt;pg_stat_user_tables&lt;/code&gt; after 15 minutes&lt;/li&gt;
&lt;li&gt;Add a monitoring alert for &lt;code&gt;n_dead_tup / n_live_tup &gt; 0.1&lt;/code&gt; on your largest tables&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>checklist</category><category>failures</category></item><item><title>PostgreSQL Statistics: Why the Optimizer Gets It Wrong</title><link>https://rajivonai.com/blog/2023-01-09-postgresql-statistics-why-the-optimizer-gets-it-wrong/</link><guid isPermaLink="true">https://rajivonai.com/blog/2023-01-09-postgresql-statistics-why-the-optimizer-gets-it-wrong/</guid><description>PostgreSQL&apos;s query planner depends entirely on per-column statistics that go stale after bulk loads — here is what that means for query plan quality and how to fix it.</description><pubDate>Mon, 09 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The PostgreSQL query planner does not look at your data. It looks at statistics about your data — histograms, most-common values, null fractions, and row count estimates stored in &lt;code&gt;pg_statistic&lt;/code&gt;. When those statistics are stale, the planner makes wrong decisions: it picks sequential scans over index scans, chooses nested loops over hash joins, and estimates 100 rows for a query that will return 10 million.&lt;/strong&gt; This is not a bug. It is an expected consequence of how cost-based optimization works, and it is entirely under operator control.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL builds query plans by estimating the cost of each possible execution path. Cost estimates depend on row count estimates, and row count estimates come from statistics. The statistics are not computed continuously — they are snapshots taken by &lt;code&gt;ANALYZE&lt;/code&gt; (or automatically by autovacuum’s analyze pass).&lt;/p&gt;
&lt;p&gt;Engineers typically encounter statistics problems in two situations. The first is after a bulk data load: a table that had 10,000 rows now has 10 million, but the planner still thinks it has 10,000 because &lt;code&gt;ANALYZE&lt;/code&gt; has not run since the load. The second is on tables with highly skewed distributions — a few values account for most rows, but the planner’s histogram does not have enough resolution to represent that accurately.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;PostgreSQL stores column statistics in &lt;code&gt;pg_statistic&lt;/code&gt;, exposed through the human-readable view &lt;code&gt;pg_stats&lt;/code&gt;. The key columns:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;most_common_vals&lt;/code&gt; — the N most frequent values and their frequencies (&lt;code&gt;most_common_freqs&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;histogram_bounds&lt;/code&gt; — bucket boundaries dividing the non-MCV value range into equal-frequency slices&lt;/li&gt;
&lt;li&gt;&lt;code&gt;null_frac&lt;/code&gt; — fraction of rows that are NULL&lt;/li&gt;
&lt;li&gt;&lt;code&gt;correlation&lt;/code&gt; — how well physical row order matches logical sort order (1.0 = perfectly sorted; near 0 = random)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The planner combines these to estimate how many rows will pass a given filter condition. When the statistics are accurate, estimates are close to reality. When they are stale, the estimates can be off by orders of magnitude.&lt;/p&gt;
&lt;p&gt;The documented failure mode from PostgreSQL’s query planning documentation: after a bulk insert of 10 million rows into a table whose last &lt;code&gt;ANALYZE&lt;/code&gt; ran when the table had 1,000 rows, the planner’s &lt;code&gt;reltuples&lt;/code&gt; estimate in &lt;code&gt;pg_class&lt;/code&gt; will still read approximately 1,000. A query with &lt;code&gt;WHERE id = $1&lt;/code&gt; on a now-large table may generate a sequential scan plan — because the planner believes the table is small and the index overhead is not worth it.&lt;/p&gt;
&lt;p&gt;The core question: which statistics settings should you tune, and when should you manually trigger &lt;code&gt;ANALYZE&lt;/code&gt;?&lt;/p&gt;
&lt;h2 id=&quot;how-statistics-collection-works&quot;&gt;How Statistics Collection Works&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;default_statistics_target&lt;/code&gt; controls how much detail is collected per column. The default is 100, meaning PostgreSQL tracks the 100 most common values and uses 100 histogram buckets. The valid range is 1 to 10,000.&lt;/p&gt;
&lt;p&gt;Increasing &lt;code&gt;default_statistics_target&lt;/code&gt; makes &lt;code&gt;ANALYZE&lt;/code&gt; slower and the statistics larger, but improves estimate accuracy for skewed distributions. For most tables, the default is fine. For columns used in highly selective filters — especially foreign keys, status columns with many distinct values, or columns where the top 100 values do not capture the actual distribution — increasing the target at the column level is the right lever:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; COLUMN &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; STATISTICS&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 500&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ANALYZE orders;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can observe what the planner currently knows about a column:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  attname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_distinct,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  most_common_vals,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  most_common_freqs,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  histogram_bounds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stats&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; tablename &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;orders&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; attname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;status&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;n_distinct&lt;/code&gt; tells you how many distinct values PostgreSQL believes exist. A value of -0.5 means the planner estimates 50% of rows have distinct values (common for primary keys). A positive value is a raw count. If this number looks wrong, the statistics are stale.&lt;/p&gt;
&lt;p&gt;After a bulk load, always run &lt;code&gt;ANALYZE&lt;/code&gt; explicitly before the new data receives production query traffic:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ANALYZE orders;           &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- whole table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ANALYZE orders (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- specific column only&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Autovacuum’s analyze pass uses &lt;code&gt;autovacuum_analyze_scale_factor&lt;/code&gt; (default: 0.2) and &lt;code&gt;autovacuum_analyze_threshold&lt;/code&gt; (default: 50). Same structural problem as vacuum thresholds: on a 50-million row table, autovacuum will not trigger &lt;code&gt;ANALYZE&lt;/code&gt; until 10 million rows have changed. For large bulk loads, waiting for autovacuum is not safe.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s query planner documentation (postgresql.org/docs/current/planner-stats.html) describes exactly how the planner uses &lt;code&gt;pg_statistic&lt;/code&gt; data: selectivity estimator functions read the statistics to produce row count estimates, and the planner chooses the lowest-cost plan based on those estimates combined with &lt;code&gt;seq_page_cost&lt;/code&gt;, &lt;code&gt;random_page_cost&lt;/code&gt;, and table and index size from &lt;code&gt;pg_class&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The correlation value in &lt;code&gt;pg_stats&lt;/code&gt; is particularly actionable: if &lt;code&gt;correlation&lt;/code&gt; for an indexed column is near 1.0 (data is physically sorted by that column), the planner will heavily favor index scans because random I/O effectively becomes sequential. If correlation is near 0 (random physical order), the planner may correctly prefer a sequential scan even for a highly selective query on a large table, because fetching scattered heap pages costs more than scanning the whole table with sequential I/O. Knowing this prevents incorrect index-forcing interventions.&lt;/p&gt;
&lt;p&gt;The documented pattern from PostgreSQL extended statistics documentation is that &lt;code&gt;CREATE STATISTICS&lt;/code&gt; (available since PostgreSQL 10) allows the planner to model correlations between columns — solving the multi-column selectivity problem that single-column histograms cannot handle. When a query filters on two correlated columns (e.g., &lt;code&gt;country&lt;/code&gt; and &lt;code&gt;city&lt;/code&gt;), single-column estimates multiply their selectivities independently, producing severely underestimated row counts.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Bulk insert without subsequent ANALYZE&lt;/td&gt;&lt;td&gt;Planner uses row counts from before the load; index scans may be abandoned for sequential scans on newly large tables&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_class.reltuples&lt;/code&gt; is only updated by ANALYZE; autovacuum’s analyze threshold may not trigger for hours&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Correlated columns with single-column statistics&lt;/td&gt;&lt;td&gt;Multi-column filter estimates are too optimistic; wrong join strategy chosen&lt;/td&gt;&lt;td&gt;Planner multiplies per-column selectivities independently, ignoring correlation between columns&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Partial index with no matching statistics&lt;/td&gt;&lt;td&gt;Planner cannot use the partial index’s selectivity correctly when the WHERE clause of the query partially matches the index predicate&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stats&lt;/code&gt; does not store per-partial-index statistics; planner falls back to whole-table estimates&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Stale statistics after bulk loads cause the planner to choose wrong execution plans — sequential scans where index scans are needed, or nested loops where hash joins would be correct.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Run &lt;code&gt;ANALYZE&lt;/code&gt; explicitly after every bulk load, reduce &lt;code&gt;autovacuum_analyze_scale_factor&lt;/code&gt; on large tables, and raise &lt;code&gt;statistics_target&lt;/code&gt; on highly selective or skewed columns.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Use &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; before and after &lt;code&gt;ANALYZE&lt;/code&gt; on a query affected by a bulk load — the estimated row counts in the plan should converge toward actual row counts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, query &lt;code&gt;SELECT tablename, last_analyze, last_autoanalyze, n_live_tup FROM pg_stat_user_tables ORDER BY last_analyze ASC NULLS FIRST LIMIT 20;&lt;/code&gt; and identify tables where statistics are old relative to write volume.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>failures</category></item><item><title>Azure E-Commerce Order Pipeline: Service Bus, Functions, SQL, and Cosmos DB</title><link>https://rajivonai.com/blog/2022-12-22-azure-e-commerce-order-pipeline-service-bus-functions-sql-and-cosmos-db/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-12-22-azure-e-commerce-order-pipeline-service-bus-functions-sql-and-cosmos-db/</guid><description>Azure checkout fails when order acceptance, payment, inventory reservation, and fulfillment are treated as one clean transaction — how Service Bus, Functions, Azure SQL, and Cosmos DB handle the recoverable steps that follow commitment.</description><pubDate>Thu, 22 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The checkout path does not fail because one service is slow. It fails because the system treats order acceptance, payment intent, inventory reservation, fulfillment, and customer visibility as one clean transaction when the cloud gives it queues, retries, leases, partitions, and partial failure.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A modern e-commerce order pipeline usually starts as a synchronous request: the customer submits a cart, the API validates it, and the platform records an order. That request feels simple because the customer sees one button.&lt;/p&gt;
&lt;p&gt;Behind it, the work is not simple. Payment authorization may involve an external provider. Inventory may live in a separate domain. Fraud checks may be asynchronous. Fulfillment may depend on warehouse systems. Customer notifications can fail independently. Analytics and support views need different read shapes from the write path.&lt;/p&gt;
&lt;p&gt;Azure gives teams a practical set of primitives for this split: Azure Service Bus for durable messaging, Azure Functions for event-driven compute, Azure SQL Database for transactional order state, and Azure Cosmos DB for low-latency read models or globally distributed customer views.&lt;/p&gt;
&lt;p&gt;The temptation is to wire them together directly: checkout API writes SQL, publishes a message, Functions consume it, Cosmos DB is updated, and everyone moves on.&lt;/p&gt;
&lt;p&gt;That is the happy path. Architecture starts when the happy path is no longer the interesting path.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The central failure is pretending that the database commit and the message publish are one atomic operation.&lt;/p&gt;
&lt;p&gt;If the checkout API writes the order to SQL and then crashes before publishing to Service Bus, the order exists but no downstream process sees it. If it publishes first and the SQL write fails, workers process an order that was never committed. If a Function retries after a timeout, the same message may execute twice. If Cosmos DB receives projections out of order, the customer page may show stale or contradictory status.&lt;/p&gt;
&lt;p&gt;Service Bus improves durability, but it does not remove distributed systems behavior. Messages can be retried. Handlers can crash after doing useful work but before completing the message. Dead-letter queues fill when poison messages are ignored. Azure Functions can scale out faster than a downstream SQL or payment dependency can absorb.&lt;/p&gt;
&lt;p&gt;SQL gives strong transactional semantics inside the database boundary. Cosmos DB gives partitioned, low-latency reads with tunable consistency. Neither gives a free cross-service transaction across the entire order lifecycle.&lt;/p&gt;
&lt;p&gt;The question is not: how do we make the order pipeline never fail?&lt;/p&gt;
&lt;p&gt;The real question is: where do we make failure explicit, durable, observable, and safe to retry?&lt;/p&gt;
&lt;h2 id=&quot;the-answer-transactional-core-asynchronous-edges&quot;&gt;The Answer: Transactional Core, Asynchronous Edges&lt;/h2&gt;
&lt;p&gt;A robust Azure order pipeline keeps the order of record in SQL, uses a transactional outbox to bridge SQL and Service Bus, makes every Function handler idempotent, and treats Cosmos DB as a projection rather than the source of truth.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[checkout API — validate cart] --&gt; B[SQL transaction — order and outbox]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[outbox publisher — claim pending events]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D[Service Bus topic — order accepted]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; E[Function — payment workflow]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; F[Function — inventory workflow]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; G[Function — projection workflow]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; H[SQL update — payment state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; I[SQL update — reservation state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; J[Cosmos DB — customer order view]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; K[dead letter queue — failed messages]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  H --&gt; L[Service Bus topic — order state changed]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; L&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  L --&gt; G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The checkout API should do the smallest durable thing possible. It validates the request, creates the order row, records the initial state, and inserts one or more outbox rows in the same SQL transaction. The response to the customer can be “order accepted” once the transaction commits. It should not depend on payment capture, warehouse confirmation, email delivery, or projection refresh.&lt;/p&gt;
&lt;p&gt;The outbox publisher is a separate process. It reads pending outbox rows, publishes them to Service Bus, and marks them as published. This can be an Azure Function on a timer, a WebJob, a containerized worker, or another background process. The important property is not the hosting model. The important property is that message publication is recovered from durable SQL state.&lt;/p&gt;
&lt;p&gt;Service Bus should use topics when multiple independent consumers need the same event. Payment, inventory, fulfillment, customer notifications, and read-model projection should not compete for one queue message if they each need to react to the same order fact. Subscriptions let each consumer own its own retry and dead-letter behavior.&lt;/p&gt;
&lt;p&gt;Each Function must be idempotent. The handler should assume it can receive the same logical event more than once. Use a stable event ID, order ID, and state transition key. Before applying work, check whether the transition has already been recorded. For external calls, persist the intent and provider correlation ID before depending on callback behavior.&lt;/p&gt;
&lt;p&gt;SQL remains the source of truth for the order aggregate: order state, payment state, inventory reservation state, fulfillment state, and the state machine that decides whether the order can advance. Cosmos DB should serve query-optimized views: customer order history, support dashboards, mobile order status, or regional read replicas. If Cosmos DB lags, the system is degraded, not corrupt.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; The documented Azure pattern is Queue-Based Load Leveling in the Microsoft Azure Architecture Center. Its core point is that a queue absorbs bursts so producers and consumers do not have to scale at exactly the same rate. In an order system, checkout traffic can spike during promotions while payment and inventory dependencies remain bounded.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Put Service Bus between order acceptance and downstream workflows. Configure subscription-level retry policies, lock durations, max delivery counts, and dead-letter handling. Scale Azure Functions with explicit concurrency limits when downstream dependencies are more fragile than the queue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The order API can commit accepted orders quickly while background processors drain work at a controlled rate. The result is not instant completion. The result is controlled backpressure.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; A queue is not just a transport. It is an operational boundary. Treating it as a hidden function call loses the main benefit.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; The documented Transactional Outbox pattern is widely used because local database transactions do not atomically include message brokers. Microsoft documents the pattern in Azure architecture guidance, and the same principle appears in microservices literature because the failure mode is structural, not vendor-specific.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Insert order state and outbox events in one SQL transaction. Publish later from the outbox table. Make publication retryable and make consumers deduplicate by event ID.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; A committed order cannot silently disappear from the pipeline because the event to publish is also committed. Duplicate publication is still possible, so consumers must remain idempotent.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The outbox does not create exactly-once processing. It creates recoverable at-least-once processing with a durable audit trail.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Azure Service Bus supports duplicate detection, message locks, delivery counts, and dead-letter queues. Azure Functions triggered by Service Bus complete messages only when the handler succeeds; failures can cause retry and eventual dead-lettering.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Design handlers so completing the message is the final step after durable state changes. Store processed message IDs or state transition records in SQL. Alert on dead-letter depth and age, not only on Function failures.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; A crash after updating SQL but before message completion becomes a duplicate delivery, not a double charge or double reservation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Idempotency is not optional ceremony. It is the price of using managed retries safely.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Cosmos DB is partitioned storage with tunable consistency. It is excellent for low-latency document reads, but cross-document modeling and partition-key choice drive correctness and cost.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Store projection documents by access pattern, such as customer ID plus order ID. Rebuild projections from SQL or event history when needed. Include projection version, source event ID, and last updated timestamp.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Customer-facing reads become fast and geographically scalable without making Cosmos DB the authority for order state transitions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; A read model should be disposable. If losing it would lose the business fact, it is not a read model.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Symptom&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;th&gt;Tradeoff&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;API commits SQL but publish fails&lt;/td&gt;&lt;td&gt;Order exists with no workflow activity&lt;/td&gt;&lt;td&gt;Transactional outbox&lt;/td&gt;&lt;td&gt;Requires publisher and outbox cleanup&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Function retries after partial success&lt;/td&gt;&lt;td&gt;Duplicate payment or reservation attempt&lt;/td&gt;&lt;td&gt;Idempotency key and transition log&lt;/td&gt;&lt;td&gt;More state and more checks per handler&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Service Bus backlog grows&lt;/td&gt;&lt;td&gt;Orders accepted faster than processed&lt;/td&gt;&lt;td&gt;Queue depth alerts and concurrency limits&lt;/td&gt;&lt;td&gt;Completion becomes eventually consistent&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Poison message loops&lt;/td&gt;&lt;td&gt;Same order fails until max delivery count&lt;/td&gt;&lt;td&gt;Dead-letter queue and replay tooling&lt;/td&gt;&lt;td&gt;Requires operational ownership&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cosmos projection lags&lt;/td&gt;&lt;td&gt;Customer page shows old status&lt;/td&gt;&lt;td&gt;Versioned projections and refresh path&lt;/td&gt;&lt;td&gt;Read model is not immediately consistent&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hot Cosmos partition&lt;/td&gt;&lt;td&gt;High RU consumption and throttling&lt;/td&gt;&lt;td&gt;Partition by customer or tenant access pattern&lt;/td&gt;&lt;td&gt;Some queries need fan-out or alternate views&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;SQL state machine is vague&lt;/td&gt;&lt;td&gt;Conflicting order states&lt;/td&gt;&lt;td&gt;Explicit transitions and constraints&lt;/td&gt;&lt;td&gt;More upfront domain modeling&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The dangerous part of the order pipeline is not the queue or the database in isolation. It is the handoff between durable state, asynchronous work, and external side effects.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Keep SQL as the transactional core, publish through an outbox, use Service Bus topics for independent workflows, make Functions idempotent, and project into Cosmos DB for reads.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The architecture follows documented cloud patterns: Queue-Based Load Leveling, Transactional Outbox, Competing Consumers, dead-letter handling, and CQRS-style read projections.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Start by modeling order state transitions in SQL, then add the outbox table, then wire Service Bus subscriptions, then build replayable Cosmos DB projections. Do not optimize the read model before the write path can survive retries.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Terraform for RDS and Aurora: What Should Be Automated and What Should Stay Manual</title><link>https://rajivonai.com/blog/2022-12-13-terraform-for-rds-and-aurora-what-should-be-automated-and-what-should-stay-manual/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-12-13-terraform-for-rds-and-aurora-what-should-be-automated-and-what-should-stay-manual/</guid><description>Database automation should encode the repetitive safety controls and leave judgment-heavy decisions to humans — what to automate in RDS and Aurora Terraform modules and what must stay gated on human review.</description><pubDate>Tue, 13 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The fastest way to lose confidence in database automation is to automate the parts that require judgment and leave the repetitive safety controls to humans.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Terraform is excellent at making infrastructure boring. A platform team can encode subnet groups, security groups, parameter groups, KMS keys, monitoring, backup retention, and tagging once, then let application teams request a database through a narrow interface. That is the right instinct. RDS and Aurora are infrastructure services, and infrastructure should be reproducible.&lt;/p&gt;
&lt;p&gt;But databases are not stateless compute. A bad EC2 instance replacement is usually a capacity event. A bad production database replacement can become data loss, downtime, or a recovery exercise. RDS and Aurora sit at the boundary between cloud control plane automation and stateful operational judgment.&lt;/p&gt;
&lt;p&gt;That boundary matters more as platform teams build self-service database modules. The module is not just a Terraform abstraction. It becomes the policy surface for encryption, backup posture, network placement, observability, deletion controls, and upgrade behavior. The design question is not “Can Terraform manage this?” It usually can. The better question is “Should a normal pull request be allowed to change this?”&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Many teams start with a single Terraform module that exposes every RDS and Aurora argument as a variable. That feels flexible, but it turns the module into a remote control for production state. A pull request can resize instances, change backup windows, replace parameter groups, alter maintenance behavior, disable deletion protection, or schedule an engine upgrade.&lt;/p&gt;
&lt;p&gt;Terraform plans are also not database runbooks. A plan can tell you that an engine version will change or a parameter group will be replaced. It cannot prove the application is compatible with the new optimizer behavior, that replication lag is acceptable, that connection pools will drain cleanly, or that the rollback path has been rehearsed.&lt;/p&gt;
&lt;p&gt;The failure mode is subtle. The team does not notice the automation boundary until an ordinary infrastructure workflow performs an extraordinary database operation. A change that should have required a maintenance window, stakeholder approval, and a tested restore path arrives as a green CI check.&lt;/p&gt;
&lt;p&gt;So the core question is: &lt;strong&gt;which RDS and Aurora changes belong in Terraform automation, and which should remain gated operational actions?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-automation-boundary&quot;&gt;The Automation Boundary&lt;/h2&gt;
&lt;p&gt;The answer is to automate the stable envelope and gate the stateful transitions.&lt;/p&gt;
&lt;p&gt;Terraform should own the database’s intended shape: network isolation, encryption, identity, monitoring, backup policy, deletion protection, parameter group definitions, option groups, log exports, tags, and alarms. These are controls that should converge toward a standard. They are also easy to review as policy.&lt;/p&gt;
&lt;p&gt;Terraform should not silently execute high-consequence transitions in production. Major version upgrades, restore decisions, failovers, blue-green switchovers, storage-class changes with uncertain impact, destructive replacement, and application schema migrations need runbooks. They may still be initiated by code, but they should be gated by explicit approval, preflight checks, and rollback criteria.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[database request — service owner] --&gt; B[Terraform module — platform contract]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[automated controls — network encryption backups monitoring]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; D[guardrails — deletion protection final snapshot policy]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[change classifier — routine or high consequence]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|routine change| F[CI plan — policy checks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Terraform apply — converged infrastructure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|high consequence| H[operations runbook — approval window rollback]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[preflight checks — backups replicas compatibility]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; J[controlled execution — upgrade restore switchover]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; K[post checks — health latency recovery point]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A practical module interface should make the safe path easy and the dangerous path hard. For production, use &lt;code&gt;deletion_protection = true&lt;/code&gt;, require final snapshots on destroy, set backup retention explicitly, enable enhanced monitoring or Performance Insights where appropriate, export database logs, and pin engine versions intentionally. Use CI policy to block disabling these controls outside a break-glass workflow.&lt;/p&gt;
&lt;p&gt;The module should also separate “definition” from “operation.” It is reasonable for Terraform to define an Aurora parameter group. It is riskier for an application team to merge a production parameter change that causes a restart without a maintenance plan. The same distinction applies to engine versions. Terraform can record the target version; the upgrade itself should be treated as a release event.&lt;/p&gt;
&lt;p&gt;This is not anti-automation. It is better automation. A manual step should not mean clicking around the console from memory. It should mean a documented workflow with named approvers, automated checks, explicit commands, and a stop condition.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS documents automated backups and point-in-time recovery as core RDS recovery mechanisms, including backup windows, snapshots, and restore to a selected time within the retention period. The documented pattern is that recovery posture must exist before an incident, not be assembled during one. See AWS Prescriptive Guidance on &lt;a href=&quot;https://docs.aws.amazon.com/prescriptive-guidance/latest/backup-recovery/rds.html&quot;&gt;backup and recovery for Amazon RDS&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Treat backup retention, backup windows, copy behavior, snapshot naming, and deletion protection as Terraform-owned controls. Require production modules to make these defaults non-optional unless a separate exception process exists.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The platform can review recovery posture in code, and every environment inherits the same minimum safety floor. Terraform is doing what it does well: keeping protective infrastructure from drifting.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Automate safety invariants before automating risky transitions. A restore workflow is only credible if the source backups, snapshots, encryption keys, and access controls were already standardized.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Terraform’s AWS provider exposes RDS lifecycle-sensitive arguments such as &lt;code&gt;deletion_protection&lt;/code&gt; and &lt;code&gt;skip_final_snapshot&lt;/code&gt; on &lt;code&gt;aws_db_instance&lt;/code&gt;. HashiCorp’s registry documents these as resource arguments, which means they can be changed through ordinary infrastructure review unless the platform blocks unsafe combinations. See the Terraform Registry documentation for &lt;a href=&quot;https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_instance&quot;&gt;&lt;code&gt;aws_db_instance&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Add policy checks that reject production plans where deletion protection is disabled, final snapshots are skipped, public accessibility is enabled without exception, or backup retention falls below the platform minimum.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The pull request becomes a review of intent, not a place where reviewers must remember every RDS footgun.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Terraform modules should encode the organization’s database posture, not merely expose the cloud provider API.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS documents RDS Blue/Green Deployments as a mechanism for safer database updates, including major version upgrades and switchovers. The documented pattern is still operational: create the green environment, validate it, then switch over under controlled conditions. See the Amazon RDS documentation for &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/blue-green-deployments.html&quot;&gt;blue-green deployments&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Keep blue-green creation and switchover behind a runbook or release workflow, even if Terraform defines surrounding infrastructure. Require application compatibility checks, replica health checks, monitoring baselines, and rollback criteria.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The team gets automation where it reduces toil, while preserving human judgment at the point where data-plane behavior changes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The dangerous moment is not creating infrastructure. It is changing which database production traffic trusts.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;







































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Decision&lt;/th&gt;&lt;th&gt;Automate with Terraform&lt;/th&gt;&lt;th&gt;Keep gated or manual&lt;/th&gt;&lt;th&gt;Why it breaks&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Subnet groups and security groups&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Deterministic network placement belongs in code.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;KMS encryption and log exports&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Security baselines should not depend on memory.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup retention and deletion protection&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Exception only&lt;/td&gt;&lt;td&gt;These are recovery invariants.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Minor version patching&lt;/td&gt;&lt;td&gt;Usually&lt;/td&gt;&lt;td&gt;Sometimes&lt;/td&gt;&lt;td&gt;Safe when tested and scheduled; risky for strict compatibility workloads.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Major engine upgrades&lt;/td&gt;&lt;td&gt;Define target carefully&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Compatibility, query plans, extensions, and rollback need validation.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Parameter group values&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Apply with care&lt;/td&gt;&lt;td&gt;Some parameters require reboot or change database behavior.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Instance class changes&lt;/td&gt;&lt;td&gt;Yes for non-prod&lt;/td&gt;&lt;td&gt;Gate in prod&lt;/td&gt;&lt;td&gt;Capacity changes can affect latency, failover, and cost.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Restores from snapshot or PITR&lt;/td&gt;&lt;td&gt;No for routine module apply&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Restore time and target selection are incident decisions.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Destroying production databases&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Destruction is never an ordinary convergence operation.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schema migrations&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Separate migration pipeline&lt;/td&gt;&lt;td&gt;Application data changes need ordering, locks, and rollback strategy.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The clean rule is this: Terraform owns desired infrastructure posture; operational workflows own irreversible or workload-sensitive transitions.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Database modules often expose too much raw RDS and Aurora control-plane power to ordinary pull requests.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Split the platform contract into automated guardrails and gated stateful operations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; AWS documents backups, point-in-time restore, and blue-green deployment as operational mechanisms; Terraform documents lifecycle-sensitive RDS arguments that must be constrained by module design and policy.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Audit the module interface this week. Lock production defaults for deletion protection, final snapshots, backup retention, encryption, log exports, and public access. Then move major upgrades, restores, switchovers, and destructive changes into explicit runbooks with automated preflight checks.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>automation</category><category>platform</category><category>ci-cd</category></item><item><title>Azure SQL vs Cosmos DB: The Partition Key Decision</title><link>https://rajivonai.com/blog/2022-11-22-azure-sql-vs-cosmos-db-the-partition-key-decision/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-11-22-azure-sql-vs-cosmos-db-the-partition-key-decision/</guid><description>The wrong Azure database choice announces itself when one tenant or region becomes hot enough to make every clean abstraction expensive — how to decide between Azure SQL and Cosmos DB based on access patterns, consistency needs, and operational cost.</description><pubDate>Tue, 22 Nov 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The wrong database choice usually announces itself late: not during schema design, but when one tenant, customer, region, or workflow becomes hot enough to make every clean abstraction look expensive.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Teams often frame Azure SQL versus Cosmos DB as a database-model decision: relational tables against JSON documents, joins against denormalization, SQL transactions against globally distributed NoSQL. That framing is useful, but incomplete.&lt;/p&gt;
&lt;p&gt;The harder question is operational. Azure SQL asks you to model consistency, indexing, and query shape around a relational engine. Cosmos DB asks you to model distribution first. The partition key is not a tuning knob in Cosmos DB. It is the boundary that determines where data lives, how requests are routed, how throughput is consumed, and which transactions are cheap.&lt;/p&gt;
&lt;p&gt;That difference matters because modern applications rarely fail evenly. A SaaS control plane might have thousands of quiet tenants and three enormous ones. A commerce system might have normal catalog traffic until one product launch concentrates writes. A telemetry platform might look horizontally scalable until every device in one fleet reports at the same minute.&lt;/p&gt;
&lt;p&gt;The database choice is not “SQL or NoSQL.” It is whether your dominant operational invariant is relational integrity or distributed access locality.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Azure SQL lets teams postpone some physical-design decisions. You can normalize first, add indexes later, tune queries, introduce read replicas, split hot tables, or shard after the access patterns prove themselves. Those moves are not free, but the engine gives you a strong relational baseline: constraints, joins, transactions, secondary indexes, and mature query planning.&lt;/p&gt;
&lt;p&gt;Cosmos DB moves the critical design decision earlier. A poor partition key can create hot partitions, expensive cross-partition queries, awkward transactions, and data models that cannot evolve without migration. A good partition key aligns with the request path: one logical operation touches one partition, consumes predictable request units, and avoids coordination.&lt;/p&gt;
&lt;p&gt;The trap is that the application model often suggests the wrong key. &lt;code&gt;tenantId&lt;/code&gt; feels natural for SaaS. &lt;code&gt;userId&lt;/code&gt; feels natural for personalization. &lt;code&gt;orderId&lt;/code&gt; feels natural for commerce. Each can be right, but only if it matches the workload’s heat distribution and transaction boundary.&lt;/p&gt;
&lt;p&gt;If the system needs relational integrity across many entities, Azure SQL absorbs that complexity better. If the system needs low-latency, high-scale access to independently partitionable records, Cosmos DB can be simpler operationally. The question is: which boundary will hurt more when the system is under load — relational coordination or partition imbalance?&lt;/p&gt;
&lt;h2 id=&quot;partition-around-the-operational-invariant&quot;&gt;Partition Around the Operational Invariant&lt;/h2&gt;
&lt;p&gt;A practical architecture starts by naming the unit of contention. That unit is not always the entity name in the domain model. It is the smallest boundary inside which the system needs fast reads, fast writes, and strong correctness.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Workload shape — read and write paths] --&gt; B[Correctness boundary — what must commit together]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt; C[Heat boundary — where traffic concentrates]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; D{Primary invariant}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|relational integrity| E[Azure SQL — constraints joins transactions]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|access locality| F[Cosmos DB — partition key document model]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G[Choose key — high cardinality even heat]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; H[Model requests — single partition first]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; I[Model schema — normalized core indexed paths]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; J[Scale plan — replicas pools sharding later]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use Azure SQL when the write path depends on relationships that must be enforced together: account balances, entitlement state, order lifecycle transitions, billing ledgers, or admin workflows where ad hoc queryability matters. The cost is that scale-out usually requires deliberate architecture: read replicas, elastic pools, caching, queue-backed writes, or sharding.&lt;/p&gt;
&lt;p&gt;Use Cosmos DB when the application can make one partition the natural home for most operations. The ideal partition key has high cardinality, even request distribution, and semantic alignment with the transaction boundary. The cost is that mistakes are structural. If every request hits one key, the system is partitioned in name only. If every query fans out across partitions, the document model has not removed coordination; it has moved it into the request path.&lt;/p&gt;
&lt;p&gt;The decision is clearest when written as a failure-mode table before implementation:&lt;/p&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Workload signal&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Azure SQL bias&lt;/th&gt;&lt;th align=&quot;right&quot;&gt;Cosmos DB bias&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Multi-entity transactions are common&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Strong&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Weak&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Queries change frequently&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Strong&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Weak&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Access pattern is stable and key-addressable&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Moderate&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Strong&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Traffic is globally distributed&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Moderate&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Strong&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hot tenants or hot users dominate traffic&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Needs sharding plan&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Needs synthetic key or redesign&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Data must be joined many ways&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Strong&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Weak&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Request latency depends on single-record lookups&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Moderate&lt;/td&gt;&lt;td align=&quot;right&quot;&gt;Strong&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context.&lt;/strong&gt; The documented Cosmos DB pattern is that partitioning is part of the logical data model, not merely infrastructure. Microsoft guidance emphasizes choosing a partition key that spreads request unit consumption and storage while supporting the application’s common queries and transactions. The documented system behavior is that items with the same logical partition key can be handled together more efficiently than operations that span many logical partitions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action.&lt;/strong&gt; For a SaaS workload, do not automatically choose &lt;code&gt;tenantId&lt;/code&gt;. First classify tenants by expected size, write rate, and query shape. If most operations are tenant-scoped and tenants are evenly sized, &lt;code&gt;tenantId&lt;/code&gt; may be correct. If a few tenants dominate traffic, a synthetic key such as &lt;code&gt;tenantId—bucketId&lt;/code&gt; may distribute heat, but it also changes query and transaction semantics. That tradeoff must be explicit, not discovered during an incident.&lt;/p&gt;
&lt;p&gt;For an order system, do not automatically choose &lt;code&gt;orderId&lt;/code&gt; either. It gives excellent point reads for a single order, but weak locality for customer history queries unless those queries are served by a separate projection. A common documented pattern in distributed systems is command-side and query-side separation: keep the write model optimized for correctness and maintain read models optimized for access paths.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result.&lt;/strong&gt; The result is not one universal database answer. It is a split architecture that often looks boring on purpose. Azure SQL owns relational control-plane state where constraints and cross-entity workflows matter. Cosmos DB owns high-volume, key-addressable documents where the partition key matches the dominant request path. Events or change feeds move data into projections when the read shape differs from the write shape.&lt;/p&gt;
&lt;p&gt;This is not polyglot persistence for fashion. It is an operational boundary. The system avoids forcing Azure SQL to behave like an infinitely distributed document store and avoids forcing Cosmos DB to behave like a relational engine with arbitrary joins.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; The partition key decision should happen after workload modeling, not after framework selection. The useful design artifact is a request matrix: operation, read keys, write keys, consistency requirement, expected cardinality, expected hot spots, and fallback behavior during partial failure. If that matrix shows many operations crossing partition boundaries, Cosmos DB is warning you early. If it shows many normalized entities changing together, Azure SQL is probably the simpler core.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Choice&lt;/th&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Azure SQL for everything&lt;/td&gt;&lt;td&gt;Hot tables, lock contention, expensive scale-up, read pressure&lt;/td&gt;&lt;td&gt;Index deliberately, separate read paths, use queues, plan sharding before emergency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cosmos DB for relational workflows&lt;/td&gt;&lt;td&gt;Cross-partition queries, duplicated state, weak ad hoc reporting, difficult migrations&lt;/td&gt;&lt;td&gt;Keep relational core in SQL, use Cosmos for projections or bounded aggregates&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;tenantId&lt;/code&gt; partition key&lt;/td&gt;&lt;td&gt;One large tenant becomes a hot partition&lt;/td&gt;&lt;td&gt;Use synthetic partitioning, isolate large tenants, or route premium tenants to dedicated containers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;userId&lt;/code&gt; partition key&lt;/td&gt;&lt;td&gt;Shared workflows require fan-out across many users&lt;/td&gt;&lt;td&gt;Add workflow-centric projections or choose a higher-level aggregate key&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;orderId&lt;/code&gt; partition key&lt;/td&gt;&lt;td&gt;Customer and support queries become cross-partition scans&lt;/td&gt;&lt;td&gt;Maintain customer-order read models keyed by customer&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Synthetic partition key&lt;/td&gt;&lt;td&gt;Better distribution but harder transactions and reads&lt;/td&gt;&lt;td&gt;Make bucket logic deterministic and visible in the domain model&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Dual stores&lt;/td&gt;&lt;td&gt;Consistency lag and operational complexity&lt;/td&gt;&lt;td&gt;Define source of truth, idempotent events, replay process, and reconciliation checks&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The database decision is being made from data shape alone. Add workload shape: request paths, write contention, query volatility, transaction boundaries, tenant skew, and failure behavior.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Choose Azure SQL when relational correctness is the primary invariant. Choose Cosmos DB when access locality and horizontal distribution are the primary invariant. Use both only when the boundary is explicit.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Build a request matrix before implementation. For every critical operation, identify whether it is single-row, single-aggregate, single-partition, cross-partition, or cross-entity. The painful cells usually reveal the right database.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Decide the partition key before writing production code. Then test the ugly cases: largest tenant, hottest key, cross-partition query, backfill, replay, support lookup, and schema migration. A partition key that survives those tests is architecture. A partition key chosen from the entity diagram is a guess.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Backups Are Not Recovery: The DBA Rule Everyone Learns Late</title><link>https://rajivonai.com/blog/2022-11-14-backups-are-not-recovery/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-11-14-backups-are-not-recovery/</guid><description>A backup file proves you captured data. Recovery is the process of producing a running, consistent database on a different system inside your RTO. They are not the same thing, and confusing them is how incidents get worse.</description><pubDate>Mon, 14 Nov 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A backup file is not proof of recoverability. It is proof that data was written to storage at a point in time. Recovery is the separate process of taking that file and producing a running, consistent database on a different system within your RTO. Engineers who conflate the two discover the gap during an actual incident — the worst possible time to find it.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most teams running production databases configure some form of backup. Nightly &lt;code&gt;pg_dump&lt;/code&gt; jobs, Aurora snapshots, &lt;code&gt;xtrabackup&lt;/code&gt; runs around low-traffic windows — the mechanics are straightforward. Monitoring confirms the job completed without error.&lt;/p&gt;
&lt;p&gt;That confirmation covers one half of the contract. It says data left the system. It says nothing about restore time, or whether WAL segments and encryption keys are available in the same failure scenario that just took down the primary.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The documented failure mode: a team runs nightly &lt;code&gt;pg_dump&lt;/code&gt;, stores output to S3, and considers their backup strategy complete. During a corruption event, they initiate a restore and discover that &lt;code&gt;pg_dump&lt;/code&gt; replays every row as SQL against a cold instance — on a large database, hours of work. With no WAL archives stored, there is no PITR capability either.&lt;/p&gt;
&lt;p&gt;The backup was real. The recovery was not viable within their RTO.&lt;/p&gt;
&lt;p&gt;The question every team must answer before an incident: have you timed a full restore on target hardware, and does that number fit inside your recovery time objective?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;RPO and RTO are different constraints governed by different mechanics.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;RPO (Recovery Point Objective)&lt;/strong&gt; is how much data loss is acceptable. A nightly backup gives an RPO of up to 24 hours. An RPO of minutes requires continuous WAL archiving (PostgreSQL) or binary log shipping (MySQL). Aurora documents this explicitly — PITR to any second within the retention window is only possible because Aurora streams redo logs continuously, not because snapshots run frequently.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;RTO (Recovery Time Objective)&lt;/strong&gt; is how long you can be down. It is determined by restore speed, not backup frequency.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Primary Database] --&gt;|Writes data| B[Base Backup]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A --&gt;|Streams changes| C[WAL Archive]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; D[Disaster Recovery Target]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|Replays until PITR| D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; E[Recovered Database]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Backup type&lt;/th&gt;&lt;th&gt;Restore speed&lt;/th&gt;&lt;th&gt;PITR capable&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Logical — &lt;code&gt;pg_dump&lt;/code&gt;, &lt;code&gt;mysqldump&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Slow — replays SQL row by row&lt;/td&gt;&lt;td&gt;No, without WAL or binlog archiving&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Physical — &lt;code&gt;pg_basebackup&lt;/code&gt;, &lt;code&gt;xtrabackup&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Fast — copies raw data files&lt;/td&gt;&lt;td&gt;Yes, when WAL or binlog archiving is configured&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cloud snapshot — Aurora, RDS&lt;/td&gt;&lt;td&gt;Fast — clones at storage layer&lt;/td&gt;&lt;td&gt;Yes, when continuous backup is enabled&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;PostgreSQL’s documentation for &lt;code&gt;pg_basebackup&lt;/code&gt; describes its output as a binary copy of the data directory that a new instance can start from directly — bypassing the replay overhead that makes logical restores slow. For large databases, the difference is not marginal.&lt;/p&gt;
&lt;p&gt;Three additional gaps close the trap:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Same-region backup storage.&lt;/strong&gt; A regional disruption takes out both the database and the S3 bucket if they share a region. A backup unavailable during the failure it is meant to cover is not a recovery asset.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Logical backup without WAL archiving.&lt;/strong&gt; A &lt;code&gt;pg_dump&lt;/code&gt; taken at 2:00 AM returns you to 2:00 AM state. If corruption happened at 11:58 PM, 22 hours of data are gone. PITR requires WAL archiving in PostgreSQL or binary logging in MySQL, both enabled explicitly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Encryption key in the failed system.&lt;/strong&gt; If the key lives in the same environment that just failed or was compromised, the backup cannot be decrypted. Key management must be independent of the system being protected.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s &lt;code&gt;pg_basebackup&lt;/code&gt; documentation notes that WAL files generated during and after the backup are required for consistency — WAL archiving is the prerequisite for any PITR capability in self-managed PostgreSQL.&lt;/p&gt;
&lt;p&gt;Percona’s XtraBackup documentation describes a hot physical backup that does not block writes. It records the binary log position at the backup’s end — the anchor required for point-in-time recovery in MySQL and MariaDB.&lt;/p&gt;
&lt;p&gt;Amazon Aurora’s PITR documentation states that restores create a new DB cluster, not an in-place restoration. Applications must re-point to the new endpoint after a PITR restore — a step that surprises engineers who have never run the procedure under pressure.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Untested restore&lt;/td&gt;&lt;td&gt;RTO is unknown until the incident&lt;/td&gt;&lt;td&gt;Restore time was assumed, never measured on comparable hardware&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Same-region backup storage&lt;/td&gt;&lt;td&gt;Backup unavailable during regional failure&lt;/td&gt;&lt;td&gt;S3 bucket and database instance share the same AWS region&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Logical backup without WAL archiving&lt;/td&gt;&lt;td&gt;No PITR capability&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_dump&lt;/code&gt; is a point-in-time snapshot; intermediate recovery requires WAL or binlog&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Encryption key in the same environment&lt;/td&gt;&lt;td&gt;Cannot decrypt backup during recovery&lt;/td&gt;&lt;td&gt;Key management system is part of the failed or compromised system&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: A backup job completing successfully does not mean recovery is possible within your RTO.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Treat backup and recovery as separate contracts — configure WAL archiving for PITR, store backups cross-region, and time a full restore on comparable hardware.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: A timed restore drill producing a running, queryable database at a point in time before a simulated event, completed inside your documented RTO.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, identify your largest production database and determine how long a full restore would take with your current backup type. If you have never timed it, schedule the drill now.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The backup proves data was written somewhere. The only thing that proves recovery is doing it.&lt;/p&gt;</content:encoded><category>databases</category><category>failures</category><category>checklist</category></item><item><title>Azure Reference Architecture: Front Door, App Service, SQL, Cache, and Service Bus</title><link>https://rajivonai.com/blog/2022-11-07-azure-reference-architecture-front-door-app-service-sql-cache-and-service-bus/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-11-07-azure-reference-architecture-front-door-app-service-sql-cache-and-service-bus/</guid><description>Azure applications typically fail first at the edges: Front Door configuration, App Service connection pools, SQL failover groups, Redis cache invalidation, and Service Bus backlog — a reference architecture that makes these failure boundaries explicit.</description><pubDate>Mon, 07 Nov 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A cloud application usually fails at the boundaries first: the global edge, the web tier, the database connection pool, the cache invalidation path, and the asynchronous backlog nobody watched until users were already waiting.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A common Azure production stack looks deceptively simple. Azure Front Door terminates global traffic. Azure App Service runs the application. Azure SQL Database stores transactional state. Azure Cache for Redis absorbs hot reads and coordination pressure. Azure Service Bus decouples slow work from request latency.&lt;/p&gt;
&lt;p&gt;On a reference diagram, that stack reads like a clean web architecture. Requests come in through the edge, application instances scale horizontally, the database remains managed, cache keeps latency low, and messages handle deferred processing. The managed services remove server maintenance, but they do not remove distributed systems behavior.&lt;/p&gt;
&lt;p&gt;The operational shift is that the application team no longer owns machines. It owns failure boundaries. Front Door can route to an unhealthy origin if health probes are weak. App Service can scale out faster than the database can absorb connections. SQL can throttle before the web tier notices. Redis can become a correctness dependency instead of a performance aid. Service Bus can preserve work while hiding a downstream outage behind a growing queue.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure mode is not that any one Azure service is unreliable. The failure mode is believing the services compose into reliability automatically.&lt;/p&gt;
&lt;p&gt;A synchronous request path couples Front Door, App Service, SQL, and Redis into a single user-visible transaction. If one component slows down, the others begin amplifying the problem. App instances retry database calls. Retries consume more connection slots. Cache misses stampede into SQL. Service Bus publishers continue accepting work that workers cannot drain. Health probes remain green because the process still returns HTTP 200 on a shallow endpoint.&lt;/p&gt;
&lt;p&gt;The design question is therefore not, “Which Azure services should be on the diagram?” The question is: where does the architecture absorb failure without making the user, database, or operators pay for it?&lt;/p&gt;
&lt;h2 id=&quot;the-reference-architecture&quot;&gt;The Reference Architecture&lt;/h2&gt;
&lt;p&gt;The practical answer is to treat the stack as five control points: edge admission, request execution, state protection, read pressure relief, and asynchronous load shedding.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    U[user request] --&gt; F[Azure Front Door — global entry]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; WAF[WAF policy — edge filtering]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WAF --&gt; APP[App Service — stateless web tier]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    APP --&gt; CACHE[Azure Cache for Redis — hot read path]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    APP --&gt; SQL[Azure SQL Database — transactional system of record]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    APP --&gt; BUS[Azure Service Bus — deferred work]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    BUS --&gt; WORKER[App Service worker — queue consumer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WORKER --&gt; SQL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    WORKER --&gt; CACHE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MON[observability — traces metrics logs] --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MON --&gt; APP&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MON --&gt; SQL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MON --&gt; CACHE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MON --&gt; BUS&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Azure Front Door should be the global admission layer, not just a vanity endpoint. It owns TLS, WAF policy, routing, and origin failover. Its health probes should test an application dependency profile that is meaningful enough to prevent routing to broken origins, but cheap enough not to become a synthetic load generator.&lt;/p&gt;
&lt;p&gt;App Service should stay stateless. Instances can scale out, restart, or move without requiring local session recovery. Any per-user state belongs in signed tokens, SQL, or a deliberately bounded cache entry. Deployment slots should be used for controlled rollouts, but slot swaps are not a replacement for backward-compatible schema and message contracts.&lt;/p&gt;
&lt;p&gt;Azure SQL Database should remain the source of truth. The application should protect it with connection limits, query timeouts, bounded retries, and circuit breakers. Retry policies must use jitter and must distinguish transient failures from sustained overload. A retry that makes sense for a single request can become an outage multiplier when thousands of instances execute it together.&lt;/p&gt;
&lt;p&gt;Azure Cache for Redis should reduce read pressure, not own correctness by accident. Cache entries need explicit TTLs, versioning where appropriate, and a safe miss path. If the cache is unavailable, the application should either degrade intentionally or shed nonessential features. It should not stampede SQL with every cache miss at once.&lt;/p&gt;
&lt;p&gt;Azure Service Bus should absorb work that does not need to complete inside the user request. It gives the architecture a buffer, but the buffer must be observable. Queue depth, message age, dead-letter count, handler failure rate, and drain time are production signals, not dashboard decoration.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Microsoft’s Azure Architecture Center documents this exact shape as a common web application pattern: a global entry service, an application hosting tier, managed data stores, caching, messaging, and centralized monitoring. Azure Well-Architected guidance repeatedly separates reliability concerns into redundancy, health modeling, retry behavior, and operational observability.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The documented pattern is to make the web tier stateless, put durable state in a managed database, use cache for performance-sensitive reads, and move long-running work onto a queue. In Azure terms, that usually means App Service instances behind Front Door, Azure SQL for transactional data, Azure Cache for Redis for hot data, and Service Bus for asynchronous workflows.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The architecture gains independent scaling axes. Front Door can manage global routing and edge protection. App Service can scale request handlers. SQL can be sized and tuned around transactional load. Redis can absorb repeated reads. Service Bus can preserve work during downstream slowness.&lt;/p&gt;
&lt;p&gt;The result is not automatic resilience. It is separability. Each layer can now have its own timeout, quota, alert, and recovery mechanism.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; The pattern works when every boundary has an explicit contract. Front Door needs a real origin health model. App Service needs bounded concurrency and dependency timeouts. SQL needs query discipline and connection governance. Redis needs a cache consistency strategy. Service Bus needs poison message handling and backlog SLOs.&lt;/p&gt;
&lt;p&gt;A documented reference architecture is a starting point. The production architecture is the reference design plus the failure policies.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it happens&lt;/th&gt;&lt;th&gt;Architectural response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Healthy process, broken dependency&lt;/td&gt;&lt;td&gt;Health endpoint only checks the web process&lt;/td&gt;&lt;td&gt;Add dependency-aware readiness with cheap critical checks&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Retry storm&lt;/td&gt;&lt;td&gt;App instances retry the same overloaded dependency&lt;/td&gt;&lt;td&gt;Use bounded retries, jitter, circuit breakers, and budgets&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;SQL connection exhaustion&lt;/td&gt;&lt;td&gt;Scale-out creates more concurrent database clients&lt;/td&gt;&lt;td&gt;Cap pool sizes, tune queries, and limit request concurrency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cache stampede&lt;/td&gt;&lt;td&gt;Popular key expires and all instances miss together&lt;/td&gt;&lt;td&gt;Use TTL jitter, request coalescing, and stale-while-revalidate where safe&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Queue hides outage&lt;/td&gt;&lt;td&gt;Service Bus accepts messages faster than workers drain them&lt;/td&gt;&lt;td&gt;Alert on message age, queue depth, dead letters, and drain time&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Poison messages block progress&lt;/td&gt;&lt;td&gt;One malformed job repeatedly fails&lt;/td&gt;&lt;td&gt;Use max delivery counts, dead-letter queues, and replay tooling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Slot swap breaks contracts&lt;/td&gt;&lt;td&gt;New code assumes new schema or message format&lt;/td&gt;&lt;td&gt;Use expand-contract migrations and versioned message handlers&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Edge failover is too late&lt;/td&gt;&lt;td&gt;Front Door probes do not match user-visible failure&lt;/td&gt;&lt;td&gt;Probe critical paths and tune origin failover thresholds&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The main risk in this architecture is hidden coupling. The diagram says the services are separate, but runtime behavior can still bind them into one failure domain.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Put explicit policies at every boundary: admission control at Front Door, concurrency limits in App Service, timeouts around SQL, cache degradation rules for Redis, and backlog controls for Service Bus.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Test the failure modes directly. Disable Redis in a staging environment. Force SQL throttling. Slow the queue consumer. Return failed readiness from one origin. Confirm that alerts fire before users become the monitoring system.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Build the first production checklist around five questions: what gets rejected at the edge, what times out in the app, what protects SQL, what happens when cache is missing, and how long Service Bus can fall behind before the business notices.&lt;/p&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>AWS Multi-Region Failover: Route 53, Global Accelerator, Aurora, and DynamoDB Global Tables</title><link>https://rajivonai.com/blog/2022-10-23-aws-multi-region-failover-route-53-global-accelerator-aurora-and-dynamodb-global-tables/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-10-23-aws-multi-region-failover-route-53-global-accelerator-aurora-and-dynamodb-global-tables/</guid><description>AWS multi-region failover fails most often in traffic steering, write promotion, and schema drift — how Route 53, Global Accelerator, Aurora global databases, and DynamoDB global tables behave under a real regional failure.</description><pubDate>Sun, 23 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Multi-region failover fails most often in the parts teams assumed were automatic: traffic steering, write ownership, schema drift, and the human decision to promote a secondary system.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Most AWS multi-region designs start with a reasonable fear: one region can become unavailable, impaired, partitioned, or operationally unsafe to use. The business wants continuity. The engineering team wants a design that can move traffic elsewhere without rewriting the application during an incident.&lt;/p&gt;
&lt;p&gt;AWS gives several building blocks that look like they solve the problem independently. Route 53 can steer DNS traffic based on health checks. AWS Global Accelerator can route users through the AWS edge network to healthy regional endpoints. Aurora Global Database can replicate relational data across regions with a primary writer and secondary readers. DynamoDB global tables can replicate items across regions with active-active writes.&lt;/p&gt;
&lt;p&gt;The trap is treating these as interchangeable failover tools. They are not. They operate at different layers, with different consistency models, different failure detection semantics, and different operational blast radii.&lt;/p&gt;
&lt;p&gt;A serious architecture has to decide which layer owns failover, which data stores are allowed to accept writes, and which recovery objective matters more: minimizing downtime or preventing incorrect writes.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The hard part of multi-region failover is not detecting that a region is broken. The hard part is proving that the replacement region is safe to make authoritative.&lt;/p&gt;
&lt;p&gt;DNS failover can move new clients, but cached answers and long-lived connections continue to exist. Global Accelerator can shift traffic faster at the network edge, but it cannot make a database replica writable or resolve application-level corruption. Aurora can replicate relational changes to another region, but the secondary is not automatically equivalent to a fully promoted primary. DynamoDB global tables can accept writes in multiple regions, but conflict resolution becomes part of the application contract.&lt;/p&gt;
&lt;p&gt;The most dangerous failure mode is split ownership. One region believes it is still primary while another region has been promoted. That creates double writes, divergent state, idempotency failures, and reconciliation work that may exceed the original outage.&lt;/p&gt;
&lt;p&gt;The second failure mode is partial failover. The load balancer moves traffic, but background workers, queues, scheduled jobs, secrets, feature flags, and observability pipelines still point at the old region. The user-facing path appears recovered while the system quietly loses work.&lt;/p&gt;
&lt;p&gt;The third failure mode is false confidence from successful read failover. Serving stale or read-only traffic from a secondary region is useful, but it is not the same as accepting new orders, payments, writes, or irreversible workflow transitions.&lt;/p&gt;
&lt;p&gt;The core question is: which part of the system is allowed to decide that a different region is now the source of truth?&lt;/p&gt;
&lt;h2 id=&quot;the-answer-separate-traffic-failover-from-authority-failover&quot;&gt;The Answer: Separate Traffic Failover from Authority Failover&lt;/h2&gt;
&lt;p&gt;A resilient design separates four concerns: client entry, regional application health, relational write authority, and globally replicated key-value state.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  U[users] --&gt; E[edge entry — Route 53 or Global Accelerator]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; A[primary region — application fleet]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; B[standby region — application fleet]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A --&gt; C[Aurora primary — write authority]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D[Aurora secondary — replicated reader]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A --&gt; G[DynamoDB global table — regional replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; H[DynamoDB global table — regional replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; H&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; I[promotion runbook — controlled authority change]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  I --&gt; J[new Aurora primary — writes enabled]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; J&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Route 53 and Global Accelerator should answer the question, “Where should clients enter the system?” They should not answer, “Which region owns the data?”&lt;/p&gt;
&lt;p&gt;Route 53 failover is a good fit when DNS-level steering is acceptable and the application can tolerate resolver caching behavior. It is simple, widely understood, and integrates with health checks. The operational cost is that failover is not instantaneous for every client, because DNS answers can live beyond the moment when health changes.&lt;/p&gt;
&lt;p&gt;Global Accelerator is better when fast traffic steering and stable anycast IP addresses matter. It routes traffic to healthy endpoints and can reduce dependency on DNS propagation behavior. It is still a traffic-entry mechanism. It does not remove the need to validate that the standby application, dependencies, and data layer are ready.&lt;/p&gt;
&lt;p&gt;Aurora Global Database should usually be treated as single-writer infrastructure. The primary region owns relational writes. Secondary regions can serve reads, support low-latency reporting, and become candidates for promotion. Promotion should be explicit, automated through a runbook, and guarded by checks: replication lag, schema version, migration state, job ownership, and write fences.&lt;/p&gt;
&lt;p&gt;DynamoDB global tables fit a different class of data. They are useful for regional session state, user preferences, idempotency records, distributed configuration, and workloads that can tolerate or resolve last-writer behavior. They are not a magic replacement for relational consistency. If an item can be updated concurrently in two regions, the application must be designed around that possibility.&lt;/p&gt;
&lt;p&gt;The practical architecture is often active-passive for relational writes and active-active for carefully selected DynamoDB tables. That gives the standby region enough live behavior to stay warm without pretending every data model supports multi-master writes.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS documents Route 53 health checks and failover routing as DNS-based mechanisms for directing traffic away from unhealthy endpoints. The documented pattern is traffic steering based on health, not transactional correctness.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Use Route 53 failover records only for endpoints whose health checks represent the full serving path. A shallow health check that returns &lt;code&gt;200&lt;/code&gt; while the application cannot write to its database is worse than no health check. For write-heavy systems, expose a regional readiness endpoint that checks dependency reachability, migration compatibility, queue access, and whether the region is currently authorized to accept writes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The failover decision becomes tied to user-visible capability rather than instance uptime. DNS still has caching behavior, so recovery expectations must be expressed as ranges, not promises of immediate global convergence.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Route 53 is useful for regional steering, but it should be downstream of an authority model. It cannot decide whether Aurora has been safely promoted.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS Global Accelerator is documented as an edge networking service that routes traffic to healthy regional endpoints using static anycast IP addresses. The pattern is faster network-level steering through AWS edge locations.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Put Global Accelerator in front of regional load balancers when fast endpoint withdrawal matters. Keep regional health checks strict, and avoid using accelerator failover as a substitute for application readiness. During an incident, the accelerator can stop sending new traffic to a region, but existing stateful workflows still need application-level recovery.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Client entry becomes less dependent on DNS resolver behavior. The system still needs a separate plan for database promotion, queue replay, and regional write fencing.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Global Accelerator improves traffic movement. It does not change the consistency model of the backing services.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Aurora Global Database is documented around one primary AWS Region for writes and secondary regions for low-latency reads and disaster recovery. The known behavior is asynchronous cross-region replication with promotion of a secondary when the primary is unavailable or intentionally moved.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Treat Aurora promotion as an authority-changing operation. Before promotion, fence old writers if possible, stop regional workers that can mutate state, check replication lag, verify schema version, and record the promotion decision in an operational log. After promotion, update application configuration so only the new primary receives relational writes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The system avoids the worst failure mode: two regions writing to different relational primaries. Recovery may take longer than pure traffic failover, but the data outcome is more defensible.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; For relational data, correctness usually deserves a human-approved or strongly guarded automated step. Fast failover that corrupts state is not resilience.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; DynamoDB global tables are documented as multi-region, multi-active replication. AWS documents conflict handling through last-writer-wins reconciliation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Use global tables for data models where concurrent regional writes are acceptable or naturally idempotent. Good candidates include session records, request deduplication keys, feature exposure state, and user-local metadata. Avoid putting strongly ordered financial ledgers or relational aggregates into global tables unless the application owns conflict resolution explicitly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The standby region can serve meaningful live traffic before Aurora promotion. Some state remains close to users and resilient to regional failure, while strict relational state stays under single-writer control.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Active-active data is an application contract, not a checkbox. If the business cannot explain the conflict rule, the table should not accept writes in multiple regions.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;What happens&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Health check lies&lt;/td&gt;&lt;td&gt;Traffic moves to a region that is alive but not capable&lt;/td&gt;&lt;td&gt;Check real dependencies and regional write authority&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DNS cache delay&lt;/td&gt;&lt;td&gt;Some clients keep using the old endpoint&lt;/td&gt;&lt;td&gt;Use low TTLs where appropriate, and consider Global Accelerator for faster steering&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aurora split brain&lt;/td&gt;&lt;td&gt;Two regions accept relational writes&lt;/td&gt;&lt;td&gt;Fence writers and make promotion explicit&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replication lag&lt;/td&gt;&lt;td&gt;Secondary region is missing recent writes&lt;/td&gt;&lt;td&gt;Measure lag before promotion and define acceptable data loss&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Global table conflict&lt;/td&gt;&lt;td&gt;Two regions update the same item&lt;/td&gt;&lt;td&gt;Design idempotent writes or explicit conflict handling&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Background jobs stay active&lt;/td&gt;&lt;td&gt;Workers mutate state in the failed or old primary region&lt;/td&gt;&lt;td&gt;Add regional job leases and disable old workers during promotion&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schema drift&lt;/td&gt;&lt;td&gt;Standby app version does not match database state&lt;/td&gt;&lt;td&gt;Make migrations region-aware and verify version before traffic shift&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Observability gap&lt;/td&gt;&lt;td&gt;The team cannot prove which region is authoritative&lt;/td&gt;&lt;td&gt;Emit authority state, promotion events, and regional dependency status&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Traffic failover and data authority are often bundled together, which creates split ownership during incidents.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Use Route 53 or Global Accelerator for entry-point steering, Aurora Global Database for controlled relational promotion, and DynamoDB global tables only for data models that tolerate multi-region writes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The documented AWS patterns line up with this separation: DNS and edge services steer traffic, Aurora preserves a primary-writer model, and DynamoDB global tables replicate active-active items with conflict semantics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Write the failover runbook before the next incident. Include health-check definitions, writer fencing, Aurora promotion steps, DynamoDB conflict assumptions, queue and worker behavior, rollback rules, and a game day that proves the standby region can become authoritative without data ambiguity.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Redis Memory Eviction Policies Explained</title><link>https://rajivonai.com/blog/2022-10-10-redis-memory-eviction-policies-explained/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-10-10-redis-memory-eviction-policies-explained/</guid><description>Redis has eight eviction policies and a maxmemory limit. The policy you pick determines whether your cache degrades safely or silently corrupts your hit rate under load.</description><pubDate>Mon, 10 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Redis does not manage memory for you.&lt;/strong&gt; You set a &lt;code&gt;maxmemory&lt;/code&gt; limit, choose an eviction policy, and Redis enforces both mechanically. Skip those settings and Redis will grow until the OS kills it, reject every write when the limit is hit, or silently evict keys you expected to stay cached. That is not a tuning detail — it is the difference between a cache that degrades gracefully and one that breaks applications under load.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A typical Redis cache deployment sets keys with TTLs, adds a &lt;code&gt;maxmemory&lt;/code&gt; directive, and moves on. The assumption is that Redis will handle the rest.&lt;/p&gt;
&lt;p&gt;Redis exposes eviction policy as an explicit operator decision because different workloads have different requirements for which keys are safe to drop. A session store, a product catalog cache, and a rate-limiter all need different behavior at the eviction boundary. Redis gives you control, but that control requires a deliberate choice.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure modes appear only under sustained write pressure. When &lt;code&gt;maxmemory&lt;/code&gt; is not set, Redis accepts all writes until the host runs out of memory and the OOM killer terminates the process. When &lt;code&gt;noeviction&lt;/code&gt; is set and the limit is reached, Redis returns &lt;code&gt;OOM command not allowed when used memory &gt; &apos;maxmemory&apos;&lt;/code&gt; on every write. When &lt;code&gt;volatile-lru&lt;/code&gt; is configured but no keys have TTLs, Redis cannot find eligible keys and silently falls back to &lt;code&gt;noeviction&lt;/code&gt; behavior.&lt;/p&gt;
&lt;p&gt;Which policy fits your workload, and where does each one fail?&lt;/p&gt;
&lt;h2 id=&quot;how-eviction-works&quot;&gt;How Eviction Works&lt;/h2&gt;
&lt;p&gt;When a write arrives and memory is at the limit, Redis runs eviction logic before accepting the write. The policy determines which key is dropped.&lt;/p&gt;
&lt;p&gt;Redis 7.x documents eight policies:&lt;/p&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Policy&lt;/th&gt;&lt;th&gt;Key pool&lt;/th&gt;&lt;th&gt;Algorithm&lt;/th&gt;&lt;th&gt;Use case&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;noeviction&lt;/code&gt;&lt;/td&gt;&lt;td&gt;—&lt;/td&gt;&lt;td&gt;Rejects writes&lt;/td&gt;&lt;td&gt;Persistent stores where data loss is unacceptable&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;allkeys-lru&lt;/code&gt;&lt;/td&gt;&lt;td&gt;All keys&lt;/td&gt;&lt;td&gt;Least recently used&lt;/td&gt;&lt;td&gt;General-purpose cache&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;volatile-lru&lt;/code&gt;&lt;/td&gt;&lt;td&gt;TTL keys only&lt;/td&gt;&lt;td&gt;LRU from TTL set&lt;/td&gt;&lt;td&gt;Mixed store where permanent keys must survive&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;allkeys-lfu&lt;/code&gt;&lt;/td&gt;&lt;td&gt;All keys&lt;/td&gt;&lt;td&gt;Least frequently used&lt;/td&gt;&lt;td&gt;Skewed access patterns with a hot key set&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;volatile-lfu&lt;/code&gt;&lt;/td&gt;&lt;td&gt;TTL keys only&lt;/td&gt;&lt;td&gt;LFU from TTL set&lt;/td&gt;&lt;td&gt;Mixed store with skewed access&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;allkeys-random&lt;/code&gt;&lt;/td&gt;&lt;td&gt;All keys&lt;/td&gt;&lt;td&gt;Random&lt;/td&gt;&lt;td&gt;Almost never correct in production&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;volatile-random&lt;/code&gt;&lt;/td&gt;&lt;td&gt;TTL keys only&lt;/td&gt;&lt;td&gt;Random from TTL set&lt;/td&gt;&lt;td&gt;Rarely useful&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;volatile-ttl&lt;/code&gt;&lt;/td&gt;&lt;td&gt;TTL keys only&lt;/td&gt;&lt;td&gt;Shortest TTL first&lt;/td&gt;&lt;td&gt;When expiry order should drive eviction&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;For a standard cache where all keys have TTLs and access is roughly uniform, &lt;code&gt;allkeys-lru&lt;/code&gt; is the documented starting recommendation in the Redis memory management documentation. It requires no TTL discipline and evicts based on recency.&lt;/p&gt;
&lt;p&gt;For workloads with a stable hot key set — recommendations, trending content, rate-limit counters — &lt;code&gt;allkeys-lfu&lt;/code&gt; is a better fit. LFU tracks frequency rather than recency, so a hot key accessed hundreds of times will not be dropped for being idle. LFU support arrived in Redis 4.0.&lt;/p&gt;
&lt;p&gt;One detail matters for both: Redis does not maintain a true LRU or LFU data structure. It samples &lt;code&gt;maxmemory-samples&lt;/code&gt; keys (default: 5) and evicts the best candidate from that sample. This is an approximation; larger sample sizes improve accuracy at the cost of CPU.&lt;/p&gt;
&lt;p&gt;Set the policy in &lt;code&gt;redis.conf&lt;/code&gt; or apply it at runtime without a restart:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;ini&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# redis.conf — set once, survives restart&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;maxmemory 2gb&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;maxmemory-policy allkeys-lru&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;maxmemory-samples 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Apply at runtime without restart&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;redis-cli&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; CONFIG&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; maxmemory-policy&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; allkeys-lru&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;redis-cli&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; CONFIG&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; SET&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; maxmemory-samples&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;volatile-*&lt;/code&gt; policies only touch keys with a TTL set. If the application writes any keys without TTLs, those keys are never eligible for eviction. As non-TTL keys accumulate, the eviction pool shrinks, and under write pressure Redis exhausts eligible keys and falls back to &lt;code&gt;noeviction&lt;/code&gt; behavior without any configuration change.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The Redis eviction policies reference at redis.io explicitly documents the &lt;code&gt;noeviction&lt;/code&gt; fallback when &lt;code&gt;volatile-*&lt;/code&gt; policies find no eligible keys. This is designed behavior. The practical consequence: &lt;code&gt;volatile-lru&lt;/code&gt; is safe only when TTL discipline is enforced at the application layer, not assumed.&lt;/p&gt;
&lt;p&gt;For diagnosis, &lt;code&gt;INFO memory&lt;/code&gt; returns &lt;code&gt;mem_fragmentation_ratio&lt;/code&gt;. The Redis documentation flags ratios above 1.5 as significant — the process RSS exceeds what Redis counts as &lt;code&gt;used_memory&lt;/code&gt;. Eviction uses &lt;code&gt;used_memory&lt;/code&gt;, not RSS, so high fragmentation means the host can approach OOM before Redis triggers any eviction.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;volatile-lru&lt;/code&gt; with no TTL keys&lt;/td&gt;&lt;td&gt;Writes fail under load; Redis behaves as &lt;code&gt;noeviction&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Eviction pool is empty; documented Redis fallback behavior&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;LRU or LFU with &lt;code&gt;maxmemory-samples 5&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Hot keys can be evicted by chance&lt;/td&gt;&lt;td&gt;Redis samples 5 keys, not the full keyspace; approximation only&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High &lt;code&gt;mem_fragmentation_ratio&lt;/code&gt; with tight &lt;code&gt;maxmemory&lt;/code&gt;&lt;/td&gt;&lt;td&gt;RSS exceeds RAM before eviction triggers&lt;/td&gt;&lt;td&gt;Eviction uses &lt;code&gt;used_memory&lt;/code&gt;, not RSS; fragmentation is invisible to eviction logic&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Unset or mismatched eviction policy causes write failures, hit-rate degradation, or OOM kills under load.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Set &lt;code&gt;maxmemory&lt;/code&gt; explicitly; use &lt;code&gt;allkeys-lru&lt;/code&gt; for general caches, &lt;code&gt;allkeys-lfu&lt;/code&gt; for skewed workloads; avoid &lt;code&gt;volatile-*&lt;/code&gt; unless TTL discipline is enforced at the application layer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: After a load test, &lt;code&gt;redis-cli INFO stats | grep evicted_keys&lt;/code&gt; should be non-zero and &lt;code&gt;used_memory&lt;/code&gt; should stay below &lt;code&gt;maxmemory&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Run &lt;code&gt;redis-cli CONFIG GET maxmemory &amp;#x26;&amp;#x26; redis-cli CONFIG GET maxmemory-policy&lt;/code&gt; across production instances; any instance returning &lt;code&gt;0&lt;/code&gt; for &lt;code&gt;maxmemory&lt;/code&gt; is unprotected.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Eviction policy is one of the few Redis settings where the wrong default does not produce an immediate visible failure — it surfaces only when the cache fills up, which is exactly when you need it most.&lt;/p&gt;</content:encoded><category>databases</category><category>failures</category></item><item><title>AWS Database Cost Triage: RDS, Aurora, DynamoDB, ElastiCache, and OpenSearch</title><link>https://rajivonai.com/blog/2022-10-08-aws-database-cost-triage-rds-aurora-dynamodb-elasticache-and-opensearch/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-10-08-aws-database-cost-triage-rds-aurora-dynamodb-elasticache-and-opensearch/</guid><description>Database bills grow when ownership, workload shape, and control loops drift apart — a structured triage approach for RDS, Aurora, DynamoDB, ElastiCache, and OpenSearch spend before it becomes an emergency.</description><pubDate>Sat, 08 Oct 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Database bills rarely explode because one engineer chose the wrong service. They usually grow because ownership, workload shape, and control loops drift apart until nobody can explain which queries, tenants, indexes, caches, or shards are buying what outcome.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;AWS gives teams a broad database portfolio: RDS for conventional relational workloads, Aurora for managed high-availability relational systems, DynamoDB for key-value and document access patterns, ElastiCache for Redis or Memcached acceleration, and OpenSearch for search and analytical indexing.&lt;/p&gt;
&lt;p&gt;That portfolio is useful because workloads are not uniform. A checkout path, a feature flag read, a session cache, a text search endpoint, and an operational dashboard should not all be forced through the same persistence layer.&lt;/p&gt;
&lt;p&gt;The cost problem begins when each service is treated as an isolated bill line. RDS cost is reviewed by instance class. Aurora cost is reviewed by cluster. DynamoDB cost is reviewed by table. OpenSearch cost is reviewed by domain. ElastiCache cost is reviewed by node group.&lt;/p&gt;
&lt;p&gt;Those views are necessary, but insufficient. They show what was purchased. They rarely show whether the purchase still matches the access pattern.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure mode is not “databases are expensive.” The failure mode is unmanaged mismatch.&lt;/p&gt;
&lt;p&gt;A relational workload moves to Aurora but keeps inefficient polling queries. DynamoDB gets adopted for scale but receives ad hoc access patterns that force scans or secondary indexes nobody budgeted. ElastiCache is added to reduce database load, but eviction policy and key design cause poor hit rates. OpenSearch becomes the destination for every debug query and slowly turns into a second data warehouse.&lt;/p&gt;
&lt;p&gt;The team then enters cost triage under pressure. Finance wants a reduction. Engineering wants reliability. Product wants no visible regression. The easy move is to resize or delete capacity. The safer move is to identify the cost control plane: the few measurements and architectural decisions that connect dollars to workload behavior.&lt;/p&gt;
&lt;p&gt;The core question is: how do you reduce database cost without turning cost cutting into an availability incident?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;Treat database cost as an operational signal attached to workload intent. The unit of analysis is not the AWS service. It is the access pattern.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[monthly bill spike — unknown workload] --&gt; B[classify access pattern — transactional or cache or search]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C[RDS and Aurora — relational query pressure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; D[DynamoDB — key access and capacity mode]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; E[ElastiCache — hit rate and memory pressure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; F[OpenSearch — index and shard pressure]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; G[query plan review — indexes and connection shape]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; H[capacity review — instance and storage and replicas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; I[partition review — hot keys and scans]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; J[capacity review — on demand or provisioned]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; K[key review — ttl and eviction]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; L[node review — memory and network]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; M[index review — mappings and retention]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; N[cluster review — shards and replicas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt; O[cost decision — remove waste with rollback]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    L --&gt; O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    M --&gt; O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    N --&gt; O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For RDS and Aurora, start with query behavior before instance behavior. Expensive instances are often compensating for missing indexes, unbounded result sets, inefficient joins, chatty connection pools, or read replicas used as a substitute for query ownership. Right-sizing helps only after the workload is legible.&lt;/p&gt;
&lt;p&gt;For DynamoDB, cost follows request shape. A table with clean partition keys and predictable access can be cheap at high scale. A table with scans, hot keys, oversized items, or poorly chosen global secondary indexes can become expensive while still looking “serverless” from the application side. Triage must inspect consumed capacity, throttling, partition heat, item size, and index usage together.&lt;/p&gt;
&lt;p&gt;For ElastiCache, the key question is whether the cache is reducing origin work. A cache with low hit rate, excessive churn, large values, or no meaningful TTL discipline can add cost without reducing database pressure. The control plane is hit rate, eviction, memory fragmentation, network throughput, and the shape of misses.&lt;/p&gt;
&lt;p&gt;For OpenSearch, cost is dominated by index design, shard count, retention, replica policy, and query fanout. A domain can be oversized because ingestion is too broad, mappings are too loose, shards are too small, or retention is treated as infinite. Search clusters need lifecycle management, not just bigger nodes.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Amazon’s DynamoDB documentation describes capacity modes, partition keys, secondary indexes, item size, and scan behavior as central to table performance and cost. This is a documented system behavior, not an anecdote.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; During cost triage, separate DynamoDB tables by access pattern: predictable high-volume tables, bursty tables, tables with global secondary indexes, and tables showing scan-heavy behavior in CloudWatch or Contributor Insights. Check whether on-demand mode is buying useful elasticity or masking a workload that should be provisioned with autoscaling.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented pattern is that DynamoDB cost optimization comes from aligning capacity mode and key design with access shape. Cutting capacity without fixing scans, hot keys, or oversized indexes only moves the failure from the bill to throttling.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; DynamoDB triage should begin with key and index behavior, then capacity mode. The billing model is downstream of the data model.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS RDS and Aurora expose database load through tools such as Performance Insights, Enhanced Monitoring, slow query logs, and engine-native explain plans. PostgreSQL and MySQL behavior around indexes, joins, locks, and connection pressure is documented and observable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Group RDS and Aurora spend by cluster role: write primary, read replica, reporting replica, and idle legacy instance. For high-cost clusters, inspect top SQL, wait events, storage growth, replica lag, and connection count before resizing. Validate reserved capacity or savings plans only after the steady-state footprint is understood.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented pattern is that relational cost optimization depends on workload diagnosis. A larger instance may be hiding missing indexes, lock contention, or application pooling problems. A smaller instance may be safe only after query pressure is reduced.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; For relational systems, instance size is the last mile of triage. Query shape, storage growth, and availability requirements decide the real envelope.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Redis and Memcached are documented as memory-backed caching systems. ElastiCache pricing follows nodes and capacity, while operational value depends on reducing backend work through cache hits and predictable eviction.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Review cache hit rate, evictions, memory utilization, key cardinality, TTL distribution, and value size. Identify caches used for durable state, caches with no expiry discipline, and caches that duplicate data already served cheaply by DynamoDB or Aurora replicas.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented pattern is that cache cost is justified only when it reduces more expensive work or protects latency. A cache with poor hit rate is not an optimization layer; it is another production datastore.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; ElastiCache triage should ask what origin load disappears because the cache exists.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; OpenSearch documentation emphasizes shard sizing, index lifecycle management, mappings, replicas, and query design. These are known drivers of cluster stability and cost.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Split indexes by purpose: product search, logs, metrics, audit, and exploratory debugging. Apply retention rules, reduce unnecessary replicas, fix oversharding, and move non-search analytics to more appropriate storage when search is being used as a warehouse.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented pattern is that OpenSearch cost is often index lifecycle cost. Compute, storage, and memory pressure follow from how much data is indexed, how it is mapped, and how widely queries fan out.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; OpenSearch is expensive when it becomes the universal answer to “we might need to query this later.”&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Service&lt;/th&gt;&lt;th&gt;Common Cost Failure&lt;/th&gt;&lt;th&gt;Safer Triage Move&lt;/th&gt;&lt;th&gt;Risk&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;RDS&lt;/td&gt;&lt;td&gt;Oversized instances hiding inefficient SQL&lt;/td&gt;&lt;td&gt;Review top queries, waits, indexes, and storage before resizing&lt;/td&gt;&lt;td&gt;Latency regression from premature downsizing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aurora&lt;/td&gt;&lt;td&gt;Read replicas used to absorb avoidable query load&lt;/td&gt;&lt;td&gt;Separate read scaling from query cleanup&lt;/td&gt;&lt;td&gt;Replica lag or failover surprises&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DynamoDB&lt;/td&gt;&lt;td&gt;Scans, hot keys, oversized items, unused indexes&lt;/td&gt;&lt;td&gt;Inspect consumed capacity and access patterns per table&lt;/td&gt;&lt;td&gt;Throttling if capacity is cut first&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ElastiCache&lt;/td&gt;&lt;td&gt;Low hit rate or unbounded key growth&lt;/td&gt;&lt;td&gt;Measure hit rate, eviction, TTLs, and origin reduction&lt;/td&gt;&lt;td&gt;Cache removal can overload the origin&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OpenSearch&lt;/td&gt;&lt;td&gt;Oversharding and infinite retention&lt;/td&gt;&lt;td&gt;Fix index lifecycle, mappings, replicas, and shard count&lt;/td&gt;&lt;td&gt;Search latency or recovery impact&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The database bill is not actionable when it is grouped only by AWS service.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Build a cost control plane around access patterns: relational queries, key-value reads, cache behavior, and search indexes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Use documented service signals: Performance Insights, CloudWatch capacity metrics, cache hit rate, eviction behavior, shard health, index retention, and query fanout.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; For each expensive datastore, write down the workload it serves, the metric proving it earns its cost, the rollback plan for any reduction, and the owner who can change the access pattern.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>MongoDB Query Performance Workflow: Deconstructing explain() Plans, Plan Cache Tournaments, and WiredTiger Cache Contention</title><link>https://rajivonai.com/blog/2022-09-26-mongodb-query-performance-workflow/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-09-26-mongodb-query-performance-workflow/</guid><description>A comprehensive diagnostic workflow for production MongoDB query degradation. This guide analyzes query planner mechanics, plan cache tournament eviction, WiredTiger ticket starvation, in-memory sort aborts, and index selectivity forensics to identify and resolve query stalls within minutes.</description><pubDate>Mon, 26 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A MongoDB query showing &lt;code&gt;COLLSCAN&lt;/code&gt; in its execution plan is not merely a missing index symptom — it is a physical I/O failure that threatens the entire database node. When a query scans 10 million documents to return three records, it reads gigabytes of compressed data from NVMe storage, decompresses every B-Tree leaf page into WiredTiger’s uncompressed cache pool, evicts active working-set pages belonging to mission-critical OLTP collections, and consumes concurrent execution tickets. Within seconds, a single unindexed reporting query or misordered compound filter cascades into cluster-wide connection exhaustion, elevated replication lag, and application request timeouts.&lt;/p&gt;
&lt;p&gt;Remediating query degradation in high-throughput MongoDB deployments requires moving beyond basic slow-log scanning. Systems engineers must understand how the query optimizer runs plan tournaments, how the plan cache evicts suboptimal shapes, how the storage engine allocates read tickets, and how physical B-Tree traversals translate into kernel direct I/O operations.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;MongoDB’s query execution engine processes millions of incoming operations per second across replica sets and sharded clusters. Client queries arrive via &lt;code&gt;mongos&lt;/code&gt; routers or directly at primary replica set nodes, where the classic query engine must convert high-level BSON filter expressions into physical storage execution trees.&lt;/p&gt;









































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Execution Stage&lt;/th&gt;&lt;th&gt;Physical Storage Action&lt;/th&gt;&lt;th&gt;Memory &amp;#x26; Cache Impact&lt;/th&gt;&lt;th&gt;Scalability Bound&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;COLLSCAN&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Iterates every sequential RecordId in the collection B-Tree&lt;/td&gt;&lt;td&gt;Decompresses all disk blocks into WiredTiger cache; triggers severe page eviction&lt;/td&gt;&lt;td&gt;$O(N)$ — linear degradation with total collection volume&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;IXSCAN&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Traverses sorted B-Tree index pages looking for matching key bounds&lt;/td&gt;&lt;td&gt;Loads narrow index leaf pages; high cache density&lt;/td&gt;&lt;td&gt;$O(\log N + K)$ — logarithmic seek, linear in matched keys ($K$)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;FETCH&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Dereferences RecordIds from &lt;code&gt;IXSCAN&lt;/code&gt; to retrieve full document payloads&lt;/td&gt;&lt;td&gt;Random read access to collection pages if not already cache-resident&lt;/td&gt;&lt;td&gt;Proportional to matched index keys; disk I/O bound if cache misses occur&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;SORT&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Buffers fetched records into a private in-memory array to perform sorting&lt;/td&gt;&lt;td&gt;Consumes up to 100 MB of ephemeral RAM per query; aborts if exceeded&lt;/td&gt;&lt;td&gt;Fails with runtime fatal exception if payload exceeds 104,857,600 bytes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;PROJECTION_COVERED&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Serves all requested fields directly from the index leaf page without document dereference&lt;/td&gt;&lt;td&gt;Zero collection page reads; minimal memory footprint&lt;/td&gt;&lt;td&gt;Optimal execution path: purely index-resident, zero disk fetch I/O&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;When collections scale from 50,000 documents in staging to 50,000,000 documents in production, queries that previously took 4ms suddenly take 12,000ms. The query planner’s choices, combined with memory cache constraints, dictate whether the cluster operates with sub-millisecond p99 latencies or collapses under foreground eviction pressure.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure modes of slow MongoDB queries fall into four mechanical categories:&lt;/p&gt;
&lt;p&gt;First, &lt;strong&gt;the plan cache tournament trap&lt;/strong&gt;. When a query shape is first encountered, the query planner evaluates all candidate indexes by running a “plan tournament”: it executes competing plans in parallel for a fixed number of “works” (typically 300 work units) or until one plan produces sufficient candidate matches. The plan that completes first is declared the winner and written to the collection’s in-memory Plan Cache. However, if data distribution is skewed, a plan that wins for a rare filter value (e.g., &lt;code&gt;status: &quot;failed&quot;&lt;/code&gt;) may be cached and subsequently reused for a common filter value (e.g., &lt;code&gt;status: &quot;completed&quot;&lt;/code&gt;), causing catastrophic performance degradation for thousands of subsequent executions until replanning is triggered.&lt;/p&gt;
&lt;p&gt;Second, &lt;strong&gt;in-memory sort boundary breaches&lt;/strong&gt;. When a query requires sorted output and no compound index satisfies the Sort condition in ESR (Equality, Sort, Range) order, MongoDB inserts an internal &lt;code&gt;SORT&lt;/code&gt; stage between &lt;code&gt;FETCH&lt;/code&gt; and client output. MongoDB strictly enforces a 100 MB memory limit for in-memory sort operations. If the intermediate document stream exceeds 104,857,600 bytes, the query instantly fails with a fatal error: &lt;code&gt;Executor error during find command: Sort exceeded memory limit&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Third, &lt;strong&gt;low selectivity and index scan bloat&lt;/strong&gt;. An index that matches too many keys forces the storage engine to perform excessive work. When &lt;code&gt;totalKeysExamined&lt;/code&gt; is 100,000 but &lt;code&gt;nReturned&lt;/code&gt; is 10, the database has spent 99.99% of its CPU time scanning unselective index pages and discarding records. This manifests as high CPU utilization with flat business throughput.&lt;/p&gt;
&lt;p&gt;Fourth, &lt;strong&gt;WiredTiger read ticket starvation&lt;/strong&gt;. MongoDB limits concurrent storage engine operations (default 128 read tickets and 128 write tickets). Slow, unindexed queries hold read tickets for seconds while waiting on disk I/O. As incoming requests arrive, the available ticket pool drops to zero:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;serverStatus&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().wiredTiger.concurrentTransactions.read.available &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Value drops to 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once read tickets are exhausted, all subsequent read operations — including lightweight 1ms primary-key lookups — are blocked in application connection queues.&lt;/p&gt;
&lt;p&gt;How do you establish a deterministic, step-by-step diagnostic workflow that identifies the exact root cause of query latency spikes within 60 seconds, without destabilizing a production cluster?&lt;/p&gt;
&lt;h2 id=&quot;the-forensic-query-performance-workflow&quot;&gt;The Forensic Query Performance Workflow&lt;/h2&gt;
&lt;p&gt;The diagnostic architecture establishes an authoritative triage pipeline that moves from live operational state down to storage engine internals and index redesign.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Alert: Latency Spike or Read Ticket Depletion] --&gt; B[Inspect Active Operations via currentOp]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt; C{Long-Running Ops &gt; 2s?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|Yes| D[Identify Query Shape &amp;#x26; Client Namespace]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt;|No| E[Inspect Slow Query Profiler &amp;#x26; Logs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt; F[Execute explain with executionStats]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt; F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    F --&gt; G{Winning Plan Stage?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|COLLSCAN| H[Missing Index — Apply ESR Rule Index Creation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|IXSCAN with SORT| I[Sort Order Mismatch — Rebuild Index to Cover Sort Key]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|IXSCAN with high totalDocsExamined| J[Poor Selectivity — Refactor Compound Prefix Order]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|IXSCAN with high executionTimeMillis| K[WiredTiger Cache Eviction — Check Direct IOPS &amp;#x26; Cache Fill]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; L[Verify Plan Transition in Staging via explain]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt; L&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt; L&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt; M[Tune wiredTigerCacheSizeGB or Scale Storage IOPS]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;stage-1-triage-active-blast-radius-with-currentop&quot;&gt;Stage 1: Triage Active Blast Radius with currentOp&lt;/h3&gt;
&lt;p&gt;When query latency spikes, the first operational imperative is inspecting the live engine to determine which namespaces are consuming read tickets:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Filter currently executing active queries exceeding 1 second&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;currentOp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  active: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  op: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;query&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  secs_running: { $gte: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}).inprog.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;forEach&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`[OPID: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;opid&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}] Namespace: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ns&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} | Duration: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;secs_running&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}s | `&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        `WaitingForLock: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;waitingForLock&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;} | ReadTicket: ${&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ticketAvailability&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &quot;N/A&quot;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;`Query Shape: ${&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;JSON&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;stringify&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;op&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;command&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;---&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If multiple operations are backed up behind a single long-running query, record the operation ID (&lt;code&gt;opid&lt;/code&gt;) and terminate the rogue query if it threatens cluster availability:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Terminate runaway operation if causing cascading connection saturation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;killOp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;12345678&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;stage-2-deep-explainexecutionstats-telemetry&quot;&gt;Stage 2: Deep explain(“executionStats”) Telemetry&lt;/h3&gt;
&lt;p&gt;Never rely on bare &lt;code&gt;.explain()&lt;/code&gt; or &lt;code&gt;.explain(&quot;queryPlanner&quot;)&lt;/code&gt;. Bare explain returns the planner’s &lt;em&gt;estimate&lt;/em&gt; without executing the query. Always execute with &lt;code&gt;&quot;executionStats&quot;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Run against the exact query filter, projection, and sort&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.orders.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tenant_id: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;acme_corp&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  status: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;processing&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  created_at: { $gte: &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ISODate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;2024-01-01T00:00:00Z&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ created_at: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; })&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;projection&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ _id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, order_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, total_amount: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, created_at: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; })&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;explain&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;executionStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Analyze the four critical forensic metrics in the JSON payload:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;executionStats.totalDocsExamined&lt;/code&gt; vs &lt;code&gt;executionStats.nReturned&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;selectivity ratio&lt;/strong&gt;: $\text{Selectivity} = \frac{\text{nReturned}}{\text{totalDocsExamined}}$.&lt;/li&gt;
&lt;li&gt;Ideal: $1.0$ (every examined document is returned).&lt;/li&gt;
&lt;li&gt;Problematic: $&amp;#x3C; 0.1$ (scanning 10 documents for every 1 returned).&lt;/li&gt;
&lt;li&gt;Critical failure: $0.0001$ (scanning 10,000 documents to return 1).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;executionStats.totalKeysExamined&lt;/code&gt; vs &lt;code&gt;executionStats.nReturned&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Shows how many index entries were traversed. If &lt;code&gt;totalKeysExamined&lt;/code&gt; is massive while &lt;code&gt;totalDocsExamined&lt;/code&gt; is low, the index is filtering keys, but the B-Tree range scan is excessively wide.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;queryPlanner.winningPlan.inputStage.stage&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Must be &lt;code&gt;IXSCAN&lt;/code&gt;. If &lt;code&gt;COLLSCAN&lt;/code&gt;, index coverage is absent or the planner rejected existing indexes.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Presence of &lt;code&gt;SORT&lt;/code&gt; stage&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If a &lt;code&gt;SORT&lt;/code&gt; stage exists above &lt;code&gt;IXSCAN&lt;/code&gt;, the index did not satisfy the sort order. The query is sorting in memory.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;stage-3-resolving-index-geometry-via-the-esr-rule&quot;&gt;Stage 3: Resolving Index Geometry via the ESR Rule&lt;/h3&gt;
&lt;p&gt;To eliminate both &lt;code&gt;COLLSCAN&lt;/code&gt; and in-memory &lt;code&gt;SORT&lt;/code&gt;, compound indexes must be built following the mathematical &lt;strong&gt;ESR Rule&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Equality (&lt;code&gt;E&lt;/code&gt;)&lt;/strong&gt;: Place fields with exact match predicates (&lt;code&gt;$eq&lt;/code&gt;, string/ID literals) first. This narrows B-Tree traversal to an exact sub-tree.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sort (&lt;code&gt;S&lt;/code&gt;)&lt;/strong&gt;: Place the field(s) defining the requested sort order immediately after the equality fields. Because the preceding equality fields fix the path, index leaf nodes within that sub-tree are already physically sorted by this key.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Range (&lt;code&gt;R&lt;/code&gt;)&lt;/strong&gt;: Place fields with inequality or range filters (&lt;code&gt;$gt&lt;/code&gt;, &lt;code&gt;$lt&lt;/code&gt;, &lt;code&gt;$in&lt;/code&gt;, &lt;code&gt;$regex&lt;/code&gt;) last. Range predicates expand the scan boundary; placing a sort key &lt;em&gt;after&lt;/em&gt; a range field breaks the physical ordering guarantees of the B-Tree.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// INCORRECT (Breaks ESR: Range before Sort):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Index: { created_at: -1, tenant_id: 1, status: 1 }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Result: Planner must scan created_at range and sort remaining documents in memory!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// CORRECT (Complies with ESR):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Equality: tenant_id, status&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Sort: created_at&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Range: (none in this example, or trailing if present)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.orders.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;createIndex&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { tenant_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, status: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, created_at: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { name: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;idx_orders_tenant_status_created&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, background: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;stage-4-creating-a-zero-fetch-covered-query&quot;&gt;Stage 4: Creating a Zero-Fetch Covered Query&lt;/h3&gt;
&lt;p&gt;When query throughput reaches tens of thousands of ops/second, dereferencing collection RecordIds (&lt;code&gt;FETCH&lt;/code&gt; stage) causes severe cache churn. By appending the projected fields to the compound index, the query engine serves the request entirely from index memory:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Index covering Equality + Sort + Projection:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.orders.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;createIndex&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { tenant_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, status: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, created_at: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, order_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, total_amount: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { name: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;idx_orders_covered_query&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, background: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Covered Query Execution (Explicitly exclude _id unless indexed):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.orders.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { tenant_id: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;acme_corp&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, status: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;processing&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { _id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, tenant_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, status: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, created_at: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, order_id: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, total_amount: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ created_at: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; })&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;explain&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;executionStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the resulting explain output, verify:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;executionStats.totalDocsExamined: 0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;winningPlan.stage: &quot;PROJECTION_COVERED&quot;&lt;/code&gt; or &lt;code&gt;winningPlan.stage: &quot;IXSCAN&quot;&lt;/code&gt; with no parent &lt;code&gt;FETCH&lt;/code&gt; stage.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Executing performance forensics on production clusters requires precise command instrumentation.&lt;/p&gt;
&lt;h3 id=&quot;profiler-query-inspection&quot;&gt;Profiler Query Inspection&lt;/h3&gt;
&lt;p&gt;To capture slow operations without overwhelming the primary node, configure profiling with a strict slow-operation threshold:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Set profiling level 1 (slow operations only) with 100ms threshold&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;setProfilingLevel&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, { slowms: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Identify the top 5 most expensive query shapes over the last hour&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.system.profile.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aggregate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;([&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { $match: { op: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;query&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, millis: { $gte: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } } },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    $group: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      _id: { ns: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$ns&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, queryHash: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$queryHash&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, planCacheKey: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$planCacheKey&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      count: { $sum: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      avgMillis: { $avg: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$millis&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      maxMillis: { $max: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$millis&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      totalDocsExamined: { $sum: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$docsExamined&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      totalKeysExamined: { $sum: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$keysExamined&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      nReturned: { $sum: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$nreturned&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { $sort: { avgMillis: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; } },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  { $limit: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;plan-cache-flushing-and-hinting&quot;&gt;Plan Cache Flushing and Hinting&lt;/h3&gt;
&lt;p&gt;When the query planner locks onto a poisoned or suboptimal plan due to data skew, flush the cached plan specifically for that query shape without restarting the server:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Inspect cached plans for the collection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; planCache&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; db.orders.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getPlanCache&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;planCache.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Clear the plan cache specifically for orders collection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.orders.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getPlanCache&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;clear&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;Plan cache cleared. New queries will re-trigger plan tournament.&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If query replanning consistently selects an inferior index under skewed distributions, use index hinting in application code as an emergency operational brake:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Force query planner to use the verified ESR compound index&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.orders.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tenant_id: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;acme_corp&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  status: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;processing&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;hint&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;idx_orders_tenant_status_created&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({ created_at: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;correlating-wiredtiger-storage-contention-via-linux-iostat&quot;&gt;Correlating WiredTiger Storage Contention via Linux iostat&lt;/h3&gt;
&lt;p&gt;When explain shows healthy &lt;code&gt;IXSCAN&lt;/code&gt; operations but &lt;code&gt;executionTimeMillis&lt;/code&gt; remains high, the bottleneck is storage layer queueing. Run &lt;code&gt;iostat&lt;/code&gt; on the host to measure block-level latency:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# Monitor disk read latency and queue saturation on the MongoDB data volume&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;iostat&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -xz&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; awk&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;BEGIN { print &quot;Device | r/s | rMB/s | r_await (ms) | aqu-sz | %util&quot; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;$1 ~ /nvme|sd/ {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  printf &quot;%-8s | %-6s | %-7s | %-12s | %-6s | %-5s\n&quot;, $1, $4, $6, $10, $9, $14&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If &lt;code&gt;r_await&lt;/code&gt; exceeds 5.0ms on NVMe or &lt;code&gt;%util&lt;/code&gt; approaches 100%, WiredTiger is continuously thrashing cache pages to disk. Adding indexes will not resolve storage saturation; the working set must be brought back within the boundaries of &lt;code&gt;wiredTigerCacheSizeGB&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Root Cause&lt;/th&gt;&lt;th&gt;Production Blast Radius&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;In-Memory Sort Abort&lt;/td&gt;&lt;td&gt;Query requires sorted output, but compound index does not cover sort key; intermediate result exceeds 100 MB&lt;/td&gt;&lt;td&gt;Fatal application error: &lt;code&gt;Sort exceeded memory limit&lt;/code&gt;; API endpoints fail with HTTP 500&lt;/td&gt;&lt;td&gt;Rebuild compound index following ESR order (Sort key immediately following Equality keys). For administrative analytics, pass &lt;code&gt;{ allowDiskUse: true }&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Low Index Selectivity Scan Bloat&lt;/td&gt;&lt;td&gt;Leading field in compound index has extremely low cardinality (e.g., boolean flag or 2-value enum)&lt;/td&gt;&lt;td&gt;High CPU consumption; millions of B-Tree keys traversed; query takes seconds despite using &lt;code&gt;IXSCAN&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Place high-cardinality equality fields (e.g., &lt;code&gt;tenant_id&lt;/code&gt;, &lt;code&gt;account_id&lt;/code&gt;) before low-cardinality status flags in compound index prefix&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;WiredTiger Read Ticket Exhaustion&lt;/td&gt;&lt;td&gt;Multiple slow queries hold read execution tickets concurrently while waiting for storage I/O&lt;/td&gt;&lt;td&gt;Cluster-wide connection saturation; lightweight primary-key reads queue; complete database freeze&lt;/td&gt;&lt;td&gt;Kill offending queries using &lt;code&gt;db.killOp()&lt;/code&gt;; configure query timeout guards via &lt;code&gt;maxTimeMS()&lt;/code&gt;; scale up instance RAM or storage IOPS&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Plan Cache Poisoning via Skewed Data&lt;/td&gt;&lt;td&gt;Plan tournament runs against rare filter argument, caching suboptimal plan for high-frequency queries&lt;/td&gt;&lt;td&gt;Sudden, unexplained latency degradation on high-volume endpoints; persists until server restart&lt;/td&gt;&lt;td&gt;Clear collection plan cache via &lt;code&gt;db.collection.getPlanCache().clear()&lt;/code&gt;; apply explicit query &lt;code&gt;.hint()&lt;/code&gt; on critical application paths&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cache Thrashing via Analytical COLLSCAN&lt;/td&gt;&lt;td&gt;Unindexed aggregation or ad-hoc admin query scans massive collection; evicts OLTP working set pages&lt;/td&gt;&lt;td&gt;Application p99 read latency spikes across unrelated collections as cache pages must be re-read from disk&lt;/td&gt;&lt;td&gt;Direct ad-hoc queries and reporting jobs to secondary replica set nodes using &lt;code&gt;readPreference: &quot;secondary&quot;&lt;/code&gt;; set strict &lt;code&gt;maxTimeMS&lt;/code&gt; guards&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Regex Prefix Scan Degradation&lt;/td&gt;&lt;td&gt;Case-insensitive regex (&lt;code&gt;$options: &quot;i&quot;&lt;/code&gt;) or leading wildcard regex (&lt;code&gt;/.*pattern/&lt;/code&gt;) used in filter predicate&lt;/td&gt;&lt;td&gt;B-Tree index cannot be traversed logarithmically; forces full index scan or complete collection scan&lt;/td&gt;&lt;td&gt;Enforce leading-anchor regex searches (&lt;code&gt;/^pattern/&lt;/code&gt;); convert case-insensitive fields to pre-computed lowercase fields; use Atlas Search&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Multikey Index Cardinality Expansion&lt;/td&gt;&lt;td&gt;Compound index covers two array fields simultaneously; or array field contains thousands of elements&lt;/td&gt;&lt;td&gt;Excessive B-Tree leaf entries created per document; massive write amplification and bloated cache usage&lt;/td&gt;&lt;td&gt;Never index parallel arrays (MongoDB blocks this, but nested sub-document arrays can cause explosive cardinality expansion); cap array lengths&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Partial Index Filter Mismatch&lt;/td&gt;&lt;td&gt;Query predicate omits fields or constraints defined in the index’s &lt;code&gt;partialFilterExpression&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Query planner cannot safely utilize the partial index; silently degrades to full &lt;code&gt;COLLSCAN&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Audit query filters against partial index expressions; verify in &lt;code&gt;explain()&lt;/code&gt; that &lt;code&gt;IXSCAN&lt;/code&gt; is selected for all target call sites&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;2024-classic-vs-sbe-query-engine-update&quot;&gt;2024 Classic vs SBE Query Engine Update&lt;/h2&gt;
&lt;p&gt;MongoDB’s query execution architecture evolved significantly with the introduction and maturation of the Slot-Based Execution (SBE) engine. Originally introduced in 5.0 for specific query shapes and substantially broadened in 6.0 and 7.0, SBE replaces the legacy AST-walking tree execution model with a compiled, vectorized engine designed to leverage modern CPU branch prediction and L1/L2 data caching.&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; output, the presence of SBE is identified by the &lt;code&gt;slotBasedPlan&lt;/code&gt; block. When active, classic stages (&lt;code&gt;IXSCAN&lt;/code&gt;, &lt;code&gt;FETCH&lt;/code&gt;) are represented within optimized slot-based stages:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Look for SBE execution markers in modern MongoDB explain output:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;queryPlanner&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;winningPlan&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      &quot;stage&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SUBSUMES&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      &quot;slotBasedPlan&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;stages&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[1] scan sbe_index ... [2] filter ... [3] project&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The operational significance of SBE for performance troubleshooting is twofold:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Lower CPU Overhead per Document&lt;/strong&gt;: SBE processes batches of records in contiguous memory buffers, reducing CPU consumption per examined document by 30–50% compared to the classic engine.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Strict Plan Shape Invariants&lt;/strong&gt;: SBE evaluates plan selectivity more aggressively. However, if a query shape falls back to the classic engine (due to unsupported aggregation operators or specific geospatial queries), performance characteristics can diverge sharply between seemingly identical query shapes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When diagnosing query degradation in modern clusters, engineers must check whether a query executed under the SBE pipeline or fell back to the classic execution engine.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Unindexed queries, misordered compound indexes, and in-memory sort stages cause massive disk I/O, evict active working-set pages from the WiredTiger cache, and exhaust concurrent read tickets, cascading into cluster-wide outages.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Execute a systematic diagnostic sequence: isolate runaway operations with &lt;code&gt;currentOp&lt;/code&gt;, generate empirical execution telemetry with &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt;, enforce ESR ordering on all compound indexes, and create covered queries for high-throughput read paths.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The WiredTiger storage engine operates on discrete physical B-Tree structures. Following ESR order guarantees that keys are physically contiguous in cache memory, eliminating random seeks, skipping collection fetches, and preventing intermediate sorting memory allocations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt;
&lt;ol&gt;
&lt;li&gt;Inspect live active operations right now using &lt;code&gt;db.currentOp({active: true, secs_running: {$gt: 1}})&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;explain(&quot;executionStats&quot;)&lt;/code&gt; on your top three slowest queries identified in the profiler.&lt;/li&gt;
&lt;li&gt;Verify that the selectivity ratio ($\frac{\text{nReturned}}{\text{totalDocsExamined}}$) is greater than $0.5$ and that no &lt;code&gt;SORT&lt;/code&gt; stage exists.&lt;/li&gt;
&lt;li&gt;Implement &lt;code&gt;maxTimeMS(5000)&lt;/code&gt; on all application database drivers to prevent runaway queries from ever exhausting cluster read tickets.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;MongoDB Manual — Analyze Query Performance with explain(): &lt;a href=&quot;https://www.mongodb.com/docs/manual/tutorial/analyze-query-plan/&quot;&gt;https://www.mongodb.com/docs/manual/tutorial/analyze-query-plan/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — Indexing Strategies and the ESR (Equality, Sort, Range) Rule: &lt;a href=&quot;https://www.mongodb.com/docs/manual/tutorial/create-indexes-to-support-queries/&quot;&gt;https://www.mongodb.com/docs/manual/tutorial/create-indexes-to-support-queries/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — Database Profiler and Diagnostic Profiling Levels: &lt;a href=&quot;https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler/&quot;&gt;https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — WiredTiger Storage Engine Concurrent Transaction Tickets: &lt;a href=&quot;https://www.mongodb.com/docs/manual/reference/parameters/#mongodb-parameter-param.wiredTigerConcurrentReadTransactions&quot;&gt;https://www.mongodb.com/docs/manual/reference/parameters/#mongodb-parameter-param.wiredTigerConcurrentReadTransactions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Architecture Guide — The Slot-Based Execution (SBE) Engine: &lt;a href=&quot;https://www.mongodb.com/docs/manual/core/query-optimization/&quot;&gt;https://www.mongodb.com/docs/manual/core/query-optimization/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;ACM SIGMOD — Architecture and Performance of the WiredTiger Storage Engine: &lt;a href=&quot;https://dl.acm.org/doi/10.1145/3299869.3300069&quot;&gt;https://dl.acm.org/doi/10.1145/3299869.3300069&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Brendan Gregg — Systems Performance (2nd ed.) Chapter 9: Storage and Disk Latency Forensics: &lt;a href=&quot;https://www.brendangregg.com/systems-performance.html&quot;&gt;https://www.brendangregg.com/systems-performance.html&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>checklist</category><category>failures</category></item><item><title>MongoDB Index Internals: B-Tree Page Splits, the ESR Rule, and Plan Cache Invalidation</title><link>https://rajivonai.com/blog/2022-09-12-mongodb-index-basics-why-your-query-became-slow/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-09-12-mongodb-index-basics-why-your-query-became-slow/</guid><description>MongoDB&apos;s query planner depends on a precisely ordered B-Tree index to avoid COLLSCAN and in-memory sort stages. This post explains B-Tree page split mechanics, the ESR compound index ordering rule, how the plan cache works and when it invalidates, and the diagnostic sequence — from explain() to $indexStats — that identifies which indexes are costing you more than they save.</description><pubDate>Mon, 12 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A MongoDB query that returns one document from a collection of 10 million can execute in under 1ms — or in 8 seconds. The difference is entirely determined by whether the query planner finds a B-Tree index path that narrows the scan to the matching key range before fetching any documents. When no suitable index exists, MongoDB executes a COLLSCAN: every document in the collection is read from WiredTiger cache (or from disk if cache-evicted), deserialized, and evaluated against the filter predicate. At 10 million documents averaging 2KB each, a COLLSCAN reads approximately 20 GB of uncompressed data. At NVMe SSD throughput of 3 GB/s with a warm WiredTiger cache, that is 6.7 seconds minimum — for a query that fetches a single document.&lt;/p&gt;
&lt;p&gt;Understanding why this happens requires understanding how WiredTiger’s B-Tree index is physically organized, how the query planner selects among competing index paths, and how the plan cache stores and invalidates those decisions. Without this understanding, engineers add indexes in the wrong order, fail to satisfy sort stages, and create maintenance overhead from index proliferation — without improving the queries that actually matter.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;MongoDB uses WiredTiger’s B-Tree storage format for all indexes. A B-Tree index is a balanced tree of pages: internal (non-leaf) pages hold key-pointer pairs that route traversal down the tree; leaf pages hold the actual index entries — the indexed field value(s) and the corresponding document identifier (RecordId pointing to the WiredTiger row in the collection file).&lt;/p&gt;

































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Index Architecture&lt;/th&gt;&lt;th&gt;Key Ordering&lt;/th&gt;&lt;th&gt;Multi-Field Support&lt;/th&gt;&lt;th&gt;Array Field Support&lt;/th&gt;&lt;th&gt;Query Planner Interaction&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;WiredTiger B-Tree (MongoDB single-field)&lt;/td&gt;&lt;td&gt;Sorted ascending or descending on one field&lt;/td&gt;&lt;td&gt;No — one field per index&lt;/td&gt;&lt;td&gt;Yes — multikey index (one entry per array element)&lt;/td&gt;&lt;td&gt;Planner uses index for equality, range, and sort on the indexed field&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;WiredTiger B-Tree (MongoDB compound)&lt;/td&gt;&lt;td&gt;Sorted on field 1, then field 2, then field 3… lexicographically&lt;/td&gt;&lt;td&gt;Yes — up to 32 fields per compound index&lt;/td&gt;&lt;td&gt;Yes — at most one array field per compound index&lt;/td&gt;&lt;td&gt;Planner uses index for all queries matching any prefix of the index fields&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;WiredTiger B-Tree (MongoDB text/hashed)&lt;/td&gt;&lt;td&gt;Text: inverted index on stemmed tokens; Hashed: uniform distribution of hash values&lt;/td&gt;&lt;td&gt;Text: fields in the text index; Hashed: single field&lt;/td&gt;&lt;td&gt;Text: array of string elements; Hashed: no array support&lt;/td&gt;&lt;td&gt;Planner uses text index for $text queries; hashed for shard key equality; neither supports range queries&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The query planner is MongoDB’s cost-based optimizer. For each query, it generates candidate plans from available indexes and selects the plan with the lowest estimated cost — measured in “works” (units of work to scan index and fetch documents). When multiple indexes could satisfy a query, MongoDB runs the candidates in parallel using a “plan tournament” — each candidate executes up to a threshold of works; the first candidate to complete or the one completing the most work within the threshold wins and is cached for future identical query shapes.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The compounding nature of B-Tree index traversal creates three failure modes that explain the majority of MongoDB performance regressions as collections grow:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The COLLSCAN growth trap.&lt;/strong&gt; A collection with no index on a query field forces a full scan. At 10,000 documents the scan takes 5–20ms — fast enough to pass QA. At 1 million documents it takes 500ms–2s. At 10 million documents, latency becomes seconds. The transition from “acceptable” to “production incident” happens between 500K and 2M documents in most applications — precisely when traffic has also grown and the incident is most disruptive.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The wrong-order compound index.&lt;/strong&gt; An engineer adds a compound index &lt;code&gt;{userId: 1, createdAt: -1}&lt;/code&gt; to support a query &lt;code&gt;db.orders.find({status: &quot;shipped&quot;}).sort({createdAt: -1})&lt;/code&gt;. This index cannot be used for this query: &lt;code&gt;status&lt;/code&gt; is not in the index prefix. An IXSCAN on &lt;code&gt;{userId: 1, createdAt: -1}&lt;/code&gt; would require scanning the entire index to find documents where &lt;code&gt;status == &quot;shipped&quot;&lt;/code&gt; — the planner correctly recognizes this is worse than a COLLSCAN and does not use the index.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The in-memory sort abort.&lt;/strong&gt; A query uses an IXSCAN for filtering but the sort field is not in the index. MongoDB buffers the filter results, then sorts in memory. If the result set before sorting exceeds 100MB (the &lt;code&gt;allowDiskUse&lt;/code&gt; default threshold), MongoDB aborts with: &lt;code&gt;Sort exceeded memory limit of 104857600 bytes&lt;/code&gt;. This failure mode appears suddenly in production when a previously filtered result set grows past the threshold.&lt;/p&gt;
&lt;p&gt;How do you determine, from first principles, what compound index field order satisfies a query’s filter, sort, and range requirements simultaneously — and how do you verify the planner is using the index you intended?&lt;/p&gt;
&lt;h2 id=&quot;b-tree-page-splits-why-index-write-cost-scales-non-linearly&quot;&gt;B-Tree Page Splits: Why Index Write Cost Scales Non-Linearly&lt;/h2&gt;
&lt;p&gt;Understanding B-Tree page splits explains why write-heavy workloads with many indexes experience disproportionate write amplification, and why index field order affects both read and write performance.&lt;/p&gt;
&lt;p&gt;WiredTiger B-Tree pages have a default maximum size of 32KB for leaf pages. When an index insert causes a leaf page to exceed 32KB, WiredTiger splits the page: the leaf page is divided into two new leaf pages, and a new key-pointer entry is added to the parent internal page pointing to the newly created right leaf. If the parent internal page also exceeds its maximum size, it splits too — propagating up the tree in a worst case that reaches the root.&lt;/p&gt;
&lt;p&gt;Page splits have two operational consequences:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Write amplification.&lt;/strong&gt; A single index insert can trigger multiple page writes: the split leaf page(s) and the updated internal page(s). On a collection with 10 indexes, each document insert can trigger up to 10 × (split factor) page writes — B-Tree maintenance multiplies write I/O beyond the raw document count.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sequential vs. random insert patterns.&lt;/strong&gt; Index entries for a monotonically increasing field (like &lt;code&gt;_id: ObjectId()&lt;/code&gt; or &lt;code&gt;createdAt: new Date()&lt;/code&gt;) are always appended to the rightmost leaf page. The rightmost page fills and splits, then the new rightmost page fills and splits — a repeating sequential write pattern that generates minimal B-Tree structural rebalancing. By contrast, inserts on a randomly distributed field (like a hashed UUID) cause page splits across the entire B-Tree — a random write pattern that generates more I/O per insert. This is why MongoDB recommends ObjectId-based shard keys for time-series collections and why monotonic fields as leading index keys minimize write amplification.&lt;/p&gt;
&lt;h2 id=&quot;in-practice-the-esr-rule-and-plan-cache-mechanics&quot;&gt;In Practice: The ESR Rule and Plan Cache Mechanics&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;The ESR rule — mechanical derivation.&lt;/strong&gt; The ESR rule (Equality fields first, Sort fields second, Range fields last) is not an arbitrary convention. It derives from how the B-Tree stores multi-field keys and how the planner traverses them:&lt;/p&gt;
&lt;p&gt;A compound index on &lt;code&gt;{status: 1, createdAt: -1, amount: 1}&lt;/code&gt; stores keys sorted first by &lt;code&gt;status&lt;/code&gt; (ascending), then within each &lt;code&gt;status&lt;/code&gt; value by &lt;code&gt;createdAt&lt;/code&gt; (descending), then within each &lt;code&gt;(status, createdAt)&lt;/code&gt; pair by &lt;code&gt;amount&lt;/code&gt; (ascending).&lt;/p&gt;
&lt;p&gt;For a query &lt;code&gt;find({status: &quot;shipped&quot;, amount: {$gt: 100}}).sort({createdAt: -1})&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Equality on &lt;code&gt;status&lt;/code&gt;&lt;/strong&gt;: The B-Tree traversal seeks directly to the contiguous key range where &lt;code&gt;status == &quot;shipped&quot;&lt;/code&gt; — an O(log N) point lookup regardless of collection size.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sort on &lt;code&gt;createdAt&lt;/code&gt;&lt;/strong&gt;: Within the &lt;code&gt;status == &quot;shipped&quot;&lt;/code&gt; range, keys are already stored in descending &lt;code&gt;createdAt&lt;/code&gt; order. The index scan returns keys in the required sort order — no in-memory sort stage needed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Range on &lt;code&gt;amount&lt;/code&gt;&lt;/strong&gt;: Within each &lt;code&gt;(status, createdAt)&lt;/code&gt; pair, &lt;code&gt;amount&lt;/code&gt; is stored sorted — but since &lt;code&gt;createdAt&lt;/code&gt; values are unique, the &lt;code&gt;amount&lt;/code&gt; dimension provides no meaningful narrowing. Range fields go last because they cannot be used for index ordering after a range boundary is set.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you reverse the order and put the range field first — &lt;code&gt;{amount: 1, status: 1, createdAt: -1}&lt;/code&gt; — the B-Tree scan must traverse the entire key range where &lt;code&gt;amount &gt; 100&lt;/code&gt; (which may be 80% of the index) scanning for &lt;code&gt;status == &quot;shipped&quot;&lt;/code&gt; values — eliminating the benefit of the equality condition’s O(log N) point lookup.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reading explain() output.&lt;/strong&gt; The definitive diagnostic:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Full execution stats — use this, not just explain() without args&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.orders.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({status: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;shipped&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, amount: {$gt: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;         .&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;({createdAt: &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;         .&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;explain&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;executionStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Fields that matter:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// In the explain output, look for these paths:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;queryPlanner&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;winningPlan&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      &quot;stage&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;FETCH&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,             &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Good — wrapping an IXSCAN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;      &quot;inputStage&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;stage&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;IXSCAN&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,          &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Good — index scan used&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;indexName&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;status_1_createdAt_-1_amount_1&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;direction&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;forward&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;        &quot;indexBounds&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {            &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Verify the bounds match your query&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          &quot;status&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;shipped&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;shipped&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;\&quot;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;]&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          &quot;createdAt&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;[MaxKey, MinKey]&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;],  &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Full range — sort direction&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;          &quot;amount&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;(100.0, inf.0]&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]        &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Range filter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;rejectedPlans&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: [&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]          &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Check what the planner rejected and why&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  },&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &quot;executionStats&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;nReturned&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;42&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,                &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Documents returned to client&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;totalDocsExamined&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;42&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,        &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Should equal nReturned for perfect index selectivity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;totalKeysExamined&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;43&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,        &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Should be close to nReturned; much higher = poor selectivity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;executionTimeMillis&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,       &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Wall clock time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    &quot;stage&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;SORT&quot;&lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;                 // RED FLAG — in-memory sort; should not appear if ESR is correct&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;totalDocsExamined&lt;/code&gt; vs &lt;code&gt;nReturned&lt;/code&gt;&lt;/strong&gt;: If &lt;code&gt;totalDocsExamined&lt;/code&gt; is 10× &lt;code&gt;nReturned&lt;/code&gt;, the index is scanning far more documents than it returns — poor selectivity, often indicating a low-cardinality leading field or a missing equality condition.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;SORT&lt;/code&gt; stage in winning plan&lt;/strong&gt;: If the winning plan includes a &lt;code&gt;SORT&lt;/code&gt; stage above the &lt;code&gt;IXSCAN&lt;/code&gt;, the index does not cover the sort. The query is sorting in memory — which aborts at 100MB.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Plan cache mechanics and invalidation.&lt;/strong&gt; MongoDB caches winning plans per query shape (the structure of the query without the literal parameter values). The cache stores the winning plan from the plan tournament. Queries with the same shape reuse the cached plan without re-running the tournament.&lt;/p&gt;
&lt;p&gt;Plan cache invalidation occurs automatically when:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;An index is added or dropped on the collection&lt;/li&gt;
&lt;li&gt;The collection is rebuilt or compacted&lt;/li&gt;
&lt;li&gt;The plan cache is explicitly cleared: &lt;code&gt;db.collection.getPlanCache().clear()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;MongoDB detects that the cached plan is performing significantly worse than expected (replanning triggered by the “works” threshold)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;After a new index is added, &lt;strong&gt;the plan cache is cleared for that collection&lt;/strong&gt; — the next queries re-run the plan tournament against all available indexes. This means a new index can cause a temporary latency increase as plan tournaments run for each distinct query shape.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;$indexStats&lt;/code&gt; — finding unused indexes:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Which indexes have never been used (candidates for removal)?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.orders.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aggregate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;([&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  {$indexStats: {}},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  {$project: {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    name: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    ops: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$accesses.ops&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    since: &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$accesses.since&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    unusedDays: {$divide: [{$subtract: [&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; Date&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(), &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;$accesses.since&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]}, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;86400000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  {$match: {ops: {$lt: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}}},    &lt;/span&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Fewer than 100 accesses since last restart&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  {$sort: {ops: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Indexes with zero &lt;code&gt;accesses.ops&lt;/code&gt; since the last mongod restart are candidates for removal. Each unused index consumes WiredTiger cache space (index pages are loaded on write, even if never used for reads) and adds write amplification to every insert, update, and delete on the collection.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Compound index key compression.&lt;/strong&gt; WiredTiger uses prefix key compression for B-Tree index pages by default. For a compound index &lt;code&gt;{status: 1, createdAt: -1}&lt;/code&gt; where many documents share the same &lt;code&gt;status&lt;/code&gt; value, WiredTiger stores the &lt;code&gt;status&lt;/code&gt; prefix once and stores only the &lt;code&gt;createdAt&lt;/code&gt; suffix for subsequent entries with the same status. This prefix compression reduces index on-disk size by 40–80% for low-cardinality leading fields — but it also means that the in-memory uncompressed representation of the index is significantly larger than the compressed on-disk representation. Index sizing using &lt;code&gt;db.collection.stats().indexSizes&lt;/code&gt; returns the compressed on-disk size; multiply by 2–4× for the actual WiredTiger cache footprint.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Multikey index internals.&lt;/strong&gt; When a field contains an array, WiredTiger creates one index entry per array element. A document with &lt;code&gt;tags: [&quot;mongodb&quot;, &quot;indexes&quot;, &quot;performance&quot;]&lt;/code&gt; creates 3 index entries under a &lt;code&gt;{tags: 1}&lt;/code&gt; index. This has two implications:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;javascript&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Check if an index is multikey (expanded from array fields)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.collection.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;getIndexes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Look for &quot;multiKey: true&quot; in the index flags&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;// Or query:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;db.collection.&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aggregate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;([{$indexStats: {}}, {$project: {name: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&quot;spec.key&quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}}])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Multikey expansion inflates index size proportionally to average array cardinality. A collection where each document has 20 tags has a &lt;code&gt;{tags: 1}&lt;/code&gt; index that is 20× larger than the equivalent single-value field index. Index size estimation must account for average array length for multikey indexes.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Root Cause&lt;/th&gt;&lt;th&gt;Production Blast Radius&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;COLLSCAN at production data volume — passed QA&lt;/td&gt;&lt;td&gt;Query lacks a covering index; collection was small during testing and masked the O(N) scan cost&lt;/td&gt;&lt;td&gt;At 10M documents, query latency is seconds; at 50M documents, minutes; collection scan holds shared collection lock on older MongoDB versions, blocking other operations&lt;/td&gt;&lt;td&gt;Run &lt;code&gt;db.collection.explain(&quot;executionStats&quot;).find(query)&lt;/code&gt; against a production-representative data volume; any COLLSCAN or &lt;code&gt;totalDocsExamined&lt;/code&gt; &gt;&gt; &lt;code&gt;nReturned&lt;/code&gt; must be resolved before launch&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;In-memory sort abort — Sort exceeded memory limit&lt;/td&gt;&lt;td&gt;Compound index does not cover the sort field (sort field not in index, or in wrong position after a range field); result set before sorting exceeds 100MB&lt;/td&gt;&lt;td&gt;Query aborts with error: &lt;code&gt;Sort exceeded memory limit of 104857600 bytes&lt;/code&gt;; if &lt;code&gt;allowDiskUse: true&lt;/code&gt; is set, sort spills to disk and operation takes 10–100× longer&lt;/td&gt;&lt;td&gt;Add sort field to the compound index in ESR order; if the sort must remain in-memory for performance reasons, reduce the pre-sort result set with tighter equality filters; for analytics, accept disk-based sort with &lt;code&gt;allowDiskUse: true&lt;/code&gt; on a secondary&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Plan cache selects wrong index after new index added&lt;/td&gt;&lt;td&gt;Plan tournament runs on new query shapes after index addition; for some shapes, the planner selects a new index that is suboptimal due to stale statistics or low-work-threshold tournament&lt;/td&gt;&lt;td&gt;Latency regression on existing queries after an index is added to speed up a different query&lt;/td&gt;&lt;td&gt;Monitor query latency after any index addition; use &lt;code&gt;$comment&lt;/code&gt; on critical queries to identify them in the slow query log; if the planner selects wrong plan, use &lt;code&gt;hint()&lt;/code&gt; to force the correct index while the planner accumulates better statistics&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Index proliferation — write amplification exceeds read benefit&lt;/td&gt;&lt;td&gt;Teams add an index for every new query without removing superceded indexes; a collection with 20 indexes has 20× write amplification per document operation&lt;/td&gt;&lt;td&gt;Write throughput drops as insert/update latency grows with index count; during high-write events (bulk imports, migrations), index maintenance creates I/O saturation&lt;/td&gt;&lt;td&gt;Audit indexes quarterly using &lt;code&gt;$indexStats&lt;/code&gt;; remove any index with &lt;code&gt;ops &amp;#x3C; 100&lt;/code&gt; since last restart; prefer compound indexes that cover multiple query patterns over single-field indexes for each&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;B-Tree page splits during bulk insert — write stall&lt;/td&gt;&lt;td&gt;Bulk inserting randomly distributed documents (e.g., random UUIDs) into a collection with compound indexes causes page splits across the entire B-Tree simultaneously; eviction and checkpoint compete for I/O&lt;/td&gt;&lt;td&gt;Bulk import throughput drops 10–100× compared to sequential inserts; &lt;code&gt;mongod&lt;/code&gt; CPU spikes on reconciliation threads&lt;/td&gt;&lt;td&gt;For bulk imports, use monotonically increasing &lt;code&gt;_id&lt;/code&gt; (ObjectId or sequential integer) to minimize page splits; temporarily drop and rebuild secondary indexes after bulk import using &lt;code&gt;db.collection.createIndex()&lt;/code&gt; with &lt;code&gt;background: true&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sparse index misuse — documents excluded silently&lt;/td&gt;&lt;td&gt;A sparse index on a field that is sometimes absent from documents skips documents where the field is missing; queries that filter on this field using sparse index miss those documents&lt;/td&gt;&lt;td&gt;Data integrity failure: query returns fewer results than actually exist; no error is thrown&lt;/td&gt;&lt;td&gt;Use sparse indexes only when the absent-field documents are correctly excluded from the query domain; for nullable-equivalent fields, use a partial index with an explicit filter expression instead: &lt;code&gt;{key: 1}&lt;/code&gt; with &lt;code&gt;{partialFilterExpression: {key: {$exists: true}}}&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Multikey compound index — array fields rejected&lt;/td&gt;&lt;td&gt;Attempting to create a compound index where two or more fields contain arrays; MongoDB/WiredTiger rejects this with &lt;code&gt;MongoServerError: cannot index parallel arrays [field1] [field2]&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Index creation fails; the application fallback (COLLSCAN or single-field index) may be inadequate for the query&lt;/td&gt;&lt;td&gt;Design document schema to avoid having two parallel array fields that both need indexing in the same compound index; refactor the document structure to embed the relationship within a single array of subdocuments&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Covered query not covered — projection omitted&lt;/td&gt;&lt;td&gt;Engineers design a compound index to cover a query but forget to include the projected fields in the index key; MongoDB must fetch the full document from the collection to apply the projection&lt;/td&gt;&lt;td&gt;The IXSCAN stage is present but followed by a FETCH stage for each document — double I/O versus a true covered query that serves results from index pages alone&lt;/td&gt;&lt;td&gt;Verify covered query by checking for absence of &lt;code&gt;FETCH&lt;/code&gt; stage in explain output; the covered index must include all fields in filter, sort, AND projection; &lt;code&gt;_id&lt;/code&gt; must be explicitly excluded (&lt;code&gt;{_id: 0}&lt;/code&gt;) if not in the index to avoid a FETCH for &lt;code&gt;_id&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;2024-index-optimizer-and-vector-search-update&quot;&gt;2024 Index Optimizer and Vector Search Update&lt;/h2&gt;
&lt;p&gt;The 2023 query engine improvements (shipping in MongoDB’s 7.x release series, GA August 2023) made the plan tournament more accurate for compound indexes on selective fields. The improved cost model accounts for index key distribution statistics more accurately, reducing cases where the planner selects a suboptimal index due to underestimating the selectivity of an equality filter on a compound index prefix.&lt;/p&gt;
&lt;p&gt;The 7.x release also introduced &lt;code&gt;$rankFusion&lt;/code&gt; and &lt;code&gt;$vectorSearch&lt;/code&gt; index types for Atlas Vector Search — hybrid indexes that combine BM25 text scoring with ANN vector similarity in a single query plan. These index types operate on a separate FAISS-based index structure, not WiredTiger B-Trees, and are not subject to the ESR rule or the plan cache mechanics described above.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;explain(&quot;queryPlanner&quot;)&lt;/code&gt; output format was enhanced in the 2023 release cycle to include &lt;code&gt;planSummary&lt;/code&gt; at the top level — a human-readable one-line description of the winning plan (e.g., &lt;code&gt;&quot;IXSCAN { status: 1, createdAt: -1 }&quot;&lt;/code&gt;) that is easier to scan in monitoring dashboards than the full JSON plan tree.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; MongoDB index selection is a function of compound index field order, query filter structure, and sort direction; when the index field order does not match the ESR pattern for the query, the planner either cannot use the index or uses it inefficiently — returning to full collection scan behavior at scale.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Apply the ESR rule to every compound index: equality-filter fields first, sort fields second, range-filter fields last; verify with &lt;code&gt;.explain(&quot;executionStats&quot;)&lt;/code&gt; that the winning plan shows IXSCAN (not COLLSCAN), that &lt;code&gt;totalDocsExamined&lt;/code&gt; equals &lt;code&gt;nReturned&lt;/code&gt;, and that no &lt;code&gt;SORT&lt;/code&gt; stage appears above the IXSCAN.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The B-Tree key storage format — sorted lexicographically on field 1, then field 2, then field 3 — means that a sort on field 2 is only index-ordered within a fixed field 1 value; placing a range condition on field 1 before a sort field on field 2 destroys the sort ordering within the scanned range, forcing an in-memory sort stage regardless of whether the index exists.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Run &lt;code&gt;db.collection.aggregate([{$indexStats: {}}])&lt;/code&gt; on your three highest-traffic collections and identify any index with fewer than 100 &lt;code&gt;accesses.ops&lt;/code&gt; since the last mongod restart — those are candidates for removal and their write amplification cost is pure overhead with no read benefit.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;sources-to-verify&quot;&gt;Sources to Verify&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;MongoDB Manual — Indexing Strategies and the ESR Rule: &lt;a href=&quot;https://www.mongodb.com/docs/manual/tutorial/create-indexes-to-support-queries/&quot;&gt;https://www.mongodb.com/docs/manual/tutorial/create-indexes-to-support-queries/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — explain() Output Reference — executionStats Fields: &lt;a href=&quot;https://www.mongodb.com/docs/manual/reference/explain-results/&quot;&gt;https://www.mongodb.com/docs/manual/reference/explain-results/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;WiredTiger B-Tree Storage Architecture — Key Ordering and Prefix Compression: &lt;a href=&quot;https://source.wiredtiger.com/develop/arch-btree.html&quot;&gt;https://source.wiredtiger.com/develop/arch-btree.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — $indexStats Aggregation Stage: &lt;a href=&quot;https://www.mongodb.com/docs/manual/reference/operator/aggregation/indexStats/&quot;&gt;https://www.mongodb.com/docs/manual/reference/operator/aggregation/indexStats/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — Multikey Indexes and Compound Index Restrictions: &lt;a href=&quot;https://www.mongodb.com/docs/manual/core/index-multikey/&quot;&gt;https://www.mongodb.com/docs/manual/core/index-multikey/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MongoDB Manual — Plan Cache Reference and Invalidation Conditions: &lt;a href=&quot;https://www.mongodb.com/docs/manual/reference/method/db.collection.getPlanCache/&quot;&gt;https://www.mongodb.com/docs/manual/reference/method/db.collection.getPlanCache/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;WiredTiger B-Tree Page Split Mechanics — WiredTiger Source Architecture Documentation: &lt;a href=&quot;https://source.wiredtiger.com/develop/arch-schema.html&quot;&gt;https://source.wiredtiger.com/develop/arch-schema.html&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content:encoded><category>databases</category><category>failures</category></item><item><title>AWS E-Commerce Checkout Architecture: SQS, Lambda, Aurora, and DynamoDB</title><link>https://rajivonai.com/blog/2022-09-08-aws-e-commerce-checkout-architecture-sqs-lambda-aurora-and-dynamodb/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-09-08-aws-e-commerce-checkout-architecture-sqs-lambda-aurora-and-dynamodb/</guid><description>Checkout fails when payment, inventory, order history, and notification are treated as one synchronous request — how to model checkout as one committed decision followed by recoverable asynchronous consequences using SQS, Lambda, Aurora, and DynamoDB.</description><pubDate>Thu, 08 Sep 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Checkout fails when the system treats payment, inventory, order history, and customer notification as one synchronous request instead of one committed decision followed by several recoverable consequences.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A modern e-commerce checkout path is no longer a single database insert behind a web form. The request usually touches pricing, promotions, tax, payment authorization, fraud screening, inventory reservation, fulfillment, email, analytics, and customer service history. Each dependency has different latency, consistency, and failure behavior.&lt;/p&gt;
&lt;p&gt;AWS makes it tempting to wire this together quickly: API Gateway receives the request, Lambda runs the workflow, Aurora stores the order, DynamoDB stores fast state, and SQS buffers downstream work. The services are individually durable and scalable. The failure mode is not usually that one service is weak. The failure mode is that the architecture does not declare which operation is the checkout decision and which operations are consequences of that decision.&lt;/p&gt;
&lt;p&gt;The central design constraint is simple: the buyer should receive one checkout result, the merchant should receive one order, and every retry should be safe.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The naive architecture puts all checkout work inside one Lambda invocation. It validates the cart, calls the payment provider, decrements inventory, writes the order, sends the email, and returns success. This looks attractive because the code follows the business process. Operationally, it couples the buyer’s request to the slowest and least reliable dependency.&lt;/p&gt;
&lt;p&gt;A timeout after the payment provider succeeds but before the order write returns creates an unknown state. Retrying the Lambda may charge twice unless the system has an idempotency key. Writing Aurora before publishing an SQS message creates a different gap: the order exists, but fulfillment never starts if the process fails between the database commit and queue send. Publishing first is not better; the consumer may process an order that the database later rolls back.&lt;/p&gt;
&lt;p&gt;SQS also changes the shape of failure. It absorbs bursts, but it does not make work exactly once. Messages can be delivered more than once, processed out of the expected wall-clock order, or moved to a dead letter queue after repeated failures. Lambda concurrency can drain a backlog faster than downstream databases or providers can tolerate. Aurora can protect transactional order state, but it can also become the choke point if every asynchronous worker opens its own connection. DynamoDB can handle high-volume key-value access, but only when the access patterns and conditional writes are designed upfront.&lt;/p&gt;
&lt;p&gt;The question is not “should checkout be synchronous or asynchronous?” The question is: what is the smallest synchronous commitment that makes the order real, and how do the remaining steps become retryable without corrupting money, inventory, or customer state?&lt;/p&gt;
&lt;h2 id=&quot;a-commit-first-checkout-architecture&quot;&gt;A Commit First Checkout Architecture&lt;/h2&gt;
&lt;p&gt;The answer is a commit-first architecture: keep the customer-facing request short, persist the checkout decision transactionally, and use queues to execute consequences with idempotent workers.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;A[buyer — submit checkout] --&gt; B[API Gateway — request boundary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;B --&gt; C[checkout Lambda — validate and price]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;C --&gt; D[Aurora — order and payment intent]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;C --&gt; E[DynamoDB — idempotency key and cart snapshot]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;C --&gt; F[SQS — checkout command queue]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;F --&gt; G[payment Lambda — charge provider]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;G --&gt; H[Aurora — payment state]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;G --&gt; I[SQS — fulfillment queue]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;I --&gt; J[fulfillment Lambda — reserve inventory]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;J --&gt; K[DynamoDB — inventory reservation]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;J --&gt; L[SQS — notification queue]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;L --&gt; M[notification Lambda — receipt and status]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;C --&gt; N[CloudWatch — metrics and traces]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;F --&gt; O[dead letter queue — poison commands]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The checkout Lambda should do only the work required to accept or reject the order. It verifies the cart, calculates the final price, checks the idempotency key, creates an order in &lt;code&gt;PENDING_PAYMENT&lt;/code&gt;, records the payment intent, and returns an order identifier. Aurora is the right fit for the order ledger when the business needs relational constraints, transactional updates, reporting joins, and a clear source of truth for financial state.&lt;/p&gt;
&lt;p&gt;DynamoDB should not be used as a generic second database. It should own access patterns that benefit from conditional writes and predictable key lookups: idempotency records keyed by request token, cart snapshots keyed by customer and checkout attempt, inventory reservations keyed by SKU and order, and short-lived workflow state with TTL. Conditional writes make retries safe because the second attempt observes the first decision instead of repeating it.&lt;/p&gt;
&lt;p&gt;SQS should carry commands between stages: authorize payment, reserve inventory, start fulfillment, send receipt, publish analytics. Each message should include an order ID, idempotency key, attempt metadata, and schema version. Consumers should be idempotent at their own boundary. The payment worker records provider request IDs. The inventory worker uses conditional reservation records. The email worker records notification type per order.&lt;/p&gt;
&lt;p&gt;The hardest boundary is the write from Aurora to SQS. A production design should use a transactional outbox: write the order and the outbound event into Aurora in the same transaction, then let a relay publish outbox rows to SQS and mark them sent. That turns an unsafe dual write into a recoverable polling problem. If the relay dies, the outbox row remains. If SQS publish succeeds but marking sent fails, the relay may publish again, so consumers still need idempotency.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS explicitly documents that distributed systems must handle ambiguous outcomes. The Amazon Builders’ Library article “Challenges with distributed systems” describes cases where a client cannot know whether a request failed before execution, failed after execution, or succeeded while the response was lost. Checkout has the same ambiguity around payment, order writes, and fulfillment commands.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; The documented pattern is to make retries safe with caller-provided idempotency tokens, as described in the Builders’ Library article “Making retries safe with idempotent APIs.” In this checkout architecture, the token is not a logging field. It is part of the write path. The first request creates the idempotency record and order. Later retries return the existing result or continue the same workflow.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The result is not exactly-once execution. The result is exactly-once business effect. SQS and Lambda may still retry work, and a worker may see the same command again. The durable state in Aurora and DynamoDB decides whether the business action has already happened.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; AWS Prescriptive Guidance for Lambda partial batch responses with SQS warns about dead letter queues and the snowball pattern, where failing messages are returned to the queue and consume more capacity over time. The operational lesson for checkout is that queue depth is not merely a scaling metric. It is a correctness signal. A growing payment queue means buyers may have accepted orders that are not yet authorized. A growing fulfillment queue means paid orders may not be reserving inventory fast enough.&lt;/p&gt;
&lt;p&gt;Amazon’s Builders’ Library article “Avoiding insurmountable queue backlogs” also treats backlog age as a first-class operational concern. The checkout version of that lesson is to alarm on age of oldest message, not only message count. Ten thousand fresh notification messages are different from one payment command that has been stuck for thirty minutes.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it hurts&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Lambda times out after payment succeeds&lt;/td&gt;&lt;td&gt;Retry can double charge&lt;/td&gt;&lt;td&gt;Provider idempotency key and local payment state&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Aurora commit succeeds but SQS publish fails&lt;/td&gt;&lt;td&gt;Order exists without downstream work&lt;/td&gt;&lt;td&gt;Transactional outbox with replayable relay&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;SQS delivers a duplicate message&lt;/td&gt;&lt;td&gt;Worker repeats side effect&lt;/td&gt;&lt;td&gt;Conditional writes and per-stage idempotency&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Poison message blocks progress&lt;/td&gt;&lt;td&gt;Queue capacity is spent on hopeless retries&lt;/td&gt;&lt;td&gt;Partial batch response and dead letter queue&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Queue drains too quickly&lt;/td&gt;&lt;td&gt;Aurora or provider is overloaded&lt;/td&gt;&lt;td&gt;Reserved concurrency and rate limits per worker&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Inventory reservation races&lt;/td&gt;&lt;td&gt;Oversell during bursts&lt;/td&gt;&lt;td&gt;DynamoDB conditional update per SKU reservation&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Reporting reads hit checkout tables&lt;/td&gt;&lt;td&gt;Customer path slows under analytics load&lt;/td&gt;&lt;td&gt;Read replicas, event projection, or separate warehouse&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Manual repair lacks state&lt;/td&gt;&lt;td&gt;Support cannot tell what happened&lt;/td&gt;&lt;td&gt;Order state machine and audit events&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; A checkout request crosses too many unreliable boundaries to be treated as one synchronous transaction.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Commit the order decision first, then drive payment, inventory, fulfillment, and notification through SQS-backed idempotent workers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; AWS documented patterns for idempotent APIs, SQS retry behavior, partial batch failure handling, and queue backlog management all point to the same conclusion: retries are normal, ambiguity is normal, and durable state must make repeated execution safe.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Design the checkout state machine before writing Lambdas. Define the Aurora order states, DynamoDB idempotency keys, SQS message contracts, dead letter replay process, and alarms for oldest message age on every queue.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>Aurora vs RDS: The Operational Difference Engineers Actually Feel</title><link>https://rajivonai.com/blog/2022-08-09-aurora-vs-rds-the-operational-difference-engineers-actually-feel/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-08-09-aurora-vs-rds-the-operational-difference-engineers-actually-feel/</guid><description>The real difference between Aurora and RDS shows up during storage stall, replica lag, and failover at 03:00 — how the two products behave differently under failure and what those differences mean for operational choice and cost.</description><pubDate>Tue, 09 Aug 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;The real difference between Aurora and standard RDS is not the API, the console, or the word “managed.” It is what happens at 03:00 when storage stalls, replicas lag, failover starts, and the application keeps asking the same brutal question: can I still commit?&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Attribute&lt;/th&gt;&lt;th&gt;Standard RDS&lt;/th&gt;&lt;th&gt;Aurora&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Storage model&lt;/td&gt;&lt;td&gt;Instance-attached EBS&lt;/td&gt;&lt;td&gt;Distributed cluster volume — 6 copies across 3 AZs&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failover mechanism&lt;/td&gt;&lt;td&gt;Standby promotion&lt;/td&gt;&lt;td&gt;Reader promotion; compute reattaches to shared storage&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Typical failover time&lt;/td&gt;&lt;td&gt;60–120s&lt;/td&gt;&lt;td&gt;30–60s&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Read replicas&lt;/td&gt;&lt;td&gt;Up to 5 (PostgreSQL), separate storage&lt;/td&gt;&lt;td&gt;Up to 15, shared cluster volume&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Replica lag&lt;/td&gt;&lt;td&gt;Independent replication delay&lt;/td&gt;&lt;td&gt;Lower lag (shared storage)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backup model&lt;/td&gt;&lt;td&gt;Scheduled snapshot against instance&lt;/td&gt;&lt;td&gt;Continuous, built into storage layer&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Storage growth&lt;/td&gt;&lt;td&gt;Manual provisioning or autoscaling policy&lt;/td&gt;&lt;td&gt;Auto-grows in 10 GiB increments&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cost model&lt;/td&gt;&lt;td&gt;Instance + EBS: straightforward&lt;/td&gt;&lt;td&gt;Instance + Aurora storage I/O: higher, separate billing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Choose when&lt;/td&gt;&lt;td&gt;Predictable moderate workload, cost-sensitive&lt;/td&gt;&lt;td&gt;High availability, read-heavy, larger scale, faster recovery&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Most engineering teams first meet Amazon RDS as a way to stop operating databases by hand. RDS gives you managed provisioning, backups, patching, monitoring hooks, parameter groups, snapshots, and Multi-AZ options across engines such as PostgreSQL and MySQL. For many systems, that is exactly the right abstraction: a familiar database engine with less host-level operational work.&lt;/p&gt;
&lt;p&gt;Aurora looks similar from the outside. It speaks PostgreSQL-compatible or MySQL-compatible protocols. Applications connect through endpoints. Engineers still think in schemas, transactions, query plans, locks, vacuum, indexes, and connection pools. That surface similarity is why Aurora is often described too casually as “faster RDS.”&lt;/p&gt;
&lt;p&gt;That framing misses the operational point.&lt;/p&gt;
&lt;p&gt;Standard RDS is primarily a managed database instance model. Aurora is closer to a distributed storage and database control-plane model with a database-compatible compute layer on top. That distinction changes the failure modes engineers feel during scaling, recovery, replica reads, backup pressure, and writer failover.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The common failure is choosing between RDS and Aurora using only benchmark numbers or monthly cost estimates. Those matter, but they do not describe the on-call experience.&lt;/p&gt;
&lt;p&gt;A standard RDS PostgreSQL or MySQL deployment still centers operationally on database instances and their attached storage. With Multi-AZ, AWS provisions a standby in another Availability Zone and uses synchronous replication for high availability. If the primary fails, RDS promotes the standby. This is a strong, well-understood pattern, but the instance boundary remains central. Storage, compute, replication topology, failover, and maintenance all feel tied to the lifecycle of database instances.&lt;/p&gt;
&lt;p&gt;Aurora changes that shape. Its storage layer is distributed across multiple Availability Zones, and compute instances attach to that shared cluster volume. Replicas do not behave like traditional independent replicas replaying a full stream into their own isolated storage. They read from the same distributed storage system. Backups are continuous and designed around the storage layer rather than a heavy snapshot event against one attached volume.&lt;/p&gt;
&lt;p&gt;That architecture does not make Aurora magic. It introduces its own constraints, costs, and surprises. But it moves several operational problems out of the database instance and into the storage service and cluster control plane.&lt;/p&gt;
&lt;p&gt;So the real question is not “Which one is faster?” It is: &lt;strong&gt;which failure boundary do you want your application and your operators to live with?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-operational-boundary-is-the-architecture&quot;&gt;The Operational Boundary Is the Architecture&lt;/h2&gt;
&lt;p&gt;In standard RDS, the primary operational unit is the database instance. In Aurora, the primary operational unit is the cluster: writer compute, reader compute, endpoints, and a distributed storage volume.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  App[application — connection pool] --&gt; Endpoint[database endpoint — routing target]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Endpoint --&gt; RDSPrimary[RDS primary — compute and storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  RDSPrimary --&gt; RDSStandby[RDS standby — synchronous replica]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  RDSPrimary --&gt; RDSBackup[RDS backup — snapshot workflow]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Endpoint --&gt; AuroraWriter[Aurora writer — compute node]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Endpoint --&gt; AuroraReader[Aurora reader — read endpoint]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  AuroraWriter --&gt; AuroraStorage[Aurora cluster volume — distributed storage]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  AuroraReader --&gt; AuroraStorage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  AuroraStorage --&gt; AZA[storage copies — zone A]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  AuroraStorage --&gt; AZB[storage copies — zone B]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  AuroraStorage --&gt; AZC[storage copies — zone C]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  RDSPrimary --&gt;|failover promotes| RDSStandby&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  AuroraWriter --&gt;|failover reattaches| AuroraReader&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What this diagram shows:&lt;/strong&gt; RDS couples compute and storage on each node — failover requires the standby to be promoted to primary, which takes time proportional to the pending WAL. Aurora separates compute from its cluster volume, which spans three availability zones. Aurora failover reattaches a reader compute node to the shared storage rather than promoting a replica — which is why Aurora’s failover is faster and doesn’t require a storage copy.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;That difference shows up in five places.&lt;/p&gt;
&lt;p&gt;First, failover is a different kind of event. In RDS Multi-AZ, failover promotes a standby instance. In Aurora, failover usually promotes an existing reader to become the writer while it continues using the shared storage layer. Both can interrupt clients. Both require connection retry discipline. But Aurora removes more of the storage catch-up problem from the failover path.&lt;/p&gt;
&lt;p&gt;Second, read scaling has a different ceiling. RDS read replicas are useful, but they are separate replicas with their own replication lag and storage. Aurora replicas share the cluster volume, which can reduce replica lag and make reader promotion operationally cleaner. This helps read-heavy systems, though it does not solve write contention, bad indexing, or overloaded connection pools.&lt;/p&gt;
&lt;p&gt;Third, backup pressure feels different. RDS automated backups and snapshots are managed, but they still feel closer to the lifecycle of an instance and its storage. Aurora’s continuous backup model is built into the distributed storage layer. That can make point-in-time recovery and backup behavior feel less intrusive, especially for larger databases.&lt;/p&gt;
&lt;p&gt;Fourth, storage growth is less of a planning ceremony in Aurora. Standard RDS storage choices still require more explicit capacity thinking. Aurora storage grows automatically in the cluster volume model. That does not mean storage cost disappears; it means the operational failure of under-provisioning disk becomes less common.&lt;/p&gt;
&lt;p&gt;Fifth, blast radius shifts. Aurora reduces several instance-local failure modes, but it increases dependence on Aurora-specific control-plane behavior, cluster endpoints, engine compatibility details, and cost mechanics. You are buying a stronger managed architecture, not a smaller mental model.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS documents RDS Multi-AZ DB instances as deployments with a primary DB instance and a synchronously replicated standby in a different Availability Zone. The documented pattern is traditional high availability through standby promotion. See AWS RDS Multi-AZ documentation: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.MultiAZ.html&quot;&gt;https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.MultiAZ.html&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Engineers using this pattern should treat failover as an application-visible event. Connection pools need short, bounded retries. Transaction retry logic must handle disconnects and ambiguous commits. Health checks should validate write capability, not merely TCP reachability.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The system can survive instance failure, but it still exposes a promotion event to clients. Applications that assume a database connection is permanent will fail noisily even when the database service is behaving correctly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Standard RDS Multi-AZ reduces infrastructure ownership, but it does not remove distributed-systems behavior from the application. The database is managed; client failure handling is still yours.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS describes Aurora storage as a cluster volume that spans multiple Availability Zones, with database instances connecting to that shared storage. Aurora Replicas use the same underlying cluster volume. See AWS Aurora storage documentation: &lt;a href=&quot;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Overview.StorageReliability.html&quot;&gt;https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.Overview.StorageReliability.html&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Engineers choosing Aurora should model the database as a cluster service. Use writer and reader endpoints intentionally. Keep write paths pinned to the writer endpoint. Route analytical or read-heavy traffic to readers only when the queries tolerate replica semantics and failover behavior.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Operationally, reader promotion and read scaling become cleaner than in many traditional replica topologies. But the application still needs endpoint-aware routing, connection draining, and retry logic during writer changes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Aurora improves the storage and replica architecture, but it does not excuse vague database access patterns. The teams that benefit most are the ones that already separate read, write, and recovery behavior clearly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; PostgreSQL and MySQL behavior still matters under both models. Long transactions hold resources. Missing indexes create table scans. Hot rows serialize writes. Poorly bounded connection pools can exhaust server capacity.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Treat Aurora as an availability and operations architecture, not as a query optimizer replacement. Keep slow-query review, index hygiene, vacuum behavior, lock analysis, and connection limits in the operating model.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Teams avoid the expensive failure mode where Aurora is adopted to solve problems caused by schema design, query shape, or application concurrency.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Aurora changes infrastructure failure boundaries. It does not repeal database fundamentals.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;





















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Decision Area&lt;/th&gt;&lt;th&gt;Standard RDS&lt;/th&gt;&lt;th&gt;Aurora&lt;/th&gt;&lt;th&gt;Operational Risk&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Cost model&lt;/td&gt;&lt;td&gt;Easier to reason about for smaller systems&lt;/td&gt;&lt;td&gt;Can become expensive through storage, IO, replicas, and cluster features&lt;/td&gt;&lt;td&gt;Aurora may surprise teams that only compare instance prices&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Engine behavior&lt;/td&gt;&lt;td&gt;Closest to familiar managed PostgreSQL or MySQL operations&lt;/td&gt;&lt;td&gt;Compatible, but not identical in every operational detail&lt;/td&gt;&lt;td&gt;Edge-case compatibility and extensions need testing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failover&lt;/td&gt;&lt;td&gt;Standby promotion in Multi-AZ&lt;/td&gt;&lt;td&gt;Reader promotion with shared storage architecture&lt;/td&gt;&lt;td&gt;Both require client reconnect and retry behavior&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Read scaling&lt;/td&gt;&lt;td&gt;Read replicas with traditional replication considerations&lt;/td&gt;&lt;td&gt;Aurora Replicas share cluster storage&lt;/td&gt;&lt;td&gt;Read scaling still does not fix write bottlenecks&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Storage operations&lt;/td&gt;&lt;td&gt;More explicit capacity planning&lt;/td&gt;&lt;td&gt;Auto-growing cluster volume&lt;/td&gt;&lt;td&gt;Easier growth can hide cost growth&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Portability&lt;/td&gt;&lt;td&gt;Simpler path to self-managed or other managed engines&lt;/td&gt;&lt;td&gt;More Aurora-specific assumptions&lt;/td&gt;&lt;td&gt;Architecture can become coupled to AWS behavior&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Simplicity&lt;/td&gt;&lt;td&gt;Better for predictable, moderate workloads&lt;/td&gt;&lt;td&gt;Better for high availability and read-heavy operational needs&lt;/td&gt;&lt;td&gt;Aurora can be overkill for small systems&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-this-post-does-not-cover&quot;&gt;What This Post Does Not Cover&lt;/h2&gt;
&lt;p&gt;This post covers the operational differences between Aurora and standard RDS MySQL/PostgreSQL. It does not cover: Aurora Serverless v2 scaling behavior, Aurora Global Database cross-region failover, Aurora I/O-Optimized pricing tier tradeoffs, RDS Proxy and its connection pooling implications, or Aurora vs. self-managed PostgreSQL on EC2. Those are distinct architectural decisions.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; If your main pain is host maintenance, backups, patching, and basic high availability, standard RDS may be enough. Do not buy a distributed storage architecture for a workload that mostly needs disciplined operations.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Choose Aurora when the operational value is clear: faster recovery posture, cleaner reader promotion, shared storage semantics, larger read scaling needs, or reduced storage capacity planning. Make that decision from failure scenarios, not dashboard marketing.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Run a failover test before production traffic depends on the database. Measure reconnect time, transaction retry behavior, writer endpoint recovery, replica read behavior, application error rates, and whether your alerting distinguishes database failure from client pool exhaustion.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Write the runbook around the boundary you chose. For RDS, document standby promotion behavior and storage planning. For Aurora, document cluster endpoints, reader routing, failover expectations, cost controls, and compatibility tests. The architecture decision is not complete until the on-call engineer knows what will happen when the writer disappears.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>system-design</category><category>cloud</category></item><item><title>DynamoDB Single-Table Design: When It Works and When It Hurts</title><link>https://rajivonai.com/blog/2022-07-25-dynamodb-single-table-design-when-it-works-and-when-it-hurts/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-07-25-dynamodb-single-table-design-when-it-works-and-when-it-hurts/</guid><description>Single-table design in DynamoDB is an operational bet that access patterns are stable enough to encode into partition and sort keys — when the approach pays off, and when evolving query requirements turn it into a migration project.</description><pubDate>Mon, 25 Jul 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Single-table design is not a clever schema trick; it is an operational bet that your access patterns are stable enough to encode into keys.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;DynamoDB rewards teams that know exactly how their application reads and writes data. It gives predictable latency at large scale, managed replication, automatic partitioning, streams, TTL, conditional writes, transactions, and global secondary indexes. In exchange, it asks a hard question early: what are the queries?&lt;/p&gt;
&lt;p&gt;That tradeoff is why single-table design exists. Instead of creating one table per entity, a team stores multiple entity types in one table and uses composite primary keys to place related items together. An order, its line items, payment events, fulfillment records, and audit entries may all share the same partition key and differ by sort key prefixes.&lt;/p&gt;
&lt;p&gt;The result can be excellent. A request that would require joins in a relational database can become one partition query. A service can fetch an aggregate view with one call, keep latency stable under load, and avoid distributed transactions across multiple tables.&lt;/p&gt;
&lt;p&gt;But the pattern gets oversold. Single-table design is not automatically more scalable than multi-table design. It is more scalable when the shape of the workload matches the shape of the keys.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure usually starts after launch, not during the first schema review.&lt;/p&gt;
&lt;p&gt;A team models the happy-path access pattern: get customer dashboard, list orders by account, fetch order detail, append events. The key design works. The service is fast. Costs are reasonable.&lt;/p&gt;
&lt;p&gt;Then product behavior changes. Support wants to find all failed payments by provider. Finance wants reconciliation by settlement date. Operations wants open orders by warehouse and priority. Analytics wants historical exports. A new feature needs to query relationships in the opposite direction from the original aggregate.&lt;/p&gt;
&lt;p&gt;The table still contains the data, but it no longer contains the access path.&lt;/p&gt;
&lt;p&gt;Now the team has bad options. Add a global secondary index and backfill it. Overload an existing index with another entity shape and hope the naming convention remains understandable. Duplicate data into another item type. Stream changes into OpenSearch, S3, or a relational store. Run scans for rare workflows and accept cost spikes. Or migrate the model while production traffic continues.&lt;/p&gt;
&lt;p&gt;The core question is: &lt;strong&gt;when is DynamoDB single-table design an architecture advantage, and when does it become accumulated coupling disguised as performance?&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;The answer is to treat single-table design as an access-pattern contract, not as a default modeling style.&lt;/p&gt;
&lt;p&gt;Use it when the service has bounded, high-volume operational queries. Avoid it when the service is still discovering its query surface, when ad hoc investigation is central to the workflow, or when many teams will independently add new entity relationships over time.&lt;/p&gt;
&lt;p&gt;A healthy single-table design starts with the request paths, not the nouns.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[product request — fetch account workspace] --&gt; B[access pattern inventory]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[partition key — account scope]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; D[sort key — entity and time ordering]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; E[primary query — account aggregate]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; F[index query — status queue]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; G[index query — user lookup]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; H[service response — bounded read]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; I[worker response — bounded queue]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  G --&gt; J[support response — bounded lookup]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The design is good when each important request maps to a bounded key condition. The design is weak when important requests require scans, client-side filtering over broad partitions, or fragile conventions that only one engineer understands.&lt;/p&gt;
&lt;p&gt;A practical test: write the production questions as code comments before writing the entity model.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Get account workspace by account id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;List open tasks by account id and status&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Fetch task detail by account id and task id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;List tasks assigned to user id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Append task event if version matches&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Expire invitation after ttl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Those statements tell you whether the table needs a primary key only, one global secondary index, a sparse index, duplicated lookup items, or a separate read model.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;h3 id=&quot;context&quot;&gt;Context&lt;/h3&gt;
&lt;p&gt;Amazon’s DynamoDB documentation and public talks describe single-table design as a pattern for known access patterns, especially workloads that need high scale and low-latency key-value or document access. The documented pattern is to model item collections around partition keys, use sort keys for hierarchy and ordering, and add secondary indexes for alternate access paths.&lt;/p&gt;
&lt;p&gt;This is not a relational modeling exercise. DynamoDB does not optimize arbitrary joins later. The schema is physical from the beginning: partition key choice affects distribution, sort key shape affects query behavior, and index definitions affect write amplification.&lt;/p&gt;
&lt;h3 id=&quot;action&quot;&gt;Action&lt;/h3&gt;
&lt;p&gt;The strong version of the pattern is deliberate denormalization.&lt;/p&gt;
&lt;p&gt;For an ecommerce workflow, an account partition might contain profile metadata, active carts, orders, order items, and order events. Sort keys encode stable query order:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;PK = ACCOUNT#123&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;SK = PROFILE#123&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;PK = ACCOUNT#123&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;SK = ORDER#2022-07-25#9001&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;PK = ACCOUNT#123&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;SK = ORDER#2022-07-25#9001#ITEM#1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;PK = ACCOUNT#123&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;SK = ORDER#2022-07-25#9001#EVENT#2022-07-25T10:30:00Z&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A sparse global secondary index might project only open fulfillment work:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;GSI1PK = FULFILLMENT#OPEN&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;GSI1SK = WAREHOUSE#DAL#PRIORITY#HIGH#ORDER#9001&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The application writes extra fields because the read path matters more than normalization. Conditional writes protect versioned updates. Transactions are reserved for small, critical multi-item changes. Streams can publish changes into downstream projections for search, analytics, or auditing.&lt;/p&gt;
&lt;h3 id=&quot;result&quot;&gt;Result&lt;/h3&gt;
&lt;p&gt;The result is operationally strong when the workload stays inside those paths.&lt;/p&gt;
&lt;p&gt;The account view is a partition query. The fulfillment queue is an index query. The order detail is a bounded range query. The service avoids joins at request time and keeps predictable latency because the database is doing exactly the work the keys describe.&lt;/p&gt;
&lt;p&gt;The result is operationally weak when the table becomes a dumping ground for every future question. Overloaded indexes become difficult to reason about because GSIs project different attributes for different entity types, forcing generic attribute names (&lt;code&gt;Data1&lt;/code&gt;, &lt;code&gt;Data2&lt;/code&gt;) and increasing storage costs. Backfills become risky because every item type has different attributes. Hot partitions appear when one tenant, status, or queue key receives disproportionate traffic. Cost shifts from read latency to write amplification and migration complexity.&lt;/p&gt;
&lt;h3 id=&quot;learning&quot;&gt;Learning&lt;/h3&gt;
&lt;p&gt;The documented pattern is not “put everything in one table.” The pattern is “put items that serve the same operational access patterns in one table.”&lt;/p&gt;
&lt;p&gt;That distinction matters. A single table can be a clean aggregate store. It can also become an undocumented protocol where every key prefix is a hidden API. The difference is whether the team maintains an access-pattern registry, capacity assumptions, ownership rules, and test coverage for key construction.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;













































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Why it hurts&lt;/th&gt;&lt;th&gt;Better response&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Unknown query surface&lt;/td&gt;&lt;td&gt;New product questions do not match existing keys&lt;/td&gt;&lt;td&gt;Start with multi-table or relational storage until access patterns stabilize&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Ad hoc investigation&lt;/td&gt;&lt;td&gt;Scans become normal operating procedure&lt;/td&gt;&lt;td&gt;Export to S3, index into OpenSearch, or use a relational read model&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Hot partitions&lt;/td&gt;&lt;td&gt;One tenant, queue, or status hits the 10GB or 1000 WCU partition limits&lt;/td&gt;&lt;td&gt;Add write sharding, redesign queue keys, or isolate the workload&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Index overloading without discipline&lt;/td&gt;&lt;td&gt;Key prefixes become tribal knowledge; GSI write amplification explodes&lt;/td&gt;&lt;td&gt;Maintain a key catalog and tests for every access pattern&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Excessive denormalization&lt;/td&gt;&lt;td&gt;Every write updates many item shapes&lt;/td&gt;&lt;td&gt;Separate read models by workflow and accept asynchronous projection&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cross-aggregate transactions&lt;/td&gt;&lt;td&gt;Business invariants span many partitions&lt;/td&gt;&lt;td&gt;Reconsider whether DynamoDB is the system of record for that workflow&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Multi-team ownership&lt;/td&gt;&lt;td&gt;Independent features mutate one physical table&lt;/td&gt;&lt;td&gt;Define table ownership or split bounded contexts&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The most dangerous failure is not a bad key name. It is a table whose operational contract is implicit.&lt;/p&gt;
&lt;p&gt;Once multiple services write different item types into the same table, the schema lives in application code, migration scripts, dashboards, and engineer memory. That can work for a disciplined platform team. It is painful for a fast-moving product surface without strong ownership.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; If your team cannot list the top access patterns, single-table design will force premature decisions into the physical schema.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Model requests first, then map each request to a primary key, sort key, index, or external projection.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Verify every critical workflow with bounded &lt;code&gt;Query&lt;/code&gt; operations, conditional write tests, backfill rehearsal, and partition hot-spot analysis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Use single-table design for stable operational aggregates; use separate tables or read models when query discovery, analytics, or independent team ownership matters more than one-call retrieval.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>databases</category><category>cloud</category></item><item><title>AWS Reference Architecture: ALB, ECS, RDS, ElastiCache, and SQS</title><link>https://rajivonai.com/blog/2022-07-10-aws-reference-architecture-alb-ecs-rds-elasticache-and-sqs/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-07-10-aws-reference-architecture-alb-ecs-rds-elasticache-and-sqs/</guid><description>The standard AWS web-tier stack works until the first dependency slows down, the cache goes cold, or a queue starts redriving poison messages — the failure modes hidden inside the ALB, ECS, RDS, ElastiCache, and SQS reference architecture.</description><pubDate>Sun, 10 Jul 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Most AWS reference architectures look clean until the first dependency slows down, the cache goes cold, or a queue starts redriving poison messages faster than the service can recover.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A common production web architecture on AWS starts with an Application Load Balancer, routes traffic to ECS services, stores transactional state in RDS, uses ElastiCache for low-latency reads or coordination, and pushes asynchronous work through SQS.&lt;/p&gt;
&lt;p&gt;On paper, this stack is straightforward. ALB terminates HTTP traffic and performs health checks. ECS runs stateless containers. RDS provides durable relational storage. ElastiCache absorbs read pressure and expensive computed lookups. SQS decouples slow work from request latency.&lt;/p&gt;
&lt;p&gt;The architecture becomes interesting when each managed service is treated less like a box on a diagram and more like an operational contract. ALB does not know whether a task is logically healthy, only whether its configured health check passes. ECS can replace containers, but replacement does not fix a bad deploy, an exhausted connection pool, or a database migration that locks hot tables. RDS is durable, but durability does not remove the need to manage connections, failover behavior, read amplification, and transaction scope. ElastiCache is fast, but it is not a source of truth. SQS gives buffering, but also at-least-once delivery, retries, and duplicate processing risk.&lt;/p&gt;
&lt;p&gt;The reference architecture is not the answer by itself. The answer is where failure boundaries are drawn.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The failure mode usually begins with a small latency shift.&lt;/p&gt;
&lt;p&gt;A downstream dependency slows. ECS tasks hold request threads longer. Connection pools fill. ALB continues sending traffic because the health endpoint still returns &lt;code&gt;200&lt;/code&gt;. Application retries multiply the load against RDS. Cache misses increase because requests are timing out before warming shared keys. SQS consumers fall behind, visibility timeouts expire, and the same messages are processed again.&lt;/p&gt;
&lt;p&gt;Nothing has fully failed, so every layer keeps trying.&lt;/p&gt;
&lt;p&gt;That is the dangerous state: partial failure with automated persistence. The system is alive enough to create more work and unhealthy enough to make that work more expensive.&lt;/p&gt;
&lt;p&gt;The core question is: how should ALB, ECS, RDS, ElastiCache, and SQS be arranged so that each layer limits blast radius instead of amplifying it?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;A practical AWS reference architecture separates synchronous request handling from asynchronous work, treats RDS as the source of truth, treats ElastiCache as disposable acceleration, and makes SQS consumers idempotent by default.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  U[users — browsers and clients] --&gt; A[ALB — public entry]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A --&gt; W[ECS web service — stateless requests]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  W --&gt; C[ElastiCache — hot reads and short lived coordination]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  W --&gt; D[RDS — transactional source of truth]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  W --&gt; Q[SQS — durable work buffer]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Q --&gt; P[ECS worker service — async processors]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  P --&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  P --&gt; C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; B[RDS backups — recovery point]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  W --&gt; M[CloudWatch — metrics and alarms]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  P --&gt; M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  Q --&gt; M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The ALB should protect the service from dead tasks, not certify the whole application. Health checks should be cheap and specific: process up, listener responsive, local dependencies initialized. Deep health checks that query RDS on every probe can turn a database incident into a load balancer incident.&lt;/p&gt;
&lt;p&gt;The ECS web service should stay stateless. Session state belongs outside the task, usually in cookies, RDS, or ElastiCache depending on durability requirements. Tasks should be replaceable without draining user identity, shopping carts, workflow state, or background progress.&lt;/p&gt;
&lt;p&gt;RDS should own facts. Orders, payments, permissions, inventory, audit records, and workflow transitions should not depend on cache survival. Use transactions where correctness requires atomicity. Keep transactions short. Avoid holding database locks across network calls.&lt;/p&gt;
&lt;p&gt;ElastiCache should reduce pressure, not define truth. Cache-aside is the default pattern: read from cache, fall back to RDS, then populate cache with a bounded TTL. When correctness matters, invalidate or version keys after writes rather than assuming TTLs will converge fast enough.&lt;/p&gt;
&lt;p&gt;SQS should absorb work that does not need to complete inside the user request. Email sends, webhook delivery, media processing, search indexing, ledger fanout, and third-party synchronization are better behind a queue than inside an ALB request path. The user request records intent in RDS, enqueues work, and returns.&lt;/p&gt;
&lt;p&gt;The worker service then processes messages with idempotency. A message can be delivered more than once. A worker can crash after performing a side effect but before deleting the message. The handler must be safe under replay.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; AWS documents ALB target health checks as a routing signal, not an application correctness proof. A target can be considered healthy when it responds successfully to the configured check path, even if a deeper dependency is degraded.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Keep ALB health checks shallow and use separate readiness, dependency, and business health metrics in CloudWatch. Route traffic based on whether the task can accept work; alert based on whether the system can complete work.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented pattern separates traffic eligibility from operational diagnosis. The load balancer removes dead targets, while alarms catch rising RDS latency, cache error rates, SQS age, and application-level failures.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; A health check is a routing primitive. It should not become a distributed transaction across every dependency.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Amazon’s Builders’ Library describes timeouts, retries, and backoff with jitter as essential tools for avoiding retry amplification during overload. The pattern is explicit: retries can help transient faults, but unbounded synchronized retries make incidents worse.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Put tight timeouts on calls from ECS to RDS, ElastiCache, and external APIs. Use bounded retries with exponential backoff and jitter. Do not retry every failed operation at every layer. For non-urgent work, prefer SQS retry behavior over holding an ALB request open.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented pattern turns retry behavior into load control. When a dependency slows, callers stop waiting indefinitely and avoid synchronized retry spikes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Retry policy is capacity policy. Treat it as part of the architecture, not as an SDK default.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Amazon SQS standard queues document at-least-once delivery. Messages can be delivered more than once, and consumers must tolerate duplicates. Visibility timeout controls when an in-flight message can be received again.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Design workers around idempotency keys stored in RDS. Record message handling state before or inside the same transaction as the durable side effect. Set visibility timeout longer than normal processing time, and send failed messages to a dead-letter queue after a bounded number of receives.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented pattern makes duplicate delivery survivable. Redrive becomes an operational tool rather than a correctness hazard.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; SQS decouples availability, not correctness. Correctness still belongs in the consumer and the database schema.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Redis and ElastiCache are commonly used for cache-aside reads, but Redis persistence and replication settings do not make cached values the system of record. AWS ElastiCache documentation emphasizes in-memory performance and managed cache operations.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Keep source-of-truth writes in RDS. Use ElastiCache for derived values, hot keys, rate counters, and short-lived coordination only when stale or lost data is acceptable. Add TTLs to all cache keys unless there is a specific invalidation mechanism.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The documented pattern allows cache nodes to fail, restart, or evict keys without losing durable business state.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Cache failure should hurt latency before it hurts correctness.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Component&lt;/th&gt;&lt;th&gt;Failure Mode&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;th&gt;Residual Risk&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;ALB&lt;/td&gt;&lt;td&gt;Health check passes while business flow fails&lt;/td&gt;&lt;td&gt;Separate shallow health checks from deep alarms&lt;/td&gt;&lt;td&gt;Bad deploys can still pass routing checks&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ECS&lt;/td&gt;&lt;td&gt;Tasks scale out but all block on RDS&lt;/td&gt;&lt;td&gt;Connection limits, timeouts, backpressure&lt;/td&gt;&lt;td&gt;Scaling compute cannot fix database contention&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;RDS&lt;/td&gt;&lt;td&gt;Locking, failover, or connection exhaustion&lt;/td&gt;&lt;td&gt;Short transactions, pool sizing, read replicas where appropriate&lt;/td&gt;&lt;td&gt;Failover can still create brief write unavailability&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ElastiCache&lt;/td&gt;&lt;td&gt;Hot key, eviction, stale value&lt;/td&gt;&lt;td&gt;TTLs, key versioning, cache-aside fallback&lt;/td&gt;&lt;td&gt;Cache loss can expose database capacity limits&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;SQS&lt;/td&gt;&lt;td&gt;Duplicate or poison messages&lt;/td&gt;&lt;td&gt;Idempotency keys, DLQs, visibility timeout tuning&lt;/td&gt;&lt;td&gt;Reprocessing still requires operational judgment&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Workers&lt;/td&gt;&lt;td&gt;Side effect succeeds before message delete&lt;/td&gt;&lt;td&gt;Durable processing records&lt;/td&gt;&lt;td&gt;External APIs may not support idempotency&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The most common mistake is treating this architecture as independently scalable boxes. ECS scales horizontally, but RDS has shared limits. ElastiCache lowers read load, but cold-start traffic can still hit the database. SQS buffers work, but a growing queue is deferred user pain, not free capacity.&lt;/p&gt;
&lt;p&gt;The second mistake is placing too much logic in the synchronous request. If the user does not need the result immediately, persist intent and enqueue work. This shortens request latency, reduces ALB exposure to downstream slowness, and creates a controlled retry surface.&lt;/p&gt;
&lt;p&gt;The third mistake is ignoring deletion semantics. A worker that completes work but fails to delete the SQS message has created a duplicate. A worker that deletes first and then performs work has created possible data loss. The only robust answer is idempotent processing with durable state.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The stack fails badly when partial dependency slowness causes every layer to retry, wait, and amplify load.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Use ALB for traffic routing, ECS for stateless execution, RDS for durable truth, ElastiCache for disposable acceleration, and SQS for asynchronous buffering.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; The architecture follows documented AWS patterns: ALB target health checks, SQS at-least-once delivery, cache-aside behavior, bounded retries, visibility timeouts, dead-letter queues, and durable relational transactions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Review one production request path and mark every synchronous dependency, retry, timeout, cache read, database transaction, and queued side effect. Then decide which failures should return fast, which should retry later, and which must stop the workflow entirely.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>architecture</category><category>cloud</category><category>failures</category></item><item><title>Terraform Module Design Checklist for Database Infrastructure</title><link>https://rajivonai.com/blog/2022-06-14-terraform-module-design-checklist-for-database-infrastructure/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-06-14-terraform-module-design-checklist-for-database-infrastructure/</guid><description>Database Terraform modules fail when they hide operational decisions behind convenient defaults — a checklist covering parameter groups, backup policies, encryption, and the boundaries that must never be automated away.</description><pubDate>Tue, 14 Jun 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Database Terraform modules fail when they hide operational decisions behind convenient defaults.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Infrastructure teams often start with Terraform modules as a reuse mechanism. One team writes an RDS module, another wraps it for PostgreSQL, and soon every service can request a database by setting &lt;code&gt;engine&lt;/code&gt;, &lt;code&gt;instance_class&lt;/code&gt;, &lt;code&gt;storage_gb&lt;/code&gt;, and &lt;code&gt;environment&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;That works until the database becomes operationally important.&lt;/p&gt;
&lt;p&gt;Database infrastructure is not just compute with a persistent disk attached. It has lifecycle constraints: backups, replication, maintenance windows, parameter groups, secrets, encryption, restore paths, connection limits, version upgrades, and deletion protection. A weak module can create databases quickly, but it cannot help a platform team answer the harder question: what should be standardized, what should remain explicit, and what must be impossible to misconfigure?&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Most Terraform modules drift toward one of two bad shapes.&lt;/p&gt;
&lt;p&gt;The first is the thin wrapper. It exposes nearly every provider argument, so every application team makes its own database architecture decisions through variables. The module creates little leverage beyond naming conventions.&lt;/p&gt;
&lt;p&gt;The second is the sealed box. It hides too much behind defaults. Teams can provision fast, but they cannot reason about failover, backup retention, version pinning, or upgrade behavior. When an outage happens, the module becomes an obstacle because the real architecture is buried in implementation details.&lt;/p&gt;
&lt;p&gt;Database modules need a different bar. They must encode platform policy without pretending that all databases are the same. They must support safe day-two operations, not just day-one creation. They must make risky operations visible in code review.&lt;/p&gt;
&lt;p&gt;So the design question is: how do you build a Terraform database module that is reusable, safe, and still honest about the operational contract it creates?&lt;/p&gt;
&lt;h2 id=&quot;design-the-module-around-the-operational-contract&quot;&gt;Design the Module Around the Operational Contract&lt;/h2&gt;
&lt;p&gt;A strong database module starts with the contract, not the resource list.&lt;/p&gt;
&lt;p&gt;The module should make policy decisions explicit: supported engines, approved versions, backup defaults, encryption requirements, deletion protection, network placement, monitoring, and maintenance windows. It should also make application-owned decisions explicit: database size, workload class, read replica need, and environment-specific capacity.&lt;/p&gt;
&lt;p&gt;The goal is not to remove choice. The goal is to put each choice at the correct boundary.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  A[service request — database intent] --&gt; B[module interface — approved inputs]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; C[policy layer — encryption backup retention deletion guard]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  B --&gt; D[capacity layer — size class replicas]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  C --&gt; E[database resources — instance subnet secrets]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  D --&gt; E&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  E --&gt; F[outputs — endpoint credentials observability hooks]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  F --&gt; G[runbook — restore upgrade failover]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Use this checklist as the design review before a database module becomes a platform primitive.&lt;/p&gt;




























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Area&lt;/th&gt;&lt;th&gt;Checklist question&lt;/th&gt;&lt;th&gt;Failure mode if ignored&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Interface&lt;/td&gt;&lt;td&gt;Are inputs based on user intent rather than provider arguments?&lt;/td&gt;&lt;td&gt;Teams inherit provider complexity and encode inconsistent architecture.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Defaults&lt;/td&gt;&lt;td&gt;Are defaults safe for production, or clearly marked as non-production?&lt;/td&gt;&lt;td&gt;A dev-friendly default becomes a production outage pattern.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Versioning&lt;/td&gt;&lt;td&gt;Are engine versions pinned and upgrade paths documented?&lt;/td&gt;&lt;td&gt;Minor upgrades surprise workloads or block future provider changes.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Backups&lt;/td&gt;&lt;td&gt;Is retention required, environment-aware, and tested through restore?&lt;/td&gt;&lt;td&gt;Backups exist on paper but cannot support recovery.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Deletion&lt;/td&gt;&lt;td&gt;Is deletion protection enabled by default for persistent environments?&lt;/td&gt;&lt;td&gt;A routine Terraform change destroys stateful infrastructure.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Networking&lt;/td&gt;&lt;td&gt;Does the module control subnet class, security groups, and exposure?&lt;/td&gt;&lt;td&gt;Databases become reachable from unintended networks.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Secrets&lt;/td&gt;&lt;td&gt;Are credentials generated, rotated, and exported through a secret manager?&lt;/td&gt;&lt;td&gt;Passwords leak through Terraform state or ad hoc outputs.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Observability&lt;/td&gt;&lt;td&gt;Are logs, metrics, and alarms part of the module contract?&lt;/td&gt;&lt;td&gt;The database is provisioned before anyone can operate it.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Extensibility&lt;/td&gt;&lt;td&gt;Are escape hatches narrow and reviewed?&lt;/td&gt;&lt;td&gt;The module becomes either unusable or ungoverned.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Testing&lt;/td&gt;&lt;td&gt;Are plan checks and destructive-change tests part of CI?&lt;/td&gt;&lt;td&gt;Reviewers approve diffs without seeing operational risk.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The strongest interface is usually small but not simplistic. For example, &lt;code&gt;workload_tier = &quot;critical&quot;&lt;/code&gt; is better than asking every service team to separately configure multi-zone placement, backup retention, deletion protection, and alarms. But &lt;code&gt;storage_gb&lt;/code&gt; and &lt;code&gt;max_connections&lt;/code&gt; may still need to remain visible because workload shape varies by service.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; HashiCorp’s public module guidance emphasizes composition, clear input variables, and stable outputs rather than copying large resource graphs into every service. The documented pattern is that modules should expose a deliberate interface and hide implementation details only where the abstraction remains stable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Apply that pattern to database infrastructure by splitting the module into three layers: intent inputs, platform policy, and provider resources. The intent layer describes what the service needs. The policy layer maps environment and workload tier to guardrails. The resource layer creates the database, networking, secret references, monitoring, and outputs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; Code review shifts from “what does this provider argument do?” to “is this workload allowed to run with this contract?” That is a better review surface for platform engineering because it focuses attention on recoverability, exposure, and lifecycle behavior.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; A database module should not be a mirror of &lt;code&gt;aws_db_instance&lt;/code&gt;, &lt;code&gt;google_sql_database_instance&lt;/code&gt;, or another provider resource. It should be a product interface for a stateful capability.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; Amazon RDS documents features such as Multi-AZ deployments, automated backups, deletion protection, maintenance windows, and parameter groups as separate operational controls. Those controls exist because database safety is multi-dimensional; availability, recovery, configuration, and lifecycle protection are not the same setting.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Treat these controls as policy bundles rather than optional one-off variables. For example, a production tier can require deletion protection, encrypted storage, backup retention above a minimum, enhanced monitoring, and a defined maintenance window. A development tier can relax some cost-heavy settings while still keeping encryption and secret handling non-negotiable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The module makes environment differences explicit without making every caller rebuild the policy matrix. The Terraform plan becomes easier to inspect because the dangerous differences stand out.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Good modules encode the platform’s minimum viable standard. They do not force every team to rediscover the same reliability controls.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context:&lt;/strong&gt; PostgreSQL behavior makes some database changes operationally sensitive even when Terraform can express them cleanly. Changes to parameters, connection limits, storage layout, extensions, and major versions may require restarts, careful sequencing, or application compatibility checks.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; Model operationally sensitive changes as explicit inputs with review friction. Use variable validation, documented upgrade paths, CI plan checks, and module versioning. Do not let a provider diff silently turn a routine merge into a database restart or replacement.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt; The module supports day-two operations because it treats lifecycle changes as events, not just configuration drift.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Learning:&lt;/strong&gt; Terraform can describe the desired state, but the module has to describe the operational risk.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Tradeoff&lt;/th&gt;&lt;th&gt;Why it breaks&lt;/th&gt;&lt;th&gt;Mitigation&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Too many presets&lt;/td&gt;&lt;td&gt;Workloads eventually need capabilities outside the matrix.&lt;/td&gt;&lt;td&gt;Keep presets small and allow reviewed extensions for known gaps.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Too many variables&lt;/td&gt;&lt;td&gt;The module stops enforcing platform policy.&lt;/td&gt;&lt;td&gt;Group decisions by intent and hide raw provider knobs by default.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cloud-specific resources&lt;/td&gt;&lt;td&gt;A portable interface can erase important provider behavior.&lt;/td&gt;&lt;td&gt;Prefer explicit provider modules over fake multi-cloud symmetry.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;State coupling&lt;/td&gt;&lt;td&gt;Database resources are costly to rename, replace, or move.&lt;/td&gt;&lt;td&gt;Use stable names, import plans, and migration runbooks before refactors.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Secret outputs&lt;/td&gt;&lt;td&gt;Terraform state may contain sensitive material.&lt;/td&gt;&lt;td&gt;Output secret references, not plaintext values.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Untested restores&lt;/td&gt;&lt;td&gt;Backup settings create confidence without proof.&lt;/td&gt;&lt;td&gt;Add restore drills to the operational checklist outside Terraform.&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Your current module may create databases faster than your team can safely operate them.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution:&lt;/strong&gt; Redesign the interface around workload intent, environment policy, lifecycle safety, and explicit operational risk.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof:&lt;/strong&gt; Compare every variable against a real failure mode: accidental deletion, exposed network path, missing restore, unsafe upgrade, leaked secret, or invisible saturation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action:&lt;/strong&gt; Before publishing the module, run a destructive-change review, document restore and upgrade paths, and require &lt;code&gt;npm run check&lt;/code&gt;-style CI gates for Terraform plan validation in the infrastructure repository.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>automation</category><category>platform</category><category>ci-cd</category></item><item><title>MySQL EXPLAIN: Reading the Plan Without Guessing</title><link>https://rajivonai.com/blog/2022-06-06-mysql-explain-reading-the-plan/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-06-06-mysql-explain-reading-the-plan/</guid><description>How to read MySQL EXPLAIN output systematically — type column, key column, rows estimate, and Extra flags — so you stop adding indexes blindly.</description><pubDate>Mon, 06 Jun 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The most common mistake engineers make with &lt;code&gt;EXPLAIN&lt;/code&gt; is treating &lt;code&gt;type: ALL&lt;/code&gt; as an alarm that requires an index. It is a data point, not a verdict.&lt;/strong&gt; Whether a full scan is a problem depends on the rows estimate, the Extra flags, and what the optimizer decided to do with the indexes that already exist. Reading the plan systematically takes two minutes.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Every engineer who has investigated a slow query has seen &lt;code&gt;EXPLAIN&lt;/code&gt; output. Most can recognize the column names — &lt;code&gt;type&lt;/code&gt;, &lt;code&gt;key&lt;/code&gt;, &lt;code&gt;rows&lt;/code&gt;, &lt;code&gt;Extra&lt;/code&gt; — but not how to read them as a system.&lt;/p&gt;
&lt;p&gt;The common workflow is: see &lt;code&gt;type: ALL&lt;/code&gt;, add an index. That misses the reason the optimizer chose the plan it chose, and misses the cases where the new index will be ignored anyway. MySQL 8.0 added &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt;, which executes the query and returns actual row counts alongside estimates. The gap between those two numbers is often the real story.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Indexes do not guarantee the optimizer will use them. InnoDB’s cost-based optimizer weighs index access cost against cardinality estimates. If those estimates suggest the index returns a large fraction of the table, the optimizer may choose a full scan instead. This behavior is documented: MySQL uses index dive estimates and statistics from &lt;code&gt;INFORMATION_SCHEMA.INNODB_TABLE_STATS&lt;/code&gt; to make that call.&lt;/p&gt;
&lt;p&gt;When statistics are stale — after bulk loads, large deletes, or fast-growing tables — the optimizer’s row estimates can be wrong by an order of magnitude. A plan that looks safe in &lt;code&gt;EXPLAIN&lt;/code&gt; may be running against a table ten times larger.&lt;/p&gt;
&lt;p&gt;What does each column actually mean, and how do you read them together to know whether the optimizer’s choice was reasonable?&lt;/p&gt;
&lt;h2 id=&quot;how-to-read-explain-output&quot;&gt;How to Read EXPLAIN Output&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;EXPLAIN&lt;/code&gt; returns one row per table in the query, in the join order the optimizer chose. The columns that carry diagnostic weight are &lt;code&gt;type&lt;/code&gt;, &lt;code&gt;key&lt;/code&gt;, &lt;code&gt;rows&lt;/code&gt;, and &lt;code&gt;Extra&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The &lt;code&gt;type&lt;/code&gt; column&lt;/strong&gt; describes the access method. From best to worst: &lt;code&gt;const&lt;/code&gt; (single-row primary key match), &lt;code&gt;eq_ref&lt;/code&gt; (one matching row per join from a unique index), &lt;code&gt;ref&lt;/code&gt; (non-unique index lookup), &lt;code&gt;range&lt;/code&gt; (bounded index scan), &lt;code&gt;index&lt;/code&gt; (full index scan), &lt;code&gt;ALL&lt;/code&gt; (full table scan). The useful breakpoint is between &lt;code&gt;range&lt;/code&gt; and &lt;code&gt;index&lt;/code&gt; — anything at &lt;code&gt;index&lt;/code&gt; or &lt;code&gt;ALL&lt;/code&gt; with a high &lt;code&gt;rows&lt;/code&gt; estimate is worth investigating.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The &lt;code&gt;key&lt;/code&gt; column&lt;/strong&gt; shows which index the optimizer actually chose. If &lt;code&gt;key&lt;/code&gt; is &lt;code&gt;NULL&lt;/code&gt; and &lt;code&gt;possible_keys&lt;/code&gt; lists candidates, the optimizer decided the available indexes were not selective enough to be worth using. That is the cardinality problem — not a missing index.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The &lt;code&gt;rows&lt;/code&gt; column&lt;/strong&gt; is the optimizer’s estimate of how many rows it will examine to satisfy the query. For &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; (MySQL 8.0+), the output also shows &lt;code&gt;actual rows&lt;/code&gt; — the count from the real execution. A large gap between estimated and actual rows means statistics are stale. Run &lt;code&gt;ANALYZE TABLE tablename;&lt;/code&gt; to refresh them.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The &lt;code&gt;Extra&lt;/code&gt; column&lt;/strong&gt; carries execution flags. &lt;code&gt;Using filesort&lt;/code&gt; means MySQL sorted the result after retrieval — no index covers the &lt;code&gt;ORDER BY&lt;/code&gt;, and on large result sets this spills to disk. &lt;code&gt;Using temporary&lt;/code&gt; means an internal temp table was created, common with &lt;code&gt;GROUP BY&lt;/code&gt; on non-indexed columns. &lt;code&gt;Using index&lt;/code&gt; is a positive signal — a covering index served the query without touching table rows.&lt;/p&gt;
&lt;p&gt;Reading these together: &lt;code&gt;type: ALL&lt;/code&gt;, &lt;code&gt;rows: 4000000&lt;/code&gt;, &lt;code&gt;Extra: Using temporary; Using filesort&lt;/code&gt; means the optimizer scanned four million rows, built a temp table, and sorted it. That is not a statistics problem — that is a schema problem.&lt;/p&gt;
&lt;p&gt;A concrete example with &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; on MySQL 8.0:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPLAIN ANALYZE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; user_id, created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;pending&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;2022-01-01&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;\G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;-&gt; Filter: ((orders.status = &apos;pending&apos;) and (orders.created_at &gt; &apos;2022-01-01&apos;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   (cost=48213.45 rows=45823)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   (actual time=0.112..842.361 rows=12847 loops=1)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   -&gt; Table scan on orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      (cost=48213.45 rows=458230)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      (actual time=0.089..721.903 rows=458230 loops=1)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;rows&lt;/code&gt; estimate (458,230 for the table scan) matches actual rows — statistics are current. But &lt;code&gt;actual time=842ms&lt;/code&gt; for a filter that returns 12,847 rows confirms the full scan is the problem: no index covers &lt;code&gt;(status, created_at)&lt;/code&gt;. Adding &lt;code&gt;idx_status_created (status, created_at)&lt;/code&gt; would reduce the scan to an index range lookup.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The MySQL 8.0 Reference Manual documents that InnoDB’s optimizer uses cardinality statistics from &lt;code&gt;INFORMATION_SCHEMA.INNODB_TABLE_STATS&lt;/code&gt; to choose between an index range scan and a full table scan. &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt;, introduced in MySQL 8.0.18, returns both estimated and actual row counts per step. The manual identifies a large gap between the two as the primary signal for stale statistics — estimated 500, actual 2,400,000 means the plan was optimized for a table that no longer exists.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Stale statistics after bulk load&lt;/td&gt;&lt;td&gt;&lt;code&gt;rows&lt;/code&gt; estimate is far below actual; optimizer picks a plan sized for the old table&lt;/td&gt;&lt;td&gt;&lt;code&gt;innodb_stats_auto_recalc&lt;/code&gt; threshold (10% of rows changed) was not met; run &lt;code&gt;ANALYZE TABLE&lt;/code&gt; manually&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;JOIN order surprises&lt;/td&gt;&lt;td&gt;&lt;code&gt;type: ALL&lt;/code&gt; appears on a table you expected to be driven by an index&lt;/td&gt;&lt;td&gt;InnoDB’s cost model may reorder joins; the &lt;code&gt;id&lt;/code&gt; column in &lt;code&gt;EXPLAIN&lt;/code&gt; output shows actual join order&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Index ignored due to low cardinality&lt;/td&gt;&lt;td&gt;&lt;code&gt;possible_keys&lt;/code&gt; lists the index; &lt;code&gt;key&lt;/code&gt; is NULL&lt;/td&gt;&lt;td&gt;Column has few distinct values (boolean, status enum); optimizer’s index dive concluded the full scan was cheaper&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: Engineers add indexes without confirming the optimizer will use them, because they read &lt;code&gt;type: ALL&lt;/code&gt; without reading &lt;code&gt;key&lt;/code&gt;, &lt;code&gt;rows&lt;/code&gt;, and &lt;code&gt;Extra&lt;/code&gt; together.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Treat EXPLAIN output as a system — check &lt;code&gt;key&lt;/code&gt; first, then &lt;code&gt;rows&lt;/code&gt;, then &lt;code&gt;Extra&lt;/code&gt;, before drawing any conclusion about what is wrong.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Run &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; on MySQL 8.0+. If actual rows diverges significantly from estimated rows, the plan is stale — run &lt;code&gt;ANALYZE TABLE&lt;/code&gt; and re-check before adding any index.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, take one slow query your team has been discussing and run &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; on it. Read &lt;code&gt;type&lt;/code&gt;, &lt;code&gt;key&lt;/code&gt;, &lt;code&gt;rows&lt;/code&gt;, &lt;code&gt;Extra&lt;/code&gt; in order. Write one sentence describing what the optimizer decided. That sentence is more useful than a blind &lt;code&gt;CREATE INDEX&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>checklist</category></item><item><title>MySQL Slow Query Playbook: From Slow Log to Fix</title><link>https://rajivonai.com/blog/2022-05-23-mysql-slow-query-playbook-from-slow-log-to-fix/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-05-23-mysql-slow-query-playbook-from-slow-log-to-fix/</guid><description>A repeatable workflow for diagnosing MySQL slow queries — from enabling the slow log through reading EXPLAIN output to committing a safe fix.</description><pubDate>Mon, 23 May 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Most MySQL slowdowns have a short list of root causes: a missing index, a lock wait, or stale optimizer statistics. The hard part is not the fix — it is getting from “p99 alert fired” to “I know which query, why it is slow, and what the safe remediation is” without wasting an hour looking at the wrong thing.&lt;/strong&gt; This playbook gives you that path as a repeatable workflow. Run these checks in order, and you will have a diagnosis before you start guessing.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;The alert fires. Maybe it is a CloudWatch &lt;code&gt;SlowQueries&lt;/code&gt; metric spike on RDS, a p99 latency alarm from your application APM, or a PagerDuty page from a long-running query threshold. You open a terminal, connect to the database, and face the standard problem: MySQL is running dozens of queries per second, and you need to identify the one that is costing you.&lt;/p&gt;
&lt;p&gt;MySQL gives you several places to look — the slow query log, Performance Schema digest tables, &lt;code&gt;SHOW PROCESSLIST&lt;/code&gt;, and InnoDB status — and the right place to start depends on whether the problem is active right now or a pattern you are trying to reconstruct after the fact. This runbook covers both: active incidents where queries are blocking or running hot, and post-incident analysis where you need to find the pattern in aggregated data.&lt;/p&gt;
&lt;p&gt;The version context matters. MySQL 8.0 added &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt;, which gives actual row counts alongside estimated ones. If you are on MySQL 5.7 or RDS Aurora MySQL, the same diagnostic steps apply but you will use &lt;code&gt;EXPLAIN FORMAT=JSON&lt;/code&gt; without &lt;code&gt;ANALYZE&lt;/code&gt; for the execution plan.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Signal&lt;/th&gt;&lt;th&gt;Where to see it&lt;/th&gt;&lt;th&gt;What it means&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Query_time&lt;/code&gt; &gt;&gt; &lt;code&gt;Lock_time&lt;/code&gt; in slow log entry&lt;/td&gt;&lt;td&gt;&lt;code&gt;slow_query_log_file&lt;/code&gt; or &lt;code&gt;mysqldumpslow&lt;/code&gt; output&lt;/td&gt;&lt;td&gt;Query is executing slowly independent of locking — likely index or scan issue&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High &lt;code&gt;Lock_time&lt;/code&gt; in slow log&lt;/td&gt;&lt;td&gt;Same source&lt;/td&gt;&lt;td&gt;Transaction waiting on a row lock before it can execute&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;rows_examined&lt;/code&gt; far exceeds &lt;code&gt;rows_sent&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Slow log entry or &lt;code&gt;events_statements_summary_by_digest&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Full or partial table scan — index not covering the WHERE clause&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Thread in &lt;code&gt;Waiting for table metadata lock&lt;/code&gt; state&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW PROCESSLIST&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Another connection holds a metadata lock, usually from an open transaction or an ALTER TABLE&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High &lt;code&gt;SUM_TIMER_WAIT&lt;/code&gt; for a specific digest&lt;/td&gt;&lt;td&gt;&lt;code&gt;performance_schema.events_statements_summary_by_digest&lt;/code&gt;&lt;/td&gt;&lt;td&gt;A specific query pattern accounts for most DB wall-clock time&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;LATEST DETECTED DEADLOCK&lt;/code&gt; section present&lt;/td&gt;&lt;td&gt;&lt;code&gt;SHOW ENGINE INNODB STATUS&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Two transactions deadlocked; one was rolled back&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Enable the slow query log and read it&lt;/strong&gt; — If the slow log is not already running, turn it on without a restart:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; GLOBAL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; slow_query_log &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; GLOBAL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; long_query_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; GLOBAL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; log_output &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;FILE&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW VARIABLES &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIKE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;slow_query_log_file&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then use &lt;code&gt;mysqldumpslow&lt;/code&gt; to aggregate entries. The &lt;code&gt;-s t&lt;/code&gt; flag sorts by total time, which surfaces the queries with the most cumulative cost rather than just the single longest run:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;mysqldumpslow&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -s&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; -t&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/lib/mysql/hostname-slow.log&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each entry shows &lt;code&gt;Query_time&lt;/code&gt;, &lt;code&gt;Lock_time&lt;/code&gt;, &lt;code&gt;Rows_sent&lt;/code&gt;, and &lt;code&gt;Rows_examined&lt;/code&gt;. A &lt;code&gt;rows_examined / rows_sent&lt;/code&gt; ratio above 100 is a strong signal of a full or near-full table scan.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Find top queries by total time in Performance Schema&lt;/strong&gt; — For RDS or environments where you cannot read the log file directly, Performance Schema digest tables give the same aggregate view:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  DIGEST_TEXT,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  COUNT_STAR,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  SUM_TIMER_WAIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000000000000&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; total_sec,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  AVG_TIMER_WAIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000000000000&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; avg_sec,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  SUM_ROWS_EXAMINED,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  SUM_ROWS_SENT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; performance_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;events_statements_summary_by_digest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SUM_TIMER_WAIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;DIGEST_TEXT&lt;/code&gt; column normalizes literals to &lt;code&gt;?&lt;/code&gt; placeholders, so you see the query pattern regardless of parameter values. Focus on rows where &lt;code&gt;SUM_ROWS_EXAMINED&lt;/code&gt; greatly exceeds &lt;code&gt;SUM_ROWS_SENT&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Check current lock waits&lt;/strong&gt; — If the incident is active and threads are blocked, identify the blocking transaction immediately. On MySQL 8.0, use &lt;code&gt;performance_schema.data_lock_waits&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  r&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; waiting_trx_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  r&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_mysql_thread_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; waiting_thread,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  r&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_query&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; waiting_query,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  b&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_trx_id,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  b&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_mysql_thread_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_thread,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  b&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_query&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_query&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;innodb_lock_waits&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; w&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INNER JOIN&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;innodb_trx&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; w&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;blocking_trx_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INNER JOIN&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;innodb_trx&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; r&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; r&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;trx_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; w&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;requesting_trx_id&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;blocking_query&lt;/code&gt; column often shows &lt;code&gt;NULL&lt;/code&gt; — this means the blocking transaction has already executed its statement and is sitting idle with an open transaction, holding row locks. Check &lt;code&gt;b.trx_started&lt;/code&gt; to see how long it has been open.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Check index usage for the affected table&lt;/strong&gt; — The &lt;code&gt;sys&lt;/code&gt; schema surfaces unused indexes, which are candidates for removal, and lets you quickly see what indexes exist:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Indexes that have never been used since last server restart&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; sys&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;schema_unused_indexes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; object_schema &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;your_db&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- All indexes on the table with cardinality&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INDEX&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; your_table;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Low &lt;code&gt;Cardinality&lt;/code&gt; on a column you are filtering by is a sign the index may not help the optimizer — or that statistics are stale and need updating. A &lt;code&gt;Cardinality&lt;/code&gt; of 1 on a column with millions of rows is usually wrong.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Get EXPLAIN for the slow query&lt;/strong&gt; — Once you have identified the query pattern, capture its execution plan. On MySQL 8.0, &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; runs the query and returns actual row counts alongside estimates:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- MySQL 8.0+ — runs the query and returns actual vs estimated rows&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPLAIN ANALYZE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; user_id, created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;pending&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;2022-01-01&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- All versions — returns JSON with full cost estimates&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPLAIN FORMAT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=JSON&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; user_id, created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;pending&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;2022-01-01&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the output, look for &lt;code&gt;type: ALL&lt;/code&gt; (full table scan), &lt;code&gt;type: index&lt;/code&gt; (full index scan), &lt;code&gt;Extra: Using filesort&lt;/code&gt;, and &lt;code&gt;Extra: Using temporary&lt;/code&gt;. Any of these signals a query that is doing more work than it needs to. The &lt;code&gt;rows&lt;/code&gt; column shows the optimizer’s estimate; with &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt;, the &lt;code&gt;actual rows&lt;/code&gt; field shows what actually happened.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Slow query alert fires] --&gt; B{rows_examined far exceeds rows_sent?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| C[Check EXPLAIN for full scan or wrong index]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    C --&gt; D{type=ALL or index in EXPLAIN?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes| E[Add or modify index based on WHERE clause]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|no| F[Check for filesort or temporary table in Extra]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| G{lock_time high in slow log?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|yes| H[Query innodb_lock_waits for blocking thread]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    H --&gt; I[Kill blocking thread or wait for commit]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|no| J{Query recently regressed?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|yes| K{Cardinality looks wrong in SHOW INDEX?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|yes| L[Run ANALYZE TABLE to refresh statistics]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|no| M[Check for schema change or data distribution shift]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    J --&gt;|no| N{I/O bound — buffer pool hit rate low?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    N --&gt;|yes| O[Check innodb_buffer_pool hit rate and increase if possible]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    N --&gt;|no| P[Profile with Performance Schema events_stages_summary]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What this diagram shows:&lt;/strong&gt; A MySQL slow query decision tree — starting with the rows_examined/rows_sent ratio to detect full scans, then lock_time for blocking threads, cardinality estimates for stale statistics, and buffer pool hit rate for I/O saturation — each branch leads to a specific actionable fix.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Option 1 — Add or modify an index based on EXPLAIN output&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When &lt;code&gt;EXPLAIN&lt;/code&gt; shows &lt;code&gt;type: ALL&lt;/code&gt; or the optimizer is choosing an index that does not cover the WHERE clause, the fix is usually a covering index that includes all columns referenced in the WHERE, ORDER BY, and SELECT list. In MySQL 8.0, &lt;code&gt;ALTER TABLE ... ADD INDEX&lt;/code&gt; uses online DDL by default, which means reads and writes continue during the operation:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Add a covering index for the query above&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  ADD&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx_status_created_user (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, created_at, user_id);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Verify the optimizer uses it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPLAIN &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; user_id, created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;pending&apos;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; created_at &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;2022-01-01&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Column order in the index matters. MySQL’s B-tree indexes support leftmost prefix matching — the optimizer can use &lt;code&gt;(status, created_at)&lt;/code&gt; for a filter on &lt;code&gt;status&lt;/code&gt; alone, but it cannot use &lt;code&gt;(created_at, status)&lt;/code&gt; for a filter on &lt;code&gt;status&lt;/code&gt; alone. Put the equality predicates first, range predicates last.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 2 — Update statistics with ANALYZE TABLE&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When the optimizer is choosing a bad plan despite a suitable index, the cause is often stale statistics. This happens after large data loads, bulk deletes, or tables that have grown significantly since the last statistics update. &lt;code&gt;ANALYZE TABLE&lt;/code&gt; is non-blocking in InnoDB and safe to run in production:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ANALYZE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Verify cardinality updated&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INDEX&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;According to the MySQL 8.0 Reference Manual, InnoDB calculates index statistics by sampling random pages — &lt;code&gt;innodb_stats_sample_pages&lt;/code&gt; controls sample size. If your table has extremely skewed data distribution, increasing this value can improve plan quality at the cost of more I/O during the statistics update.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 3 — Kill the blocking transaction&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When lock waits are causing the slowdown, the fastest resolution is to identify and kill the blocking thread. Use the blocking thread ID from the lock wait query in Check 3:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Show full information about the blocking thread&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;processlist&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;blocking_thread_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Kill it (this rolls back the blocking transaction)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;KILL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;blocking_thread_id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;KILL&lt;/code&gt; in MySQL sends a signal to the thread to terminate cleanly. The thread’s current transaction is rolled back. This is the correct tool for a long-running idle transaction holding row locks — not a hard connection reset. After killing, verify the waiting queries resume with &lt;code&gt;SHOW PROCESSLIST&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Adding an index&lt;/strong&gt; — Reversible at any time with &lt;code&gt;DROP INDEX&lt;/code&gt;. The online DDL used in MySQL 8.0 InnoDB means the add is also reversible mid-execution by canceling the ALTER (though partial progress is lost and the operation must restart). To remove: &lt;code&gt;ALTER TABLE orders DROP INDEX idx_status_created_user;&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;ANALYZE TABLE&lt;/strong&gt; — No rollback needed. &lt;code&gt;ANALYZE TABLE&lt;/code&gt; updates statistics but does not change data. If the new statistics produce a worse plan, you can hint the optimizer with &lt;code&gt;USE INDEX (index_name)&lt;/code&gt; as a temporary workaround while investigating the plan regression. Statistics will also auto-update over time as InnoDB detects data changes.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;KILL thread&lt;/strong&gt; — The killed transaction is rolled back. There is no undo for the kill itself — the work that transaction had done is lost. Before killing, check &lt;code&gt;trx_query&lt;/code&gt; and &lt;code&gt;trx_rows_modified&lt;/code&gt; to understand what the transaction was doing. For a long-running OLAP query that was just reading, the only cost is rerunning the query. For a transaction in the middle of writes, the application will see a lost connection error and should retry.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;The diagnosis steps in this playbook can be partially automated with two tools.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Percona Toolkit’s &lt;code&gt;pt-query-digest&lt;/code&gt;&lt;/strong&gt; processes slow log files and produces an aggregated report sorted by total time, showing query patterns, execution statistics, and EXPLAIN output. It is the documented standard for batch slow log analysis and handles log rotation correctly:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pt-query-digest&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/lib/mysql/hostname-slow.log&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; digest_report.txt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;pt-query-digest&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; --since=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;1h&apos;&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; /var/lib/mysql/hostname-slow.log&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Percona Toolkit is open-source and documented at &lt;a href=&quot;https://www.percona.com/software/database-tools/percona-toolkit&quot;&gt;percona.com/software/database-tools/percona-toolkit&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Trending with Performance Schema&lt;/strong&gt; — The digest table retains aggregated data across the server’s uptime. A scheduled query that snapshots &lt;code&gt;SUM_TIMER_WAIT&lt;/code&gt; and &lt;code&gt;COUNT_STAR&lt;/code&gt; into a monitoring table every 5 minutes gives you a trend line for query cost over time, which is more useful than a point-in-time alert:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Snapshot top 20 digests into a monitoring table every 5 minutes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; perf_snapshots (captured_at, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;digest&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, total_sec, call_count)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  NOW&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  DIGEST_TEXT,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  SUM_TIMER_WAIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000000000000&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  COUNT_STAR&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; performance_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;events_statements_summary_by_digest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; SUM_TIMER_WAIT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On RDS, the &lt;code&gt;SlowQueries&lt;/code&gt; CloudWatch metric counts queries exceeding &lt;code&gt;long_query_time&lt;/code&gt; per minute. Set an alarm at a threshold above your baseline (e.g., more than 5 slow queries per minute) to trigger early before p99 latency is customer-visible.&lt;/p&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;A database query exceeded the response time threshold, causing elevated p99 latency visible in application monitoring.&lt;/li&gt;
&lt;li&gt;The slow query was identified using Performance Schema digest tables and the slow query log; root cause was a missing index causing a full table scan. The index was added using online DDL with no downtime.&lt;/li&gt;
&lt;li&gt;Automated slow query alerting via CloudWatch and a scheduled Performance Schema snapshot prevents undetected regressions going forward.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;checklist&quot;&gt;Checklist&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Confirm &lt;code&gt;slow_query_log = ON&lt;/code&gt; and &lt;code&gt;long_query_time&lt;/code&gt; is set to a meaningful threshold (1 second is standard; 0.5 on high-volume OLTP).&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;mysqldumpslow -s t -t 10&lt;/code&gt; on the slow log to identify the top queries by total time.&lt;/li&gt;
&lt;li&gt;Query &lt;code&gt;performance_schema.events_statements_summary_by_digest&lt;/code&gt; sorted by &lt;code&gt;SUM_TIMER_WAIT DESC&lt;/code&gt; to confirm the same pattern.&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;information_schema.innodb_lock_waits&lt;/code&gt; for any active lock waits involving the slow query’s table.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;SHOW INDEX FROM &amp;#x3C;table&gt;&lt;/code&gt; and check &lt;code&gt;Cardinality&lt;/code&gt; values — anomalously low values indicate stale statistics.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;EXPLAIN FORMAT=JSON&lt;/code&gt; (or &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; on MySQL 8.0+) on the identified query and look for &lt;code&gt;type: ALL&lt;/code&gt;, &lt;code&gt;Using filesort&lt;/code&gt;, and &lt;code&gt;Using temporary&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If a full scan is confirmed, design a covering index that places equality predicates first and range predicates last, then test with &lt;code&gt;EXPLAIN&lt;/code&gt; before adding.&lt;/li&gt;
&lt;li&gt;If lock contention is confirmed, identify the blocking thread using &lt;code&gt;innodb_lock_waits&lt;/code&gt; and decide whether to kill it based on transaction age and &lt;code&gt;trx_rows_modified&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If plan is bad despite good indexes, run &lt;code&gt;ANALYZE TABLE&lt;/code&gt; to refresh InnoDB statistics.&lt;/li&gt;
&lt;li&gt;After adding an index, re-run the original query under load and verify &lt;code&gt;rows_examined&lt;/code&gt; drops to near &lt;code&gt;rows_sent&lt;/code&gt; in the slow log.&lt;/li&gt;
&lt;li&gt;Set up a CloudWatch alarm on &lt;code&gt;SlowQueries&lt;/code&gt; above baseline, or configure a Performance Schema snapshot job to trend query cost over time.&lt;/li&gt;
&lt;li&gt;Document the root cause, the index added, and the cardinality values before and after for the incident record.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;what-this-post-does-not-cover&quot;&gt;What This Post Does Not Cover&lt;/h2&gt;
&lt;p&gt;This post covers identifying and resolving an active slow query in MySQL or Aurora MySQL. It does not cover: InnoDB full-text search tuning, ProxySQL query routing and query cache invalidation, Aurora Serverless v2 capacity scaling behavior during query spikes, or MySQL Group Replication lag as a driver of secondary read slowness. Those are distinct triage paths.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: When a slow query alert fires, engineers waste time looking at the wrong signal — checking instance CPU when the real cause is a missing index, or tuning configuration when lock contention is blocking a single thread.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Run the five checks in order — slow log, Performance Schema digest, lock waits, index cardinality, EXPLAIN — before touching any configuration or schema. Each check either confirms the cause or narrows it to the next step.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: After applying the fix, &lt;code&gt;rows_examined&lt;/code&gt; drops to within 2× of &lt;code&gt;rows_sent&lt;/code&gt; in the slow log and &lt;code&gt;SUM_TIMER_WAIT&lt;/code&gt; for the affected digest falls out of the top-10 list.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, confirm &lt;code&gt;slow_query_log = ON&lt;/code&gt; and &lt;code&gt;long_query_time &amp;#x3C;= 1&lt;/code&gt; on every production MySQL instance, and set a CloudWatch &lt;code&gt;SlowQueries&lt;/code&gt; alarm above your normal baseline so the next regression is detected before it reaches p99 latency.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>checklist</category><category>failures</category></item><item><title>MySQL InnoDB Buffer Pool: The First Thing to Check</title><link>https://rajivonai.com/blog/2022-05-09-mysql-innodb-buffer-pool-the-first-thing-to-check/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-05-09-mysql-innodb-buffer-pool-the-first-thing-to-check/</guid><description>The InnoDB buffer pool hit ratio and size are the first metrics to verify on any MySQL server — a default 128MB pool on a 32GB machine sends every query to disk.</description><pubDate>Mon, 09 May 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;The InnoDB buffer pool is MySQL’s most important tuning knob, and it ships with a default that is wrong for almost every production server.&lt;/strong&gt; On a dedicated 32 GB database host, the default &lt;code&gt;innodb_buffer_pool_size&lt;/code&gt; is 128 MB. Every page that does not fit in that 128 MB goes to disk. The result is predictable: IOPS saturate, query latency climbs, and the server looks overloaded even at modest traffic levels.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;InnoDB is a disk-based storage engine. It caches data pages, index pages, and undo information in the buffer pool — a region of RAM managed entirely by the engine. When a query reads a row, InnoDB first checks the buffer pool. A hit means the row is returned from memory. A miss means InnoDB issues a read from the underlying block device, which costs orders of magnitude more time.&lt;/p&gt;
&lt;p&gt;On a freshly provisioned MySQL server, &lt;code&gt;innodb_buffer_pool_size&lt;/code&gt; defaults to 128 MB. That number was chosen for embedded and low-memory deployments. It has nothing to do with what a production workload needs. Engineers who inherit a server and do not check this setting often spend weeks chasing index problems, connection pool tuning, and query rewrites that cannot fix a fundamentally undersized memory tier.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;When the buffer pool is too small for the active working set, InnoDB continuously evicts pages to make room for new reads. Every evicted page that is needed again becomes a physical disk read. At high request rates, that eviction cycle saturates storage I/O, drives up query latency, and eventually limits throughput entirely.&lt;/p&gt;
&lt;p&gt;The failure is not subtle. IOPS on the storage volume spike to near its limit. Query latency climbs. CPU stays moderate because the bottleneck is I/O wait, not compute. SHOW ENGINE INNODB STATUS reports high physical reads per second. The standard diagnostic path — look at slow query log, add indexes, tune joins — does not help because the bottleneck is upstream of query execution.&lt;/p&gt;
&lt;p&gt;The core question is simple: does the buffer pool hold your working set, or is MySQL reading from disk on every cache miss?&lt;/p&gt;
&lt;h2 id=&quot;core-concept&quot;&gt;Core Concept&lt;/h2&gt;
&lt;p&gt;InnoDB divides the buffer pool into pages (16 KB by default). It manages those pages using a modified LRU algorithm: pages accessed recently stay near the head; pages that have not been touched are evicted from the tail when space is needed. A read-ahead mechanism pre-fetches sequential pages during full scans — useful for analytics queries, but a source of unnecessary eviction pressure when it floods the pool with pages that will not be reused.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Query[Client Query] --&gt; Engine[InnoDB Storage Engine]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Engine --&gt; Check{Page in Buffer Pool}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Check --&gt;|Hit| HitNode[Return Row from Memory]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Check --&gt;|Miss| MissNode[Read Page from Disk]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    MissNode --&gt; Load[Load Page into LRU Head]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Load --&gt; Evict[Evict Page from LRU Tail if Full]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Evict --&gt; HitNode&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Checking hit ratio and sizing:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Buffer pool metrics&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;SHOW &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;STATUS&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; LIKE&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;Innodb_buffer_pool%&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The key metrics:&lt;/p&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Metric&lt;/th&gt;&lt;th&gt;What it measures&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Innodb_buffer_pool_read_requests&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Logical reads attempted from the pool&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Innodb_buffer_pool_reads&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Physical reads from disk (pool misses)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Innodb_buffer_pool_pages_data&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Pages currently holding data&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;Innodb_buffer_pool_pages_free&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Pages available for new data&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Hit ratio formula:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  (&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    variable_value &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; variable_value &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;global_status&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;     WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; variable_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;Innodb_buffer_pool_read_requests&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  )) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; buffer_pool_hit_ratio_pct&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; information_schema&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;global_status&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; variable_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;Innodb_buffer_pool_reads&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A healthy server runs above 99%. Below 95% is a strong signal that the pool is undersized for the workload.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sizing guidance from MySQL InnoDB documentation:&lt;/strong&gt; set &lt;code&gt;innodb_buffer_pool_size&lt;/code&gt; to 70–80% of available RAM on a dedicated MySQL server. On a 32 GB server, that is 22–25 GB. On a 64 GB server, 45–50 GB.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Multiple instances:&lt;/strong&gt; For multi-core servers where the buffer pool is larger than 1 GB, MySQL documentation recommends setting &lt;code&gt;innodb_buffer_pool_instances&lt;/code&gt; to one instance per 1 GB of pool size (capped at 64). Multiple instances reduce internal mutex contention on the pool itself.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;ini&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;# /etc/mysql/mysql.conf.d/mysqld.cnf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;innodb_buffer_pool_size&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 24G&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;innodb_buffer_pool_instances&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; = 24&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Changes require a server restart. On MySQL 5.7.5 and later, dynamic resizing is supported with some limitations; for large changes, a coordinated restart is safer.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;SHOW ENGINE INNODB STATUS&lt;/strong&gt; provides additional diagnostics in the &lt;code&gt;BUFFER POOL AND MEMORY&lt;/code&gt; section, including pages read, pages written, buffer pool hit rate (as a rolling 1000-second average), and pending reads.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;The documented behavior of InnoDB, as described in the MySQL 8.0 Reference Manual (chapter “InnoDB Buffer Pool”), is that the buffer pool is the primary memory structure controlling InnoDB I/O performance. MySQL documentation explicitly states the 70–80% guideline for dedicated servers and notes that the default 128 MB is appropriate only for small or testing environments.&lt;/p&gt;
&lt;p&gt;The pattern of buffer pool undersizing causing I/O saturation is documented in the MySQL performance schema and SHOW STATUS output — the ratio of &lt;code&gt;Innodb_buffer_pool_reads&lt;/code&gt; to &lt;code&gt;Innodb_buffer_pool_read_requests&lt;/code&gt; directly reflects how often the server falls through to disk. Any ratio above 1–2% physical reads warrants investigation of pool size against working set.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Working set grows beyond pool size&lt;/td&gt;&lt;td&gt;Hit ratio drops; IOPS spike&lt;/td&gt;&lt;td&gt;Eviction cycle exceeds storage bandwidth&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Buffer pool sized too large on a shared host&lt;/td&gt;&lt;td&gt;OS swap pressure; latency spikes&lt;/td&gt;&lt;td&gt;MySQL takes memory the OS needed for file cache&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Many small short-lived transactions&lt;/td&gt;&lt;td&gt;Pool fragmented with small dirty pages&lt;/td&gt;&lt;td&gt;Checkpoint pressure increases; write amplification grows&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: The buffer pool is sized at default 128 MB on a production server, sending nearly every cache miss to disk and saturating storage I/O.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Set &lt;code&gt;innodb_buffer_pool_size&lt;/code&gt; to 70–80% of RAM on dedicated servers; set &lt;code&gt;innodb_buffer_pool_instances&lt;/code&gt; to one per GB of pool size.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Run &lt;code&gt;SHOW STATUS LIKE &apos;Innodb_buffer_pool%&apos;&lt;/code&gt; before and after resize and verify the hit ratio climbs above 99%; watch &lt;code&gt;Innodb_buffer_pool_reads&lt;/code&gt; drop toward zero.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, calculate the current hit ratio using the formula above. If it is below 99%, check the configured pool size and compare it against the server’s total RAM.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The buffer pool is not a performance optimization — it is the baseline. Everything else in InnoDB tuning assumes the working set fits in memory. If it does not, no amount of index work or query rewriting closes the gap.&lt;/p&gt;</content:encoded><category>databases</category></item><item><title>PostgreSQL Autovacuum: What Every Engineer Should Know</title><link>https://rajivonai.com/blog/2022-04-11-postgresql-autovacuum-what-every-engineer-should-know/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-04-11-postgresql-autovacuum-what-every-engineer-should-know/</guid><description>Autovacuum is not optional maintenance — it is the mechanism that prevents table bloat and transaction ID wraparound from taking your database offline.</description><pubDate>Mon, 11 Apr 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Autovacuum is not a background nicety. It is the process that keeps PostgreSQL’s MVCC machinery from accumulating dead tuples until the table is unreadable, and the process that prevents transaction ID wraparound — a condition where PostgreSQL freezes all writes and forces an emergency vacuum on the entire cluster.&lt;/strong&gt; Treating autovacuum as optional, throttling it too hard on OLTP servers, or simply not knowing what its thresholds mean is one of the most common ways production PostgreSQL clusters degrade over months before anyone notices.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;PostgreSQL uses multi-version concurrency control (MVCC). When a row is updated or deleted, PostgreSQL does not overwrite it in place — it marks the old row version as dead and writes a new version. The dead row versions (dead tuples) accumulate on disk and remain visible to old transactions that might still need them. This is what makes non-blocking reads possible: readers never block writers, and writers never block readers.&lt;/p&gt;
&lt;p&gt;But dead tuples cost disk space, and they slow down sequential scans because the storage engine has to skip over them. At the extreme end, transaction IDs are 32-bit integers — after about 2 billion transactions, PostgreSQL will wrap around and enter a state where it cannot guarantee which data is old and which is new. To prevent corruption, PostgreSQL will refuse all writes and force a full-cluster VACUUM FREEZE.&lt;/p&gt;
&lt;p&gt;Autovacuum is the background daemon that reclaims dead tuples and advances the freeze horizon before either of these problems becomes a crisis.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The default autovacuum thresholds are designed for small-to-medium tables. The trigger condition is:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;autovacuum_vacuum_threshold + autovacuum_vacuum_scale_factor × n_live_tup&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With &lt;code&gt;autovacuum_vacuum_scale_factor = 0.2&lt;/code&gt; (the default), autovacuum triggers a VACUUM when 20% of the live row count has accumulated as dead tuples. On a table with 1,000 rows, this fires after 200 dead tuples — reasonable. On a table with 50 million rows, it fires after 10 million dead tuples have accumulated. That is a lot of bloat before the cleanup runs.&lt;/p&gt;
&lt;p&gt;High-write tables — event logs, audit trails, queues, sessions — accumulate dead tuples faster than autovacuum can clear them at the default settings. The table grows. Indexes bloat. Query plans drift toward sequential scans. The system appears slow without an obvious cause, and the only way to recover is an explicit VACUUM or, worse, a VACUUM FULL (which rewrites the entire table and requires an exclusive lock).&lt;/p&gt;
&lt;p&gt;The core question: how do you tune autovacuum before table bloat becomes a production incident?&lt;/p&gt;
&lt;h2 id=&quot;how-autovacuum-threshold-and-cost-throttling-work&quot;&gt;How Autovacuum Threshold and Cost Throttling Work&lt;/h2&gt;
&lt;p&gt;Autovacuum has two independently important levers: &lt;strong&gt;when it runs&lt;/strong&gt; and &lt;strong&gt;how fast it runs&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;When it runs&lt;/strong&gt; is controlled by the threshold formula above. For large, high-write tables, you almost always need to override &lt;code&gt;autovacuum_vacuum_scale_factor&lt;/code&gt; at the table level rather than globally:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; events &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  autovacuum_vacuum_scale_factor &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;01&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  autovacuum_vacuum_threshold &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This tells autovacuum to trigger after 1% of rows become dead (plus a baseline of 1,000 dead tuples), rather than 20%. For a 50 million row table, that fires after 500,000 dead tuples instead of 10 million.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How fast it runs&lt;/strong&gt; is controlled by &lt;code&gt;autovacuum_vacuum_cost_delay&lt;/code&gt; (default: 2ms in PG13+, 20ms in older versions). This is a per-page throttle: after vacuuming &lt;code&gt;autovacuum_vacuum_cost_limit&lt;/code&gt; worth of pages, autovacuum sleeps for &lt;code&gt;autovacuum_vacuum_cost_delay&lt;/code&gt; milliseconds. The intent is to prevent autovacuum from overwhelming I/O on a shared server. The side effect is that on OLTP servers with continuous high write throughput, autovacuum can be so throttled that it never catches up.&lt;/p&gt;
&lt;p&gt;You can observe the current autovacuum state per-table in &lt;code&gt;pg_stat_user_tables&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  relname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_live_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_dead_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_autovacuum,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_autoanalyze&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_dead_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A table with a high &lt;code&gt;n_dead_tup&lt;/code&gt; relative to &lt;code&gt;n_live_tup&lt;/code&gt; and a stale &lt;code&gt;last_autovacuum&lt;/code&gt; timestamp is a table where autovacuum is not keeping up.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;autovacuum_max_workers&lt;/code&gt; (default: 3) controls how many autovacuum processes can run simultaneously. On clusters with many high-write tables, this can become the binding constraint — all workers are busy on large tables and smaller tables go unvacuumed.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;PostgreSQL’s autovacuum documentation (postgresql.org/docs/current/routine-vacuuming.html) documents the wraparound risk directly: when a table’s &lt;code&gt;relfrozenxid&lt;/code&gt; age approaches &lt;code&gt;autovacuum_freeze_max_age&lt;/code&gt; (default: 200 million transactions), PostgreSQL will force an anti-wraparound vacuum that ignores the normal cost throttling. This means a heavily throttled autovacuum configuration will eventually be overridden by the system — but not before the forced vacuum causes a visible I/O spike.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;pg_stat_user_tables&lt;/code&gt; view is the documented interface for observing autovacuum behavior per table. The columns &lt;code&gt;n_dead_tup&lt;/code&gt;, &lt;code&gt;last_autovacuum&lt;/code&gt;, &lt;code&gt;last_autoanalyze&lt;/code&gt;, and &lt;code&gt;autovacuum_count&lt;/code&gt; give the observable signal for whether thresholds are tuned correctly.&lt;/p&gt;
&lt;p&gt;The documented pattern from PostgreSQL’s VACUUM documentation is that per-table storage parameters (&lt;code&gt;autovacuum_vacuum_scale_factor&lt;/code&gt;, &lt;code&gt;autovacuum_vacuum_cost_delay&lt;/code&gt;) override the server-level &lt;code&gt;postgresql.conf&lt;/code&gt; settings — this is the correct mechanism for table-level tuning without changing global behavior.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;

























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Scenario&lt;/th&gt;&lt;th&gt;What breaks&lt;/th&gt;&lt;th&gt;Why&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Autovacuum disabled explicitly (&lt;code&gt;autovacuum = off&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;Dead tuples accumulate unbounded; XID wraparound will eventually force a full-cluster emergency vacuum&lt;/td&gt;&lt;td&gt;The only thing preventing unbounded table bloat is operator-run VACUUM; one missed cycle compounds&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Cost delay set too high on OLTP servers&lt;/td&gt;&lt;td&gt;Autovacuum runs slower than dead tuples accumulate; table bloat grows continuously&lt;/td&gt;&lt;td&gt;Each worker sleeps too long between pages; on high-write tables the math never closes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;XID wraparound forces anti-wraparound vacuum&lt;/td&gt;&lt;td&gt;All autovacuum workers redirect to the aging table, ignoring cost limits; other tables go unvacuumed&lt;/td&gt;&lt;td&gt;Anti-wraparound vacuum is not throttled — it will consume I/O to protect data integrity&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: On large, high-write tables the default 20% scale factor lets millions of dead tuples accumulate before autovacuum triggers, causing progressive table and index bloat.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Override &lt;code&gt;autovacuum_vacuum_scale_factor&lt;/code&gt; at the table level (set to 0.01–0.05 for tables over 1M rows) and reduce &lt;code&gt;autovacuum_vacuum_cost_delay&lt;/code&gt; on servers where autovacuum is falling behind.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Query &lt;code&gt;pg_stat_user_tables&lt;/code&gt; and confirm &lt;code&gt;n_dead_tup&lt;/code&gt; on your high-write tables stays below 1–2% of &lt;code&gt;n_live_tup&lt;/code&gt; over a 24-hour window.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: This week, run &lt;code&gt;SELECT relname, n_dead_tup, n_live_tup, last_autovacuum FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 20;&lt;/code&gt; and identify which tables have not been vacuumed recently or have high dead tuple ratios — those are the candidates for per-table threshold tuning.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>checklist</category></item><item><title>PostgreSQL Slow Query Triage Workflow</title><link>https://rajivonai.com/blog/2022-03-21-postgresql-slow-query-triage-workflow/</link><guid isPermaLink="true">https://rajivonai.com/blog/2022-03-21-postgresql-slow-query-triage-workflow/</guid><description>A structured runbook for diagnosing slow query root causes in PostgreSQL — missing indexes, stale statistics, lock contention, and I/O saturation — in the order that wastes the least time.</description><pubDate>Mon, 21 Mar 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;When p95 latency spikes and the on-call alert fires, most engineers open the slow query log and immediately jump to the biggest query by average execution time. That is the wrong move. The query that shows up longest in &lt;code&gt;pg_stat_statements&lt;/code&gt; is often not the query that caused the spike — it is the query that was already slow. The blocking transaction, the missing index on a newly-deployed code path, or autovacuum being interrupted mid-table are the usual culprits. This runbook gives you the order to check that actually closes incidents.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;A p95 latency spike lands in monitoring. The graphs show it clearly: something changed in the last five to fifteen minutes. The application is returning slow responses. Your first instinct is to check the dashboard, which shows elevated CPU and read latency on the database host. &lt;code&gt;pg_stat_activity&lt;/code&gt; has more active connections than usual. The alert threshold on slow queries crossed.&lt;/p&gt;
&lt;p&gt;At this point, engineers split into two groups. The first opens the slow query log, picks the worst query, and starts trying to add an index or rewrite the SQL. The second checks what PostgreSQL is actually doing right now — what is blocked, what is waiting, and what happened to statistics or autovacuum in the last hour. The second group resolves the incident faster because they are reading system state rather than historical averages.&lt;/p&gt;
&lt;p&gt;The problem with jumping straight to the slow query log is that &lt;code&gt;pg_stat_statements&lt;/code&gt; accumulates over time. A query that has always been slow will look exactly like a query that just started being slow because of a table scan it previously avoided. You need the current state first, then the cumulative data as context.&lt;/p&gt;
&lt;p&gt;PostgreSQL exposes the information you need through its system catalog views. The triage workflow below uses five queries — in order — to eliminate root causes before you start making changes.&lt;/p&gt;
&lt;h2 id=&quot;symptoms&quot;&gt;Symptoms&lt;/h2&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Signal&lt;/th&gt;&lt;th&gt;Where to see it&lt;/th&gt;&lt;th&gt;What it means&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Active query count above baseline&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_activity&lt;/code&gt;, CloudWatch connections metric&lt;/td&gt;&lt;td&gt;Connection pressure or query backup — check for lock waits first&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Queries appearing in slow query log with new query shapes&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_statements&lt;/code&gt;, auto_explain log output&lt;/td&gt;&lt;td&gt;New code path or table growth crossed a plan-change threshold&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Sequential scan on a large table in explain output&lt;/td&gt;&lt;td&gt;&lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; output&lt;/td&gt;&lt;td&gt;Missing index or statistics too stale to use an existing one&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;waiting&lt;/code&gt; column true for multiple queries&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_activity&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Lock contention — one transaction is blocking others&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;High read I/O on the database host&lt;/td&gt;&lt;td&gt;CloudWatch read latency, Datadog disk metrics&lt;/td&gt;&lt;td&gt;Table or index bloat forcing extra page reads; autovacuum may be behind&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;last_autoanalyze&lt;/code&gt; timestamp hours or days old on active table&lt;/td&gt;&lt;td&gt;&lt;code&gt;pg_stat_user_tables&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Stale statistics — planner is working from outdated row estimates&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;first-five-checks&quot;&gt;First Five Checks&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Find currently running slow queries&lt;/strong&gt; — This is always first. Before looking at anything historical, see what PostgreSQL is doing right now. Queries held open for more than five seconds are either blocked, doing real work, or stuck. The &lt;code&gt;state&lt;/code&gt; column tells you whether they are actively executing or waiting.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  pid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; query_start &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; duration,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  wait_event_type,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  wait_event,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  query&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;idle&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; query_start &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; interval &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;5 seconds&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; duration &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Look at &lt;code&gt;wait_event_type&lt;/code&gt;. If it reads &lt;code&gt;Lock&lt;/code&gt;, you have a lock contention issue. If it reads &lt;code&gt;IO&lt;/code&gt;, the query is waiting on disk. If it is null, the query is actively executing — check the plan next.&lt;/p&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;&lt;strong&gt;Find top queries by cumulative execution time&lt;/strong&gt; — Once you know what is running now, pull the historical picture from &lt;code&gt;pg_stat_statements&lt;/code&gt;. This extension is documented in the PostgreSQL &lt;code&gt;pg_stat_statements&lt;/code&gt; module reference and accumulates statistics since the last reset. Sort by &lt;code&gt;total_exec_time&lt;/code&gt; to find queries that are expensive in aggregate, not just occasionally slow.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  query,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  calls,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  total_exec_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; calls &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; avg_ms,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  total_exec_time,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  rows&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_statements&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; total_exec_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A query with high &lt;code&gt;avg_ms&lt;/code&gt; but low &lt;code&gt;calls&lt;/code&gt; is an outlier. A query with moderate &lt;code&gt;avg_ms&lt;/code&gt; but millions of &lt;code&gt;calls&lt;/code&gt; is a throughput problem. Both need attention, but the right fix differs.&lt;/p&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;&lt;strong&gt;Check for lock waits&lt;/strong&gt; — If check 1 showed any &lt;code&gt;wait_event_type = &apos;Lock&apos;&lt;/code&gt; rows, this query identifies the full blocking chain. &lt;code&gt;pg_blocking_pids()&lt;/code&gt; is a PostgreSQL built-in that returns the PIDs of sessions blocking a given session.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  blocked&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  blocked&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  blocked&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;wait_event_type&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  blocking&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_pid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  blocking&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;query&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_query,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;  blocking&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_state,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocking&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;query_start&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_duration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity blocked&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity blocking&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocking&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ANY(pg_blocking_pids(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;blocked&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;blocking_query&lt;/code&gt; column often reveals the transaction holding the lock. An idle-in-transaction connection is a common culprit: a transaction that opened, ran one query, and then paused while the application did something else — holding its lock the whole time.&lt;/p&gt;
&lt;ol start=&quot;4&quot;&gt;
&lt;li&gt;&lt;strong&gt;Check table statistics age&lt;/strong&gt; — If lock waits are not the issue, check whether the planner is working from stale statistics. PostgreSQL uses statistics collected by &lt;code&gt;ANALYZE&lt;/code&gt; to estimate row counts and choose access paths. When statistics fall behind the actual table state — after a large data load, a batch delete, or a period when autovacuum was interrupted — the planner can choose a sequential scan where an index would be far faster.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  schemaname,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  tablename,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_analyze,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_autoanalyze,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_live_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_dead_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_dead_tup::&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; NULLIF&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(n_live_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_dead_tup, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; dead_ratio&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; n_dead_tup &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LIMIT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A table with a &lt;code&gt;last_autoanalyze&lt;/code&gt; timestamp more than a few hours old on a high-write workload, or a &lt;code&gt;dead_ratio&lt;/code&gt; above 10–20%, is a candidate. The autovacuum capacity implications of this pattern are covered in depth in &lt;a href=&quot;https://rajivonai.com/blog/2025-09-13-autovacuum-is-a-capacity-problem-not-a-maintenance-task/&quot;&gt;Autovacuum Is a Capacity Problem, Not a Maintenance Task&lt;/a&gt;.&lt;/p&gt;
&lt;ol start=&quot;5&quot;&gt;
&lt;li&gt;&lt;strong&gt;Get EXPLAIN ANALYZE for the slow query&lt;/strong&gt; — Once you have identified the specific query from checks 1 or 2, pull the execution plan with buffer statistics. &lt;code&gt;BUFFERS&lt;/code&gt; output shows how many shared buffer hits versus disk reads the query required, which distinguishes a missing index (high shared hits, no index scan) from an I/O problem (high disk reads).&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;EXPLAIN (ANALYZE, BUFFERS, FORMAT &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;TEXT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;paste slow query here&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Look for: &lt;code&gt;Seq Scan&lt;/code&gt; on a table with high &lt;code&gt;rows=&lt;/code&gt; estimates, &lt;code&gt;rows=1&lt;/code&gt; estimates on nodes where the actual rows are in the thousands (stale statistics), and &lt;code&gt;Buffers: shared read=&lt;/code&gt; values that are high relative to table size.&lt;/p&gt;
&lt;h2 id=&quot;decision-tree&quot;&gt;Decision Tree&lt;/h2&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    A[Slow query alert fires] --&gt; B{pg_stat_activity — queries waiting on Lock?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|yes| C[Check blocking chain — kill or wait out blocker]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    B --&gt;|no| D{EXPLAIN shows Seq Scan on large table?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    D --&gt;|yes| E{Index exists for this predicate?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|no| F[Add index with CREATE INDEX CONCURRENTLY]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    E --&gt;|yes| G{Statistics stale — last_autoanalyze old?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|yes| H[Run ANALYZE on table — recheck plan]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    G --&gt;|no| I{High Buffers: shared read in EXPLAIN?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|yes| J[Check table bloat and autovacuum lag]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    I --&gt;|no| K{Connection count near pool limit?}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|yes| L[Check pool settings and idle-in-transaction connections]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    K --&gt;|no| M[Profile query logic — may be algorithmic]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What this diagram shows:&lt;/strong&gt; A decision tree for PostgreSQL slow query triage — starting with active lock waits, then sequential scans on large tables, missing indexes, stale statistics (last_autoanalyze), high shared buffer reads indicating bloat, and connection pool saturation — in the order that eliminates the most common root causes first.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;remediation-options&quot;&gt;Remediation Options&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Option 1 — Add a missing index&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When &lt;code&gt;EXPLAIN&lt;/code&gt; shows a sequential scan on a large table and no index covers the query predicate, create one online. &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; builds the index without blocking reads or writes. It takes longer than a standard index build, and it can fail if the transaction load is very high, but it is the safe choice for production.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; INDEX&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; CONCURRENTLY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; idx_orders_customer_created&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  ON&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders (customer_id, created_at)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;cancelled&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Partial indexes (the &lt;code&gt;WHERE&lt;/code&gt; clause above) reduce size and improve selectivity when the query always filters on a stable condition. After creation, run &lt;code&gt;EXPLAIN&lt;/code&gt; again to confirm the planner picks up the new index. If it does not, check that the statistics are current — &lt;code&gt;ANALYZE orders;&lt;/code&gt; and re-examine.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 2 — Refresh stale statistics&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When &lt;code&gt;EXPLAIN&lt;/code&gt; shows row estimates that are far off from actual rows — typically &lt;code&gt;rows=1&lt;/code&gt; or a small number where the actual is thousands — and &lt;code&gt;pg_stat_user_tables&lt;/code&gt; shows a stale &lt;code&gt;last_autoanalyze&lt;/code&gt;, run &lt;code&gt;ANALYZE&lt;/code&gt; manually.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;ANALYZE &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VERBOSE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;ANALYZE&lt;/code&gt; is always safe. It takes a &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt; lock, which does not block reads or writes. It completes quickly on most tables. After it finishes, run &lt;code&gt;EXPLAIN&lt;/code&gt; again. If the plan does not change, the statistics were not the issue — move to the next check.&lt;/p&gt;
&lt;p&gt;If autovacuum is consistently falling behind on this table, the default &lt;code&gt;autovacuum_analyze_scale_factor&lt;/code&gt; of 20% is too coarse for large or frequently-modified tables. Lower it per-table:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ALTER&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (autovacuum_analyze_scale_factor &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;01&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Option 3 — Resolve lock contention&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When the blocking chain query from check 3 shows a long-running transaction holding a lock that others are waiting on, you have two choices: wait for it to finish, or terminate it.&lt;/p&gt;
&lt;p&gt;Terminate with care. &lt;code&gt;pg_terminate_backend()&lt;/code&gt; sends SIGTERM to the backend process; the transaction rolls back and its locks are released immediately. Use it when the blocking transaction has been idle for longer than your incident SLA, or when it is clearly stuck.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_terminate_backend(blocking_pid)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  SELECT DISTINCT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocking&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; AS&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; blocking_pid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity blocked&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  JOIN&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity blocking&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    ON&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocking&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ANY(pg_blocking_pids(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;blocked&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;pid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  WHERE&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocking&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;idle in transaction&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    AND&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; blocking&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;query_start&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &gt;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; interval &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;2 minutes&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) sub;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After terminating, investigate why the transaction stayed open. Idle-in-transaction connections usually point to application-side connection pool misconfiguration or missing error handling that closes transactions on exception.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Option 4 — Address bloat and autovacuum lag&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When &lt;code&gt;EXPLAIN&lt;/code&gt; shows high &lt;code&gt;Buffers: shared read=&lt;/code&gt; values disproportionate to the query’s logical data needs, and &lt;code&gt;pg_stat_user_tables&lt;/code&gt; shows high &lt;code&gt;n_dead_tup&lt;/code&gt; on the relevant table, dead row versions are inflating the table and causing unnecessary disk reads.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Check bloat on a specific table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_live_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  n_dead_tup,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_autovacuum,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  last_autoanalyze&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_user_tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; relname &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;orders&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Force vacuum manually during the incident&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;VACUUM (&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;VERBOSE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, ANALYZE) orders;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Standard &lt;code&gt;VACUUM&lt;/code&gt; — as opposed to &lt;code&gt;VACUUM FULL&lt;/code&gt; — does not block reads or writes. It reclaims dead tuple space and updates statistics. &lt;code&gt;VACUUM FULL&lt;/code&gt; requires an exclusive lock and rewrites the table; it should not be used on production tables during an incident.&lt;/p&gt;
&lt;h2 id=&quot;rollback-plan&quot;&gt;Rollback Plan&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Created index with &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt;&lt;/strong&gt; — Drop it with &lt;code&gt;DROP INDEX CONCURRENTLY&lt;/code&gt;. The drop is also online and does not block queries. If the index was a partial index, dropping it has no data impact.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Ran &lt;code&gt;ANALYZE&lt;/code&gt;&lt;/strong&gt; — No rollback needed. &lt;code&gt;ANALYZE&lt;/code&gt; updates statistics only. The planner reverts to the previous plan at the next statistics collection if the table state reverts. There is no mechanism to restore old statistics directly.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Killed a blocking transaction&lt;/strong&gt; — The killed transaction rolls back automatically. Any work it had done is undone. Monitor &lt;code&gt;pg_stat_activity&lt;/code&gt; to confirm the blocked queries resume. If they do not, check for a new blocking chain.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Ran &lt;code&gt;VACUUM&lt;/code&gt;&lt;/strong&gt; — No rollback needed. Vacuum is additive: it reclaims space but does not modify live rows. Re-enable autovacuum if it was disabled during the incident.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;automation-opportunity&quot;&gt;Automation Opportunity&lt;/h2&gt;
&lt;p&gt;Two automation patterns are worth implementing before the next incident rather than after.&lt;/p&gt;
&lt;p&gt;The first is continuous slow query capture. PostgreSQL’s &lt;code&gt;auto_explain&lt;/code&gt; extension logs execution plans automatically when a query exceeds a duration threshold. Add these settings to &lt;code&gt;postgresql.conf&lt;/code&gt; (or as session-level settings for testing):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Load the extension (requires restart or ALTER SYSTEM)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;LOAD&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;auto_explain&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; auto_explain&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;log_min_duration&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;1s&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; auto_explain&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;log_analyze&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; true;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SET&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; auto_explain&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;log_buffers&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; true;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With &lt;code&gt;auto_explain&lt;/code&gt; active, every query over one second logs its plan to the PostgreSQL log. Feed those logs to a log aggregator and you will have plan history before the incident rather than needing to reconstruct it after.&lt;/p&gt;
&lt;p&gt;The second is a scheduled &lt;code&gt;pg_stat_activity&lt;/code&gt; snapshot. Use &lt;code&gt;pg_cron&lt;/code&gt; to capture long-running queries every minute to a local table. This gives you a timeline to review post-incident that &lt;code&gt;pg_stat_statements&lt;/code&gt; alone cannot provide, since &lt;code&gt;pg_stat_statements&lt;/code&gt; aggregates across time but does not record when queries were running.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Requires pg_cron extension&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; cron&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;schedule&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;capture-slow-queries&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  &apos;* * * * *&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  $$&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    INSERT INTO&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; slow_query_log (captured_at, pid, duration, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, query)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(), pid, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; query_start, &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, query&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; pg_stat_activity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;idle&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      AND&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; query_start &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; now&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; interval &lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&apos;10 seconds&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  $$&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Alert on this table when row counts spike: that is an early signal that something is blocking normal query throughput before the application-side p95 alert fires.&lt;/p&gt;
&lt;h2 id=&quot;leadership-summary&quot;&gt;Leadership Summary&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What broke&lt;/strong&gt;: Queries slowed because of lock contention from a long-running transaction, or because the query planner chose a sequential scan after table statistics fell out of date.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;What was done&lt;/strong&gt;: Identified the root cause using PostgreSQL system catalog queries, terminated the blocking connection or added a missing index, and ran &lt;code&gt;ANALYZE&lt;/code&gt; to refresh planner statistics.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;What prevents recurrence&lt;/strong&gt;: &lt;code&gt;auto_explain&lt;/code&gt; now captures slow query plans automatically; per-table autovacuum thresholds are set for high-write tables; a &lt;code&gt;pg_cron&lt;/code&gt; job snapshots long-running queries every minute for post-incident review.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;checklist&quot;&gt;Checklist&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Pull currently running queries from &lt;code&gt;pg_stat_activity&lt;/code&gt; — check &lt;code&gt;wait_event_type&lt;/code&gt; before anything else&lt;/li&gt;
&lt;li&gt;Identify any sessions with &lt;code&gt;wait_event_type = &apos;Lock&apos;&lt;/code&gt; and trace the blocking chain&lt;/li&gt;
&lt;li&gt;Pull top queries by &lt;code&gt;total_exec_time&lt;/code&gt; from &lt;code&gt;pg_stat_statements&lt;/code&gt; — distinguish outliers from throughput problems&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; on the specific slow query — look for Seq Scan and row estimate mismatches&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;pg_stat_user_tables&lt;/code&gt; for tables with stale &lt;code&gt;last_autoanalyze&lt;/code&gt; or high &lt;code&gt;n_dead_tup&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;If lock contention: terminate idle-in-transaction connections blocking others for more than two minutes&lt;/li&gt;
&lt;li&gt;If missing index: create with &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; — confirm plan change with &lt;code&gt;EXPLAIN&lt;/code&gt; afterward&lt;/li&gt;
&lt;li&gt;If stale statistics: run &lt;code&gt;ANALYZE&lt;/code&gt; on the affected table — always safe, non-blocking&lt;/li&gt;
&lt;li&gt;If bloat: run &lt;code&gt;VACUUM (VERBOSE, ANALYZE)&lt;/code&gt; — do not use &lt;code&gt;VACUUM FULL&lt;/code&gt; during an incident&lt;/li&gt;
&lt;li&gt;After resolving: lower &lt;code&gt;autovacuum_analyze_scale_factor&lt;/code&gt; on high-write tables to prevent recurrence&lt;/li&gt;
&lt;li&gt;Enable &lt;code&gt;auto_explain&lt;/code&gt; with &lt;code&gt;log_min_duration&lt;/code&gt; set to your slow query threshold&lt;/li&gt;
&lt;li&gt;Schedule a &lt;code&gt;pg_cron&lt;/code&gt; job to snapshot &lt;code&gt;pg_stat_activity&lt;/code&gt; for future post-incident timelines&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;what-this-post-does-not-cover&quot;&gt;What This Post Does Not Cover&lt;/h2&gt;
&lt;p&gt;This post covers triage of an active slow query incident. It does not cover: &lt;code&gt;pg_partman&lt;/code&gt; partition pruning for large tables, physical replication lag as a source of slow reads on replicas, connection pooler (PgBouncer) saturation that precedes the slow query symptom, or schema migration locking analysis. Each of those is a distinct failure mode with its own triage path.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: A slow query alert fires and the on-call engineer spends 30 minutes checking the wrong root cause — stale statistics were the issue, not the query they were tuning.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: Work through the five checks in order: current activity first, then historical aggregates, then lock contention, then statistics age, then the execution plan.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: Running &lt;code&gt;pg_stat_activity&lt;/code&gt; before touching anything else shows whether the incident is lock-driven within 60 seconds — that confirmation eliminates half the possible root causes immediately.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: Add &lt;code&gt;pg_stat_statements&lt;/code&gt; and &lt;code&gt;auto_explain&lt;/code&gt; to your PostgreSQL configuration this week; validate they are collecting data; add the five check queries to your team’s runbook.&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>checklist</category><category>failures</category></item><item><title>Cassandra Data Modeling Fundamentals: Design From Queries, Not Entities</title><link>https://rajivonai.com/blog/2021-08-03-cassandra-data-modeling-fundamentals-query-first/</link><guid isPermaLink="true">https://rajivonai.com/blog/2021-08-03-cassandra-data-modeling-fundamentals-query-first/</guid><description>Why Cassandra schemas must be designed backward from access patterns rather than forward from entities, and how partition keys, clustering columns, and denormalization actually behave in production.</description><pubDate>Tue, 03 Aug 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;A Cassandra schema that mirrors an entity-relationship diagram is unlikely to survive production traffic.&lt;/strong&gt; Cassandra does not provide relational joins or automatic indexes over arbitrary columns. Tables should be designed for named access patterns, consistency requirements, and bounded result sets. The partition key determines data placement and whether a request can route to a bounded replica set.&lt;/p&gt;
&lt;h2 id=&quot;situation&quot;&gt;Situation&lt;/h2&gt;
&lt;p&gt;Apache Cassandra 4.0 became generally available in July 2021 while established estates still operated 3.11. The durable lesson from that transition is version-independent: engineers arriving from PostgreSQL, MySQL, or Oracle often bring an entity-first design sequence to a database whose predictable read path depends on the partition key.&lt;/p&gt;
&lt;p&gt;The failure is not ignorance of CQL syntax — &lt;code&gt;CREATE TABLE&lt;/code&gt;, &lt;code&gt;PRIMARY KEY&lt;/code&gt;, and &lt;code&gt;SELECT&lt;/code&gt; all look reassuringly familiar. The failure is applying relational design order: model the entities first, normalize to remove duplication, and let the query layer (&lt;code&gt;JOIN&lt;/code&gt;, secondary indexes, ad hoc &lt;code&gt;WHERE&lt;/code&gt; clauses) figure out access later. Cassandra has none of the machinery that makes that order viable. It has a distributed hash table with clustered rows bolted on, and a design process that runs in the opposite direction.&lt;/p&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;Entity-first (relational instinct)&lt;/th&gt;&lt;th&gt;Query-first (Cassandra requirement)&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Design starts from&lt;/td&gt;&lt;td&gt;Nouns: &lt;code&gt;customers&lt;/code&gt;, &lt;code&gt;orders&lt;/code&gt;, &lt;code&gt;order_items&lt;/code&gt;&lt;/td&gt;&lt;td&gt;Verbs: “get order by id,” “get a customer’s order history”&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Normalization&lt;/td&gt;&lt;td&gt;Remove duplication, join at read time&lt;/td&gt;&lt;td&gt;Duplicate deliberately, denormalize at write time&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Where correctness lives&lt;/td&gt;&lt;td&gt;Foreign keys, referential integrity, transactions&lt;/td&gt;&lt;td&gt;Application write path, idempotent fan-out&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Failure mode when wrong&lt;/td&gt;&lt;td&gt;Slow joins, missing indexes — usually fixable post hoc&lt;/td&gt;&lt;td&gt;Full cluster scan per query — usually requires a schema migration&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;Take a conventional e-commerce domain: customers, orders, and order line items. A relational schema for this is unremarkable:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Relational (PostgreSQL-style) baseline&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; customers&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    customer_id uuid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    email &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id uuid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    customer_id uuid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;REFERENCES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; customers(customer_id),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; text&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_date &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    total &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; order_items&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_item_id uuid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id uuid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;REFERENCES&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders(order_id),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    product_id uuid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    quantity &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; NOT NULL&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    unit_price &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;numeric&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;NOT NULL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The application needs to answer at least five questions against this data: fetch one order by ID, list a customer’s order history, list orders currently in a given fulfillment status, list orders placed on a given date for reporting, and fetch the line items for an order. In PostgreSQL, all five are &lt;code&gt;SELECT&lt;/code&gt; statements with the right index or join — the schema doesn’t change shape based on the query.&lt;/p&gt;
&lt;p&gt;A team new to Cassandra frequently ports this schema almost unchanged, then bridges the missing query flexibility with &lt;code&gt;ALLOW FILTERING&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Naive Cassandra port of the relational schema&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; orders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id uuid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    customer_id uuid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; text&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_date &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    total &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;decimal&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- &quot;Give me this customer&apos;s orders&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; customer_id &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ? ALLOW FILTERING;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- &quot;Give me everything pending fulfillment&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;SELECT&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; FROM&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; orders &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WHERE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &apos;PENDING&apos;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ALLOW FILTERING;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This looks reasonable for a specific reason: it compiles, it returns correct rows, and in a development environment with a few hundred synthetic orders it is fast enough that nobody notices anything wrong. That is exactly the trap — Cassandra will let you write a query with no relationship to its physical data layout, and it will not tell you it was a mistake until the partition count on that table crosses into the hundreds of thousands.&lt;/p&gt;
&lt;p&gt;Here is why it fails once real traffic arrives. &lt;code&gt;orders&lt;/code&gt; has &lt;code&gt;order_id&lt;/code&gt; as its partition key, so Cassandra hashes that value and places the row in its token range. &lt;code&gt;customer_id&lt;/code&gt; and &lt;code&gt;status&lt;/code&gt; are regular columns with no routing role. Without a usable partition-key or index restriction, Cassandra may have to scan partitions across the ring and apply server-side filtering. The CQL reference deliberately requires &lt;code&gt;ALLOW FILTERING&lt;/code&gt; because performance can depend on the total data scanned even when the result is small. It is an explicit acceptance of unpredictable work, not a tuning hint. The core question this forces is: if a table can only be read predictably through the key it was designed for, how many tables does one domain need?&lt;/p&gt;
&lt;h2 id=&quot;query-first-table-design&quot;&gt;Query-First Table Design&lt;/h2&gt;
&lt;p&gt;The answer, in Cassandra, is one table per access pattern. Before writing a single &lt;code&gt;CREATE TABLE&lt;/code&gt; statement, the design process is: enumerate every query the application will run against this data, including read frequency and expected result size, and only then decide what the partition key and clustering columns for each query need to be.&lt;/p&gt;
&lt;p&gt;For the order domain above, that inventory looks like this:&lt;/p&gt;



































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Access pattern&lt;/th&gt;&lt;th&gt;Frequency&lt;/th&gt;&lt;th&gt;Result shape&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Get one order by order ID&lt;/td&gt;&lt;td&gt;High (order detail page)&lt;/td&gt;&lt;td&gt;Single row&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Get a customer’s order history, newest first&lt;/td&gt;&lt;td&gt;High (account page)&lt;/td&gt;&lt;td&gt;Bounded list per customer&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Get orders in a given fulfillment status&lt;/td&gt;&lt;td&gt;Medium (ops/warehouse tooling)&lt;/td&gt;&lt;td&gt;Unbounded list, keeps growing&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Get orders placed on a given date&lt;/td&gt;&lt;td&gt;Low (daily reporting)&lt;/td&gt;&lt;td&gt;Bounded list per day&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Get line items for an order&lt;/td&gt;&lt;td&gt;High (order detail page)&lt;/td&gt;&lt;td&gt;Small bounded list per order&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Each row in that table becomes its own Cassandra table, denormalizing the order attributes into each one:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;mermaid&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;flowchart TD&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    R[Relational model — customers orders order_items] --&gt; Q{Enumerate access patterns}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Q --&gt; T1[orders_by_id — order detail lookup]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Q --&gt; T2[orders_by_customer — order history]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Q --&gt; T3[orders_by_status — fulfillment queue]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Q --&gt; T4[orders_by_date — daily reporting]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    Q --&gt; T5[order_items_by_order — line items]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;sql&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Query 1: order detail lookup by order_id&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; orders_by_id&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id uuid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    customer_id uuid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_date &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; text&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    total &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;decimal&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    shipping_address &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Query 2: a customer&apos;s order history, newest first&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; orders_by_customer&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    customer_id uuid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id uuid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; text&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    total &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;decimal&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ((customer_id), order_time, order_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CLUSTERING &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (order_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Query 3: fulfillment queue by status — flagged, not finished (see below)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; orders_by_status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; text&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id uuid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    customer_id uuid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    total &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;decimal&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ((&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), order_time, order_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CLUSTERING &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (order_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Query 4: daily reporting&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; orders_by_date&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_date &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;timestamp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id uuid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    customer_id uuid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    status&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; text&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    total &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;decimal&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ((order_date), order_time, order_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; CLUSTERING &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;ORDER BY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (order_time &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D&quot;&gt;-- Query 5: line items for an order&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;CREATE&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; TABLE&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; order_items_by_order&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    order_id uuid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    line_no &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    product_id uuid,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    product_name &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    quantity &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    unit_price &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;decimal&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    PRIMARY KEY&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; ((order_id), line_no)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Walk through what each part of the primary key is actually doing, because this is the piece relational engineers most often misread:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Partition key — &lt;code&gt;(customer_id)&lt;/code&gt;, &lt;code&gt;(status)&lt;/code&gt;, &lt;code&gt;(order_date)&lt;/code&gt;, &lt;code&gt;(order_id)&lt;/code&gt;.&lt;/strong&gt; This is the only part of the key the partitioner hashes. It determines which node(s) own the data and is the only column CQL can use to route a read to a bounded set of replicas instead of scanning the ring. Get this wrong and every other part of the design is cosmetic.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Clustering columns — &lt;code&gt;order_time, order_id&lt;/code&gt;.&lt;/strong&gt; These determine the on-disk sort order of rows &lt;em&gt;within&lt;/em&gt; a partition. They don’t affect routing at all — a query still needs the partition key — but they let you range-scan and paginate within a partition without filtering in memory (e.g., “give me this customer’s last 20 orders” is a clustering-key range scan, not a filter).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;CLUSTERING ORDER BY (order_time DESC)&lt;/code&gt;.&lt;/strong&gt; Cassandra writes and compacts rows in clustering order on disk; declaring descending order at table creation means “most recent first” reads are a forward scan (cheap), not a reverse scan (expensive) or an application-side sort.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The trailing &lt;code&gt;order_id&lt;/code&gt; in &lt;code&gt;(customer_id), order_time, order_id&lt;/code&gt;.&lt;/strong&gt; &lt;code&gt;order_time&lt;/code&gt; alone is not guaranteed unique — two orders from the same customer in the same millisecond would collide without it. Appending the natural unique ID makes the clustering key deterministic.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Read path and write path for &lt;code&gt;orders_by_customer&lt;/code&gt;: a read for “this customer’s orders” routes to the replicas that own the hash of &lt;code&gt;customer_id&lt;/code&gt; and range-scans clustering rows from that partition. A write mutates that same logical partition. Compare that bounded route to the naive table’s non-partition-key filter, whose work can grow with data outside the requested customer’s result set.&lt;/p&gt;
&lt;p&gt;Notice that &lt;code&gt;orders_by_status&lt;/code&gt; is marked “flagged, not finished.” That is deliberate: a partition key with a handful of fixed values (&lt;code&gt;PENDING&lt;/code&gt;, &lt;code&gt;PAID&lt;/code&gt;, &lt;code&gt;SHIPPED&lt;/code&gt;, &lt;code&gt;DELIVERED&lt;/code&gt;, &lt;code&gt;CANCELLED&lt;/code&gt;) gives correct routing but terrible cardinality. Historical rows for one status accumulate on one replica set. A production design must bucket that key using measured arrival rate and bounded read fan-out, then explicitly delete the old-status row when an order transitions. Updating only the new partition leaves stale queue entries.&lt;/p&gt;
&lt;h2 id=&quot;why-the-naive-design-fails&quot;&gt;Why the Naive Design Fails&lt;/h2&gt;
&lt;p&gt;The mechanism is worth stating precisely, because “don’t use &lt;code&gt;ALLOW FILTERING&lt;/code&gt;” as a rule of thumb without the mechanism behind it gets argued with in code review. Cassandra’s CQL execution model, per the official CQL reference, routes a query using only the partition key restriction present in the &lt;code&gt;WHERE&lt;/code&gt; clause. When that restriction is absent — as it is for &lt;code&gt;customer_id&lt;/code&gt; or &lt;code&gt;status&lt;/code&gt; against the &lt;code&gt;orders&lt;/code&gt; table’s actual partition key of &lt;code&gt;order_id&lt;/code&gt; — the coordinator cannot determine which nodes hold matching data, because matching rows are hashed by &lt;code&gt;order_id&lt;/code&gt; and distributed with no relationship to &lt;code&gt;customer_id&lt;/code&gt; or &lt;code&gt;status&lt;/code&gt; at all. The only correct execution plan left is to query every token range and evaluate the filter against every row returned, which is exactly what &lt;code&gt;ALLOW FILTERING&lt;/code&gt; authorizes the coordinator to do.&lt;/p&gt;
&lt;p&gt;This cost is invisible in development for one reason: it scales with total table size, not with query selectivity. A hundred orders across the whole cluster and a hundred thousand orders per day look identical in code review — the query is unchanged, the explain-plan-equivalent (Cassandra tracing) is unchanged in shape — only the wall-clock cost changes, and it changes linearly with data volume the team has been adding the entire time the query “worked fine.”&lt;/p&gt;
&lt;h2 id=&quot;why-this-design-scales-better&quot;&gt;Why This Design Scales Better&lt;/h2&gt;
&lt;p&gt;The query-first tables above scale because each one’s cost is bounded by the partition it touches, not by the table’s total size. &lt;code&gt;orders_by_customer&lt;/code&gt; at ten million total orders costs the same per-read as it did at ten thousand, provided per-customer order counts don’t grow unbounded (they can — see Article 2 for bucketing that partition by time when they do). &lt;code&gt;orders_by_id&lt;/code&gt; and &lt;code&gt;order_items_by_order&lt;/code&gt; are effectively flat-cost regardless of table size, because each is a single-partition point lookup or a small bounded range scan.&lt;/p&gt;
&lt;p&gt;The price paid for this is storage and write amplification: the same order exists, in some form, in four tables (&lt;code&gt;orders_by_id&lt;/code&gt;, &lt;code&gt;orders_by_customer&lt;/code&gt;, &lt;code&gt;orders_by_status&lt;/code&gt;, &lt;code&gt;orders_by_date&lt;/code&gt;) plus its line items in a fifth. That is not accidental duplication to normalize away later—it is the data model. Keeping those representations aligned requires an idempotent fan-out design, version-aware repair, and explicit handling for updates and deletes. A logged batch is atomic only within Cassandra’s documented batch semantics; it is not a substitute for arbitrary workflow transactions.&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;In Practice&lt;/h2&gt;
&lt;p&gt;Context: the Apache Cassandra CQL reference documents &lt;code&gt;ALLOW FILTERING&lt;/code&gt; as a required clause specifically because, without a partition key restriction, the query “may involve a lot of data and be too resource-intensive” to run without explicit authorization, and it explicitly separates queries that can be served from the partition index from queries that require this fallback scan.&lt;/p&gt;
&lt;p&gt;Action: teams porting relational schemas typically discover this the same way — by running the exact query pattern above (&lt;code&gt;WHERE non_partition_column = ?&lt;/code&gt;) against a table whose partition key is something else, hitting &lt;code&gt;InvalidRequest: ... requires ALLOW FILTERING&lt;/code&gt;, adding the clause to make the error go away, and moving on because the query returns correct results in a small dataset.&lt;/p&gt;
&lt;p&gt;Result: for the unindexed example above, the coordinator can require work across token ranges and filter rows after reading them. The amount of examined data can grow with the table even when the returned set stays small. The precise cost depends on topology, data distribution, paging, and version; the documentation intentionally refuses to promise a bounded plan.&lt;/p&gt;
&lt;p&gt;Learning: I have not benchmarked this specific order schema against a specific cluster size — the point isn’t a number, it’s that the query and the table’s partition key have to agree before the query is written, because Cassandra will not stop you from writing one that doesn’t.&lt;/p&gt;
&lt;h2 id=&quot;where-it-breaks&quot;&gt;Where It Breaks&lt;/h2&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Failure mode&lt;/th&gt;&lt;th&gt;Trigger&lt;/th&gt;&lt;th&gt;Fix&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;ALLOW FILTERING&lt;/code&gt; query degrades from milliseconds to seconds&lt;/td&gt;&lt;td&gt;Table grows past what fits comfortably in page cache across all nodes&lt;/td&gt;&lt;td&gt;Replace with a query-specific table keyed on the actual filter column&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;orders_by_status&lt;/code&gt; partition grows without bound&lt;/td&gt;&lt;td&gt;&lt;code&gt;PENDING&lt;/code&gt; orders accumulate faster than they transition out of that status&lt;/td&gt;&lt;td&gt;Bucket the partition key by time (&lt;code&gt;(status, order_month)&lt;/code&gt;) — see Article 2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Denormalized tables drift out of sync&lt;/td&gt;&lt;td&gt;Application fan-out write fails partway (one table succeeds, another doesn’t)&lt;/td&gt;&lt;td&gt;Idempotent, retry-safe writes and an explicit consistency story — see Article 5&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Schema “works” through QA and breaks in production&lt;/td&gt;&lt;td&gt;Test data volumes never exercise the partition-count or row-count regime production reaches&lt;/td&gt;&lt;td&gt;Load-test with production-representative cardinality before sign-off&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;operational-signals&quot;&gt;Operational Signals&lt;/h2&gt;
&lt;p&gt;The earliest signal that a table’s key design does not match its query is usually a latency and work curve, not a new error. Trend p99 read latency, rows scanned, tombstones scanned, coordinator latency, and timeouts against data growth. Use CQL tracing only for sampled diagnosis because tracing itself has overhead. The relevant warning sign is a request whose examined data and contacted ranges grow while its returned result stays small.&lt;/p&gt;
&lt;h2 id=&quot;migration--remediation-strategy&quot;&gt;Migration / Remediation Strategy&lt;/h2&gt;
&lt;p&gt;Fixing this in production is a schema migration, not a query rewrite, which is why catching it in design review is worth the time it costs. The standard path: stand up the new query-specific table, backfill it from the existing data (batch job or dual-write from the application), cut reads over once backfill is verified complete and current, then decommission the old access path once nothing references it. There is no &lt;code&gt;ALTER TABLE&lt;/code&gt; that turns a poorly-keyed table into a well-keyed one — the partition key is fixed at creation because it determines physical data placement across the ring.&lt;/p&gt;
&lt;h2 id=&quot;2025-update&quot;&gt;2025 Update&lt;/h2&gt;
&lt;p&gt;Storage-Attached Indexing (SAI) in Cassandra 5.0 changes the cost of indexing a non-partition-key column. SAI indexes memtables and SSTables and supports documented equality, range, and collection predicates. That is a genuine capability increase; see the &lt;a href=&quot;https://cassandra.apache.org/doc/latest/cassandra/developing/cql/indexing/sai/sai-overview.html&quot;&gt;Apache Cassandra SAI overview&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;What SAI does not change is the need to reason about routing and selectivity. When a query omits a partition key, the coordinator scans token ranges for indexed matches before reading matching partitions. SAI can replace some query-specific tables, but its suitability depends on cardinality, selectivity, result size, and workload tests. Primary access patterns should still prefer predictable partition-key routing. &lt;a href=&quot;https://cassandra.apache.org/doc/latest/cassandra/developing/cql/indexing/sai/sai-faq.html&quot;&gt;Apache Cassandra’s SAI FAQ&lt;/a&gt; describes the token-range behavior explicitly.&lt;/p&gt;
&lt;h2 id=&quot;design-review-checklist&quot;&gt;Design Review Checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Every table’s queries are enumerated &lt;em&gt;before&lt;/em&gt; the schema is written, with expected frequency and result size for each.&lt;/li&gt;
&lt;li&gt;The partition key for each table is the dominant filter of its one intended query — not the entity’s natural primary key by default.&lt;/li&gt;
&lt;li&gt;Any &lt;code&gt;WHERE&lt;/code&gt; clause on a column that isn’t the partition key is either a clustering-column range scan (fine) or a signal that a dedicated table is missing (not fine).&lt;/li&gt;
&lt;li&gt;Any partition key with low, fixed cardinality (a status enum, a boolean, a small lookup category) is checked against expected row growth per value before it ships — see Article 2.&lt;/li&gt;
&lt;li&gt;Denormalized copies of the same logical entity have an explicit, tested write path for keeping them consistent — not an assumption that they will.&lt;/li&gt;
&lt;li&gt;Load tests use production-representative cardinality and partition counts, not a QA-sized dataset that happens to hide a full-cluster scan.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h2&gt;
&lt;p&gt;Cassandra’s data model is a physical placement decision disguised as a schema. The partition key isn’t a modeling convenience — it is the mechanism that determines which of the cluster’s nodes will ever be asked to do work for a given query. Design backward from the queries you actually run, accept duplication as the price of that design, and treat any query that needs a column outside the partition key as a signal to build another table, not another &lt;code&gt;WHERE&lt;/code&gt; clause.&lt;/p&gt;
&lt;h2 id=&quot;what-to-do-next&quot;&gt;What to Do Next&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem&lt;/strong&gt;: relational entity modeling produces Cassandra schemas that only work at development-sized data volumes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;: enumerate access patterns first and build one table per query, denormalizing deliberately.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proof&lt;/strong&gt;: the documented CQL execution model makes partition-key-routed work predictable, while filtering without a usable routing or index restriction can depend on total data scanned.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action&lt;/strong&gt;: pull up your current highest-traffic Cassandra table this week and check whether any production query against it requires &lt;code&gt;ALLOW FILTERING&lt;/code&gt; or a non-partition-key restriction — if it does, it already has a redesign due.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Apache Cassandra CQL reference — &lt;a href=&quot;https://cassandra.apache.org/doc/stable/cassandra/developing/cql/cql_singlefile.html&quot;&gt;&lt;code&gt;ALLOW FILTERING&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Apache Cassandra project — &lt;a href=&quot;https://cassandra.apache.org/_/blog/Apache-Cassandra-4.0-is-Here.html&quot;&gt;Cassandra 4.0 release announcement&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Apache Cassandra documentation — &lt;a href=&quot;https://cassandra.apache.org/doc/latest/cassandra/developing/cql/indexing/indexing-concepts.html&quot;&gt;indexing concepts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Apache Cassandra documentation — &lt;a href=&quot;https://cassandra.apache.org/doc/latest/cassandra/developing/cql/indexing/sai/sai-query.html&quot;&gt;querying with SAI&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content:encoded><category>databases</category><category>architecture</category></item></channel></rss>