Skip to main content
09

Design an interactive graph visualization with thousands of nodes

HardCanvasWebGLWeb WorkersGraph layout

Design an interactive graph visualization that shows thousands of connected entities, for example cloud resources and their dependencies, services calling each other, or people in an organization. Users pan, zoom, search, click a node to see its details, and expand its neighbors.

A typical framing: "An infrastructure map shows every resource in a company's cloud and how they depend on each other: from a few hundred nodes for a small team to 20,000+ for a large enterprise. It should stay smooth on a normal laptop, help users find what matters, and be usable by people who can't use a mouse."

TL;DR

The two hard problems are rendering cost and readability, and the second matters more: 20,000 nodes on one screen is a hairball nobody can read, no matter how fast it draws.

So design for progressive disclosure: start from a summary (nodes grouped by application, region or type), let the user search, filter and expand into detail, and use level of detail as they zoom (dots when far away, labels and icons when close).

For rendering, pick by scale: SVG up to a few hundred nodes (easy styling, events and accessibility), Canvas 2D up to a few thousand, WebGL beyond that. Run the layout in a Web Worker (or precompute it on the server), because force-directed layouts are expensive and would freeze the page. Use a spatial index for hit testing and viewport culling, keep the graph data outside React state, and provide a table or list view of the same data for accessibility and for users who just need answers.

1. Requirements

Questions worth asking:

  • How many nodes and edges, typically and at the extreme?
  • Is the layout meaningful (a hierarchy, a flow, a network) or just "show me the connections"?
  • Does the data change live, or is it a snapshot?
  • What do users actually do: explore, find a path between two things, see the impact of one node ("blast radius")?
  • Which devices: desktop only, or also tablets?

Functional

  • Pan and zoom, select a node to open a details panel.
  • Search for a node and focus it. Filter by type, tag, status.
  • Expand and collapse groups and neighbors.
  • Highlight paths and the neighbors of the selected node.
  • Shareable views: a link restores the focus, filters and zoom.

Non-functional

  • 60 fps pan and zoom; first meaningful render within ~2 s for large graphs.
  • Stable layout: nodes don't jump around when data or filters change.
  • Readable at every zoom level.
  • Accessible: keyboard navigation and a non-visual alternative.

2. Architecture

Loading diagram...
  • Graph model: an in-memory store of nodes, edges, positions and view state (selection, expanded groups). It lives outside React; React renders only the surrounding UI (toolbar, panel, table).
  • Layout worker: computes positions without blocking the main thread, and streams intermediate results so the graph settles visibly instead of appearing after a long freeze.
  • Spatial index (a quadtree or R-tree): finds which node is under the cursor and which nodes are in the viewport.
  • Renderer: draws only what's visible, at the right level of detail.
  • Graph API: returns subgraphs, not the whole world: a summary, the neighborhood of a node, or search results.

3. Data model

ts
type GraphNode = {
  id: string
  type: 'service' | 'database' | 'bucket' | 'function'
  label: string
  groupId?: string // application, region or account, used for clustering
  status?: 'ok' | 'warning' | 'critical'
}

type GraphEdge = {
  id: string
  source: string
  target: string
  kind: 'calls' | 'reads' | 'backs_up'
}

type Position = { x: number; y: number }

type ViewState = {
  camera: { x: number; y: number; zoom: number }
  selectedId?: string
  expandedGroups: Set<string>
  filters: { types?: GraphNode['type'][]; status?: GraphNode['status'][] }
}

/** Positions live in typed arrays for speed: index i holds node i's x and y. */
type LayoutBuffer = { ids: string[]; xy: Float32Array }

Typed arrays (Float32Array) are compact, fast to iterate, and can be transferred between the worker and the main thread without copying.

4. Interface (API)

http
GET /api/graph/summary                          -> groups and the edges between groups
GET /api/graph/groups/:groupId                  -> nodes and edges inside one group
GET /api/graph/nodes/:id/neighbors?depth=1      -> neighborhood of a node
GET /api/graph/search?q=billing                 -> matching nodes with their groupId
GET /api/graph/path?from=a&to=b                 -> shortest path between two nodes
GET /api/graph/nodes/:id                        -> full details for the side panel

