Databases
After today, you can read a simple data model, explain what an index is for, and say why adding a field can take two weeks.
Dip your toes 1 min
A database is where a product keeps the things it has to remember. In Slack, every message you scroll back to, every channel name, and every person lives in one. It sits on computers Slack rents or runs, not in the app on your phone. The data sits in tables, like spreadsheets with strict rules about what each column can hold. Each message stores its channel’s ID, a unique label, instead of the channel’s name, so renaming a channel changes one row. That strictness keeps data clean, but it makes change slow. Adding one new field to a table with billions of messages can take a couple of weeks, and old messages start out blank.
The full lesson 10 min
Why this exists
Scroll up in a busy Slack channel and you can read messages from years ago. Two people post at the same moment, and both messages appear, in the same order, for everyone in the channel. Rename the channel, and every old message still sits in it under the new name. The app on your phone does none of this: delete it and reinstall it, and everything is still there.
Most of that lives in a database on Slack’s servers. A database is built for three jobs that ordinary files do badly. It keeps data safe when a machine fails, and it finds one item among billions without reading them all. And it stays correct when thousands of requests write at the same moment. A spreadsheet can hold a list, but it can’t do those three things for millions of people at once.
There’s a fourth job that matters most to you. A database enforces a shape: every message belongs to a channel, every channel has a name, every timestamp is a real time. That shape decides what the product can show quickly, what it can’t show at all, and how hard each one is to change. Most of the “why is this so hard?” moments in a roadmap trace back to it.
What it is
Day 6 introduced the database as the part of the system that remembers. Most products keep their core data in a relational database, which stores everything in tables. A table is a set of rows that all describe one kind of thing. Each row is one item, like one message. Each column is one fact about it, like the text or the time it was sent.
Slack is a well-documented case. In a 2020 post, Slack’s engineers described MySQL as the storage for all of its data. MySQL is one of the most widely used relational databases. A simplified version of Slack’s data would have a table for channels, a table for messages, and a table for users, among many others.
The tables connect through IDs. Every channel gets a unique ID when it’s created. Every message stores the ID of its channel, not the channel’s name. Show a message, and the backend looks up its channel by that ID.
That one choice explains a lot of product behavior. Renaming a channel is instant, because the name lives in exactly one row. Engineers call this normalization: keeping each fact in one place. Copy the name into every message instead, and a rename means rewriting millions of rows, with a window where some messages show the old name.
The rules for all of this live in the schema, the blueprint of the database. It lists the tables, the columns in each, the type of value each column holds, and which columns point to other tables. The schema is strict on purpose: a message can’t be saved without a channel ID. A timestamp column won’t accept the word “yesterday” in place of a time. Those rules catch bad data at the door, before it can spread through the product.
The schema is also a promise to every piece of code that reads the table. A messages table might be read by the code that serves the apps, by search, by notifications, and by analysts’ reports. Change what a column is called or what it means, and every one of those readers has to change with it. That’s the real reason schemas move slowly. The database could make the change in seconds, but the code around it can’t.
The backend reads data by sending a query. A typical one asks for every message in channel C2. Without help, the database answers by reading every message it has and keeping the ones that match. That’s fine for a hundred rows. It’s hopeless for billions.
An index fixes that. An index is a sorted lookup the database keeps alongside a table, like the index at the back of a book. An index on channel_id lists every channel ID in order, each with the rows that belong to it. The database looks up C2, then jumps straight to those rows. Most tables have several indexes, one for each common way the product looks things up.
Indexes aren’t free. Every new message has to be added to every index on the messages table, so each index makes writing slower and takes storage. Teams add indexes for the questions the product asks often, and skip the rest. A missing index often shows up as a page that was fast with test data and slow in production, where the table is far bigger. That’s why a new way to sort or filter a list can turn into backend work.
Not every database uses tables. Some store whole documents, and some store simple pairs of keys and values. Most products still keep their core records in tables. There’s more here about choosing between kinds of databases; you don’t need it yet.
How it shows up in your work
Every piece of information in your mockups comes from a column somewhere, and one screen often needs several tables. Stitching rows from different tables together in one query is called a join. Your Slack channel view is a join: the channel’s name, each message’s text, and each author’s name and photo all live in different tables. When a design puts a new fact next to existing ones, ask whether it lives in the same table or needs another join.
Sorting and filtering come next. “Sort by most reactions” sounds like a frontend control. If there’s no index that fits, and no stored count of reactions per message, the database has to count them on every request. Engineers might need a new index, a new column, or both. Bringing the idea up in a design review, before the ticket is written, turns a surprise into a plan.
Names are the third. The schema’s names tend to outlive the product’s names, because renaming a table is risky. Slack’s product says “workspace,” but its developer docs still describe a workspace as a team, in method names like team.info. When your team renames a concept in the interface, ask what it’s called in the schema. Engineers will keep translating between the two names for years, and so will you.
The one thing people get wrong
How hard can one new field be? On a screen, it’s one more label and one more value. In the database, it’s a change to the schema of a table that might hold billions of rows and serve live traffic every second. That change happens through a migration: a script, kept in the repository, that changes the schema in a recorded way. It runs in order in every environment, from a laptop to staging to production.
Then there are the rows that already exist. A new column starts empty for every old message. Filling it in for those rows is called a backfill, and on a big table it’s the slowest part. It has to run in small batches, so the database keeps answering users while it works. Meanwhile, the code has to cope with both shapes: new rows that have the field, and old rows that don’t yet.
Size changes the risk, too. On a big table, some schema changes make the database rebuild the whole table. Some databases block new writes to a table while that happens. For Slack, a blocked messages table would mean nobody can send a message. So teams use careful tools instead, like gh-ost, which GitHub built and shared to change MySQL tables by working on a copy in the background.
That’s where the two weeks go. Each step is short on its own, but they have to happen in order, and some wait on others. Undoing a migration is harder than undoing a code change, because data written in the new shape doesn’t un-write itself. Bigger moves take longer still: Slack moved its data onto Vitess, a system that spreads MySQL across many machines. That move began in 2017 and was close to done three years later.
This changes what you design. During the backfill, some rows have the field and some don’t, so the screen needs a blank or fallback state. Sometimes the best answer is to skip the backfill: show the new field only on messages sent after launch. That call belongs in the design conversation, not only the engineering one. Ask early whether old rows can be filled in at all, because sometimes the information was never recorded.
Ask an engineer this
“Which table in our database is the hardest to change, and what makes it hard?”
Ask before you plan a feature that adds or changes stored data, not after the designs are approved. Most engineers can name one right away, along with the reason: it’s huge, many services read it, or its old rows are messy. Follow up on whether migrations on it need special care, like running at quiet hours or in small batches. It also helps to know how long the last change to that table took from start to finish. By then you’ll know which parts of your product are cheap to change and which ones deserve a conversation first.
Terms from today
- Table
- A set of rows in a database that all describe one kind of thing, like messages or channels, and share the same columns.
- Schema
- The blueprint of a database, listing its tables, their columns, the type of value each column holds, and how tables point to each other.
- Index
- A sorted lookup kept alongside a table that lets the database find matching rows without reading every row.
- Migration
- A script, kept in the repository, that changes a database’s schema in a recorded way and runs in order in every environment.
- Backfill
- Filling in a new or changed column for the rows that already existed before the change.