Skip to content

feat(backend): Redis cache for quote Soroban simulations - #273

Merged
jhayniffy merged 2 commits into
InsurNiffy:mainfrom
Jambox11:feature/quote-simulation-redis-cache
Mar 29, 2026
Merged

feat(backend): Redis cache for quote Soroban simulations#273
jhayniffy merged 2 commits into
InsurNiffy:mainfrom
Jambox11:feature/quote-simulation-redis-cache

Conversation

@Jambox11

Copy link
Copy Markdown
Contributor

Summary

Adds a short-TTL Redis cache for successful Soroban generate_premium simulations behind POST /api/quote/generate-premium, keyed by a SHA-256 of normalized quote inputs plus contract id and network passphrase. Reduces duplicate RPC work for identical quotes within the TTL window.
closes #203

What changed

Quote simulation cache

  • Key: quote:sim:v1: + SHA-256(CONTRACT_ID + STELLAR_NETWORK_PASSPHRASE + canonical JSON with sorted fields: age, coverage_tier, policy_type, region, risk_score, source_account).
  • Stored: Successful responses with source: "simulation" only — not local_fallback, not thrown errors (avoids caching transient failures).
  • Config (env.validation.ts): QUOTE_SIMULATION_CACHE_ENABLED (default true), QUOTE_SIMULATION_CACHE_TTL_SECONDS (default 30, max 600).
  • Bypass: Cache-Control header containing the no-cache directive skips Redis read/write.
  • Metrics: quote_simulation_cache_requests_total{result="hit"|"miss"|"bypass"}.
  • Invalidation: Indexer calls cache delPattern when parseEvent returns niffyins:tbl_upd (multiplier table update).

Files

  • quote-simulation-cache-key.util.ts, quote-simulation-cache.service.ts
  • quote.service.ts, quote.controller.ts, quote.module.ts (exports cache service for indexer)
  • indexer.service.ts / indexer.module.ts — invalidation hook
  • metrics.service.ts — new counter
  • Docs: docs/quote-simulation-cache.md, docs/observability.md

Tests

  • quote-simulation-cache-key.util.test.ts — canonical JSON + hash stability
  • quote-simulation-cache.integration.test.ts — hit, miss, bypass, TTL expiry, no cache for local_fallback (in-memory TTL Redis mock)
cd backend && npx jest src/quote/quote-simulation-cache --runInBand

- SHA-256 keys from sorted normalized GeneratePremiumDto + contract + network
- Store only successful source=simulation results; TTL via QUOTE_SIMULATION_CACHE_TTL_SECONDS (default 30s)
- Cache-Control: no-cache bypasses cache; counter quote_simulation_cache_requests_total{result}
- Invalidate quote:sim:v1:* on indexer niffyins:tbl_upd (parseEvent)
- Fix claims.controller missing class brace, indexer duplicate handleClaimFiled
- Fix throttler Redis storage import path and ThrottlerStorageRecord type import
- app.module: import RedisService and RedisThrottlerStorage for ThrottlerModule factory
- Docs: quote-simulation-cache.md; observability metric table
- Tests: cache key util + integration with in-memory TTL Redis mock

Made-with: Cursor
@drips-wave

drips-wave Bot commented Mar 28, 2026

Copy link
Copy Markdown

@Jambox11 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@jhayniffy
jhayniffy merged commit 725787a into InsurNiffy:main Mar 29, 2026
1 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backend — Soroban simulation cache: short-TTL Redis layer for quote responses

3 participants