Get upto 8x more serverless function invocations (GB-Hrs) on Vercel’s free-tier for your Next.js 13 project in an Simple & Effective way!
Adding a simple configuration to your project can potentially give you upto 8x more Next.js 13 serverless function calls on the already generous 100GB-Hrs that Vercel provides in its free-tier allowing you more room to host your side projects :)
The above image shows a very simple API with Memory Size customized to 128MB instead of the default 1024MB.
The Math goes as follows for calculating usage as per Vercel’s own guide:-
Serverless Function Execution on Vercel is measured using a metric called GB-Hrs. GB-Hrs is the duration (Hours) your Serverless Function runs for, multipled by the amount of memory (GB) allocated. The more memory you specify, or the longer your Serverless Function runs, the more GB-Hrs you will consume.
By default, Serverless Functions are allocated 1024 MB of memory, but can be configured to use more.
Serverless Function Execution Example
In this example, we will allocate 512 MB of memory and execute it 3 million times at a 1 second duration:
Total Seconds: 3M * (1s) = 3,000,000 Seconds
Total GB-Seconds: 512/1024 GB * 3,000,000 Seconds = 1,500,000 GB-Seconds
Total GB-Hrs: 1,500,000 GB-Seconds / 3600 = 416.67 GB-Hrs
The total Serverless Function Execution is 416.67 GB-Hrs.
Hence if we can reduce the memory allocation to our serverless functions, we can get more serverless function executions for the same free-tier limit.
Configuring Projects with vercel.json
According to the old blog post from Vercel, the maximum memory limit for a serverless function on free-tier is 1024MB which now has been increased to 3008MB as seen in the above image in 2023. 1024MB still remains the default value for all the serverless functions generated. Though the maxDuration has increased from 5s to 10s on the free-tier
Pages Directory `vercel.json` config example
{
"functions": {
"pages/api/hello.ts": {
"memory": 3008,
"maxDuration": 60
},
"pages/api/another.ts": {
"memory": 1024,
"maxDuration": 30
}
}
}
App Directory `vercel.json` config example
{
"functions": {
"app/api/hello/route.ts": {
"memory": 3008,
"maxDuration": 60
},
"app/api/another/route.ts": {
"memory": 1024,
"maxDuration": 30
}
}
}
Sample config with multiple APIs and custom options
Happy Hacking & Optimizing :)