Bird
Raised Fist0
Redisquery~10 mins

APPEND for string concatenation in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - APPEND for string concatenation
Start with key and initial string
↓
Call APPEND command with key and new string
↓
Redis checks if key exists
↓
Return new length of string stored at key
↓
End
APPEND adds a new string to the existing string stored at a key or creates the key if it doesn't exist, then returns the new string length.
Execution Sample
Redis
SET greeting "Hello"
APPEND greeting " World"
GET greeting
Starts with 'Hello', appends ' World', then retrieves the full string.
Execution Table
StepCommandKey Exists?ActionString Value at KeyReturn Value
1SET greeting "Hello"NoCreate key 'greeting' with 'Hello'HelloOK
2APPEND greeting " World"YesConcatenate ' World' to 'Hello'Hello World11
3GET greetingYesRetrieve valueHello WorldHello World
4APPEND newkey "Hi"NoCreate key 'newkey' with 'Hi'Hi2
5GET newkeyYesRetrieve valueHiHi
💡 Commands complete; APPEND returns new string length, GET returns full string.
Variable Tracker
KeyStartAfter Step 1After Step 2After Step 4
greetingnullHelloHello WorldHello World
newkeynullnullnullHi
Key Moments - 3 Insights
What happens if the key does not exist when APPEND is called?
Redis creates the key with the new string as its value, as shown in step 4 of the execution_table.
What does APPEND return after adding the new string?
It returns the length of the new string stored at the key, for example 11 after step 2.
Does APPEND overwrite the existing string or add to it?
APPEND adds to the existing string, it does not overwrite it, as seen in step 2 where ' World' is added to 'Hello'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the string value at key 'greeting' after step 2?
A" World"
B"Hello"
C"Hello World"
Dnull
💡 Hint
Check the 'String Value at Key' column for step 2.
At which step does APPEND create a new key because it does not exist?
AStep 4
BStep 3
CStep 2
DStep 1
💡 Hint
Look for 'Key Exists?' = No and APPEND command in the execution_table.
If you APPEND "!!!" to 'greeting' after step 2, what would the return value be?
A11
B14
C8
Dnull
💡 Hint
Add the length of '!!!' (3) to the current string length (11) from step 2.
Concept Snapshot
APPEND key value
- Adds 'value' to string at 'key'.
- Creates key if missing.
- Returns new string length.
- Does not overwrite existing string.
- Useful for building strings step-by-step.
Full Transcript
The APPEND command in Redis adds a new string to the existing string stored at a key. If the key does not exist, Redis creates it with the new string as its value. After appending, the command returns the length of the new string stored at the key. For example, starting with key 'greeting' set to 'Hello', APPEND ' World' results in 'Hello World' with length 11. This command is useful for concatenating strings incrementally without overwriting the original content.

Practice

(1/5)
1. What does the Redis APPEND command do when used on a string key?
easy
A. Returns the current string value without changes.
B. Replaces the entire string value with the new text.
C. Deletes the string key from the database.
D. Adds the given text to the end of the existing string value.

Solution

  1. Step 1: Understand the APPEND command purpose

    The APPEND command adds new text to the end of an existing string without removing the old text.
  2. Step 2: Compare options with command behavior

    Only Adds the given text to the end of the existing string value. correctly describes adding text to the end, not replacing or deleting.
  3. Final Answer:

    Adds the given text to the end of the existing string value. -> Option D
  4. Quick Check:

    APPEND adds text to end [OK]
Hint: APPEND never replaces, it always adds at the end [OK]
Common Mistakes:
  • Thinking APPEND replaces the string
  • Confusing APPEND with DELETE command
  • Assuming APPEND returns the string value
2. Which of the following is the correct syntax to append the string 'world' to the key 'greeting' in Redis?
easy
A. APPEND greeting world
B. APPEND 'greeting' world
C. APPEND greeting 'world'
D. APPEND 'greeting' 'world'

