10 / 17

Can workers share memory or state in a Node.js cluster?

No. Each worker in a Node.js cluster is a separate OS process with its own isolated memory space. Workers cannot share variables or in-memory state. Shared state must be managed using external tools like Redis, a database, or IPC messaging.

This is a fundamental limitation of the process-based clustering model. Unlike threads, OS processes have completely separate memory spaces. Any in-memory data — such as session stores, counters, or cached data — will be duplicated independently in each worker and not synchronized. This is a major architectural consideration when building clustered applications.

Problems Caused by No Shared Memory
  1. 1

    User sessions may not persist across requests if different workers handle them

  2. 2

    In-memory caches are duplicated and can go out of sync between workers

  3. 3

    Counters or rate-limiters stored in memory will be inaccurate across workers

  4. 4

    WebSocket connections are tied to a specific worker and not portable to others

Solutions for Shared State
  1. 1

    Redis — most common solution for sessions, caching, pub/sub, and rate limiting

  2. 2

    Database (MongoDB, PostgreSQL) — for persistent shared state across workers

  3. 3

    IPC Messaging — workers send state to master, which aggregates and rebroadcasts

  4. 4

    Sticky Sessions — route same client to same worker (partial solution, not true sharing)

  5. 5

    Memcached — in-memory distributed cache for high-speed shared data

Using Redis for Shared Sessions Across Workers
Difficulty: 5/10
Topics: cluster module, inter-process communication, shared state

Scenario Questions

0-2 years experience
  1. 1

    If you need to keep a counter that increments across multiple worker processes in a Node.js cluster, how would you implement it? What would happen if you tried to store the counter in a variable defined in the master file?

  2. 2

    When you spawn workers using the cluster module, can they directly read and write to each other's in‑memory objects? Explain what you would see if you logged a variable modified in one worker from another.

2-5 years experience
  1. 1

    We have a Node.js service that uses a cluster of workers to handle HTTP requests, and we need to share a cache of recent database lookups across workers. Describe the options you would consider and why a simple shared variable won't work.

  2. 2

    During a load test, you notice that a configuration flag changed in one worker doesn't affect the others, causing inconsistent behavior. Walk me through how you would debug this and what mechanisms you could use to propagate the change.

5-8 years experience
  1. 1

    Our platform runs 32 workers per node and needs to maintain a rate‑limit counter that is consistent across all workers. Design a solution that minimizes latency and avoids a single point of failure, discussing trade‑offs of using IPC, external stores, or native addons.

  2. 2

    Explain the performance and reliability implications of using a shared memory segment via a native addon versus using a Redis pub/sub for synchronizing state across workers in a high‑throughput Node.js cluster.

8+ years experience
  1. 1

    We are planning to migrate a monolithic Node.js app that currently uses the cluster module with ad‑hoc process messaging to a microservices architecture. How would you restructure state sharing to support future scaling, and what patterns would you adopt to avoid tight coupling between workers?

  2. 2

    Consider a long‑running data processing pipeline where multiple worker processes need to coordinate checkpoints. Propose an architecture that ensures exactly‑once processing without relying on in‑process memory, and justify your choice of coordination mechanism.

Follow-up Questions

  • What are the failure modes when using process.send for state synchronization?
  • How would you handle a worker crash that was holding the only copy of critical state?
  • Can you name any Node.js APIs or third‑party libraries that facilitate shared memory across workers?