HLD Phase 2: Scalability & Databases

Vertical vs Horizontal Scaling, Sharding, Consistent Hashing, CAP Theorem, Database choices.

1. Vertical vs Horizontal Scaling

Vertical Scaling (Scale-Up) means adding more RAM/CPU to a single machine. It is easy but has a hard limit. Horizontal Scaling (Scale-Out) means adding more machines. It essentially scales infinitely but introduces massive complexity (load balancers, distributed state).

2. Consistent Hashing

When horizontally scaling a database or cache, how do you know which server holds your data? Standard modulo hashing (`server = hash(key) % N`) completely breaks if a server goes down, because `N` changes. Consistent Hashing places servers and keys onto a virtual "ring", minimizing data movement when scaling up or down.

3. Redis & Caching Strategies

A fast application never hits the database for read-heavy operations. The most standard approach is the **Cache-Aside Strategy**.
typescript
// Cache-Aside Pattern Implementation
async function getUserProfile(userId: string) {
    // 1. Check Cache first
    const cachedData = await redisClient.get(`user:${userId}`);
    if (cachedData) {
        return JSON.parse(cachedData); // Cache HIT
    }

    // 2. Cache MISS: Fetch from Database
    const dbData = await db.query('SELECT * FROM Users WHERE id = ?', [userId]);

    // 3. Write data to cache with a TTL (Time To Live) to prevent stale data
    await redisClient.setex(`user:${userId}`, 3600, JSON.stringify(dbData));

    return dbData;
}

4. The CAP Theorem

Consistency, Availability, and Partition Tolerance. In distributed systems, networks will fail (Partition Tolerance is mandatory). Therefore, you must choose between **Consistency** (always seeing the latest data, but allowing downtime) or **Availability** (always returning data even if it's stale).

🎯

Take the Phase Interview

Solidify your knowledge. We will ask you 5 random highly-technical questions from the HLD Phase 2: Scalability & Databases bucket. A Staff Engineer AI will strictly evaluate your answers.