BBackend CourseLeroy Labs
0/0
Search the course

Normalization & deliberate denormalization

1NF, 2NF, 3NF and BCNF explained with your schemas, and the times you rightly broke the rules (order snapshots, cached occupancy, GIN tag arrays).

● intermediateSpring-Boot-StoreHostel-Web-AppSperoTek (in dev)

The concept

Normalization removes redundancy so that each fact is stored in exactly one place, which prevents update anomalies (changing the fact in one place but not another).

Normal form Rule Violation example
1NF Atomic values, no repeating groups phones = "024..., 055..." in one column
2NF 1NF, and every non-key column depends on the whole key In order_items(order_id, product_id, product_name), product_name depends only on product_id
3NF 2NF, and no non-key column depends on another non-key column rooms(hostel_id, hostel_name): hostel_name depends on hostel_id
BCNF Every determinant is a candidate key Rare edge cases of 3NF

The memory aid: every non-key column depends on “the key, the whole key, and nothing but the key”.

Denormalization: breaking the rules on purpose

You sometimes duplicate data on purpose, for history, speed or simplicity. The rule is to know you’re doing it and to keep the copy consistent.

In your projects

Spring-Boot-Storedb/script/orders-schema.sql

order_items stores unit_price and total_price at the time of purchase, even though products.price exists. That looks like a 2NF violation, but it is correct: an order is a historical record. If a product’s price changes tomorrow, yesterday’s invoice must not change. This is a snapshot, not redundancy.

Hostel-Web-Appinitial-schema.sql · Room.current_occupancy

rooms.current_occupancy is a cached derived value, since it could be computed by counting bookings. It is protected by CHECK (current_occupancy <= capacity), @Version optimistic locking and pessimistic row locks. Hostel also computes reserved beds per period with a CTE query (countReservedBeds). Keeping two sources of truth needs discipline.

Hostel-Web-Appindex.sql (GIN on student_preferences.tags)

tags is a Postgres array column, which strictly violates 1NF. A GIN index makes “find preferences containing tag X” fast. The normalized alternative is a student_preference_tags(pref_id, tag) join table. Arrays are fine for simple, bounded sets that you query as a whole.

SperoTek (in dev)ledger: journal_entries / journal_lines

The ledger is highly normalized: account balances are never stored. They are derived by summing journal lines (JdbcBalanceQuery). In accounting, the journal is the source of truth, and a stored balance that drifts is a serious bug. Reporting can add cached snapshots later if needed.

When to denormalize

Reason Technique
Historical truth Snapshot columns (order prices, invoice addresses)
Read performance Cached counters, materialized views, read models (CQRS)
Avoiding joins on hot paths Duplicated columns kept in sync by triggers or events
Flexible attributes JSONB columns (careful: the schema lives in code)
  • Materialized views. CREATE MATERIALIZED VIEW ...; REFRESH MATERIALIZED VIEW CONCURRENTLY gives precomputed reports.
  • JSONB. Postgres’s indexed binary JSON. Hostel includes hypersistence-utils for mapping JSON types.
  • OLTP vs OLAP. Transactional systems normalize; analytics warehouses denormalize into star schemas.
  • Functional dependency. The formal idea underneath every normal form (A → B).

Check yourself

1. Store copies unit_price into order_items. This is…
2. Why does SperoTek derive balances rather than storing them?