Learn how SP8D enables high-performance, lock-free concurrency in JavaScript and Node.js. This guide covers SPSC, MPSC, and MPMC models, with practical tips for scalable, multi-threaded apps.
SP8D Concurrency Models: SPSC, MPSC, MPMC Explained for JavaScript & Node.js
Master SP8D concurrency models—SPSC, MPSC, and MPMC—for high-performance, lock-free communication in JavaScript and Node.js. This guide compares patterns, use cases, and implementation tips to help you optimize your multi-threaded apps.
Concurrency Models: SPSC, MPSC, MPMC
SP8D supports all major cross-thread concurrency patterns out of the box, so you can pick the right one for your workload—from ultra-low-latency pipelines to scalable, many-agent systems.
Quick Reference Table
Use this table to quickly match your concurrency needs to the right SP8D channel model.
Model | # Producers | # Consumers | Typical Use Case |
---|---|---|---|
SPSC | 1 | 1 | Sensor → Processor, UI event, SISO |
MPSC | N | 1 | Many workers → main aggregator |
MPMC | N | M | Multi-agent, load-balancing, simulation |
Single-Producer, Single-Consumer (SPSC)
SPSC is the simplest and fastest concurrency model—ideal for direct, one-to-one data flows where throughput and minimal contention are critical.
For best practices on handling buffer full, fairness, and lossless delivery in SPSC, see Fairness & Backpressure.
Diagram: One producer, one consumer. Highest throughput, minimal contention.
Key Points
- Highest throughput, minimal contention.
- Zero ambiguity: slots always move forward linearly.
- Great for: video/audio pipelines, UI events, one-off data flows.
SPSC is not ideal for fan-in or fan-out scenarios. Use MPSC or MPMC for those patterns.
How to use
const { channel } = createChannel({ mode: "SPSC" });
Looking for installation or setup? See Quickstart: Installation →
Multi-Producer, Single-Consumer (MPSC)
MPSC lets you aggregate work from many sources into a single target—perfect for logging, worker pools, or parallel data collection.
Learn how SP8D prevents starvation and ensures fair access in MPSC: Fairness & Backpressure.
Diagram: Multiple producers, one consumer. Good for parallel data collection, AI tasks fanning in, worker pool → main thread.
Key Points
- Multiple producers, single sink.
- Producers contend for slots — SP8D’s segments reduce collisions.
- Good for: parallel data collection, AI tasks fanning in to model, worker pool → main thread.
MPSC is not ideal for multi-consumer or mesh scenarios. Use MPMC for those patterns.
How to use
const { channel } = createChannel({ mode: "MPSC", segments: 2 });
Need a minimal working code sample? See Quickstart: Minimal Example →
Multi-Producer, Multi-Consumer (MPMC)
MPMC is the most flexible model, supporting many producers and many consumers—ideal for load-balancing, multi-agent systems, or simulations at scale.
High-contention MPMC setups require careful fairness and backpressure handling. See Fairness & Backpressure for advanced scenarios and mitigation strategies.
Diagram: Many producers, many consumers. True concurrent mesh for multi-agent, load-balancing, or simulation workloads.
Key Points
- True concurrent mesh: many-in, many-out.
- Perfect for multi-agent AIs, trading engines, any “work distributed among many actors.”
- Segmentation is critical for high contention/low latency.
MPMC is not ideal for simple point-to-point or single-sink scenarios. Use SPSC or MPSC for those patterns.
How to use
const { channel } = createChannel({ mode: "MPMC", segments: 4 });
Want real-world integration code? See Quickstart: Common Recipes →
How to Choose?
Unsure which model to use? Match your use case to the recommended model below. For advanced tuning, fairness, and troubleshooting, see Fairness & Backpressure.
Your Need: | Recommended Model |
---|---|
Point-to-point data | SPSC |
Fan-in (best for logging, etc) | MPSC |
Multi-agent, load balancing | MPMC |
Interop & Scaling
Segmentation reduces contention and enables scaling—set segments > 1
for multiple producers or consumers. Map each producerId
or consumerId
to a segment to further reduce slot collisions.
Looking for protocol internals or slot state machine details? See Slot State Machine →
Code Patterns in Practice
Choose the right model for your architecture:
- Single-thread to worker: SPSC
- Several AI inferences → UI: MPSC
- Market sim, multi-copilot: MPMC