Distributed Stampede Shield
Stops dogpiling during traffic spikes. Only one worker computes the query while concurrent requests wait and read the cached result from memory.
v1.5 • Laravel 10, 11, 12 & 13
Atomic distributed locks to prevent dogpiling, zero-crash virtual tags for every cache store, and transaction-safe invalidation deferred to database commits.
use App\Models\Report;
use KaziOmar\AtomicCache\Facades\AtomicCache;
// 1,000 concurrent requests execute only 1 database query:
$report = AtomicCache::tags(['reports', 'analytics'])
->rememberWithLock('annual_sales_2026', 86400, function () {
return Report::generateAggregatedFinancials();
}, lockSeconds: 15, waitSeconds: 5);namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use KaziOmar\AtomicCache\Traits\AutoClearsCache;
class Product extends Model
{
use AutoClearsCache;
// Automatically flushes parent store caches on DB::afterCommit:
protected array $cacheParents = ['store'];
// Ignores high-frequency counter updates to prevent cache churn:
protected array $cacheIgnoreColumns = ['view_count', 'meta'];
}use App\Models\Product;
// Auto-generates sha256 hash from SQL + bindings + eager loads
// Tagged with 'products' for automatic invalidation on updates:
$featured = Product::where('status', 'active')
->where('featured', true)
->atomicCache(3600);
// Single record query:
$product = Product::where('slug', $slug)->atomicCacheFirst(3600);namespace App\Services;
use App\Models\Product;
use KaziOmar\AtomicCache\Traits\HasAtomicCache;
class ProductService
{
use HasAtomicCache;
protected string $cacheResource = 'products';
public function getCatalog()
{
// Automatically isolates page numbers and query filters:
return $this->rememberCollection('catalog', fn() => Product::paginate(20));
}
}How Laravel Atomic Cache solves common production challenges versus standard caching.
| Feature | Standard Laravel Cache | Laravel Atomic Cache |
|---|---|---|
| Stampede Protection | ✕ None (all requests hit DB) | ✓ Atomic Lock (1 worker computes, others wait & read) |
| Large Payload Size | ✕ Raw uncompressed data | ✓ Auto Gzip (>50KB cuts RAM by up to 70–80%) |
| Mega Datasets | ✕ Fails on 1MB Memcached / Redis freeze | ✓ Transparent Chunking (splits safely, preserves keys) |
| Redundant Writes | ✕ Every put hits storage and network | ✓ Write Deduplication (skips duplicate compression) |
| Driver Tag Support | ✕ Crashes on File and Database stores | ✓ Virtual Tagging (works across all stores without Redis) |
| Corrupted Data | ✕ Throws crash exception to user | ✓ Self-Healing (cleans key, regenerates silently) |
| Simultaneous Expiry | ✕ All keys expire in the same second | ✓ TTL Jitter (random variance prevents herd spikes) |
| Model Invalidation | ✕ Observers fire before commit | ✓ DB::afterCommit (prevents transaction race conditions) |
| Query Caching | ✕ Manual key hashing required | ✓ Zero-Key Macros (auto SQL and eager load hash) |
| Architecture Audit | ✕ Manual DIY logging | ✓ Artisan Doctor (atomic-cache:check) |