Skip to content

Placing services

Start with one process. A call within a process is a method call, while a call across processes costs a network round trip and JSON encoding. Give a service its own process when you have a reason, which the layout then records. Moving it back costs one edit.

Reasons to split

It parses untrusted files. A process with privileges = "reduced" runs in a sandbox, so a parser's bug reaches less. The security guide says what each target does.

It blocks, or uses the CPU for long. One process runs one event loop. Work that holds it delays every call in that process, so put the work in a job, in a worker process.

It has heavy dependencies. A process imports only the services it runs. A service that needs machine-learning libraries declares them as an optional extra of its package, and an image for the other processes leaves them out.

It needs a different number of replicas. instances is a setting of the process, so services that scale apart live apart.

It needs threads to run in parallel. A process marked free_threaded refuses to start with the GIL, on a build without one, or after an import turns it back on. Keep there only what the free-threaded build supports. Every other process stays free to use anything.

It should restart alone. A process that gives up on one service stops the others in it.

Connecting processes

A process that others call needs a url:

url When
unix:run/web.sock processes on one host. It needs no port, and only the application's user can open it
http://host:port processes on several hosts, or a platform that gives each process an address

A process that only calls others needs no url, as worker shows in the tutorial. It then has no listener, and so no /readyz.

The layout can read these from the environment, which lets one file serve both a box and a cluster:

[processes.web]
services = ["web", "book"]
url = "${MYXO_URL_WEB:-unix:run/web.sock}"
bind = "${MYXO_BIND:-127.0.0.1}"

The database

The services that publish events must write to the layout's database, since an event commits in the service's transaction. A service may keep other data elsewhere.

On SQLite, all processes must run on one host. The layout is refused otherwise.

Which process serves the public port

On the platforms, the process named web, or the only process, serves $PORT. Name it web in any layout with several processes: myxo export refuses one where it can't tell.