{
  "id": 20245157,
  "title": "Part 3: Building the App & Caching for Data Latency",
  "url": "https://urgent.news/2026/11/16/part-3-building-the-app-caching-for-data-latency",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-16T00:38:13.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/spmahapatra/part-3-building-the-app-caching-for-data-latency-474i"
  },
  "original_language": "en",
  "account": "Building the App & Caching for Data Latency\n\nThe application is a Python Flask REST API that aggregates data from the external Rick & Morty API. Its main job is to filter the data based on strict criteria: species=human, status=alive, and origin=earth. The core structure is defined in app.py, with a custom cache middleware initialized from the cache detection module.\n\nThe primary challenge is data latency due to the reliance on an external upstream API. To address this, the CacheMiddleware backed by Redis is introduced. The architectural flow of a request involves checking Redis first (Cache Check) with a latency of ~2ms. If the data is not found (Cache Miss), the Flask API fetches data over the Internet (Latency: ~300ms), saves it to Redis with a Time-To-Live (TTL), and returns it to the user with appropriate headers (X-Cache: HIT or MISS).\n\nThe middleware generates deterministic cache keys based on the HTTP method, endpoint, and query parameters. It intercepts requests before they reach the business logic and hooks into Flask's before_request and after_request lifecycle. Before a request, it checks Redis for cached data; if found, it returns the cached JSON immediately. After a request, if data had to be fetched from the upstream API, the middleware captures the response payload and saves it into Redis for future requests.\n\nGraceful degradation is ensured by wrapping the cache connection in try/except blocks. If Redis is unreachable, the application logs a warning and falls back to fetching data directly from the upstream API. Advanced caching strategies include addressing the Thundering Herd Problem by implementing Soft TTL, which stores data with an expiration timestamp. When a request comes in and the Soft TTL has passed, a background thread fetches fresh data from the upstream API while users receive stale data without experiencing the 300ms latency penalty. Additionally, memory eviction policies such as allkeys-lru (Least Recently Used) are configured in redis.conf to automatically delete cache keys that haven't been accessed recently, preventing OOM issues.",
  "summary": "Part 3: Building the App & Caching for Data Latency Navigation: ← Part 2: Optimizing Dockerfiles with Multi-Stage Builds | Part 4: Full-Cycle CI/CD — From Local Hooks to AWS ECR → 📦 Companion Repository: Complete source code, Dockerfiles, and Compose configurations are available on GitHub: spmahapatra/rick-morty-api-helm . Key Takeaways Platform engineers: Standardise your cache connection…",
  "key_points": [
    "Python Flask REST API aggregates Rick & Morty data",
    "CacheMiddleware with Redis reduces data latency",
    "Soft TTL and LRU eviction manage cache memory"
  ],
  "editors_take": "Implementing Redis middleware with Soft TTL strategies transforms a high-latency external dependency into a responsive service by prioritizing immediate data delivery over strict freshness during upstream delays.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}