Typorithm · medium

Key Namespacing

redis-key-patternsredisnamespacingscankeys
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Key Namespacing

0%

Strings & Keys

Colon-delimited namespaces like user:1:name organize the flat keyspace; MSET sets many keys at once, while KEYS does a blocking O(N) pattern match best avoided in production and SCAN is its cursor-based non-blocking alternative, and EXISTS checks presence.

Redis has one flat keyspace, so teams impose structure with colon-delimited names like user:1:name and user:1:city. MSET sets several of these pairs in a single atomic command.

To find keys by pattern, KEYS user:1:* matches a glob — but it is O(N) and blocks the server while it walks every key, which can stall a busy instance, so it is reserved for debugging.

SCAN 0 MATCH user:* COUNT 100 is the production-safe alternative: it returns keys in small batches using a cursor, never blocking. EXISTS user:1:name simply reports whether a key is present (1) or not (0).

01

Colons namespace keys: user:1:name.

02

KEYS is O(N) and blocking — avoid in prod.

03

SCAN is the cursor-based non-blocking alternative.

SET user:1:name "Ada"
MSET user:1:age 36 user:1:city "London"
KEYS user:1:*
SCAN 0 MATCH user:* COUNT 100
EXISTS user:1:name

Read the implementation

Connect each part of the code to the concept before you type.

1. Namespaced key

Colons split a key into a namespace path like user:1:name, grouping related data.

SET user:1:name "Ada"

2. Set many at once

MSET assigns several key/value pairs in a single atomic command.

MSET user:1:age 36 user:1:city "London"

3. Match keys (blocking)

KEYS scans the entire keyspace for a glob pattern — O(N) and blocking, so avoid it in production.

KEYS user:1:*

4. Cursor-based scan

SCAN walks the keyspace incrementally with a cursor, never blocking the server.

SCAN 0 MATCH user:* COUNT 100

5. Check presence

EXISTS returns 1 if the key is present and 0 otherwise.

EXISTS user:1:name

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Key Namespacing: Lesson & Practice · Typorithm | ApplyDjinn