Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 95/ 160
05. Docker Compose & Multi-Container Apps

Resource Limits & Reservations in Compose (CPU & Memory)

Set CPU/memory limits and reservations so noisy neighbors cannot starve the host.

Intermediatecpus · memory3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Translate observed working sets and CPU saturation into caps, then load-test memory OOM and CPU throttling explicitly.

Think of it like…

Limits are a fuse box and throttle; reservations are reserved parking spaces that do nothing to the car driving outside its space.

Where you’ve seen it

In practice, deploy: { resources: { limits: { cpus: "0.75", memory: 512M }, reservations: { memory: 256M } } }; mem_limit: 512M is the concrete move for this job: set CPU/memory limits and reservations so noisy neighbors cannot starve the host. The release is not accepted until docker stats --no-stream and docker inspect memory/cpu config, then force a controlled memory allocation to see OOMKilled confirms the result.

Words you’ll hear

cpus
The implementation control in deploy: { resources: { limits: { cpus: "0.75", memory: 512M }, reservations: { memory: 256M } } }; mem_limit: 512M for set CPU/memory limits and reservations so noisy neighbors cannot starve the host.
memory
The result to observe with docker stats --no-stream and docker inspect memory/cpu config, then force a controlled memory allocation to see OOMKilled, rather than infer from a successful command alone.
Reservations
The production boundary exposed when setting reservations and expecting the non-Swarm Compose daemon to enforce them everywhere, or setting a memory cap below the JVM heap plus native overhead.

Visual diagram

Loading interactive diagram

Theory & deep dive

cpus control path

The concrete mechanism is deploy: { resources: { limits: { cpus: "0.75", memory: 512M }, reservations: { memory: 256M } } }; mem_limit: 512M. The observable result is docker stats --no-stream and docker inspect memory/cpu config, then force a controlled memory allocation to see OOMKilled; if those disagree, inspect the runtime evidence before changing unrelated settings.

Set CPU/memory limits and reservations so noisy neighbors cannot starve the host. Keep cpus, memory and Reservations in the same deployment record so the intent and the observed state cannot drift apart.

Concrete cpus setup

deploy: { resources: { limits: { cpus: "0.75", memory: 512M }, reservations: { memory: 256M } } }; mem_limit: 512M

Key takeaway: Prove memory with docker stats --no-stream and docker inspect memory/cpu config, then force a controlled memory allocation to see OOMKilled, not with command success alone.

Reservations failure boundary

A Java/Node service can still allocate until the guest process believes more memory exists than its cgroup limit. An overly tight CPU cap throttles latency, and a reservation without a limit does not prevent a runaway query from consuming the host. The narrow failure to design for is setting reservations and expecting the non-Swarm Compose daemon to enforce them everywhere, or setting a memory cap below the JVM heap plus native overhead.

Accept this compromise explicitly: Limits protect the host and reveal capacity needs; reservations aid placement in orchestrators but add headroom that may reduce density. The controlled check-docker stats --no-stream and docker inspect memory/cpu config, then force a controlled memory allocation to see OOMKilled-also identifies the point where a different design is safer.

Deliberate failure test

Exercise this failure safely: setting reservations and expecting the non-Swarm Compose daemon to enforce them everywhere, or setting a memory cap below the JVM heap plus native overhead.

Key takeaway: Limits protect the host and reveal capacity needs; reservations aid placement in orchestrators but add headroom that may reduce density.

How it works, step by step

Step 1 of 4
Design

1. State the boundary

Write down why the workload needs this: A Java/Node service can still allocate until the guest process believes more memory exists than its cgroup limit. An overly tight CPU cap throttles latency, and a reservation without a limit does not prevent a runaway query from consuming the host.

Translate observed working sets and CPU saturation into caps, then load-test memory OOM and CPU throttling explicitly.
Key numbers at this step
Intent
cpus

When to use it & trade-offs

What it is

Compose cpus and mem_limit constrain a service at runtime; reservation-oriented deploy resources inform compatible orchestrators or may be advisory in one-daemon Compose. Resource limits are enforced by host cgroups and should leave headroom for the daemon and neighboring workloads.

Why it matters: A Java/Node service can still allocate until the guest process believes more memory exists than its cgroup limit. An overly tight CPU cap throttles latency, and a reservation without a limit does not prevent a runaway query from consuming the host.

When to reach for it

  • The fit is strongest for this job: set CPU/memory limits and reservations so noisy neighbors cannot starve the host. Pair it with docker stats --no-stream and docker inspect memory/cpu config, then force a controlled memory allocation to see OOMKilled in the runbook.
  • Production cpus changes should make memory observable through deploy: { resources: { limits: { cpus: "0.75", memory: 512M }, reservations: { memory: 256M } } }; mem_limit: 512M, rather than trusting the runtime default.
  • Keep a rollback example for the case where setting reservations and expecting the non-Swarm Compose daemon to enforce them everywhere, or setting a memory cap below the JVM heap plus native overhead.

When to avoid or be careful

  • Do not accept command success while the system can still fail by setting reservations and expecting the non-Swarm Compose daemon to enforce them everywhere, or setting a memory cap below the JVM heap plus native overhead.
  • Avoid erasing Reservations protection merely to dodge this cost: “Limits protect the host and reveal capacity needs; reservations aid placement in orchestrators but add headroom that may reduce density”.
  • Do not replace the evidence path-docker stats --no-stream and docker inspect memory/cpu config, then force a controlled memory allocation to see OOMKilled-with a screenshot or a mutable tag.

deploy: { resources: { limits: { cpus: "0.75", memory: 512M }, reservations: { memory: 256M } } }; mem_limit: 512M: practical choices

Explicit cpus
Best for: Set CPU/memory limits and reservations so noisy neighbors cannot starve the host, with a reviewed runbook.

Strengths: Compose cpus and mem_limit constrain a service at runtime; reservation-oriented deploy resources inform compatible orchestrators or may be advisory in one-daemon Compose. Resource limits are enforced by host cgroups and should leave headroom for the daemon and neighboring workloads.

Watch out: Limits protect the host and reveal capacity needs; reservations aid placement in orchestrators but add headroom that may reduce density.

Ambient default
Best for: Short-lived memory experiments with low blast radius.

Strengths: It can begin with deploy: { resources: { limits: { cpus: "0.75", memory: 512M }, reservations: { memory: 256M } } }; mem_limit: 512M and fewer surrounding controls.

Watch out: It silently accepts setting reservations and expecting the non-Swarm Compose daemon to enforce them everywhere, or setting a memory cap below the JVM heap plus native overhead, which makes the result hard to reproduce after promotion.

Managed Reservations
Best for: Teams that centralize Reservations policy across many workloads.

Strengths: A Java/Node service can still allocate until the guest process believes more memory exists than its cgroup limit. An overly tight CPU cap throttles latency, and a reservation without a limit does not prevent a runaway query from consuming the host.

Watch out: The platform adds an ownership and debugging layer; retain docker stats --no-stream and docker inspect memory/cpu config, then force a controlled memory allocation to see OOMKilled as the direct check.

Interview signals & numbers to know

When this topic comes up

  • deploy: { resources: { limits: { cpus: "0.75", memory: 512M }, reservations: { memory: 256M } } }; mem_limit: 512M
  • docker stats --no-stream and docker inspect memory/cpu config, then force a controlled memory allocation to see OOMKilled
  • Failure signature: setting reservations and expecting the non-Swarm Compose daemon to enforce them everywhere, or setting a memory cap below the JVM heap plus native overhead
  • Trade-off: Limits protect the host and reveal capacity needs; reservations aid placement in orchestrators but add headroom that may reduce density.

Benchmarks & rules of thumb

0.75 CPU -
quota for the example service
512 MiB -
hard memory ceiling
256 MiB -
requested/reserved baseline in the example

Common mistakes & follow-ups

Common interview mistakes

  • Treating a successful deploy: { resources: { limits: { cpus: "0.75", memory: 512M }, reservations: { memory: 256M } } }; mem_limit: 512M as proof and skipping docker stats --no-stream and docker inspect memory/cpu config, then force a controlled memory allocation to see OOMKilled.
  • Setting reservations and expecting the non-Swarm Compose daemon to enforce them everywhere, or setting a memory cap below the JVM heap plus native overhead
  • Optimizing cpus without accepting that limits protect the host and reveal capacity needs; reservations aid placement in orchestrators but add headroom that may reduce density.

Likely interviewer follow-ups

  • Why does deploy: { resources: { limits: { cpus: "0.75", memory: 512M }, reservations: { memory: 256M } } }; mem_limit: 512M address cpus here rather than through application code?
  • How would you distinguish healthy memory from the failure “setting reservations and expecting the non-Swarm Compose daemon to enforce them everywhere, or setting a memory cap below the JVM heap plus native overhead” using docker stats --no-stream and docker inspect memory/cpu config, then force a controlled memory allocation to see OOMKilled?
  • When would this trade-off-“Limits protect the host and reveal capacity needs; reservations aid placement in orchestrators but add headroom that may reduce density”-justify a different Reservations design?
Interview tip: Give one concrete operation-deploy: { resources: { limits: { cpus: "0.75", memory: 512M }, reservations: { memory: 256M } } }; mem_limit: 512M-and one proof-docker stats --no-stream and docker inspect memory/cpu config, then force a controlled memory allocation to see OOMKilled. Then name the boundary: setting reservations and expecting the non-Swarm Compose daemon to enforce them everywhere, or setting a memory cap below the JVM heap plus native overhead.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Resource Limits & Reservations in Compose (CPU & Memory) with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Live Development Workflows with docker compose watch (Sync & Rebuild)
Next topic
Logging Drivers & Log Rotation in Docker Compose