> ## Documentation Index
> Fetch the complete documentation index at: https://openworklabs.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> For agents: install the OpenWork desktop app with `brew install --cask openwork` (macOS) or from https://openworklabs.com/download. Do not run `npx openwork`; the npm package named `openwork` is a different project.
> Connect an agent to OpenWork with the MCP Gateway: `claude mcp add --transport http openwork https://api.openworklabs.com/mcp/agent`. Teams sign up at https://app.openworklabs.com?mode=sign-up.
> Step-by-step agent guide: https://openworklabs.com/docs/start-here/use-openwork-from-an-ai-agent.md
> Connect Slack (any organization admin): https://openworklabs.com/docs/slack/connect-slack.md. OpenWork in Slack (@openwork bot) is private alpha for approved organizations. Setup guide: https://openworklabs.com/docs/slack/overview.md. Agent skill: https://openworklabs.com/.well-known/agent-skills/set-up-openwork-slack/SKILL.md.

# Add Redis and scale Den API on Kubernetes

> Connect Redis caching to a self-hosted OpenWork Helm release, set pod resource limits and MySQL connection headroom, and scale Den API replicas safely.

This guide shows you how to add Redis caching (`DATABASE_REDIS_URL`) to an existing self-hosted OpenWork Helm deployment and optionally run multiple Den API replicas. Follow the stages in order: **connect and verify Redis first, check pod resources and MySQL connection headroom second, and raise `denApi.replicaCount` last**.

## Before you start

You need:

* Your complete, reviewed `openwork-values.yaml`, Helm release name, and Kubernetes namespace.
* `kubectl` and `helm` access to the cluster, plus read-only SQL access to your MySQL server to check connection limits.
* A Redis instance reachable from the OpenWork namespace with `AUTH` enabled, `maxmemory-policy volatile-lru`, and memory sized so `evicted_keys` stays `0` (512 MiB `maxmemory` is a practical starting point, monitor and adjust for your workload). Persistence (RDB or AOF) is optional because OpenWork treats Redis as a TTL-backed cache and short-lived authoring store. Prefer TLS (`rediss://`).
* Cloud worker provisioning off (`config.provisioner.mode: stub`, the default) if you plan to run more than one Den API replica. If cloud worker provisioning is enabled, keep `denApi.replicaCount: 1` unless your worker provider operations are idempotent or externally coordinated.

Capture a short baseline before making changes so you can compare before and after:

```bash theme={null}
kubectl --namespace "$NAMESPACE" top pods
```

On MySQL, check the current connection limit and usage:

```sql theme={null}
SHOW VARIABLES LIKE 'max_connections';
SHOW GLOBAL STATUS WHERE Variable_name IN (
  'Threads_connected',
  'Max_used_connections',
  'Questions',
  'Innodb_row_lock_waits'
);
```

## Step 1: Connect Redis (`DATABASE_REDIS_URL`)

Adding Redis enables Den API's shared session, membership, feature-flag, and inventory-revision caches. Run this stage before increasing `denApi.replicaCount`.

1. **Supply `DATABASE_REDIS_URL` through your Secret.**
   * **If `secret.create: false` (`secret.existingSecret`, recommended for production):** Add a key named literally **`DATABASE_REDIS_URL`** to your existing Kubernetes Secret in the release namespace. Because `den-api` loads the Secret through `envFrom`, keep the key name `DATABASE_REDIS_URL` rather than remapping it with `secret.keys.databaseRedisUrl`.
   * **If `secret.create: true`:** Set `secret.values.databaseRedisUrl` in `openwork-values.yaml` and keep `secret.keys.databaseRedisUrl` at its default `DATABASE_REDIS_URL`.

