camzabriskie.com

Primers › Deployment

Building the fully pooled version

06 / 06 updated Oct 23, 2026
On this page
  1. A few terms
  2. One request, end to end
  3. Where the tenant comes from
  4. Put isolation in one place
  5. The database
    1. Row-level security on every tenant table
    2. Set the tenant per transaction, not per connection
    3. Indexes start with the tenant
  6. Everything else that stores data
  7. Testing that it holds
  8. Noisy neighbors
  9. Seeing each tenant
  10. A tenant’s life
  11. Releases
  12. Moving a customer out
  13. Where to start
  14. References

The customer separation primer ended at the bottom rung: one deployment of the app, one database, one set of tables, and a tenant_id on every row. This primer is about building that rung so customers stay apart.

Ledgerly, the made-up invoicing app, has put every customer on its standard plan into the pool: Birch, Cedar, Dune, and whoever signs next. There’s one production environment and one test environment for all of them. Nothing in the infrastructure separates Birch from Cedar any more, so the app and the database have to.

AWS’s guidance on tenant isolation starts from a few rules that shape everything below [1]:

A few terms

One request, end to end

A Birch user loading their invoices, and the background job that request starts, take this path:

Teal follows the request. The tenant is read once from the signed token, then set again wherever work is picked up: each database transaction (3) and each background job (4, 5).

Where the tenant comes from

Every user belongs to a tenant, and the identity provider records it. AWS’s SaaS guidance calls this binding of user to tenant a “SaaS identity” and recommends putting the tenant into the token at sign-in, so it travels with every request and every service can read it without a round trip to another service [3]. With Amazon Cognito, for example, it’s a custom attribute on the user, which carries a custom: prefix [6]. Birch’s users get a token that includes something like:

{
  "sub": "user-4821",
  "email": "pat@birch.example.com",
  "custom:tenant_id": "6f1c2e9a-...",
  "custom:tier": "standard"
}

The app reads the tenant from that claim and nowhere else. A tenant ID in the URL (/tenants/6f1c…/invoices) or the request body is something the user typed or the browser sent, and anyone can change it; the signed claim they can’t. When one service calls another, it passes the token along so the next service reads the same claim [3].

Put isolation in one place

The pool model depends on every piece of code acquiring the tenant context before it touches data, which is exactly what AWS warns developers will sometimes forget [1]. Its recommended answer is to move that work into shared libraries, so code gets tenant-scoped access by default rather than by remembering to ask [7].

For Ledgerly that’s two pieces of shared code:

  1. Middleware that runs before every request handler, checks the token, and puts the tenant ID into the request’s context. A request without a valid tenant never reaches a handler.
  2. A database helper that every query goes through. It opens a transaction, sets the tenant from the request context, runs the work, and commits. Handlers never get a raw connection.

A developer writing a new endpoint calls the helper and writes ordinary queries with no WHERE tenant_id in them. Forgetting the tenant isn’t possible, because the helper won’t run without one.

The database

Row-level security on every tenant table

AWS’s PostgreSQL guidance calls RLS required for the pool model and recommends enabling it on every table that holds tenant data [8]. Each such table gets a tenant_id column and a policy comparing it to a per-transaction setting:

ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
ALTER TABLE invoices FORCE ROW LEVEL SECURITY;

CREATE POLICY tenant_isolation ON invoices
  USING (tenant_id = current_setting('app.current_tenant')::uuid);

Three PostgreSQL rules decide whether that policy does anything [4]:

If a connection never set app.current_tenant, current_setting raises an error instead of returning a value [9], so a query that skipped the helper fails loudly rather than reading everything.

Set the tenant per transaction, not per connection

Connection pools reuse one database connection for many requests. A tenant set on the connection with a plain SET would still be there when the next request, maybe Cedar’s, borrows the same connection. PgBouncer’s transaction pooling mode, which hands a connection to a client only for the length of one transaction, lists SET as never supported in that mode [10].

The fix is to scope the setting to the transaction. SET LOCAL takes effect only until the transaction commits or rolls back [11], and set_config does the same when its third argument is true [9], which is handier from application code because the tenant can be passed as a query parameter:

