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:

  1. Authenticate the client with an SMTP credential or an API key of a server.
  2. 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).
  3. Store it with a two-step protocol (Karta.Messages): a pending row is committed, the body (with Karta's Received: field in front) is encrypted and written to the blob store, and then, in one transaction, the row becomes accepted, each recipient gets a row and each recipient domain gets a delivery job. Only then does the client get 250 or 202. 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:

  1. Waits until the recipients are due (scheduled sending, retry times) and until the server's send limit allows another attempt.
  2. Resolves the domain's MX hosts through the local validating resolver (Karta.Outbound.MXResolver), skipping the addresses Karta.NetGuard refuses (private, loopback, carrier-grade NAT, unique local, NAT64, 6to4 and other special ranges).
  3. 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.
  4. 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.
  5. Adds tracking and List-Unsubscribe per recipient when the server has them on, then signs with the domain's active DKIM keys.
  6. Runs an SMTP transaction with the first address that answers (Karta.Outbound.Transfer), with opportunistic or verified TLS.
  7. 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:

ListenerDefault portServes
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

WorkWhenModule
Deliveryone job per message and recipient domainKarta.Outbound.Deliverer
Webhook requestsone job per event and webhook (Oban queue webhooks)Karta.Webhooks.Sender
Removing old webhook eventsevery night at 03:52 UTC (Oban cron)Karta.Webhooks.Pruner
Sweep for lost delivery jobsevery 15 minutes (Oban cron)Karta.Outbound.Sweeper
Which IP pool addresses this node holds; starting and pausing the pools' queuesevery 5 seconds, on every nodeKarta.IPPools.Monitor
Retentionevery hour, first 60 seconds after startKarta.Messages.Retention
Oban job pruningfinished jobs older than 7 daysOban pruner
Rescue of jobs of a dead nodeafter 60 minutesOban lifeline

Source layout

The Development page maps the source tree; the module reference in the sidebar documents every public module.