Architecture

Architecture

rLightning is built as a modular, high-performance in-memory data store in Rust. This page describes the internal architecture, core modules, and design decisions that enable Redis 7.x compatibility with strong concurrency guarantees.

Module Overview

Networking (src/networking/)

The networking layer handles all client connections and protocol handling.

Storage Engine (src/storage/)

The storage engine is the core of rLightning, designed for lock-free concurrent access.

Command Handler (src/command/)

The command layer dispatches and executes all Redis commands.

Persistence (src/persistence/)

Persistence ensures data durability across restarts.

Replication (src/replication/)

Replication provides high availability through leader-follower topology.

Pub/Sub (src/pubsub/)

The publish/subscribe system supports real-time messaging.

Lua Scripting (src/scripting/)

Server-side scripting provides atomic command execution.

Security (src/security/)

The security module implements Redis-compatible access control.

Cluster (src/cluster/)

Cluster mode provides horizontal scaling and automatic failover.

Sentinel (src/sentinel/)

Sentinel provides high availability monitoring and automatic failover for non-cluster deployments.

Data Flow

The request lifecycle follows a clean, linear path through the system:

Client Connection
       |
       v
  TCP Accept (Tokio async)
       |
       v
  RESP Protocol Parse (RESP2 or RESP3)
       |
       v
  Command Validation & ACL Check
       |
       v
  Command Dispatch (fast path / slow path)
       |
       v
  Storage Engine (DashMap atomic operations)
       |
       v
  RESP Response Serialization
       |
       v
  Client Response

In parallel with the main request path:

Key Design Decisions

Lock-Free Concurrency

rLightning uses DashMap (a concurrent hash map based on sharded locking) instead of a single global lock. This enables high throughput under concurrent workloads. Individual entries can be read and modified without blocking other operations on different keys.

Sorted-Order Key Locking

For multi-key operations like MSET, SMOVE, and GEOSEARCHSTORE, rLightning acquires locks in sorted key order to prevent deadlocks. This ensures that two concurrent operations on overlapping key sets will always acquire locks in the same order.

Task-Local Database Index

Each connection’s selected database index is stored in a Tokio task-local variable (CURRENT_DB_INDEX). This avoids passing the database index through every function call while maintaining per-connection isolation.

Atomic Read-Modify-Write

Rather than exposing raw lock/unlock APIs, the storage engine provides atomic primitives (atomic_incr, atomic_modify, etc.) that accept closures. This guarantees that modifications are applied atomically without the caller needing to manage locks directly.