Skip to content

[redis] Auto-set maxmemory from resource limits#2339

Open
officialasishkumar wants to merge 1 commit intocozystack:mainfrom
officialasishkumar:feat/redis-auto-maxmemory
Open

[redis] Auto-set maxmemory from resource limits#2339
officialasishkumar wants to merge 1 commit intocozystack:mainfrom
officialasishkumar:feat/redis-auto-maxmemory

Conversation

@officialasishkumar
Copy link
Copy Markdown

@officialasishkumar officialasishkumar commented Apr 6, 2026

What this PR does

Automatically sets Redis maxmemory to 80% of the pod's memory resource limit. This prevents OOM kills by ensuring Redis respects its container memory budget, leaving 20% headroom for replication buffers, the exporter sidecar, and Redis internal overhead.

How it works:

  • Resolves the effective memory limit from either the explicit resources.memory parameter or the selected resourcesPreset using the existing cozy-lib.resources.defaultingSanitize and toFloat helpers
  • Computes maxmemory as memoryLimitBytes * 4 / 5 (integer arithmetic, equivalent to 80%)
  • Injects the value into spec.redis.customConfig as maxmemory <bytes>

Override: Users can set an explicit maxmemory value (in bytes) to bypass the auto-computation.

Examples by preset:

Preset Memory Limit Computed maxmemory
nano 128Mi ~102Mi
micro 256Mi ~204Mi
small 512Mi ~409Mi
medium 1Gi ~819Mi
large 2Gi ~1.6Gi

Fixes #2155

Release note

[redis] Automatically set maxmemory to 80% of the memory resource limit to prevent OOM kills. Override with the new maxmemory parameter.

Summary by CodeRabbit

  • New Features
    • Added configurable maxmemory parameter for Redis memory limit management
    • When not specified, memory limit automatically defaults to 80% of allocated resources

Automatically compute Redis maxmemory as 80% of the pod memory resource
limit (4/5 of the limit in bytes). This prevents OOM kills by ensuring
Redis stays within its allocated memory, leaving 20% headroom for
replication buffers, the exporter sidecar, and Redis internal overhead.

The maxmemory value is derived from the resolved resources (either the
explicit resources parameter or the selected resourcesPreset) using
cozy-lib.resources.defaultingSanitize and toFloat helpers.

Users can override the auto-computed value by setting the new maxmemory
parameter explicitly.

Fixes cozystack#2155

Signed-off-by: Asish Kumar <[email protected]>
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. enhancement New feature or request labels Apr 6, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 6, 2026

📝 Walkthrough

Walkthrough

The Redis Helm chart now includes automatic maxmemory configuration. When explicitly set, the provided value is used; otherwise, the chart computes it as 80% of the pod's memory resource limit. Changes include the new values parameter, schema definition, and template logic for conditional configuration injection.

Changes

Cohort / File(s) Summary
Configuration & Schema
packages/apps/redis/values.yaml, packages/apps/redis/values.schema.json
Added new maxmemory parameter (string type, default empty) to chart values and schema with documentation describing auto-computation behavior.
Template Implementation
packages/apps/redis/templates/redisfailover.yaml
Implemented conditional logic in customConfig to inject maxmemory: uses explicit value if provided, otherwise computes 80% of memory resource limit from resource preset or limits.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A hop, a skip, through memory's flow,
Redis now knows just how high to go,
Eighty percent of the limit, so neat,
No more OOM surprises—the tale's complete! 🍃

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly and concisely summarizes the main change: automatically setting Redis maxmemory based on resource limits.
Linked Issues check ✅ Passed The PR fully addresses all acceptance criteria from issue #2155: auto-sets maxmemory from resource limits with 80% headroom, supports both resources and resourcesPreset, and allows user override via explicit parameter.
Out of Scope Changes check ✅ Passed All changes are directly aligned with issue #2155 objectives: three files modified (redisfailover.yaml, values.schema.json, values.yaml) implement the auto-computed maxmemory feature without unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces an optional 'maxmemory' configuration for Redis, which defaults to 80% of the memory resource limit if not explicitly set. The review feedback suggests refining the conditional logic in the template to correctly handle a value of '0', updating the JSON schema to support both integer and string types for the 'maxmemory' parameter to improve usability, and ensuring the documentation reflects these supported types.

