43. Capstone: a mini Redis-like key-value store with a wire protocol
This capstone combines nearly every earlier Part: Section 21's sockets, Section 18.2's hash table, Section 23.2's event-loop style, Section 16.3's parsing technique (applied to a wire protocol instead of JSON), and Section 40's API discipline.
The protocol — a simplified line-based version of Redis's real RESP protocol:
SET key value\r\n -> +OK\r\n
GET key\r\n -> $value\r\n (or $-1\r\n if missing)
DEL key\r\n -> :1\r\n (or :0\r\n if it wasn't present)
// kvserver.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/select.h>
#include "hashtable_str.h" // Section 18.2's hash table, adapted to store string values
#define MAX_CLIENTS 64
#define BUF_SIZE 4096
static void handle_command(HashTable *db, char *line, char *response, size_t resp_cap) {
char cmd[16], key[128], value[BUF_SIZE];
int n = sscanf(line, "%15s %127s %4095[^\r\n]", cmd, key, value);
if (strcmp(cmd, "SET") == 0 && n == 3) {
ht_set_str(db, key, value);
snprintf(response, resp_cap, "+OK\r\n");
} else if (strcmp(cmd, "GET") == 0 && n >= 2) {
const char *val = ht_get_str(db, key);
if (val) snprintf(response, resp_cap, "$%s\r\n", val);
else snprintf(response, resp_cap, "$-1\r\n");
} else if (strcmp(cmd, "DEL") == 0 && n >= 2) {
int deleted = ht_del_str(db, key);
snprintf(response, resp_cap, ":%d\r\n", deleted);
} else {
snprintf(response, resp_cap, "-ERR unknown command\r\n");
}
}
int main(void) {
HashTable *db = ht_new(1024);
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
int opt = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt);
struct sockaddr_in addr = { .sin_family = AF_INET, .sin_port = htons(6380),
.sin_addr.s_addr = INADDR_ANY };
bind(server_fd, (struct sockaddr *)&addr, sizeof addr);
listen(server_fd, 16);
printf("mini-kv listening on :6380\n");
int clients[MAX_CLIENTS] = {0};
while (1) {
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(server_fd, &readfds);
int max_fd = server_fd;
for (int i = 0; i < MAX_CLIENTS; i++)
if (clients[i] > 0) { FD_SET(clients[i], &readfds); if (clients[i] > max_fd) max_fd = clients[i]; }
select(max_fd + 1, &readfds, NULL, NULL, NULL);
if (FD_ISSET(server_fd, &readfds)) {
int fd = accept(server_fd, NULL, NULL);
for (int i = 0; i < MAX_CLIENTS; i++) if (clients[i] == 0) { clients[i] = fd; break; }
}
for (int i = 0; i < MAX_CLIENTS; i++) {
if (clients[i] > 0 && FD_ISSET(clients[i], &readfds)) {
char line[BUF_SIZE] = {0};
ssize_t n = read(clients[i], line, sizeof line - 1);
if (n <= 0) { close(clients[i]); clients[i] = 0; continue; }
char response[BUF_SIZE];
handle_command(db, line, response, sizeof response);
write(clients[i], response, strlen(response));
}
}
}
}
./kvserver &
printf 'SET name kumaran\r\n' | nc -q1 localhost 6380 # +OK
printf 'GET name\r\n' | nc -q1 localhost 6380 # $kumaran
printf 'DEL name\r\n' | nc -q1 localhost 6380 # :1
printf 'GET name\r\n' | nc -q1 localhost 6380 # $-1
Where to take it next, each a direct application of an earlier Part:
persistence via an append-only log you replay on startup (a simplified
version of Redis's AOF); a background save via fork() (Section 26) so
the child writes a point-in-time snapshot while the parent keeps serving
requests, unaffected by the child's memory writes thanks to
copy-on-write; multiple databases via multiple hash tables; and
expiring keys via a min-heap of (expire_time, key) pairs checked once
per event-loop iteration. This is, concept for concept, how Redis itself
grew from a single-file prototype into the production system Section 34
walked through.