Implementation Patterns for Production Systems
Data Modeling Patterns
In relational systems, normalize to eliminate duplication: store each fact once and use foreign keys to link tables. Add indexes for query patterns, and use materialized views (precomputed query results stored as tables) when read latency matters more than write complexity. In NoSQL, model around access patterns: choose partition keys to distribute load evenly and co-locate data read together. Pre-join related data into single documents or rows that match query shapes, accepting duplication for faster reads.
Consistency Patterns
For user-facing read-after-write, use session consistency: route requests to the replica that handled the most recent write. For critical invariants like counters or unique IDs, use conditional updates with optimistic concurrency control (read current version, modify, write only if version unchanged). If the version changed, retry. For globally unique IDs, use centralized allocators rather than per-region generators that risk collisions.
Multi-Region Architecture
For strong consistency across regions, use quorum replication: with 5 replicas across 3 regions, require 3 to acknowledge writes. Expect 50-150ms added write latency for cross-continent consensus. For eventual consistency with active-active writes, define conflict resolution rules upfront: typically last-write-wins using timestamps, with tie-breakers for simultaneous writes. Design UIs to tolerate temporary divergence between regions.