Development
Popular article

NestJS Microservices: Medical Platform Architecture

Wapiki Team
January 20, 2026
9 min read
NestJSMicroservicesPostgreSQLMongoDBRedis

Why Microservices for Keneya?

A medical platform must be scalable, resilient and modular. Microservices allow us to:

  • Independent deployment of each service
  • Targeted scaling based on load
  • Different technologies per service
  • Fault isolation
  • Global Architecture

    Keneya consists of 7 microservices:

    Main Services

  • **Auth Service**: JWT authentication, refresh tokens
  • **User Service**: Patient and doctor management
  • **Consultation Service**: Teleconsultations and appointments
  • **Medical Records Service**: Medical records
  • **Payment Service**: Orange Money, Moov Money
  • **Notification Service**: SMS, emails, push
  • **WebRTC Signaling Service**: Video signaling
  • Inter-Service Communication

    1. REST API via API Gateway

    typescript
    // api-gateway/src/main.ts
    import { NestFactory } from '@nestjs/core'
    import { AppModule } from './app.module'
    
    async function bootstrap() {
      const app = await NestFactory.create(AppModule)
    
      // Proxy to microservices
      app.setGlobalPrefix('api/v1')
    
      await app.listen(3000)
    }

    2. Event-driven with Redis Pub/Sub

    typescript
    // consultation.service.ts
    async completeConsultation(consultationId: string) {
      const consultation = await this.consultationRepo.findById(consultationId)
      consultation.status = 'completed'
      await consultation.save()
    
      // Publish event
      await this.redisClient.publish('consultation.completed', {
        consultationId,
        patientId: consultation.patientId,
        doctorId: consultation.doctorId
      })
    }
    
    // medical-records.service.ts - Subscriber
    this.redisClient.subscribe('consultation.completed')
    this.redisClient.on('message', async (channel, message) => {
      if (channel === 'consultation.completed') {
        const data = JSON.parse(message)
        // Update medical record automatically
        await this.updateMedicalRecord(data)
      }
    })

    3. Real-time with Socket.io

    typescript
    // notification.gateway.ts
    @WebSocketGateway({ namespace: 'notifications' })
    export class NotificationGateway {
      @WebSocketServer() server: Server
    
      async sendToUser(userId: string, notification: any) {
        this.server.to(`user:${userId}`).emit('notification', notification)
      }
    }

    Data Strategy

    PostgreSQL for Critical Data

  • Users, doctors
  • Consultations, appointments
  • Financial transactions
  • MongoDB for Documents

  • Medical records (flexible)
  • Consultation history
  • Scanned documents (references)
  • Redis for Cache

  • User sessions
  • Rate limiting
  • Frequent query cache
  • Deployment with Docker Compose

    yaml
    version: '3.8'
    services:
      auth-service:
        build: ./services/auth
        environment:
          - DATABASE_URL=postgresql://...
          - REDIS_URL=redis://redis:6379
        depends_on:
          - postgres
          - redis
    
      consultation-service:
        build: ./services/consultation
        environment:
          - DATABASE_URL=postgresql://...
          - MONGO_URL=mongodb://mongo:27017
        depends_on:
          - postgres
          - mongo

    Results

  • 🚀 **Deployments**: 20+ per week
  • ⚡ **Availability**: 99.8%
  • 📈 **Scaling**: Auto-scaling on consultation-service
  • 🔧 **Maintenance**: Isolated services, zero downtime
  • Conclusion

    Microservices add complexity, but for a platform like Keneya, the benefits in scalability and resilience are well worth the investment.


    *Need help architecting your platform? [Contact us](/contact).*

    Did you like this article?

    Share it with your network!