Add resilient Valkey User cache with PostgreSQL authorization

This commit is contained in:
Самохвалов Андрей Ремович
2026-10-02 11:25:10 +03:00
parent ed7a27629d
commit d59d3f4adc
8 changed files with 205 additions and 13 deletions
+48
View File
@@ -0,0 +1,48 @@
Lab 2 U4: Valkey User cache, 2026-10-02, macOS/Colima arm64.
Commands were run on the local Compose stack with PostgreSQL data volume preserved.
docker compose --env-file .env up -d --build --wait
PASS: api, postgres, valkey healthy; valkey/valkey:8.0.2-alpine.
docker compose --env-file .env exec -T api python - < student-tests/test_cache_failures.py
Ran 2 tests in 0.207s: OK.
SQL queries (miss/hit/reload): 2/1/2; Valkey hit/miss deltas: 1/2.
The reload deletes only the disposable test user's cache key.
The SQL counter wraps real psycopg connection.execute calls in the Store process.
Every authenticated request still checks its session in PostgreSQL; only User lookup is cached.
The test also points Valkey at a closed TCP port and verifies PostgreSQL fallback,
then revokes the token and verifies 401.
make test BASE_URL=http://127.0.0.1:8080
PASS: lab2 public API scenario with Valkey healthy.
docker compose --env-file .env stop valkey
make test BASE_URL=http://127.0.0.1:8080
docker compose --env-file .env up -d --wait valkey
PASS: same public API scenario with Valkey stopped; it was restored healthy.
Before persistence hook, docker inspect StartedAt (UTC):
api 2026-10-02T08:05:51.833412908Z
postgres 2026-10-02T07:24:25.230933733Z
valkey 2026-10-02T08:05:51.322012488Z
make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://127.0.0.1:8080
PASS: persistence preserved confirmed data and existing session.
After persistence hook, docker inspect StartedAt (UTC):
api 2026-10-02T08:07:11.335901585Z
postgres 2026-10-02T08:07:08.754444696Z
valkey 2026-10-02T08:07:08.754742886Z
All three containers really restarted; PostgreSQL volume was not deleted.
Manual HTTP failure run (one token held in process memory, no token printed):
Warm User cache through two authenticated GET /users/me requests.
Stop PostgreSQL: /health/ready=503, authenticated cached-User request=503.
Restore PostgreSQL: authenticated request=200.
Revoke session: DELETE /auth/sessions/current=204.
Stop/start Valkey: same revoked token still receives 401.
The HTTP observer needed timeout=15s; timeout=5s expired at the same boundary
as the configured PostgreSQL pool timeout and did not observe the 503.
make check
PASS: skeleton structure, refs and Python syntax (not an application test).