Vercel Functions enable developers to write server-side logic to connect to their database or external APIs. When a function starts for the first time, it’s a "cold start". Subsequent requests to that function are then considered warm.
This guide will help you improve the performance of your functions and understand how to determine if the latency increase is from a cold start. We recommend enabling Fluid compute. Fluid, which is our new execution model for Vercel Functions, is more cost-efficient and has automatic optimizations to prevent cold starts.
The following suggestions will help you ensure optimal performance of your Vercel Functions:
- Observe your function performance: Use the Observability tab to understand function startup performance, the percentage of cold starts, latency to external APIs, and more.
- Upgrade to Fluid compute: Fluid is more cost-efficient and has automatic optimizations to prevent cold starts Learn more.
- Choose the correct region for your functions: Functions are deployed to US East by default. You can change the default region for functions in your project settings. Choose a region that’s closest to your data source for optimal performance.
- Choose smaller dependencies inside your functions: Startup times are correlated to function size, which is often mostly from external dependencies. If you have large dependencies, parsing and evaluating JavaScript code can take seconds or longer. Review your bundle and try to eliminate larger dependencies using a bundle analyzer.
- Use proper caching headers: Function responses can be cached using
Cache-Control
headers. This will help ensure optimal performance for repeat visitors, and Vercel’s Edge Network cache even supportsstale-while-revalidate
headers. Note that cache misses will still need to request data from your origin (e.g. database) rather than reading directly from the cache (faster). - Update to the latest version of Next.js: The latest version of Next.js (v14.2+) includes significant startup performance improvements, especially if using external packages like Sentry.
- If using Pages Router, bundle external dependencies: Setting
bundlePagesExternals
totrue
under theexperimental
flag is strongly recommended for customers using Pages Router, and will result in a large reduction in cold start seconds. - Use dynamic imports: While an uncommon use case, customers with sites using divergent code paths, using dynamic imports to load only the necessary code can reduce cold start times.