Skip to content

Shard hot metric writes by scheduler, and add a stub build - #44

Merged
jessestimpson merged 8 commits into
masterfrom
feature/perf-work-rc
Oct 21, 2026
Merged

jessestimpson merged 8 commits into
masterfrom
feature/perf-work-rc

Conversation

@jkashimba

Copy link
Copy Markdown
Collaborator

Many processes writing the same counter, histogram bucket or exemplar serialized on one ETS row lock, which can saturate every scheduler under load. Writes now go to one row per scheduler, keyed {Key, SchedulerId} and created on first use, and reads combine the shards.

Sharding

  • Counters and histogram bucket counts (new imetrics_hist_counts table) are summed across shards on read. Exemplars are sharded the same way, and get_exemplar/1 returns the one with the newest timestamp.
  • A full scrape reads each table in one scan: get_all/0 for histograms, and the new imetrics:get_exemplars/0 for exemplars in the /metrics exporter.
  • Write-heavy tables use write_concurrency auto, so OTP 25 is now the minimum version. Histogram definitions move to a read_concurrency table.
  • Gauges are not sharded.

Behaviour changes

  • add/add_m return the calling scheduler's shard value, not the total, and histogram add returns {Bucket, ShardCount}.
  • Points within a counter come out sorted by tags.
  • An exemplar set later with an older explicit timestamp is no longer reported over a newer one.
  • Memory and full-read cost grow with series x schedulers.

Stub build
Public API modules are facades over *_impl or no-op *_stub modules, selected by the IMETRICS_STUB define, and imetrics:impl/0 reports which. Stubs return the same shapes, and tick/tock still measure durations. A stub rebar3 profile and CI step run the tests against it.

Also adds imetrics_bench (run on demand through the bench common_test group), shard tests with writers pinned to every scheduler, and README sections on concurrency, trade-offs, benchmarks and the stub build.

Many processes writing the same counter, histogram bucket or exemplar
serialized on one ETS row lock, which can saturate every scheduler under
load. Writes now go to one row per scheduler, keyed {Key, SchedulerId} and
created on first use, and reads combine the shards.

Sharding
- Counters and histogram bucket counts (new imetrics_hist_counts table) are
  summed across shards on read. Exemplars are sharded the same way, and
  get_exemplar/1 returns the one with the newest timestamp.
- A full scrape reads each table in one scan: get_all/0 for histograms, and
  the new imetrics:get_exemplars/0 for exemplars in the /metrics exporter.
- Write-heavy tables use write_concurrency auto, so OTP 25 is now the
  minimum version. Histogram definitions move to a read_concurrency table.
- Gauges are not sharded.

Behaviour changes
- add/add_m return the calling scheduler's shard value, not the total, and
  histogram add returns {Bucket, ShardCount}.
- Points within a counter come out sorted by tags.
- An exemplar set later with an older explicit timestamp is no longer
  reported over a newer one.
- Memory and full-read cost grow with series x schedulers.

Stub build
Public API modules are facades over *_impl or no-op *_stub modules,
selected by the IMETRICS_STUB define, and imetrics:impl/0 reports which.
Stubs return the same shapes, and tick/tock still measure durations. A
`stub` rebar3 profile and CI step run the tests against it.

Also adds imetrics_bench (run on demand through the `bench` common_test
group), shard tests with writers pinned to every scheduler, and README
sections on concurrency, trade-offs, benchmarks and the stub build.
Every counter write converted its name and tags to binaries before
touching ETS: atoms to charlists to binaries, plus three maps. That
was about 380 of the 560 ns an add_m took, and about 1.1 KB of
garbage per call.

Counter rows are now keyed {{Name, Tags}, SchedulerId}, with Name and
Tags stored as given, and converted on read.
- The fast path is ets:update_counter/3 on an existing row. Only the
  first write of a series on a scheduler converts the key, to check it,
  before creating the row, so an invalid key still returns
  {error, {function_clause, check_inputs}} and stores nothing.
- get_counter_points/0 converts each distinct raw key once per read,
  and merges raw keys that read as the same series (v, "v", <<"v">>).
- delete_counter/2 removes every raw form of a series.
- Tuple names and tuple add_m keys are still joined with the separator
  when written, so changing the separator later doesn't rename them.

add_m now takes about 141 ns, 8 reductions and 10 heap words per call,
against 560 ns, 41 and about 138 before (OTP 25.3, one process, dev
machine benchmark).

Histograms and exemplars still convert their keys on write.

Adds shard tests for merged raw keys, invalid keys, delete, and tuple
add_m keys.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 870357f Previous: 14d7f61 Ratio
Metrics throughput (avg between workers) 2268407.655340109 metrics/second 822037.9132002811 metrics/second 2.76

This comment was automatically generated by workflow using github-action-benchmark.

@jessestimpson

Copy link
Copy Markdown
Member

The sharding approach makes sense, but the stub modules make the review very difficult.

If we want a compile-time flag, I think it would be simpler to build that as a feature driven by macros (like logger does) rather than having to swap out module implementations.

To that end, can we back out the stub modules so that we can review the sharding in isolation?

@jkashimba

Copy link
Copy Markdown
Collaborator Author

The sharding approach makes sense, but the stub modules make the review very difficult.

If we want a compile-time flag, I think it would be simpler to build that as a feature driven by macros (like logger does) rather than having to swap out module implementations.

To that end, can we back out the stub modules so that we can review the sharding in isolation?

Totally. I originally did the stub first when I was running down performance issues, then just left it in. Sorry it definitely does make the review more difficult. Per our meeting discussion I'll let you run with that part. Just let me know what you need from me!

@jessestimpson

Copy link
Copy Markdown
Member

Two removals:

  1. Removed stubbed modules. We'll rethink this in the form of imetrics macros instead of a module replacement.
  2. Removed the changes to the binary key construction. I would like to defer this to a separate change. I think it's good, but I want to make sure we can understand the effects of each change in isolation. Sharded keys is the big win, so I want to make sure we can get that applied first.

@jessestimpson

Copy link
Copy Markdown
Member
commit contended ops/s (18 workers) sparse write metrics/s sparse scrape time
master (483337e) 78.7K 2.56M 855 µs
870357f (sharding, stub build, raw keys) 16.4M 2.38M 2,628 µs
HEAD (200e413, sharding only) 13.0M 3.16M 916 µs

@jessestimpson
jessestimpson merged commit ac862ef into master Oct 21, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants