perf: batch cache (and some misc) optimizations#120
Draft
Conversation
Instead of filtering a single table by `api_request_started_at` on every
`put/2`, use two separate Etso tables: PendingLoggerEvent for events
waiting to be sent, and InFlightLoggerEvent for events currently being
posted to the API. This avoids scanning and sorting all pending entries
on each insert. The pending count is now retrieved via `:ets.info/2`
which is O(1).
Benchmark (1000x HttpBackend.handle_event/2):
Before After Change
ips 19.01 29.38 +55%
average 52.62 ms 34.04 ms -35%
memory 79.01 MB 25.97 MB -67%
reductions 4.74 M 2.21 M -53%
Use insert_all for pending-to-in-flight moves (one ETS call vs N) and
delete_all for clear (O(1) via ets:delete_all_objects). Pre-generate
in-flight IDs to avoid re-reading the table after insert.
To safely use these bulk operations, introduce a BatchServer GenServer
that serializes flush/clear/reset. put/2 still writes directly to ETS.
This also fixes a correctness issue where LogflareLogger.log/3 could
trigger concurrent flushes from arbitrary processes.
Benchmark (1000x HttpBackend.handle_event/2):
Before After Change
ips 29.38 41.26 +40%
average 34.04 ms 24.24 ms -29%
memory 25.97 MB 18.23 MB -30%
reductions 2.21 M 1.44 M -35%
Combine the read and reset into a single GenServer call so in-flight events are moved back to pending atomically instead of two separate round-trips.
In-flight events were only recovered for the HTTP backend. By handling recovery at the BatchServer level it works for LogflareLogger too.
Avoids intermediate list allocation by combining the map and collect steps into a single Map.new/2 call.
The result is discarded (only used for encodability check), so skip the unnecessary IO.iodata_to_binary step.
Build ISO8601 binary directly from erlang datetime tuple components instead of going through NaiveDateTime.from_erl!/to_iso8601.
v0idpwn
commented
Apr 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently,
BatchCache.put/2always reads all pending messages and sorts them to detect overflow and discard the oldest messages. Since pending and in-flight messages are in the same table, it must filter to select only the pending ones.This PR avoids the
ets:selecton every iteration by splitting pending and in-flight into two different tables. This allows us to use the size of the pending mesages ETS instead of selecting messages every time. I've considered a few different approaches (like using counters) but I felt like this was more straightforward.When the batch is overflowing, it still runs the select and sort. So in case of a Logflare outage, #115 still sort of applies. But this should improve the performance when logflare is operational. In my tests, with a batch size of 100, the performance more than doubled, and memory was also decreased. I have added some stats in the commits. Notice that after I introduced the BatchServer, memory measurements must be done differently.
Some notes: