πŸ”„ 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. 1. Premise β€” Concurrency Adalah Norma, Bukan Fitur
  2. 2. Six-Layer Concurrency Hierarchy
  3. 3. Layer C0 β€” Physical Parallelism
  4. 4. Layer C1 β€” Process/Thread
  5. 5. Layer C2 β€” Lock-Based Synchronization
  6. 6. Layer C3 β€” Lock-Free & Wait-Free
  7. 7. Layer C4 β€” Async/Await & Event Loop
  8. 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

PlatformThreads/TimeModel
Single-core1Time-sharing
Multi-core (2-8)2-8Symmetric multiprocessing (SMP)
Many-core (8-64)8-64Non-uniform memory access (NUMA)
GPU (CUDA)10K+SIMT (Single Instruction Multiple Thread)
Array (SIMD)256/512 bitsVectorized instructions (AVX-512)

Kernel vs User Threads

ModelDescriptionExample
1:1 (kernel)Every user thread β†’ kernel threadLinux pthreads, Windows
N:1 (user-space)Many user threads β†’ 1 kernel threadGreen threads (old Java)
M:N (hybrid)M user β†’ N kernelGo goroutines, Erlang processes

4. Layer C1 β€” Lock-Based Synchronization

Primitives

PrimitiveSemanticsTypical Uses
MutexMutual exclusionProtect critical section
SemaphoreCounted signalingResource pool
RWLockReaders–writerRead-heavy workloads
Condition VariableBlock until predicateProducer–consumer
BarrierWait for N threadsParallel phase

Deadlock Prevention

Four necessary conditions (Coffman, 1971):

  1. Mutual exclusion
  2. Hold and wait
  3. No preemption
  4. Circular wait

Break any one to prevent. Practical approach: lock ordering (hierarchical locking).


5. Layer C2 β€” Lock-Free & Wait-Free

LevelGuaranteeTechnique
Obstruction-freeGuaranteed to complete if no contentionCAS loop
Lock-freeSystem makes progress even if thread diesCAS + retry
Wait-freeEvery thread completes in bounded stepsRead, copy, update (RCU)

Hardware Atomics

OperationExample
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 MemoryIntel 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

RuntimeLanguageModelThroughput
Node.js (libuv)JavaScriptSingle-threaded event loop50K req/s
asyncioPythonEvent loop + thread pool10K req/s
TokioRustWork-stealing multi-thread100K+ req/s
ZioScalaFiber-based200K+ req/s

7. Layer C4 β€” Actor Model

FeatureDescription
ActorPrimitive unit: state + behavior + mailbox
CommunicateOnly via asynchronous messages
CreateSpawn new actors
SupervisionParent monitors children
Location TransparencyActors can be local or remote

Platforms

PlatformLanguageKey Features
BEAM (Erlang/Elixir)Erlang, ElixirPer-actor heap, preemptive scheduler
AkkaScala/JavaJVM-based, typed actors
OrleansC#Virtual actors (grains)
PonyPonyReference capability system (no data race by compile time)
Proto.Actor.NET, GoMulti-language actor framework

8. Layer C5 β€” Consensus Protocol

ProtocolFault TolerancePerformanceUse Case
2PCCrash (non-Byzantine)LowDistributed transaction
PaxosCrash (N/2+1)HighState machine replication (Google Spanner, Kafka)
RaftCrash (N/2+1)High (simpler)etcd, Consul, TiKV
PBFTByzantine (N/3+1)Medium (O(nΒ²))Blockchain, Hyperledger
HotStuffByzantine (N/3+1)High (O(n))Diem/Libra
Snowman (Avalanche)ByzantineHighAvalanche subnet
TendermintByzantine (N/3+1)MediumCosmos chain

9. Trade-off Matrix

LayerComplexityScalingFailure ModelTypical Latency
C0 (Thread)LowCore countProcess crashSub-Β΅s
C1 (Lock)MediumDegrade with contentionDeadlockΒ΅s
C2 (Lock-Free)HighGoodStarvationns-Β΅s
C3 (Async)Medium10K-100K tasksRuntime failureΒ΅s-ms
C4 (Actor)High1M+ actors per nodeSupervisor restartΒ΅s-ms
C5 (Consensus)Very highNetwork boundPartition tolerancems-s

10. Analogi Lintas Domain

LayerAnalogi KimiaAnalogi Hukum
C0Reaksi paralelSidang majelis hakim (3 hakim concurrent)
C1Katalisator (locks onto substrate)Eksklusivitas paten
C2Katalis enzim tanpa inhibitorSelf-executing treaty
C3Katalis heterogen (menunggu permukaan)Sidang adjourn (menunggu saksi)
C4Sel biologi independen (komunikasi sinyal)Otonomi daerah (message = regulation)
C5Reaksi rantai (chain reaction)Pengadilan internasional (consensus antar negara)

11. Cross-Reference ke Vault

LayerCatatan Vault Terkait
C0hierarchy-operating-systems β€” Process scheduling
C1-C2hierarchy-programming-language β€” Language concurrency
C3hierarchy-kernel-bypass-networking β€” io_uring async pattern
C4hierarchy-llm-ai-systems β€” Agent orchestration
C5hierarchy-database-storage-systems β€” Consensus in DB
Allhierarchy-abstraction-layers β€” Foundation layers

References

  1. Herlihy, M. & Shavit, N. β€œThe Art of Multiprocessor Programming.” Morgan Kaufmann, 2012.
  2. Goetz, B. et al. β€œJava Concurrency in Practice.” Addison-Wesley, 2006.
  3. Hewitt, C. et al. β€œA Universal Modular ACTOR Formalism for Artificial Intelligence.” 1973.
  4. Armstrong, J. β€œProgramming Erlang.” Pragmatic Bookshelf, 2007.
  5. Lamport, L. β€œTime, Clocks, and the Ordering of Events in a Distributed System.” CACM 1978.
  6. Lamport, L. β€œThe Part-Time Parliament (Paxos).” ACM TOCS 1998.
  7. Ongaro, D. & Ousterhout, J. β€œIn Search of an Understandable Consensus Algorithm (Raft).” USENIX 2014.
  8. Castro, M. & Liskov, B. β€œPractical Byzantine Fault Tolerance.” OSDI 1999.
  9. Hoare, C.A.R. β€œCommunicating Sequential Processes (CSP).” 1978.
  10. Wait, D. β€œlock-free linked lists.” 2003.
  11. McKenney, P. β€œRCU (Read-Copy-Update).” Linux Foundation.