perf(api-gateway): cache media resolution metadata in Redis to reduce TTFB #8

Open
opened 2026-11-11 06:50:05 +00:00 by crapougnax · 0 comments
Owner

Summary

In \handleMediaRequest\ (\containers/api-gateway/src/media.ts), every incoming media request currently invokes the upstream API endpoint (/internal/:uid?action=:action) via Express. This upstream call queries PostgreSQL for the media record, verifies permissions, and generates the storage S3 URL.

Benchmark analysis revealed that this internal roundtrip adds ~390 ms of latency to the TTFB (Time To First Byte) on every request, even for subsequent downloads of the same static media.

Proposed Solution

  • Cache the upstream resolution payload ({ url, mimeType, size }) in Redis using a key such as \media:meta::\ (or partitioned by user when user-specific ACLs are active).
  • Introduce a configurable TTL (e.g., \GATEWAY_CACHE_MEDIA_METADATA_TTL_SEC, default: 300 seconds / 5 min).
  • Honor bypass flags (\Cache-Control: no-cache\ or
    ocache=true).
  • Return early from \handleMediaRequest\ using the cached metadata without calling the upstream Express service.

Expected Impact

  • Eliminates ~350-400 ms from TTFB on repeat media downloads.
  • Relieves PostgreSQL connection pressure and Express CPU load during concurrent media syncs (such as Unity scene initialization).

Migrated from GitHub mirror: https://github.com/Quatrain/CoreApps/issues/13

## Summary In \handleMediaRequest\ (\containers/api-gateway/src/media.ts\), every incoming media request currently invokes the upstream API endpoint (\/internal/:uid?action=:action\) via Express. This upstream call queries PostgreSQL for the media record, verifies permissions, and generates the storage S3 URL. Benchmark analysis revealed that this internal roundtrip adds **~390 ms of latency to the TTFB (Time To First Byte)** on every request, even for subsequent downloads of the same static media. ## Proposed Solution - Cache the upstream resolution payload (\{ url, mimeType, size }\) in Redis using a key such as \media:meta:<uid>:<action>\ (or partitioned by user when user-specific ACLs are active). - Introduce a configurable TTL (e.g., \GATEWAY_CACHE_MEDIA_METADATA_TTL_SEC\, default: 300 seconds / 5 min). - Honor bypass flags (\Cache-Control: no-cache\ or \ ocache=true\). - Return early from \handleMediaRequest\ using the cached metadata without calling the upstream Express service. ## Expected Impact - Eliminates ~350-400 ms from TTFB on repeat media downloads. - Relieves PostgreSQL connection pressure and Express CPU load during concurrent media syncs (such as Unity scene initialization). --- _Migrated from GitHub mirror: https://github.com/Quatrain/CoreApps/issues/13_
Sign in to join this conversation.
No labels
enhancement
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
quatrain/core-apps#8
No description provided.