intermediate

Command

Represent an action as an object or record so it can be queued, logged, retried, authorized, undone, or replayed.

Command represents an action as an object or record so it can be queued, logged, retried, authorized, undone, or replayed. It powers job systems, CQRS write models, text editors, and macro automation. Commands should carry enough data to execute idempotently where possible.

On interviews: walk through undo/redo or an async task queue. Contrast Command with event notification and with bare function calls.

Common pitfalls: commands that reach into global singletons; missing correlation metadata; undo stacks without invariant restoration.

The trade-off is flexibility versus complexity—know when the simpler path is enough.

Checklist:

  • Command encapsulates intent and payload.
  • Handler is separate from invocation site.
  • Idempotency keys for retried commands.
  • Audit log stores command history for support.