Redis is an open-source key-value store that offers an efficient data structure server. Redis is popular for its speed and adaptability. You can use Redis as a database, cache, and message broker.

Redis supports a wider range of data structures, including but not limited to strings, hashes, lists, sets, sorted sets, bitmaps, hyperloglogs, and geospatial indexes. Redis enables the storage and retrieval of data using key-value pairs, where each key is associated with a value. In addition, Redis' in-memory design enables speed and provides capabilities like replication, durability, transactions, and pub/sub messaging.

What is Redis

Prerequisites

Before we begin, there are some prerequisites you’ll need to have in place:

Installing Redis

There are 2 options to install Redis on your Civo Kubernetes cluster:

  1. Civo Marketplace UI
  2. Civo CLI

Civo Marketplace UI

To install Redis on Civo Marketplace, follow these steps:

Step 1: After you have launched your Kubernetes cluster, go to the "Kubernetes" section in your Civo dashboard and choose the specific Kubernetes cluster where you wish to deploy Redis.

Note: This will be based on your own project or the purpose of the cluster.

Install Redis on Civo Marketplace

Step 2: In your chosen cluster's dashboard, locate the "Marketplace" section. Within the marketplace, navigate to the "Database" category. From here, find and select the "Redis" option. To start the installation process, click on "Install Apps."

Install Redis on Civo Marketplace

Step 3: Upon successful initiation, you should see a confirmation message or progress indicator. Ensure that the installation is completed without any errors.

Note: In case of any issues during installation, check our Kubernetes docs or reach out to Civo support.

Civo CLI

To install Redis through the Civo CLI, use the following command:

civo kubernetes applications add Redis --cluster 

Use the actual name of your Kubernetes cluster in place of CLUSTER-NAME. If there is no error during installation, the Redis application will be successfully installed into the Kubernetes cluster, and you'll get this result:

The application was installed in the Kubernetes cluster 

Accessing Redis

Accessing the Redis port externally isn't available by default. To enable external access to Redis, apply the following YAML to your cluster:

apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  type: LoadBalancer
  ports:
    - port: 6379
      targetPort: 6379
      protocol: TCP
  selector:
    app: redis

Use this command to apply the YAML file to your cluster:

kubectl apply -f .yaml

After applying the service, you can retrieve the external IP address assigned to the LoadBalancer with the following command:

kubectl get svc redis -o=jsonpath='{.status.loadBalancer.ingress[0].ip}'

Once you have the IP address, you can access Redis on that IP at port 6379.

To start using Redis in your cluster, browse the list of applications installed on your dashboard, find and choose the Redis application, and then read the "Usage Instructions" displayed.

Accessing Redis

Note: During this process, a load balancer will be automatically created at an additional cost. It is important to be aware that separate charges will apply for the load balancer service. When accessing Redis externally, ensure that you utilize a firewall to restrict access to the Redis server to trusted IP addresses only. Also, consider using SSL/TLS encryption to secure communication between clients and the Redis server.

Redis Terminology

The following terminologies will help you understand and work with Redis:

Terminology Description
Key-Value Store Data is kept in Redis as key-value pairs in a key-value store. Each key is connected to a value because keys serve as unique identifiers for values.
Key In Redis, a key is a string that serves as a unique identifier for a specific value. To obtain, modify, or remove values, keys are used.
Value The information linked to a key is called a value. Various data types, such as strings, hashes, lists, sets, sorted sets, and more, can be used as values.
Bitmap Bitmaps are data structures that represent an array of bits, and they are used for usage tracking or flagging.
Transaction Transactions in Redis are commands executed all at once.
HyperLogLog A variety of different objects can be approximated utilizing the probabilistic data structure called HyperLogLog.
Replication Replication in Redis is the process of synchronizing data from a master instance to one or more replica instances for fault tolerance and scalability.
Persistence Redis offers the ability to save data to disk and to recover its exact state after a restart. This is known as Persistence in Redis.
Pub/Sub (Publish/Subscribe) In the Pub/Sub messaging paradigm, clients can sign up for channels to receive messages that have been published to those channels.

Redis Operations

Redis offers a wide range of commands and data types for processing data in real time. Redis commands that are frequently utilized include:

Note: Before executing these commands, ensure you're connected to your Redis server using a Redis client.

Creating a Key-Value Pair: To create a new key-value pair, use the SET command.

Use the following command to create a key-value pair where the value is "Arua" and the key is named "user":

SET user Arua

To retrieve the value for the key "user," use the GET command:

GET user

The SET command in Redis is used to create a new key-value pair and also to update an existing key with a new value.

