> ## Documentation Index
> Fetch the complete documentation index at: https://latchkey.getgalaxy.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Latchkey architecture

> How Latchkey answers checks from memory, keeps every replica current through the access revision, and behaves when parts fail.

Every check is answered from memory. Postgres holds the truth. The
management API's lists, and its check that the caller is an Admin, read the
database directly, so an Admin always sees their own last write. In service
mode a Go binary runs as one or more replicas over a single Postgres 15 or
later database. Products call the Check API, register their kinds when they
start and purge bindings after deletes. Admins write through the management
API, and the directory owner writes through directory sync. Every write
reaches all replicas through a Postgres `NOTIFY`.

In embedded mode there is no separate service. The product runs the same
package and holds the snapshot itself. See
[Deployment](https://latchkey.getgalaxy.io/pages/guides/deployment/overview.md) for choosing a mode.

## The snapshot

Each replica loads a full snapshot when it starts and evaluates against it.
The snapshot holds kinds, roles, bindings, subjects and group membership, and
checks never touch the database.

Subjects and memberships set a tenant's size, not bindings. A tenant with
20,000 users in 100,000 group memberships takes roughly 10 to 20 MB. The
snapshot interns ids as integers and shares kinds and built-in roles across
tenants. Each replica exports its snapshot size per tenant, and the chart
sets `GOMEMLIMIT`. A standalone instance that serves many large tenants
should be split across instances. In embedded mode the snapshot lives in each
product replica.

## The access revision

1. Each tenant has one counter that only goes up. It is incremented in the
   same transaction as any write that can change a decision, which covers
   roles, bindings, groups, group membership and subject activation. Saving a
   just-in-time subject does not bump it, and neither does a write that
   changes nothing. Domains and kinds, which every tenant shares, bump a
   separate instance revision.
2. `NOTIFY` carries the tenant and its new revision, or the instance and its
   new revision, and is only a hint. The replica reads the revision again and
   does nothing if it has not moved. Reloads run one at a time, and one
   that an earlier reload already satisfied is skipped.
   Each replica listens on one connection of its own, outside its pool. A
   connection pooler in transaction mode does not deliver notifications, so
   a replica behind one relies on its poll, which runs every 5 seconds.
3. Every decision returns both of the snapshot's revisions, the tenant's and
   the instance's. Management writes return the new revision of whichever
   counter they bumped.
4. Callers may cache a decision for a short time, keyed by tenant and inputs.
   When a response carries a newer revision for a tenant, the caller drops
   that tenant's older entries. Kind changes reach SDK caches within their
   time to live.
5. A caller that needs to read its own writes passes the revision its write
   returned. The admin UI after a grant passes `min_revision`, and a product
   checking right after registering a new kind passes
   `min_instance_revision`. The two are separate counters, so a high tenant
   revision never stands in for a missing kind change. A replica that is
   behind reads the revisions at once rather than waiting for `NOTIFY`. A
   write's revision is committed before its caller learns it, so if that read
   does not reach it the check denies with `REVISION_TIMEOUT` at once. Only a
   failed read is retried, backing off, for up to 2 seconds. If no read
   succeeds the check answers `UNAVAILABLE`, because the answer is unknown
   rather than a deny.
6. A replica that cannot confirm a tenant's revision against Postgres for 5
   minutes, which `LATCHKEY_MAX_STALENESS` configures, answers `UNAVAILABLE`
   for that tenant. Callers treat that as a deny, so a replica cut off from
   the database cannot keep serving revoked access. Each tenant is confirmed
   on its own, so one tenant that fails to load does not stop the others.

## Failure behavior

| Failure | Effect |
| - | - |
| Postgres down | Replicas keep answering from their last snapshot for up to 5 minutes, then deny. Management writes and directory sync fail and their callers retry. New users are still evaluated in memory and saved when the database returns. |
| One replica down | Other replicas serve. With a single replica, every product in the tenant denies until it restarts. |
| A replica restarts while Postgres is down | It cannot load a snapshot and stays unready. If no other replica is up, the tenant denies. |
| A replica loses its `LISTEN` connection | On reconnect it compares revisions and catches up. It also polls every tenant it serves in one query every few seconds as a backstop. |
| All replicas unreachable | Callers deny. Cached allows are not served past their time to live. |
| Embedded mode | There is no separate service to lose. Authorization is up whenever the product is. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.