How to spin up one fly machine per request?

I’d like to make a service which spins up workers to process jobs on fly machines, they would need to be individually addressable so a client can send information about the job.

They don’t need their own IP address, but a client external to fly would need to be able to contact their machine specifically so it can process the job then exit.

Is this possible?

The end goal is to have a server that can create workers to do a job, some client can (maybe) send information to the worker, the worker can do the job then exit. Each worker would only be responsible for one job, not multiple so clients must be able to send info specifically to the worker spun up for them by the server.

Sounds like a usecase for AWS Simple Workflow (use it with Flow) or Temporal, imo.

With Fly Machines, one can consider using a (correct) distributed lock and leasing service (like the DynamoDB Lock Client) to get a mutex for a particular client + task combination.

As for one Machine per request, I think you can set request / connections count to 1 and may be that works (haven’t tried it myself)?

Looks like one machine per request may work: Add concurrency to machine config · superfly/docs@2808323 · GitHub? @kurt?

Very similar to Lambda, Machines take some time to get created but then only ms to (re)start once stopped.

So you could try to replicate Lambda where you create a new machine when needed but suffer a “cold start” time (similar to Lambda, altho I suspect it’ll be longer on Fly), and then keep machines around for a period of time to accept new requests.

Alternatively, you can have a pool of machines ready to go (just stopped). They’ll wakeup on network access.

You’ll have to figure out the business logic of sending “jobs” to the machines so they can process whatever work needs to get done.

An external client would likely have to contact a service you host on Fly, and that service would broker the job to a machine. However Machines can accept external connections from the public internet. But they need to exist already, they won’t get created on the fly from a new request.

I personally like the model of having your own little application act as a broker to create or run a user request on a machine, without exposing machines to your users directly (if possible in your model).

1 Like

Per this thread, it looks like one request per machine should work when hard_limit is set to 1.

Unsure if max-per-region in the [deploy] section of a fly.toml config is honoured for Machine apps: how are app vms (re)used - #7 by jeffmcaffer