Temporal
- Anand Nerurkar
- 2 days ago
- 5 min read
1. What is Temporal?
Temporal is a workflow orchestration engine for microservices.It allows you to write reliable, durable, and fault-tolerant workflows that can run across multiple services.
Key points:
Durable Workflows: Temporal keeps the state of long-running workflows, so even if a service crashes, the workflow continues where it left off.
Retry & Timeout Management: Automatically retries failed tasks according to rules you define.
Language Support: Java, Go, Python, TypeScript (via SDKs).
Event-driven & Microservices Friendly: Can coordinate multiple services asynchronously.
Terminology:
Term | Meaning |
Workflow | The business process you want to orchestrate (e.g., loan approval). |
Activity | A task or step in the workflow (can be a microservice call). |
Worker | Executes activities and workflows. |
Temporal Server | The backend that manages workflow states, retries, and task queues. |
2. How Temporal is Used in Microservices
In a microservices architecture:
Each service focuses on its own business logic.
Temporal coordinates end-to-end workflows, handling retries, failures, and state.
Services don’t need to worry about orchestration; Temporal acts as the central orchestrator.
Benefits:
No need for custom saga or workflow implementation.
Supports long-running transactions across services.
Handles failure recovery automatically.
Integrates with existing message brokers or databases if needed.
3. End-to-End Example: Loan Approval in a Digital Lending Platform
Scenario: A customer applies for a loan. The workflow involves multiple microservices:
KYC Service – Verify customer identity.
Credit Score Service – Check credit score.
Loan Evaluation Service – Decide loan eligibility.
Disbursement Service – Approve and transfer funds if eligible.
Step 1: Define the Workflow
Workflow coordinates all microservices:
Workflow LoanApprovalWorkflow(customerId):
try:
kycResult = KYCService.verify(customerId) # Activity 1
if kycResult == "FAILED":
return "KYC Failed"
creditScore = CreditService.getCreditScore(customerId) # Activity 2
if creditScore < 700:
return "Loan Rejected"
loanDecision = LoanEvaluationService.evaluate(customerId, creditScore) # Activity 3
if loanDecision == "APPROVED":
DisbursementService.disburse(customerId) # Activity 4
return loanDecision
catch Exception e:
retry according to policy
Notes:
Each Activity is implemented by a separate microservice.
Temporal handles retries if any service is temporarily unavailable.
Workflow state is persisted in Temporal Server; even if the worker crashes, the workflow resumes.
Step 2: Microservices as Activities
KYC Service: Receives customer info → calls Fenergo API → returns KYC result.
Credit Service: Calls credit bureau → returns score.
Loan Evaluation Service: Business rules → returns “APPROVED” or “REJECTED”.
Disbursement Service: Calls banking API to transfer funds.
Each service registers activities with Temporal and listens to tasks.
Step 3: Temporal Execution Flow (Text)
Customer applies for a loan → API Gateway receives request → Temporal Workflow is triggered:
1. Temporal schedules KYCService.verify(customerId)
- KYCService worker picks the task → performs verification → returns result
2. Temporal schedules CreditService.getCreditScore(customerId)
- CreditService worker executes → returns score
3. Temporal schedules LoanEvaluationService.evaluate(customerId, creditScore)
- Evaluates rules → returns "APPROVED" or "REJECTED"
4. If approved, Temporal schedules DisbursementService.disburse(customerId)
- Funds transferred → workflow completes
Key Features Handled by Temporal:
Retries: If KYC Service fails temporarily, Temporal retries automatically.
State Persistence: Workflow state stored in Temporal DB.
Timeout: Each activity can have a timeout; failure handled automatically.
Long-Running Support: Workflow can wait days for external approvals.
Step 4: Benefits in Microservices Context
Decouples microservices from orchestration logic.
Ensures reliability across distributed services.
Reduces boilerplate code for retries, sagas, or event handling.
Provides auditable workflow logs for compliance.
✅ Summary:
Temporal = Orchestrator for Microservices Workflows
Microservices remain independent.
Temporal handles orchestration, retries, persistence, and failures.
Ideal for long-running, multi-step business processes like loan approvals, KYC/CDD checks, insurance claims, or order fulfillment.
1. Overview
Goal: Customer applies for a loan → Workflow coordinates multiple microservices → Loan approved or rejected → Funds disbursed.
Microservices:
KYC Service – Verify customer identity.
Credit Service – Fetch credit score.
Loan Evaluation Service – Determine loan eligibility.
Disbursement Service – Transfer funds.
Notification Service – Send emails/SMS for status updates.
Orchestration: Temporal workflow coordinates all steps.
Messaging / Event Bus: Kafka (optional DAPR Pub/Sub abstraction).
2. Flow Step by Step (Text)
1. Customer submits loan application via Frontend UI
↓
2. API Gateway receives request
- Generates a unique requestId / correlationId
- Sends request to LoanApplicationService (entry point microservice)
- Initiates Temporal Workflow: LoanApprovalWorkflow(requestId, customerId)
3. Temporal Workflow Execution
LoanApprovalWorkflow(requestId, customerId):
Step 1: KYC Verification
- Temporal schedules activity: KYCService.verify(customerId)
- KYCService worker executes:
- Fetches customer info
- Calls Fenergo API
- Stores result in Redis / Temporal DB
- Returns "PASSED" or "FAILED"
- Temporal handles retries if service fails
- Workflow continues only if KYC PASSED, else returns "Rejected: KYC Failed"
Step 2: Credit Score Check
- Temporal schedules activity: CreditService.getCreditScore(customerId)
- CreditService worker executes:
- Calls credit bureau API
- Returns credit score
- Workflow evaluates score:
- If score < threshold → Reject loan
- Else → Proceed
Step 3: Loan Evaluation
- Temporal schedules activity: LoanEvaluationService.evaluate(customerId, creditScore)
- Evaluates business rules, eligibility
- Returns "APPROVED" or "REJECTED"
- Workflow continues if approved
Step 4: Fund Disbursement
- Temporal schedules activity: DisbursementService.disburse(customerId)
- Calls banking API to transfer funds
- Updates loan status table
4. Event-Driven / Messaging Layer (Optional)
- Each microservice can emit events for asynchronous processing:
- "loan-kyc-completed" → consumed by CreditService
- "loan-credit-completed" → consumed by LoanEvaluationService
- "loan-approved" → consumed by DisbursementService
- Kafka topics are used for pub/sub
- Optional: DAPR Pub/Sub can abstract Kafka, so services just publish/subscribe without broker-specific code
5. Observability and State Management
- Temporal DB stores workflow states:
- requestId, customerId, workflow step, timestamps, retries
- Redis can cache intermediate states for fast access
- Observability:
- Prometheus + Grafana for metrics
- OpenTelemetry for distributed tracing
6. Notification Flow
- NotificationService subscribes to workflow completion events:
- Loan Approved → send SMS / email
- Loan Rejected → send rejection reason
- Temporal can schedule activities for notifications directly or via event bus
7. Key Advantages of this Setup
Fault-Tolerant: Temporal retries failed activities automatically.
Decoupled Services: Microservices only implement business logic; orchestration is handled by Temporal.
Long-Running Workflows: Temporal can wait for external approvals or manual review for days.
Event-Driven Integration: Kafka or DAPR allows async communication and further scalability.
Traceable & Auditable: Every workflow step is logged and can be queried by correlationId.
8. Optional DAPR Integration
DAPR can replace direct Kafka calls:
Services use DAPR Pub/Sub APIs → decouples from Kafka implementation
State management can also use DAPR for Redis/CosmosDB abstraction
Can still use Temporal for orchestration; DAPR only handles messaging/state
Text flow with DAPR:
KYCService → DAPR Pub/Sub → CreditService → DAPR Pub/Sub → LoanEvaluationService → Temporal Workflow → DisbursementService
✅ Summary of End-to-End Flow (Text Version)
Customer applies → API Gateway → LoanApplicationService triggers Temporal workflow
Temporal schedules KYC → Credit → Loan Evaluation → Disbursement
Kafka/DAPR handles async events if needed
Temporal persists workflow states; handles retries, failures, and timeouts
NotificationService informs customer
Observability via Prometheus/Grafana/OpenTelemetry
Optional DAPR simplifies pub/sub and state abstraction
Comments