# Production Redis Setup Guide for TravelCam Backend

This guide outlines the steps to enable **Redis** for in-memory caching and high-speed queue handling in production.

---

## 1. Why Redis in Production?

- **Sub-millisecond latency (<1ms)**: Stored directly in RAM rather than on disk.
- **Horizontal Scaling**: Multiple backend API workers or load-balanced containers share the exact same central cache.
- **Queue Performance**: Running Laravel background workers (`php artisan queue:work`) on Redis eliminates database table locks.
- **Cache Tags & Expiry**: Advanced memory eviction algorithms (LRU) manage storage automatically.

---

## 2. Server Installation

### On Ubuntu / Debian (VPS / EC2 / Droplet):
```bash
# 1. Update package list and install Redis server
sudo apt update
sudo apt install redis-server -y

# 2. Ensure Redis starts automatically on system boot
sudo systemctl enable redis-server
sudo systemctl start redis-server

# 3. Verify Redis is running
redis-cli ping
# Expected output: PONG
```

### On Managed Services (AWS ElastiCache, Upstash, Redis Cloud, DigitalOcean):
- Simply provision a Redis instance from your cloud dashboard.
- Note down the `Host`, `Port`, and `Password`.

---

## 3. Install the PHP Redis Client

In the `backend` directory, install **Predis** (official pure-PHP client recommended by Laravel):

```bash
composer require predis/predis
```

*(Alternatively, if using the PHP C-extension: `sudo apt install php8.3-redis` and set `REDIS_CLIENT=phpredis`)*

---

## 4. Environment Configuration (`.env`)

Add or update the following settings in your `backend/.env` file:

```env
# 1. Switch Cache and Queue drivers to Redis
CACHE_STORE=redis
QUEUE_CONNECTION=redis

# 2. Redis Client Driver
REDIS_CLIENT=predis

# 3. Redis Connection Credentials
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

# (Optional) If using Redis for user sessions:
# SESSION_DRIVER=redis
```

> **Note for remote Redis (e.g., Upstash or AWS ElastiCache)**:
> Replace `REDIS_HOST` with your endpoint domain and set `REDIS_PASSWORD` to your secret key. Set `REDIS_PORT=6379` (or `REDIS_PORT=6380` with TLS).

---

## 5. Apply Changes in Laravel

Whenever you switch or update environment settings in production, clear and rebuild Laravel's config cache:

```bash
php artisan config:clear
php artisan cache:clear

# Cache configs and routes for maximum production performance
php artisan config:cache
php artisan route:cache
```

---

## 6. Verification

### Test Redis Cache:
```bash
php artisan tinker
```
Inside Tinker:
```php
Cache::put('test_key', 'Hello Redis', 60);
Cache::get('test_key'); // Should output "Hello Redis"
exit
```

### Inspect via Redis CLI:
```bash
redis-cli
127.0.0.1:6379> keys *
# You will see Laravel cache keys prefixed with "travelcam_database_..."
127.0.0.1:6379> exit
```

---

## 7. Reverting to File Cache (if ever needed)

Because the controllers use Laravel's default `Cache` facade, you can revert back to file cache at any time simply by changing `.env`:

```env
CACHE_STORE=file
```
and running `php artisan config:clear`.
