Multi-tenant CRM architecture: how we keep each estate agency's data apart

Tanvir Akhter Shakib6 min read

The short answer

Multi-tenant CRM architecture means many agencies share one application and one database while each can reach only its own rows. In Propily that separation is enforced in application code: the agency comes from the signed-in user's membership, any agency a request names is checked against it, and every query, tool call and search is filtered by it.

What multi-tenant CRM architecture means for agency data isolation

A multi-tenant CRM serves many agencies from one application and one database. The alternative is a database per agency, which isolates strongly and costs more to run and migrate. We use shared tables with an agency_id column on the data an agency owns. Separation then becomes a property of every query, and that is the whole risk: a missing filter is a leak.

There are two places to enforce the filter. The application can add it to every query, or Postgres can enforce it with row-level security policies keyed on a per-request setting. We scope in the application. The rest of this article covers how.

Where the agency comes from: the membership, not the request

The first rule is that a request is never trusted to say which agency it acts for. The access token proves who the user is. The agency comes from the user's membership row, resolved on the server by one shared function. Most routers depend on it, and permission checks use it: they take the agency from the signed-in user's membership instead of the body, path or query, then check the permission for that user in that agency.

People can hold seats at more than one agency, so resolution has to be deterministic. The token's agency claim is a preference among seats the user already holds, never a filter that widens access. After that comes the account's home agency, then the oldest seat. A support session opened from our admin console is held to the agency its grant names, and a user who also belongs to another agency cannot carry that session there. Suspended agencies are refused at the same point. Routes that do accept an agency id treat it as a claim and cross-check it against an active membership.

How queries stay scoped: an id in a request is a claim

Most cross-tenant bugs are not clever. They are a lookup by id alone: the handler fetches a record by primary key and nobody checks whose it is. So we treat any id in a request as a claim. Shared helpers look the record up under the caller's agency, and anything that is not theirs answers 404, exactly as an id that does not exist would. A 403 would confirm that another agency holds that record.

Lookups go through agency-scoped helpers, and each has a test with two agencies that checks for the 404. Inside an agency we narrow further by branch: admins see every branch, managers see their own branch plus leads not yet routed, and everyone else sees their own branch plus leads assigned to them.

async def get_agency_record(db, model, record_id, agency_id):
    result = await db.execute(
        select(model).where(model.id == record_id,
                            model.agency_id == agency_id)
    )
    return result.scalar_one_or_none()  # None: missing, or another agency's

async def require_agency_record(db, model, record_id, agency_id, label):
    row = await get_agency_record(db, model, record_id, agency_id)
    if row is None:
        raise HTTPException(404, f"{label} not found")
    return row

How AI tools, vector search and webhooks are scoped

The same rule applies where a model is involved, and it matters more there. The model never supplies an agency. The in-app assistant builds its tool context from the signed-in user, and every handler reads the agency from that context. The receptionist's tools run on an MCP server whose scope arrives as headers, and each request carries an HMAC that only our own processes can compute, so anyone sending the headers by hand is refused before a tool runs.

Vector search is the other shared resource. Property embeddings from all agencies sit in shared collections, so the agency id is stored on every embedding, indexed, and required by the property search functions, which raise if called without one. Inbound webhooks follow the same pattern. A WhatsApp message is mapped to an agency from the number that received it, and if none matches the message is refused rather than assigned to an agency by guesswork. Per-agency credentials, such as an agency's own Twilio token, are encrypted at rest and masked in API responses.

Where Postgres row-level security fits, and where it does not

Row-level security is the database-side way to enforce tenancy: a policy on each table, keyed on a setting the application makes for each request or transaction, so a query that forgot its filter still returns only the caller's rows. It is a second line of defence, and it is also more work: a policy on every tenant table, a setting on every connection (including background workers and webhooks), and care with the database roles that bypass it.

We scope in the application, with shared helpers, review and tests. Which approach fits depends on how many entry points reach the data and how much of the enforcement a team wants the database to carry.

How we test tenant isolation

A rule nobody tests decays. The pattern we use is a fixture with two agencies, A and B, and a user at A who is handed B's ids. Every endpoint that takes an id is called with them, and the test asserts a 404 and that nothing was written. Separate tests cover the branch rules above. They run offline against a fake table layer that executes the statements the code builds, so they are cheap enough to add with every endpoint.

    Where to go next

    If you are choosing an estate agency CRM, ask how it separates agencies and ask to see the test that proves it. Our real estate CRM page and the guide on choosing an estate agent CRM list the other questions worth putting to a vendor.

    Questions people ask

    What is multi-tenant architecture in a CRM?

    It is a design where many agencies, the tenants, use one application and one database while each sees only its own data. The separation is enforced by the software, usually with an agency identifier on every tenant record and a filter on every query.

    Do you need Postgres row-level security for multi-tenant isolation?

    No. Row-level security is one way to enforce the filter inside the database. You can instead scope every query in application code, as we do, but then a forgotten filter is not caught by the database, so tests and shared helpers have to do that job.

    How do you stop one agency reading another's records by guessing an id?

    Look every record up under the caller's agency and return 404 when it is not theirs. A 403 would confirm the record exists, which tells an attacker something. Test it with two agencies and an id from the wrong one.

    How does an AI assistant stay inside one agency's data?

    The agency comes from the signed-in user's session or from a signed internal scope, never from the model's arguments. Every tool reads it from that context, and vector search requires it as a filter.

    Who answers your phones tonight?Book a demo
    Multi-tenant CRM architecture: how we isolate agency data | Propily