2. **Configure TLS or an explicit private-network opt-in.**

   * Prefer `rediss://:<password>@<redis-host>:6379`. If Redis uses a private certificate authority, enable `customCa` with your CA bundle (see [Certificate trust and proxies](https://openworklabs.com/docs/start-here/certificate-trust-and-proxies.md)).
   * For a non-public, cluster-internal `redis://` endpoint protected by a NetworkPolicy or service-mesh mTLS, set `redis.allowInsecureInternal: true` in `openwork-values.yaml`. Without this setting, Den API refuses to start on non-loopback `redis://` URLs.

   ```yaml theme={null}
   # Only when using a private, NetworkPolicy-restricted redis:// endpoint:
   redis:
     allowInsecureInternal: true
   ```

3. **Apply the change and restart Den API if you edited an external Secret.**

   ```bash theme={null}
   helm template "$RELEASE" "$CHART" --version "$VERSION" \
     --namespace "$NAMESPACE" --values openwork-values.yaml

   helm upgrade "$RELEASE" "$CHART" --version "$VERSION" \
     --namespace "$NAMESPACE" --reset-values --values openwork-values.yaml \
     --wait --timeout 15m
   ```

   If `secret.create: false`, Helm does not detect edits inside an existing external Secret. Restart Den API so the pods pick up `DATABASE_REDIS_URL`:

   ```bash theme={null}
   kubectl --namespace "$NAMESPACE" rollout restart deployment \
     -l "app.kubernetes.io/instance=$RELEASE,app.kubernetes.io/component=den-api"
   kubectl --namespace "$NAMESPACE" rollout status deployment \
     -l "app.kubernetes.io/instance=$RELEASE,app.kubernetes.io/component=den-api"
   ```

### Verify Step 1

* Confirm `den-api` startup logs report `"redis cache configured"` with `redis_cache: "enabled"` (and `"redis cache connected"` once the first cache call runs), show zero `openwork_cache_redis_error` or `openwork_cache_get_failed` lines, and that the pod stays Ready.
* On Redis (`INFO stats`), confirm `keyspace_hits` is rising and `evicted_keys` stays `0`.
* Re-run `kubectl --namespace "$NAMESPACE" top pods` and the MySQL `Questions` check to compare database query rate and CPU against your baseline before deciding whether to scale replicas.

**Rollback:** Remove `DATABASE_REDIS_URL` from your Secret (or clear `secret.values.databaseRedisUrl`), revert `redis.allowInsecureInternal` if added, and restart Den API. No database schema change is involved.

## Step 2: Set pod resources and verify MySQL connection headroom

The Helm chart leaves `resources: {}` by default, which places pods in Kubernetes `BestEffort` QoS. Before running multiple API replicas, set explicit requests and limits and verify that MySQL has enough connection slots.

1. **Add provisional resource requests and limits to `openwork-values.yaml`.** These values are starting envelopes to leave `BestEffort` QoS, not load-tested sizing guarantees. Measure and adjust for your cluster.

   ```yaml theme={null}
   denApi:
     resources:
       requests:
         cpu: "500m"
         memory: "1Gi"
       limits:
         cpu: "2000m"
         memory: "2Gi"

   denWeb:
     resources:
       requests:
         cpu: "250m"
         memory: "512Mi"
       limits:
         cpu: "1000m"
         memory: "1Gi"

   # When AI Gateway is enabled:
   gateway:
     resources:
       requests:
         cpu: "500m"
         memory: "512Mi"
       limits:
         cpu: "2000m"
         memory: "1Gi"

   migrations:
     resources:
       requests:
         cpu: "250m"
         memory: "512Mi"
       limits:
         memory: "1Gi"
   ```

   Leave `config.denApiNodeOptions: ""` blank unless you need custom Node flags, so Node sizes its heap automatically from the container memory limit. If you set `--max-old-space-size` explicitly, keep it roughly 25% below `denApi.resources.limits.memory` (for example `--max-old-space-size=1536` inside a `2Gi` limit) to avoid OOMKills.

2. **Check MySQL `max_connections` before scaling.** With default pool settings (`config.database.pool`), each pod opens its own `mysql2` pool: up to `10` connections per `den-api` pod, and up to `20` connections per AI Gateway pod (`10` primary plus `2 * 5` auxiliary pools; releases prior to configurable pool defaults open up to `30` per AI Gateway pod). Double those per-pod totals if `DATABASE_READ_USERNAME` and `DATABASE_READ_PASSWORD` are configured for read replicas against the same MySQL server. Default rolling updates temporarily surge by 25% rounded up (one extra pod per Deployment up to four replicas), and the pre-upgrade migration Job uses up to `10` connections:

   ```text theme={null}
   Peak connections ~= 10 * (denApi + surge) + 20 * (gateway + surge) + 10 (migrate) + admin and CronJob headroom
   ```

   MySQL's default `max_connections` (`151`) fits `denApi=2, gateway=1` (\~95 peak without read replicas; \~115 on earlier releases with 30-connection Gateway defaults), but becomes tight at `denApi=3, gateway=2` (\~125 peak; \~155 on earlier releases) and leaves little margin if you raise `config.database.pool.connectionLimit`. Raise MySQL `max_connections` to `300` or higher, and verify MySQL CPU and memory headroom, before scaling beyond two Den API replicas or one Gateway replica, or before raising pool limits in [`config.database.pool`](https://github.com/different-ai/openwork/blob/dev/packaging/helm/openwork-ee/README.md#mysql-connection-pool).

## Step 3: Scale Den API replicas (optional)

Proceed only after **Step 1 (Redis)** and **Step 2 (resources and MySQL headroom)** are verified. Running `denApi.replicaCount > 1` without Redis degrades caching and breaks cross-pod workflow authoring receipts.

1. Set `denApi.replicaCount` (and optionally `denWeb.replicaCount` for web redundancy) in `openwork-values.yaml`:

   ```yaml theme={null}
   denApi:
     replicaCount: 2

   denWeb:
     replicaCount: 2
   ```

2. Render with `helm template`, verify the Deployment replica counts and resource blocks, and apply with `helm upgrade --reset-values --values openwork-values.yaml --wait --timeout 15m`.

### Verify Step 3

* Run `kubectl --namespace "$NAMESPACE" get pods` and confirm all `den-api` replicas are Ready and balanced under `kubectl top pods`.
* On MySQL, confirm `Max_used_connections` stays safely below `max_connections` (target under 50%).
* Verify end-to-end flows across replicas: sign in and sign out (confirming session revocation), open the dashboard and AI Gateway pages, run an MCP tool call from desktop, and test saving a workflow if enabled.
* If you mount local desktop installers on `den-api`, use a shared read-only PVC across replicas (see [Installer delivery](https://openworklabs.com/docs/start-here/installer-delivery.md)).

**Rollback:** Set `denApi.replicaCount: 1` in `openwork-values.yaml` and run `helm upgrade --reset-values`. No data is lost.

## Reference

See the [Helm chart reference](https://github.com/different-ai/openwork/blob/dev/packaging/helm/openwork-ee/README.md) for the complete `secret`, `redis`, `database.pool`, `denApiNodeOptions`, and worker-provisioning configuration options.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.