BEGIN;
SELECT set_config('app.current_tenant', $1, true);  -- $1 = Birch's tenant ID
SELECT * FROM invoices WHERE status = 'overdue';    -- RLS adds the tenant filter
COMMIT;                                             -- the setting ends here

The database helper from the previous section runs this around every unit of work.

Indexes start with the tenant

Every query in a pooled database is filtered by tenant, whether written or added by RLS. PostgreSQL’s multicolumn indexes work best when the query constrains the leading (leftmost) columns [12], so indexes on tenant tables usually start with tenant_id: (tenant_id, status) rather than (status). Without that, one large tenant’s rows slow down every other tenant’s lookups.

Everything else that stores data

The database is the obvious store and usually not the only one. AWS points out that each storage service needs its own isolation approach [13]:

StoreHow the tenant is enforced
PostgreSQLRLS, as above; IAM can’t scope access to rows [13]
DynamoDBAn IAM policy condition limiting access to items whose leading key matches the tenant ID [13]
S3Objects under a per-tenant prefix (tenants/<id>/…), read with credentials scoped to that prefix
Caches, search indexesTenant ID in every key or document, and in every lookup
Logs and analyticsTenant ID as a field on every record

For AWS services, “credentials scoped to the tenant” works like this. The pooled service has a broad role, and for each request it obtains temporary credentials narrowed to the current tenant [14]. IAM’s attribute-based access control does this neatly: the tenant ID is passed as a session tag when the role is assumed, and one policy allows access only where the resource’s tag matches that tag [15], [16].

Testing that it holds

A test suite can check isolation, which is cheaper than a customer discovering a gap. These tests cover the common gaps:

Noisy neighbors

In the pool everything is shared, so one customer’s month-end batch can slow everyone. AWS says this can be reduced but not eliminated in a pooled database, through enough capacity, read replicas, and caching [17]. The rest has to happen before the load reaches the database:

Seeing each tenant

A shared database doesn’t know which tenant a slow query belonged to; AWS notes that per-tenant monitoring in the pool model needs instrumentation in the app [17]. So the middleware that sets the tenant also adds it to every log line, metric, and trace. With it, someone can answer “is Birch’s slowness Birch’s own load or everyone’s?” and “which tenant is costing us the most?”.

The same data feeds billing if the price depends on usage. AWS calls this metering: the system measures each tenant’s consumption and sends it to a billing system [20].

A tenant’s life

Releases

One deployment serves everyone, so every release reaches every customer at once, and the shared test environment is the only rehearsal. Two habits soften that. Release behind feature flags that can be turned on per tenant, starting with an internal tenant and a few willing customers. And roll the deployment itself out gradually where the platform supports it, so a bad build reaches a fraction of traffic before all of it.

Moving a customer out

Some customers outgrow the pool or later need a contract that rules it out. Because the app is tenant-aware, a dedicated stack for them is the same app with one tenant in it, on the same version as the pool [21]. Moving them is copying their rows, filtered by tenant_id, into the new stack’s database and pointing their users there. The pool is the cheap default, and the account-per-customer setup is where a customer goes when they need more.

Where to start

  1. Tenant in the token. Add the tenant claim to sign-in before anything else; every later step reads it.
  2. The middleware and the database helper, so no handler can reach data without a tenant.
  3. RLS on every tenant table, with the app on a role that can’t bypass it, and the tests above in CI.
  4. Tenant ID in logs and metrics, so problems can be traced to a customer from the first day.
  5. Per-tenant rate limits, before the first large customer arrives rather than after their first month-end.

References

