lesson
Databases & Caching
SQL vs NoSQL, indexing, replication, Redis caching patterns.
Databases & Caching
SQL vs NoSQL — When to Use Each
| SQL (Postgres, MySQL) | NoSQL (MongoDB, DynamoDB) | |
|---|---|---|
| Schema | Fixed, relational | Flexible, document/key-value |
| Consistency | Strong (ACID) | Eventually consistent (tunable) |
| Joins | Native | Manual (application-level) |
| Scaling | Vertical (read replicas help) | Horizontal (sharding built-in) |
| Best for | Complex queries, transactions, reporting | High throughput, flexible schema, key-value access |
Caching with Redis
Cache-Aside Pattern (most common)
1. Check cache → hit? Return cached value.
Miss? Query database, store result in cache, return.
On write: invalidate cache key. Cache-Through Pattern
Application → Cache → Database (cache handles all reads/writes)Key Decisions
Solutions: cache warming, request coalescing (single-flight), stale-while-revalidate.
Indexing
Golden rule: index columns that appear in WHERE, JOIN, ORDER BY. Don't over-index (slows writes).
Sign in to use the AI study buddy on this lesson.