How afloresc/redis-clone works
Purpose and Entry Points
GoCache is a Go 1.23+ Redis-compatible in-memory data server. It accepts RESP2 commands over TCP (default port 6380), supports Redis clients, and optionally persists or replicates data. Process startup is main.go; server lifecycle and per-connection handling are centered in internal/core/app.go.
Network Protocol and Dispatch
TCP connections are handled concurrently, decoded from RESP, and routed to command implementations. The router maps the Redis command surface to handlers; command responses are encoded back as RESP.
• internal/core/app.go
• internal/core/router.go
• internal/utils/resp.go
Command Execution Layer
The command package is the application-facing API over the store and cross-cutting services. CommandImpl/the command interface provide shared command context, while command-family files implement strings, collections, GEO, bitmaps, HyperLogLog, transactions, and pub/sub semantics.
• internal/command/command.go
• internal/command/string.go
• internal/command/zset.go
In-Memory Data and Expiration
The store is the authoritative mutable state, holding Redis-compatible typed values and protecting operations with sync.RWMutex. TTL management runs as a background expiry-cleanup concern and commands must observe expired-key semantics. This is the key concurrency boundary for normal command execution.
• internal/store/store.go
• internal/store/ttl.go
Transaction and Messaging State
Transactions queue commands for MULTI and execute/discard them under transaction rules, including WATCH state. Pub/sub maintains channel and pattern subscriptions and pushes asynchronous messages to subscribed clients, which differs from ordinary request/response command flow.
• internal/transaction/transaction.go
• internal/pubsub/pubsub.go
• internal/command/transaction.go
Durability, Capacity, and Replication
Persistence provides two independent durability paths: binary RDB snapshot save/load and append-only command logging with rewrite/fsync behavior. Eviction applies configured LRU/LFU/random/TTL policies when writes exceed memory limits. Replication manages master/slave support and is a boundary for propagating state beyond local memory.
• internal/persistence/rdb.go
• internal/persistence/aof.go
• internal/eviction/eviction.go
• internal/repl/repl.go
Configuration and Access Control
JSON-backed configuration controls network settings, databases, persistence, eviction, replication, and ACL enablement; runtime Redis CONFIG commands expose mutable settings. ACL/authentication establishes connection authorization before protected command access.
• internal/config/config.go
• internal/acl/acl.go
• internal/command/string.go
Deployment Runtime
The service is packaged as a standalone Go binary and can run in Docker/Compose. External integration is primarily TCP Redis clients (redis-cli and compatible libraries); persistence files are local runtime storage rather than a separate database service.
• Dockerfile
• docker-compose.yml
• main.go
Main entry points: config.go Configuration · app.go TCP server lifecycle · store.go In-memory store · command.go Command API
Components 18
Runtime
- Redis clients · redis-cli and compatible TCP clients.
- Process startup
main.go - Container runtime
Dockerfile - Configuration · Controls network, storage, eviction, replication, and ACL settings.
internal/config/config.go
Protocol
- TCP server lifecycle · Accepts concurrent RESP2 connections on the Redis port.
internal/core/app.go - RESP codec · Decodes requests and encodes command responses.
internal/utils/resp.go - Command router · Maps Redis command names to handlers.
internal/core/router.go
Command execution
- Command API · Shared command context and Redis data-type command families.
internal/command/command.go - Transaction commands · Applies transaction rules to queued commands.
internal/command/transaction.go - ACL and authentication · Authorizes connections before protected command access.
internal/acl/acl.go
Local state
- In-memory store · Authoritative mutable state guarded by sync.RWMutex.
internal/store/store.go - TTL cleanup · Removes expired keys and supports expired-key semantics.
internal/store/ttl.go - Transaction state
internal/transaction/transaction.go - Pub/sub broker · Maintains channel and pattern subscriptions and pushes messages.
internal/pubsub/pubsub.go
Durability and distribution
- RDB snapshots · Saves and loads local snapshots.
internal/persistence/rdb.go - Append-only log · Logs commands with rewrite and fsync behavior.
internal/persistence/aof.go - Eviction policy · Applies configured LRU, LFU, random, or TTL eviction.
internal/eviction/eviction.go - Replication · Propagates state beyond local memory.
internal/repl/repl.go
Connections 25
- Container runtime → Process startup: runs
- Process startup → Configuration: loads
- Process startup → TCP server lifecycle: starts
- Configuration → TCP server lifecycle: network settings
- Configuration → ACL and authentication: ACL enablement
- Configuration → RDB snapshots: persistence settings
- Configuration → Append-only log: persistence settings
- Configuration → Eviction policy: memory policy
- Configuration → Replication: replication settings
- Redis clients → TCP server lifecycle: RESP2 over TCP
- TCP server lifecycle → RESP codec: decode / encode
- TCP server lifecycle → ACL and authentication: authenticate
- TCP server lifecycle → Command router: dispatch request
- Command router → Command API: routes commands
- Command router → Transaction commands: routes transaction commands
- Command API → In-memory store: read and mutate
- Command API → Pub/sub broker: publish / subscribe
- Transaction commands → Transaction state: queue and WATCH
- Transaction commands → Command API: execute queued commands
- TTL cleanup → In-memory store: expires keys
- Eviction policy → In-memory store: evicts keys
- RDB snapshots → In-memory store: save / restore
- Append-only log → In-memory store: replay / record mutations
- Replication → In-memory store: synchronize state
- Pub/sub broker → Redis clients: asynchronous messages