Counting Fast in Erlang with :counters and :atomics

· ai · Source ↗

TLDR

  • BEAM’s :atomics and :counters are off-heap, shared integer arrays that outperform ETS for high-concurrency counting workloads.

Key Takeaways

  • :atomics provides CAS, exchange, and atomic add-get mapped to CPU instructions with sequential-consistency guarantees across array cells.
  • :counters in [:write_concurrency] mode shards one logical integer across per-scheduler slots, eliminating write contention at the cost of eventually-consistent reads.
  • Benchmarks on Apple M4 Pro (14 cores, OTP 28, JIT) show :counters throughput scales linearly up to core count; :atomics holds steady; ETS degrades past core count.
  • Broadway’s rate-limiter uses :atomics in production, a concrete real-world anchor for the atomic primitives.
  • :counters.put/3 is expensive: it resets every scheduler’s slot to 0 then writes the new value to one, making bulk sets rare-use operations.

Hacker News Comment Review

  • No substantive HN discussion yet.

Original | Discuss on HN