Contents
About Redis
Set Up
Application
Access
User Management
Basic Operations
Data Operations
Maintenance
Advanced
Security
Monitoring and Performance
About Redis
Overview
Redis is an open-source, in-memory data structure store
Used for multiple purposes, as
A database
A cache
A message broker
Known for its speed and flexibility
Supports various data structures such as strings, hashes, lists, sets, and more
Commonly used for caching, real-time analytics, session management, and message queuing
Key Features
In-Memory Storage
Stores data in memory
Provides extremely fast read and write operations
Data Structures
Supports a wide variety of data structures
Includes - strings, hashes, lists, sets, sorted sets, bitmaps, hyperloglogs, geospatial indexes
Persistence
Offers options for snapshotting (RDB) and append-only file (AOF) for data durability
Replication
Supports master-slave replication for high availability and read scalability
Transactions
Provides support for transactions with the ability to execute multiple commands atomically
Pub/Sub Messaging
Built-in publish/subscribe messaging capabilities for real-time communication
Comparison to Alternatives
MySQL
A traditional relational database better suited for structured data and complex queries
Redis is designed for high-speed, in-memory operations and is often used alongside relational databases for caching
SQLite
Lightweight and serverless, ideal for embedded systems and small apps
Redis offers much faster performance and is used for different purposes such as caching and real-time data processing
PostgreSQL
An advanced RDBMS with strong ACID compliance and support for complex queries
Redis is faster for in-memory operations and is often used for caching and real-time analytics, complementing relational databases.
MongoDB
A NoSQL database designed for high flexibility and scalability with unstructured data
Redis excels in speed and is used for caching, real-time analytics, and session management, often alongside NoSQL databases for performance optimization
Set-Up
Check Installation and Version
$ redis-server --version
Installation
$ sudo apt update
$ sudo apt install redis-server
Uninstallation
$ sudo apt remove redis-server
$ sudo apt autoremove
Application
Start-Up
$ sudo service redis-server start
Check Status
$ sudo service redis-server status
Check Port
$ sudo netstat -tuln | grep 6379
Power-Off
$ sudo service redis-server stop
Access
Single Login into Database
$ redis-cli
Persist Login into Database
Redis doesn't require persistent login management
It simply uses simple authentication with a password set in the configuration file
User Management
Redis does not support creating users and managing privileges
Redis is simply designed to be a simple, key-value store database engine
Access control is typically handled via configuration, through the /etc/redis/redis.conf
Basic Operations
List Databases
Redis has 16 databases by default, identified by numbers 0 to 15:
$ redis-cli INFO keyspace
Create Database
To create a database, with a database number from 0 to 15:
$ redis-cli
SELECT database_number
Example:
Since Redis is a key-value store, we simulate the employee management database using hashes and sets
In this example, we will create a database and connect to it
$ redis-cli
SELECT 0
Clear Database Data
Clear all the data from a single database, the currently selected one:
$ redis-cli FLUSHDB
To delete all data from all databases:
$ redis-cli FLUSHALL
Delete Database
Redis databases are fixed - so you cannot delete them.
However, you can clear them, as shown above
Data Operations
Create Table
Redis is a key-value store and does not support tables
However, you can simulate tables using keys and values
Example:
Use a hash to store department information:
HSET dept:1 name "Finance"
HSET dept:2 name "Human Resources"
HSET dept:3 name "Marketing"
Example:
Use a hash to store employee information and a set for department membership:
HSET emp:1 name "John Doe" email "john.doe@example.com" dept_id 1
HSET emp:2 name "Jane Smith" email "jane.smith@example.com" dept_id 2
HSET emp:3 name "Michael Johnson" email "michael.johnson@example.com" dept_id 3
SADD dept:1:employees 1
SADD dept:2:employees 2
SADD dept:3:employees 3
Insert Data
To set a key-value pair:
$ SET key value
Example:
Add a new employee and assign to a department:
HSET emp:4 name "Emily Davis" email "emily.davis@example.com" dept_id 1
SADD dept:1:employees 4
Update Data
$ SET key new_value
Example:
Update an employee's email:
HSET emp:1 email "john.doe.new@example.com"
Delete Data
$ DEL key
Example:
Delete an employee and remove from the department set:
DEL emp:3
SREM dept:3:employees 3
Query Data
$ GET key
Example:
Retrieve all employees in a department:
SMEMBERS dept:1:employees
Example:
Get employee details:
HGETALL emp:1
Maintenance
Backup Database
To create a backup of the Redis database, use the SAVE command:
This performs a synchronous save of the dataset
$ redis-cli SAVE
Redis configuration and data files are stored in the following directories:
Configuration File (redis.conf):
Location: /etc/redis/redis.conf
This is the default location for the Redis configuration file in Ubuntu
Data Directory:
By default, Redis stores its data files, including a backup (example: dump.rdb) snapshot file, in a directory specified by the dir directive in the redis.conf file
If you haven't changed the default configuration, Redis stores data in /var/lib/redis/
The dump.rdb file, created when you issue a SAVE command, will be located in this directory
$ sudo ls/etc/redis
Output:
redis.conf
$ sudo nano /etc/redis/redis.conf
$ sudo ls /var/lib/redis
Output:
dumb.rdb
Restore Database
To restore a Redis database:
Copy the backup file (dump.rdb) to the Redis data directory
Recall that from above, the data directory is specified in /var/lib/redis/ by default
Then, restart the Redis service
$ cp backup.rdb /var/lib/redis/dump.rdb
$ sudo service redis-server restart
Optimize Database
Redis automatically manages memory efficiently, but you can use the MEMORY command to get insights
Check Logs
Redis logs are typically located in /var/log/redis/
You can view them using:
$ tail -f /var/log/redis/redis-server.log