Solution

  1. Step 1: Recall Redis command syntax

    Redis commands use the format: COMMAND key value without quotes unless the value contains spaces.
  2. Step 2: Identify correct option

    APPEND greeting world uses correct syntax: APPEND greeting world (no quotes needed here).
  3. Final Answer:

    APPEND greeting world -> Option A
  4. Quick Check:

    Redis commands: APPEND key value [OK]
Hint: Use APPEND key value without quotes unless spaces exist [OK]
Common Mistakes:
  • Adding quotes unnecessarily around key or value
  • Swapping key and value order
  • Using commas or other separators
3. Given the following Redis commands executed in order:
SET key1 "Hello"
APPEND key1 " World"
APPEND key1 "!"
What will be the final length returned by the last APPEND command?
medium
A. 12
B. 13
C. 6
D. 11

Solution

  1. Step 1: Calculate initial string length

    SET key1 "Hello" sets string length to 5 characters.
  2. Step 2: Add " World" with APPEND

    " World" has 6 characters (including space), new length = 5 + 6 = 11.
  3. Step 3: Add "!" with APPEND

    Adding "!" (1 character) makes length 11 + 1 = 12, but note quotes are not stored.
  4. Step 4: Recount carefully

    "Hello" (5) + " World" (6) + "!" (1) = 12 characters total.
  5. Final Answer:

    12 -> Option A
  6. Quick Check:

    5 + 6 + 1 = 12 [OK]
Hint: Count characters added stepwise, including spaces [OK]
Common Mistakes:
  • Counting quotes as characters
  • Forgetting spaces in appended strings
  • Adding lengths incorrectly
4. You run the command APPEND mykey "text" but get an error: "WRONGTYPE Operation against a key holding the wrong kind of value". What is the most likely cause?
medium
A. The key 'mykey' does not exist yet.
B. The key 'mykey' holds a non-string value like a list or set.
C. The APPEND command syntax is incorrect.
D. Redis server is down.

Solution

  1. Step 1: Understand the error message

    "WRONGTYPE" means the key exists but is not a string type.
  2. Step 2: Analyze possible causes

    If the key held a string or did not exist, APPEND would work or create it. Syntax error or server down would cause different errors.
  3. Final Answer:

    The key 'mykey' holds a non-string value like a list or set. -> Option B
  4. Quick Check:

    WRONGTYPE means key type mismatch [OK]
Hint: WRONGTYPE means key is not a string [OK]
Common Mistakes:
  • Assuming key absence causes WRONGTYPE
  • Blaming syntax for WRONGTYPE error
  • Ignoring key data type
5. You want to build a Redis string key 'log' that starts empty and appends multiple messages separated by commas. Which sequence of commands correctly achieves this without leading commas?
hard
A. APPEND log ",message1" APPEND log ",message2" APPEND log ",message3"
B. APPEND log "message1" APPEND log "message2" APPEND log "message3"
C. SET log "message1" APPEND log ",message2" APPEND log ",message3"
D. SET log "message1," APPEND log "message2," APPEND log "message3,"

Solution

  1. Step 1: Initialize the key without leading comma

    Use SET to start 'log' with "message1" (no comma at start).
  2. Step 2: Append subsequent messages with leading commas

    Use APPEND to add ",message2" and ",message3" to separate messages properly.
  3. Step 3: Verify no leading comma and correct separators

    SET log "message1" APPEND log ",message2" APPEND log ",message3" correctly avoids leading comma and separates messages with commas.
  4. Final Answer:

    SET log "message1" APPEND log ",message2" APPEND log ",message3" -> Option C
  5. Quick Check:

    Start with SET, then APPEND with commas [OK]
Hint: Start with SET, then APPEND with commas to separate [OK]
Common Mistakes:
  • Starting with APPEND adding commas first
  • Appending without commas causing merged words
  • Adding trailing commas unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard