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
| Pattern | Protocol | Use Case | Latency |
|---|---|---|---|
| Synchronous | gRPC, REST | Real-time predictions | Low (ms-s) |
| Asynchronous | Kafka, RabbitMQ | Batch inference, training | Higher (s-min) |
| Event-driven | Kafka, Pulsar | Feature updates, model triggers | Variable |
| Request-reply | gRPC streaming | LLM token streaming | Streaming |
# 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;
}
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
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX