Beginner

AI Microservices Architecture

Learn design patterns for AI microservices including service boundaries, communication protocols, data flow, and deployment topologies.

Defining Service Boundaries

The most important architectural decision is where to draw service boundaries. For AI systems, consider these dimensions:

  • Model boundary: Each ML model or model family becomes its own service (recommendation service, NLP service, vision service).
  • Pipeline stage: Separate preprocessing, inference, and postprocessing into distinct services.
  • Data domain: Group services around business domains (user service, product service, content service).
  • Compute requirement: Separate GPU-intensive services from CPU-only services for efficient resource allocation.

Communication Patterns

PatternProtocolUse CaseLatency
SynchronousgRPC, RESTReal-time predictionsLow (ms-s)
AsynchronousKafka, RabbitMQBatch inference, trainingHigher (s-min)
Event-drivenKafka, PulsarFeature updates, model triggersVariable
Request-replygRPC streamingLLM token streamingStreaming
# gRPC service definition for model serving
syntax = "proto3";

service ModelService {
  // Synchronous prediction
  rpc Predict(PredictRequest) returns (PredictResponse);

  // Streaming prediction for LLMs
  rpc StreamPredict(PredictRequest) returns (stream PredictChunk);

  // Health check
  rpc HealthCheck(Empty) returns (HealthStatus);
}

message PredictRequest {
  string model_id = 1;
  bytes input_data = 2;
  map<string, string> parameters = 3;
}

message PredictResponse {
  bytes output_data = 1;
  float latency_ms = 2;
  TokenUsage usage = 3;
}
Use gRPC for internal communication between AI microservices. It offers 2-10x better performance than REST due to binary serialization (Protocol Buffers), HTTP/2 multiplexing, and bidirectional streaming. Reserve REST for external-facing APIs.

Design Patterns for AI Microservices

Model Gateway

A single entry point that routes requests to the appropriate model service based on model ID, handles authentication, and applies rate limits.

Ensemble Pattern

Combine predictions from multiple models in parallel. An aggregator service collects results and applies voting or weighted averaging.

Pipeline Pattern

Chain services sequentially: preprocessing, feature extraction, inference, postprocessing. Each stage is an independent service.

Sidecar Pattern

Attach helper containers alongside model servers for logging, monitoring, model loading, and configuration management.

Kubernetes Deployment

Kubernetes is the standard platform for orchestrating AI microservices:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sentiment-model
spec:
  replicas: 3
  selector:
    matchLabels:
      app: sentiment-model
  template:
    metadata:
      labels:
        app: sentiment-model
    spec:
      containers:
      - name: model-server
        image: ml-registry/sentiment:v2.1
        resources:
          limits:
            nvidia.com/gpu: 1
            memory: "8Gi"
          requests:
            memory: "4Gi"
        ports:
        - containerPort: 8080
        readinessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 30
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
          periodSeconds: 10
GPU scheduling is tricky: GPUs cannot be shared across pods by default. Use NVIDIA MPS (Multi-Process Service) or time-slicing for better GPU utilization. Consider GPU node pools with taints and tolerations to ensure only GPU workloads land on expensive GPU nodes.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.