Designing a Multi-Tenant Database on Shared Hosting

MyStageBio runs on one shared MySQL database - not a database per customer, not flat JSON files per performer. Here's the schema, and the one rule that makes sharing a database safe.

The core tables

users holds the account itself: subdomain, email, password hash, plan, and the account lifecycle fields (account_status, demo and grace-period timestamps, Stripe customer and subscription IDs). resumes is one row per user - bio, headshot path, theme and layout choice. credits is many rows per user - role, production, company, director, year, with a sort order. A portfolio_images table exists in the schema already for a photo-gallery feature that isn't built yet, so adding it later won't need a migration. custom_domains, rate_limits, and feature_requests round it out.

Nothing exotic. The interesting part isn't the tables, it's what has to be true about every query that touches them.

The one rule

Every query in the admin panel is scoped to the logged-in user's own user_id, pulled from the session - never from anything a request could supply. The public-facing side works the same way in reverse: a visitor's page is looked up by subdomain, which has a UNIQUE constraint as a backstop, and nothing about what a visitor can control ever reaches that lookup. That's the entire tenant-isolation model. It's not a clever technique - it's one discipline point, applied everywhere, with no exceptions. The moment one query somewhere forgets it, that's a real vulnerability, not a bug that just looks wrong on screen.

A smaller lesson, in the same spirit

Every read of the credits table is shaped the same way: WHERE user_id = ? ORDER BY sort_order, including on the public page every visitor loads. A plain index on user_id alone still left MySQL doing a filesort on every single request, because the index didn't cover the ordering. Changing it to a composite index on (user_id, sort_order) fixed that - a small thing, but exactly the kind of small thing that matters more once a table is read on every public page load rather than occasionally from an admin screen.

Don't miss a post

Get new posts by email

An email whenever something new goes up on the blog. No spam, unsubscribe anytime.

Building something of your own?

If you need a custom site or web application built the same hands-on way, let's talk.

Get in touch