Nearly every service in my search engine's Docker setup had a mem_limit. That's good practice on small shared servers, and it was behind a good part of the summer's mysterious slowness, because a limit only works if you know everything that has to fit under it. Over July and August, the MySQL container and the OpenSearch container each had their limits changed three times. Each change taught something different about what a database actually keeps in memory.
MySQL: the buffer pool plus everything else
On the pipeline server, MySQL ran with a 2 GB InnoDB buffer pool inside a 2,560 MB container. That looks like plenty. In practice the container sat at 97% of its limit, with about 582 MB swapped out.
Two usual suspects could be ruled out. The configuration used innodb_flush_method = O_DIRECT, which MySQL documents as opening the data files with O_DIRECT, bypassing the operating system's page cache for data files, so file caching wasn't inflating the container's memory. And the per-connection buffers were negligible: max_connections was 151, but only about 3 connections were open. What was left was simply the buffer pool, the log buffer and the server's own overhead, which didn't fit. The limit went up to 3,072 MB.
Two weeks later the container was at 92.57% of that, and the limit went to 4,096 MB, with the extra gigabyte going into the buffer pool itself, now 3 GB.
The third time was the expensive one. While profiling the slowest live query, I found outliers of 15 to 319 seconds on a query with almost no lock time and correct index use. The container was at 99.8% of its 4 GB limit, and the host showed 2.2 GB of active swap. A database whose memory is being swapped looks exactly like a database with bad queries, except that the slow ones don't correlate with anything in the query. The limit went to 5,632 MB, sized from the memory that was really free on the host (2.1 GB available), not from the budget written at the top of the compose file, which by then no longer matched reality. The buffer pool stayed at 3 GB: the problem was the squeeze, not the cache size.
OpenSearch: the heap that wasn't locked
On the frontend server, OpenSearch was intermittently throwing circuit_breaking_exception under light query load. Its circuit breaker is there to refuse requests that would exhaust the JVM heap, and OpenSearch's documentation says the parent breaker considers actual memory usage by default. So the breaker was reading real memory, and real memory was misbehaving.
The cause was swapping. The container had been granted permission to lock memory, through its memlock ulimit, but bootstrap.memory_lock had never been set, so nothing used it. OpenSearch's own Docker example sets bootstrap.memory_lock=true with the comment "Disable JVM heap memory swapping". Without it, about 470 MB of heap had been swapped out under host memory pressure, which makes garbage collection pauses long and breaker readings erratic. The fix enabled the lock and raised the container limit from 1,280 to 1,792 MB, so the 768 MB heap, the JVM's non-heap memory and Lucene's overhead could all stay resident.
That stopped the swapping and not the breaker. With the heap now fully resident, real usage under ordinary load reached 733 to 750 MB of the 768 MB heap. That wasn't a swap artefact anymore; the heap was too small for the node's shards plus continuous indexing from another server. It went to 1,024 MB, in a 2,304 MB container.
The same commit capped Prometheus, Loki and Grafana, the only services on that server with no limit at all, and free to grow into the memory OpenSearch needed.
OpenSearch again: memory that isn't heap
A month later, the remaining search latency turned out to be cold reads. A query nobody had run before took about 60 times longer than the same query repeated, 468 ms against 7.7 ms, and the only difference was what was cached.
That cache isn't the JVM heap. Lucene's index files are read through the operating system's page cache, and the kernel's cgroup documentation lists page cache among the memory a cgroup's memory controller tracks. So the container's limit caps the heap, and the page cache that makes searches fast, together. The fix raised the container limit from 2,304 to 2,816 MB and deliberately left the heap at 1,024 MB: a bigger heap would have added garbage-collection risk and done nothing for cold reads.
What has to fit under the limit
The common lesson is that a database container's memory limit isn't the database's memory setting:
- For MySQL with
O_DIRECT: the buffer pool, plus log buffers, plus per-connection memory, plus the server's own overhead. - For OpenSearch: the heap, plus the JVM's non-heap memory, plus the page cache Lucene's reads depend on. And lock the heap, or it can be swapped.
- For both: when a container sits near its limit with swap in use, the symptoms look like slow queries or random failures, not like a memory problem.
Check the container's resident memory and the host's swap before tuning the query. Several of these incidents looked like application problems for exactly as long as nobody looked at memory.
Leave a comment
Comments are reviewed before they appear.