cloudchat-v2 is the production-ready successor to cloudchat-lite. It implements a modern messaging backend, inspired by WhatsApp-scale design principles.
This repository focuses exclusively on the backend, with a placeholder for a future React client.
- โจ Why this exists
- ๐ Core Capabilities
- ๐ Tech Stack
- ๐งฑ Architecture Overview
- โก Real-Time Messaging Architecture
- ๐ WebSocket Presence & Connection Lifecycle
- ๐ฆ Quick Start (Optional)
- ๐งช Testing
- โก Real-Time Messaging (Live Demo)
- ๐ท๏ธ License
cloudchat-lite (v1) was a solid first iteration with a clean single-table DynamoDB design.
As the project evolved and access patterns were examined more deeply, two schema issues emerged that would limit correctness and scalability in a real messaging workload.
This repository (cloudchat-v2) exists to correct those specific issues while preserving the original design philosophy.
-
Message ordering v1 relied on timestamp-based sort keys, which break under concurrent writes. v2 uses ULID-based ordering for guaranteed uniqueness and correct time sequencing.
-
Inbox ordering v1โs GSI design prevented server-side sorting and pagination. v2 introduces a time-ordered inbox GSI.
v2 preserves the original single-table design and access philosophy โ it is a targeted schema correction, not a rewrite.
๐ For the full breakdown, see
docs/schema-evolution.md
While cloudchat-lite (v1) was implemented in Node.js, cloudchat-v2 was intentionally rebuilt in Python from the ground up.
Rather than reusing existing code, the v2 backend was rewritten entirely to:
- Practice production-grade async Python
- Explore Pythonโs ecosystem for serverless and data modeling
- Validate that the original design principles translate cleanly across languages
Only the architecture and access patterns were carried forward โ all implementation details were rethought and reimplemented.
- ULID-ordered message writes (collision-proof)
- Atomic metadata updates for all participants
- Correct inbox ordering guaranteed
- GSI sorted by
<LastTimestamp>#<conversation_id> - Server-side ordering and pagination
- Stable under concurrent message writes
- Query by conversation ID
- ULID-ordered messages
- Efficient pagination
- Online fanout via WebSockets
- Presence modeled as TTL-based soft state
- Decoupled from message persistence
| Technology | Usage |
|---|---|
| Python 3.13+ | Core application language |
| AWS Lambda | Serverless compute runtime |
| AWS API Gateway | HTTP + WebSocket entry point |
| WebSockets (APIGW) | Real-time message delivery |
| DynamoDB (single table) | Messages, metadata, and presence (system of record) |
| DynamoDB Local | Local validation of single-table design and read receipts POC |
| DynamoDB Streams | Change data capture for real-time fanout |
| SNS | Decoupled event distribution for message delivery |
| aioboto3 | Async DynamoDB client |
| Pydantic v2 | Data validation and serialization |
| pytest | Testing framework |
| UV | Dependency and environment management |
| AWS SAM | Infrastructure-as-code, build, and deployment |
| IAM | Least-privilege access control between services |
Client (future React)
|
API Gateway (HTTP)
|
Lambda Handlers
|
DynamoDB (Single Table)
โโ Message rows
โโ Per-user conversation metadata for n-way chats
โณ Inbox GSI (sorted by recency)
- Fully async Python handlers
- DynamoDB schema designed for high write throughput
Implemented two real-time delivery pipelines to demonstrate scalable, decoupled fanout patterns.
-
DynamoDB Streams trigger a Lambda on new messages
-
Lambda:
- Hydrates message from DynamoDB
- Resolves online recipients via Presence table
- Sends directly over WebSocket
-
Simple, low latency, tightly coupled
-
DDB Stream Lambda publishes
MessageSentevents to SNS -
SNS consumer Lambda:
- Hydrates message from DynamoDB
- Resolves online recipients via Presence table
- Fans out to active WebSocket connections
Benefits
- Independent scaling
- Failure isolation and retries
- Extensible to analytics, notifications, moderation
- Clean separation of persistence vs delivery
- Stream processing never blocks on fanout
- Proven production chat pattern
โโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ Client โ โ โ API / Lambda โ โ โ DDB (Messages) โ โ โ DDB Stream Lambda โ โ โ SNS โ
โ โ โ(send message)โ โ โ โ (publish event) โ โ MessageSentTopic โ
โโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ SNS Consumer Lambda โ โ โ WebSocket API โ โ โ Online Clients โ
โ - hydrate message from DDB โ โ โ โ โ
โ - lookup presence โ โโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ - fanout to connections โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโ
โ Client โ
โโโโโโฌโโโโโ
โ connect / heartbeat
v
โโโโโโโโโโโโโโโโโโโโ
โ WebSocket API โ
โโโโโโโโโโโฌโโโโโโโโโ
โ
โ refresh ttl
v
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ DDB (Presence / TTL) โ
โ ttl = now + interval โ
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Fanout / Send Logic โ
โโโโโโโโโโโโฌโโโโโโโโโโโโโโ
โ
โ check ttl > now
v
โโโโโโโโโโโโโโโโโโโโโ
โ WebSocket API โ
โโโโโโโโโโโฌโโโโโโโโโโ
โ
โ 410 Gone
v
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Connection Cleanup โ
โ (defensive / future hook) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
IAM permissions are tightly scoped so Lambdas can only be invoked by the specific WebSocket API routes.
WebSockets are used for delivery only; DynamoDB TTL-based presence is the source of truth.
cd backend
uv sync
sam build
sam deploy --guidedThis provisions:
- Lambda functions
- DynamoDB message table (single-table design)
- DynamoDB presence table (TTL-based)
- Inbox GSI (sorted by recency)
- API Gateway (HTTP + WebSocket)
- IAM roles and permissions
๐ AWS credentials and SAM CLI required.
The backend includes a focused test suite covering core correctness and edge cases:
-
Unit tests
- DynamoDB key construction and schema models
- ULID ordering and timestamp extraction
- Message fanout recipient parsing
-
Integration tests
- TTL-based presence behavior
Tests are written using pytest with strict async validation.
cd backend
uv run pytest -q tests
......................... [100%]
25 passed in 6.60sManual AWS test scripts were used to validate live DynamoDB access patterns, Lambda execution, inbox ordering, and real-time fanout behavior against deployed infrastructure.
These scripts live under:
backend/aws_tests/
The system supports live WebSocket message delivery to online users, driven by DynamoDB Streams and SNS-based fanout.
Messages are delivered asynchronously; ordering is preserved at the data layer and resolved client-side.
MIT โ free for personal and professional use.