Skip to content
ARTICLE // ARCHITECTURE

Architecting Event-Driven Mobile Backends with NestJS and Redis

How to structure WebSocket channels, pub/sub queues, and database locking for low-latency live tracking and order state machines.

February 15, 2025
6 min read
Architecture, NestJS, Redis, WebSockets

Architecting Event-Driven Mobile Backends

When building real-time mobile applications — such as on-demand service dispatchers, live transit monitors, or multi-role commerce apps — request/response HTTP cycles are insufficient for maintaining consistent UI state across active clients.

The Dual-Layer Architecture

A resilient architecture decouples state mutation from broadcast fan-out:

Client Action -> API Gateway (NestJS) -> Database Transaction (PostgreSQL)
                                      \-> Event Publish (Redis Pub/Sub)
                                            \-> WebSocket Cluster -> Active Clients

1. Transactional Integrity First

Never broadcast an event before the underlying database transaction commits. If the client receives a DISPATCH_ASSIGNED socket message before the database writes the assignment record, any subsequent API query from the client will race against the uncommitted state.

2. Idempotent Redis Event Channels

Structure Redis channel naming by domain boundary: service:order:<order_id>:events service:geo:<technician_id>:telemetry

Each message payload should contain a monotonic version counter to let mobile clients discard out-of-order packets during spotty mobile connectivity.

Resilient Mobile Socket Reconnection

On the React Native / Expo client side, maintain an offline optimistic state queue. When reconnecting after cellular dropouts:

  • Send the last acknowledged event timestamp
  • Server replays missed delta events
  • Client applies reconciliation before unfreezing UI interactions
React Native
Next.js
TypeScript
Node.js
NestJS
PostgreSQL
Prisma
Redis
Docker
GraphQL
Tailwind CSS
MongoDB
Express
AWS
Socket.io
Expo
System Design
REST APIs
Git
React Native
Next.js
TypeScript
Node.js
NestJS
PostgreSQL
Prisma
Redis
Docker
GraphQL
Tailwind CSS
MongoDB
Express
AWS
Socket.io
Expo
System Design
REST APIs
Git