Skip to content

Commit c818f1b

Browse files
author
Vasile Gorcinschi
committed
feat: Implemented POST, GET and DELETE
1 parent dc8bf69 commit c818f1b

4 files changed

Lines changed: 367 additions & 39 deletions

File tree

solutions/message-queue-elasticache/README.md

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ The application provides a single API route (`/api/messages`) with three HTTP me
108108

109109
## Testing
110110

111-
**Important**: Consumer groups track which messages have been delivered. Once a message is consumed (via GET), it moves to the Pending Entries List (PEL) and won't appear in subsequent GET requests until acknowledged. Always complete the full flow: POST → GET → DELETE.
111+
**Important Notes:**
112+
113+
- Consumer groups track which messages have been delivered. Once a message is consumed (via GET), it moves to the Pending Entries List (PEL) and won't appear in subsequent GET requests until acknowledged.
114+
- The `streamMessageId` returned by GET is Valkey's unique stream entry ID and **must be used** for the DELETE operation.
115+
- Always complete the full flow: POST → GET → DELETE.
112116

113117
### Complete Message Flow Example
114118

@@ -117,14 +121,18 @@ The application provides a single API route (`/api/messages`) with three HTTP me
117121
```bash
118122
curl -X POST http://localhost:3000/api/messages \
119123
-H "Content-Type: application/json" \
120-
-d '{"name": "John Doe", "email": "john@example.com", "message": "Hello!"}'
124+
-d '{
125+
"name": "Test User",
126+
"email": "test@example.com",
127+
"message": "Hello from local dev!"
128+
}'
121129
```
122130

123131
Response:
124132

125133
```json
126134
{
127-
"messageId": "fa2382a5-24bf-4070-8a48-1d525e457307",
135+
"streamMessageId": "1764009314892-0",
128136
"timestamp": "2024-11-24T18:35:14.890Z"
129137
}
130138
```
@@ -140,12 +148,12 @@ Response with message:
140148
```json
141149
{
142150
"message": {
143-
"streamMessageId": "1732471514890-0",
144-
"id": "fa2382a5-24bf-4070-8a48-1d525e457307",
145-
"name": "John Doe",
146-
"email": "john@example.com",
147-
"message": "Hello!",
148-
"timestamp": "2024-11-24T18:35:14.890Z"
151+
"streamMessageId": "1764009314892-0",
152+
"name": "Test User",
153+
"email": "test@example.com",
154+
"message": "Hello from local dev!",
155+
"timestamp": "2024-11-24T18:35:14.890Z",
156+
"claimed": true
149157
}
150158
}
151159
```
@@ -156,12 +164,14 @@ Response when queue is empty:
156164
{ "message": null }
157165
```
158166

167+
**Note**: The `claimed` field indicates whether this message was recovered from the Pending Entries List (a previously delivered but unacknowledged message). Messages idle for more than 60 seconds are automatically reclaimed.
168+
159169
**3. Acknowledge Message (Mark as Processed)**
160170

161-
Use the `streamMessageId` from step 2:
171+
**Critical**: Use the `streamMessageId` from step 2 for the DELETE operation:
162172

163173
```bash
164-
curl -X DELETE "http://localhost:3000/api/messages?messageId=1732471514890-0"
174+
curl -X DELETE "http://localhost:3000/api/messages?messageId=1764009314892-0"
165175
```
166176

167177
Response:
@@ -172,14 +182,32 @@ Response:
172182

173183
### Troubleshooting
174184

175-
If you GET a message but don't DELETE (acknowledge) it, the message stays in the Pending Entries List. Subsequent GET requests will return `{"message":null}` because the consumer group only delivers new, undelivered messages. To reset for testing:
185+
**Message Recovery**: If you GET a message but don't DELETE (acknowledge) it, the message stays in the Pending Entries List. After 60 seconds of idle time, subsequent GET requests will **automatically reclaim** that message (indicated by `"claimed": true` in the response). This is a reliability feature that handles consumer failures.
186+
187+
**For clean testing**, if you want to reset and start fresh:
176188

177189
```bash
178190
# Access your Valkey instance
179191
docker exec -it <container_id> valkey-cli
180192

181-
# Delete the consumer group to reset
193+
# Delete the entire stream (removes consumer group too)
194+
DEL contact-messages
195+
196+
# Or just delete the consumer group
182197
XGROUP DESTROY contact-messages contact-processors
183198

184199
# The consumer group will be recreated automatically on next GET
185200
```
201+
202+
**Checking Pending Messages**: To see what's currently in the Pending Entries List:
203+
204+
```bash
205+
# Access Valkey
206+
docker exec -it <container_id> valkey-cli
207+
208+
# View pending messages
209+
XPENDING contact-messages contact-processors
210+
211+
# View all messages in the stream
212+
XRANGE contact-messages - +
213+
```

0 commit comments

Comments
 (0)