𝗦𝗰𝗮𝗹𝗶𝗻𝗴 𝗖𝗦𝗩 𝗜𝗺𝗽𝗼𝗿𝘁𝘀 𝗪𝗶𝘁𝗵 𝗟𝗮𝗿𝗮𝘃𝗲𝗹 𝗝𝗼𝗯 𝗕𝗮𝘁𝗰𝗵𝗶𝗻𝗴
B2B SaaS clients often upload large files. Imagine a CSV with 100,000 rows.
Most developers make a mistake. They use a loop to send 100,000 separate background jobs. This creates problems.
If job number 45,000 fails, you have no way to tell the user. You cannot track progress. You cannot send a single email once the whole file finishes. The jobs are disconnected.
You need Laravel Job Batching to fix this.
The Solution: Bus::batch()
Job batching groups thousands of jobs into one unit. Laravel gives this unit a unique ID. Your frontend uses this ID to show a progress bar. It also provides hooks to handle success or failure.
How to implement it:
- Use a memory-safe generator to read your CSV.
- Chunk your data into manageable pieces.
- Use Bus::batch() to group the jobs.
- Use allowFailures() so one bad row does not stop the entire process.
The lifecycle hooks work like this:
- then(): Runs only if every job succeeds.
- catch(): Runs when the first error occurs.
- finally(): Runs when all jobs finish, even if some failed.
The Benefit for Users
Your React frontend can poll the batch ID. It shows a 0-100% progress bar. This turns a mysterious background task into a transparent experience.
The Engineering Value
You gain control over distributed tasks. You stop silent failures. You provide real-time data to your users. You ensure post-processing logic runs at the correct time.
Source: https://dev.to/iprajapatiparesh/scaling-csv-imports-master-laravel-job-batching-iaa