πŸ—οΈ SYSTEM DESIGN β€” Database Internals & Software Architecture

Cara berpikir sistem besar. Database Internals menjawab β€œkenapa query-mu lambat dan bagaimana storage bekerja di level disk.” Software Architecture menjawab β€œbagaimana membangun sistem yang tidak runtuh saat tumbuh 100x.”

Cara Baca

Database Internals = fondasi sebelum pakai ORM/query optimizer dengan benar. Software Architecture = fondasi sebelum design sistem yang harus bertahan bertahun-tahun. Keduanya tentang keputusan yang mahal untuk diubah nanti.


Sheet 1 β€” Database Internals: Dari Disk sampai Query Optimizer

πŸ—„οΈ Level & Konsep⚑ Cara Kerja & Sweet Spot☠️ Tembok KematianπŸ”— Koneksi ke Topik Lain
Level 0 β€” Storage Engine & Page Layout (heap file, page, slotted page, row vs column store)Data disimpan dalam page (biasanya 4KB atau 16KB). Heap file: page tidak terurut, insert cepat tapi scan lambat. Slotted page: array offset di awal page, data dari belakang β€” support variable-length row. Row store (OLTP): satu row disimpan bersama, bagus untuk point query. Column store (OLAP): satu kolom disimpan bersama, bagus untuk aggregate query.Column store buruk untuk update single row (harus update banyak kolom file). Row store buruk untuk SELECT AVG(salary) yang scan jutaan row tapi hanya butuh satu kolom.Koneksi ke Data Recovery (page forensik, carving), koneksi ke OS (filesystem page, mmap)
Level 1 β€” Indexing (B-tree, B+tree, hash index, bitmap, covering index, index selectivity)B+tree: semua data di leaf node, leaf node linked list β†’ range scan efisien. Internal node hanya key untuk navigasi. Hash index: O(1) point lookup, tidak support range query. Covering index: index menyimpan semua kolom yang dibutuhkan query β€” tidak perlu balik ke heap. Index selectivity: rasio distinct value / total row β€” index pada kolom low selectivity (gender) tidak efektif.Over-indexing: setiap write harus update semua index β†’ write amplification. Index tidak dipakai jika: fungsi di kolom (WHERE YEAR(date)=2024), implicit cast, leading wildcard (LIKE '%abc').B+tree = aplikasi langsung dari Algoritma Level 2, koneksi ke Database Recovery (index reconstruction)
Level 2 β€” Query Execution (query parser, planner, optimizer, execution engine, vectorized execution)Query β†’ parse β†’ AST β†’ logical plan β†’ physical plan (optimizer pilih join order, index usage, execution strategy) β†’ execution. Cost-based optimizer: estimasi cost berdasarkan statistik (cardinality, histogram). Vectorized execution (SIMD): proses batch kolom sekaligus, bukan row per row β€” ClickHouse, DuckDB.Stale statistics β†’ optimizer pilih plan buruk β†’ query 100x lebih lambat. N+1 query problem: ORM yang generate satu query per row.EXPLAIN ANALYZE adalah senjata wajib, koneksi ke OS (execution = syscall ke disk/memory)
Level 3 β€” Transaction & Concurrency (ACID, MVCC, isolation levels, 2PL, deadlock detection)ACID: Atomicity (all or nothing), Consistency (invariant terjaga), Isolation (transaksi tidak interference), Durability (commit survive crash). MVCC (Multi-Version Concurrency Control): setiap write buat versi baru, reader lihat snapshot lama β†’ no read-write conflict. Isolation levels: Read Uncommitted < Read Committed < Repeatable Read < Serializable. 2PL: lock semua resource sebelum release β†’ deadlock possible.Phantom read terjadi di Repeatable Read. Long-running transaction di MVCC β†’ version bloat (PostgreSQL vacuum). Deadlock: detect via wait-for graph, resolve via timeout atau victim selection.Koneksi ke Distributed Systems (distributed transaction), koneksi ke OS (mutex, semaphore)
Level 4 β€” Write-Ahead Log & Recovery (WAL, redo log, undo log, checkpoint, ARIES)WAL: tulis ke log dulu sebelum modifikasi actual data page. Crash β†’ replay log dari last checkpoint. Redo log: apa yang harus dilakukan ulang (committed transaction). Undo log: apa yang harus dibatalkan (uncommitted transaction). ARIES algorithm: Analysis β†’ Redo β†’ Undo. Checkpoint: batas mulai replay agar tidak dari awal.Log volume besar pada write-heavy workload. Checkpoint terlalu jarang β†’ recovery lama. fsync bottleneck: WAL harus fsync ke disk untuk Durability β€” tidak bisa diabaikan.Koneksi langsung ke Data Recovery (WAL sebagai forensik artefak), koneksi ke OS (filesystem journaling = konsep serupa)
Level 5 β€” Distributed Database (replication, sharding, CAP theorem, consensus, two-phase commit)Replication: primary-replica (async) atau synchronous (lebih lambat, lebih aman). Sharding: partisi data ke node berbeda β€” range shard, hash shard, directory shard. CAP theorem: Consistency, Availability, Partition tolerance β€” pilih 2 dari 3 (partition selalu terjadi β†’ pilih CA atau CP). Paxos/Raft: consensus algorithm untuk agree on single value di distributed system. 2PC: koordinasi atomic commit di multi-node.Split-brain: dua node sama-sama kira dirinya primary β†’ data conflict. 2PC blocking: coordinator crash β†’ participant stuck. CAP sering disalahpahami β€” lebih tepatnya PACELC.Koneksi ke Cloud Level 7 (Vitess, CockroachDB), koneksi ke Blockchain (consensus)
☠️ Level 6 β€” Storage Engine Internals (LSM-tree, SSTable, compaction, bloom filter, write amplification)LSM-tree (Log-Structured Merge): tulis ke memtable (in-memory) β†’ flush ke SSTable (immutable sorted file on disk) β†’ compaction gabungkan SSTable. Write amplification: data ditulis berkali-kali selama compaction. Read amplification: scan banyak SSTable untuk satu key. Bloom filter: cek apakah key mungkin ada di SSTable sebelum disk read. Dipakai: RocksDB, Cassandra, LevelDB, HBase.Write amplification bisa 10-30x pada workload random write. Compaction overhead bisa spike latency. Space amplification: data lama belum tercompact.Koneksi ke Flash Drive Forensics (wear leveling mirip compaction concern), koneksi ke Algoritma (Bloom filter)

Sheet 2 β€” Software Architecture: Dari Monolith sampai Event-Driven

πŸ›οΈ Level & Pattern⚑ Cara Kerja & Sweet Spot☠️ Tembok Kematian🎯 Contoh Nyata
Level 0 β€” Design Principles (SOLID, DRY, KISS, YAGNI, separation of concerns)Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion. DRY (Don’t Repeat Yourself). KISS (Keep It Simple). YAGNI (You Aren’t Gonna Need It). Separation of concerns: setiap modul punya satu alasan untuk berubah.SOLID diterapkan berlebihan β†’ over-abstraction, terlalu banyak interface untuk hal sederhana. DRY berlebihan β†’ coupling tinggi antara modul yang seharusnya independen.Prinsip ini ada di semua codebase yang bertahan > 5 tahun
Level 1 β€” Design Patterns (creational, structural, behavioral β€” GoF 23 patterns)Creational: Singleton, Factory, Abstract Factory, Builder, Prototype. Structural: Adapter, Bridge, Composite, Decorator, Facade, Proxy. Behavioral: Observer, Strategy, Command, Iterator, State, Template Method, Chain of Responsibility. Pattern = solusi yang sudah terbukti untuk masalah yang sering berulang.Pattern bukan tujuan β€” solusi untuk masalah spesifik. Pattern yang dipaksakan pada masalah yang tidak cocok β†’ complexity tanpa benefit. β€œIf all you have is a hammer, everything looks like a nail.”Observer = Event listener di semua framework. Strategy = sort algorithm pluggable. Decorator = Python decorator
Level 2 β€” Monolithic Architecture (layered, modular monolith, MVC, clean architecture)Satu deployable unit. Layered: Presentation β†’ Business Logic β†’ Data Access. MVC: Model-View-Controller. Clean Architecture (Uncle Bob): dependency selalu mengarah ke dalam (domain tidak tahu framework). Modular monolith: monolith dengan batas modul yang jelas β€” langkah sebelum microservices.Big ball of mud: monolith tanpa struktur β†’ setiap perubahan bisa break hal lain. Scaling hanya bisa vertical (scale seluruh aplikasi, bukan hanya komponen yang bottleneck).WordPress, Django app sederhana, awal semua startup
Level 3 β€” Microservices (service decomposition, API gateway, service discovery, circuit breaker)Dekomposisi berdasarkan business domain (Domain-Driven Design). Setiap service punya database sendiri (database per service pattern). API Gateway: single entry point, routing, rate limiting, auth. Service discovery: Consul, Kubernetes DNS. Circuit Breaker: hentikan request ke service yang failing agar tidak cascade failure.Distributed monolith: microservices yang tightly coupled β€” dapat kompleksitas distributed system tanpa benefit. Overhead operasional besar. Debugging lintas service sangat sulit tanpa distributed tracing.Netflix, Uber, Tokopedia di scale tertentu
Level 4 β€” Event-Driven Architecture (event sourcing, CQRS, message queue, Kafka, saga pattern)Event sourcing: simpan sequence of events, bukan current state β€” audit trail sempurna, bisa replay. CQRS (Command Query Responsibility Segregation): pisahkan write model (command) dari read model (query). Kafka: distributed event streaming, retention lama, consumer bisa replay. Saga: long-running distributed transaction via compensating transaction.Eventual consistency bisa bingungkan user. Event schema evolution sulit (backward/forward compatibility). Debugging event-driven system kompleks β€” membutuhkan event correlation.Gojek (order processing), bank (transaction log = event sourcing alami)
Level 5 β€” Distributed System Patterns (rate limiting, backpressure, bulkhead, sidecar, service mesh)Rate limiting: token bucket, leaky bucket, sliding window. Backpressure: producer tidak boleh lebih cepat dari consumer mampu proses. Bulkhead: isolasi failure β€” pool thread terpisah per service dependency. Sidecar: container terpisah yang handle cross-cutting concern (logging, tracing, mTLS).Backpressure yang tidak diimplementasi β†’ memory exhaustion. Rate limiting yang terlalu ketat β†’ false throttling legitimate user. Cascading failure tanpa circuit breaker.Istio (sidecar), Netflix Hystrix (circuit breaker), AWS API Gateway (rate limiting)
Level 6 β€” Domain-Driven Design (bounded context, aggregate, repository, domain event, ubiquitous language)Bounded Context: batas domain yang jelas β€” β€œOrder” di Inventory berbeda dari β€œOrder” di Shipping. Aggregate: cluster entity yang diperlakukan sebagai unit consistency. Ubiquitous Language: developer dan domain expert pakai terminologi yang sama. Domain Event: sesuatu yang terjadi di domain yang significant. Anti-corruption layer: terjemahan antar bounded context.DDD over-engineering untuk domain sederhana (CRUD aplikasi tidak butuh DDD). Bounded context yang salah β†’ tight coupling.Fondasi cara decompose microservices yang benar
☠️ Level 7 β€” Resilience & Chaos Engineering (chaos monkey, fault injection, SLA/SLO/SLI, error budget)Chaos engineering: sengaja inject failure ke production untuk uji resilience sebelum failure terjadi sendiri. Netflix Chaos Monkey: randomly terminate VM di production. SLI (Service Level Indicator): metrik konkret (latency P99). SLO (Objective): target SLI (P99 < 200ms). SLA (Agreement): kontrak dengan konsekuensi. Error budget: (1 - SLO) Γ— time = β€œjatah gagal” yang bisa dipakai untuk ship fitur baru.Chaos engineering tanpa observability yang baik = tidak bisa diagnose apa yang break. Error budget habis β†’ freeze fitur baru sampai reliability pulih.Google SRE book, Netflix Chaos Monkey, Gremlin

