Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/redis_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ The following tables lists the default variables located in `roles/redis/default
| `redis_bind_addr_source` | String | The bind address source. Will default to the Ansible `inventory_hostname` unless explicitly set to `default_ipv4_address`. | `inventory_hostname` |
| `redis_bind_addrs` | String | A space-separated list of hostnames/IP addresses on which Redis listeners will be created. If `redis_bind_ipv6` is set to `true`, `::1` will be added to the addresses. The `redis_bind_addr_source` will also be added to the addresses. | `127.0.0.1` |
| `redis_tls_enabled` | Boolean | Flag to enable TLS connections. | `false` |
| `redis_maxmemory_bytes` | Integer | Maximum memory Redis can use (maxmemory). When set to auto, the installer calculates the value from the system RAM using: `maxmemory = max(redis_maxmemory_min_mb, system_ram × redis_maxmemory_ratio)`. If a numeric value is provided, that value (in bytes) is used directly and the automatic calculation is skipped. | `auto` |
| `redis_maxmemory_ratio` | Float | Define how much memory the system will use. Only work if `redis_maxmemory_bytes` is configured as auto. Default value 0.6 means 60%. | `0.60` |
| `redis_maxmemory_min_mb` | Integer | This parameter defines the minimum amount of memory Redis is allowed to use, even if the automatic calculation would result in a smaller value. It acts as a safety floor for the maxmemory calculation. | `512` |

### Auth Variables

Expand Down
10 changes: 10 additions & 0 deletions roles/redis/defaults/main/redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,13 @@ redis_user_repluser_password: repluser
redis_user_sentineladmin_password: admin
redis_user_sentineluser_password: sentineluser
redis_user_prometheus_password: prometheus

# Redis memory sizing defaults (dedicated Redis node)

# If set to "auto", maxmemory is calculated from system RAM.
# If set to a number (bytes), that value is used verbatim.
redis_maxmemory_bytes: auto

# Used only when redis_maxmemory_bytes == "auto"
redis_maxmemory_ratio: 0.60
redis_maxmemory_min_mb: 512
43 changes: 43 additions & 0 deletions roles/redis/tasks/configure-redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,49 @@
group: root
mode: "0644"

# Calculate the Redis maxmemory value automatically when the parameter
# `redis_maxmemory_bytes` is set to "auto".
#
# The calculation uses the following formula:
#
# maxmemory = max(redis_maxmemory_min_mb, system_ram × redis_maxmemory_ratio)
#
# Where:
# system_ram -> Total system memory detected by Ansible (in MB)
# redis_maxmemory_ratio -> Percentage of system memory Redis is allowed to use
# redis_maxmemory_min_mb -> Minimum memory Redis should be assigned
#
# The `max()` function ensures Redis always receives at least the configured
# minimum memory, even on small systems.
#
# The final value is converted from MB to bytes because Redis expects the
# `maxmemory` configuration parameter to be expressed in bytes.
#
# Example (10 GB system):
# system_ram = 10240 MB
# ratio = 0.60
# result = 6144 MB
# redis_maxmemory_bytes = 6442450944
#
# If `redis_maxmemory_bytes` is set to a numeric value instead of "auto",
# this calculation is skipped and the provided value is used directly.
- name: Calculate Redis maxmemory automatically
ansible.builtin.set_fact:
redis_maxmemory_bytes: >-
{{
(
[
redis_maxmemory_min_mb | int,
(
(ansible_facts.memtotal_mb | int)
* (redis_maxmemory_ratio | float)
) | int
] | max
) * 1024 * 1024
}}
when: redis_maxmemory_bytes == "auto"
tags: configure_redis

Comment on lines +38 to +54
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably need to document the algorithm, either here or in the defaults file. And in the Redis guide.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I documented in the configure-redis.yml take a look.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm thinking we should put this in the redis_guide.md instead. That is a lot for a code comment.
Look how I did it for the replica priority.

- name: Use template to generate redis.conf
ansible.builtin.template:
src: redis.conf.j2
Expand Down
2 changes: 1 addition & 1 deletion roles/redis/templates/redis.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ acllog-max-len 128
# limit for maxmemory so that there is some free RAM on the system for replica
# output buffers (but this is not needed if the policy is 'noeviction').
#
# maxmemory <bytes>
maxmemory {{ redis_maxmemory_bytes }}

# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select one from the following behaviors:
Expand Down