DEV Community

Cover image for πŸš€ Building a Real-Time Notification Service with FastAPI, Redis Streams, and WebSockets
Geetansh Garg
Geetansh Garg

Posted on

πŸš€ Building a Real-Time Notification Service with FastAPI, Redis Streams, and WebSockets

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 πŸ˜„

Top comments (0)