
Redis Core Data Structures and Internals
A deep dive into Redis data structures and C internals, exploring SDS, quicklists, skiplists, memory encodings, progressive rehashing, and performance optimization.
Software systems အများစုမှာ Redis ကို basic string caching key-value store တစ်ခုအနေနဲ့သာ စတင်သုံးစွဲလေ့ရှိကြပါတယ်။ ဒါပေမဲ့ Redis ရဲ့ တကယ့် စွမ်းရည်စစ်စစ်ဟာ ရိုးရှင်းတဲ့ dumb cache တစ်ခု မဟုတ်ဘဲ RAM ပေါ်မှာ sub-millisecond latency ဖြင့် တိုက်ရိုက် run ပေးနိုင်တဲ့ advanced "Data Structure Server" ဖြစ်ပါတယ်။
Database architecture တစ်ခုမှာ appropriate data structure ကို မှန်ကန်စွာ ရွေးချယ်နိုင်ခြင်းဟာ memory consumption ကို 60% ထိ ချုံ့ချနိုင်သလို network round-trips တွေနဲ့ serialization overhead တွေကိုလည်း သိသိသာသာ လျှော့ချပေးနိုင်ပါတယ်။ ဒီ insight မှာ Redis ရဲ့ core data structures တွေဖြစ်တဲ့ Strings, Lists, Hashes, Sets, Sorted Sets တွေရဲ့ internal C memory representations (SDS, quicklist, listpack, skiplist, dict) နဲ့ production-grade optimization strategies တွေကို အသေးစိတ် လေ့လာဆန်းစစ်သွားပါမယ်။
Beyond Simple Key-Value Storage
ရိုးရိုး relational database ဒါမှမဟုတ် document store တွေမှာ complex data update လုပ်ချင်ရင် record တစ်ခုလုံးကို memory ထဲ fetch လုပ်၊ deserialize ပြုလုပ်၊ update လုပ်ပြီးမှ database ထဲ ပြန် write လုပ်ရလေ့ရှိပါတယ်။ ဒီ access pattern ဟာ concurrent writes တွေမှာ race conditions တွေ ဖြစ်ပေါ်စေသလို network bandwidth ကိုလည်း ဖြုန်းတီးစေပါတယ်။
Redis မှာတော့ server-side data structures တွေပေါ်မှာ atomic operations တွေ တိုက်ရိုက် run နိုင်ပါတယ်။ ဥပမာ - list ထဲ element အသစ် push လုပ်တာ၊ hash field တစ်ခုတည်းကို increment ပြုလုပ်တာ၊ ဒါမှမဟုတ် leaderboard score တွေကို sorted set ထဲ ranking တွက်ချက်တာတွေကို application logic မလိုဘဲ in-memory data structures တွေက zero serialization latency ဖြင့် atomic execution ပေးစွမ်းပါတယ်။
Redis Object Model and Type Encoding Architecture
Redis ထဲက key-value entry တိုင်းကို C struct ဖြစ်တဲ့ redisObject (robj) ဖြင့် wrap လုပ်ထားပါတယ်။ ဒီ object wrapper ထဲမှာ metadata fields တွေ ပါဝင်ပါတယ်:
type(4 bits): High-level data type (OBJ_STRING,OBJ_LIST,OBJ_HASH,OBJ_SET,OBJ_ZSET) ဖြစ်ပါတယ်။encoding(4 bits): RAM ပေါ်က physical C data structure encoding ဖြစ်ပါတယ်။lru/lfu(24 bits): Cache eviction အတွက် object access timestamp ဖြစ်ပါတယ်။refcount(4 bytes): Shared memory objects reference count ဖြစ်ပါတယ်။ptr(8 bytes): Data တည်ရှိရာ memory address pointer ဖြစ်ပါတယ်။
Redis ဟာ dataset အရွယ်အစားသေးငယ်ချိန်မှာ memory-efficient ဖြစ်တဲ့ compact encodings တွေကို အသုံးပြုပြီး threshold ကျော်လွန်သွားချိန်မှာ high-performance encodings ဆီ dynamic upgrade ပြုလုပ်ပေးပါတယ်။
Strings and Simple Dynamic Strings (SDS)
Redis Strings ဟာ text သာမက serialized JSON, protocol buffers နဲ့ raw binary images တွေကိုပါ 512MB အထိ သိမ်းဆည်းနိုင်ပါတယ်။ C programming language ရဲ့ standard string (char*) ဟာ null character (\0) ဖြင့် အဆုံးသတ်တာကြောင့် binary data မသိမ်းနိုင်သလို strlen() ခေါ်တိုင်း O(N) time complexity ဖြစ်ပေါ်စေပါတယ်။
ဒါကို ကျော်လွှားဖို့ Redis က Simple Dynamic String (SDS) struct ကို သုံးပါတယ်။ SDS မှာ string length (len), allocated buffer capacity (alloc), flags နှင့် byte buffer (buf[]) တို့ ပါဝင်ပါတယ်။ ဒါကြောင့် string length ရှာဖွေခြင်းဟာ O(1) instantaneous ဖြစ်ပြီး buffer pre-allocation ကြောင့် continuous append operations တွေမှာ OS memory reallocation overhead ကို သိသိသာသာ လျှော့ချပေးနိုင်ပါတယ်။
Lists: From Linked Lists to Quicklists and Listpacks
Redis Lists တွေဟာ chronological timeline feeds, message queues နဲ့ activity logs တွေအတွက် အဓိက အသုံးပြုကြပါတယ်။ သမိုင်းကြောင်းအရ Redis Lists တွေကို doubly linked lists (adlist) ဖြင့် သိမ်းဆည်းခဲ့ပေမဲ့ node တစ်ခုချင်းစီအတွက် forward/backward pointers တွေကြောင့် 64-bit architecture မှာ memory overhead အလွန်ကြီးမားခဲ့ပါတယ်။
ခေတ်သစ် Redis မှာတော့ Quicklist architecture ကို standard အဖြစ် သုံးပါတယ်။ Quicklist ဆိုတာ doubly linked list of listpacks ဖြစ်ပြီး node တစ်ခုချင်းစီဟာ memory ပေါ်မှာ contiguous byte blocks အဖြစ် compress လုပ်ထားတဲ့ listpack array တစ်ခု ဖြစ်ပါတယ်။ ဒီ hybrid design ကြောင့် list head နဲ့ tail တွေမှာ O(1) push/pop operations တွေကို dynamic memory compactness ဖြင့် တွဲဖက်ရရှိစေပါတယ်။
Hashes and Sets: Memory Packing with Intset and Dict
User profiles, product catalogs နဲ့ session states တွေကို JSON string အစား Redis Hashes သုံးပြီး သိမ်းဆည်းခြင်းဟာ fields တွေကို individually read/write လုပ်နိုင်စေပါတယ်။
- Hashes (
OBJ_HASH): Field အရေအတွက်နဲ့ value size သေးငယ်ချိန်မှာ memory compact listpack အနေနဲ့ သိမ်းပြီး threshold ကျော်လွန်ပါက hash table (dict) အဖြစ် ပြောင်းလဲပါတယ်။dictဟာ bucket collision တွေကို chaining ဖြင့် ရှင်းလင်းပြီး hash table နှစ်ခုကြား incremental step-by-step progressive rehashing နည်းလမ်းဖြင့် main thread မ block အောင် rehash ပြုလုပ်ပေးပါတယ်။ - Sets (
OBJ_SET): Unique values တွေကို သိမ်းဆည်းတဲ့အခါ values အားလုံးဟာ integer သက်သက်ဖြစ်ပြီး elements နည်းပါးပါက contiguous sorted array ဖြစ်တဲ့intsetဖြင့် သိမ်းဆည်းပါတယ်။ String values တွေ ပါဝင်လာချိန် သို့မဟုတ် element limit ကျော်လွန်ချိန်မှာ standarddicthash table (where values are NULL) အဖြစ် auto-upgrade ဖြစ်သွားပါတယ်။
Sorted Sets: The Power of Skiplists and Dict Pairs
Sorted Sets (ZSET) ဟာ Redis ရဲ့ အစွမ်းထက်ဆုံး data structures ထဲက တစ်ခုဖြစ်ပြီး real-time gaming leaderboards, priority task dispatchers နဲ့ rate limiting sliding windows တွေမှာ မရှိမဖြစ် သုံးပါတယ်။
ZSET မှာ element တစ်ခုချင်းစီကို floating-point score တစ်ခုနဲ့ တွဲဖက်ထားပါတယ်။ Redis ဟာ memory table အနေနဲ့ B-Tree အစား Skip List (zskiplist) နှင့် Hash Table (dict) ကို တွဲဖက်အသုံးပြုပါတယ်:
- Hash Table (
dict): Member key မှ score ကို O(1) complexity ဖြင့် instant lookup ရယူနိုင်ပါတယ်။ - Skip List (
zskiplist): Multi-level forward pointers တွေ ပါဝင်ပြီး insertion, deletion နဲ့ range queries (ZRANGEBYSCORE) တွေကို average O(log N) ဖြင့် execute လုပ်ပေးနိုင်ပါတယ်။ B-Tree ထက် pointer balancing ရိုးရှင်းပြီး RAM usage သက်သာစေပါတယ်။
Memory Mapping and Internal Encodings Overview
Redis high-level data types တွေကနေ underlying physical C memory representations ဆီ ဘယ်လို mapping ချိတ်ဆက်ထားပုံကို အောက်ပါ architectural diagram မှာ မြင်တွေ့နိုင်ပါတယ်:
Dataset အရွယ်အစား ကြီးထွားလာတာနဲ့အမျှ Redis ဟာ compact byte-array representations (Listpack / Intset) ကနေ fast-lookup pointer structures (Dict / Skiplist) ဆီ automatically shift လုပ်ပေးပါတယ်။
Practical Pipeline Automation with Core Structures
Node.js application တစ်ခုမှာ ioredis library သုံးပြီး multi-structure operations တွေကို single network round-trip ဖြင့် atomic pipeline execute လုပ်ပုံ code snippet ကို အောက်မှာ လေ့လာနိုင်ပါတယ်:
import Redis from 'ioredis';
const redis = new Redis({ host: '127.0.0.1', port: 6379 });
export async function recordUserActivity(userId: string, action: string, score: number) {
const pipeline = redis.pipeline();
const timestamp = Date.now();
pipeline.hset(`user:${userId}`, { lastAction: action, updatedAt: timestamp });
pipeline.lpush(`user:${userId}:timeline`, JSON.stringify({ action, timestamp }));
pipeline.ltrim(`user:${userId}:timeline`, 0, 99);
pipeline.sadd('active_users', userId);
pipeline.zadd('leaderboard:activity', score, userId);
const results = await pipeline.exec();
return results?.every(([err]) => err === null);
}
ဒီ implementation မှာ single round-trip ဖြင့် Hash, List, Set နဲ့ Sorted Set operations တွေကို batch dispatch လုပ်ပေးတာကြောင့် network latency overhead ကို သိသာစွာ လျှော့ချပေးနိုင်ပါတယ်။
Core Data Structures and Complexity Matrix
Redis data structures တွေရဲ့ underlying memory encodings နဲ့ operational time complexities တွေကို အောက်ပါ comparison table မှာ အကျဉ်းချုပ် ဖော်ပြထားပါတယ်:
| Data Type | Primary C Encoding | Time Complexity | Memory Profile | Primary Use Case |
|---|---|---|---|---|
| String | Simple Dynamic String (SDS) | O(1) get/set, O(1) append | Medium (Overhead per key) | Caching, session tokens, distributed locks |
| List | Quicklist (Listpack blocks) | O(1) push/pop, O(N) index | High compact efficiency | Event queues, recent activity feeds |
| Hash | Listpack or Hash Table (Dict) | O(1) field lookup / update | Very High (When in listpack) | User objects, entity states, counters |
| Set | Intset or Hash Table (Dict) | O(1) add/contains, O(N) union | Extremely low with intset | Unique tags, friend lists, deduplication |
| Sorted Set | Listpack or Skiplist + Dict | O(log N) insert/range, O(1) score | Medium to High | Leaderboards, rate limiters, priority queues |
Final Thoughts
Redis ရဲ့ core data structures တွေကို နက်နက်နဲနဲ နားလည်ထားခြင်းဟာ backend engineers တွေအတွက် high-performance caching နှင့် low-latency systems တွေ တည်ဆောက်ရာမှာ ကြီးမားတဲ့ အားသာချက်တစ်ခု ဖြစ်စေပါတယ်။ Key တိုင်းကို JSON string အဖြစ် serialize မလုပ်ဘဲ Hashes သို့မဟုတ် Sets တွေအဖြစ် split လုပ်ခြင်းက memory footprint ကို သက်သာစေသလို partial updates တွေကို atomic ဖြစ်စေပါတယ်။
ဒါ့အပြင် large datasets တွေမှာ single thread blocking မဖြစ်စေဖို့ O(N) commands တွေဖြစ်တဲ့ KEYS *, HGETALL ဒါမှမဟုတ် SMEMBERS တွေကို production မှာ ရှောင်ရှားပြီး SCAN, HSCAN နှင့် pipelining patterns တွေကို သုံးစွဲရပါမယ်။ Memory thresholds (hash-max-listpack-entries, zset-max-listpack-entries) တွေကို workload အလိုက် tune လုပ်ပေးခြင်းအားဖြင့် scale ဖြစ်ပြီး memory-efficient ဖြစ်တဲ့ caching architecture အဖြစ် production မှာ စိတ်ချလက်ချ ထိန်းကျောင်း မောင်းနှင်နိုင်မှာ ဖြစ်ပါတယ်။