Urgent.News

What's breaking now, across thousands of outlets.

Tech

Part 3: Building the App & Caching for Data Latency

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…

Building the App & Caching for Data Latency

The 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.

The 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).

The 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.

Graceful 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.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

My most-read page has 1,322,170 reads. My site has had 4 outbound clicks.

My daily writing queue is a list of the "most-read" pages on my AI tools directory. Today it put Deevid AI at #1 with 1,322,170 reads, Text to Video AI: Deevid at #2 with 983,454, and Deevid AI Image…

  • My most-read page has 1,322,170 reads
  • Site has had 4 outbound clicks total
  • Counter inherited from scraped directory

More from Monday 16 November →