Show all 21 sourcesHide sources
  1. [1] ↩ a b Amazon Web Services, “The isolation mindset,” SaaS Tenant Isolation Strategies (AWS whitepaper), Aug. 2020. Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/whitepapers/latest/saas-tenant-isolation-strategies/the-isolation-mindset.html
  2. [2] ↩ Amazon Web Services, “Pool isolation,” SaaS Tenant Isolation Strategies (AWS whitepaper), Aug. 2020. Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/whitepapers/latest/saas-tenant-isolation-strategies/pool-isolation.html
  3. [3] ↩ a b c Amazon Web Services, “Identity and access management,” SaaS Lens, AWS Well-Architected Framework. Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/wellarchitected/latest/saas-lens/identity-and-access-management.html
  4. [4] ↩ a b The PostgreSQL Global Development Group, “Row Security Policies,” PostgreSQL Documentation. Accessed: Oct. 23, 2026. [Online]. Available: https://www.postgresql.org/docs/current/ddl-rowsecurity.html
  5. [5] ↩ Amazon Web Services, “Noisy neighbor,” SaaS Lens, AWS Well-Architected Framework. Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/wellarchitected/latest/saas-lens/noisy-neighbor.html
  6. [6] ↩ Amazon Web Services, “Working with user attributes,” Amazon Cognito Developer Guide. Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html
  7. [7] ↩ Amazon Web Services, “Hiding the details of pooled isolation,” SaaS Tenant Isolation Strategies (AWS whitepaper), Aug. 2020. Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/whitepapers/latest/saas-tenant-isolation-strategies/hiding-the-details-of-pooled-isolation.html
  8. [8] ↩ Amazon Web Services, “Row-level security recommendations,” Using managed PostgreSQL for multi-tenant SaaS applications on AWS (AWS Prescriptive Guidance). Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/prescriptive-guidance/latest/saas-multitenant-managed-postgresql/rls.html
  9. [9] ↩ a b The PostgreSQL Global Development Group, “System Administration Functions,” PostgreSQL Documentation. Accessed: Oct. 23, 2026. [Online]. Available: https://www.postgresql.org/docs/current/functions-admin.html
  10. [10] ↩ PgBouncer project, “Features,” PgBouncer Documentation. Accessed: Oct. 23, 2026. [Online]. Available: https://www.pgbouncer.org/features.html
  11. [11] ↩ The PostgreSQL Global Development Group, “SET,” PostgreSQL Documentation. Accessed: Oct. 23, 2026. [Online]. Available: https://www.postgresql.org/docs/current/sql-set.html
  12. [12] ↩ The PostgreSQL Global Development Group, “Multicolumn Indexes,” PostgreSQL Documentation. Accessed: Oct. 23, 2026. [Online]. Available: https://www.postgresql.org/docs/current/indexes-multicolumn.html
  13. [13] ↩ a b c Amazon Web Services, “Pooled storage isolation strategies,” SaaS Tenant Isolation Strategies (AWS whitepaper), Aug. 2020. Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/whitepapers/latest/saas-tenant-isolation-strategies/pooled-storage-isolation-strategies.html
  14. [14] ↩ Amazon Web Services, “Run-time, policy-based isolation with IAM,” SaaS Tenant Isolation Strategies (AWS whitepaper), Aug. 2020. Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/whitepapers/latest/saas-tenant-isolation-strategies/run-time-policy-based-isolation-with-iam.html
  15. [15] ↩ Amazon Web Services, “Define permissions based on attributes with ABAC authorization,” AWS Identity and Access Management User Guide. Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction_attribute-based-access-control.html
  16. [16] ↩ Amazon Web Services, “Pass session tags in AWS STS,” AWS Identity and Access Management User Guide. Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_session-tags.html
  17. [17] ↩ a b c Amazon Web Services, “PostgreSQL pool model,” Using managed PostgreSQL for multi-tenant SaaS applications on AWS (AWS Prescriptive Guidance). Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/prescriptive-guidance/latest/saas-multitenant-managed-postgresql/pool.html
  18. [18] ↩ Amazon Web Services, “Usage plans and API keys for REST APIs in API Gateway,” Amazon API Gateway Developer Guide. Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-api-usage-plans.html
  19. [19] ↩ Amazon Web Services, “Tenant Tiers,” SaaS Lens, AWS Well-Architected Framework. Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/wellarchitected/latest/saas-lens/tenant-tiers.html
  20. [20] ↩ Amazon Web Services, “Tenant Activity and Consumption,” SaaS Lens, AWS Well-Architected Framework. Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/wellarchitected/latest/saas-lens/tenant-activity-and-consumption.html
  21. [21] ↩ Amazon Web Services, “Full stack silo and pool,” SaaS Architecture Fundamentals (AWS whitepaper). Accessed: Oct. 23, 2026. [Online]. Available: https://docs.aws.amazon.com/whitepapers/latest/saas-architecture-fundamentals/full-stack-silo-and-pool.html

Every source cited across the primers is collected on one page.