Bird
Raised Fist0
Redisquery~20 mins

Stream entry IDs in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stream ID Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this XADD command?
Consider the Redis command:
XADD mystream * sensor-id 1234 temperature 19.8
What does the command return?
Redis
XADD mystream * sensor-id 1234 temperature 19.8
AAn error message about missing stream key
BThe string "OK"
CThe number of fields added, e.g., 2
DA generated stream entry ID in the format <millisecondsTime>-<sequenceNumber>, e.g., "1687000000000-0"
Attempts:
2 left
💡 Hint
XADD returns the ID of the added entry, which is a timestamp plus a sequence number.
🧠 Conceptual
intermediate
2:00remaining
What does the stream entry ID '1687000000000-5' represent?
In Redis streams, what do the two parts of the entry ID '1687000000000-5' mean?
AThe first part is a Unix timestamp in milliseconds; the second part is a sequence number for entries with the same timestamp
BThe first part is the stream length; the second part is the entry's position
CThe first part is the server's process ID; the second part is the entry's priority
DThe first part is the entry's hash; the second part is a random number
Attempts:
2 left
💡 Hint
Think about how Redis ensures unique IDs for entries added at the same millisecond.
📝 Syntax
advanced
2:00remaining
Which XADD command syntax is correct to add an entry with a custom ID?
You want to add an entry to 'mystream' with ID '1687000000000-0' and fields 'name'='sensor1' and 'value'='42'. Which command is correct?
AXADD mystream 1687000000000-0 name sensor1 value 42
BXADD mystream * 1687000000000-0 name sensor1 value 42
CXADD mystream name sensor1 value 42 1687000000000-0
DXADD mystream ID 1687000000000-0 name sensor1 value 42
Attempts:
2 left
💡 Hint
The ID comes immediately after the stream key in XADD syntax.
❓ optimization
advanced
2:00remaining
How to efficiently trim a Redis stream to keep only the last 1000 entries?
Which command efficiently trims the stream 'mystream' to keep only the latest 1000 entries?
AXTRIM mystream MAXLEN 1000
BXTRIM mystream MAXLEN ~ 1000
CXTRIM mystream MINLEN 1000
DXTRIM mystream COUNT 1000
Attempts:
2 left
💡 Hint
Use approximate trimming for better performance.
🔧 Debug
expert
2:00remaining
Why does this XREAD command return no entries?
You run:
XREAD COUNT 2 STREAMS mystream 1687000000000-5
But get no results, even though the stream has entries. Why?
ABecause the stream key 'mystream' does not exist
BBecause the COUNT parameter must be after STREAMS keyword
CBecause XREAD returns entries with IDs strictly greater than the given ID, so if no newer entries exist, it returns nothing
DBecause the ID '1687000000000-5' is invalid format
Attempts:
2 left
💡 Hint
XREAD returns entries with IDs greater than the one specified, not equal or less.

Practice

(1/5)
1. What does a Redis stream entry ID consist of?
easy
A. Only a timestamp
B. A timestamp and a sequence number separated by a dash
C. Only a sequence number
D. A random string generated by Redis

Solution

  1. Step 1: Understand Redis stream entry ID format

    Redis stream entry IDs are made of two parts: a timestamp and a sequence number.
  2. Step 2: Identify the separator

    These two parts are separated by a dash (-), for example, 1526985058136-0.
  3. Final Answer:

    A timestamp and a sequence number separated by a dash -> Option B
  4. Quick Check:

    Stream entry ID = timestamp-sequence [OK]
Hint: Remember: ID = timestamp + dash + sequence number [OK]
Common Mistakes:
  • Thinking ID is only timestamp
  • Confusing sequence number as random string
  • Ignoring the dash separator
2. Which of the following is the correct way to add a new entry with an auto-generated ID to a Redis stream named mystream?
easy
A. XADD mystream # field1 value1
B. XADD mystream 1234567890-0 field1 value1
C. XADD mystream * field1 value1
D. XADD mystream $ field1 value1

Solution

  1. Step 1: Recall how to auto-generate IDs in Redis streams

    Using '*' as the ID in the XADD command tells Redis to generate the ID automatically.
  2. Step 2: Check the options for correct syntax

    XADD mystream * field1 value1 uses '*', which is the correct way to auto-generate the ID.
  3. Final Answer:

    XADD mystream * field1 value1 -> Option C
  4. Quick Check:

    Use '*' to auto-generate stream entry ID [OK]
Hint: Use '*' in XADD to auto-generate IDs [OK]
Common Mistakes:
  • Using a fixed ID instead of '*'
  • Using '#' or '$' which are invalid here
  • Forgetting to include field-value pairs
3. Given the following Redis stream entries:
1526985058136-0: field1=value1
1526985058136-1: field1=value2
What will be the ID of the next entry if you add it with XADD mystream * field1 value3 immediately after?
medium
A. 1526985058136-2
B. 1526985058137-0
C. 1526985058136-0
D. 1526985058135-0

Solution

  1. Step 1: Understand sequence number increment for same timestamp

    When multiple entries share the same timestamp, Redis increments the sequence number.
  2. Step 2: Identify the next sequence number

    Existing entries have sequence numbers 0 and 1 for timestamp 1526985058136, so the next will be 2.
  3. Final Answer:

    1526985058136-2 -> Option A
  4. Quick Check:

    Next sequence number increments by 1 for same timestamp [OK]
Hint: Next ID increments sequence if timestamp same [OK]
Common Mistakes:
  • Assuming timestamp always increments
  • Repeating an existing ID
  • Using a lower timestamp than existing
4. You run the command XADD mystream 1526985058136-1 field1 value1 but get an error. What is the most likely cause?
medium
A. The ID 1526985058136-1 already exists in the stream
B. The timestamp part is not a valid number
C. The field name is missing
D. The sequence number is negative

Solution

  1. Step 1: Understand Redis stream ID uniqueness

    Each stream entry ID must be unique; duplicate IDs cause errors.
  2. Step 2: Check the given ID

    If 1526985058136-1 already exists, adding it again causes an error.
  3. Final Answer:

    The ID 1526985058136-1 already exists in the stream -> Option A
  4. Quick Check:

    Duplicate stream entry IDs cause errors [OK]
Hint: Duplicate IDs cause errors in XADD [OK]
Common Mistakes:
  • Assuming timestamp format error
  • Forgetting field-value pairs
  • Thinking negative sequence numbers are allowed
5. You want to add multiple entries to a Redis stream with IDs that must be strictly increasing, but your system clock is not reliable and may go backward. Which approach ensures correct stream entry IDs?
hard
A. Use fixed sequence numbers with zero timestamps
B. Manually specify IDs with timestamps from your system clock
C. Generate random IDs and add them to the stream
D. Use XADD mystream * to let Redis generate IDs automatically

Solution

  1. Step 1: Understand the problem with unreliable clocks

    Manually specifying timestamps can cause IDs to go backward if the clock is wrong.
  2. Step 2: Use Redis auto-generated IDs

    Using '*' lets Redis generate strictly increasing IDs based on its own clock and sequence logic.
  3. Final Answer:

    Use XADD mystream * to let Redis generate IDs automatically -> Option D
  4. Quick Check:

    Auto-generated IDs ensure increasing order despite clock issues [OK]
Hint: Let Redis generate IDs with '*' for reliable ordering [OK]
Common Mistakes:
  • Trusting unreliable system clock for IDs
  • Using fixed or random IDs causing duplicates
  • Ignoring Redis auto-generation feature
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