# tech-pw/pw-redis architecture

> Architecture diagram of the GitHub repository tech-pw/pw-redis, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tech-pw/pw-redis
- Repository: https://github.com/tech-pw/pw-redis

## Overview

1. **Project type and scope**  
   This is a small TypeScript library, not an application. Its purpose is to extend `ioredis` with a cluster-aware pipeline API called `clusterPipeline`, while preserving the normal `Redis` and `Cluster` client behavior.

2. **Public API boundary**  
   The external surface is intentionally thin: consumers import from `src/index.ts` (compiled to `dist/index.js`) and get the existing `ioredis` client classes plus the added `clusterPipeline` method on `Cluster`.

3. **Core subsystem: cluster pipeline orchestration**  
   The main logic lives in `src/clusterPipeline.ts` (compiled to `dist/clusterPipeline.js`). This is the only real implementation module and is responsible for taking a list of Redis commands, determining their target shard/node, executing them efficiently, and returning results in input order.

4. **Cluster topology awareness**  
   The library’s key architectural concern is Redis Cluster slot distribution. Instead of sending an entire pipeline to one node like vanilla `ioredis`, it maps keys/slots to the correct cluster nodes using Redis cluster metadata, including the `SHARDS` view described in the README.

5. **Execution flow**  
   The data flow is: input command array → slot/node resolution → grouping commands by target node → parallel pipeline execution per node → merge of results back into the original command order. That “fan out / fan in” pattern is the central architecture of the library.

6. **Error recovery and cache refresh boundary**  
   The README indicates that slot mapping is cached in memory and refreshed when Redis reports slot movement or topology changes. So there is a runtime boundary between cached cluster metadata and live Redis errors that trigger re-discovery.

7. **Compatibility layer over ioredis**  
   The library is a wrapper, not a reimplementation of Redis access. It delegates actual network I/O and client management to `ioredis`, then adds cluster-pipeline semantics on top. This means the architecture depends on `ioredis` for connection handling, command serialization, and cluster client behavior.

8. **Build and packaging**  
   The repository is packaged as a Node.js/TypeScript library. `tsconfig.json` drives compilation from `src/` to `dist/`, and the checked-in `dist/` folder is the distributable runtime artifact. There are no separate services, queues, or databases inside the repo beyond the external Redis Cluster.

9. **Runtime environment**  
   The code runs in a Node.js process as an npm package. It is designed to be embedded inside another service, where that service owns the Redis deployment and supplies the cluster endpoints.

10. **Operational impact**  
   The architecture is optimized for reducing network round-trips in clustered Redis environments. The README’s production metrics show the library is intended to improve API latency by batching commands where business logic allows, while preserving correctness across shards.

11. **Repository structure summary**  
   The repo is intentionally minimal: source, compiled output, package metadata, and documentation/images. That suggests the architecture is centered on a single reusable algorithm rather than multiple collaborating modules or infrastructure components.