For large, slow-changing graphs the server can also return precomputed positions, so the client skips most of the layout work.

5. Optimizations and deep dives

Choosing the rendering technology

TechnologyComfortable scaleStrengthsWeaknesses
SVG (DOM)Up to a few hundred nodesCSS styling, native events, accessible, crisp textEvery node is a DOM element: slow with thousands
Canvas 2DUp to a few thousandOne element, fast drawing, simple APIManual hit testing, text and accessibility
WebGLTens of thousands and moreGPU draws huge numbers of shapes per frameComplex (shaders, text rendering), usually through a library

A common hybrid: draw nodes and edges on Canvas or WebGL, and render the selected node's label, tooltip and context menu as regular DOM on top, so they're crisp and accessible. Libraries like Cytoscape.js, Sigma.js or React Flow (for small, editable diagrams) solve much of this; the interviewer mostly wants to see that you know the trade-offs.

Layout

  • Force-directed layouts (nodes repel, edges pull) work for general networks, but are roughly O(n²) per iteration without approximations like Barnes-Hut. Hierarchical layouts (dagre, ELK) suit dependency trees and flows.
  • Run layout in a Web Worker and send positions back in batches with transferable typed arrays. Show the graph settling instead of a spinner.
  • Stability: when nodes are added or filters change, start from previous positions and pin existing nodes, so the user's mental map survives. Cache positions per view.
  • For very large graphs, lay out groups first (a small graph of clusters), then lay out each group only when it's expanded.

Taming the hairball

  • Grouping: collapse nodes into clusters by application, region or account, with edge counts between clusters. Expand one cluster at a time.
  • Level of detail: far away, draw nodes as dots and skip labels and minor edges; closer in, show icons, labels and edge directions. Show labels only for the largest or selected nodes when zoomed out.
  • Focus + context: on selection, highlight the node and its neighbors and dim everything else.
  • Filtering by type and status, and search that zooms the camera to the result.

Rendering performance

  • Viewport culling: query the spatial index for nodes inside the visible area and draw only those.
  • Redraw only when something changed (camera, data, hover), batched into one requestAnimationFrame. Never redraw on every mouse event.
  • Keep positions and the camera in mutable structures, not React state, so panning doesn't trigger React renders.
  • During pan and zoom, temporarily drop expensive details (labels, curved edges) and restore them when the movement stops.
  • Use OffscreenCanvas to move drawing itself into a worker when the main thread is busy.

Interaction

  • Hit testing through the spatial index: O(log n) instead of checking every node.
  • Zoom around the cursor, support trackpad pinch, and clamp zoom levels.
  • Keep selection and camera in the URL so a view can be shared.

Accessibility

A canvas is invisible to screen readers, so:

  • Provide a table or tree view of the same nodes and relationships, with the same filters. Often it's the faster tool for answering questions.
  • Support keyboard navigation in the graph: Tab into the canvas, arrow keys move between connected nodes, Enter opens details. Announce the focused node and its connections through an aria-live region.
  • Don't rely on color alone for status: add icons or shapes.

Live updates

If the data changes, apply diffs (added, removed, changed nodes) to the model, run an incremental layout only for the affected area, and animate the changes briefly so users notice them without the whole graph moving.

Common follow-ups

  • "How would you show 1 million nodes?" You wouldn't draw them all at once. Aggregate on the server into a hierarchy of clusters, render the top level with WebGL, and load deeper levels on demand as the user zooms (like map tiles).
  • "How do you find the impact of one failing node?" Fetch its downstream dependencies (a server-side traversal with a depth limit), highlight that subgraph, and list the affected nodes in the side panel.
  • "How do you test this?" Unit-test the model, filters and layout logic outside the canvas. Use visual regression snapshots for rendering, and performance benchmarks with large generated graphs to catch frame-rate regressions.

What interviewers look for

  • Recognizing that readability is the real problem: grouping, level of detail, focus and search.
  • A reasoned SVG vs. Canvas vs. WebGL choice based on scale.
  • Layout in a worker, with stable positions.
  • Spatial indexing, viewport culling and keeping hot data out of React state.
  • Accessibility through a table view and keyboard navigation.

Was this helpful?