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
ECMAScript 2025 Detailed Update Guide for Frontend Developers
Jul 06, 2025
🔍 ECMAScript 2025 – Detailed Feature Guide All new ECMAScript 2025 features with code examples and explanation of their importance for front...
Analyze Laravel Projects with Introspect
Jul 01, 2025
Analyze Laravel Codebases with Laravel Introspect If you’re doing a complex refactor or building dev tools, Laravel Introspect helps you quer...
Using Web Components the Smart Way
Jul 06, 2025
Using Web Components the Smart Way A lot of developers assume Web Components are meant to replace full SPA frameworks like React or Vue. But in rea...
Supercharge Your PHP Enums with archtechx/enums
Jul 01, 2025
Supercharge Your PHP Enums with archtechx/enums PHP 8.1 introduced native enums—type‑safe sets of named values like statuses or roles. The arch...
Mastering Modern CSS: The Power of if(), Popover Hints, and Smart Styling
Jul 16, 2025
🌐 Mastering Modern CSS: The Power of if(), Popover Hints, and Smart Styling CSS is getting smarter. In this guide, we’ll explore how the new...
Color Everything in CSS – Simple Guide
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(...