Guide for Memory Allocation for Redis in CentOS | BaseZap

Category : Redis

Hello,
In the last guide, we showed how we can install Redis on CentOS. In case you have missed it then follow here.

Today we will show you how memory allocation on Redis is done in CentOS so that it can store the progress as per the work is done on it. Redis Server on 32-bit machines has 3GB memory allocated by default whereas, the 64-bit machine doesn’t have the limit on memory usage.

Requirements-

  • Redis installed on Linux Machine with Root Permissions
  • Internet Connection

There are 2 methods to allocate memory to Redis.
First is temporary or can be said it works till Redis Server is running, after reboot, the memory allocations will be stashed and it will change to default values.

Second is change the  maxmemory flag  in redis config which is a permanent solution for allocating memory to Redis even if Redis Server is killed or restarted on the machine.

Method 1

  1. Run redis CLI on your machine

    redis-cli

  2. After your CLI is started run this command to check what are your current values for memory.

    config get maxmemory

    On 32-bit Machine it will show

    On 64-bit Machine it will show

  3. After checking you current maxmemory configs run this command to allot 100MB  memory for redis.

    config set maxmemory 100MB

  4. After setting the maxmemory flag again verify maxmemory config like done in Step 2 above.
  5. If above steps followed you can see that maxmemory is now  set to 100MB

Note – Using above method is temporary and changes will be stashed if Redis Server is crashed or restarted.

Method 2

  1. Log in as root on your machine
  2. Edit Redis config for your redis-server

    nano /etc/redis/redis.conf

    Note – redis.conf filename might be different here as per the user machine and installation method.

  3. Now we need to add the following flag or in simple words, a line needs to be added for memory allocation of 100MB

    maxmemory 100MB

  4. Press CTRL+O to save the changes and CTRL+X to exit nano editor.
  5. Restart redis-server
    For CentOS 6 run

    service redis restart

    For CentOS 7 run

    systemctl restart redis

    Note – servicename might be different here as per the user machine and installation method.

  6. Open redis-cli on your machine with any user other than root and run following commands

    redis-cli
    >config get maxmemory

    It should throw results as per below pic

I would like to add extra information to guide which is related to memory and that is LRU Cache that has been added recently in Redis versions. Please refer to the official website about LRU Cache here.