Back to all terms
Database
Databaseintermediate

SQL vs NoSQL Selection

Choosing between relational and non-relational databases based on data shape, query patterns, and consistency requirements.

Also known as: relational vs non-relational, RDBMS vs document store, SQL vs MongoDB

Description

SQL databases (PostgreSQL, MySQL, SQL Server) enforce a rigid schema with tables, rows, and relationships, providing strong ACID guarantees and powerful JOIN operations. They excel when data is highly relational, when you need complex ad-hoc queries, or when transactional integrity is non-negotiable -- think financial systems, inventory management, or user account data.

NoSQL databases span multiple paradigms: document stores (MongoDB, CouchDB), key-value stores (Redis, DynamoDB), wide-column stores (Cassandra, HBase), and graph databases (Neo4j). They trade schema rigidity and sometimes consistency for horizontal scalability, flexible data models, and optimized performance for specific access patterns. A document store is ideal when your data is naturally hierarchical and you read/write entire documents; a key-value store shines for caching and session management; a graph database handles deeply connected data like social networks.

The decision should never be ideological. Evaluate your read/write ratio, data relationships, consistency requirements (CP vs AP in CAP theorem), scaling projections, and team expertise. Many production systems use polyglot persistence -- a relational database for transactional core data alongside Redis for caching and Elasticsearch for search -- rather than forcing one engine to do everything.

Prompt Snippet

Evaluate data access patterns and consistency requirements before selecting a database engine. For transactional workloads with complex relationships, default to PostgreSQL with proper indexing. For high-throughput document storage with flexible schemas, consider MongoDB with WiredTiger. For caching and ephemeral session data, deploy Redis with RDB+AOF persistence. Document the CAP theorem trade-offs for each data store in your architecture decision record (ADR).

Tags

architecturedata-modelingscalabilitydecision-making