Part 6 of 8 · 2 min read
Schema and change
How strict structure helps and hurts as the product changes.
Think of a form with fixed boxes against a blank sheet. The form makes every answer comparable. The blank sheet lets any answer in.
Relational stores use schema-on-write: the database checks every write against the declared columns. Many document and wide-column stores use schema-on-read: any shape is accepted and the reading code copes (module 1).
The effects over time:
| Strict schema | Flexible schema | |
|---|---|---|
| Bad data | Refused at the door | Gets in, found later by a failing reader |
| Adding a field | Migration step, planned | Write it, readers handle both shapes |
| Documentation | The schema is the document | Hidden in the code that reads |
| Many teams share the data | Safe contract | Risky, each reads in their own way |
| Early prototype with a changing shape | Slower | Faster |
A worked example. A field phone appears in a user record. At first it is a string. A year later the product wants a country code stored apart.
- Strict schema: add a column
country_code, fill it from the old data in a migration, deploy code that uses both. On a table with 20 million rows the fill takes about 20 million ÷ 5,000 rows per second = 4,000 seconds, a little over an hour, if done in small steps without locking the table (rows per second made up). - Flexible schema: new code writes
country_code. Reading code uses it if present, otherwise it parsesphone. A background job tidies old documents over time.
Both work. The flexible one is faster to start. The strict one leaves less hidden complexity, since after the migration only one shape exists. The book notes that schemaless is a misleading word: the code that reads still assumes a structure, and the structure is simply not enforced by the database.
A good habit with a flexible schema is to keep a version number in each document, so reading code knows which shape it has.
{ "schema_version": 2, "name": "Asha", "phone": "9876543210", "country_code": "+91" }
Common mistake
Treating a flexible schema as no schema. Five teams reading the same documents in five ways will, in time, produce five bugs. Agree on the shape, write it down and check it in code.
Check yourself:
- What does schema-on-write catch that schema-on-read does not?
- Why is a version number in each document useful?