পাঠ্যক্রম খুলুন
মধ্যবর্তী পর্যায়

NoSQL Model: document, key-value, wide-column ও graph

Document, Key-Value, Wide-Column, and Graph Models

এই পাঠের কুইজ ↓৬ মিনিট পড়াPHP 8.3+ · Laravel 12
এই পাঠের সূচিপত্র

এই পাঠে যা শিখবেন

  • Document, key-value, wide-column ও graph
  • Aggregate boundary ও partition key
  • Laravel থেকে key-value access-এর সীমা

সমস্যা দিয়ে শুরু

এক API-তে user session lookup, product variation, time-series events ও friend-of-friend query আছে। সবকিছুকে “NoSQL data” বললে গুরুত্বপূর্ণ access pattern হারিয়ে যায়। কোন identity দিয়ে lookup, কোন fields একসঙ্গে update এবং কোন relationship traverse করবেন তার ওপর model বেছে নিতে হয়।

সহজ করে বুঝি

Key-value store key দিয়ে opaque/structured value পড়ে; session ও disposable cache-এ useful। Document store nested attributes এক aggregate-এ রাখতে পারে; বড় unbounded array update cost বাড়ায়। Wide-column model partition key ও clustering order অনুযায়ী query optimize করে; arbitrary joins সাধারণত primary strength নয়। Graph model vertices/edges দিয়ে relationship traversal প্রকাশ করে। Model নির্বাচন product-এর actual query, transaction, indexing ও durability features-এর সঙ্গে মিলিয়ে করতে হবে।

পরিচিত একটি উপমা

Key-value labeled locker, document একটি dossier, wide-column নির্দিষ্ট আলমারির ordered register, graph পরিচিতির network map। Locker দিয়ে বন্ধুদের বন্ধু খোঁজা যায়, কিন্তু অনেক box খুলতে হবে। Map-এ সেই প্রশ্ন স্বাভাবিক, তবে এক key-এর value আনা আলাদা সহজ কাজ।

নকশাটি দেখুন

Diagram লোড হচ্ছে…

এটি decision aid, universal product recommendation নয়।
Diagram-এর text flow দেখুন
flowchart TD
 Q[What question do we ask] --> K[Exact identity lookup]
 Q --> D[Nested aggregate read]
 Q --> W[Partition ordered range]
 Q --> G[Relationship traversal]
 K --> KV[Key value]
 D --> DC[Document]
 W --> WC[Wide column]
 G --> GR[Graph]

এটি decision aid, universal product recommendation নয়। Exact lookup relational index-এও efficient হতে পারে। Document aggregate-এর ভিতরে frequently accessed data একসঙ্গে রাখার সুবিধা আছে। Wide-column partition bounded হওয়া দরকার। Graph traversal-এর depth/degree limit না দিলে query explosion হতে পারে। Model query-এর cost shape বোঝাতে সাহায্য করে।

PHP ও Laravel-এ প্রয়োগ

Laravel Redis connection দিয়ে bounded session-like ephemeral preference example। Redis configured এবং authenticated server-derived user ID দরকার। এটি login session implementation নয়; actual auth session Laravel session subsystem-এ রাখুন। TTL-সহ Redis string content JSON-এ encode/decode করা হয়েছে।

php
use Illuminate\Support\Facades\Redis;

$key = "preferences:preview:{$userId}";
$value = json_encode(['theme' => 'dark', 'locale' => 'bn'], JSON_THROW_ON_ERROR);
Redis::connection()->setex($key, 300, $value);

$raw = Redis::connection()->get($key);
$preferences = $raw === null
    ? ['theme' => 'light', 'locale' => 'bn']
    : json_decode($raw, true, 512, JSON_THROW_ON_ERROR);

সুবিধা, সীমাবদ্ধতা ও trade-off

Embedding related data one read-এ আনতে পারে, কিন্তু repeated shared facts update করা কঠিন। Reference normalization কম duplication রাখে, extra lookup লাগে। Wide partition predictable range scan দেয়, unbounded partition hotspot ও size issue তৈরি করতে পারে। Graph traversal expressive, কিন্তু high-degree nodes ও depth cost measure করতে হবে। Operational maturity ও query flexibility একই গুরুত্বে দেখুন।

সাধারণ ভুল

  • Nested array চিরদিন বাড়তে দেওয়া।
  • Key-value store-এ arbitrary filtering primary query হিসেবে ধরা।
  • Partition key না ভেবে সব events এক tenant bucket-এ রাখা।

আরও একটু গভীরে

Data model আর consistency আলাদা axes। Document store multi-document transaction দিলে তার cost/scope পড়ুন; key-value store replication lag থাকতে পারে। Relationship denormalize করে adjacency list বানানো সম্ভব, তবে write-side consistency-এর দায় application-এ আসে। Schema evolution non-relational store-এও লাগে: old document missing field, versioned serializer ও backfill plan দরকার। “Schema-less” বলে validation বাদ দিলে corrupt shape downstream failure আনে।

মনে রাখুন

প্রতিটি model একটি query shape স্বাভাবিক করে, অন্যটি ব্যয়বহুল করতে পারে। Aggregate ও partition size bounded রাখুন। Actual database guarantee category label থেকে অনুমান করবেন না।

নিজে চেষ্টা করুন

Product variations, user preference, telemetry per device এবং friendship recommendation-এর জন্য model candidate দিন। প্রতিটির key/partition, expected maximum size এবং expensive query লিখুন। Single relational database alternative-ও তুলনায় রাখুন।

নিজের বোঝা যাচাই করুন

1. Document মানেই arbitrary shape?

Flexible হতে পারে, কিন্তু application contract ও validation দরকার। পুরোনো ও নতুন document version coexist করার planও লাগবে।

2. Wide-column-এ partition কেন?

Data locality ও range query shape নির্ধারণ করে। Skewed বা unbounded partition load ও storage imbalance বাড়াতে পারে।

3. Graph কখন useful?

Relationship traversal প্রশ্নে, যেমন constrained friends-of-friends। Depth ও high-degree node limit না দিলে query cost বড় হয়।

4. Key-value-তে search?

Possible extra indexes বা scanning লাগতে পারে, কিন্তু exact key lookup তার মূল strength। Access pattern বদলালে model পুনর্বিবেচনা করুন।

5. Redis example auth session?

না। এটি ephemeral preferences demo। Secure authentication lifecycle Laravel session/auth mechanisms দিয়ে manage করতে হবে।

আরও পড়ুন

এই পাঠের কুইজ

এই পাঠ থেকে ২টি প্রশ্ন। উত্তর দিয়ে ব্যাখ্যা দেখুন; অধ্যায়ের কুইজ আলাদাভাবে দিতে হবে।

এই পাঠের ২টি প্রশ্নের উত্তর দিন। পাস করতে দুটিই সঠিক হতে হবে।

জমা দেওয়ার পর সঠিক উত্তর ও ব্যাখ্যা দেখবেন। ফলাফল এই ব্রাউজারে সংরক্ষিত থাকবে। অনুশীলনের ফল অধ্যায় সম্পন্নের হিসাবে যোগ হয় না।

সব বিষয় মিলিয়ে অনুশীলন করুন →

অগ্রগতি এই ব্রাউজারে সংরক্ষিত থাকে। শুধু পাঠ খুললে সম্পন্ন হয় না।