A simple implementation of Redis server in Go programming lanugage.
I built this Redis server in Go as a personal project and a learning experience. It helped me better understand in-memory databases and how to handle network connections in Go efficiently.
Ensure you have either Docker or Go environments set up. Additionally, make sure Redis is installed on your local machine.
git clone https://github.com/danilovict2/go-redis.git
cd go-redisdocker build .
docker container run -p "6379:6379" <IMAGE ID>./your_program.shOpen a new terminal and run the following command to start the Redis client:
redis-cliPINGECHOSETGETCONFIG GETKEYSINFO REPLICATIONREPLCONF GETACKWAITTYPEXADDXRANGEXREADINCR
A replica is a clone of a Redis instance that listens to all write commands and executes them.
- Create a network:
docker network create my-network- Run the main program:
docker container run -p "6379:6379" --network my-network --name leader <IMAGE ID>- Start the replica in a new terminal window:
docker container run -p "<PORT>:<PORT>" --network my-network --name replica <IMAGE ID> --port <PORT> --replicaof "leader 6379"- Connect to the replica:
redis-cli -p <PORT>- Run the main program:
./your_program.sh- Start the replica in a new terminal window:
./your_program.sh --port <PORT> --replicaof "localhost 6379"- Connect to the replica:
redis-cli -p <PORT>To load data from an RDB file into the program, you can either:
- Place the file in the project's root directory and name it
dump.rdb. - Or, run the following command to specify the file's location and name:
./your_program --dir "/path/to/your/file" --dbfilename "file.rdb"For example, if you have file.rdb in the home directory, run:
./your_program.sh --dir "/home" --dbfilename "file.rdb"To create a stream and add an entry to it, use the following command:
redis-cli XADD some_key 1526985054069-0 temperature 36 humidity 95The output will be the ID of the first added entry:
"1526985054069-0"Once you've added a few entries to the stream, you can query them using the XRANGE command.
- Querying a Specific Range:
$ redis-cli XADD some_key 1526985054069-0 temperature 36 humidity 95
"1526985054069-0"
$ redis-cli XADD some_key 1526985054079-0 temperature 37 humidity 94
"1526985054079-0"
$ redis-cli XRANGE some_key 1526985054069 1526985054079
1) 1) 1526985054069-0
2) 1) temperature
2) 36
3) humidity
4) 95
2) 1) 1526985054079-0
2) 1) temperature
2) 37
3) humidity
4) 94- Querying Until the End of the Stream:
Use the + symbol to query entries up to the end of the stream:
$ redis-cli XRANGE some_key 1526985054069 +
1) 1) 1526985054069-0
2) 1) temperature
2) 36
3) humidity
4) 95
2) 1) 1526985054079-0
2) 1) temperature
2) 37
3) humidity
4) 94- Querying from the Beginning of the Stream:
Use the - symbol to query entries from the beginning of the stream:
$ redis-cli XRANGE some_key - 1526985054079
1) 1) 1526985054069-0
2) 1) temperature
2) 36
3) humidity
4) 95
2) 1) 1526985054079-0
2) 1) temperature
2) 37
3) humidity
4) 94The XREAD command is used to read data from one or more streams, starting from a specified entry ID:
$ redis-cli XADD some_key 1526985054069-0 temperature 36 humidity 95
"1526985054069-0"
$ redis-cli XADD some_key 1526985054079-0 temperature 37 humidity 94
"1526985054079-0"
$ redis-cli XREAD streams some_key 1526985054069-0
1) 1) "some_key"
2) 1) 1) 1526985054079-0
2) 1) temperature
2) 37
3) humidity
4) 94You can block reads until a new entry is added to the stream or the specified timeout is reached. Use the $ symbol for this:
$ redis-cli XADD some_key 1526985054069-0 temperature 36 humidity 95
"1526985054069-0"
$ redis-cli XREAD block 1000 streams some_key $This command will block reads until either 1000ms has passed or another Redis client adds a new entry to the stream:
$ redis-cli -p <PORT> XADD some_key 1526985054079-0 temperature 37 humidity 94
"1526985054079-0"> RPUSH list "bar" "baz"
> LPUSH list_key "a" "b" "c"> RPUSH list_key "a" "b" "c" "d" "e"
(integer) 5
# List first 2 items
> LRANGE list_key 0 1
1) "a"
2) "b"
# List last 2 items
> LRANGE list_key -2 -1
1) "d"
2) "e"> RPUSH list_key "a" "b" "c" "d"
(integer) 4
> LLEN list_key
(integer) 4> RPUSH list_key "a" "b" "c" "d"
(integer) 4
> LPOP list_key 2
1) "a"
2) "b"
> LRANGE list_key 0 -1
1) "c"
2) "d"> BLPOP list_key 0
# ... this blocks until an element is added to the list
# As soon as an element is added, it responds with a resp array:
1) "list_key"
2) "foobar"
$ redis-cli BLPOP list_key 0.1
# (Blocks for 0.1 seconds)go build -o redis app/*.go./redisIf you'd like to contribute, please fork the repository and open a pull request to the master branch.