N-tier Architecture
Application-কে আলাদা layer-এ ভাগ — পরিষ্কার responsibility।
একটি রেস্টুরেন্টে ভাবুন: customer order দেন waiter-কে (UI), waiter rsoiyaghr-এ পাঠান (logic), রান্না হয় কাঁচামাল থেকে (data)। তিনটি স্তর — পরিষ্কার দায়িত্ব। কেউ অন্যের কাজে নাক গলায় না। এটাই N-tier Architecture।
N-tier কী?
N-tier Architecture = software-কে N-সংখ্যক logical layer-এ ভাগ করা। প্রতিটি layer specific responsibility-তে কাজ করে এবং পরের layer-এর সাথে define-করা interface দিয়ে যোগাযোগ করে।
Tier vs Layer
- Layer: Logical separation (code-এর organization)। একই machine-এ থাকতে পারে।
- Tier: Physical separation (deployment unit)। আলাদা server-এ।
সব tier layer, কিন্তু সব layer tier নয়।
1-Tier (Single-tier)
সব কিছু এক জায়গায় — UI, logic, data এক executable-এ।
- উদাহরণ: MS Word, Notepad — sole installation।
- সুবিধা: সহজ, fast (no network)।
- অসুবিধা: Multi-user difficult, scale impossible।
2-Tier (Client-Server)
Client + Server। Client UI ও কিছু logic; Server data ও back-end logic।
- উদাহরণ: পুরাতন banking app, accounting software।
- সুবিধা: Better than 1-tier, multi-user।
- অসুবিধা: Business logic client ও server-এ ছড়ানো; update painful।
3-Tier — সবচেয়ে কমন
Web app-এর standard:
Presentation Tier
UI/UX — যা user দেখে।
- HTML/CSS/JS
- Mobile native
- React/Vue/Angular
- কোনো business logic নেই
Application/Logic Tier
Business rule, validation, processing।
- API server (Node.js, Django, Spring)
- Authentication
- Workflow logic
- Data transformation
Data Tier
Persistence — data store ও retrieve।
- RDBMS (PostgreSQL, MySQL)
- NoSQL (MongoDB, Cassandra)
- Cache (Redis)
- File storage
3-tier-এর সুবিধা
- Separation of concerns: Each layer-এর specific job।
- Independent scaling: API server hot হলে শুধু সেটা scale।
- Maintainability: এক layer-এর change অন্য-কে affect করে না।
- Reusability: Same API multiple UI-তে (web, mobile)।
- Security: DB direct exposed না — API behind।
- Technology choice: Layer-wise different tech।
4-tier, 5-tier, N-tier
Modern web-এ আরও layer যোগ:
Common 4-tier
- Web tier: NGINX/Apache — static file, SSL, routing।
- Application tier: business logic API।
Modern N-tier উদাহরণ
Design Pattern Connection
MVC (Model-View-Controller)
- View → Presentation tier
- Controller → Application tier (entry)
- Model → Data access logic
MVVM, MVP
Variants — কোনটি view ও logic আরও আলাদা করে।
বাস্তব উদাহরণ
Facebook (high-level)
- CDN (static)
- Load balancer
- Web tier (PHP/Hack)
- Service tier (microservices)
- Cache (Memcached, TAO)
- DB (MySQL, Cassandra)
Banking System
- Mobile/Web client
- API Gateway
- Business logic server
- Transaction processing
- Database (Oracle)
Anti-patterns
- Layer skip: UI সরাসরি DB call — bypassing logic tier।
- Logic in UI: Business rule presentation-এ — duplicated, hard to maintain।
- Logic in DB: অনেক stored procedure-এ business rule — testing/debug হারাম।
- Tight coupling: Layer-গুলো তীব্রভাবে depend — change cascading।
সাধারণ ভুল ধারণা
- "Tier ও layer একই": Layer logical, tier physical।
- "More tiers = better": না — overhead বাড়ে। যথেষ্ট দিয়ে শুরু।
- "Microservice replaces N-tier": Microservice-ও N-tier-এর extension।
Best Practices
- 3-tier দিয়ে শুরু করুন — সবচেয়ে কমন।
- Layer interface (API/contract) clearly define।
- Layer skip করবেন না — discipline বজায় রাখুন।
- Each tier-এ separate scaling strategy।
- Stateless logic tier — horizontal scale-friendly।
- Layer-এ technology change করতে ভয় পাবেন না (API contract intact থাকলে)।
📌 চ্যাপ্টার সারমর্ম
- N-tier = software-কে N layer-এ ভাগ।
- Layer logical, Tier physical।
- 3-tier (Presentation, Logic, Data) সবচেয়ে কমন।
- Separation of concerns + independent scaling।
- Layer skip বা logic ভুল জায়গায় = anti-pattern।