Home >Backend Development >Golang >From Node.js to Go: Supercharging Sownloads of Thousands of Files as a Single Zip
As developers, we often face challenges when dealing with large-scale data processing and delivery. At Kamero, we recently tackled a significant bottleneck in our file delivery pipeline. Our application allows users to download thousands of files associated with a particular event as a single zip file. This feature, powered by a Node.js-based Lambda function responsible for fetching and zipping files from S3 buckets, was struggling with memory constraints and long execution times as our user base grew.
This post details our journey from a resource-hungry Node.js implementation to a lean and lightning-fast Go solution that efficiently handles massive S3 downloads. We'll explore how we optimized our system to provide users with a seamless experience when requesting large numbers of files from specific events, all packaged into a convenient single zip download.
Our original Lambda function faced several critical issues when processing large event-based file sets:
Our original implementation used the s3-zip library to create zip files from S3 objects. Here's a simplified snippet of how we were processing files:
const s3Zip = require("s3-zip"); // ... other code ... const body = s3Zip.archive( { bucket: bucketName }, eventId, files, entryData ); await uploadZipFile(Upload_Bucket, zipfileKey, body);
While this approach worked, it loaded all files into memory before creating the zip, leading to high memory usage and potential out-of-memory errors for large file sets.
We decided to rewrite our Lambda function in Go, leveraging its efficiency and built-in concurrency features. The results were astounding:
We used the AWS SDK for Go v2, which offers better performance and lower memory usage compared to v1:
cfg, err := config.LoadDefaultConfig(context.TODO()) s3Client = s3.NewFromConfig(cfg)
Go's goroutines allowed us to process multiple files concurrently:
var wg sync.WaitGroup sem := make(chan struct{}, 10) // Limit concurrent operations for _, photo := range photos { wg.Add(1) go func(photo Photo) { defer wg.Done() sem <- struct{}{} // Acquire semaphore defer func() { <-sem }() // Release semaphore // Process photo }(photo) } wg.Wait()
This approach allows us to process multiple files simultaneously while controlling the level of concurrency to prevent overwhelming the system.
Instead of loading all files into memory, we stream the zip content directly to S3:
pipeReader, pipeWriter := io.Pipe() go func() { zipWriter := zip.NewWriter(pipeWriter) // Add files to zip zipWriter.Close() pipeWriter.Close() }() // Upload streaming content to S3 uploader.Upload(ctx, &s3.PutObjectInput{ Bucket: &destBucket, Key: &zipFileKey, Body: pipeReader, })
This streaming approach significantly reduces memory usage and allows us to handle much larger file sets.
The rewrite to Go delivered impressive improvements:
Rewriting our Lambda function in Go not only solved our immediate scaling issues but also provided a more robust and efficient solution for our file processing needs. While Node.js served us well initially, this experience highlighted the importance of choosing the right tool for the job, especially when dealing with resource-intensive tasks at scale.
Remember, the best language or framework depends on your specific use case. In our scenario, Go's performance characteristics aligned perfectly with our needs, resulting in a significantly improved user experience and reduced operational costs.
Have you faced similar challenges with serverless functions? How did you overcome them? We'd love to hear about your experiences in the comments below!
The above is the detailed content of From Node.js to Go: Supercharging Sownloads of Thousands of Files as a Single Zip. For more information, please follow other related articles on the PHP Chinese website!