All skills
asyrafhussin avatar

/laravel-database-optimization

@ac26821

Laravel database optimization patterns. Use when writing Eloquent queries, creating migrations, configuring caching, debugging slow queries, or optimizing database performance. Triggers on tasks involving N+1 queries, indexing, Redis caching, pagination, or database transactions.

Use this Skill: https://skilld.dev/gh/asyrafhussin/agent-skills/laravel-database-optimization

This session only. Nothing lands on disk.

rulescache-remember.md

≈659 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Use Cache::remember() Pattern

Impact: HIGH (Atomic cache population eliminates redundant database queries and race conditions)

Manually checking the cache and repopulating it in separate steps creates a race condition where multiple concurrent requests can miss the cache simultaneously and all hit the database. Cache::remember() handles this atomically in a single call.

Incorrect

// ❌ Manual get/put — race condition on cache miss
$products = Cache::get('products:featured');

if ($products === null) {
    // Multiple concurrent requests can reach here simultaneously
    $products = Product::where('featured', true)
        ->with('category')
        ->get();

    Cache::put('products:featured', $products, 3600);
}

return $products;

Problems:

  • Race condition: concurrent requests during cache miss all execute the expensive query
  • Cache stampede on high-traffic endpoints — database gets hammered when cache expires
  • More code to maintain and more opportunities for bugs (e.g., forgetting to set TTL)
  • No guarantee the cached value matches what was just queried (another request may overwrite)

Correct

// ✅ Cache::remember() — atomic get-or-set with TTL
$products = Cache::remember('products:featured', 3600, function () {
    return Product::where('featured', true)
        ->with('category')
        ->get();
});

// ✅ Cache::rememberForever() — for rarely-changing reference data
$countries = Cache::rememberForever('reference:countries', function () {
    return Country::orderBy('name')->get();
});

// ✅ With dynamic keys for per-user or per-entity caching
$userStats = Cache::remember(
    "users:{$user->id}:stats",
    now()->addMinutes(15),
    function () use ($user) {
        return [
            'posts_count' => $user->posts()->count(),
            'followers_count' => $user->followers()->count(),
            'total_likes' => $user->posts()->withSum('reactions', 'count')->sum('reactions_sum_count'),
        ];
    }
);

Benefits:

  • Atomic operation: only one request regenerates the cache on miss
  • Eliminates cache stampede — concurrent requests wait for or get the freshly cached value
  • Cleaner, less error-prone code with TTL and closure in a single call
  • rememberForever() variant for data that changes only via explicit invalidation

Reference: Laravel Cache Usage

Source: SKILL.md on GitHub

1 warning16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill is a comprehensive documentation set for optimizing database performance in Laravel 13 applications. It provides best practices for Eloquent queries, indexing, Redis caching, and safe migrations. No malicious patterns, prompt injections, or data exfiltration attempts were detected.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    6/39 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at ac26821. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago
metadata
{
  "author": "agent-skills",
  "version": "1.1.1"
}

README badge

README badge for asyrafhussin/agent-skills/laravel-database-optimization