⚡ ~/naveed Interview Prep
⚡ Portfolio Home ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Docker & Containers Interview Questions Scenario 141 of 158 in Docker & Containers
Staff Infrastructure Architect Docker Container Runtime & Systems Engineering Production Scenario

Q: Your Redis cache containers and Java JVM microservices run on multi-tenant Docker hosts with host swap enabled. When host memory pressure occurs, the Linux kernel aggressively swaps out JVM heap pages to host swap disk, degrading API response times from 5ms to 1,200ms due to disk paging thrashing. Meanwhile, engineers are confused by how `--memory-swap` interacts with `--memory`. You must formulate the mathematical relationship of Docker memory flags, disable swap for latency-critical caches, and tune swappiness.

Configure and calculate Docker container memory ceilings: `--memory`, `--memory-swap`, and `--memory-swappiness`. Prevent severe disk thrashing caused by excessive swapping and avoid sudden OOM kills.

#Docker #Linux #Memory #Performance #SRE
🎙️ Candidate Opening & Architectural Context
"Configure and calculate Docker container memory ceilings: `--memory`, `--memory-swap`, and `--memory-swappiness`. Prevent severe disk thrashing caused by excessive swapping and avoid sudden OOM kills."
Advertisement
⚡ Recommended Practice Lab

Want to master this scenario in a live sandbox? KodeKloud's Docker Certified Associate (DCA) Hands-On Lab Course covers this exact problem with hands-on terminal drills.

🛠️ Production Runbook & Step-by-Step Resolution

Step 1

Deconstruct the Mathematical Relationship of Docker Memory Flags

Understand the precise calculation: `--memory-swap` represents total memory (RAM + SWAP combined), NOT swap alone. If `--memory=1g` and `--memory-swap=3g`, the container has 1GB of physical RAM and 2GB of swap space.

<!-- Docker Memory & Swap Formula -->
Total Container Memory Ceiling = --memory-swap
Physical RAM Allocation        = --memory
Actual Permitted Swap Space    = --memory-swap MINUS --memory

Rule Examples:
- Set RAM to 2GB, Swap to 1GB:     --memory=2g --memory-swap=3g
- Disable swap completely:         --memory=2g --memory-swap=2g (RAM == Total)
- Allow unlimited swap (Host max): --memory=2g --memory-swap=-1
Pro Tip: Deconstruct the Mathematical Relationship of Docker Memory Flags
Step 2

Completely Disable Swap for Latency-Critical Workloads

For latency-critical in-memory databases (Redis, Memcached) or garbage-collected runtimes (Java/Go), swapping degrades throughput catastrophically. Set `--memory-swap` equal to `--memory` to strictly prohibit swap paging.

# Prohibit swap paging for Redis
docker run -d \
  --name redis-cache \
  --memory="4g" \
  --memory-swap="4g" \
  redis:7-alpine
Pro Tip: Completely Disable Swap for Latency-Critical Workloads
Advertisement
Step 3

Tune Container Swappiness (--memory-swappiness)

Adjust the kernel's aggressiveness when swapping page cache versus anonymous memory using `--memory-swappiness` (0 to 100). A value of 0 instructs the kernel to avoid swapping process memory unless absolutely necessary.

# Set swappiness to 0 to prioritize file cache reclamation over anonymous heap paging
docker run -d \
  --name java-api \
  --memory="2g" \
  --memory-swap="3g" \
  --memory-swappiness=0 \
  openjdk:21-slim
Pro Tip: Tune Container Swappiness (--memory-swappiness)
Step 4

Verify Cgroup Memory and Swap Limits in the Host Kernel

Inspect the container's cgroup controllers to confirm limits are applied correctly by the kernel.

# In cgroups v2:
CONTAINER_ID=$(docker inspect redis-cache --format '{{.Id}}')
cat /sys/fs/cgroup/docker/${CONTAINER_ID}/memory.max
cat /sys/fs/cgroup/docker/${CONTAINER_ID}/memory.swap.max

