Data integrity
Making sure stored data is accurate, complete and valid.
Integrity comes from constraints, validation, foreign keys and careful backfills. Bad data spreads into analytics, emails and decisions. It is much cheaper to prevent than to clean up later.
Why it matters for a product engineer
Integrity comes from constraints, validation, foreign keys and careful backfills. Bad data spreads: into analytics, emails, invoices and decisions. Once it is copied into other systems, nobody can find all of it. It is much cheaper to prevent than to clean up, and most prevention is a few lines in the schema.
How to do it
- Define the rules in the database:
NOT NULL, unique keys, foreign keys and check constraints. - Validate at the edge too, so users get a clear message, but do not rely on the edge alone.
- Store values in their real types: dates as dates, money as integers in the smallest unit, codes as text.
- Never let a tool guess types for you on import. Declare them.
- Run backfills in batches, check the counts before and after, and keep the old values until you are sure.
- Add checks that alert you when data breaks a rule that the schema cannot express.
Common mistakes
- Rules only in the application, so a script or a second service skips them.
- Free text for values that should come from a fixed list.
- Imports through spreadsheets that convert values silently.
- Deleting rows that other tables still point to.
Example
Spreadsheet software can change gene names on import: SEPT2 becomes “2-Sep” and MARCH1 becomes “1-Mar”. In 2016, researchers scanned the Excel files published with papers in 18 journals from 2005 to 2015. Of 3,597 papers with gene lists, 704 had such errors. The wrong names went out with the published papers.
Papers with gene lists in Excel files, 18 journals, 2005 to 2015
- 3,597
- Papers with gene lists
- 704
- With gene names turned into dates or numbers
- 19.6%
- Of those papers affected
The tool guessed a type, and nothing in the process checked the guess. A declared type at import would have stopped it.
MeasuredZiemann, Eren and El-Osta, Genome Biology, 2016
Sources: Evolutionary database design (Pramod Sadalage and Martin Fowler) ↗ · Gene name errors are widespread in the scientific literature (Ziemann, Eren and El-Osta, Genome Biology, 2016) ↗
Resources
1- articleGo deeper
Evolutionary database design ↗
Pramod Sadalage and Martin Fowler · 40 minHow to change a database schema in small, safe steps alongside the code that uses it.
Tools
1- ToolOpen source