``Hi All,
I have come across an interesting issue where I have two messages being written into the stream at the same millsecond.
Where the XREAD command in the consumer service is able to read the first message but not the second one. For example, if two messages are sent at the same millisecond from a publishing service, the Redis XADD command in the publishing service attaches:
1st Message: EPOCH Time - 0
2nd Message: EPOCH Time - 1
On the other hand. the XREAD command on the consuming service is currently only able to read the first message (EPOCH Time - 0), while the second message is being ignored and not being retrieved even if I set the max count of messages to be received as 1:
However, I was able to solve this issue once I switched to the consumer group approach, by creating consumer groups and then using the XREADGROUP command along with XACK to ack the received messages
My Publishing snippet:
await redis.xadd(
streamName,
"MAXLEN",
"~",
"1000",
"*",
"message",
JSON.stringify(message)
);
My consuming service snippet:
`
let lastReadId=0;
const messages = await redis.xread(
"COUNT",
1,
"BLOCK",
0,
"STREAMS",
streamName,
lastReadId
);
Note: I am using id returned from the last time the `XREAD` was executed.
const msgs = messages![0][1].map((message) => {
const id = message[0];
const rawMsg = message[1][1];
lastReadId = id; -> here I am storing the latest id
return { id: id, rawMsg: rawMsg };
});
return msgs;`
NOTE: I have also tried changing the COUNT from 1 to 2 or many messages at a time.
and, I was using a for of loop to do some business operation with the messages retrieved from stream as such and deleting them:
`for (const msg of msgs) {
const {id, rawMsg} = msg;
someBusinessOperation();
if(businessOperation === "Completed"){
redis.xdel(streamName, id);
}
`
NOTE: I am using ioredis npm library.
I assume that no matter what approach we follow, the reading behaviour should be same for both XREAD and XREADGROUP.
Please correct me if I am wrong and advise me if there is an actual issue with redis XREAD command?
Thank You!
Comment From: sundb
@Kripu77 Do you use multiple clients to consume a stream group or the same stream at the same time?
Comment From: Kripu77
@sundb "I have a single consumer that consumes messages from the stream group."
Comment From: sundb
I can't fingure out what the problem is, in such case there is no different between XREAD and XREADGROUP.
Can you use MONITOR to get all the received commands?