Functional Requirements
The primary operations of the database include:- GET: Retrieve the value associated with a specified key.
- SET: Store or update a key-value pair.
- DELETE: Remove a key-value pair.

Non-functional Requirements
- Performance: Handle a moderate number of concurrent client connections.
- Reliability: Ensure data integrity through robust error handling and periodic snapshots.
- Simplicity: Utilize a minimal and easy-to-understand design.
- Security: Validate inputs to safely manage malicious or malformed requests.
High-Level Architecture
The system is organized into several key components:- Networking Layer: Manages incoming TCP connections using an asynchronous runtime (e.g., Tokio).
- Command Parser: Transforms raw client input into validated and structured commands.
- Core Key-Value Store: Maintains an in-memory data structure with thread-safe access.
- Persistence Module: Persists snapshots of the in-memory data to disk and reloads them on startup.
- Command Execution Engine: Processes the commands by interfacing with both the key-value store and the persistence module.

- A client sends a command (e.g., GET, SET, DELETE) over TCP.
- The networking layer reads the incoming request asynchronously.
- The command parser converts the raw input into a structured command.
- The command execution engine processes the command against the key-value store.
- For SET and DELETE, changes are applied in-memory and immediately persisted to disk.
- A response such as “OK”, “ERROR”, or the retrieved value is sent back to the client.
API Commands
The server supports the following commands:-
GET key
Retrieves the value associated with the specified key. -
SET key value
Stores the provided key-value pair. The server responds with “OK” upon success. -
DELETE key
Removes the key-value pair, returning “OK” on successful deletion.
Design Considerations
Key design aspects of this project include:- Thread Safety: Utilize mutexes to manage concurrent read/write operations securely.
- Data Durability: Persist updates immediately to disk and perform periodic snapshots.
- Error Handling: Provide descriptive errors for invalid or malformed inputs.
- Scalability: Process multiple client requests concurrently using asynchronous programming.

Technology Stack

Setting Up the Project
Begin by creating a new Rust project using Cargo. Update yourCargo.toml file with the necessary dependencies:
anyhow crate simplifies error handling using trait object–based error types.
Networking Layer Implementation
The networking layer handles incoming TCP connections concurrently. The code below shows the main server logic along with the client-handling function.Main Function
Handle Client Function
Command Parsing
The command parser converts raw string inputs into structured commands for the system to process. Create a file namedcommand.rs with the following content:
Key-Value Store Implementation
The in-memory key-value store is implemented using Rust’s asynchronous mutex for safe concurrency. Create a file namedstore.rs with the following code:
Persistence Module
To ensure data durability between server restarts, create a file namedpersistence.rs. This module extends KeyValueStore with methods to load from and save data to disk using JSON serialization with asynchronous file I/O provided by Tokio.
Testing the Application
After running the application with:-
Connect to the server:
-
Use these commands to interact with the server:
GET x→ Expected response:ERROR Key not foundSET x 100→ Expected response:OKGET x→ Expected response:VALUE 100DELETE x→ Expected response:OK- For an unknown command like
FOO, the server replies withERROR Unknown Command
data.json file is created and properly populated. Restarting the server will reload the stored keys automatically.
Example content of data.json after some operations:
Congratulations! You have successfully implemented a robust key-value store with comprehensive networking, command parsing, thread-safe in-memory storage, and persistence. This project demonstrates key Rust programming principles and asynchronous programming using Tokio.