-
Introduction Redis, short for Remote Dictionary Server, is an open-source, in-memory data structure store used as a database, cache, and message broker. It supports a variety of data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams.
-
Core Features
-
In-Memory Storage
Redis stores data in memory, which allows for extremely fast read and write operations compared to traditional disk-based databases. This makes Redis an excellent choice for use cases requiring low latency and high throughput. -
Data Persistence
Although Redis is an in-memory database, it offers various persistence mechanisms. You can configure Redis to save the dataset to disk every once in a while, append-only file mode (AOF), or a combination of both. -
Data Structures
Redis supports a wide array of data structures, enabling complex data manipulation. The primary structures include:- Strings:
Simple key-value pairs. - Hashes:
Maps of fields and values, similar to JSON objects. - Lists:
Ordered collections of strings, useful for tasks like message queues. - Sets:
Unordered collections of unique strings. - Sorted Sets:
Sets ordered by a score, useful for ranking systems. - Bitmaps:
Compact data structure used for bitwise operations. - HyperLogLogs:
Probabilistic data structures used for counting unique elements. - Streams:
Logs of entries with an automatic ID, useful for event sourcing.
- Strings:
-
Atomic Operations
All operations in Redis are atomic, which ensures that each command is completed entirely or not at all, maintaining data consistency. -
Replication
Redis supports master-slave replication, allowing data to be replicated to any number of slave servers. This ensures high availability and fault tolerance. -
High Availability with Redis Sentinel
Redis Sentinel provides high availability and monitoring. It can automatically failover to a replica in case the master fails. -
Sharding
Redis supports partitioning data across multiple Redis instances, enabling horizontal scaling and improving performance for large datasets. -
Extensibility
With the ability to write Lua scripts for execution in Redis, you can extend its capabilities and perform complex operations directly within the database. -
Pub/Sub Messaging
Redis has a built-in Publish/Subscribe messaging system, making it suitable for real-time messaging and notification systems. -
Geospatial Indexes:
Redis supports geospatial data, allowing you to perform radius queries, making it ideal for location-based applications.
-
-
Use Cases of Redis
-
Caching
Redis is widely used as a caching layer to reduce the load on traditional databases and improve application response times. -
Session Store
Due to its fast performance, Redis is an excellent choice for storing user sessions in web applications. -
Real-Time Analytics
The ability to perform quick read and write operations makes Redis suitable for real-time data analytics. -
Message Queues
Redis can act as a message broker, facilitating communication between different parts of a distributed application. -
Leaderboard/Counting Systems
The sorted sets data structure is perfect for implementing leaderboards, counters, and other ranking systems. -
Geos-patial Applications
Redis's support for geospatial indexes allows for efficient location-based searches and services.
-
-
Getting Started with Redis
To get started with Redis, you can follow these basic steps, which are not for the Inception, because we have to install everything from the Docker file, but is important to know in advance:-
Installation: Install Redis on your machine. You can download it from the official Redis website or use a package manager.
sudo apt-get install redis-server # For Debian-based systems
Start the Redis Server: Start the Redis server using the following command.
redis-server
Connecting to Redis: Use the Redis CLI to connect to the Redis server and execute commands.
redis-cli
Basic Commands: Learn and experiment with basic Redis commands.
SET mykey "Hello, Redis!" GET mykey
-
-
Conclusion
Redis's versatility, high performance, and wide range of features make it a popular choice for developers looking to enhance their applications with fast and reliable data storage solutions. Whether you are building a simple cache or a complex distributed system, Redis provides the tools you need to achieve your goals.
For the bonus services we will have to create another folder called bonus inside the requirements directory
mkdir -p ~/Inception/src/requirements/bonus
And create inside id the specific folder for Redis
mkdir -p ~/Inception/src/requirements/bonus/redis
Vim into the docker file
vim ~/Inception/src/requirements/bonus/redis/Dockerfile
And write the following content inside it:
FROM alpine:3.18
ARG WP_REDIS_PASSWORD
ENV WP_REDIS_PASSWORD=${WP_REDIS_PASSWORD}
RUN apk update && \
apk upgrade && \
apk add --no-cache redis && \
sed -i \
-e "s|bind 127.0.0.1|#bind 127.0.0.1|g" \
-e "s|# maxmemory <bytes>|maxmemory 20mb|g" \
/etc/redis.conf && \
echo "maxmemory-policy allkeys-lru" >> /etc/redis.conf
EXPOSE 6379
CMD ["redis-server", "--protected-mode", "no"]
Explanation for each line:
- Specify the Base Image
FROM alpine:3.19.2
This line specifies the base image for your Docker container. alpine:3.19.2 refers to Alpine Linux version 3.19.2, a lightweight and efficient Linux distribution known for its minimal size. Using a specific version ensures that your container environment remains consistent.
- Setting the Arguments
ARG WP_REDIS_PASSWORD
This line defines a build-time argument WP_REDIS_PASSWORD. It allows the passing of a Redis password during the Docker build process.
- Setting the ENV variable
ENV WP_REDIS_PASSWORD=${WP_REDIS_PASSWORD}
This line sets an environment variable WP_REDIS_PASSWORD inside the container to the value of the build-time argument WP_REDIS_PASSWORD. This makes the Redis password available to the container during runtime.
- Install Dependencies
RUN apk update && \
apk upgrade && \
apk add --no-cache redis && \
sed -i \
-e "s|bind 127.0.0.1|#bind 127.0.0.1|g" \
-e "s|# maxmemory <bytes>|maxmemory 20mb|g" \
/etc/redis.conf && \
echo "maxmemory-policy allkeys-lru" >> /etc/redis.conf
This RUN
command updates the Alpine package index (apk update) and installs a comprehensive set of packages needed for running Redis:
apk update && apk upgrade
: Updates the package index and upgrades any installed packages to the latest version.apk add --no-cache redis
: Installs the Redis package without caching the package index, which saves space.sed -i -e "s|bind 127.0.0.1|#bind 127.0.0.1|g" -e "s|# maxmemory <bytes>|maxmemory 20mb|g" /etc/redis.conf
: Modifies the Redis configuration file (/etc/redis.conf
) usingsed
. Specifically, it:- Comments out the
bind 127.0.0.1
line to allow Redis to listen on all network interfaces. - Sets the
maxmemory
directive to20mb
to limit the maximum memory usage of Redis to 20MB.
- Comments out the
echo "maxmemory-policy allkeys-lru" >> /etc/redis.conf
: Appends the linemaxmemory-policy allkeys-lru
to the Redis configuration file. This sets the eviction policy toallkeys-lru
, meaning that when Redis reaches the memory limit, it will evict the least recently used keys to make room for new data.
- Exposing the port
EXPOSE 6379
This line tells Docker to expose port 6379
, which is the default port that Redis listens on. This makes the port accessible from outside the container.
- Specifin a comand with its argumes
CMD ["redis-server", "--protected-mode", "no"]
This line specifies the command to run when the container starts. It runs the redis-server with the --protected-mode no
option. By default, Redis enables protected mode when not bound to localhost. This command disables protected mode, allowing connections from any IP address, which is necessary for containers in some network setups.
This Dockerfile creates a Redis container based on Alpine Linux. It allows for setting a Redis password through a build-time argument and environment variable. It updates and upgrades the Alpine packages, installs Redis, and configures it to listen on all network interfaces, set a memory limit, and use an allkeys-LRU eviction policy. Finally, it exposes the Redis port and starts the Redis server with protected mode disabled.
Check the other links below for setting up the other services
- Mandatory
- Bonus
- DockerCompose and Makefile
- Project Subject