How antonychr/redisgo works
Project Type and Purpose
• This is a networked server application written in Go that implements a subset of Redis functionality (RESP protocol parsing, command execution, in-memory storage, and master-slave replication).
• It’s not a full “frontend/backend” web app but a standalone TCP service that speaks the Redis protocol.
File Structure
• Top-level entry point: main.go (starts the server)
• command/ : extracts and dispatches Redis commands
• network/ : TCP server implementation
• protocol/ : RESP protocol parser
• redis/ : core Redis instance logic (manages config, info, etc.)
• replica/ : replication (master/slave) logic
• storage/ : in-memory key-value and list storage
• utils/ : helper functions (e.g., crypto if authentication is added)
README for Stack Insights
• Uses Go ≥1.18
• Implements RESPv2 parsing, concurrent client handling, basic commands (PING, SET, GET, LPUSH, etc.), and simple replication.
• No external databases—pure in-process storage in storage/.
Main Components
a. Client(s)
• External Redis CLI or any RESP client
b. TCP Server (network/server.go)
• Listens on port 6379
• Spawns a goroutine per connection
c. Protocol Parser (protocol/parser.go)
• Parses incoming RESP messages into structured commands
d. Command Handler (command/…)
• extract_commands.go → maps parsed input to a command enum
• concrete_handlers.go → business logic for each command
e. Redis Instance (redis/Instance.go & info_controller.go)
• Configures the server, holds references to storage and replica modules
• Exposes an interface to execute commands and retrieve INFO
f. Storage Engine (storage/storage.go)
• In-memory data structures for string and list types
g. Replication Module (replica/replica.go)
• Coordinates master-slave syncing; applies commands on the slave
h. Utilities (utils/crypto.go)
• Any helper routines (e.g., hashing or authentication support)
Relationships & Data Flow
1. Client → TCP Server: client sends raw RESP data over TCP
2. TCP Server → Parser: raw bytes streamed into parser
3. Parser → Command Handler: parser emits a command object
4. Command Handler → Redis Instance: calls into instance.Execute(cmd)
5. Redis Instance → Storage / Replication
• For read/write: interacts with in-memory storage
• For replication: if master, forwards command to replica; if slave, applies incoming replication data
6. Redis Instance → Command Handler: returns result
7. Command Handler → TCP Server: serializes result to RESP
8. TCP Server → Client: writes RESP-encoded response
Architectural Patterns & Principles
• Modular layering: network → protocol → handler → core logic → storage/replica
• Concurrency: goroutine per client, sync primitives in storage/replica
• Separation of concerns: parsing vs. execution vs. persistence vs. replication