KatInterview

Comparing Multithreading and Multiprocessing for Low-Latency Systems

System DesignModel answer
MediumModel answerSystem Designmultithreadingmultiprocessingoperating-systems

Last updated

Analyze thread vs. process execution models at the kernel, memory, and hardware level, and design a low-latency execution topology for an order book trading engine.

Selecting between multithreaded and multiprocess software designs involves fundamental trade-offs among memory isolation, CPU scheduling costs, IPC mechanisms, and fault resilience.

Analyze these two concurrency models from an operating system and processor perspective, and apply them to a real-world system architecture.

Part 1: Core Architectural Mechanics

Contrast multithreading and multiprocessing across four structural areas:

  1. Memory Boundaries & Resource Ownership: How do OS constructs differ across threads within a shared process versus independent processes regarding virtual memory maps, stacks, heaps, file descriptor tables, and handle allocation?
  2. CPU Context Switching Costs: Analyze the direct and indirect performance penalties incurred during a thread-to-thread switch vs. a process-to-process switch. Detail hardware components involved, including page tables, the CR3 register (x86-64), and the Translation Lookaside Buffer (TLB).
  3. Communication & Synchronization Mechanisms: Contrast intra-process synchronization tools (atomics, spinlocks, mutexes) with inter-process communication (pipes, Unix sockets, POSIX shared memory). Detail how hardware cache coherency and false sharing affect both models equally.
  4. Fault Propagation & Crash Domain Isolation: Evaluate how unexpected exceptions, segment faults, or memory corruption impact neighboring execution units in both models.

Part 2: Applied Architecture Design

Design the software architecture for an algorithmic order routing system that must execute with microsecond-level latency:

  • Receive high-throughput UDP multicast market data updates.
  • Parse tick data and maintain an active in-memory Limit Order Book (LOB).
  • Compute order routing logic, apply pre-trade risk checks, and dispatch orders over TCP to an exchange interface.

Propose a concrete multi-process and multi-thread deployment scheme. Detail how work should be isolated across distinct processes versus shared within common processes using threads. Justify your layout based on deterministic latency, CPU core affinity, cache locality, and failure containment.

Hints

Hint 1

At the OS kernel level (e.g., Linux), how do `clone()` flags differentiate process creation from thread creation?

Hint 2

Consider the indirect microarchitectural costs of context switching beyond register saving (e.g., L1/L2 cache pollution and TLB invalidation via PCID).

Hint 3

Can two separate processes share memory with zero kernel-space copy overhead on the critical path?