Skip to content
GET / PUT / DELETE
step 1/5

Reading — step 1 of 5

Read

~2 min readIn-Memory Foundations

GET / PUT / DELETE

Every key-value store — Redis, LevelDB, DynamoDB, etcd — is, at its core, a map with three operations. Before we add logs, SSTables, and transactions, get the in-memory contract exactly right, because every later lesson layers on top of it.

python

A Python dict gives O(1) average PUT/GET/DELETE. What it does NOT give you is ordering or persistence — the next lessons and chapters fix that.

API design decisions real stores disagree on

Should PUT return the value it replaced? Should DELETE say whether the key existed?

  • Redis SET: returns OK, or the old value if you ask for it.
  • LevelDB Put: returns nothing; if you care about the old value, Get first.
  • etcd Put: can return the previous value plus a revision number.

Returning the old value is handy for compare-and-set patterns, but it forces a read on every write path. LevelDB refuses to pay that, and our protocol takes the same position: mutations acknowledge with a bare OK and return nothing else. DELETE on a missing key still prints OK — deletes are idempotent, which matters later when a crash-recovery replay applies the same delete twice.

This course's wire protocol

Your program reads commands from stdin, one per line, and answers on stdout:

  • PUT <key> <value>OK (silently overwrites)
  • GET <key> → the value, or <nil> if missing
  • DELETE <key>OK (even if the key was absent)
  • LIST → one <key>=<value> line per pair, sorted by key ascending; prints nothing at all when the store is empty
  • COUNT → number of keys

Two details are traps:

Values may contain spaces. PUT k hello world stores the value hello world. The idiom is a bounded split:

python

An unbounded line.split() would shred the value into pieces and keep only hello.

LIST prints pairs, not keys. Each line is name=alice, never a bare name, and there are no spaces around the =.

Keys are case-sensitive (Name and name are different keys), and a re-PUT replaces in place: after PUT x 1, PUT y 2, PUT x 3, the store holds two keys and x is 3.

Your exercise

Implement the five commands. The grader-caught mistakes, from the real tests: (1) PUT k hello world then GET k must print hello world — a full split() prints hello and fails the hidden test; (2) on an empty store, GET missing prints <nil>, DELETE missing prints OK (not an error), COUNT prints 0, and LIST prints nothing — there is no (empty) marker in this lesson; (3) overwrites replace rather than duplicate — PUT x 1, PUT y 2, PUT x 3 must leave GET x at 3 and COUNT at 2.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…