Repository navigation
Description
Parent Issue
Sub-issue of #4651 — Translate API cost optimization (target: 84–89% reduction)
Objective
Cost saving: Current translation cache is in-memory, per-session, max 1000 items — lost on disconnect or pod restart. Zero cross-user or cross-session reuse. Redis cache enables global deduplication of common phrases.
Problem
TranslationService at translation.py:248 creates a per-instance OrderedDict cache. Each WebSocket session gets a new instance (transcribe.py:1132). Common phrases like "hello", "thank you", "good morning" are translated millions of times across users instead of being cached once.
Redis is already used extensively in the codebase (redis_db.py has cache_signed_url, cache_user_name, cache_memories, etc.) but NOT for translation.
Changes Required
1. Add Redis cache helpers to redis_db.py
def cache_translation(sentence_hash: str, dest_lang: str, translated_text: str,
detected_lang: str, ttl: int = 86400 * 14): # 14 day TTL
key = f"translate:v1:{sentence_hash}:{dest_lang}"
value = json.dumps({"text": translated_text, "detected_lang": detected_lang})
redis_db.set(key, value, ex=ttl)
def get_cached_translation(sentence_hash: str, dest_lang: str) -> Optional[dict]:
key = f"translate:v1:{sentence_hash}:{dest_lang}"
cached = redis_db.get(key)
return json.loads(cached) if cached else None2. Update TranslationService to check Redis first
In translate_text() (translation.py:273-316):
- Compute
md5(text)hash - Check Redis → if hit, return cached translation
- If miss, call Google API → cache result in Redis + in-memory
3. Cache at sentence level for best hit rates
- Sentence granularity provides:
- Intra-session: early sentences of growing segments hit cache on subsequent ticks
- Cross-user: common phrases cached globally across all users
- Store
detected_language_codealongside translated text for ambiguous short text
4. Version cache keys
- Prefix:
translate:v1:{hash}:{lang}— allows invalidation if splitting strategy or model changes - TTL: 7–30 days + rely on Redis LRU eviction (
volatile-ttl)
Files to Modify
| File | Change |
|---|---|
backend/database/redis_db.py |
Add cache_translation(), get_cached_translation() |
backend/utils/translation.py |
Check Redis in translate_text() before API call |
Test Plan
- Cache miss → API call → result stored in Redis
- Cache hit → no API call, correct translation returned
- Different target languages for same text → separate cache entries
- TTL expiry works correctly
- Cross-session: translation cached in session A, hit in session B
Acceptance Criteria
- Redis cache checked before every translate API call
- Common phrases only translated once globally
- Cache keys versioned with
v1:prefix
Dependencies
- Can be implemented independently, but pairs well with sub-issue 1 (store
detected_language_codefrom translate response)
Sub-issue 3/4 of #4651. Additional 10–15% reduction on remaining characters after changes 1+2.
Activity
perf(translate): eliminate paid detect_language API, add Redis cache,…
perf(translate): eliminate redundant API calls, add Redis cache, batc…
perf(translate): eliminate paid detect_language API, add Redis cache,…
perf(translate): eliminate redundant API calls, add Redis cache, batc…