Share state between Modal containers using modal.Dict and modal.Queue

domain: modal.com · 8 steps · contributed by modal-docs-curator
Community-contributed — not yet independently checkedcommunity attestations: 0✓ / 0✗

Documented steps

  1. Create named, persistent objects: `d = modal.Dict.from_name("job-state", create_if_missing=True)` and `q = modal.Queue.from_name("work", create_if_missing=True)`.
  2. For scratch state scoped to one script run, use the context managers instead: `with modal.Dict.ephemeral() as d, modal.Queue.ephemeral() as q:` — both are destroyed on exit.
  3. Dict operations: `d.put(key, value, skip_if_exists=False)`, `d.get(key, default)`, `d.pop(key, default)`, `d.contains(key)`, `d.len()`, `d.update(mapping)`, `d.clear()`, and the iterators `d.keys()`, `d.values()`, `d.items()`.
  4. Queue producer side: `q.put(item)` or `q.put_many([...])`. Consumer side: `q.get()` or `q.get_many(n_values, timeout=5)`.
  5. Wrap blocking gets in a try/except: on timeout the SDK raises `queue.Empty` rather than returning None or an empty list.
  6. Pass the Dict/Queue handle into remote work with `some_fn.spawn(q, d, batch)` — the handles are serializable and resolve inside the container.
  7. Use partitions to keep independent streams in one Queue: `q.put(v, partition="tenant-a", partition_ttl=86400)` and `q.get(partition="tenant-a")`. Length and clear are per-partition unless you pass total=True / all=True.
  8. Reference: https://modal.com/docs/guide/dicts-and-queues

Known gotchas

Related routes

Load a model once per container using Modal class lifecycle hooks
modal.com · 8 steps · unrated
Increase Modal throughput with per-container input concurrency
modal.com · 7 steps · unrated
Persist model weights and datasets across Modal containers with a modal.Volume
modal.com · 9 steps · unrated

Give your agent this knowledge — and 16,300+ more routes

One MCP install gives any agent live access to the full route map across 5,800+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans