How to setup Redis listen to Unix socket in Ubuntu

1 min read

Diagram illustrating Redis setup with Unix socket connection in Ubuntu environment

What’s Redis

Redis is an in-memory data structure store, used as a database, cache, and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

Why use Redis

  1. Performance: Redis stores data in memory and accesses it at lightning-fast speeds.
  2. Flexibility: Supports a variety of data types and advanced data operations, making it suitable for a wide range of applications.
  3. Simplicity: Easy to install and set up, with a straightforward data model and command interface.
  4. Durability: Offers options for persistent storage, ensuring that your data is not lost even after a restart.
  5. High Availability: Features like Redis Sentinel and Redis Cluster provide mechanisms for high availability and automatic partitioning.

How to enable unix socket in Redis

Install Redis

First, ensure Redis is installed. If not, you can install it using the package manager:

apt update && apt install lsb-release curl gpg

curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

apt update && apt install redis

Configure Redis to Use Unix Socket

Find the section related to the Unix socket. Uncomment and modify the following lines:

vim /etc/redis/redis.conf

bind 127.0.0.1 -::1
port 0

unixsocket /var/run/redis/redis.sock
unixsocketperm 700

The unixsocket line specifies the path to the Unix socket file that Redis will use. The unixsocketperm sets the file permissions of the socket file. Here, 700 means that only the owner can read, write, or execute.

Restart Redis

To apply the changes, restart the Redis service:

systemctl restart redis.service

Test the Setup

Verify Redis is now listening on the Unix socket:

redis-cli -s /var/run/redis/redis.sock ping

If everything is set up correctly, it should return:

PONG

Reference

https://redis.io/docs/latest/operate/oss_and_stack/install/install-redis/install-redis-on-linux