🔒 Database System Design: Transaction Isolation Levels



This content originally appeared on DEV Community and was authored by Sajjad Rahman

ডাটাবেস ডিজাইন শেখার সময় আমরা প্রায়ই একটা শব্দ শুনি — “Isolation Level”। অনেকে একে ভুল করে “Isolation Layer” বলে , কিন্তু আসলে এটা হলো Transaction Isolation Level, যা ডাটাবেসের অন্যতম গুরুত্বপূর্ণ বিষয়।

আমরা একদম সহজভাবে বুঝব — Read Uncommitted, Read Committed, Repeatable Read, Serializable — এই চারটা level কী করে, তাদের পার্থক্য কী, আর কবে কোনটা ব্যবহার করা উচিত।

🧠 Transaction Isolation Level কী?

Transaction Isolation Level হলো এমন একটা নিয়ম যা নির্ধারণ করে —

একটি transaction চলার সময় অন্য transaction-এর data access বা modification কতটা দেখতে পারবে।

ডাটাবেস (যেমন: MySQL, PostgreSQL, SQL Server) এই isolation level ব্যবহার করে
Concurrency Control বজায় রাখে, অর্থাৎ একাধিক transaction একসাথে চললেও data inconsistency না হয়।

⚙ চারটি মূল Isolation Level

Level Prevents Allows Risk
Read Uncommitted ❌ কিছুই না সব Dirty Read
Read Committed Dirty Read Non-repeatable Read Medium risk
Repeatable Read Dirty + Non-repeatable Read Phantom Read Low risk
Serializable সব কিছুই না Slowest, but safest

🔍 প্রতিটা Isolation Level বিস্তারিতভাবে

1⃣ Read Uncommitted

🔹 সবচেয়ে কম isolation লেভেল।
🔹 এখানে এক transaction অন্য transaction-এর uncommitted data পর্যন্ত দেখতে পারে।

📘 Example:

  • Transaction A data update করছে কিন্তু এখনো commit করে নাই।
  • Transaction B ওই data read করে ফেলল।
  • পরে A rollback করলে B যেটা পড়েছে সেটা ভুল data (Dirty Read)।

⚠ Problem: Dirty Read
📈 Use case: Performance খুব দরকার, consistency কম গুরুত্বপূর্ণ হলে (খুব কম ব্যবহার হয়)।

2⃣ Read Committed (👉 সবচেয়ে বেশি ব্যবহৃত)

🔹 এক transaction শুধুমাত্র অন্যদের committed data পড়তে পারে।
🔹 Uncommitted data দেখা যায় না।

📘 Example:

  • T1 reads balance = 100
  • T2 updates balance = 200 (commits)
  • T1 আবার read করলে দেখে 200 😵 (value change হয়ে গেছে)

⚠ Problem: Non-repeatable Read
📈 Use case: SQL Server, Oracle – এই level ডিফল্টভাবে ব্যবহার করে।

3⃣ Repeatable Read

🔹 একবার read করা data transaction চলাকালীন সময়ে অপরিবর্তিত থাকে।
🔹 অর্থাৎ একই row বারবার পড়লেও একই value পাওয়া যাবে।
🔹 তবে নতুন row add হলে সেটা দেখা যেতে পারে (Phantom Read)।

⚠ Problem: Phantom Read
📈 Use case: MySQL-এর default isolation level।

4⃣ Serializable (🔒 Highest Isolation)

🔹 এই লেভেলে database ensure করে transactions একে অপরের থেকে সম্পূর্ণ serially execute হয়।
🔹 কোনও Dirty Read, Non-repeatable Read বা Phantom Read হয় না।

⚠ Problem: Performance কমে যায় (lock বেশি লাগে)।
📈 Use case: Banking, Accounting, বা Financial systems যেখানে 100% consistency দরকার।

🧩 Quick Comparison Table

Isolation Level Dirty Read Non-repeatable Read Phantom Read Performance
Read Uncommitted ❌ Allowed ❌ Allowed ❌ Allowed ✅ Fastest
Read Committed ✅ Prevented ❌ Allowed ❌ Allowed ⚖ Medium
Repeatable Read ✅ Prevented ✅ Prevented ❌ Allowed ⚖ Medium-Low
Serializable ✅ Prevented ✅ Prevented ✅ Prevented 🐢 Slowest

💡 সহজে মনে রাখার উপায়

RU → RC → RR → S
“U Commit, Repeat, Serialize” 🧠

এই ক্রমে Isolation Level যত বাড়বে:
🔼 Consistency বাড়বে
🔽 Performance কমবে

🔒 Transaction Phenomena Explained

Phenomenon Description Prevented by
Dirty Read Uncommitted data পড়া Read Committed ↑
Non-repeatable Read একই row দু’বার পড়লে value change হয় Repeatable Read ↑
Phantom Read একই query দু’বার করলে নতুন row দেখা যায় Serializable ↑

উপসংহার

Transaction Isolation Level হলো database consistency, concurrency আর performance এর মধ্যে balance বজায় রাখার উপায়। তুমি কোন level ব্যবহার করবে সেটা নির্ভর করে তোমার application-এর nature এর উপর।

🔹 High performance দরকার?Read Committed
🔹 High consistency দরকার?Serializable
🔹 Balanced system চাইলে?Repeatable Read


This content originally appeared on DEV Community and was authored by Sajjad Rahman