---
# System prepended metadata

title: Redis Email Implementation

---

# Title

## How You Can Choose Perfect Alternative for AWS SQS
![image](https://hackmd.io/_uploads/SkK0s3oYR.png)

## AWS SQS Free Tier Credits Got Depleted? Implement this Free Alternative
![image](https://hackmd.io/_uploads/SJEW22iK0.png)

## Have you ever signed up for a new service and then waited... and waited... for that confirmation email? 

In today's fast-paced world, prompt notifications are crucial for a positive user experience. This is especially true for our applications, where features like leave management and peer-to-peer feedback rely heavily on immediate updates.

At **[Hexmos](https://hexmos.com)**, we are building innovative products like **[Feedback By Hexmos](https://hexmos.com/feedback?ref=journal.hexmos.com)** and **[FeedZap](https://hexmos.com/feedzap?ref=journal.hexmos.com)**.

To keep our systems efficient and adaptable, we embraced a microservices architecture. This means we built separate, smaller services that work together seamlessly.

One of these services is a unified Identity Service, which manages user accounts and payments across all our products.

But behind the scenes, ensuring smooth communication between different parts of our system can be a challenge. 

We leverage a microservices architecture, which breaks down our application into smaller, independent services that work together seamlessly. 

However, coordinating communication between these services, especially for time-sensitive tasks like sending emails, requires a robust and reliable solution.

This article explores how we overcame the limitations of our initial approach and discovered a powerful solution with Redis Streams. 

We'll delve into our challenges, why traditional message queuing systems weren't the perfect fit, and how Redis Streams helped us build a resilient notification system that keeps users informed and engaged.

## Our Existing Microservices Architecture

### Building Scalable Systems with Microservices

Our current architecture involves two services:


1. **Leave Request API:**
    - Processes leave requests.
    - Creates a message containing relevant data.
    - Pushes the message to the message queue.
2. **Email API:**
    - Processes received messages by sending appropriate emails.


Direct communication between these services can cause problems:

- **Tight Coupling:** Changes in one service impact the other, hindering independent development.
- **Data Loss:** If the Email API is down during signup, the email notification might be lost. Customers might not receive verification emails or leave notifications.

```mermaid
graph LR
A(User) -->B( Leave Request API) --> C(Email API) --> D(Email Notification)
```

### The Identity Service: A Foundation for User Experience

**Why Two Services?**

Explaining the use of microservices in our architecture:

- **Backend dedicated to product:** Handles core functionalities of the product.
- **Backend dedicated to emails and notifications:** Manages all email and notification-related tasks.

This separation allows each service to be developed, deployed, and scaled independently, increasing the system's resilience and maintainability.
###  [AWS SQS](https://docs.aws.amazon.com/sqs/): A Step Towards Decoupling

We integrated AWS SQS as a connection between these services:

1. **Leave Request API:**
    
    - Processes leave requests.
    - Creates a message (e.g., JSON object) containing relevant data (user details, leave type, duration, etc.).
    - Pushes the message to the message queue.
2. **Message Queue (SQS) :**
    - Stores the message until it's processed.
    - Provides reliability and durability guarantees.
    - Can handle varying message rates, ensuring system scalability.
3. **Email API:**
    - Continuously polls the message queue for new messages.
    - Processes received messages by sending appropriate emails.

```mermaid
graph LR
A(User) -->B( Leave Request API) --> C(AWS SQS) -->D(Email API) --> F(Email Notification)
```

If the Consumer BE (Email API) is down, messages are queued and not lost. Once the Consumer BE is back online, it processes the queued messages and sends email notifications using data from SQS.

#### Benefits of Using a Message Queue

- **Decoupling:** Services become independent, improving maintainability and scalability.
- **Reliability:** Messages are persisted in the queue, ensuring delivery even if the Email API is temporarily unavailable.
- **Performance:** The Leave Request API can process requests faster without waiting for email delivery.
- **Scalability:** Each service can be scaled independently to handle increasing load.
- **Error Handling:** Implement retry mechanisms and dead-letter queues to handle failed message processing.
<div style="text-align: center;">
 <img src="https://hackmd.io/_uploads/BkGlARwY0.png" alt="Centered Image">
</div>


## AWS SQS Challenges and Finding Perfect Alternatives

As We integrated SQS to 7 different services we started depleting usage limits Due to Free Tier Limitations.

### AWS SQS Free Tier Limitations

While AWS SQS offers a convenient solution for message queuing, it presents some limitations for our specific needs:

**[Free Tier Limitations:](https://aws.amazon.com/sqs/pricing/#:~:text=All%20customers%20can%20make%201,within%20this%20Free%20Tier%20limit.&text=AWS%20Free%20Tier%20includes%201,Simple%20Queue%20Service%20(SQS).)** The free tier offered by SQS restricts the number of requests, hindering the scalability of our growing application.

![image](https://hackmd.io/_uploads/SJzgezYK0.png)

**Hidden Costs:** Exceeding the free tier results in significant cost increases, potentially impacting our budget.

![image](https://hackmd.io/_uploads/BkZCpZYYC.png)


### Finding a Temporary Fix for It

Amazon SQS offers [Short and Long polling](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-short-and-long-polling.html) options for receiving messages from a queue.

**Short polling (default)** – The ReceiveMessage request queries a subset of servers (based on a weighted random distribution) to find available messages and sends an immediate response, even if no messages are found.

**Long polling** – ReceiveMessage queries all servers for messages, sending a response once at least one message is available, up to the specified maximum. An empty response is sent only if the polling wait time expires. This option can reduce the number of empty responses and potentially lower costs.

For Temporary solutions, we choose Long polling. But this didn't solve Our Problem.

<div style="text-align: center;">
 <img src="https://hackmd.io/_uploads/ry-TTZFK0.png" alt="Centered Image">
</div>
<div style="text-align: center;">
 <img src="https://hackmd.io/_uploads/ByAt0bFtC.png" alt="Centered Image">
</div>

### Why [Pub/Sub](https://cloud.google.com/pubsub/docs/overview) Wasn't the Perfect Fit

We explored alternative message queuing solutions, including Pub/Sub. However, Pub/Sub wasn't a suitable choice due to specific requirements:

#### Pros of Pub/Sub

**Scalability:** Pub/Sub systems are designed to handle a large number of publishers and subscribers efficiently.

**Decoupling:** It promotes loose coupling between systems, as publishers and subscribers don't need to know about each other.

**Flexibility:** Pub/Sub can be used for various messaging patterns, including publish-subscribe, fan-out, and request-reply.

#### Cons of Pub/Sub

**Message Loss:** Pub/Sub systems typically don't guarantee message delivery, especially if subscribers are offline or unable to process messages.

**Message Ordering:** Message order is not guaranteed in most Pub/Sub systems, which can be a limitation for certain applications.

**Complexity:** Managing subscribers and handling message delivery can be complex, especially for large-scale systems.

**Latency:** While Pub/Sub is generally fast, it might not be suitable for applications with strict real-time requirements.

The search for a viable alternative led us to Redis Streams, a powerful data structure within the Redis database.

## [Redis ](https://redis.io/docs/latest/)Streams: A Game-Changer

While we leverage several open-source products like [Meilisearch](https://journal.hexmos.com/we-chose-meilisearch-over-10-other-search-engines-despite-a-major-drawback/), [Gitlab](https://journal.hexmos.com/gilab-integration-with-webhooks/), [Listmonk](https://journal.hexmos.com/avoid-logging-into-listmonk-for-campaign-previews-use-this-hack-instead/), and [Ghost](https://journal.hexmos.com/glee/) for our notification system, we require a robust and high-performance solution.
After careful consideration, Redis Streams emerged as the ideal choice for our specific needs.
### Redis Streams: A High-Performance Message Queue

<div style="text-align: center;">
  <img src="https://hackmd.io/_uploads/BkucTRwtR.png" alt="Centered Image">
</div>


According to its [G](https://github.com/antirez/redis)[it](https://github.com/antirez/redis)[H](https://github.com/antirez/redis)[ub repository](https://github.com/antirez/redis), Redis (**Re**mote **Di**rectory **S**erver) is an [in-memory](https://en.wikipedia.org/wiki/In-memory_database) data structure store. It is a disk-persistent key-value database with support for [multiple data structures or data types](https://redis.io/topics/data-types-intro).


This means that while Redis supports mapped key-value-based strings to store and retrieve data (analogous to the data model supported in traditional kinds of databases), it also supports other complex data structures like lists, sets, etc. As we proceed, we will look at the data structures supported by Redis. We will also get to learn about the unique features of Redis.

Redis is an open-source, highly [replicated](https://redis.io/topics/replication), [performant](https://redis.io/topics/benchmarks), non-relational kind of database and caching server. It works by mapping keys to values with a sort of predefined data model. Its benefits include:

- Mapped key-value-based caching system, almost comparable to [Memcached](https://memcached.org/)
- No strict rules pertaining to defining strict schemas or tables for data (schemaless)
- Support for multiple data models or types
- Offers more advanced features when compared to other kinds of database systems
- Ability to withstand multiple concurrent write requests or transactions per second, via a technique known as [sharding](https://en.wikipedia.org/wiki/Shard_(database_architecture))
- Can be used together with other databases as a support to reduce load and improve performance, but can also be used as a primary database. Note that this is usually based on individual needs and use cases
- Can come in handy in the areas of quick data ingestion with data integrity in the mix, where features like high efficiency and replication are paramount
- As it is open-source it is Free to use 

> **Note**: Redis has a variety of use cases in large enterprise applications. Apart from acting as a caching server, it can also act as a message broker or be used in publisher/subscriber kind of systems. For detailed information about other use cases, we can check [this section of the documentation](https://redislabs.com/redis-enterprise/use-cases).

<div style="text-align: center;">
  <img src="https://hackmd.io/_uploads/BkucTRwtR.png" alt="Centered Image">
</div>

**Performance:** Redis Streams are renowned for their high throughput and low latency, ensuring efficient message delivery for our real-time notifications.

**Flexibility:** Redis Streams provide various features like message IDs and stream groups, catering to our evolving needs and complex scenarios.

### Efficiently Managing Data Streams with Redis

Using Redis Streams within our microservices architecture streamlines message processing:

**Leave API triggers an event:** Upon the successful user Leaving the application, the Signup API publishes a message to a Redis Stream.

**Notification API listens and reacts:** The Notification API continuously listens for new messages on the stream.

**Real-time Processing:** New messages are processed immediately by the Notification API, triggering actions like sending an email welcome message.

This approach ensures real-time communication and timely notification delivery, enhancing the user experience.

<div style="text-align: center;">
  <img src="https://hackmd.io/_uploads/r1iN0RvtA.png" alt="Centered Image">
</div>

### Leveraging Redis Streams for Robust Message Handling

#### Publishing to a Stream

A publisher in Redis Streams is the agent responsible for adding new elements to the stream. In this role, the publisher is central to real-time data processing. By publishing data to a stream, other parts of your application (the consumers) can react to that data almost instantly.

To add new elements to the stream, we can use the XADD command. It appends an element consisting of key-value pairs to the specified stream.

If the stream doesn’t exist, Redis will create it automatically.

The command also generates a unique identifier for the element, which is based on the current timestamp and a sequence number by default.

Code snippet

| Redis Command | Key of the Stream | ID  | 1st Field     | 1st Value | 2nd Field | 2nd Value |
| ------------- | ----------------- | --- | ------------- | --------- | --------- | --------- |
| `XADD`        | `mystream`        | `*` | `temperature` | `22`      | `city`    | `Lisbon`  |

```bash
XADD mystream * temperature 22 city Lisbon
```

Breaking down this command, we have:

- **Key of the stream**: The name of the stream you want to add the element to.
- **ID**: The identifier for the element. You can use the special character * to let Redis automatically generate an ID based on the current timestamp and a sequence number.
- **Element in the Stream**: The key-value pairs that make up the element.

As a publisher, you can add elements to the stream continuously. For instance, in the context of a concert, the ticket scanner could be the publisher. As it scans each ticket, it could publish an element to the stream, including information like the ticket ID, seat number, and element time. Other parts of the system could consume these messages in real-time, for instance, to keep track of the occupied and available seats.

Here is an example of adding a new element to the ‘email’ stream:
```bash
XADD email * id 102
```
By entering this command, Redis will respond with the generated ID for the new element, which can be used later to read or reference the element in the stream.

"1678900012656-0"

The above element can then be consumed by a consumer or consumer group for further processing.

Publishers play a crucial role in Redis Streams, facilitating real-time data processing by pushing new elements to the stream. They are not constrained by the number of consumers; they can continue publishing irrespective of the number of consumers or the speed at which consumers are processing elements.

#### Consuming from a stream

In a real-time processing scenario, it’s not just about adding elements to the stream but also reading them, analyzing the data, and possibly taking some action based on that data. That’s where stream consumers come into play.

A consumer in Redis Streams reads the data from the stream and processes it. There can be multiple consumers reading from the same stream simultaneously, which allows for parallel processing of the data.

To read elements from a stream, we use the `XREAD` command.

| Redis Command | Number of Records to be Returned | Millis that the Client will be Blocked | Keys of the Streams to be Read | IDs from which the Elements will be Returned |
|---------------|----------------------------------|----------------------------------------|--------------------------------|---------------------------------------------|
| `XREAD`       | `COUNT 10`                       | `BLOCK 3000`                           | `STREAM tickets [..]`           | `0-0 [..]`                                   |

```bash
XREAD COUNT 10 BLOCK 3000 STREAM tickets [..] 0-0 [..]
```

Breaking down this command, we have:

- **COUNT count:** This is optional and is used to limit the number of messages returned by the command.
- **BLOCK milliseconds:** The `BLOCK` option in the `XREAD` command is optional and sets a timeout in milliseconds for how long the command should wait if no data is available. If the timeout expires and no data is received, the command returns an empty response.
- **STREAMS stream_name:** The name of the streams from which you want to read elements.
- **ID:** The ID from which to start reading elements. If you want to read all elements, you can use “0–0” as the ID.

This command will return all elements in the “tickets” stream. And the reason why we use 0–0 to read from the beginning of the stream is that the IDs are incremental by default. They are a combination of timestamps and sequence numbers `(1678900012656-0)`. 0–0 would be the lowest one.

But if you want to listen only to new messages, you can use the special $ ID with the BLOCK option. The $ ID will only return messages that have been added to the stream after the `XREAD` command was sent.

| Redis Command | Number of Records to be Returned | Millis that the Client will be Blocked | Keys of the Streams to be Read | IDs from which the Elements will be Returned |
| ------------- | -------------------------------- | -------------------------------------- | ------------------------------ | -------------------------------------------- |
| `XREAD`       | `COUNT 10`                       | `BLOCK 3000`                           | `STREAM tickets [..]`          | `$`                                          |

```bash
XREAD COUNT 10 BLOCK 3000 STREAM tickets [..] $
```

So, it is important to understand that you should use the $ ID only for the first call to `XREAD`. Later the ID should be the one from the last reported element in the stream. Otherwise, you could miss all the entries that are added in between.

#### Managing Message Lifecycle with Redis Streams 

Redis Streams, while powerful, can become a bottleneck if not managed correctly. Unprocessed messages can accumulate, leading to performance issues, consumer crashes, and potential data loss.

**The Role of XDEL**

The `XDEL` command is a fundamental tool for managing message lifecycle in Redis Streams. It removes messages from a stream based on their IDs. However, it’s crucial to use `XDEL` strategically to avoid unintended consequences.
The `XDEL` command is used to delete one or more messages from a stream using their IDs.

| Redis Command | Stream Name   | IDs of Messages to be Deleted      |
|---------------|---------------|------------------------------------|
| `XDEL`        | `stream_name` | `id1 [id2 id3 ...]`                |
```bash
XDEL stream_name id [id …]
```


For example, to delete the message with the ID ‘1685734200000-0 from the “email” stream, you would use:


```bash
XDEL email 1685734200000-0 
```

To Understand More about Redis Stream You can check this [Article](https://medium.com/redis-with-raphael-de-lio/understanding-redis-streams-33aa96ca7206)
## Conclusion

