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
Jun 26, 2025
Color Everything in CSS – Simple Guide Today we’re diving into CSS colors: how to define them, especially with modern methods like lab(...
Aug 06, 2025
When building applications with React, there’s always a need to manage data that changes based on user interaction or from fetching external r...
Jul 07, 2025
How to cache report queries with fixed timelines How to generate large reports asynchronously using job queues 1. 🧠 Report Query Caching wi...
Jun 17, 2025
Laravel 12.18.0 Update The Laravel team released version 12.18.0 with several cool updates: String encrypt() & decrypt() helpers are...
Jul 20, 2025
Laravel Context is one of the most powerful new features in Laravel 12. It allows you to attach contextual data (like user ID, IP address, request pat...
Jul 01, 2025
🚀 Laravel Queue & Job System We’re gonna walk you through Laravel queues from setup to deploying in production using Supervisor. Step 1...