forked from mtusi2026/mtusi2026lab2
Уровень 4: - Redis как кэш сессий (cache-aside, источник истины — PostgreSQL): TTL <= остатка сессии, метка revoked при logout + SET NX против гонки, при отказе Redis — работа через базу и фоновая проверка доступности - тесты: гонка регистрации, откат посреди транзакции, недоступность БД и Redis (44 теста) - EXPLAIN (ANALYZE, BUFFERS) на 22 000 сообщений, history_query общий для API и скрипта - эксперименты отказа Redis/PostgreSQL под нагрузкой; dns_opt у API, чтобы отказ БД давал быстрый 503, а не зависание на поиске имени остановленного контейнера Уровень 5, трек 2: - 105 000 сообщений в неравномерных чатах, 4 варианта индекса + OFFSET против keyset, стоимость записи (время, WAL), два независимых прогона - pg_dump -> pg_restore в новый контейнер -> сверка md5 всех таблиц и выборки сообщений, RTO 14 с, демонстрация RPO
91 lines
5.2 KiB
Plaintext
91 lines
5.2 KiB
Plaintext
# 2026-10-29T10:48:16Z PostgreSQL 17.10
|
|
# messages: 22024 rows total, 5560 kB with indexes; big chat 12000, 20 small chats x 500; ANALYZE done
|
|
# indexes on messages:
|
|
# CREATE INDEX messages_chat_history_idx ON public.messages USING btree (chat_id, created_at, id)
|
|
# CREATE UNIQUE INDEX messages_pkey ON public.messages USING btree (id)
|
|
|
|
# SUMMARY (top node, rows removed by filter, buffers of the top node, execution time)
|
|
# 1. big chat, first page (no cursor): Index Scan using messages_chat_history_idx; removed=0; buffers hit=5 read=0; 0.237 ms
|
|
# 2. big chat, middle page (cursor after #6000): Index Scan using messages_chat_history_idx; removed=0; buffers hit=5 read=0; 0.282 ms
|
|
# 3. big chat, last page (cursor after #11949): Index Scan using messages_chat_history_idx; removed=0; buffers hit=5 read=0; 0.241 ms
|
|
# 4. small chat, first page: Index Scan using messages_chat_history_idx; removed=0; buffers hit=6 read=0; 0.226 ms
|
|
# 5. for comparison: OFFSET pagination to the same depth as case 3 (NOT used by the API): Seq Scan on messages; removed=10024; buffers hit=296 read=0; 12.874 ms
|
|
|
|
## 1. big chat, first page (no cursor)
|
|
-- SQL generated by the API (history_query):
|
|
SELECT messages.id, messages.chat_id, messages.sender_id, messages.created_at, messages.text
|
|
FROM messages
|
|
WHERE messages.chat_id = %(chat_id_1)s::UUID ORDER BY messages.created_at, messages.id
|
|
LIMIT %(param_1)s::INTEGER
|
|
|
|
Limit (cost=0.41..8.65 rows=51 width=79) (actual time=0.054..0.075 rows=51 loops=1)
|
|
Buffers: shared hit=5
|
|
-> Index Scan using messages_chat_history_idx on messages (cost=0.41..1938.38 rows=12000 width=79) (actual time=0.052..0.066 rows=51 loops=1)
|
|
Index Cond: (chat_id = '46e81345-6190-4647-a359-24a7b2b54811'::uuid)
|
|
Buffers: shared hit=5
|
|
Planning:
|
|
Buffers: shared hit=6
|
|
Planning Time: 0.548 ms
|
|
Execution Time: 0.237 ms
|
|
|
|
## 2. big chat, middle page (cursor after #6000)
|
|
-- SQL generated by the API (history_query):
|
|
SELECT messages.id, messages.chat_id, messages.sender_id, messages.created_at, messages.text
|
|
FROM messages
|
|
WHERE messages.chat_id = %(chat_id_1)s::UUID AND (messages.created_at, messages.id) > (%(param_1)s::TIMESTAMP WITH TIME ZONE, %(param_2)s::UUID) ORDER BY messages.created_at, messages.id
|
|
LIMIT %(param_3)s::INTEGER
|
|
|
|
Limit (cost=0.41..10.57 rows=51 width=79) (actual time=0.044..0.064 rows=51 loops=1)
|
|
Buffers: shared hit=5
|
|
-> Index Scan using messages_chat_history_idx on messages (cost=0.41..1736.13 rows=8717 width=79) (actual time=0.043..0.055 rows=51 loops=1)
|
|
Index Cond: ((chat_id = '46e81345-6190-4647-a359-24a7b2b54811'::uuid) AND (ROW(created_at, id) > ROW('2026-10-29 10:48:07.896359+00'::timestamp with time zone, '402e4609-d59c-4a14-8778-c1b2ea8f741c'::uuid)))
|
|
Buffers: shared hit=5
|
|
Planning Time: 0.140 ms
|
|
Execution Time: 0.282 ms
|
|
|
|
## 3. big chat, last page (cursor after #11949)
|
|
-- SQL generated by the API (history_query):
|
|
SELECT messages.id, messages.chat_id, messages.sender_id, messages.created_at, messages.text
|
|
FROM messages
|
|
WHERE messages.chat_id = %(chat_id_1)s::UUID AND (messages.created_at, messages.id) > (%(param_1)s::TIMESTAMP WITH TIME ZONE, %(param_2)s::UUID) ORDER BY messages.created_at, messages.id
|
|
LIMIT %(param_3)s::INTEGER
|
|
|
|
Limit (cost=0.41..14.36 rows=51 width=79) (actual time=0.103..0.131 rows=50 loops=1)
|
|
Buffers: shared hit=5
|
|
-> Index Scan using messages_chat_history_idx on messages (cost=0.41..1509.60 rows=5517 width=79) (actual time=0.101..0.119 rows=50 loops=1)
|
|
Index Cond: ((chat_id = '46e81345-6190-4647-a359-24a7b2b54811'::uuid) AND (ROW(created_at, id) > ROW('2026-10-29 10:48:14.005334+00'::timestamp with time zone, 'd99d5478-4411-4166-b023-54ff684194b4'::uuid)))
|
|
Buffers: shared hit=5
|
|
Planning Time: 0.199 ms
|
|
Execution Time: 0.241 ms
|
|
|
|
## 4. small chat, first page
|
|
-- SQL generated by the API (history_query):
|
|
SELECT messages.id, messages.chat_id, messages.sender_id, messages.created_at, messages.text
|
|
FROM messages
|
|
WHERE messages.chat_id = %(chat_id_1)s::UUID ORDER BY messages.created_at, messages.id
|
|
LIMIT %(param_1)s::INTEGER
|
|
|
|
Limit (cost=0.41..108.66 rows=51 width=79) (actual time=0.101..0.127 rows=51 loops=1)
|
|
Buffers: shared hit=6
|
|
-> Index Scan using messages_chat_history_idx on messages (cost=0.41..1061.66 rows=500 width=79) (actual time=0.099..0.118 rows=51 loops=1)
|
|
Index Cond: (chat_id = '074dc7b2-e596-4686-b75e-ee925fd2bd87'::uuid)
|
|
Buffers: shared hit=6
|
|
Planning Time: 0.144 ms
|
|
Execution Time: 0.226 ms
|
|
|
|
## 5. for comparison: OFFSET pagination to the same depth as case 3 (NOT used by the API)
|
|
SELECT * FROM messages WHERE chat_id = %(c)s ORDER BY created_at, id OFFSET %(o)s LIMIT %(l)s
|
|
|
|
Limit (cost=1414.22..1414.34 rows=51 width=79) (actual time=11.721..11.735 rows=51 loops=1)
|
|
Buffers: shared hit=296
|
|
-> Sort (cost=1384.34..1414.34 rows=12000 width=79) (actual time=9.709..10.792 rows=12000 loops=1)
|
|
Sort Key: created_at, id
|
|
Sort Method: quicksort Memory: 1978kB
|
|
Buffers: shared hit=296
|
|
-> Seq Scan on messages (cost=0.00..571.30 rows=12000 width=79) (actual time=0.025..3.964 rows=12000 loops=1)
|
|
Filter: (chat_id = '46e81345-6190-4647-a359-24a7b2b54811'::uuid)
|
|
Rows Removed by Filter: 10024
|
|
Buffers: shared hit=296
|
|
Planning Time: 0.127 ms
|
|
Execution Time: 12.874 ms
|