<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sabuirahul on Medium]]></title>
        <description><![CDATA[Stories by Sabuirahul on Medium]]></description>
        <link>https://medium.com/@sabuirahul90?source=rss-97b05788311------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Cb_22suVwrsNn9jC</url>
            <title>Stories by Sabuirahul on Medium</title>
            <link>https://medium.com/@sabuirahul90?source=rss-97b05788311------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 08:00:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sabuirahul90/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Designing a Scalable Real-Time Chat System with Django, WebSockets, PostgreSQL, Redis & React…]]></title>
            <link>https://medium.com/@sabuirahul90/designing-a-scalable-real-time-chat-system-with-django-websockets-postgresql-redis-react-671d2d26539e?source=rss-97b05788311------2</link>
            <guid isPermaLink="false">https://medium.com/p/671d2d26539e</guid>
            <category><![CDATA[websocket]]></category>
            <category><![CDATA[database-design]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[celery-django]]></category>
            <category><![CDATA[django-rest-framework]]></category>
            <dc:creator><![CDATA[Sabuirahul]]></dc:creator>
            <pubDate>Thu, 23 Jul 2026 08:32:02 GMT</pubDate>
            <atom:updated>2026-07-23T08:32:02.085Z</atom:updated>
            <content:encoded><![CDATA[<p>Designing a Scalable Real-Time Chat System with Django, WebSockets, PostgreSQL, Redis &amp; React Native</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7_aUW4gtfiTUCL6hpcLeNQ.png" /></figure><p>Over the past few weeks, I’ve been designing a production-style real-time messaging architecture to better understand how applications like <strong>WhatsApp, Telegram, Discord, Messenger, and Slack</strong> work behind the scenes.</p><p>Rather than jumping directly into coding, I wanted to answer an important question first:</p><blockquote><strong><em>“How should a modern chat application be architected if it needs to support thousands (or even millions) of users?”</em></strong></blockquote><p>Instead of treating it as another CRUD application, I approached it like a distributed system.</p><h3>Why This Project?</h3><p>Many tutorials show how to send a message over WebSockets.</p><p>Very few explain:</p><ul><li>Why REST APIs and WebSockets both exist</li><li>Why Redis is required</li><li>Why PostgreSQL is still important</li><li>How typing indicators work</li><li>How read receipts work</li><li>How online status is tracked</li><li>How push notifications are delivered</li><li>How multiple servers communicate</li></ul><p>I wanted to understand all of these pieces together.</p><h3>Tech Stack</h3><h3>Mobile Client</h3><ul><li>React Native</li></ul><h3>Backend</h3><ul><li>Django</li><li>Django REST Framework</li><li>Django Channels</li></ul><h3>Database</h3><ul><li>PostgreSQL</li></ul><h3>Messaging Layer</h3><ul><li>Redis</li></ul><h3>Background Processing</h3><ul><li>Celery</li></ul><h3>File Storage</h3><ul><li>Amazon S3 / Cloud Storage</li></ul><h3>Push Notifications</h3><ul><li>Firebase Cloud Messaging (Android)</li><li>Apple Push Notification Service (iOS)</li></ul><p>This stack is widely used in production because each component solves a different problem.</p><h3>High Level Architecture</h3><p>The application is divided into multiple layers.</p><h3>1. Client</h3><p>The React Native application handles:</p><ul><li>Login</li><li>Registration</li><li>User profiles</li><li>Conversations</li><li>Sending messages</li><li>Media uploads</li><li>Typing indicators</li><li>Online presence</li><li>Read receipts</li><li>Push notifications</li></ul><p>The client communicates with the backend in two different ways.</p><h3>REST APIs</h3><p>REST APIs are responsible for operations that don’t require instant updates.</p><p>Examples include:</p><ul><li>Authentication</li><li>Fetching conversations</li><li>Loading chat history</li><li>Uploading media</li><li>Fetching user profiles</li></ul><p>These requests are stateless and travel over HTTPS.</p><h3>WebSockets</h3><p>WebSockets handle everything that must happen instantly.</p><p>For example:</p><ul><li>Sending messages</li><li>Receiving messages</li><li>Typing indicators</li><li>Online/offline status</li><li>Read receipts</li><li>Delivered status</li></ul><p>Instead of opening a new HTTP connection for every request, WebSockets maintain one persistent connection.</p><p>That is why chat applications feel instantaneous.</p><h3>Django REST Framework</h3><p>The REST layer is responsible for:</p><p>✅ Authentication</p><p>✅ User APIs</p><p>✅ Conversation APIs</p><p>✅ Message history</p><p>✅ Media upload</p><p>This layer communicates directly with PostgreSQL.</p><h3>Django Channels</h3><p>Django Channels enables asynchronous communication through WebSockets.</p><p>It manages:</p><ul><li>Connection authentication</li><li>WebSocket consumers</li><li>Chat rooms</li><li>Presence</li><li>Broadcasting events</li><li>Typing indicators</li></ul><p>Without Channels, Django works only with HTTP requests.</p><p>Channels extends Django into a real-time framework.</p><h3>PostgreSQL</h3><p>Many developers assume WebSockets eliminate the need for a database.</p><p>That’s not true.</p><p>Every message is still stored permanently inside PostgreSQL.</p><p>The database stores:</p><ul><li>Users</li><li>Conversations</li><li>Conversation members</li><li>Messages</li><li>Read status</li><li>Delivery status</li></ul><p>The database becomes the source of truth.</p><p>If Redis restarts tomorrow, no chat history is lost because PostgreSQL contains everything.</p><h3>Why Redis?</h3><p>Redis is one of the most important components in a real-time architecture.</p><p>Imagine two backend servers.</p><p>User A connects to Server 1.</p><p>User B connects to Server 2.</p><p>If User A sends a message, how does Server 2 know about it?</p><p>This is where Redis Pub/Sub becomes essential.</p><p>The flow looks like this:</p><p>Server 1</p><p>↓</p><p>Publishes event</p><p>↓</p><p>Redis Channel</p><p>↓</p><p>Server 2 receives event</p><p>↓</p><p>Server 2 broadcasts to connected users</p><p>Redis acts as the communication layer between all backend servers.</p><p>It is also useful for:</p><ul><li>Presence</li><li>Caching</li><li>Temporary session data</li><li>Fast lookups</li></ul><h3>Why Celery?</h3><p>Some operations should never block the user.</p><p>Examples:</p><ul><li>Sending push notifications</li><li>Compressing images</li><li>Video processing</li><li>Generating thumbnails</li><li>Background cleanup</li><li>Scheduled jobs</li></ul><p>Instead of making users wait, Django sends these jobs to Celery workers.</p><p>Celery processes them asynchronously.</p><p>This keeps the application responsive.</p><h3>Database Design</h3><p>I kept the schema intentionally simple.</p><h3>User</h3><p>Stores:</p><ul><li>Name</li><li>Email</li><li>Avatar</li><li>Last seen</li><li>Online status</li></ul><h3>Conversation</h3><p>Represents either:</p><ul><li>Private chat</li><li>Group chat</li></ul><p>Each conversation contains metadata like:</p><ul><li>Creator</li><li>Created time</li><li>Updated time</li></ul><h3>ConversationMember</h3><p>This table maps users to conversations.</p><p>It stores:</p><ul><li>User</li><li>Conversation</li><li>Role</li><li>Joined date</li></ul><p>This design allows unlimited participants in a conversation.</p><h3>Message</h3><p>Every message contains:</p><ul><li>Sender</li><li>Conversation</li><li>Message type</li><li>Text</li><li>Media URL</li><li>Reply information</li><li>Edited status</li><li>Deleted status</li><li>Timestamp</li></ul><p>Message types include:</p><ul><li>Text</li><li>Image</li><li>Video</li><li>Audio</li><li>File</li><li>Contact</li><li>Location</li></ul><h3>MessageStatus</h3><p>Instead of storing read information inside the message table, a separate table tracks:</p><ul><li>Sent</li><li>Delivered</li><li>Read</li></ul><p>for every user.</p><p>This makes group chat read receipts much easier to implement.</p><h3>WebSocket Event Design</h3><p>Rather than sending random JSON objects, every message follows an event-driven structure.</p><p>Client events include:</p><ul><li>join_chat</li><li>leave_chat</li><li>send_message</li><li>typing</li><li>stop_typing</li><li>read_message</li></ul><p>Server events include:</p><ul><li>new_message</li><li>message_delivered</li><li>message_read</li><li>online</li><li>offline</li></ul><p>Using structured events makes the frontend and backend easier to maintain.</p><h3>Real-Time Message Flow</h3><p>Let’s walk through a typical message.</p><h3>Step 1</h3><p>User A sends a message.</p><h3>Step 2</h3><p>The WebSocket Consumer receives it.</p><h3>Step 3</h3><p>The message is saved in PostgreSQL.</p><h3>Step 4</h3><p>An event is published to Redis.</p><h3>Step 5</h3><p>Redis broadcasts the event.</p><h3>Step 6</h3><p>Every connected server receives the event.</p><h3>Step 7</h3><p>The message is delivered to User B instantly.</p><h3>Step 8</h3><p>When User B opens the chat, a read receipt is sent.</p><h3>Step 9</h3><p>User A immediately sees:</p><p>✔ Delivered</p><p>✔ Read</p><p>This entire process usually takes only milliseconds.</p><h3>Authentication Flow</h3><p>Authentication happens only once.</p><p>The user logs in using REST APIs.</p><p>The server returns:</p><ul><li>Access Token</li><li>Refresh Token</li></ul><p>Later, the WebSocket connection also uses the JWT token.</p><p>This means REST APIs and WebSockets share the same authentication mechanism.</p><h3>Media Upload</h3><p>Media is handled differently from text.</p><p>Instead of sending large files through WebSockets:</p><ol><li>Client uploads file using REST API</li><li>Backend stores it in S3</li><li>S3 returns a file URL</li><li>Client sends only the URL over WebSocket</li></ol><p>This significantly reduces bandwidth and keeps WebSocket traffic lightweight.</p><h3>Why Separate REST and WebSockets?</h3><p>Many beginners ask why everything isn’t sent through WebSockets.</p><p>The answer is simple.</p><p>REST is excellent for:</p><ul><li>CRUD operations</li><li>Authentication</li><li>Pagination</li><li>File uploads</li><li>Historical data</li></ul><p>WebSockets are excellent for:</p><ul><li>Live events</li><li>Notifications</li><li>Presence</li><li>Typing</li><li>Real-time messaging</li></ul><p>Using both together provides the best architecture.</p><h3>Challenges I Encountered</h3><p>Designing this system made me think beyond writing APIs.</p><p>Some challenges included:</p><ul><li>Preventing duplicate conversations</li><li>Managing online presence</li><li>Scaling WebSocket connections</li><li>Redis synchronization</li><li>Message ordering</li><li>Read receipts</li><li>Delivery acknowledgements</li><li>Background workers</li><li>Database indexing</li><li>Handling reconnections</li></ul><p>Each problem required a different solution.</p><p>That’s what made this project enjoyable.</p><h3>Key Takeaways</h3><p>Building a chat application isn’t just about sending messages.</p><p>It’s about designing systems that remain fast, reliable, and scalable as the number of users grows.</p><p>This project reinforced several important concepts:</p><ul><li>Choose the right tool for the right job — REST APIs for request/response workflows, WebSockets for real-time communication, Redis for event distribution, PostgreSQL for durable storage, and Celery for asynchronous processing.</li><li>Design with scalability in mind from the beginning by separating responsibilities across components.</li><li>Keep the database as the source of truth while using Redis for speed and coordination.</li><li>Think beyond code and focus on architecture, reliability, and user experience.</li></ul><p>Every large-scale application is essentially a collection of well-designed components working together. Understanding how those components interact is just as important as knowing how to implement them.</p><p>This architecture is part of my ongoing journey to deepen my knowledge of backend engineering, distributed systems, and scalable software design. I’m continuing to explore topics like microservices, event-driven systems, caching strategies, high-throughput APIs, and cloud-native deployment.</p><p>I’d love to hear how others approach real-time messaging systems or any suggestions for improving this architecture.</p><p><strong>#Django #Python #DjangoChannels #Redis #PostgreSQL #ReactNative #WebSockets #SystemDesign #BackendDevelopment #SoftwareArchitecture #Celery #AWS #FullStackDevelopment #ScalableSystems #TechLearning</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=671d2d26539e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>