SQL vs NoSQL
কোনটি কোথায়? — সিস্টেম ডিজাইনের সবচেয়ে কমন প্রশ্ন।
System Design ইন্টারভিউয়ে এই প্রশ্ন প্রায় গ্যারান্টিযুক্ত: "এই use case-এ SQL না NoSQL?" — সঠিক উত্তরের চেয়ে গুরুত্বপূর্ণ কেন সেই উত্তর। চলুন গভীরে দেখি।
এক নজরে
SQL (Relational)
- Structured table
- Fixed schema
- SQL query language
- ACID transaction
- Vertical scaling
- JOIN supported
- Strong consistency
NoSQL
- Document/KV/Column/Graph
- Flexible schema
- Custom API/CQL/etc.
- BASE (eventually consistent)
- Horizontal scaling
- Limited JOIN
- Eventual consistency
বিভিন্ন মাত্রায় তুলনা
১. Schema
- SQL: Strict — column, type আগে define। Schema পরিবর্তন expensive।
- NoSQL: Flexible — yyyymm.json-এ যেকোনো field।
২. Scalability
- SQL: Vertical scale primary। Sharding সম্ভব কিন্তু complex।
- NoSQL: Horizontal scale-এর জন্য designed। Auto-shard।
৩. Consistency
- SQL: Strong consistency (ACID)।
- NoSQL: সাধারণত eventual consistency (BASE)। কিছু DB tunable।
৪. Query Power
- SQL: Powerful — JOIN, GROUP BY, subquery, complex aggregate।
- NoSQL: Limited — primarily key/index based; complex query কঠিন।
৫. Transactions
- SQL: Multi-row, multi-table ACID transaction।
- NoSQL: Single document/row guaranteed; multi-document limited (MongoDB 4+)।
৬. Data Relationships
- SQL: Foreign key, JOIN দিয়ে strong relational integrity।
- NoSQL: Embed (denormalize) বা reference (manual JOIN code)।
৭. Maturity
- SQL: ৫০ বছরের পরিপক্কতা — টুল, library, expert।
- NoSQL: ১৫-২০ বছর — কিন্তু দ্রুত পরিপক্ক হচ্ছে।
CAP Theorem-এর প্রিজম-এ
Distributed system-এ CAP-এর তিনটির মধ্যে দুটি possible:
- SQL DB: সাধারণত CP — Consistency এবং Partition tolerance।
- NoSQL DB: অনেকে AP — Availability এবং Partition tolerance। (Cassandra, DynamoDB)
- কিছু NoSQL CP (MongoDB single-node default)।
কখন SQL?
- ACID transaction critical: Banking, payment, inventory।
- Complex query: Reporting, BI, analytics।
- Strong relationships: User → Order → Product → Inventory।
- Data integrity essential: Healthcare, legal, government।
- Schema stable: Established business logic।
- Small to medium scale: ১M user-এ MySQL fine।
কখন NoSQL?
- Massive scale: Petabyte data, millions QPS।
- Flexible schema: Rapid product evolution।
- Specialized data: Time-series, graph, geospatial।
- Real-time: Cache, leaderboard, session।
- Hierarchical/nested: JSON document directly।
- Eventually consistent OK: Social feed, news, view count।
বাস্তব use case
SQL উপযুক্ত
- E-commerce order/payment
- HR system
- Banking
- Inventory management
- Booking system
- CRM
NoSQL উপযুক্ত
- Social media feed
- IoT sensor data
- Real-time chat
- Analytics dashboard
- Content CMS
- Caching/session
Polyglot Persistence — দুটোই
Modern সিস্টেমে একটি app একাধিক DB ব্যবহার করে:
E-commerce Architecture:
- PostgreSQL → orders, payments (ACID)
- MongoDB → product catalog (flexible)
- Redis → session, cache
- Elasticsearch → product search
- Cassandra → user activity log
বাস্তব উদাহরণ
- Facebook: MySQL (user, post) + Cassandra (messaging) + Memcached + TAO (graph)।
- Netflix: MySQL (billing) + Cassandra (history) + DynamoDB।
- Uber: Schemaless on MySQL (custom) + Cassandra + Redis।
সাধারণ ভুল ধারণা
- "NoSQL modern, SQL old": Both modern। PostgreSQL-এর update ২০২৪-এ — খুব advanced।
- "একটাই বেছে নিতে হবে": Polyglot স্বাভাবিক — দুটোই।
- "NoSQL = schema-less": Schema থাকে — DB-তে enforce হয় না কিন্তু code-এ থাকে।
- "SQL scale করা যায় না": Twitter, Facebook প্রথমে MySQL — sharding-এ ভালোই scale হয়েছে।
Best Practices
- Use case define করুন — তারপর DB।
- "NoSQL trendy" বলে SQL ছাড়বেন না।
- একই অ্যাপে multiple DB acceptable।
- Tooling, team expertise বিবেচনা করুন।
- সন্দেহে — PostgreSQL দিয়ে শুরু করুন (JSON support আছে — flexibility)।
📌 চ্যাপ্টার সারমর্ম
- SQL: structured, ACID, vertical scale, complex query।
- NoSQL: flexible, BASE, horizontal scale, specialized।
- SQL = banking, transaction; NoSQL = social, IoT, real-time।
- Polyglot persistence — modern সিস্টেমে দুটোই।
- Use case-ই decision driver।