Openstatus www.openstatus.dev
6
fork

Configure Feed

Select the types of activity you want to include in your feed.

Blog post fly (#419)

* 📝 blog post

* 📝 blog post

authored by

Thibault Le Ouay and committed by
GitHub
8ba290c4 3bafddf2

+123
apps/web/public/assets/posts/migration-backend-from-vercel-to-fly/fly-dashboard.png

This is a binary file and will not be displayed.

apps/web/public/assets/posts/migration-backend-from-vercel-to-fly/hono-server.png

This is a binary file and will not be displayed.

apps/web/public/assets/posts/migration-backend-from-vercel-to-fly/next-server.png

This is a binary file and will not be displayed.

+123
apps/web/src/content/posts/migration-backend-from-vercel-to-fly.mdx
··· 1 + --- 2 + title: 3 + Why we migrated our backend from Vercel to Fly.io and the challenges we faced. 4 + description: 5 + Learn why we switched from Vercel to Fly.io for our backend and the challenges 6 + we encountered. 7 + author: 8 + name: Thibault Le Ouay Ducasse 9 + url: https://twitter.com/thibaultleouay 10 + publishedAt: 2023-10-29 11 + image: /assets/posts/migration-backend-from-vercel-to-fly/fly-dashboard.png 12 + --- 13 + 14 + In this article we are going to see the reason that made us change our backend 15 + to Fly.io and the challenges we had during the migrations. 16 + 17 + We chose [Hono](https://hono.dev/) as our API server with [Bun](https://bun.sh/) 18 + as the runtime and pick Fly.io as your hosting service. 19 + 20 + ## 🤔 Why did we want to move our backend from Vercel? 21 + 22 + ### ⚡ A lightweight Server 23 + 24 + We required a lightweight server with a simple REST API for our monitoring 25 + endpoint. Deploying a simple Express server is possible, but it is not 26 + specifically designed for this purpose. 27 + 28 + `It's possible to deploy an Express.js application as a single Serverless Function, but it comes with drawbacks and should only be used as a migration path. Instead, use Next.js or embrace multiple Serverless Functions as you incrementally migrate to the Vercel platform.` 29 + 30 + [Source Vercel Doc](https://vercel.com/guides/using-express-with-vercel) 31 + 32 + Also launching a clean and new Next.js server takes 2.5 seconds on my MacBook 33 + Pro M1 and takes 110mb of RAM, and it includes unnecessary extra features from 34 + Next.js. Our prod Next.js app takes about 5 seconds to launch on my computer 35 + (contentlayer). 36 + 37 + ![Next server](/assets/posts/migration-backend-from-vercel-to-fly/next-server.png) 38 + 39 + For comparaison launching our current server takes 0.19ms and only takes 91mb. 40 + Our current server stack is Hono + Bun. 41 + 42 + ![Hono server](/assets/posts/migration-backend-from-vercel-to-fly/hono-server.png) 43 + 44 + ### 💸 Pricing 45 + 46 + We initially aimed to provide multi-region monitoring for all users while 47 + maintaining a free tier. On Vercel, if you want a multi-region function, you 48 + need to opt for Edge Functions. Edge functions are cost-effective as you only 49 + pay for the actual CPU execution. This means that you won't be billed for idle 50 + times when fetching data. 51 + 52 + It's still affordable, but we are a bootstrap business and it difficult for us 53 + to predict our monthly expenses. If we experience an increase in new users, our 54 + costs will also increase accordingly. 55 + 56 + Here's the math for the cost of one monitor for a user: 57 + 58 + ``` 59 + 6 (10 min monitors) * 24 * 30 * 3 (average execution unit per monitor) * 6 (number of regions) = 77,760 executions units per month 60 + 61 + 77,600 * (2/1,000,000) = 0.15c per monitor monthly 62 + ``` 63 + 64 + We have over 1000 monitors, and the monthly cost to run them would be $150. 65 + 66 + While on fly we only have 6 servers with 2vcpu/512Mb It cost us $23.34 monthly 67 + ($3.89\*6). 68 + 69 + ## 🤯 What challenges did we face during our migrations? 70 + 71 + ### Docker + monorepo = 🪨 72 + 73 + We are deploying to fly.io. We have to setup our app as a Docker image. Our apps 74 + is in a monorepo. Our initial image was over 2 GB in size, which was excessively 75 + large for a basic server. 76 + 77 + Our image included everything, which was inefficient. After optimizing, our 78 + image now occupies only 700MB. Although it is still somewhat large, it is a 79 + significant improvement over our initial version. 80 + 81 + It was something we never had to manage with Vercel deployment. 82 + 83 + ### ⏳ Fly deployment timed 84 + 85 + Our Fly deployments have been experiencing frequent timeouts without any 86 + specific reason. The only solution we have discovered is to increase the timeout 87 + duration. 88 + 89 + ``` 90 + flyctl deploy --wait-timeout=500 91 + ``` 92 + 93 + Based on our experience, Fly deployments are generally less reliable compared 94 + (more timed out) to Vercel deployments. Additionally, we have not discovered a 95 + quick method to rollback to the previous version. 96 + 97 + ### 🐛 The Bun bug 98 + 99 + When we migrated to Fly, we decided to use Bun as our runtime. However, in the 100 + first few hours after the migration, we observed an unexplained increase in 101 + request failures. 102 + 103 + After digging into the Bun GitHub we found a solution: We needed to set the 104 + `keepalive` parameter to `false`. This is necessary because closed connections 105 + are not automatically removed and remain in the `CLOSE_WAIT` state. 106 + 107 + Here's the GitHub issue: 108 + 109 + https://github.com/oven-sh/bun/issues/3327 110 + 111 + I wish it had been documented elsewhere. 112 + 113 + ## Our conclusion 114 + 115 + We are pleased with our migration to Fly.io, although it was accompanied by a 116 + challenging weekend. And we still love Vercel, they offer a super product, it 117 + removes a lot of pain for the developers.However, if you require a hosting an 118 + application other than Next.js, it may not be the best option. 119 + 120 + We are still using it for our frontend. 121 + 122 + Create an account on [OpenStatus](https://www.openstatus.dev/sign-up?ref=blog3) 123 + to start monitoring our website and managing your incidents for free.