# Creating a Database

Redis is a popular in-memory key-value data store known for its speed, flexibility, and support for a wide range of data structures. Setting up Redis properly is essential to ensure high availability, data persistence, and performance in modern applications. This guide walks through different ways to run and connect to Redis: using the Redis CLI, using Docker containers, and using Redis CLI tools. It also emphasizes best practices that should be followed at each step.

### **Creating Using redis-cli**

The Redis command-line interface (<span class="s2">redis-cli</span>) is a built-in tool that allows direct interaction with a Redis server. It supports connecting to local or remote Redis instances and executing all supported Redis commands interactively or non-interactively.

##### **Connect to Redis:**

If you’re running Redis locally (e.g., from a system package or Docker container on your machine), you can simply run the CLI tool with no arguments:

```
redis-cli
```

For remote connections, you need to provide the hostname or IP address, the port number (default is 6379), and the password if the instance is protected:

```
redis-cli -h <host> -p <port> -a <password>
```

You’ll be dropped into the Redis shell, where commands can be executed directly.

### **Running Redis Using Docker**

Docker is a widely-used tool for running applications in isolated environments called containers. Redis can be deployed in a container for fast and consistent setup across different environments, making it ideal for both development and production.

#### **Access Elestio Terminal**

If you’re using Elestio for your Redis deployment, log into the Elestio dashboard. Go to your Redis service, then navigate to <span class="s2">**Tools &gt; Terminal**</span>. This opens a browser-based terminal already configured for the correct environment.

[![image.png](https://docs.elest.io/uploads/images/gallery/2025-05/scaled-1680-/s4himage.png)](https://docs.elest.io/uploads/images/gallery/2025-05/s4himage.png)

Once in the terminal, change the directory to the project root where the Docker services are defined. This is typically:

```
cd /opt/app/
```

#### **Access the Redis Container Shell**

Elestio services are managed using Docker Compose, which orchestrates multiple containers. To enter the Redis container’s shell and run Redis commands, use the following:

```
docker-compose exec redis bash
```

This command opens a shell inside the running Redis container.

#### **Access Redis CLI from Within the Container**

Inside the Redis container, the <span class="s2">redis-cli</span> command is already available. Use it to access the Redis instance. If authentication is enabled, supply the password using the <span class="s2">-a</span> flag:

```
redis-cli -a <password>
```

You will be connected directly to the Redis server running inside the container.

#### **Test Connectivity**

Use a simple <span class="s2">SET</span> command to store a value and then retrieve it using <span class="s2">GET</span> to ensure Redis is functioning properly:

```
set testkey "Hello Redis"
get testkey
```

Expected output:

```
"Hello Redis"
```

This verifies read/write operations are working as expected.

### **Connecting Using redis-cli in Scripts**

<span class="s2">redis-cli</span> isn’t just for interactive use — it can also be used within shell scripts and automated pipelines. This is useful for deployment tasks, monitoring scripts, and health checks.

For example, to set a key from a script or cron job:

```
redis-cli -h <host> -p <port> -a <password> SET example_key "example_value"
```

This command connects to the Redis server and sets the specified key in a single line, suitable for automation.

### **Best Practices for Setting Up Redis**

##### **Use Meaningful Key Naming Conventions**

Organizing your Redis data is essential for maintainability and clarity. Use descriptive keys and adopt a namespace-style format using colons (<span class="s2">:</span>) to group related keys:

```
user:1001:profile
order:2023:total
```

This makes it easier to debug, analyze, and migrate data in the future.

##### **Follow Consistent Data Structures**

Redis supports multiple data types: strings, hashes, lists, sets, and sorted sets. Choose the most appropriate structure for your use case, and apply it consistently. For example, use hashes to store user attributes and lists for ordered items.

Inconsistent or incorrect use of data structures can lead to performance issues and logic errors.

##### **Enable Authentication and TLS**

Security is critical in production environments. Always set a strong password using the <span class="s2">requirepass</span> directive in <span class="s2">redis.conf</span>, and consider enabling TLS for encrypted communication if Redis is exposed over a network.

Example `<span class="s2">redis.conf</span>` settings:

```
requirepass strong_secure_password
tls-port 6379
tls-cert-file /path/to/cert.pem
tls-key-file /path/to/key.pem
```

This helps prevent unauthorized access and secures data in transit.

##### **Configure Persistence Options**

Redis is in-memory, but it supports two main persistence options:

- <span class="s1">**RDB (snapshotting)**</span>: takes periodic snapshots of the data.
- <span class="s1">**AOF (Append Only File)**</span>: logs each write operation for more durable persistence.

Set these options in <span class="s2">redis.conf</span>:

```
save 900 1
appendonly yes
appendfsync everysec
```

Use AOF for durability and RDB for faster restarts — or combine both for a balance.

##### **Monitor and Tune Performance**

Use Redis’s built-in tools (<span class="s2">INFO</span>, <span class="s2">MONITOR</span>, <span class="s2">SLOWLOG</span>) to analyze behavior, identify slow queries, and monitor memory usage. This helps maintain performance and plan for scaling.

External tools like <span class="s3">**RedisInsight**</span>, <span class="s3">**Prometheus**</span>, or <span class="s3">**Grafana**</span> can visualize metrics and alert on anomalies.

### **Common Issues and Their Solutions**

<table border="1" id="bkmrk-issue-cause-solution" style="font-family: -apple-system, 'system-ui', 'Segoe UI', Oxygen, Ubuntu, Roboto, Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; font-size: 14px; border-collapse: collapse; border-color: rgb(0, 0, 0);"><thead><tr><th style="border-color: rgb(0, 0, 0);">**Issue**

</th><th style="border-color: rgb(0, 0, 0);">**Cause**

</th><th style="border-color: rgb(0, 0, 0);">**Solution**

</th></tr></thead><tbody><tr><td style="border-color: rgb(0, 0, 0);">NOAUTH Authentication required.

</td><td style="border-color: rgb(0, 0, 0);">Connecting to a password-protected Redis without a password

</td><td style="border-color: rgb(0, 0, 0);">Use the <span class="s1">-a &lt;password&gt;</span> flag or the <span class="s1">AUTH</span> command before other commands

</td></tr><tr><td style="border-color: rgb(0, 0, 0);">ERR Client sent AUTH, but no password is set

</td><td style="border-color: rgb(0, 0, 0);">The Redis server does not require a password

</td><td style="border-color: rgb(0, 0, 0);">Remove the <span class="s1">-a</span> flag or check <span class="s1">requirepass</span> in <span class="s1">redis.conf</span>

</td></tr><tr><td style="border-color: rgb(0, 0, 0);">Can’t connect to Redis on ‘localhost’

</td><td style="border-color: rgb(0, 0, 0);">Redis is not running or using the wrong port

</td><td style="border-color: rgb(0, 0, 0);">Start Redis and check <span class="s1">redis.conf</span> or Docker port mappings

</td></tr><tr><td style="border-color: rgb(0, 0, 0);">Docker Redis container refuses connections

</td><td style="border-color: rgb(0, 0, 0);">Container is not ready or misconfigured network

</td><td style="border-color: rgb(0, 0, 0);">Check logs using <span class="s1">docker-compose logs redis</span> and verify port exposure

</td></tr><tr><td style="border-color: rgb(0, 0, 0);">Data not persisted after restart

</td><td style="border-color: rgb(0, 0, 0);">Persistence settings are disabled

</td><td style="border-color: rgb(0, 0, 0);">Enable AOF or RDB in <span class="s1">redis.conf</span>

</td></tr></tbody></table>