Chapter 3 β€’ Premium Interview Kit System Design Standard ⏱️ 35 Min Read

Chapter 3: System Design & Distributed Systems Architecture Internals

Deep-dive exploration of CAP/PACELC theorems, Consistent Hashing with VNodes, Raft Consensus protocol, B-Trees vs LSM-Trees, Vector Clocks, Snowflake ID generation, and Fencing Tokens.

πŸ‘¨β€πŸ’»
Nagendra Rana
SDE-2 @ Digilytics AI • Lead Architect WhatInfoTech

1. Concept Introduction

The Transition from Monolith to Distributed Systems

As digital applications scaled from thousands to hundreds of millions of concurrent users, single-node hardware ceiling limits (vertical scaling / scale-up) were breached. While a single server can be upgraded with 128+ CPU cores and terabytes of RAM, hardware costs scale non-linearly, and physical constraints (memory bus bandwidth, socket limits, thermal throttling) create hard ceilings. Furthermore, single-node systems represent single points of failure (SPOFs).

scaleup-vs-scaleout.txt • Scale-Up vs Horizontal Scale-Out
+-------------------------------------------------------------------------+
|                       HORIZONTAL SCALING (SCALE-OUT)                    |
|  +--------------+   +--------------+   +--------------+   +-----------+ |
|  | Worker Node 1|   | Worker Node 2|   | Worker Node 3|   | Node N... | |
|  +--------------+   +--------------+   +--------------+   +-----------+ |
|        β”‚                   β”‚                  β”‚                 β”‚       |
|  ========= Inter-Node Network Mesh (Unreliable, Asynchronous) ========= |
+-------------------------------------------------------------------------+

Core Fundamental Theorems

1. CAP Theorem (Brewer’s Theorem)

In any asynchronous network subject to partitions, a distributed datastore can simultaneously guarantee at most two of the following three properties:

  • Consistency (C): Every read receives the most recent write or an error (Linearizability).
  • Availability (A): Every non-failing node returns a non-error response for every request.
  • Partition Tolerance (P): The system continues operating despite arbitrary message drops or network delays.

2. PACELC Theorem (Abadi’s Extension)

System Partition State (P) Normal State (E) Classification Typical Use-Case
Apache Cassandra Availability (A) Latency (L) PA/EL High-throughput write logs, social feeds
Amazon DynamoDB Availability (A) Latency (L) PA/EL E-commerce shopping carts
Google Cloud Spanner Consistency (C) Consistency (C) PC/EC Global financial ledgers, transactional banking
MongoDB (Default) Consistency (C) Latency (L) PC/EL Document management systems

2. Theory & Low-Level Internals

A. Consistent Hashing Ring Mechanics

consistent-hashing-ring.txt • 32-bit Integer Space Topology
                      CONSISTENT HASHING RING (32-bit Space)

                             Node_A (Hash: 0x1000)
                                  0 / 2^32
                                  . . .
                          .                   .
                      .                           .
           Node_D (Hash: 0xC000)                Key_1 (Hash: 0x3000)
                .                                    .
               .                                      .  --> Maps Clockwise
              .                                        .     to Node_B
               .                                      .
           Key_2 (Hash: 0x8000)                 Node_B (Hash: 0x5000)
                      .                           .
                          .                   .
                                  . . .
                             Node_C (Hash: 0x9000)

B. Storage Engines: B-Trees vs. LSM-Trees

Attribute B-Tree Engine (InnoDB / Postgres) LSM-Tree Engine (RocksDB / Cassandra)
Write Strategy In-place update to fixed 16KB disk pages. Sequential append-only to RAM MemTable + WAL.
Write Performance Slower (Random I/O due to page splits). Extremely High (Pure sequential append).
Read Performance Extremely Fast (Direct page indexing). Slower (Checks MemTable + Bloom Filters + SSTables).
Write Amplification High (Rewrites 16KB page for 10-byte change). Moderate (Driven by SSTable Compaction).

3. Real Production Architecture Example

Global Multi-Region Low-Latency Rate Limiter serving 2,000,000 requests/sec across 3 geographic regions (US-East, EU-West, AP-South) with sub-10ms evaluation latency.

multi-region-rate-limiter.txt • Anycast DNS + Local Redis + Kafka Sync
                                GLOBAL EDGE LAYER
                          +----------------------------+
                          |   Anycast DNS / Cloudflare |
                          +----------------------------+
                                        β”‚
             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
             β–Ό                          β–Ό                          β–Ό
