Skip to content

feat(cache): add 100MB MaxSize and sets Discard MaxBytes limit to NATS KV cache buckets#3014

Merged
migmartri merged 2 commits intochainloop-dev:mainfrom
migmartri:feat/cache-maxbytes-limit
Apr 9, 2026
Merged

feat(cache): add 100MB MaxSize and sets Discard MaxBytes limit to NATS KV cache buckets#3014
migmartri merged 2 commits intochainloop-dev:mainfrom
migmartri:feat/cache-maxbytes-limit

Conversation

@migmartri
Copy link
Copy Markdown
Member

@migmartri migmartri commented Apr 9, 2026

Summary

Improves behavior of Nats Cache implementation to leverage DiscardOld capabilities, to do that we needed to

  • Add a 100MB MaxBytes cap to both NATS KV cache buckets (attestation bundles and policy evaluation bundles) to prevent unbounded memory growth. When the limit is reached, NATS evicts the oldest entries automatically.
  • Update default behavior to discardOld (discardNew is default in nats upstream configuration for KV)

Cap both attestation bundle and policy evaluation bundle NATS KV
buckets to 100MB to prevent unbounded memory growth. When the limit
is reached, NATS automatically evicts the oldest entries.

Also cleans up the unused maxSize config field by moving the
defaultMaxSize constant directly into the in-memory backend where
it is actually used.

Signed-off-by: Miguel Martinez Trivino <migmartri@gmail.com>
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 5 files

NATS KV hardcodes DiscardNew on the backing stream, which rejects
writes when MaxBytes is reached. For cache use-cases, update the
backing stream to DiscardOld so oldest entries are evicted
automatically to make room for new ones.

Signed-off-by: Miguel Martinez Trivino <migmartri@gmail.com>
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
@migmartri migmartri changed the title feat(cache): add 100MB MaxBytes limit to NATS KV cache buckets feat(cache): add 100MB MaxSize and sets Discard MaxBytes limit to NATS KV cache buckets Apr 9, 2026
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 2 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="pkg/cache/natskv.go">

<violation number="1" location="pkg/cache/natskv.go:81">
P1: Cache init now hard-fails on backing stream info/update errors when MaxBytes is enabled, introducing a new startup compatibility/availability regression.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.

// the oldest entries are evicted automatically to make room for new ones.
if c.cfg.maxBytes > 0 {
streamName := fmt.Sprintf("KV_%s", c.bucket)
stream, err := js.Stream(context.Background(), streamName)
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot Apr 9, 2026

Choose a reason for hiding this comment

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

P1: Cache init now hard-fails on backing stream info/update errors when MaxBytes is enabled, introducing a new startup compatibility/availability regression.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At pkg/cache/natskv.go, line 81:

<comment>Cache init now hard-fails on backing stream info/update errors when MaxBytes is enabled, introducing a new startup compatibility/availability regression.</comment>

<file context>
@@ -72,6 +73,22 @@ func (c *natsKVCache[T]) initBucket() error {
+	// the oldest entries are evicted automatically to make room for new ones.
+	if c.cfg.maxBytes > 0 {
+		streamName := fmt.Sprintf("KV_%s", c.bucket)
+		stream, err := js.Stream(context.Background(), streamName)
+		if err != nil {
+			return fmt.Errorf("cache: failed to get backing stream %s: %w", streamName, err)
</file context>
Fix with Cubic

@migmartri migmartri merged commit ebf5448 into chainloop-dev:main Apr 9, 2026
15 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.

3 participants