Database Architectures: Indexing Keys, MongoDB Design, Sharding, and Redis Caching
A production-grade playbook for selecting, designing, and scaling databases. Deep-dive into B-Tree indexes, NoSQL document modeling, cluster sharding, and cache eviction patterns.
Data management is the core bottleneck of distributed software systems. Building a system that scales requires a deep understanding of relational storage engines, NoSQL document schemas, scaling strategies, and high-performance memory caches.
1. SQL Query Indexing: How B-Trees Work
Relational databases like MySQL and PostgreSQL utilize B-Trees (specifically B+ Trees) to store indices. A B+ Tree organizes data in a balanced search tree where all data rows are referenced in leaf nodes.
┌───────────────┐
│ Root │
│ [ 50 ] │
└───────┬───────┘
│
┌────────────┴────────────┐
▼ ▼
┌─────────────┐ ┌─────────────┐
│ Internal 1 │ │ Internal 2 │
│ [ 20|35 ] │ │ [ 65|80 ] │
└──────┬──────┘ └──────┬──────┘
│ │
┌──────┴──────┬──────┐ ┌──────┴──────┬──────┐
▼ ▼ ▼ ▼ ▼ ▼
[10..19] [20..34] ... [50..64] [65..79] ...
When you execute a query using an indexed field, the complexity is $O(\log N)$ instead of an $O(N)$ full table scan.
[!TIP] Always inspect queries using the
EXPLAINkeyword to verify that your queries are hitting the indexes you created.
EXPLAIN SELECT id, title FROM posts WHERE category = 'devops';
2. MongoDB Document Design & Sharding
In MongoDB, documents are stored in BSON format. Unlike normalized relational tables, MongoDB documents allow embedding arrays and sub-objects, which matches the object models inside your application.
Designing for Scale
- One-to-Few Relationship: Embed the child data directly in the parent document (e.g. FAQ questions inside a blog post).
- One-to-Many Relationship: Store references to children in the parent document, or references to the parent inside child documents to prevent exceeding the 16MB document size limit.
Horizontal Scaling: Sharding
Sharding distributes collection data across a cluster of independent machines using a Shard Key. MongoDB uses the shard key to route operations to target nodes:
┌───────────────┐
│ Application │
└───────┬───────┘
│
┌───────▼───────┐
│ Mongos │ (Router)
└───────┬───────┘
┌─────────────┼─────────────┐
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Shard A │ │ Shard B │ │ Shard C │
│ [0..30] │ │[31..60] │ │ [61..+] │
└─────────┘ └─────────┘ └─────────┘
3. Redis In-Memory Caching & Eviction
Redis acts as a sub-millisecond memory store. A common architecture pattern is the Cache-Aside Pattern:
- Application queries Redis.
- If cache hit: Return data.
- If cache miss: Query database, update Redis, and return data.
import { createClient } from 'redis';
const client = createClient();
await client.connect();
async function getOrSetCache(key, dbQueryFn, ttl = 3600) {
const cachedData = await client.get(key);
if (cachedData) {
return JSON.parse(cachedData);
}
const freshData = await dbQueryFn();
await client.setEx(key, ttl, JSON.stringify(freshData));
return freshData;
}
To avoid filling RAM, always configure a key eviction policy like LRU (Least Recently Used) or LFU (Least Frequently Used) in your redis.conf configuration.
Related Articles
The Distributed System Design Blueprint: Architecting for High Availability & Scaling
An expert-level system design playbook. Learn database scaling, load balancing configurations, caching patterns (Redis), message queues (Kafka), CDN routing, and microservices decoupling.
Read Article →Step-by-Step MERN Stack Real-World Projects Setup Guide 29
Accelerate your engineering workflow with this masterclass on MERN Stack. We go from linear setups to complex distributed operations.
Read Article →Step-by-Step MERN Stack Real-World Projects Setup Guide 59
Accelerate your engineering workflow with this masterclass on MERN Stack. We go from linear setups to complex distributed operations.
Read Article →Continue Reading
Developer Career Accelerator: Resumes, DSA Coding Tests, and Behavioral Interviews
An actionable roadmap for landing top-tier software engineering roles. Learn how to optimize developer resumes, study for DSA coding tests, and ace behavioral interview questions.
Read Article →Git & GitHub: Branching Strategies, Conflict Resolution, SSH Keys, and Actions CI/CD
A developer playbook for professional version control and automation. Learn how to manage trunk-based branches, resolve rebase conflicts, configure secure SSH keys, and write YAML pipelines.
Read Article →Node.js & Express.js: Event Loop, Middleware Routing, and Cluster Scaling
A comprehensive guide to building high-throughput backends with Node.js. Learn about the Libuv event loop, writing custom Express middleware, and scaling with cluster processes.
Read Article →Prerequisites
The Distributed System Design Blueprint: Architecting for High Availability & Scaling
An expert-level system design playbook. Learn database scaling, load balancing configurations, caching patterns (Redis), message queues (Kafka), CDN routing, and microservices decoupling.
Read Article →Popular Articles
The Complete C Programming Roadmap: From Syntax to Memory Control
A comprehensive deep-dive into C programming, memory optimization, dynamic memory allocation, pointers, data structures, and production-grade coding standards.
Read Article →The Complete C++ Journey: From OOP Fundamentals to Modern Architectures
A comprehensive developer's guide to C++ programming. Deep-dive into class designs, move semantics, template metaprogramming, STL, smart pointers, multithreading, and concurrency.
Read Article →Developer Career Accelerator: Resumes, DSA Coding Tests, and Behavioral Interviews
An actionable roadmap for landing top-tier software engineering roles. Learn how to optimize developer resumes, study for DSA coding tests, and ace behavioral interview questions.
Read Article →Recent Articles
The Complete C Programming Roadmap: From Syntax to Memory Control
A comprehensive deep-dive into C programming, memory optimization, dynamic memory allocation, pointers, data structures, and production-grade coding standards.
Read Article →The Complete C++ Journey: From OOP Fundamentals to Modern Architectures
A comprehensive developer's guide to C++ programming. Deep-dive into class designs, move semantics, template metaprogramming, STL, smart pointers, multithreading, and concurrency.
Read Article →Developer Career Accelerator: Resumes, DSA Coding Tests, and Behavioral Interviews
An actionable roadmap for landing top-tier software engineering roles. Learn how to optimize developer resumes, study for DSA coding tests, and ace behavioral interview questions.
Read Article →