Skip to content

Sql_nosql

Choosing the Right Database for Your Application

Section titled “Choosing the Right Database for Your Application”

The choice between SQL and NoSQL databases is fundamental to system design. Each has distinct characteristics, trade-offs, and ideal use cases.

Database Landscape
=================
+------------------------+ +------------------------+
| SQL Databases | | NoSQL Databases |
+------------------------+ +------------------------+
| PostgreSQL | | |
| MySQL | | Document: MongoDB |
| Oracle | | Key-Value: Redis |
| SQL Server | | Column: Cassandra |
| SQLite | | Graph: Neo4j |
+------------------------+ +------------------------+

SQL databases are relational databases that use Structured Query Language (SQL) for defining and manipulating data. They organize data into tables with rows and columns.

SQL Database Structure
=====================
Users Table
+--------+-----------+------------------+------------+
| id | name | email | created |
+--------+-----------+------------------+------------+
| 1 | John | john@email.com | 2024-01-15|
| 2 | Jane | jane@email.com | 2024-01-16|
+--------+-----------+------------------+------------+
Orders Table
+--------+--------+----------+-------+
| id | user_id| amount | status|
+--------+--------+----------+-------+
| 1 | 1 | 100.00 |shipped|
| 2 | 1 | 50.00 | pending|
+--------+--------+----------+-------+
Relationship: Orders.user_id -> Users.id
CharacteristicDescription
SchemaPre-defined, structured schema
ACIDAtomicity, Consistency, Isolation, Durability
RelationshipsForeign keys, JOIN operations
Query LanguageStandard SQL
ScalingTypically vertical (scale up)
Use CaseWhy SQL Works
Financial transactionsACID guarantees data integrity
Complex queriesPowerful JOINs and aggregations
Structured dataFixed schema works well
Reporting/AnalyticsPowerful query capabilities
Data integrity requiredForeign keys, constraints

NoSQL databases are non-relational databases designed for flexible schemas, horizontal scaling, and specific data models beyond tabular storage.

NoSQL Database Types
===================
+----------------+ +----------------+ +----------------+
| Document | | Key-Value | | Column-Family|
+----------------+ +----------------+ +----------------+
| MongoDB | | Redis | | Cassandra |
| CouchDB | | DynamoDB | | HBase |
| | | Memcached | | Bigtable |
+----------------+ +----------------+ +----------------+
+----------------+ +----------------+
| Graph | | Time-Series|
+----------------+ +----------------+
| Neo4j | | InfluxDB |
| Amazon Neptune | | TimescaleDB |
| | | |
+----------------+ +----------------+
Document Database (MongoDB)
===========================
Collection: users
+--------------------------------------------------+
| { |
| "_id": "ObjectId(...)", |
| "name": "John", |
| "email": "john@email.com", |
| "address": { |
| "city": "NYC", |
| "country": "USA" |
| }, |
| "orders": [ |
| {"id": 1, "amount": 100}, |
| {"id": 2, "amount": 50} |
| ] |
| } |
+--------------------------------------------------+
Flexible schema: Each document can have different fields
Key-Value Store (Redis)
======================
SET user:1:name "John"
SET user:1:email "john@email.com"
SET user:1:session "abc123xyz"
GET user:1:name
-> "John"
Use Cases:
- Session storage
- Caching
- Leaderboards
- Real-time analytics
Column-Family Store (Cassandra)
==============================
Row Key: user_id
Column Families:
+------------------------+
| user_profile |
+------------------------+
| name: John |
| email: john@... |
| city: NYC |
+------------------------+
+------------------------+
| user_orders |
+------------------------+
| order_1: 100 |
| order_2: 50 |
| order_3: 200 |
+------------------------+
Optimized for:
- Write-heavy workloads
- Time-series data
- Big data applications
Graph Database (Neo4j)
=====================
Nodes: John, Jane, ProductA, ProductB
Relationships:
(John)-[:KNOWS]->(Jane)
(John)-[:BOUGHT]->(ProductA)
(Jane)-[:BOUGHT]->(ProductB)
Cypher Query:
MATCH (john {name: 'John'})-[:KNOWS]->(friend)
RETURN friend.name
Use Cases:
- Social networks
- Recommendation engines
- Fraud detection
- Network analysis
CharacteristicDescription
SchemaFlexible or schema-less
BASEBasically Available, Soft state, Eventual consistency
DenormalizedData embedded in documents
APIsDocument/JSON, key-value APIs
ScalingTypically horizontal (scale out)
Use CaseWhy NoSQL Works
Rapid developmentFlexible schema
Massive scaleHorizontal scaling
Unstructured dataJSON/document support
High write throughputCassandra, DynamoDB
CachingRedis, Memcached
Graph relationshipsNeo4j

