The Cost Structure of using Serverless Functions on Vercel
This concise guide will help you understand the reasons behind unexpectedly large bills from Vercel and identify specific code sections that contribute to higher costs. Recently, many developers have expressed concerns about escalating charges from Vercel. Let's examine one of the key factors driving these increased costs.
TLDR;
- Function Invocations: Charged per function call, excluding cache hits.
- Free for the first 100K invocations
- $20 for the first 1M invocations
- $0.60 per additional 1M invocations.
- Function Duration: Charged based on execution time in GB-Hours.
- Free for the first 100 GB-Hours
- pro plan: $20 for 1,000 GB-Hours
- $180 per additional 1,000 GB-Hours/ $0.18 per 1 GB-Hour.
- Edge Network:
- Fast Data Transfer: refers to the data transferred between Vercel's CDN and the end user's device
- Free for 100 GB
- $20 for 1TB
- $150 per 1TB/ $0.15 per 1 GB
- Fast Origin Transfer: refers to the data transferred between Vercel's CDN and the "origin" - which in this case refers to Vercel's compute resources like Serverless Functions, Middleware, or the Data Cache
- Free for 10 GB
- $20 for 100 GB
- $6 per 100 GB/ $0.06 per 1 GB
- Edge Requests
- Free for the first 1M requests
- $20 for the 10M requests
- $20 per additional 10M requests/ $2 per 1M requests
- Edge Request Additional CPU Duration
- Free till unspecified time
- $20 for the first hour
- $0.30 per additional hour
- Fast Data Transfer: refers to the data transferred between Vercel's CDN and the end user's device
Serverless Functions
Serverless functions in Next.js are a way to create backend logic without managing a traditional server. They're essentially small pieces of code that run on demand in response to specific events or requests.
Here are some common use cases for serverless functions in Next.js:
- API Endpoints
- Form Handling
- Authentication
- External API Integration
- Image Processing
- Email Sending
- Database Operations
- Webhooks
- Payment Processing
Cost Structure
When you create a serverless function you will be charged for the following
1. Function Invocations
You are charged based on the number of times your functions get invoked. This includes both successful and errored invocations, but does not include cache hits. The number of invocations is calculated based on the number of times your function gets called, regardless of the response status code. Vercel Docs
Each time your application triggers your serverless function, it will raise the usage plan by counting the number of invocations that have occurred.
- Free for the first 100K invocations
- $20 for the first 1M invocations
- $0.60 per additional 1M invocations.
2. Function Duration
You are charged based on the amount of time your Serverless Functions has run. This is sometimes called "wall-clock time , which refers to the actual time elapsed during a process, similar to how you would measure time passing on a wall clock. It includes all time spent from start to finish of the process, regardless of whether that time was actively used for processing or spent waiting for a streamed response. Function Duration is calculated in GB-Hours, which is the memory allocated for each Function in GB x the time in hours they were running. Vercel Docs
Whenever your app calls your serverless function, it increases the usage plan.
- Free the first 1,00 GB-Hours
- $20 for the first 1,000 GB-Hours
- $180 per additional 1,000 GB-Hours/ $0.18 per 1 GB-Hour.
3. Edge Network Usage
Vercel doesn't charge you only for function calls and function duration, but also for the response size and the request count.
Serverless Function execution will increase edge network usage as well. Caching your Serverless Function reduces the GB-hours of your Serverless Function, but does not reduce the network usage that comes with executing your Serverless Function. Vercel Docs
3.1 Transferred data size
The size of the requests as well as the response of the serverless functions
will increase the following metrics:
- Fast Data Transfer (FDT): refers to the data transferred between Vercel's CDN and the end user's device
- Free for 100 GB
- $20 for 1TB
- $150 per 1TB/ $0.15 per 1 GB
- Fast Origin Transfer (FOT): refers to the data transferred between Vercel's CDN and the "origin" - which in this case refers to Vercel's compute resources like Serverless Functions, Middleware, or the Data Cache
- Free for 10 GB
- $20 for 100 GB
- $6 per 100 GB/ $0.06 per 1 GB
3.2 Requests
Also, Vercel charges for the count of requests your app makes after 1M requests.
- Edge Requests are all requests made to your site, including static assets and functions.
- Free for the first 1M requests
- $20 for the 10M requests
- $20 per additional 10M requests/ $2 per 1M requests
- Edge Request Additional CPU Duration
- Free till unspecified time
- $20 for the first hour
- $0.30 per additional hour
Finally
While Vercel offers an outstanding DX and unparalleled convenience, these benefits come with a price. It's essential to evaluate whether your project's revenue can comfortably cover total costs. If not, consider alternatives such as self-hosting (Coolify), other hosting providers such as Netlity, Cloudflare Pages, Render, and Fly.io
I hope this guide provides you with a more comprehensive understanding of the expenses linked to your serverless functions.
Need help lowering your costs and optimizing your usage plan on Vercel? Feel free to reach out to me via email or DM me on X.