cardinal

Request path

What happens between a request reaching a node and its response.

Every node runs the same code, and a request keeps nothing on the node after it answers. The next request from the same browser can land on any node.

One API request

Take PUT /api/rec/Customer/CUST-0001 from a signed-in user.

Endpoint. Bandit receives the request. CardinalWeb.Endpoint hands every path but the realtime socket to CardinalWeb.Router.

Request id. The router drops any client x-request-id and assigns its own. That id becomes the trace_id of any error.

Health. /health answers here, before any site lookup.

Identity. The identity plug reads the client IP and scheme through CARDINAL_TRUSTED_PROXIES, finds the site by the Host header (404 unknown, 503 not active), then the user from an API key or the session cookie, or Guest. It builds Cardinal.Ctx with the site, user, roles, locale and CSRF token.

CSRF and body. A session's unsafe request must carry x-cardinal-csrf. The JSON body is parsed, up to 8 MB.

Dispatch. /api/rec goes to CardinalWeb.Api, which checks the user's right on the RecType before it looks at the request's shape.

Transaction. TxnService.run opens one Postgres transaction, sets search_path to the site's schema with SET LOCAL, and reads the site's meta_version. GET requests run read-only.

Metadata. MetaService looks up the compiled RecTypes in the node's ETS cache under (site, meta_version). On a miss it compiles them through the cardinal_meta NIF.

Record. Cardinal.DB runs the lifecycle: prepare and validate in Rust, the app's controller hooks in Elixir, the row write with the optimistic lock on modified, and child rows. Permission checks run through the cardinal_perm NIF.

Commit. The transaction commits. Realtime messages, queued mail and jobs written during it take effect only now, and TxnService then runs the after-commit effects the request queued. A rollback sends and queues nothing.

Response. The answer is {"data": ...} with the x-cardinal-meta and x-cardinal-perm headers. A failure is the error envelope. An exception answers 500 and writes an Error Log under the request id.

Where each kind of path goes

PathHandler
/api/realtime/websocketCardinalWeb.Endpoint, then RealtimeSocket and RealtimeChannel
/api/auth/*, /api/setup/*, /api/desk/bootCardinalWeb.Router
/api/rec, /api/meta, /api/rpc, /api/file, /api/print, /api/desk/*, /api/blobCardinalWeb.Api
/api/<app>/<route>The app's Cardinal.ApiRoute module
/desk/*, /assets/*Static files from the built bundles
/<mount>/*CardinalWeb.AppFrontend, an app's own frontend
Anything elseCardinalWeb.Website and WebsiteService

Full list in Routes.

Why no step needs node state

StepShared state it reads
IdentityThe session row in Postgres
MetadataPostgres's meta_version. The ETS cache only saves work
Record writePostgres, under the request's own transaction
Realtimepg_notify inside the transaction. Every node listens
JobsOban rows inserted in the same transaction
FilesThe S3 bucket

A job follows the same path from step 7 on. Its worker restores the Ctx of the user who queued it and runs perform/2 in that site's transaction.

On this page