Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:(sks): Add queue between kafka and lambda #1067

Closed
wants to merge 11 commits into from
Closed

Conversation

benjaminpaige
Copy link
Collaborator

@benjaminpaige benjaminpaige commented Jan 27, 2025

Before After
Email processing pipeline lacks delay queue between Kafka and Lambda functions. Introduced an SQS delay queue to improve fault tolerance and added monitoring for delays and errors.

🔧 Changes

  • Added an SQS delay queue between Kafka and Lambda for improved message handling.
  • Updated processEmails.ts to handle both Kafka and SQS events:
    • Forward Kafka events to the delay queue.
    • Process delayed SQS messages for email generation and sending.
  • Enhanced error handling in processEmails.ts:
    • Implemented error handling for failed SQS SendMessageCommand.
    • Added debug logging for easier troubleshooting.
  • Updated email.ts stack:
    • Configured a new delay queue with a 2-minute delivery delay and required IAM permissions.
    • Added CloudWatch alarms for Lambda errors, throttling, and DLQ message count.
    • Improved alarm configurations and added SNS notifications for critical events.
  • Documented key functions and stack components with JSDoc comments for clarity.

🚀 How to Test

  1. Deploy the updated stack:
    • Ensure the environment variables (e.g., DELAY_QUEUE_URL) are set correctly in the Lambda configurations.
  2. Test Kafka event processing:
    • Publish Kafka messages and verify that they are forwarded to the SQS delay queue.
  3. Test SQS message processing:
    • Check if delayed messages from the queue are processed by the Lambda and trigger the email sending flow.
  4. Simulate failure scenarios:
    • Introduce errors in the processing pipeline to validate error handling, DLQ behavior, and alarm triggers.
  5. Review CloudWatch logs and alarms:
    • Confirm that appropriate metrics are being tracked and alarms are configured as expected.

📄 Additional Notes

  • The processEmails.ts handler now logs detailed debug information for both Kafka and SQS events, aiding in identifying potential issues.
  • New alarms monitor Lambda execution errors, throttling, and DLQ activity, ensuring visibility into critical operational metrics.
  • This update is backward-compatible with the existing email processing logic.

Copy link
Contributor

github-actions bot commented Jan 27, 2025

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 0% 0 / 4515
🔵 Statements 0% 0 / 4752
🔵 Functions 0% 0 / 1370
🔵 Branches 0% 0 / 2639
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
lib/lambda/delayedEmailProcessor.ts 0% 0% 0% 0% 28-299
lib/lambda/kafkaToSqs.ts 0% 0% 0% 0% 5-34
Generated in workflow #1733 for commit aaa8fcb by the Vitest Coverage Report Action

Copy link
Collaborator

@asharonbaltazar asharonbaltazar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work. What wizardry 🧙🏻‍♂️

Comment on lines +59 to +65
const kafkaRecord = JSON.parse(sqsRecord.body) as KafkaRecord;

const { key, value } = kafkaRecord; // sanity check
if (!key || !value) {
console.log("No key/value present. Possibly a tombstone or invalid data.");
continue;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can make a guard function that narrows the sqsRecord.body to a KafkaRecord

const isKafkaRecord = (parsedBody: unknown): parsedBody is KafkaRecord => /* logic pertaining to kafkaRecord, i.e. typeof parsedBody === 'object' && parsedBody?.value */ 

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that's a nit

@benjaminpaige benjaminpaige deleted the fix-emails branch January 29, 2025 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants