π Concurrency & Consensus Hierarchy β Dari Thread ke Distributed Agreement
Concurrency bukan hanya tentang multi-threading β ia adalah 6 lapisan model dari thread di OS hingga consensus global di blockchain. Catatan ini memetakan evolusi model concurrency (multiprocessing, multithreading, async/await, actor, consensus protocol) dengan trade-off matrix, failure mode (deadlock, race, byzantine), dan analogi lintas domain.
Daftar Isi
1. Premise β Concurrency Adalah Norma, Bukan Fitur
2. Six-Layer Concurrency Hierarchy
3. Layer C0 β Physical Parallelism
4. Layer C1 β Process/Thread
5. Layer C2 β Lock-Based Synchronization
6. Layer C3 β Lock-Free & Wait-Free
7. Layer C4 β Async/Await & Event Loop
8. Layer C5 β Actor Model
1. Premise β Concurrency Adalah Norma, Bukan Fitur
CPUs tidak menjadi lebih cepat (clock speed plateau ~5 GHz sejak 2010). Mereka menjadi lebih banyak (multi-core). Jika kode tidak concurrent, ia hanya menggunakan 1/N core.
Hukum:
Amdahl: Speedup = 1 / (1 - P + P/N) β diminishing return
Gustafson: scaled speedup = N + (1 - N) Γ s β better scaling dengan problem size
Klasifikasi concurrency:
ββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββ
β β Single instruction β Multiple instruction β
ββββββββββββββββΌββββββββββββββββββββββΌβββββββββββββββββββββββββ€
β Single data β SISD (serial) β MISD (redundant) β
β Multiple dat β SIMD (vector) β MIMD (multi-core) β
ββββββββββββββββ΄ββββββββββββββββββββββ΄βββββββββββββββββββββββββ
2. Six-Layer Concurrency Hierarchy
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β C5 β Consensus Protocol (Paxos, Raft, BFT) β β global agreement
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β C4 β Actor Model (Erlang, Akka, Orleans) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β C3 β Async/Await & Event Loop (JS, Rust, Python asyncio) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β C2 β Lock-Free & Wait-Free (atomic, CAS) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β C1 β Lock-Based (mutex, semaphore, RWLock) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β C0 β Process/Thread (OS-level parallelism) β β hardware
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
3. Layer C0 β Physical Parallelism & OS Thread
Hardware Context
Platform Threads/Time Model Single-core 1 Time-sharing Multi-core (2-8) 2-8 Symmetric multiprocessing (SMP) Many-core (8-64) 8-64 Non-uniform memory access (NUMA) GPU (CUDA) 10K+ SIMT (Single Instruction Multiple Thread) Array (SIMD) 256/512 bits Vectorized instructions (AVX-512)
Kernel vs User Threads
Model Description Example 1:1 (kernel) Every user thread β kernel thread Linux pthreads, Windows N:1 (user-space) Many user threads β 1 kernel thread Green threads (old Java) M:N (hybrid) M user β N kernel Go goroutines, Erlang processes
4. Layer C1 β Lock-Based Synchronization
Primitives
Primitive Semantics Typical Uses Mutex Mutual exclusion Protect critical section Semaphore Counted signaling Resource pool RWLock Readersβwriter Read-heavy workloads Condition Variable Block until predicate Producerβconsumer Barrier Wait for N threads Parallel phase
Deadlock Prevention
Four necessary conditions (Coffman, 1971):
Mutual exclusion
Hold and wait
No preemption
Circular wait
Break any one to prevent. Practical approach: lock ordering (hierarchical locking).
5. Layer C2 β Lock-Free & Wait-Free
Level Guarantee Technique Obstruction-free Guaranteed to complete if no contention CAS loop Lock-free System makes progress even if thread dies CAS + retry Wait-free Every thread completes in bounded steps Read, copy, update (RCU)
Hardware Atomics
Operation Example CAS (Compare-And-Swap) cmpxchg (x86), LDREX/STREX (ARM)FAA (Fetch-And-Add) lock xadd (x86)LL/SC (Load-Linked/Store-Conditional) ARM, PowerPC Transactional Memory Intel TSX (buggy), HTM in POWER8+
6. Layer C3 β Async/Await & Event Loop
ββββββββββββββββββββββββββββββββββββββββββββββββ
β Application Code β
β async fn fetch_data(url) β Result<Data> β
β await http::get(url) β
ββββββββββββββββββββββββββββββββββββββββββββββββ€
β Runtime (Executor) β
β ββ Event loop (epoll/kqueue/IOCP) β
β ββ Task scheduler (work-stealing) β
β ββ Timer wheel (timeout, delay) β
ββββββββββββββββββββββββββββββββββββββββββββββββ€
β OS (I/O multiplexing) β
β ββ epoll (Linux), kqueue (macOS), β
β IOCP (Windows), io_uring (Linux 5.1+) β
ββββββββββββββββββββββββββββββββββββββββββββββββ
Runtime Comparison
Runtime Language Model Throughput Node.js (libuv) JavaScript Single-threaded event loop 50K req/s asyncio Python Event loop + thread pool 10K req/s Tokio Rust Work-stealing multi-thread 100K+ req/s Zio Scala Fiber-based 200K+ req/s
7. Layer C4 β Actor Model
Feature Description Actor Primitive unit: state + behavior + mailbox Communicate Only via asynchronous messages Create Spawn new actors Supervision Parent monitors children Location Transparency Actors can be local or remote
Platform Language Key Features BEAM (Erlang/Elixir) Erlang, Elixir Per-actor heap, preemptive scheduler Akka Scala/Java JVM-based, typed actors Orleans C# Virtual actors (grains) Pony Pony Reference capability system (no data race by compile time) Proto.Actor .NET, Go Multi-language actor framework
8. Layer C5 β Consensus Protocol
Protocol Fault Tolerance Performance Use Case 2PC Crash (non-Byzantine) Low Distributed transaction Paxos Crash (N/2+1) High State machine replication (Google Spanner, Kafka) Raft Crash (N/2+1) High (simpler) etcd, Consul, TiKV PBFT Byzantine (N/3+1) Medium (O(nΒ²)) Blockchain, Hyperledger HotStuff Byzantine (N/3+1) High (O(n)) Diem/Libra Snowman (Avalanche) Byzantine High Avalanche subnet Tendermint Byzantine (N/3+1) Medium Cosmos chain
9. Trade-off Matrix
Layer Complexity Scaling Failure Model Typical Latency C0 (Thread) Low Core count Process crash Sub-Β΅s C1 (Lock) Medium Degrade with contention Deadlock Β΅s C2 (Lock-Free) High Good Starvation ns-Β΅s C3 (Async) Medium 10K-100K tasks Runtime failure Β΅s-ms C4 (Actor) High 1M+ actors per node Supervisor restart Β΅s-ms C5 (Consensus) Very high Network bound Partition tolerance ms-s
10. Analogi Lintas Domain
Layer Analogi Kimia Analogi Hukum C0 Reaksi paralel Sidang majelis hakim (3 hakim concurrent) C1 Katalisator (locks onto substrate) Eksklusivitas paten C2 Katalis enzim tanpa inhibitor Self-executing treaty C3 Katalis heterogen (menunggu permukaan) Sidang adjourn (menunggu saksi) C4 Sel biologi independen (komunikasi sinyal) Otonomi daerah (message = regulation) C5 Reaksi rantai (chain reaction) Pengadilan internasional (consensus antar negara)
11. Cross-Reference ke Vault
References
Herlihy, M. & Shavit, N. βThe Art of Multiprocessor Programming.β Morgan Kaufmann, 2012.
Goetz, B. et al. βJava Concurrency in Practice.β Addison-Wesley, 2006.
Hewitt, C. et al. βA Universal Modular ACTOR Formalism for Artificial Intelligence.β 1973.
Armstrong, J. βProgramming Erlang.β Pragmatic Bookshelf, 2007.
Lamport, L. βTime, Clocks, and the Ordering of Events in a Distributed System.β CACM 1978.
Lamport, L. βThe Part-Time Parliament (Paxos).β ACM TOCS 1998.
Ongaro, D. & Ousterhout, J. βIn Search of an Understandable Consensus Algorithm (Raft).β USENIX 2014.
Castro, M. & Liskov, B. βPractical Byzantine Fault Tolerance.β OSDI 1999.
Hoare, C.A.R. βCommunicating Sequential Processes (CSP).β 1978.
Wait, D. βlock-free linked lists.β 2003.
McKenney, P. βRCU (Read-Copy-Update).β Linux Foundation.