Bird
Raised Fist0
Interview Prepdatabase-management-systemsmediumAmazonGoogleMicrosoftFlipkartSwiggyZepto

SQL vs NoSQL - When to Use Which (CAP Theorem Context)

Choose your preparation mode3 modes available
▶
Steps
setup

Initialize Decision Tree

The algorithm initializes a B-tree representing decision points about SQL and NoSQL databases under the CAP theorem. The root node contains the main CAP properties: Consistency, Availability, Partition tolerance.

💡 Setting up the decision tree structure is crucial to visualize how the algorithm will traverse and decide between SQL and NoSQL based on CAP properties.
Line:decision_tree = initialize_decision_tree()
💡 The decision tree models the CAP theorem constraints and database properties as nodes and keys.
📊
SQL vs NoSQL - When to Use Which (CAP Theorem Context) - Watch the Algorithm Execute, Step by Step
Watching this step-by-step traversal helps you understand the decision-making process behind choosing SQL or NoSQL databases, especially how CAP theorem constraints influence these choices.
Step 1/10
·Active fill★Answer cell
setup
ConsistencyAvailabilityPartition Tolerance
SQL
leaf
NoSQL
leaf
Hybrid
leaf
Initialized decision tree with CAP theorem properties at root.
traverse
ConsistencyAvailabilityPartition Tolerance
SQL
leaf
NoSQL
leaf
Hybrid
leaf
Traversal started at root node to evaluate CAP properties.
compareKey: Consistency
ConsistencyAvailabilityPartition Tolerance
SQL
leaf
NoSQL
leaf
Hybrid
leaf
Strong consistency required, moving to SQL node.
traverse
ConsistencyAvailabilityPartition Tolerance
SQL
leaf
NoSQL
leaf
Hybrid
leaf
Confirmed SQL suitability for strong consistency.
compareKey: Availability
ConsistencyAvailabilityPartition Tolerance
SQL
leaf
NoSQL
leaf
Hybrid
leaf
High availability required, moving to NoSQL node.
traverse
ConsistencyAvailabilityPartition Tolerance
SQL
leaf
NoSQL
leaf
Hybrid
leaf
Confirmed NoSQL suitability for high availability and partition tolerance.
compareKey: Partition Tolerance
ConsistencyAvailabilityPartition Tolerance
SQL
leaf
NoSQL
leaf
Hybrid
leaf
Balanced CAP requirements, moving to Hybrid node.
traverse
ConsistencyAvailabilityPartition Tolerance
SQL
leaf
NoSQL
leaf
Hybrid
leaf
Confirmed Hybrid suitability for balanced CAP trade-offs.
traverse
ConsistencyAvailabilityPartition Tolerance
SQL
leaf
NoSQL
leaf
Hybrid
leaf
Decision summarized based on CAP requirements and traversal.
traverse
ConsistencyAvailabilityPartition Tolerance
SQL
leaf
NoSQL
leaf
Hybrid
leaf
Final recommendation output based on CAP theorem decision tree.

Key Takeaways

✓ The CAP theorem properties directly guide the traversal path in the decision tree, determining the database choice.

This insight is hard to see from code alone because the logical flow of CAP constraints to database types is abstract without visualization.

✓ SQL databases prioritize consistency, NoSQL prioritize availability and partition tolerance, and hybrid solutions balance these trade-offs.

Visualizing these trade-offs as nodes clarifies how different database types fit different CAP requirements.

✓ The traversal path and node states highlight how decisions are made step-by-step, showing the reasoning behind each choice.

Seeing active and highlighted nodes helps concretely understand branching decisions that are implicit in code.

Practice

(1/5)
1. Which of the following statements about enforcing Isolation levels in a database is TRUE regarding performance trade-offs?
medium
A. Serializable isolation can cause significant performance degradation due to locking overhead
B. Snapshot isolation eliminates all concurrency anomalies without any trade-offs
C. Read uncommitted isolation prevents dirty reads without any performance cost
D. Higher isolation levels always improve throughput by reducing conflicts

Solution

  1. Step 1: Understand isolation levels and trade-offs

    Higher isolation levels like Serializable prevent anomalies but increase locking and reduce concurrency.
  2. Step 2: Analyze options

    A is false because higher isolation often reduces throughput. B is false; snapshot isolation can have anomalies like write skew. C is false; read uncommitted allows dirty reads.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Serializable isolation trades performance for correctness.
Hint: Serializable isolation = correctness at performance cost
Common Mistakes:
  • Believing higher isolation always improves throughput
  • Assuming read uncommitted prevents dirty reads
