Efficient Reporting & Big Data Reports with Queues
July 7, 2025- How to cache report queries with fixed timelines
- How to generate large reports asynchronously using job queues
1. 🧠 Report Query Caching with Fixed Timelines
If your dashboard shows sales from the last 30 days, running that heavy database query every time someone loads the page can be wasteful and slow. Instead, you can cache the results until the end of the day.
Why fixed timelines?
- All users get the same snapshot of the data.
- Cache expiration is predictable (e.g., at midnight).
How to implement it in Laravel:
use Illuminate\Support\Facades\Cache;
public function getReport()
{
$cacheKey = 'sales_report_daily';
$expiresAt = now()->endOfDay();
return Cache::remember($cacheKey, $expiresAt, function () {
return DB::table('sales')
->whereDate('created_at', '>=', now()->subDays(30))
->get();
});
}
This caches the data until 11:59 PM. After that, the next request will regenerate the cache with updated data. You can also change the period by using endOfWeek() or endOfMonth() depending on your needs.
2. 📦 Big Data Report Generation via Job Queues
Generating reports with millions of rows in real-time is risky — it may slow down your server or cause timeouts. The better approach is to queue the job, process the report in the background, and notify the user (or allow the frontend to check periodically) when the file is ready.
Step 1: Dispatch the report job
public function requestReport()
{
GenerateBigReport::dispatch(auth()->id());
return response()->json([
'message' => 'Report generation started. You will be notified once it’s ready.'
]);
}
Step 2: Create the job that generates the report
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Storage;
class GenerateBigReport implements ShouldQueue
{
use Queueable;
public $userId;
public function __construct($userId)
{
$this->userId = $userId;
}
public function handle()
{
$data = DB::table('transactions')->get();
$path = storage_path("app/reports/report_{$this->userId}.csv");
$csv = fopen($path, 'w');
// Add headers
fputcsv($csv, ['ID', 'Amount', 'Date']);
// Write rows
foreach ($data as $row) {
fputcsv($csv, [$row->id, $row->amount, $row->created_at]);
}
fclose($csv);
// Optional: notify the user (e.g., via email or notification)
}
}
Step 3: Check from the frontend if the report is ready
public function checkReport()
{
$path = "reports/report_" . auth()->id() . ".csv";
if (Storage::exists($path)) {
return response()->json([
'ready' => true,
'download_url' => Storage::url($path)
]);
}
return response()->json(['ready' => false]);
}
Then on the frontend:
async function checkReport() {
const res = await fetch('/api/check-report');
const json = await res.json();
if (json.ready) {
showDownloadButton(json.download_url);
} else {
showProcessingState();
}
}
This strategy ensures the user interface stays fast and smooth, while heavy operations happen in the background.
✨ Final Thoughts
- Use fixed timeline caching to reduce unnecessary database queries and improve speed.
- Handle large report generation in the background using Laravel job queues.
- Make your frontend smarter by checking if the report is ready before offering the download.
These techniques will make your reporting system scalable, efficient, and much more user-friendly.
Blog
Mastering Laravel 12 Conditional Validation
Jul 07, 2025
Mastering Laravel 12 Conditional Validation Laravel 12's validation system is super powerful, and conditional validation makes your forms...
Bypassing $fillable Safely with forceFill() in Laravel
Jul 02, 2025
Bypassing $fillable Safely with forceFill() in Laravel Ever used create() in Laravel and noticed some fields like role or status didn’t save? T...
Stop Copy-Pasting Code! Learn How to Use Traits in Laravel the Right Way
Jul 01, 2025
🚫 Stop Copy-Pasting Code! Ever duplicated slug logic or logging across multiple models? Laravel's Traits got your back. 1. What’s a Trait?...
Object-Oriented Programming (OOP) – Core Concepts
Aug 09, 2025
Object-Oriented Programming (OOP) is a modern software development approach that divides an application into units called Objects that interact with...
Mastering Async Iteration in JavaScript with Array.fromAsync()
Jul 27, 2025
🔍 What Exactly is Array.fromAsync()? Array.fromAsync() is a static method introduced in ES2024 as part of JavaScript's growing support for asynchr...
React Hooks Complete Guide
Jul 01, 2025
🎣 Complete React Hooks Guide with Practical Examples 🧠 useState What it does: Adds local state to a function component. Code Example: impo...