Перенести мессенджер в PostgreSQL и сессии в Redis.

Подтверждённые сообщения и действующие сессии переживают перезапуск, история читается по индексу, копия восстанавливается в новый том.
This commit is contained in:
2026-10-23 12:50:45 +03:00
parent 65003b6119
commit f9b2fd11e0
29 changed files with 2933 additions and 35 deletions
+80
View File
@@ -0,0 +1,80 @@
{
"redis_and_postgres": [
{
"name": "redis down, no token",
"status": 401,
"code": "unauthorized",
"www_authenticate": "Bearer realm=\"messenger\""
},
{
"name": "redis down, bearer",
"status": 503,
"code": "unavailable",
"www_authenticate": null
},
{
"name": "redis down, live",
"status": 200,
"code": null,
"www_authenticate": null
},
{
"name": "redis down, ready",
"status": 503,
"code": "unavailable",
"www_authenticate": null
},
{
"name": "redis back, same token",
"status": 200,
"code": null,
"www_authenticate": null
},
{
"name": "history after redis stop",
"status": 200,
"same_message": true
},
{
"name": "postgres down, no token",
"status": 401,
"code": "unauthorized",
"www_authenticate": "Bearer realm=\"messenger\""
},
{
"name": "postgres down, bearer",
"status": 503,
"code": "unavailable",
"www_authenticate": null
},
{
"name": "postgres down, live",
"status": 200,
"code": null,
"www_authenticate": null
},
{
"name": "postgres down, ready",
"status": 503,
"code": "unavailable",
"www_authenticate": null
},
{
"name": "postgres back, same token",
"status": 200,
"code": null,
"www_authenticate": null
},
{
"name": "history after postgres stop",
"status": 200,
"same_message": true
}
],
"short_ttl": {
"ttl_window_seconds": 1.991,
"after_expiry": 401,
"www_authenticate": "Bearer realm=\"messenger\"",
"error_code": "unauthorized"
}
}
+14
View File
@@ -0,0 +1,14 @@
Limit (cost=2.65..122.42 rows=50 width=66) (actual time=0.042..0.064 rows=50 loops=1)
Buffers: shared hit=259
-> Incremental Sort (cost=2.65..8232.71 rows=3436 width=66) (actual time=0.042..0.061 rows=50 loops=1)
Sort Key: created_at, id
Presorted Key: created_at
Full-sort Groups: 2 Sort Method: quicksort Average Memory: 29kB Peak Memory: 29kB
Buffers: shared hit=259
-> Index Scan using messages_created_only_idx on messages (cost=0.29..8078.09 rows=3436 width=66) (actual time=0.015..0.053 rows=51 loops=1)
Index Cond: (created_at >= '2026-01-01 00:00:00.310001+00'::timestamp with time zone)
Filter: ((chat_id = '9af8786b-05c3-4793-b29e-a0702fab01fc'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.310001+00'::timestamp with time zone, '50567333-6286-45dd-99db-ccbf6c94925b'::uuid)))
Rows Removed by Filter: 205
Buffers: shared hit=259
Planning Time: 0.051 ms
Execution Time: 0.073 ms
+7
View File
@@ -0,0 +1,7 @@
Limit (cost=0.42..10.37 rows=50 width=66) (actual time=0.009..0.018 rows=50 loops=1)
Buffers: shared hit=6
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..6377.26 rows=32040 width=66) (actual time=0.008..0.015 rows=50 loops=1)
Index Cond: ((chat_id = '87a5ea91-8f1b-41ae-8ff7-8f67b1711963'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:06.2+00'::timestamp with time zone, '9ad9f914-0b9b-41f7-814d-284d3d4e85e9'::uuid)))
Buffers: shared hit=6
Planning Time: 0.046 ms
Execution Time: 0.027 ms
+7
View File
@@ -0,0 +1,7 @@
Limit (cost=4354.45..4359.89 rows=50 width=66) (actual time=4.163..4.171 rows=50 loops=1)
Buffers: shared hit=788
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..8739.72 rows=80287 width=66) (actual time=0.008..3.212 rows=40050 loops=1)
Index Cond: (chat_id = '87a5ea91-8f1b-41ae-8ff7-8f67b1711963'::uuid)
Buffers: shared hit=788
Planning Time: 0.037 ms
Execution Time: 4.180 ms
+7
View File
@@ -0,0 +1,7 @@
Limit (cost=0.42..10.31 rows=50 width=66) (actual time=0.009..0.019 rows=50 loops=1)
Buffers: shared hit=6
-> Index Scan using messages_chat_history_idx on messages (cost=0.42..6254.08 rows=31611 width=66) (actual time=0.009..0.016 rows=50 loops=1)
Index Cond: ((chat_id = '87a5ea91-8f1b-41ae-8ff7-8f67b1711963'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:06.2+00'::timestamp with time zone, '9ad9f914-0b9b-41f7-814d-284d3d4e85e9'::uuid)))
Buffers: shared hit=6
Planning Time: 0.045 ms
Execution Time: 0.027 ms
+12
View File
@@ -0,0 +1,12 @@
Limit (cost=3112.96..3113.09 rows=50 width=66) (actual time=3.970..3.974 rows=50 loops=1)
Buffers: shared hit=1248
-> Sort (cost=3112.96..3121.48 rows=3408 width=66) (actual time=3.969..3.971 rows=50 loops=1)
Sort Key: created_at, id
Sort Method: top-N heapsort Memory: 31kB
Buffers: shared hit=1248
-> Seq Scan on messages (cost=0.00..2999.75 rows=3408 width=66) (actual time=3.147..3.873 rows=1999 loops=1)
Filter: ((chat_id = '9af8786b-05c3-4793-b29e-a0702fab01fc'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:00:00.310001+00'::timestamp with time zone, '50567333-6286-45dd-99db-ccbf6c94925b'::uuid)))
Rows Removed by Filter: 98101
Buffers: shared hit=1248
Planning Time: 0.048 ms
Execution Time: 3.986 ms
+4
View File
@@ -0,0 +1,4 @@
aof_enabled:1
appendfsync:always
aof_last_write_status:ok
aof_current_size:3709
+87
View File
@@ -0,0 +1,87 @@
{
"platform": "Windows-10-10.0.26100-SP0",
"processor": "Intel64 Family 6 Model 154 Stepping 3, GenuineIntel",
"python": "3.11.5",
"seed_seconds": 2.356,
"chat_sizes_head": [
80000,
4000,
4000,
4000,
4000,
300,
300,
300
],
"chat_count": 155,
"smallest_chat": 1,
"largest_chat": 80000,
"source": {
"users": 3,
"chats": 155,
"chat_members": 165,
"messages": 100100,
"version": "PostgreSQL 16.8 (Debian 16.8-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit",
"synchronous_commit": "on",
"fsync": "on",
"shared_buffers": "128MB"
},
"restored": {
"users": 3,
"chats": 155,
"chat_members": 165,
"messages": 100100
},
"counts_match": true,
"samples_match": {
"huge_first": true,
"huge_middle": true,
"huge_last": true,
"medium": true,
"tiny": true
},
"dump_seconds": 0.477,
"dump_bytes": 3094487,
"restore_seconds": 0.61,
"create_index_ms": 71.9,
"write_cost": {
"rows": 2000,
"with_history_index_ms": 31.8,
"without_secondary_index_ms": 27.6
},
"keyset": {
"scan": "-> Index Scan using messages_chat_history_idx on messages (cost=0.42..6377.26 rows=32040 width=66) (actual time=0.008..0.015 rows=50 loops=1)",
"buffers": "Buffers: shared hit=6",
"execution_time": "Execution Time: 0.027 ms",
"planning_time": "Planning Time: 0.046 ms"
},
"offset": {
"scan": "-> Index Scan using messages_chat_history_idx on messages (cost=0.42..8739.72 rows=80287 width=66) (actual time=0.008..3.212 rows=40050 loops=1)",
"buffers": "Buffers: shared hit=788",
"execution_time": "Execution Time: 4.180 ms",
"planning_time": "Planning Time: 0.037 ms"
},
"seqscan": {
"scan": "-> Seq Scan on messages (cost=0.00..2999.75 rows=3408 width=66) (actual time=3.147..3.873 rows=1999 loops=1)",
"buffers": "Buffers: shared hit=1248",
"execution_time": "Execution Time: 3.986 ms",
"planning_time": "Planning Time: 0.048 ms"
},
"created_only": {
"scan": "-> Index Scan using messages_created_only_idx on messages (cost=0.29..8078.09 rows=3436 width=66) (actual time=0.015..0.053 rows=51 loops=1)",
"buffers": "Buffers: shared hit=259",
"execution_time": "Execution Time: 0.073 ms",
"planning_time": "Planning Time: 0.051 ms"
},
"restored_keyset": {
"scan": "-> Index Scan using messages_chat_history_idx on messages (cost=0.42..6254.08 rows=31611 width=66) (actual time=0.009..0.016 rows=50 loops=1)",
"buffers": "Buffers: shared hit=6",
"execution_time": "Execution Time: 0.027 ms",
"planning_time": "Planning Time: 0.045 ms"
},
"restored_message_indexes": [
"messages_chat_history_idx",
"messages_pkey"
],
"dump_file": "backups/messenger_bench.dump"
}