Overview
CryptoPulse is built on a modern, scalable architecture designed to efficiently handle concurrent cryptocurrency price requests while minimizing external API calls through intelligent batching.Tech Stack
The system leverages proven technologies optimized for high-performance API services:NestJS + Fastify
Enterprise-grade Node.js framework with high-performance HTTP server
PostgreSQL + TypeORM
Reliable persistence layer for price history storage
Redis (ioredis)
Distributed coordination for batching and rate limiting
JWT Authentication
Secure token-based API access control
System Components
Application Layer
The application is initialized insrc/main.ts using NestJS with Fastify adapter:
src/main.ts
Authentication Layer
JWT-based authentication protects all price endpoints. TheJwtAuthGuard validates bearer tokens on every protected request.
Service Layer
ThePriceService orchestrates:
- Request batching coordination via Redis
- External API calls to CoinGecko
- Price persistence to PostgreSQL
- Result distribution to waiting clients
Data Layer
- PostgreSQL
- Redis
Stores historical price records with timestamps for trending and analysis.Schema:
PriceRecord entity tracks coinId, vsCurrency, price, and fetchedAtRequest Flow
The following diagram illustrates how a price request flows through the system:Flow Steps
Timer or threshold
Batch flushes when:
- Counter reaches
BATCH_THRESHOLD(default: 3), OR - Timer expires after
BATCH_WINDOW_MS(default: 5000ms)
Multi-Instance Architecture
CryptoPulse supports horizontal scaling through Redis-based coordination:
Key features:
- Shared Redis ensures only ONE batch flush occurs per coin, even across instances
- Rate limiting counters are synchronized
- Each instance maintains its own in-memory waiters but coordinates via pub/sub
- Load balancer (e.g., Nginx) distributes incoming requests
Configuration for Multi-Instance
Error Handling
The architecture includes comprehensive error handling:| Scenario | Response |
|---|---|
| Redis unavailable during batch admission | 503 Service Unavailable |
| Batch result timeout (> 8s) | 504 Gateway Timeout |
| CoinGecko API failure | 502 Bad Gateway |
| Rate limit exceeded | 429 Too Many Requests |
| Invalid JWT | 401 Unauthorized |
All errors are logged with structured JSON for monitoring and debugging.
Performance Characteristics
Batch efficiency
Batch efficiency
With a threshold of 3 and 5-second window, the system reduces external API calls by ~66% under concurrent load.
Response latency
Response latency
- First request in batch: 5s max (window timeout) + CoinGecko latency
- Subsequent requests: Near-instant once batch flushes
- Threshold-triggered flush: Minimal delay
Scalability
Scalability
Stateless API instances can scale horizontally. Bottlenecks are:
- Redis throughput (typically 100k+ ops/sec)
- PostgreSQL write capacity
- CoinGecko API rate limits
Next Steps
Request Batching
Deep dive into the batching algorithm
Authentication
Learn how JWT auth secures endpoints