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
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...
Jul 28, 2025
🚀 What’s New in React Native 0.80? The React Native 0.80 release marks a pivotal moment in mobile development. This update not only enhances...
Jul 27, 2025
Here’s a complete breakdown of essential tools to level up your Laravel development: Inertia.js v2, View Creators, and the Laravel HLS package...
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...
Jun 30, 2025
📌 Building a Task Reminder App This guide shows how to set up a Laravel app using MongoDB to implement a task reminder system with authentication,...
Jul 30, 2025
Why Performance Is Non-Negotiable In today’s fast-paced world, no one has time to wait for a slow-loading website. On mobile, users abandon...