Fly.ioTutorial
Delegating tasks to Fly Machines
Summary
Resource-intensive web tasks such as resizing high-resolution images can require substantially more memory or CPU than ordinary request handling, making an always-powerful web server inefficient. The article presents a framework- and language-agnostic delegation pattern using Fly Machines, the private network, and two process groups that run the same application with different Machine specifications. A Next.js route calls a wrapper that executes the task when FLY_PROCESS_GROUP is worker; otherwise, it sends a POST request to the identical endpoint through the worker’s internal hostname, returning the worker’s response. The example uses a lighter web group and a performance-4x worker group, with local development using separate ports and production routing over .internal addresses; the author calls the implementation naïve and suggests queues and worker timeouts to improve resilience and control worker availability.
Context
Web applications may have occasional tasks, such as resizing images, that demand substantially more memory or CPU than ordinary request handling. Keeping that capacity available for every web request can be costly when the resource-intensive work is infrequent.
Approach / What changed
Run the application in two process groups: a web group on lighter Machines and an internal worker group on more powerful Machines. A wrapper executes the task directly when running on the worker; otherwise, it forwards a POST request to the same endpoint using the worker’s Fly.io private-network hostname and returns the response.
Takeaways
The delegation wrapper checks FLY_PROCESS_GROUP to avoid forwarding when the request has already reached a worker.
Production requests reach the worker through a .internal hostname, while local development uses separate processes on ports 3000 and 3001.
The implementation is described as naïve; a queue could improve resilience, and timeouts could start or stop worker Machines as needed.
Source and freshness
- Source
- fly.io
- Published
- Added to Yomu
- Last updated
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.