You are building a multi-tenant B2B SaaS app. How would you design data isolation between tenants, and what are the trade-offs?
What the interviewer wants to hear
- Options: shared tables with tenant_id, schema per tenant, database per tenant
- Enforce tenant filtering centrally, for example with Postgres row level security
- Tenant id comes from the authenticated session, never from user input
- Trade-offs in cost, isolation, migrations and noisy neighbours
- Tests and audits that prove cross-tenant access is impossible
Model answer
There are three common models. Shared tables with a tenant_id column are cheapest and easiest to migrate, but one missing WHERE clause can leak data. Schema per tenant gives stronger separation but makes migrations slow with thousands of tenants. Database per tenant gives the best isolation and per-customer backups, and suits large enterprise customers, but costs more and needs automation. For most products I start with shared tables and make isolation hard to get wrong: the tenant id comes from the authenticated session, never from the request body, and I enforce it centrally with Postgres row level security or a data access layer that always adds the tenant filter. Every tenant-owned table has tenant_id in its indexes and unique constraints. I add rate limits per tenant against noisy neighbours, automated tests that try cross-tenant access, and a path to move a big customer to its own database later.
Can you answer it under time pressure?
Take a timed hard round and get scored on every answer.
More hard Full Stack Developer questions
- Your app works well for 1,000 users. Traffic is expected to grow to 1 million users. How would you prepare the system, and in what order?
- An order flow touches your database, a payment provider and an email service. How do you keep the system consistent when any step can fail?
- You need to split a large users table column "name" into "first_name" and "last_name" on a table with 50 million rows, with no downtime. Walk through your plan.
- For a large e-commerce site in Next.js, how would you choose the rendering and caching strategy for the home page, product pages, cart and account pages?
- Design an e-commerce checkout end to end: cart, payment with a provider like Stripe, inventory and order confirmation. What can go wrong and how do you make it reliable?
- Your startup’s Next.js monolith is growing and some engineers want to split it into microservices. How do you decide, and what would you do?
- Ten minutes after a deploy, 500 errors jump from 0.1% to 8% on the main API. Walk through how you handle the incident.
- Customers need to import CSV files with up to two million rows into your app. Design the feature end to end.
- You are opening your product’s API to outside partners. How do you design authentication, authorization, rate limiting and change management for it?