SQL & MySQL Interview Questions 26-30 (Scaling, Transactions & Concurrency)

Welcome to the next lesson on database concepts! We've covered how to structure, query, and optimize tables. Now, we're going to focus on two of the biggest topics in database engineering: scaling (how to handle massive amounts of data) and reliability (how to make sure your data is always safe and correct).

These concepts—sharding, partitioning, transactions, ACID, and locking—are the foundation of any large-scale, professional application. Understanding them will show your interviewer you aren't just thinking about code, but about building robust systems.

26. What is database sharding?

Sharding is a database architecture technique for horizontal scaling. It involves splitting one, massive logical database into multiple, smaller, and more manageable pieces called "shards."

Crucially, these shards are spread across multiple physical machines (servers).

Analogy: Imagine a single, colossal library (one database server) that has 50 billion books. It's so big that the check-out line is hours long, and the building is about to collapse.

Sharding is the decision to build three separate library buildings (three different servers) across the city.

  • Building 1 (Shard 1): Holds books for `user_id`s 1 - 1,000,000
  • Building 2 (Shard 2): Holds books for `user_id`s 1,000,001 - 2,000,000
  • Building 3 (Shard 3): Holds books for `user_id`s 2,000,001+

When a user comes to check out a book, the application (the librarian) first looks at their `user_id` (the "shard key") and sends them to the correct building.

  • Pros: Almost infinitely scalable. Queries are much faster because they only run against a fraction of the data.
  • Cons: Significantly more complex. The application has to be "shard-aware." Joins across different shards (different buildings) are extremely difficult and slow.

27. What is partitioning in MySQL? Explain types of partitioning.

Partitioning is a concept that looks like sharding, but it all happens on a single database server.

You take one very large table and split it into smaller, physical sub-tables (partitions), but to your application, it still looks like a single table.

Analogy: Sharding was building new libraries. Partitioning is taking your one massive library building (one server) and organizing its one giant, messy room (one table) into separate, labeled sections (partitions).

For example, you partition your `Orders` table by year. The database creates hidden sub-tables: `Orders_2023`, `Orders_2024`, `Orders_2025`.

Why is this useful? When you run a query like `SELECT * FROM Orders WHERE order_date = '2024-05-15'`, the database is smart enough to only scan the `Orders_2024` partition. This is called "partition pruning" and it's extremely fast.

Common Types of Partitioning:

  • RANGE: Splits data based on a range of values. Perfect for dates or prices. (e.g., `PARTITION BY RANGE(YEAR(order_date))`).
  • LIST: Splits data based on a list of discrete values. Perfect for categories. (e.g., `PARTITION BY LIST(region)` where one partition is `VALUES IN ('North', 'East')` and another is `VALUES IN ('South', 'West')`).
  • HASH: Splits data based on a hash of a key (like `user_id`). The database handles the distribution. This is great for ensuring partitions are all of equal size.
# Example of creating a RANGE-partitioned table
CREATE TABLE Orders (
    order_id INT PRIMARY KEY,
    customer_id INT,
    order_date DATE,
    amount DECIMAL(10, 2)
)
PARTITION BY RANGE (YEAR(order_date)) (
    PARTITION p_2023 VALUES LESS THAN (2024),
    PARTITION p_2024 VALUES LESS THAN (2025),
    PARTITION p_2025 VALUES LESS THAN (2026),
    PARTITION p_max VALUES LESS THAN MAXVALUE
);

-- When you run this, MySQL is smart enough
-- to only search in the 'p_2024' partition.
EXPLAIN SELECT * FROM Orders 
WHERE order_date = '2024-03-01';

28. What are transactions in MySQL?

A transaction is a way to group one or more SQL commands into a single, all-or-nothing unit of work.

This is one of the most important concepts for data reliability.

Analogy: A Bank Transfer
Imagine you want to transfer $100 from your Savings account to your Checking account. This is two separate `UPDATE` commands:

  • 1. `UPDATE Savings SET balance = balance - 100 WHERE id = 123;`
  • 2. `UPDATE Checking SET balance = balance + 100 WHERE id = 123;`

What happens if the database server crashes right after step 1, but before step 2? The $100 is gone. Your database is now corrupt and inconsistent.

A transaction prevents this. By wrapping both commands in a transaction, you tell the database: "You must do both of these things successfully. If anything fails (even a power outage), you must undo all of it."

The key commands are:

  • `START TRANSACTION;`: Begins the unit of work.
  • `COMMIT;`: All commands were successful. Save all changes permanently.
  • `ROLLBACK;`: An error occurred. Undo all changes back to the `START`.
START TRANSACTION;

-- Step 1: Subtract from Savings
UPDATE Savings SET balance = balance - 100 WHERE user_id = 123;

-- Step 2: Add to Checking
UPDATE Checking SET balance = balance + 100 WHERE user_id = 123;

-- If we get here with no errors, save the changes.
COMMIT;

-- If there was an error, a CATCH block in an application
-- would run this command instead:
-- ROLLBACK;

29. Explain ACID properties with examples.

ACID is an acronym for the four guarantees that a good transactional database (like MySQL's InnoDB engine) provides.

These are the rules that make transactions reliable. We can use our bank transfer analogy for all of them.

PropertyStands ForSimple MeaningBank Transfer Example
AAtomicity"All or nothing."The transfer must fully complete (both accounts updated), or not happen at all (both unchanged). It can't be half-done.
CConsistency"Follow the rules."The database starts in a valid state and ends in a valid state. A `CHECK` constraint (like `balance >= 0`) cannot be violated. The bank's total money is conserved.
IIsolation"No interference."If two transfers happen at the same time, they must not see each other's half-finished work. It appears as if one transfer finished before the other began.
DDurability"It's permanent."Once the `COMMIT` is successful, the money is transferred forever, even if the server loses power 1 second later (it's saved to a transaction log).

30. What is locking in MySQL? Explain shared and exclusive locks.

Locking is the mechanism that the database uses to enforce the 'I' (Isolation) in ACID. It's how the database prevents two transactions from interfering with each other and corrupting data.

Analogy: Think of a spreadsheet on a shared drive.

Lock TypeAnalogyAllows Other Readers?Allows Other Writers?
Shared Lock (Read Lock)"Read-only" mode. You are just looking at the spreadsheet.Yes. Multiple people can read at the same time.No. The system won't let anyone else start editing while you're reading.
Exclusive Lock (Write Lock)"Editing" mode. You have the file open and are typing.No. No one else can even open the file to read it (they can't see your half-finished work).No. No one else can edit it at the same time.

In general, MySQL (InnoDB) handles this automatically:

  • `SELECT` commands acquire Shared Locks.
  • `INSERT`, `UPDATE`, and `DELETE` commands acquire Exclusive Locks.

You can also explicitly ask for a stronger lock in your query:

START TRANSACTION;

-- This is a "pessimistic" lock.
-- It acquires an EXCLUSIVE (Write) lock on row 123.
-- It says, "I am going to update this row,
-- so don't let *anyone* else even read it
-- until I am done."
SELECT * FROM Savings WHERE user_id = 123 FOR UPDATE;

-- ... application logic ...

UPDATE Savings SET balance = balance - 100 WHERE user_id = 123;

COMMIT;

Tip: Understanding ACID properties is non-negotiable for a backend or full-stack role. Be sure you can explain Atomicity and Isolation with a simple, clear example like the bank transfer.

🚀 Deep Dive With AI Scholar