Main entry points: [`src/clusterPipeline.ts`](https://github.com/tech-pw/pw-redis/blob/main/src/clusterPipeline.ts) (clusterPipeline), [`src/index.ts`](https://github.com/tech-pw/pw-redis/blob/main/src/index.ts) (index), [`tsconfig.json`](https://github.com/tech-pw/pw-redis/blob/main/tsconfig.json) (tsconfig), [`package.json`](https://github.com/tech-pw/pw-redis/blob/main/package.json) (package.json)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public_api["Public API"]
  node_src_index["index<br/>entrypoint<br/>[index.ts]"]
end

subgraph group_core["Core Engine"]
  node_cluster_pipeline["clusterPipeline<br/>orchestrator<br/>[clusterPipeline.ts]"]
  node_redis_cluster(("ioredis<br/>external dependency"))
  node_slot_metadata[("slot map<br/>cluster topology")]
  node_slot_cache[("slot cache<br/>runtime cache")]
  node_command_grouping{{"group by node<br/>planner"}}
  node_fanout_exec["fan-out exec<br/>executor"]
  node_merge_results{{"merge results<br/>reducer"}}
  node_topology_refresh(("refresh topology<br/>recovery"))
end

subgraph group_packaging["Packaging"]
  node_dist_index["dist index<br/>built artifact<br/>[index.js]"]
  node_dist_cluster_pipeline["dist pipeline<br/>built artifact<br/>[clusterPipeline.js]"]
  node_package_json["package.json<br/>package manifest<br/>[package.json]"]
  node_tsconfig["tsconfig<br/>build config<br/>[tsconfig.json]"]
  node_readme["README<br/>documentation<br/>[README.md]"]
end

node_src_index -->|"wraps"| node_redis_cluster
node_src_index -->|"exposes"| node_cluster_pipeline
node_cluster_pipeline -->|"reads"| node_slot_metadata
node_slot_metadata -->|"cached as"| node_slot_cache
node_cluster_pipeline -->|"plans"| node_command_grouping
node_command_grouping -->|"dispatches"| node_fanout_exec
node_fanout_exec -->|"executes via"| node_redis_cluster
node_fanout_exec -->|"returns to"| node_merge_results
node_cluster_pipeline -->|"orders"| node_merge_results
node_cluster_pipeline -->|"recovers on"| node_topology_refresh
node_topology_refresh -->|"updates"| node_slot_cache
node_package_json -->|"uses"| node_tsconfig
node_tsconfig -->|"compiles"| node_src_index
node_tsconfig -->|"compiles"| node_cluster_pipeline
node_src_index -->|"outputs"| node_dist_index
node_cluster_pipeline -->|"outputs"| node_dist_cluster_pipeline
node_readme -.->|"describes"| node_cluster_pipeline

click node_src_index "https://github.com/tech-pw/pw-redis/blob/main/src/index.ts"
click node_cluster_pipeline "https://github.com/tech-pw/pw-redis/blob/main/src/clusterPipeline.ts"
click node_dist_index "https://github.com/tech-pw/pw-redis/blob/main/dist/index.js"
click node_dist_cluster_pipeline "https://github.com/tech-pw/pw-redis/blob/main/dist/clusterPipeline.js"
click node_package_json "https://github.com/tech-pw/pw-redis/blob/main/package.json"
click node_tsconfig "https://github.com/tech-pw/pw-redis/blob/main/tsconfig.json"
click node_readme "https://github.com/tech-pw/pw-redis/blob/main/README.md"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_src_index toneBlue
class node_cluster_pipeline,node_redis_cluster,node_slot_metadata,node_slot_cache,node_command_grouping,node_fanout_exec,node_merge_results,node_topology_refresh toneAmber
class node_dist_index,node_dist_cluster_pipeline,node_package_json,node_tsconfig,node_readme toneMint
```

## Components

### Public API

Thin entrypoint surface exported to consumers.

- [index](https://github.com/tech-pw/pw-redis/blob/main/src/index.ts): entrypoint · Exports the library surface and augments cluster clients with clusterPipeline. · `src/index.ts`

### Core Engine

Cluster-aware pipeline orchestration and Redis Cluster behavior.

- [clusterPipeline](https://github.com/tech-pw/pw-redis/blob/main/src/clusterPipeline.ts): orchestrator · Coordinates shard-aware command dispatch, execution, and ordered result collection. · `src/clusterPipeline.ts`
- **ioredis**: external dependency · Provides Redis and Cluster client behavior, networking, and command execution primitives.
- **slot map**: cluster topology · Redis Cluster shard and slot metadata used to resolve command targets.
- **slot cache**: runtime cache · In-memory mapping of slots to nodes, refreshed when topology changes are detected.
- **group by node**: planner · Buckets commands by their target shard or node before execution.
- **fan-out exec**: executor · Runs per-node pipelines in parallel across the cluster.
- **merge results**: reducer · Reassembles per-node responses into the original command order.
- **refresh topology**: recovery · Handles slot movement and rediscovery when cluster state no longer matches cached mappings.

### Packaging

TypeScript build and distributable artifacts.

- [dist index](https://github.com/tech-pw/pw-redis/blob/main/dist/index.js): built artifact · Compiled public entrypoint shipped to consumers. · `dist/index.js`
- [dist pipeline](https://github.com/tech-pw/pw-redis/blob/main/dist/clusterPipeline.js): built artifact · Compiled implementation of the cluster pipeline orchestration. · `dist/clusterPipeline.js`
- [package.json](https://github.com/tech-pw/pw-redis/blob/main/package.json): package manifest · Defines package metadata, dependencies, and scripts. · `package.json`
- [tsconfig](https://github.com/tech-pw/pw-redis/blob/main/tsconfig.json): build config · Controls TypeScript compilation from src to dist. · `tsconfig.json`
- [README](https://github.com/tech-pw/pw-redis/blob/main/README.md): documentation · Documents the cluster-pipeline behavior, topology model, and usage intent. · `README.md`

## Connections

- index → ioredis: wraps · Re-exports and extends ioredis client classes.
- index → clusterPipeline: exposes · Makes clusterPipeline available on Cluster clients.
- clusterPipeline → slot map: reads · Uses Redis Cluster slot and shard metadata to choose targets.
- slot map → slot cache: cached as · Topology data is retained in memory for reuse.
- clusterPipeline → group by node: plans · Partitions the input pipeline by target node.
- group by node → fan-out exec: dispatches · Produces node-local pipelines for parallel execution.
- fan-out exec → ioredis: executes via · Delegates actual Redis I/O and command execution to ioredis.
- fan-out exec → merge results: returns to · Collected responses are merged back into input order.
- clusterPipeline → merge results: orders · Preserves original command ordering in the final response.
- clusterPipeline → refresh topology: recovers on · Redis errors or slot movement trigger rediscovery and cache refresh.
- refresh topology → slot cache: updates · Refreshes the in-memory topology mapping.
- package.json → tsconfig: uses · Package scripts depend on the TypeScript compilation settings.
- tsconfig → index: compiles · Builds the source entrypoint into dist.
- tsconfig → clusterPipeline: compiles · Builds the core implementation into dist.
- index → dist index: outputs · Source entrypoint is emitted as the distributable module.
- clusterPipeline → dist pipeline: outputs · Core implementation is emitted as the distributable artifact.
- README → clusterPipeline: describes · Documentation explains the fan-out/fan-in pipeline behavior.
