How to Cache News API Responses for Better Performance

Erick Horn

Erick Horn

·

14 minuten Lesen

How to Cache News API Responses for Better Performance

How to Cache News API Responses for Better Performance

You don't need a faster server. You need to stop calling the news API.

Most teams hit a news API for every page render, every search, every user refresh. Then they wonder why their p95 latency is 800ms and the bill grows every month. The fix is to cache news API responses—but only if you cache by content half-life, not by a single TTL.

News API caching stores news API responses in a fast key-value store such as Redis and serves subsequent matching requests from that store until the entry exceeds a content-aware TTL. Unlike generic REST API caching that uses one TTL for all endpoints, news API caching tiers TTL by content type—breaking stories stay fresh while archive search stops burning quota.

This guide is for backend developers who already pay for a news API and want to cut latency and cost. We'll build a Redis cache-aside layer with content-aware TTLs, a published_at-window-aware cache key, breaking-news bypass, and stale-while-revalidate. Working code in Node.js, Python, and curl against the real APITube response shape.

What You'll Build

A cache layer with four parts:

  • Content-aware TTL matrix—breaking 30s, top headlines 5 min, archive search 1 h, sentiment and entities 24 h
  • Cache key that hashes endpoint + sorted query params + a published_at window bucket
  • Cache-aside reader with stale-while-revalidate
  • Breaking-news bypass for explicit fresh queries

By the end you'll serve 95%+ of news traffic from Redis without showing stale breaking news.

Offenlegung

We're APITube. Yes, we benefit when you cache fewer of our endpoints. We still recommend caching aggressively because it's the right answer for your latency and budget. The patterns below work against any news API—we use APITube's actual response shape and the free-tier limit of 30 requests per 30 minutes so the math is concrete.

Voraussetzungen

  • Node.js 18+ or Python 3.10+
  • Redis 7+–docker run -p 6379:6379 redis:7
  • An APITube key (free tier works): get one at apitube.io
  • Familiarity with cache-aside; otherwise skim the Redis cache-aside reference

The News Caching Problem in One Paragraph

A breaking-news query at 09:01 must reflect a story published at 09:00:42. A search across last week's archive can be 24 hours stale and nobody cares. Generic "set TTL to 5 minutes" advice gets one of these wrong every time. The fix is to classify each request and apply a TTL that matches the content's half-life—and to design your cache key so that windowed date filters don't silently serve yesterday's results today.

How Long Should You Cache News API Responses?

News API responses should be cached for 30 seconds (breaking news), 5 minutes (top headlines), 1 hour (archive search), or 24 hours (sentiment and entity enrichment)—each TTL matched to the content's half-life rather than a single global default. Top headlines update on a publishing cadence; sentiment for an article published last week never changes again.

Content typeTTLWhy this number
Breaking news (is_breaking=true)30 secondsStories appear minute-by-minute; staleness costs trust
Top headlines (/v1/news/top-headlines)5 minutesEditorial cadence is 3–10 min between updates
Archive search (published_at.end > 24 h ago)1 hourHistorical results don't change
Sentiment + entities on a fixed article24 StundenNLP outputs are deterministic per article
Source / category metadata24 StundenReference data shifts weekly, not hourly

The most common mistake is setting one TTL—usually 5 minutes—for everything. That's stale for breaking and wasteful for archive search. This matrix is the difference between a cache that helps and a cache that misleads.

Step 1—Design a Cache Key That Handles published_at Windows

Generic tutorials hash the endpoint URL. That breaks when you call:

/v1/news/everything?published_at.start=2026-05-01T00:00:00Z&category.id=medtop:13000000

at 23:59 and again at 00:01. Same URL, different expected results. Bucket the published_at window into the key:

import crypto from 'crypto';

function cacheKey(endpoint, params) {
  const sorted = Object.keys(params).sort()
    .reduce((acc, k) => (acc[k] = params[k], acc), {});

  if (sorted['published_at.start']) {
    const start = new Date(sorted['published_at.start']);
    const ageHours = (Date.now() - start.getTime()) / 3.6e6;
    const bucket = ageHours < 24 ? 'hour' : 'day';
    sorted._bucket = bucket + ':' + (bucket === 'hour'
      ? Math.floor(Date.now() / 3.6e6)
      : Math.floor(Date.now() / 8.64e7));
  }

  const raw = endpoint + ':' + JSON.stringify(sorted);
  return 'apitube:' + crypto.createHash('sha1').update(raw).digest('hex').slice(0, 16);
}

