Bird
Raised Fist0
Redisquery~10 mins

XADD for adding entries in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - XADD for adding entries
Start XADD command
↓
Check stream exists?
No→Create new stream
Yes↓
Generate entry ID
↓
Add entry with fields
↓
Return entry ID
↓
End
This flow shows how Redis XADD adds an entry: it checks the stream, creates if missing, generates an ID, adds the entry, then returns the ID.
Execution Sample
Redis
XADD mystream * sensor-id 1234 temperature 19.8
XADD mystream * sensor-id 1235 temperature 20.1
Adds two entries to 'mystream' with auto-generated IDs and fields for sensor-id and temperature.
Execution Table
StepCommandStream Exists?Entry ID GeneratedEntry AddedOutput
1XADD mystream * sensor-id 1234 temperature 19.8No (stream created)1609459200000-0Yes1609459200000-0
2XADD mystream * sensor-id 1235 temperature 20.1Yes1609459200001-0Yes1609459200001-0
3End---No more commands
💡 All entries added; no more commands to process.
Variable Tracker
VariableStartAfter 1After 2Final
mystream entriesempty[1609459200000-0: {sensor-id:1234, temperature:19.8}][1609459200000-0: {...}, 1609459200001-0: {sensor-id:1235, temperature:20.1}]2 entries
Key Moments - 3 Insights
Why does XADD create the stream if it doesn't exist?
XADD automatically creates the stream to simplify adding entries, so you don't need to create it manually first. See execution_table step 1 where the stream was created.
What does the '*' mean in the XADD command?
The '*' tells Redis to auto-generate a unique entry ID based on the current time. This is shown in execution_table where IDs like '1609459200000-0' are generated.
Can the entry ID be specified manually instead of '*'?
Yes, you can specify your own ID, but using '*' is easier and ensures uniqueness. This example uses '*', as shown in the execution_sample.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the entry ID generated for the first XADD command?
A*
B1609459200001-0
C1609459200000-0
Dmystream
💡 Hint
Check the 'Entry ID Generated' column in row 1 of the execution_table.
At which step does the stream get created?
AStep 2
BStep 1
CStep 3
DIt is never created
💡 Hint
Look at the 'Stream Exists?' column in the execution_table for step 1.
If you replace '*' with a manual ID, how would the execution_table change?
AEntry ID Generated would show the manual ID instead of auto-generated
BStream would not be created
CEntry would not be added
DOutput would be empty
💡 Hint
Consider the 'Entry ID Generated' column and how IDs are assigned.
Concept Snapshot
XADD key * field value [field value ...]
- Adds an entry to a Redis stream
- '*' auto-generates a unique ID
- Creates stream if missing
- Returns the entry ID
- Fields are key-value pairs for the entry
Full Transcript
The XADD command in Redis adds entries to a stream. If the stream does not exist, XADD creates it automatically. The '*' symbol tells Redis to generate a unique entry ID based on the current time. Each entry consists of fields as key-value pairs. After adding, XADD returns the entry ID. This process is shown step-by-step in the execution table, tracking stream creation, ID generation, and entry addition.

Practice

(1/5)
1. What does the Redis command XADD do?
easy
A. Adds a new entry to a Redis stream with a unique ID
B. Deletes an entry from a Redis stream
C. Reads entries from a Redis stream
D. Creates a new Redis database

Solution

  1. Step 1: Understand the purpose of XADD

    XADD is used to add new entries to a Redis stream, each with a unique ID generated by Redis or specified by the user.
  2. Step 2: Compare with other commands

    Deleting entries is done by XDEL, reading by XRANGE or XREAD, and creating databases is unrelated to XADD.
  3. Final Answer:

    Adds a new entry to a Redis stream with a unique ID -> Option A
  4. Quick Check:

    XADD adds entries = A [OK]
Hint: XADD always adds entries to streams, not deletes or reads [OK]
Common Mistakes:
  • Confusing XADD with XDEL or XRANGE
  • Thinking XADD creates databases
  • Assuming XADD reads data instead of adding
