forked from mtusi2026/mtusi2026lab2
Series B located the bottleneck. At eight replicas the API has 40 % of its CPU spare and the balancer 60 % of a core, yet three quarters of the pool connections wait for a chat row lock: the locking SELECT of the chat averages 14.2 ms and the lock holders sit idle in transaction while the replica's event loop gets round to the next statement. The critical section, three round trips long, is what more replicas lengthen. The design now records the choice: one CTE that locks the chat with the membership check, stamps the message no earlier than the last one, claims the idempotency key and inserts the message, committed by the statement itself, so the lock never waits for the application. A taken key is read back by a second statement outside the lock. The price is that the chat aggregate no longer guards posting; the rules move into the adapter's SQL and are held by integration tests. The rejected alternatives and the before/after plan are listed, and the spec gains the observable requirement of a short critical section. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V1d3RLHNBBMA6UqBLLJv27