Databases 18 min read

Build a Simple NoSQL Key‑Value Database in Python from Scratch

This article explains what NoSQL means, compares it with traditional SQL databases, and walks through creating a lightweight Python key‑value store with TCP/IP commands, illustrating schema design, indexing, query examples, and the full source code for a toy NoSQL system.

ITPUB
ITPUB
ITPUB
Build a Simple NoSQL Key‑Value Database in Python from Scratch

What Is NoSQL?

NoSQL has become a buzzword, but the article first defines the term and explains why it is useful by building a simple NoSQL database in pure Python, treating the code as “lightly structured pseudo‑code.”

SQL vs. RDBMS

SQL is a language for querying relational database management systems (RDBMS) such as MySQL, SQL Server, and Oracle. Data in an RDBMS is organized into tables with a fixed schema; each row represents a record and a primary key uniquely identifies rows.

Example table Car with columns:

Make (string)

Model (string)

Year (four‑digit number or date)

Color (string)

VIN (string, primary key)

Queries retrieve columns using statements like SELECT Make, Model FROM Car. The article shows how to filter by year, color, or VIN.

Relations and Normalization

To avoid redundant data, the article demonstrates splitting a ServiceHistory table into two tables: Vehicle (VIN, Make, Model, Year, Color) and ServiceHistory (VIN, Service Performed, Mechanic, Price, Date). The VIN serves as a foreign key linking the two tables.

It also explains the performance impact of table scans versus indexed look‑ups and why indexes improve query speed at the cost of extra memory.

Key/Value Stores Before NoSQL

Before the NoSQL term, key/value stores like memcached provided schema‑less storage using hash tables. The article proposes implementing a similar store in Python, using a dict as the underlying data structure.

Design of the Toy NoSQL Database

The design includes:

Data stored in a Python dict where keys are strings and values can be integers, strings, or lists.

A simple ASCII‑based TCP/IP server that receives commands via a semicolon‑separated message format.

Supported Commands

PUT – Insert a new key/value pair.

GET – Retrieve the value for a key.

PUTLIST – Insert a list value.

APPEND – Append an element to an existing list.

INCREMENT – Increment an integer value.

DELETE – Remove a key.

STATS – Return success/failure statistics for each command.

Message Structure

A request message consists of COMMAND;KEY;VALUE;TYPE where the last three fields are optional but the semicolons must always be present. A response message starts with True|False followed by either a result value or an error description.

Implementation Details

The article provides the full source code (about 180 lines) split into sections:

Setup – Imports, data initialization, and a lookup table COMMAND_HANDLERS that maps commands to handler functions.

Command Parser – Parses incoming messages, performs type conversion (e.g., int(), str.split(',')), and dispatches to the appropriate handler.

Command Handlers – Functions such as handle_put, handle_get, handle_append, each returning a tuple indicating success and the result.

The code demonstrates multiple assignment, code reuse, and minimal error checking, illustrating how a tiny key/value store can behave like a NoSQL database.

Why This Is Still a NoSQL Database

Although it lacks the full features of an RDBMS, the implementation qualifies as a NoSQL database because it stores data without a fixed schema, offers simple CRUD operations via a network protocol, and can be extended with schemas for more structured queries.

Querying Limitations

Because the store only supports key‑based look‑ups, complex queries (e.g., “find all 1994 cars’ colors”) require scanning every entry, which is less efficient than indexed relational queries. The article discusses possible extensions such as JSON‑structured values or namespaces to improve queryability.

Conclusion

The tutorial clarifies the NoSQL concept, reviews basic SQL and RDBMS operations, and walks through building a functional toy NoSQL key/value store in Python, highlighting trade‑offs between simplicity, flexibility, and query performance.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SQLdatabaseTutorialNoSQLkey-value store
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.