Skip to content

perf(3scale_batcher): filter by keys to avoid expensive shared dict read - #1615

Open
tkan145 wants to merge 1 commit into
3scale:masterfrom
tkan145:THREESCALE-16640-part-2
Open

tkan145 wants to merge 1 commit into
3scale:masterfrom
tkan145:THREESCALE-16640-part-2

Conversation

@tkan145

@tkan145 tkan145 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

What

Previously, we need to fetches all keys from the shared dict, then loop through each key and retrieve each key from the shared_dict and discard all keys that does not match service_id. This PR will filter by keys to avoid expensive shared dict read.

Benchmark

Benchmark script

Details
local root = require('pl.path').currentdir()
package.path = root .. '/?.lua;' ..
               root .. '/gateway/src/?.lua;' ..
               root .. '/gateway/src/apicast/policy/3scale_batcher/?.lua;' ..
               package.path

local monotonic_msec = require("resty.core.time").monotonic_msec
local update_time = ngx.update_time
local print = print
local Usage = require('apicast.usage')
local s, e
---------------------------------------------------------------------------
-- Benchmark: reports_batcher add/get_all, scaled by number of services
-- sharing the 'batched_reports' shdict.
--
-- This isolates the Lua-level cost of ReportsBatcher:get_all() from
-- end-to-end request latency (dominated by the backend auth round trip,
-- which swamps anything measurable here). It shows how get_all()'s cost for
-- ONE service grows as OTHER services accumulate keys in the same shared
-- dict, since storage:get_keys() has no way to filter by prefix and returns
-- every key in the dict regardless of which service is being flushed.
---------------------------------------------------------------------------
print('\n--- reports_batcher add/get_all (shared dict scan cost) ---')

local ReportsBatcher = require('apicast.policy.3scale_batcher.reports_batcher')
local Transaction = require('apicast.policy.3scale_batcher.transaction')

local GET_ALL_ITERATIONS = 1000
local CREDS_PER_SERVICE = 50
local METRICS_PER_CRED = 3

local function add_reports_for(reports_batcher, service_id, creds_per_service, metrics_per_cred)
  for c = 1, creds_per_service do
    local creds = { user_key = 'user_' .. service_id .. '_' .. c }
    local usage = Usage.new()
    for m = 1, metrics_per_cred do
      usage:add('metric_' .. m, 1)
    end
    reports_batcher:add(Transaction.new(service_id, creds, usage))
  end
end

local function bench_get_all(num_other_services)
  ngx.shared.batched_reports:flush_all()
  ngx.shared.batched_reports:flush_expired()

  local reports_batcher = ReportsBatcher.new(
    ngx.shared.batched_reports, 'batched_reports_locks')

  local target_service = 'svc_target'

  -- Other services' pending reports that get_all(target_service) has to
  -- scan past and discard on every call.
  for s = 1, num_other_services do
    add_reports_for(reports_batcher, 'svc_other_' .. s, CREDS_PER_SERVICE, METRICS_PER_CRED)
  end

  update_time()
  s = monotonic_msec()
  for _ = 1, GET_ALL_ITERATIONS do
    -- get_all() deletes the target service's keys as it collects them, so
    -- repopulate before each call to keep the scan size constant.
    add_reports_for(reports_batcher, target_service, CREDS_PER_SERVICE, METRICS_PER_CRED)
    reports_batcher:get_all(target_service)
  end
  update_time()
  e = monotonic_msec()

  print(string.format(
    '  get_all (own creds=%d, other services=%d, total keys~=%d): ',
    CREDS_PER_SERVICE, num_other_services,
    CREDS_PER_SERVICE * METRICS_PER_CRED * (num_other_services + 1)),
    e - s, ' ms for ', GET_ALL_ITERATIONS, ' calls')
end

bench_get_all(0)
bench_get_all(10)
bench_get_all(50)
bench_get_all(200)

Run with

resty --shdict 'batched_reports 50m' --shdict 'batched_reports_locks 1m' benchmark/3scale_batcher.lua

Results

Before

--- reports_batcher add/get_all (shared dict scan cost) ---
  get_all (own creds=50, other services=0, total keys~=150): 285 ms for 1000 calls
  get_all (own creds=50, other services=10, total keys~=1650): 1104 ms for 1000 calls
  get_all (own creds=50, other services=50, total keys~=7650): 1284 ms for 1000 calls
  get_all (own creds=50, other services=200, total keys~=30150): 1476 ms for 1000 calls

After

--- reports_batcher add/get_all (shared dict scan cost) ---
  get_all (own creds=50, other services=0, total keys~=150): 297 ms for 1000 calls
  get_all (own creds=50, other services=10, total keys~=1650): 220 ms for 1000 calls
  get_all (own creds=50, other services=50, total keys~=7650): 223 ms for 1000 calls
  get_all (own creds=50, other services=200, total keys~=30150): 226 ms for 1000 calls

@tkan145
tkan145 requested a review from a team as a code owner November 27, 2026 07:09
@tkan145
tkan145 force-pushed the THREESCALE-16640-part-2 branch from df698c7 to 7c46e69 Compare November 30, 2026 04:12
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.

1 participant