Architecture
Karta is one Elixir application (an OTP release) that runs every part of the platform in one process per node: the SMTP listeners, the HTTP endpoints, the delivery queue and the housekeeping jobs. State lives in Postgres (metadata and the job queue) and in S3-compatible object storage (message bodies). Several nodes can share one database and one bucket.
applications recipients' mail servers
│ ▲
SMTP 587/465 │ HTTP 4000 (/api/v1, /api/v2, /mailgun/v3, │ SMTP 25
(submission) │ /sendgrid/v3, /postmark) │
▼ │
┌───────────────────────────────────────────────────────────────────┐
│ Karta node │
│ │
│ SMTP listeners ─┐ ┌─> delivery jobs ─────┘
│ ├─> authorise ─> store ───┤ (Oban, queue
│ HTTP APIs ──────┘ (verified (row + │ "outbound")
│ domains) blob) │
│ └─> tracking host (4001)
│ retention, sweeper, metrics (9568), /health │
└───────────────┬──────────────────────────────┬────────────────────┘
│ │
Postgres S3-compatible bucket
(organisations, servers, (message bodies, AES-256-GCM,
messages, recipients, a key per message)
deliveries, jobs, ...)Tenancy
- An organisation is a customer (tenant). It owns domains and mail servers.
- A mail server (
Karta.Servers.Server) is one sending unit: it has its own credentials, suppression list, messages, retention settings, send limit and tracking settings. It is not a host; the name comes from Postal. - A domain belongs to an organisation and must be verified before
any of the organisation's servers can send from it: by a TXT record with
its token, which Karta checks every hour, or by an administrator. A
domain name is verified for at most one organisation at a time. Karta
checks its DNS records hourly too (
Karta.Domains.Health), takes its DKIM key rotations a step further (Karta.Domains.Rotation) and, where the instance publishes its zone, publishes its records through its DNS provider (Karta.DNS.Publish; PowerDNS by default). It can serve the domain's MTA-STS policy (Karta.MTASTS.Hosting) and BIMI logo (Karta.BIMI) on the tracking endpoint. - Credentials belong to a server: API keys (for the HTTP APIs) and SMTP credentials (for submission).
Every lookup in Karta is scoped by server or organisation: a message, a credential or a suppression of another server is simply not found.
Accepting a message
Mail enters through the submission listeners (Karta.SMTP.Policy.Submission)
or one of the HTTP APIs (Karta.Sending). Both paths do the same:
- Authenticate the client with an SMTP credential or an API key of a server.
- Authorise the message: every mailbox in From and Sender, and the
envelope sender, must be at a verified domain of the server's
organisation. Addresses are parsed, never matched as text
(
Karta.Mail.Authorisation). - Store it with a two-step protocol (
Karta.Messages): apendingrow is committed, the body (with Karta'sReceived:field in front) is encrypted and written to the blob store, and then, in one transaction, the row becomesaccepted, each recipient gets a row and each recipient domain gets a delivery job. Only then does the client get250or202. A failure on the way returns a temporary error and removes what was written.
Recipients on the server's suppression list are recorded with the status
suppressed and are never delivered to.
After the transaction commits, the acceptance is announced on the
server's topic (Karta.Changes, over Phoenix.PubSub on every connected
node), as are a delivery run, an open or a click and the removal of
messages: admin pages open on that server read what they show again
(Live updates).
Delivering a message
A delivery job (Karta.Outbound.Deliverer, Oban queue outbound, or
outbound_<pool> for a server in an IP pool, which runs
only on the nodes that hold the pool's addresses) handles one message for
the recipients at one domain:
- Waits until the recipients are due (scheduled sending, retry times) and until the server's send limit allows another attempt.
- Resolves the domain's MX hosts through the local validating resolver
(
Karta.Outbound.MXResolver), skipping the addressesKarta.NetGuardrefuses (private, loopback, carrier-grade NAT, unique local, NAT64, 6to4 and other special ranges). - Takes what the limits allow (
Karta.Outbound.Limits): a connection slot and recipients per minute for the destination, and room under the warm-up limits of the pool's addresses. Recipients held back wait, without an attempt. - Reads and decrypts the stored message in pieces, once for the facts a delivery needs (its size, whether it is 8-bit, the canonical body hash a signature covers) and once per address it is sent to, so that a message of any size costs the memory of a few chunks.
- Adds tracking and
List-Unsubscribeper recipient when the server has them on, then signs with the domain's active DKIM keys. - Runs an SMTP transaction with the first address that answers
(
Karta.Outbound.Transfer), with opportunistic or verified TLS. - Writes a delivery record per recipient and the recipient's new state (delivered, deferred until a retry time, or bounced) in one transaction, together with a delivery status notification when one is due, and the webhook events of the outcomes with their jobs.
Temporary failures are retried for 5 days. A sweep every 15 minutes queues delivery again for recipients whose job was lost.
See Delivery, retries and bounces.
Storage and encryption
- Postgres holds everything except message bodies: organisations, domains, DKIM keys (private keys encrypted), credentials (only hashes), servers, messages, recipients, delivery attempts, suppressions, tracked links and events, and the Oban job table.
- The blob store holds message bodies, each encrypted with AES-256-GCM
in 64 KiB chunks under a key derived for that message from the vault key
(
Karta.Storage.Cipher). - The vault key (
KARTA_VAULT_KEY) encrypts secrets at rest and is the root of the keys for stored bodies, tracking tokens, VERP tags and paging tokens. See Storage, encryption and retention.
DNS
Karta has its own DNS client (Karta.DNS) and asks one recursive,
DNSSEC-validating resolver, normally Unbound on the same host. Answers are
cached in memory within TTL bounds. See DNS resolver.
HTTP endpoints
Karta runs up to three HTTP listeners:
| Listener | Default port | Serves |
|---|---|---|
Main endpoint (KartaWeb.Endpoint) | 4000 (PORT) | /health, the four APIs, /api/v2/openapi.json |
Tracking host (KartaWeb.TrackingEndpoint) | 4001 (KARTA_TRACKING_PORT) | only /o/…, /c/…, /u/…, and /tls/ask for the reverse proxy |
Metrics (KartaWeb.MetricsPlug) | 9568 (METRICS_PORT) | only GET /metrics |
The tracking host is a separate endpoint so that neither the APIs nor the
tracking links can be reached through the other, whatever Host header a
client sends.
SMTP listeners
Up to three listeners (Karta.SMTP.Listener): mx (port 25),
submission (587, STARTTLS) and submissions (465, implicit TLS). The
mx listener accepts mail for the addresses that the servers' routes name
on their verified domains and refuses every other recipient
(Receiving mail). See SMTP listeners.
Background work
| Work | When | Module |
|---|---|---|
| Delivery | one job per message and recipient domain | Karta.Outbound.Deliverer |
| Webhook requests | one job per event and webhook (Oban queue webhooks) | Karta.Webhooks.Sender |
| Removing old webhook events | every night at 03:52 UTC (Oban cron) | Karta.Webhooks.Pruner |
| Sweep for lost delivery jobs | every 15 minutes (Oban cron) | Karta.Outbound.Sweeper |
| Which IP pool addresses this node holds; starting and pausing the pools' queues | every 5 seconds, on every node | Karta.IPPools.Monitor |
| Retention | every hour, first 60 seconds after start | Karta.Messages.Retention |
| Oban job pruning | finished jobs older than 7 days | Oban pruner |
| Rescue of jobs of a dead node | after 60 minutes | Oban lifeline |
Source layout
The Development page maps the source tree; the module reference in the sidebar documents every public module.