Не доверять кэшу сессий после рестарта API

Флаг очистки кэша жил только в памяти процесса. Сценарий: logout при
недоступном Valkey (отзыв записан в PostgreSQL, tombstone нет) -> рестарт
API -> Valkey вернулся. Новый процесс читал устаревшую положительную запись,
и отозванный токен получал 200 на /users/me и 201 на отправку сообщения
до истечения TTL записи (до 60 s).

Теперь процесс стартует с включённым флагом и выполняет FLUSHDB перед
первым использованием кэша. Добавлен регрессионный тест
test_logout_during_outage_then_api_restart, evidence и отчёт обновлены.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-03 10:31:12 +03:00
co-authored by Claude Opus 5.5
parent 8e97518120
commit ed012ba09d
9 changed files with 126 additions and 81 deletions
+1 -1
View File
@@ -31,7 +31,7 @@
## Нагрузка
Профиль: 4 concurrent клиента, 15 seconds, closed-loop `POST /api/v1/chats/{chat_id}/messages`, без automatic retries.
Результат: 3324 успешных запроса, 0 ошибок, ≈221 RPS, p50/p95/p99 = 15.8/30.1/59.6 ms.
Результат: 4776 успешных запросов, 0 ошибок, ≈318 RPS, p50/p95/p99 = 12.1/15.6/19.4 ms.
Это локальное измерение на Docker Desktop/WSL2, а не production SLA.
Latency включает client overhead; closed-loop нагрузка подвержена coordinated omission.
+31 -31
View File
@@ -5,64 +5,64 @@ PASS: chat row was rolled back together with members
PASS: no chats without members exist in the database
== EXPLAIN for message history pagination
messages total: 65588; in explained chat: 10000
messages total: 50010; in explained chat: 10000
PASS: explained chat has >= 10 000 messages
-- first page
Limit (actual time=0.010..0.016 rows=51 loops=1)
Limit (actual time=0.011..0.018 rows=51 loops=1)
Buffers: shared hit=4
-> Index Scan using messages_keyset_idx on messages (actual time=0.010..0.013 rows=51 loops=1)
Index Cond: (chat_id = '7c57b8e9-42a1-4cf8-904b-08545328a890'::uuid)
-> Index Scan using messages_keyset_idx on messages (actual time=0.010..0.014 rows=51 loops=1)
Index Cond: (chat_id = 'bc6206c8-0e5d-48f3-94ea-a5fe15e0a9e5'::uuid)
Buffers: shared hit=4
Planning:
Buffers: shared hit=6
Planning Time: 0.084 ms
Execution Time: 0.034 ms
Buffers: shared hit=8
Planning Time: 0.075 ms
Execution Time: 0.033 ms
-- keyset page after position 9000
Limit (actual time=0.013..0.022 rows=51 loops=1)
Limit (actual time=0.083..0.093 rows=51 loops=1)
Buffers: shared hit=5
-> Index Scan using messages_keyset_idx on messages (actual time=0.012..0.018 rows=51 loops=1)
Index Cond: ((chat_id = '7c57b8e9-42a1-4cf8-904b-08545328a890'::uuid) AND (ROW(created_at, id) > ROW('2026-10-01 07:31:21.6187+00'::timestamp with time zone, '6cbb1e03-f1e2-4113-8a7b-612c90c9761f'::uuid)))
-> Index Scan using messages_keyset_idx on messages (actual time=0.082..0.089 rows=51 loops=1)
Index Cond: ((chat_id = 'bc6206c8-0e5d-48f3-94ea-a5fe15e0a9e5'::uuid) AND (ROW(created_at, id) > ROW('2026-10-01 19:08:45.800971+00'::timestamp with time zone, '6f9dad83-2a94-400a-877b-7202790747a3'::uuid)))
Buffers: shared hit=5
Planning Time: 0.057 ms
Execution Time: 0.157 ms
Planning Time: 0.040 ms
Execution Time: 0.149 ms
-- OFFSET 9000 (not used by the API)
Limit (actual time=4.069..4.075 rows=51 loops=1)
Limit (actual time=3.359..3.364 rows=51 loops=1)
Buffers: shared hit=208
-> Sort (actual time=3.561..3.851 rows=9051 loops=1)
-> Sort (actual time=2.897..3.166 rows=9051 loops=1)
Sort Key: created_at, id
Sort Method: quicksort Memory: 1713kB
Buffers: shared hit=208
-> Bitmap Heap Scan on messages (actual time=0.928..1.508 rows=10000 loops=1)
Recheck Cond: (chat_id = '7c57b8e9-42a1-4cf8-904b-08545328a890'::uuid)
-> Bitmap Heap Scan on messages (actual time=0.209..0.786 rows=10000 loops=1)
Recheck Cond: (chat_id = 'bc6206c8-0e5d-48f3-94ea-a5fe15e0a9e5'::uuid)
Heap Blocks: exact=134
Buffers: shared hit=208
-> Bitmap Index Scan on messages_keyset_idx (actual time=0.913..0.914 rows=10000 loops=1)
Index Cond: (chat_id = '7c57b8e9-42a1-4cf8-904b-08545328a890'::uuid)
-> Bitmap Index Scan on messages_keyset_idx (actual time=0.194..0.194 rows=10000 loops=1)
Index Cond: (chat_id = 'bc6206c8-0e5d-48f3-94ea-a5fe15e0a9e5'::uuid)
Buffers: shared hit=74
Planning Time: 0.070 ms
Execution Time: 4.209 ms
Planning Time: 0.048 ms
Execution Time: 3.476 ms
-- keyset page WITHOUT messages_keyset_idx (rolled back)
Limit (actual time=2.486..2.492 rows=51 loops=1)
Buffers: shared hit=877
-> Sort (actual time=2.484..2.486 rows=51 loops=1)
Limit (actual time=1.986..1.991 rows=51 loops=1)
Buffers: shared hit=667
-> Sort (actual time=1.985..1.986 rows=51 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 31kB
Buffers: shared hit=877
-> Seq Scan on messages (actual time=0.376..2.401 rows=999 loops=1)
Filter: ((chat_id = '7c57b8e9-42a1-4cf8-904b-08545328a890'::uuid) AND (ROW(created_at, id) > ROW('2026-10-01 07:31:21.6187+00'::timestamp with time zone, '6cbb1e03-f1e2-4113-8a7b-612c90c9761f'::uuid)))
Rows Removed by Filter: 64589
Buffers: shared hit=877
Buffers: shared hit=667
-> Seq Scan on messages (actual time=0.398..1.897 rows=999 loops=1)
Filter: ((chat_id = 'bc6206c8-0e5d-48f3-94ea-a5fe15e0a9e5'::uuid) AND (ROW(created_at, id) > ROW('2026-10-01 19:08:45.800971+00'::timestamp with time zone, '6f9dad83-2a94-400a-877b-7202790747a3'::uuid)))
Rows Removed by Filter: 49011
Buffers: shared hit=667
Planning:
Buffers: shared hit=4
Planning Time: 0.107 ms
Execution Time: 2.537 ms
Planning Time: 0.109 ms
Execution Time: 2.019 ms
PASS: first page reads the index in order, no Sort
PASS: keyset cursor is an Index Cond, not a filter over the history
PASS: keyset deep page touches fewer buffers than OFFSET (5 < 208)
PASS: index reduces buffers for the deep page (5 < 877)
PASS: index reduces buffers for the deep page (5 < 667)
PASS: db checks
+9 -1
View File
@@ -38,7 +38,7 @@ PASS: forged token still 401 without Valkey
PASS: anonymous still 401 without Valkey
PASS: logout works without Valkey
PASS: token revoked during outage -> 401
PASS: bounded latency during outage (max 0.27s)
PASS: bounded latency during outage (max 0.26s)
PASS: token revoked during outage stays 401 after Valkey returns (stale cache flushed)
PASS: live session works after recovery
@@ -55,6 +55,14 @@ PASS: bounded latency during outage (max 0.26s)
PASS: token revoked during outage stays 401 after Valkey returns (stale cache flushed)
PASS: live session works after recovery
== logout while Valkey hangs, API restart, Valkey returns
PASS: token cached before outage
PASS: positive cache entry exists in Valkey
PASS: logout committed in PostgreSQL, tombstone not written
PASS: API restarted while Valkey is unreachable
PASS: revoked token -> 401 after API restart (got 401)
PASS: revoked token cannot send messages (got 401)
== PostgreSQL stop: limited 503, anonymous is never trusted
PASS: token cached before outage
PASS: write -> 503 unavailable with request id (3.01s)
+6 -6
View File
@@ -2,16 +2,16 @@
"lab": 2,
"workload": "closed-loop POST messages; no automatic retries",
"requested_duration_seconds": 15.0,
"elapsed_seconds": 15.02,
"elapsed_seconds": 15.018,
"concurrency": 4,
"successful_requests": 4278,
"successful_requests": 4776,
"failed_requests": 0,
"error_ratio": 0.0,
"successful_rps": 284.815,
"successful_rps": 318.026,
"successful_latency_ms": {
"p50": 12.842,
"p95": 21.09,
"p99": 31.042
"p50": 12.148,
"p95": 15.627,
"p99": 19.403
},
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
}
+6 -6
View File
@@ -2,16 +2,16 @@
"lab": 2,
"workload": "closed-loop POST messages; no automatic retries",
"requested_duration_seconds": 15.0,
"elapsed_seconds": 15.02,
"elapsed_seconds": 15.018,
"concurrency": 4,
"successful_requests": 4278,
"successful_requests": 4776,
"failed_requests": 0,
"error_ratio": 0.0,
"successful_rps": 284.815,
"successful_rps": 318.026,
"successful_latency_ms": {
"p50": 12.842,
"p95": 21.09,
"p99": 31.042
"p50": 12.148,
"p95": 15.627,
"p99": 19.403
},
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
}
+10 -10
View File
@@ -1,14 +1,14 @@
## containers before restart hook
api running Up 8 minutes 2026-10-02 09:54:27 +0300 MSK
api_short_ttl running Up 8 minutes 2026-10-02 09:54:27 +0300 MSK
db running Up 7 minutes (healthy) 2026-10-02 09:39:03 +0300 MSK
migrate exited Exited (0) 8 minutes ago 2026-10-02 09:54:26 +0300 MSK
valkey running Up 7 minutes (healthy) 2026-10-02 09:47:53 +0300 MSK
api running Up 24 seconds 2026-10-03 10:27:58 +0300 MSK
api_short_ttl running Up 32 seconds 2026-10-03 10:27:58 +0300 MSK
db running Up About a minute (healthy) 2026-10-02 21:00:15 +0300 MSK
migrate exited Exited (0) 33 seconds ago 2026-10-03 10:27:58 +0300 MSK
valkey running Up About a minute (healthy) 2026-10-02 21:09:53 +0300 MSK
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
## containers after restart hook (Status shows new uptime)
api running Up 2 seconds 2026-10-02 09:54:27 +0300 MSK
api_short_ttl running Up 8 minutes 2026-10-02 09:54:27 +0300 MSK
db running Up 3 seconds (health: starting) 2026-10-02 09:39:03 +0300 MSK
migrate exited Exited (0) 8 minutes ago 2026-10-02 09:54:26 +0300 MSK
valkey running Up 3 seconds (health: starting) 2026-10-02 09:47:53 +0300 MSK
api running Up 2 seconds 2026-10-03 10:27:58 +0300 MSK
api_short_ttl running Up 37 seconds 2026-10-03 10:27:58 +0300 MSK
db running Up 3 seconds (health: starting) 2026-10-02 21:00:15 +0300 MSK
migrate exited Exited (0) 37 seconds ago 2026-10-03 10:27:58 +0300 MSK
valkey running Up 3 seconds (health: starting) 2026-10-02 21:09:53 +0300 MSK