GeneriCon 2023Join us in Denver from June 7 – 9 to see what’s coming next.

Register now

Atul Lal

Scalable notification system microservice

Cover Image for Scalable notification system microservice
Atul Lal
Atul Lal

Designing a scalable notification system is crucial for applications that serve a large number of users. Microservices architecture has gained popularity for its ability to break down complex applications into smaller, more manageable components. In this article, we'll explore a scalable notification system microservice built using NestJS, Redis pub/sub model, and authenticated websockets. We'll discuss how this solution provides horizontal scalability and dive into key components such as the RedisPropagator, Socket states, and gateways.

Redis Pub/Sub Model

A critical aspect of a scalable notification system is the ability to handle numerous concurrent connections. Redis, an in-memory data structure store, provides a publish/subscribe (pub/sub) messaging pattern that enables efficient communication between different services or components. In this NestJS implementation, the Redis pub/sub model serves as the backbone for the real-time notification system, allowing it to manage a high volume of notifications while maintaining low latency.

Authenticated Websockets

To ensure secure communication between the server and clients, the NestJS notification system employs authenticated websockets. By implementing authentication, the system can verify the identity of connected clients and control access to notifications. In this implementation, the websocket authentication process is built on top of the WebSocket Gateway and involves exchanging JSON Web Tokens (JWTs) to establish a secure connection.

Horizontal Scalability

One of the main benefits of this notification system is its horizontal scalability. As the number of users increases, the system can simply add more instances to handle the growing demand. This is achieved by decoupling the notification logic from the rest of the application, which allows the system to scale independently. The use of Redis pub/sub and stateless websockets further enhances the system's ability to scale, as it can distribute the load across multiple instances.

RedisPropagator

The RedisPropagator is a crucial component in this implementation, responsible for managing the communication between the websocket instances and the Redis pub/sub system. It handles the distribution of messages across the various instances and ensures that all connected clients receive their notifications in a timely manner.

Socket States and Gateways

Socket states are essential for managing the lifecycle of websocket connections in the system. The NestJS implementation employs socket states to track the connection status of clients and determine when they are online or offline. By monitoring socket states, the system can effectively route notifications to connected clients.

The WebSocket Gateway is another vital component that manages the incoming websocket connections and their authentication. It serves as an entry point for clients connecting to the notification system, ensuring that only authenticated users gain access to notifications.

Conclusion

The NestJS-based scalable notification system microservice described in this article leverages the Redis pub/sub model, authenticated websockets, and various key components such as RedisPropagator, Socket states, and gateways to create a robust and horizontally scalable solution. By utilizing these technologies and patterns, the system can efficiently manage a large number of concurrent connections and deliver real-time notifications to users securely.

Image of Atul Lal

About Atul Lal

I am a software engineer with a passion for creating innovative and impactful applications that solve real-world problems. At Commvault Systems, I optimized APIs, developed distributed systems, and automated cloud environments for over two years.