2. Which of the following is the correct syntax to add an entry with fields name and age to a Redis stream mystream using XADD?
easy
A. XADD mystream name John age 30 *
B. XADD mystream name=John age=30 *
C. XADD mystream * name John age 30
D. XADD mystream * (name John) (age 30)

Solution

  1. Step 1: Recall XADD syntax

    The correct syntax is: XADD stream_name * field1 value1 field2 value2 ... The asterisk (*) tells Redis to generate the ID automatically.
  2. Step 2: Check each option

    XADD mystream * name John age 30 matches the syntax exactly. Options A, C, and D have incorrect order or formatting.
  3. Final Answer:

    XADD mystream * name John age 30 -> Option C
  4. Quick Check:

    XADD syntax = XADD stream * fields [OK]
Hint: Use * right after stream name to auto-generate ID [OK]
Common Mistakes:
  • Placing * after fields instead of immediately after stream name
  • Using equal signs (=) in field-value pairs
  • Adding parentheses around fields
3. Given the Redis command: XADD mystream MAXLEN 2 * temperature 22 humidity 60 executed three times in a row, how many entries will mystream contain afterwards?
medium
A. 2 entries
B. 0 entries
C. 1 entry
D. 3 entries

Solution

  1. Step 1: Understand MAXLEN usage

    The MAXLEN 2 option limits the stream length to 2 entries by trimming older entries when new ones are added.
  2. Step 2: Apply the command three times

    After the first two adds, the stream has 2 entries. The third add triggers trimming, keeping only the latest 2 entries.
  3. Final Answer:

    2 entries -> Option A
  4. Quick Check:

    MAXLEN 2 limits stream to 2 entries [OK]
Hint: MAXLEN limits entries; stream never exceeds that number [OK]
Common Mistakes:
  • Assuming all 3 entries remain without trimming
  • Confusing MAXLEN with deleting all entries
  • Thinking MAXLEN sets minimum length
4. What is wrong with this command? XADD mystream MAXLEN 5 name John age
medium
A. Stream name cannot be 'mystream'
B. MAXLEN must be after the fields
C. XADD does not support MAXLEN option
D. Missing value for field 'age'

Solution

  1. Step 1: Check field-value pairs

    Fields and values must come in pairs. Here, 'age' has no value following it.
  2. Step 2: Verify MAXLEN position and support

    MAXLEN is correctly placed after stream name and before fields. XADD supports MAXLEN.
  3. Final Answer:

    Missing value for field 'age' -> Option D
  4. Quick Check:

    Fields need values; 'age' missing one [OK]
Hint: Every field must have a value; check pairs carefully [OK]
Common Mistakes:
  • Forgetting to provide value for last field
  • Misplacing MAXLEN option
  • Thinking stream name is invalid
5. You want to add multiple entries to a Redis stream sensor_data but keep only the latest 100 entries to save memory. Which command correctly adds a new entry with fields temp and humidity and enforces this limit?
hard
A. XADD sensor_data * MAXLEN 100 temp 25 humidity 40
B. XADD sensor_data MAXLEN 100 * temp 25 humidity 40
C. XADD sensor_data MAXLEN ~ 100 * temp 25 humidity 40
D. XADD sensor_data * temp 25 humidity 40 MAXLEN 100

Solution

  1. Step 1: Understand MAXLEN usage and placement

    MAXLEN must come immediately after the stream name, optionally with ~ for approximate trimming. The * must come after MAXLEN to auto-generate ID.
  2. Step 2: Analyze options

    XADD sensor_data MAXLEN 100 * temp 25 humidity 40 correctly places MAXLEN 100 after stream name and before *, then fields. XADD sensor_data MAXLEN ~ 100 * temp 25 humidity 40 uses ~ (approximate) which is optional but not required. Options B and D have wrong order.
  3. Final Answer:

    XADD sensor_data MAXLEN 100 * temp 25 humidity 40 -> Option B
  4. Quick Check:

    MAXLEN 100 before * enforces limit [OK]
Hint: Put MAXLEN right after stream name, then * for ID [OK]
Common Mistakes:
  • Placing MAXLEN after * or fields
  • Using ~ when exact trimming is needed
  • Adding MAXLEN at the end of command
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