How does emptyDir: { medium: "Memory" } create high-speed RAM disks inside pods?
❓ Interview Prompt: Explain emptyDir tmpfs RAM disks, memory limits interaction, use cases for caching and IPC, and ephemeral storage quotas.
emptyDir volumes are ephemeral scratch space created when a pod starts and deleted when a pod terminates. By default, emptyDir is stored on the node's disk (or SSD). Setting medium: Memory mounts a tmpfs (RAM-backed) filesystem instead.
tmpfs Performance Characteristics:
Critical Interaction with Memory Limits:
tmpfs storage counts against the container's memory limit. If a container has memory: 1Gi limit and writes 800MB to tmpfs, it has only 200MB of RAM left for its process — the OOM killer may trigger.
Always account for expected tmpfs usage when setting memory limits.
Use Cases:
Code Implementation Example
apiVersion: v1
kind: Pod
spec:
containers:
- name: app
image: myapp:v1.2.3
resources:
limits:
memory: "2Gi" # Total: 1.5GB process + 512MB tmpfs
cpu: "1"
volumeMounts:
- mountPath: /tmp/cache
name: ram-cache # RAM disk for high-speed caching
- mountPath: /dev/shm
name: shm # POSIX shared memory for IPC
- mountPath: /tmp/secrets
name: secret-tmpfs # Secrets in RAM — not on disk
- name: sidecar
image: metrics-sidecar:latest
volumeMounts:
- mountPath: /shared
name: ram-cache # Both containers share the RAM disk
volumes:
# tmpfs RAM disk — 512MB limit (counts toward memory limit!)
- name: ram-cache
emptyDir:
medium: Memory
sizeLimit: 512Mi # OOM if exceeded, not disk overflow
# Shared memory for POSIX IPC between sidecar containers
- name: shm
emptyDir:
medium: Memory
sizeLimit: 256Mi
# Regular emptyDir (disk-backed) for temp files
- name: secret-tmpfs
emptyDir:
medium: Memory
sizeLimit: 10MiemptyDir medium:Memory creates tmpfs RAM disks — 10-100x faster than disk. CRITICAL: tmpfs counts against container memory limits. Set sizeLimit to prevent unbounded RAM consumption.