# Inspect memory statistics and major page faults (disk paging)
cat /sys/fs/cgroup/docker/${CONTAINER_ID}/memory.stat | grep -E 'anon|file|pgpgin|pgpgout'
Pro Tip: Verify Cgroup Memory and Swap Limits in the Host Kernel
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"`--memory-swap` represents total memory (RAM plus Swap), not standalone swap. Setting `--memory-swap` equal to `--memory` prohibits swapping, preventing disk thrashing and maintaining microsecond response times for databases and garbage-collected runtimes."
⚡ 60-Second Elevator Pitch Talking Points
  • W
  • e
  • s
  • o
  • l
  • v
  • e
  • d
  • s
  • e
  • v
  • e
  • r
  • e
  • 1
  • ,
  • 0
  • 0
  • 0
  • m
  • s
  • l
  • a
  • t
  • e
  • n
  • c
  • y
  • s
  • p
  • i
  • k
  • e
  • s
  • i
  • n
  • o
  • u
  • r
  • c
  • o
  • n
  • t
  • a
  • i
  • n
  • e
  • r
  • i
  • z
  • e
  • d
  • J
  • a
  • v
  • a
  • a
  • n
  • d
  • R
  • e
  • d
  • i
  • s
  • s
  • e
  • r
  • v
  • i
  • c
  • e
  • s
  • b
  • y
  • c
  • o
  • r
  • r
  • e
  • c
  • t
  • i
  • n
  • g
  • t
  • h
  • e
  • i
  • r
  • m
  • e
  • m
  • o
  • r
  • y
  • f
  • l
  • a
  • g
  • s
  • .
  • B
  • e
  • c
  • a
  • u
  • s
  • e
  • `
  • -
  • -
  • m
  • e
  • m
  • o
  • r
  • y
  • -
  • s
  • w
  • a
  • p
  • `
  • d
  • e
  • f
  • i
  • n
  • e
  • s
  • t
  • o
  • t
  • a
  • l
  • R
  • A
  • M
  • p
  • l
  • u
  • s
  • s
  • w
  • a
  • p
  • ,
  • s
  • e
  • t
  • t
  • i
  • n
  • g
  • `
  • -
  • -
  • m
  • e
  • m
  • o
  • r
  • y
  • -
  • s
  • w
  • a
  • p
  • `
  • e
  • q
  • u
  • a
  • l
  • t
  • o
  • `
  • -
  • -
  • m
  • e
  • m
  • o
  • r
  • y
  • `
  • a
  • l
  • o
  • n
  • g
  • s
  • i
  • d
  • e
  • `
  • -
  • -
  • m
  • e
  • m
  • o
  • r
  • y
  • -
  • s
  • w
  • a
  • p
  • p
  • i
  • n
  • e
  • s
  • s
  • =
  • 0
  • `
  • c
  • o
  • m
  • p
  • l
  • e
  • t
  • e
  • l
  • y
  • p
  • r
  • e
  • v
  • e
  • n
  • t
  • e
  • d
  • t
  • h
  • e
  • k
  • e
  • r
  • n
  • e
  • l
  • f
  • r
  • o
  • m
  • p
  • a
  • g
  • i
  • n
  • g
  • a
  • p
  • p
  • l
  • i
  • c
  • a
  • t
  • i
  • o
  • n
  • h
  • e
  • a
  • p
  • s
  • t
  • o
  • d
  • i
  • s
  • k
  • ,
  • r
  • e
  • s
  • t
  • o
  • r
  • i
  • n
  • g
  • s
  • t
  • e
  • a
  • d
  • y
  • -
  • s
  • t
  • a
  • t
  • e
  • 5
  • m
  • s
  • l
  • a
  • t
  • e
  • n
  • c
  • y
  • .
Advertisement
Want more Docker & Containers scenarios?
Explore our complete collection of scenario-based Docker & Containers interview runbooks.
Browse All Docker & Containers Questions →