ЛР 3: Балансировка API и отказ реплики
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
# PostgreSQL schema and transactions
|
||||
|
||||
## Tables and invariants
|
||||
|
||||
- `users`: UUID primary key, unique constrained username, display name, Argon2 password hash, server timestamp.
|
||||
- `chats`: UUID primary key and title.
|
||||
- `chat_members`: composite `(chat_id, user_id)` primary key, foreign keys to both parent tables, unique stable member position per chat. The `(user_id, chat_id)` index supports listing a user's chats.
|
||||
- `messages`: UUID primary key and foreign keys to chat and sender. `(chat_id, created_at, id)` supports the API's ascending keyset history query.
|
||||
- `sessions`: SHA-256 digest of a random opaque token as primary key, user foreign key, creation and expiry timestamps. Raw tokens are returned only once and are never stored.
|
||||
- `idempotency_records`: unique per user, method, request path, and key; stores the request hash and committed response for 24 hours. Expired records are removed when a message is submitted.
|
||||
- `schema_migrations`: migration filename and application timestamp; created by the migration runner.
|
||||
|
||||
All timestamps are `TIMESTAMPTZ`. PostgreSQL owns generated UUIDs and creation timestamps. Named migrations run once in lexicographic order; an advisory transaction lock serializes startup migration attempts.
|
||||
|
||||
## Transaction boundaries
|
||||
|
||||
Each HTTP request uses one pooled PostgreSQL connection and one transaction. A successful response is written only after that transaction commits. Creating a chat and inserting every member is one transaction, so a failed member insert rolls back both the chat and any earlier member rows. Registration relies on the unique username constraint as the final arbiter of concurrent registrations; the losing request returns `409`.
|
||||
|
||||
Creating a message and recording its idempotency result share one transaction. PostgreSQL's composite primary key serializes concurrent requests across API replicas. Matching bodies replay the original status and message; a different body returns `409`. Authentication and chat membership are checked before replay.
|
||||
|
||||
Message history uses `WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT $4`, matching `messages_chat_history_idx`. Cursors include their resource scope and the last row's timestamp and UUID.
|
||||
|
||||
## Durability
|
||||
|
||||
Compose mounts PostgreSQL's data directory on the named `postgres_data` volume. The persistence hook restarts the API and database containers without removing that volume. Backups and destructive volume resets are deliberately separate operator actions.
|
||||
Reference in New Issue
Block a user