Pattern trong các hệ phân tán
Các hệ thống nhắn tin và giao dịch throughput cao đẩy các pattern throughput tới mức cực hạn.
| Pattern | Dự án | Tại đâu | Tác dụng |
|---|---|---|---|
| Ring Buffer | LMAX Disruptor | RingBuffer.java | Cấu trúc dữ liệu cốt lõi — 6M lệnh/giây tại LMAX Exchange |
| Batch Processing | Apache Kafka | RecordAccumulator.java | Gom record thành lô theo partition để tăng throughput |
| Circuit Breaker | Netflix Hystrix | HystrixCircuitBreaker.java | Circuit breaker 3 trạng thái cho phục hồi microservice |
| Circuit Breaker | Sony gobreaker | gobreaker.go | Circuit breaker Go với phát hiện cũ dựa trên generation |
| Backpressure | Reactive Streams | Subscription.java | Spec kiểm soát luồng kiểu pull request(n) |
| Write-Ahead Log | etcd | wal.go | WAL đồng thuận Raft — nguồn sự thật cho state phân tán |
| Write-Ahead Log | PostgreSQL | xlog.c | WAL transaction để khôi phục crash, replication, PITR |
| MVCC | PostgreSQL | heapam_visibility.c | HeapTupleSatisfiesMVCC — kiểm tra hiển thị cô lập theo snapshot |
| MVCC | etcd | kvstore.go | Kho key-value đa phiên bản chạy config Kubernetes |
| Consistent Hashing | groupcache | consistenthash.go | Vòng hash với virtual replica cho cache phân tán |
| Actor Model | Akka | Actor.scala | trait Actor — concurrency hướng thông điệp cho JVM |
| Actor Model | Erlang/OTP | erl_process.h | Struct process BEAM VM — actor nhẹ với mailbox |
| Rate Limiter | Nginx | ngx_http_limit_req_module.c | Leaky bucket rate limiting cho request HTTP |
| Logical Clock | etcd | mvcc/revision.go | Bộ đếm revision tăng dần để sắp xếp sự kiện qua cluster |
| Logical Clock | LevelDB | db_impl.cc sequence number | Sequence number sắp xếp mọi thao tác ghi không cần wall-clock |
| Retry Backoff | Kubernetes | backoff.go | Backoff khởi động lại pod, retry API server với delay tăng cấp số nhân |
| Tombstone | Cassandra | Tombstone markers | Marker xoá trong việc lan truyền xoá phân tán |
| LSM Tree | LevelDB | db_impl.cc | Đệm ghi trong bộ nhớ, flush thành file đã sắp xếp, compaction ở background |
| Checkpointing | PostgreSQL | checkpointer.c | Snapshot state định kỳ giới hạn thời gian replay WAL khi khôi phục |
Cách chúng kết hợp: Một thao tác ghi phân tán
Khi client ghi một key vào database phân tán như etcd, các pattern móc nối nhau qua toàn bộ đường đi:
Client: PUT /key "value"▼The gateway applies a token bucket to prevent any single client from overwhelming the cluster.
The router determines which node owns this key. Virtual nodes ensure load stays balanced even when nodes join/leave.
Before modifying state, the leader appends the operation to a WAL on disk. If the process crashes, replay recovers state.
The write gets a monotonic revision number. No wall-clock sync needed — all nodes agree on ordering via the revision.
The new version is stored alongside old versions. Concurrent readers see a consistent snapshot without blocking the write.
Periodically, the system takes a snapshot. Future crash recovery replays only the WAL entries after the last checkpoint.
Các pattern tạo thành một pipeline bền vững: rate limit bảo vệ hệ thống, consistent hashing định tuyến request, WAL đảm bảo bền vững, logical clock sắp xếp sự kiện, MVCC cung cấp cô lập và checkpoint giới hạn thời gian khôi phục.