This is my solution to the "Build Your Own Redis" Challenge.
Spawning a new server instance (--port is optional, defaults to port 6379):
$ ./spawn_redis_server.sh --port 6379
Spawning a new server instance, loading a saved rdb file data located at /tmp/redis/dump.rdb:
$ ./spawn_redis_server.sh --dir /tmp/redis-files --dbfilename dump.rdb
Spawning a new replica of an existing server @localhost:6379:
$ ./spawn_redis_server.sh --port 8899 --replicaof localhost 6379
The server can respond to PING and ECHO command.
On the first terminal:
$ ./spawn_redis_server.sh
On the second terminal:
$ redis-cli ping # "PONG"
$ redis-cli echo "Hello world!" # "Hello world!"
The server can respond to SET and GET command.
On the first terminal:
$ ./spawn_redis_server.sh
On the second terminal:
$ redis-cli set foo bar # "OK"
$ redis-cli get foo # "bar"
The server also supports SET with timeout:
// Setting timeout to 5000 ms
$ redis-cli set foo bar px 5000 # "OK"
$ redis-cli get foo # "bar"
// after 5 seconds
$ redis-cli get foo # (nil)
All data are forwarded to each replica. A client can send a SET command to the master and the data will be duplicated on the other replicas.
On the first terminal:
$ ./spawn_redis_server.sh
$ ./spawn_redis_server.sh --port 8899 --replicaof localhost 6379
On the second terminal:
$ redis-cli set foo bar
// talk to the replica instead
$ redis-cli -p 8899 get foo # "bar"
The server also supports the WAIT command. WAIT N TIMEOUT will wait for TIMEOUT milliseconds or until the server receives an acknowledgement from at least N servers, whichever is faster.
Continuing from previous scenario, on the second terminal:
$ redis-cli set key value
$ redis-cli WAIT 1 500 # returns 1 immediately
$ redis-cli WAIT 3 1000 # returns 1 after timeout of 1 second
The server can read a .rdb file generated by a redis server (https://redis.io/download/) and load its content.
Generating a .rdb file:
On the first terminal:
$ redis-server
On the second terminal:
$ redis-cli set foo bar
$ redis-cli save
Then, press Ctrl+C on the first terminal. A file dump.rdb will be created.
The server can use this .rdb file to update its key-value store:
On the first terminal:
$ ./spawn_redis_server.sh --dir . --dbfilename dump.rdb
On the second terminal:
$ redis-cli get foo # "bar"
The server can also persist its key-value store to a .rdb file which can be read by a redis server.
Client needs to send the "save" command to instruct the server to write its key-value store to disk upon exit.
On the first terminal:
$ ./spawn_redis_server.sh
On the second terminal:
$ redis-cli set foo bar
$ redis-cli save
Then, press Ctrl+C on the first terminal. A file TIMESTAMPdump.rdb will be created. TIMESTAMP will be in
YYMMDD_HHMMSS format.
Simply rename this file to dump.rdb and a redis server will be able to load the contents.
$ redis-server