diff --git a/content/Projects/GC/queues/BullMQ.md b/content/Projects/GC/queues/BullMQ.md new file mode 100644 index 0000000..776d516 --- /dev/null +++ b/content/Projects/GC/queues/BullMQ.md @@ -0,0 +1,10 @@ +## Introduction + +### Queue + +A queue is a fundamental data structure that follows the First-In-First-Out (FIFO) principle, meaning elements are added at the rear and removed from the front. In computing, queues are widely used for managing asynchronous operations, task scheduling, and buffering data between processes. The primary operations in a queue are enqueue (adding elements) and dequeue (removing elements), both typically having O(1) time complexity. Queues play a crucial role in message brokers, event processing systems, and job scheduling frameworks. + +### BullMQ + +BullMQ is a modern, Redis-based queue system designed specifically for Node.js applications. It excels at handling distributed job processing with features like job prioritization, delayed jobs, job retries, and rate limiting. BullMQ supports multiple producers and consumers, making it ideal for microservices architectures. Unlike simple in-memory queues, BullMQ provides persistence, ensuring jobs aren't lost even if the application crashes. Its event-driven architecture allows developers to track job progress, handle failures gracefully, and implement complex workflow patterns. BullMQ's robust concurrency model helps prevent race conditions and ensures reliable job processing in high-throughput scenarios. + diff --git a/content/Projects/GC/queues/OTP Flow.md b/content/Projects/GC/queues/OTP Flow.md new file mode 100644 index 0000000..c639a5d --- /dev/null +++ b/content/Projects/GC/queues/OTP Flow.md @@ -0,0 +1,61 @@ + +This diagram illustrates the architecture and flow of an OTP (One-Time Password) generation and verification system. The system ensures secure email-based OTP delivery and verification, leveraging Redis for temporary storage and a Bull queue for job processing. + +![[otp flow.png]] + +## Components and Flow + +### 1. **User Interaction** + +- **Generate OTP:** + - The user initiates the OTP generation process by hitting the `otp/generate` endpoint (a POST request). +- **Verify OTP:** + - Once the user receives the OTP via email, they enter it and call the `otp/verify` endpoint (a POST request) for verification. + +### 2. **Generate OTP Endpoint** + +- When the user calls the `otp/generate` endpoint: + 1. A request is sent to the **Bull Queue Producer**, which is responsible for queuing the task. + 2. The Bull Queue Producer adds the job to the **OTP Jobs Queue**. + 3. The expected body is + ```json + { + email : "example@gamil.com" + } + ``` + +### 3. **OTP Jobs Queue** + +- The **OTP Jobs Queue** acts as a middleware between job creation and processing. It: + 1. Receives queued tasks from the Bull Queue Producer. + 2. Passes the tasks to the **Processor** for execution. + +### 4. **Processor** + +- The **Processor** is a critical component responsible for: + 1. Generating the OTP. + 2. Temporarily storing the OTP in Redis with a **Time-to-Live (TTL)** value with 10 minute expiry. + 3. Forwarding the OTP and email details to the **Mailer**. + +### 5. **Mailer** + +- The **Mailer** calls an external email service to send the generated OTP to the user’s submitted email address. Once the email is sent: + 1. The mailer service uses an `otp.hbs` file for the mail template located in mailer folder. + 2. The OTP is stored in Redis (TTL ensures automatic deletion after expiry). + 4. A success acknowledgment is returned. + +### 6. **Verify OTP Endpoint** + +- The `otp/verify` endpoint: + 1. Retrieves the user-submitted OTP. + 2. Checks the OTP stored in Redis. + 3. If the OTP matches and is valid (not expired), it returns a positive response (e.g., `OTP is correct`). + 4. If the OTP does not match or is expired, it returns an error response (e.g., `OTP is incorrect`). + 5. Expected payload in body + ```json + { + email : "test@gmail.com", + otp : "456123" + } +``` + diff --git a/content/otp flow.png b/content/otp flow.png new file mode 100644 index 0000000..92aa203 Binary files /dev/null and b/content/otp flow.png differ