Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

C++intermediatebackendAI generated

In-Memory Key-Value Store with TCP Interface

Build a small in-memory database server in C++, like a mini Redis. Clients connect over TCP (with telnet or netcat) and send one text command per line, such as SET name Ana, GET name or DEL name, and the server answers each one. Connections are handled asynchronously with Boost.Asio, so one server handles many clients at once without a thread for each. Data lives in a std::unordered_map protected by a mutex, and a background timer saves a snapshot to disk every few seconds, which is loaded again at startup. You'll learn asynchronous networking with Boost.Asio, designing a simple text protocol, thread safety, writing files safely, and testing a network server.

Estimate
~10.5h
Steps
6
Completed by
0
Proposed by
codeseed.app

Boost.Asio · std::unordered_map

Project roadmap

  1. 01

    Accept connections with Boost.Asio

    ~2h

    Set up a CMake project with Boost (header-only Asio is enough). Write a server that accepts connections with async_accept, and a Session class (kept alive with shared_from_this) that reads lines with async_read_until and echoes them back. Run io_context on a few threads. Done when: several telnet clients connect at once and get their lines echoed.

  2. 02

    Parse commands

    ~1.5h

    Write a parser that turns a line into a command: SET key value (the value is the rest of the line), GET key, DEL key, EXISTS key, KEYS and PING. Reject unknown commands, wrong numbers of arguments, and lines over 64 KB with an ERR message. Keep the parser separate from the networking so it can be tested alone. Done when: unit tests cover every command and every kind of invalid input.

  3. 03

    Build the thread-safe store

    ~1.5h

    Create a Store class wrapping std::unordered_map<std::string, std::string> with get, set, del, exists and keys methods. Protect it with a std::shared_mutex, so many reads can happen at once while writes are exclusive. Done when: a test with 8 threads doing mixed reads and writes finishes without errors under ThreadSanitizer.

  4. 04

    Connect commands to responses

    ~1.5h

    Run each parsed command against the store and reply with one line: OK, the value, (nil) when the key doesn't exist, the number of keys deleted, PONG, or ERR followed by the reason. Write replies with async_write, making sure a new write never starts before the previous one finishes, for example with a queue per session. Done when: a telnet session can set, get and delete keys with the right replies.

  5. 05

    Save snapshots to disk

    ~2h

    Use a steady_timer to save the store every N seconds (configurable): copy the data while holding the lock briefly, then write it without the lock to a temporary file, and rename it over the real snapshot so a crash never leaves a half-written file. Use a format that handles spaces and newlines in values, such as length-prefixed strings. Load the snapshot at startup. Done when: data set before a restart is still there afterwards.

  6. 06

    Test the server

    ~2h

    Write an integration test that starts the server on a free port, connects with a Boost.Asio client, and checks a full sequence of commands and replies. Add a small load test with 50 clients each sending 1,000 commands, and check every reply is correct. Done when: both tests pass, and ThreadSanitizer reports no data races.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~10.5h · 6 steps

Tech stack

Boost.Asiostd::unordered_map