🚀 Building a Real-Time Notification Service with FastAPI, Redis Streams, and WebSockets
Geetansh Garg

Geetansh Garg @geetnsh2k1

About: Hi, I'm Geetansh Garg, a Senior Software Engineer passionate about system design, scalability, and clean code.

Location:
Gurugram, Haryana
Joined:
May 8, 2024

🚀 Building a Real-Time Notification Service with FastAPI, Redis Streams, and WebSockets

Publish Date: Jun 20
2 0

Hi DEV Community! 👋

I'm Geetansh Garg, a Senior Software Engineer passionate about scalable backend systems, clean architecture, and real-world problem solving. This is my first post here, and I wanted to kick things off by sharing one of the most exciting problems I’ve been working on lately.


🧩 The Problem

We needed to deliver real-time notifications to users in a FastAPI-based product. But it wasn’t just about sending messages — we had to handle:

  • Scalable delivery (thousands of users)
  • Persistence (so no message is lost)
  • Unread tracking
  • Internationalization (i18n)
  • Prioritization & retries

The traditional Redis Pub/Sub model just didn’t cut it — messages would be lost if a user was offline. So we decided to re-architect using Redis Streams for reliable and replayable messaging.


🛠️ The Stack

  • FastAPI: Async Python framework for APIs and WebSockets
  • Redis Streams: Durable, ordered messaging system
  • PostgreSQL: To store notification history and unread status
  • WebSockets: For pushing updates in real-time to the user
  • Pydantic & SQLAlchemy: For models and data validation
  • Docker + AWS Lambda (future): For production-ready deployment

🔄 How It Works

  1. A notification is created (user action, cron job, webhook, etc.)
  2. It's pushed into a Redis Stream with metadata like user_id, priority, lang, etc.
  3. A Redis Stream consumer reads it, stores it in PostgreSQL, and pushes to the WebSocket server.
  4. If the user is connected — great, they get it instantly!
  5. If not, it’s stored, marked as unread, and delivered when they come back online.

✨ Key Highlights

  • Message durability with Redis Streams
  • 🔄 Replay logic in consumer to handle restarts
  • 🌍 Multi-language templates per user
  • 🔒 Secure WebSocket authentication using JWT and x-api-key
  • 📬 Push fallback logic coming soon (Slack/Email integration)

📈 What's Next?

I’m looking to open-source this soon and write detailed breakdowns of:

  • Redis Streams vs Pub/Sub for real-time systems
  • Notification retry and backoff strategies
  • WebSocket gateway with reconnect handling

💬 Let’s Connect!

Have you built similar real-time systems? Or explored Redis Streams for production workloads?

Let’s connect, geek out, and share learnings!

Also, I’m open to feedback — especially since this is my first post 😄

Comments 0 total

    Add comment