# nubskr/nubmq architecture

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

- Interactive diagram: https://gitdiagram.com/nubskr/nubmq
- Repository: https://github.com/nubskr/nubmq

## Overview

##### Project Type and Purpose
- Project Type: This is a high-performance in-memory key-value store with native pub-sub support. It’s similar to Redis but built in Go and optimized for high-frequency, contention-heavy workloads.
- Purpose: nubmq is engineered for ultra-fast SET/GET operations with features like adaptive sharding, dynamic resizing of shards, event-based notification scheduling for key expiry and updates, and a dual-engine model that allows zero downtime migration between store instances.

##### File Structure
- Top-Level Organization:
- Core Golang source files: Files like ShardUtils.go, connectionHandler.go, customShard.go, engine.go, getter.go, hasher.go, init.go, notificationHandler.go, resizer.go, scheduler.go, setter.go, and stupid_map.go indicate different components encapsulated as functions & modules.
- Client Directory: Contains a go.mod and client/main.go, which suggests a separate executable to act as a client interface (think redis-cli) to the nubmq engine.
- Build and Configuration: Makefile, go.mod, go.sum, and configuration files (e.g., .gitignore, LICENSE, CONTRIBUTING.md) indicate a typical Go project.
- Analysing-Stuff: Contains Python scripts and benchmarks (plot_stuff.py, throughput.py, server_sider_bench.py, requirements.txt) that help in analyzing performance and plotting benchmark results.

- Architectural Clues:
- The presence of files like customShard.go, resizer.go, and scheduler.go indicate that adaptive sharding, dynamic resizing, and schedule-based tasks (e.g., notification scheduling) are core functions.
- Connection handling and notification modules (connectionHandler.go and notificationHandler.go) affirm the event-driven, non-blocking design.

##### README for Additional Insights
- The README confirms:
- The existence of “Adaptive Sharding” with exponential shard scaling and a Two-Engine Model (separate engines for reads and writes) for continuous operations.
- Native pub/sub support via event-based notifications and SSE, eliminating polling.
- Emphasis on performance: parallelized goroutines, atomic operations, and benchmark data.
- A dual-tier storage system that allows seamless transitions, zero downtime, and lock-free operations.
- It includes architectural and benchmark images, giving additional context to component relationships (e.g., scaling mechanisms, engine migration, subscription notifications).

a. Main Components:
- Client Interface: Represented by the client directory (client/main.go), enabling users to interact with the server similar to redis-cli.
- Server/Core Engine: All the Go source files (engine.go, init.go, connectionHandler.go, customShard.go, etc.) implement the core engine. This should be depicted as the central engine that supports SET/GET operations and dynamic shard management.
- Adaptive Sharding Module: Highlight components like customShard.go, resizer.go, and ShardUtils.go which manage sharding, scaling up/down, and zero downtime migrations.
- Notification Service: Include modules such as notificationHandler.go and scheduler.go that manage pub/sub functionality, SSE, and event-driven notification scheduling.
- Connection Management: A dedicated component (connectionHandler.go) that handles each connection with its own read/write buffers to enable non-blocking I/O.

b. Relationships/Interactions:
- Clients connect to the Core Engine via a well-defined API protocol (e.g., commands like SET, GET, SUBSCRIBE).
- The Core Engine communicates with both the Adaptive Sharding Module and the Notification Service. For instance, data writes trigger events if the Adaptive Sharding criteria change (e.g., scaling operations), and key expiry or updates trigger notification events.
- The Adaptive Sharding Module interacts bidirectionally with the engine: shards are migrated dynamically based on load and key expiry, while the engine routes read/write operations to the appropriate shard.
- The Notification Service is hooked into engine events, dispatching notifications to subscribed clients.
- There’s also internal inter-module communication possibly using Go channels, atomic operations, and concurrent goroutines for high throughput.

c. Architectural Patterns & Design Principles:
- Adaptive Sharding: The dynamic scaling of storage buckets (shards) in real-time.
- Two-Engine Model: Separation between read-serving and write-serving engines for zero downtime.
- Event-Driven, Non-Blocking I/O: Each connection gets dedicated buffers with prioritized queues for critical operations, ensuring minimal latency.
- Modular & Self-Managing: Components are designed to self-adjust (auto-scaling and self-healing).

d. Relevant Technologies & Frameworks:
- Golang for concurrent programming.
- Native libraries for event-driven design and SSE implementation.
- Custom libraries for hashtable and shard management without external dependencies.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Interface"
        CI["Client Interface"]:::client
    end

    subgraph "Server/Core Engine"
        CE1["engine.go"]:::core
        CE2["init.go"]:::core
        CE1 -->|"continuesTo"| CE2
    end

    subgraph "Adaptive Sharding Module"
        AS1["customShard.go"]:::shard
        AS2["resizer.go"]:::shard
        AS3["ShardUtils.go"]:::shard
        AS1 -->|"flowsTo"| AS2
        AS2 -->|"flowsTo"| AS3
    end

    subgraph "Notification Service"
        NS1["notificationHandler.go"]:::notif
        NS2["scheduler.go"]:::notif
        NS1 -->|"flowsTo"| NS2
    end

    CM["connectionHandler.go"]:::connection
    PB["analysing-stuff/"]:::analytics

    CI -->|"CommandInput"| CE1
    CE1 -->|"ShardManagement"| AS1
    CE1 -->|"PublishEvents"| NS1
    CE1 -->|"HandleConnections"| CM
    NS1 -->|"SSENotifications"| CI
    AS3 -->|"ShardMigration"| CE1
    CE1 -->|"BenchmarkData"| PB

    click CI "https://github.com/nubskr/nubmq/blob/master/client/main.go"
    click CE1 "https://github.com/nubskr/nubmq/blob/master/engine.go"
    click CE2 "https://github.com/nubskr/nubmq/blob/master/init.go"
    click AS1 "https://github.com/nubskr/nubmq/blob/master/customShard.go"
    click AS2 "https://github.com/nubskr/nubmq/blob/master/resizer.go"
    click AS3 "https://github.com/nubskr/nubmq/blob/master/ShardUtils.go"
    click NS1 "https://github.com/nubskr/nubmq/blob/master/notificationHandler.go"
    click NS2 "https://github.com/nubskr/nubmq/blob/master/scheduler.go"
    click CM "https://github.com/nubskr/nubmq/blob/master/connectionHandler.go"
    click PB "https://github.com/nubskr/nubmq/tree/master/analysing-stuff/"

    classDef client fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef core fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef shard fill:#FFDEAD,stroke:#000,stroke-width:2px;
    classDef notif fill:#FFA07A,stroke:#000,stroke-width:2px;
    classDef connection fill:#87CEFA,stroke:#000,stroke-width:2px;
    classDef analytics fill:#D3D3D3,stroke:#000,stroke-width:2px;
```
