Eliminating additional bandwidth charges for multi-zone sites on Vercel
Tags: vercel, cloudflare
Published: 2023-12-22 16:03:10

When deploying applications to any kind of cloud platform, be that AWS, Azure, GCP or even a higher level abstraction like Vercel, monitoring cost is an important aspect of the development lifecycle. We should be continuously trying to reduce our monthly spend whenever possible. In this post, we show how we eliminated additional bandwidth charges on a recent multi-zone Vercel client project.

## A Multi Zone Project
The client in question has a large Vercel-based application that currently has 10 projects deployed with a routing structure like this:

![](https://instil.co/static/d5a4e6972f28b3023f9ed5d95354622e/e8950/vercel-charges-multi-zone.png)
_<p class="text-center mb-5">Project structure showing parent project rewriting to several child projects.</p>_


Next.js/Vercel refer to this kind of setup as a [“Multi-Zone” Project](https://vercel.com/guides/how-can-i-serve-multiple-projects-under-a-single-domain).

> With multi zones support, you can merge both these apps into a single one allowing your customers to browse it using a single URL, but you can develop and deploy both apps independently.

This enabled us to deploy multiple versions of the same project with different configuration but with the feel of a single site to the customers.

## Multi Zones == Double Billing
There is one issue, however, that both Vercel and Next.js documentation neglects to mention and that is, you will be "double billed" for bandwidth usage in a multi zone project. This is because the rewrites work by fetching and serving the content from the child site. Therefore, both the parent site and the rewritten target would consume bandwidth by serving the same content.

This is fine if you can safely live within the [1TB limit on bandwidth](https://vercel.com/docs/limits/usage#bandwidth) but go above this limit and things start to get expensive at $40 per 100GB (at the time of writing this article).

We decided to see if we could provide the same functionality provided by the rewrites on Vercel but not pay a premium for the privilege.

## Rewriting the rewrites
Our site used [Cloudflare](https://www.cloudflare.com) to handle all our DNS needs. Cloudfare has a number of rewrite options but as we needed to make a decision based on a) the URL path and b) rewriting to a specific domain, the only option (as a non enterprise customer) was to use Cloudflare workers (via [worker routes](https://developers.cloudflare.com/workers/configuration/routing/routes/)).

The functionality is simple - we configure a worker route for the path that should rewrite to a child project, fetch the content from the child site in our Cloudflare worker and then return it.

![](https://instil.co/static/bcf299cefd352de7d4be714e8140963a/0a47e/vercel-charges-cloudflare-worker-route.png)
_<p class="text-center mb-5">Configuring a Cloudflare worker route, with a wildcard based path and which worker to execute</p>_


Worker routes have wildcard support as [documented here](https://developers.cloudflare.com/workers/configuration/routing/routes/#matching-behavior), so it’s easy to add a catch-all route for a specific path.

The code in the url rewrite worker is simple too:

```typescript
export default {
  async fetch(originalRequest, env, ctx) {
    const originalUrl = new URL(originalRequest.url);
    const newHost = "child-project.co.uk";
    const newUrl = originalRequest.url.replace(originalUrl.host, newHost);
    const request = new Request(newUrl, {
      body: originalRequest.body,
      headers: originalRequest.headers,
      method: originalRequest.method,
      redirect: originalRequest.redirect
    });
    return await fetch(request);
  },
};
```
This snippet simply fetches the content from `child-project.co.uk` but maintains the path, headers, body etc.

## Let's talk cost

With Cloudflare workers you don’t pay **any** egress fees. So this simple hack will immediately save you the $40 per 100GB bandwidth cost. For our client, that represented a total saving of **$400 per month** based on an average monthly usage of 2TB. (Remember the first 1TB is free and Vercel will round the 1.9TB bandwidth usage up to 2TB!)

![](https://instil.co/static/ae19e7c8084e30a032e20e4e2ee46296/84a78/vercel-charges-bandwidth-usage.png)
_<p class="text-center mb-5">Total bandwidth usage for the project</p>_


Looking across 7 days of requests on Cloudflare for our busiest child project, you can see that our little workers are handling plenty of traffic. This particular site serves double the traffic of all our other child projects combined.

![](https://instil.co/static/1d2e2193d3d0cb6f3e4311ad768bd22f/e8950/vercel-charges-cloudflare-worker-summary.png)
_<p class="text-center mb-5">Graph showing how many times the worker has been invoked over a 7 day period.</p>_


The costs for these requests are incredibly cheap as well. When we plugged these numbers into the [Unofficial Cloudflare Workers' pricing calculator](https://pricing.ceru.dev/?rpm=4500000&spr=0.5&url=), the weekly cost for this site was $5.53. The cost for all sites works out at about $50 per month.

## Conclusion

In conclusion, by using Cloudflare workers and configuring worker routes, we were able to replicate the rewrite functionality provided by Vercel without incurring any egress fees. This resulted in significant cost savings for our customer, with an estimated **reduction of 87.5% in monthly costs**. With Cloudflare Workers free egress fees, our customer saved $40 per 100GB of bandwidth. This cost-effective approach allowed us to efficiently handle high traffic volumes while keeping cost low.