Jun 25, 2024
6 min read

Deploy Redis as an In-Memory Database for Koyeb Applications

Introduction

Redis is a powerful in-memory storage technology often used a key-value store, caching layer, and message broker. It is a key component in many application architectures due to its speed, flexibility, and ease-of-use.

In this guide, we will go over how to deploy and configure Redis to act as a data store for applications on Koyeb. We will deploy Redis to Koyeb as a private web service, meaning the instance will only be accessible to other applications using the service mesh. Afterwards, we will demonstrate how to connect to it from a companion service.

You can deploy the Redis instance by clicking the Deploy to Koyeb button below:

Deploy to Koyeb

Requirements

To successfully follow and complete this guide, you need:

Steps

To complete this guide and deploy a Redis instance for your applications, you'll need to follow these steps:

  1. Deploy the Redis server on Koyeb
  2. Deploy a Redis client
  3. Test the Redis server

Deploy the Redis server on Koyeb

The simplest way of to deploy Redis on Koyeb is to use the Redis Docker image.

To begin, navigate to the Overview tab of the Koyeb control panel, click Create Service to begin:

  1. Select Private service as the service type and then click Create private service. This service type allows other Koyeb applications to access the Redis using the service mesh rather than exposing the service publicly.
  2. Select Docker as the deployment source and click Next.
  3. In the image field, type docker.io/redis to pull the latest Redis tag. To deploy a specific Redis tag, append a colon (:) and the tag name to the end of the field. Click Next to continue.
  4. Select your preferred Instance type and region and click Next.
  5. In the Exposed ports section, change the Port value to 6379.
  6. Click Deploy.

Koyeb will pull the specified Redis Docker image and launch it using the provided configuration.

Once the deployment is complete and the Instance is healthy, click Inspect and manage your service to access the Service's Overview page. Find the Private domain value and click the associated copy icon to copy the Redis server's private domain name. It should use the following format:

<YOUR_SERVICE_NAME>.<YOUR_APP_NAME>.internal
Note

Your private domain may also end in .koyeb. This domain format is currently being phased out in favor of the .internal domain format.

Now that the Redis service is deployed, you can launch a second service to test it.

Deploy a Redis client

To connect to the Redis service, we will launch another service to act as the client. You can use any Redis library to interact with your service.

For simplicity's sake, we will demonstrate by deploying the same Redis Docker image we used for the server in a different configuration. This time, instead of starting the Redis server process in the container, we will override the image's default command during deployment. We will configure this service as a worker so that we can use the bundled CLI client as needed.

To begin, return to the Overview tab of the Koyeb control panel. Find your existing Redis service and click the ellipsis next to the App name. In the menu that appears, select Add service:

  1. Select Worker as the service type and then click Create worker service. This service type is suitable for deployments that do not provide a network service.
  2. Select Docker as the deployment source and click Next.
  3. In the image field, type docker.io/redis to pull the latest Redis tag. To deploy a specific Redis tag, append a colon (:) and the tag name to the end of the field. Click Next to continue.
  4. Select your preferred Instance type. Deploy the new service to the same region as your Redis server and click Next.
  5. In the Deployment section, click the Override toggle associated with the Command field and enter sleep.
  6. Click the Override toggle associated with the Command args field and enter 3600. Together, these two settings override the default Redis server command and instead sleep for an hour. This ensures that a process will be running that won't exit during deployment.
  7. Edit the Service name to ensure that the new service has a unique name.
  8. Click Deploy.

Koyeb will pull the specified Redis Docker image and launch it using the provided configuration.

Once the deployment is complete and the Instance is healthy, click Inspect and manage your service to access the Service's Overview page.

Test the Redis server

Once the worker service is up and running, visit the Service's Overview page. From there, click Console to connect to your Instance's shell.

In the web terminal window, press Enter to spawn a Bourne shell session or replace the shell with /bin/bash and press Enter to spawn a Bash session.

In the web terminal window, connect to your Redis server using the redis-cli command and the -h flag. Use the private domain of your Redis server that you copied earlier.

redis-cli -h <REDIS_SERVER_PRIVATE_DOMAIN>

The client will attempt to connect to the server at the given address and will start an interactive Redis session.

In the new session, verify that the connection was successful with the Redis PING command:

PING

The server should respond with PONG if everything is working as intended.

Next, check what keys are defined on the server:

KEYS *

Because we have not yet added any keys, the server should respond with (empty array). Define a new key-value pair by typing:

SET test "Hello world"

If we check the keys again, a single key should be returned:

KEYS *

This time the server responds with 1) "test". Retrieve the key's value by typing:

GET test

The value should be returned as expected.

When you are finished testing your Redis server, exit the session by typing:

QUIT

Conclusion

In this guide, we demonstrated how to launch a Redis server on Koyeb using the Redis Docker image. We began by deploying Redis to Koyeb as a private web service exposed through the private network. Afterwards, we deployed a separate worker service in the same region to act as the Redis client. Using the client service, we connected to the Redis server and verified that it responded as expected.

To learn more about how to configure and use Redis, check out the Redis documentation and explore the various libraries and tools available.


Deploy AI apps to production in minutes

Koyeb is a developer-friendly serverless platform to deploy apps globally. No-ops, servers, or infrastructure management.
All systems operational
© Koyeb