Choose a one-statement message write as the track 1 improvement

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
This commit is contained in:
Artyom
2026-10-29 01:06:10 +03:00
co-authored by Claude Opus 5.5
parent 3bdc8fad23
commit edc091dbc4
3 changed files with 29 additions and 2 deletions
@@ -19,7 +19,7 @@
## 4. Улучшение
- [ ] 4.1 Выбор улучшения по узкому месту серии B, запись в `design.md`
- [x] 4.1 Выбор улучшения по узкому месту серии B, запись в `design.md`: запись сообщения одним оператором SQL
- [ ] 4.2 Реализация улучшения
- [ ] 4.3 `make track1-compare`: до и после на той же конфигурации и интенсивностях, ≥3 повтора
- [ ] 4.4 Проверка группы: `make lint`, `make student-test`, `make test`