responsive.sk

Article

Cache wrappers ... good servant, bad master

Article cover illustration: Architecture, security-series, php, cache

Developers fall into two camps: cache everything or cache nothing. Both are wrong. Cache is a tool, and every tool has its place.

Cache has a bad reputation

Developers fall into two camps:

  1. Cache everything ... Redis, OPcache, CDN, Varnish, lost hours of life
  2. No cache ... "We'll hit LCP 100/100 without it"

Both are wrong. Cache isn't an automatic answer or an enemy. It's a tool ... and every tool has its place.

Two types of cache

1. Read cache

You store answers to questions that repeat:

  • Static HTML pages
  • Expensive query results
  • API responses
// PageCacheService ... stores HTML responses as files
$cache->set($key, $response, ttl: 300); // 5 minutes

// On next request ... reading from cache
$cached = $cache->get($key);
if (null !== $cached) {
    return Response::html($cached['content'], 200);
}

2. Write cache (Outbox)

You store events for later processing:

  • Email notifications
  • Webhook calls
  • P2P broadcasts
// Outbox ... stores domain events for async processing
OutboxMessage::create('OrderCreated', $order->toArray());

Cache = "What did you tell me?" → READ Outbox = "What should I tell you?" → WRITE

When cache helps

Use caseCache typeWhy
Rate-limit fallbackReadServe cached on 429
Static assetsReadCDN, browser cache
Rendered HTMLReadExpensive rendering
Session dataReadUser state
Database queriesReadDB-specific

Example: Rate-limit cache fallback

When a client exceeds the rate limit (60 requests/min), they get a cached version of the page instead of bare "Too Many Requests":

// RateLimitMiddleware ... when client is blocked
private function blockedResponse(...): Response
{
    if (null !== $this->cache) {
        $cached = $this->cache->get($cacheKey);
        if (null !== $cached) {
            $response = Response::html($cached['content'], 429);
            $response->setHeader('Retry-After', (string) $retryAfter);
            $response->setHeader('X-Cache', 'HIT');
            return $response; // Cached page with 429
        }
    }
    return Response::html('Too Many Requests', 429); // Bare text
}

Normal requests are not affected by cache ... cache is populated passively via PageCacheMiddleware. Rate-limit cache fallback is a backup plan, not an optimization.

When cache hurts

Use caseWhy NOTAlternative
User-specific dataSee other users' dataDirect query
Real-time dataStale dataWebSocket
Write-heavy pathsInvalidation complexitySkip cache
Simple readsOverhead > benefitDirect query

Example: Why we don't cache user data

//  BAD: cache for user-specific data
$cached = $cache->get('user-' . $userId);
if ($cached) return $cached;
$user = $db->query('SELECT * FROM users WHERE id = ?', $userId);
$cache->set('user-' . $userId, $user);

// Problem: User A sees User B's cached data
// When User B changes name, cache still has old value
//  GOOD: direct query for user-specific data
$user = $db->query('SELECT * FROM users WHERE id = ?', $userId);
// No invalidation, no stale data, no complexity

Cache in our project

PageCacheService ... file-based HTML cache

final readonly class PageCacheService
{
    private const int DEFAULT_TTL = 300; // 5 minutes
    private const int MAX_CACHE_SIZE = 1_048_576; // 1 MB

    // Key = SHA256(method + path + query + locale)
    public function cacheKey(Request $request, string $locale = ''): string
    {
        $data = $request->getMethod()
            . '|' . $request->getPathInfo()
            . '|' . http_build_query($request->getQueryParams())
            . '|' . $locale;
        return hash('sha256', $data);
    }

    // Lazy pruning ... cleans expired files once per 5 minutes
    private function lazyPrune(): void { ... }
}

Key decisions:

  • File-based ... no Redis, no external dependency
  • Lazy pruning ... doesn't clean on every request
  • Admin bypass ... /mark, /debug, /storage aren't cached
  • Auth bypass ... /login, /register aren't cached

PageCacheMiddleware ... passive population

// Every cacheable GET response is saved to cache
// But only when:
// - It's a GET request
// - Status 200
// - Content-Type text/html
// - Not an admin/auth path
// - Content is not empty

Cache is never actively populated ... only passively, when a request already passed through the normal pipeline.

Evolution stage

1. No cache
   → Raw performance, direct queries
   → LCP 100/100 achievable without cache

2. Cache wrappers
   → Rate-limit fallback (cached on 429)
   → OPcache for PHP bytecode
   → Browser cache for static assets

3. Outbox pattern
   → Cache for events (not just data)
   → Async side effects with retry
   → Write-path optimization

4. P2P cache
   → Distributed availability
   → Each node is consumer and provider
   → Knowledge chunks shared across network

The lesson

Cache isn't a mistake ... but it's not an automatic answer either.

When cache helps (rate-limit fallback, external systems, expensive rendering) ... use it. When it hurts (user data, real-time, write-heavy) ... skip it.

And remember: cache wrappers aren't mistakes ... they're evolution stages. Each one taught you something about data access that prepared you for the next. The progression cache → outbox → P2P is really about understanding that "the answer doesn't always need to be new, just close."

Cache remains a good servant. The mistake is letting it become the master. Rate-limit fallback, expensive rendering, external systems, cache earns its place there. User data, real-time, write-heavy paths, cache has no business there.


The answer doesn't always need to be new. Just close.


→ ACID for beginners: why transactions aren't boring → Outbox pattern: cache for the write path → How argon2id crashed shared hosting: infrastructure limits choices

Sources

Comments (0)

No comments yet — be the first to react.

Reader questions (0)

No questions yet — be the first to ask.

Stuck on something in this article? Ask below. Your name is shown publicly; your email is never stored. A question appears here once a moderator approves it.