Bucketing—not raw timestamps—is what creates a hit ratio. Every request inside the same hour (or day, for archive) shares a key. That's the difference between an 80% hit rate and 0%.

Step 2—Cache-Aside with Content-Aware TTL (Node.js)

Classic cache-aside, with the TTL pulled from the matrix above:

import Redis from 'ioredis';
import fetch from 'node-fetch';

const redis = new Redis();
const API_KEY = process.env.APITUBE_KEY;

const TTL = { breaking: 30, topHeadlines: 300, archive: 3600, enrichment: 86400 };

function classify(endpoint, params) {
  if (params['is_breaking'] === 'true' || params['is_breaking'] === true) return TTL.breaking;
  if (endpoint.includes('top-headlines')) return TTL.topHeadlines;
  if (params['published_at.end']) {
    const end = new Date(params['published_at.end']);
    if (Date.now() - end.getTime() > 86_400_000) return TTL.archive;
  }
  return TTL.topHeadlines;
}

export async function getNews(endpoint, params) {
  const key = cacheKey(endpoint, params);
  const cached = await redis.get(key);
  if (cached) return JSON.parse(cached);

  const url = `https://api.apitube.io${endpoint}?${new URLSearchParams(params)}`;
  const res = await fetch(url, { headers: { 'X-API-Key': API_KEY } });
  if (!res.ok) throw new Error(`APITube ${res.status}`);
  const body = await res.json();

  await redis.setex(key, classify(endpoint, params), JSON.stringify(body));
  return body;
}

APITube returns { status, results[], has_next_pages, page, request_id }. Cache the whole envelope. Each results[i] includes sentiment and entities—both expensive to recompute and free to cache for 24 hours.

Step 3—Stale-While-Revalidate Plus Breaking-News Bypass

Stale-while-revalidate returns a stale value immediately while refreshing in the background—perfect for top headlines under load. Bypass skips the cache entirely—required when a query explicitly asks for fresh content. Composing both is what makes a news cache feel real-time:

const STALE_MULTIPLIER = 3;

export async function getNewsSWR(endpoint, params, opts = {}) {
  if (opts.bypass || params.no_cache) return fetchFresh(endpoint, params);

  const key = cacheKey(endpoint, params);
  const ttl = classify(endpoint, params);

  const [cached, age] = await Promise.all([redis.get(key), redis.ttl(key)]);

  if (cached && age > 0) return JSON.parse(cached);                       // fresh
  if (cached && age > -ttl * STALE_MULTIPLIER) {                          // stale-but-usable
    refreshInBackground(endpoint, params, key, ttl).catch(console.error);
    return JSON.parse(cached);
  }
  return fetchFresh(endpoint, params, key, ttl);                          // miss
}

The decision tree: if the caller passes bypass, hit the API. If the cached entry is younger than its TTL, return it. If it's stale but within three TTLs, return stale and refetch in the background. Otherwise fetch synchronously and cache.

The Same Pattern in Python and curl

Python (redis-py 5.x):

import hashlib, json, requests, redis

r = redis.Redis()
TTL = {"breaking": 30, "top": 300, "archive": 3600, "enrich": 86400}

def cache_key(endpoint, params):
    raw = endpoint + ":" + json.dumps(dict(sorted(params.items())))
    return "apitube:" + hashlib.sha1(raw.encode()).hexdigest()[:16]

def get_news(endpoint, params, api_key):
    key = cache_key(endpoint, params)
    cached = r.get(key)
    if cached:
        return json.loads(cached)

    res = requests.get(f"https://api.apitube.io{endpoint}",
                       params=params,
                       headers={"X-API-Key": api_key})
    res.raise_for_status()
    body = res.json()
    r.setex(key, TTL["top"], json.dumps(body))
    return body

Probe the API once with curl to confirm the shape you're caching:

curl -H "X-API-Key: $APITUBE_KEY" \
  "https://api.apitube.io/v1/news/top-headlines?language.code=en&per_page=10"

