Fetch and compute first, write later

Search for a command to run...

No comments yet. Be the first to comment.
In this series/category, I would share with you guys all of my exp and best practices, to build the awesome products
Hey guys, An easy topic for today! Make something a default (e.g., category, product, campaign, etc). Let’s go with the categories table for the examples. Let’s say we only want one category to be the default one. And find out which is the optimal wa...
Hey guys, An easy topic for today! Make something a default (e.g., category, product, campaign, etc). Let’s go with the categories table for the examples. Let’s say we only want one category to be the default one. And find out which is the optimal wa...
Hey guys, Probably a chapter 2 of this https://sethphat.dev/why-is-wordpress-still-an-awesome-cms blog post, haha. Here was the story: I’ve been spending like 2 days figuring out which CMS I want to use for my new blog (for my new hobby lol) Let’s se...

Hey guys, Well damn, it has been a while since my last post lol, lazy & busy. But here I am, back to write useful tips for y’all 🥹 Let us do some exporting tasks for today, using Laravel Excel to export XLSX (yeah CSV is so 2010 lol). But, append mo...

Hey guys, Recently, I just bought Herd PRO (a yearly subscription). I’ve been using the basic Herd since the first release. So, my latest work required interaction with S3 storage, there are several ways to achieve that in the local env: Use Cloud (...

On this page
Hey guys,
An update after I posted the “Laravel Excel: How to append rows to an existing Excel file“ a while back. This post will give you another solution and probably a better way to export that fits every case.
Previously, when trying to export more than 30k records, the overall process was getting slow.
A diagram to show the previous implementation.

The bottleneck was between the “Write to Excel”. Where we have to:
Download the Excel file from S3
Write (append)
Upload to S3 again
After 30k records, the file was huge (IIRC, 100~150MB). Each iteration took a lot of time and surpassed $timeout = 600 lol.
Well, at least we spotted the bottleneck; let’s enhance it.
Every exporter out there, we always do:
Fetch data
Compute data (transform into readable or accountable data)
Write
Let’s add some love for fetch & compute.
Fetch & compute are the most important tasks. We should handle it with care, indeed. So I created a new table:
export_rows
id
export_id
data (json column)
After fetching & computing each record, we’ll write into the export_rows table. The data column will store an array of values, e.g. ['Seth', 'Vietnam', 'github.com/sethsandaru']
To ensure fetch runs fast, I believe you already know how to optimize your query and add enough indexes.
Once we insert all the export_rows. It’s time to write. We create a new Excel/CSV file and simply:
Pull data by chunk
Write it into the file
Upload to S3
This will take faster since it’s purely reading simple things from the DB, no hard pressure. Then write & upload.
Using AWS Lambda, average processing time takes around ~20s for writing & uploading 100k records, it’s pretty fast, I’d say.
Note: after uploading the exported file to S3, we should delete all of export_rows to save space.
From exporting in hours for thousands of records, it is now minutes. And of course, once it’s done, users will get notified via email.
Thanks for reading, and I hope it helps to improve your exporters!