EngineeringSeries · Part 6 of 6

How one database keeps every shop's data apart

Why every shop gets its own schema in one database, the options we weighed, and how many shops one database should hold

Every shop on GrowMagik keeps its data in its own schema, a separate set of tables inside one shared PostgreSQL database. This post explains why we chose that over shared tables or a database for each shop, and how many shops we will put in one database before we add another.

In our first post we made a promise to shop owners. Many shops share one platform, but no shop ever sees another shop's customers. In the last post we saw how one codebase serves jewellers and travel agencies. This time we look at where their data lives, and how we keep that promise.

Two jewellers on the same street

Picture two jewellers on the same street in Thrissur. Both run on GrowMagik. Mary Aunty saves a little every month with both of them, because one makes the better chains and the other gives the better bonus.

Neither shop should ever see the other's customers, plans or payments. Mary Aunty gives both shops the same phone number, and she is still two separate customers, one in each shop's records. Each shop sees only what she does with that shop.

At the same time, most of our shops are small. A shop in a small town cannot pay for a database server of its own, and running one for every shop would make GrowMagik too costly for the very shops it is built for.

So the real question was how to keep shops apart without giving each one its own database. Engineers call each customer of a product like ours a tenant, and this question is called multi-tenancy.

The options we weighed

There are three common ways to keep many tenants' data apart in one product. They are usually called rows, schemas and databases.

First, one word. A schema is a named set of tables inside a database. Two schemas can each have a table called customers, and the two never mix.

Way What it is good at The risk we take on
Shared tables, with every row marked with its shop The cheapest to run, and the number of tables never grows Every query must remember the shop. One forgotten filter can show one shop's customers to another
A schema for each shop, inside one database A query that forgets the shop still finds only that shop's tables. It is still one database to run, back up and pay for Everything per shop multiplies. A change runs once for every shop, and the database's list of tables grows with each one
A database for each shop The strongest separation, and a shop can be restored or moved on its own Hundreds of databases to run, upgrade and pay for, which is far too costly for a small shop

We chose the middle one. Every shop gets its own schema, and every schema lives in one database. The rest of this post is about what that gives us, what it costs, and where it stops working.

One database, a schema for every shop. A jeweller and a travel agency each have their own schema with the same tables in it, such as customers, plans, payments and coupons. Beside them sits one small shared schema with the list of shops, the trades and our default settings. A query asks for plans and gets the plans of the shop it belongs to, and no other

What we chose, and when we would change it

1. A schema for every shop, in one database

When a shop joins, we create a schema for it and build its tables. There are about 230 of them, covering customers, savings plans, money books, payments, coupons, campaigns, visits and more. Every shop gets the same tables, built by the same scripts.

Think of one building with a locked room for every shop. The rooms are the same size and have the same cupboards, and each one holds only its own shop's papers. We look after one building, take one backup and pay one bill.

A schema also moves as one piece, with its name and its own record of changes. So if a large shop one day needs a database of its own, we can copy its schema across without changing anything inside it. We have planned that move, and we will build it when a shop needs it.

We change it when a shop needs something only a database of its own can give, such as a restore of its data alone or a server sized for it. That one shop moves, and every other shop stays where it is.

2. The code never has to remember the shop

Our code writes the same query for every shop. When a piece of work starts, whether it is a customer opening the app or a nightly job, it is tied to one shop. The database then looks for tables in that shop's schema.

So the code that lists a shop's savings plans is exactly the same for every shop. Even on a day when someone forgets to filter by shop, the query can only find that shop's tables.

This is the main reason we chose schemas over shared tables. With shared tables, keeping shops apart depends on every query, written by every engineer, remembering the shop every time. With a schema for each shop, the shop is decided once, at the start of the work, and every query after that inherits it.

We change it when a feature truly needs to look across shops, such as a report for our own team. Then we build a summary for that one purpose. We never write a query that joins two shops' schemas.

3. A small shared space for what every shop needs

A few things belong to everyone. The list of shops, the trades from our last post, the plans a shop can buy from us and our default settings sit in one shared schema beside the shops' own. Every shop reads it, and only our team changes it.

We keep this shared space small on purpose. A shop's customers, plans, payments and coupons live in that shop's own schema.

We change it when we put shops in a second database. The shared space then has to sit where every database can reach it, and that is the biggest part of that work.

4. One change reaches every shop

Every change to a table is a small script, such as "add a column for the branch". At each release, before the new version of our software takes a single request, it runs the new scripts for every shop's schema, in the same order. If one shop's tables cannot be brought up to date, the new version does not start and the old one keeps serving. Each schema keeps its own record of the scripts it has run.

A new shop's schema is built by the same scripts. Before we went live, the system had collected 784 small scripts. We folded them into one starting script for each part of the system and checked, table by table, that the result was exactly the same. A new shop's tables now take about 10 seconds to build instead of 20.

Once a script has gone out, we never edit it again. A fix is always a new script, because every shop's record says the old one already ran.

One change, every shop. A release carries a new script, such as adding a column for the branch. Before the new version serves anyone, the script runs in every shop's schema, and each schema records that it ran. If one shop fails, the new version does not start and the old one keeps serving. A new shop's schema is built from the same scripts in about 10 seconds

We change it when bringing every shop up to date makes a release too slow. Then the scripts run once, as a separate step before the new version starts, instead of inside it.

5. Work outside the database carries the shop too

Not all work starts with a person tapping a screen. A nightly job that sends reminders does not run once for everyone. It visits the shops one at a time, does one shop's work, and moves on, so a problem in one shop never stops the next.

The same holds for everything else. A message from one part of the system to another carries the shop it belongs to, and the part that receives it opens that shop's schema before it does anything. Uploaded files and cached values are filed under their shop as well.

We change it when visiting every shop every night costs more than the work itself. With thousands of shops, a job should go only to the shops that have something due.

The take-away

A schema per shop keeps shops apart even when a query forgets to, and its real limit is the size of one database, so decide early how many shops you will put in each.

Coming up next

On Tuesday, 8 December, we follow one request from a shop's web address or app all the way to that shop's own schema.

How do you keep your customers' data apart? We'd love to hear which of the three you chose, and why.

Discuss this post on LinkedIn →

Written by

Linjith Kunnon

Co-Founder and Chief Architect, QuickBee

Follow along

New posts appear here first. Follow QuickBee to see the rest of what we are building.