APPEND for string concatenation in Redis - Time & Space Complexity
We want to understand how the time it takes to add text to a string in Redis changes as the string gets longer.
How does the APPEND command's speed change when the string grows?
Analyze the time complexity of the following code snippet.
APPEND mykey "Hello"
APPEND mykey " World"
APPEND mykey "!"
This code adds pieces of text to the end of the string stored at key "mykey" step by step.
- Primary operation: Adding new text to the end of an existing string.
- How many times: Once per APPEND command, each time the string grows.
Each time we add text, Redis must copy the whole existing string plus the new text to create the updated string.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to copy existing text |
| 100 | About 100 steps to copy existing text |
| 1000 | About 1000 steps to copy existing text |
Pattern observation: The work grows roughly in direct proportion to the string length.
Time Complexity: O(n)
This means the time to append grows linearly with the length of the string already stored.
[X] Wrong: "Appending text always takes the same time no matter how long the string is."
[OK] Correct: Because Redis must copy the whole string each time it adds more text, longer strings take more time.
Understanding how string operations scale helps you explain performance in real applications and shows you can think about efficiency clearly.
"What if Redis used a linked list of strings internally instead of copying the whole string each time? How would the time complexity change?"
Practice
APPEND command do when used on a string key?Solution
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.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.Final Answer:
Adds the given text to the end of the existing string value. -> Option DQuick Check:
APPEND adds text to end [OK]
- Thinking APPEND replaces the string
- Confusing APPEND with DELETE command
- Assuming APPEND returns the string value
Solution
Step 1: Recall Redis command syntax
Redis commands use the format: COMMAND key value without quotes unless the value contains spaces.Step 2: Identify correct option
APPEND greeting world uses correct syntax: APPEND greeting world (no quotes needed here).Final Answer:
APPEND greeting world -> Option AQuick Check:
Redis commands: APPEND key value [OK]
- Adding quotes unnecessarily around key or value
- Swapping key and value order
- Using commas or other separators
SET key1 "Hello" APPEND key1 " World" APPEND key1 "!"What will be the final length returned by the last APPEND command?
Solution
Step 1: Calculate initial string length
SET key1 "Hello" sets string length to 5 characters.Step 2: Add " World" with APPEND
" World" has 6 characters (including space), new length = 5 + 6 = 11.Step 3: Add "!" with APPEND
Adding "!" (1 character) makes length 11 + 1 = 12, but note quotes are not stored.Step 4: Recount carefully
"Hello" (5) + " World" (6) + "!" (1) = 12 characters total.Final Answer:
12 -> Option AQuick Check:
5 + 6 + 1 = 12 [OK]
- Counting quotes as characters
- Forgetting spaces in appended strings
- Adding lengths incorrectly
APPEND mykey "text" but get an error: "WRONGTYPE Operation against a key holding the wrong kind of value". What is the most likely cause?Solution
Step 1: Understand the error message
"WRONGTYPE" means the key exists but is not a string type.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.Final Answer:
The key 'mykey' holds a non-string value like a list or set. -> Option BQuick Check:
WRONGTYPE means key type mismatch [OK]
- Assuming key absence causes WRONGTYPE
- Blaming syntax for WRONGTYPE error
- Ignoring key data type
Solution
Step 1: Initialize the key without leading comma
Use SET to start 'log' with "message1" (no comma at start).Step 2: Append subsequent messages with leading commas
Use APPEND to add ",message2" and ",message3" to separate messages properly.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.Final Answer:
SET log "message1" APPEND log ",message2" APPEND log ",message3" -> Option CQuick Check:
Start with SET, then APPEND with commas [OK]
- Starting with APPEND adding commas first
- Appending without commas causing merged words
- Adding trailing commas unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
