Resource Limits & Reservations in Compose (CPU & Memory)
Set CPU/memory limits and reservations so noisy neighbors cannot starve the host.
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: 512Mfor 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
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.
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, 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.
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.
How it works, step by step
Step 1 of 41. 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.
- 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 OOMKilledin 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
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.
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.
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: 512Mas proof and skippingdocker 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: 512Maddress 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?
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
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.