FeatureSQLNoSQL
Data ModelTables (rows/columns)Documents, key-value, graphs
SchemaFixedFlexible
Query LanguageSQL (standardized)API/SDK based
ACIDFull supportLimited
ScalabilityVerticalHorizontal
RelationshipsForeign keys, JOINsEmbed or link
TransactionsMulti-statement ACIDLimited
IndexingRichVaries
Learning CurveModerateVaries
Visual Comparison
=================
SQL:
+------------------------------------------+
| Table -> Row -> Columns |
| Strict Schema |
| Complex Queries via JOIN |
+------------------------------------------+
NoSQL (Document):
+------------------------------------------+
| Collection -> Document -> Fields |
| Flexible Schema |
| Data embedded in documents |
+------------------------------------------+
NoSQL (Key-Value):
+------------------------------------------+
| Key -> Value |
| Simplest model |
| Very fast lookups |
+------------------------------------------+

Decision Tree
=============
Start
|
v
Need ACID transactions?
|
+--------+--------+
| Yes | No
v v
SQL Is data highly structured?
| |
| +------+------+
| | Yes | No
| v v
| SQL Is scale critical?
| | |
| | +------+------+
| | | Yes | No
| | v v
| | NoSQL Need complex
| | | queries?
| | | +------+------+
| | | | Yes | No
| | | v v
| | | SQL Document DB
| | | |
v v v v
RequirementRecommended Database
Financial transactionsPostgreSQL, MySQL
Analytics/ReportingPostgreSQL, ClickHouse
User profilesPostgreSQL, MongoDB
Session storageRedis
Real-time inventoryCassandra, DynamoDB
Product catalogMongoDB, Elasticsearch
Chat messagesCassandra, DynamoDB
Social graphNeo4j
Time-series dataInfluxDB, TimescaleDB
Caching layerRedis, Memcached

Modern Application Architecture
==============================
+---------------------------------------------------+
| Application |
+---------------------------------------------------+
| | | |
v v v v
+-----------+ +-------+ +------+ +------+
| PostgreSQL| |Redis | |Elastic| |S3 |
| User | |Session| |search | |Files |
| Orders | |Cache | | | | |
| Payments | | | | | | |
+----------+ +------+ +------+ +------+
Each data type uses the best-fit database
Database Selection by Domain
===========================
+-------------------+--------------------------------+
| Domain | Database |
+-------------------+--------------------------------+
| User accounts | PostgreSQL (ACID required) |
| Product catalog | MongoDB (flexible schema) |
| Shopping cart | Redis (fast, session-like) |
| Orders | PostgreSQL (transactional) |
| Product search | Elasticsearch (full-text) |
| Inventory | Cassandra (high write) |
| Recommendations | Neo4j (graph relationships) |
| Logs/Analytics | ClickHouse (OLAP) |
+-------------------+--------------------------------+

ConsiderationDetails
Schema DesignEmbed vs reference decisions
Data ModelingDifferent approach to relationships
QueriesNo JOINs, denormalize data
TransactionsMay need application-level handling
MigrationsPlan for data transformation
ConsiderationDetails
SchemaDefine upfront
RelationshipsUse foreign keys
QueriesUse JOINs
ACIDCan now use transactions

Key points for SQL vs NoSQL:

  1. SQL for structure and transactions - Use when data integrity is critical
  2. NoSQL for scale and flexibility - Use when scaling is priority
  3. Consider polyglot persistence - Use multiple databases
  4. Match database to data characteristics - Different data, different DBs
  5. Plan schema design - SQL needs upfront, NoSQL can evolve
  6. Think about operations - Different operational complexity

Next: Chapter 8: Database Replication