Files
zarinpal-analytics-backend/docs/decisions/0011-lazy-redis-analytics-cache.md

3.1 KiB
Raw Blame History

ADR 0011: Calculate analytics sections lazily and cache them in Redis

  • Status: Accepted
  • Date: 2026-08-20

Context

The hackathon dataset is static after its one-time bootstrap. Continuously refreshing analytics would spend resources before a merchant requests them. A first calculation may contain several independent dashboard sections, and the user should see completed sections without waiting for the slowest query. Future append-only rows may be written directly by ZarinPal, bypassing Go request handlers.

Decision

On first dashboard access, an authenticated idempotent POST creates or returns the current analytics run for that merchant/source/cohort/metric-definition revision. It enqueues four section ids—overview, payment_performance, fees, and peer_comparison—for bounded parallel calculation by a separate Go worker service. This worker is event-driven, not scheduled.

Cache each completed typed section in Redis using a key derived from the merchant id, merchant source revision, relevant cohort revision, metric-definition version, and section id. Use a 30-day TTL. Redis-backed idempotency/single-flight coordination prevents duplicate equivalent jobs. Old-revision keys expire naturally and are never treated as current.

Expose a separate authenticated GET SSE endpoint for the run. Stream stable event ids and typed events for section started, section ready, section failed, overall complete, and heartbeat. Support safe reconnect without exposing another merchant’s jobs or values. One section failure does not suppress sections that succeeded.

Give each section attempt a 60-second timeout. Retry only classified transient failures at most twice, for a maximum of three attempts. Use exponential backoff with jitter (initial nominal delay 1 second, then 2 seconds; jitter prevents synchronized retry bursts). Do not retry authorization, validation, unsupported-metric, or other deterministic failures.

Schema-owned PostgreSQL logic atomically increments the affected merchant revision whenever a related payment fact is inserted. Inserts affecting peer benchmarks also increment the corresponding category/cohort revision. This covers direct ZarinPal writes. Payment facts are append-only for the MVP; invalidation for update/delete remains out of scope.

Redis is a private disposable cache and coordination system, not a source of truth. PostgreSQL remains authoritative. Cached values contain approved aggregates only and exclude raw rows, payer-card keys, credentials, session tokens, and cross-tenant details.

Consequences

  • All dashboard analytics begin together, while the UI reveals each section independently as it completes.
  • Redis loss or key expiry causes safe lazy recomputation; Redis is excluded from backup/restore requirements.
  • No scheduled refresh or time-based freshness claim is required.
  • The deployment includes private Redis and on-demand analytics-worker services.
  • Global/per-merchant concurrency and SSE connection/replay-retention limits still require explicit values.
  • Trigger/revision behavior, tenant isolation, job deduplication, partial failures, SSE reconnect, and cache data minimization require integration/security tests.