Skip to content

fix(secret): refresh stale lru cache item in background#12614

Merged
nic-6443 merged 3 commits into
apache:masterfrom
nic-6443:nic/secret-lrucache2
Sep 16, 2025
Merged

fix(secret): refresh stale lru cache item in background#12614
nic-6443 merged 3 commits into
apache:masterfrom
nic-6443:nic/secret-lrucache2

Conversation

@nic-6443

@nic-6443 nic-6443 commented Sep 14, 2025

Copy link
Copy Markdown
Member

Description

Our secret feature includes an LRU cache to store values obtained from the secret provider (vault, AWS, GCP) to reduce the number of requests to the secret provider. However, due to without invalid_stale parameter, this cache never expires.

local new_ssl_value = secret.fetch_secrets(matched_ssl.value, true, matched_ssl.value, "")
or matched_ssl.value

apisix/apisix/secret.lua

Lines 188 to 190 in 9ae2403

local secrets_lrucache = core.lrucache.new({
ttl = 300, count = 512
})
local obj, stale_obj = lru_obj:get(key)
if obj and obj.ver == version then
return obj
end
if not invalid_stale and stale_obj and stale_obj.ver == version then
lru_obj:set(key, stale_obj, item_ttl)
return stale_obj
end

However, if only the invalid_stale parameter is specified, when the cache entry expires, it will be necessary to query the secret provider to obtain the secret while receiving downstream requests. This can result in some high-latency long-tail requests.
To address this, an refresh_stale option is provided in the LRU cache to asynchronously refresh expired items within a timer, reducing the impact on downstream requests.

Which issue(s) this PR fixes:

Fixes #

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@nic-6443
nic-6443 marked this pull request as ready for review September 15, 2025 03:10
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Sep 15, 2025
@nic-6443
nic-6443 merged commit 04bb011 into apache:master Sep 16, 2025
56 of 63 checks passed
@nic-6443
nic-6443 deleted the nic/secret-lrucache2 branch September 16, 2025 12:36
jizhuozhi pushed a commit to jizhuozhi/apisix that referenced this pull request Oct 18, 2025
shreemaan-abhishek pushed a commit to shreemaan-abhishek/apisix that referenced this pull request Jan 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants