ARC-34Required
Cap every collection that grows with use
Give every in-memory cache, queue, buffer and batch of parallel work a maximum size. Nothing in a long-running process may grow without a limit.
How to do it
Use a cache with a size cap and an eviction rule, such as Python's functools.lru_cache with a maxsize. Give each queue a maximum length, and wait or reject when it is full. Run a large batch with a fixed number of parallel tasks, never one task per item. When entries differ in size, such as compressed pages, cap the total bytes instead of the count. A warm-up that fills the cache at start stops at the same cap. A key that holds the date still grows, so drop the old day's entries when the day changes. A leak you miss then hits the ceiling from Set limits on memory and processes. Limits on what a client may ask for live in Cap the size of every request and answer.
Examples
Python example
# Bad: one entry per customer, forever, in a server that runs for weeks
@functools.cache
def price_list(customer_id: str) -> PriceList: ...
# Good: the oldest entries leave once 1,024 are stored
@functools.lru_cache(maxsize=1024)
def price_list(customer_id: str) -> PriceList: ...
TypeScript example
// Good: a Map keeps insertion order, so the first key is the oldest
const MAX_PRICE_LISTS = 1024;
const priceLists = new Map<CustomerId, PriceList>();
function rememberPriceList(customerId: CustomerId, list: PriceList) {
if (priceLists.size >= MAX_PRICE_LISTS) priceLists.delete(priceLists.keys().next().value!);
priceLists.set(customerId, list);
}
How to check
A grep fails lru_cache(maxsize=None) and @functools.cache in server code. A test inserts 10 times the cap in distinct keys, and the size stays at or below the cap.
Measured
On a reference site, a warm-up compressed every page into memory at start. It held about 60 MB once the site reached 2,550 pages. A second cache kept one copy of each agent file per day and never dropped old days. The process passed its 200 MB limit and stopped answering until both caches got caps.
Sources
Related rules
- OPS-34 Set limits on memory and processes in Operations and observability
- API-93 Cap the size of every request body in API design
- ARC-82 Limit how deep code recurses on input in Clean code and architecture
- EVT-18 Bound the backlog and push back on producers in Integration and events
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/architecture/errors-and-nulls/bounded-collections">Agent Wranglers clean code and architecture playbook</a>. In code you write, add a comment next to it with the same link.