Step 1 of 3 · Reading · ~2 min
Multiple Keys & DBSIZE
String Commands
Multiple Keys & Overwrite
With the basic dict in place, this lesson pushes on it in three directions that expose real design decisions: overwrite semantics, aggregate introspection (DBSIZE), and quoted-argument parsing.
Overwrite is just dict assignment — but verify it
SET should be idempotent-safe to call repeatedly on the same key with no special-casing:
This seems too obvious to test, but it's worth explicitly writing a test for because it's the first place a buggy implementation (e.g. one that used setdefault, or that appended to a list per key instead of overwriting) would surface. Multiple independent keys stored simultaneously should never interfere with each other — a common bug is accidentally sharing mutable state (like reusing the same list object) across keys instead of storing independent values.
DBSIZE
Simple now — len(store) — but note this command's contract for the future: once you add expiry in the next chapter, DBSIZE must not count keys whose TTL has lapsed, even though they may still physically be sitting in the dict (lazy expiry means you don't always clean up eagerly). Keep that constraint in mind: DBSIZE's implementation will need to change from "count of dict entries" to "count of live dict entries" later — don't hard-code an assumption that dict size always equals live key count.
Quoted values
Real Redis clients never need quoting because they use length-prefixed RESP arrays (as you'll build in the RESP Array Parsing lesson). But for this line-based exercise, supporting SET greeting "hello world" means your tokenizer needs to treat a double-quoted span as one argument, not split it on internal spaces.
A minimal quote-aware tokenizer:
Python's shlex.split handles this correctly out of the box, including quote removal — shlex.split('SET greeting "hello world"') gives ["SET", "greeting", "hello world"]. If you're implementing this in another language, you'll need to hand-write a small state machine: track whether you're inside a quoted span, and only split on unquoted whitespace.
Edge cases
- Unterminated quotes (
SET key "oops) — decide how you want to fail gracefully;shlexraisesValueError, which you should catch and turn into a protocol error rather than crashing the process. - Quoted empty string:
SET key ""should produce the value""(empty string), not be dropped as an argument. - This entire quoting concern disappears once you move to real RESP array parsing later in this chapter — that's intentional; it's a stepping stone, not the final architecture.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…