Server Setup

The first step is to start a TCP listener so that any Client can communicate with it.

l, err := net.Listen("tcp", ":6379")
if err != nil {
    fmt.Println(err)
    return
}

Then we start receiving requests.

conn, err := l.Accept()
if err != nil {
    fmt.Println(err)
    return
}

defer conn.Close() // close connection once finished

After that, we create an infinite loop and receive commands from clients and respond to them.

for {
    buf := make([]byte, 1024)

    // read message from client
    _, err = conn.Read(buf)
    if err != nil {
        if err == io.EOF {
            break
        }
        fmt.Println("error reading from client: ", err.Error())
        os.Exit(1)
    }

    // ignore request and send back a PONG
    conn.Write([]byte("+OK\r\n"))
}

Note that in the last line, we respond with “Ok” regardless of the command received from the client.

Therefore, the complete code will be as follows:

package main

import (
	"fmt"
	"io"
	"net"
	"os"
)

func main() {
	fmt.Println("Listening on port :6379")

	// Create a new server
	l, err := net.Listen("tcp", ":6379")
	if err != nil {
		fmt.Println(err)
		return
	}

	// Listen for connections
	conn, err := l.Accept()
	if err != nil {
		fmt.Println(err)
		return
	}

	defer conn.Close()

	for {
		buf := make([]byte, 1024)

		// read message from client
		_, err = conn.Read(buf)
		if err != nil {
			if err == io.EOF {
				break
			}
			fmt.Println("error reading from client: ", err.Error())
			os.Exit(1)
		}

		// ignore request and send back a PONG
		conn.Write([]byte("+OK\r\n"))
	}
}

If we try to run the server and communicate with it using redis-cli, we can see the expected response.

Note that the port number is the same as Redis.

CLI OUTPUT

Conclusion

That’s it for the first part. In this part, we created a server that can be communicated with by any Redis client and receive responses from it.

Currently, the response is just “Ok”. However, in the upcoming parts, we will start changing this to respond to commands like set, get, hset, hget.