Part 2 of 6 · 1 min read
Get and put
A key-value store looks up a value by its exact key only, like a dictionary, not by any field inside the value.
Think of a cloakroom at a theater. A visitor hands over a coat and receives a numbered ticket. Later, that same numbered ticket is the only way to retrieve the coat. The attendant never looks inside the coat to find it.
In plain words: a key-value store offers exactly two operations. put stores a value under a given key. get retrieves the value stored under that exact key. The value itself can be almost anything: a string, a JSON document, a binary blob. The store does not look inside it. There is no way to ask for "every value where some field equals X." The only question a key-value store answers is: what value, if any, is stored under this exact key?
A worked example. The bookstore wants to cache a customer's shopping cart while they browse. The key is the customer's ID. The value is a JSON blob holding the cart's contents. put("cart:501", '{"items": [...]}') stores it. get("cart:501") retrieves it later. No query ever asks for carts containing a particular book; the application always already knows the customer ID it wants.
The key lines. No SQL; a key-value store is typically accessed through a small get-and-put API, not a query language.
put("cart:501", '{"items": [{"book_id": 1, "quantity": 2}]}')
get("cart:501")
-> '{"items": [{"book_id": 1, "quantity": 2}]}'
Key-value store
A key-value store is a database that supports exactly two core operations: storing a value under a given key, and retrieving a value by that exact key. It treats the value as an opaque blob and cannot query by any field inside it.
Example: a theater cloakroom, where a numbered ticket is the only way to retrieve a coat, with the attendant never inspecting the coat's contents, mirrors a key-value store's get-and-put model.