Transactions & Concurrency Flashcards

Transactions: BEGIN, COMMIT, ROLLBACK Savepoints and nested transactions ACID properties: Atomicity, Consistency, Isolation, Durability Isolation levels: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE Locking: Shared vs Exclusive locks Deadlocks and prevention (35 cards)

1
Q

What is a transaction in SQL?

A

A transaction is a sequence of operations performed as a single logical unit of work, using BEGIN, COMMIT, and ROLLBACK.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does COMMIT do in SQL?

A

COMMIT makes all changes made during the transaction permanent in the database.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does ROLLBACK do in SQL?

A

ROLLBACK undoes all changes made during the transaction, reverting the database to its previous state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are savepoints in SQL?

A

Savepoints allow partial rollbacks within a transaction by marking intermediate points.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are nested transactions?

A

Nested transactions simulate sub-transactions using savepoints but aren’t fully supported in all DBMSs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the ACID acronym stand for?

A

Atomicity, Consistency, Isolation, Durability — the key properties of a reliable transaction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Atomicity in ACID?

A

Ensures all operations in a transaction succeed or none do, maintaining database integrity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Consistency in ACID?

A

Ensures a transaction transforms the database from one valid state to another, enforcing rules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Isolation in ACID?

A

Ensures concurrent transactions do not interfere with each other’s intermediate states.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Durability in ACID?

A

Ensures committed data is permanently stored and survives system failures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is READ UNCOMMITTED isolation?

A

Allows reading uncommitted changes, risking dirty reads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is READ COMMITTED isolation?

A

Prevents dirty reads but allows non-repeatable reads and phantom reads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is REPEATABLE READ isolation?

A

Prevents dirty and non-repeatable reads but may allow phantom reads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is SERIALIZABLE isolation?

A

The strictest level — fully isolates transactions as if executed serially, preventing all anomalies.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a shared lock?

A

Allows multiple transactions to read a resource but not modify it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an exclusive lock?

A

Prevents any other transaction from reading or writing the locked resource.

17
Q

What causes a deadlock in SQL?

A

A circular wait condition where two or more transactions are waiting for each other’s resources.

18
Q

What is deadlock prevention?

A

Techniques like timeout, resource ordering, and wait-die schemes to avoid circular waits.

19
Q

What’s the advantage of using transactions?

A

They ensure data consistency, integrity, and error recovery.

20
Q

What’s a disadvantage of transactions?

A

They may increase complexity and cause contention or deadlocks in high-concurrency systems.

21
Q

What is a best practice for transactions?

A

Keep transactions short, well-scoped, and avoid user interaction during them.

22
Q

What is a common use case for savepoints?

A

Undoing specific parts of a transaction while preserving others.

23
Q

How do transactions impact system design?

A

They help ensure consistency but require careful handling in distributed or high-concurrency systems.

24
Q

What are architectural implications of isolation levels?

A

Higher isolation improves accuracy but reduces concurrency and performance.

25
What is a performance issue with SERIALIZABLE isolation?
It limits parallelism and may cause contention or blocking in high-load systems.
26
What is a way to monitor transaction behavior?
Use logs, slow query analysis, deadlock detection tools, and transaction counters.
27
What is a debugging tip for deadlocks?
Use database logs or SHOW ENGINE INNODB STATUS (MySQL) to identify lock waits.
28
What is a tradeoff between consistency and concurrency?
Stricter consistency often reduces concurrency, while looser models improve performance at risk of anomalies.
29
What’s a gotcha with nested transactions?
ROLLBACK of an inner savepoint doesn’t undo outer operations — full rollback requires top-level control.
30
What’s a real-world example of deadlock?
Two users transferring money between accounts where each locks one account and waits for the other.
31
What is a common interview question about isolation levels?
Explain the difference between READ COMMITTED and REPEATABLE READ with examples.
32
What is an advantage of using savepoints?
They provide finer control over rollbacks within complex transactions.
33
What’s a good practice for avoiding deadlocks?
Access tables and rows in the same order across transactions to prevent circular waits.
34
What is phantom read in SQL?
A row appears in a subsequent query of the same transaction due to another committed insert.
35
What’s the role of locking in concurrency control?
Prevents conflicts and anomalies between simultaneous transactions.