Deploy Redis as an In-Memory Database for Koyeb Applications
Tags: redis, webdev, tutorial, programming
Published: 2024-07-01 09:16:00

[Redis](https://redis.io/) 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](/docs/reference/service-mesh-and-discovery). 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](/docs/build-and-deploy/deploy-to-koyeb-button) below:

[![Deploy to Koyeb](https://www.koyeb.com/static/images/deploy/button.svg)](https://app.koyeb.com/deploy?type=docker&image=redis&ports=6379;tcp&name=redis-on-koyeb)

## Requirements

To successfully follow and complete this guide, you need:

- A [Koyeb account](https://app.koyeb.com) to deploy and configure Redis.

## 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](#deploy-the-redis-server-on-koyeb)
2. [Deploy a Redis client](#deploy-a-redis-client)
3. [Test the Redis server](#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](https://hub.docker.com/_/redis).

To begin, navigate to the **Overview** tab of the [Koyeb control panel](https://app.koyeb.com/), 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](/docs/reference/service-mesh-and-discovery) 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](https://hub.docker.com/_/redis/tags), append a colon (`:`) and the tag name to the end of the field. Click **Next** to continue.
4. Select your preferred [Instance type](/docs/reference/instances#instance-types) and [region](/docs/reference/regions#core-regions) 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:

```none /<YOUR_SERVICE_NAME>/ /<YOUR_APP_NAME>/ copy
<YOUR_SERVICE_NAME>.<YOUR_APP_NAME>.internal
```

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

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](https://redis.io/docs/latest/integrate/) 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](https://app.koyeb.com/). 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](https://hub.docker.com/_/redis/tags), append a colon (`:`) and the tag name to the end of the field. Click **Next** to continue.
4. Select your preferred [Instance type](/docs/reference/instances#instance-types). Deploy the new service to the same [region](/docs/reference/regions#core-regions) 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.

```bash copy
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](https://redis.io/docs/latest/commands/ping/):

```none copy
PING
```

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

Next, check what keys are defined on the server:

```none copy
KEYS *
```

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

```none copy
SET test "Hello world"
```

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

```none copy
KEYS *
```

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

```none copy
GET test
```

The value should be returned as expected.

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

```none copy
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](https://redis.io/docs/latest/develop/) and explore the various [libraries and tools](https://redis.io/docs/latest/integrate/) available.