Data integrity
Trace a failed write, distinguish enforced constraints from informational keys, and design changes that can be replayed safely.
Data corruption and how integrity is protected
Data corruption is data that is not in its intended state, causing data loss or misinformation. It comes from hardware failures, human input errors, malicious changes, or code with unforeseen side effects. Validation alone does not guarantee integrity; integrity covers the whole life cycle.
| Technique (video) | Databricks mechanism |
|---|---|
| Well-defined, documented data model | Table schemas, comments and tags in Unity Catalog |
| Logical constraints | NOT NULL, CHECK, pipeline expectations |
| Redundant versions to compare and restore | Delta time travel and RESTORE TABLE (Delta Lake) |
| Human review | Data quality monitoring, dashboards, alerts |
| Hash functions to detect tampering (for example an MD5 checksum on a download) | md5(), sha2() SQL functions to fingerprint rows or files |
| Principle of least privilege | Grant only the Unity Catalog privileges a role needs (Access control) |
Kinds of integrity
- Entity integrity: each row is uniquely identifiable (primary key). In Databricks,
PRIMARY KEYandFOREIGN KEYconstraints on Unity Catalog tables are informational only: they are not enforced, but they help the optimizer and BI tools. - Referential integrity: foreign keys point to rows that exist.
- Domain integrity: values are valid for their column (type, range, allowed set).
Normalized vs denormalized
How a model is designed also affects integrity. A normalized model protects it by design; a denormalized model trades that for read speed.
| Normalized | Denormalized |
|---|---|
| Little or no redundant data | Redundant data is common |
| Integrity maintained by design | Integrity needs extra work |
| Many tables, more joins | Fewer, wider tables, faster reads |
| Storage-efficient | Uses more storage |
| Typical of OLTP and silver layers | Typical of NoSQL and gold layers |
ACID transactions
| Property | Guarantee | In Delta Lake |
|---|---|---|
| Atomicity | A write fully succeeds or fully fails | A commit to the transaction log is all or nothing. A failed job leaves no partial files visible. |
| Consistency | Data moves from one valid state to another | Schema enforcement and constraints reject invalid writes |
| Isolation | Concurrent transactions do not see each other's partial work | Optimistic concurrency control. Readers see a snapshot. Conflicting writers retry or fail. |
| Durability | Committed data survives failures | Data and log live in durable cloud object storage |
Consistency models
- Strongly consistent: every read returns the latest committed data, possibly after waiting a defined time. Never stale.
- Eventually consistent: a read may briefly return stale data, in exchange for faster responses. Wait and it converges.
Delta Lake gives readers a consistent snapshot: a query sees one committed table version, never a half-written one. Cloud object stores used by Databricks (S3 since December 2020, ADLS, GCS) provide strong read-after-write consistency.
Synchronous vs asynchronous replication
| Synchronous | Asynchronous | |
|---|---|---|
| Timing | Guaranteed; data is readable only once the transfer completes | No guarantee; readable any time |
| Consistency | Guaranteed (strong) | May return an older version or an empty placeholder |
| Speed | Slower | Faster |
| Typical use | Standby backup database that must not lose any data | Read replica for heavy analytical reports |
Enforced constraints in Delta
-- NOT NULL is enforced
ALTER TABLE silver.orders ALTER COLUMN order_id SET NOT NULL;
-- CHECK constraints are enforced; a violating write fails as a whole
ALTER TABLE silver.orders ADD CONSTRAINT valid_qty CHECK (quantity > 0);
ALTER TABLE silver.orders DROP CONSTRAINT valid_qty;
Adding a CHECK constraint fails if existing rows already violate it.
Schema enforcement and schema evolution
Schema enforcement (schema on write): Delta rejects appends whose columns or types do not match the table. Schema evolution lets you allow additive changes deliberately:
df.write.mode("append").option("mergeSchema", "true").saveAsTable("bronze.events")
Overwriting a table's schema entirely uses .option("overwriteSchema", "true") with mode("overwrite").
Data quality dimensions
Accuracy, completeness (no missing values), consistency (same value everywhere), timeliness (fresh enough), validity (conforms to rules), uniqueness (no duplicates). Tools on Databricks: Delta constraints, pipeline expectations (Lakeflow Declarative Pipelines), deduplication (Spark DataFrame API), and data quality monitoring on Unity Catalog tables.
Idempotency
An operation is idempotent if running it twice has the same effect as running it once. It matters because jobs get retried. COPY INTO and Auto Loader are idempotent (they track loaded files). A keyed MERGE that sets target fields to deterministic source values is idempotent. A merge that increments a target counter each time is replayed twice after a retry. A plain INSERT INTO is not: rerunning duplicates rows.
Predict the effect of a retry
Replay a file that contains a single payment. Compare an unconditional append, a key-based merge that sets the amount, and a merge that adds the amount to the existing balance.
Check your reasoning
The append duplicates the row. Reassigning the same amount by key preserves the result. Adding to the balance applies the payment twice.
Product reference
Data integrity quiz
Answer 6 questions. Check each answer for an explanation.