Peta Keputusan Arsitektur

MULAI PROYEK BARU
        β”‚
        β–Ό
Berapa user? Seberapa kompleks domain?
        β”‚
   Sederhana/kecil          Kompleks/besar
        β”‚                        β”‚
        β–Ό                        β–Ό
   [Monolith]            [Modular Monolith]
   Clean Architecture     DDD + Bounded Context
        β”‚                        β”‚
        β”‚ Traffic grow           β”‚ Team grow
        β–Ό                        β–Ό
   [Scale vertical]       [Microservices]
   atau tambah read        hanya jika needed
   replica                        β”‚
                                  β”‚ Event complexity tinggi
                                  β–Ό
                          [Event-Driven]
                          Kafka + CQRS + Saga
                                  β”‚
                                  β”‚ Multi-region
                                  β–Ό
                          [Distributed DB]
                          CockroachDB / Vitess

Aturan Martin Fowler soal Microservices

β€œDon’t start with microservices. Start with a monolith, understand your domain, then extract services when you feel the pain of the monolith.” Mayoritas startup yang mulai dengan microservices di hari pertama akhirnya refactor ke modular monolith.

System Design Interview vs Realita

System design interview mengajarkan cara presentasi keputusan arsitektur, bukan cara membuat keputusan yang benar. Di realita: mulai sederhana, ukur, optimasi berdasarkan data aktual β€” bukan asumsi di whiteboard.


πŸ”— Lihat Juga


System Design | Database Internals + Software Architecture Β· Dari Disk Page sampai Chaos Engineering