- appendonly no
- save ""
{{- end }}
{{- if .Values.maxmemory }}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using if .Values.maxmemory will evaluate to false if the value is 0, causing the auto-computation to trigger even when an explicit 0 (unlimited) is desired. Comparing against an empty string is more robust for optional parameters that can accept numeric zero.

      {{- if ne (toString .Values.maxmemory) "" }}

},
"maxmemory": {
"description": "Override the auto-computed maxmemory (bytes). When omitted, maxmemory is set to 80% of the memory resource limit.",
"type": "string",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The maxmemory parameter is currently restricted to a string type. Allowing both integer and string (via anyOf) improves usability by allowing users to provide raw byte counts without quotes, while still supporting strings with units (e.g., '100mb'). This is consistent with how other quantity fields like size are defined in this schema.

Suggested change
"type": "string",
"anyOf": [
{ "type": "integer" },
{ "type": "string" }
],

## @section Application-specific parameters
##

## @param {string} [maxmemory] - Override the auto-computed maxmemory (bytes). When omitted, maxmemory is set to 80% of the memory resource limit.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Update the parameter type documentation to reflect that both strings and integers are supported for the maxmemory override.

## @param {string|int} [maxmemory] - Override the auto-computed maxmemory (bytes). When omitted, maxmemory is set to 80% of the memory resource limit.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dd44c569e6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

},
"maxmemory": {
"description": "Override the auto-computed maxmemory (bytes). When omitted, maxmemory is set to 80% of the memory resource limit.",
"type": "string",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Allow numeric maxmemory overrides in values schema

Defining maxmemory as only "type": "string" makes common byte-style overrides fail schema validation when users provide an unquoted number in values.yaml (e.g., maxmemory: 536870912), even though the template itself can render numeric values. This effectively breaks the new override path for a natural input format; the schema should accept integers (or int-or-string) for this field.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/apps/redis/templates/redisfailover.yaml (1)

67-73: Reuse resolved resources instead of recalculating inside customConfig.

Line 70 recomputes defaultingSanitize even though the same effective resources are already rendered earlier. Caching once improves readability and avoids future drift.

♻️ Suggested refactor
 apiVersion: databases.spotahome.com/v1
 kind: RedisFailover
 metadata:
   name: {{ .Release.Name }}
@@
 spec:
+  {{- $resolvedResources := include "cozy-lib.resources.defaultingSanitize" (list .Values.resourcesPreset .Values.resources $) | fromYaml }}
   sentinel:
     replicas: 3
-    resources: {{- include "cozy-lib.resources.defaultingSanitize" (list .Values.resourcesPreset .Values.resources $) | nindent 6 }}
+    resources: {{- $resolvedResources | toYaml | nindent 6 }}
   redis:
     image: "redis:{{ include "redis.versionMap" $ }}"
-    resources: {{- include "cozy-lib.resources.defaultingSanitize" (list .Values.resourcesPreset .Values.resources $) | nindent 6 }}
+    resources: {{- $resolvedResources | toYaml | nindent 6 }}
@@
       {{- if .Values.maxmemory }}
       - maxmemory {{ .Values.maxmemory }}
       {{- else }}
-      {{- $resolved := include "cozy-lib.resources.defaultingSanitize" (list .Values.resourcesPreset .Values.resources $) | fromYaml }}
-      {{- $memLimitBytes := include "cozy-lib.resources.toFloat" $resolved.limits.memory | float64 | int64 }}
+      {{- $memLimitBytes := include "cozy-lib.resources.toFloat" $resolvedResources.limits.memory | float64 | int64 }}
       - maxmemory {{ div (mul $memLimitBytes 4) 5 }}
       {{- end }}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/apps/redis/templates/redisfailover.yaml` around lines 67 - 73, The
template currently recomputes the sanitized resources inside the customConfig
block by calling include "cozy-lib.resources.defaultingSanitize" again and
deriving $memLimitBytes; instead, reuse the previously-resolved resources object
(e.g. the existing $resolved or a renamed shared variable) and compute maxmemory
from that single resolved.limits.memory to avoid duplication and drift; update
the customConfig block to reference the shared $resolved and remove the second
include and $memLimitBytes calculation so maxmemory uses div (mul (include
"cozy-lib.resources.toFloat" $resolved.limits.memory | float64 | int64) 4) 5 (or
equivalent) with the shared variable.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/apps/redis/templates/redisfailover.yaml`:
- Around line 67-73: The template currently recomputes the sanitized resources
inside the customConfig block by calling include
"cozy-lib.resources.defaultingSanitize" again and deriving $memLimitBytes;
instead, reuse the previously-resolved resources object (e.g. the existing
$resolved or a renamed shared variable) and compute maxmemory from that single
resolved.limits.memory to avoid duplication and drift; update the customConfig
block to reference the shared $resolved and remove the second include and
$memLimitBytes calculation so maxmemory uses div (mul (include
"cozy-lib.resources.toFloat" $resolved.limits.memory | float64 | int64) 4) 5 (or
equivalent) with the shared variable.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e18f094e-ef70-47ca-a4c6-c78786a7d7da

📥 Commits

Reviewing files that changed from the base of the PR and between 38624b7 and dd44c56.

📒 Files selected for processing (3)
  • packages/apps/redis/templates/redisfailover.yaml
  • packages/apps/redis/values.schema.json
  • packages/apps/redis/values.yaml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redis: auto-set maxmemory from resource limits

1 participant