← Back

How to Implement Mass Notifications in 1:n Model Using AWS

Introduction

I recently had the opportunity to work on a project that required me to architect a mass-notification service for hundreds of thousands of users. The users were part of a social app for sharing book reviews, bookshelves, and everything related to books.

The main challenge was to create a way to send thousands of notifications per second to users subscribed to a favorite author, publisher, or any other object they could subscribe to.

I decided to use AWS SQS, Lambda, and PostgreSQL to keep users' device tokens. In the previous article "Enhancing User Engagement with Push Notifications in Mobile App", I showed you how to create 1:1 notifications. The 1:n approach extends this by adding a middleware Lambda that takes the main event (e.g., an author publishes a new book) and spreads events to all subscribers of the author. The subscribers list is kept in a PostgreSQL table.

Process Overview

The process looks similar to this:

       +------------------+
       |    User Action   |  (e.g., an author publishes a new book)
       +--------+---------+
                |
                v
       +--------+---------+
       | Middleware Lambda|  (Handles the main event)
       +--------+---------+
                |
                v
       +--------+---------+
       | PostgreSQL DB    |  (Fetch subscribers based on the event)
       +--------+---------+
                |
                v
       +--------+---------+
       |    SQS Queue     |  (Queue to manage notification tasks)
       +--------+---------+
                |
                v                
       +--------+---------+
       |    Lambda 2      |  (Processes notifications for each subscriber)
       +--------+---------+
                |
                v
       +--------+---------+
       |   Send Notification 
       +--------+---------+
    

Process Explanation

  1. User Action: An event, such as an author publishing a new book, triggers the process.
  2. Middleware Lambda: This Lambda function is invoked first. It handles the main event and queries the PostgreSQL database to fetch the list of subscribers.
  3. PostgreSQL DB: A query is made to the PostgreSQL database to retrieve the list of subscribers who are interested in the event.
  4. SQS Queue: The middleware Lambda sends a message to an SQS queue for each subscriber.
  5. Lambda 2: This Lambda function processes each message from the SQS queue, representing an individual subscriber.
  6. Send Notification: For each subscriber, a push notification is sent to the user's device.

Conclusion

This setup ensures that the workload is managed efficiently using SQS, and the database operations are handled within the initial middleware Lambda function, enabling scalable and efficient mass notifications.