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

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

- [1 Cache Services Overview](#Cache_Services_Overview)

- [1.1 Supported Cache Services](#Supported_Cache_Services)

- [1.2 Cache Use Cases in DreamFactory](#Cache_Use_Cases_in_DreamFactory)

- [1.2.1 API Response Caching](#API_Response_Caching)

- [1.2.2 Session Storage](#Session_Storage)

- [1.2.3 Rate Limiting](#Rate_Limiting)

- [1.3 Choosing a Cache Backend](#Choosing_a_Cache_Backend)

- [1.3.1 Redis vs Memcached](#Redis_vs_Memcached)

- [1.3.2 Recommendation](#Recommendation)

- [1.4 Cache Architecture](#Cache_Architecture)

- [1.4.1 Cache Flow](#Cache_Flow)

- [1.4.2 Cache Invalidation](#Cache_Invalidation)

- [1.5 Configuring Cache Services](#Configuring_Cache_Services)

- [1.5.1 System-Wide Cache](#System-Wide_Cache)

- [1.5.2 Per-Service Caching](#Per-Service_Caching)

- [1.6 Cache API Endpoints](#Cache_API_Endpoints)

- [1.6.1 Example: Store a Value](#Example:_Store_a_Value)

- [1.6.2 Example: Retrieve a Value](#Example:_Retrieve_a_Value)

- [1.7 Performance Best Practices](#Performance_Best_Practices)

- [1.7.1 TTL Configuration](#TTL_Configuration)

- [1.7.2 Cache Key Design](#Cache_Key_Design)

- [1.7.3 Memory Management](#Memory_Management)

- [1.8 Common Errors](#Common_Errors)

- [1.9 Next Steps](#Next_Steps)

# Cache Services Overview

DreamFactory supports multiple caching backends to improve API performance. Caching stores frequently accessed data in memory, reducing database load and improving response times for read-heavy workloads.

---

## Supported Cache Services

Service
Type
Use Case
Documentation

**Redis**
In-memory store
Production caching, sessions, queues
[Redis](/Redis)

**Memcached**
In-memory cache
Simple key-value caching
[Memcached](/Memcached)

**Local**
File-based
Development, single-instance deployments
Built-in

**File**
File-based
Development, disk-based persistence
Built-in

**Database**
SQL-backed
Simple deployments without external cache
Built-in

---

## Cache Use Cases in DreamFactory

### API Response Caching

Cache API responses to reduce database queries:

- **Per-service caching**: Enable caching on individual database or file services

- **TTL configuration**: Set expiration times based on data volatility

- **Cache invalidation**: Automatic clearing on write operations

### Session Storage

Store user sessions in a distributed cache:

- **Scalability**: Share sessions across multiple DreamFactory instances

- **Performance**: Faster session lookups than database

- **Persistence**: Redis can persist sessions across restarts

### Rate Limiting

Track API request counts per user or role:

- **Distributed counting**: Works across load-balanced instances

- **Atomic operations**: Accurate request tracking

- **Automatic expiry**: Counters reset after time windows

---

## Choosing a Cache Backend

### Redis vs Memcached

Feature
Redis
Memcached

Data structures
Strings, lists, sets, hashes, sorted sets
Strings only

Persistence
Optional disk persistence
Memory only

Replication
Master-slave, clustering
None (client-side)

Pub/Sub
Yes
No

Lua scripting
Yes
No

Memory efficiency
Good
Excellent for simple data

Max key size
512 MB
1 MB

Use case
Feature-rich, sessions, queues
Simple caching

### Recommendation

- **Production**: Use **Redis** for its flexibility, persistence options, and support for sessions

- **Development**: Use **Local** cache for simplicity

- **High-volume simple caching**: Use **Memcached** for maximum memory efficiency

---

## Cache Architecture

```
┌─────────────────┐     ┌──────────────────┐     ┌─────────────────┐
│   API Client    │────▶│   DreamFactory   │────▶│    Database     │
│                 │◀────│                  │◀────│                 │
└─────────────────┘     └────────┬─────────┘     └─────────────────┘
                                 │
                                 │ Cache check
                                 ▼
                        ┌─────────────────┐
                        │  Cache Backend  │
                        │  (Redis/Memcached)
                        └─────────────────┘

```

### Cache Flow

1. **Request arrives** at DreamFactory

2. **Cache check**: If cached response exists and is valid, return it

3. **Cache miss**: Query the database/service

4. **Store in cache**: Save response with configured TTL

5. **Return response** to client

### Cache Invalidation

DreamFactory automatically invalidates cache entries when:

- **POST/PUT/PATCH/DELETE** operations modify data

- **TTL expires** based on configured duration

- **Manual flush** via admin API

---

## Configuring Cache Services

### System-Wide Cache

Configure the default cache backend in DreamFactory's environment:

```
CACHE_DRIVER=redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=your_password

```

### Per-Service Caching

Enable caching on individual services:

1. Navigate to the service configuration

2. Enable **Cache Enabled** option

3. Set **Cache TTL** in seconds

4. Save the service

---

## Cache API Endpoints

DreamFactory exposes a Cache API for direct cache operations:

Method
Endpoint
Description

`GET`
`/api/v2/{cache_service}/{key}`
Get cached value

`POST`
`/api/v2/{cache_service}`
Store value in cache

`DELETE`
`/api/v2/{cache_service}/{key}`
Delete cached value

`DELETE`
`/api/v2/{cache_service}`
Flush all cached values

### Example: Store a Value

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

```

### Example: Retrieve a Value

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

```

---

## Performance Best Practices

### TTL Configuration

Data Type
Recommended TTL
Rationale

Static reference data
24 hours
Rarely changes

User profiles
5-15 minutes
Balances freshness and performance

Real-time data
30-60 seconds
Frequently updated

Session data
30 minutes - 2 hours
Matches session duration

### Cache Key Design

Use structured, predictable key patterns:

```
{resource_type}:{id}:{sub_resource}

```

Examples:

- `user:123:profile`

- `product:456:inventory`

- `order:789:items`

### Memory Management

- **Monitor memory usage** to prevent eviction

- **Set appropriate TTLs** to expire stale data

- **Use Redis maxmemory-policy** for graceful eviction

---

## Common Errors

Error Code
Message
Cause
Solution

400
Bad Request
Invalid key or value format
Check request payload

401
Unauthorized
Missing API key
Add API key header

404
Not Found
Key does not exist
Handle cache miss in client

503
Service Unavailable
Cache backend unreachable
Check cache server status

---

## Next Steps

- **[Redis](/Redis)**: Configure Redis for production caching

- **[Memcached](/Memcached)**: Configure Memcached for simple caching

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

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