> ## Documentation Index
> Fetch the complete documentation index at: https://ayakaleaf-pro.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 3. Deploy MongoDB and Redis

> Run MongoDB as a replica set and Redis with persistence, or point to your own.

Ayakaleaf Pro needs:

* **MongoDB 8** as a **replica set** (it uses transactions). Three members spread over three nodes survive the loss of a node; one member also works.
* **Redis 7**, one server with AOF persistence. Redis holds the edits that are not yet written to MongoDB, the sessions, and the messages between Overleaf services. Overleaf connects to a single Redis server: Sentinel and Redis Cluster are not supported.

> [!NOTE]
>
> You can use managed services instead, such as MongoDB Atlas or your cloud's Redis. Skip this page and set `OVERLEAF_MONGO_URL` and `OVERLEAF_REDIS_HOST` to them in [step 4](https://ayakaleaf-pro.mintlify.site/on-premises/installation/using-kubernetes/4.-personalizing-your-instance.md).

**1. MongoDB**

```yaml title="10-mongo.yaml" wrap theme={null}
apiVersion: v1
kind: Service
metadata:
  name: mongo
  namespace: overleaf
spec:
  clusterIP: None                 # headless: gives mongo-0.mongo, mongo-1.mongo, ...
  publishNotReadyAddresses: true  # members must resolve each other before the set is initiated
  selector:
    app: mongo
  ports:
    - name: mongo
      port: 27017
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo
  namespace: overleaf
spec:
  serviceName: mongo
  replicas: 3
  selector:
    matchLabels:
      app: mongo
  template:
    metadata:
      labels:
        app: mongo
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchLabels:
                  app: mongo
              topologyKey: kubernetes.io/hostname
      containers:
        - name: mongo
          image: mongo:8.0
          args:
            - --replSet=overleaf
            - --bind_ip_all
            - --wiredTigerCacheSizeGB=0.25   # default is 50% of (RAM - 1 GB); size it for your nodes
          # env:
          #   - name: GLIBC_TUNABLES        # only on kernel 6.19+ (SERVER-121912)
          #     value: glibc.pthread.rseq=1
          ports:
            - containerPort: 27017
          readinessProbe:
            tcpSocket:
              port: 27017
          resources:
            requests:
              cpu: 100m
              memory: 384Mi
            limits:
              memory: 768Mi
          volumeMounts:
            - name: data
              mountPath: /data/db
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: [ReadWriteOnce]
        resources:
          requests:
            storage: 5Gi
---
# Initiates the replica set once. Safe to re-run: it does nothing when the set exists.
apiVersion: batch/v1
kind: Job
metadata:
  name: mongo-init-replica-set
  namespace: overleaf
spec:
  backoffLimit: 20
  template:
    spec:
      restartPolicy: OnFailure
      containers:
        - name: init
          image: mongo:8.0
          command:
            - mongosh
            - --quiet
            - --host=mongo-0.mongo
            - --eval
            - |
              try {
                rs.status()
                print('replica set already initiated')
              } catch (e) {
                rs.initiate({
                  _id: 'overleaf',
                  members: [
                    { _id: 0, host: 'mongo-0.mongo.overleaf.svc.cluster.local:27017' },
                    { _id: 1, host: 'mongo-1.mongo.overleaf.svc.cluster.local:27017' },
                    { _id: 2, host: 'mongo-2.mongo.overleaf.svc.cluster.local:27017' },
                  ],
                })
              }
```

```bash theme={null}
kubectl apply -f 10-mongo.yaml
kubectl -n overleaf rollout status statefulset/mongo
kubectl -n overleaf wait --for=condition=complete job/mongo-init-replica-set --timeout=5m
kubectl -n overleaf exec mongo-0 -- mongosh --quiet --eval 'rs.status().members.map(m => m.name + " " + m.stateStr)'
```

One member should be `PRIMARY` and the others `SECONDARY` after a few seconds. The Job retries until all members answer, so a few failed Job pods are normal.

* The headless Service needs `publishNotReadyAddresses: true`: the members must find each other by name before the replica set exists.
* The members are added with their stable names (`mongo-0.mongo.overleaf.svc.cluster.local`), never pod IPs, which change on every restart.
* For a single member, set `replicas: 1` and keep only `mongo-0` in the Job.

**2. Redis**

```yaml title="20-redis.yaml" wrap theme={null}
apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: overleaf
spec:
  selector:
    app: redis
  ports:
    - name: redis
      port: 6379
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
  namespace: overleaf
spec:
  serviceName: redis
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis
          image: redis:7.4
          args: [--appendonly, "yes"]
          ports:
            - containerPort: 6379
          readinessProbe:
            tcpSocket:
              port: 6379
          resources:
            requests:
              cpu: 50m
              memory: 128Mi
            limits:
              memory: 512Mi
          volumeMounts:
            - name: data
              mountPath: /data
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: [ReadWriteOnce]
        resources:
          requests:
            storage: 2Gi
```

```bash theme={null}
kubectl apply -f 20-redis.yaml
kubectl -n overleaf rollout status statefulset/redis
```

### Q\&A

> [accordion] MongoDB 8 crashes at startup on a recent kernel. Why?
>
> **Answer:** MongoDB 8 fails on Linux kernel 6.19 and later ([SERVER-121912](https://jira.mongodb.org/browse/SERVER-121912)). Uncomment the `GLIBC_TUNABLES` environment variable in `10-mongo.yaml`.

> [accordion] How much memory does MongoDB need?
>
> **Answer:** By default MongoDB uses half of the node's memory minus 1 GB for its cache, and it sees the node's memory, not the pod's limit. Set `--wiredTigerCacheSizeGB` to fit your nodes; `0.25` kept each member at about 200 to 260 MiB on our 4 GB test nodes.


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