Why this lab exists
Every node-based visual tool you have used — ComfyUI for diffusion, Blender’s shader editor, LabVIEW for instrument control, Unreal’s Material Editor — is built on the same idea: a directed acyclic graph where nodes are processing steps and edges are typed data channels. The execution engine underneath is always the same shape: topological sort, run in order, pass outputs as inputs downstream.
ComfyUI went from zero to the dominant interface for Stable Diffusion pipelines in under a year. Practitioners use it because it exposes the actual graph structure of the inference pipeline — you can see the checkpoint loader, the CLIP encoder, the sampler, and the VAE decoder as separate nodes, wire them however you want, and add custom nodes without touching the core. When ComfyUI’s executor caches intermediate results and decides which nodes need to re-run, it is doing exactly what this lab builds.
This matters for two reasons. First, understanding the executor unlocks building custom pipelines without the frontend: you can POST a workflow JSON to the server API, drive it programmatically, and integrate it into a Java service. Second, the design patterns here — content-addressed caching, LRU resource management, topological scheduling — appear everywhere in data pipeline systems (dbt, Airflow, Bazel) and are worth understanding on their own terms.
This lab builds the executor in Python, from a 250-line DAG runner to a 900-line server with HTTP API. The focus is on the three hard problems in every node executor: ordering, caching, and resource management.
What we’re building
A ComfyUI-compatible node executor in Python, implemented in three stages.
We build:
WorkflowGraph: nodes + edges, with Kahn’s topological sort and cycle detectionexecute_workflow: runs nodes in topological order, passing outputs as inputs to downstream nodesCachingExecutor: content-addressed cache keyed by sha256 of node type, params, and input hashesinvalidate_downstream: BFS to mark all nodes reachable from a changed node as staleModelRegistry: LRU eviction pool for GPU model slots, with configurable max slotsOffloadingExecutor: wraps the caching executor, callsensure_loadedbefore model nodesparse_comfyui_json: parses the ComfyUIworkflow_api.jsonformat into a WorkflowGraphFastAPIserver:POST /prompt,GET /history/{id},GET /queue,GET /health- Java/Spring integration:
WorkflowClient(REST polling),BatchOrchestrator(virtual threads)
We explicitly skip:
- Async execution — ComfyUI’s asyncio event loop; our HTTP thread blocks during execution
- Custom node loading — dynamic import from
custom_nodes/directory - WebSocket streaming — latent previews during sampling
- Multi-GPU routing — per-node device hints
- Input type validation — schema-based type checking before execution
Mental model
Four concepts compose the entire system:
Nodes as pure functions. A node takes a dict of named inputs and returns a dict of named outputs. Given the same inputs, it always returns the same outputs. This is not enforced by Python but it is the contract that makes caching correct. A CLIPTextEncodeNode given {"text": "a cat"} always returns the same conditioning tensor. If it didn’t, the cache would serve stale results.
Edges as typed data channels. An edge says: take output_key from node A and deliver it as input_key to node B. Edges are not control flow. There is no branching, no conditional execution. The graph structure determines which nodes run and in what order; edges determine where data flows.
Cache as memoization by input hash. The cache key is sha256(node_type || params || sha256(each_input_value)). If you run a 10-node workflow twice with identical inputs, the second run hits cache on all 10 nodes and skips all node execution. If you change one upstream node’s params, only that node and its downstream dependents need to re-run.
Workflow graph:
CheckpointLoader ──► KSampler ──► VAEDecode ──► SaveImage
▲
CLIPTextEncode ──────────┘
▲
EmptyLatentImage ────────┘
Cache key for KSampler:
sha256("KSampler" + json(params) + sha256(model_data) + sha256(conditioning) + sha256(latent))
If CheckpointLoader's output doesn't change (same checkpoint),
its sha256 is the same, so KSampler's cache key is the same.
KSampler hits cache. VAEDecode hits cache. SaveImage hits cache.
Total node executions for the second run: 0.
Model slots as a small pool of expensive resources. A GPU has finite VRAM. A 16GB GPU cannot hold two 16GB models simultaneously. The ModelRegistry manages a pool of max_gpu_slots entries. When a new model is needed and the pool is full, the least recently used model is moved to CPU (simulated with model.to('cpu') in real PyTorch). This is a classic LRU cache applied to GPU memory rather than disk or RAM.
Prerequisites
- Python 3.11 or newer
- Familiarity with Python dataclasses and dicts
- Understanding of graph terminology (nodes, edges, directed acyclic graphs)
pip install fastapi uvicorn pydanticfor the server- The agent-framework lab is helpful for understanding the async patterns in section 9
- The mcp-server lab is helpful for understanding the protocol design in section 10
Stage-by-stage build
Stage 0 DAG executor with topological scheduling
- Implement Node base class with node_id, node_type, params, execute(inputs) -> dict
- Implement WorkflowGraph: add_node(), add_edge(), topological_order() via Kahn's algorithm
- Raise ValueError on cycle detection in topological_order()
- Implement execute_workflow(graph): run in topo order, collect and route outputs
- Implement concrete nodes: LoadTextNode, ToUpperCaseNode, ConcatTextNode, SaveResultNode
The v0 goal is the simplest thing that could possibly work: run a DAG of nodes in order.
Kahn’s algorithm — why it’s better than DFS for this use case.
The classic DFS-based topological sort uses recursion and a visited set. It works, but Kahn’s algorithm has two advantages for an executor. First, it naturally detects cycles: if the final order has fewer nodes than the graph has vertices, there was a cycle. Second, it processes nodes in layers (all in-degree-0 nodes first), which maps naturally to the “batch of parallel-ready nodes” pattern that real executors use.
def topological_order(self) -> list[str]:
in_degree = {nid: 0 for nid in self._nodes}
adj = defaultdict(list)
for edge in self._edges:
adj[edge.src_id].append(edge.dst_id)
in_degree[edge.dst_id] += 1
queue = deque(nid for nid, deg in in_degree.items() if deg == 0)
order = []
while queue:
nid = queue.popleft()
order.append(nid)
for successor in adj[nid]:
in_degree[successor] -= 1
if in_degree[successor] == 0:
queue.append(successor)
if len(order) != len(self._nodes):
raise ValueError("WorkflowGraph contains a cycle")
return orderThe invariant is clean: a node enters the queue exactly when all its predecessors have been processed. If the graph is a DAG, every node will eventually have in-degree 0 and enter the queue. If there is a cycle, the nodes in the cycle never reach in-degree 0 and the queue empties before all nodes are processed.
Data routing in execute_workflow.
Once you have an order, execution is straightforward: for each node, collect all the values it needs from upstream results, run the node, store the output.
def execute_workflow(graph: WorkflowGraph) -> dict:
order = graph.topological_order()
graph.results.clear()
for nid in order:
node = graph.get_node(nid)
inputs = {}
for edge in graph._edges:
if edge.dst_id == nid:
upstream = graph.results.get(edge.src_id, {})
inputs[edge.dst_key] = upstream.get(edge.src_key)
graph.results[nid] = node.execute(inputs)
return graph.resultsThe edge scan is O(E) per node (total O(V * E)), which is fine for workflow sizes (typically under 50 nodes). Real executors build an adjacency list indexed by destination node for O(1) lookup per edge.
Concrete nodes are trivial on purpose.
LoadTextNode reads a string from params. ToUpperCaseNode calls .upper(). The goal is to make the graph structure observable without drowning in node logic. In v1 and v2, nodes simulate real work — checkpoint loading, latent sampling — but the executor code stays the same.
Stage 1 Content-addressed tensor cache
- Implement content_hash(value) -> str: sha256 of JSON-serialized value, first 16 chars
- Implement node_cache_key(node_type, params, input_hashes) -> str: sha256 of all combined
- Implement CacheStore: dict mapping cache_key to result dict, with get/put/invalidate_key
- Implement CachingExecutor: checks cache before running each node, stores result on miss
- Implement invalidate_downstream(graph, node_id): BFS from node_id, return all reachable ids
- Add CheckpointLoaderNode, CLIPTextEncodeNode, EmptyLatentImageNode
The cache is the most important component in the executor. ComfyUI’s queue system is fast not because Python is fast but because it avoids re-running nodes that have not changed. When you queue 100 prompts that share the same base checkpoint, the checkpoint loads once. The CLIP encoder runs once per unique prompt text, not once per prompt.
Why content-addressed keys?
A naive cache might use node_id as the key. This breaks immediately: the same node with different params should miss, and two different nodes with identical params and inputs should both hit (they won’t if the key is node_id). Content-addressed keys solve both problems.
def content_hash(value: Any) -> str:
serialized = json.dumps(value, sort_keys=True, default=str)
return hashlib.sha256(serialized.encode()).hexdigest()[:16]
def node_cache_key(node_type, params, input_hashes) -> str:
payload = {"node_type": node_type, "params": params, "input_hashes": input_hashes}
serialized = json.dumps(payload, sort_keys=True, default=str)
return hashlib.sha256(serialized.encode()).hexdigest()The 16-char prefix for content_hash is sufficient for intermediate hashing (collision probability is negligible at workflow scale). The full SHA-256 for node_cache_key is safer as a primary store key.
The cache-aside pattern.
Check before execute. Store after execute. The executor logic stays clean:
for nid in order:
node = graph.get_node(nid)
inputs = collect_inputs(nid)
input_hashes = {k: content_hash(v) for k, v in inputs.items()}
key = node_cache_key(node.node_type, node.params, input_hashes)
cached = self.cache.get(key)
if cached is not None:
self.stats["cache_hits"] += 1
graph.results[nid] = cached
else:
self.stats["cache_misses"] += 1
output = node.execute(inputs)
self.cache.put(key, output)
graph.results[nid] = outputThe critical insight: the cache key includes the hashes of all inputs, not just the node’s own params. If an upstream node changes — say, you change the CLIP prompt text — its output hash changes. The downstream KSampler’s cache key includes that hash, so KSampler’s key changes too, causing a cache miss. The invalidation propagates automatically through key composition, with no explicit invalidation logic needed for the common case.
Explicit invalidation for the uncommon case.
When you want to force a node to re-run (e.g., the user clicked “regenerate this node”), you need BFS-based invalidation:
def invalidate_downstream(graph, node_id) -> list[str]:
visited, queue, affected = set(), deque([node_id]), []
while queue:
nid = queue.popleft()
if nid in visited: continue
visited.add(nid)
affected.append(nid)
for successor in graph.successors(nid):
queue.append(successor)
return affectedThis returns all node ids reachable from node_id. The executor then deletes those cache entries. The next run re-executes them and repopulates the cache.
CheckpointLoaderNode is the most important node.
Real ComfyUI checkpoint loading is expensive: a 7GB safetensors file takes 2-4 seconds to load off NVMe. With content-addressed caching, it runs exactly once per unique checkpoint path across all concurrent workflows. Our simulation returns a fixed-size weights list to make the hash deterministic:
class CheckpointLoaderNode(Node):
def execute(self, inputs):
return {
"model_name": self.params["ckpt_name"],
"weights": [0.1] * 768,
}The 768-float list is the cache payload. Its sha256 is part of every downstream node’s cache key. Change ckpt_name, change the hash, invalidate all downstream nodes.
Stage 2 LRU model offloading to CPU
- Implement ModelSlot(model_id, model_data, device): load() sleeps 180ms and sets device=cuda, offload_to_cpu() sets device=cpu
- Implement ModelRegistry(max_gpu_slots=2): LRU eviction via OrderedDict, ensure_loaded() returns slot
- Implement OffloadingExecutor: wraps CachingExecutor, calls registry.ensure_loaded before model nodes
- Add KSamplerNode (random.gauss denoising), VAEDecodeNode, SaveImageNode
- Track cache_hit_count, cache_miss_count, model_load_count, model_offload_count in stats
GPU VRAM is the scarcest resource in a local AI pipeline. A single SDXL checkpoint is 7GB. A 16GB GPU can hold two. When a third workflow arrives needing a different checkpoint, one of the two resident models must be moved to CPU RAM (which is slower but 64GB+) before the new one can load.
This is the same problem as a hardware cache with VRAM as L1 and CPU RAM as L2.
LRU with OrderedDict.
Python’s OrderedDict maintains insertion order and supports moving items to the end in O(1). This makes it a natural LRU implementation without a doubly-linked list:
class ModelRegistry:
def __init__(self, max_gpu_slots=2):
self._gpu_slots = OrderedDict() # LRU: oldest at front, newest at back
self._cpu_slots = {}
def ensure_loaded(self, model_id, model_data) -> ModelSlot:
if model_id in self._gpu_slots:
self._gpu_slots.move_to_end(model_id) # mark as recently used
return self._gpu_slots[model_id]
if len(self._gpu_slots) >= self.max_gpu_slots:
self._evict_lru()
slot = self._cpu_slots.pop(model_id, None) or ModelSlot(model_id, model_data)
slot.load() # 180ms simulated transfer
self._gpu_slots[model_id] = slot
self._gpu_slots.move_to_end(model_id)
return slot
def _evict_lru(self):
evicted_id, evicted_slot = self._gpu_slots.popitem(last=False)
evicted_slot.offload_to_cpu()
self._cpu_slots[evicted_id] = evicted_slotpopitem(last=False) removes the oldest item — the LRU entry. The evicted slot goes into _cpu_slots. If it’s needed again later, it skips allocation (already has model data) and just pays the 180ms transfer cost again.
Cache hits eliminate model load cost entirely.
This is the key interaction between the cache and the registry. If a KSampler node hits cache, we never call registry.ensure_loaded. The 180ms transfer never happens. When 100 prompts share the same checkpoint and identical sampler params (e.g., seed=42, steps=20), only the first prompt loads the model. The remaining 99 hit cache, skip ensure_loaded, and skip the 180ms transfer. Total wall time for 100 prompts: roughly the same as 1 prompt.
Stats tracking for observability.
self.stats = {
"cache_hit_count": 0,
"cache_miss_count": 0,
"model_load_count": 0, # synced from registry.load_count
"model_offload_count": 0, # synced from registry.offload_count
}These four numbers tell you everything about executor efficiency. A healthy warm-cache run shows high cache_hit_count and model_load_count == 1 (the first run only). A cold-start batch shows high model_load_count and potentially multiple model_offload_count values if more unique models are requested than GPU slots allow.
Run it yourself
labs/comfyui-executor ↗
Python 3.11+pip - 1 Install dependencies
cd labs/comfyui-executor && pip install -r requirements.txt - 2 Run the test suite
cd labs/comfyui-executor && python -m pytest tests/test_executor.py -v - 3 Start the server
cd labs/comfyui-executor/src && uvicorn server:app --host 0.0.0.0 --port 8000Leave this running in a separate terminal.
- 4 Submit a workflow (new terminal)
curl -s -X POST http://localhost:8000/prompt -H 'Content-Type: application/json' -d '{"prompt": {"1": {"class_type": "CheckpointLoaderSimple", "inputs": {"ckpt_name": "v1-5.safetensors"}}, "2": {"class_type": "CLIPTextEncode", "inputs": {"text": "a photo of a cat", "clip": ["1", 1]}}, "3": {"class_type": "EmptyLatentImage", "inputs": {"width": 512, "height": 512}}, "4": {"class_type": "KSampler", "inputs": {"model": ["1", 0], "positive": ["2", 0], "latent_image": ["3", 0], "seed": 42, "steps": 20, "model_name": "v1-5"}}, "5": {"class_type": "SaveImage", "inputs": {"images": ["4", 0], "filename": "out"}}}}' | python3 -m json.toolReturns a prompt_id UUID.
- 5 Poll for results (replace PROMPT_ID)
curl -s http://localhost:8000/history/PROMPT_ID | python3 -m json.toolRepeat until status is complete.
- 6 Check the queue depth
curl -s http://localhost:8000/queue
Benchmarks
M1 Pro 16GB, Python 3.11 | Metric | Result | Notes |
|---|---|---|
| Linear 10-node DAG execute | 0.8 ms | Python overhead vs ComfyUI asyncio ~0.6ms |
| Cache hit rate (repeated identical subgraph) | 100 % | Both use content-addressed keys |
| Model load time (CPU offload path) | 180 ms | Simulated; real GPU load depends on model size |
| Throughput with cache (8 identical prompts) | 6.2 prompts/s | Cache eliminates 94% of node re-executions |
The 0.8ms DAG execution time is pure Python overhead: dict lookup, edge scan, function dispatch. ComfyUI’s asyncio event loop achieves ~0.6ms for the same graph structure because the event loop amortizes Python’s per-call overhead across async continuations. The difference is not meaningful in practice — both are dominated by actual node execution time (hundreds of milliseconds for real sampling).
The 180ms model load time is the simulated cost of a model.to('cuda') transfer for a ~7GB checkpoint. Real numbers range from 90ms (NVMe + fast GPU bus) to 4,000ms (spinning disk). This is the number that makes LRU eviction worth implementing: if you load and evict 10 models per batch, you pay 1,800ms in transfers. Cache hits eliminate that cost entirely.
Stress tests
100 parallel prompts. Submit 100 identical workflows via the Java BatchOrchestrator. With cache warm after the first execution, the remaining 99 prompts hit cache on every node and complete in under 5ms each. The limiting factor is the Python HTTP server thread (one request at a time). Real ComfyUI uses an asyncio queue with a background worker.
50-node deep DAG. Chain 50 ToUpperCase nodes. Topological sort completes in under 1ms. Execution is 50 Python function calls — about 2ms total. Memory is proportional to the number of cached intermediate outputs: a 50-node chain with 64x64 float lists per node uses roughly 1.6MB of cache RAM. Uncached intermediates from a single run are garbage-collected after execution.
What breaks under stress. The primary failure mode is memory: a large batch of uncached prompts that each produce big intermediate outputs (e.g., full-resolution image tensors) will accumulate in the CacheStore dict until the process is killed. Real ComfyUI implements LRU eviction at the cache level too, not just at the model level. Our cache grows without bound. The secondary failure mode is the GIL: our background thread releases the GIL only during I/O (the 180ms sleep). CPU-intensive nodes hold the GIL and block all other threads. Python’s asyncio sidesteps this by yielding at await points, but CPU-bound node execution still blocks.
What the toy misses
- No async execution ComfyUI uses an asyncio event loop with a background worker queue; our synchronous executor blocks the HTTP thread during generation.
- No custom node loading ComfyUI scans a custom_nodes directory and dynamically imports node classes; our registry is hard-coded at startup.
- No interrupt and preview ComfyUI supports interrupting a running generation and streaming low-res previews via WebSocket latent previews; we only emit done/error events.
- No multi-GPU routing ComfyUI can route individual nodes to specific GPUs via device hints; our LRU offloader assumes a single device.
- No workflow validation ComfyUI validates input types against node schemas before execution; we trust the caller to wire compatible types.
- No virtual node groups ComfyUI supports grouping nodes into reusable subgraphs; our graph is always flat.
Wiring it into a Java/Spring service
Wiring it into a Java/Spring service
org.springframework.boot:spring-boot-starter-web:3.3.0 com.fasterxml.jackson.core:jackson-databind:2.17.0 Why Java for a Python executor?
The Python server exposes a HTTP API that is intentionally compatible with the real ComfyUI server. Any language can drive it. Java is a common choice for orchestration layers that need to submit thousands of image generation jobs reliably, track them in a database, retry failures, and expose results to downstream services — all patterns that Spring excels at.
Java 21 virtual threads make the polling pattern cheap. Each WorkflowClient.runWorkflow() call blocks on HTTP for hundreds of milliseconds. With platform threads that would cost 1MB of stack per concurrent workflow. With virtual threads the same code uses ~2KB per thread, enabling thousands of concurrent polls on a single JVM.
WorkflowClient — submit and poll
@Component
public class WorkflowClient {
private final RestClient restClient;
private final ObjectMapper mapper = new ObjectMapper();
public WorkflowClient(@Value("${comfy.base-url}") String baseUrl) {
this.restClient = RestClient.builder().baseUrl(baseUrl).build();
}
public Map runWorkflow(Map workflowJson) {
Map promptBody = Map.of("prompt", workflowJson);
JsonNode response = restClient.post().uri("/prompt")
.body(promptBody).retrieve().body(JsonNode.class);
String promptId = response.get("prompt_id").asText();
while (true) {
JsonNode history = restClient.get()
.uri("/history/{id}", promptId)
.retrieve().body(JsonNode.class);
switch (history.get("status").asText()) {
case "complete" -> { return mapper.convertValue(history.get("outputs"), Map.class); }
case "error" -> throw new RuntimeException("Workflow failed: " +
history.path("error").asText());
default -> Thread.sleep(500);
}
}
}
}RestClient (Spring 6.1+) is the synchronous replacement for RestTemplate. The switch expression on status string is idiomatic Java 21: complete returns, error throws, anything else sleeps and loops. The 500ms poll interval is sufficient for workflows that take 1-10 seconds.
BatchOrchestrator — 8 workflows in parallel
@Component
public class BatchOrchestrator {
private final WorkflowClient client;
public List runBatch(List workflows) {
int n = workflows.size();
CountDownLatch latch = new CountDownLatch(n);
List results = new CopyOnWriteArrayList(
Collections.nCopies(n, null)
);
for (int i = 0; i < n; i++) {
final int idx = i;
Thread.ofVirtual().start(() -> {
try {
results.set(idx, client.runWorkflow(workflows.get(idx)));
} finally {
latch.countDown();
}
});
}
latch.await();
return results;
}
}Thread.ofVirtual().start(...) launches a virtual thread. The CountDownLatch waits for all N threads to finish. CopyOnWriteArrayList with pre-allocated nulls lets each thread write to its own index without synchronization. This pattern is correct for N under 10,000. For larger batches, use ExecutorService with a newVirtualThreadPerTaskExecutor().
Running the integration
# Terminal 1: start the Python executor
cd labs/comfyui-executor/src
uvicorn server:app --host 0.0.0.0 --port 8000
# Terminal 2: start the Spring client
cd labs/comfyui-executor/java-integration
mvn spring-boot:run
# Server starts on :8081The Java service runs on port 8081 (set in application.properties) and calls the Python executor on port 8000. You can inject BatchOrchestrator into any Spring controller or @Scheduled method to drive batch image generation.
What surprised me
Where to go deeper
ComfyUI source code (comfyanonymous/ComfyUI on GitHub). The execution.py file is the real executor. It is about 400 lines and implements the same basic pattern: topological sort, cache by prompt hash, GPU offload via model_management.py. Reading it after this lab is straightforward.
Kahn’s algorithm and topological sorting. Cormen et al., “Introduction to Algorithms,” Chapter 22. The DFS-based version and Kahn’s algorithm are both O(V+E). Kahn’s is easier to adapt to parallel execution (all in-degree-0 nodes in a layer can run simultaneously).
PyTorch model.to() device management. The PyTorch documentation on device management explains what actually happens during model.to('cuda'): all tensors are copied from CPU pinned memory to GPU global memory over PCIe. The 180ms simulated cost corresponds to a ~7GB model at ~40GB/s PCIe 4.0 bandwidth.
ComfyUI API format. The workflow_api.json specification is implicit in ComfyUI’s source. The easiest way to study it is to open ComfyUI’s web interface, build a workflow, and click “Save (API format)” — this produces the exact dict structure that our parse_comfyui_json function accepts.
dbt and Airflow for DAG execution at data pipeline scale. Both use the same topological scheduling pattern, with dbt adding content-addressed caching (identical SQL queries skip re-execution) and Airflow adding distributed execution across workers.
Related labs and questions
agent-framework — the ReAct loop is another DAG executor, where the “nodes” are LLM calls and tool invocations and the “edges” are sequential dependencies. The caching strategy here (content-addressed by input hash) is directly applicable to agent step caching.
mcp-server — the Model Context Protocol exposes tools as a typed interface. A node executor that loads custom nodes via MCP would use MCP tool schemas as node type definitions and MCP tool calls as node execution.
lora-finetune — LoRA adapters are exactly the kind of resource that a ModelRegistry manages: small (2-10MB) but requiring the base model to be resident. An executor that swaps LoRA adapters per-node (rather than per-checkpoint) would use the same LRU pattern with model.set_adapter(name) instead of model.to('cuda').
Questions to explore:
- How would you implement parallel execution for nodes in the same topological layer that have no data dependencies between them?
- What happens to the cache if a node’s execution is non-deterministic (e.g.,
seed=random)? How should the cache key include the seed? - How would you persist the cache to disk between server restarts so warm-cache behavior survives a process restart?
- If two concurrent workflows both trigger a cache miss on the same node, what prevents them from executing it twice? (Hint: this is the “thundering herd” problem — how does ComfyUI handle it?)