---
title: "Redis - DreamFactory Documentation"
source: "https://wiki.dreamfactory.com/Redis"
canonical_url: "https://wiki.dreamfactory.com/Redis"
converted_at: "2026-04-05T06:17:49.241Z"
format: "markdown"
converted_by: "html-to-md-ai"
---
[]()
	
	
	
	# Redis

	
		From DreamFactory Wiki
		
		
		
		
		[Jump to navigation](#mw-head)
		[Jump to search](#searchInput)
		## Contents

- [1 Redis](#Redis)

- [1.1 Use Cases](#Use_Cases)

- [1.2 Prerequisites](#Prerequisites)

- [1.2.1 Redis Server](#Redis_Server)

- [1.2.2 Verify Installation](#Verify_Installation)

- [1.3 Configuring Redis in DreamFactory](#Configuring_Redis_in_DreamFactory)

- [1.3.1 System Cache Configuration](#System_Cache_Configuration)

- [1.3.2 Configuration Options](#Configuration_Options)

- [1.3.3 Session Storage](#Session_Storage)

- [1.3.4 Queue Backend](#Queue_Backend)

- [1.4 Creating a Redis Cache Service](#Creating_a_Redis_Cache_Service)

- [1.4.1 Step 1: Navigate to API Generation](#Step_1:_Navigate_to_API_Generation)

- [1.4.2 Step 2: Create New Service](#Step_2:_Create_New_Service)

- [1.4.3 Step 3: Configure Service Details](#Step_3:_Configure_Service_Details)

- [1.4.4 Step 4: Configure Connection](#Step_4:_Configure_Connection)

- [1.4.5 Step 5: Save and Test](#Step_5:_Save_and_Test)

- [1.5 API Endpoints](#API_Endpoints)

- [1.6 API Examples](#API_Examples)

- [1.6.1 Store a Value](#Store_a_Value)

- [1.6.2 Store Multiple Values](#Store_Multiple_Values)

- [1.6.3 Retrieve a Value](#Retrieve_a_Value)

- [1.6.4 Delete a Key](#Delete_a_Key)

- [1.6.5 Check if Key Exists](#Check_if_Key_Exists)

- [1.7 TTL (Time-To-Live)](#TTL_(Time-To-Live))

- [1.7.1 Recommended TTL Values](#Recommended_TTL_Values)

- [1.8 Redis Configuration Best Practices](#Redis_Configuration_Best_Practices)

- [1.8.1 Security](#Security)

- [1.8.2 Memory Management](#Memory_Management)

- [1.8.3 Persistence](#Persistence)

- [1.9 High Availability](#High_Availability)

- [1.9.1 Redis Sentinel](#Redis_Sentinel)

- [1.9.2 Redis Cluster](#Redis_Cluster)

- [1.10 Monitoring](#Monitoring)

- [1.10.1 Key Metrics to Watch](#Key_Metrics_to_Watch)

- [1.10.2 Redis CLI Commands](#Redis_CLI_Commands)

- [1.11 Common Errors](#Common_Errors)

- [1.11.1 Troubleshooting Connection Issues](#Troubleshooting_Connection_Issues)

- [1.12 Performance Tuning](#Performance_Tuning)

- [1.12.1 Connection Pooling](#Connection_Pooling)

- [1.12.2 Pipelining](#Pipelining)

- [1.12.3 Key Design](#Key_Design)

- [1.13 Next Steps](#Next_Steps)

# Redis

Redis is a high-performance, in-memory data store that DreamFactory uses for caching, session storage, and rate limiting. It provides sub-millisecond response times and supports advanced data structures for complex caching scenarios.

---

## Use Cases

- **API response caching**: Store database query results for faster subsequent requests

- **Session storage**: Distribute user sessions across multiple DreamFactory instances

- **Rate limiting**: Track API request counts with automatic expiry

- **Queue backend**: Process background jobs asynchronously

- **Pub/Sub messaging**: Real-time event distribution (advanced)

---

## Prerequisites

### Redis Server

Install Redis on your server or use a managed service:

**Ubuntu/Debian:**

```
sudo apt update
sudo apt install redis-server
sudo systemctl enable redis-server
sudo systemctl start redis-server

```

**Managed Services:**

- AWS ElastiCache for Redis

- Azure Cache for Redis

- Google Cloud Memorystore

- Redis Cloud

### Verify Installation

```
redis-cli ping
# Expected response: PONG

```

---

## Configuring Redis in DreamFactory

### System Cache Configuration

Set Redis as the system cache backend in your DreamFactory environment file (`.env`):

```
CACHE_DRIVER=redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=your_secure_password
REDIS_DATABASE=0

```

### Configuration Options

Variable
Required
Default
Description

`CACHE_DRIVER`
Yes
`file`
Set to `redis` to enable

`REDIS_HOST`
Yes
`localhost`
Redis server hostname or IP

`REDIS_PORT`
No
`6379`
Redis server port

`REDIS_PASSWORD`
No
-
Redis authentication password

`REDIS_DATABASE`
No
`0`
Redis database number (0-15)

`REDIS_PREFIX`
No
`dreamfactory`
Key prefix for namespacing

### Session Storage

To store sessions in Redis:

```
SESSION_DRIVER=redis

```

### Queue Backend

To use Redis for background job queues:

```
QUEUE_DRIVER=redis

```

---

## Creating a Redis Cache Service

In addition to system caching, you can create a Redis service for direct cache API access.

### Step 1: Navigate to API Generation

Log in to your DreamFactory instance and select **API Generation & Connections**. Set API Type to **Cache**.

### Step 2: Create New Service

Click the plus button and select **Redis**.

### Step 3: Configure Service Details

Field
Description
Example

Name
Service name (used in API URL)
`redis`

Label
Display name
`Redis Cache`

Description
Service description
`Application cache service`

### Step 4: Configure Connection

Field
Required
Default
Description

Host
No
`127.0.0.1`
Redis server hostname or IP

Port
No
`6379`
Redis port number

Password
No
-
Authentication password

Database Index
No
`0`
Redis database number (0-15)

Default TTL
No
`300`
Time to live in minutes before cached values expire

### Step 5: Save and Test

Save the service and use API Docs to test cache operations.

---

## API Endpoints

Method
Endpoint
Description

`GET`
`/api/v2/{service}/`
List all keys (use sparingly)

`GET`
`/api/v2/{service}/{key}`
Get value by key

`POST`
`/api/v2/{service}`
Store one or more key-value pairs

`PUT`
`/api/v2/{service}/{key}`
Update existing key

`DELETE`
`/api/v2/{service}/{key}`
Delete a key

`DELETE`
`/api/v2/{service}`
Flush all keys (dangerous)

---

## API Examples

### Store a Value

```
curl -X POST "https://example.com/api/v2/redis" \
  -H "X-DreamFactory-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "resource": [
      {
        "key": "user:123:profile",
        "value": {"name": "John Doe", "email": "[email protected]"},
        "ttl": 3600
      }
    ]
  }'

```

**Response:**

```
{
  "resource": [
    {
      "key": "user:123:profile",
      "success": true
    }
  ]
}

```

### Store Multiple Values

```
curl -X POST "https://example.com/api/v2/redis" \
  -H "X-DreamFactory-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "resource": [
      {"key": "setting:theme", "value": "dark", "ttl": 86400},
      {"key": "setting:language", "value": "en", "ttl": 86400},
      {"key": "setting:timezone", "value": "UTC", "ttl": 86400}
    ]
  }'

```

### Retrieve a Value

```
curl -X GET "https://example.com/api/v2/redis/user:123:profile" \
  -H "X-DreamFactory-API-Key: YOUR_API_KEY"

```

**Response:**

```
{
  "key": "user:123:profile",
  "value": {
    "name": "John Doe",
    "email": "[email protected]"
  }
}

```

### Delete a Key

```
curl -X DELETE "https://example.com/api/v2/redis/user:123:profile" \
  -H "X-DreamFactory-API-Key: YOUR_API_KEY"

```

### Check if Key Exists

```
curl -X GET "https://example.com/api/v2/redis/user:123:profile?check_exist=true" \
  -H "X-DreamFactory-API-Key: YOUR_API_KEY"

```

**Response:**

```
{
  "key": "user:123:profile",
  "exists": true
}

```

---

## TTL (Time-To-Live)

Set expiration times when storing values:

TTL Value
Behavior

Positive integer
Expire after N seconds

`0`
No expiration (persist indefinitely)

`-1`
Use default TTL from service config

### Recommended TTL Values

Data Type
TTL (seconds)
Duration

Session data
1800
30 minutes

User profiles
300
5 minutes

Reference data
86400
24 hours

Rate limit counters
60
1 minute

Real-time data
30
30 seconds

---

## Redis Configuration Best Practices

### Security

**Bind to localhost or private network:**

```
# /etc/redis/redis.conf
bind 127.0.0.1

```

**Require authentication:**

```
requirepass your_strong_password_here

```

**Disable dangerous commands:**

```
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command DEBUG ""

```

### Memory Management

**Set memory limit:**

```
maxmemory 256mb

```

**Configure eviction policy:**

```
maxmemory-policy allkeys-lru

```

Policy
Description

`noeviction`
Return error when memory limit reached

`allkeys-lru`
Evict least recently used keys (recommended)

`volatile-lru`
Evict LRU keys with TTL set

`allkeys-random`
Random eviction

### Persistence

**RDB snapshots (default):**

```
save 900 1    # Save if 1 key changed in 900 seconds
save 300 10   # Save if 10 keys changed in 300 seconds
save 60 10000 # Save if 10000 keys changed in 60 seconds

```

**AOF for durability:**

```
appendonly yes
appendfsync everysec

```

---

## High Availability

### Redis Sentinel

For automatic failover:

```
REDIS_SENTINEL_HOST=sentinel1.example.com,sentinel2.example.com
REDIS_SENTINEL_PORT=26379
REDIS_SENTINEL_MASTER=mymaster

```

### Redis Cluster

For horizontal scaling, configure multiple Redis nodes with clustering enabled.

---

## Monitoring

### Key Metrics to Watch

Metric
Description
Alert Threshold

`used_memory`
Current memory usage
> 80% of maxmemory

`connected_clients`
Active connections
> 80% of maxclients

`keyspace_hits`
Cache hit count
-

`keyspace_misses`
Cache miss count
High miss ratio

`evicted_keys`
Keys removed due to memory
Any eviction

### Redis CLI Commands

```
# Check server status
redis-cli info

# Monitor memory
redis-cli info memory

# Check connected clients
redis-cli client list

# Get hit/miss ratio
redis-cli info stats | grep keyspace

```

---

## Common Errors

Error Code
Message
Cause
Solution

400
Bad Request
Invalid key or value
Check request format

401
Unauthorized
Missing API key
Add API key header

404
Not Found
Key does not exist
Handle cache miss

500
Connection refused
Redis server unreachable
Check Redis server status

500
NOAUTH
Authentication required
Add REDIS_PASSWORD to .env

### Troubleshooting Connection Issues

```
# Test Redis connectivity
redis-cli -h localhost -p 6379 ping

# Test with password
redis-cli -h localhost -p 6379 -a your_password ping

# Check Redis logs
sudo tail -f /var/log/redis/redis-server.log

```

---

## Performance Tuning

### Connection Pooling

Configure persistent connections in Laravel/DreamFactory:

```
REDIS_CLIENT=phpredis

```

### Pipelining

For bulk operations, the API supports batch requests that are automatically pipelined.

### Key Design

- Use colons `:` as separators: `user:123:profile`

- Keep keys short but descriptive

- Use consistent naming conventions

- Avoid spaces and special characters

---

## Next Steps

- **[Cache Overview](/Cache_Overview)**: Compare caching backends

- **[Memcached](/Memcached)**: Alternative caching option

- **[Performance Tuning](/index.php?title=Getting_Started/Optimizing_Dreamfactory/Database&action=edit&redlink=1)**: Optimize DreamFactory

Retrieved from "[https://wiki.dreamfactory.com/index.php?title=Redis&oldid=831](https://wiki.dreamfactory.com/index.php?title=Redis&oldid=831)"
		[Category](/Special:Categories): - [API](/Category:API)