FSD Frontend System Design

Part VI — The Blueprint · Lesson 11.12

HLD – Google Sheets

In one lineA virtualized grid over a dependency graph — render what's visible, recalculate only what changed.

Where you've seen itTyping a number into A1 and having exactly the cells that reference it update, instantly, in a sheet with a million rows.

  • Time21 min
  • DiagramVirtualized viewport + formula dependency DAG
  • Chapter11 · High Level Design

Diagram

Step 1⁠–⁠2Scope and scale
In scopegrid · editing · formulas · scroll · selection · collab
Out of scopecharts · pivot tables · apps script · import/export
Scaleup to 10M cells · 1M rows × 26 cols visible-addressable · only ~1,000 cells on screen at any moment
diagram

Lessons in this diagram10.7 Infinite Scroll

diagram
diagram
6b · DEEP DIVE — KEEPING EDITS AT 60fps

  parse formula once → AST, cached per cell
  recalc in a Web Worker when the dirty set is large (5.1)
  batch renders: one paint per frame, not per recalculated cell
  canvas rendering for the grid body if DOM cells prove too slow
  virtualize selection: a 10,000-cell range is a rectangle,
                        not 10,000 selected elements

How it works

  1. Sparse storage: a map keyed by "R3C7", so empty cells cost nothing.
  2. Virtualize both axes, with frozen headers rendered as separate panes.
  3. Build a dependency graph as formulas are parsed; edits walk reverse edges and recalculate topologically.
  4. Detect cycles at parse time and report #CIRCULAR! rather than hanging.
  5. Collaborate per cell — unlike prose (11.11), cells are independent, so per-cell last-write-wins is usually acceptable.

Trade-offs

ChoiceCost
DOM cellsEasy styling and a11y; slower past a few thousand
Canvas gridFast at any size; you rebuild a11y and editing
Full recalcTrivial to write; unusable at scale
DAG recalcFast and correct; graph maintenance complexity

Interview angle

"How do you recalculate a sheet with 10 million cells?"

  • You don't recalculate the sheet — you recalculate the dependents of the edited cell, found via reverse edges in a DAG and ordered topologically.
  • Detect cycles at parse time so a circular reference errors instead of hanging the tab.
  • If the dirty set is large, run recalculation in a Web Worker and batch one repaint per frame so typing stays responsive.

Recap

  • Virtualize both axes; only ~1,000 cells ever exist in the DOM.
  • The dependency DAG is what makes editing fast — recalc the dependents, not the sheet.
  • Cells are independent, so collaboration is far simpler here than in a document.