Response: {"status":"ok","results":[...],"has_next_pages":true,"page":1,"request_id":"..."}. Save the JSON, gzip it if your envelopes exceed 100 KB, and store as one Redis value.

How Do You Invalidate a Redis Cache When Data Changes?

You invalidate a Redis cache in three ways, ordered by complexity: TTL-based expiration (let the key expire automatically—use this for everything in the matrix above), manual deletion via redis.del(key) when a user forces a refresh, and event-driven invalidation that subscribes to a webhook such as article.published and deletes affected keys via a prefix scan. Most news apps need only TTL plus manual delete on user-triggered refresh.

Does Caching Reduce API Costs?

Caching reduces news API costs proportionally to its hit ratio: a 10,000-MAU app generating 9 million upstream calls per month needs only a 67% cache hit ratio to fit a 100k-call/day quota—comfortably achievable with content-aware TTL. APITube's free tier of 30 requests per 30 minutes is the floor for the math:

required_hit_ratio = 1 - (allowed_calls / actual_traffic)

free tier:  1 - (30 calls / 30 min × 1440 min/day × 30 days) / 9,000,000
            = 1 - 43,200 / 9,000,000
            = 99.5%   → unrealistic; free tier is for prototypes

100k/day:   1 - 3,000,000 / 9,000,000 = 67%   → comfortable with content-aware TTL

Caching turns a "we need a bigger plan" conversation into "we're fine on this plan." Plug your own MAU and session numbers into the formula before negotiating tier.

What Is the Best Way to Cache API Data in Node.js?

The best way to cache API data in Node.js is Redis with the ioredis client—it shares state across replicas, survives process restarts, and scales horizontally. Add an in-process LRU cache only if hot keys exceed about 100 requests per second per process; then it acts as L1 in front of Redis. Single-process, single-region apps can skip LRU entirely. Avoid in-memory-only caches in production: they don't share state across replicas.

Decision Framework

SituationWhat to use
Single-region, single replicaRedis only
Multiple replicas, low QPSRedis only
Multiple replicas, hot keys > 100 RPS eachIn-process LRU → Redis
CDN-fronted public news feedCache-Control + stale-while-revalidate at the CDN, Redis at origin
Per-user personalized feedRedis with user-scoped key, no CDN

Try It Free

You can validate this entire cache-aside layer on APITube's free tier in an afternoon. Every result already includes sentiment and entities—both worth caching for 24 hours, both included on every article. Try APITube free at apitube.io.

Ressourcen

Verwandte Anleitungen:

APITube - News API

Verwandte Artikel

Stock News Alert System with Node.js: 2026 Tutorial
Developer Guides

Stock News Alert System with Node.js: 2026 Tutorial

Build a Node.js stock news alert system in one evening: poll APITube, dedupe, gate by sentiment, send Slack alerts. Full code + API comparison.

News API Pagination: Cursor, Offset & the 10k Cap
Developer Guides

News API Pagination: Cursor, Offset & the 10k Cap

Stop losing articles between pages. A consumer-side guide to news API pagination: date-window slicing, 429 retries, async streaming, and dedup.

Real-time news alerts with WebSockets (Socket.IO 2026)
Developer Guides

Real-time news alerts with WebSockets (Socket.IO 2026)

Build real-time news alerts with WebSockets in Node.js using Socket.IO 4.8 and APITube SSE. Room filters, reconnect, Redis adapter, full code.

Wie man die Kosten für Nachrichten-API reduziert: 8 Strategien (sparen Sie 70-90%)
Developer Guides

Wie man die Kosten für Nachrichten-API reduziert: 8 Strategien (sparen Sie 70-90%)

Überprüfen Sie Ihre Aufrufe, stufen Sie den Cache TTL nach Nachrichten-Geschwindigkeit, tauschen Sie Polling gegen Webhooks und exportieren Sie Backfills in großen Mengen, um die Nachrichten-API-Ausgaben 70-90% zu senken. Kostenrechnung pro -100k + Code.

Wir verwenden Cookies

Durch Klicken auf "Akzeptieren" stimmen Sie der Verwendung von Cookies auf Ihrem Gerät zu, damit die Website funktioniert und für Analysen.