Skip to content

Redis installation on Linux

Redis (Remote Dictionary Server) is an open-source, in-memory data structure store used as a database, cache, and message broker.^[600-developer-big-data-redis-redis-01-install.md]

System Prerequisites

Redis requires a compiler to build from source. On Linux systems, gcc and gcc-c++ are typically necessary.^[600-developer-big-data-redis-redis-01-install.md] You can verify the compiler and install the dependencies using the following commands:^[600-developer-big-data-redis-redis-01-install.md]

gcc -v
yum install gcc-c++

Installation

The standard method for installing Redis on Linux involves downloading the source code, compiling it, and optionally creating a symbolic link for easier version management.

1. Download and Extract

Download the latest stable release (e.g., 5.0.0) and extract the tarball:^[600-developer-big-data-redis-redis-01-install.md]

$ wget http://download.redis.io/releases/redis-5.0.0.tar.gz
$ tar zxvf redis-5.0.0.tar.gz

2. Compile

Navigate to the directory and run make:^[600-developer-big-data-redis-redis-01-install.md]

$ ln -s redis-5.0.0 redis
$ cd redis
$ make

3. Install (Optional)

To install the binaries system-wide, you can use make install. If you wish to install to a specific directory, use the PREFIX option:^[600-developer-big-data-redis-redis-01-install.md]

$ make PREFIX=/some/other/directory install

Running Redis

To start the Redis server using the default configuration, navigate to the src directory within the source tree and execute the server binary:^[600-developer-big-data-redis-redis-01-install.md]

% cd src
% ./redis-server

Verification

You can verify that the Redis server is running and listening on the default port (6379) using system tools:^[600-developer-big-data-redis-redis-01-install.md]

ps -ef | grep redis | grep -v grep
netstat -tlanp | grep 6379
lsof -i:6379

Command-Line Interaction

Once the server is running, you can interact with it using redis-cli:^[600-developer-big-data-redis-redis-01-install.md]

% cd src
% ./redis-cli
redis> ping
PONG
redis> set foo bar
OK
redis> get foo
"bar"

Kernel Optimization

Running Redis in production often requires adjusting Linux kernel parameters to avoid warnings and ensure stability.

TCP Backlog (somaxconn)

Warning: "The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128."^[600-developer-big-data-redis-redis-01-install.md]

The net.core.somaxconn parameter defines the maximum listening queue length (backlog). If the server processes requests slowly, the backlog may fill up, causing new requests to be rejected. To increase the limit to 32768:^[600-developer-big-data-redis-redis-01-install.md]

vim /etc/sysctl.conf
# Add or modify:
net.core.somaxconn=32768
sysctl -p

Overcommit Memory

Warning: "overcommit_memory is set to 0! Background save may fail under low memory condition."^[600-developer-big-data-redis-redis-01-install.md]

To fix this, set vm.overcommit_memory to 1 in /etc/sysctl.conf and apply the change:^[600-developer-big-data-redis-redis-01-install.md]

sysctl vm.overcommit_memory=1

Transparent Huge Pages (THP)

Warning: "You have Transparent Huge Pages (THP) support enabled... This will create latency and memory usage issues with Redis."^[600-developer-big-data-redis-redis-01-install.md]

To disable THP, run the following command as root and add it to /etc/rc.local to persist across reboots:^[600-developer-big-data-redis-redis-01-install.md]

echo never > /sys/kernel/mm/transparent_hugepage/enabled

Useful Commands

  • SELECT index: Switch to a specific database.^[600-developer-big-data-redis-redis-01-install.md]
  • DBSIZE: View the number of keys in the current database.^[600-developer-big-data-redis-redis-01-install.md]
  • FLUSHDB: Clear (delete) all keys in the current database.^[600-developer-big-data-redis-redis-01-install.md]
  • FLUSHALL: Clear all keys in all databases.^[600-developer-big-data-redis-redis-01-install.md]

Sources

  • 600-developer-big-data-redis-redis-01-install.md