Bird
Raised Fist0
Interview Prepdatabase-management-systemshardAmazonGoogleFlipkartSwiggyRazorpayPhonePeZepto

Database Sharding vs Partitioning vs Replication

Choose your preparation mode3 modes available
▶
Steps
setup

Initialize B-tree Structure

We start with an empty B-tree representing the database system with no shards, partitions, or replicas yet. The root node is created with no keys and no children.

💡 Initialization sets the foundation for visualizing how data distribution mechanisms build upon a base structure.
Line:root = BTreeNode(keys=[], children=[], is_leaf=True, parent=None)
💡 The empty root node represents a database with no data distribution applied yet.
📊
Database Sharding vs Partitioning vs Replication - Watch the Algorithm Execute, Step by Step
Watching this step-by-step visualization helps you grasp the subtle differences and operational mechanisms of sharding, partitioning, and replication, which are often confused in theory but become clear when seen as a process.
Step 1/10
·Active fill★Answer cell
setup
leaf
Starting with an empty B-tree representing the database.
insertKey: Shard1
Shard1
leaf
Inserted first shard key 'Shard1' into root node.
insertKey: Shard2
Shard1Shard2
leaf
Inserted second shard key 'Shard2' into root node.
splitKey: Shard3↑ promoted: Shard2
Shard2
Shard1
leaf
Shard3
leaf
Root node split after inserting 'Shard3', promoting 'Shard2'.
replicate
Shard2
Shard1
leaf
Shard3
leaf
Replicated data to child nodes for fault tolerance.
traverseKey: Shard3
Shard2
Shard1
leaf
Shard3
leaf
Traversed to child node 3 to locate partition for 'Shard3'.
insertKey: PartitionA
Shard2
Shard1
leaf
PartitionAShard3
leaf
Inserted partition key 'PartitionA' into leaf node for 'Shard3'.
replicateKey: PartitionA
Shard2
Shard1
leaf
PartitionAShard3
leaf
Replicated partition data 'PartitionA' to sibling node.
traverseKey: PartitionA
Shard2
Shard1
leaf
PartitionAShard3
leaf
Traversed to leaf node to query partition 'PartitionA'.
traverseKey: PartitionA
Shard2
Shard1
leaf
PartitionAShard3
leaf
Read data from partition 'PartitionA' in replicated node.

Key Takeaways

✓ Sharding partitions data horizontally by splitting keys across nodes.

This insight is hard to see from code alone because the conceptual meaning of keys as shards is abstract without visualization.

✓ Partitioning further divides shards internally to organize data efficiently.

Visualizing partition keys inside leaf nodes clarifies how data is managed at multiple granularities.

✓ Replication duplicates data across nodes to ensure availability and fault tolerance.

Seeing replication as highlighting multiple nodes with the same data helps understand its role beyond simple data storage.

Practice

(1/5)
1. In which scenario is the Isolation property of ACID most critical to ensure correct database behavior?
easy
A. When a transaction must be completed entirely or not at all to avoid partial updates
B. When multiple transactions run concurrently and must not see each other's intermediate states
C. When the database must maintain integrity constraints after each transaction
D. When committed transactions must survive system crashes and power failures

Solution

  1. Step 1: Understand Isolation's role

    Isolation ensures that concurrent transactions do not interfere by hiding intermediate states from each other.
  2. Step 2: Analyze each option

    A describes Atomicity, C describes Consistency, D describes Durability.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Isolation is about concurrency control, so B is correct.
Hint: Isolation = hiding intermediate states during concurrency
Common Mistakes:
  • Confusing Isolation with Atomicity
  • Mixing Consistency with Durability
2. Consider a 1:N relationship between entities Department and Employee, where one department has many employees. When a new employee is added, what is the sequence of steps to maintain referential integrity in the database?
easy
A. Insert the employee record without any reference to the department.
B. Insert the employee record with a foreign key referencing the department's primary key; verify the department exists.
C. Insert the department record with a foreign key referencing the employee's primary key.
D. Insert the employee record first, then create the department record if missing.

Solution

  1. Step 1: Understand the 1:N relationship

    Each employee belongs to one department; department is the 'one' side.
  2. Step 2: Referential integrity requires foreign key

    Employee table must have a foreign key referencing Department's primary key.
  3. Step 3: Insert employee with valid department reference

    Before inserting employee, ensure the department exists to avoid foreign key violation.
  4. Step 4: Evaluate other options

    Insert the employee record first, then create the department record if missing inserts employee before department, risking integrity violation. Insert the department record with a foreign key referencing the employee's primary key reverses foreign key direction, which is incorrect. Insert the employee record without any reference to the department omits foreign key, breaking relationship.
  5. Final Answer:

    Option B -> Option B
  6. Quick Check:

    Foreign keys must reference existing primary keys to maintain integrity.
Hint: In 1:N, the 'many' side holds the foreign key referencing the 'one' side.
Common Mistakes:
  • Inserting dependent records before referenced records
  • Misplacing foreign keys on the 'one' side instead of 'many' side
3. Trace the sequence of events when a network partition occurs in a distributed NoSQL database that prioritizes availability and partition tolerance. What happens to consistency and client reads during the partition?
easy
A. Clients may read stale data as writes continue independently on partitioned nodes
B. Consistency is immediately restored by synchronizing all nodes during the partition
C. The database rejects all reads and writes to prevent inconsistency
D. The database blocks all writes until the partition heals to maintain consistency

Solution

  1. Step 1: Understand CAP priorities

    The database prioritizes availability and partition tolerance, so it must remain operational during partitions.
  2. Step 2: Effect on writes and reads

    Writes continue independently on partitioned nodes to maintain availability, which can cause data divergence.
  3. Step 3: Impact on consistency

    Because nodes can't synchronize during partition, clients may read stale or divergent data temporarily.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Availability + Partition tolerance -> possible stale reads during partition [OK]
Hint: Availability + Partition tolerance -> stale reads allowed during partition
Common Mistakes:
  • Assuming writes are blocked to maintain consistency
  • Believing reads are rejected during partitions
  • Thinking consistency is instantly restored during partition
4. When a row is inserted into a table with an associated AFTER INSERT trigger, what is the correct sequence of events?
easy
A. The trigger and insertion happen simultaneously.
B. The trigger executes first, then the row is inserted.
C. The row is inserted first, then the trigger executes.
D. The trigger executes only if the insertion fails.

Solution

  1. Step 1: Understand AFTER INSERT trigger semantics

    AFTER INSERT triggers execute only after the row has been successfully inserted into the table.
  2. Step 2: Exclude options contradicting this order

    Triggers do not execute before insertion or simultaneously; they also do not execute on failure.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    AFTER triggers run post-operation, confirming the insertion occurred.
Hint: AFTER triggers run after the data change is committed.
Common Mistakes:
  • Assuming triggers run before the data change
  • Believing triggers run on failure
  • Confusing BEFORE and AFTER triggers
5. Which of the following statements about deadlocks in Two-Phase Locking (2PL) is INCORRECT?
medium
A. Strict 2PL completely prevents deadlocks by ordering lock acquisition
B. Deadlock detection algorithms can resolve deadlocks by aborting one of the transactions
C. Deadlocks occur when two or more transactions wait indefinitely for locks held by each other
D. Deadlocks can arise even if transactions follow the 2PL protocol

Solution

  1. Step 1: Understand deadlock conditions

    Deadlocks happen when transactions cyclically wait for locks held by each other, which can occur under 2PL.
  2. Step 2: Analyze deadlock prevention

    Strict 2PL does not guarantee deadlock prevention; it only ensures serializability.
  3. Step 3: Evaluate options

    A, B, and D are correct statements. C is incorrect because strict 2PL does not prevent deadlocks by ordering lock acquisition.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Deadlocks require detection and resolution even under strict 2PL.
Hint: Strict 2PL ensures serializability but not deadlock prevention [OK]
Common Mistakes:
  • Assuming strict 2PL prevents deadlocks
  • Confusing deadlock detection with prevention
  • Believing 2PL eliminates all concurrency issues
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard