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
Laravel 12.19: Elegant Query Builders with PHP Attributes
Jul 07, 2025
Laravel 12.19: Elegant Query Builders with PHP Attributes In Laravel 12.19, you can now use the #[UseEloquentBuilder] PHP attribute to assign a cus...
React Native 0.80 & ExecuTorch: A Powerful Leap into Offline AI for Mobile Apps
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...
Efficient Reporting & Big Data Reports with Queues
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...
Task Reminder with Laravel & MongoDB
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,...
What’s New in ECMAScript 2025
Jun 30, 2025
What’s New in ECMAScript 2025 On June 25, 2025, Ecma International officially approved ES2025, adding several useful features: 1. 📦 Import At...
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...
