Redis
Overview¶
This wiki documents the production Redis deployment for the ticketing system on k3s, used as a cache layer to prevent memory spikes from repeated external API calls.
Why Redis¶
The ticketing app calls two external APIs on every request:
eglobal2.ddns.net/johorzooapi/api/JohorZoo/GetOnlineItem2
eglobal2.ddns.net:18086/KSMWebAPI/api/ThirdParties/GetOnlineItem
Without Redis, concurrent requests load full day ticket data into memory simultaneously causing OOMKill:
No cache:
Request 1 → loads 100MB into memory
Request 2 → loads 100MB into memory
Request 3 → loads 100MB into memory
Total → 300MB+ → OOMKill
With Redis:
Request 1 → loads from API → caches in Redis
Request 2 → served from Redis cache → ~0MB memory spike
Architecture¶
| Component | Namespace | Type | Storage |
|---|---|---|---|
| Redis | production |
StatefulSet | Longhorn (1Gi) |
Storage Class¶
A dedicated longhorn-redis storage class is used, consistent with the pattern of other services (longhorn-postgres, longhorn-metrics).
# 00-storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: longhorn-redis
provisioner: driver.longhorn.io
reclaimPolicy: Retain
volumeBindingMode: Immediate
allowVolumeExpansion: true
parameters:
numberOfReplicas: "2"
staleReplicaTimeout: "2880"
fromBackup: ""
| Parameter | Value | Reason |
|---|---|---|
reclaimPolicy |
Retain |
Data survives PVC deletion |
allowVolumeExpansion |
true |
Can resize PVC without downtime |
numberOfReplicas |
2 |
Data replicated across 2 nodes |
staleReplicaTimeout |
2880 |
48 hours before marking replica stale |
Repository Structure¶
overlays/prod/redis/
├── 00-storageclass.yaml
├── 01-secret.yaml
├── 02-statefulset.yaml
├── 03-service.yaml
└── kustomization.yaml
Files¶
00-storageclass.yaml¶
See Storage Class section above.
01-secret.yaml¶
apiVersion: v1
kind: Secret
metadata:
name: redis-secret
namespace: production
type: Opaque
stringData:
password: "<REDIS_PASSWORD>" # real value is kept only in the cluster Secret; never commit it
02-statefulset.yaml¶
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
namespace: production
spec:
serviceName: redis-headless
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:7-alpine
command:
- redis-server
- --appendonly
- "yes"
- --appendfsync
- everysec
- --maxmemory
- 128mb
- --maxmemory-policy
- allkeys-lru
- --requirepass
- $(REDIS_PASSWORD)
ports:
- containerPort: 6379
name: redis
env:
- name: REDIS_PASSWORD
valueFrom:
secretKeyRef:
name: redis-secret
key: password
volumeMounts:
- name: redis-data
mountPath: /data
resources:
requests:
memory: "64Mi"
cpu: "50m"
limits:
memory: "192Mi"
cpu: "200m"
livenessProbe:
exec:
command:
- sh
- -c
- redis-cli -a $REDIS_PASSWORD ping
initialDelaySeconds: 10
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 5
readinessProbe:
exec:
command:
- sh
- -c
- redis-cli -a $REDIS_PASSWORD ping
initialDelaySeconds: 5
periodSeconds: 5
timeoutSeconds: 3
failureThreshold: 3
volumeClaimTemplates:
- metadata:
name: redis-data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: longhorn-redis
resources:
requests:
storage: 1Gi
Redis command flags explained¶
| Flag | Value | Purpose |
|---|---|---|
--appendonly |
yes |
AOF persistence — survives restart |
--appendfsync |
everysec |
Flush to disk every 1 second — balanced safety/performance |
--maxmemory |
128mb |
Redis data limit — 67% of pod memory limit |
--maxmemory-policy |
allkeys-lru |
Evict least recently used keys when full |
--requirepass |
$(REDIS_PASSWORD) |
Password from secret |
Why both maxmemory and pod memory limit¶
--maxmemory 128mb ← Redis data limit
memory limit 192Mi ← Kubernetes container limit
Redis total usage = data (128MB) + process overhead (~30MB) = ~158MB
Pod limit 192Mi safely covers both
Without maxmemory → Redis could grow to 180MB data
+ 30MB overhead = 210MB → exceeds 192Mi → OOMKill
03-service.yaml¶
# Headless service — required by StatefulSet for stable pod DNS
# Used internally by Kubernetes, not by the app
apiVersion: v1
kind: Service
metadata:
name: redis-headless
namespace: production
spec:
selector:
app: redis
clusterIP: None
ports:
- port: 6379
targetPort: 6379
name: redis
---
# ClusterIP service — your app connects to this
apiVersion: v1
kind: Service
metadata:
name: redis
namespace: production
spec:
selector:
app: redis
ports:
- port: 6379
targetPort: 6379
name: redis
Note: The headless service is a Kubernetes requirement for StatefulSet — it gives each pod a stable DNS name (
redis-0.redis-headless). Your app does not use it directly. With a single replica you don't technically need it, but keeping it makes future scaling easier.
kustomization.yaml¶
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: production
resources:
- 00-storageclass.yaml
- 01-secret.yaml
- 02-statefulset.yaml
- 03-service.yaml
App Configuration¶
The ticketing app is already configured to connect to Redis via ticketing-config ConfigMap:
REDIS_HOST: redis.production.svc.cluster.local
REDIS_PORT: 6379
REDIS_PASSWORD: <REDIS_PASSWORD> ← must match redis-secret
No app code changes needed — the app already uses go-redis/v9 and gracefully falls back if Redis is unavailable:
Deployment¶
# Apply storage class first
kubectl apply -f 00-storageclass.yaml
# Apply everything via kustomize
kubectl apply -k overlays/prod/redis/
# Verify
kubectl get pods -n production | grep redis
kubectl exec -it redis-0 -n production -- redis-cli -a <REDIS_PASSWORD> ping
# Expected: PONG
Verification¶
# Check Redis is running
kubectl get pods -n production | grep redis
# Check app connected (no WARN Redis unavailable in logs)
kubectl logs -n production <ticketing-pod> | grep -i redis
# Check cache keys are being set
kubectl exec -it redis-0 -n production -- redis-cli -a <REDIS_PASSWORD> keys "*"
# Check total key count
kubectl exec -it redis-0 -n production -- redis-cli -a <REDIS_PASSWORD> dbsize
# Check memory usage
kubectl exec -it redis-0 -n production -- redis-cli -a <REDIS_PASSWORD> info memory | grep used_memory_human
Maintenance¶
Expand PVC¶
kubectl edit pvc redis-data-redis-0 -n production
# Change storage: 1Gi → storage: 2Gi
# Longhorn handles resize automatically — no downtime
Increase memory limit¶
Edit 02-statefulset.yaml:
resources:
limits:
memory: "256Mi" # increase from 192Mi
# Also increase --maxmemory proportionally
# --maxmemory 180mb (70% of 256Mi)
Flush cache manually¶
Scaling Considerations¶
| Option | Use case | Complexity |
|---|---|---|
| Single replica (current) | Cache only, app tolerates Redis downtime | Low |
| Redis Sentinel | HA needed, auto failover | Medium — requires app code change |
| Redis Cluster | Massive data, horizontal write scaling | High — overkill for cache |
Current setup is sufficient — Redis is cache only and the app gracefully falls back to calling the external API if Redis is unavailable. Sentinel/Cluster only needed if Redis becomes a hard dependency.
Important: Simply increasing
replicas: 3without Redis Cluster configuration will break everything — all pods think they are master and data becomes inconsistent.
Troubleshooting¶
App still shows Redis unavailable¶
# Check Redis pod is running
kubectl get pods -n production | grep redis
# Check password matches between secret and configmap
kubectl get secret redis-secret -n production -o jsonpath='{.data.password}' | base64 -d
kubectl get configmap ticketing-config -n production -o jsonpath='{.data.REDIS_PASSWORD}'
Redis OOMKilled¶
# Check memory usage
kubectl top pods -n production | grep redis
# Check if maxmemory is set correctly
kubectl exec -it redis-0 -n production -- redis-cli -a <REDIS_PASSWORD> config get maxmemory
Increase both --maxmemory flag and pod memory limit proportionally.
PVC expansion not working¶
Verify allowVolumeExpansion: true is set on the storage class: