Concurrency di Go Dari Basic hingga Advanced
Daftar Isi
1. Pengenalan Concurrency
Apa itu Concurrency?
Concurrency adalah kemampuan program untuk menangani beberapa tugas secara bersamaan (concurrent), bukan paralel. Perbedaannya:
Concurrency: Menangani banyak tugas dalam periode waktu yang sama (dealing with lots of things at once)
Parallelism: Menjalankan banyak tugas secara simultan (doing lots of things at once)
Mengapa Go Unggul dalam Concurrency?
Goroutines: Lightweight threads yang dikelola oleh Go runtime
Channels: Komunikasi antar goroutines yang aman
Built-in scheduler: Mengelola goroutines secara efisien
Memory footprint kecil: Goroutine hanya ~2KB vs thread OS ~1MB
Konsep Dasar
2. Goroutines - Basic
Apa itu Goroutine?
Goroutine adalah fungsi atau method yang berjalan secara concurrent dengan fungsi lain. Sangat ringan dan efisien.
Hands-On: Goroutine Sederhana
Output:
Hands-On: Anonymous Goroutines
⚠️ Common Pitfall: Closure dan Loop Variables
3. Channels - Basic
Apa itu Channel?
Channel adalah mekanisme komunikasi antar goroutines. Seperti "pipa" untuk mengirim dan menerima data.
Prinsip: "Don't communicate by sharing memory; share memory by communicating."
Hands-On: Channel Dasar
Hands-On: Channel sebagai Synchronization
Channel Operations
Hands-On: Ping-Pong dengan Channels
4. Channel Operations
Closing Channels
Range over Channels
Hands-On: Producer-Consumer Pattern
5. Select Statement
Apa itu Select?
Select memungkinkan goroutine menunggu multiple channel operations. Seperti switch tapi untuk channels.
Hands-On: Basic Select
Hands-On: Select dengan Default
Hands-On: Timeout Pattern
Hands-On: Ticker & Timer
6. Buffered Channels
Perbedaan Unbuffered vs Buffered
Unbuffered: Send blocks sampai ada yang receive
Buffered: Send blocks hanya jika buffer penuh
Hands-On: Buffered Channels
Hands-On: Channel Capacity & Length
Use Case: Semaphore Pattern
7. WaitGroups
Apa itu WaitGroup?
WaitGroup digunakan untuk menunggu collection of goroutines selesai.
Hands-On: Basic WaitGroup
Hands-On: WaitGroup dengan Error Handling
8. Mutex & RWMutex
Apa itu Mutex?
Mutex (Mutual Exclusion) melindungi shared data dari race conditions.
Race Condition Demo
Hands-On: Mutex Solution
Hands-On: RWMutex (Read-Write Mutex)
Best Practices
9. Atomic Operations
Apa itu Atomic?
Atomic operations adalah operasi yang tidak bisa di-interrupt. Lebih cepat dari Mutex untuk operasi sederhana.
Hands-On: Atomic Counter
Hands-On: Atomic Operations Lengkap
Hands-On: Atomic Value (Any Type)
Performance Comparison
10. Worker Pool Pattern
Konsep Worker Pool
Worker pool adalah pattern dimana sejumlah goroutines (workers) memproses jobs dari shared queue.
Hands-On: Basic Worker Pool
Hands-On: Advanced Worker Pool dengan Error Handling
11. Pipeline Pattern
Konsep Pipeline
Pipeline adalah series of stages yang terhubung oleh channels, dimana setiap stage adalah group of goroutines yang menjalankan fungsi yang sama.
Hands-On: Simple Pipeline
Hands-On: Data Processing Pipeline
Hands-On: Pipeline dengan Cancellation
12. Fan-Out Fan-In Pattern
Konsep Fan-Out Fan-In
Fan-Out: Multiple goroutines membaca dari channel yang sama Fan-In: Multiple goroutines menulis ke channel yang sama
Hands-On: Fan-Out Fan-In
Hands-On: Advanced Fan-Out dengan Load Balancing
13. Context Package
Apa itu Context?
Context digunakan untuk:
Cancellation signals
Deadlines & timeouts
Request-scoped values
Hands-On: Context dengan Cancellation
Hands-On: Context dengan Timeout
Hands-On: Context dengan Deadline
Hands-On: Context Values
Hands-On: Real-World HTTP Request dengan Context
14. Rate Limiting
Konsep Rate Limiting
Rate limiting membatasi jumlah operasi per unit waktu.
Hands-On: Simple Rate Limiter dengan Ticker
Hands-On: Bursty Rate Limiter
Hands-On: Token Bucket Rate Limiter
Hands-On: Rate Limiter dengan golang.org/x/time/rate
15. Error Handling dalam Concurrency
Challenge dalam Error Handling
Goroutines tidak bisa return error secara langsung ke caller.
Hands-On: Error Handling dengan Channels
Hands-On: errgroup Package Pattern
Hands-On: Panic Recovery dalam Goroutines
Hands-On: Timeout Pattern dengan Error
16. Advanced Patterns
16.1 Semaphore Pattern
16.2 Future/Promise Pattern
16.3 Bounded Parallelism
16.4 Or-Channel Pattern
16.5 Tee-Channel Pattern
16.6 Bridge-Channel Pattern
16.7 Or-Done Channel Pattern
16.8 Replicated Requests Pattern
16.9 Queuing Pattern
16.10 Heartbeat Pattern
Best Practices & Common Pitfalls
✅ Best Practices
Always close channels from sender side
Use defer untuk unlock mutex
Pass channels sebagai parameter, bukan variable
Gunakan context untuk cancellation
Buffer channels ketika producer/consumer independent
❌ Common Pitfalls
Goroutine Leak
Closing channel multiple times
Sending to closed channel
Data race
Forgetting WaitGroup.Done()
Performance Tips
1. Profile Concurrent Code
2. Right-size Worker Pools
3. Use Buffered Channels Wisely
4. Prefer Atomic for Simple Counters
5. Context Overhead
Testing Concurrent Code
Test dengan Race Detector
Test dengan Timeout
Resources & Further Learning
Official Documentation
Go Concurrency Patterns: https://go.dev/blog/pipelines
Effective Go: https://go.dev/doc/effective_go#concurrency
Go Memory Model: https://go.dev/ref/mem
Books
"Concurrency in Go" by Katherine Cox-Buday
"The Go Programming Language" by Donovan & Kernighan
Tools
go test -race: Race detectorgo tool trace: Execution tracerpprof: Performance profiler
Summary
Key Takeaways
Goroutines: Lightweight, efficient concurrency primitives
Channels: Safe communication between goroutines
Select: Multiplexing channel operations
Sync Package: Low-level synchronization (Mutex, WaitGroup, etc.)
Context: Cancellation, timeouts, and request-scoped values
Patterns: Worker pools, pipelines, fan-out/fan-in
When to Use What
Share memory
Mutex, RWMutex
Simple counter
Atomic operations
Communicate data
Channels
Wait for goroutines
WaitGroup
Limit concurrency
Semaphore, Buffered channel
Cancellation
Context
Timeout
Context.WithTimeout, time.After
Remember
"Don't communicate by sharing memory; share memory by communicating."
Selamat belajar concurrency di Go!
Last updated