Let’s Get Certified
Module 04 / 21 · Data foundations

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 modelTable schemas, comments and tags in Unity Catalog
Logical constraintsNOT NULL, CHECK, pipeline expectations
Redundant versions to compare and restoreDelta time travel and RESTORE TABLE (Delta Lake)
Human reviewData 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 privilegeGrant 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 KEY and FOREIGN KEY constraints 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.

NormalizedDenormalized
Little or no redundant dataRedundant data is common
Integrity maintained by designIntegrity needs extra work
Many tables, more joinsFewer, wider tables, faster reads
Storage-efficientUses more storage
Typical of OLTP and silver layersTypical of NoSQL and gold layers

ACID transactions

PropertyGuaranteeIn Delta Lake
AtomicityA write fully succeeds or fully failsA commit to the transaction log is all or nothing. A failed job leaves no partial files visible.
ConsistencyData moves from one valid state to anotherSchema enforcement and constraints reject invalid writes
IsolationConcurrent transactions do not see each other's partial workOptimistic concurrency control. Readers see a snapshot. Conflicting writers retry or fail.
DurabilityCommitted data survives failuresData 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

SynchronousAsynchronous
TimingGuaranteed; data is readable only once the transfer completesNo guarantee; readable any time
ConsistencyGuaranteed (strong)May return an older version or an empty placeholder
SpeedSlowerFaster
Typical useStandby backup database that must not lose any dataRead 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

Constraints on Databricks in the Databricks documentation

Knowledge check

Data integrity quiz

Answer 6 questions. Check each answer for an explanation.