+-------------------------+  +-------------------------+  +-------------------------+
|   REGION: US-EAST       |  |   REGION: EU-WEST       |  |   REGION: AP-SOUTH      |
|  +-------------------+  |  |  +-------------------+  |  |  +-------------------+  |
|  | API Gateway Proxy |  |  |  | API Gateway Proxy |  |  |  | API Gateway Proxy |  |
|  +-------------------+  |  |  +-------------------+  |  |  +-------------------+  |
|            β”‚            |  |            β”‚            |  |            β”‚            |
|            β–Ό            |  |            β–Ό            |  |            β–Ό            |
|  +-------------------+  |  |  +-------------------+  |  |  +-------------------+  |
|  | Local Redis Cluster| |  |  | Local Redis Cluster| |  |  | Local Redis Cluster| |  |
|  | (Sliding Window)  |  |  |  | (Sliding Window)  |  |  |  | (Sliding Window)  |  |
|  +-------------------+  |  |  +-------------------+  |  |  +-------------------+  |
+-------------------------+  +-------------------------+  +-------------------------+

4. Code Examples

A. Consistent Hashing Ring with Virtual Nodes (VNodes) in Java

package com.interviewkit.systemdesign.hashing;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.SortedMap;
import java.util.ConcurrentSkipListMap;

/**
 * PRODUCTION PRACTICE: Consistent Hash Ring with Virtual Nodes.
 * Solves non-uniform key distribution and hotspotting.
 */
public class ConsistentHashRing<T> {

    private final int numberOfReplicas; // VNodes per Physical Server
    private final SortedMap<Long, T> circle = new ConcurrentSkipListMap<>();

    public ConsistentHashRing(int numberOfReplicas, Collection<T> nodes) {
        this.numberOfReplicas = numberOfReplicas;
        for (T node : nodes) {
            add(node);
        }
    }

    public void add(T node) {
        for (int i = 0; i < numberOfReplicas; i++) {
            String vNodeKey = node.toString() + "-VNODE-" + i;
            long hash = computeHash(vNodeKey);
            circle.put(hash, node);
        }
    }

    public T get(String key) {
        if (circle.isEmpty()) return null;
        long hash = computeHash(key);
        if (!circle.containsKey(hash)) {
            SortedMap<Long, T> tailMap = circle.tailMap(hash);
            hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey();
        }
        return circle.get(hash);
    }

    private long computeHash(String key) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] digest = md5.digest(key.getBytes(StandardCharsets.UTF_8));
            long hash = ((long) (digest[3] & 0xFF) << 24) |
                         ((long) (digest[2] & 0xFF) << 16) |
                         ((long) (digest[1] & 0xFF) << 8) |
                         ((long) (digest[0] & 0xFF));
            return hash & 0xffffffffL;
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("MD5 missing", e);
        }
    }
}

B. Distributed Sliding Window Rate Limiter Lua Script for Redis

-- SLIDING WINDOW RATE LIMITER LUA SCRIPT
local rateLimitKey = KEYS[1]
local now = tonumber(ARGV[1])
local windowSize = tonumber(ARGV[2])
local maxRequests = tonumber(ARGV[3])
local requestId = ARGV[4]

local clearBefore = now - windowSize

-- 1. Remove expired timestamps
redis.call('ZREMRANGEBYSCORE', rateLimitKey, 0, clearBefore)

-- 2. Count active requests in sliding window
local currentRequestCount = redis.call('ZCARD', rateLimitKey)

if currentRequestCount < maxRequests then
    redis.call('ZADD', rateLimitKey, now, requestId)
    redis.call('PEXPIRE', rateLimitKey, windowSize)
    return 1 -- PERMITTED
else
    return 0 -- RATE LIMITED (HTTP 429)
end

5. Tiered Interview Questions

Medium Level • Senior SDE

Q1: How do Vector Clocks detect and resolve concurrent update conflicts in key-value stores?

Ideal Answer: Physical CPU wall-clock timestamps drift independently across nodes. Vector Clocks maintain `[Node_ID, Version_Counter]` tuples. If two concurrent updates occur during a network partition where neither clock vector dominates the other, a Causal Conflict (Sibling) is stored and returned to the client to execute application-level merge logic.

Architect Level • Principal Engineer

Q2: Design a 64-bit Globally Monotonic Snowflake ID Generator at 100k IDs/sec per node

Ideal Answer: Layout: `[1-bit Sign | 41-bit Timestamp MS | 10-bit Worker Node ID | 12-bit Sequence Counter]`. Supports 69.7 years baseline lifetime, 1,024 worker nodes, and 4,096 IDs/ms per node. NTP clock rollback is handled by detecting `currentTimestamp < lastTimestamp` and rejecting/pausing ID generation.

6. Production Debugging Scenario

Symptom: Distributed Lock Loss & Double Execution during GC Pause

Root Cause: Node A suffered a 12-second Stop-The-World JVM GC pause. Redis TTL (5s) expired, allowing Node B to acquire the lock. When Node A unpaused, both nodes executed the billing batch simultaneously!

Resolution: Enforce the Fencing Tokens Pattern where database resources reject writes containing stale/lower fencing token integers.

Official Partner Verified Opportunity

πŸ“œ Official Developer Certification & Cloud Hosting

Validate your software engineering skills with official developer certifications and high-performance cloud hosting.

β˜• Support My Work