{"id":479617,"date":"2026-05-13T14:20:25","date_gmt":"2026-05-13T14:20:25","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=479617"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=479617","title":{"rendered":"Production-\u0441\u0442\u0435\u043a \u0434\u043b\u044f \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440\u0430 \u043d\u0430 10\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439: FastAPI, SQLite \u0432 \u043f\u0440\u043e\u0434\u0435 \u0438 \u043f\u043e\u0447\u0435\u043c\u0443 \u043c\u043e\u043d\u043e\u043b\u0438\u0442"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<blockquote>\n<p>\u0423\u0440\u043e\u0432\u0435\u043d\u044c: middle\/senior backend \u0421\u0442\u0435\u043a: FastAPI 0.115, SQLite, WebSocket, slowapi, JWT-like sessions, Docker \u0427\u0442\u043e \u0432\u043d\u0443\u0442\u0440\u0438: \u043a\u0430\u043a \u0443 \u043c\u0435\u043d\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0431\u044d\u043a\u0435\u043d\u0434 \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440\u0430, \u043f\u043e\u0447\u0435\u043c\u0443 SQLite \u0432\u043c\u0435\u0441\u0442\u043e Postgres, \u043c\u043e\u043d\u043e\u043b\u0438\u0442 \u043d\u0430 19\u043a \u0441\u0442\u0440\u043e\u043a, \u0441\u0430\u043c\u043e\u043f\u0438\u0441\u043d\u044b\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438, sliding expiration \u0434\u043b\u044f \u0442\u043e\u043a\u0435\u043d\u043e\u0432<\/p>\n<\/blockquote>\n<h3>\u041f\u0440\u0435\u0430\u043c\u0431\u0443\u043b\u0430<\/h3>\n<p>\u042d\u0442\u043e \u0432\u043e\u0441\u044c\u043c\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0438\u0437 \u043c\u043e\u0435\u0439 \u0441\u0435\u0440\u0438\u0438 \u043f\u0440\u043e \u0438\u043d\u0436\u0435\u043d\u0435\u0440\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432 ONEMIX. \u0414\u043e \u044d\u0442\u043e\u0433\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440\u0430: <a href=\"#\" rel=\"noopener noreferrer nofollow\">\u043a\u044d\u0448 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439<\/a>, <a href=\"#\" rel=\"noopener noreferrer nofollow\">E2E<\/a>, <a href=\"#\" rel=\"noopener noreferrer nofollow\">WebRTC \u0437\u0432\u043e\u043d\u043a\u0438<\/a>, <a href=\"#\" rel=\"noopener noreferrer nofollow\">Electron<\/a>, <a href=\"#\" rel=\"noopener noreferrer nofollow\">outbox-\u043f\u0430\u0442\u0442\u0435\u0440\u043d<\/a>. \u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e AI-\u0430\u0433\u0435\u043d\u0442\u0430 <a href=\"#\" rel=\"noopener noreferrer nofollow\">\u041b\u0438\u0440\u0443<\/a> \u0438 <a href=\"#\" rel=\"noopener noreferrer nofollow\">\u043c\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e \u0432\u0430\u0439\u0431-\u043a\u043e\u0434\u0438\u043d\u0433<\/a>.<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043f\u0440\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443. Backend ONEMIX \u2014 \u044d\u0442\u043e <strong>\u043e\u0434\u0438\u043d \u0444\u0430\u0439\u043b <\/strong><a href=\"http:\/\/main.py\" rel=\"noopener noreferrer nofollow\"><strong>main.py<\/strong><\/a><strong> \u043d\u0430 19603 \u0441\u0442\u0440\u043e\u043a\u0438<\/strong>, 379 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u043e\u0432, FastAPI + SQLite, \u0434\u0435\u0440\u0436\u0438\u0442 \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440 \u0441 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0435\u0439 \u0447\u0435\u0440\u0435\u0437 SMS, \u0437\u0432\u043e\u043d\u043a\u0430\u043c\u0438 \u0447\u0435\u0440\u0435\u0437 LiveKit, E2E \u0447\u0435\u0440\u0435\u0437 Double Ratchet, push-\u043d\u043e\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f\u043c\u0438 \u043d\u0430 iOS \u0438 Android. \u042d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b \u044f \u043f\u0438\u0448\u0443 \u0431\u043e\u043b\u044c\u0448\u0435 \u0433\u043e\u0434\u0430. \u0417\u0430 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043e\u043d \u044d\u0432\u043e\u043b\u044e\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043b \u0438\u0437 \u043f\u0440\u043e\u0442\u043e\u0442\u0438\u043f\u0430 \u043d\u0430 800 \u0441\u0442\u0440\u043e\u043a \u0432 production \u043c\u043e\u043d\u043e\u043b\u0438\u0442.<\/p>\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0443 \u043f\u043e\u0447\u0435\u043c\u0443 \u0441\u0442\u0435\u043a \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a\u043e\u0439, \u043a\u0430\u043a\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043e\u043a\u0430\u0437\u0430\u043b\u0438\u0441\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c\u0438, \u043a\u0430\u043a\u0438\u0435 \u044f \u0431\u044b \u043f\u043e\u043c\u0435\u043d\u044f\u043b \u0435\u0441\u043b\u0438 \u0431\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u043b \u0441\u0435\u0439\u0447\u0430\u0441, \u0438 \u0433\u0434\u0435 \u0443 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0421\u0440\u0430\u0437\u0443 \u0432\u0430\u0436\u043d\u0430\u044f \u043e\u0433\u043e\u0432\u043e\u0440\u043a\u0430. \u0423 \u043c\u0435\u043d\u044f <strong>\u043d\u0435 \u0431\u044b\u043b\u043e<\/strong> \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0435\u0440\u0436\u0430\u0442\u044c 100\u043a \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438\u043b\u0438 10\u043a RPS. \u042d\u0442\u043e \u0431\u044d\u043a\u0435\u043d\u0434 <strong>\u043f\u043e\u0434 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/strong> \u0441 \u0442\u0440\u0430\u0444\u0438\u043a\u043e\u043c \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043b\u044f \u0441\u043e\u043b\u043e-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0440\u0430\u0437\u0443\u043c\u043d\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u043e\u043c\u0443. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0437\u0430\u0434\u0430\u0447\u0438 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0430, \u043c\u043e\u0439 \u043e\u043f\u044b\u0442 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u043f\u043e\u0434\u043e\u0439\u0442\u0438.<\/p>\n<h3>\u0421\u0442\u0435\u043a \u043e\u0434\u043d\u0438\u043c \u0432\u0437\u0433\u043b\u044f\u0434\u043e\u043c<\/h3>\n<pre><code class=\"python\"># requirements.txtfastapi==0.115.0uvicorn[standard]==0.30.6psycopg2-binary==2.9.9   # \u0434\u043b\u044f \u0431\u0443\u0434\u0443\u0449\u0435\u0439 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438, \u0441\u0435\u0439\u0447\u0430\u0441 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044fsqlite3                   # \u0438\u0437 stdlib, \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0411\u0414python-dotenv==1.0.1pydantic==2.8.2python-multipart==0.0.9httpx==0.27.0slowapi==0.1.9pillow==10.4.0pillow-heif==0.18.0aiofiles==24.1.0websockets==13.0PyJWT==2.9.0cryptography==43.0.1<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:87px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>15 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 ORM (SQLAlchemy \u043d\u0435\u0442), \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e Celery, \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e Redis. Pydantic \u0434\u043b\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 JSON, slowapi \u0434\u043b\u044f rate limiting, \u0432\u0441\u0451 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435.<\/p>\n<p>Docker-compose \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432: backend \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0438 Postgres. \u0422\u0443\u0442 \u0443\u0436\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442. Postgres \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0451\u043d, \u043d\u043e \u0432 \u043a\u043e\u0434\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f SQLite. \u041e\u0431\u044a\u044f\u0441\u043d\u044e \u0437\u0430\u0447\u0435\u043c \u0434\u0430\u043b\u044c\u0448\u0435.<\/p>\n<figure class=\"full-width \"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/6a5\/81e\/5d6\/6a581e5d6278d33caae91168b8b4dd3e.png\" width=\"1767\" height=\"1016\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/6a5\/81e\/5d6\/6a581e5d6278d33caae91168b8b4dd3e.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/6a5\/81e\/5d6\/6a581e5d6278d33caae91168b8b4dd3e.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/figure>\n<h3>SQLite \u0432 production<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"http:\/\/main.py\" rel=\"noopener noreferrer nofollow\"><code>main.py<\/code><\/a>, \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u0432\u043e\u0442 \u044d\u0442\u043e:<\/p>\n<pre><code class=\"python\">import sqlite3BASE_DIR = os.path.abspath(os.path.dirname(__file__))DATABASE_PATH = os.path.join(BASE_DIR, \"messenger.db\")def get_db_connection():    conn = sqlite3.connect(DATABASE_PATH, check_same_thread=False)    conn.row_factory = sqlite3.Row    return conn<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u042d\u0442\u043e production, \u043d\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0430\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430, \u043d\u0435 \u0442\u0435\u0441\u0442\u044b, \u043d\u0435 \u043f\u0440\u043e\u0442\u043e\u0442\u0438\u043f. SQLite \u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u0435\u0441\u044c \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440: \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438, \u0447\u0430\u0442\u044b, \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f, \u0440\u0435\u0430\u043a\u0446\u0438\u0438, \u0437\u0432\u043e\u043d\u043a\u0438, \u0441\u0435\u0441\u0441\u0438\u0438. \u041d\u0430 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 <code>messenger.db<\/code>.<\/p>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 SQLite \u0432\u043c\u0435\u0441\u0442\u043e Postgres \u0434\u043b\u044f production \u2014 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441. \u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u2014 Postgres \u0443 \u043c\u0435\u043d\u044f \u0433\u043e\u0442\u043e\u0432. Docker-compose \u043f\u043e\u0434\u043d\u0438\u043c\u0430\u0435\u0442 \u0435\u0433\u043e \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e, <code>psycopg2-binary<\/code> \u0432 requirements.txt, \u0435\u0441\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 <code>POSTGRES_<\/code><a href=\"http:\/\/MIGRATION.md\" rel=\"noopener noreferrer nofollow\"><code>MIGRATION.md<\/code><\/a> \u0441 \u043f\u043b\u0430\u043d\u043e\u043c \u043f\u0435\u0440\u0435\u0435\u0437\u0434\u0430. \u042f \u043c\u043e\u0433\u0443 \u043c\u0438\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0433\u0434\u0430 \u0437\u0430\u0445\u043e\u0447\u0443. \u041f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043a\u0430 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e.<\/p>\n<p><strong>\u041f\u043e\u0447\u0435\u043c\u0443 SQLite \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f<\/strong><\/p>\n<p>\u0412 \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440\u0435 90% \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u044d\u0442\u043e INSERT \u043d\u043e\u0432\u044b\u0445 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0438 SELECT \u043f\u043e chat_id. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 JOIN&#8217;\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u0448\u0430\u0440\u0434\u0430\u043c\u0438, \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0438 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043e\u0442\u0447\u0451\u0442\u043e\u0432 \u043f\u043e \u043e\u0433\u0440\u043e\u043c\u043d\u044b\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c. SQLite \u043d\u0430 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 SSD \u0434\u0435\u0440\u0436\u0438\u0442 \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u0442\u044b\u0441\u044f\u0447 INSERT \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435. \u042d\u0442\u043e\u0433\u043e \u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0437\u0430\u043f\u0430\u0441\u043e\u043c.<\/p>\n<p>WAL-\u0440\u0435\u0436\u0438\u043c (Write-Ahead Logging) \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u043e\u0439 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0447\u0438\u0442\u0430\u0442\u044c \u0411\u0414 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u0441 \u0437\u0430\u043f\u0438\u0441\u044c\u044e. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 lock contention \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u0441\u0435 \u0431\u043e\u044f\u0442\u0441\u044f. \u041d\u0430 \u043c\u043e\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u0438\u0447\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 0.1-2\u043c\u0441.<\/p>\n<p>Backup \u2014 \u044d\u0442\u043e <code>cp messenger.db messenger.db.bak<\/code>. \u0411\u0435\u0437 mysqldump, \u0431\u0435\u0437 pg_dump, \u0431\u0435\u0437 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b. \u0424\u0430\u0439\u043b \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f \u2014 backup \u0433\u043e\u0442\u043e\u0432.<\/p>\n<p><strong>\u0413\u0434\u0435 SQLite \u043d\u0435 \u0441\u043f\u0440\u0430\u0432\u0438\u0442\u0441\u044f<\/strong><\/p>\n<p>\u042f \u043d\u0435 \u0434\u0435\u043b\u0430\u044e \u0432\u0438\u0434 \u0447\u0442\u043e SQLite \u044d\u0442\u043e \u0441\u0435\u0440\u0435\u0431\u0440\u044f\u043d\u0430\u044f \u043f\u0443\u043b\u044f. \u0413\u0434\u0435 \u043e\u043d\u0430 <strong>\u0442\u043e\u0447\u043d\u043e<\/strong> \u043f\u0440\u043e\u0438\u0433\u0440\u0430\u0435\u0442:<\/p>\n<p>\u041c\u043d\u043e\u0433\u043e\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 5 uvicorn-\u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432 \u043f\u0438\u0448\u0443\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u2014 \u0431\u0443\u0434\u0435\u0442 lock contention. \u042f \u0434\u0435\u0440\u0436\u0443 \u043e\u0434\u0438\u043d \u0432\u043e\u0440\u043a\u0435\u0440 \u0441 asyncio, \u0438 \u044d\u0442\u043e\u0433\u043e \u0445\u0432\u0430\u0442\u0430\u0435\u0442.<\/p>\n<p>\u0421\u0435\u0442\u0435\u0432\u043e\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0411\u0414. SQLite \u044d\u0442\u043e \u0444\u0430\u0439\u043b, \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043c\u0430\u0448\u0438\u043d\u0430\u043c\u0438. \u0415\u0441\u043b\u0438 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e \u2014 \u043d\u0443\u0436\u0435\u043d \u0441\u0435\u0442\u0435\u0432\u043e\u0439 DB.<\/p>\n<p>\u0421\u043b\u043e\u0436\u043d\u0430\u044f \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0430. \u0415\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f crosstable JOIN&#8217;\u044b \u043f\u043e \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430\u043c \u0441\u0442\u0440\u043e\u043a \u2014 Postgres \u043b\u0443\u0447\u0448\u0435, SQLite \u044d\u0442\u043e OLTP-only.<\/p>\n<p>Postgres-\u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u044b\u0435 \u0444\u0438\u0447\u0438. JSON-\u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b, GIS, full-text search \u0447\u0435\u0440\u0435\u0437 pg_trgm \u2014 \u044d\u0442\u043e \u0432\u0441\u0451 \u0431\u043e\u0433\u0430\u0447\u0435 \u0447\u0435\u043c SQLite. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u2014 Postgres.<\/p>\n<p><strong>\u041a\u043e\u0433\u0434\u0430 \u044f \u043c\u0438\u0433\u0440\u0438\u0440\u0443\u044e<\/strong><\/p>\n<p>\u0422\u0440\u0438\u0433\u0433\u0435\u0440 \u0434\u043b\u044f \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u044d\u0442\u043e \u043d\u0435 &#171;\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0441\u0442\u0430\u043b\u043e \u0431\u043e\u043b\u044c\u0448\u0435&#187;, \u0430 &#171;\u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0437\u0430\u0434\u0430\u0447\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e SQLite \u043d\u0435 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442&#187;. \u041f\u043e\u043a\u0430 \u0435\u0451 \u043d\u0435\u0442.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0437\u0430\u0432\u0442\u0440\u0430 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u0430\u044f \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0430 \u2014 \u043c\u0438\u0433\u0440\u0438\u0440\u0443\u044e \u0437\u0430 \u043d\u0435\u0434\u0435\u043b\u044e. \u041f\u043b\u0430\u043d \u0437\u0430\u043f\u0438\u0441\u0430\u043d, \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0433\u043e\u0442\u043e\u0432\u0430.<\/p>\n<h3>\u041c\u043e\u043d\u043e\u043b\u0438\u0442 \u043d\u0430 19603 \u0441\u0442\u0440\u043e\u043a\u0438<\/h3>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u2014 \u043e\u0434\u0438\u043d \u0444\u0430\u0439\u043b <a href=\"http:\/\/main.py\" rel=\"noopener noreferrer nofollow\"><code>main.py<\/code><\/a> \u043d\u0430 19603 \u0441\u0442\u0440\u043e\u043a\u0438 \u0441 379 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u0430\u043c\u0438. \u041d\u0435 \u043f\u0430\u043f\u043a\u0430 \u0441 \u043c\u043e\u0434\u0443\u043b\u044f\u043c\u0438, \u043d\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u043e \u0434\u043e\u043c\u0435\u043d\u0430\u043c, \u043d\u0435 APIRouter&#8217;\u044b.<\/p>\n<p>\u042d\u0442\u043e <strong>\u0441\u043e\u0437\u043d\u0430\u0442\u0435\u043b\u044c\u043d\u043e<\/strong>. \u041e\u0431\u044a\u044f\u0441\u043d\u044e \u043f\u043e\u0447\u0435\u043c\u0443.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0448\u044c \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0448\u044c \u0431\u044d\u043a\u0435\u043d\u0434 <strong>\u0432 \u043e\u0434\u0438\u043d\u043e\u0447\u043a\u0443<\/strong>, \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u0440\u0435\u0441\u0443\u0440\u0441 \u044d\u0442\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438 \u0432 \u043a\u043e\u0434\u0435. \u041a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 <code>\/chats\/{id}\/messages<\/code>, \u043d\u0443\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c \u0433\u0434\u0435 \u043e\u043d \u043b\u0435\u0436\u0438\u0442. \u0412 \u043c\u043e\u043d\u043e\u043b\u0438\u0442\u0435 \u043e\u0442\u0432\u0435\u0442 \u043e\u0434\u0438\u043d: \u0432 <a href=\"http:\/\/main.py\" rel=\"noopener noreferrer nofollow\"><code>main.py<\/code><\/a>. \u0412 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u0430\u0445 \u0438\u043b\u0438 \u043c\u0443\u043b\u044c\u0442\u0438-\u043c\u043e\u0434\u0443\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u2014 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043a\u0430\u043a\u0430\u044f \u043f\u0430\u043f\u043a\u0430, \u043a\u0430\u043a\u043e\u0439 router, \u043a\u0430\u043a\u043e\u0439 \u0444\u0430\u0439\u043b.<\/p>\n<p>\u0421 Cmd+P \u0432 IDE \u044f \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u044e <a href=\"http:\/\/main.py\" rel=\"noopener noreferrer nofollow\">main.py<\/a> \u0437\u0430 \u0441\u0435\u043a\u0443\u043d\u0434\u0443. \u0421 Cmd+Shift+O (Go to Symbol) \u043d\u0430\u0445\u043e\u0436\u0443 \u043b\u044e\u0431\u043e\u0439 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0438\u0445 379. \u042d\u0442\u0430 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043b\u043e\u0445\u043e \u0432 \u043c\u0443\u043b\u044c\u0442\u0438\u043c\u043e\u0434\u0443\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445. \u0418\u043c\u0435\u043d\u0430 \u043a\u043e\u043b\u043b\u0438\u0437\u0438\u043e\u043d\u0438\u0440\u0443\u044e\u0442 \u043c\u0435\u0436\u0434\u0443 \u043c\u043e\u0434\u0443\u043b\u044f\u043c\u0438, \u043d\u0443\u0436\u043d\u043e \u0445\u043e\u0434\u0438\u0442\u044c \u043f\u043e \u043f\u0430\u043f\u043a\u0430\u043c.<\/p>\n<p>\u0427\u0442\u043e \u044f \u0434\u0435\u043b\u0430\u044e \u0447\u0442\u043e\u0431\u044b 19\u043a \u0441\u0442\u0440\u043e\u043a \u043d\u0435 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u043b\u0438\u0441\u044c \u0432 \u0445\u0430\u043e\u0441:<\/p>\n<p><strong>\u041b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u0438 \u0431\u043b\u043e\u0447\u043d\u044b\u043c\u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c\u0438<\/strong>. \u0413\u0440\u0443\u043f\u043f\u044b \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u043e\u0432 \u043e\u0442\u0434\u0435\u043b\u0435\u043d\u044b \u0442\u0430\u043a\u0438\u043c\u0438 \u0431\u043b\u043e\u043a\u0430\u043c\u0438:<\/p>\n<pre><code class=\"python\"># ============ AUTH ============# ============ CHATS ============# ============ MESSAGES ============<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0412 IDE \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 code folding \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0447\u0442\u0438 \u043a\u0430\u043a \u043f\u0430\u043f\u043a\u0438.<\/p>\n<p><strong>Stand-alone \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u044b<\/strong>. \u041a\u0430\u0436\u0434\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f-\u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u0430, \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043e\u0431\u0449\u0438\u0445 \u0445\u0435\u043b\u043f\u0435\u0440\u043e\u0432 \u043d\u0430 \u043f\u043e\u043b\u0441\u0442\u0430\u0442\u044c\u0438. \u0427\u0438\u0442\u0430\u044e \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u2014 \u0432\u0441\u044f \u0435\u0433\u043e \u043b\u043e\u0433\u0438\u043a\u0430 \u0432 \u043d\u0451\u043c \u0436\u0435, \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u044b\u0433\u0430\u0442\u044c \u043f\u043e 5 \u0444\u0430\u0439\u043b\u0430\u043c.<\/p>\n<p><strong>Pydantic-\u043c\u043e\u0434\u0435\u043b\u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u0431\u043b\u043e\u043a\u0435 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u0444\u0430\u0439\u043b\u0430<\/strong>. \u0412\u0441\u0435 request\/response \u0441\u0445\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432\u043c\u0435\u0441\u0442\u0435, \u043b\u0435\u0433\u043a\u043e \u043d\u0430\u0439\u0442\u0438.<\/p>\n<p><strong>\u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u0439 grep<\/strong>. \u041a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043f\u043e\u043c\u043d\u044e \u0433\u0434\u0435 \u0447\u0442\u043e, \u0434\u0435\u043b\u0430\u044e <code>grep \"chats\/.*messages\" <\/code><a href=\"http:\/\/main.py\" rel=\"noopener noreferrer nofollow\"><code>main.py<\/code><\/a>. \u041d\u0430\u0439\u0434\u0451\u0442 \u0437\u0430 \u0441\u0435\u043a\u0443\u043d\u0434\u0443.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043c\u043e\u043d\u043e\u043b\u0438\u0442 \u0441\u043b\u043e\u043c\u0430\u0435\u0442\u0441\u044f. \u0415\u0441\u043b\u0438 \u0431\u044b \u044f \u043d\u0430\u043d\u0438\u043c\u0430\u043b \u0432\u0442\u043e\u0440\u043e\u0433\u043e backend-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430, \u043c\u043e\u043d\u043e\u043b\u0438\u0442 \u0431\u044b\u043b \u0431\u044b \u043f\u043b\u043e\u0445\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c. \u0414\u0432\u0430 \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0432 \u043e\u0434\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0434\u0430\u044e\u0442 merge-\u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u044b \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u043a\u043e\u043c\u043c\u0438\u0442\u0435. \u042f \u0431\u044b \u0442\u043e\u0433\u0434\u0430 \u0440\u0430\u0437\u0431\u0438\u043b \u043f\u043e \u0434\u043e\u043c\u0435\u043d\u0430\u043c \u0441 APIRouter \u0438 \u043f\u0430\u043a\u0435\u0442\u0430\u043c\u0438. \u0421\u0435\u0439\u0447\u0430\u0441 \u043e\u0434\u0438\u043d \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0438 \u043e\u0434\u0438\u043d \u0444\u0430\u0439\u043b, \u044d\u0442\u043e \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e.<\/p>\n<h3>\u0421\u0430\u043c\u043e\u043f\u0438\u0441\u043d\u044b\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0431\u0435\u0437 alembic<\/h3>\n<p>\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0441\u043e\u0432\u0435\u0442 \u0434\u043b\u044f FastAPI + \u0411\u0414, \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c alembic. \u042f \u043d\u0435 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043b, \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0434\u0435\u043b\u0430\u044e \u0441\u0430\u043c:<\/p>\n<pre><code class=\"python\">def add_column_if_missing(table_name, column_name, column_type, index=False):    cursor.execute(f\"PRAGMA table_info({table_name})\")    columns = [row[1] for row in cursor.fetchall()]    if column_name not in columns:        print(f\"[DB] Migrating {table_name}: adding {column_name} column\")        try:            cursor.execute(f\"ALTER TABLE {table_name} ADD COLUMN {column_name} {column_type}\")            if index:                cursor.execute(f\"CREATE UNIQUE INDEX IF NOT EXISTS idx_{table_name}_{column_name} ON {table_name}({column_name})\")            conn.commit()        except Exception as e:            print(f\"[DB] Migration error ({table_name}.{column_name}): {e}\")# \u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435add_column_if_missing('users', 'phone', 'TEXT', index=True)add_column_if_missing('users', 'username', 'TEXT', index=True)add_column_if_missing('users', 'bio', 'TEXT')add_column_if_missing('chats', 'is_private', 'INTEGER DEFAULT 0')# ... \u0435\u0449\u0451 50 \u0442\u0430\u043a\u0438\u0445 \u0441\u0442\u0440\u043e\u043a<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u042d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 self-healing \u043f\u043e\u0434\u0445\u043e\u0434. \u041f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 <code>init_db()<\/code> \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u043f\u043e \u0432\u0441\u0435\u043c \u043d\u0443\u0436\u043d\u044b\u043c \u043a\u043e\u043b\u043e\u043d\u043a\u0430\u043c \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0435. SQLite \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 <code>ALTER TABLE ADD COLUMN<\/code> \u0431\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a.<\/p>\n<p>\u041f\u043e\u0447\u0435\u043c\u0443 \u043d\u0435 alembic. Alembic \u044d\u0442\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0441\u043e \u0441\u0432\u043e\u0435\u0439 CLI, \u0441\u0432\u043e\u0435\u0439 \u043d\u0443\u043c\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439, \u0441\u0432\u043e\u0438\u043c\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0444\u0430\u0439\u043b\u0430\u043c\u0438. \u0427\u0442\u043e\u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u043b\u043e\u043d\u043a\u0443 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c migration file, \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c upgrade\/downgrade, \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c <code>alembic upgrade head<\/code>, \u043d\u0435 \u0437\u0430\u0431\u044b\u0442\u044c \u0437\u0430\u043a\u043e\u043c\u043c\u0438\u0442\u0438\u0442\u044c migration file. \u042d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u0435 \u0433\u0434\u0435 \u043d\u0443\u0436\u043d\u0430 \u0444\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u0414\u043b\u044f \u043e\u0434\u0438\u043d\u043e\u0447\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u044d\u0442\u043e overhead.<\/p>\n<p>\u0421 \u043c\u043e\u0438\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u043c \u043f\u0440\u043e\u0449\u0435. \u0414\u043e\u0431\u0430\u0432\u0438\u043b \u0441\u0442\u0440\u043e\u043a\u0443 <code>add_column_if_missing<\/code>, \u0440\u0435\u0441\u0442\u0430\u0440\u0442\u0430\u043d\u0443\u043b \u0441\u0435\u0440\u0432\u0435\u0440, \u0433\u043e\u0442\u043e\u0432\u043e. \u0414\u0435\u0432-\u0446\u0438\u043a\u043b \u0432 3 \u0440\u0430\u0437\u0430 \u043a\u043e\u0440\u043e\u0447\u0435.<\/p>\n<p>\u0413\u0434\u0435 \u044d\u0442\u043e \u0441\u043b\u043e\u043c\u0430\u0435\u0442\u0441\u044f. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u044b \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0441 \u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u043b\u043e\u043d\u043e\u043a, \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0435\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446, \u043d\u0443\u0436\u0435\u043d \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u043f\u043e\u0441\u0435\u0440\u044c\u0451\u0437\u043d\u0435\u0435. \u0423 \u043c\u0435\u043d\u044f \u0442\u0430\u043a\u0438\u0445 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439 \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u044b\u043b\u043e. \u041a\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u2014 \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044e alembic \u0440\u0435\u0442\u0440\u043e\u0441\u043f\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u043b\u0438 \u043c\u0438\u0433\u0440\u0438\u0440\u0443\u044e \u043d\u0430 Postgres + \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442.<\/p>\n<p>\u0418 \u044f <strong>\u043d\u0435 \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u044e<\/strong> \u0447\u0442\u043e \u044d\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0434\u043b\u044f \u0432\u0441\u0435\u0445. \u042d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043b\u044f \u043c\u043e\u0435\u0433\u043e \u043f\u0440\u043e\u0444\u0438\u043b\u044f \u0437\u0430\u0434\u0430\u0447. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u0435 5 \u0447\u0435\u043b\u043e\u0432\u0435\u043a \u0434\u0435\u043b\u0430\u044e\u0442 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438, alembic \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u0435\u043d, \u0438\u043d\u0430\u0447\u0435 \u0445\u0430\u043e\u0441 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d.<\/p>\n<h3>Sessions \u0432\u043c\u0435\u0441\u0442\u043e JWT \u0441\u043e sliding expiration<\/h3>\n<p>\u0422\u0440\u0435\u0442\u0438\u0439 \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440. Auth \u043d\u0435 \u0447\u0435\u0440\u0435\u0437 JWT, \u0430 \u0447\u0435\u0440\u0435\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 sessions \u0432 \u0411\u0414.<\/p>\n<pre><code class=\"python\">def get_current_user(request, authorization, db):    if not authorization:        raise HTTPException(401, \"Not authenticated\")        token = authorization.replace(\"Bearer \", \"\")    cursor = db.cursor()    cursor.execute(\"\"\"        SELECT s.user_id, s.expires_at, u.id, u.name, u.phone, ...        FROM sessions s        JOIN users u ON s.user_id = u.id        WHERE s.token = ?    \"\"\", (token,))    row = cursor.fetchone()    if not row:        raise HTTPException(401, \"Invalid token\")        expires_at = datetime.fromisoformat(row[\"expires_at\"].replace('Z', '+00:00'))    if datetime.now(timezone.utc) &gt; expires_at:        raise HTTPException(401, \"Token expired\")        # Sliding expiration \u2014 \u043f\u0440\u043e\u0434\u043b\u0435\u0432\u0430\u0435\u043c \u0441\u0440\u043e\u043a \u0436\u0438\u0437\u043d\u0438 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430    days_left = (expires_at - now_dt).total_seconds() \/ 86400    if days_left &lt; 7:        new_expires = (now_dt + timedelta(days=30)).isoformat()        cursor.execute(\"UPDATE sessions SET expires_at = ? WHERE token = ?\", (new_expires, token))        return {\"id\": row[\"id\"], \"name\": row[\"name\"], ...}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>JWT \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u0430 <code>sessions<\/code>, \u0433\u0434\u0435 token \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0411\u0414 \u0438 \u0441\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435.<\/p>\n<p>\u0427\u0442\u043e \u044f \u043f\u043e\u043b\u0443\u0447\u0430\u044e \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 JWT:<\/p>\n<p><strong>\u041c\u043e\u0436\u043d\u043e \u043e\u0442\u043e\u0437\u0432\u0430\u0442\u044c \u0442\u043e\u043a\u0435\u043d<\/strong>. JWT \u043e\u0442\u043e\u0437\u0432\u0430\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f \u0431\u0435\u0437 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e blacklist&#8217;\u0430. Sessions \u043f\u0440\u043e\u0441\u0442\u043e <code>DELETE FROM sessions WHERE token = ?<\/code> \u0438 \u0432\u0441\u0451, \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0441\u0440\u0430\u0437\u0443 \u0440\u0430\u0437\u043b\u043e\u0433\u0438\u043d\u0435\u043d.<\/p>\n<p><strong>\u041c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u0441\u0435\u0441\u0441\u0438\u0438<\/strong>. SELECT \u043f\u043e user_id \u0438 \u044e\u0437\u0435\u0440 \u0432\u0438\u0434\u0438\u0442 \u0432\u0441\u0435 \u0441\u0432\u043e\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430, \u043c\u043e\u0436\u0435\u0442 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0435.<\/p>\n<p><strong>Sliding expiration<\/strong>. \u042d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0448\u0442\u0443\u043a\u0430. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f JWT-\u0441\u0445\u0435\u043c\u0430: \u0442\u043e\u043a\u0435\u043d \u0436\u0438\u0432\u0451\u0442 1 \u0447\u0430\u0441, \u043f\u043e\u0442\u043e\u043c refresh-token \u043c\u0435\u043d\u044f\u0435\u0442 \u043d\u0430 \u043d\u043e\u0432\u044b\u0439. \u0423 \u043c\u0435\u043d\u044f \u043f\u043e-\u0434\u0440\u0443\u0433\u043e\u043c\u0443. \u0422\u043e\u043a\u0435\u043d \u0436\u0438\u0432\u0451\u0442 30 \u0434\u043d\u0435\u0439 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e. \u041a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043a\u043e\u0433\u0434\u0430 \u044e\u0437\u0435\u0440 \u0434\u0435\u043b\u0430\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043e \u0438\u0441\u0442\u0435\u0447\u0435\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u043c\u0435\u043d\u044c\u0448\u0435 7 \u0434\u043d\u0435\u0439, \u0442\u043e\u043a\u0435\u043d \u043f\u0440\u043e\u0434\u043b\u0435\u0432\u0430\u0435\u0442\u0441\u044f \u0435\u0449\u0451 \u043d\u0430 30. \u0410\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u044e\u0437\u0435\u0440 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0440\u0430\u0437\u043b\u043e\u0433\u0438\u043d\u0438\u0432\u0430\u0435\u0442\u0441\u044f. \u041d\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0447\u0435\u0440\u0435\u0437 30 \u0434\u043d\u0435\u0439 \u0431\u0435\u0437 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0426\u0435\u043d\u0430 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u2014 JOIN \u0441 users \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435. \u0423 \u043c\u0435\u043d\u044f \u044d\u0442\u043e \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 0.5-2\u043c\u0441 \u043d\u0430 SQLite, \u043d\u0435 \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u043e.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0441\u0442\u043e\u0438\u0442 JWT. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0442\u043e\u043a\u0435\u043d \u0431\u0435\u0437 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a centralized auth-\u0411\u0414, JWT \u0434\u0430\u0451\u0442 stateless \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443. \u0423 \u043c\u0435\u043d\u044f \u043e\u0434\u0438\u043d \u043c\u043e\u043d\u043e\u043b\u0438\u0442, \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b stateless \u043d\u0435\u0442.<\/p>\n<h3>WebSocket connection manager<\/h3>\n<p>Real-time \u0447\u0435\u0440\u0435\u0437 <code>WebSocket<\/code> \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432 FastAPI, \u0431\u0435\u0437 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e Redis Pub\/Sub. \u0412\u0441\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0434\u0435\u0440\u0436\u0430\u0442\u0441\u044f \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430.<\/p>\n<pre><code class=\"python\">class ConnectionManager:    def __init__(self):        self.active_connections: Dict[str, Set[WebSocket]] = {}        self.online_users: Set[str] = set()        # \u041b\u043e\u043a \u0422\u041e\u041b\u042c\u041a\u041e \u0434\u043b\u044f \u043c\u0443\u0442\u0430\u0446\u0438\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u2014 \u043d\u0435 \u0443\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f IO        self._conn_lock = asyncio.Lock()        async def send_personal_message(self, message: dict, user_id: str):        # \u0421\u043d\u0438\u043c\u043e\u043a \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 \u0431\u0435\u0437 \u0443\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f \u043b\u043e\u043a\u0430 \u043d\u0430 \u0432\u0440\u0435\u043c\u044f IO        connections = set(self.active_connections.get(user_id, set()))        if not connections:            return        dead: Set[WebSocket] = set()        for connection in connections:            try:                await connection.send_json(message)            except Exception:                dead.add(connection)        if dead:            async with self._conn_lock:                bucket = self.active_connections.get(user_id)                if bucket:                    bucket -= dead        async def broadcast_to_users(self, message: dict, user_ids: List[str]):        # \u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u0430\u044f \u0440\u0430\u0441\u0441\u044b\u043b\u043a\u0430 \u0447\u0435\u0440\u0435\u0437 asyncio.gather        await asyncio.gather(            *(self.send_personal_message(message, uid) for uid in user_ids),            return_exceptions=True        )<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041a\u043b\u044e\u0447\u0435\u0432\u0430\u044f \u0448\u0442\u0443\u043a\u0430. \u041b\u043e\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043c\u0443\u0442\u0430\u0446\u0438\u044e \u0441\u043b\u043e\u0432\u0430\u0440\u044f, \u043d\u0435 \u043d\u0430 IO. \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0436\u0430\u0442\u044c <code>async with self._conn_lock<\/code> \u043f\u043e\u043a\u0430 \u0438\u0434\u0451\u0442 <code>await connection.send_json(message)<\/code>, \u0432\u0441\u0435 broadcasts \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u044e\u0442\u0441\u044f. \u041e\u0434\u0438\u043d \u0437\u0430\u0432\u0438\u0441\u0448\u0438\u0439 \u043a\u043b\u0438\u0435\u043d\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 \u0440\u0430\u0441\u0441\u044b\u043b\u043a\u0443 \u0432\u0441\u0435\u043c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u043c.<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0435\u043b\u0430\u044e \u0441\u043d\u0438\u043c\u043e\u043a \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043f\u043e\u0434 \u043b\u043e\u043a\u043e\u043c, \u043e\u0442\u043f\u0443\u0441\u043a\u0430\u044e \u043b\u043e\u043a, \u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u0448\u043b\u044e \u0432\u0441\u0435\u043c \u0431\u0435\u0437 \u0443\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f \u043b\u043e\u043a\u0430. \u0415\u0441\u043b\u0438 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043c\u0451\u0440\u0442\u0432\u043e\u0435, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e \u0432 <code>dead<\/code> set, \u0438 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043f\u043e\u0434 \u043b\u043e\u043a\u043e\u043c \u0443\u0434\u0430\u043b\u044f\u044e.<\/p>\n<p>broadcast_to_users \u0447\u0435\u0440\u0435\u0437 asyncio.gather \u0441 return_exceptions=True. \u042d\u0442\u043e \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u0430\u044f \u0440\u0430\u0441\u0441\u044b\u043b\u043a\u0430 \u0432\u0441\u0435\u043c \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u0430\u043c \u0433\u0440\u0443\u043f\u043f\u044b. \u041d\u0430 50 \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u043e\u0432 \u0433\u0440\u0443\u043f\u043f\u043e\u0432\u043e\u0433\u043e \u0447\u0430\u0442\u0430 \u044d\u0442\u043e 1-3\u043c\u0441 \u0432\u043c\u0435\u0441\u0442\u043e 50\u043c\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0440\u0430\u0441\u0441\u044b\u043b\u043a\u0438. \u041e\u0434\u0438\u043d \u0443\u043f\u0430\u0432\u0448\u0438\u0439 WebSocket \u043d\u0435 \u0440\u043e\u043d\u044f\u0435\u0442 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435, exceptions \u043b\u043e\u0432\u044f\u0442\u0441\u044f \u0438 \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u044e\u0442\u0441\u044f.<\/p>\n<figure class=\"full-width \"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/1ab\/7d4\/ad5\/1ab7d4ad528fdd8917d164baf763c448.png\" width=\"1767\" height=\"1167\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/1ab\/7d4\/ad5\/1ab7d4ad528fdd8917d164baf763c448.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/1ab\/7d4\/ad5\/1ab7d4ad528fdd8917d164baf763c448.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/figure>\n<p>\u041a\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u0441\u043b\u043e\u043c\u0430\u0435\u0442\u0441\u044f. \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c 2+ uvicorn \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u043c\u0435\u0435\u0442 \u0441\u0432\u043e\u0439 ConnectionManager \u0432 \u043f\u0430\u043c\u044f\u0442\u0438. \u042e\u0437\u0435\u0440 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0451\u043d \u043a \u0432\u043e\u0440\u043a\u0435\u0440\u0443 A, broadcast \u0438\u0434\u0451\u0442 \u0447\u0435\u0440\u0435\u0437 \u0432\u043e\u0440\u043a\u0435\u0440 B, \u044e\u0437\u0435\u0440 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435. \u0422\u0443\u0442 \u043d\u0443\u0436\u0435\u043d Redis Pub\/Sub \u043c\u0435\u0436\u0434\u0443 \u0432\u043e\u0440\u043a\u0435\u0440\u0430\u043c\u0438 \u0438\u043b\u0438 Postgres LISTEN\/NOTIFY. \u0423 \u043c\u0435\u043d\u044f \u043e\u0434\u0438\u043d \u0432\u043e\u0440\u043a\u0435\u0440, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<h3>pts: \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0430\u044f \u0441\u0442\u043e\u0440\u043e\u043d\u0430 sync state<\/h3>\n<p>\u0414\u043b\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e pts (Points of Tracking State), \u043a\u0430\u043a \u0432 MTProto Telegram. \u042f \u0443\u0436\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u043b \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u0440\u043e \u043a\u044d\u0448 (\u0441\u0441\u044b\u043b\u043a\u0430). \u0417\u0434\u0435\u0441\u044c \u043f\u043e\u043a\u0430\u0436\u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0443\u044e.<\/p>\n<pre><code class=\"python\">def _get_next_pts(cursor, chat_id: str) -&gt; int:    \"\"\"\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u043e \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0435\u0442 pts \u0434\u043b\u044f \u0447\u0430\u0442\u0430 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.\"\"\"    cursor.execute(\"\"\"        INSERT INTO chat_pts (chat_id, pts) VALUES (?, 1)        ON CONFLICT(chat_id) DO UPDATE SET pts = pts + 1    \"\"\", (chat_id,))    cursor.execute(\"SELECT pts FROM chat_pts WHERE chat_id = ?\", (chat_id,))    row = cursor.fetchone()    return row[\"pts\"] if row else 1def pts_increment(cursor, chat_id: str, event: str, data: dict) -&gt; int:    \"\"\"\u0415\u0434\u0438\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430 \u0432\u0445\u043e\u0434\u0430: \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442 pts, \u043f\u0438\u0448\u0435\u0442 \u043b\u043e\u0433.\"\"\"    pts = _get_next_pts(cursor, chat_id)    _log_pts_event(cursor, chat_id, pts, event, data)    return pts<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>pts \u044d\u0442\u043e \u043c\u043e\u043d\u043e\u0442\u043e\u043d\u043d\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u044e\u0449\u0438\u0439 \u0441\u0447\u0451\u0442\u0447\u0438\u043a \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0447\u0430\u0442\u0430. \u041f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 (\u043d\u043e\u0432\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435, \u0440\u0435\u0430\u043a\u0446\u0438\u044f, \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435) pts \u0440\u0430\u0441\u0442\u0451\u0442 \u043d\u0430 1. \u041a\u043b\u0438\u0435\u043d\u0442 \u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u0432\u043e\u0439 pts \u0438 \u043f\u0440\u0438 \u0440\u0435\u043a\u043e\u043d\u043d\u0435\u043a\u0442\u0435 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 <code>getDifference(pts=N)<\/code>. \u0421\u0435\u0440\u0432\u0435\u0440 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u0441\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e pts.<\/p>\n<p>\u042d\u0442\u043e \u043d\u0430\u0434\u0451\u0436\u043d\u0435\u0435 timestamp. Timestamp \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0432\u043f\u0430\u0441\u0442\u044c \u0443 \u0434\u0432\u0443\u0445 \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u043c\u043e\u0436\u0435\u0442 \u043f\u0435\u0440\u0435\u0441\u043a\u043e\u0447\u0438\u0442\u044c \u0438\u0437-\u0437\u0430 \u0441\u043c\u0435\u043d\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0442\u043e\u0447\u043d\u044b\u043c \u0438\u0437-\u0437\u0430 \u043e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u044f. pts \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043e\u0432.<\/p>\n<p><code>INSERT ... ON CONFLICT DO UPDATE<\/code> \u044d\u0442\u043e \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0432 SQLite (\u0438 \u0432 Postgres, \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0442\u043e\u0442 \u0436\u0435). \u0411\u0435\u0437 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439, \u0431\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a, \u0431\u0435\u0437 race condition \u043c\u0435\u0436\u0434\u0443 \u0434\u0432\u0443\u043c\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438 INSERT.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0435, \u0442\u0430\u043c \u0441\u043b\u043e\u0436\u043d\u0435\u0435. \u041d\u0443\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c pts \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u044d\u0448 \u0438 \u043f\u0440\u0438 \u0445\u043e\u043b\u043e\u0434\u043d\u043e\u043c \u0441\u0442\u0430\u0440\u0442\u0435 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0431\u0435\u0437 \u043f\u043e\u043b\u043d\u043e\u0439 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438. \u042d\u0442\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043d\u043e \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0441\u0435\u0440\u0438\u0438 \u043f\u0440\u043e \u043a\u044d\u0448.<\/p>\n<h3>\u0427\u0442\u043e \u0431\u044b \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u043f\u043e-\u0434\u0440\u0443\u0433\u043e\u043c\u0443<\/h3>\n<p>\u0427\u0442\u043e \u0431\u044b \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u043f\u043e-\u0434\u0440\u0443\u0433\u043e\u043c\u0443, \u0435\u0441\u043b\u0438 \u0431\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u043b \u0441\u0435\u0439\u0447\u0430\u0441:<\/p>\n<p><strong>\u0421\u0440\u0430\u0437\u0443 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u043b \u0431\u044b \u043d\u0430 2-3 \u0444\u0430\u0439\u043b\u0430<\/strong>. \u041d\u0435 \u043d\u0430 \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0443\u043b\u0438, \u0430 \u043f\u0440\u043e\u0441\u0442\u043e <a href=\"http:\/\/auth.py\" rel=\"noopener noreferrer nofollow\"><code>auth.py<\/code><\/a>, <a href=\"http:\/\/chats.py\" rel=\"noopener noreferrer nofollow\"><code>chats.py<\/code><\/a>, <a href=\"http:\/\/admin.py\" rel=\"noopener noreferrer nofollow\"><code>admin.py<\/code><\/a> \u043f\u043b\u044e\u0441 <a href=\"http:\/\/main.py\" rel=\"noopener noreferrer nofollow\">main.py<\/a> \u0441 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c. 19\u043a \u0441\u0442\u0440\u043e\u043a \u0432 \u043e\u0434\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0432\u0441\u0451-\u0442\u0430\u043a\u0438 \u043c\u043d\u043e\u0433\u043e. 8-10\u043a \u043d\u0430 \u0444\u0430\u0439\u043b \u0431\u044b\u043b\u043e \u0431\u044b \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e.<\/p>\n<p><strong>\u041d\u0435 \u0434\u0435\u043b\u0430\u043b \u0431\u044b \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438 \u0432 \u043c\u043e\u043d\u043e\u043b\u0438\u0442<\/strong>. \u0423 \u043c\u0435\u043d\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0438 \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440, \u0438 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438 \u0441 Botemy, \u0438 Lyra-bridge, \u0438 admin-\u043f\u0430\u043d\u0435\u043b\u044c. \u041f\u043e-\u0445\u043e\u0440\u043e\u0448\u0435\u043c\u0443 \u044d\u0442\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0434\u0435\u043f\u043b\u043e\u044f\u0442\u0441\u044f, \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043b\u043e\u043c\u0430\u044e\u0442\u0441\u044f, \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043d\u0430\u0433\u0440\u0443\u0436\u0430\u044e\u0442\u0441\u044f. \u0421\u0435\u0439\u0447\u0430\u0441 \u0435\u0441\u043b\u0438 \u043f\u0430\u0434\u0430\u0435\u0442 \u043e\u0434\u0438\u043d \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0430\u0434\u043c\u0438\u043d\u043a\u0438, \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0434\u0435\u0442\u044c \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440.<\/p>\n<p><strong>\u041f\u043e\u0441\u0442\u0430\u0432\u0438\u043b \u0431\u044b Postgres \u0441\u0440\u0430\u0437\u0443<\/strong>. \u041d\u0435 \u0434\u043b\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u0430 \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u043e\u043c \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445. SQLite \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043d\u043e \u043f\u0440\u0438 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 30 \u0413\u0411 accumulated data \u044d\u0442\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u0432\u0435\u0441\u0442. \u041b\u0443\u0447\u0448\u0435 \u043d\u0430\u0447\u0430\u0442\u044c \u0441 Postgres \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c SQLite.<\/p>\n<p><strong>\u041f\u043e\u0441\u0442\u0430\u0432\u0438\u043b \u0431\u044b Sentry \u0438\u043b\u0438 \u043f\u043e\u0445\u043e\u0436\u0438\u0439 error tracking \u0441 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0434\u043d\u044f<\/strong>. \u0421\u0435\u0439\u0447\u0430\u0441 \u0443 \u043c\u0435\u043d\u044f \u043b\u043e\u0433\u0438 \u0432 stdout, \u0438 \u044f \u0445\u043e\u0436\u0443 \u043f\u043e \u043d\u0438\u043c \u0433\u043b\u0430\u0437\u0430\u043c\u0438. \u041d\u0430 production-\u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u044d\u0442\u043e \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. Sentry \u0438\u043b\u0438 Glitchtip \u044d\u0442\u043e \u0432\u0435\u0447\u0435\u0440 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0438 \u043f\u043e\u0442\u043e\u043c \u0433\u043e\u0434\u044b \u0441\u043f\u043e\u043a\u043e\u0439\u043d\u043e\u0439 \u0436\u0438\u0437\u043d\u0438.<\/p>\n<h3>\u041a\u043e\u0433\u0434\u0430 \u044d\u0442\u043e\u0442 \u0441\u0442\u0435\u043a \u041d\u0415 \u043f\u043e\u0434\u043e\u0439\u0434\u0451\u0442<\/h3>\n<p>\u0420\u0435\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u043e, \u0433\u0434\u0435 \u043c\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c, \u0430 \u0433\u0434\u0435 \u043d\u0435\u0442.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u043c: \u0441\u043e\u043b\u043e-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0438\u043b\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0434\u043e 3 \u0447\u0435\u043b\u043e\u0432\u0435\u043a, \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438\u043b\u0438 \u0432\u0435\u0431 \u0441 \u0440\u0430\u0437\u0443\u043c\u043d\u043e\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u043e\u0439 (\u0442\u044b\u0441\u044f\u0447\u0438 RPS \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c), \u043c\u043e\u043d\u043e\u043b\u0438\u0442\u043d\u044b\u0439 \u0434\u043e\u043c\u0435\u043d (\u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0434\u0443\u043a\u0442), \u043d\u0435\u0442 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0439 99.99% uptime.<\/p>\n<p>\u041d\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c: \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043e\u0442 5 \u0447\u0435\u043b\u043e\u0432\u0435\u043a, \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u043a horizontal scaling, \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u0442\u044b\u0441\u044f\u0447 RPS, \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u043e\u0432 \u0441 \u0448\u0430\u0440\u0438\u043d\u0433\u043e\u043c \u0411\u0414, \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f (GDPR, HIPAA) \u0441 extensive audit logging.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u0448 \u043f\u0440\u043e\u0444\u0438\u043b\u044c \u0438\u0437 \u043f\u0435\u0440\u0432\u043e\u0439 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438, \u043c\u043e\u0439 \u0441\u0442\u0435\u043a \u0440\u0430\u0437\u0443\u043c\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440. \u0415\u0441\u043b\u0438 \u0438\u0437 \u0432\u0442\u043e\u0440\u043e\u0439, \u043d\u0443\u0436\u043d\u044b \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u044b, Kubernetes, Postgres \u0441 \u0440\u0435\u043f\u043b\u0438\u043a\u0430\u0446\u0438\u0435\u0439, \u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 DevOps. \u042d\u0442\u043e \u0434\u0440\u0443\u0433\u0430\u044f \u043b\u0438\u0433\u0430.<\/p>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u0442\u0435\u0437\u0438\u0441. \u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u0442\u0435\u043a \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442\u0435 \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043b\u0443\u0447\u0448\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e\u0442 \u0432\u0441\u0435. SQLite \u0432\u043c\u0435\u0441\u0442\u043e Postgres \u043d\u0435 \u0434\u043b\u044f \u0432\u0441\u0435\u0445, \u043d\u043e \u0434\u043b\u044f \u0441\u043e\u043b\u043e-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0447\u0430\u0441\u0442\u043e \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e. \u041c\u043e\u043d\u043e\u043b\u0438\u0442 \u0432\u043c\u0435\u0441\u0442\u043e \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u043d\u0435 \u0434\u043b\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u044b, \u043d\u043e \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e. \u0421\u0430\u043c\u043e\u043f\u0438\u0441\u043d\u044b\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u043e alembic, \u044d\u0442\u043e \u043a\u043e\u043c\u043f\u0440\u043e\u043c\u0438\u0441\u0441 \u043c\u0435\u0436\u0434\u0443 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c\u044e \u0438 \u0444\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>\u042f \u0447\u0430\u0441\u0442\u043e \u0432\u0438\u0436\u0443 \u043a\u0430\u043a \u0441\u043e\u043b\u043e-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043c\u0443\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u044e\u0442 Kubernetes, Redis-\u043a\u043b\u0430\u0441\u0442\u0435\u0440, RabbitMQ \u0434\u043b\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0435\u0440\u0436\u0438\u0442 50 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439. \u042d\u0442\u043e \u043a\u0430\u0440\u0433\u043e-\u043a\u0443\u043b\u044c\u0442, \u043d\u0435 \u0438\u043d\u0436\u0435\u043d\u0435\u0440\u0438\u044f. \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0442\u043e\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0440\u0435\u0430\u043b\u044c\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e, \u0430 \u043d\u0435 \u0442\u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 &#171;\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u043c \u0438\u043d\u0434\u0443\u0441\u0442\u0440\u0438\u0438&#187;.<\/p>\n<p>\u0412 Telegram-\u043a\u0430\u043d\u0430\u043b\u0435 \u043f\u0438\u0448\u0443 \u043c\u0435\u043b\u043a\u0438\u0435 \u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f \u043f\u043e \u0433\u043e\u0440\u044f\u0447\u0435\u043c\u0443. \u041d\u0435\u0434\u0430\u0432\u043d\u043e \u0431\u044b\u043b\u0438 \u043f\u043e\u0441\u0442\u044b \u043f\u0440\u043e race condition \u0432 outbox, attention degradation \u0432 \u0434\u043b\u0438\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u043c\u043f\u0442\u0430\u0445. \u0418\u043d\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0432\u043e\u0436\u0443 \u043a\u0443\u0441\u043a\u0438 \u043a\u043e\u0434\u0430 \u043f\u0440\u044f\u043c\u043e \u0441\u043e \u0441\u043a\u0440\u0438\u043d\u0448\u043e\u0442\u0430\u043c\u0438 IDE. \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e: <a href=\"https:\/\/t.me\/SkillTrackr\" rel=\"noopener noreferrer nofollow\">https:\/\/t.me\/SkillTrackr<\/a>.<\/p>\n<hr\/>\n<p>\u042d\u0442\u043e \u0432\u043e\u0441\u044c\u043c\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0438\u0437 \u0441\u0435\u0440\u0438\u0438. \u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0431\u044b\u043b \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0438\u0435\u043d\u0442 ONEMIX: <a href=\"#\" rel=\"noopener noreferrer nofollow\">\u0442\u0440\u0451\u0445\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0439 \u043a\u044d\u0448<\/a>, <a href=\"#\" rel=\"noopener noreferrer nofollow\">Double Ratchet E2E<\/a>, <a href=\"#\" rel=\"noopener noreferrer nofollow\">WebRTC \u0437\u0432\u043e\u043d\u043a\u0438<\/a>, <a href=\"#\" rel=\"noopener noreferrer nofollow\">vanilla Electron<\/a>, <a href=\"#\" rel=\"noopener noreferrer nofollow\">outbox-\u043f\u0430\u0442\u0442\u0435\u0440\u043d<\/a>. \u0410 \u0442\u0430\u043a\u0436\u0435 <a href=\"#\" rel=\"noopener noreferrer nofollow\">\u043c\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e \u0432\u0430\u0439\u0431-\u043a\u043e\u0434\u0438\u043d\u0433<\/a> \u0438 <a href=\"#\" rel=\"noopener noreferrer nofollow\">AI-\u0430\u0433\u0435\u043d\u0442 \u041b\u0438\u0440\u0430<\/a>. \u042d\u0442\u0430 \u043f\u0440\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u043a\u0443\u0441\u043a\u0438 \u2014 \u043a\u0430\u043a \u0438\u043c\u0435\u043d\u043d\u043e \u0443\u0441\u0442\u0440\u043e\u0435\u043d SMS-auth flow, \u043a\u0430\u043a pts \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0435, \u043a\u0430\u043a \u043f\u043e\u0434\u043d\u044f\u0442\u044c Postgres-\u043c\u0438\u0433\u0440\u0430\u0446\u0438\u044e \u0431\u0435\u0437 \u0434\u0430\u0443\u043d\u0442\u0430\u0439\u043c\u0430 \u2014 \u043f\u0438\u0448\u0438\u0442\u0435, \u0440\u0430\u0437\u0431\u0435\u0440\u0443 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u0438\u043b\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<\/div>\n<p>\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/1034814\/\">https:\/\/habr.com\/ru\/articles\/1034814\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0423\u0440\u043e\u0432\u0435\u043d\u044c: middle\/senior backend \u0421\u0442\u0435\u043a: FastAPI 0.115, SQLite, WebSocket, slowapi, JWT-like sessions, Docker \u0427\u0442\u043e \u0432\u043d\u0443\u0442\u0440\u0438: \u043a\u0430\u043a \u0443 \u043c\u0435\u043d\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0431\u044d\u043a\u0435\u043d\u0434 \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440\u0430, \u043f\u043e\u0447\u0435\u043c\u0443 SQLite \u0432\u043c\u0435\u0441\u0442\u043e Postgres, \u043c\u043e\u043d\u043e\u043b\u0438\u0442 \u043d\u0430 19\u043a \u0441\u0442\u0440\u043e\u043a, \u0441\u0430\u043c\u043e\u043f\u0438\u0441\u043d\u044b\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438, sliding expiration \u0434\u043b\u044f \u0442\u043e\u043a\u0435\u043d\u043e\u0432\u041f\u0440\u0435\u0430\u043c\u0431\u0443\u043b\u0430\u042d\u0442\u043e \u0432\u043e\u0441\u044c\u043c\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0438\u0437 \u043c\u043e\u0435\u0439 \u0441\u0435\u0440\u0438\u0438 \u043f\u0440\u043e \u0438\u043d\u0436\u0435\u043d\u0435\u0440\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432 ONEMIX. \u0414\u043e \u044d\u0442\u043e\u0433\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440\u0430: \u043a\u044d\u0448 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439, E2E, WebRTC \u0437\u0432\u043e\u043d\u043a\u0438, Electron, outbox-\u043f\u0430\u0442\u0442\u0435\u0440\u043d. \u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e AI-\u0430\u0433\u0435\u043d\u0442\u0430 \u041b\u0438\u0440\u0443 \u0438 \u043c\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e \u0432\u0430\u0439\u0431-\u043a\u043e\u0434\u0438\u043d\u0433.\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043f\u0440\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443. Backend ONEMIX \u2014 \u044d\u0442\u043e \u043e\u0434\u0438\u043d \u0444\u0430\u0439\u043b main.py \u043d\u0430 19603 \u0441\u0442\u0440\u043e\u043a\u0438, 379 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u043e\u0432, FastAPI + SQLite, \u0434\u0435\u0440\u0436\u0438\u0442 \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440 \u0441 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0435\u0439 \u0447\u0435\u0440\u0435\u0437 SMS, \u0437\u0432\u043e\u043d\u043a\u0430\u043c\u0438 \u0447\u0435\u0440\u0435\u0437 LiveKit, E2E \u0447\u0435\u0440\u0435\u0437 Double Ratchet, push-\u043d\u043e\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f\u043c\u0438 \u043d\u0430 iOS \u0438 Android. \u042d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b \u044f \u043f\u0438\u0448\u0443 \u0431\u043e\u043b\u044c\u0448\u0435 \u0433\u043e\u0434\u0430. \u0417\u0430 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043e\u043d \u044d\u0432\u043e\u043b\u044e\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043b \u0438\u0437 \u043f\u0440\u043e\u0442\u043e\u0442\u0438\u043f\u0430 \u043d\u0430 800 \u0441\u0442\u0440\u043e\u043a \u0432 production \u043c\u043e\u043d\u043e\u043b\u0438\u0442.\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0443 \u043f\u043e\u0447\u0435\u043c\u0443 \u0441\u0442\u0435\u043a \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a\u043e\u0439, \u043a\u0430\u043a\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043e\u043a\u0430\u0437\u0430\u043b\u0438\u0441\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c\u0438, \u043a\u0430\u043a\u0438\u0435 \u044f \u0431\u044b \u043f\u043e\u043c\u0435\u043d\u044f\u043b \u0435\u0441\u043b\u0438 \u0431\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u043b \u0441\u0435\u0439\u0447\u0430\u0441, \u0438 \u0433\u0434\u0435 \u0443 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u043e\u0441\u0442\u0438.\u0421\u0440\u0430\u0437\u0443 \u0432\u0430\u0436\u043d\u0430\u044f \u043e\u0433\u043e\u0432\u043e\u0440\u043a\u0430. \u0423 \u043c\u0435\u043d\u044f \u043d\u0435 \u0431\u044b\u043b\u043e \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0435\u0440\u0436\u0430\u0442\u044c 100\u043a \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438\u043b\u0438 10\u043a RPS. \u042d\u0442\u043e \u0431\u044d\u043a\u0435\u043d\u0434 \u043f\u043e\u0434 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441 \u0442\u0440\u0430\u0444\u0438\u043a\u043e\u043c \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043b\u044f \u0441\u043e\u043b\u043e-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0440\u0430\u0437\u0443\u043c\u043d\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u043e\u043c\u0443. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0437\u0430\u0434\u0430\u0447\u0438 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0430, \u043c\u043e\u0439 \u043e\u043f\u044b\u0442 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u043f\u043e\u0434\u043e\u0439\u0442\u0438.\u0421\u0442\u0435\u043a \u043e\u0434\u043d\u0438\u043c \u0432\u0437\u0433\u043b\u044f\u0434\u043e\u043c# requirements.txtfastapi==0.115.0uvicorn[standard]==0.30.6psycopg2-binary==2.9.9   # \u0434\u043b\u044f \u0431\u0443\u0434\u0443\u0449\u0435\u0439 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438, \u0441\u0435\u0439\u0447\u0430\u0441 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044fsqlite3                   # \u0438\u0437 stdlib, \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0411\u0414python-dotenv==1.0.1pydantic==2.8.2python-multipart==0.0.9httpx==0.27.0slowapi==0.1.9pillow==10.4.0pillow-heif==0.18.0aiofiles==24.1.0websockets==13.0PyJWT==2.9.0cryptography==43.0.115 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 ORM (SQLAlchemy \u043d\u0435\u0442), \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e Celery, \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e Redis. Pydantic \u0434\u043b\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 JSON, slowapi \u0434\u043b\u044f rate limiting, \u0432\u0441\u0451 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435.Docker-compose \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432: backend \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0438 Postgres. \u0422\u0443\u0442 \u0443\u0436\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442. Postgres \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0451\u043d, \u043d\u043e \u0432 \u043a\u043e\u0434\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f SQLite. \u041e\u0431\u044a\u044f\u0441\u043d\u044e \u0437\u0430\u0447\u0435\u043c \u0434\u0430\u043b\u044c\u0448\u0435.SQLite \u0432 production\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c main.py, \u0443\u0432\u0438\u0434\u0438\u0442\u0435 \u0432\u043e\u0442 \u044d\u0442\u043e:import sqlite3BASE_DIR = os.path.abspath(os.path.dirname(__file__))DATABASE_PATH = os.path.join(BASE_DIR, &#171;messenger.db&#187;)def get_db_connection():    conn = sqlite3.connect(DATABASE_PATH, check_same_thread=False)    conn.row_factory = sqlite3.Row    return conn\u042d\u0442\u043e production, \u043d\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0430\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430, \u043d\u0435 \u0442\u0435\u0441\u0442\u044b, \u043d\u0435 \u043f\u0440\u043e\u0442\u043e\u0442\u0438\u043f. SQLite \u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u0435\u0441\u044c \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440: \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438, \u0447\u0430\u0442\u044b, \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f, \u0440\u0435\u0430\u043a\u0446\u0438\u0438, \u0437\u0432\u043e\u043d\u043a\u0438, \u0441\u0435\u0441\u0441\u0438\u0438. \u041d\u0430 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 messenger.db.\u041f\u043e\u0447\u0435\u043c\u0443 SQLite \u0432\u043c\u0435\u0441\u0442\u043e Postgres \u0434\u043b\u044f production \u2014 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441. \u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u2014 Postgres \u0443 \u043c\u0435\u043d\u044f \u0433\u043e\u0442\u043e\u0432. Docker-compose \u043f\u043e\u0434\u043d\u0438\u043c\u0430\u0435\u0442 \u0435\u0433\u043e \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e, psycopg2-binary \u0432 requirements.txt, \u0435\u0441\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 POSTGRES_MIGRATION.md \u0441 \u043f\u043b\u0430\u043d\u043e\u043c \u043f\u0435\u0440\u0435\u0435\u0437\u0434\u0430. \u042f \u043c\u043e\u0433\u0443 \u043c\u0438\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0433\u0434\u0430 \u0437\u0430\u0445\u043e\u0447\u0443. \u041f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043a\u0430 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e.\u041f\u043e\u0447\u0435\u043c\u0443 SQLite \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f\u0412 \u043c\u0435\u0441\u0441\u0435\u043d\u0434\u0436\u0435\u0440\u0435 90% \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u044d\u0442\u043e INSERT \u043d\u043e\u0432\u044b\u0445 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0438 SELECT \u043f\u043e chat_id. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 JOIN&#8217;\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u0448\u0430\u0440\u0434\u0430\u043c\u0438, \u043d\u0438\u043a\u0430\u043a\u043e\u0439 \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0438 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043e\u0442\u0447\u0451\u0442\u043e\u0432 \u043f\u043e \u043e\u0433\u0440\u043e\u043c\u043d\u044b\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c. SQLite \u043d\u0430 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 SSD \u0434\u0435\u0440\u0436\u0438\u0442 \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u0442\u044b\u0441\u044f\u0447 INSERT \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435. \u042d\u0442\u043e\u0433\u043e \u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0437\u0430\u043f\u0430\u0441\u043e\u043c.WAL-\u0440\u0435\u0436\u0438\u043c (Write-Ahead Logging) \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u0447\u043a\u043e\u0439 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0447\u0438\u0442\u0430\u0442\u044c \u0411\u0414 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u0441 \u0437\u0430\u043f\u0438\u0441\u044c\u044e. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 lock contention \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u0441\u0435 \u0431\u043e\u044f\u0442\u0441\u044f. \u041d\u0430 \u043c\u043e\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u0438\u0447\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 0.1-2\u043c\u0441.Backup \u2014 \u044d\u0442\u043e cp messenger.db messenger.db.bak. \u0411\u0435\u0437 mysqldump, \u0431\u0435\u0437 pg_dump, \u0431\u0435\u0437 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b. \u0424\u0430\u0439\u043b \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f \u2014 backup \u0433\u043e\u0442\u043e\u0432.\u0413\u0434\u0435 SQLite \u043d\u0435 \u0441\u043f\u0440\u0430\u0432\u0438\u0442\u0441\u044f\u042f \u043d\u0435 \u0434\u0435\u043b\u0430\u044e \u0432\u0438\u0434 \u0447\u0442\u043e SQLite \u044d\u0442\u043e \u0441\u0435\u0440\u0435\u0431\u0440\u044f\u043d\u0430\u044f \u043f\u0443\u043b\u044f. \u0413\u0434\u0435 \u043e\u043d\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0438\u0433\u0440\u0430\u0435\u0442:\u041c\u043d\u043e\u0433\u043e\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 5 uvicorn-\u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432 \u043f\u0438\u0448\u0443\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u2014 \u0431\u0443\u0434\u0435\u0442 lock contention. \u042f \u0434\u0435\u0440\u0436\u0443 \u043e\u0434\u0438\u043d \u0432\u043e\u0440\u043a\u0435\u0440 \u0441 asyncio, \u0438 \u044d\u0442\u043e\u0433\u043e \u0445\u0432\u0430\u0442\u0430\u0435\u0442.\u0421\u0435\u0442\u0435\u0432\u043e\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0411\u0414. SQLite \u044d\u0442\u043e \u0444\u0430\u0439\u043b, \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043c\u0430\u0448\u0438\u043d\u0430\u043c\u0438. \u0415\u0441\u043b\u0438 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e \u2014 \u043d\u0443\u0436\u0435\u043d \u0441\u0435\u0442\u0435\u0432\u043e\u0439 DB.\u0421\u043b\u043e\u0436\u043d\u0430\u044f \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0430. \u0415\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f crosstable JOIN&#8217;\u044b \u043f\u043e \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430\u043c \u0441\u0442\u0440\u043e\u043a \u2014 Postgres \u043b\u0443\u0447\u0448\u0435, SQLite \u044d\u0442\u043e OLTP-only.Postgres-\u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u044b\u0435 \u0444\u0438\u0447\u0438. JSON-\u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b, GIS, full-text search \u0447\u0435\u0440\u0435\u0437 pg_trgm \u2014 \u044d\u0442\u043e \u0432\u0441\u0451 \u0431\u043e\u0433\u0430\u0447\u0435 \u0447\u0435\u043c SQLite. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u2014 Postgres.\u041a\u043e\u0433\u0434\u0430 \u044f \u043c\u0438\u0433\u0440\u0438\u0440\u0443\u044e\u0422\u0440\u0438\u0433\u0433\u0435\u0440 \u0434\u043b\u044f \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u044d\u0442\u043e \u043d\u0435 &#171;\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0441\u0442\u0430\u043b\u043e \u0431\u043e\u043b\u044c\u0448\u0435&#187;, \u0430 &#171;\u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0437\u0430\u0434\u0430\u0447\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e SQLite \u043d\u0435 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442&#187;. \u041f\u043e\u043a\u0430 \u0435\u0451 \u043d\u0435\u0442.\u0415\u0441\u043b\u0438 \u0437\u0430\u0432\u0442\u0440\u0430 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u0430\u044f \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0430 \u2014 \u043c\u0438\u0433\u0440\u0438\u0440\u0443\u044e \u0437\u0430 \u043d\u0435\u0434\u0435\u043b\u044e. \u041f\u043b\u0430\u043d \u0437\u0430\u043f\u0438\u0441\u0430\u043d, \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0433\u043e\u0442\u043e\u0432\u0430.\u041c\u043e\u043d\u043e\u043b\u0438\u0442 \u043d\u0430 19603 \u0441\u0442\u0440\u043e\u043a\u0438\u0412\u0442\u043e\u0440\u043e\u0439 \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u2014 \u043e\u0434\u0438\u043d \u0444\u0430\u0439\u043b main.py \u043d\u0430 19603 \u0441\u0442\u0440\u043e\u043a\u0438 \u0441 379 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u0430\u043c\u0438. \u041d\u0435 \u043f\u0430\u043f\u043a\u0430 \u0441 \u043c\u043e\u0434\u0443\u043b\u044f\u043c\u0438, \u043d\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u043e \u0434\u043e\u043c\u0435\u043d\u0430\u043c, \u043d\u0435 APIRouter&#8217;\u044b.\u042d\u0442\u043e \u0441\u043e\u0437\u043d\u0430\u0442\u0435\u043b\u044c\u043d\u043e. \u041e\u0431\u044a\u044f\u0441\u043d\u044e \u043f\u043e\u0447\u0435\u043c\u0443.\u041a\u043e\u0433\u0434\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0448\u044c \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0448\u044c \u0431\u044d\u043a\u0435\u043d\u0434 \u0432 \u043e\u0434\u0438\u043d\u043e\u0447\u043a\u0443, \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u0440\u0435\u0441\u0443\u0440\u0441 \u044d\u0442\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438 \u0432 \u043a\u043e\u0434\u0435. \u041a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \/chats\/{id}\/messages, \u043d\u0443\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c \u0433\u0434\u0435 \u043e\u043d \u043b\u0435\u0436\u0438\u0442. \u0412 \u043c\u043e\u043d\u043e\u043b\u0438\u0442\u0435 \u043e\u0442\u0432\u0435\u0442 \u043e\u0434\u0438\u043d: \u0432 main.py. \u0412 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u0430\u0445 \u0438\u043b\u0438 \u043c\u0443\u043b\u044c\u0442\u0438-\u043c\u043e\u0434\u0443\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u2014 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043a\u0430\u043a\u0430\u044f \u043f\u0430\u043f\u043a\u0430, \u043a\u0430\u043a\u043e\u0439 router, \u043a\u0430\u043a\u043e\u0439 \u0444\u0430\u0439\u043b.\u0421 Cmd+P \u0432 IDE \u044f \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u044e main.py \u0437\u0430 \u0441\u0435\u043a\u0443\u043d\u0434\u0443. \u0421 Cmd+Shift+O (Go to Symbol) \u043d\u0430\u0445\u043e\u0436\u0443 \u043b\u044e\u0431\u043e\u0439 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0438\u0445 379. \u042d\u0442\u0430 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043b\u043e\u0445\u043e \u0432 \u043c\u0443\u043b\u044c\u0442\u0438\u043c\u043e\u0434\u0443\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445. \u0418\u043c\u0435\u043d\u0430 \u043a\u043e\u043b\u043b\u0438\u0437\u0438\u043e\u043d\u0438\u0440\u0443\u044e\u0442 \u043c\u0435\u0436\u0434\u0443 \u043c\u043e\u0434\u0443\u043b\u044f\u043c\u0438, \u043d\u0443\u0436\u043d\u043e \u0445\u043e\u0434\u0438\u0442\u044c \u043f\u043e \u043f\u0430\u043f\u043a\u0430\u043c.\u0427\u0442\u043e \u044f \u0434\u0435\u043b\u0430\u044e \u0447\u0442\u043e\u0431\u044b 19\u043a \u0441\u0442\u0440\u043e\u043a \u043d\u0435 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u043b\u0438\u0441\u044c \u0432 \u0445\u0430\u043e\u0441:\u041b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u0438 \u0431\u043b\u043e\u0447\u043d\u044b\u043c\u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c\u0438. \u0413\u0440\u0443\u043f\u043f\u044b \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u043e\u0432 \u043e\u0442\u0434\u0435\u043b\u0435\u043d\u044b \u0442\u0430\u043a\u0438\u043c\u0438 \u0431\u043b\u043e\u043a\u0430\u043c\u0438:# ============ AUTH ============# ============ CHATS ============# ============ MESSAGES ============\u0412 IDE \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 code folding \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0447\u0442\u0438 \u043a\u0430\u043a \u043f\u0430\u043f\u043a\u0438.Stand-alone \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u044b. \u041a\u0430\u0436\u0434\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f-\u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u0430, \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043e\u0431\u0449\u0438\u0445 \u0445\u0435\u043b\u043f\u0435\u0440\u043e\u0432 \u043d\u0430 \u043f\u043e\u043b\u0441\u0442\u0430\u0442\u044c\u0438. \u0427\u0438\u0442\u0430\u044e \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u2014 \u0432\u0441\u044f \u0435\u0433\u043e \u043b\u043e\u0433\u0438\u043a\u0430 \u0432 \u043d\u0451\u043c \u0436\u0435, \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u044b\u0433\u0430\u0442\u044c \u043f\u043e 5 \u0444\u0430\u0439\u043b\u0430\u043c.Pydantic-\u043c\u043e\u0434\u0435\u043b\u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u0431\u043b\u043e\u043a\u0435 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u0444\u0430\u0439\u043b\u0430. \u0412\u0441\u0435 request\/response \u0441\u0445\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432\u043c\u0435\u0441\u0442\u0435, \u043b\u0435\u0433\u043a\u043e \u043d\u0430\u0439\u0442\u0438.\u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u0439 grep. \u041a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043f\u043e\u043c\u043d\u044e \u0433\u0434\u0435 \u0447\u0442\u043e, \u0434\u0435\u043b\u0430\u044e grep &#171;chats\/.*messages&#187; main.py. \u041d\u0430\u0439\u0434\u0451\u0442 \u0437\u0430 \u0441\u0435\u043a\u0443\u043d\u0434\u0443.\u041a\u043e\u0433\u0434\u0430 \u043c\u043e\u043d\u043e\u043b\u0438\u0442 \u0441\u043b\u043e\u043c\u0430\u0435\u0442\u0441\u044f. \u0415\u0441\u043b\u0438 \u0431\u044b \u044f \u043d\u0430\u043d\u0438\u043c\u0430\u043b \u0432\u0442\u043e\u0440\u043e\u0433\u043e backend-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430, \u043c\u043e\u043d\u043e\u043b\u0438\u0442 \u0431\u044b\u043b \u0431\u044b \u043f\u043b\u043e\u0445\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c. \u0414\u0432\u0430 \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0432 \u043e\u0434\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0434\u0430\u044e\u0442 merge-\u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u044b \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u043a\u043e\u043c\u043c\u0438\u0442\u0435. \u042f \u0431\u044b \u0442\u043e\u0433\u0434\u0430 \u0440\u0430\u0437\u0431\u0438\u043b \u043f\u043e \u0434\u043e\u043c\u0435\u043d\u0430\u043c \u0441 APIRouter \u0438 \u043f\u0430\u043a\u0435\u0442\u0430\u043c\u0438. \u0421\u0435\u0439\u0447\u0430\u0441 \u043e\u0434\u0438\u043d \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0438 \u043e\u0434\u0438\u043d \u0444\u0430\u0439\u043b, \u044d\u0442\u043e \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e.\u0421\u0430\u043c\u043e\u043f\u0438\u0441\u043d\u044b\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0431\u0435\u0437 alembic\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0441\u043e\u0432\u0435\u0442 \u0434\u043b\u044f FastAPI + \u0411\u0414, \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c alembic. \u042f \u043d\u0435 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043b, \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0434\u0435\u043b\u0430\u044e \u0441\u0430\u043c:def add_column_if_missing(table_name, column_name, column_type, index=False):    cursor.execute(f&#187;PRAGMA table_info({table_name})&#187;)    columns = [row[1] for row in cursor.fetchall()]    if column_name not in columns:        print(f&#187;[DB] Migrating {table_name}: adding {column_name} column&#187;)        try:            cursor.execute(f&#187;ALTER TABLE {table_name} ADD COLUMN {column_name} {column_type}&#187;)            if index:                cursor.execute(f&#187;CREATE UNIQUE INDEX IF NOT EXISTS idx_{table_name}_{column_name} ON {table_name}({column_name})&#187;)            conn.commit()        except Exception as e:            print(f&#187;[DB] Migration error ({table_name}.{column_name}): {e}&#187;)# \u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435add_column_if_missing(&#8216;users&#8217;, &#8216;phone&#8217;, &#8216;TEXT&#8217;, index=True)add_column_if_missing(&#8216;users&#8217;, &#8216;username&#8217;, &#8216;TEXT&#8217;, index=True)add_column_if_missing(&#8216;users&#8217;, &#8216;bio&#8217;, &#8216;TEXT&#8217;)add_column_if_missing(&#8216;chats&#8217;, &#8216;is_private&#8217;, &#8216;INTEGER DEFAULT 0&#8217;)# &#8230; \u0435\u0449\u0451 50 \u0442\u0430\u043a\u0438\u0445 \u0441\u0442\u0440\u043e\u043a\u042d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 self-healing \u043f\u043e\u0434\u0445\u043e\u0434. \u041f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 init_db() \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u043f\u043e \u0432\u0441\u0435\u043c \u043d\u0443\u0436\u043d\u044b\u043c \u043a\u043e\u043b\u043e\u043d\u043a\u0430\u043c \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0435. SQLite \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 ALTER TABLE ADD COLUMN \u0431\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a.\u041f\u043e\u0447\u0435\u043c\u0443 \u043d\u0435 alembic. Alembic \u044d\u0442\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0441\u043e \u0441\u0432\u043e\u0435\u0439 CLI, \u0441\u0432\u043e\u0435\u0439 \u043d\u0443\u043c\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439, \u0441\u0432\u043e\u0438\u043c\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0444\u0430\u0439\u043b\u0430\u043c\u0438. \u0427\u0442\u043e\u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u043b\u043e\u043d\u043a\u0443 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c migration file, \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c upgrade\/downgrade, \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c alembic upgrade head, \u043d\u0435 \u0437\u0430\u0431\u044b\u0442\u044c \u0437\u0430\u043a\u043e\u043c\u043c\u0438\u0442\u0438\u0442\u044c migration file. \u042d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u0435 \u0433\u0434\u0435 \u043d\u0443\u0436\u043d\u0430 \u0444\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u0414\u043b\u044f \u043e\u0434\u0438\u043d\u043e\u0447\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u044d\u0442\u043e overhead.\u0421 \u043c\u043e\u0438\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u043c \u043f\u0440\u043e\u0449\u0435. \u0414\u043e\u0431\u0430\u0432\u0438\u043b \u0441\u0442\u0440\u043e\u043a\u0443 add_column_if_missing, \u0440\u0435\u0441\u0442\u0430\u0440\u0442\u0430\u043d\u0443\u043b \u0441\u0435\u0440\u0432\u0435\u0440, \u0433\u043e\u0442\u043e\u0432\u043e. \u0414\u0435\u0432-\u0446\u0438\u043a\u043b \u0432 3 \u0440\u0430\u0437\u0430 \u043a\u043e\u0440\u043e\u0447\u0435.\u0413\u0434\u0435 \u044d\u0442\u043e \u0441\u043b\u043e\u043c\u0430\u0435\u0442\u0441\u044f. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u044b \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0441 \u043f\u0435\u0440\u0435\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u043b\u043e\u043d\u043e\u043a, \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0435\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446, \u043d\u0443\u0436\u0435\u043d \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u043f\u043e\u0441\u0435\u0440\u044c\u0451\u0437\u043d\u0435\u0435. \u0423 \u043c\u0435\u043d\u044f \u0442\u0430\u043a\u0438\u0445 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439 \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u044b\u043b\u043e. \u041a\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u2014 \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044e alembic \u0440\u0435\u0442\u0440\u043e\u0441\u043f\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u043b\u0438 \u043c\u0438\u0433\u0440\u0438\u0440\u0443\u044e \u043d\u0430 Postgres + \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442.\u0418 \u044f \u043d\u0435 \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u044e \u0447\u0442\u043e \u044d\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0434\u043b\u044f \u0432\u0441\u0435\u0445. \u042d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043b\u044f \u043c\u043e\u0435\u0433\u043e \u043f\u0440\u043e\u0444\u0438\u043b\u044f \u0437\u0430\u0434\u0430\u0447. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u0435 5 \u0447\u0435\u043b\u043e\u0432\u0435\u043a \u0434\u0435\u043b\u0430\u044e\u0442 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438, alembic \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u0435\u043d, \u0438\u043d\u0430\u0447\u0435 \u0445\u0430\u043e\u0441 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d.Sessions \u0432\u043c\u0435\u0441\u0442\u043e JWT \u0441\u043e sliding expiration\u0422\u0440\u0435\u0442\u0438\u0439 \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440. Auth \u043d\u0435 \u0447\u0435\u0440\u0435\u0437 JWT, \u0430 \u0447\u0435\u0440\u0435\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 sessions \u0432 \u0411\u0414.def get_current_user(request, authorization, db):    if not authorization:        raise HTTPException(401, &#171;Not authenticated&#187;)        token = authorization.replace(&#171;Bearer &#171;, &#171;&#187;)    cursor = db.cursor()    cursor.execute(&#171;&#187;&#187;        SELECT s.user_id, s.expires_at, u.id, u.name, u.phone, &#8230;        FROM sessions s        JOIN users u ON s.user_id = u.id        WHERE s.token = ?    &#171;&#187;&#187;, (token,))    row = cursor.fetchone()    if not row:        raise HTTPException(401, &#171;Invalid token&#187;)        expires_at = datetime.fromisoformat(row[&#171;expires_at&#187;].replace(&#8216;Z&#8217;, &#8216;+00:00&#8217;))    if datetime.now(timezone.utc) &gt; expires_at:        raise HTTPException(401, &#171;Token expired&#187;)        # Sliding expiration \u2014 \u043f\u0440\u043e\u0434\u043b\u0435\u0432\u0430\u0435\u043c \u0441\u0440\u043e\u043a \u0436\u0438\u0437\u043d\u0438 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0442\u043e\u043a\u0435\u043d\u0430    days_left = (expires_at &#8212; now_dt).total_seconds() \/ 86400    if days_left &lt; 7:        new_expires = (now_dt + timedelta(days=30)).isoformat()        cursor.execute(&#171;UPDATE sessions SET expires_at = ? WHERE token = ?&#187;, (new_expires, token))        return {&#171;id&#187;: row[&#171;id&#187;], &#171;name&#187;: row[&#171;name&#187;], &#8230;}JWT \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u0430 sessions, \u0433\u0434\u0435 token \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0411\u0414 \u0438 \u0441\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435.\u0427\u0442\u043e \u044f \u043f\u043e\u043b\u0443\u0447\u0430\u044e \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 JWT:\u041c\u043e\u0436\u043d\u043e \u043e\u0442\u043e\u0437\u0432\u0430\u0442\u044c \u0442\u043e\u043a\u0435\u043d. JWT \u043e\u0442\u043e\u0437\u0432\u0430\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f \u0431\u0435\u0437 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e blacklist&#8217;\u0430. Sessions \u043f\u0440\u043e\u0441\u0442\u043e DELETE FROM sessions WHERE token = ? \u0438 \u0432\u0441\u0451, \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0441\u0440\u0430\u0437\u0443 \u0440\u0430\u0437\u043b\u043e\u0433\u0438\u043d\u0435\u043d.\u041c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u0441\u0435\u0441\u0441\u0438\u0438. SELECT \u043f\u043e user_id \u0438 \u044e\u0437\u0435\u0440 \u0432\u0438\u0434\u0438\u0442 \u0432\u0441\u0435 \u0441\u0432\u043e\u0438 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430, \u043c\u043e\u0436\u0435\u0442 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0435.Sliding expiration. \u042d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0448\u0442\u0443\u043a\u0430. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f JWT-\u0441\u0445\u0435\u043c\u0430: \u0442\u043e\u043a\u0435\u043d \u0436\u0438\u0432\u0451\u0442 1 \u0447\u0430\u0441, \u043f\u043e\u0442\u043e\u043c refresh-token \u043c\u0435\u043d\u044f\u0435\u0442 \u043d\u0430 \u043d\u043e\u0432\u044b\u0439. \u0423 \u043c\u0435\u043d\u044f \u043f\u043e-\u0434\u0440\u0443\u0433\u043e\u043c\u0443. \u0422\u043e\u043a\u0435\u043d&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-479617","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/479617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=479617"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/479617\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=479617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=479617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=479617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}