Incrementing a Key: In Redis, incrementing a key involves increasing the numerical value associated with the key by a specified increment. To increase a key, use the INCRBY command.

To increase a key named "user" by 5, use:

INCRBY user 5

After executing this command, the value associated with the key "user" will be incremented by 5. By default, if you try to increment a key that does not have a value, Redis will assume the value to be 0, and then perform an increment, effectively creating the key with the incremented value.

Expiring a Key: In Redis, you can set an expiration time on a key. The expiration time is commonly measured in seconds.

To expire the key named “user” in 60 seconds, use:

EXPIRE user 60

Deleting a Key: In Redis, you can remove a key or multiple keys from the database.

To delete the key “user,” use:

DEL user

PUBLISH/SUBSCRIBE: The Publish/Subscribe commands are unique commands in Redis used to implement Pub/Sub messaging.

To publish the title of this tutorial to a channel named “user,” use:

PUBLISH user "Optimizing Data Flow: Redis Integration in Civo Kubernetes"

To subscribe to the user, use:

SUBSCRIBE user

This subscribes you to the "user" channel to receive messages published to that channel.

Real-time Analytics using Redis

With Redis, we can process and analyze data in real-time to generate insights. Let us explore a system that collects the number of users on a website.

To begin, we use this Python script:

import http.server
import redis

r = redis.Redis(host='redis', port=6379)

class RequestHandler(http.server.BaseHTTPRequestHandler):
    def do_GET(self):
        
        self.send_response(200)
        self.send_header('Content-type', 'text/plain')
        self.end_headers()

        if self.path == '/':
            r.incr('counter')
            self.wfile.write(b'This page was visited %s times.' % r.get('counter'))

server = http.server.HTTPServer(('localhost', 8080), RequestHandler)

server.serve_forever()

Create a Dockerfile for the Python application to run it in your Kubernetes cluster:

FROM python:3

RUN pip install redis

COPY main.py /app/main.py

CMD ["python", "/app/main.py"]

To build the Docker image from the specified Dockerfile, Run:

docker build . -t python-server:latest

After the docker build is completed, use the following commands to deploy and expose the server:

kubectl create deployment python-server --image=python-server:latest
kubectl expose deployment python-server --port=8080

Lastly, use port-forwarding to connect to the server:

kubectl port-forward service/python-server 8080:8080

If there are no errors, then proceed to load your localhost:8080 on your web browser, and you will see the counter on the webpage.

Real-time Analytics using Redis

Each time you refresh your webpage, the number will be increased by 1.

Redis as Cache

Caching is a technique used to store data that is frequently accessed in a fast-access storage area. Caching optimizes performance, reduces latency, and improves scalability in the development of an application.

Redis is used for cache, database, or message broker because of its in-memory nature and speed. Redis offers multiple data structures, replication, high availability, and more. This makes Redis the most preferred caching option in a Kubernetes environment.

Redis Sentinel

Redis Sentinel is a distributed system within Redis that offers monitoring, automatic failover, and notification in a Redis instance.

Let’s explore these features:

  • Monitoring:Redis Sentinel keeps track of the activity, responsiveness, latency, throughput, and connections of Redis instances to determine their overall health. The Master and replicas are both observed by Redis sentinel.
  • Automatic failover: In a distributed system like Kubernetes, where pods come and go, you need to be able to link the active pod with the current Redis master. Sentinel performs the automated failover when a Redis master dies because it is responsible for elevating the Redis replica to the master.
  • Notification:Sentinel notifies administrators or automated systems when a Redis master or replica goes down to ensure a quick resolution of the problem.

Benefits of using Redis

Redis provides several advantages within a Kubernetes environment, they include:

  • Ease of Use: Redis is not only easy to deploy in a Kubernetes cluster using tools like Helm charts or operators, but its intuitive command set also makes it straightforward to use and manage.
  • Scalability: Redis supports horizontal partitioning or sharding, making it highly scalable. In Kubernetes, Redis can be configured in a master-slave replication mode, distributing data across multiple nodes and further enhancing scalability.
  • Low Latency: With its in-memory architecture, Redis ensures incredibly low latencies, making it ideal for use cases requiring rapid data access, such as caching, session storage, and real-time analytics.

Summary

Redis offers an effective data processing solution when launching a cluster with Civo Kubernetes. The implementation of Redis and its advantages in a Kubernetes environment has been covered in this tutorial. Redis provides a variety of use cases that enable seamless data processing optimization.

Additional resources

If you want to know more about data processing and the Redis application, take a look at these resources: