Hard20 minLaravel Fundamentals
UpdatedAug 5, 2026
Edit

Laravel Caching

Question Variations

  • "How should cache keys handle tenant data?"
  • "Why can `remember` serve stale data after a write?"
  • "When should Laravel use Redis for caching?"

Why This Is Asked

Laravel cache helpers are simple, but candidates must still reason about expiry, invalidation, and distributed consistency.

Key Concepts

  • Cache::remember reads or computes a value with an expiry.
  • Cache keys must include all relevant identity and scope.
  • Writes need a deliberate invalidation or update strategy.
  • Local caches behave differently from shared Redis-backed caches.

Question Variations

  • “How should cache keys handle tenant data?”
  • “Why can remember serve stale data after a write?”
  • “When should Laravel use Redis for caching?”

Answers by Technology

+ Add Variant
LaravelImprove this answer ✏️

Expected Answer (Laravel 13 / PHP 8.3+)

Use Cache::remember for a value that is safe to reuse, with a key that includes its tenant, user, locale, or other scope. Define expiry and invalidation when writes change the underlying data. For replicas, choose a shared store such as Redis when instances need a common cache view. Cache is an optimization, never the authoritative authorization or business-state source.

Why It Matters

Bad keys leak data between tenants; missing invalidation returns stale results after writes.

Code Example

$product = Cache::remember("tenant:{$tenantId}:product:{$id}", now()->addMinutes(5),
    fn () => Product::findOrFail($id)
);

Common Mistakes

  • Leaving tenant data out of keys: One tenant can receive another tenant’s cached result.
  • Assuming expiry alone solves invalidation: Writes may need immediate eviction.

Follow-up Questions

  • Why use Redis in a fleet? (Answer: It provides shared cache state.)
  • What does remember do? (Answer: Reads a key or computes and stores it on a miss.)