https://harshita9104.medium.com/built-my-own-redis-cbbba57c0111
This project is a simplified, educational implementation of an in-memory database inspired by Redis, built using Go. It demonstrates core database concepts like network communication, data parsing, command handling, and persistence.
-
Basic Redis Command Support:
- Implements
PING,SET,GET,HSET, andHGET. PING: Responds with "PONG" or echoes the provided argument. Implemented inhandler.gowithin thepingfunction.SETandGET: Stores and retrieves key-value pairs using a Go map. Thread safety is ensured withsync.RWMutex. Implemented inhandler.gowithin thesetandgetfunctions.HSETandHGET: Stores and retrieves key-value pairs within hashes, using a nested Go map. Thread safety is also provided bysync.RWMutex. Implemented inhandler.gowithin thehsetandhgetfunctions.
- Implements
-
RESP Protocol:
- Parses and serializes data using the Redis Serialization Protocol (RESP).
- The
resp.gofile contains theRespstruct and its methods for parsing RESP data from client connections. - It also contains the
Valuestruct, and its methods for marshalling data to RESP format for sending responses.
-
Concurrency:
- Handles multiple client connections using Go routines.
- The
main.gofile spawns a new goroutine for each incoming client connection, allowing the server to handle multiple clients concurrently.
-
- Implements Append Only File (AOF) persistence for data durability.
- The
aof.gofile manages the AOF file, writingSETandHSETcommands to the file. - On server startup, it reads the AOF file and replays the commands to restore the database state.
- A goroutine is used to sync the file to disk every second.
- Go 1.16 or later
-
Clone the repository:
git clone https://github.com/harshita9104/Redis_Golang.git
-
Build the server:
go build
-
Run the executable:
go run . -
Connect using
redis-cli:redis-cli
-
Use the implemented Redis commands.
main.go: Contains the main server logic, including connection handling and command dispatch.resp.go: Handles RESP parsing and serialization, defining theRespandValuestructs.handler.go: Implements Redis command handlers, using Go maps for data storage.aof.go: Manages AOF persistence, providing methods for writing and reading commands.


