diff --git a/docs/redis_guide.md b/docs/redis_guide.md index b14b739d..96fde629 100644 --- a/docs/redis_guide.md +++ b/docs/redis_guide.md @@ -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 diff --git a/roles/redis/defaults/main/redis.yml b/roles/redis/defaults/main/redis.yml index afde9d4d..3b31f0cb 100644 --- a/roles/redis/defaults/main/redis.yml +++ b/roles/redis/defaults/main/redis.yml @@ -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 diff --git a/roles/redis/tasks/configure-redis.yml b/roles/redis/tasks/configure-redis.yml index f0a872cf..d65de0ce 100644 --- a/roles/redis/tasks/configure-redis.yml +++ b/roles/redis/tasks/configure-redis.yml @@ -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 + - name: Use template to generate redis.conf ansible.builtin.template: src: redis.conf.j2 diff --git a/roles/redis/templates/redis.conf.j2 b/roles/redis/templates/redis.conf.j2 index 6d807084..a9bc2ea9 100644 --- a/roles/redis/templates/redis.conf.j2 +++ b/roles/redis/templates/redis.conf.j2 @@ -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 +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: