You are building a B2B SaaS backend used by many companies. How would you design multi-tenancy and make sure one tenant can never see another tenant data?
What the interviewer wants to hear
- Models: shared tables with tenant_id, schema per tenant, database per tenant
- Trade-offs in cost, isolation, migrations and noisy neighbours
- Enforce tenant scoping centrally, e.g. row-level security or a data access layer
- Tenant in auth token, never trusted from request body
- Tenant-aware caching, logging, testing and backups
Model answer
There are three main models. Shared tables with a tenant_id column is cheapest and simplest to migrate, but a missing WHERE clause can leak data, and big tenants can be noisy neighbours. Schema per tenant gives more isolation but migrations across thousands of schemas are slow. Database per tenant gives the strongest isolation and easy per-tenant backups, which enterprise or compliance customers may require, but costs more to run. Many products use shared tables by default and dedicated databases for large customers. For safety, I would never rely on each developer remembering the filter. The tenant id comes from the verified auth token, not the request body, and is enforced centrally, for example with PostgreSQL row-level security using a session variable, or a repository layer that always adds it. Cache keys, file storage paths and search indexes must include the tenant, and I would add automated tests that try cross-tenant access.
Can you answer it under time pressure?
Take a timed hard round and get scored on every answer.
More hard Backend Developer questions
- Design a URL shortener like bit.ly that handles 100 million new links per month and many more redirects. Cover IDs, storage, caching and scale.
- A product manager says each payment event must be processed exactly once. How do you achieve that with real message queues and networks?
- Your single PostgreSQL database is reaching its limits. How would you scale reads and then writes, and what problems does each step bring?
- In a microservices system, placing an order touches the Order, Payment and Inventory services, each with its own database. How do you keep data consistent?
- Design a notification service that sends email, SMS and push messages for many products, up to tens of millions of messages a day.
- Your company wants to split a large monolith into microservices. How would you approach it, and when would you argue against it?
- Design the backend for a real-time chat app with one-to-one and group chats, online status and message history.
- One slow downstream dependency caused your whole platform to go down. How would you handle the incident, and what would you change afterwards?
- How would you design a ledger for a wallet or payments product so balances are always correct?