2. 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
3. In a system that uses denormalization for performance, what is a robust strategy to handle updates to redundant data without sacrificing too much query speed?
hard
A. Use triggers or stored procedures to synchronously update all redundant copies during each write operation
B. Manually update redundant data only during off-peak hours to minimize performance impact
C. Avoid denormalization entirely and rely solely on normalized tables with optimized indexes
D. Accept eventual consistency by asynchronously propagating updates, allowing stale reads temporarily

Solution

  1. Step 1: Understand update challenges in denormalization

    Synchronously updating all copies can degrade write performance and increase latency.
  2. Step 2: Evaluate asynchronous update benefits

    Asynchronous propagation allows writes to complete quickly, trading immediate consistency for eventual consistency.
  3. Step 3: Consider alternatives

    Avoid denormalization entirely and rely solely on normalized tables with optimized indexes avoids denormalization but sacrifices read speed. Manually update redundant data only during off-peak hours to minimize performance impact delays updates, risking long inconsistency windows.
  4. Step 4: Assess triggers/stored procedures

    Use triggers or stored procedures to synchronously update all redundant copies during each write operation can cause write bottlenecks and complexity.
  5. Final Answer:

    Option D -> Option D
  6. Quick Check:

    Eventual consistency balances performance and data freshness in denormalized systems [OK]
Hint: Eventual consistency enables fast writes with acceptable stale reads [OK]
Common Mistakes:
  • Assuming synchronous updates are always best
  • Ignoring the trade-off between consistency and performance
  • Believing manual batch updates are practical for real-time systems
4. In a scenario where a many-to-many (N:M) relationship between Students and Clubs includes an attribute 'MembershipDate' specific to the relationship, how should this be modeled in the ER diagram and relational schema?
hard
A. Add 'MembershipDate' as an attribute to either Students or Clubs entity.
B. Store 'MembershipDate' in both Students and Clubs tables separately.
C. Create an associative entity between Students and Clubs with 'MembershipDate' as its attribute.
D. Ignore 'MembershipDate' as attributes cannot belong to relationships.

Solution

  1. Step 1: Recognize attribute belongs to relationship

    'MembershipDate' describes the association, not individual entities.
  2. Step 2: Model associative entity

    Introduce an associative entity (junction table) representing the N:M relationship with 'MembershipDate' as its attribute.
  3. Step 3: Evaluate other options

    Add 'MembershipDate' as an attribute to either Students or Clubs entity incorrectly assigns relationship attribute to an entity. Store 'MembershipDate' in both Students and Clubs tables separately duplicates data and breaks normalization. Ignore 'MembershipDate' as attributes cannot belong to relationships is false; relationships can have attributes.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    Relationship attributes belong in associative entities for N:M relationships.
Hint: Attributes describing relationships go into associative entities, not individual entities.
Common Mistakes:
  • Placing relationship attributes in entities
  • Ignoring relationship attributes in schema design
5. Consider a database system using strict 2PL with shared and exclusive locks. If a transaction T1 holds a shared lock on data item X and requests an exclusive lock on X (lock upgrade), but another transaction T2 also holds a shared lock on X, what advanced mechanism can the system use to avoid deadlock in this scenario?
hard
A. Allow T1 to preemptively abort T2 to acquire the exclusive lock immediately
B. Permit T1 to acquire the exclusive lock without waiting since it already holds a shared lock
C. Use a wait-die or wound-wait deadlock prevention scheme based on transaction timestamps
D. Disable lock upgrades entirely to prevent deadlocks

Solution

  1. Step 1: Identify the deadlock risk

    T1 waits for T2 to release shared lock to upgrade to exclusive, creating potential deadlock if T2 waits on T1.
  2. Step 2: Understand deadlock prevention schemes

    Wait-die and wound-wait use timestamps to decide which transaction waits or aborts, preventing deadlocks.
  3. Step 3: Analyze options

    A is aggressive and can cause starvation. C violates lock compatibility rules. D is too restrictive and reduces concurrency. B correctly describes a standard advanced mechanism.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    Timestamp-based schemes are standard advanced mechanisms to avoid deadlocks in 2PL.
Hint: Use timestamp-based wait-die or wound-wait to prevent deadlocks on lock upgrades [OK]
Common Mistakes:
  • Assuming lock upgrades never cause deadlocks
  • Thinking lock upgrades can bypass compatibility rules
  • Believing aborting transactions arbitrarily is acceptable
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