CounterEx

High-performance counter library with pluggable backends and namespace support.

Build StatusHex.pmLicense

CounterEx provides atomic counter operations with three backend options:

Features

Installation

Add counter_ex to your list of dependencies in mix.exs:

def deps do
  [
    {:counter_ex, "~> 0.2.0"}
  ]
end

Quick Start

# Add to your supervision tree
def start(_type, _args) do
  children = [
    {CounterEx, []}  # Uses ETS backend by default
  ]

  Supervisor.start_link(children, strategy: :one_for_one)
end

# Basic operations
{:ok, 1} = CounterEx.inc(:my_counter)
{:ok, 2} = CounterEx.inc(:my_counter)
{:ok, 2} = CounterEx.get(:my_counter)

# Use namespaces to organize counters
{:ok, 1} = CounterEx.inc(:http, :requests)
{:ok, 1} = CounterEx.inc(:http, :errors)
{:ok, 1} = CounterEx.inc(:db, :queries)

# Get all counters in a namespace
{:ok, counters} = CounterEx.all(:http)
# => %{requests: 1, errors: 1}

Backend Selection

ETS (Default) - Best for Dynamic Counters

{CounterEx, backend: CounterEx.Backend.ETS}

Use when:

Performance: ~256K inc/s, ~270K get/s (4 workers on M4 Max)

Atomics - Best for Maximum Performance

{CounterEx,
  backend: CounterEx.Backend.Atomics,
  backend_opts: [capacity: 10_000]
}

Use when:

Performance: ~253K inc/s, ~291K get/s (4 workers on M4 Max)

Counters - Best for Write-Heavy Workloads

{CounterEx,
  backend: CounterEx.Backend.Counters,
  backend_opts: [capacity: 5_000]
}

Use when:

Performance: ~248K inc/s, ~269K get/s (4 workers on M4 Max)

See the Backend Comparison Guide for detailed comparison.

API Overview

Basic Operations

# Increment
{:ok, 1} = CounterEx.inc(:counter)
{:ok, 11} = CounterEx.inc(:counter, 10)
{:ok, 105} = CounterEx.inc(:counter, 5, 100)  # with default value

# Get
{:ok, 105} = CounterEx.get(:counter)
{:ok, nil} = CounterEx.get(:nonexistent)

# Set
{:ok, 42} = CounterEx.set(:counter, 42)

# Reset
{:ok, 0} = CounterEx.reset(:counter)
{:ok, 10} = CounterEx.reset(:counter, 10)

# Delete
:ok = CounterEx.delete(:counter)

Namespace Operations

# Increment in namespace
{:ok, 1} = CounterEx.inc(:metrics, :requests, 1, 0)

# Get from namespace
{:ok, 1} = CounterEx.get(:metrics, :requests)

# Get all counters in namespace
{:ok, %{requests: 1, errors: 0}} = CounterEx.all(:metrics)

# Delete entire namespace
:ok = CounterEx.delete_namespace(:metrics)

Advanced Operations

# Compare-and-swap
{:ok, 10} = CounterEx.set(:counter, 10)
{:ok, 20} = CounterEx.compare_and_swap(:counter, 10, 20)  # succeeds
{:error, :mismatch, 20} = CounterEx.compare_and_swap(:counter, 10, 30)  # fails

# Get backend info
{:ok, info} = CounterEx.info()
# %{
#   type: :ets,
#   counters_count: 42,
#   namespaces: [:default, :http, :db],
#   ...
# }

Use Cases

HTTP Request Tracking

defmodule MyApp.Metrics do
  def track_request(status) do
    CounterEx.inc(:http, :total_requests)

    case status do
      s when s in 200..299 -> CounterEx.inc(:http, :success)
      s when s in 400..499 -> CounterEx.inc(:http, :client_errors)
      s when s in 500..599 -> CounterEx.inc(:http, :server_errors)
    end
  end

  def get_metrics do
    CounterEx.all(:http)
  end
end

Rate Limiting

defmodule MyApp.RateLimiter do
  @max_requests 100

  def check_rate_limit(user_id) do
    key = :"user_#{user_id}"

    case CounterEx.get(:rate_limits, key) do
      {:ok, count} when count >= @max_requests ->
        {:error, :rate_limit_exceeded}

      {:ok, _count} ->
        {:ok, _} = CounterEx.inc(:rate_limits, key)
        :ok

      {:ok, nil} ->
        {:ok, _} = CounterEx.inc(:rate_limits, key, 1, 0)
        :ok
    end
  end
end

Configuration

Periodic Sweep

Clear all counters periodically:

{CounterEx, interval: 60_000}  # Clear every 60 seconds

Multiple Counter Instances

Run multiple instances with different backends:

children = [
  # Fast metrics with Atomics
  {CounterEx, name: :metrics, backend: CounterEx.Backend.Atomics, backend_opts: [capacity: 1000]},

  # Dynamic user counters with ETS
  {CounterEx, name: :users, backend: CounterEx.Backend.ETS}
]

Performance

Benchmarks on Apple M4 Max (16 cores, 64GB RAM) with 4 parallel workers:

Backend Operation Throughput Notes
Atomics get() 291K ops/s Fastest reads
Atomics inc() 253K ops/s Best overall
Atomics set() 257K ops/s
ETS get() 270K ops/s
ETS inc() 256K ops/s Most flexible
ETS set() 262K ops/s
Counters get() 269K ops/s
Counters inc() 248K ops/s Write-optimized
Counters set() 256K ops/s

System Configuration:

Run your own benchmarks:

CounterEx.benchmark(parallel: 4)

Telemetry

CounterEx emits telemetry events for observability:

:telemetry.attach(
  "counter-handler",
  [:counter_ex, :backend, :ets, :inc],
  &handle_event/4,
  nil
)

def handle_event([:counter_ex, :backend, :ets, :inc], measurements, metadata, _config) do
  # measurements: %{count: 1}
  # metadata: %{namespace: :http, key: :requests, step: 1}
end

Events emitted:

Migration from 0.1.x

See the Migration Guide for upgrading from version 0.1.x.

Key Changes:

Documentation

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

Copyright 2025 ยฉ nyo16

Licensed under the Apache License, Version 2.0. See LICENSE.md for details.